Re: [PATCH v6 02/18] mm/memcontrol: allow update of LRU statistic without holding LRU lock

From: Kairui Song

Date: Thu Nov 26 2026 - 07:38:49 EST


On Thu, Nov 26, 2026 at 05:43:14PM +0800, Ridong Chen wrote:
>
>
> On 11/24/2026 2:52 AM, Kairui Song via B4 Relay wrote:
> > From: Kairui Song <kasong@xxxxxxxxxxx>
> >
> > To enable moving file pages in folio_mark_accessed directly and lazily
> > for MGLRU, allow updating the LRU statistic atomically without holding a
> > lock. It may cause temporary counter underflow, which should be fine as
> > the counter is eventually consistent and only serves as an estimate.
> >
> > Reviewed-by: Barry Song <baohua@xxxxxxxxxx>
> > Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
> > Tested-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Acked-by: Muchun Song (ByteDance) <muchun.song@xxxxxxxxx>
> > Reviewed-by: Lian Wang <lianux.mm@xxxxxxxxx>
> > Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
> > ---
> > include/linux/mm_inline.h | 5 ++---
> > 1 file changed, 2 insertions(+), 3 deletions(-)
> >
> > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
> > index 2395ca8ee305..2edb9beef6d3 100644
> > --- a/include/linux/mm_inline.h
> > +++ b/include/linux/mm_inline.h
> > @@ -47,12 +47,11 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,
> > {
> > struct pglist_data *pgdat = lruvec_pgdat(lruvec);
> > - lockdep_assert_held(&lruvec->lru_lock);
> > WARN_ON_ONCE(nr_pages != (int)nr_pages);
> > mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
> > - __mod_zone_page_state(&pgdat->node_zones[zid],
> > - NR_ZONE_LRU_BASE + lru, nr_pages);
> > + mod_zone_page_state(&pgdat->node_zones[zid],
> > + NR_ZONE_LRU_BASE + lru, nr_pages);
> > }
> > static __always_inline void update_lru_size(struct lruvec *lruvec,
> >
>
> Hi Kairui,
>
> Just a heads-up: this may conflict with Joanne's "mm: move LRU size stat
> updates out of lru_lock in folio batches" [1], which is in linux-next now.
>
> That patch makes __update_lru_size() add to a batch attached to the
> lruvec by the lru_lock holder, and relies on lockdep_assert_held() there.
> Once this patch lets folio_inc_lru_refs()/folio_reset_lru_refs() call it
> without the lock, they could write into another CPU's on-stack batch.
> So the lockless paths probably need to bypass the batch.
>
> [1] https://lore.kernel.org/20261029204157.117879-1-joannelkoong@xxxxxxxxx

Yes I'm aware of that, the fix is easy too, something like:

diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 4ca8011bd288..fb5e3990402c 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -64,6 +64,30 @@ static __always_inline void __update_lru_size(struct lruvec *lruvec,
NR_ZONE_LRU_BASE + lru, nr_pages);
}

+static __always_inline void __update_lru_size_lockless(struct lruvec *lruvec,
+ enum lru_list lru, enum zone_type zid,
+ long nr_pages)
+{
+ struct pglist_data *pgdat = lruvec_pgdat(lruvec);
+
+ VM_WARN_ON_ONCE(nr_pages != (int)nr_pages);
+ mod_lruvec_state(lruvec, NR_LRU_BASE + lru, nr_pages);
+ mod_zone_page_state(&pgdat->node_zones[zid],
+ NR_ZONE_LRU_BASE + lru, nr_pages);
+}
+
static __always_inline void update_lru_size(struct lruvec *lruvec,
enum lru_list lru, enum zone_type zid,
long nr_pages)

---

And I think that batch design could be somehow improved too,
holding a stack variable in a global struct looks a bit odd, could
be simplified if we audit and update all users for update_lru_size
later.