Re: [PATCH net-next v10 02/12] netmem: use netmem_desc instead of page to access ->pp in __netmem_get_pp()

From
Mina Almasry <hidden>
Date
2025-07-14 19:38:02
Also in
bpf, imx, intel-wired-lan, linux-mediatek, linux-mm, linux-rdma, lkml, netdev
Reply to
Byungchul Park, 2025-07-14
On Mon, Jul 14, 2025 at 5:01 AM Byungchul Park [off-list ref] wrote:
quoted hunk ↗ jump to hunk
To eliminate the use of the page pool fields in struct page, the page
pool code should use netmem descriptor and APIs instead.

However, __netmem_get_pp() still accesses ->pp via struct page.  So
change it to use struct netmem_desc instead, since ->pp no longer will
be available in struct page.

While at it, add a helper, pp_page_to_nmdesc(), that can be used to
extract netmem_desc from page only if it's pp page.  For now that
netmem_desc overlays on page, it can be achieved by just casting.

Signed-off-by: Byungchul Park <byungchul@sk.com>
---
 include/net/netmem.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/include/net/netmem.h b/include/net/netmem.h
index 535cf17b9134..2b8a7b51ac99 100644
--- a/include/net/netmem.h
+++ b/include/net/netmem.h
@@ -267,6 +267,17 @@ static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
        return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV);
 }

+static inline struct netmem_desc *pp_page_to_nmdesc(struct page *page)
+{
+       DEBUG_NET_WARN_ON_ONCE(!page_pool_page_is_pp(page));
+
+       /* XXX: How to extract netmem_desc from page must be changed,
+        * once netmem_desc no longer overlays on page and will be
+        * allocated through slab.
+        */
+       return (struct netmem_desc *)page;
+}
+
Same thing. Do not create a generic looking pp_page_to_nmdesc helper
which does not check that the page is the correct type. The
DEBUG_NET... is not good enough.

You don't need to add a generic helper here. There is only one call
site. Open code this in the callsite. The one callsite is marked as
unsafe, only called by code that knows that the netmem is specifically
a pp page. Open code this in the unsafe callsite, instead of creating
a generic looking unsafe helper and not even documenting it's unsafe.
quoted hunk ↗ jump to hunk
 /**
  * __netmem_get_pp - unsafely get pointer to the &page_pool backing @netmem
  * @netmem: netmem reference to get the pointer from
@@ -280,7 +291,7 @@ static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
  */
 static inline struct page_pool *__netmem_get_pp(netmem_ref netmem)
 {
-       return __netmem_to_page(netmem)->pp;
+       return pp_page_to_nmdesc(__netmem_to_page(netmem))->pp;
 }
This makes me very sad. Casting from netmem -> page -> nmdesc...

Instead, we should be able to go from netmem directly to nmdesc. I
would suggest rename __netmem_clear_lsb to netmem_to_nmdesc and have
it return netmem_desc instead of net_iov. Then use it here.

We could have an unsafe version of netmem_to_nmdesc which converts the
netmem to netmem_desc without clearing the lsb and mark it unsafe.

-- 
Thanks,
Mina
Thread (27 messages) 27 messages, 3 authors, last reply 2025-07-17
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help