All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: Gregory Price <gourry@gourry.net>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Jane Chu <jane.chu@oracle.com>,
	linux-mm@kvack.org, 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 v7 06/13] hugetlb: Use the has_hwpoisoned flag
Date: Wed, 29 Jul 2026 03:55:10 +0100	[thread overview]
Message-ID: <amlrjtaFSEQLYbEO@casper.infradead.org> (raw)
In-Reply-To: <amlQYkGgLmxmwX9_@gourry-fedora-PF4VCD3F>

On Tue, Jul 28, 2026 at 09:36:37PM -0400, Gregory Price wrote:
> On Tue, Jul 28, 2026 at 09:43:58PM +0100, Matthew Wilcox (Oracle) wrote:
> > +
> > +#ifdef CONFIG_MEMORY_FAILURE
> > +static inline bool folio_test_huge_poison(const struct folio *folio)
> > +{
> > +	return (READ_ONCE(folio->page.page_type) >> 23) ==
> > +		((PGTY_hugetlb << 1) | 1);
> > +}
> > +
> > +static inline void folio_set_huge_poison(struct folio *folio)
> > +{
> > +	folio->page.page_type |= (1 << 23);
> > +}
> > +
> > +static inline void folio_clear_huge_poison(struct folio *folio)
> > +{
> > +	folio->page.page_type &= ~(1 << 23);
> > +}
> > +#else
> 
> Took me a second to realize why this was open-coded instead of using
> the generator macros.  The reasoning is buried in this comment:

I believe hugetlb is the first user of the low 24 bits of page_type.
As such, there aren't any generator macros yet ... and I'd probably want
there to be at least three before creating any.

> /*
>  * Check if a page is currently marked HWPoisoned.  This check is best
>  * effort only and inherently racy: there is no way to synchronize with
>  * failing hardware.  The caller may not have a refcount on the folio
>  * containing the page, so we must be careful to not trip any assertions.
>  */
>  static inline bool is_page_hwpoison(const struct page *page)
>  ...
> 
> which i presume is referring to VM_BUG_ON_FOLIO() in the set/clear in
> the macros.  Might be worth just note here if only so no one comes along
> and "fixes" this or writes more of these without the same reasoning.

It's even in the test ...

FOLIO_FLAG(has_hwpoisoned, FOLIO_SECOND_PAGE)
#define FOLIO_SECOND_PAGE       1
#define FOLIO_FLAG(name, page)                                          \
FOLIO_TEST_FLAG(name, page)                                             \
#define FOLIO_TEST_FLAG(name, page)                                     \
static __always_inline bool folio_test_##name(const struct folio *folio) \
{ return test_bit(PG_##name, const_folio_flags(folio, page)); }

static const unsigned long *const_folio_flags(const struct folio *folio,
                unsigned n)
{
        const struct page *page = &folio->page;

        VM_BUG_ON_PGFLAGS(page->compound_info & 1, page);
        VM_BUG_ON_PGFLAGS(n > 0 && !test_bit(PG_head, &page->flags.f), page);
        return &page[n].flags.f;
}

So there's two asserts that might trip; the first if the folio is freed
then reallocated into a larger folio and the former folio becomes a tail
page.  That's pretty unlikely for a hugetlb folio, but the second is
more likely.  That'll happen just by freeing the folio.

> > @@ -2725,13 +2748,19 @@ int unpoison_memory(unsigned long pfn)
> >  
> >  	ghp = get_hwpoison_page(p, MF_UNPOISON);
> >  	if (!ghp) {
> > +		spin_lock_irq(&hugetlb_lock);
> >  		if (folio_test_hugetlb(folio)) {
> >  			huge = true;
> >  			count = folio_free_raw_hwp(folio, false);
> > -			if (count == 0)
> > +			if (count == 0) {
> > +				spin_unlock_irq(&hugetlb_lock);
> >  				goto unlock_mutex;
> > +			}
> > +			ret = hugetlb_clear_poison(folio);
> > +		} else {
> > +			ret = TestClearPageHWPoison(p) ? 0 : -EBUSY;
> >  		}
> > -		ret = folio_test_clear_hwpoison(folio) ? 0 : -EBUSY;
> > +		spin_unlock_irq(&hugetlb_lock);
> 
> Adding new synchronization here without noting why - it's not
> immediately clear what this synchronizes.
> 
> Is this just fixing an unrelated bug / should it be a separate patch?

This prevents a hugetlb folio (which in this arm we don't have a
reference on) from being freed.  I think it ended up in this patch
because Sashiko pointed out the race during review of this patch.
It could probably go in its own patch 

  reply	other threads:[~2026-07-29  2:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 20:43 [PATCH v7 00/13] Use generic_file_read_iter() in hugetlbfs Matthew Wilcox (Oracle)
2026-07-28 20:43 ` [PATCH v7 01/13] memory-failure: Fix hardware poison check in unpoison_memory() again Matthew Wilcox (Oracle)
2026-07-29  0:39   ` Gregory Price
2026-07-29  1:43     ` Matthew Wilcox
2026-07-28 20:43 ` [PATCH v7 02/13] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page() Matthew Wilcox (Oracle)
2026-07-28 20:43 ` [PATCH v7 03/13] hugetlb: Mark some function arguments as const Matthew Wilcox (Oracle)
2026-07-28 20:43 ` [PATCH v7 04/13] guest_memfd: Use folio_has_hwpoisoned_page() Matthew Wilcox (Oracle)
2026-07-28 20:43 ` [PATCH v7 05/13] hugetlb: Move poison to pages before clearing hugetlb page type Matthew Wilcox (Oracle)
2026-07-29  0:41   ` Gregory Price
2026-07-28 20:43 ` [PATCH v7 06/13] hugetlb: Use the has_hwpoisoned flag Matthew Wilcox (Oracle)
2026-07-29  1:36   ` Gregory Price
2026-07-29  2:55     ` Matthew Wilcox [this message]
2026-07-29 15:05       ` Matthew Wilcox
2026-07-28 20:43 ` [PATCH v7 07/13] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage() Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 08/13] mm: Check individual hugetlb pages for poison Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 09/13] filemap: Add hwpoison handling to filemap_read() Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 10/13] filemap: Remove checks in mapping_set_folio_order_range() Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 11/13] hugetlb: Set mapping folio order Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 12/13] filemap: Add support for authoritative mappings Matthew Wilcox (Oracle)
2026-07-28 20:44 ` [PATCH v7 13/13] hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter() Matthew Wilcox (Oracle)
2026-07-29 15:17 ` [PATCH v7 00/13] Use generic_file_read_iter() in hugetlbfs 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=amlrjtaFSEQLYbEO@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=christian@brauner.io \
    --cc=david@kernel.org \
    --cc=gourry@gourry.net \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.