From nobody Fri Sep 25 01:22:24 2026 Received: from mail-pz2-f6.google.com (mail-pz2-f6.google.com [74.125.228.6]) (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 D962B4F93A1 for ; Thu, 17 Sep 2026 18:11:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.6 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668683; cv=none; b=A8ohrwKEypnWEKt1gUC3wp762rZOEdui1XcRFEijuz0ye1dNoDWGfpUALofm3+YH/Ndp4sZK7mSSKnt0QI2FNJHzZ3GpjTH8OK6H2mQXsXt035I8bpbbCdKln7NS0e06peyywJ2R4jXdHQhpdv0gnftjA4FSYeVermesRNXSCgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668683; c=relaxed/simple; bh=+xgFA1SB2Xp6UmAnIjOjmJ4KEbqPgxrCzKm51uLQBxI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jr0EVsJ0u0kny2n+HroFG1cH1djutgMPK9le6R9R1iRVbRWOb3zYD0moV9ckQ3tdRpfEMHiKpVyalZGnc2kSUdvJEwcU8kM83jas0lfKJVOAg+uA1Lla0OI19izX3SRrYqV9xNZAma8jWEwmcDGwkheO8jQEqjVKQvdPdDh7H1k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WrK3e7Q4; arc=none smtp.client-ip=74.125.228.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WrK3e7Q4" Received: by mail-pz2-f6.google.com with SMTP id 41be03b00d2f7-cc51dd43909so419474a12.1 for ; Thu, 17 Sep 2026 11:11:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789668678; x=1790273478; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OewrFGRHJOj2WekPA/CAeklK7TzcU4RR3M015GddGVk=; b=WrK3e7Q4/une2zNNY0kmCw1Hdy/Cc4fUmwGiiGBskU8qejaor4/3pLd5nFG7WNfUYm cqudNmcXP3Kgwk03zI2zqgL+34PMLgLPP/213M8yLOWTwpn1QD0DU5FJXehJqne3kgv9 7t+G03DGe9FxF/xpfWctrnLvSedNG/89HODuPStiqB9y8anLRQ0UWxJIWY1wQzwMcSlZ CNN8robTBdIep1gdMKTglbLOObnBkRvhXCFr+rh1mWFQ8FvR3uPdUlzIoE1kxVlI/pOE Ss51pghS4kWwd3WN4WD1mR886KloPRXsAqHNVa2zIkYyjK1sY86kSnAexX6ltu0cuAjj mytA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789668678; x=1790273478; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=OewrFGRHJOj2WekPA/CAeklK7TzcU4RR3M015GddGVk=; b=Bx9v1dyGZ8NSdzhNCJIPN669wwzrjh1uFH7jTZzLkShojNsSDh3gdlPD16UufXwaup +pGX8Y8AW/NzQzDeoRRGx1OGbkoQKPqf7+0hzU4E5XeTvZhHGC4cTU3qaM6k2SWHBrGo dncP2tziQXBo7GYccv9IGjHg2VuJq6u5AjeQTXtSKZnUM9Qt2Rse9+IxmyYUGlE+W7C7 8315W7fAkpg5Pk6OLa2lyGqIUL6rfOBe5SHCmjzuSd/2kHaOyYfobzLukZBDzIpjV1Ir niqOktVVSR3CqaE6xGTwe4jIGbGUJrJ5pEcJWQfHeHFtZUSeyyLdpOXMql61uBb76lY8 7OBA== X-Forwarded-Encrypted: i=1; AKwUvBxz7NaxsW3pP2ooe1IATzhZhud8TFIw9sRsQobvu3AV3Lu9/a1FjX4e1pxbdxsrtiHyx2Pi4Z3NCfV/M+E=@vger.kernel.org X-Gm-Message-State: AFuF++lv0ChGykfQ2oTOLv0xfZFYW2Y1sSf5I7vKcMAmVln5QCXAh6vH X7GRAMYhnNXvfehX51bGWGrcBB3q1oGEfaYaxE0Jw94Ru2tLlxOZjl6drDbQSGvm X-Gm-Gg: AYBFou0W5VlcBXJtN4rcpFeOAKPobO0m1cPkZpsh5UO3x+9opxTTG554P3Hva7h5fSC Ah6HKNAGqBceiWvdOINAfsHDlophHMLTMwuv78tIB4AOl1hz1ikhHjoz9dlp+lbjAjypTV+IUaj z02tBKwGkVTxvHNvKx4mFv2PqfEmdIaHqSnX4EWhYvHp8b4oHCXoD19juGTNr8HGaFguY5dn2H7 6CukfbgTGi+jysOAHKGubCXB33Id4HXdEyZOaRZOQynnGv1fy9vBnCkeHddaJumRisBuPqvIRFb ZzeKblLZYAUCZn2Om1Z/YWYHP6lbDZK39a1yMKvpVYHg6F3XGYjUzgbgS6wtRDW8V7+vO+S1Lyk hj409NjaTt0Ft8t0qPZr9Ac5CoKXyDV1J+X0s2RrdcQq4I6k/basBVjkPhzmOpe08eHOYiJdoA6 3Gus7PioOPGS+IjTMNLYBW4/23+Z6XbxO6vP6u8hU/vSCCJ3Z0dJNfRuguDuawmJDK X-Received: by 2002:a05:6a00:9291:b0:857:7337:5dba with SMTP id d2e1a72fcca58-8723d4366d8mr14304218b3a.24.1789668678268; Thu, 17 Sep 2026 11:11:18 -0700 (PDT) Received: from localhost ([2a03:2880:2ff:51::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-872025dd04csm3272663b3a.60.2026.09.17.11.11.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Sep 2026 11:11:17 -0700 (PDT) From: Stanislav Fomichev X-Google-Original-From: Stanislav Fomichev 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 1/3] net: devmem: replace gen_pool with freelist Date: Thu, 17 Sep 2026 11:11:14 -0700 Message-ID: <20260917181116.3319017-2-sdf@fomichev.me> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260917181116.3319017-1-sdf@fomichev.me> References: <20260917181116.3319017-1-sdf@fomichev.me> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" devmem only needs fixed-size net_iov allocations for each dma-buf binding. The gen_pool tracks the same free set indirectly through DMA addresses, which makes devmem depend on the generic allocator even though the users are fixed-size net_iov chunks. Mirror the io_uring zcrx model more closely by keeping a binding-level freelist protected by spin_lock_bh(). Use a single net_iov_area owner for the binding, populate each net_iov's DMA address while walking the SG table, and check at teardown that all net_iovs have returned to the freelist. Validate the DMA-BUF size against the net_iov size up front and derive the RX and TX entry count from it, avoiding a separate SG-table pass. Drop the NET_DEVMEM select of GENERIC_ALLOCATOR now that devmem no longer calls gen_pool APIs. Signed-off-by: Stanislav Fomichev Reviewed-by: Mina Almasry --- net/Kconfig | 1 - net/core/devmem.c | 182 +++++++++++++++++++++------------------------- net/core/devmem.h | 15 ++-- 3 files changed, 88 insertions(+), 110 deletions(-) diff --git a/net/Kconfig b/net/Kconfig index e38477393551..76ab44aa439a 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -68,7 +68,6 @@ config SKB_EXTENSIONS =20 config NET_DEVMEM def_bool y - select GENERIC_ALLOCATOR depends on DMA_SHARED_BUFFER depends on PAGE_POOL =20 diff --git a/net/core/devmem.c b/net/core/devmem.c index f4d60654ce7f..2ac9d74010a9 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -8,7 +8,6 @@ */ =20 #include -#include #include #include #include @@ -30,23 +29,13 @@ static DEFINE_XARRAY_FLAGS(net_devmem_dmabuf_bindings, = XA_FLAGS_ALLOC1); =20 static const struct memory_provider_ops dmabuf_devmem_ops; =20 -static void net_devmem_dmabuf_free_chunk_owner(struct gen_pool *genpool, - struct gen_pool_chunk *chunk, - void *not_used) +static void +net_devmem_dmabuf_free_chunk_owner(struct dmabuf_genpool_chunk_owner *owne= r) { - struct dmabuf_genpool_chunk_owner *owner =3D chunk->owner; - - kvfree(owner->area.niovs); - kfree(owner); -} - -static dma_addr_t net_devmem_get_dma_addr(const struct net_iov *niov) -{ - struct dmabuf_genpool_chunk_owner *owner; - - owner =3D net_devmem_iov_to_chunk_owner(niov); - return owner->base_dma_addr + - ((dma_addr_t)net_iov_idx(niov) << owner->binding->niov_shift); + if (owner) { + kvfree(owner->area.niovs); + kfree(owner); + } } =20 static void net_devmem_dmabuf_binding_release(struct percpu_ref *ref) @@ -62,24 +51,20 @@ void __net_devmem_dmabuf_binding_free(struct work_struc= t *wq) { struct net_devmem_dmabuf_binding *binding =3D container_of(wq, typeof(*bi= nding), unbind_w); =20 - size_t size, avail; - - gen_pool_for_each_chunk(binding->chunk_pool, - net_devmem_dmabuf_free_chunk_owner, NULL); - - size =3D gen_pool_size(binding->chunk_pool); - avail =3D gen_pool_avail(binding->chunk_pool); - - if (!WARN(size !=3D avail, "can't destroy genpool. size=3D%zu, avail=3D%z= u", - size, avail)) - gen_pool_destroy(binding->chunk_pool); + if (binding->freelist) + WARN(binding->free_count !=3D binding->chunk_owner->area.num_niovs, + "destroying dmabuf binding with outstanding net_iovs: total=3D%zu, = free=3D%zu", + binding->chunk_owner->area.num_niovs, + binding->free_count); =20 + net_devmem_dmabuf_free_chunk_owner(binding->chunk_owner); dma_buf_unmap_attachment_unlocked(binding->attachment, binding->sgt, binding->direction); dma_buf_detach(binding->dmabuf, binding->attachment); dma_buf_put(binding->dmabuf); xa_destroy(&binding->bound_rxqs); percpu_ref_exit(&binding->ref); + kvfree(binding->freelist); kvfree(binding->tx_vec); kfree(binding); } @@ -87,21 +72,15 @@ void __net_devmem_dmabuf_binding_free(struct work_struc= t *wq) 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 =3D gen_pool_alloc_owner(binding->chunk_pool, - 1UL << binding->niov_shift, - (void **)&owner); - if (!dma_addr) + spin_lock_bh(&binding->freelist_lock); + if (unlikely(!binding->free_count)) { + spin_unlock_bh(&binding->freelist_lock); return NULL; + } =20 - offset =3D dma_addr - owner->base_dma_addr; - index =3D offset >> binding->niov_shift; - niov =3D &owner->area.niovs[index]; + niov =3D binding->freelist[--binding->free_count]; + spin_unlock_bh(&binding->freelist_lock); =20 niov->desc.pp_magic =3D 0; niov->desc.pp =3D NULL; @@ -113,14 +92,16 @@ net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_bindi= ng *binding) void net_devmem_free_dmabuf(struct net_iov *niov) { struct net_devmem_dmabuf_binding *binding =3D net_devmem_iov_binding(niov= ); - unsigned long dma_addr =3D net_devmem_get_dma_addr(niov); - size_t niov_size =3D 1UL << binding->niov_shift; =20 - if (WARN_ON(!gen_pool_has_addr(binding->chunk_pool, dma_addr, - niov_size))) + spin_lock_bh(&binding->freelist_lock); + if (WARN_ON_ONCE(binding->free_count >=3D + binding->chunk_owner->area.num_niovs)) { + spin_unlock_bh(&binding->freelist_lock); return; + } =20 - gen_pool_free(binding->chunk_pool, dma_addr, niov_size); + binding->freelist[binding->free_count++] =3D niov; + spin_unlock_bh(&binding->freelist_lock); } =20 void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding) @@ -194,12 +175,15 @@ 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 =3D 1UL << niov_shift; static u32 id_alloc_next; struct scatterlist *sg; struct dma_buf *dmabuf; - unsigned int sg_idx, i; - unsigned long virtual; + unsigned int sg_idx; + size_t total_niovs; + size_t niov_idx; + size_t i; int err; =20 if (!dma_dev) { @@ -249,33 +233,55 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *= vdev, goto err_detach; } =20 + if (!IS_ALIGNED(dmabuf->size, niov_size)) { + err =3D -EINVAL; + NL_SET_ERR_MSG_FMT(extack, + "dmabuf size %zu not aligned to niov size %zu", + dmabuf->size, niov_size); + goto err_unmap; + } + + total_niovs =3D dmabuf->size >> niov_shift; if (direction =3D=3D DMA_TO_DEVICE) { - if (!IS_ALIGNED(dmabuf->size, PAGE_SIZE)) { - err =3D -EINVAL; - NL_SET_ERR_MSG(extack, "TX dma-buf size must be a multiple of PAGE_SIZE= "); + binding->tx_vec =3D kvmalloc_objs(struct net_iov *, total_niovs); + if (!binding->tx_vec) { + err =3D -ENOMEM; goto err_unmap; } - binding->tx_vec =3D kvmalloc_objs(struct net_iov *, - dmabuf->size / PAGE_SIZE); - if (!binding->tx_vec) { + } else { + spin_lock_init(&binding->freelist_lock); + binding->freelist =3D kvmalloc_array(total_niovs, + sizeof(binding->freelist[0]), + GFP_KERNEL); + if (!binding->freelist) { err =3D -ENOMEM; goto err_unmap; } } + owner =3D kzalloc_node(sizeof(*owner), GFP_KERNEL, + dev_to_node(&dev->dev)); + if (!owner) { + err =3D -ENOMEM; + goto err_free_freelist; + } =20 - binding->chunk_pool =3D gen_pool_create(niov_shift, - dev_to_node(&dev->dev)); - if (!binding->chunk_pool) { + owner->area.base_virtual =3D 0; + owner->area.num_niovs =3D total_niovs; + owner->binding =3D binding; + owner->area.niovs =3D kvmalloc_objs(*owner->area.niovs, + owner->area.num_niovs); + if (!owner->area.niovs) { err =3D -ENOMEM; - goto err_tx_vec; + goto err_free_owner; } + binding->chunk_owner =3D owner; =20 - virtual =3D 0; + niov_idx =3D 0; for_each_sgtable_dma_sg(binding->sgt, sg, sg_idx) { dma_addr_t dma_addr =3D sg_dma_address(sg); - struct dmabuf_genpool_chunk_owner *owner; size_t len =3D sg_dma_len(sg); struct net_iov *niov; + size_t nr_niovs; =20 if (!IS_ALIGNED(dma_addr, niov_size) || !IS_ALIGNED(len, niov_size)) { @@ -283,64 +289,40 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *= vdev, NL_SET_ERR_MSG_FMT(extack, "dmabuf sg entry (addr=3D%pad, len=3D%zu) not aligned to niov size= %zu", &dma_addr, len, niov_size); - goto err_free_chunks; - } - - owner =3D kzalloc_node(sizeof(*owner), GFP_KERNEL, - dev_to_node(&dev->dev)); - if (!owner) { - err =3D -ENOMEM; - goto err_free_chunks; + goto err_free_chunk_owner; } =20 - owner->area.base_virtual =3D virtual; - owner->base_dma_addr =3D dma_addr; - owner->area.num_niovs =3D len >> niov_shift; - owner->binding =3D binding; - - err =3D gen_pool_add_owner(binding->chunk_pool, dma_addr, - dma_addr, len, dev_to_node(&dev->dev), - owner); - if (err) { - kfree(owner); - err =3D -EINVAL; - goto err_free_chunks; - } - - owner->area.niovs =3D kvmalloc_objs(*owner->area.niovs, - owner->area.num_niovs); - if (!owner->area.niovs) { - err =3D -ENOMEM; - goto err_free_chunks; - } - - for (i =3D 0; i < owner->area.num_niovs; i++) { - niov =3D &owner->area.niovs[i]; + nr_niovs =3D len >> niov_shift; + for (i =3D 0; i < nr_niovs; i++, niov_idx++) { + niov =3D &owner->area.niovs[niov_idx]; net_iov_init(niov, &owner->area, NET_IOV_DMABUF); page_pool_set_dma_addr_netmem(net_iov_to_netmem(niov), - net_devmem_get_dma_addr(niov)); + dma_addr); if (direction =3D=3D DMA_TO_DEVICE) - binding->tx_vec[owner->area.base_virtual / PAGE_SIZE + i] =3D niov; + binding->tx_vec[niov_idx] =3D niov; + else + binding->freelist[binding->free_count++] =3D niov; + dma_addr +=3D niov_size; } - - virtual +=3D len; } =20 err =3D xa_alloc_cyclic(&net_devmem_dmabuf_bindings, &binding->id, binding, xa_limit_32b, &id_alloc_next, GFP_KERNEL); if (err < 0) - goto err_free_chunks; + goto err_free_chunk_owner; =20 list_add(&binding->list, &priv->bindings); =20 return binding; =20 -err_free_chunks: - gen_pool_for_each_chunk(binding->chunk_pool, - net_devmem_dmabuf_free_chunk_owner, NULL); - gen_pool_destroy(binding->chunk_pool); -err_tx_vec: +err_free_chunk_owner: + net_devmem_dmabuf_free_chunk_owner(binding->chunk_owner); + goto err_free_freelist; +err_free_owner: + kfree(owner); +err_free_freelist: + kvfree(binding->freelist); kvfree(binding->tx_vec); err_unmap: dma_buf_unmap_attachment_unlocked(binding->attachment, binding->sgt, diff --git a/net/core/devmem.h b/net/core/devmem.h index 4a293a7d1149..db2eb6372aee 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -14,6 +14,7 @@ #include =20 struct netlink_ext_ack; +struct dmabuf_genpool_chunk_owner; =20 struct net_devmem_dmabuf_binding { struct dma_buf *dmabuf; @@ -26,7 +27,7 @@ struct net_devmem_dmabuf_binding { * dereferenced. */ void *vdev; - struct gen_pool *chunk_pool; + struct dmabuf_genpool_chunk_owner *chunk_owner; /* Protect dev */ struct mutex lock; =20 @@ -57,6 +58,10 @@ struct net_devmem_dmabuf_binding { /* rxq's this binding is active on. */ struct xarray bound_rxqs; =20 + spinlock_t freelist_lock ____cacheline_aligned_in_smp; + size_t free_count; + struct net_iov **freelist; + /* ID of this binding. Globally unique to all bindings currently * active. */ @@ -77,17 +82,9 @@ struct net_devmem_dmabuf_binding { }; =20 #if defined(CONFIG_NET_DEVMEM) -/* Owner of the dma-buf chunks inserted into the gen pool. Each scatterlist - * entry from the dmabuf is inserted into the genpool as a chunk, and needs - * this owner struct to keep track of some metadata necessary to create - * allocations from this chunk. - */ struct dmabuf_genpool_chunk_owner { struct net_iov_area area; struct net_devmem_dmabuf_binding *binding; - - /* dma_addr of the start of the chunk. */ - dma_addr_t base_dma_addr; }; =20 void __net_devmem_dmabuf_binding_free(struct work_struct *wq); --=20 2.53.0-Meta From nobody Fri Sep 25 01:22:24 2026 Received: from mail-pz2-f1.google.com (mail-pz2-f1.google.com [74.125.228.1]) (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 793A74FD270 for ; Thu, 17 Sep 2026 18:11:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668686; cv=none; b=bIWszkNtF8kwxT3Oah5tQrruc6q+hAWxx0bOGS4iGVAHbHngDciayj2Cunscmphp3kb4KSQZAbVylKzA1KE9g7zqniVFkqWQWtXF/Sb2RviTcoPp+KBH9zCkp+OqrJ1BuPpfUvm/mCXULzz4hMXuXwkANHWseccjQv9kZ6owacI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668686; c=relaxed/simple; bh=YozIsMvQDzfNdXt3OLFHnc8aWl9/GFlZtvg1jfBFGhs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C74lQR3vOsxswhegw/9uZ+inQTwGF4Q7OphkmJ2HjMd4o1xMFYIRo+8kHQdthVNhZu5f3ehwdSxmiJn+gWkLIF/7T3mPUvHM3Ldmq3aLgZlJWebWduQrDJFW0uPX9JJVhwC+i1eB1JjJKB/+wGef58TvBIDzgoD3pI5ue90iYv4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DDxniRGk; arc=none smtp.client-ip=74.125.228.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DDxniRGk" Received: by mail-pz2-f1.google.com with SMTP id 41be03b00d2f7-cbb818e57c9so479791a12.0 for ; Thu, 17 Sep 2026 11:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789668679; x=1790273479; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PJwoQyONWQntFcfUwerIyiwwlB2/wy713uP/MmK5r2A=; b=DDxniRGkWuiublWw19MFDBsyir4MqHziN6WDJdHb3OnmGwaxMlTKhhvzUwBSs1Mm0+ R9MOp8K3Aswm4hZaLUDt0qq/8llihFc3LVdiQq1yjxzgjJ7WF+QYYVJ6J838GBj2ZmMI NLhmY3BerE0+EPQENaf5ehrZwuZ6rM2discdTpck6CAXGwNbnRhtxCT8UVAw6ZtC4U8H 8+Zi3mAfSOXFc95ghjmf3JJc5/DPqcj6Y2M5kBhN88RUliDixrKqAXIt69XiuvAASiWY IDSCU9UwjdEyb5NzjuISrh6n2ZtyKcnHbyhTIq8MAGMgFdbCJJQ+r9l3gQx1rut/kW/K iFiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789668679; x=1790273479; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=PJwoQyONWQntFcfUwerIyiwwlB2/wy713uP/MmK5r2A=; b=tbNuY+ZIpR8E03bovX1vTZn3vlCOhWZlK/PPidMpjWy0cXwTwLvCxIq71dZErfcsHo qoN94iHi/+T2IhFLhsTiUlVq2GJXUkp+mxTCwi7DsbcpQ97/Li0kVnGTmAz1kvUBRkhr WG9bgNt/b7gljM4MMhhOKxGMd47S1JneiGEureKRu443NqJ+JA3n5AowDnB/hzWMU+Sd dJH1quFt06QOVg22A6Yyb7WOavPQ/mcZmZZ2Q4C33M+2T0GoAM5XGIxmVu/b6QUYDVSC 3yOVmNJ6bsKjT8PfrV8JhLBIRday0K5SJkt4MYmHUjua/HhmIEtlJ1OhyCgGHXKW8Pa4 XslA== X-Forwarded-Encrypted: i=1; AKwUvBwGIHN5TgnWv5a7AjS6AKVCPrPXZVEb9MVwnlwc0Bv1J6W1aDKQ1IqbThU6r2ef0+y1taFyCjOycy+Ekic=@vger.kernel.org X-Gm-Message-State: AFuF++nx6Rrst5UX4gAkcA4ZZSTUyXfQhbqFb8AtnGf1ku8YCxVnQztl aLZuVYnKbOmlmeEcIpLTxRJct5oXUvnkRe1bd0PcArwgFPjhi0pD//nftVZrZiBa X-Gm-Gg: AYBFou0gpq+FmSfUTltPekCXJtMS9Xo3PddVrDTHcwye9He5OhhY7tjhlc//+OthwDg Le6c97p1qsVnLAJI/rNcRGUx+V00Aqku/Rjli+xN49Kgf5XdIGegUTBj+xv98o5u5UpnR6EBciM mG55JIKJS4u3R2EEYuZnNSoU+j14NQE/WCIqqV4f6CyWyDDVhfo5r/VzonwNO2JMcHsIPGKbkrl oJ/j4oSvfLRzVttJZJH7S3FCxAu0+vsXZDM6r6LchxhARTIBfi1XozYu2JLsYE6B1CuTOe6ONT5 CEtzxBh28PzhxZyi+738DWig8aMCO8NqYPDMIWYppSCEfQetWS+3TyeJ29nrSPsqjmzf3pOjsz9 YhS4DJea0V2n01Uu4vXHDNsE3L/TLTF2DOXZRTeWPPO6UjIWVafCScBbSFbS3QTLXV0sSDA+z2A UnUZTfrUczBvqiQGTMsxHK12Sp7QreLrn4o/wpRq/x9Sxe8J9MKTx2D00WLO8zzznU X-Received: by 2002:a05:6a21:9086:b0:3d2:5eb3:596 with SMTP id adf61e73a8af0-3dd5f57c1admr19617115637.16.1789668679280; Thu, 17 Sep 2026 11:11:19 -0700 (PDT) Received: from localhost ([2a03:2880:2ff:59::]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc5256e04c7sm1192926a12.22.2026.09.17.11.11.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Sep 2026 11:11:18 -0700 (PDT) From: Stanislav Fomichev X-Google-Original-From: Stanislav Fomichev 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 Message-ID: <20260917181116.3319017-3-sdf@fomichev.me> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260917181116.3319017-1-sdf@fomichev.me> References: <20260917181116.3319017-1-sdf@fomichev.me> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Stanislav Fomichev --- 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, X= A_FLAGS_ALLOC1); =20 static const struct memory_provider_ops dmabuf_devmem_ops; =20 -static void -net_devmem_dmabuf_free_chunk_owner(struct dmabuf_genpool_chunk_owner *owne= r) -{ - 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 =3D @@ -52,12 +43,11 @@ void __net_devmem_dmabuf_binding_free(struct work_struc= t *wq) struct net_devmem_dmabuf_binding *binding =3D container_of(wq, typeof(*bi= nding), unbind_w); =20 if (binding->freelist) - WARN(binding->free_count !=3D binding->chunk_owner->area.num_niovs, + WARN(binding->free_count !=3D binding->area.num_niovs, "destroying dmabuf binding with outstanding net_iovs: total=3D%zu, = free=3D%zu", - binding->chunk_owner->area.num_niovs, - binding->free_count); + binding->area.num_niovs, binding->free_count); =20 - 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 =3D net_devmem_iov_binding(niov= ); =20 spin_lock_bh(&binding->freelist_lock); - if (WARN_ON_ONCE(binding->free_count >=3D - binding->chunk_owner->area.num_niovs)) { + if (WARN_ON_ONCE(binding->free_count >=3D 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 =3D 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; } =20 - total_niovs =3D dmabuf->size >> niov_shift; + binding->area.base_virtual =3D 0; + binding->area.num_niovs =3D dmabuf->size >> niov_shift; if (direction =3D=3D DMA_TO_DEVICE) { - binding->tx_vec =3D kvmalloc_objs(struct net_iov *, total_niovs); + binding->tx_vec =3D kvmalloc_objs(struct net_iov *, + binding->area.num_niovs); if (!binding->tx_vec) { err =3D -ENOMEM; goto err_unmap; } } else { spin_lock_init(&binding->freelist_lock); - binding->freelist =3D kvmalloc_array(total_niovs, + binding->freelist =3D 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 =3D kzalloc_node(sizeof(*owner), GFP_KERNEL, - dev_to_node(&dev->dev)); - if (!owner) { + binding->area.niovs =3D kvmalloc_objs(*binding->area.niovs, + binding->area.num_niovs); + if (!binding->area.niovs) { err =3D -ENOMEM; goto err_free_freelist; } =20 - owner->area.base_virtual =3D 0; - owner->area.num_niovs =3D total_niovs; - owner->binding =3D binding; - owner->area.niovs =3D kvmalloc_objs(*owner->area.niovs, - owner->area.num_niovs); - if (!owner->area.niovs) { - err =3D -ENOMEM; - goto err_free_owner; - } - binding->chunk_owner =3D owner; - niov_idx =3D 0; for_each_sgtable_dma_sg(binding->sgt, sg, sg_idx) { dma_addr_t dma_addr =3D 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=3D%pad, len=3D%zu) not aligned to niov size= %zu", &dma_addr, len, niov_size); - goto err_free_chunk_owner; + goto err_free_niovs; } =20 nr_niovs =3D len >> niov_shift; for (i =3D 0; i < nr_niovs; i++, niov_idx++) { - niov =3D &owner->area.niovs[niov_idx]; - net_iov_init(niov, &owner->area, NET_IOV_DMABUF); + niov =3D &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 =3D=3D 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; =20 list_add(&binding->list, &priv->bindings); =20 return binding; =20 -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 =20 struct netlink_ext_ack; -struct dmabuf_genpool_chunk_owner; =20 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; =20 @@ -82,11 +81,6 @@ struct net_devmem_dmabuf_binding { }; =20 #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); =20 -static inline struct dmabuf_genpool_chunk_owner * -net_devmem_iov_to_chunk_owner(const struct net_iov *niov) -{ - struct net_iov_area *owner =3D 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); } =20 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 str= uct net_iov *niov) =20 static inline unsigned long net_iov_virtual_addr(const struct net_iov *nio= v) { - struct dmabuf_genpool_chunk_owner *co =3D - net_devmem_iov_to_chunk_owner(niov); + struct net_devmem_dmabuf_binding *binding =3D + net_devmem_iov_binding(niov); =20 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); } =20 static inline bool --=20 2.53.0-Meta From nobody Fri Sep 25 01:22:24 2026 Received: from mail-pz2-f9.google.com (mail-pz2-f9.google.com [74.125.228.9]) (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 069C04FD277 for ; Thu, 17 Sep 2026 18:11:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668684; cv=none; b=WViKEFLqnKLrd+Duldu9f6/nl+EyYnC/o2nng/+gPb9kXct5W4bJBB9IQqKBK5pHxxgJdchbt+Xoqvgh5/Vrv1aUmyaGL0P3zDoc7Vjg3+ymrG4wIPrw0FHd++czwHAREBbWkEUky440DfKkJwC1D/jWCcf9EMe1irMuC3xJRUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789668684; c=relaxed/simple; bh=sOv9MrPuMkxhkcI5OSCAWCad/gcsIcyR0kbOkg9j3E4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GjyTinEDZHMixCJwk2yBx/xMDdVcwDcyOEVBp0EB9RRnDigAlJgRkb8rP3G69CWyDuQghPNVEEy/63yRB9clh/SzaxRHdSvCSwKVZY/g7obrxG+5E06Nq54qwXdvKk6Cg+ct4AXUXEmanfL1UV1BTcKJIRxlB4bxBZQzp1ATilM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FnEOWWpb; arc=none smtp.client-ip=74.125.228.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FnEOWWpb" Received: by mail-pz2-f9.google.com with SMTP id 41be03b00d2f7-cc150711112so155507a12.0 for ; Thu, 17 Sep 2026 11:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789668680; x=1790273480; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9MPaV07+oksypE3IXx8WkM76dXMLUQTsKgP+7C1tfNM=; b=FnEOWWpb0MWL2Yn4HxqdNuCy5vkfBT0V6t2RdHHdU3Oy/JSZNgGhKdEEvTw8Dpy7eo ox3zrXeSgf3m+suHMh6J+GJ/b704pBebS4nLA5QLm4ft+1DgcnE6kxecDsGQm0STb9FX sX6FORCFU1ui22rBDRUj2Oh1hYZVLFl4BejgaAwjr4FCb5/7VsAhcUuEp3uycTaJuMOe oV8JFR7G8p9y11HCiPto5/n2WCEcIPKYOFYNOgupQnuYc18nwQHUsQlYuXVJ9Aw47vyP 7IT3cYjAygUUnnjfAdOxGe/BDF45xVszifBHFPBe0l/3WUDn7k888SR22RYM/NWnMPHP zOOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789668680; x=1790273480; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=9MPaV07+oksypE3IXx8WkM76dXMLUQTsKgP+7C1tfNM=; b=FK8r7AgGvm5s4WHqBZJGWe0y8oq9dL6rH8Zf1dAdAEuLszmIj0OY65jKtIESpD4Pds RRGmqrgPKzxnE6ZAULWdiI62xBXwtBWRvAUZAT62TkQlV9DjpmfNEb3AZMhof5ouLGPw LCVjqUHFiCa9Max1J1K9fwyNQuyjbnxVagvrOfsMCKDZtvkW4NmVB68dC9ix2hI1YOTw j3OFiyuCpWZbtsrAqeDz1JWuYCyJ63k4YNNpKh+npq73adliEbLq5viWL/1MHofjSdK2 9V7qVH6a8eFghVZl/qYuv+8yXW3lJ2nAC4gBibbPoNmcJeorxPv4JB48ZMVLTZifUzqZ NrFg== X-Forwarded-Encrypted: i=1; AKwUvBxfmGsZpsTJqqsMtJW0oghOkjtHAYDO6uWZmKv+ZitB+fALo/vSH0TfC76WgjybLTWqZg9FVeCs5jP2HL0=@vger.kernel.org X-Gm-Message-State: AFuF++kLuM/hCGtGpg+mngKtmgQNa1svR7Vr1nGhdKBT+MsbW3lvFqiY SQObJqpi2HtoSsO1aWaHOh0BvVQvOTAmSUntYGKM/Aim3OunUdp8ezAM X-Gm-Gg: AYBFou3F4eCaO/9/gM2He8/0ZdKK2jvkHsuwCucRpLFFZ/6zxOPVLzHz+/u0GNoxl2H rODEJ4YVcaiNsHjLwmurrPkXUCTUsSqyIkYnEFhqXz32Fa5CI2oVG96IW3vLjP9uqm3epnEVVJg BCs6annqzfoE5S1+K0k/pY4duIlJykLrGefILfCIbF5cySgVAfrnzbo/HB4KcvE49M3aqiiGcrq JUQHo64dlY8Hq2oZmuw6ZdRWFHOnPb74P+Eqc21LdrpCylf8gRFVtc0sIlq81fWTt2/jV8PYehR /KA9gqTrwgptW6sSxxMHf7SZb+Ak3qxU620hdjCDZi3C4OefUrxC/jra2RotUTt0iAYL2R1wlo/ /dwShst7cHGBuW3AIZ2/0b8cEyqN+uq4aww1drOQAOSrBBYcWFk6ju8ZtqDw0TbTX8tgcWrhf+z TJmLoG9sdOXu4SJcFQrkpsqxVI0lXuk5GAL/ThI5W/UTfz8ABkeKI+ChEMkQ+NrhI= X-Received: by 2002:a05:6a00:a112:b0:84c:5b65:ef86 with SMTP id d2e1a72fcca58-87238f87793mr13313083b3a.17.1789668680213; Thu, 17 Sep 2026 11:11:20 -0700 (PDT) Received: from localhost ([2a03:2880:2ff:9::]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc50c24487bsm3366301a12.12.2026.09.17.11.11.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Sep 2026 11:11:19 -0700 (PDT) From: Stanislav Fomichev X-Google-Original-From: Stanislav Fomichev 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 3/3] net: devmem: batch net_iov allocations into the page_pool cache Date: Thu, 17 Sep 2026 11:11:16 -0700 Message-ID: <20260917181116.3319017-4-sdf@fomichev.me> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260917181116.3319017-1-sdf@fomichev.me> References: <20260917181116.3319017-1-sdf@fomichev.me> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename net_devmem_alloc_dmabuf() into net_devmem_alloc_dmabuf_bulk() and make it refill page pool with up to PP_ALLOC_CACHE_REFILL NIOVs, similar to io_pp_zc_alloc_netmems(). That should amortize recently introduced freelist_lock. Reviewed-by: Mina Almasry Signed-off-by: Stanislav Fomichev --- net/core/devmem.c | 57 ++++++++++++++++++++++++++++++----------------- net/core/devmem.h | 8 ------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/net/core/devmem.c b/net/core/devmem.c index 7cac0dab8ea8..a9d86b5a5588 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -59,24 +59,24 @@ void __net_devmem_dmabuf_binding_free(struct work_struc= t *wq) kfree(binding); } =20 -struct net_iov * -net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +static unsigned int +net_devmem_alloc_dmabuf_bulk(struct net_devmem_dmabuf_binding *binding, + netmem_ref *netmems, unsigned int count) { - struct net_iov *niov; + unsigned int i; + spin_lock_bh(&binding->freelist_lock); - if (unlikely(!binding->free_count)) { - spin_unlock_bh(&binding->freelist_lock); - return NULL; + + count =3D min_t(size_t, count, binding->free_count); + for (i =3D 0; i < count; i++) { + struct net_iov *niov =3D binding->freelist[--binding->free_count]; + + netmems[i] =3D net_iov_to_netmem(niov); } =20 - niov =3D binding->freelist[--binding->free_count]; spin_unlock_bh(&binding->freelist_lock); =20 - niov->desc.pp_magic =3D 0; - niov->desc.pp =3D NULL; - atomic_long_set(&niov->desc.pp_ref_count, 0); - - return niov; + return count; } =20 void net_devmem_free_dmabuf(struct net_iov *niov) @@ -428,20 +428,35 @@ int mp_dmabuf_devmem_init(struct page_pool *pool) netmem_ref mp_dmabuf_devmem_alloc_netmems(struct page_pool *pool, gfp_t gf= p) { struct net_devmem_dmabuf_binding *binding =3D pool->mp_priv; - struct net_iov *niov; - netmem_ref netmem; + netmem_ref *netmems =3D pool->alloc.cache; + unsigned int allocated, i; + + if (WARN_ON_ONCE(pool->alloc.count)) + return 0; =20 - niov =3D net_devmem_alloc_dmabuf(binding); - if (!niov) + allocated =3D net_devmem_alloc_dmabuf_bulk(binding, netmems, + PP_ALLOC_CACHE_REFILL); + if (unlikely(!allocated)) return 0; =20 - netmem =3D net_iov_to_netmem(niov); + for (i =3D 0; i < allocated; i++) { + struct net_iov *niov =3D netmem_to_net_iov(netmems[i]); =20 - page_pool_set_pp_info(pool, netmem); + niov->desc.pp_magic =3D 0; + niov->desc.pp =3D NULL; + atomic_long_set(&niov->desc.pp_ref_count, 0); + + page_pool_set_pp_info(pool, netmems[i]); + + pool->pages_state_hold_cnt++; + trace_page_pool_state_hold(pool, netmems[i], + pool->pages_state_hold_cnt); + } =20 - pool->pages_state_hold_cnt++; - trace_page_pool_state_hold(pool, netmem, pool->pages_state_hold_cnt); - return netmem; + /* Return the last one, the rest stay in the page_pool cache. */ + allocated--; + pool->alloc.count =3D allocated; + return netmems[allocated]; } =20 void mp_dmabuf_devmem_destroy(struct page_pool *pool) diff --git a/net/core/devmem.h b/net/core/devmem.h index 6ed16b4b4687..f71d9a2bcb3d 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -131,8 +131,6 @@ net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_= binding *binding) void net_devmem_get_net_iov(struct net_iov *niov); void net_devmem_put_net_iov(struct net_iov *niov); =20 -struct net_iov * -net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding); void net_devmem_free_dmabuf(struct net_iov *ppiov); =20 =20 @@ -189,12 +187,6 @@ net_devmem_bind_dmabuf_to_queue(struct net_device *dev= , u32 rxq_idx, return -EOPNOTSUPP; } =20 -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) { } --=20 2.53.0-Meta