From: Matthew Wilcox <willy@infradead.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: Gregory Price <gourry@gourry.net>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com, akpm@linux-foundation.org,
liam@infradead.org, david@kernel.org, vbabka@kernel.org,
jannh@google.com, sashiko-bot <sashiko-bot@kernel.org>
Subject: Re: [PATCH] mm/madvise: use folio_trylock() in the cold/pageout PMD split
Date: Fri, 21 Aug 2026 20:48:12 +0100 [thread overview]
Message-ID: <aoirfBmxkQA-HwJ_@casper.infradead.org> (raw)
In-Reply-To: <aoiWZjgcitp0nSk0@gremlin>
On Fri, Aug 21, 2026 at 07:23:34PM +0100, Lorenzo Stoakes (ARM) wrote:
> On Fri, Aug 21, 2026 at 11:09:12AM -0400, Gregory Price wrote:
> > MADV_COLD or MADV_PAGEOUT over part of a PMD splits the THP in
> > madvise_cold_or_pageout_pte_range(). Two threads doing that to
> > the same THP create spurious failures.
> >
> > CPU0 CPU1
> > ---- ----
> > folio_get()
> > spin_unlock(ptl)
> > folio_lock()
> > folio_get()
> > spin_unlock(ptl)
> > folio_lock() <- blocks, keeps its ref
> > split_folio()
> > folio_expected_ref_count(folio) != folio_ref_count(folio) - 1
> > -EAGAIN
>
> Hmm, but doesn't converting to a folio_trylock() introduce entirely new spurious
> failures due to folio lock contention?
For the task running on CPU 1, yes. But the folio does get split rather
than probably both failing.
> > +++ b/mm/madvise.c
> > @@ -405,9 +405,10 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
> > if (next - addr != HPAGE_PMD_SIZE) {
> > int err;
> >
> > + if (!folio_trylock(folio))
> > + goto huge_unlock;
>
> Doesn't this violate lock ordering?
>
> >From rmap.c:
>
> folio_lock
> ...
> mm->page_table_lock or pte_lock
>
> So now you hold the ptl lock _before_ you obtain the folio lock?
>
> I'm not sure if it being a trylock gets us out of that particular situation? And
> I'd be reticent for us to violate it... unless I'm missing something :)
It's a common way of getting out of a lock ordering problem. Surprised
you've not encountered it as a solution to the Dining Philosophers problem.
We have even weirder solutions to "I want to sleep on the folio lock
but not with a reference held", and such might be appropriate here if
we want to prevent the spurious failure on CPU 1. See the DROP behavior
in mm/filemap.c. See folio_put_wait_locked() in mm/filemap.c, not that
it's exported.
We couldn't quite make that work here since the whole point is to _never_
get the refcount on the folio if somebody else has the lock, and once
we've dropped the PTL, the folio might have been split and thus not be
the folio we want any more (indeed it may have been freed, reallocated
and now be a pointer to a tail page instead of a folio).
next prev parent reply other threads:[~2026-08-21 19:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-21 15:09 [PATCH] mm/madvise: use folio_trylock() in the cold/pageout PMD split Gregory Price
2026-08-21 18:23 ` Lorenzo Stoakes (ARM)
2026-08-21 18:42 ` Gregory Price
2026-08-21 19:18 ` Lorenzo Stoakes (ARM)
2026-08-21 19:48 ` Matthew Wilcox [this message]
2026-08-21 20:38 ` Gregory Price
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=aoirfBmxkQA-HwJ_@casper.infradead.org \
--to=willy@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=jannh@google.com \
--cc=kernel-team@meta.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=sashiko-bot@kernel.org \
--cc=vbabka@kernel.org \
/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