mm/page_alloc: Fix couple of unaccepted memory issues
Subsystems (5)
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
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.
--- mm/page_alloc.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-)
mm/page_alloc.c +15 -13
@@ -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
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?
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
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_lockinsideaccept_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().
Re: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
In reply to Kirill A. ShutemovQuoted 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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.Signed-off-by Kirill A. Shutemov <[email protected]>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.
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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.Signed-off-by Kirill A. Shutemov <[email protected]>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
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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Signed-off-by Kirill A. Shutemov <[email protected]>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.
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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.Signed-off-by Kirill A. Shutemov <[email protected]>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
Andrew Morton
maintainerRe: [PATCH 1/2] mm/page_alloc: Ensure try_alloc_pages() plays well with unaccepted memory
In reply to Kirill A. ShutemovQuoted 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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.
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.
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' tocond_accept_memory()and do not accept new memory forALLOC_TRYLOCKrequests.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
PATCH 2/2 mm/page_alloc: Fix race condition in unaccepted memory handling
In reply to Kirill A. ShutemovThe 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.
--- mm/internal.h | 1 - mm/mm_init.c | 1 - mm/page_alloc.c | 47 ----------------------------------------------- 3 files changed, 49 deletions(-)
mm/internal.h +0 -1
@@ -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
@@ -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
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_branchare not serialized against adding/removing unaccepted pages to/from the zone.
Also, that sentence needs massaging.
Quoted 4 lines
The effect of this
static_branchoptimization is only visible on microbenchmark.Instead of adding more complexity around it, remove it altogether.
Yah, good idea.
Quoted 4 lines
Signed-off-by Kirill A. Shutemov <[email protected]>Fixes dcdfdd40fa82 ("mm: Add support for unaccepted memory")Reported-by Borislav Petkov <[email protected]>
Thx for the quick fix.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 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.
---
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
@@ -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
@@ -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