From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30A122F37; Tue, 20 May 2025 14:05:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747749927; cv=none; b=oLOArQpMMiIL+uEl1uRPDamqgd3dukPgWZve2Py5KCTqrySRDedkIO5lFcEaKxUQuzXnCTjNB1oe4urtcu/JDAlYcl95RpAqqgZ0hjFSuRQwC5J6bYKBubw85X9g5Finfhh4jAx2ug1Q6wo1d+Bu+5Slu4O4nJTMPEF+eRrvfJw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747749927; c=relaxed/simple; bh=SqamiV8I18T8cjb8bb316UzMM86EJNNf6iJ1qEOYomM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KHuQvDjXje/rT9dJJVI7QxI9cnIezAWlW0VWNCsR3menTNFaZvKOYaZs/+r4V6fIUzSqTMTnXTwYpOzaGjt+bdRuKt0vglzMuUTZa24o3N57cZRQJuWnD+wckyniqlwnS4VNNaTzWjKyqAxomBlWMRCnz89zHEpJujbLnDY+KbY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=gPGURp50; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="gPGURp50" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD266C4CEEA; Tue, 20 May 2025 14:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1747749927; bh=SqamiV8I18T8cjb8bb316UzMM86EJNNf6iJ1qEOYomM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gPGURp50oWDprTUkuzAfV0lmNOUNzLa7HwlMieGT6nD412GFq2s1a6Glt2+GydmaA mTZJ6KVifAt5r6NYa4XzePwS5sKHUODrXNGHxu1U90agVxLq78TsoJPizjwDB++MHx i7gNaxbOxvXxLUEJSgUSAKWrRxEG4ExBCbF2m2z0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Kirill A. Shutemov" , Borislav Petkov , Thomas Gleixner , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Andrew Morton Subject: [PATCH 6.6 104/117] mm/page_alloc: fix race condition in unaccepted memory handling Date: Tue, 20 May 2025 15:51:09 +0200 Message-ID: <20250520125808.124375642@linuxfoundation.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250520125803.981048184@linuxfoundation.org> References: <20250520125803.981048184@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kirill A. Shutemov commit fefc075182275057ce607effaa3daa9e6e3bdc73 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 Fixes: dcdfdd40fa82 ("mm: Add support for unaccepted memory") Link: https://lore.kernel.org/all/20250506092445.GBaBnVXXyvnazly6iF@fat_crate.local Reported-by: Borislav Petkov Tested-by: Borislav Petkov (AMD) Reported-by: Thomas Gleixner Cc: Vlastimil Babka Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Brendan Jackman Cc: Johannes Weiner Cc: [6.5+] Signed-off-by: Andrew Morton Signed-off-by: Kirill A. Shutemov Signed-off-by: Greg Kroah-Hartman --- mm/page_alloc.c | 27 --------------------------- 1 file changed, 27 deletions(-) --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -303,7 +303,6 @@ EXPORT_SYMBOL(nr_online_nodes); static bool page_contains_unaccepted(struct page *page, unsigned int order); static void accept_page(struct page *page, unsigned int order); static bool cond_accept_memory(struct zone *zone, unsigned int order); -static inline bool has_unaccepted_memory(void); static bool __free_unaccepted(struct page *page); int page_group_by_mobility_disabled __read_mostly; @@ -6586,9 +6585,6 @@ bool has_managed_dma(void) #ifdef CONFIG_UNACCEPTED_MEMORY -/* Counts number of zones with unaccepted pages. */ -static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages); - static bool lazy_accept = true; static int __init accept_memory_parse(char *p) @@ -6624,7 +6620,6 @@ static bool try_to_accept_memory_one(str { unsigned long flags; struct page *page; - bool last; spin_lock_irqsave(&zone->lock, flags); page = list_first_entry_or_null(&zone->unaccepted_pages, @@ -6635,7 +6630,6 @@ static bool try_to_accept_memory_one(str } list_del(&page->lru); - last = list_empty(&zone->unaccepted_pages); __mod_zone_freepage_state(zone, -MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); __mod_zone_page_state(zone, NR_UNACCEPTED, -MAX_ORDER_NR_PAGES); @@ -6645,9 +6639,6 @@ static bool try_to_accept_memory_one(str __free_pages_ok(page, MAX_ORDER, FPI_TO_TAIL); - if (last) - static_branch_dec(&zones_with_unaccepted_pages); - return true; } @@ -6656,9 +6647,6 @@ static bool cond_accept_memory(struct zo long to_accept, wmark; bool ret = false; - if (!has_unaccepted_memory()) - return false; - if (list_empty(&zone->unaccepted_pages)) return false; @@ -6688,30 +6676,20 @@ static bool cond_accept_memory(struct zo return ret; } -static inline bool has_unaccepted_memory(void) -{ - return static_branch_unlikely(&zones_with_unaccepted_pages); -} - static bool __free_unaccepted(struct page *page) { struct zone *zone = page_zone(page); unsigned long flags; - bool first = false; if (!lazy_accept) return false; spin_lock_irqsave(&zone->lock, flags); - first = list_empty(&zone->unaccepted_pages); list_add_tail(&page->lru, &zone->unaccepted_pages); __mod_zone_freepage_state(zone, MAX_ORDER_NR_PAGES, MIGRATE_MOVABLE); __mod_zone_page_state(zone, NR_UNACCEPTED, MAX_ORDER_NR_PAGES); spin_unlock_irqrestore(&zone->lock, flags); - if (first) - static_branch_inc(&zones_with_unaccepted_pages); - return true; } @@ -6730,11 +6708,6 @@ static bool cond_accept_memory(struct zo { return false; } - -static inline bool has_unaccepted_memory(void) -{ - return false; -} static bool __free_unaccepted(struct page *page) {