Skip to content

Commit 71dda1c

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/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> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 361b4cc commit 71dda1c

1 file changed

Lines changed: 0 additions & 23 deletions

File tree

mm/page_alloc.c

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

69526952
#ifdef CONFIG_UNACCEPTED_MEMORY
69536953

6954-
/* Counts number of zones with unaccepted pages. */
6955-
static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages);
6956-
69576954
static bool lazy_accept = true;
69586955

69596956
static int __init accept_memory_parse(char *p)
@@ -6980,11 +6977,7 @@ static bool page_contains_unaccepted(struct page *page, unsigned int order)
69806977
static void __accept_page(struct zone *zone, unsigned long *flags,
69816978
struct page *page)
69826979
{
6983-
bool last;
6984-
69856980
list_del(&page->lru);
6986-
last = list_empty(&zone->unaccepted_pages);
6987-
69886981
account_freepages(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
69896982
__mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES);
69906983
__ClearPageUnaccepted(page);
@@ -6993,9 +6986,6 @@ static void __accept_page(struct zone *zone, unsigned long *flags,
69936986
accept_memory(page_to_phys(page), PAGE_SIZE << MAX_PAGE_ORDER);
69946987

69956988
__free_pages_ok(page, MAX_PAGE_ORDER, FPI_TO_TAIL);
6996-
6997-
if (last)
6998-
static_branch_dec(&zones_with_unaccepted_pages);
69996989
}
70006990

70016991
void accept_page(struct page *page)
@@ -7032,19 +7022,11 @@ static bool try_to_accept_memory_one(struct zone *zone)
70327022
return true;
70337023
}
70347024

7035-
static inline bool has_unaccepted_memory(void)
7036-
{
7037-
return static_branch_unlikely(&zones_with_unaccepted_pages);
7038-
}
7039-
70407025
static bool cond_accept_memory(struct zone *zone, unsigned int order)
70417026
{
70427027
long to_accept, wmark;
70437028
bool ret = false;
70447029

7045-
if (!has_unaccepted_memory())
7046-
return false;
7047-
70487030
if (list_empty(&zone->unaccepted_pages))
70497031
return false;
70507032

@@ -7078,22 +7060,17 @@ static bool __free_unaccepted(struct page *page)
70787060
{
70797061
struct zone *zone = page_zone(page);
70807062
unsigned long flags;
7081-
bool first = false;
70827063

70837064
if (!lazy_accept)
70847065
return false;
70857066

70867067
spin_lock_irqsave(&zone->lock, flags);
7087-
first = list_empty(&zone->unaccepted_pages);
70887068
list_add_tail(&page->lru, &zone->unaccepted_pages);
70897069
account_freepages(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE);
70907070
__mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES);
70917071
__SetPageUnaccepted(page);
70927072
spin_unlock_irqrestore(&zone->lock, flags);
70937073

7094-
if (first)
7095-
static_branch_inc(&zones_with_unaccepted_pages);
7096-
70977074
return true;
70987075
}
70997076

0 commit comments

Comments
 (0)