Skip to content

Commit 74953f9

Browse files
kirylgregkh
authored andcommitted
mm/page_alloc: fix race condition in unaccepted memory handling
commit fefc075 upstream. 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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kirill A. Shutemov <[email protected]> Fixes: dcdfdd4 ("mm: Add support for unaccepted memory") Link: https://lore.kernel.org/all/20250506092445.GBaBnVXXyvnazly6iF@fat_crate.local Reported-by: Borislav Petkov <[email protected]> Tested-by: Borislav Petkov (AMD) <[email protected]> Reported-by: Thomas Gleixner <[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]> Cc: <[email protected]> [6.5+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 260ca2e commit 74953f9

File tree

1 file changed

+0
-23
lines changed

1 file changed

+0
-23
lines changed

mm/page_alloc.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7041,9 +7041,6 @@ bool has_managed_dma(void)
70417041

70427042
#ifdef CONFIG_UNACCEPTED_MEMORY
70437043

7044-
/* Counts number of zones with unaccepted pages. */
7045-
static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages);
7046-
70477044
static bool lazy_accept = true;
70487045

70497046
static int __init accept_memory_parse(char *p)
@@ -7070,11 +7067,7 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
70707067
static void __accept_page(struct zone *zone, unsigned long *flags,
70717068
struct page *page)
70727069
{
7073-
bool last;
7074-
70757070
list_del(&page->lru);
7076-
last = list_empty(&zone->unaccepted_pages);
7077-
70787071
account_freepages(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
70797072
__mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES);
70807073
__ClearPageUnaccepted(page);
@@ -7083,9 +7076,6 @@ static void __accept_page(struct zone *zone, unsigned long *flags,
70837076
accept_memory(page_to_phys(page), PAGE_SIZE << MAX_PAGE_ORDER);
70847077

70857078
__free_pages_ok(page, MAX_PAGE_ORDER, FPI_TO_TAIL);
7086-
7087-
if (last)
7088-
static_branch_dec(&zones_with_unaccepted_pages);
70897079
}
70907080

70917081
void accept_page(struct page *page)
@@ -7122,19 +7112,11 @@ static bool try_to_accept_memory_one(struct zone *zone)
71227112
return true;
71237113
}
71247114

7125-
static inline bool has_unaccepted_memory(void)
7126-
{
7127-
return static_branch_unlikely(&zones_with_unaccepted_pages);
7128-
}
7129-
71307115
static bool cond_accept_memory(struct zone *zone, unsigned int order)
71317116
{
71327117
long to_accept, wmark;
71337118
bool ret = false;
71347119

7135-
if (!has_unaccepted_memory())
7136-
return false;
7137-
71387120
if (list_empty(&zone->unaccepted_pages))
71397121
return false;
71407122

@@ -7168,22 +7150,17 @@ static bool __free_unaccepted(struct page *page)
71687150
{
71697151
struct zone *zone = page_zone(page);
71707152
unsigned long flags;
7171-
bool first = false;
71727153

71737154
if (!lazy_accept)
71747155
return false;
71757156

71767157
spin_lock_irqsave(&zone->lock, flags);
7177-
first = list_empty(&zone->unaccepted_pages);
71787158
list_add_tail(&page->lru, &zone->unaccepted_pages);
71797159
account_freepages(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
71807160
__mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES);
71817161
__SetPageUnaccepted(page);
71827162
spin_unlock_irqrestore(&zone->lock, flags);
71837163

7184-
if (first)
7185-
static_branch_inc(&zones_with_unaccepted_pages);
7186-
71877164
return true;
71887165
}
71897166

0 commit comments

Comments
 (0)