[PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()

Byungchul Park posted 1 patch 5 days, 19 hours ago
include/net/netmem.h   | 66 +++++++++++++++++++++---------------------
net/core/netmem_priv.h | 16 +++++-----
2 files changed, 41 insertions(+), 41 deletions(-)
[PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Byungchul Park 5 days, 19 hours ago
Changes from RFC v2:
	1. Add a Reviewed-by tag (Thanks to Mina)
	2. Rebase on main branch as of Sep 22

Changes from RFC:
	1. Optimize the implementation of netmem_to_nmdesc to use less
	   instructions (feedbacked by Pavel)

--->8---
From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
From: Byungchul Park <byungchul@sk.com>
Date: Tue, 29 Jul 2025 19:34:12 +0900
Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()

Now that we have struct netmem_desc, it'd better access the pp fields
via struct netmem_desc rather than struct net_iov.

Introduce netmem_to_nmdesc() for safely converting netmem_ref to
netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.

While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
used instead.

Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Byungchul Park <byungchul@sk.com>
Reviewed-by: Mina Almasry <almasrymina@google.com>
---
 include/net/netmem.h   | 66 +++++++++++++++++++++---------------------
 net/core/netmem_priv.h | 16 +++++-----
 2 files changed, 41 insertions(+), 41 deletions(-)

diff --git a/include/net/netmem.h b/include/net/netmem.h
index f7dacc9e75fd..651e2c62d1dd 100644
--- a/include/net/netmem.h
+++ b/include/net/netmem.h
@@ -247,6 +247,23 @@ static inline unsigned long netmem_pfn_trace(netmem_ref netmem)
 	return page_to_pfn(netmem_to_page(netmem));
 }
 
+/* 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.
+ */
+#define __pp_page_to_nmdesc(p)	(_Generic((p),				\
+	const struct page * :	(const struct netmem_desc *)(p),	\
+	struct page * :		(struct netmem_desc *)(p)))
+
+/* CAUTION: Check if the page is a pp page before calling this helper or
+ * know it's a pp page.
+ */
+#define pp_page_to_nmdesc(p)						\
+({									\
+	DEBUG_NET_WARN_ON_ONCE(!page_pool_page_is_pp(p));		\
+	__pp_page_to_nmdesc(p);						\
+})
+
 /**
  * __netmem_to_nmdesc - unsafely get pointer to the &netmem_desc backing
  * @netmem
@@ -265,42 +282,25 @@ static inline struct netmem_desc *__netmem_to_nmdesc(netmem_ref netmem)
 	return (__force struct netmem_desc *)netmem;
 }
 
-/* __netmem_clear_lsb - convert netmem_ref to struct net_iov * for access to
- * common fields.
- * @netmem: netmem reference to extract as net_iov.
- *
- * All the sub types of netmem_ref (page, net_iov) have the same pp, pp_magic,
- * dma_addr, and pp_ref_count fields at the same offsets. Thus, we can access
- * these fields without a type check to make sure that the underlying mem is
- * net_iov or page.
+/* netmem_to_nmdesc - convert netmem_ref to struct netmem_desc * for
+ * access to common fields.
+ * @netmem: netmem reference to get netmem_desc.
  *
- * The resulting value of this function can only be used to access the fields
- * that are NET_IOV_ASSERT_OFFSET'd. Accessing any other fields will result in
- * undefined behavior.
+ * All the sub types of netmem_ref (netmem_desc, net_iov) have the same
+ * pp, pp_magic, dma_addr, and pp_ref_count fields via netmem_desc.
  *
- * Return: the netmem_ref cast to net_iov* regardless of its underlying type.
+ * Return: the pointer to struct netmem_desc * regardless of its
+ * underlying type.
  */
-static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
+static inline struct netmem_desc *netmem_to_nmdesc(netmem_ref netmem)
 {
-	return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV);
-}
+	void *p = (void *)((__force unsigned long)netmem & ~NET_IOV);
 
-/* 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.
- */
-#define __pp_page_to_nmdesc(p)	(_Generic((p),				\
-	const struct page * :	(const struct netmem_desc *)(p),	\
-	struct page * :		(struct netmem_desc *)(p)))
+	if (netmem_is_net_iov(netmem))
+		return &((struct net_iov *)p)->desc;
 
-/* CAUTION: Check if the page is a pp page before calling this helper or
- * know it's a pp page.
- */
-#define pp_page_to_nmdesc(p)						\
-({									\
-	DEBUG_NET_WARN_ON_ONCE(!page_pool_page_is_pp(p));		\
-	__pp_page_to_nmdesc(p);						\
-})
+	return __pp_page_to_nmdesc((struct page *)p);
+}
 
 /**
  * __netmem_get_pp - unsafely get pointer to the &page_pool backing @netmem
@@ -320,12 +320,12 @@ static inline struct page_pool *__netmem_get_pp(netmem_ref netmem)
 
 static inline struct page_pool *netmem_get_pp(netmem_ref netmem)
 {
-	return __netmem_clear_lsb(netmem)->pp;
+	return netmem_to_nmdesc(netmem)->pp;
 }
 
 static inline atomic_long_t *netmem_get_pp_ref_count_ref(netmem_ref netmem)
 {
-	return &__netmem_clear_lsb(netmem)->pp_ref_count;
+	return &netmem_to_nmdesc(netmem)->pp_ref_count;
 }
 
 static inline bool netmem_is_pref_nid(netmem_ref netmem, int pref_nid)
@@ -390,7 +390,7 @@ static inline bool netmem_is_pfmemalloc(netmem_ref netmem)
 
 static inline unsigned long netmem_get_dma_addr(netmem_ref netmem)
 {
-	return __netmem_clear_lsb(netmem)->dma_addr;
+	return netmem_to_nmdesc(netmem)->dma_addr;
 }
 
 void get_netmem(netmem_ref netmem);
diff --git a/net/core/netmem_priv.h b/net/core/netmem_priv.h
index cd95394399b4..23175cb2bd86 100644
--- a/net/core/netmem_priv.h
+++ b/net/core/netmem_priv.h
@@ -5,19 +5,19 @@
 
 static inline unsigned long netmem_get_pp_magic(netmem_ref netmem)
 {
-	return __netmem_clear_lsb(netmem)->pp_magic & ~PP_DMA_INDEX_MASK;
+	return netmem_to_nmdesc(netmem)->pp_magic & ~PP_DMA_INDEX_MASK;
 }
 
 static inline void netmem_or_pp_magic(netmem_ref netmem, unsigned long pp_magic)
 {
-	__netmem_clear_lsb(netmem)->pp_magic |= pp_magic;
+	netmem_to_nmdesc(netmem)->pp_magic |= pp_magic;
 }
 
 static inline void netmem_clear_pp_magic(netmem_ref netmem)
 {
-	WARN_ON_ONCE(__netmem_clear_lsb(netmem)->pp_magic & PP_DMA_INDEX_MASK);
+	WARN_ON_ONCE(netmem_to_nmdesc(netmem)->pp_magic & PP_DMA_INDEX_MASK);
 
-	__netmem_clear_lsb(netmem)->pp_magic = 0;
+	netmem_to_nmdesc(netmem)->pp_magic = 0;
 }
 
 static inline bool netmem_is_pp(netmem_ref netmem)
@@ -27,13 +27,13 @@ static inline bool netmem_is_pp(netmem_ref netmem)
 
 static inline void netmem_set_pp(netmem_ref netmem, struct page_pool *pool)
 {
-	__netmem_clear_lsb(netmem)->pp = pool;
+	netmem_to_nmdesc(netmem)->pp = pool;
 }
 
 static inline void netmem_set_dma_addr(netmem_ref netmem,
 				       unsigned long dma_addr)
 {
-	__netmem_clear_lsb(netmem)->dma_addr = dma_addr;
+	netmem_to_nmdesc(netmem)->dma_addr = dma_addr;
 }
 
 static inline unsigned long netmem_get_dma_index(netmem_ref netmem)
@@ -43,7 +43,7 @@ static inline unsigned long netmem_get_dma_index(netmem_ref netmem)
 	if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
 		return 0;
 
-	magic = __netmem_clear_lsb(netmem)->pp_magic;
+	magic = netmem_to_nmdesc(netmem)->pp_magic;
 
 	return (magic & PP_DMA_INDEX_MASK) >> PP_DMA_INDEX_SHIFT;
 }
@@ -57,6 +57,6 @@ static inline void netmem_set_dma_index(netmem_ref netmem,
 		return;
 
 	magic = netmem_get_pp_magic(netmem) | (id << PP_DMA_INDEX_SHIFT);
-	__netmem_clear_lsb(netmem)->pp_magic = magic;
+	netmem_to_nmdesc(netmem)->pp_magic = magic;
 }
 #endif

base-commit: 312e6f7676e63bbb9b81e5c68e580a9f776cc6f0
-- 
2.17.1
Re: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Simon Horman 5 days, 8 hours ago
On Fri, Sep 26, 2025 at 12:54:23PM +0900, Byungchul Park wrote:
> Changes from RFC v2:
> 	1. Add a Reviewed-by tag (Thanks to Mina)
> 	2. Rebase on main branch as of Sep 22
> 
> Changes from RFC:
> 	1. Optimize the implementation of netmem_to_nmdesc to use less
> 	   instructions (feedbacked by Pavel)
> 
> --->8---
> >From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
> From: Byungchul Park <byungchul@sk.com>
> Date: Tue, 29 Jul 2025 19:34:12 +0900
> Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
> 
> Now that we have struct netmem_desc, it'd better access the pp fields
> via struct netmem_desc rather than struct net_iov.
> 
> Introduce netmem_to_nmdesc() for safely converting netmem_ref to
> netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.
> 
> While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
> used instead.
> 
> Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
> Signed-off-by: Byungchul Park <byungchul@sk.com>
> Reviewed-by: Mina Almasry <almasrymina@google.com>

Hi Byungchul,

Some process issues from my side.

1. The revision information, up to including the '--->8---' line above
   should be below the scissors ('---') below.

   This is so that it is available to reviewers, appears in mailing
   list archives, and so on. But is not included in git history.

2. Starting the patch description with a 'From: ' line is fine.
   But 'Date:" and 'Subject:' lines don't belong there.

   Perhaps 1 and 2 are some sort of tooling error?

3. Unfortunately while this patch is targeted at net-next,
   it doesn't apply cleanly there.

When you repost, be sure to observe the 24h rule.

Link: https://docs.kernel.org/process/maintainer-netdev.html

-- 
pw-bot: changes-requested

...
Re: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Byungchul Park 2 days, 21 hours ago
On Fri, Sep 26, 2025 at 04:18:45PM +0100, Simon Horman wrote:
> On Fri, Sep 26, 2025 at 12:54:23PM +0900, Byungchul Park wrote:
> > Changes from RFC v2:
> >       1. Add a Reviewed-by tag (Thanks to Mina)
> >       2. Rebase on main branch as of Sep 22
> >
> > Changes from RFC:
> >       1. Optimize the implementation of netmem_to_nmdesc to use less
> >          instructions (feedbacked by Pavel)
> >
> > --->8---
> > >From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
> > From: Byungchul Park <byungchul@sk.com>
> > Date: Tue, 29 Jul 2025 19:34:12 +0900
> > Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
> >
> > Now that we have struct netmem_desc, it'd better access the pp fields
> > via struct netmem_desc rather than struct net_iov.
> >
> > Introduce netmem_to_nmdesc() for safely converting netmem_ref to
> > netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.
> >
> > While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
> > used instead.
> >
> > Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
> > Signed-off-by: Byungchul Park <byungchul@sk.com>
> > Reviewed-by: Mina Almasry <almasrymina@google.com>
> 
> Hi Byungchul,
> 
> Some process issues from my side.
> 
> 1. The revision information, up to including the '--->8---' line above
>    should be below the scissors ('---') below.
> 
>    This is so that it is available to reviewers, appears in mailing
>    list archives, and so on. But is not included in git history.

Ah yes.  Thank you.  Lemme check.

> 2. Starting the patch description with a 'From: ' line is fine.
>    But 'Date:" and 'Subject:' lines don't belong there.
> 
>    Perhaps 1 and 2 are some sort of tooling error?
> 
> 3. Unfortunately while this patch is targeted at net-next,
>    it doesn't apply cleanly there.

I don't understand why.  Now I just rebased on the latest 'main' and it
works well.  What should I check else?

> When you repost, be sure to observe the 24h rule.

Thanks!

	Byungchul

> Link: https://docs.kernel.org/process/maintainer-netdev.html
> 
> --
> pw-bot: changes-requested
> 
> ...
Re: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Mina Almasry 2 days, 6 hours ago
On Sun, Sep 28, 2025 at 6:46 PM Byungchul Park <byungchul@sk.com> wrote:
>
> On Fri, Sep 26, 2025 at 04:18:45PM +0100, Simon Horman wrote:
> > On Fri, Sep 26, 2025 at 12:54:23PM +0900, Byungchul Park wrote:
> > > Changes from RFC v2:
> > >       1. Add a Reviewed-by tag (Thanks to Mina)
> > >       2. Rebase on main branch as of Sep 22
> > >
> > > Changes from RFC:
> > >       1. Optimize the implementation of netmem_to_nmdesc to use less
> > >          instructions (feedbacked by Pavel)
> > >
> > > --->8---
> > > >From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
> > > From: Byungchul Park <byungchul@sk.com>
> > > Date: Tue, 29 Jul 2025 19:34:12 +0900
> > > Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
> > >
> > > Now that we have struct netmem_desc, it'd better access the pp fields
> > > via struct netmem_desc rather than struct net_iov.
> > >
> > > Introduce netmem_to_nmdesc() for safely converting netmem_ref to
> > > netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.
> > >
> > > While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
> > > used instead.
> > >
> > > Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
> > > Signed-off-by: Byungchul Park <byungchul@sk.com>
> > > Reviewed-by: Mina Almasry <almasrymina@google.com>
> >
> > Hi Byungchul,
> >
> > Some process issues from my side.
> >
> > 1. The revision information, up to including the '--->8---' line above
> >    should be below the scissors ('---') below.
> >
> >    This is so that it is available to reviewers, appears in mailing
> >    list archives, and so on. But is not included in git history.
>
> Ah yes.  Thank you.  Lemme check.
>
> > 2. Starting the patch description with a 'From: ' line is fine.
> >    But 'Date:" and 'Subject:' lines don't belong there.
> >
> >    Perhaps 1 and 2 are some sort of tooling error?
> >
> > 3. Unfortunately while this patch is targeted at net-next,
> >    it doesn't apply cleanly there.
>
> I don't understand why.  Now I just rebased on the latest 'main' and it
> works well.  What should I check else?
>
> > When you repost, be sure to observe the 24h rule.
>
> Thanks!
>

Additionally net-next is closed:

https://lore.kernel.org/netdev/20250928212617.7e0cbfe4@kernel.org/

Changes targetting net-next need to be sent after the re-open. RFC is
always welcome.



-- 
Thanks,
Mina
Re: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Byungchul Park 2 days, 15 hours ago
On Mon, Sep 29, 2025 at 10:46:19AM +0900, Byungchul Park wrote:
> On Fri, Sep 26, 2025 at 04:18:45PM +0100, Simon Horman wrote:
> > On Fri, Sep 26, 2025 at 12:54:23PM +0900, Byungchul Park wrote:
> > > Changes from RFC v2:
> > >       1. Add a Reviewed-by tag (Thanks to Mina)
> > >       2. Rebase on main branch as of Sep 22
> > >
> > > Changes from RFC:
> > >       1. Optimize the implementation of netmem_to_nmdesc to use less
> > >          instructions (feedbacked by Pavel)
> > >
> > > --->8---
> > > >From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
> > > From: Byungchul Park <byungchul@sk.com>
> > > Date: Tue, 29 Jul 2025 19:34:12 +0900
> > > Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
> > >
> > > Now that we have struct netmem_desc, it'd better access the pp fields
> > > via struct netmem_desc rather than struct net_iov.
> > >
> > > Introduce netmem_to_nmdesc() for safely converting netmem_ref to
> > > netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.
> > >
> > > While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
> > > used instead.
> > >
> > > Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
> > > Signed-off-by: Byungchul Park <byungchul@sk.com>
> > > Reviewed-by: Mina Almasry <almasrymina@google.com>
> > 
> > Hi Byungchul,
> > 
> > Some process issues from my side.
> > 
> > 1. The revision information, up to including the '--->8---' line above
> >    should be below the scissors ('---') below.
> > 
> >    This is so that it is available to reviewers, appears in mailing
> >    list archives, and so on. But is not included in git history.
> 
> Ah yes.  Thank you.  Lemme check.
> 
> > 2. Starting the patch description with a 'From: ' line is fine.
> >    But 'Date:" and 'Subject:' lines don't belong there.
> > 
> >    Perhaps 1 and 2 are some sort of tooling error?
> > 
> > 3. Unfortunately while this patch is targeted at net-next,
> >    it doesn't apply cleanly there.
> 
> I don't understand why.  Now I just rebased on the latest 'main' and it
> works well.  What should I check else?

I think 1 and 2 ends in 3.  I will fix it and resend it after the merge
window.

	Byungchul

> > When you repost, be sure to observe the 24h rule.
> 
> Thanks!
> 
> 	Byungchul
> 
> > Link: https://docs.kernel.org/process/maintainer-netdev.html
> > 
> > --
> > pw-bot: changes-requested
> > 
> > ...
Re: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
Posted by Simon Horman 2 days, 9 hours ago
On Mon, Sep 29, 2025 at 04:48:40PM +0900, Byungchul Park wrote:
> On Mon, Sep 29, 2025 at 10:46:19AM +0900, Byungchul Park wrote:
> > On Fri, Sep 26, 2025 at 04:18:45PM +0100, Simon Horman wrote:
> > > On Fri, Sep 26, 2025 at 12:54:23PM +0900, Byungchul Park wrote:
> > > > Changes from RFC v2:
> > > >       1. Add a Reviewed-by tag (Thanks to Mina)
> > > >       2. Rebase on main branch as of Sep 22
> > > >
> > > > Changes from RFC:
> > > >       1. Optimize the implementation of netmem_to_nmdesc to use less
> > > >          instructions (feedbacked by Pavel)
> > > >
> > > > --->8---
> > > > >From 01d23fc4b20c369a2ecf29dc92319d55a4e63aa2 Mon Sep 17 00:00:00 2001
> > > > From: Byungchul Park <byungchul@sk.com>
> > > > Date: Tue, 29 Jul 2025 19:34:12 +0900
> > > > Subject: [PATCH net-next v3] netmem: replace __netmem_clear_lsb() with netmem_to_nmdesc()
> > > >
> > > > Now that we have struct netmem_desc, it'd better access the pp fields
> > > > via struct netmem_desc rather than struct net_iov.
> > > >
> > > > Introduce netmem_to_nmdesc() for safely converting netmem_ref to
> > > > netmem_desc regardless of the type underneath e.i. netmem_desc, net_iov.
> > > >
> > > > While at it, remove __netmem_clear_lsb() and make netmem_to_nmdesc()
> > > > used instead.
> > > >
> > > > Suggested-by: Pavel Begunkov <asml.silence@gmail.com>
> > > > Signed-off-by: Byungchul Park <byungchul@sk.com>
> > > > Reviewed-by: Mina Almasry <almasrymina@google.com>
> > > 
> > > Hi Byungchul,
> > > 
> > > Some process issues from my side.
> > > 
> > > 1. The revision information, up to including the '--->8---' line above
> > >    should be below the scissors ('---') below.
> > > 
> > >    This is so that it is available to reviewers, appears in mailing
> > >    list archives, and so on. But is not included in git history.
> > 
> > Ah yes.  Thank you.  Lemme check.
> > 
> > > 2. Starting the patch description with a 'From: ' line is fine.
> > >    But 'Date:" and 'Subject:' lines don't belong there.
> > > 
> > >    Perhaps 1 and 2 are some sort of tooling error?
> > > 
> > > 3. Unfortunately while this patch is targeted at net-next,
> > >    it doesn't apply cleanly there.
> > 
> > I don't understand why.  Now I just rebased on the latest 'main' and it
> > works well.  What should I check else?
> 
> I think 1 and 2 ends in 3.  I will fix it and resend it after the merge
> window.

Great, thanks!