Skip to contentSkip to conversation

mm: make watermark checks honour ALLOC_NOFRAGMENT

PATCH Superseded Kiryl Shutsemau (Meta) 3 participants 15 messages Nov 2 – Nov 16

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm: make watermark checks honour ALLOC_NOFRAGMENT

With defrag_mode, 64K mTHP faults under memory pressure stall in compaction 18 times as often as without it, and get about a third of the folios. Dev Jain reported it [1].

The high-order watermark check counts free pages of every migratetype. With defrag_mode, every slowpath allocation is ALLOC_NOFRAGMENT, and a request below pageblock_order cannot take free pages from another migratetype's pageblock. Under pressure, that is where the large free pages end up. Two callers act on the wrong answer:

- direct compaction's up-front check returns COMPACT_SUCCESS without

    compacting, so compaction is never deferred;

- direct reclaim for a costly order stops early, having freed little.

Patches 1-2 give the check the request's migratetype and make it skip the free lists the request cannot take. Patch 3 tells direct reclaim that the allocation is ALLOC_NOFRAGMENT.

Dev's benchmark in a 7G VM with only 64K mTHP enabled, medians of 15 runs:

defrag_mode=0 defrag_mode=1 defrag_mode=1
+ series
compact_stall 1690 30669 7366
64K faults allocated 20750 7391 20129
64K faults falling back to 4K 725 13832 1339

defrag_mode=1 still compacts more than defrag_mode=0: by design, it keeps 64K faults out of unmovable and reclaimable pageblocks [2]. defrag_mode=0 is unchanged: 20698 64K faults allocated with the series.

v2:
- Patch 2 changelog: describe what the fast path change does to pages on the per-cpu lists (Andrew); drop the should_reclaim_retry() paragraph
- Patch 3 changelog: say that the benchmark baseline already has the high-order check honouring ALLOC_NOFRAGMENT (Andrew)
- Rebased onto v7.4-rc1

v1: https://lore.kernel.org/all/[email protected]/

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/auCDnqJc7fsBpUW2@thinkstation/

Kiryl Shutsemau (Meta) (3):
mm: page_alloc: add a watermark check that knows the migratetype
mm: page_alloc: make the high-order watermark check honour
ALLOC_NOFRAGMENT
mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT

 include/linux/mmzone.h |  3 --
 mm/compaction.c        | 16 +++++++----
 mm/internal.h          | 10 ++++++-
 mm/page_alloc.c        | 64 +++++++++++++++++++++++++++++++++---------
 mm/vmscan.c            | 24 ++++++++++++----
 5 files changed, 89 insertions(+), 28 deletions(-)

Range-diff against v1:

1:  c630170105ac = 1:  ed3ef9f9fd13 mm: page_alloc: add a watermark check that knows the migratetype
2:  918bed282fed ! 2:  3347b0a24b0e mm: page_alloc: make the high-order watermark check honour ALLOC_NOFRAGMENT

@@ Commit message
__compact_finished() the same rule for its fallback test, so that compaction, once it runs, stops on a page the request can take.

- The allocator fast path gets the same answer. A zone whose large free
- pages all belong to other migratetypes now fails the watermark check,
- where before rmqueue failed, and the allocator moves on to the next zone.
-
- should_reclaim_retry() keeps counting free pages of every migratetype.
- It does not ask whether the allocation can be served now, but whether
- reclaim and compaction can still make progress, and they work across
- migratetypes to free a whole pageblock the allocation can claim.
+ zone_watermark_fast() calls __zone_watermark_ok_mt() too. With
+ defrag_mode, an allocation now wakes kswapd and starts reclaim or
+ compaction as soon as the free lists hold nothing it can take. For the
+ orders the per-cpu lists serve, that is earlier than before: the check
+ does not count their pages, so the allocation no longer uses up the local
+ list first. A request that may not wake kswapd or reclaim, such as an
+ mTHP fault with THP defrag=never, falls back to a smaller folio instead.

Fixes e3aa7df331bc ("mm: page_alloc: defrag_mode")

3: a7ebde665647 ! 3: 0ecbcc5f5fb0 mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT
@@ Commit message
and cannot take the free pages left in unmovable and reclaimable
pageblocks. Reclaim stops on them anyway, having freed little.

- In Dev Jain's 64K mTHP benchmark, an order-4 direct reclaim frees 16
+ In Dev Jain's 64K mTHP benchmark, with the high-order watermark check
+ already honouring ALLOC_NOFRAGMENT, an order-4 direct reclaim frees 16
pages on average with defrag_mode and 45 without. 64K faults get a bit over half as many folios as without defrag_mode (medians of 15 runs):

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 mm: page_alloc: add a watermark check that knows the migratetype

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(), which takes the migratetype as well, and make __zone_watermark_ok() a wrapper around it for the second kind of check. These use the new function:

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

Declare both in mm/internal.h: nothing outside mm uses them.

Nothing reads the migratetype yet: no functional change.

---
 include/linux/mmzone.h |  3 ---
 mm/compaction.c        | 11 ++++++-----
 mm/internal.h          |  7 +++++++
 mm/page_alloc.c        | 37 ++++++++++++++++++++++++++++---------
 4 files changed, 41 insertions(+), 17 deletions(-)
include/linux/mmzone.h +0 -3
index ecd1ebd49756..4d9347a399fa 100644
@@ -1637,9 +1637,6 @@ static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
 #include <linux/memory_hotplug.h>
 
 void build_all_zonelists(pg_data_t *pgdat);
-bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
-			 int highest_zoneidx, unsigned int alloc_flags,
-			 long free_pages);
 bool zone_watermark_ok(struct zone *z, unsigned int order,
 		unsigned long mark, int highest_zoneidx,
 		unsigned int alloc_flags);
mm/compaction.c +6 -5
index f1b2060eb201..6c553d0e90f8 100644
@@ -2520,7 +2520,7 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
 static enum compact_result
 compaction_suit_allocation_order(struct zone *zone, unsigned int order,
 				 int highest_zoneidx, unsigned int alloc_flags,
-				 bool async, bool kcompactd)
+				 int migratetype, bool async, bool kcompactd)
 {
 	unsigned long free_pages;
 	unsigned long watermark;
@@ -2531,8 +2531,8 @@ compaction_suit_allocation_order(struct zone *zone, unsigned int order,
 		free_pages = zone_page_state(zone, NR_FREE_PAGES);
 
 	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
-	if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx,
-				alloc_flags, free_pages))
+	if (__zone_watermark_ok_mt(zone, order, watermark, highest_zoneidx,
+				   alloc_flags, free_pages, migratetype))
 		return COMPACT_SUCCESS;
 
 	/*
@@ -2588,6 +2588,7 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
 		ret = compaction_suit_allocation_order(cc->zone, cc->order,
 						       cc->highest_zoneidx,
 						       cc->alloc_flags,
+						       cc->migratetype,
 						       cc->mode == MIGRATE_ASYNC,
 						       !cc->direct_compaction);
 		if (ret != COMPACT_CONTINUE)
@@ -3073,7 +3074,7 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat)
 		ret = compaction_suit_allocation_order(zone,
 				pgdat->kcompactd_max_order,
 				highest_zoneidx, alloc_flags,
-				false, true);
+				MIGRATE_MOVABLE, false, true);
 		if (ret == COMPACT_CONTINUE)
 			return true;
 	}
@@ -3116,7 +3117,7 @@ static void kcompactd_do_work(pg_data_t *pgdat)
 
 		ret = compaction_suit_allocation_order(zone,
 				cc.order, zoneid, cc.alloc_flags,
-				false, true);
+				MIGRATE_MOVABLE, false, true);
 		if (ret != COMPACT_CONTINUE)
 			continue;
 
mm/internal.h +7 -0
index 566cbd2dce48..8e58e2db5e09 100644
@@ -726,6 +726,13 @@ extern int min_free_kbytes;
 extern int defrag_mode;
 
 void setup_per_zone_wmarks(void);
+bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
+			 int highest_zoneidx, unsigned int alloc_flags,
+			 long free_pages);
+bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
+			    unsigned long mark, int highest_zoneidx,
+			    unsigned int alloc_flags, long free_pages,
+			    int migratetype);
 void calculate_min_free_kbytes(void);
 int __meminit init_per_zone_wmark_min(void);
 
mm/page_alloc.c +28 -9
index a8cac5e01066..7730660bdd09 100644
@@ -3587,9 +3587,10 @@ static inline long __zone_watermark_unusable_free(struct zone *z,
  * one free page of a suitable size. Checking now avoids taking the zone lock
  * to check in the allocation paths if no pages are free.
  */
-bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
-			 int highest_zoneidx, unsigned int alloc_flags,
-			 long free_pages)
+bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
+			    unsigned long mark, int highest_zoneidx,
+			    unsigned int alloc_flags, long free_pages,
+			    int migratetype)
 {
 	long min = mark;
 	int o;
@@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 	return false;
 }
 
+/*
+ * For checks that are not about serving one allocation now: kswapd and
+ * kcompactd balancing a zone, order-0 thresholds, forecasts whether
+ * reclaim and compaction can still make progress.  They count free pages
+ * of every migratetype: reclaim and compaction work across migratetypes
+ * to free whole pageblocks.
+ */
+bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
+			 int highest_zoneidx, unsigned int alloc_flags,
+			 long free_pages)
+{
+	return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
+				      alloc_flags, free_pages, MIGRATE_MOVABLE);
+}
+
 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 		      int highest_zoneidx, unsigned int alloc_flags)
 {
@@ -3697,8 +3713,8 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 			return true;
 	}
 
-	if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags,
-					free_pages))
+	if (__zone_watermark_ok_mt(z, order, mark, highest_zoneidx, alloc_flags,
+				   free_pages, gfp_migratetype(gfp_mask)))
 		return true;
 
 	/*
@@ -3710,8 +3726,9 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 	if (unlikely(!order && (alloc_flags & ALLOC_MIN_RESERVE) && z->watermark_boost
 		&& ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) {
 		mark = z->_watermark[WMARK_MIN];
-		return __zone_watermark_ok(z, order, mark, highest_zoneidx,
-					alloc_flags, free_pages);
+		return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
+					      alloc_flags, free_pages,
+					      gfp_migratetype(gfp_mask));
 	}
 
 	return false;
@@ -3923,8 +3940,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 				continue;
 
 			/* did we reclaim enough */
-			if (!zone_watermark_ok(zone, order, mark,
-					       ac->highest_zoneidx, alloc_flags))
+			if (!__zone_watermark_ok_mt(zone, order, mark,
+					ac->highest_zoneidx, alloc_flags,
+					zone_page_state(zone, NR_FREE_PAGES),
+					gfp_migratetype(gfp_mask)))
 				continue;
 		}
 
Signature · 1 line
2.54.0
#9 lore raw

PATCH v2 2/3 mm: page_alloc: make the high-order watermark check honour ALLOC_NOFRAGMENT

In reply to Kiryl Shutsemau (Meta)

With defrag_mode, every slowpath allocation carries ALLOC_NOFRAGMENT. A request below pageblock_order may take a page of its own migratetype or claim a whole free block, but not steal from another migratetype's block. The high-order part of the watermark check still accepts a free page of any migratetype.

Under memory pressure, movable allocations take free movable pages as soon as reclaim frees them. Free space in unmovable and reclaimable pageblocks is off-limits to them under ALLOC_NOFRAGMENT, and builds up. The free pages of order 4 and up end up mostly in those pageblocks.

A 64K mTHP fault cannot take them, but direct compaction's up-front check counts them and returns COMPACT_SUCCESS without compacting anything. The allocation then fails. Because compaction "succeeded", it is never deferred, and every fault stalls in it again before falling back to small pages.

Dev Jain's benchmark faults 64K mTHPs under swap pressure, in a 7G VM with only the 64K size enabled. With defrag_mode, those faults stall in compaction 18 times as often and get about a third of the folios (medians of 15 runs):

defrag_mode=0 defrag_mode=1 defrag_mode=1
+ this patch
compact_stall 1690 30669 2607
compact_fail 621 30531 906
64K faults allocated 20750 7391 11538
64K faults falling back to 4K 725 13832 9945

Make __zone_watermark_ok_mt() skip other migratetypes' free lists below pageblock_order for an ALLOC_NOFRAGMENT request. Give __compact_finished() the same rule for its fallback test, so that compaction, once it runs, stops on a page the request can take.

zone_watermark_fast() calls __zone_watermark_ok_mt() too. With defrag_mode, an allocation now wakes kswapd and starts reclaim or compaction as soon as the free lists hold nothing it can take. For the orders the per-cpu lists serve, that is earlier than before: the check does not count their pages, so the allocation no longer uses up the local list first. A request that may not wake kswapd or reclaim, such as an mTHP fault with THP defrag=never, falls back to a smaller folio instead.

---
 mm/compaction.c |  5 +++++
 mm/page_alloc.c | 21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)
mm/compaction.c +5 -0
index 6c553d0e90f8..7b2267bbf115 100644
@@ -2365,6 +2365,11 @@ static enum compact_result __compact_finished(struct compact_control *cc)
 			!free_area_empty(area, MIGRATE_CMA))
 			return COMPACT_SUCCESS;
 #endif
+		/* Under ALLOC_NOFRAGMENT it may only claim a whole block */
+		if ((cc->alloc_flags & ALLOC_NOFRAGMENT) &&
+		    order < pageblock_order)
+			continue;
+
 		/*
 		 * Job done if allocation would steal freepages from
 		 * other migratetype buddy lists.
mm/page_alloc.c +18 -3
index 7730660bdd09..d53fb2c67ae1 100644
@@ -3586,6 +3586,11 @@ static inline long __zone_watermark_unusable_free(struct zone *z,
  * will return true of the order-0 watermark is reached and there is at least
  * one free page of a suitable size. Checking now avoids taking the zone lock
  * to check in the allocation paths if no pages are free.
+ *
+ * Under ALLOC_NOFRAGMENT, a request below pageblock_order can take another
+ * migratetype's page only by claiming the whole block, so only free pages of
+ * @migratetype count below pageblock_order.  Without ALLOC_NOFRAGMENT,
+ * @migratetype is not used.
  */
 bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
 			    unsigned long mark, int highest_zoneidx,
@@ -3648,6 +3653,9 @@ bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
 			continue;
 
 		for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
+			if ((alloc_flags & ALLOC_NOFRAGMENT) &&
+			    mt != migratetype && o < pageblock_order)
+				continue;
 			if (!free_area_empty(area, mt))
 				return true;
 		}
@@ -3677,8 +3685,12 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 			 int highest_zoneidx, unsigned int alloc_flags,
 			 long free_pages)
 {
+	/* Serving an ALLOC_NOFRAGMENT request needs __zone_watermark_ok_mt() */
+	VM_WARN_ON_ONCE(order && order < pageblock_order &&
+			(alloc_flags & ALLOC_NOFRAGMENT));
 	return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
-				      alloc_flags, free_pages, MIGRATE_MOVABLE);
+				      alloc_flags & ~ALLOC_NOFRAGMENT,
+				      free_pages, MIGRATE_MOVABLE);
 }
 
 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
@@ -4685,10 +4697,13 @@ should_reclaim_retry(gfp_t gfp_mask, unsigned order,
 
 		/*
 		 * Would the allocation succeed if we reclaimed all
-		 * reclaimable pages?
+		 * reclaimable pages?  Count free pages of every migratetype:
+		 * reclaim and compaction work across migratetypes to free a
+		 * whole pageblock, which the allocation can then claim.
 		 */
 		wmark = __zone_watermark_ok(zone, order, min_wmark,
-				ac->highest_zoneidx, alloc_flags, available);
+				ac->highest_zoneidx,
+				alloc_flags & ~ALLOC_NOFRAGMENT, available);
 		trace_reclaim_retry_zone(z, order, reclaimable,
 				available, min_wmark, *no_progress_loops, wmark);
 		if (wmark) {
Signature · 1 line
2.54.0
#11 lore raw

PATCH v2 3/3 mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT

In reply to Kiryl Shutsemau (Meta)

Direct reclaim for a costly order stops as soon as the allocation could succeed. compaction_ready() skips a zone and should_continue_reclaim() stops reclaiming when zone_watermark_ok() passes, and zone_watermark_ok() accepts a free page of any migratetype.

With defrag_mode, the allocation behind the reclaim is ALLOC_NOFRAGMENT and cannot take the free pages left in unmovable and reclaimable pageblocks. Reclaim stops on them anyway, having freed little.

In Dev Jain's 64K mTHP benchmark, with the high-order watermark check already honouring ALLOC_NOFRAGMENT, an order-4 direct reclaim frees 16 pages on average with defrag_mode and 45 without. 64K faults get a bit over half as many folios as without defrag_mode (medians of 15 runs):

defrag_mode=0 defrag_mode=1 defrag_mode=1
+ this patch
64K faults allocated 20750 11538 19759
64K faults falling back to 4K 725 9945 1664

Pass ALLOC_NOFRAGMENT from the allocation down to try_to_free_pages(). Both checks then ask __zone_watermark_ok_mt() for a free page the allocation can take, and an order-4 direct reclaim frees 40 pages on average.

---
 mm/internal.h   |  3 ++-
 mm/page_alloc.c |  8 +++++---
 mm/vmscan.c     | 24 +++++++++++++++++++-----
 3 files changed, 26 insertions(+), 9 deletions(-)
mm/internal.h +2 -1
index 8e58e2db5e09..b43947818f9a 100644
@@ -65,7 +65,8 @@ void folio_mark_lazyfree(struct folio *folio);
 /* mm/vmscan.c */
 unsigned long zone_reclaimable_pages(struct zone *zone);
 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
-				gfp_t gfp_mask, const nodemask_t *mask);
+				gfp_t gfp_mask, const nodemask_t *mask,
+				bool nofragment);
 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
 			      int zone_idx);
 
mm/page_alloc.c +5 -3
index d53fb2c67ae1..08f9bdcd3b9f 100644
@@ -4451,7 +4451,7 @@ static unsigned int check_retry_zonelist(unsigned int seq)
 
 /* Perform direct synchronous page reclaim */
 static unsigned long
-__perform_reclaim(gfp_t gfp_mask, unsigned int order,
+__perform_reclaim(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags,
 					const struct alloc_context *ac)
 {
 	unsigned int noreclaim_flag;
@@ -4465,7 +4465,8 @@ __perform_reclaim(gfp_t gfp_mask, unsigned int order,
 	noreclaim_flag = memalloc_noreclaim_save();
 
 	progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
-								ac->nodemask);
+				     ac->nodemask,
+				     alloc_flags & ALLOC_NOFRAGMENT);
 
 	memalloc_noreclaim_restore(noreclaim_flag);
 	fs_reclaim_release(gfp_mask);
@@ -4487,7 +4488,8 @@ __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
 	int reclaim_order = ac->reclaim_compact_order;
 
 	psi_memstall_enter(&pflags);
-	*did_some_progress = __perform_reclaim(gfp_mask, reclaim_order, ac);
+	*did_some_progress = __perform_reclaim(gfp_mask, reclaim_order,
+					       alloc_flags, ac);
 	if (unlikely(!(*did_some_progress)))
 		goto out;
 
mm/vmscan.c +19 -5
index 9959ca158ce8..54a16dbb84dd 100644
@@ -142,6 +142,9 @@ struct scan_control {
 	/* One of the zones is ready for compaction */
 	unsigned int compaction_ready:1;
 
+	/* The allocation cannot fall back to other migratetypes' blocks */
+	unsigned int nofragment:1;
+
 	/* There is easily reclaimable cold cache in the current node */
 	unsigned int cache_trim_mode:1;
 
@@ -6316,6 +6319,17 @@ static bool in_reclaim_compaction(struct scan_control *sc)
 	return false;
 }
 
+static bool allocation_can_succeed(struct zone *zone, struct scan_control *sc,
+				   unsigned long mark)
+{
+	unsigned int alloc_flags = sc->nofragment ? ALLOC_NOFRAGMENT : 0;
+	long free_pages = zone_page_state(zone, NR_FREE_PAGES);
+	int migratetype = gfp_migratetype(sc->gfp_mask);
+
+	return __zone_watermark_ok_mt(zone, sc->order, mark, sc->reclaim_idx,
+				      alloc_flags, free_pages, migratetype);
+}
+
 /*
  * Reclaim/compaction is used for high-order allocation requests. It reclaims
  * order-0 pages before compacting the zone. should_continue_reclaim() returns
@@ -6354,8 +6368,7 @@ static inline bool should_continue_reclaim(struct pglist_data *pgdat,
 		unsigned long watermark = min_wmark_pages(zone);
 
 		/* Allocation can already succeed, nothing to do */
-		if (zone_watermark_ok(zone, sc->order, watermark,
-				      sc->reclaim_idx, 0))
+		if (allocation_can_succeed(zone, sc, watermark))
 			return false;
 
 		if (compaction_suitable(zone, sc->order, watermark,
@@ -6580,8 +6593,7 @@ static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
 		return false;
 
 	/* Allocation can already succeed, nothing to do */
-	if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone),
-			      sc->reclaim_idx, 0))
+	if (allocation_can_succeed(zone, sc, min_wmark_pages(zone)))
 		return true;
 
 	/*
@@ -6965,7 +6977,8 @@ static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
 }
 
 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
-				gfp_t gfp_mask, const nodemask_t *nodemask)
+				gfp_t gfp_mask, const nodemask_t *nodemask,
+				bool nofragment)
 {
 	unsigned long nr_reclaimed;
 	struct scan_control sc = {
@@ -6978,6 +6991,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 		.may_writepage = 1,
 		.may_unmap = 1,
 		.may_swap = 1,
+		.nofragment = nofragment,
 	};
 
 	/*
Signature · 1 line
2.54.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Kiryl Shutsemau (Meta)
Quoted 9 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

Quoted 5 lines

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

nit: perhaps we could make the changelog above bit more concise.

I think the necessary bit here is "With defrag_mode, allocations cannot steal a page from pageblocks of other migrate type, thus __zone_watermark_ok() needs to take that into account."

... and maybe

- "Ignoring the allocation's migrate type leads to stopping direct compaction and reclamation prematurely".

- "Indirect compaction and reclamation are not affected as they do not serve specific allocations"

Quoted 9 lines

Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(), which takes the migratetype as well, and make __zone_watermark_ok() a wrapper around it for the second kind of check. These use the new function:

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

Quoted 7 lines

Declare both in mm/internal.h: nothing outside mm uses them.

Nothing reads the migratetype yet: no functional change.

---

otherwise looks good to me,

with a nit below.

Quoted 11 lines
 include/linux/mmzone.h |  3 ---
 mm/compaction.c        | 11 ++++++-----
 mm/internal.h          |  7 +++++++
 mm/page_alloc.c        | 37 ++++++++++++++++++++++++++++---------
 4 files changed, 41 insertions(+), 17 deletions(-)
mm/compaction.c +0 -0
index f1b2060eb201..6c553d0e90f8 100644

mm/compaction.c static void kcompactd_do_work(pg_data_t *pgdat) · line 3120

Quoted 5 lines
mm/compaction.c +1 -1
@@ -3116,7 +3117,7 @@ static void kcompactd_do_work(pg_data_t *pgdat)
 
 		ret = compaction_suit_allocation_order(zone,
 				cc.order, zoneid, cc.alloc_flags,
-				false, true);
+				MIGRATE_MOVABLE, false, true);

nit: maybe let's set cc.migratetype and pass it?

Quoted 4 lines
mm/compaction.c +0 -0
@@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 	return false;
 }
 
Signature · 2 lines
Cheers,
Harry / Hyeonggon
#3 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

Quoted 16 lines · Harry Yoo
Quoted 9 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

Quoted 5 lines

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Quoted 5 lines

nit: perhaps we could make the changelog above bit more concise.

I think the necessary bit here is "With defrag_mode, allocations cannot steal a page from pageblocks of other migrate type, thus __zone_watermark_ok() needs to take that into account."

It is ALLOC_NOFRAGMENT, not defrag_mode: Normal zone allocations get it on the first attempt when DMA32 is populated, with defrag_mode off. And such an allocation can still claim a whole free pageblock of another migratetype.

But fair, it can be shorter. A tighter version is below; it also says why kcompactd passes MIGRATE_MOVABLE.

Quoted 1 line

nit: maybe let's set cc.migratetype and pass it?

The value is not read there: __zone_watermark_ok_mt() uses the migratetype only under ALLOC_NOFRAGMENT, and kcompactd never passes it. kcompactd_node_suitable() makes the same call and has no compact_control to take it from.

Thanks for the review.

The new changelog:

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Only a check whether one allocation can be served now should count just the pages it can take. A check whether a zone has enough free memory serves no single allocation and counts every migratetype: reclaim and compaction free whole pageblocks across migratetypes.

Add __zone_watermark_ok_mt(), which takes the migratetype, for the first kind:

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

__zone_watermark_ok() wraps it for the second kind. kcompactd shares compaction_suit_allocation_order() with direct compaction and passes MIGRATE_MOVABLE, which is not read without ALLOC_NOFRAGMENT.

Declare both in mm/internal.h: nothing outside mm uses them.

Nothing reads the migratetype yet: no functional change. Preparation for making the high-order check honour ALLOC_NOFRAGMENT.

Looks good?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#6 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Kiryl Shutsemau (Meta)
Quoted 20 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(), which takes the migratetype as well, and make __zone_watermark_ok() a wrapper around it for the second kind of check. These use the new function:

Quoted 5 lines

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

Declare both in mm/internal.h: nothing outside mm uses them.

Why not mm/page_alloc.h instead, now that it exists.

Quoted 112 lines

Nothing reads the migratetype yet: no functional change.

---
 include/linux/mmzone.h |  3 ---
 mm/compaction.c        | 11 ++++++-----
 mm/internal.h          |  7 +++++++
 mm/page_alloc.c        | 37 ++++++++++++++++++++++++++++---------
 4 files changed, 41 insertions(+), 17 deletions(-)
include/linux/mmzone.h +0 -3
index ecd1ebd49756..4d9347a399fa 100644
@@ -1637,9 +1637,6 @@ static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
 #include <linux/memory_hotplug.h>
 
 void build_all_zonelists(pg_data_t *pgdat);
-bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
-			 int highest_zoneidx, unsigned int alloc_flags,
-			 long free_pages);
 bool zone_watermark_ok(struct zone *z, unsigned int order,
 		unsigned long mark, int highest_zoneidx,
 		unsigned int alloc_flags);
mm/compaction.c +6 -5
index f1b2060eb201..6c553d0e90f8 100644
@@ -2520,7 +2520,7 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
 static enum compact_result
 compaction_suit_allocation_order(struct zone *zone, unsigned int order,
 				 int highest_zoneidx, unsigned int alloc_flags,
-				 bool async, bool kcompactd)
+				 int migratetype, bool async, bool kcompactd)
 {
 	unsigned long free_pages;
 	unsigned long watermark;
@@ -2531,8 +2531,8 @@ compaction_suit_allocation_order(struct zone *zone, unsigned int order,
 		free_pages = zone_page_state(zone, NR_FREE_PAGES);
 
 	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
-	if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx,
-				alloc_flags, free_pages))
+	if (__zone_watermark_ok_mt(zone, order, watermark, highest_zoneidx,
+				   alloc_flags, free_pages, migratetype))
 		return COMPACT_SUCCESS;
 
 	/*
@@ -2588,6 +2588,7 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
 		ret = compaction_suit_allocation_order(cc->zone, cc->order,
 						       cc->highest_zoneidx,
 						       cc->alloc_flags,
+						       cc->migratetype,
 						       cc->mode == MIGRATE_ASYNC,
 						       !cc->direct_compaction);
 		if (ret != COMPACT_CONTINUE)
@@ -3073,7 +3074,7 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat)
 		ret = compaction_suit_allocation_order(zone,
 				pgdat->kcompactd_max_order,
 				highest_zoneidx, alloc_flags,
-				false, true);
+				MIGRATE_MOVABLE, false, true);
 		if (ret == COMPACT_CONTINUE)
 			return true;
 	}
@@ -3116,7 +3117,7 @@ static void kcompactd_do_work(pg_data_t *pgdat)
 
 		ret = compaction_suit_allocation_order(zone,
 				cc.order, zoneid, cc.alloc_flags,
-				false, true);
+				MIGRATE_MOVABLE, false, true);
 		if (ret != COMPACT_CONTINUE)
 			continue;
 
mm/internal.h +7 -0
index 566cbd2dce48..8e58e2db5e09 100644
@@ -726,6 +726,13 @@ extern int min_free_kbytes;
 extern int defrag_mode;
 
 void setup_per_zone_wmarks(void);
+bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
+			 int highest_zoneidx, unsigned int alloc_flags,
+			 long free_pages);
+bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
+			    unsigned long mark, int highest_zoneidx,
+			    unsigned int alloc_flags, long free_pages,
+			    int migratetype);
 void calculate_min_free_kbytes(void);
 int __meminit init_per_zone_wmark_min(void);
 
mm/page_alloc.c +4 -3
index a8cac5e01066..7730660bdd09 100644
@@ -3587,9 +3587,10 @@ static inline long __zone_watermark_unusable_free(struct zone *z,
  * one free page of a suitable size. Checking now avoids taking the zone lock
  * to check in the allocation paths if no pages are free.
  */
-bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
-			 int highest_zoneidx, unsigned int alloc_flags,
-			 long free_pages)
+bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
+			    unsigned long mark, int highest_zoneidx,
+			    unsigned int alloc_flags, long free_pages,
+			    int migratetype)
 {
 	long min = mark;
 	int o;

mm/page_alloc.c bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, · line 3675

Quoted 10 lines
mm/page_alloc.c +7 -0
@@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 	return false;
 }
 
+/*
+ * For checks that are not about serving one allocation now: kswapd and
+ * kcompactd balancing a zone, order-0 thresholds, forecasts whether
+ * reclaim and compaction can still make progress.  They count free pages
+ * of every migratetype: reclaim and compaction work across migratetypes
+ * to free whole pageblocks.
+ */

What a slop comment, sigh. Can you rewrite it normally?

Quoted 47 lines
mm/page_alloc.c +17 -6
+bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
+			 int highest_zoneidx, unsigned int alloc_flags,
+			 long free_pages)
+{
+	return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
+				      alloc_flags, free_pages, MIGRATE_MOVABLE);
+}
+
 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 		      int highest_zoneidx, unsigned int alloc_flags)
 {
@@ -3697,8 +3713,8 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 			return true;
 	}
 
-	if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags,
-					free_pages))
+	if (__zone_watermark_ok_mt(z, order, mark, highest_zoneidx, alloc_flags,
+				   free_pages, gfp_migratetype(gfp_mask)))
 		return true;
 
 	/*
@@ -3710,8 +3726,9 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 	if (unlikely(!order && (alloc_flags & ALLOC_MIN_RESERVE) && z->watermark_boost
 		&& ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) {
 		mark = z->_watermark[WMARK_MIN];
-		return __zone_watermark_ok(z, order, mark, highest_zoneidx,
-					alloc_flags, free_pages);
+		return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
+					      alloc_flags, free_pages,
+					      gfp_migratetype(gfp_mask));
 	}
 
 	return false;
@@ -3923,8 +3940,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 				continue;
 
 			/* did we reclaim enough */
-			if (!zone_watermark_ok(zone, order, mark,
-					       ac->highest_zoneidx, alloc_flags))
+			if (!__zone_watermark_ok_mt(zone, order, mark,
+					ac->highest_zoneidx, alloc_flags,
+					zone_page_state(zone, NR_FREE_PAGES),
+					gfp_migratetype(gfp_mask)))
 				continue;
 		}
 
#4 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Kiryl Shutsemau
Quoted 43 lines · Kiryl Shutsemau
Quoted 16 lines · Harry Yoo
Quoted 9 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

Quoted 5 lines

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Quoted 5 lines

nit: perhaps we could make the changelog above bit more concise.

I think the necessary bit here is "With defrag_mode, allocations cannot steal a page from pageblocks of other migrate type, thus __zone_watermark_ok() needs to take that into account."

It is ALLOC_NOFRAGMENT, not defrag_mode: Normal zone allocations get it on the first attempt when DMA32 is populated, with defrag_mode off. And such an allocation can still claim a whole free pageblock of another migratetype.

But fair, it can be shorter. A tighter version is below; it also says why kcompactd passes MIGRATE_MOVABLE.

Quoted 1 line

nit: maybe let's set cc.migratetype and pass it?

The value is not read there: __zone_watermark_ok_mt() uses the migratetype only under ALLOC_NOFRAGMENT, and kcompactd never passes it. kcompactd_node_suitable() makes the same call and has no compact_control to take it from.

Thanks for the review.

The new changelog:

Quoted 7 lines

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Only a check whether one allocation can be served now should count just the pages it can take.

Please try actually reading this sentence, does it make any sense? It instantly reminded me of the meme classic: "Has anyone really been far even as decided to use even go want to do look more like?"

Quoted 3 lines

A check whether a zone has enough free memory
serves no single allocation and counts every migratetype: reclaim and compaction free whole pageblocks across migratetypes.

Oh yeah always this damned style again. What did they train the models on?

Quoted 11 lines

Add __zone_watermark_ok_mt(), which takes the migratetype, for the first kind:

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

__zone_watermark_ok() wraps it for the second kind. kcompactd shares compaction_suit_allocation_order() with direct compaction and passes MIGRATE_MOVABLE, which is not read without ALLOC_NOFRAGMENT.

Quoted 6 lines

Declare both in mm/internal.h: nothing outside mm uses them.

Nothing reads the migratetype yet: no functional change. Preparation for making the high-order check honour ALLOC_NOFRAGMENT.

Looks good?

Not really :(

#10 lore raw

Re: [PATCH v2 2/3] mm: page_alloc: make the high-order watermark check honour ALLOC_NOFRAGMENT

In reply to Kiryl Shutsemau (Meta)
Quoted 42 lines · Kiryl Shutsemau (Meta)

With defrag_mode, every slowpath allocation carries ALLOC_NOFRAGMENT. A request below pageblock_order may take a page of its own migratetype or claim a whole free block, but not steal from another migratetype's block. The high-order part of the watermark check still accepts a free page of any migratetype.

Under memory pressure, movable allocations take free movable pages as soon as reclaim frees them. Free space in unmovable and reclaimable pageblocks is off-limits to them under ALLOC_NOFRAGMENT, and builds up. The free pages of order 4 and up end up mostly in those pageblocks.

A 64K mTHP fault cannot take them, but direct compaction's up-front check counts them and returns COMPACT_SUCCESS without compacting anything. The allocation then fails. Because compaction "succeeded", it is never deferred, and every fault stalls in it again before falling back to small pages.

Dev Jain's benchmark faults 64K mTHPs under swap pressure, in a 7G VM with only the 64K size enabled. With defrag_mode, those faults stall in compaction 18 times as often and get about a third of the folios (medians of 15 runs):

defrag_mode=0 defrag_mode=1 defrag_mode=1
+ this patch
compact_stall 1690 30669 2607
compact_fail 621 30531 906
64K faults allocated 20750 7391 11538
64K faults falling back to 4K 725 13832 9945

Make __zone_watermark_ok_mt() skip other migratetypes' free lists below pageblock_order for an ALLOC_NOFRAGMENT request. Give __compact_finished() the same rule for its fallback test, so that compaction, once it runs, stops on a page the request can take.

zone_watermark_fast() calls __zone_watermark_ok_mt() too. With defrag_mode, an allocation now wakes kswapd and starts reclaim or compaction as soon as the free lists hold nothing it can take. For the orders the per-cpu lists serve, that is earlier than before: the check does not count their pages, so the allocation no longer uses up the local list first. A request that may not wake kswapd or reclaim, such as an mTHP fault with THP defrag=never, falls back to a smaller folio instead.

Quoted 5 lines

This seems fine enough.

Quoted 77 lines
---
 mm/compaction.c |  5 +++++
 mm/page_alloc.c | 21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)
mm/compaction.c +5 -0
index 6c553d0e90f8..7b2267bbf115 100644
@@ -2365,6 +2365,11 @@ static enum compact_result __compact_finished(struct compact_control *cc)
 			!free_area_empty(area, MIGRATE_CMA))
 			return COMPACT_SUCCESS;
 #endif
+		/* Under ALLOC_NOFRAGMENT it may only claim a whole block */
+		if ((cc->alloc_flags & ALLOC_NOFRAGMENT) &&
+		    order < pageblock_order)
+			continue;
+
 		/*
 		 * Job done if allocation would steal freepages from
 		 * other migratetype buddy lists.
mm/page_alloc.c +18 -3
index 7730660bdd09..d53fb2c67ae1 100644
@@ -3586,6 +3586,11 @@ static inline long __zone_watermark_unusable_free(struct zone *z,
  * will return true of the order-0 watermark is reached and there is at least
  * one free page of a suitable size. Checking now avoids taking the zone lock
  * to check in the allocation paths if no pages are free.
+ *
+ * Under ALLOC_NOFRAGMENT, a request below pageblock_order can take another
+ * migratetype's page only by claiming the whole block, so only free pages of
+ * @migratetype count below pageblock_order.  Without ALLOC_NOFRAGMENT,
+ * @migratetype is not used.
  */
 bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
 			    unsigned long mark, int highest_zoneidx,
@@ -3648,6 +3653,9 @@ bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
 			continue;
 
 		for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
+			if ((alloc_flags & ALLOC_NOFRAGMENT) &&
+			    mt != migratetype && o < pageblock_order)
+				continue;
 			if (!free_area_empty(area, mt))
 				return true;
 		}
@@ -3677,8 +3685,12 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 			 int highest_zoneidx, unsigned int alloc_flags,
 			 long free_pages)
 {
+	/* Serving an ALLOC_NOFRAGMENT request needs __zone_watermark_ok_mt() */
+	VM_WARN_ON_ONCE(order && order < pageblock_order &&
+			(alloc_flags & ALLOC_NOFRAGMENT));
 	return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
-				      alloc_flags, free_pages, MIGRATE_MOVABLE);
+				      alloc_flags & ~ALLOC_NOFRAGMENT,
+				      free_pages, MIGRATE_MOVABLE);
 }
 
 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
@@ -4685,10 +4697,13 @@ should_reclaim_retry(gfp_t gfp_mask, unsigned order,
 
 		/*
 		 * Would the allocation succeed if we reclaimed all
-		 * reclaimable pages?
+		 * reclaimable pages?  Count free pages of every migratetype:
+		 * reclaim and compaction work across migratetypes to free a
+		 * whole pageblock, which the allocation can then claim.
 		 */
 		wmark = __zone_watermark_ok(zone, order, min_wmark,
-				ac->highest_zoneidx, alloc_flags, available);
+				ac->highest_zoneidx,
+				alloc_flags & ~ALLOC_NOFRAGMENT, available);
 		trace_reclaim_retry_zone(z, order, reclaimable,
 				available, min_wmark, *no_progress_loops, wmark);
 		if (wmark) {
#12 lore raw

Re: [PATCH v2 3/3] mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT

In reply to Kiryl Shutsemau (Meta)
Quoted 24 lines · Kiryl Shutsemau (Meta)

Direct reclaim for a costly order stops as soon as the allocation could succeed. compaction_ready() skips a zone and should_continue_reclaim() stops reclaiming when zone_watermark_ok() passes, and zone_watermark_ok() accepts a free page of any migratetype.

With defrag_mode, the allocation behind the reclaim is ALLOC_NOFRAGMENT and cannot take the free pages left in unmovable and reclaimable pageblocks. Reclaim stops on them anyway, having freed little.

In Dev Jain's 64K mTHP benchmark, with the high-order watermark check already honouring ALLOC_NOFRAGMENT, an order-4 direct reclaim frees 16 pages on average with defrag_mode and 45 without. 64K faults get a bit over half as many folios as without defrag_mode (medians of 15 runs):

defrag_mode=0 defrag_mode=1 defrag_mode=1
+ this patch
64K faults allocated 20750 11538 19759
64K faults falling back to 4K 725 9945 1664

Pass ALLOC_NOFRAGMENT from the allocation down to try_to_free_pages(). Both checks then ask __zone_watermark_ok_mt() for a free page the allocation can take, and an order-4 direct reclaim frees 40 pages on average.

Quoted 5 lines

I'd perhaps pass whole alloc_flags to try_to_free_pages(), but other than that LGTM.

Quoted 123 lines
---
 mm/internal.h   |  3 ++-
 mm/page_alloc.c |  8 +++++---
 mm/vmscan.c     | 24 +++++++++++++++++++-----
 3 files changed, 26 insertions(+), 9 deletions(-)
mm/internal.h +2 -1
index 8e58e2db5e09..b43947818f9a 100644
@@ -65,7 +65,8 @@ void folio_mark_lazyfree(struct folio *folio);
 /* mm/vmscan.c */
 unsigned long zone_reclaimable_pages(struct zone *zone);
 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
-				gfp_t gfp_mask, const nodemask_t *mask);
+				gfp_t gfp_mask, const nodemask_t *mask,
+				bool nofragment);
 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
 			      int zone_idx);
 
mm/page_alloc.c +5 -3
index d53fb2c67ae1..08f9bdcd3b9f 100644
@@ -4451,7 +4451,7 @@ static unsigned int check_retry_zonelist(unsigned int seq)
 
 /* Perform direct synchronous page reclaim */
 static unsigned long
-__perform_reclaim(gfp_t gfp_mask, unsigned int order,
+__perform_reclaim(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags,
 					const struct alloc_context *ac)
 {
 	unsigned int noreclaim_flag;
@@ -4465,7 +4465,8 @@ __perform_reclaim(gfp_t gfp_mask, unsigned int order,
 	noreclaim_flag = memalloc_noreclaim_save();
 
 	progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
-								ac->nodemask);
+				     ac->nodemask,
+				     alloc_flags & ALLOC_NOFRAGMENT);
 
 	memalloc_noreclaim_restore(noreclaim_flag);
 	fs_reclaim_release(gfp_mask);
@@ -4487,7 +4488,8 @@ __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
 	int reclaim_order = ac->reclaim_compact_order;
 
 	psi_memstall_enter(&pflags);
-	*did_some_progress = __perform_reclaim(gfp_mask, reclaim_order, ac);
+	*did_some_progress = __perform_reclaim(gfp_mask, reclaim_order,
+					       alloc_flags, ac);
 	if (unlikely(!(*did_some_progress)))
 		goto out;
 
mm/vmscan.c +19 -5
index 9959ca158ce8..54a16dbb84dd 100644
@@ -142,6 +142,9 @@ struct scan_control {
 	/* One of the zones is ready for compaction */
 	unsigned int compaction_ready:1;
 
+	/* The allocation cannot fall back to other migratetypes' blocks */
+	unsigned int nofragment:1;
+
 	/* There is easily reclaimable cold cache in the current node */
 	unsigned int cache_trim_mode:1;
 
@@ -6316,6 +6319,17 @@ static bool in_reclaim_compaction(struct scan_control *sc)
 	return false;
 }
 
+static bool allocation_can_succeed(struct zone *zone, struct scan_control *sc,
+				   unsigned long mark)
+{
+	unsigned int alloc_flags = sc->nofragment ? ALLOC_NOFRAGMENT : 0;
+	long free_pages = zone_page_state(zone, NR_FREE_PAGES);
+	int migratetype = gfp_migratetype(sc->gfp_mask);
+
+	return __zone_watermark_ok_mt(zone, sc->order, mark, sc->reclaim_idx,
+				      alloc_flags, free_pages, migratetype);
+}
+
 /*
  * Reclaim/compaction is used for high-order allocation requests. It reclaims
  * order-0 pages before compacting the zone. should_continue_reclaim() returns
@@ -6354,8 +6368,7 @@ static inline bool should_continue_reclaim(struct pglist_data *pgdat,
 		unsigned long watermark = min_wmark_pages(zone);
 
 		/* Allocation can already succeed, nothing to do */
-		if (zone_watermark_ok(zone, sc->order, watermark,
-				      sc->reclaim_idx, 0))
+		if (allocation_can_succeed(zone, sc, watermark))
 			return false;
 
 		if (compaction_suitable(zone, sc->order, watermark,
@@ -6580,8 +6593,7 @@ static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
 		return false;
 
 	/* Allocation can already succeed, nothing to do */
-	if (zone_watermark_ok(zone, sc->order, min_wmark_pages(zone),
-			      sc->reclaim_idx, 0))
+	if (allocation_can_succeed(zone, sc, min_wmark_pages(zone)))
 		return true;
 
 	/*
@@ -6965,7 +6977,8 @@ static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
 }
 
 unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
-				gfp_t gfp_mask, const nodemask_t *nodemask)
+				gfp_t gfp_mask, const nodemask_t *nodemask,
+				bool nofragment)
 {
 	unsigned long nr_reclaimed;
 	struct scan_control sc = {
@@ -6978,6 +6991,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 		.may_writepage = 1,
 		.may_unmap = 1,
 		.may_swap = 1,
+		.nofragment = nofragment,
 	};
 
 	/*
#7 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Vlastimil Babka (SUSE)
Quoted 21 lines · Vlastimil Babka (SUSE)
Quoted 20 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(), which takes the migratetype as well, and make __zone_watermark_ok() a wrapper around it for the second kind of check. These use the new function:

Quoted 7 lines
Quoted 5 lines

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

Declare both in mm/internal.h: nothing outside mm uses them.

Why not mm/page_alloc.h instead, now that it exists.

Ack. Will change.

Quoted 4 lines
Quoted 4 lines
mm/page_alloc.c +0 -0
@@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 	return false;
 }
 
Quoted 9 lines

mm/page_alloc.c bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, · line 3675

Quoted 7 lines
mm/page_alloc.c +7 -0
+/*
+ * For checks that are not about serving one allocation now: kswapd and
+ * kcompactd balancing a zone, order-0 thresholds, forecasts whether
+ * reclaim and compaction can still make progress.  They count free pages
+ * of every migratetype: reclaim and compaction work across migratetypes
+ * to free whole pageblocks.
+ */

What a slop comment, sigh. Can you rewrite it normally?

/*
* Return true if free base pages in the zone are above 'mark' regardless
* of migratetype. Use it to check whether the zone as a whole has enough
* free memory. Use __zone_watermark_ok_mt() instead to check whether a
* particular allocation can be served.
*/

Okay?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#5 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Vlastimil Babka (SUSE)
Quoted 3 lines · Vlastimil Babka (SUSE)
Quoted 1 line

Looks good?

Not really :(

My bad. Will do better.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#8 lore raw

Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

In reply to Kiryl Shutsemau (Meta)
Quoted 46 lines · Kiryl Shutsemau (Meta)
Quoted 21 lines · Vlastimil Babka (SUSE)
Quoted 20 lines · Kiryl Shutsemau (Meta)

The high-order check in __zone_watermark_ok() accepts a free page of any migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can only take a page of its own migratetype or claim a whole free block, so the check can pass when the allocation cannot succeed.

Telling those apart needs the request's migratetype, but not every check wants them told apart. A check whether an allocation can be served now has to count only the pages it can take.

A check whether there is enough free memory counts every migratetype:
kswapd and kcompactd balancing a zone, order-0 thresholds, the slowpath's forecast whether reclaim and compaction can still make progress. Reclaim and compaction work across migratetypes to free whole pageblocks.

Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(), which takes the migratetype as well, and make __zone_watermark_ok() a wrapper around it for the second kind of check. These use the new function:

Quoted 7 lines
Quoted 5 lines

- the allocator fast path, zone_watermark_fast();
- its recheck after node reclaim in get_page_from_freelist();
- compaction's up-front check, compaction_suit_allocation_order().

Declare both in mm/internal.h: nothing outside mm uses them.

Why not mm/page_alloc.h instead, now that it exists.

Ack. Will change.

Quoted 4 lines
Quoted 4 lines
mm/page_alloc.c +0 -0
@@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 	return false;
 }
 
Quoted 9 lines

mm/page_alloc.c bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, · line 3675

Quoted 7 lines
mm/page_alloc.c +7 -0
+/*
+ * For checks that are not about serving one allocation now: kswapd and
+ * kcompactd balancing a zone, order-0 thresholds, forecasts whether
+ * reclaim and compaction can still make progress.  They count free pages
+ * of every migratetype: reclaim and compaction work across migratetypes
+ * to free whole pageblocks.
+ */

What a slop comment, sigh. Can you rewrite it normally?

Quoted 8 lines

/*
* Return true if free base pages in the zone are above 'mark' regardless
* of migratetype. Use it to check whether the zone as a whole has enough
* free memory. Use __zone_watermark_ok_mt() instead to check whether a
* particular allocation can be served.
*/

Okay?

Yes, thanks!

#13 lore raw

Re: [PATCH v2 3/3] mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT

In reply to Vlastimil Babka (SUSE)
Quoted 2 lines · Vlastimil Babka (SUSE)

I'd perhaps pass whole alloc_flags to try_to_free_pages(), but other than that LGTM.

Passing alloc_flags down is easy. The question is what reclaim does with them.

I can keep using only ALLOC_NOFRAGMENT from them, as the nofragment bit in struct scan_control.

Do you want anything beyond this?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#14 lore raw

Re: [PATCH v2 3/3] mm: vmscan: tell direct reclaim the allocation is ALLOC_NOFRAGMENT

Quoted 11 lines · Kiryl Shutsemau (Meta)
Quoted 2 lines · Vlastimil Babka (SUSE)

I'd perhaps pass whole alloc_flags to try_to_free_pages(), but other than that LGTM.

Passing alloc_flags down is easy. The question is what reclaim does with them.

I can keep using only ALLOC_NOFRAGMENT from them, as the nofragment bit in struct scan_control.

Do you want anything beyond this?

Dunno if anything else makes sense to use in that context. Maybe not now, but in the future it might.