FAILED: patch "[PATCH] mm/memory-failure: fix hugetlb_lock AA deadlock in" failed to apply to 6.1-stable tree
Subsystems (5)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 3c2d42b8ee345b17a4ba56b0f6492d1ff4c1178e
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026061514-giblet-unsworn-8735@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 3c2d42b8ee345b17a4ba56b0f6492d1ff4c1178e Mon Sep 17 00:00:00 2001
From: Wupeng Ma <[email protected]>
Date: Fri, 22 May 2026 09:03:05 +0800
Subject: [PATCH] mm/memory-failure: fix hugetlb_lock AA deadlock in
get_huge_page_for_hwpoison
Two concurrent madvise(MADV_HWPOISON) calls on the same hugetlb page can trigger a recursive spinlock self-deadlock (AA deadlock) on hugetlb_lock when racing with a concurrent unmap:
thread#0 thread#1
-------- --------
madvise(folio, MADV_HWPOISON)
-> poisons the folio successfully
madvise(folio, MADV_HWPOISON) unmap(folio)
try_memory_failure_hugetlb
get_huge_page_for_hwpoison
spin_lock_irq(&hugetlb_lock) <- held
__get_huge_page_for_hwpoison
hugetlb_update_hwpoison()
-> MF_HUGETLB_FOLIO_PRE_POISONED
goto out:
folio_put()
refcount: 1 -> 0
free_huge_folio()
spin_lock_irqsave(&hugetlb_lock)
-> AA DEADLOCK!The out: path in __get_huge_page_for_hwpoison() calls folio_put() to drop the GUP reference while the hugetlb_lock is still held by the hugetlb.c wrapper get_huge_page_for_hwpoison(). If concurrent unmap has released the page table mapping reference, folio_put() drops the folio refcount to zero, triggering free_huge_folio() which attempts to re-acquire the non-recursive hugetlb_lock.
Fix this by moving hugetlb_lock acquisition from the hugetlb.c wrapper into get_huge_page_for_hwpoison(). Place spin_unlock_irq() before the folio_put() at the out: label so the folio is always released outside the lock.
include/linux/hugetlb.h +0 -8
@@ -153,8 +153,6 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end, long freed);bool folio_isolate_hugetlb (struct folio *folio,struct list_head *list);int get_hwpoison_hugetlb_folio (struct folio *folio,bool *hugetlb,bool unpoison); -int get_huge_page_for_hwpoison (unsigned long pfn,int flags, -bool *migratable_cleared);void folio_putback_hugetlb (struct folio *folio);void move_hugetlb_state (struct folio *old_folio,struct folio *new_folio,int reason);void hugetlb_fix_reserve_counts (struct inode *inode);
@@ -421,12 +419,6 @@ static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb,return 0 ; } -static inline int get_huge_page_for_hwpoison (unsigned long pfn,int flags, -bool *migratable_cleared) -{ -return 0 ; -} -static inline void folio_putback_hugetlb (struct folio *folio) { }
include/linux/mm.h +0 -8
@@ -4975,8 +4975,6 @@ extern int soft_offline_page(unsigned long pfn, int flags);*/ extern const struct attribute_group memory_failure_attr_group;extern void memory_failure_queue (unsigned long pfn,int flags); -extern int __get_huge_page_for_hwpoison (unsigned long pfn,int flags, -bool *migratable_cleared);void num_poisoned_pages_inc (unsigned long pfn);void num_poisoned_pages_sub (unsigned long pfn,long i);#else
@@ -4984,12 +4982,6 @@ static inline void memory_failure_queue(unsigned long pfn, int flags) { } -static inline int __get_huge_page_for_hwpoison (unsigned long pfn,int flags, -bool *migratable_cleared) -{ -return 0 ; -} -static inline void num_poisoned_pages_inc (unsigned long pfn) { }
mm/hugetlb.c +0 -11
@@ -7161,17 +7161,6 @@ int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoisonreturn ret; } -int get_huge_page_for_hwpoison (unsigned long pfn,int flags, -bool *migratable_cleared) -{ -int ret; - -spin_lock_irq (&hugetlb_lock); - ret =__get_huge_page_for_hwpoison (pfn, flags, migratable_cleared); -spin_unlock_irq (&hugetlb_lock); -return ret; -} -/** * folio_putback_hugetlb - unisolate a hugetlb folio * @folio: the isolated hugetlb folio
mm/memory-failure.c +10 -9
@@ -1966,20 +1966,19 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio)folio_free_raw_hwp (folio, true); } -/* -* Called from hugetlb code with hugetlb_lock held. -*/ -int __get_huge_page_for_hwpoison (unsigned long pfn,int flags, +static int get_huge_page_for_hwpoison (unsigned long pfn,int flags,bool *migratable_cleared) {struct page *page =pfn_to_page (pfn); -struct folio *folio =page_folio (page); +struct folio *folio;bool count_increased = false;int ret, rc; +spin_lock_irq (&hugetlb_lock); + folio =page_folio (page);if (!folio_test_hugetlb (folio)) { ret =MF_HUGETLB_NON_HUGEPAGE ; - goto out; + goto out_unlock; }else if (flags &MF_COUNT_INCREASED ) { ret =MF_HUGETLB_IN_USED ; count_increased = true;
@@ -1995,13 +1994,13 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, }else { ret =MF_HUGETLB_RETRY ;if (!(flags &MF_NO_RETRY )) - goto out; + goto out_unlock; } rc =hugetlb_update_hwpoison (folio, page);if (rc >=MF_HUGETLB_FOLIO_PRE_POISONED ) { ret = rc; - goto out; + goto out_unlock; }/*
@@ -2013,8 +2012,10 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags, *migratable_cleared = true; } +spin_unlock_irq (&hugetlb_lock);return ret; -out: +out_unlock: +spin_unlock_irq (&hugetlb_lock);if (count_increased)folio_put (folio);return ret;
Sasha Levin
maintainerPATCH 1/5 mm/hugetlb: rename isolate_hugetlb() to folio_isolate_hugetlb()
From: David Hildenbrand <[email protected]>
[ Upstream commit 4c640f128074e0d4459ecf072595a44df5c2ae18 ]
Let's make the function name match "folio_isolate_lru()", and add some kernel doc.
Stable-dep-of: 3c2d42b8ee34 ("mm/memory-failure: fix hugetlb_lock AA deadlock in get_huge_page_for_hwpoison")
--- include/linux/hugetlb.h | 4 ++-- mm/gup.c | 2 +- mm/hugetlb.c | 23 ++++++++++++++++++++--- mm/memory-failure.c | 2 +- mm/memory_hotplug.c | 2 +- mm/mempolicy.c | 2 +- mm/migrate.c | 4 ++-- 7 files changed, 28 insertions(+), 11 deletions(-)
include/linux/hugetlb.h +2 -2
@@ -182,7 +182,7 @@ bool hugetlb_reserve_pages(struct inode *inode, long from, long to, vm_flags_t vm_flags);long hugetlb_unreserve_pages (struct inode *inode,long start,long end,long freed); -int isolate_hugetlb (struct page *page,struct list_head *list); +int folio_isolate_hugetlb (struct page *page,struct list_head *list);int get_hwpoison_huge_page (struct page *page,bool *hugetlb);int get_huge_page_for_hwpoison (unsigned long pfn,int flags);void putback_active_hugepage (struct page *page);
@@ -428,7 +428,7 @@ static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,return NULL ; } -static inline int isolate_hugetlb (struct page *page,struct list_head *list) +static inline int folio_isolate_hugetlb (struct page *page,struct list_head *list) {return -EBUSY ; }
mm/gup.c +1 -1
@@ -1986,7 +1986,7 @@ static unsigned long collect_longterm_unpinnable_pages(continue ;if (folio_test_hugetlb (folio)) { -isolate_hugetlb (&folio->page, movable_page_list); +folio_isolate_hugetlb (&folio->page, movable_page_list);continue ; }
mm/hugetlb.c +20 -3
@@ -2991,7 +2991,7 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page,* Fail with -EBUSY if not possible. */ spin_unlock_irq (&hugetlb_lock); - ret =isolate_hugetlb (old_page, list); + ret =folio_isolate_hugetlb (old_page, list);spin_lock_irq (&hugetlb_lock); goto free_new; }else if (!HPageFreed (old_page)) {
@@ -3067,7 +3067,7 @@ int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list)if (hstate_is_gigantic (h))return -ENOMEM ; -if (page_count (head) && !isolate_hugetlb (head, list)) +if (page_count (head) && !folio_isolate_hugetlb (head, list)) ret =0 ;else if (!page_count (head)) ret =alloc_and_dissolve_huge_page (h, head, list);
@@ -7427,7 +7427,24 @@ follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flareturn pte_page (*(pte_t *)pgd) + ((address & ~PGDIR_MASK ) >>PAGE_SHIFT ); } -int isolate_hugetlb (struct page *page,struct list_head *list) +/** +* folio_isolate_hugetlb - try to isolate an allocated hugetlb page +* @page: the page to isolate +* @list: the list to add the page to on success +* +* Isolate an allocated (refcount > 0) hugetlb page, marking it as +* isolated/non-migratable, and moving it from the active list to the +* given list. +* +* Isolation will fail if @page is not an allocated hugetlb page, or if +* it is already isolated/non-migratable. +* +* On success, an additional page reference is taken that must be dropped +* using putback_active_hugepage() to undo the isolation. +* +* Return: 0 if isolation worked, otherwise -EBUSY. +*/ +int folio_isolate_hugetlb (struct page *page,struct list_head *list) {int ret =0 ;
mm/memory-failure.c +1 -1
@@ -2444,7 +2444,7 @@ static bool isolate_page(struct page *page, struct list_head *pagelist)bool isolated = false;if (PageHuge (page)) { - isolated = !isolate_hugetlb (page, pagelist); + isolated = !folio_isolate_hugetlb (page, pagelist); }else {bool lru = !__PageMovable (page);
mm/memory_hotplug.c +1 -1
@@ -1641,7 +1641,7 @@ do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)if (PageHuge (page)) { pfn =page_to_pfn (head) +compound_nr (head) -1 ; -isolate_hugetlb (head, &source); +folio_isolate_hugetlb (head, &source);continue ; }else if (PageTransHuge (page)) pfn =page_to_pfn (head) +thp_nr_pages (page) -1 ;
mm/mempolicy.c +1 -1
@@ -598,7 +598,7 @@ static int queue_pages_hugetlb(pte_t *pte, unsigned long hmask,if (flags & (MPOL_MF_MOVE_ALL ) || (flags &MPOL_MF_MOVE &&page_mapcount (page) ==1 && !hugetlb_pmd_shared (pte))) { -if (isolate_hugetlb (page, qp->pagelist) && +if (folio_isolate_hugetlb (page, qp->pagelist) && (flags &MPOL_MF_STRICT ))/* * Failed to isolate page but allow migrating pages
mm/migrate.c +2 -2
@@ -133,7 +133,7 @@ static void putback_movable_page(struct page *page)* * This function shall be used whenever the isolated pageset has been * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range() -* and isolate_hugetlb(). +* and folio_isolate_hugetlb(). */ void putback_movable_pages (struct list_head *l) {
@@ -1995,7 +1995,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,if (PageHuge (page)) {if (PageHead (page)) { - err =isolate_hugetlb (page, pagelist); + err =folio_isolate_hugetlb (page, pagelist);if (!err) err =1 ; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/5 mm/migrate: don't call folio_putback_active_hugetlb() on dst hugetlb folio
From: David Hildenbrand <[email protected]>
[ Upstream commit ba23f58de896842028b4b33b95530f08288396fe ]
We replaced a simple put_page() by a putback_active_hugepage() call in commit 3aaa76e125c1 ("mm: migrate: hugetlb: putback destination hugepage to active list"), to set the "active" flag on the dst hugetlb folio.
Nowadays, we decoupled the "active" list from the flag, by calling the flag "migratable".
Calling "putback" on something that wasn't allocated is weird and not future proof, especially if we might reach that path when migration failed and we just want to free the freshly allocated hugetlb folio.
Let's simply handle the migratable flag and the active list flag in move_hugetlb_state(), where we know that allocation succeeded and already handle the temporary flag; use a simple folio_put() to return our reference.
Stable-dep-of: 3c2d42b8ee34 ("mm/memory-failure: fix hugetlb_lock AA deadlock in get_huge_page_for_hwpoison")
--- mm/hugetlb.c | 10 ++++++++++ mm/migrate.c | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-)
mm/hugetlb.c +10 -0
@@ -7537,6 +7537,16 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason) }spin_unlock_irq (&hugetlb_lock); } + +/* +* Our old page is isolated and has "migratable" cleared until it +* is putback. As migration succeeded, set the new page "migratable" +* and add it to the active list. +*/ +spin_lock_irq (&hugetlb_lock); +SetHPageMigratable (newpage); +list_move_tail (&newpage->lru, &(page_hstate (newpage))->hugepage_activelist); +spin_unlock_irq (&hugetlb_lock); }/*
mm/migrate.c +4 -4
@@ -1460,14 +1460,14 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,list_move_tail (&src->lru, ret);/* -* If migration was not successful and there's a freeing callback, use -* it. Otherwise, put_page() will drop the reference grabbed during -* isolation. +* If migration was not successful and there's a freeing callback, +* return the folio to that special allocator. Otherwise, simply drop +* our additional reference. */ if (put_new_page)put_new_page (new_hpage, private);else -putback_active_hugepage (new_hpage); +folio_put (dst);return rc; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/5 mm/hugetlb: rename folio_putback_active_hugetlb() to folio_putback_hugetlb()
In reply to Sasha LevinFrom: David Hildenbrand <[email protected]>
[ Upstream commit b235448e8cab7eea17d164efc7bf55505985ba65 ]
Now that folio_putback_hugetlb() is only called on folios that were previously isolated through folio_isolate_hugetlb(), let's rename it to match folio_putback_lru().
Add some kernel doc to clarify how this function is supposed to be used.
Stable-dep-of: 3c2d42b8ee34 ("mm/memory-failure: fix hugetlb_lock AA deadlock in get_huge_page_for_hwpoison")
--- include/linux/hugetlb.h | 4 ++-- mm/hugetlb.c | 15 +++++++++++++-- mm/migrate.c | 6 +++--- 3 files changed, 18 insertions(+), 7 deletions(-)
include/linux/hugetlb.h +2 -2
@@ -185,7 +185,7 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end,int folio_isolate_hugetlb (struct page *page,struct list_head *list);int get_hwpoison_huge_page (struct page *page,bool *hugetlb);int get_huge_page_for_hwpoison (unsigned long pfn,int flags); -void putback_active_hugepage (struct page *page); +void folio_putback_hugetlb (struct page *page);void move_hugetlb_state (struct page *oldpage,struct page *newpage,int reason);void free_huge_page (struct page *page);void hugetlb_fix_reserve_counts (struct inode *inode);
@@ -443,7 +443,7 @@ static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags)return 0 ; } -static inline void putback_active_hugepage (struct page *page) +static inline void folio_putback_hugetlb (struct page *page) { }
mm/hugetlb.c +13 -2
@@ -7440,7 +7440,7 @@ follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int fla* it is already isolated/non-migratable. * * On success, an additional page reference is taken that must be dropped -* using putback_active_hugepage() to undo the isolation. +* using folio_putback_hugetlb() to undo the isolation. * * Return: 0 if isolation worked, otherwise -EBUSY. */
@@ -7491,7 +7491,18 @@ int get_huge_page_for_hwpoison(unsigned long pfn, int flags)return ret; } -void putback_active_hugepage (struct page *page) +/** +* folio_putback_hugetlb - unisolate a hugetlb page +* @page: the isolated hugetlb page +* +* Putback/un-isolate the hugetlb page that was previous isolated using +* folio_isolate_hugetlb(): marking it non-isolated/migratable and putting it +* back onto the active list. +* +* Will drop the additional page reference obtained through +* folio_isolate_hugetlb(). +*/ +void folio_putback_hugetlb (struct page *page) {spin_lock_irq (&hugetlb_lock);SetHPageMigratable (page);
mm/migrate.c +3 -3
@@ -142,7 +142,7 @@ void putback_movable_pages(struct list_head *l)list_for_each_entry_safe (page, page2, l, lru) {if (unlikely (PageHuge (page))) { -putback_active_hugepage (page); +folio_putback_hugetlb (page);continue ; }list_del (&page->lru);
@@ -1371,7 +1371,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,if (folio_ref_count (src) ==1 ) {/* page was freed from under us. So we are done. */ -putback_active_hugepage (hpage); +folio_putback_hugetlb (hpage);return MIGRATEPAGE_SUCCESS ; }
@@ -1455,7 +1455,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,folio_unlock (src); out:if (rc ==MIGRATEPAGE_SUCCESS ) -putback_active_hugepage (hpage); +folio_putback_hugetlb (hpage);else if (rc != -EAGAIN )list_move_tail (&src->lru, ret);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/5 mm/memory-failure: fix missing ->mf_stats count in hugetlb poison
In reply to Sasha LevinFrom: Jane Chu <[email protected]>
[ Upstream commit a148a2040191b12b45b82cb29c281cb3036baf90 ]
When a newly poisoned subpage ends up in an already poisoned hugetlb folio, 'num_poisoned_pages' is incremented, but the per node ->mf_stats is not. Fix the inconsistency by designating action_result() to update them both.
While at it, define __get_huge_page_for_hwpoison() return values in terms of symbol names for better readibility. Also rename folio_set_hugetlb_hwpoison() to hugetlb_update_hwpoison() since the function does more than the conventional bit setting and the fact three possible return values are expected.
Stable-dep-of: 3c2d42b8ee34 ("mm/memory-failure: fix hugetlb_lock AA deadlock in get_huge_page_for_hwpoison")
--- mm/memory-failure.c | 71 +++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 28 deletions(-)
mm/memory-failure.c +43 -28
@@ -1730,12 +1730,22 @@ static unsigned long __free_raw_hwp_pages(struct page *hpage, bool move_flag)return count; } -static int hugetlb_set_page_hwpoison (struct page *hpage,struct page *page) +#define MF_HUGETLB_FREED 0/* freed hugepage */ +#define MF_HUGETLB_IN_USED 1/* in-use hugepage */ +#define MF_HUGETLB_NON_HUGEPAGE 2/* not a hugepage */ +#define MF_HUGETLB_FOLIO_PRE_POISONED 3/* folio already poisoned */ +#define MF_HUGETLB_PAGE_PRE_POISONED 4/* exact page already poisoned */ +#define MF_HUGETLB_RETRY 5/* hugepage is busy, retry */ +/* +* Set hugetlb page as hwpoisoned, update page private raw hwpoison list +* to keep track of the poisoned pages. +*/ +static int hugetlb_update_hwpoison (struct page *hpage,struct page *page) {struct llist_head *head;struct raw_hwp_page *raw_hwp;struct llist_node *t, *tnode; -int ret =TestSetPageHWPoison (hpage) ? -EHWPOISON :0 ; +int ret =TestSetPageHWPoison (hpage) ?MF_HUGETLB_FOLIO_PRE_POISONED :0 ;/* * Once the hwpoison hugepage has lost reliable raw error info,
@@ -1743,13 +1753,13 @@ static int hugetlb_set_page_hwpoison(struct page *hpage, struct page *page)* so skip to add additional raw error info. */ if (HPageRawHwpUnreliable (hpage)) -return -EHWPOISON ; +return MF_HUGETLB_FOLIO_PRE_POISONED ; head =raw_hwp_list_head (hpage);llist_for_each_safe (tnode, t, head->first) { struct raw_hwp_page *p = container_of(tnode, struct raw_hwp_page, node);if (p->page == page) -return -EHWPOISON ; +return MF_HUGETLB_PAGE_PRE_POISONED ; } raw_hwp =kmalloc (sizeof (struct raw_hwp_page ),GFP_ATOMIC );
@@ -1804,41 +1814,38 @@ void hugetlb_clear_page_hwpoison(struct page *hpage)/* * Called from hugetlb code with hugetlb_lock held. -* -* Return values: -* 0 - free hugepage -* 1 - in-use hugepage -* 2 - not a hugepage -* -EBUSY - the hugepage is busy (try to retry) -* -EHWPOISON - the hugepage is already hwpoisoned */ int __get_huge_page_for_hwpoison (unsigned long pfn,int flags) {struct page *page =pfn_to_page (pfn);struct page *head =compound_head (page); -int ret =2 ;/* fallback to normal page handling */ bool count_increased = false; +int ret, rc; -if (!PageHeadHuge (head)) +if (!PageHeadHuge (head)) { + ret =MF_HUGETLB_NON_HUGEPAGE ; goto out; - -if (flags &MF_COUNT_INCREASED ) { - ret =1 ; + }else if (flags &MF_COUNT_INCREASED ) { + ret =MF_HUGETLB_IN_USED ; count_increased = true; }else if (HPageFreed (head)) { - ret =0 ; + ret =MF_HUGETLB_FREED ; }else if (HPageMigratable (head)) { - ret =get_page_unless_zero (head); -if (ret) +if (get_page_unless_zero (head)) { + ret =MF_HUGETLB_IN_USED ; count_increased = true; + }else { + ret =MF_HUGETLB_FREED ; + } }else { - ret = -EBUSY ; + ret =MF_HUGETLB_RETRY ;if (!(flags &MF_NO_RETRY )) goto out; } -if (hugetlb_set_page_hwpoison (head, page)) { - ret = -EHWPOISON ; + rc =hugetlb_update_hwpoison (head, page); +if (rc >=MF_HUGETLB_FOLIO_PRE_POISONED ) { + ret = rc; goto out; }
@@ -1854,6 +1861,12 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags)* with basic operations like hugepage allocation/free/demotion. * So some of prechecks for hwpoison (pinning, and testing/setting * PageHWPoison) should be done in single hugetlb_lock range. +* Returns: +* 0 - not hugetlb, or recovered +* -EBUSY - not recovered +* -EOPNOTSUPP - hwpoison_filter'ed +* -EHWPOISON - folio or exact page already poisoned +* -EFAULT - kill_accessing_process finds current->mm null */ static int try_memory_failure_hugetlb (unsigned long pfn,int flags,int *hugetlb) {
@@ -1865,23 +1878,25 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb *hugetlb =1 ; retry: res =get_huge_page_for_hwpoison (pfn, flags); -if (res ==2 ) {/* fallback to normal page handling */ +if (res ==MF_HUGETLB_NON_HUGEPAGE ) {/* fallback to normal page handling */ *hugetlb =0 ;return 0 ; - }else if (res == -EHWPOISON ) { + }else if (res ==MF_HUGETLB_FOLIO_PRE_POISONED || + res ==MF_HUGETLB_PAGE_PRE_POISONED ) {pr_err ("%#lx: already hardware poisoned\n" , pfn); + res = -EHWPOISON ;if (flags &MF_ACTION_REQUIRED ) { head =compound_head (p); res =kill_accessing_process (current,page_to_pfn (head), flags); }return res; - }else if (res == -EBUSY ) { + }else if (res ==MF_HUGETLB_RETRY ) {if (!(flags &MF_NO_RETRY )) { flags |=MF_NO_RETRY ; goto retry; }action_result (pfn,MF_MSG_UNKNOWN ,MF_IGNORED ); -return res; +return -EBUSY ; } head =compound_head (p);
@@ -1890,7 +1905,7 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlbif (hwpoison_filter (p)) {hugetlb_clear_page_hwpoison (head);unlock_page (head); -if (res ==1 ) +if (res ==MF_HUGETLB_IN_USED )put_page (head);return -EOPNOTSUPP ; }
@@ -1899,7 +1914,7 @@ static int try_memory_failure_hugetlb(unsigned long pfn, int flags, int *hugetlb* Handling free hugepage. The possible race with hugepage allocation * or demotion can be prevented by PageHWPoison flag. */ -if (res ==0 ) { +if (res ==MF_HUGETLB_FREED ) {unlock_page (head);if (__page_handle_poison (p) >0 ) {page_ref_inc (p);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/5 mm/memory-failure: fix hugetlb_lock AA deadlock in get_huge_page_for_hwpoison
In reply to Sasha LevinFrom: Wupeng Ma <[email protected]>
[ Upstream commit 3c2d42b8ee345b17a4ba56b0f6492d1ff4c1178e ]
Two concurrent madvise(MADV_HWPOISON) calls on the same hugetlb page can trigger a recursive spinlock self-deadlock (AA deadlock) on hugetlb_lock when racing with a concurrent unmap:
thread#0 thread#1
-------- --------
madvise(folio, MADV_HWPOISON)
-> poisons the folio successfully
madvise(folio, MADV_HWPOISON) unmap(folio)
try_memory_failure_hugetlb
get_huge_page_for_hwpoison
spin_lock_irq(&hugetlb_lock) <- held
__get_huge_page_for_hwpoison
hugetlb_update_hwpoison()
-> MF_HUGETLB_FOLIO_PRE_POISONED
goto out:
folio_put()
refcount: 1 -> 0
free_huge_folio()
spin_lock_irqsave(&hugetlb_lock)
-> AA DEADLOCK!The out: path in __get_huge_page_for_hwpoison() calls folio_put() to drop the GUP reference while the hugetlb_lock is still held by the hugetlb.c wrapper get_huge_page_for_hwpoison(). If concurrent unmap has released the page table mapping reference, folio_put() drops the folio refcount to zero, triggering free_huge_folio() which attempts to re-acquire the non-recursive hugetlb_lock.
Fix this by moving hugetlb_lock acquisition from the hugetlb.c wrapper into get_huge_page_for_hwpoison(). Place spin_unlock_irq() before the folio_put() at the out: label so the folio is always released outside the lock.
--- include/linux/hugetlb.h | 6 ------ include/linux/mm.h | 5 ----- mm/hugetlb.c | 10 ---------- mm/memory-failure.c | 19 ++++++++++--------- 4 files changed, 10 insertions(+), 30 deletions(-)
include/linux/hugetlb.h +0 -6
@@ -184,7 +184,6 @@ long hugetlb_unreserve_pages(struct inode *inode, long start, long end, long freed);int folio_isolate_hugetlb (struct page *page,struct list_head *list);int get_hwpoison_huge_page (struct page *page,bool *hugetlb); -int get_huge_page_for_hwpoison (unsigned long pfn,int flags);void folio_putback_hugetlb (struct page *page);void move_hugetlb_state (struct page *oldpage,struct page *newpage,int reason);void free_huge_page (struct page *page);
@@ -438,11 +437,6 @@ static inline int get_hwpoison_huge_page(struct page *page, bool *hugetlb)return 0 ; } -static inline int get_huge_page_for_hwpoison (unsigned long pfn,int flags) -{ -return 0 ; -} -static inline void folio_putback_hugetlb (struct page *page) { }
include/linux/mm.h +0 -5
@@ -3429,15 +3429,10 @@ extern atomic_long_t num_poisoned_pages __read_mostly;extern int soft_offline_page (unsigned long pfn,int flags);#ifdef CONFIG_MEMORY_FAILURE extern void memory_failure_queue (unsigned long pfn,int flags); -extern int __get_huge_page_for_hwpoison (unsigned long pfn,int flags);#else static inline void memory_failure_queue (unsigned long pfn,int flags) { } -static inline int __get_huge_page_for_hwpoison (unsigned long pfn,int flags) -{ -return 0 ; -}#endif #ifndef arch_memory_failure
mm/hugetlb.c +0 -10
@@ -7481,16 +7481,6 @@ int get_hwpoison_huge_page(struct page *page, bool *hugetlb)return ret; } -int get_huge_page_for_hwpoison (unsigned long pfn,int flags) -{ -int ret; - -spin_lock_irq (&hugetlb_lock); - ret =__get_huge_page_for_hwpoison (pfn, flags); -spin_unlock_irq (&hugetlb_lock); -return ret; -} -/** * folio_putback_hugetlb - unisolate a hugetlb page * @page: the isolated hugetlb page
mm/memory-failure.c +10 -9
@@ -1812,19 +1812,18 @@ void hugetlb_clear_page_hwpoison(struct page *hpage)free_raw_hwp_pages (hpage, true); } -/* -* Called from hugetlb code with hugetlb_lock held. -*/ -int __get_huge_page_for_hwpoison (unsigned long pfn,int flags) +static int get_huge_page_for_hwpoison (unsigned long pfn,int flags) {struct page *page =pfn_to_page (pfn); -struct page *head =compound_head (page); +struct page *head;bool count_increased = false;int ret, rc; +spin_lock_irq (&hugetlb_lock); + head =compound_head (page);if (!PageHeadHuge (head)) { ret =MF_HUGETLB_NON_HUGEPAGE ; - goto out; + goto out_unlock; }else if (flags &MF_COUNT_INCREASED ) { ret =MF_HUGETLB_IN_USED ; count_increased = true;
@@ -1840,17 +1839,19 @@ int __get_huge_page_for_hwpoison(unsigned long pfn, int flags) }else { ret =MF_HUGETLB_RETRY ;if (!(flags &MF_NO_RETRY )) - goto out; + goto out_unlock; } rc =hugetlb_update_hwpoison (head, page);if (rc >=MF_HUGETLB_FOLIO_PRE_POISONED ) { ret = rc; - goto out; + goto out_unlock; } +spin_unlock_irq (&hugetlb_lock);return ret; -out: +out_unlock: +spin_unlock_irq (&hugetlb_lock);if (count_increased)put_page (head);return ret;
Signature · 1 line
2.53.0