Re: [PATCH v5 04/17] mm/memcg: add folio-based lruvec live helper
From: Muchun Song
Date: Mon Nov 23 2026 - 07:32:48 EST
> On Nov 23, 2026, at 01:03, Kairui Song <ryncsn@xxxxxxxxx> wrote:
>
> On Sun, Nov 22, 2026 at 03:53:44PM +0800, Muchun Song wrote:
>>
>>
>> On 2026/11/19 05:03, Kairui Song via B4 Relay wrote:
>>> From: Kairui Song <kasong@xxxxxxxxxxx>
>>>
>>> Add a helper that resolves a live lruvec for a folio under RCU
>>> without taking the lruvec lock. It takes a folio directly so the
>>> lruvec lookup happens inside the RCU read-side critical section,
>>> which a lruvec-based interface cannot guarantee. The first user is
>>> the lockless folio_inc_lru_refs() path later in the series.
>>>
>>> Factor the ancestor walk out of lruvec_live_lock_irq() into
>>> __lruvec_get_live(), so that both entry points share it, and spell
>>> out why the walk is enough while only the RCU read lock is held:
>>> reparenting of a memcg that starts dying waits for a grace period,
>>> so it cannot race with the caller until the lock is released.
>>>
>>> Move memcg_is_dying() up in memcontrol.h, next to the other memcg state
>>> predicates, and use it for the walk rather than repeating the open coded
>>> css_is_dying() test a few definitions away from where it is explained.
>>>
>>> No functional change for existing callers.
>>>
>>> Tested-by: Zhao Zhenzhuo <zhaozhengzhuo@xxxxxxxxxxxxx>
>>> Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
>>
>> Reviewed-by: Muchun Song (ByteDance) <muchun.song@xxxxxxxxx>
>>
>> Some nits bellow:
>>
>>> ---
>>> include/linux/memcontrol.h | 71 ++++++++++++++++++++++++++++++++++++----------
>>> 1 file changed, 56 insertions(+), 15 deletions(-)
>>>
>>> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
>>> index 980358bc6f49..441a5d57e723 100644
>>> --- a/include/linux/memcontrol.h
>>> +++ b/include/linux/memcontrol.h
>>> @@ -571,6 +571,11 @@ static inline bool mem_cgroup_disabled(void)
>>> return !cgroup_subsys_enabled(memory_cgrp_subsys);
>>> }
>>> +static inline bool memcg_is_dying(struct mem_cgroup *memcg)
>>
>> Let's make it const for @memcg along the way.
>
> Good idea, but css_is_dying isn't constified, maybe we can do it later I think.
Fine.
>
>>> @@ -1510,28 +1520,41 @@ static inline void lruvec_lock_irq(struct lruvec *lruvec)
>>> spin_lock_irq(&lruvec->lru_lock);
>>> }
>>> -static inline struct lruvec *lruvec_live_lock_irq(struct lruvec *lruvec)
>>> +/*
>>> + * Reparenting moves a dying memcg's LRU state to its parent at any time,
>>> + * so anything added to that LRU afterwards would be lost. This helper
>>> + * walks up to the nearest live ancestor while the caller must hold the
>>> + * RCU read lock: reparenting of a memcg that starts dying waits for a
>>> + * grace period, so it cannot race with the caller until the lock is
>>> + * released.
>>> + */
>>> +static inline struct lruvec *__lruvec_get_live(struct lruvec *lruvec)
>>
>> Cound @lruvec be const as well?
>
> The argument is fine, but tricky for the return value: when nothing is dying it
> hands back @lruvec unchanged, so a const argument means a const return (which
> isn't right) or a cast dropping it again.
A cast is not nice. Let's keep things as they are.
Thanks,
Muchun
>
>>> @@ -1561,6 +1584,34 @@ static inline bool folio_matches_lruvec(const struct folio *folio,
>>> lruvec_memcg(lruvec) == folio_memcg(folio);
>>> }
>>> +/**
>>> + * folio_lruvec_live_get - get a live lruvec for a folio under RCU
>>> + * @folio: the folio
>>> + *
>>> + * Same as folio_lruvec(), except that a folio whose memcg is dying is
>>> + * accounted to the nearest live ancestor, which is where reparenting
>>> + * would put it anyway.
>>> + *
>>> + * Return: the live lruvec, with rcu_read_lock() held. Pair it with
>>> + * folio_lruvec_live_put().
>>> + */
>>> +static inline struct lruvec *folio_lruvec_live_get(struct folio *folio)
>>
>> const folio?
>
> This is doable, will do!
>
> Thanks for the review!