skbuff: Optimize SKB coalescing for page pool
Subsystems (7)
v7 cover skbuff: Optimize SKB coalescing for page pool
The combination of the following condition was excluded from skb coalescing:
from->pp_recycle = 1from->cloned = 1to->pp_recycle = 1
With page pool in use, this combination can be quite common(ex. NetworkMananger may lead to the additional packet_type being registered, thus the cloning). In scenarios with a higher number of small packets, it can significantly affect the success rate of coalescing.
This patchset aims to optimize this scenario and enable coalescing of this particular combination. That also involves supporting multiple users referencing the same fragment of a pp page to accomondate the need to increment the "from" SKB page's pp page reference count.
Changes from v6:
- provide sufficient description for the first patch
- rename the utility function for checking pp page
Liang Chen (4):
page_pool: transition to reference count management after page
draining
page_pool: halve BIAS_MAX for multiple user references of a fragment
skbuff: Add a function to check if a page belongs to page_pool
skbuff: Optimization of SKB coalescing for page pool
.../net/ethernet/mellanox/mlx5/core/en_rx.c | 4 +- include/linux/mm_types.h | 2 +- include/net/page_pool/helpers.h | 50 +++++++++++-------- include/net/page_pool/types.h | 6 +-- net/core/page_pool.c | 14 +++--- net/core/skbuff.c | 48 +++++++++++++----- 6 files changed, 78 insertions(+), 46 deletions(-)
Signature · 1 line
2.31.1
PATCH v7 1/4 page_pool: transition to reference count management after page draining
To support multiple users referencing the same fragment, 'pp_frag_count' is renamed to 'pp_ref_count', transitioning pp pages from fragment management to reference count management after draining based on the suggestion from [1].
The idea is that the concept of fragmenting exists before the page is drained, and all related functions retain their current names. However, once the page is drained, its management shifts to being governed by 'pp_ref_count'. Therefore, all functions associated with that lifecycle stage of a pp page are renamed.
[1]
http://lore.kernel.org/netdev/[email protected]
--- .../net/ethernet/mellanox/mlx5/core/en_rx.c | 4 +- include/linux/mm_types.h | 2 +- include/net/page_pool/helpers.h | 45 ++++++++++--------- include/net/page_pool/types.h | 6 +-- net/core/page_pool.c | 12 ++--- 5 files changed, 37 insertions(+), 32 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +2 -2
@@ -298,8 +298,8 @@ static void mlx5e_page_release_fragmented(struct mlx5e_rq *rq,u16 drain_count =MLX5E_PAGECNT_BIAS_MAX - frag_page->frags;struct page *page = frag_page->page; -if (page_pool_defrag_page (page, drain_count) ==0 ) -page_pool_put_defragged_page (rq->page_pool, page,-1 , true); +if (page_pool_unref_page (page, drain_count) ==0 ) +page_pool_put_unrefed_page (rq->page_pool, page,-1 , true); } static inline int mlx5e_get_rx_frag(struct mlx5e_rq *rq,
include/linux/mm_types.h +1 -1
@@ -125,7 +125,7 @@ struct page {struct page_pool *pp;unsigned long _pp_mapping_pad;unsigned long dma_addr; -atomic_long_t pp_frag_count; +atomic_long_t pp_ref_count; };struct {/* Tail pages of compound page */ unsigned long compound_head;/* Bit zero is set */
include/net/page_pool/helpers.h +25 -20
111 lines not on this page · raw message
include/net/page_pool/types.h +3 -3
@@ -224,9 +224,9 @@ static inline void page_pool_put_page_bulk(struct page_pool *pool, void **data, }#endif -void page_pool_put_defragged_page (struct page_pool *pool,struct page *page, -unsigned int dma_sync_size, -bool allow_direct); +void page_pool_put_unrefed_page (struct page_pool *pool,struct page *page, +unsigned int dma_sync_size, +bool allow_direct);static inline bool is_page_pool_compiled_in (void ) {
net/core/page_pool.c +6 -6
@@ -650,8 +650,8 @@ __page_pool_put_page(struct page_pool *pool, struct page *page,return NULL ; } -void page_pool_put_defragged_page (struct page_pool *pool,struct page *page, -unsigned int dma_sync_size,bool allow_direct) +void page_pool_put_unrefed_page (struct page_pool *pool,struct page *page, +unsigned int dma_sync_size,bool allow_direct) { page =__page_pool_put_page (pool, page, dma_sync_size, allow_direct);if (page && !page_pool_recycle_in_ring (pool, page)) {
@@ -660,7 +660,7 @@ void page_pool_put_defragged_page(struct page_pool *pool, struct page *page,page_pool_return_page (pool, page); } } -EXPORT_SYMBOL (page_pool_put_defragged_page); +EXPORT_SYMBOL (page_pool_put_unrefed_page);/** * page_pool_put_page_bulk() - release references on multiple pages
@@ -687,7 +687,7 @@ void page_pool_put_page_bulk(struct page_pool *pool, void **data,struct page *page =virt_to_head_page (data[i]);/* It is not the last user for the page frag case */ -if (!page_pool_is_last_frag (page)) +if (!page_pool_is_last_ref (page))continue ; page =__page_pool_put_page (pool, page,-1 , false);
@@ -729,7 +729,7 @@ static struct page *page_pool_drain_frag(struct page_pool *pool,long drain_count =BIAS_MAX - pool->frag_users;/* Some user is still using the page frag */ -if (likely (page_pool_defrag_page (page, drain_count))) +if (likely (page_pool_unref_page (page, drain_count)))return NULL ;if (page_ref_count (page) ==1 && !page_is_pfmemalloc (page)) {
@@ -750,7 +750,7 @@ static void page_pool_free_frag(struct page_pool *pool) pool->frag_page =NULL ; -if (!page ||page_pool_defrag_page (page, drain_count)) +if (!page ||page_pool_unref_page (page, drain_count))return ;page_pool_return_page (pool, page);
Signature · 1 line
2.31.1
PATCH v7 2/4 page_pool: halve BIAS_MAX for multiple user references of a fragment
In reply to Liang ChenReferring to patch [1], in order to support multiple users referencing the same fragment and prevent overflow from pp_ref_count growing, the initial value of pp_ref_count is halved, leaving room for pp_ref_count to increment before the page is drained.
[1]
https://lore.kernel.org/all/[email protected]/
--- net/core/page_pool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/page_pool.c +1 -1
@@ -26,7 +26,7 @@#define DEFER_TIME (msecs_to_jiffies(1000))#define DEFER_WARN_INTERVAL (60 * HZ) -#define BIAS_MAX LONG_MAX +#define BIAS_MAX (LONG_MAX >> 1)#ifdef CONFIG_PAGE_POOL_STATS /* alloc_stat_inc is intended to be used in softirq context */
Signature · 1 line
2.31.1
PATCH v7 3/4 skbuff: Add a function to check if a page belongs to page_pool
In reply to Liang ChenWrap code for checking if a page is a page_pool page into a function for better readability and ease of reuse.
--- net/core/skbuff.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/core/skbuff.c +6 -1
@@ -890,6 +890,11 @@ static void skb_clone_fraglist(struct sk_buff *skb)skb_get (list); } +static bool is_pp_page (struct page *page) +{ +return (page->pp_magic & ~0x3UL ) ==PP_SIGNATURE ; +} +#if IS_ENABLED (CONFIG_PAGE_POOL )bool napi_pp_put_page (struct page *page,bool napi_safe) {
@@ -905,7 +910,7 @@ bool napi_pp_put_page(struct page *page, bool napi_safe)* and page_is_pfmemalloc() is checked in __page_pool_put_page() * to avoid recycling the pfmemalloc page. */ -if (unlikely ((page->pp_magic & ~0x3UL ) !=PP_SIGNATURE )) +if (unlikely (!is_pp_page (page)))return false; pp = page->pp;
Signature · 1 line
2.31.1
PATCH v7 4/4 skbuff: Optimization of SKB coalescing for page pool
In reply to Liang ChenIn order to address the issues encountered with commit 1effe8ca4e34 ("skbuff: fix coalescing for page_pool fragment recycling"), the combination of the following condition was excluded from skb coalescing:
from->pp_recycle = 1from->cloned = 1to->pp_recycle = 1
However, with page pool environments, the aforementioned combination can be quite common(ex. NetworkMananger may lead to the additional packet_type being registered, thus the cloning). In scenarios with a higher number of small packets, it can significantly affect the success rate of coalescing. For example, considering packets of 256 bytes size, our comparison of coalescing success rate is as follows:
Without page pool: 70%
With page pool: 13%
Consequently, this has an impact on performance:
Without page pool: 2.57 Gbits/sec
With page pool: 2.26 Gbits/sec
Therefore, it seems worthwhile to optimize this scenario and enable coalescing of this particular combination. To achieve this, we need to ensure the correct increment of the "from" SKB page's page pool reference count (pp_ref_count).
Following this optimization, the success rate of coalescing measured in our environment has improved as follows:
With page pool: 60%
This success rate is approaching the rate achieved without using page pool, and the performance has also been improved:
With page pool: 2.52 Gbits/sec
Below is the performance comparison for small packets before and after this optimization. We observe no impact to packets larger than 4K.
packet size before after improved (bytes) (Gbits/sec) (Gbits/sec) 128 1.19 1.27 7.13% 256 2.26 2.52 11.75% 512 4.13 4.81 16.50% 1024 6.17 6.73 9.05% 2048 14.54 15.47 6.45% 4096 25.44 27.87 9.52%
--- include/net/page_pool/helpers.h | 5 ++++ net/core/skbuff.c | 41 +++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-)
include/net/page_pool/helpers.h +5 -0
@@ -278,6 +278,11 @@ static inline long page_pool_unref_page(struct page *page, long nr)return ret; } +static inline void page_pool_ref_page (struct page *page) +{ +atomic_long_inc (&page->pp_ref_count); +} +static inline bool page_pool_is_last_ref (struct page *page) {/* If page_pool_unref_page() returns 0, we were the last user */
net/core/skbuff.c +29 -12
60 lines not on this page · raw message
Signature · 1 line
2.31.1
Jakub Kicinski
maintainerRe: [PATCH net-next v7 1/4] page_pool: transition to reference count management after page draining
In reply to Liang ChenQuoted 7 lines · On Wed, 6 Dec 2023 18:54:16 +0800 Liang Chen wrote:
include/net/page_pool/helpers.h +1
-1-/* pp_frag_count represents the number of writers who can update the page +/* pp_ref_count represents the number of writers who can update the page * either by updating skb->data or via DMA mappings for the device. * We can't rely on the page refcnt for that as we don't know who might be * holding page references and we can't reliably destroy or sync DMA mappings * of the fragments. *
include/net/page_pool/helpers.h inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool) · line 231
Quoted 10 lines
include/net/page_pool/helpers.h +6
-1-* When pp_frag_count reaches 0 we can either recycle the page if the page +* pp_ref_count initially corresponds to the number of fragments. However, +* when multiple users start to reference a single fragment, for example in +* skb_try_coalesce, the pp_ref_count will become greater than the number of +* fragments. +* +* When pp_ref_count reaches 0 we can either recycle the page if the page * refcnt is 1 or return it back to the memory allocator and destroy any * mappings we have. */
Sorry to nit pick but I think this whole doc has to be rewritten completely. It does state the most important thing which is that the caller must have just allocated the page.
How about:
/**
* page_pool_fragment_page() - split a fresh page into fragments
* @.. fill these in
*
* pp_ref_count represents the number of outstanding references
* to the page, which will be freed using page_pool APIs (rather
* than page allocator APIs like put_page()). Such references are
* usually held by page_pool-aware objects like skbs marked for
* page pool recycling.
*
* This helper allows the caller to take (set) multiple references
* to a freshly allocated page. The page must be freshly allocated
* (have a pp_ref_count of 1). This is commonly done by drivers
* and "fragment allocators" to save atomic operations - either
* when they know upfront how many references they will need; or
* to take MAX references and return the unused ones with a single
* atomic dec(), instead of performing multiple atomic inc()
* operations.
*/
I think that's more informative at this stage of evolution of the page pool API, when most users aren't experts on internals. But feel free to disagree..
include/net/page_pool/helpers.h inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool) · line 235
Quoted 5 lines
static inline void
page_pool_fragment_page(struct page *page, long nr){include/net/page_pool/helpers.h +1
-1-atomic_long_set (&page->pp_frag_count, nr); +atomic_long_set (&page->pp_ref_count, nr); }
The code itself and rest of the patches LGTM, although it would be great to get ACKs from pp maintainers..
Re: [PATCH net-next v7 4/4] skbuff: Optimization of SKB coalescing for page pool
In reply to Liang ChenQuoted 91 lines · Liang Chen
In order to address the issues encountered with commit
1effe8ca4e34("skbuff: fix coalescing forpage_poolfragment recycling"), the combination of the following condition was excluded from skb coalescing:
from->pp_recycle= 1from->cloned= 1to->pp_recycle= 1However, with page pool environments, the aforementioned combination can be quite common(ex. NetworkMananger may lead to the additional
packet_typebeing registered, thus the cloning). In scenarios with a higher number of small packets, it can significantly affect the success rate of coalescing. For example, considering packets of 256 bytes size, our comparison of coalescing success rate is as follows:Without page pool: 70%
With page pool: 13%Consequently, this has an impact on performance:
Without page pool: 2.57 Gbits/sec
With page pool: 2.26 Gbits/secTherefore, it seems worthwhile to optimize this scenario and enable coalescing of this particular combination. To achieve this, we need to ensure the correct increment of the "from" SKB page's page pool reference count (
pp_ref_count).Following this optimization, the success rate of coalescing measured in our environment has improved as follows:
With page pool: 60%
This success rate is approaching the rate achieved without using page pool, and the performance has also been improved:
With page pool: 2.52 Gbits/sec
Below is the performance comparison for small packets before and after this optimization. We observe no impact to packets larger than 4K.
packet size before after improved (bytes) (Gbits/sec) (Gbits/sec) 128 1.19 1.27 7.13% 256 2.26 2.52 11.75% 512 4.13 4.81 16.50% 1024 6.17 6.73 9.05% 2048 14.54 15.47 6.45% 4096 25.44 27.87 9.52%Signed-off-by Liang Chen <[email protected]>Reviewed-by Yunsheng Lin <[email protected]>Suggested-by Jason Wang <[email protected]>--- include/net/page_pool/helpers.h | 5 ++++ net/core/skbuff.c | 41 +++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-)include/net/page_pool/helpers.h +5
-0@@ -278,6 +278,11 @@ static inline long page_pool_unref_page(struct page *page, long nr)return ret; } +static inline void page_pool_ref_page (struct page *page) +{ +atomic_long_inc (&page->pp_ref_count); +} +static inline bool page_pool_is_last_ref (struct page *page) {/* If page_pool_unref_page() returns 0, we were the last user */ net/core/skbuff.c +8
-0@@ -947,6 +947,24 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe)return napi_pp_put_page (virt_to_page (data), napi_safe); } +/** +* skb_pp_frag_ref() - Increase fragment reference count of a page +* @page: page of the fragment on which to increase a reference +* +* Increase fragment reference count (pp_ref_count) on a page, but if it is +* not a page pool page, fallback to increase a reference(_refcount) on a +* normal page. +*/
net/core/skbuff.c static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) · line 967
Quoted 10 lines
net/core/skbuff.c +10
-0+static void skb_pp_frag_ref (struct page *page) +{ +struct page *head_page =compound_head (page); + +if (likely (is_pp_page (head_page))) +page_pool_ref_page (head_page); +else +page_ref_inc (head_page); +} +
I am confused by this, why add a new helper instead of modifying the existing helper, skb_frag_ref()?
My mental model is that if the net stack wants to acquire a reference on a frag, it calls skb_frag_ref(), and if it wants to drop a reference on a frag, it should call skb_frag_unref(). Internally skb_frag_ref/unref() can do all sorts of checking to decide whether to increment page->refcount or page->pp_ref_count. I can't wrap my head around the introduction of skb_pp_frag_ref(), but no equivalent skb_pp_frag_unref().
But even if skb_pp_frag_unref() was added, when should the net stack use skb_frag_ref/unref, and when should the stack use skb_pp_ref/unref? The docs currently describe what the function does, but when a program unfamiliar with the page pool should use it.
Quoted 28 lines
net/core/skbuff.c +5
-10static void skb_kfree_head (void *head,unsigned int end_offset) {if (end_offset ==SKB_SMALL_HEAD_HEADROOM )@@ -5769,17 +5787,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,return false;/* In general, avoid mixing page_pool and non-page_pool allocated -* pages within the same SKB. Additionally avoid dealing with clones -* with page_pool pages, in case the SKB is using page_pool fragment -* references (page_pool_alloc_frag()). Since we only take full page -* references for cloned SKBs at the moment that would result in -* inconsistent reference counts. -* In theory we could take full references if @from is cloned and -* !@to->pp_recycle but its tricky (due to potential race with -* the clone disappearing) and rare, so not worth dealing with. +* pages within the same SKB. In theory we could take full +* references if @from is cloned and !@to->pp_recycle but its +* tricky (due to potential race with the clone disappearing) and +* rare, so not worth dealing with. */ -if (to->pp_recycle != from->pp_recycle || - (from->pp_recycle &&skb_cloned (from))) +if (to->pp_recycle != from->pp_recycle)return false;if (len <=skb_tailroom (to)) {@@ -5836,8 +5849,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,/* if the skb is not cloned this does nothing
net/core/skbuff.c bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, · line 5857
Quoted 10 lines
net/core/skbuff.c +6
-2* since we set nr_frags to 0. */ -for (i =0 ; i < from_shinfo->nr_frags; i++) -__skb_frag_ref (&from_shinfo->frags[i]); +if (from->pp_recycle) +for (i =0 ; i < from_shinfo->nr_frags; i++) +skb_pp_frag_ref (skb_frag_page (&from_shinfo->frags[i])); +else +for (i =0 ; i < from_shinfo->nr_frags; i++) +__skb_frag_ref (&from_shinfo->frags[i]);
You added a check here to use skb_pp_frag_ref() instead of skb_frag_ref() here, but it's not clear to me why other callsites of skb_frag_ref() don't need to be modified in the same way after your patch.
After your patch:
skb_frag_ref() will always increment page->_refcount skb_frag_unref() will either decrement page->_refcount or decrement page->pp_ref_count (depending on the value of skb->pp_recycle). skb_pp_frag_ref() will either increment page->_refcount or increment page->pp_ref_count (depending on the value of is_pp_page(), not skb->pp_recycle).skb_pp_frag_unref() doesn't exist.
Is this not confusing? Can we streamline things:
skb_frag_ref() increments page->pp_ref_count for skb->pp_recycle, page->_refcount otherwise.skb_frag_unref() decrement page->pp_ref_count for skb->pp_recycle, page->_refcount otherwise.
Or am I missing something that causes us to require this asymmetric reference counting?
Quoted 7 lines
net/core/skbuff.c +0
-1to->truesize += delta; to->len += len; --2.31.1
Signature · 2 lines
Thanks, Mina
Re: [PATCH net-next v7 1/4] page_pool: transition to reference count management after page draining
In reply to Jakub KicinskiQuoted 46 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 6 Dec 2023 18:54:16 +0800 Liang Chen wrote:
include/net/page_pool/helpers.h +1
-1-/* pp_frag_count represents the number of writers who can update the page +/* pp_ref_count represents the number of writers who can update the page * either by updating skb->data or via DMA mappings for the device. * We can't rely on the page refcnt for that as we don't know who might be * holding page references and we can't reliably destroy or sync DMA mappings * of the fragments. * include/net/page_pool/helpers.h inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool) · line 231
Quoted 10 lines
include/net/page_pool/helpers.h +6
-1-* When pp_frag_count reaches 0 we can either recycle the page if the page +* pp_ref_count initially corresponds to the number of fragments. However, +* when multiple users start to reference a single fragment, for example in +* skb_try_coalesce, the pp_ref_count will become greater than the number of +* fragments. +* +* When pp_ref_count reaches 0 we can either recycle the page if the page * refcnt is 1 or return it back to the memory allocator and destroy any * mappings we have. */ Sorry to nit pick but I think this whole doc has to be rewritten completely. It does state the most important thing which is that the caller must have just allocated the page.
How about:
/**
*page_pool_fragment_page()- split a fresh page into fragments
* @.. fill these in
*
*pp_ref_countrepresents the number of outstanding references
* to the page, which will be freed usingpage_poolAPIs (rather
* than page allocator APIs likeput_page()). Such references are
* usually held by page_pool-aware objects like skbs marked for
* page pool recycling.
*
* This helper allows the caller to take (set) multiple references
* to a freshly allocated page. The page must be freshly allocated
* (have app_ref_countof 1). This is commonly done by drivers
* and "fragment allocators" to save atomic operations - either
* when they know upfront how many references they will need; or
* to take MAX references and return the unused ones with a single
* atomicdec(), instead of performing multiple atomicinc()
* operations.
*/
Quoted 4 lines
I think that's more informative at this stage of evolution of the page pool API, when most users aren't experts on internals. But feel free to disagree..
Thanks for the help! This is certainly better.
Quoted 8 lines
include/net/page_pool/helpers.h inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool) · line 235
Quoted 5 lines
static inline void
page_pool_fragment_page(struct page *page, long nr){include/net/page_pool/helpers.h +1
-1-atomic_long_set (&page->pp_frag_count, nr); +atomic_long_set (&page->pp_ref_count, nr); }The code itself and rest of the patches LGTM, although it would be great to get ACKs from pp maintainers..
Re: [PATCH net-next v7 4/4] skbuff: Optimization of SKB coalescing for page pool
In reply to Mina AlmasryQuoted 176 lines · Mina Almasry
Quoted 91 lines · Liang Chen
In order to address the issues encountered with commit
1effe8ca4e34("skbuff: fix coalescing forpage_poolfragment recycling"), the combination of the following condition was excluded from skb coalescing:
from->pp_recycle= 1from->cloned= 1to->pp_recycle= 1However, with page pool environments, the aforementioned combination can be quite common(ex. NetworkMananger may lead to the additional
packet_typebeing registered, thus the cloning). In scenarios with a higher number of small packets, it can significantly affect the success rate of coalescing. For example, considering packets of 256 bytes size, our comparison of coalescing success rate is as follows:Without page pool: 70%
With page pool: 13%Consequently, this has an impact on performance:
Without page pool: 2.57 Gbits/sec
With page pool: 2.26 Gbits/secTherefore, it seems worthwhile to optimize this scenario and enable coalescing of this particular combination. To achieve this, we need to ensure the correct increment of the "from" SKB page's page pool reference count (
pp_ref_count).Following this optimization, the success rate of coalescing measured in our environment has improved as follows:
With page pool: 60%
This success rate is approaching the rate achieved without using page pool, and the performance has also been improved:
With page pool: 2.52 Gbits/sec
Below is the performance comparison for small packets before and after this optimization. We observe no impact to packets larger than 4K.
packet size before after improved (bytes) (Gbits/sec) (Gbits/sec) 128 1.19 1.27 7.13% 256 2.26 2.52 11.75% 512 4.13 4.81 16.50% 1024 6.17 6.73 9.05% 2048 14.54 15.47 6.45% 4096 25.44 27.87 9.52%Signed-off-by Liang Chen <[email protected]>Reviewed-by Yunsheng Lin <[email protected]>Suggested-by Jason Wang <[email protected]>--- include/net/page_pool/helpers.h | 5 ++++ net/core/skbuff.c | 41 +++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-)include/net/page_pool/helpers.h +5
-0@@ -278,6 +278,11 @@ static inline long page_pool_unref_page(struct page *page, long nr)return ret; } +static inline void page_pool_ref_page (struct page *page) +{ +atomic_long_inc (&page->pp_ref_count); +} +static inline bool page_pool_is_last_ref (struct page *page) {/* If page_pool_unref_page() returns 0, we were the last user */ net/core/skbuff.c +8
-0@@ -947,6 +947,24 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe)return napi_pp_put_page (virt_to_page (data), napi_safe); } +/** +* skb_pp_frag_ref() - Increase fragment reference count of a page +* @page: page of the fragment on which to increase a reference +* +* Increase fragment reference count (pp_ref_count) on a page, but if it is +* not a page pool page, fallback to increase a reference(_refcount) on a +* normal page. +*/ net/core/skbuff.c static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) · line 967
Quoted 10 lines
net/core/skbuff.c +10
-0+static void skb_pp_frag_ref (struct page *page) +{ +struct page *head_page =compound_head (page); + +if (likely (is_pp_page (head_page))) +page_pool_ref_page (head_page); +else +page_ref_inc (head_page); +} +I am confused by this, why add a new helper instead of modifying the existing helper,
skb_frag_ref()?My mental model is that if the net stack wants to acquire a reference on a frag, it calls
skb_frag_ref(), and if it wants to drop a reference on a frag, it should callskb_frag_unref(). Internally skb_frag_ref/unref() can do all sorts of checking to decide whether to incrementpage->refcountorpage->pp_ref_count. I can't wrap my head around the introduction ofskb_pp_frag_ref(), but no equivalentskb_pp_frag_unref().But even if
skb_pp_frag_unref()was added, when should the net stack use skb_frag_ref/unref, and when should the stack use skb_pp_ref/unref? The docs currently describe what the function does, but when a program unfamiliar with the page pool should use it.Quoted 28 lines
net/core/skbuff.c +5
-10static void skb_kfree_head (void *head,unsigned int end_offset) {if (end_offset ==SKB_SMALL_HEAD_HEADROOM )@@ -5769,17 +5787,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,return false;/* In general, avoid mixing page_pool and non-page_pool allocated -* pages within the same SKB. Additionally avoid dealing with clones -* with page_pool pages, in case the SKB is using page_pool fragment -* references (page_pool_alloc_frag()). Since we only take full page -* references for cloned SKBs at the moment that would result in -* inconsistent reference counts. -* In theory we could take full references if @from is cloned and -* !@to->pp_recycle but its tricky (due to potential race with -* the clone disappearing) and rare, so not worth dealing with. +* pages within the same SKB. In theory we could take full +* references if @from is cloned and !@to->pp_recycle but its +* tricky (due to potential race with the clone disappearing) and +* rare, so not worth dealing with. */ -if (to->pp_recycle != from->pp_recycle || - (from->pp_recycle &&skb_cloned (from))) +if (to->pp_recycle != from->pp_recycle)return false;if (len <=skb_tailroom (to)) {@@ -5836,8 +5849,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,/* if the skb is not cloned this does nothing net/core/skbuff.c bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, · line 5857
Quoted 10 lines
net/core/skbuff.c +6
-2* since we set nr_frags to 0. */ -for (i =0 ; i < from_shinfo->nr_frags; i++) -__skb_frag_ref (&from_shinfo->frags[i]); +if (from->pp_recycle) +for (i =0 ; i < from_shinfo->nr_frags; i++) +skb_pp_frag_ref (skb_frag_page (&from_shinfo->frags[i])); +else +for (i =0 ; i < from_shinfo->nr_frags; i++) +__skb_frag_ref (&from_shinfo->frags[i]);You added a check here to use
skb_pp_frag_ref()instead ofskb_frag_ref()here, but it's not clear to me why other callsites ofskb_frag_ref()don't need to be modified in the same way after your patch.After your patch:
skb_frag_ref()will always incrementpage->_refcountskb_frag_unref()will either decrementpage->_refcountor decrementpage->pp_ref_count(depending on the value ofskb->pp_recycle).skb_pp_frag_ref()will either incrementpage->_refcountor incrementpage->pp_ref_count(depending on the value ofis_pp_page(), notskb->pp_recycle).skb_pp_frag_unref()doesn't exist.Is this not confusing? Can we streamline things:
Quoted 8 lines
skb_frag_ref()incrementspage->pp_ref_countforskb->pp_recycle,page->_refcountotherwise.skb_frag_unref()decrementpage->pp_ref_countforskb->pp_recycle,page->_refcountotherwise.Or am I missing something that causes us to require this asymmetric reference counting?
This idea was previously implemented, as shown here:
https://lore.kernel.org/all/[email protected]/.
But implementing this would result in some unnecessary overhead, since currently, 'skb_try_coalesce' is the only place where the page pool reference count for skb frag might be increased. I would prefer to move the logic to '__skb_frag_ref' when such a need becomes more common. Thanks!
Quoted 12 lines
Quoted 7 lines
net/core/skbuff.c +0
-1to->truesize += delta; to->len += len; --2.31.1
--
Thanks,
Mina
Re: [PATCH net-next v7 4/4] skbuff: Optimization of SKB coalescing for page pool
Quoted 187 lines · Liang Chen
Quoted 176 lines · Mina Almasry
Quoted 91 lines · Liang Chen
In order to address the issues encountered with commit
1effe8ca4e34("skbuff: fix coalescing forpage_poolfragment recycling"), the combination of the following condition was excluded from skb coalescing:
from->pp_recycle= 1from->cloned= 1to->pp_recycle= 1However, with page pool environments, the aforementioned combination can be quite common(ex. NetworkMananger may lead to the additional
packet_typebeing registered, thus the cloning). In scenarios with a higher number of small packets, it can significantly affect the success rate of coalescing. For example, considering packets of 256 bytes size, our comparison of coalescing success rate is as follows:Without page pool: 70%
With page pool: 13%Consequently, this has an impact on performance:
Without page pool: 2.57 Gbits/sec
With page pool: 2.26 Gbits/secTherefore, it seems worthwhile to optimize this scenario and enable coalescing of this particular combination. To achieve this, we need to ensure the correct increment of the "from" SKB page's page pool reference count (
pp_ref_count).Following this optimization, the success rate of coalescing measured in our environment has improved as follows:
With page pool: 60%
This success rate is approaching the rate achieved without using page pool, and the performance has also been improved:
With page pool: 2.52 Gbits/sec
Below is the performance comparison for small packets before and after this optimization. We observe no impact to packets larger than 4K.
packet size before after improved (bytes) (Gbits/sec) (Gbits/sec) 128 1.19 1.27 7.13% 256 2.26 2.52 11.75% 512 4.13 4.81 16.50% 1024 6.17 6.73 9.05% 2048 14.54 15.47 6.45% 4096 25.44 27.87 9.52%Signed-off-by Liang Chen <[email protected]>Reviewed-by Yunsheng Lin <[email protected]>Suggested-by Jason Wang <[email protected]>--- include/net/page_pool/helpers.h | 5 ++++ net/core/skbuff.c | 41 +++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-)include/net/page_pool/helpers.h +5
-0@@ -278,6 +278,11 @@ static inline long page_pool_unref_page(struct page *page, long nr)return ret; } +static inline void page_pool_ref_page (struct page *page) +{ +atomic_long_inc (&page->pp_ref_count); +} +static inline bool page_pool_is_last_ref (struct page *page) {/* If page_pool_unref_page() returns 0, we were the last user */ net/core/skbuff.c +8
-0@@ -947,6 +947,24 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe)return napi_pp_put_page (virt_to_page (data), napi_safe); } +/** +* skb_pp_frag_ref() - Increase fragment reference count of a page +* @page: page of the fragment on which to increase a reference +* +* Increase fragment reference count (pp_ref_count) on a page, but if it is +* not a page pool page, fallback to increase a reference(_refcount) on a +* normal page. +*/ net/core/skbuff.c static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) · line 967
Quoted 10 lines
net/core/skbuff.c +10
-0+static void skb_pp_frag_ref (struct page *page) +{ +struct page *head_page =compound_head (page); + +if (likely (is_pp_page (head_page))) +page_pool_ref_page (head_page); +else +page_ref_inc (head_page); +} +I am confused by this, why add a new helper instead of modifying the existing helper,
skb_frag_ref()?My mental model is that if the net stack wants to acquire a reference on a frag, it calls
skb_frag_ref(), and if it wants to drop a reference on a frag, it should callskb_frag_unref(). Internally skb_frag_ref/unref() can do all sorts of checking to decide whether to incrementpage->refcountorpage->pp_ref_count. I can't wrap my head around the introduction ofskb_pp_frag_ref(), but no equivalentskb_pp_frag_unref().But even if
skb_pp_frag_unref()was added, when should the net stack use skb_frag_ref/unref, and when should the stack use skb_pp_ref/unref? The docs currently describe what the function does, but when a program unfamiliar with the page pool should use it.Quoted 28 lines
net/core/skbuff.c +5
-10static void skb_kfree_head (void *head,unsigned int end_offset) {if (end_offset ==SKB_SMALL_HEAD_HEADROOM )@@ -5769,17 +5787,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,return false;/* In general, avoid mixing page_pool and non-page_pool allocated -* pages within the same SKB. Additionally avoid dealing with clones -* with page_pool pages, in case the SKB is using page_pool fragment -* references (page_pool_alloc_frag()). Since we only take full page -* references for cloned SKBs at the moment that would result in -* inconsistent reference counts. -* In theory we could take full references if @from is cloned and -* !@to->pp_recycle but its tricky (due to potential race with -* the clone disappearing) and rare, so not worth dealing with. +* pages within the same SKB. In theory we could take full +* references if @from is cloned and !@to->pp_recycle but its +* tricky (due to potential race with the clone disappearing) and +* rare, so not worth dealing with. */ -if (to->pp_recycle != from->pp_recycle || - (from->pp_recycle &&skb_cloned (from))) +if (to->pp_recycle != from->pp_recycle)return false;if (len <=skb_tailroom (to)) {@@ -5836,8 +5849,12 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,/* if the skb is not cloned this does nothing net/core/skbuff.c bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, · line 5857
Quoted 10 lines
net/core/skbuff.c +6
-2* since we set nr_frags to 0. */ -for (i =0 ; i < from_shinfo->nr_frags; i++) -__skb_frag_ref (&from_shinfo->frags[i]); +if (from->pp_recycle) +for (i =0 ; i < from_shinfo->nr_frags; i++) +skb_pp_frag_ref (skb_frag_page (&from_shinfo->frags[i])); +else +for (i =0 ; i < from_shinfo->nr_frags; i++) +__skb_frag_ref (&from_shinfo->frags[i]);You added a check here to use
skb_pp_frag_ref()instead ofskb_frag_ref()here, but it's not clear to me why other callsites ofskb_frag_ref()don't need to be modified in the same way after your patch.After your patch:
skb_frag_ref()will always incrementpage->_refcountskb_frag_unref()will either decrementpage->_refcountor decrementpage->pp_ref_count(depending on the value ofskb->pp_recycle).skb_pp_frag_ref()will either incrementpage->_refcountor incrementpage->pp_ref_count(depending on the value ofis_pp_page(), notskb->pp_recycle).skb_pp_frag_unref()doesn't exist.Is this not confusing? Can we streamline things:
Quoted 8 lines
skb_frag_ref()incrementspage->pp_ref_countforskb->pp_recycle,page->_refcountotherwise.skb_frag_unref()decrementpage->pp_ref_countforskb->pp_recycle,page->_refcountotherwise.Or am I missing something that causes us to require this asymmetric reference counting?
Quoted 8 lines
This idea was previously implemented, as shown here:
https://lore.kernel.org/all/[email protected]/.
But implementing this would result in some unnecessary overhead, since currently, 'skb_try_coalesce' is the only place where the page pool reference count for skb frag might be increased. I would prefer to move the logic to '__skb_frag_ref' when such a need becomes more common. Thanks!
Is it possible/desirable to add a comment to skb_frag_ref() that it should not be used with skb->pp_recycle? At least I was tripped by this, but maybe it's considered obvious somehow.
But I feel like this maybe needs to be fixed. Why does the page_pool need a separate page->pp_ref_count? Why not use page->_refcount like the rest of the code? Is there a history here behind this decision that you can point me to? It seems to me that incrementing/decrementing page->pp_ref_count may be equivalent to doing the same on page->_refcount.
Quoted 12 lines
Quoted 12 lines
Quoted 7 lines
net/core/skbuff.c +0
-1to->truesize += delta; to->len += len; --2.31.1
--
Thanks,
Mina
Signature · 2 lines
Thanks, Mina
Jakub Kicinski
maintainerRe: [PATCH net-next v7 4/4] skbuff: Optimization of SKB coalescing for page pool
Quoted 10 lines · On Sun, 10 Dec 2023 20:21:21 -0800 Mina Almasry wrote:
Is it possible/desirable to add a comment to
skb_frag_ref()that it should not be used withskb->pp_recycle? At least I was tripped by this, but maybe it's considered obvious somehow.But I feel like this maybe needs to be fixed. Why does the
page_poolneed a separatepage->pp_ref_count? Why not usepage->_refcountlike the rest of the code? Is there a history here behind this decision that you can point me to? It seems to me that incrementing/decrementingpage->pp_ref_countmay be equivalent to doing the same onpage->_refcount.
Does reading the contents of the comment I proposed here:
https://lore.kernel.org/all/[email protected]/
elucidate it? The pp_ref_count means the holder is aware that they can't release the reference by calling put_page(). Because (a) we may need to clean up the pp state, unmap DMA etc. and (b) one day it may not even be a real page (your work).
TBH I'm partial to the rename from patch 1, so I wouldn't delay this work any more :) But you have a point that we should inspect the code and consider making the semantics of skb_frag_ref() stronger all by itself, without the need to add a new flavor of the helper.. Are you okay with leaving that as a follow up or do you reckon it's easy enough we should push for it now?
Re: [PATCH net-next v7 4/4] skbuff: Optimization of SKB coalescing for page pool
Quoted 13 lines · Jakub Kicinski
Quoted 10 lines · On Sun, 10 Dec 2023 20:21:21 -0800 Mina Almasry wrote:
Is it possible/desirable to add a comment to
skb_frag_ref()that it should not be used withskb->pp_recycle? At least I was tripped by this, but maybe it's considered obvious somehow.But I feel like this maybe needs to be fixed. Why does the
page_poolneed a separatepage->pp_ref_count? Why not usepage->_refcountlike the rest of the code? Is there a history here behind this decision that you can point me to? It seems to me that incrementing/decrementingpage->pp_ref_countmay be equivalent to doing the same onpage->_refcount.
Quoted 7 lines
Does reading the contents of the comment I proposed here:
https://lore.kernel.org/all/[email protected]/
elucidate it? Thepp_ref_countmeans the holder is aware that they can't release the reference by callingput_page(). Because (a) we may need to clean up the pp state, unmap DMA etc. and (b) one day it may not even be a real page (your work).
Thank you, that makes sense.
Quoted 6 lines
TBH I'm partial to the rename from patch 1, so I wouldn't delay this work any more :) But you have a point that we should inspect the code and consider making the semantics of
skb_frag_ref()stronger all by itself, without the need to add a new flavor of the helper.. Are you okay with leaving that as a follow up or do you reckon it's easy enough we should push for it now?
I think the rename from pp_frag_count -> pp_ref_count is a huge improvement, and I think the fact that the netstack has a way to obtain a reference on a pp frag is also a huge improvement. Please go ahead mearging this if you like, I was asking questions for my own education/follow up work to consider.
Signature · 2 lines
Thanks, Mina