Skip to contentSkip to conversation

mm/page_alloc: Fix couple of unaccepted memory issues

PATCH In review Kirill A. Shutemov 5 participants 14 messages May 6, 2025 – May 7, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover mm/page_alloc: Fix couple of unaccepted memory issues

Fix issues with unaccepted memory:

- try_alloc_pages() gives up too early on machines with unaccepted

    memory;

- race around zones_with_unaccepted_pages static branch;

Kirill A. Shutemov (2):
mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted
memory
mm/page_alloc: Fix race condition in unaccepted memory handling

 mm/internal.h   |  1 -
 mm/mm_init.c    |  1 -
 mm/page_alloc.c | 73 ++++++++++---------------------------------------
 3 files changed, 14 insertions(+), 61 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/2 mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

Fixes 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
Cc Alexei Starovoitov <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 mm/page_alloc.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
mm/page_alloc.c +15 -13
index 5669baf2a6fe..5fccf5fce084 100644
@@ -290,7 +290,8 @@ EXPORT_SYMBOL(nr_online_nodes);
 #endif
 
 static bool page_contains_unaccepted(struct page *page, unsigned int order);
-static bool cond_accept_memory(struct zone *zone, unsigned int order);
+static bool cond_accept_memory(struct zone *zone, unsigned int order,
+			       int alloc_flags);
 static bool __free_unaccepted(struct page *page);
 
 int page_group_by_mobility_disabled __read_mostly;
@@ -3616,7 +3617,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			}
 		}
 
-		cond_accept_memory(zone, order);
+		cond_accept_memory(zone, order, alloc_flags);
 
 		/*
 		 * Detect whether the number of free pages is below high
@@ -3643,7 +3644,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 				       gfp_mask)) {
 			int ret;
 
-			if (cond_accept_memory(zone, order))
+			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/*
@@ -3696,7 +3697,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 			return page;
 		} else {
-			if (cond_accept_memory(zone, order))
+			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
 			/* Try again if zone has deferred pages */
@@ -4849,7 +4850,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			goto failed;
 		}
 
-		cond_accept_memory(zone, 0);
+		cond_accept_memory(zone, 0, alloc_flags);
 retry_this_zone:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
 		if (zone_watermark_fast(zone, 0,  mark,
@@ -4858,7 +4859,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 			break;
 		}
 
-		if (cond_accept_memory(zone, 0))
+		if (cond_accept_memory(zone, 0, alloc_flags))
 			goto retry_this_zone;
 
 		/* Try again if zone has deferred pages */
@@ -7284,7 +7285,8 @@ static inline bool has_unaccepted_memory(void)
 	return static_branch_unlikely(&zones_with_unaccepted_pages);
 }
 
-static bool cond_accept_memory(struct zone *zone, unsigned int order)
+static bool cond_accept_memory(struct zone *zone, unsigned int order,
+			       int alloc_flags)
 {
 	long to_accept, wmark;
 	bool ret = false;
@@ -7295,6 +7297,10 @@ static bool cond_accept_memory(struct zone *zone, unsigned int order)
 	if (list_empty(&zone->unaccepted_pages))
 		return false;
 
+	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
+	if (alloc_flags & ALLOC_TRYLOCK)
+		return false;
+
 	wmark = promo_wmark_pages(zone);
 
 	/*
@@ -7351,7 +7357,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)
+static bool cond_accept_memory(struct zone *zone, unsigned int order,
+			       int alloc_flags)
 {
 	return false;
 }
@@ -7422,11 +7429,6 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order)
 	if (!pcp_allowed_order(order))
 		return NULL;
 
-#ifdef CONFIG_UNACCEPTED_MEMORY
-	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
-	if (has_unaccepted_memory())
-		return NULL;
-#endif
 	/* Bailout, since _deferred_grow_zone() needs to take a lock */
 	if (deferred_pages_enabled())
 		return NULL;
Signature · 1 line
2.47.2
#2 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

mm/page_alloc.c static bool cond_accept_memory(struct zone *zone, unsigned int order) · line 7303

Quoted 4 lines · Kirill A. Shutemov
mm/page_alloc.c +4 -0
+	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
+	if (alloc_flags & ALLOC_TRYLOCK)
+		return false;
+

Quick lazy question: why don't we just trylock it like we do for the zone lock?

#3 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 8 lines · Brendan Jackman

mm/page_alloc.c static bool cond_accept_memory(struct zone *zone, unsigned int order) · line 7303

Quoted 4 lines · Kirill A. Shutemov
mm/page_alloc.c +4 -0
+	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
+	if (alloc_flags & ALLOC_TRYLOCK)
+		return false;
+

Quick lazy question: why don't we just trylock it like we do for the zone lock?

It is not only zone lock. There's also unaccepted_memory_lock inside accept_memory().

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

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 10 lines · Kirill A. Shutemov
Quoted 8 lines · Brendan Jackman

mm/page_alloc.c static bool cond_accept_memory(struct zone *zone, unsigned int order) · line 7303

Quoted 4 lines · Kirill A. Shutemov
mm/page_alloc.c +4 -0
+	/* Bailout, since try_to_accept_memory_one() needs to take a lock */
+	if (alloc_flags & ALLOC_TRYLOCK)
+		return false;
+

Quick lazy question: why don't we just trylock it like we do for the zone lock?

Quoted 2 lines

It is not only zone lock. There's also unaccepted_memory_lock inside accept_memory().

Right, but my lazy question was why can't we "just" trylock that too?

But anyway, that's no use because if we win the trylock we'd still have to do __free_pages_ok().

Acked-by on v1 1/2
#5 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

In reply to Kirill A. Shutemov
Quoted 5 lines · Kirill A. Shutemov

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Quoted 8 lines

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

Fixes 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")

Thanks for working on this, but the fixes tag is overkill. This limitation is not causing any issues in our setups. Improving it is certainly better, of course.

#6 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 10 lines · Alexei Starovoitov
Quoted 8 lines · Kirill A. Shutemov

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Quoted 8 lines
Quoted 5 lines

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

Fixes 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")

Thanks for working on this, but the fixes tag is overkill. This limitation is not causing any issues in our setups.

Have you had chance to test it on any platform with unaccepted memory? So far it is only Intel TDX and AMD SEV guests.

Quoted 2 lines

Improving it is certainly better, of course.

Thanks!

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

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 15 lines · Kirill A. Shutemov
Quoted 13 lines · Alexei Starovoitov
Quoted 11 lines · Kirill A. Shutemov

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Fixes 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")

Thanks for working on this, but the fixes tag is overkill. This limitation is not causing any issues in our setups.

Have you had chance to test it on any platform with unaccepted memory? So far it is only Intel TDX and AMD SEV guests.

We don't use them, and my understanding is that such unaccepted memory will be there only during boot time.

#8 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 20 lines · Alexei Starovoitov
Quoted 18 lines · Kirill A. Shutemov
Quoted 16 lines · Alexei Starovoitov
Quoted 13 lines · Kirill A. Shutemov

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

Fixes 97769a53f117 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks for working on this, but the fixes tag is overkill. This limitation is not causing any issues in our setups.

Have you had chance to test it on any platform with unaccepted memory? So far it is only Intel TDX and AMD SEV guests.

We don't use them, and my understanding is that such unaccepted memory will be there only during boot time.

That's false. Unaccepted memory can be there indefinitely after boot. It only gets accepted on demand.

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

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

In reply to Kirill A. Shutemov
Quoted 9 lines · On Tue, 6 May 2025 14:25:08 +0300 "Kirill A. Shutemov"…

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

What are the userspace-visible effects, please?

Was the omission of cc:stable intentional? I cannot locally determine this without the above info.

If the cc:stable omission was indeed intentional then it would be better if this series was presented as two standalone patches.

#10 lore raw

Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory

Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Tue, 6 May 2025 14:25:08 +0300 "Kirill A. Shutemov"…

try_alloc_pages() will not attempt to allocate memory if the system has *any* unaccepted memory. Memory is accepted as needed and can remain in the system indefinitely, causing the interface to always fail.

Quoted 7 lines
Quoted 5 lines

Rather than immediately giving up, attempt to use already accepted memory on free lists.

Pass 'alloc_flags' to cond_accept_memory() and do not accept new memory for ALLOC_TRYLOCK requests.

What are the userspace-visible effects, please?

I cannot say I fully understand the implications.

The interface obviously allows failure, but the caller might expect eventual success on retry.

So far, only BPF uses the interface. Maybe Alexei can comment on what will happen if the function always fails.

I noticed the issue by code analysis because the second patch removes has_unaccepted_memory().

Quoted 5 lines

Was the omission of cc:stable intentional? I cannot locally determine this without the above info.

If the cc:stable omission was indeed intentional then it would be better if this series was presented as two standalone patches.

Given that the second patch cannot be applied to current Linus' tree without this one, it is better to add stable@.

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

PATCH 2/2 mm/page_alloc: Fix race condition in unaccepted memory handling

In reply to Kirill A. Shutemov

The page allocator tracks the number of zones that have unaccepted memory using static_branch_enc/dec() and uses that static branch in hot paths to determine if it needs to deal with unaccepted memory.

Borisal and Thomas pointed out that the tracking is racy operations on static_branch are not serialized against adding/removing unaccepted pages to/from the zone.

The effect of this static_branch optimization is only visible on microbenchmark.

Instead of adding more complexity around it, remove it altogether.

Fixes dcdfdd40fa82 ("mm: Add support for unaccepted memory")
Cc Andrew Morton <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 mm/internal.h   |  1 -
 mm/mm_init.c    |  1 -
 mm/page_alloc.c | 47 -----------------------------------------------
 3 files changed, 49 deletions(-)
mm/internal.h +0 -1
index e9695baa5922..50c2f590b2d0 100644
@@ -1595,7 +1595,6 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc);
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
 void accept_page(struct page *page);
-void unaccepted_cleanup_work(struct work_struct *work);
 #else /* CONFIG_UNACCEPTED_MEMORY */
 static inline void accept_page(struct page *page)
 {
mm/mm_init.c +0 -1
index 9659689b8ace..84f14fa12d0d 100644
@@ -1441,7 +1441,6 @@ static void __meminit zone_init_free_lists(struct zone *zone)
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
 	INIT_LIST_HEAD(&zone->unaccepted_pages);
-	INIT_WORK(&zone->unaccepted_cleanup, unaccepted_cleanup_work);
 #endif
 }
 
mm/page_alloc.c +0 -47

97 lines not on this page · raw message

Signature · 1 line
2.47.2
Tested-by on v1 2/2
#12 lore raw

Re: [PATCH 2/2] mm/page_alloc: Fix race condition in unaccepted memory handling

Quoted 5 lines · Kirill A. Shutemov

The page allocator tracks the number of zones that have unaccepted memory using static_branch_enc/dec() and uses that static branch in hot paths to determine if it needs to deal with unaccepted memory.

Borisal and Thomas pointed out that the tracking is racy operations on

Boris or Borislav would be nice.

Quoted 2 lines

static_branch are not serialized against adding/removing unaccepted pages to/from the zone.

Also, that sentence needs massaging.

Quoted 4 lines

The effect of this static_branch optimization is only visible on microbenchmark.

Instead of adding more complexity around it, remove it altogether.

Yah, good idea.

Quoted 4 lines

Thx for the quick fix.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

PATCH v2 1/1 mm/page_alloc: Fix race condition in unaccepted memory handling

The page allocator tracks the number of zones that have unaccepted memory using static_branch_enc/dec() and uses that static branch in hot paths to determine if it needs to deal with unaccepted memory.

Borislav and Thomas pointed out that the tracking is racy: operations on static_branch are not serialized against adding/removing unaccepted pages to/from the zone.

Sanity checks inside static_branch machinery detects it:

WARNING: CPU: 0 PID: 10 at kernel/jump_label.c:276 __static_key_slow_dec_cpuslocked+0x8e/0xa0

The comment around the WARN() explains the problem:

/*
* Warn about the '-1' case though; since that means a
* decrement is concurrent with a first (0->1) increment. IOW
* people are trying to disable something that wasn't yet fully
* enabled. This suggests an ordering problem on the user side. */

The effect of this static_branch optimization is only visible on microbenchmark.

Instead of adding more complexity around it, remove it altogether.

Fixes dcdfdd40fa82 ("mm: Add support for unaccepted memory")
Cc Andrew Morton <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>

---

v2:
- Update commit message;
- Apply Borislav's Tested-by tag;

---
 mm/internal.h   |  1 -
 mm/mm_init.c    |  1 -
 mm/page_alloc.c | 47 -----------------------------------------------
 3 files changed, 49 deletions(-)
mm/internal.h +0 -1
index e9695baa5922..50c2f590b2d0 100644
@@ -1595,7 +1595,6 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc);
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
 void accept_page(struct page *page);
-void unaccepted_cleanup_work(struct work_struct *work);
 #else /* CONFIG_UNACCEPTED_MEMORY */
 static inline void accept_page(struct page *page)
 {
mm/mm_init.c +0 -1
index 9659689b8ace..84f14fa12d0d 100644
@@ -1441,7 +1441,6 @@ static void __meminit zone_init_free_lists(struct zone *zone)
 
 #ifdef CONFIG_UNACCEPTED_MEMORY
 	INIT_LIST_HEAD(&zone->unaccepted_pages);
-	INIT_WORK(&zone->unaccepted_cleanup, unaccepted_cleanup_work);
 #endif
 }
 
mm/page_alloc.c +0 -47

97 lines not on this page · raw message

Signature · 1 line
2.47.2