* [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio()
@ 2024-07-08 2:51 Miaohe Lin
2024-07-08 7:08 ` Muchun Song
2024-07-08 8:24 ` Oscar Salvador
0 siblings, 2 replies; 3+ messages in thread
From: Miaohe Lin @ 2024-07-08 2:51 UTC (permalink / raw)
To: akpm, muchun.song; +Cc: linmiaohe, linux-mm, linux-kernel
There is a potential race between __update_and_free_hugetlb_folio() and
try_memory_failure_hugetlb():
CPU1 CPU2
__update_and_free_hugetlb_folio try_memory_failure_hugetlb
folio_test_hugetlb
-- It's still hugetlb folio.
folio_clear_hugetlb_hwpoison
spin_lock_irq(&hugetlb_lock);
__get_huge_page_for_hwpoison
folio_set_hugetlb_hwpoison
spin_unlock_irq(&hugetlb_lock);
spin_lock_irq(&hugetlb_lock);
__folio_clear_hugetlb(folio);
-- Hugetlb flag is cleared but too late.
spin_unlock_irq(&hugetlb_lock);
When above race occurs, raw error page info will be leaked. Even worse,
raw error pages won't have hwpoisoned flag set and hit pcplists/buddy.
Fix this issue by deferring folio_clear_hugetlb_hwpoison() until
__folio_clear_hugetlb() is done. So all raw error pages will have
hwpoisoned flag set.
Fixes: 32c877191e02 ("hugetlb: do not clear hugetlb dtor until allocating vmemmap")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: <stable@vger.kernel.org>
---
mm/hugetlb.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 61847f799b6a..63a2f1132a81 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1729,13 +1729,6 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
return;
}
- /*
- * Move PageHWPoison flag from head page to the raw error pages,
- * which makes any healthy subpages reusable.
- */
- if (unlikely(folio_test_hwpoison(folio)))
- folio_clear_hugetlb_hwpoison(folio);
-
/*
* If vmemmap pages were allocated above, then we need to clear the
* hugetlb flag under the hugetlb lock.
@@ -1746,6 +1739,13 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
spin_unlock_irq(&hugetlb_lock);
}
+ /*
+ * Move PageHWPoison flag from head page to the raw error pages,
+ * which makes any healthy subpages reusable.
+ */
+ if (unlikely(folio_test_hwpoison(folio)))
+ folio_clear_hugetlb_hwpoison(folio);
+
folio_ref_unfreeze(folio, 1);
/*
--
2.33.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio()
2024-07-08 2:51 [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio() Miaohe Lin
@ 2024-07-08 7:08 ` Muchun Song
2024-07-08 8:24 ` Oscar Salvador
1 sibling, 0 replies; 3+ messages in thread
From: Muchun Song @ 2024-07-08 7:08 UTC (permalink / raw)
To: Miaohe Lin; +Cc: Andrew Morton, Linux-MM, linux-kernel
> On Jul 8, 2024, at 10:51, Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> There is a potential race between __update_and_free_hugetlb_folio() and
> try_memory_failure_hugetlb():
>
> CPU1 CPU2
> __update_and_free_hugetlb_folio try_memory_failure_hugetlb
> folio_test_hugetlb
> -- It's still hugetlb folio.
> folio_clear_hugetlb_hwpoison
> spin_lock_irq(&hugetlb_lock);
> __get_huge_page_for_hwpoison
> folio_set_hugetlb_hwpoison
> spin_unlock_irq(&hugetlb_lock);
> spin_lock_irq(&hugetlb_lock);
> __folio_clear_hugetlb(folio);
> -- Hugetlb flag is cleared but too late.
> spin_unlock_irq(&hugetlb_lock);
>
> When above race occurs, raw error page info will be leaked. Even worse,
> raw error pages won't have hwpoisoned flag set and hit pcplists/buddy.
> Fix this issue by deferring folio_clear_hugetlb_hwpoison() until
> __folio_clear_hugetlb() is done. So all raw error pages will have
> hwpoisoned flag set.
>
> Fixes: 32c877191e02 ("hugetlb: do not clear hugetlb dtor until allocating vmemmap")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Acked-by: Muchun Song <muchun.song@linux.dev>
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio()
2024-07-08 2:51 [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio() Miaohe Lin
2024-07-08 7:08 ` Muchun Song
@ 2024-07-08 8:24 ` Oscar Salvador
1 sibling, 0 replies; 3+ messages in thread
From: Oscar Salvador @ 2024-07-08 8:24 UTC (permalink / raw)
To: Miaohe Lin; +Cc: akpm, muchun.song, linux-mm, linux-kernel
On Mon, Jul 08, 2024 at 10:51:27AM +0800, Miaohe Lin wrote:
> There is a potential race between __update_and_free_hugetlb_folio() and
> try_memory_failure_hugetlb():
>
> CPU1 CPU2
> __update_and_free_hugetlb_folio try_memory_failure_hugetlb
> folio_test_hugetlb
> -- It's still hugetlb folio.
> folio_clear_hugetlb_hwpoison
> spin_lock_irq(&hugetlb_lock);
> __get_huge_page_for_hwpoison
> folio_set_hugetlb_hwpoison
> spin_unlock_irq(&hugetlb_lock);
> spin_lock_irq(&hugetlb_lock);
> __folio_clear_hugetlb(folio);
> -- Hugetlb flag is cleared but too late.
> spin_unlock_irq(&hugetlb_lock);
>
> When above race occurs, raw error page info will be leaked. Even worse,
> raw error pages won't have hwpoisoned flag set and hit pcplists/buddy.
> Fix this issue by deferring folio_clear_hugetlb_hwpoison() until
> __folio_clear_hugetlb() is done. So all raw error pages will have
> hwpoisoned flag set.
>
> Fixes: 32c877191e02 ("hugetlb: do not clear hugetlb dtor until allocating vmemmap")
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> Cc: <stable@vger.kernel.org>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
--
Oscar Salvador
SUSE Labs
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-07-08 8:24 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-08 2:51 [PATCH] mm/hugetlb: fix potential race in __update_and_free_hugetlb_folio() Miaohe Lin
2024-07-08 7:08 ` Muchun Song
2024-07-08 8:24 ` Oscar Salvador
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox