From patchwork Tue Mar 26 22:50:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 13605271 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB45913F010 for ; Tue, 26 Mar 2024 22:51:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711493471; cv=none; b=Tc4rhOLFfv/uaL456U3Qz2eV5o9M7k5fZXTngJxG2wi7q/dIn/dpy6SGG8w5NjcR35HTz4npYj2x3B8BjnBa95LrEHFNPFuqY0kzDmA84DkyAYiWAuFSYeKbhsNzK0ro+blMBQYhdFqiLMNQd6/g7YhIekD0BJ1ugKuPc5f5UAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711493471; c=relaxed/simple; bh=fyoG6SJAMUgsWcl/fUChSkVlsU1fny97Ckd1JSlEmug=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bLKHyNlGM2DFAACg6WQVxfllNv6hBxSV7W8PqVn2GqRKMvr9tgbblYVGBJCpz7rz8ExWzZRawQUiRXq/0TeTj4/nB3WtV5nq73wbIO+P/2PDxegXmOIP5qAxO+mlF6P6WIjG/IVnicxHHMkGz6yP9cbSuCf1CiqBaeQHsmeVDBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yeOolNZm; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yeOolNZm" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc64f63d768so10080503276.2 for ; Tue, 26 Mar 2024 15:51:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711493466; x=1712098266; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ucJ+Yb7mPnPv2YiFHbfkvluKQ3UkYd4IiLcbNLjHrm8=; b=yeOolNZmgdxLgcLeWm2ZZIwEBKJqrao9wBDoEoLode0t/tLtjj7zGOudmUUfLHqZtN 13CIQx/tA9W6HNh0O55FyTLG63wq+ZzYuNDfB+EB/DOJbHQKlczH0MI8wh+shY2dIQEr jk/7wHQuO9VdHtDE2rKjPg3Zx5Mlza/TFuz9zlPYUuiSpIgfrF8lIpt0ZA7R3tmMWPSI HyRDckcCyMcxA9G49pnwvbQINjShGx4E6+/vH5xHeuUDguQHYFo8yQ4ZUXKuBGEFlkS+ XdUpCVfS9jcscT/yNK7jYnDWmRmA/n6wkzE6ZdRlx4wI0cWyReG4rSFhTRALarNvBBlM TJmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711493466; x=1712098266; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ucJ+Yb7mPnPv2YiFHbfkvluKQ3UkYd4IiLcbNLjHrm8=; b=jJfk/8Vu4kGU3zr2bfhng9e+FPlbIY8XNAWgBD6+ryxvjkpVsjlzzvPN77MFG0dny5 QQyFsmk0cZ73XwTEg+waJS2fn5X8MfYb3HljVZ+gjYqA5/i8ERCq908RFYYFZit0aTf3 iM/LO8qBLBMLFVNKyEvDK/SMgtOdG/tbz72HSBLvGboxjlV69YfSdK88VT+WX/AByXsi ehqeaIQ64EoR7EajfKopEjorA0dufxINT7SeGFGjFU5mZkhvdyo4Aqxgc69Iyzb9SD2P AV5Tgc7LH8QvwX1jCxkRni7b7RTLS/h5BPV6xz4Arvommj4ttuTyWyaMV6lkpaM+P0V1 1GPA== X-Forwarded-Encrypted: i=1; AJvYcCWqC1bzQxDg/KQ7Or31kJddi73L25Ydbf0fQC9yxr0X0ijYgpYXs54jE0yIFDd6UH5G1odpT4NRv7N+se4zQUcxMnaQkotq1IAS5bMO X-Gm-Message-State: AOJu0YxzhZY4Gz9zxnabFRaBI8jEBX6ZFDEbUmE7srj4yLGnj2EVpA6C /r5ySWhHsf7iZXwnEpM30Q4ailIgz7D/ZZIHedKLw3tyDmjgb1gONOYJ2kmmdTFJAuVBR7glB9q pcDHD0HgkC1QjpCA/rtRHbQ== X-Google-Smtp-Source: AGHT+IEN3wCVVqpH9kmie/X7Whzt0Eu/DWEheWzZCPoBySQucFlLPTNfizOewVcdQboN6cL33/Eld+TMUNduGTaRGA== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:c51e:bdd0:7cc8:695c]) (user=almasrymina job=sendgmr) by 2002:a05:6902:100a:b0:dc6:ff2b:7e1b with SMTP id w10-20020a056902100a00b00dc6ff2b7e1bmr3451328ybt.4.1711493465800; Tue, 26 Mar 2024 15:51:05 -0700 (PDT) Date: Tue, 26 Mar 2024 15:50:36 -0700 In-Reply-To: <20240326225048.785801-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-parisc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240326225048.785801-1-almasrymina@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240326225048.785801-6-almasrymina@google.com> Subject: [RFC PATCH net-next v7 05/14] netdev: netdevice devmem allocator From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jonathan Corbet , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Andreas Larsson , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Steffen Klassert , Herbert Xu , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , " =?utf-8?q?Christian_K=C3=B6nig?= " , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi , Willem de Bruijn , Kaiyuan Zhang Implement netdev devmem allocator. The allocator takes a given struct netdev_dmabuf_binding as input and allocates net_iov from that binding. The allocation simply delegates to the binding's genpool for the allocation logic and wraps the returned memory region in a net_iov struct. Signed-off-by: Willem de Bruijn Signed-off-by: Kaiyuan Zhang Signed-off-by: Mina Almasry --- v7: - netdev_ -> net_devmem_* naming (Yunsheng). v6: - Add comment on net_iov_dma_addr to explain why we don't use niov->dma_addr (Pavel) - Refactor new functions into net/core/devmem.c (Pavel) v1: - Rename devmem -> dmabuf (David). --- include/net/devmem.h | 13 +++++++++++++ include/net/netmem.h | 40 ++++++++++++++++++++++++++++++++++++++++ net/core/devmem.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/include/net/devmem.h b/include/net/devmem.h index fa03bdabdffd..cd3186f5d1fb 100644 --- a/include/net/devmem.h +++ b/include/net/devmem.h @@ -68,7 +68,20 @@ int net_devmem_bind_dmabuf(struct net_device *dev, unsigned int dmabuf_fd, void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding); int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, struct net_devmem_dmabuf_binding *binding); +struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding); +void net_devmem_free_dmabuf(struct net_iov *ppiov); #else +static inline struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +{ + return NULL; +} + +static inline void net_devmem_free_dmabuf(struct net_iov *ppiov) +{ +} + static inline void __net_devmem_dmabuf_binding_free(struct net_devmem_dmabuf_binding *binding) { diff --git a/include/net/netmem.h b/include/net/netmem.h index 72e932a1a948..ca17ea1d33f8 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -14,8 +14,48 @@ struct net_iov { struct dmabuf_genpool_chunk_owner *owner; + unsigned long dma_addr; }; +static inline struct dmabuf_genpool_chunk_owner * +net_iov_owner(const struct net_iov *niov) +{ + return niov->owner; +} + +static inline unsigned int net_iov_idx(const struct net_iov *niov) +{ + return niov - net_iov_owner(niov)->niovs; +} + +/* This returns the absolute dma_addr_t calculated from + * net_iov_owner(niov)->owner->base_dma_addr, not the page_pool-owned + * niov->dma_addr. + * + * The absolute dma_addr_t is a dma_addr_t that is always uncompressed. + * + * The page_pool-owner niov->dma_addr is the absolute dma_addr compressed into + * an unsigned long. Special handling is done when the unsigned long is 32-bit + * but the dma_addr_t is 64-bit. + * + * In general code looking for the dma_addr_t should use net_iov_dma_addr(), + * while page_pool code looking for the unsigned long dma_addr which mirrors + * the field in struct page should use niov->dma_addr. + */ +static inline dma_addr_t net_iov_dma_addr(const struct net_iov *niov) +{ + struct dmabuf_genpool_chunk_owner *owner = net_iov_owner(niov); + + return owner->base_dma_addr + + ((dma_addr_t)net_iov_idx(niov) << PAGE_SHIFT); +} + +static inline struct netdev_dmabuf_binding * +net_iov_binding(const struct net_iov *niov) +{ + return net_iov_owner(niov)->binding; +} + /* netmem */ /** diff --git a/net/core/devmem.c b/net/core/devmem.c index e49f9ca74f67..84e88955ff2d 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -103,6 +103,45 @@ static int net_devmem_restart_rx_queue(struct net_device *dev, int rxq_idx) return err; } +struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +{ + struct dmabuf_genpool_chunk_owner *owner; + unsigned long dma_addr; + struct net_iov *niov; + ssize_t offset; + ssize_t index; + + dma_addr = gen_pool_alloc_owner(binding->chunk_pool, PAGE_SIZE, + (void **)&owner); + if (!dma_addr) + return NULL; + + offset = dma_addr - owner->base_dma_addr; + index = offset / PAGE_SIZE; + niov = &owner->niovs[index]; + + niov->pp_magic = 0; + niov->pp = NULL; + niov->dma_addr = 0; + atomic_long_set(&niov->pp_ref_count, 0); + + net_devmem_dmabuf_binding_get(binding); + + return niov; +} + +void net_devmem_free_dmabuf(struct net_iov *niov) +{ + struct net_devmem_dmabuf_binding *binding = net_iov_binding(niov); + unsigned long dma_addr = net_iov_dma_addr(niov); + + if (gen_pool_has_addr(binding->chunk_pool, dma_addr, PAGE_SIZE)) + gen_pool_free(binding->chunk_pool, dma_addr, PAGE_SIZE); + + net_devmem_dmabuf_binding_put(binding); +} + /* Protected by rtnl_lock() */ static DEFINE_XARRAY_FLAGS(net_devmem_dmabuf_bindings, XA_FLAGS_ALLOC1);