From: Stanislav Fomichev <sdf.kernel@gmail.com>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, sdf@fomichev.me,
bobbyeshleman@meta.com, almasrymina@google.com,
linux-kernel@vger.kernel.org
Subject: [PATCH net-next v3 2/3] net: devmem: embed net_iov_area in binding
Date: Thu, 17 Sep 2026 11:11:15 -0700 [thread overview]
Message-ID: <20260917181116.3319017-3-sdf@fomichev.me> (raw)
In-Reply-To: <20260917181116.3319017-1-sdf@fomichev.me>
After replacing the gen_pool with a binding-level freelist, devmem no
longer needs a separate chunk owner object. There is only one
net_iov_area for the binding, so store it directly in struct
net_devmem_dmabuf_binding.
Derive the binding from net_iov_owner() with container_of(), matching the
pattern used by io_uring zcrx. This removes the leftover
dmabuf_genpool_chunk_owner wrapper and its allocation/free path.
Reviewed-by: Mina Almasry <almasrymina@google.com>
Signed-off-by: Stanislav Fomichev <sdf@fomichev.me>
---
net/core/devmem.c | 61 ++++++++++++++---------------------------------
net/core/devmem.h | 25 +++++--------------
2 files changed, 24 insertions(+), 62 deletions(-)
diff --git a/net/core/devmem.c b/net/core/devmem.c
index 2ac9d74010a9..7cac0dab8ea8 100644
--- a/net/core/devmem.c
+++ b/net/core/devmem.c
@@ -29,15 +29,6 @@ static DEFINE_XARRAY_FLAGS(net_devmem_dmabuf_bindings, XA_FLAGS_ALLOC1);
static const struct memory_provider_ops dmabuf_devmem_ops;
-static void
-net_devmem_dmabuf_free_chunk_owner(struct dmabuf_genpool_chunk_owner *owner)
-{
- if (owner) {
- kvfree(owner->area.niovs);
- kfree(owner);
- }
-}
-
static void net_devmem_dmabuf_binding_release(struct percpu_ref *ref)
{
struct net_devmem_dmabuf_binding *binding =
@@ -52,12 +43,11 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
struct net_devmem_dmabuf_binding *binding = container_of(wq, typeof(*binding), unbind_w);
if (binding->freelist)
- WARN(binding->free_count != binding->chunk_owner->area.num_niovs,
+ WARN(binding->free_count != binding->area.num_niovs,
"destroying dmabuf binding with outstanding net_iovs: total=%zu, free=%zu",
- binding->chunk_owner->area.num_niovs,
- binding->free_count);
+ binding->area.num_niovs, binding->free_count);
- net_devmem_dmabuf_free_chunk_owner(binding->chunk_owner);
+ kvfree(binding->area.niovs);
dma_buf_unmap_attachment_unlocked(binding->attachment, binding->sgt,
binding->direction);
dma_buf_detach(binding->dmabuf, binding->attachment);
@@ -94,8 +84,7 @@ void net_devmem_free_dmabuf(struct net_iov *niov)
struct net_devmem_dmabuf_binding *binding = net_devmem_iov_binding(niov);
spin_lock_bh(&binding->freelist_lock);
- if (WARN_ON_ONCE(binding->free_count >=
- binding->chunk_owner->area.num_niovs)) {
+ if (WARN_ON_ONCE(binding->free_count >= binding->area.num_niovs)) {
spin_unlock_bh(&binding->freelist_lock);
return;
}
@@ -175,13 +164,11 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
struct netlink_ext_ack *extack)
{
struct net_devmem_dmabuf_binding *binding;
- struct dmabuf_genpool_chunk_owner *owner;
size_t niov_size = 1UL << niov_shift;
static u32 id_alloc_next;
struct scatterlist *sg;
struct dma_buf *dmabuf;
unsigned int sg_idx;
- size_t total_niovs;
size_t niov_idx;
size_t i;
int err;
@@ -241,16 +228,18 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
goto err_unmap;
}
- total_niovs = dmabuf->size >> niov_shift;
+ binding->area.base_virtual = 0;
+ binding->area.num_niovs = dmabuf->size >> niov_shift;
if (direction == DMA_TO_DEVICE) {
- binding->tx_vec = kvmalloc_objs(struct net_iov *, total_niovs);
+ binding->tx_vec = kvmalloc_objs(struct net_iov *,
+ binding->area.num_niovs);
if (!binding->tx_vec) {
err = -ENOMEM;
goto err_unmap;
}
} else {
spin_lock_init(&binding->freelist_lock);
- binding->freelist = kvmalloc_array(total_niovs,
+ binding->freelist = kvmalloc_array(binding->area.num_niovs,
sizeof(binding->freelist[0]),
GFP_KERNEL);
if (!binding->freelist) {
@@ -258,24 +247,13 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
goto err_unmap;
}
}
- owner = kzalloc_node(sizeof(*owner), GFP_KERNEL,
- dev_to_node(&dev->dev));
- if (!owner) {
+ binding->area.niovs = kvmalloc_objs(*binding->area.niovs,
+ binding->area.num_niovs);
+ if (!binding->area.niovs) {
err = -ENOMEM;
goto err_free_freelist;
}
- owner->area.base_virtual = 0;
- owner->area.num_niovs = total_niovs;
- owner->binding = binding;
- owner->area.niovs = kvmalloc_objs(*owner->area.niovs,
- owner->area.num_niovs);
- if (!owner->area.niovs) {
- err = -ENOMEM;
- goto err_free_owner;
- }
- binding->chunk_owner = owner;
-
niov_idx = 0;
for_each_sgtable_dma_sg(binding->sgt, sg, sg_idx) {
dma_addr_t dma_addr = sg_dma_address(sg);
@@ -289,13 +267,13 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
NL_SET_ERR_MSG_FMT(extack,
"dmabuf sg entry (addr=%pad, len=%zu) not aligned to niov size %zu",
&dma_addr, len, niov_size);
- goto err_free_chunk_owner;
+ goto err_free_niovs;
}
nr_niovs = len >> niov_shift;
for (i = 0; i < nr_niovs; i++, niov_idx++) {
- niov = &owner->area.niovs[niov_idx];
- net_iov_init(niov, &owner->area, NET_IOV_DMABUF);
+ niov = &binding->area.niovs[niov_idx];
+ net_iov_init(niov, &binding->area, NET_IOV_DMABUF);
page_pool_set_dma_addr_netmem(net_iov_to_netmem(niov),
dma_addr);
if (direction == DMA_TO_DEVICE)
@@ -310,17 +288,14 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
binding, xa_limit_32b, &id_alloc_next,
GFP_KERNEL);
if (err < 0)
- goto err_free_chunk_owner;
+ goto err_free_niovs;
list_add(&binding->list, &priv->bindings);
return binding;
-err_free_chunk_owner:
- net_devmem_dmabuf_free_chunk_owner(binding->chunk_owner);
- goto err_free_freelist;
-err_free_owner:
- kfree(owner);
+err_free_niovs:
+ kvfree(binding->area.niovs);
err_free_freelist:
kvfree(binding->freelist);
kvfree(binding->tx_vec);
diff --git a/net/core/devmem.h b/net/core/devmem.h
index db2eb6372aee..6ed16b4b4687 100644
--- a/net/core/devmem.h
+++ b/net/core/devmem.h
@@ -14,9 +14,9 @@
#include <net/netdev_netlink.h>
struct netlink_ext_ack;
-struct dmabuf_genpool_chunk_owner;
struct net_devmem_dmabuf_binding {
+ struct net_iov_area area;
struct dma_buf *dmabuf;
struct dma_buf_attachment *attachment;
struct sg_table *sgt;
@@ -27,7 +27,6 @@ struct net_devmem_dmabuf_binding {
* dereferenced.
*/
void *vdev;
- struct dmabuf_genpool_chunk_owner *chunk_owner;
/* Protect dev */
struct mutex lock;
@@ -82,11 +81,6 @@ struct net_devmem_dmabuf_binding {
};
#if defined(CONFIG_NET_DEVMEM)
-struct dmabuf_genpool_chunk_owner {
- struct net_iov_area area;
- struct net_devmem_dmabuf_binding *binding;
-};
-
void __net_devmem_dmabuf_binding_free(struct work_struct *wq);
struct net_devmem_dmabuf_binding *
net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
@@ -101,18 +95,11 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
struct net_devmem_dmabuf_binding *binding,
struct netlink_ext_ack *extack);
-static inline struct dmabuf_genpool_chunk_owner *
-net_devmem_iov_to_chunk_owner(const struct net_iov *niov)
-{
- struct net_iov_area *owner = net_iov_owner(niov);
-
- return container_of(owner, struct dmabuf_genpool_chunk_owner, area);
-}
-
static inline struct net_devmem_dmabuf_binding *
net_devmem_iov_binding(const struct net_iov *niov)
{
- return net_devmem_iov_to_chunk_owner(niov)->binding;
+ return container_of(net_iov_owner(niov),
+ struct net_devmem_dmabuf_binding, area);
}
static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
@@ -122,11 +109,11 @@ static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
{
- struct dmabuf_genpool_chunk_owner *co =
- net_devmem_iov_to_chunk_owner(niov);
+ struct net_devmem_dmabuf_binding *binding =
+ net_devmem_iov_binding(niov);
return net_iov_owner(niov)->base_virtual +
- ((unsigned long)net_iov_idx(niov) << co->binding->niov_shift);
+ ((unsigned long)net_iov_idx(niov) << binding->niov_shift);
}
static inline bool
--
2.53.0-Meta
next prev parent reply other threads:[~2026-09-17 18:11 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-17 18:11 [PATCH net-next v3 0/3] net: devmem: remove gen_pool from dma-buf allocations Stanislav Fomichev
2026-09-17 18:11 ` [PATCH net-next v3 1/3] net: devmem: replace gen_pool with freelist Stanislav Fomichev
2026-09-20 21:01 ` netdev-bot+sashiko
2026-09-21 16:58 ` Stanislav Fomichev
2026-09-21 23:29 ` Jakub Kicinski
2026-09-21 23:34 ` Mina Almasry
2026-09-17 18:11 ` Stanislav Fomichev [this message]
2026-09-20 21:01 ` [PATCH net-next v3 2/3] net: devmem: embed net_iov_area in binding netdev-bot+sashiko
2026-09-21 16:58 ` Stanislav Fomichev
2026-09-17 18:11 ` [PATCH net-next v3 3/3] net: devmem: batch net_iov allocations into the page_pool cache Stanislav Fomichev
2026-09-21 23:50 ` [PATCH net-next v3 0/3] net: devmem: remove gen_pool from dma-buf allocations patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260917181116.3319017-3-sdf@fomichev.me \
--to=sdf.kernel@gmail.com \
--cc=almasrymina@google.com \
--cc=bobbyeshleman@meta.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox