Re: [PATCH] mm/madvise: reclaim isolated folios if PTE restart fails

From: Lorenzo Stoakes (ARM)

Date: Wed Sep 16 2026 - 10:23:55 EST


On Wed, Sep 16, 2026 at 09:57:44AM -0400, Gregory Price wrote:
> On Wed, Sep 16, 2026 at 02:03:29PM +0100, Lorenzo Stoakes (ARM) wrote:
> > On Tue, Sep 15, 2026 at 01:12:03PM -0400, Gregory Price wrote:
> > > On Tue, Sep 15, 2026 at 04:55:47PM +0100, Lorenzo Stoakes (ARM) wrote:
> > > > On Sat, Sep 12, 2026 at 07:08:32AM -0400, Gregory Price wrote:
> > > > > MADV_PAGEOUT collects isolated folios on a local list before reclaiming
> > > > > them after the PTE walk. The reschedule path drops the PTE lock and then
> > > > > restarts the mapping with pte_offset_map_lock().
> > > >
> > > > Is this reschedule path even necessary? It's pretty bloody sketchy.
> > > >
> > > > I thought the modern approach (TM) was to not do cond_resched() and friends so
> > > > can we actually look at removing this?
> > > >
> > >
> > > Sorry - meant to reply to this.
> > >
> > > Honestly there's so much jammed into this function i can't give you a
> > > straight answer. I can take a short detour to figure out if we can
> > > rework this as a whole.
> >
> > s/short detour/long journey through hell/ perhaps? :P ;)
> >
>
> Not as much as you'd think.
>
> I did it yesterday and left an agent to validate it overnight with
> existing in-tree tests, LTP tests, and added some new tests.
>
> The naming could use some work (cold_or_pageout -> lru_op, bleh),
> but new call graph / pseudo-code:
>
> • walk_page_range_vma()
> └── madvise_lru_op_pmd() [PMD callback]
>
> ├── PMD is a huge mapping
> │ └── madvise_lru_op_huge_pmd()
> │ ├── acquire PMD lock
> │ ├── madvise_lru_op_huge_pmd_locked()
> │ │ ├── process whole folio, or
> │ │ └── return locked+referenced split candidate
> │ ├── release PMD lock
> │ └── split folio / reclaim isolated folios
>
> └── PMD points to a PTE table
> └── madvise_lru_op_ptes()
> ├── map PTE table and acquire PTL
> ├── madvise_lru_op_pte_range_locked()
> │ └── madvise_lru_op_pte_batch_locked()
> │ ├── process one folio/PTE batch, or
> │ └── return locked+referenced split candidate
> ├── leave lazy-MMU mode
> ├── unmap PTE table and release PTL
> ├── split candidate if necessary
> ├── reclaim isolated folios
> └── reschedule and restart from current address if necessary
>
> Looks like a normal table walk now. Significantly easier to review and
> doesn't make my eyes vomit. (there's still an ifdef wart, but it's not
> mid-function anymore... so that's nice).

OK nice, I will have to see the code to comment intelligently though I
think :)

>
> Going to spend a little more time testing and tweaking before I post it.
>
> I've been working on a making more extensive unit tests for certain
> parts of mm/ and this might be a good time to look at whether I can
> introduce a piece of it.

Nice, curious as to how?

Have you tried the userland VMA tests btw? Not tooting my own horn so to
speak :P but it's nice, though it comes at a bit of a cost in how the files
have to be set up...

I always feel like I should do more with it but don't have the time
atm. May set the LLM on it though...

>
> ~Gregory

--
Cheers, Lorenzo