Skip to contentSkip to conversation

Introduce a bulk order-0 page allocator with two in-tree users

PATCH In review Mel Gorman 5 participants 15 messages Mar 1, 2021 – Mar 12, 2021

Subsystems (8)
lore
Share & tools
lore

Mel Gorman

author
#0 lore raw

cover Introduce a bulk order-0 page allocator with two in-tree users

This series introduces a bulk order-0 page allocator with sunrpc and the network page pool being the first users. The implementation is not particularly efficient and the intention is to iron out what the semantics of the API should have for users. Once the semantics are ironed out, it can be made more efficient.

Improving the implementation requires 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 overall -- 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).

Light testing passed, I'm relying on Chuck and Jesper to test the target users more aggressively but both report performance improvements with the initial RFC.

Patch 1 of this series is a cleanup to sunrpc, it could be merged
separately but is included here as a pre-requisite.

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?)

Patch 4 is a preparation patch only for the network user

Patch 5 converts the net page pool to the bulk allocator for order-0 pages.

 include/linux/gfp.h   |  13 +++++
 mm/page_alloc.c       | 113 +++++++++++++++++++++++++++++++++++++++++-
 net/core/page_pool.c  | 102 +++++++++++++++++++++++---------------
 net/sunrpc/svc_xprt.c |  47 ++++++++++++------
 4 files changed, 220 insertions(+), 55 deletions(-)
Signature · 1 line
2.26.2
author
#1 lore raw

PATCH 1/5 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
index dcc50ae54550..cfa7e4776d0e 100644
@@ -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
author
#2 lore raw

PATCH 2/5 mm/page_alloc: Add a bulk page allocator

In reply to Mel Gorman

This 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
index 8572a1474e16..4903d1cc48dc 100644
@@ -515,6 +515,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);
@@ -525,6 +529,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().
@@ -594,6 +606,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
#3 lore raw

Re: [PATCH 2/5] mm/page_alloc: Add a bulk page allocator

Would vmalloc be another good user of this API?

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5004

Quoted 2 lines
mm/page_alloc.c +2 -0
+	/* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */
+	if (!prepare_alloc_pages(gfp_mask, 0, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags))

This crazy long line is really hard to follow.

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5009

Quoted 5 lines
mm/page_alloc.c +5 -0
+		return 0;
+	gfp_mask = alloc_mask;
+
+	/* Find an allowed local zone that meets the high watermark. */
+	for_each_zone_zonelist_nodemask(zone, z, ac.zonelist, ac.highest_zoneidx, ac.nodemask) {

Same here.

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5015

Quoted 6 lines
mm/page_alloc.c +6 -0
+		unsigned long mark;
+
+		if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) &&
+		    !__cpuset_zone_allowed(zone, gfp_mask)) {
+			continue;
+		}

No need for the curly braces.

mm/page_alloc.c __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, · line 4985

Quoted 4 lines
mm/page_alloc.c +0 -2
 	}
 
-	gfp_mask &= gfp_allowed_mask;
-	alloc_mask = gfp_mask;

Is this change intentional?

author
#4 lore raw

Re: [PATCH 2/5] mm/page_alloc: Add a bulk page allocator

Quoted 7 lines · Christoph Hellwig

Would vmalloc be another good user of this API?

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5004

Quoted 2 lines
mm/page_alloc.c +2 -0
+	/* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */
+	if (!prepare_alloc_pages(gfp_mask, 0, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags))

This crazy long line is really hard to follow.

It's not crazier than what is already in alloc_pages_nodemask to share code.

Quoted 8 lines

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5009

Quoted 5 lines
mm/page_alloc.c +5 -0
+		return 0;
+	gfp_mask = alloc_mask;
+
+	/* Find an allowed local zone that meets the high watermark. */
+	for_each_zone_zonelist_nodemask(zone, z, ac.zonelist, ac.highest_zoneidx, ac.nodemask) {

Same here.

Similar to what happens in get_page_from_freelist with the for_next_zone_zonelist_nodemask iterator.

Quoted 9 lines

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5015

Quoted 6 lines
mm/page_alloc.c +6 -0
+		unsigned long mark;
+
+		if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) &&
+		    !__cpuset_zone_allowed(zone, gfp_mask)) {
+			continue;
+		}

No need for the curly braces.

Yes, but it's for coding style. MM has no hard coding style guidelines around this but for sched, it's generally preferred that if the "if" statement spans multiple lines then it should use {} even if the block is one line long for clarity.

Quoted 6 lines

mm/page_alloc.c __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, · line 4985

Quoted 4 lines
mm/page_alloc.c +0 -2
 	}
 
-	gfp_mask &= gfp_allowed_mask;
-	alloc_mask = gfp_mask;

Is this change intentional?

Yes so that prepare_alloc_pages works for both the single page and bulk allocator. Slightly less code duplication.

Signature · 2 lines
Mel Gorman
SUSE Labs
#5 lore raw

Re: [PATCH 2/5] mm/page_alloc: Add a bulk page allocator

In reply to Mel Gorman
Quoted 35 lines · Mel Gorman
include/linux/gfp.h +4 -0
index 8572a1474e16..4903d1cc48dc 100644
@@ -515,6 +515,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,

include/linux/gfp.h +5 -0
 							nodemask_t 
 *nodemask);
@@ -525,6 +529,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,

include/linux/gfp.h +1 -0
+							nr_pages, 
Quoted 1 line

list);

Is the second line indentation intentional ? Why not align it to the first argument (gfp_mask) ?

Quoted 38 lines
include/linux/gfp.h +3 -0
+}
+
 /*
  * Allocate pages, preferring the node given as nid. The node 
  must be valid and
  * online. For more general interface, see alloc_pages_node().
@@ -594,6 +606,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 +2 -0
index 3e4b29ee2b1e..ff1e55793786 100644
@@ -4436,6 +4436,21 @@ static void wake_all_kswapds(unsigned int 
 order, gfp_t gfp_mask,
 	}
 }
...
 
+/*
+ * This is a batched version of the page allocator that 

attempts to
+ * allocate nr_pages quickly from the preferred zone and add them to list.
+ */
+int __alloc_pages_bulk_nodemask(gfp_t gfp_mask, int preferred_nid,

mm/page_alloc.c +3 -0
+			nodemask_t *nodemask, int nr_pages,
+			struct list_head *alloc_list)
+{

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 4998

Quoted 10 lines
mm/page_alloc.c +10 -0
+	struct page *page;
+	unsigned long flags;
+	struct zone *zone;
+	struct zoneref *z;
+	struct per_cpu_pages *pcp;
+	struct list_head *pcp_list;
+	struct alloc_context ac;
+	gfp_t alloc_mask;
+	unsigned int alloc_flags;
+	int alloced = 0;

Does alloced count the number of allocated pages ? Do you mind renaming it to 'allocated' ?

Quoted 49 lines
mm/page_alloc.c +5 -0
+
+	if (nr_pages == 1)
+		goto failed;
+
+	/* May set ALLOC_NOFRAGMENT, fragmentation will return 1 

page. */

mm/page_alloc.c +1 -0
+	if (!prepare_alloc_pages(gfp_mask, 0, preferred_nid, 

nodemask, &ac, &alloc_mask, &alloc_flags))

mm/page_alloc.c +4 -0
+		return 0;
+	gfp_mask = alloc_mask;
+
+	/* Find an allowed local zone that meets the high 

watermark. */

mm/page_alloc.c +1 -0
+	for_each_zone_zonelist_nodemask(zone, z, ac.zonelist, 

ac.highest_zoneidx, ac.nodemask) {

mm/page_alloc.c +3 -0
+		unsigned long mark;
+
+		if (cpusets_enabled() && (alloc_flags & 

ALLOC_CPUSET) &&

mm/page_alloc.c +5 -0
+		    !__cpuset_zone_allowed(zone, gfp_mask)) {
+			continue;
+		}
+
+		if (nr_online_nodes > 1 && zone != 

ac.preferred_zoneref->zone &&

mm/page_alloc.c +1 -0
+		    zone_to_nid(zone) != 

zone_to_nid(ac.preferred_zoneref->zone)) {

mm/page_alloc.c +4 -0
+			goto failed;
+		}
+
+		mark = wmark_pages(zone, alloc_flags & 

ALLOC_WMARK_MASK) + nr_pages;

mm/page_alloc.c +2 -0
+		if (zone_watermark_fast(zone, 0,  mark,
+ 

zonelist_zone_idx(ac.preferred_zoneref),

mm/page_alloc.c +14 -0
+				alloc_flags, gfp_mask)) {
+			break;
+		}
+	}
+	if (!zone)
+		return 0;
+
+	/* Attempt the batch allocation */
+	local_irq_save(flags);
+	pcp = &this_cpu_ptr(zone->pageset)->pcp;
+	pcp_list = &pcp->lists[ac.migratetype];
+
+	while (alloced < nr_pages) {
+		page = __rmqueue_pcplist(zone, ac.migratetype, 
Quoted 3 lines

alloc_flags,
+
pcp, pcp_list);

Same indentation comment as before

Quoted 49 lines
mm/page_alloc.c +13 -0
+		if (!page)
+			break;
+
+		prep_new_page(page, 0, gfp_mask, 0);
+		list_add(&page->lru, alloc_list);
+		alloced++;
+	}
+
+	if (!alloced)
+		goto failed_irq;
+
+	if (alloced) {
+		__count_zid_vm_events(PGALLOC, zone_idx(zone), 

alloced);

mm/page_alloc.c +12 -0
+		zone_statistics(zone, zone);
+	}
+
+	local_irq_restore(flags);
+
+	return alloced;
+
+failed_irq:
+	local_irq_restore(flags);
+
+failed:
+	page = __alloc_pages_nodemask(gfp_mask, 0, preferred_nid, 

nodemask);

mm/page_alloc.c +9 -2
+	if (page) {
+		alloced++;
+		list_add(&page->lru, alloc_list);
+	}
+
+	return alloced;
+}
+EXPORT_SYMBOL_GPL(__alloc_pages_bulk_nodemask);
+
 /*
  * This is the 'heart' of the zoned buddy allocator.
  */
@@ -4981,8 +5092,6 @@ __alloc_pages_nodemask(gfp_t gfp_mask, 
 unsigned int order, int preferred_nid,
 		return NULL;
 	}
 
-	gfp_mask &= gfp_allowed_mask;
-	alloc_mask = gfp_mask;
 	if (!prepare_alloc_pages(gfp_mask, order, preferred_nid, 
 nodemask, &ac, &alloc_mask, &alloc_flags))

return NULL;

author
#6 lore raw

Re: [PATCH 2/5] mm/page_alloc: Add a bulk page allocator

Quoted 26 lines · Shay Agroskin
Quoted 23 lines · Mel Gorman
include/linux/gfp.h +3 -0
index 8572a1474e16..4903d1cc48dc 100644
@@ -515,6 +515,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,

include/linux/gfp.h +2 -0
 							nodemask_t  *nodemask);
@@ -525,6 +529,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
Quoted 9 lines

include/linux/gfp.h __alloc_pages(gfp_t gfp_mask, unsigned int order, int preferred_nid) · line 537

Quoted 5 lines
include/linux/gfp.h +4 -0
+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);

Is the second line indentation intentional ? Why not align it to the first argument (gfp_mask) ?

No particular reason. I usually pick this as it's visually clearer to me that it's part of the same line when the multi-line is part of an if block.

Quoted 36 lines
Quoted 36 lines
include/linux/gfp.h +2 -0
+}
+
 /*
  * Allocate pages, preferring the node given as nid. The node   must be

valid and
* online. For more general interface, see alloc_pages_node().

include/linux/gfp.h +1 -0
@@ -594,6 +606,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 +3 -0
index 3e4b29ee2b1e..ff1e55793786 100644
@@ -4436,6 +4436,21 @@ static void wake_all_kswapds(unsigned int order,
 gfp_t gfp_mask,
 	}
 }
...
+/*
+ * This is a batched version of the page allocator that attempts to
+ * allocate nr_pages quickly from the preferred zone and add them to

list.
+ */

mm/page_alloc.c +8 -0
+int __alloc_pages_bulk_nodemask(gfp_t gfp_mask, int preferred_nid,
+			nodemask_t *nodemask, int nr_pages,
+			struct list_head *alloc_list)
+{
+	struct page *page;
+	unsigned long flags;
+	struct zone *zone;
+	struct zoneref *z;
Quoted 8 lines

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 4998

Quoted 6 lines
mm/page_alloc.c +6 -0
+	struct per_cpu_pages *pcp;
+	struct list_head *pcp_list;
+	struct alloc_context ac;
+	gfp_t alloc_mask;
+	unsigned int alloc_flags;
+	int alloced = 0;

Does alloced count the number of allocated pages ?

Yes.

Quoted 1 line

Do you mind renaming it to 'allocated' ?

I will if there is another version as I do not feel particularly strongly about alloced vs allocated. alloc was to match the function name and I don't think the change makes it much clearer.

Quoted 12 lines

mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5039

Quoted 9 lines

<SNIP>

mm/page_alloc.c +8 -0
+	/* Attempt the batch allocation */
+	local_irq_save(flags);
+	pcp = &this_cpu_ptr(zone->pageset)->pcp;
+	pcp_list = &pcp->lists[ac.migratetype];
+
+	while (alloced < nr_pages) {
+		page = __rmqueue_pcplist(zone, ac.migratetype, alloc_flags,
+ pcp, pcp_list);

Same indentation comment as before

Again, simple personal perference to avoid any possibility it's mixed up with a later line. There has not been consistent code styling enforcement of what indentation style should be used for a multi-line within mm/page_alloc.c

Signature · 2 lines
Mel Gorman
SUSE Labs
#7 lore raw

Re: [PATCH 2/5] mm/page_alloc: Add a bulk page allocator

Quoted 42 lines · On Wed, 10 Mar 2021 11:38:36 +0000 Mel Gorman <[email protected]>…
Quoted 26 lines · Shay Agroskin
Quoted 23 lines · Mel Gorman
include/linux/gfp.h +3 -0
index 8572a1474e16..4903d1cc48dc 100644
@@ -515,6 +515,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,

include/linux/gfp.h +2 -0
 							nodemask_t  *nodemask);
@@ -525,6 +529,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
Quoted 9 lines

include/linux/gfp.h __alloc_pages(gfp_t gfp_mask, unsigned int order, int preferred_nid) · line 537

Quoted 5 lines
include/linux/gfp.h +4 -0
+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);  

Is the second line indentation intentional ? Why not align it to the first argument (gfp_mask) ?

No particular reason. I usually pick this as it's visually clearer to me that it's part of the same line when the multi-line is part of an if block.

Quoted 2 lines
Quoted 2 lines
include/linux/gfp.h +2 -0
+}
+

[...]

Quoted 8 lines
Quoted 3 lines

Same indentation comment as before

Again, simple personal perference to avoid any possibility it's mixed up with a later line. There has not been consistent code styling enforcement of what indentation style should be used for a multi-line within mm/page_alloc.c

Hi Shay, it is might be surprising that indentation style actually differs slightly in different parts of the kernel. I started in networking area of the kernel, and I was also surprised when I started working in MM area that the coding style differs. I can tell you that the indentation style Mel choose is consistent with the code styling in MM area. I usually respect that even-though I prefer the networking style as I was "raised" with that style.

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
author
#8 lore raw

PATCH 3/5 SUNRPC: Refresh rq_pages using a bulk page allocator

In reply to Mel Gorman

From: 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
index cfa7e4776d0e..38a8d6283801 100644
@@ -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
author
#9 lore raw

PATCH 4/5 net: page_pool: refactor dma_map into own function page_pool_dma_map

In reply to Mel Gorman

From: Jesper Dangaard Brouer <[email protected]>

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
index ad8b0707af04..a26f2ceb6a87 100644
@@ -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. */
Signature · 1 line
2.26.2
maintainer Reviewed-by on v1 4/5
#10 lore raw

Re: [PATCH 4/5] net: page_pool: refactor dma_map into own function page_pool_dma_map

Hi Mel,

Can you please CC me in future revisions. I almost missed that!

Quoted 7 lines · Mel Gorman

From: Jesper Dangaard Brouer <[email protected]>

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)

[...]

Quoted 17 lines

static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,

net/core/page_pool.c +1 -4
 						 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;
-

net/core/page_pool.c static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, · line 237

Quoted 10 lines
net/core/page_pool.c +1 -9
-	/* 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 &&

Nit pick but can we have if ((pp_flags & PP_FLAG_DMA_MAP) && ...

Quoted 19 lines
net/core/page_pool.c +1 -6
+	    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. */
Signature · 1 line
2.26.2

Otherwise

author
#11 lore raw

Re: [PATCH 4/5] net: page_pool: refactor dma_map into own function page_pool_dma_map

Quoted 4 lines · Ilias Apalodimas

Hi Mel,

Can you please CC me in future revisions. I almost missed that!

Will do.

Quoted 15 lines · Mel Gorman
Quoted 7 lines

From: Jesper Dangaard Brouer <[email protected]>

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)

[...]

Quoted 4 lines
net/core/page_pool.c +0 -0
@@ -211,30 +234,14 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
 	if (!page)
 		return NULL;
 
Quoted 16 lines

net/core/page_pool.c static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, · line 237

Quoted 13 lines
net/core/page_pool.c +1 -12
-	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 &&

Nit pick but can we have if ((pp_flags & PP_FLAG_DMA_MAP) && ...

Done.

Quoted 6 lines

[...]

Quoted 1 line

Otherwise

Thanks. I'll wait for other review feedback before sending a V2.

Signature · 2 lines
Mel Gorman
SUSE Labs
#12 lore raw

Re: [PATCH 4/5] net: page_pool: refactor dma_map into own function page_pool_dma_map

Quoted 33 lines · On Wed, 3 Mar 2021 09:18:25 +0000 Mel Gorman <[email protected]>…
Quoted 15 lines · Ilias Apalodimas
Quoted 7 lines · Mel Gorman

From: Jesper Dangaard Brouer <[email protected]>

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)

[...]

Quoted 4 lines
net/core/page_pool.c +0 -0
@@ -211,30 +234,14 @@ static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
 	if (!page)
 		return NULL;
 
Quoted 16 lines

net/core/page_pool.c static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool, · line 237

Quoted 13 lines
net/core/page_pool.c +1 -12
-	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 &&  

Nit pick but can we have if ((pp_flags & PP_FLAG_DMA_MAP) && ...

Quoted 1 line

Done.

Thanks for fixing this nitpick, and carrying the patch.

Signature · 4 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer
author
#13 lore raw

PATCH 5/5 net: page_pool: use alloc_pages_bulk in refill code path

In reply to Mel Gorman

From: Jesper Dangaard Brouer <[email protected]>

There 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
index a26f2ceb6a87..567680bd91c4 100644
@@ -208,44 +208,61 @@ noinline
 static 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
Signature · 1 line
2.26.2
maintainer Reviewed-by on v1 5/5
#14 lore raw

Re: [PATCH 5/5] net: page_pool: use alloc_pages_bulk in refill code path

Quoted 115 lines · Mel Gorman

From: Jesper Dangaard Brouer <[email protected]>

There 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
index a26f2ceb6a87..567680bd91c4 100644
@@ -208,44 +208,61 @@ noinline
 static 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
Signature · 1 line
2.26.2