Skip to content

Commit fefc075

Browse files
kirylakpm00
authored andcommitted
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. Link: https://lkml.kernel.org/r/20250506133207.1009676-1-kirill.shutemov@linux.intel.com Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Fixes: dcdfdd4 ("mm: Add support for unaccepted memory") Link: https://lore.kernel.org/all/20250506092445.GBaBnVXXyvnazly6iF@fat_crate.local Reported-by: Borislav Petkov <bp@alien8.de> Tested-by: Borislav Petkov (AMD) <bp@alien8.de> Reported-by: Thomas Gleixner <tglx@linutronix.de> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Brendan Jackman <jackmanb@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: <stable@vger.kernel.org> [6.5+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 23fa022 commit fefc075

File tree

3 files changed

+0
-49
lines changed

3 files changed

+0
-49
lines changed

mm/internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,6 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc);
15901590

15911591
#ifdef CONFIG_UNACCEPTED_MEMORY
15921592
void accept_page(struct page *page);
1593-
void unaccepted_cleanup_work(struct work_struct *work);
15941593
#else /* CONFIG_UNACCEPTED_MEMORY */
15951594
static inline void accept_page(struct page *page)
15961595
{

mm/mm_init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,6 @@ static void __meminit zone_init_free_lists(struct zone *zone)
14411441

14421442
#ifdef CONFIG_UNACCEPTED_MEMORY
14431443
INIT_LIST_HEAD(&zone->unaccepted_pages);
1444-
INIT_WORK(&zone->unaccepted_cleanup, unaccepted_cleanup_work);
14451444
#endif
14461445
}
14471446

mm/page_alloc.c

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7172,16 +7172,8 @@ bool has_managed_dma(void)
71727172

71737173
#ifdef CONFIG_UNACCEPTED_MEMORY
71747174

7175-
/* Counts number of zones with unaccepted pages. */
7176-
static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages);
7177-
71787175
static bool lazy_accept = true;
71797176

7180-
void unaccepted_cleanup_work(struct work_struct *work)
7181-
{
7182-
static_branch_dec(&zones_with_unaccepted_pages);
7183-
}
7184-
71857177
static int __init accept_memory_parse(char *p)
71867178
{
71877179
if (!strcmp(p, "lazy")) {
@@ -7206,11 +7198,7 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
72067198
static void __accept_page(struct zone *zone, unsigned long *flags,
72077199
struct page *page)
72087200
{
7209-
bool last;
7210-
72117201
list_del(&page->lru);
7212-
last = list_empty(&zone->unaccepted_pages);
7213-
72147202
account_freepages(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
72157203
__mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES);
72167204
__ClearPageUnaccepted(page);
@@ -7219,28 +7207,6 @@ static void __accept_page(struct zone *zone, unsigned long *flags,
72197207
accept_memory(page_to_phys(page), PAGE_SIZE << MAX_PAGE_ORDER);
72207208

72217209
__free_pages_ok(page, MAX_PAGE_ORDER, FPI_TO_TAIL);
7222-
7223-
if (last) {
7224-
/*
7225-
* There are two corner cases:
7226-
*
7227-
* - If allocation occurs during the CPU bring up,
7228-
* static_branch_dec() cannot be used directly as
7229-
* it causes a deadlock on cpu_hotplug_lock.
7230-
*
7231-
* Instead, use schedule_work() to prevent deadlock.
7232-
*
7233-
* - If allocation occurs before workqueues are initialized,
7234-
* static_branch_dec() should be called directly.
7235-
*
7236-
* Workqueues are initialized before CPU bring up, so this
7237-
* will not conflict with the first scenario.
7238-
*/
7239-
if (system_wq)
7240-
schedule_work(&zone->unaccepted_cleanup);
7241-
else
7242-
unaccepted_cleanup_work(&zone->unaccepted_cleanup);
7243-
}
72447210
}
72457211

72467212
void accept_page(struct page *page)
@@ -7277,20 +7243,12 @@ static bool try_to_accept_memory_one(struct zone *zone)
72777243
return true;
72787244
}
72797245

7280-
static inline bool has_unaccepted_memory(void)
7281-
{
7282-
return static_branch_unlikely(&zones_with_unaccepted_pages);
7283-
}
7284-
72857246
static bool cond_accept_memory(struct zone *zone, unsigned int order,
72867247
int alloc_flags)
72877248
{
72887249
long to_accept, wmark;
72897250
bool ret = false;
72907251

7291-
if (!has_unaccepted_memory())
7292-
return false;
7293-
72947252
if (list_empty(&zone->unaccepted_pages))
72957253
return false;
72967254

@@ -7328,22 +7286,17 @@ static bool __free_unaccepted(struct page *page)
73287286
{
73297287
struct zone *zone = page_zone(page);
73307288
unsigned long flags;
7331-
bool first = false;
73327289

73337290
if (!lazy_accept)
73347291
return false;
73357292

73367293
spin_lock_irqsave(&zone->lock, flags);
7337-
first = list_empty(&zone->unaccepted_pages);
73387294
list_add_tail(&page->lru, &zone->unaccepted_pages);
73397295
account_freepages(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
73407296
__mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES);
73417297
__SetPageUnaccepted(page);
73427298
spin_unlock_irqrestore(&zone->lock, flags);
73437299

7344-
if (first)
7345-
static_branch_inc(&zones_with_unaccepted_pages);
7346-
73477300
return true;
73487301
}
73497302

0 commit comments

Comments
 (0)