[PATCH net-next 09/12] page_pool: add a couple of netmem counterparts

Alexander Lobakin posted 12 patches 1 year ago
[PATCH net-next 09/12] page_pool: add a couple of netmem counterparts
Posted by Alexander Lobakin 1 year ago
Add the following Page Pool netmem wrappers to be able to implement
an MP-agnostic driver:

* page_pool{,_dev}_alloc_best_fit_netmem()

Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around
the new helper (as a page is always a netmem, but not vice versa).
'page_pool_alloc_netmem' is already busy, hence '_best_fit' (which
also says what the helper tries to do).

* page_pool_dma_sync_for_cpu_netmem()

Same as page_pool_dma_sync_for_cpu(). Performs DMA sync only if
the netmem comes from the host.

Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
---
 include/net/page_pool/helpers.h | 46 ++++++++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/page_pool/helpers.h b/include/net/page_pool/helpers.h
index 26caa2c20912..d75d10678958 100644
--- a/include/net/page_pool/helpers.h
+++ b/include/net/page_pool/helpers.h
@@ -115,22 +115,22 @@ static inline struct page *page_pool_dev_alloc_frag(struct page_pool *pool,
 	return page_pool_alloc_frag(pool, offset, size, gfp);
 }
 
-static inline struct page *page_pool_alloc(struct page_pool *pool,
-					   unsigned int *offset,
-					   unsigned int *size, gfp_t gfp)
+static inline netmem_ref
+page_pool_alloc_best_fit_netmem(struct page_pool *pool, unsigned int *offset,
+				unsigned int *size, gfp_t gfp)
 {
 	unsigned int max_size = PAGE_SIZE << pool->p.order;
-	struct page *page;
+	netmem_ref netmem;
 
 	if ((*size << 1) > max_size) {
 		*size = max_size;
 		*offset = 0;
-		return page_pool_alloc_pages(pool, gfp);
+		return page_pool_alloc_netmem(pool, gfp);
 	}
 
-	page = page_pool_alloc_frag(pool, offset, *size, gfp);
-	if (unlikely(!page))
-		return NULL;
+	netmem = page_pool_alloc_frag_netmem(pool, offset, *size, gfp);
+	if (unlikely(!netmem))
+		return 0;
 
 	/* There is very likely not enough space for another fragment, so append
 	 * the remaining size to the current fragment to avoid truesize
@@ -141,7 +141,25 @@ static inline struct page *page_pool_alloc(struct page_pool *pool,
 		pool->frag_offset = max_size;
 	}
 
-	return page;
+	return netmem;
+}
+
+static inline netmem_ref
+page_pool_dev_alloc_best_fit_netmem(struct page_pool *pool,
+				    unsigned int *offset,
+				    unsigned int *size)
+{
+	gfp_t gfp = GFP_ATOMIC | __GFP_NOWARN;
+
+	return page_pool_alloc_best_fit_netmem(pool, offset, size, gfp);
+}
+
+static inline struct page *page_pool_alloc(struct page_pool *pool,
+					   unsigned int *offset,
+					   unsigned int *size, gfp_t gfp)
+{
+	return netmem_to_page(page_pool_alloc_best_fit_netmem(pool, offset,
+							      size, gfp));
 }
 
 /**
@@ -440,6 +458,16 @@ static inline void page_pool_dma_sync_for_cpu(const struct page_pool *pool,
 				      page_pool_get_dma_dir(pool));
 }
 
+static inline void
+page_pool_dma_sync_for_cpu_netmem(const struct page_pool *pool,
+				  netmem_ref netmem, u32 offset,
+				  u32 dma_sync_size)
+{
+	if (!netmem_is_net_iov(netmem))
+		page_pool_dma_sync_for_cpu(pool, netmem_to_page(netmem),
+					   offset, dma_sync_size);
+}
+
 static inline bool page_pool_put(struct page_pool *pool)
 {
 	return refcount_dec_and_test(&pool->user_cnt);
-- 
2.47.1
Re: [PATCH net-next 09/12] page_pool: add a couple of netmem counterparts
Posted by Mina Almasry 1 year ago
On Wed, Dec 11, 2024 at 9:31 AM Alexander Lobakin
<aleksander.lobakin@intel.com> wrote:
>
> Add the following Page Pool netmem wrappers to be able to implement
> an MP-agnostic driver:
>

Sorry, we raced a bit here. Jakub merged my "page_pool_alloc_netmem",
which does similar to what this patch does.

> * page_pool{,_dev}_alloc_best_fit_netmem()
>
> Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around
> the new helper (as a page is always a netmem, but not vice versa).
> 'page_pool_alloc_netmem' is already busy, hence '_best_fit' (which
> also says what the helper tries to do).
>

I freed the page_pool_alloc_netmem name by doing a rename, and now
page_pool_alloc_netmem is the netmem counterpart to page_pool_alloc. I
did not however add a page_pool_dev_alloc equivalent.

> * page_pool_dma_sync_for_cpu_netmem()
>
> Same as page_pool_dma_sync_for_cpu(). Performs DMA sync only if
> the netmem comes from the host.
>

My series also adds page_pool_dma_sync_netmem_for_cpu, which should be
the same as your page_pool_dma_sync_for_cpu_netmem.
Re: [PATCH net-next 09/12] page_pool: add a couple of netmem counterparts
Posted by Alexander Lobakin 12 months ago
From: Mina Almasry <almasrymina@google.com>
Date: Fri, 13 Dec 2024 11:13:33 -0800

> On Wed, Dec 11, 2024 at 9:31 AM Alexander Lobakin
> <aleksander.lobakin@intel.com> wrote:
>>
>> Add the following Page Pool netmem wrappers to be able to implement
>> an MP-agnostic driver:
>>
> 
> Sorry, we raced a bit here. Jakub merged my "page_pool_alloc_netmem",
> which does similar to what this patch does.
> 
>> * page_pool{,_dev}_alloc_best_fit_netmem()
>>
>> Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around
>> the new helper (as a page is always a netmem, but not vice versa).
>> 'page_pool_alloc_netmem' is already busy, hence '_best_fit' (which
>> also says what the helper tries to do).
>>
> 
> I freed the page_pool_alloc_netmem name by doing a rename, and now
> page_pool_alloc_netmem is the netmem counterpart to page_pool_alloc. I
> did not however add a page_pool_dev_alloc equivalent.
> 
>> * page_pool_dma_sync_for_cpu_netmem()
>>
>> Same as page_pool_dma_sync_for_cpu(). Performs DMA sync only if
>> the netmem comes from the host.
>>
> 
> My series also adds page_pool_dma_sync_netmem_for_cpu, which should be
> the same as your page_pool_dma_sync_for_cpu_netmem.

Yep, I saw your changes, rebasing soon.

Thanks,
Olek