Introduce a bulk order-0 page allocator for sunrpc
Subsystems (9)
cover Introduce a bulk order-0 page allocator for sunrpc
This is a prototype series that introduces a bulk order-0 page allocator with sunrpc being the first user. The implementation is not particularly efficient and the intention is to iron out what the semantics of the API should be. That said, sunrpc was reported to have reduced allocation latency when refilling a pool.
As a side-note, while the implementation could be more efficient, it would require fairly deep surgery in numerous places. The lock scope would need to be significantly reduced, particularly as vmstat, per-cpu and the buddy allocator have different locking protocol that overal -- e.g. all partially depend on irqs being disabled at various points. Secondly, the core of the allocator deals with single pages where as both the bulk allocator and per-cpu allocator operate in batches. All of that has to be reconciled with all the existing users and their constraints (memory offline, CMA and cpusets being the trickiest).
In terms of semantics required by new users, my preference is that a pair of patches be applied -- the first which adds the required semantic to the bulk allocator and the second which adds the new user.
Patch 1 of this series is a cleanup to sunrpc, it could be merged
separately but is included here for convenience.
Patch 2 is the prototype bulk allocator
Patch 3 is the sunrpc user. Chuck also has a patch which further caches
pages but is not included in this series. It's not directly related to the bulk allocator and as it caches pages, it might have other concerns (e.g. does it need a shrinker?)
This has only been lightly tested on a low-end NFS server. It did not break but would benefit from an evaluation to see how much, if any, the headline performance changes. The biggest concern is that a light test case showed that there are a *lot* of bulk requests for 1 page which gets delegated to the normal allocator. The same criteria should apply to any other users.
include/linux/gfp.h | 13 +++++ mm/page_alloc.c | 113 +++++++++++++++++++++++++++++++++++++++++- net/sunrpc/svc_xprt.c | 47 ++++++++++++------ 3 files changed, 157 insertions(+), 16 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/3 SUNRPC: Set rq_page_end differently
From: Chuck Lever <[email protected]>
Refactor:
I'm about to use the loop variable @i for something else.
As far as the "i++" is concerned, that is a post-increment. The value of @i is not used subsequently, so the increment operator is unnecessary and can be removed.
Also note that nfsd_read_actor() was renamed nfsd_splice_actor() by commit cf8208d0eabd ("sendfile: convert nfsd to splice_direct_to_actor()").
--- net/sunrpc/svc_xprt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/sunrpc/svc_xprt.c +2 -2
@@ -667,8 +667,8 @@ static int svc_alloc_arg(struct svc_rqst *rqstp) } rqstp->rq_pages[i] = p; } - rqstp->rq_page_end = &rqstp->rq_pages[i]; - rqstp->rq_pages[i++] =NULL ;/* this might be seen in nfs_read_actor */ + rqstp->rq_page_end = &rqstp->rq_pages[pages]; + rqstp->rq_pages[pages] =NULL ;/* this might be seen in nfsd_splice_actor() */ /* Make arg->head point to first page and arg->pages point to rest */ arg = &rqstp->rq_arg;
Signature · 1 line
2.26.2
PATCH 2/3 mm, page_alloc: Add a bulk page allocator
In reply to Mel GormanThis patch adds a new page allocator interface via alloc_pages_bulk, and __alloc_pages_bulk_nodemask. A caller requests a number of pages to be allocated and added to a list. They can be freed in bulk using free_pages_bulk().
The API is not guaranteed to return the requested number of pages and may fail if the preferred allocation zone has limited free memory, the cpuset changes during the allocation or page debugging decides to fail an allocation. It's up to the caller to request more pages in batch if necessary.
Note that this implementation is not very efficient and could be improved but it would require refactoring. The intent is to make it available early to determine what semantics are required by different callers. Once the full semantics are nailed down, it can be refactored.
--- include/linux/gfp.h | 13 +++++ mm/page_alloc.c | 113 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)
include/linux/gfp.h +13 -0
@@ -501,6 +501,10 @@ static inline int arch_make_page_accessible(struct page *page) }#endif +int __alloc_pages_bulk_nodemask (gfp_t gfp_mask,int preferred_nid, +nodemask_t *nodemask,int nr_pages, +struct list_head *list); +struct page *__alloc_pages_nodemask (gfp_t gfp_mask,unsigned int order,int preferred_nid,nodemask_t *nodemask);
@@ -511,6 +515,14 @@ __alloc_pages(gfp_t gfp_mask, unsigned int order, int preferred_nid)return __alloc_pages_nodemask (gfp_mask, order, preferred_nid,NULL ); } +/* Bulk allocate order-0 pages */ +static inline unsigned long +alloc_pages_bulk (gfp_t gfp_mask,unsigned long nr_pages,struct list_head *list) +{ +return __alloc_pages_bulk_nodemask (gfp_mask,numa_mem_id (),NULL , + nr_pages, list); +} +/* * Allocate pages, preferring the node given as nid. The node must be valid and * online. For more general interface, see alloc_pages_node().
@@ -580,6 +592,7 @@ void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask);extern void __free_pages (struct page *page,unsigned int order);extern void free_pages (unsigned long addr,unsigned int order); +extern void free_pages_bulk (struct list_head *list);struct page_frag_cache ;extern void __page_frag_cache_drain (struct page *page,unsigned int count);
mm/page_alloc.c +111 -2
137 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH 3/3 SUNRPC: Refresh rq_pages using a bulk page allocator
In reply to Mel GormanFrom: Chuck Lever <[email protected]>
Reduce the rate at which nfsd threads hammer on the page allocator. This improve throughput scalability by enabling the threads to run more independently of each other.
--- net/sunrpc/svc_xprt.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-)
net/sunrpc/svc_xprt.c +31 -12
@@ -642,11 +642,12 @@ static void svc_check_conn_limits(struct svc_serv *serv)static int svc_alloc_arg (struct svc_rqst *rqstp) {struct svc_serv *serv = rqstp->rq_server; +unsigned long needed;struct xdr_buf *arg; +struct page *page;int pages;int i; -/* now allocate needed pages. If we get a failure, sleep briefly */ pages = (serv->sv_max_mesg +2 *PAGE_SIZE ) >>PAGE_SHIFT ;if (pages >RPCSVC_MAXPAGES ) { pr_warn_once("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n",
@@ -654,19 +655,28 @@ static int svc_alloc_arg(struct svc_rqst *rqstp)/* use as many pages as possible */ pages =RPCSVC_MAXPAGES ; } -for (i =0 ; i < pages ; i++) -while (rqstp->rq_pages[i] ==NULL ) { -struct page *p =alloc_page (GFP_KERNEL ); -if (!p) { -set_current_state (TASK_INTERRUPTIBLE ); -if (signalled () ||kthread_should_stop ()) { -set_current_state (TASK_RUNNING ); -return -EINTR ; - } -schedule_timeout (msecs_to_jiffies (500 )); + +for (needed =0 , i =0 ; i < pages ; i++) +if (!rqstp->rq_pages[i]) + needed++; +if (needed) { +LIST_HEAD (list); + +retry: +alloc_pages_bulk (GFP_KERNEL , needed, &list); +for (i =0 ; i < pages; i++) { +if (!rqstp->rq_pages[i]) { + page =list_first_entry_or_null (&list, + struct page, + lru); +if (unlikely (!page)) + goto empty_list; +list_del (&page->lru); + rqstp->rq_pages[i] = page; + needed--; } - rqstp->rq_pages[i] = p; } + } rqstp->rq_page_end = &rqstp->rq_pages[pages]; rqstp->rq_pages[pages] =NULL ;/* this might be seen in nfsd_splice_actor() */
@@ -681,6 +691,15 @@ static int svc_alloc_arg(struct svc_rqst *rqstp) arg->len = (pages-1 )*PAGE_SIZE ; arg->tail[0 ].iov_len =0 ;return 0 ; + +empty_list: +set_current_state (TASK_INTERRUPTIBLE ); +if (signalled () ||kthread_should_stop ()) { +set_current_state (TASK_RUNNING ); +return -EINTR ; + } +schedule_timeout (msecs_to_jiffies (500 )); + goto retry; } static bool
Signature · 1 line
2.26.2
Quoted 5 lines · On Wed, 24 Feb 2021 10:26:00 +0000 Mel Gorman <[email protected]>…
This is a prototype series that introduces a bulk order-0 page allocator with sunrpc being the first user. The implementation is not particularly efficient and the intention is to iron out what the semantics of the API should be. That said, sunrpc was reported to have reduced allocation latency when refilling a pool.
I also have a use-case in page_pool, and I've been testing with the earlier patches, results are here[1]
[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
Awesome to see this newer patchset! thanks a lot for working on this! I'll run some new tests based on this.
Quoted 9 lines
As a side-note, while the implementation could be more efficient, it would require fairly deep surgery in numerous places. The lock scope would need to be significantly reduced, particularly as vmstat, per-cpu and the buddy allocator have different locking protocol that overal -- e.g. all partially depend on irqs being disabled at various points. Secondly, the core of the allocator deals with single pages where as both the bulk allocator and per-cpu allocator operate in batches. All of that has to be reconciled with all the existing users and their constraints (memory offline, CMA and cpusets being the trickiest).
As you can see in[1], I'm getting a significant speedup from this. I guess that the cost of finding the "zone" is higher than I expected, as this basically what we/you amortize for the bulk.
Quoted 14 lines
In terms of semantics required by new users, my preference is that a pair of patches be applied -- the first which adds the required semantic to the bulk allocator and the second which adds the new user.
Patch 1 of this series is a cleanup to sunrpc, it could be merged
separately but is included here for convenience.Patch 2 is the prototype bulk allocator
Patch 3 is the sunrpc user. Chuck also has a patch which further caches
pages but is not included in this series. It's not directly related to the bulk allocator and as it caches pages, it might have other concerns (e.g. does it need a shrinker?)
Quoted 5 lines
This has only been lightly tested on a low-end NFS server. It did not break but would benefit from an evaluation to see how much, if any, the headline performance changes. The biggest concern is that a light test case showed that there are a *lot* of bulk requests for 1 page which gets delegated to the normal allocator. The same criteria should apply to any other users.
If you change local_irq_save(flags) to local_irq_disable() then you can likely get better performance for 1 page requests via this API. This limits the API to be used in cases where IRQs are enabled (which is most cases). (For my use-case I will not do 1 page requests).
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Quoted 9 lines · Jesper Dangaard Brouer
Quoted 5 lines · On Wed, 24 Feb 2021 10:26:00 +0000 Mel Gorman <[email protected]>…
This is a prototype series that introduces a bulk order-0 page allocator with sunrpc being the first user. The implementation is not particularly efficient and the intention is to iron out what the semantics of the API should be. That said, sunrpc was reported to have reduced allocation latency when refilling a pool.
Quoted 8 lines
I also have a use-case in
page_pool, and I've been testing with the earlier patches, results are here[1][1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
Awesome to see this newer patchset! thanks a lot for working on this! I'll run some new tests based on this.
Thanks and if they get finalised, a patch on top for review would be nice with the results included in the changelog. Obviously any change that would need to be made to the allocator would happen first.
Quoted 10 lines
Quoted 9 lines
As a side-note, while the implementation could be more efficient, it would require fairly deep surgery in numerous places. The lock scope would need to be significantly reduced, particularly as vmstat, per-cpu and the buddy allocator have different locking protocol that overal -- e.g. all partially depend on irqs being disabled at various points. Secondly, the core of the allocator deals with single pages where as both the bulk allocator and per-cpu allocator operate in batches. All of that has to be reconciled with all the existing users and their constraints (memory offline, CMA and cpusets being the trickiest).
Quoted 4 lines
As you can see in[1], I'm getting a significant speedup from this. I guess that the cost of finding the "zone" is higher than I expected, as this basically what we/you amortize for the bulk.
The obvious goal would be that if a refactoring did happen that the performance would be at least neutral but hopefully improved.
Quoted 20 lines
Quoted 14 lines
In terms of semantics required by new users, my preference is that a pair of patches be applied -- the first which adds the required semantic to the bulk allocator and the second which adds the new user.
Patch 1 of this series is a cleanup to sunrpc, it could be merged
separately but is included here for convenience.Patch 2 is the prototype bulk allocator
Patch 3 is the sunrpc user. Chuck also has a patch which further caches
pages but is not included in this series. It's not directly related to the bulk allocator and as it caches pages, it might have other concerns (e.g. does it need a shrinker?)Quoted 5 lines
This has only been lightly tested on a low-end NFS server. It did not break but would benefit from an evaluation to see how much, if any, the headline performance changes. The biggest concern is that a light test case showed that there are a *lot* of bulk requests for 1 page which gets delegated to the normal allocator. The same criteria should apply to any other users.
Quoted 5 lines
If you change
local_irq_save(flags)tolocal_irq_disable()then you can likely get better performance for 1 page requests via this API. This limits the API to be used in cases where IRQs are enabled (which is most cases). (For my use-case I will not do 1 page requests).
I do not want to constrain the API to being IRQ-only prematurely. An obvious alternative use case is the SLUB allocation path when a high-order allocation fails. It's known that if the SLUB order is reduced that it has an impact on hackbench communicating over sockets. It would be interesting to see what happens if order-0 pages are bulk allocated when s->min == 0 and that can be called from a blocking context. Tricky to test but could be fudged by forcing all high-order allocations to fail when s->min == 0 to evaluate the worst case scenario. In addition, it would constrain any potential refactoring if the lower levels have to choose between local_irq_disable() vs local_irq_save() depending on the caller context.
Signature · 2 lines
Mel Gorman SUSE Labs
Quoted 40 lines · Mel Gorman
This is a prototype series that introduces a bulk order-0 page allocator with sunrpc being the first user. The implementation is not particularly efficient and the intention is to iron out what the semantics of the API should be. That said, sunrpc was reported to have reduced allocation latency when refilling a pool.
As a side-note, while the implementation could be more efficient, it would require fairly deep surgery in numerous places. The lock scope would need to be significantly reduced, particularly as vmstat, per-cpu and the buddy allocator have different locking protocol that overal -- e.g. all partially depend on irqs being disabled at various points. Secondly, the core of the allocator deals with single pages where as both the bulk allocator and per-cpu allocator operate in batches. All of that has to be reconciled with all the existing users and their constraints (memory offline, CMA and cpusets being the trickiest).
In terms of semantics required by new users, my preference is that a pair of patches be applied -- the first which adds the required semantic to the bulk allocator and the second which adds the new user.
Patch 1 of this series is a cleanup to sunrpc, it could be merged
separately but is included here for convenience.Patch 2 is the prototype bulk allocator
Patch 3 is the sunrpc user. Chuck also has a patch which further caches
pages but is not included in this series. It's not directly related to the bulk allocator and as it caches pages, it might have other concerns (e.g. does it need a shrinker?)This has only been lightly tested on a low-end NFS server. It did not break but would benefit from an evaluation to see how much, if any, the headline performance changes. The biggest concern is that a light test case showed that there are a *lot* of bulk requests for 1 page which gets delegated to the normal allocator. The same criteria should apply to any other users.
Quoted 4 lines
include/linux/gfp.h | 13 +++++ mm/page_alloc.c | 113 +++++++++++++++++++++++++++++++++++++++++- net/sunrpc/svc_xprt.c | 47 ++++++++++++------ 3 files changed, 157 insertions(+), 16 deletions(-)
Hi Mel-
Thank you for carrying the torch!
--
Chuck Lever
cover Use bulk order-0 page allocator API for page_pool
In reply to Mel GormanThis is a followup to Mel Gorman's patchset:
- Message-Id: <[email protected]>
- https://lore.kernel.org/netdev/[email protected]/
Showing page_pool usage of the API for alloc_pages_bulk().
---
Jesper Dangaard Brouer (3):
net: page_pool: refactor dma_map into own function page_pool_dma_map
net: page_pool: use alloc_pages_bulk in refill code path
mm: make zone->free_area[order] access faster
include/linux/mmzone.h | 6 ++- net/core/page_pool.c | 96 +++++++++++++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 37 deletions(-)
--
RFC PATCH 1/3 net: page_pool: refactor dma_map into own function page_pool_dma_map
In preparation for next patch, move the dma mapping into its own function, as this will make it easier to follow the changes.
--- net/core/page_pool.c | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-)
net/core/page_pool.c +29 -20
@@ -180,6 +180,31 @@ static void page_pool_dma_sync_for_device(struct page_pool *pool, pool->p.dma_dir); } +static struct page *page_pool_dma_map (struct page_pool *pool, +struct page *page) +{ +dma_addr_t dma; + +/* Setup DMA mapping: use 'struct page' area for storing DMA-addr +* since dma_addr_t can be either 32 or 64 bits and does not always fit +* into page private data (i.e 32bit cpu with 64bit DMA caps) +* This mapping is kept for lifetime of page, until leaving pool. +*/ + dma =dma_map_page_attrs (pool->p.dev, page,0 , + (PAGE_SIZE << pool->p.order), + pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); +if (dma_mapping_error (pool->p.dev, dma)) { +put_page (page); +return NULL ; + } + page->dma_addr = dma; + +if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) +page_pool_dma_sync_for_device (pool, page, pool->p.max_len); + +return page; +} +/* slow path */ noinline static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
@@ -187,7 +212,6 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, {struct page *page;gfp_t gfp = _gfp; -dma_addr_t dma;/* We could always set __GFP_COMP, and avoid this branch, as * prep_new_page() can handle order-0 with __GFP_COMP.
@@ -211,27 +235,12 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,if (!page)return NULL ; -if (!(pool->p.flags &PP_FLAG_DMA_MAP )) - goto skip_dma_map; - -/* Setup DMA mapping: use 'struct page' area for storing DMA-addr -* since dma_addr_t can be either 32 or 64 bits and does not always fit -* into page private data (i.e 32bit cpu with 64bit DMA caps) -* This mapping is kept for lifetime of page, until leaving pool. -*/ - dma =dma_map_page_attrs (pool->p.dev, page,0 , - (PAGE_SIZE << pool->p.order), - pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); -if (dma_mapping_error (pool->p.dev, dma)) { -put_page (page); -return NULL ; +if (pool->p.flags &PP_FLAG_DMA_MAP ) { + page =page_pool_dma_map (pool, page); +if (!page) +return NULL ; } - page->dma_addr = dma; - -if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) -page_pool_dma_sync_for_device (pool, page, pool->p.max_len); -skip_dma_map:/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++;
Ilias Apalodimas
maintainerRe: [PATCH RFC net-next 1/3] net: page_pool: refactor dma_map into own function page_pool_dma_map
Quoted 13 lines · Jesper Dangaard Brouer
In preparation for next patch, move the dma mapping into its own function, as this will make it easier to follow the changes.
Signed-off-by Jesper Dangaard Brouer <[email protected]>--- net/core/page_pool.c | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-)net/core/page_pool.c +0
-0
net/core/page_pool.c static void page_pool_dma_sync_for_device(struct page_pool *pool, · line 185
Quoted 6 lines
net/core/page_pool.c +3
-0@@ -180,6 +180,31 @@ static void page_pool_dma_sync_for_device(struct page_pool *pool, pool->p.dma_dir); } +static struct page *page_pool_dma_map (struct page_pool *pool, +struct page *page) +{
Why return a struct page* ?
boolean maybe?
net/core/page_pool.c static void page_pool_dma_sync_for_device(struct page_pool *pool, · line 197
Quoted 12 lines
net/core/page_pool.c +12
-0+dma_addr_t dma; + +/* Setup DMA mapping: use 'struct page' area for storing DMA-addr +* since dma_addr_t can be either 32 or 64 bits and does not always fit +* into page private data (i.e 32bit cpu with 64bit DMA caps) +* This mapping is kept for lifetime of page, until leaving pool. +*/ + dma =dma_map_page_attrs (pool->p.dev, page,0 , + (PAGE_SIZE << pool->p.order), + pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); +if (dma_mapping_error (pool->p.dev, dma)) { +put_page (page);
This is a bit confusing when reading it.
The name of the function should try to map the page and report a yes/no, instead of trying to call put_page as well.
Can't we explicitly ask the user to call put_page() if the mapping failed?
A clear example is on patch 2/3, when on the first read I was convinced there was a memory leak.
Quoted 45 lines
net/core/page_pool.c +14
-15+return NULL ; + } + page->dma_addr = dma; + +if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) +page_pool_dma_sync_for_device (pool, page, pool->p.max_len); + +return page; +} +/* slow path */ noinline static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,@@ -187,7 +212,6 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, {struct page *page;gfp_t gfp = _gfp; -dma_addr_t dma;/* We could always set __GFP_COMP, and avoid this branch, as * prep_new_page() can handle order-0 with __GFP_COMP. @@ -211,27 +235,12 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,if (!page)return NULL ; -if (!(pool->p.flags &PP_FLAG_DMA_MAP )) - goto skip_dma_map; - -/* Setup DMA mapping: use 'struct page' area for storing DMA-addr -* since dma_addr_t can be either 32 or 64 bits and does not always fit -* into page private data (i.e 32bit cpu with 64bit DMA caps) -* This mapping is kept for lifetime of page, until leaving pool. -*/ - dma =dma_map_page_attrs (pool->p.dev, page,0 , - (PAGE_SIZE << pool->p.order), - pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); -if (dma_mapping_error (pool->p.dev, dma)) { -put_page (page); -return NULL ; +if (pool->p.flags &PP_FLAG_DMA_MAP ) { + page =page_pool_dma_map (pool, page); +if (!page) +return NULL ;
net/core/page_pool.c static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, · line 246
Quoted 10 lines
net/core/page_pool.c +0
-5} - page->dma_addr = dma; - -if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) -page_pool_dma_sync_for_device (pool, page, pool->p.max_len); -skip_dma_map:/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++;
Thanks!
/Ilias
RFC PATCH 2/3 net: page_pool: use alloc_pages_bulk in refill code path
In reply to Jesper Dangaard BrouerThere are cases where the page_pool need to refill with pages from the page allocator. Some workloads cause the page_pool to release pages instead of recycling these pages.
For these workload it can improve performance to bulk alloc pages from the page-allocator to refill the alloc cache.
For XDP-redirect workload with 100G mlx5 driver (that use page_pool) redirecting xdp_frame packets into a veth, that does XDP_PASS to create an SKB from the xdp_frame, which then cannot return the page to the page_pool. In this case, we saw[1] an improvement of 18.8% from using the alloc_pages_bulk API (3,677,958 pps -> 4,368,926 pps).
[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
--- net/core/page_pool.c | 65 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 24 deletions(-)
net/core/page_pool.c +41 -24
@@ -210,44 +210,61 @@ noinlinestatic struct page *__page_pool_alloc_pages_slow (struct page_pool *pool,gfp_t _gfp) { -struct page *page; +const int bulk =PP_ALLOC_CACHE_REFILL ; +struct page *page, *next, *first_page; +unsigned int pp_flags = pool->p.flags; +unsigned int pp_order = pool->p.order; +int pp_nid = pool->p.nid; +LIST_HEAD (page_list);gfp_t gfp = _gfp; -/* We could always set __GFP_COMP, and avoid this branch, as -* prep_new_page() can handle order-0 with __GFP_COMP. -*/ -if (pool->p.order) +/* Don't support bulk alloc for high-order pages */ +if (unlikely (pp_order)) { gfp |= __GFP_COMP; + first_page =alloc_pages_node (pp_nid, gfp, pp_order); +if (unlikely (!first_page)) +return NULL ; + goto out; + } -/* FUTURE development: -* -* Current slow-path essentially falls back to single page -* allocations, which doesn't improve performance. This code -* need bulk allocation support from the page allocator code. -*/ - -/* Cache was empty, do real allocation */ -#ifdef CONFIG_NUMA - page =alloc_pages_node (pool->p.nid, gfp, pool->p.order); -#else - page =alloc_pages (gfp, pool->p.order); -#endif -if (!page) +if (unlikely (!__alloc_pages_bulk_nodemask (gfp, pp_nid,NULL , + bulk, &page_list)))return NULL ; +/* First page is extracted and returned to caller */ + first_page = list_first_entry(&page_list, struct page, lru); +list_del (&first_page->lru); + +/* Remaining pages store in alloc.cache */ +list_for_each_entry_safe (page, next, &page_list, lru) { +list_del (&page->lru); +if (pp_flags &PP_FLAG_DMA_MAP ) { + page =page_pool_dma_map (pool, page); +if (!page) +continue ; + } +if (likely (pool->alloc.count <PP_ALLOC_CACHE_SIZE )) { + pool->alloc.cache[pool->alloc.count++] = page; + pool->pages_state_hold_cnt++; +trace_page_pool_state_hold (pool, page, + pool->pages_state_hold_cnt); + }else { +put_page (page); + } + } +out:if (pool->p.flags &PP_FLAG_DMA_MAP ) { - page =page_pool_dma_map (pool, page); -if (!page) + first_page =page_pool_dma_map (pool, first_page); +if (!first_page)return NULL ; }/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; - -trace_page_pool_state_hold (pool, page, pool->pages_state_hold_cnt); +trace_page_pool_state_hold (pool, first_page, pool->pages_state_hold_cnt);/* When page just alloc'ed is should/must have refcnt 1. */ -return page; +return first_page; }/* For using page_pool replace: alloc_pages() API calls, but provide
Ilias Apalodimas
maintainerRe: [PATCH RFC net-next 2/3] net: page_pool: use alloc_pages_bulk in refill code path
Hi Jesper,
Quoted 16 lines · Jesper Dangaard Brouer
There are cases where the
page_poolneed to refill with pages from the page allocator. Some workloads cause thepage_poolto release pages instead of recycling these pages.For these workload it can improve performance to bulk alloc pages from the page-allocator to refill the alloc cache.
For XDP-redirect workload with 100G mlx5 driver (that use
page_pool) redirectingxdp_framepackets into a veth, that doesXDP_PASSto create an SKB from thexdp_frame, which then cannot return the page to thepage_pool. In this case, we saw[1] an improvement of 18.8% from using thealloc_pages_bulkAPI (3,677,958 pps -> 4,368,926 pps).[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
Signed-off-by Jesper Dangaard Brouer <[email protected]>
[...]
net/core/page_pool.c noinline · line 243
Quoted 6 lines
net/core/page_pool.c +6
-0+/* Remaining pages store in alloc.cache */ +list_for_each_entry_safe (page, next, &page_list, lru) { +list_del (&page->lru); +if (pp_flags &PP_FLAG_DMA_MAP ) { + page =page_pool_dma_map (pool, page); +if (!page)
As I commented on the previous patch, i'd prefer the put_page() here to be explicitly called, instead of hiding in the page_pool_dma_map()
Quoted 10 lines
net/core/page_pool.c +9
-0+continue ; + } +if (likely (pool->alloc.count <PP_ALLOC_CACHE_SIZE )) { + pool->alloc.cache[pool->alloc.count++] = page; + pool->pages_state_hold_cnt++; +trace_page_pool_state_hold (pool, page, + pool->pages_state_hold_cnt); + }else { +put_page (page);
net/core/page_pool.c noinline · line 260
Quoted 10 lines
net/core/page_pool.c +5
-2+ } + } +out:if (pool->p.flags &PP_FLAG_DMA_MAP ) { - page =page_pool_dma_map (pool, page); -if (!page) + first_page =page_pool_dma_map (pool, first_page); +if (!first_page)return NULL ; }
[...]
Cheers
/Ilias
Re: [PATCH RFC net-next 2/3] net: page_pool: use alloc_pages_bulk in refill code path
Quoted 22 lines · On Wed, 24 Feb 2021 22:15:22 +0200 Ilias Apalodimas…
Hi Jesper,
Quoted 16 lines · Jesper Dangaard Brouer
There are cases where the
page_poolneed to refill with pages from the page allocator. Some workloads cause thepage_poolto release pages instead of recycling these pages.For these workload it can improve performance to bulk alloc pages from the page-allocator to refill the alloc cache.
For XDP-redirect workload with 100G mlx5 driver (that use
page_pool) redirectingxdp_framepackets into a veth, that doesXDP_PASSto create an SKB from thexdp_frame, which then cannot return the page to thepage_pool. In this case, we saw[1] an improvement of 18.8% from using thealloc_pages_bulkAPI (3,677,958 pps -> 4,368,926 pps).[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
Signed-off-by Jesper Dangaard Brouer <[email protected]>[...]
Quoted 9 lines
net/core/page_pool.c noinline · line 243
Quoted 6 lines
net/core/page_pool.c +6
-0+/* Remaining pages store in alloc.cache */ +list_for_each_entry_safe (page, next, &page_list, lru) { +list_del (&page->lru); +if (pp_flags &PP_FLAG_DMA_MAP ) { + page =page_pool_dma_map (pool, page); +if (!page)As I commented on the previous patch, i'd prefer the
put_page()here to be explicitly called, instead of hiding in thepage_pool_dma_map()
I fully agree. I will fixup the code.
Quoted 21 lines
Quoted 20 lines
net/core/page_pool.c +14
-2+continue ; + } +if (likely (pool->alloc.count <PP_ALLOC_CACHE_SIZE )) { + pool->alloc.cache[pool->alloc.count++] = page; + pool->pages_state_hold_cnt++; +trace_page_pool_state_hold (pool, page, + pool->pages_state_hold_cnt); + }else { +put_page (page); + } + } +out:if (pool->p.flags &PP_FLAG_DMA_MAP ) { - page =page_pool_dma_map (pool, page); -if (!page) + first_page =page_pool_dma_map (pool, first_page); +if (!first_page)return NULL ; }[...]
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
kernel test robot
build botRe: [PATCH RFC net-next 2/3] net: page_pool: use alloc_pages_bulk in refill code path
In reply to Jesper Dangaard Brouerreproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…
Hi Jesper,
[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on net-next/master]
url: https://github.com/0day-ci/linux/commits/Jesper-Dangaard-Brouer/net-page_pool-refactor-dma_map-into-own-function-page_pool_dma_map/20210225-035708 base: https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git d310ec03a34e92a77302edb804f7d68ee4f01ba0 config: sh-allmodconfig (attached as .config) compiler: sh4-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/5bd829f1f045742893822328d28d2f3a7df94806
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Jesper-Dangaard-Brouer/net-page_pool-refactor-dma_map-into-own-function-page_pool_dma_map/20210225-035708 git checkout 5bd829f1f045742893822328d28d2f3a7df94806
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/kernel.h:10,
from net/core/page_pool.c:9:
net/core/page_pool.c: In function '__page_pool_alloc_pages_slow':
>> net/core/page_pool.c:230:16: error: implicit declaration of function '__alloc_pages_bulk_nodemask'; did you mean '__alloc_pages_nodemask'? [-Werror=implicit-function-declaration]
230 | if (unlikely(!__alloc_pages_bulk_nodemask(gfp, pp_nid, NULL,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/compiler.h:78:42: note: in definition of macro 'unlikely'
78 | # define unlikely(x) __builtin_expect(!!(x), 0)
| ^cc1: some warnings being treated as errors
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for SND_ATMEL_SOC_PDC
Depends on SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && HAS_DMA Selected by
- SND_ATMEL_SOC_SSC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC
- SND_ATMEL_SOC_SSC_PDC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && ATMEL_SSC
vim +230 net/core/page_pool.c
207
208 /* slow path */
209 noinline
210 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
211 gfp_t _gfp)
212 {
213 const int bulk = PP_ALLOC_CACHE_REFILL;
214 struct page *page, *next, *first_page;
215 unsigned int pp_flags = pool->p.flags;
216 unsigned int pp_order = pool->p.order;
217 int pp_nid = pool->p.nid;
218 LIST_HEAD(page_list);
219 gfp_t gfp = _gfp;
220
221 /* Don't support bulk alloc for high-order pages */
222 if (unlikely(pp_order)) {
223 gfp |= __GFP_COMP;
224 first_page = alloc_pages_node(pp_nid, gfp, pp_order);
225 if (unlikely(!first_page))
226 return NULL;
227 goto out;
228 }
229
> 230 if (unlikely(!__alloc_pages_bulk_nodemask(gfp, pp_nid, NULL,
231 bulk, &page_list)))
232 return NULL;
233
234 /* First page is extracted and returned to caller */
235 first_page = list_first_entry(&page_list, struct page, lru);
236 list_del(&first_page->lru);
237
238 /* Remaining pages store in alloc.cache */
239 list_for_each_entry_safe(page, next, &page_list, lru) {
240 list_del(&page->lru);
241 if (pp_flags & PP_FLAG_DMA_MAP) {
242 page = page_pool_dma_map(pool, page);
243 if (!page)
244 continue;
245 }
246 if (likely(pool->alloc.count < PP_ALLOC_CACHE_SIZE)) {
247 pool->alloc.cache[pool->alloc.count++] = page;
248 pool->pages_state_hold_cnt++;
249 trace_page_pool_state_hold(pool, page,
250 pool->pages_state_hold_cnt);
251 } else {
252 put_page(page);
253 }
254 }
255 out:
256 if (pool->p.flags & PP_FLAG_DMA_MAP) {
257 first_page = page_pool_dma_map(pool, first_page);
258 if (!first_page)
259 return NULL;
260 }
261
262 /* Track how many pages are held 'in-flight' */
263 pool->pages_state_hold_cnt++;
264 trace_page_pool_state_hold(pool, first_page, pool->pages_state_hold_cnt);
265
266 /* When page just alloc'ed is should/must have refcnt 1. */
267 return first_page;
268 }
269
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC PATCH 3/3 mm: make zone->free_area[order] access faster
In reply to Jesper Dangaard BrouerAvoid multiplication (imul) operations when accessing:
zone->free_area[order].nr_free
This was really tricky to find. I was puzzled why perf reported that rmqueue_bulk was using 44% of the time in an imul operation:
│ del_page_from_free_list(): 44,54 │ e2: imul $0x58,%rax,%rax
This operation was generated (by compiler) because the struct free_area have size 88 bytes or 0x58 hex. The compiler cannot find a shift operation to use and instead choose to use a more expensive imul, to find the offset into the array free_area[].
The patch align struct free_area to a cache-line, which cause the compiler avoid the imul operation. The imul operation is very fast on modern Intel CPUs. To help fast-path that decrement 'nr_free' move the member 'nr_free' to be first element, which saves one 'add' operation.
Looking up instruction latency this exchange a 3-cycle imul with a 1-cycle shl, saving 2-cycles. It does trade some space to do this.
Used: gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
--- include/linux/mmzone.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/mmzone.h +4 -2
@@ -93,10 +93,12 @@ extern int page_group_by_mobility_disabled;#define get_pageblock_migratetype (page) \ get_pfnblock_flags_mask(page, page_to_pfn(page), MIGRATETYPE_MASK) +/* Aligned struct to make zone->free_area[order] access faster */ struct free_area { -struct list_head free_list[MIGRATE_TYPES ];unsigned long nr_free; -}; +unsigned long __pad_to_align_free_list; +struct list_head free_list[MIGRATE_TYPES ]; +}____cacheline_aligned_in_smp ;static inline struct page *get_page_from_free_area (struct free_area *area,int migratetype)
Re: [PATCH RFC net-next 3/3] mm: make zone->free_area[order] access faster
As a side-node, I didn't pick up the other patches as there is review feedback and I didn't have strong opinions either way. Patch 3 is curious though, it probably should be split out and sent separetly but still;
Quoted 19 lines · Jesper Dangaard Brouer
Avoid multiplication (imul) operations when accessing:
zone->free_area[order].nr_freeThis was really tricky to find. I was puzzled why perf reported that
rmqueue_bulkwas using 44% of the time in an imul operation:??? del_page_from_free_list():44,54 ??? e2: imul $0x58,%rax,%rax
This operation was generated (by compiler) because the struct
free_areahave size 88 bytes or0x58hex. The compiler cannot find a shift operation to use and instead choose to use a more expensive imul, to find the offset into the array free_area[].The patch align struct
free_areato a cache-line, which cause the compiler avoid the imul operation. The imul operation is very fast on modern Intel CPUs. To help fast-path that decrement 'nr_free' move the member 'nr_free' to be first element, which saves one 'add' operation.
Quoted 5 lines
Looking up instruction latency this exchange a 3-cycle imul with a 1-cycle shl, saving 2-cycles. It does trade some space to do this.
Used: gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
I'm having some trouble parsing this and matching it to the patch itself.
First off, on my system (x86-64), the size of struct free area is 72, not 88 bytes. For either size, cache-aligning the structure is a big increase in the struct size.
struct free_area {
struct list_head free_list[4]; /* 0 64 */
/* --- cacheline 1 boundary (64 bytes) --- */
long unsigned int nr_free; /* 64 8 */
/* size: 72, cachelines: 2, members: 2 */
/* last cacheline: 8 bytes */
};Are there other patches in the tree? What does pahole say?
With gcc-9, I'm also not seeing the imul instruction outputted like you described in rmqueue_pcplist which inlines rmqueue_bulk. At the point where it calls get_page_from_free_area, it's using shl for the page list operation. This might be a compiler glitch but given that free_area is a different size, I'm less certain and wonder if something else is going on.
Finally, moving nr_free to the end and cache aligning it will make the started of each free_list cache-aligned because of its location in the struct zone so what purpose does __pad_to_align_free_list serve?
Signature · 2 lines
Mel Gorman SUSE Labs
Re: [PATCH RFC net-next 3/3] mm: make zone->free_area[order] access faster
Quoted 27 lines · On Thu, 25 Feb 2021 11:28:49 +0000 Mel Gorman <[email protected]>…
As a side-node, I didn't pick up the other patches as there is review feedback and I didn't have strong opinions either way. Patch 3 is curious though, it probably should be split out and sent separetly but still;
Quoted 22 lines · Jesper Dangaard Brouer
Avoid multiplication (imul) operations when accessing:
zone->free_area[order].nr_freeThis was really tricky to find. I was puzzled why perf reported that
rmqueue_bulkwas using 44% of the time in an imul operation:??? del_page_from_free_list():44,54 ??? e2: imul $0x58,%rax,%rax
This operation was generated (by compiler) because the struct
free_areahave size 88 bytes or0x58hex. The compiler cannot find a shift operation to use and instead choose to use a more expensive imul, to find the offset into the array free_area[].The patch align struct
free_areato a cache-line, which cause the compiler avoid the imul operation. The imul operation is very fast on modern Intel CPUs. To help fast-path that decrement 'nr_free' move the member 'nr_free' to be first element, which saves one 'add' operation.Looking up instruction latency this exchange a 3-cycle imul with a 1-cycle shl, saving 2-cycles. It does trade some space to do this.
Quoted 8 lines
Quoted 2 lines
Used: gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
I'm having some trouble parsing this and matching it to the patch itself.
First off, on my system (x86-64), the size of struct free area is 72, not 88 bytes. For either size, cache-aligning the structure is a big increase in the struct size.
Yes, the increase in size is big. For the struct free_area 40 bytes for my case and 56 bytes for your case. The real problem is that this is multiplied by 11 (MAX_ORDER) and multiplied by number of zone structs (is it 5?). Thus, 56*11*5 = 3080 bytes.
Thus, I'm not sure it is worth it! As I'm only saving 2-cycles, for something that depends on the compiler generating specific code. And the compiler can easily change, and "fix" this on-its-own in a later release, and then we are just wasting memory.
I did notice this imul happens 45 times in mm/page_alloc.o, with this offset 0x58, but still this is likely not on hot-path.
Quoted 10 lines
struct free_area { struct list_head free_list[4]; /* 0 64 */ /* --- cacheline 1 boundary (64 bytes) --- */ long unsigned int nr_free; /* 64 8 */ /* size: 72, cachelines: 2, members: 2 */ /* last cacheline: 8 bytes */ };Are there other patches in the tree? What does pahole say?
The size of size of struct free_area varies based on some CONFIG setting, as free_list[] array size is determined by MIGRATE_TYPES, which on my system is 5, and not 4 as on your system.
struct list_head free_list[MIGRATE_TYPES];
CONFIG_CMA and CONFIG_MEMORY_ISOLATION both increase MIGRATE_TYPES with one. Thus, the array size can vary from 4 to 6.
Quoted 5 lines
With gcc-9, I'm also not seeing the imul instruction outputted like you described in
rmqueue_pcplistwhich inlinesrmqueue_bulk. At the point where it callsget_page_from_free_area, it's using shl for the page list operation. This might be a compiler glitch but given thatfree_areais a different size, I'm less certain and wonder if something else is going on.
I think it is the size variation.
Quoted 3 lines
Finally, moving
nr_freeto the end and cache aligning it will make the started of eachfree_listcache-aligned because of its location in the struct zone so what purpose does__pad_to_align_free_listserve?
The purpose of purpose of __pad_to_align_free_list is because struct list_head is 16 bytes, thus I wanted to align free_list to 16, given we already have wasted the space.
Notice I added some more detailed notes in[1]:
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Re: [PATCH RFC net-next 3/3] mm: make zone->free_area[order] access faster
Quoted 37 lines · Jesper Dangaard Brouer
Quoted 23 lines · Jesper Dangaard Brouer
Quoted 22 lines
Avoid multiplication (imul) operations when accessing:
zone->free_area[order].nr_freeThis was really tricky to find. I was puzzled why perf reported that
rmqueue_bulkwas using 44% of the time in an imul operation:??? del_page_from_free_list():44,54 ??? e2: imul $0x58,%rax,%rax
This operation was generated (by compiler) because the struct
free_areahave size 88 bytes or0x58hex. The compiler cannot find a shift operation to use and instead choose to use a more expensive imul, to find the offset into the array free_area[].The patch align struct
free_areato a cache-line, which cause the compiler avoid the imul operation. The imul operation is very fast on modern Intel CPUs. To help fast-path that decrement 'nr_free' move the member 'nr_free' to be first element, which saves one 'add' operation.Looking up instruction latency this exchange a 3-cycle imul with a 1-cycle shl, saving 2-cycles. It does trade some space to do this.
Quoted 8 lines
Quoted 2 lines
Used: gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
I'm having some trouble parsing this and matching it to the patch itself.
First off, on my system (x86-64), the size of struct free area is 72, not 88 bytes. For either size, cache-aligning the structure is a big increase in the struct size.
Yes, the increase in size is big. For the struct
free_area40 bytes for my case and 56 bytes for your case. The real problem is that this is multiplied by 11 (MAX_ORDER) and multiplied by number of zone structs (is it 5?). Thus, 56*11*5 = 3080 bytes.
Quoted 8 lines
Thus, I'm not sure it is worth it! As I'm only saving 2-cycles, for something that depends on the compiler generating specific code. And the compiler can easily change, and "fix" this on-its-own in a later release, and then we are just wasting memory.
I did notice this imul happens 45 times in
mm/page_alloc.o, with this offset0x58, but still this is likely not on hot-path.
Yeah, I'm not convinced it's worth it. The benefit of 2 cycles is small and it's config-dependant. While some configurations will benefit, others do not but the increased consumption is universal. I think there are better ways to save 2 cycles in the page allocator and this seems like a costly micro-optimisation.
Quoted 10 lines
Quoted 7 lines
<SNIP>
With gcc-9, I'm also not seeing the imul instruction outputted like you described in
rmqueue_pcplistwhich inlinesrmqueue_bulk. At the point where it callsget_page_from_free_area, it's using shl for the page list operation. This might be a compiler glitch but given thatfree_areais a different size, I'm less certain and wonder if something else is going on.I think it is the size variation.
Yes.
Quoted 8 lines
Quoted 3 lines
Finally, moving
nr_freeto the end and cache aligning it will make the started of eachfree_listcache-aligned because of its location in the struct zone so what purpose does__pad_to_align_free_listserve?The purpose of purpose of
__pad_to_align_free_listis because structlist_headis 16 bytes, thus I wanted to alignfree_listto 16, given we already have wasted the space.
Ok, that's fair enough but it's also somewhat of a micro-optimisation as whether it helps or not depends on the architecture.
I don't think I'll pick this up, certainly in the context of the bulk allocator but it's worth keeping in mind. It's an interesting corner case at least.
Signature · 2 lines
Mel Gorman SUSE Labs
Re: [PATCH RFC net-next 3/3] mm: make zone->free_area[order] access faster
Quoted 75 lines · On Thu, 25 Feb 2021 15:38:15 +0000 Mel Gorman <[email protected]>…
Quoted 37 lines · Jesper Dangaard Brouer
Quoted 23 lines · Jesper Dangaard Brouer
Quoted 22 lines
Avoid multiplication (imul) operations when accessing:
zone->free_area[order].nr_freeThis was really tricky to find. I was puzzled why perf reported that
rmqueue_bulkwas using 44% of the time in an imul operation:??? del_page_from_free_list():44,54 ??? e2: imul $0x58,%rax,%rax
This operation was generated (by compiler) because the struct
free_areahave size 88 bytes or0x58hex. The compiler cannot find a shift operation to use and instead choose to use a more expensive imul, to find the offset into the array free_area[].The patch align struct
free_areato a cache-line, which cause the compiler avoid the imul operation. The imul operation is very fast on modern Intel CPUs. To help fast-path that decrement 'nr_free' move the member 'nr_free' to be first element, which saves one 'add' operation.Looking up instruction latency this exchange a 3-cycle imul with a 1-cycle shl, saving 2-cycles. It does trade some space to do this.
Quoted 8 lines
Quoted 2 lines
Used: gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
I'm having some trouble parsing this and matching it to the patch itself.
First off, on my system (x86-64), the size of struct free area is 72, not 88 bytes. For either size, cache-aligning the structure is a big increase in the struct size.
Yes, the increase in size is big. For the struct
free_area40 bytes for my case and 56 bytes for your case. The real problem is that this is multiplied by 11 (MAX_ORDER) and multiplied by number of zone structs (is it 5?). Thus, 56*11*5 = 3080 bytes.Quoted 8 lines
Thus, I'm not sure it is worth it! As I'm only saving 2-cycles, for something that depends on the compiler generating specific code. And the compiler can easily change, and "fix" this on-its-own in a later release, and then we are just wasting memory.
I did notice this imul happens 45 times in
mm/page_alloc.o, with this offset0x58, but still this is likely not on hot-path.Yeah, I'm not convinced it's worth it. The benefit of 2 cycles is small and it's config-dependant. While some configurations will benefit, others do not but the increased consumption is universal. I think there are better ways to save 2 cycles in the page allocator and this seems like a costly micro-optimisation.
Quoted 10 lines
Quoted 7 lines
<SNIP>
With gcc-9, I'm also not seeing the imul instruction outputted like you described in
rmqueue_pcplistwhich inlinesrmqueue_bulk. At the point where it callsget_page_from_free_area, it's using shl for the page list operation. This might be a compiler glitch but given thatfree_areais a different size, I'm less certain and wonder if something else is going on.I think it is the size variation.
Yes.
Quoted 8 lines
Quoted 3 lines
Finally, moving
nr_freeto the end and cache aligning it will make the started of eachfree_listcache-aligned because of its location in the struct zone so what purpose does__pad_to_align_free_listserve?The purpose of purpose of
__pad_to_align_free_listis because structlist_headis 16 bytes, thus I wanted to alignfree_listto 16, given we already have wasted the space.
Quoted 6 lines
Ok, that's fair enough but it's also somewhat of a micro-optimisation as whether it helps or not depends on the architecture.
I don't think I'll pick this up, certainly in the context of the bulk allocator but it's worth keeping in mind. It's an interesting corner case at least.
I fully agree. Lets drop this patch.
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
v2 cover Use bulk order-0 page allocator API for page_pool
In reply to Mel GormanThis is a followup to Mel Gorman's patchset:
- Message-Id: <[email protected]>
- https://lore.kernel.org/netdev/[email protected]/
Showing page_pool usage of the API for alloc_pages_bulk().
Maybe Mel Gorman will/can carry these patches? (to keep it together with the alloc_pages_bulk API)
---
Jesper Dangaard Brouer (2):
net: page_pool: refactor dma_map into own function page_pool_dma_map
net: page_pool: use alloc_pages_bulk in refill code path
net/core/page_pool.c | 102 +++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 39 deletions(-)
--
RFC PATCH v2 1/2 net: page_pool: refactor dma_map into own function page_pool_dma_map
In preparation for next patch, move the dma mapping into its own function, as this will make it easier to follow the changes.
V2: make page_pool_dma_map return boolean (Ilias)
--- net/core/page_pool.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-)
net/core/page_pool.c +26 -19
@@ -180,14 +180,37 @@ static void page_pool_dma_sync_for_device(struct page_pool *pool, pool->p.dma_dir); } +static bool page_pool_dma_map (struct page_pool *pool,struct page *page) +{ +dma_addr_t dma; + +/* Setup DMA mapping: use 'struct page' area for storing DMA-addr +* since dma_addr_t can be either 32 or 64 bits and does not always fit +* into page private data (i.e 32bit cpu with 64bit DMA caps) +* This mapping is kept for lifetime of page, until leaving pool. +*/ + dma =dma_map_page_attrs (pool->p.dev, page,0 , + (PAGE_SIZE << pool->p.order), + pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); +if (dma_mapping_error (pool->p.dev, dma)) +return false; + + page->dma_addr = dma; + +if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) +page_pool_dma_sync_for_device (pool, page, pool->p.max_len); + +return true; +} +/* slow path */ noinline static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,gfp_t _gfp) { +unsigned int pp_flags = pool->p.flags;struct page *page;gfp_t gfp = _gfp; -dma_addr_t dma;/* We could always set __GFP_COMP, and avoid this branch, as * prep_new_page() can handle order-0 with __GFP_COMP.
@@ -211,30 +234,14 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,if (!page)return NULL ; -if (!(pool->p.flags &PP_FLAG_DMA_MAP )) - goto skip_dma_map; - -/* Setup DMA mapping: use 'struct page' area for storing DMA-addr -* since dma_addr_t can be either 32 or 64 bits and does not always fit -* into page private data (i.e 32bit cpu with 64bit DMA caps) -* This mapping is kept for lifetime of page, until leaving pool. -*/ - dma =dma_map_page_attrs (pool->p.dev, page,0 , - (PAGE_SIZE << pool->p.order), - pool->p.dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); -if (dma_mapping_error (pool->p.dev, dma)) { +if (pp_flags &PP_FLAG_DMA_MAP && +unlikely (!page_pool_dma_map (pool, page))) {put_page (page);return NULL ; } - page->dma_addr = dma; -if (pool->p.flags &PP_FLAG_DMA_SYNC_DEV ) -page_pool_dma_sync_for_device (pool, page, pool->p.max_len); - -skip_dma_map:/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; -trace_page_pool_state_hold (pool, page, pool->pages_state_hold_cnt);/* When page just alloc'ed is should/must have refcnt 1. */
RFC PATCH v2 2/2 net: page_pool: use alloc_pages_bulk in refill code path
In reply to Jesper Dangaard BrouerThere are cases where the page_pool need to refill with pages from the page allocator. Some workloads cause the page_pool to release pages instead of recycling these pages.
For these workload it can improve performance to bulk alloc pages from the page-allocator to refill the alloc cache.
For XDP-redirect workload with 100G mlx5 driver (that use page_pool) redirecting xdp_frame packets into a veth, that does XDP_PASS to create an SKB from the xdp_frame, which then cannot return the page to the page_pool. In this case, we saw[1] an improvement of 18.8% from using the alloc_pages_bulk API (3,677,958 pps -> 4,368,926 pps).
[1] https://github.com/xdp-project/xdp-project/blob/master/areas/mem/page_pool06_alloc_pages_bulk.org
--- net/core/page_pool.c | 63 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 23 deletions(-)
net/core/page_pool.c +40 -23
@@ -208,44 +208,61 @@ noinlinestatic struct page *__page_pool_alloc_pages_slow (struct page_pool *pool,gfp_t _gfp) { +const int bulk =PP_ALLOC_CACHE_REFILL ; +struct page *page, *next, *first_page;unsigned int pp_flags = pool->p.flags; -struct page *page; +unsigned int pp_order = pool->p.order; +int pp_nid = pool->p.nid; +LIST_HEAD (page_list);gfp_t gfp = _gfp; -/* We could always set __GFP_COMP, and avoid this branch, as -* prep_new_page() can handle order-0 with __GFP_COMP. -*/ -if (pool->p.order) +/* Don't support bulk alloc for high-order pages */ +if (unlikely (pp_order)) { gfp |= __GFP_COMP; + first_page =alloc_pages_node (pp_nid, gfp, pp_order); +if (unlikely (!first_page)) +return NULL ; + goto out; + } -/* FUTURE development: -* -* Current slow-path essentially falls back to single page -* allocations, which doesn't improve performance. This code -* need bulk allocation support from the page allocator code. -*/ - -/* Cache was empty, do real allocation */ -#ifdef CONFIG_NUMA - page =alloc_pages_node (pool->p.nid, gfp, pool->p.order); -#else - page =alloc_pages (gfp, pool->p.order); -#endif -if (!page) +if (unlikely (!__alloc_pages_bulk_nodemask (gfp, pp_nid,NULL , + bulk, &page_list)))return NULL ; +/* First page is extracted and returned to caller */ + first_page = list_first_entry(&page_list, struct page, lru); +list_del (&first_page->lru); + +/* Remaining pages store in alloc.cache */ +list_for_each_entry_safe (page, next, &page_list, lru) { +list_del (&page->lru); +if (pp_flags &PP_FLAG_DMA_MAP && +unlikely (!page_pool_dma_map (pool, page))) { +put_page (page); +continue ; + } +if (likely (pool->alloc.count <PP_ALLOC_CACHE_SIZE )) { + pool->alloc.cache[pool->alloc.count++] = page; + pool->pages_state_hold_cnt++; +trace_page_pool_state_hold (pool, page, + pool->pages_state_hold_cnt); + }else { +put_page (page); + } + } +out:if (pp_flags &PP_FLAG_DMA_MAP && -unlikely (!page_pool_dma_map (pool, page))) { -put_page (page); +unlikely (!page_pool_dma_map (pool, first_page))) { +put_page (first_page);return NULL ; }/* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; -trace_page_pool_state_hold (pool, page, pool->pages_state_hold_cnt); +trace_page_pool_state_hold (pool, first_page, pool->pages_state_hold_cnt);/* When page just alloc'ed is should/must have refcnt 1. */ -return page; +return first_page; }/* For using page_pool replace: alloc_pages() API calls, but provide
kernel test robot
build botRe: [PATCH RFC V2 net-next 2/2] net: page_pool: use alloc_pages_bulk in refill code path
reproduce (this is a W=1 build):
Hi Jesper,
[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on net-next/master]
url: https://github.com/0day-ci/linux/commits/Jesper-Dangaard-Brouer/net-page_pool-refactor-dma_map-into-own-function-page_pool_dma_map/20210301-223012 base: https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git d310ec03a34e92a77302edb804f7d68ee4f01ba0 config: x86_64-kexec (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/1a84d75cc5924b2e673d4a4d5524a283e52e7b63 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Jesper-Dangaard-Brouer/net-page_pool-refactor-dma_map-into-own-function-page_pool_dma_map/20210301-223012 git checkout 1a84d75cc5924b2e673d4a4d5524a283e52e7b63 # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/export.h:43,
from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from net/core/page_pool.c:9:
net/core/page_pool.c: In function '__page_pool_alloc_pages_slow':
>> net/core/page_pool.c:228:16: error: implicit declaration of function '__alloc_pages_bulk_nodemask'; did you mean '__alloc_pages_nodemask'? [-Werror=implicit-function-declaration]
228 | if (unlikely(!__alloc_pages_bulk_nodemask(gfp, pp_nid, NULL,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/compiler.h:78:42: note: in definition of macro 'unlikely'
78 | # define unlikely(x) __builtin_expect(!!(x), 0)
| ^cc1: some warnings being treated as errors
vim +228 net/core/page_pool.c
205
206 /* slow path */
207 noinline
208 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
209 gfp_t _gfp)
210 {
211 const int bulk = PP_ALLOC_CACHE_REFILL;
212 struct page *page, *next, *first_page;
213 unsigned int pp_flags = pool->p.flags;
214 unsigned int pp_order = pool->p.order;
215 int pp_nid = pool->p.nid;
216 LIST_HEAD(page_list);
217 gfp_t gfp = _gfp;
218
219 /* Don't support bulk alloc for high-order pages */
220 if (unlikely(pp_order)) {
221 gfp |= __GFP_COMP;
222 first_page = alloc_pages_node(pp_nid, gfp, pp_order);
223 if (unlikely(!first_page))
224 return NULL;
225 goto out;
226 }
227
> 228 if (unlikely(!__alloc_pages_bulk_nodemask(gfp, pp_nid, NULL,
229 bulk, &page_list)))
230 return NULL;
231
232 /* First page is extracted and returned to caller */
233 first_page = list_first_entry(&page_list, struct page, lru);
234 list_del(&first_page->lru);
235
236 /* Remaining pages store in alloc.cache */
237 list_for_each_entry_safe(page, next, &page_list, lru) {
238 list_del(&page->lru);
239 if (pp_flags & PP_FLAG_DMA_MAP &&
240 unlikely(!page_pool_dma_map(pool, page))) {
241 put_page(page);
242 continue;
243 }
244 if (likely(pool->alloc.count < PP_ALLOC_CACHE_SIZE)) {
245 pool->alloc.cache[pool->alloc.count++] = page;
246 pool->pages_state_hold_cnt++;
247 trace_page_pool_state_hold(pool, page,
248 pool->pages_state_hold_cnt);
249 } else {
250 put_page(page);
251 }
252 }
253 out:
254 if (pp_flags & PP_FLAG_DMA_MAP &&
255 unlikely(!page_pool_dma_map(pool, first_page))) {
256 put_page(first_page);
257 return NULL;
258 }
259
260 /* Track how many pages are held 'in-flight' */
261 pool->pages_state_hold_cnt++;
262 trace_page_pool_state_hold(pool, first_page, pool->pages_state_hold_cnt);
263
264 /* When page just alloc'ed is should/must have refcnt 1. */
265 return first_page;
266 }
267
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org