Skip to content

Commit 4067196

Browse files
kirylakpm00
authored andcommitted
mm/page_alloc: fix deadlock on cpu_hotplug_lock in __accept_page()
When the last page in the zone is accepted, __accept_page() calls static_branch_dec(). This function takes cpu_hotplug_lock, which can lead to a deadlock if the allocation occurs during CPU bringup path as _cpu_up() also takes the lock. To prevent this deadlock, defer static_branch_dec() to a workqueue. Call static_branch_dec() only when the workqueue is not yet initialized. Workqueues are initialized before CPU bring up, so this will not conflict with the first scenario. Link: https://lkml.kernel.org/r/20250329171030.3942298-1-kirill.shutemov@linux.intel.com Fixes: 55ad43e ("mm: add a helper to accept page") Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reported-by: Srikanth Aithal <sraithal@amd.com> Tested-by: Srikanth Aithal <sraithal@amd.com> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Ashish Kalra <ashish.kalra@amd.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: "Mike Rapoport (IBM)" <rppt@kernel.org> Cc: Thomas Lendacky <thomas.lendacky@amd.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent fc96b23 commit 4067196

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

include/linux/mmzone.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ struct zone {
967967
#ifdef CONFIG_UNACCEPTED_MEMORY
968968
/* Pages to be accepted. All pages on the list are MAX_PAGE_ORDER */
969969
struct list_head unaccepted_pages;
970+
971+
/* To be called once the last page in the zone is accepted */
972+
struct work_struct unaccepted_cleanup;
970973
#endif
971974

972975
/* zone flags, see below */

mm/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ unsigned long move_page_tables(struct pagetable_move_control *pmc);
15951595

15961596
#ifdef CONFIG_UNACCEPTED_MEMORY
15971597
void accept_page(struct page *page);
1598+
void unaccepted_cleanup_work(struct work_struct *work);
15981599
#else /* CONFIG_UNACCEPTED_MEMORY */
15991600
static inline void accept_page(struct page *page)
16001601
{

mm/mm_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ 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);
14441445
#endif
14451446
}
14461447

mm/page_alloc.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7191,6 +7191,11 @@ static DEFINE_STATIC_KEY_FALSE(zones_with_unaccepted_pages);
71917191

71927192
static bool lazy_accept = true;
71937193

7194+
void unaccepted_cleanup_work(struct work_struct *work)
7195+
{
7196+
static_branch_dec(&zones_with_unaccepted_pages);
7197+
}
7198+
71947199
static int __init accept_memory_parse(char *p)
71957200
{
71967201
if (!strcmp(p, "lazy")) {
@@ -7229,8 +7234,27 @@ static void __accept_page(struct zone *zone, unsigned long *flags,
72297234

72307235
__free_pages_ok(page, MAX_PAGE_ORDER, FPI_TO_TAIL);
72317236

7232-
if (last)
7233-
static_branch_dec(&zones_with_unaccepted_pages);
7237+
if (last) {
7238+
/*
7239+
* There are two corner cases:
7240+
*
7241+
* - If allocation occurs during the CPU bring up,
7242+
* static_branch_dec() cannot be used directly as
7243+
* it causes a deadlock on cpu_hotplug_lock.
7244+
*
7245+
* Instead, use schedule_work() to prevent deadlock.
7246+
*
7247+
* - If allocation occurs before workqueues are initialized,
7248+
* static_branch_dec() should be called directly.
7249+
*
7250+
* Workqueues are initialized before CPU bring up, so this
7251+
* will not conflict with the first scenario.
7252+
*/
7253+
if (system_wq)
7254+
schedule_work(&zone->unaccepted_cleanup);
7255+
else
7256+
unaccepted_cleanup_work(&zone->unaccepted_cleanup);
7257+
}
72347258
}
72357259

72367260
void accept_page(struct page *page)

0 commit comments

Comments
 (0)