From: Matthew Wilcox <willy@infradead.org>
To: Andrew Morton <akpm@linux-foundation.org>,
Jane Chu <jane.chu@oracle.com>,
linux-mm@kvack.org
Cc: Muchun Song <muchun.song@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
David Hildenbrand <david@kernel.org>,
Miaohe Lin <linmiaohe@huawei.com>,
Naoya Horiguchi <nao.horiguchi@gmail.com>,
Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org,
Christian Brauner <christian@brauner.io>,
Jiaqi Yan <jiaqiyan@google.com>
Subject: Re: [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag
Date: Fri, 24 Jul 2026 14:21:02 +0100 [thread overview]
Message-ID: <amNmvtOLZxEwF-cg@casper.infradead.org> (raw)
In-Reply-To: <20260723143034.175661-8-willy@infradead.org>
Sashiko review:
> --- Patch [7]: [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag ---
> commit f67cbd0b3ba7a67258490d423b9fc8352cb972aa
> Author: Matthew Wilcox (Oracle) <willy@infradead.org>
>
> hugetlb: Use the has_hwpoisoned flag
>
> Other large folios use the has_hwpoisoned flag. Convert hugetlb to match.
> This will help us use the per-page hwpoison flag in the future.
>
> > diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> > index 18691c905ac94..4200deab2c3c4 100644
> > --- a/mm/memory-failure.c
> > +++ b/mm/memory-failure.c
>
> [ ... ]
>
> > @@ -1874,6 +1874,23 @@ static unsigned long __folio_free_raw_hwp(struct folio *folio, bool move_flag)
> > #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 */
> > +
> > +static inline int hugetlb_set_poison(struct folio *folio)
> > +{
> > + if (folio_test_set_has_hwpoisoned(folio))
> > + return MF_HUGETLB_FOLIO_PRE_POISONED;
> > + folio_set_huge_poison(folio);
> > + return 0;
> > +}
>
> By replacing folio_test_set_hwpoison(folio) and no longer propagating
> PG_hwpoison to the head page, does this break memory failure unmapping and
> silently bypass hwpoison swap entry creation?
>
> If a hardware memory error occurs on a tail page of a hugetlb folio,
> memory_failure() calls TestSetPageHWPoison(p) on the exact tail page. It then
> calls hugetlb_set_poison(), which sets PG_has_hwpoisoned but no longer sets
> PG_hwpoison on the head page.
>
> When unmapping the folio in try_to_unmap_one() in mm/rmap.c, the code
> extracts the base PFN from the hugetlb PTE, calculates subpage as the head
> page, and checks PageHWPoison(subpage):
>
> mm/rmap.c:try_to_unmap_one() {
> ...
> VM_BUG_ON_PAGE(!PageHWPoison(subpage), subpage);
> ...
> if (PageHWPoison(subpage) && (flags & TTU_HWPOISON)) {
> pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
> ...
> }
>
> Since PG_hwpoison is no longer on the head page, this check fails, triggering
> VM_BUG_ON_PAGE. Without DEBUG_VM, the code skips inserting the hwpoison swap
> entry, simply clearing the PTE, which silently allocates a new zeroed page on
> subsequent accesses instead of delivering SIGBUS.
Oh; this is a good catch. I have now reworked try_to_unmap() to handle
hugetlb pages a bit better and it'll be part of this patch in v5.
> > +
> > +static inline int hugetlb_clear_poison(struct folio *folio)
> > +{
> > + if (!folio_test_clear_has_hwpoisoned(folio))
> > + return -EBUSY;
> > + folio_clear_huge_poison(folio);
> > + return 0;
> > +}
>
> Does this sequence open a race window with concurrent hugetlb freeing that
> results in memory corruption?
>
> When unpoison_memory() is called on a free hugetlb folio,
> hugetlb_clear_poison() atomically clears PG_has_hwpoisoned first. This
> exposes the folio to concurrent hugetlb demotion (demote_pool_huge_page()),
> which can pick up the folio and call __update_and_free_hugetlb_folio(). This
> function sets folio->page.page_type = UINT_MAX to return it to the buddy
> allocator.
>
> Concurrently, hugetlb_clear_poison() proceeds to execute
> folio_clear_huge_poison() (defined in include/linux/page-flags.h), which
> performs a non-atomic read-modify-write:
>
> include/linux/page-flags.h:folio_clear_huge_poison() {
> folio->page.page_type &= ~(1 << 23);
> }
>
> This non-atomic write can overwrite the buddy allocator's UINT_MAX with
> 0xFF7FFFFF. When the buddy allocator receives the page, it expects
> page_type == UINT_MAX and will crash.
I'm not sure that it can be demoted while we're holding a reference to
the folio. But I think it makes sense to change thusly:
+static inline int hugetlb_clear_poison(struct folio *folio)
+{
+ if (!folio_test_has_hwpoisoned(folio))
+ return -EBUSY;
+ folio_clear_huge_poison(folio);
+ folio_clear_has_hwpoisoned(folio));
+ return 0;
+}
(looking at Jiaqi's patches, maybe we shouldn't be clearing has_hwpoisoned
before freeing the folio?)
next prev parent reply other threads:[~2026-07-24 13:21 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 14:30 [PATCH v4 00/14] Use generic_file_read_iter() in hugetlbfs Matthew Wilcox (Oracle)
2026-07-23 14:30 ` [PATCH v4 01/14] memory-failure: Fix hardware poison check in unpoison_memory() again Matthew Wilcox (Oracle)
2026-07-24 12:32 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 02/14] memory-failure: Test the page is hwpoison before taking the mutex Matthew Wilcox (Oracle)
2026-07-24 12:34 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 03/14] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page() Matthew Wilcox (Oracle)
2026-07-24 12:36 ` Matthew Wilcox
2026-07-24 12:52 ` Michael S. Tsirkin
2026-07-24 13:24 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 04/14] hugetlb: Mark some function arguments as const Matthew Wilcox (Oracle)
2026-07-24 12:38 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 05/14] guest_memfd: Use folio_has_hwpoisoned_page() Matthew Wilcox (Oracle)
2026-07-24 12:44 ` Matthew Wilcox
2026-07-24 15:16 ` Sean Christopherson
2026-07-24 16:51 ` Ackerley Tng
2026-07-24 18:04 ` Matthew Wilcox
2026-07-24 15:16 ` Sean Christopherson
2026-07-24 16:40 ` Ackerley Tng
2026-07-23 14:30 ` [PATCH v4 06/14] memory-failure: Remove raw_hwp_list_head() Matthew Wilcox (Oracle)
2026-07-24 12:45 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 07/14] hugetlb: Use the has_hwpoisoned flag Matthew Wilcox (Oracle)
2026-07-24 13:21 ` Matthew Wilcox [this message]
2026-07-23 14:30 ` [PATCH v4 08/14] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage() Matthew Wilcox (Oracle)
2026-07-24 13:41 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 09/14] mm: Check individual hugetlb pages for poison Matthew Wilcox (Oracle)
2026-07-24 14:00 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 10/14] filemap: Add hwpoison handling to filemap_read() Matthew Wilcox (Oracle)
2026-07-24 16:06 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 11/14] filemap: Remove checks in mapping_set_folio_order_range() Matthew Wilcox (Oracle)
2026-07-24 16:08 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 12/14] hugetlb: Set mapping folio order Matthew Wilcox (Oracle)
2026-07-24 16:16 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 13/14] filemap: Add support for authoritative mappings Matthew Wilcox (Oracle)
2026-07-24 16:15 ` Matthew Wilcox
2026-07-24 16:29 ` Matthew Wilcox
2026-07-23 14:30 ` [PATCH v4 14/14] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter() Matthew Wilcox (Oracle)
2026-07-24 16:36 ` Matthew Wilcox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=amNmvtOLZxEwF-cg@casper.infradead.org \
--to=willy@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=christian@brauner.io \
--cc=david@kernel.org \
--cc=jack@suse.cz \
--cc=jane.chu@oracle.com \
--cc=jiaqiyan@google.com \
--cc=linmiaohe@huawei.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=nao.horiguchi@gmail.com \
--cc=osalvador@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox