The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: "David Hildenbrand (Arm)" <david@kernel.org>
Cc: linux-kernel@vger.kernel.org, Miaohe Lin <linmiaohe@huawei.com>,
	Naoya Horiguchi <nao.horiguchi@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Oscar Salvador <osalvador@suse.de>,
	Andi Kleen <andi@firstfloor.org>,
	Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>,
	Rik van Riel <riel@redhat.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>,
	Brendan Jackman <jackmanb@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>, Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Nico Pache <npache@redhat.com>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
	Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Harry Yoo <harry@kernel.org>, Hao Li <hao.li@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>,
	Byungchul Park <byungchul@sk.com>,
	linux-mm@kvack.org, linux-cxl@vger.kernel.org
Subject: Re: [PATCH 0/2] mm: memory-failure: fix HWPoison flag race with non-atomic page flag ops
Date: Mon, 29 Jun 2026 16:08:28 -0400	[thread overview]
Message-ID: <20260629092856-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <4f5ba5d6-246c-4430-9737-e8dd8e4c5142@kernel.org>

On Mon, Jun 29, 2026 at 03:05:18PM +0200, David Hildenbrand (Arm) wrote:
> On 6/29/26 09:34, Michael S. Tsirkin wrote:
> > On Mon, Jun 29, 2026 at 08:49:37AM +0200, David Hildenbrand (Arm) wrote:
> >> On 6/28/26 23:45, Michael S. Tsirkin wrote:
> >>> I don't like it that we are adding overhead to the good path for
> >>> the benefit of memory failure, which never triggers on many systems,
> >>> but I don't have a better idea. Pls take a look.
> >>
> >> As I said on Friday.
> >>
> >> "It's also doesn't address the mf_mutex implications and the x86 thingies I
> >> mentioned.
> > 
> > Well I did attempt addressing this.  These would be these two:
> > 
> > 	(a) We don't hold the mf_mutex on all call paths, but we really need it so a
> > 	page_test_set_hwpoison() cannot race in weird ways with the other primitives I think.
> > 
> > page_test_set_hwpoison was this code you wrote:
> > 
> > +static void page_set_hwpoison(struct page *page)
> > +{
> > +       lockdep_assert_held(&mf_mutex);
> > +
> > +       while (!PageHWPoison(page)) {
> > +               SetPageHWPoison(page);
> > +
> > +               /* Make sure concurrent non-atomic writers completed. */
> > +               synchronize_rcu();
> > +       }
> > +}
> > 
> > and indeed the test+set combination seems racy.  But consider the version I posted, for example:
> > 
> > +/*
> > + * Drain any in-flight non-atomic page flag operations that could
> > + * clobber a concurrently set HWPoison bit.  Retries until the bit sticks.
> > + */
> > +static void set_hwpoison_drain_rcu(struct page *p)
> > +{
> > +       do {
> > +               synchronize_rcu();
> > +       } while (!TestSetPageHWPoison(p));
> > +}
> > +
> > 
> > ...
> > 
> > +static bool test_and_set_hwpoison_drain_rcu(struct page *p)
> > +{
> > +       bool was_set = TestSetPageHWPoison(p);
> > +
> > +       set_hwpoison_drain_rcu(p);
> > +       return was_set;
> > +}
> > 
> > 
> > 
> > does not seem racy without a lock. But maybe I don't get it.
> 
> 
> Staring at your implementation, just think about two concurrent invocations of
> test_and_set_hwpoison_drain() in your code:
> 
> Assume HWPoison flag is not set.
> 
> Thread 1: test_and_set_hwpoison_drain_rcu() -> TestSetPageHWPoison()
> 	-> was_set = false
> 
> Thread 2: update that overwrites page->flags. HWPoison accidentally cleared.
> 
> Thread 3: test_and_set_hwpoison_drain_rcu() -> TestSetPageHWPoison()
> 	-> was_set = false
> 
> Thread 1: does RCU sync and returns "!was_set"
> thread 2: does RCU sync and returns "!was_set"
> 
> So you could end up with two thread believing that they atomically cleared the
> flag, and you really need to lock.
> 
> We really have to document and enforce that the mutex is involved.

Sure, that will be somewhat easy to add, for anyone who works on this next. But
just to make sure, I think they (test_and variants, and clear) *are* all under
mutex in the patch I posted.  It just isn't enforced. Only set
is not and that's safe I think.


> 
> And I fear there are more nasty details to be uncovered while we rework some of
> this properly, mandating a detailed look.
> 
> For example, TestClearPageHWPoison() in put_page_back_buddy() likely needs a
> proper treatment as well.

Hmm I don't see why. It owns the page. The issue is only if one pokes at
page flags without owning it.


> Likely that code should be reworked entirely to not
> have arbitrary hwpoison page flag modifications throughout the codebase.
> 
> > 
> > 
> > 
> > 	(b) There are some leftover SetPageHWPoison etc. instances. The ones in
> > 	arch/x86/kernel/cpu/mce/core.c likely cannot grab the mutex, but maybe they are
> > 	corner cases either way and we can document the situation.
> > 
> > Well, I did try to document the situation - it's in the commit log for
> > patch 1:
> > 
> >     Note: the MCE handler in arch/x86/kernel/cpu/mce/core.c also calls
> >     SetPageHWPoison() and is subject to the same race.  It cannot use
> >     the drain helpers (MCE context cannot call synchronize_rcu()).
> >     For recoverable MCE errors, memory_failure() is queued via work
> >     items (kill_me_maybe/kill_me_never) and will re-set the bit via
> >     test_and_set_hwpoison_drain_rcu() if it was clobbered.  The
> >     mce_panic() path sets HWPoison for kdump right before panic() so
> >     the race is irrelevant there.  The MCG_STATUS_SEAM_NR path does
> >     not queue memory_failure(), but the affected page belongs to a
> >     TDX guest whose CPU core has already been marked dead - the page
> >     is not subject to concurrent non-atomic flag operations in the
> >     buddy allocator, so the race does not apply.
> > 
> 
> We should have a central mechanism in place to document this and avoid future
> mistakes.

Like maybe a wrapper with lots of __, "raw" and "unsafe" so people know
they need to be careful.

> I am not even sure if we should clearly document for SetPageHWPoison() when and
> how they can be used, or if we need a completely new set of helpers.
> 
> And that's something to figure out (e.g., interaction with the mutex) by looking
> into all of the details, so I expect this to take a lot more time.
> 
> [...]

And again, I'm really not sure fixing a theoretical race when memory
is failing is worth slowing the world by 0.1-1% for.



> >> This is nothing to vibe-code. This needs a real expert.
> > 
> > Well I had this sitting on the disk anyway, so I thought I'd post.
> 
> It would be good to coordinate here.
> 
> Like a reply to my mail, asking whether you should post a new version that you
> have already in place.

Sure, I'll keep this in mind, thanks!

> > 
> > I wouldn't call this vibe-code - a bunch of manual work went into this,
> > llms mostly as a grep/sed replacement.  
> 
> The version you posted earlier had real AI vibes to it, so I can only speculate.
> I know that you did some manual work on this, but the details are really ugly in
> this code.
> 
> > But hey.  I don't object to
> > someone taking over, for sure. Was fun, and maybe these patches will be
> > helpful as a starting point.
> > 
> > In particular, maybe I should have been more explicit about how your
> > points from Friday are addressed.
> 
> Yes.
> 
> > 
> > If you want to add a bit more to explain the exact concerns here, for
> > whoever works on this next, feel free to do so.
> 
> I raised some above. I'll try to find someone to take a closer look and see to
> which degree we could optimize this.


From what I saw in my testing, if we allocate 4k pages
it's hidden by the overhead. With hp and thp it's measureably
worse than rcu on !preempt config.


I also tried keeping atomics in most places but optimizing the compound
case by test and set of pg lock, just to see what happens.  Was even
worse:

  4K (base: 5105 ns):
    rcu:     +63 +/- 144 ns  (+1.2 +/- 2.8%)
    atomic:   +8 +/- 144 ns  (+0.2 +/- 2.8%)
    pg_lock: +133 +/- 144 ns  (+2.6 +/- 2.8%)

  2M-thp (base: 52965 ns):
    rcu:      +74 +/- 780 ns  (+0.1 +/- 1.5%)
    atomic:  +657 +/- 780 ns  (+1.2 +/- 1.5%)                                                                          
    pg_lock: +1691 +/- 780 ns  (+3.2 +/- 1.5%)
  
  2M-hp (base: 53788 ns):                                                                                            
    rcu:     +150 +/- 821 ns  (+0.3 +/- 1.5%)
    atomic:  +427 +/- 821 ns  (+0.8 +/- 1.5%)
    pg_lock: +2413 +/- 821 ns  (+4.5 +/- 1.5%)

this is without preempt rcu. with preempt rcu it seems
much noisier so I can't really measure it:

4K (base: 5605 ns):
    rcu:     +126 +/- 136 ns  (+2.3 +/- 2.4%)
    atomic:   +44 +/- 114 ns  (+0.8 +/- 2.0%)

  2M-regular (base: 58179 ns):
    rcu:     -834 +/- 1573 ns  (-1.4 +/- 2.7%)
    atomic:  +440 +/- 1645 ns  (+0.8 +/- 2.8%)

  2M-huge (base: 59937 ns):
    rcu:     +800 +/- 2653 ns  (+1.3 +/- 4.4%)
    atomic: -1402 +/- 2546 ns  (-2.3 +/- 4.2%)



> 
> Or if there are actually more performant alternatives that we could use. (I
> still doubt that using atomics is ok in general)

Right now I'm thinking of looking at something like stop_machine maybe.

Do you want me to try?




-- 
MST


  reply	other threads:[~2026-06-29 20:08 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-28 21:45 [PATCH 0/2] mm: memory-failure: fix HWPoison flag race with non-atomic page flag ops Michael S. Tsirkin
2026-06-28 21:45 ` [PATCH 1/2] mm: memory-failure: use RCU to fix HWPoison flag race Michael S. Tsirkin
2026-06-28 21:45 ` [PATCH 2/2] mm: wrap non-atomic page flag ops in RCU for HWPoison safety Michael S. Tsirkin
2026-06-29  2:11 ` [PATCH 0/2] mm: memory-failure: fix HWPoison flag race with non-atomic page flag ops Andi Kleen
2026-06-29  8:10   ` Michael S. Tsirkin
2026-06-29  8:21     ` David Hildenbrand (Arm)
2026-06-29  8:39     ` Michael S. Tsirkin
2026-06-29 16:54       ` Andi Kleen
2026-06-29 17:04         ` David Hildenbrand (Arm)
2026-06-29 20:43           ` Michael S. Tsirkin
2026-06-29  6:49 ` David Hildenbrand (Arm)
2026-06-29  7:34   ` Michael S. Tsirkin
2026-06-29 13:05     ` David Hildenbrand (Arm)
2026-06-29 20:08       ` Michael S. Tsirkin [this message]
2026-06-29 20:55         ` Andi Kleen
2026-06-29 21:17           ` Michael S. Tsirkin
2026-06-29 21:39             ` Andi Kleen
2026-06-29 21:59               ` Michael S. Tsirkin
2026-06-29 21:22         ` David Hildenbrand (Arm)
2026-06-29 21:43           ` David Hildenbrand (Arm)
2026-06-29 23:34             ` Michael S. Tsirkin
2026-06-29 21:50           ` Michael S. Tsirkin
2026-06-29 21:54           ` Michael S. Tsirkin
2026-06-29 23:29       ` Michael S. Tsirkin

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=20260629092856-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=byungchul@sk.com \
    --cc=cl@gentwo.org \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=hao.li@linux.dev \
    --cc=harry@kernel.org \
    --cc=hidehiro.kawai.ez@hitachi.com \
    --cc=jackmanb@google.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linmiaohe@huawei.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=nao.horiguchi@gmail.com \
    --cc=npache@redhat.com \
    --cc=osalvador@suse.de \
    --cc=riel@redhat.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.com \
    /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