Re: [PATCH] mm/mglru: check need_resched() in fill/drain_evictable()
From: Kairui Song
Date: Thu Nov 12 2026 - 04:26:12 EST
On Thu, Nov 12, 2026 at 4:04 PM Kairui Song <ryncsn@xxxxxxxxx> wrote:
>
> On Thu, Nov 12, 2026 at 03:58:27PM +0800, Hui Zhu wrote:
> > From: Hui Zhu <zhuhui@xxxxxxxxxx>
> >
> > lru_gen_change_state() migrates folios in batches of up to
> > MAX_LRU_BATCH (4096) folios under lruvec_lock_irq(). A full batch
> > keeps local interrupts off for up to about a millisecond, and CPUs
> > contending for the same lru_lock spin with interrupts disabled for
> > that long.
> >
> > The caller already reschedules between batches, but a batch itself
> > only ends after 4096 folios. Stop it early when a resched is
> > pending; the caller restarts the migration where it left off. A
> > remote resched_curr() sets TIF_NEED_RESCHED directly in the target's
> > thread_info, so the flag can still be set with interrupts off.
> >
> > This follows walk_pud_range(), which already pairs need_resched()
> > with a MAX_LRU_BATCH cap, and commit 6e9be217a3ce ("mm/mglru: use a
> > smaller batch for reclaim").
> >
> > Signed-off-by: Hui Zhu <zhuhui@xxxxxxxxxx>
> > ---
> > mm/vmscan.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/vmscan.c b/mm/vmscan.c
> > index 6a1af9b92349..47f2cb25eb46 100644
> > --- a/mm/vmscan.c
> > +++ b/mm/vmscan.c
> > @@ -5533,7 +5533,8 @@ static bool fill_evictable(struct lruvec *lruvec)
> > success = lru_gen_add_folio(lruvec, folio, !active);
> > VM_WARN_ON_ONCE(!success);
> >
> > - if (!--remaining)
> > + /* stop early to shorten the irq-off section */
> > + if (!--remaining || need_resched())
>
> Hi Hui,
>
> I've been thinking about a different design, what if we just
> delete the `--remaining` check completely, and check
> spin_is_contended on the lruvec and need_resched()?
>
> In the optimal case we don't need to keep lock/unlock the lruvec
> at all so this is faster. And it still avoids lock contention or
> blocked schedule, and it might be even more responsive as we are
> release the lock earlier on contention.
Hmm, nevermind, I just realized we have irq off here so it's
definitely a bad idea to drop batch limit. But maybe we can check the
lock too besides need_resched()?