Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/page_alloc: fix lazy memory acceptance before watermarks are set
@ 2026-11-30 17:26 Pankaj Gupta
  2026-11-30 17:26 ` [PATCH 1/2] mm/page_alloc: rename cond_accept_memory() to accept_memory_to_wmark() Pankaj Gupta
  2026-11-30 17:26 ` [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set Pankaj Gupta
  0 siblings, 2 replies; 4+ messages in thread
From: Pankaj Gupta @ 2026-11-30 17:26 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Johannes Weiner
  Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
	Kiryl Shutsemau, Tom Lendacky, Farrah Chen, linux-mm,
	linux-kernel, pankaj.gupta.linux, Pankaj Gupta

Lazy memory acceptance should accept memory only when it is needed.
Since commit 800f1059c99e ("mm/page_alloc: fix memory accept before
watermarks gets initialized"), one MAX_PAGE_ORDER page is accepted on
every page allocation until the zone watermarks are set. On a 19 GiB
SEV-SNP guest this accepts about ~14 GiB during boot and hence reduce the
boot time.

Patch 1 renames cond_accept_memory() to accept_memory_to_wmark().

Patch 2 accepts memory before the watermarks are set only when an
allocation cannot be satisfied.

SEV-SNP guest with 19 GiB of memory, memory left unaccepted after boot,
before and after this change:

             before      after
  4 vCPUs    3.1 GiB     14.7 GiB
 64 vCPUs    2.8 GiB     13.9 GiB

This also reduces the boot time of the guest.

Pankaj Gupta (2):
  mm/page_alloc: rename cond_accept_memory() to accept_memory_to_wmark()
  mm/page_alloc: do not accept memory proactively before watermarks are
    set

 mm/page_alloc.c | 59 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 17 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.34.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] mm/page_alloc: rename cond_accept_memory() to accept_memory_to_wmark()
  2026-11-30 17:26 [PATCH 0/2] mm/page_alloc: fix lazy memory acceptance before watermarks are set Pankaj Gupta
@ 2026-11-30 17:26 ` Pankaj Gupta
  2026-11-30 17:26 ` [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set Pankaj Gupta
  1 sibling, 0 replies; 4+ messages in thread
From: Pankaj Gupta @ 2026-11-30 17:26 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Johannes Weiner
  Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
	Kiryl Shutsemau, Tom Lendacky, Farrah Chen, linux-mm,
	linux-kernel, pankaj.gupta.linux, Pankaj Gupta

cond_accept_memory() accepts memory until the zone is back at its promo
watermark. Rename it to accept_memory_to_wmark() to say what it does.

No functional change.

Signed-off-by: Pankaj Gupta <pankaj.gupta@amd.com>
---
 mm/page_alloc.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2fef4092c913..adba323ba894 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -299,8 +299,8 @@ EXPORT_SYMBOL(nr_online_nodes);
 static unsigned long alloc_stall_warn_jiffies = INITIAL_JIFFIES;
 
 static bool page_contains_unaccepted(struct page *page, unsigned int order);
-static bool cond_accept_memory(struct zone *zone, unsigned int order,
-			       int alloc_flags);
+static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
+				   int alloc_flags);
 static bool __free_unaccepted(struct page *page);
 
 int page_group_by_mobility_disabled __read_mostly;
@@ -3902,7 +3902,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			continue;
 		}
 
-		cond_accept_memory(zone, order, alloc_flags);
+		accept_memory_to_wmark(zone, order, alloc_flags);
 
 		/*
 		 * Detect whether the number of free pages is below high
@@ -3927,7 +3927,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		if (!zone_watermark_fast(zone, order, mark,
 				       ac->highest_zoneidx, alloc_flags,
 				       gfp_mask)) {
-			if (cond_accept_memory(zone, order, alloc_flags))
+			if (accept_memory_to_wmark(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/*
@@ -3964,7 +3964,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 			return page;
 		} else {
-			if (cond_accept_memory(zone, order, alloc_flags))
+			if (accept_memory_to_wmark(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/* Try again if zone has deferred pages */
@@ -5286,7 +5286,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			goto failed;
 		}
 
-		cond_accept_memory(zone, 0, alloc_flags);
+		accept_memory_to_wmark(zone, 0, alloc_flags);
 retry_this_zone:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
@@ -5295,7 +5295,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			break;
 		}
 
-		if (cond_accept_memory(zone, 0, alloc_flags))
+		if (accept_memory_to_wmark(zone, 0, alloc_flags))
 			goto retry_this_zone;
 
 		/* Try again if zone has deferred pages */
@@ -8037,8 +8037,8 @@ static bool try_to_accept_memory_one(struct zone *zone)
 	return true;
 }
 
-static bool cond_accept_memory(struct zone *zone, unsigned int order,
-			       int alloc_flags)
+static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
+				   int alloc_flags)
 {
 	long to_accept, wmark;
 	bool ret = false;
@@ -8101,8 +8101,8 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
 	return false;
 }
 
-static bool cond_accept_memory(struct zone *zone, unsigned int order,
-			       int alloc_flags)
+static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
+				   int alloc_flags)
 {
 	return false;
 }
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set
  2026-11-30 17:26 [PATCH 0/2] mm/page_alloc: fix lazy memory acceptance before watermarks are set Pankaj Gupta
  2026-11-30 17:26 ` [PATCH 1/2] mm/page_alloc: rename cond_accept_memory() to accept_memory_to_wmark() Pankaj Gupta
@ 2026-11-30 17:26 ` Pankaj Gupta
  2026-12-01 17:27   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Pankaj Gupta @ 2026-11-30 17:26 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Johannes Weiner
  Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
	Kiryl Shutsemau, Tom Lendacky, Farrah Chen, linux-mm,
	linux-kernel, pankaj.gupta.linux, Pankaj Gupta

Zone watermarks are set in init_per_zone_wmark_min(), a postcore
initcall. Until then promo_wmark_pages() is 0, and
accept_memory_to_wmark() accepts one MAX_PAGE_ORDER page on every call
to ensure progress.

get_page_from_freelist() and alloc_pages_bulk() call it for every zone
before they try to allocate, so every early page allocation accepts
4 MiB, needed or not. This defeats lazy acceptance.

Make accept_memory_to_wmark() do nothing while the watermarks are 0 and
move the "accept one MAX_PAGE_ORDER page" fallback to a new helper,
accept_memory_for_alloc(), which is called where the watermark check or
the allocation has failed.

SEV-SNP guest with 19 GiB of memory, memory left unaccepted after boot,
before and after this change:

             before      after
  4 vCPUs    3.1 GiB     14.7 GiB
 64 vCPUs    2.8 GiB     13.9 GiB

This also reduces the boot time of the guest.

Fixes: 800f1059c99e ("mm/page_alloc: fix memory accept before watermarks gets initialized")
Signed-off-by: Pankaj Gupta <pankaj.gupta@amd.com>
---
 mm/page_alloc.c | 43 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index adba323ba894..0ca638c0da46 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -301,6 +301,8 @@ static unsigned long alloc_stall_warn_jiffies = INITIAL_JIFFIES;
 static bool page_contains_unaccepted(struct page *page, unsigned int order);
 static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
 				   int alloc_flags);
+static bool accept_memory_for_alloc(struct zone *zone, unsigned int order,
+				    int alloc_flags);
 static bool __free_unaccepted(struct page *page);
 
 int page_group_by_mobility_disabled __read_mostly;
@@ -3927,7 +3929,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		if (!zone_watermark_fast(zone, order, mark,
 				       ac->highest_zoneidx, alloc_flags,
 				       gfp_mask)) {
-			if (accept_memory_to_wmark(zone, order, alloc_flags))
+			if (accept_memory_for_alloc(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/*
@@ -3964,7 +3966,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 			return page;
 		} else {
-			if (accept_memory_to_wmark(zone, order, alloc_flags))
+			if (accept_memory_for_alloc(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/* Try again if zone has deferred pages */
@@ -5295,7 +5297,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			break;
 		}
 
-		if (accept_memory_to_wmark(zone, 0, alloc_flags))
+		if (accept_memory_for_alloc(zone, 0, alloc_flags))
 			goto retry_this_zone;
 
 		/* Try again if zone has deferred pages */
@@ -8052,13 +8054,8 @@ static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
 
 	wmark = promo_wmark_pages(zone);
 
-	/*
-	 * Watermarks have not been initialized yet.
-	 *
-	 * Accepting one MAX_PAGE_ORDER page to ensure progress.
-	 */
 	if (!wmark)
-		return try_to_accept_memory_one(zone);
+		return false;
 
 	/* How much to accept to get to promo watermark? */
 	to_accept = wmark -
@@ -8076,6 +8073,28 @@ static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
 	return ret;
 }
 
+/*
+ * Accept memory after the watermark check or the allocation itself has failed
+ * for @zone.
+ *
+ * Returns true if memory was accepted and the allocation should be retried.
+ */
+static bool accept_memory_for_alloc(struct zone *zone, unsigned int order,
+				    int alloc_flags)
+{
+	if (list_empty(&zone->unaccepted_pages))
+		return false;
+
+	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
+	if (alloc_flags & ALLOC_NOLOCK)
+		return false;
+
+	if (!promo_wmark_pages(zone))
+		return try_to_accept_memory_one(zone);
+
+	return accept_memory_to_wmark(zone, order, alloc_flags);
+}
+
 static bool __free_unaccepted(struct page *page)
 {
 	struct zone *zone = page_zone(page);
@@ -8107,6 +8126,12 @@ static bool accept_memory_to_wmark(struct zone *zone, unsigned int order,
 	return false;
 }
 
+static bool accept_memory_for_alloc(struct zone *zone, unsigned int order,
+				    int alloc_flags)
+{
+	return false;
+}
+
 static bool __free_unaccepted(struct page *page)
 {
 	BUILD_BUG();
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set
  2026-11-30 17:26 ` [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set Pankaj Gupta
@ 2026-12-01 17:27   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-01 17:27 UTC (permalink / raw)
  To: Pankaj Gupta, Andrew Morton, Vlastimil Babka, Johannes Weiner
  Cc: Suren Baghdasaryan, Michal Hocko, Brendan Jackman, Zi Yan,
	Kiryl Shutsemau, Tom Lendacky, Farrah Chen, linux-mm,
	pankaj.gupta.linux

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/page_alloc: broken unaccepted memory fallback in
  alloc_pages_bulk_noprof()
--

commit 5ff45011bdd58457e1cacc254e4e75587e5fd4af
Author: Pankaj Gupta <pankaj.gupta@amd.com>

mm/page_alloc: do not accept memory proactively before watermarks are set

This commit makes accept_memory_to_wmark() return false while zone
watermarks are 0 and introduces accept_memory_for_alloc() to accept one
MAX_PAGE_ORDER page on watermark or allocation failure during early boot.

> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index adba323ba8942..0ca638c0da462 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
[ ... ]
> @@ -5295,7 +5297,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
>  			break;
>  		}
>  
> -		if (accept_memory_to_wmark(zone, 0, alloc_flags))
> +		if (accept_memory_for_alloc(zone, 0, alloc_flags))
>  			goto retry_this_zone;
>  
>  		/* Try again if zone has deferred pages */

[Severity: Medium]
Does moving the early-boot acceptance fallback to
accept_memory_for_alloc() work in alloc_pages_bulk_noprof() before zone
watermarks are initialized?

Unaccepted pages are already counted in NR_FREE_PAGES by
__free_unaccepted() and are not subtracted by
__zone_watermark_unusable_free():

mm/page_alloc.c:zone_watermark_fast() {
	free_pages = zone_page_state(z, NR_FREE_PAGES);
	...
	if (!order) {
		long usable_free;
		long reserved;

		usable_free = free_pages;
		reserved = __zone_watermark_unusable_free(z, 0, alloc_flags);

		/* reserved may over estimate high-atomic reserves. */
		usable_free -= min(usable_free, reserved);
		if (usable_free > mark + z->lowmem_reserve[highest_zoneidx])
			return true;
	}
	...
}

While promo_wmark_pages(zone) is 0, accept_memory_to_wmark() now does
nothing.  If the zone's accepted free pages are exhausted while
NR_UNACCEPTED still exceeds mark + lowmem_reserve, zone_watermark_fast()
for order 0 passes and breaks out of the loop before reaching
accept_memory_for_alloc().  Because the __rmqueue_pcplist() loop below has
no accept_memory_for_alloc() fallback, alloc_pages_bulk_noprof() either
returns a partial bulk allocation or falls back to single-page allocation
at failed:.

Conversely, if zone_watermark_fast() fails (because total NR_FREE_PAGES
including NR_UNACCEPTED is below mark + lowmem_reserve),
try_to_accept_memory_one() in accept_memory_for_alloc() does not increase
NR_FREE_PAGES, so jumping to retry_this_zone loops and accepts every
unaccepted page in the zone without making zone_watermark_fast() pass.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130172641.237214-1-pankaj.gupta@amd.com?part=2


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 17:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 17:26 [PATCH 0/2] mm/page_alloc: fix lazy memory acceptance before watermarks are set Pankaj Gupta
2026-11-30 17:26 ` [PATCH 1/2] mm/page_alloc: rename cond_accept_memory() to accept_memory_to_wmark() Pankaj Gupta
2026-11-30 17:26 ` [PATCH 2/2] mm/page_alloc: do not accept memory proactively before watermarks are set Pankaj Gupta
2026-12-01 17:27   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox