Re: [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time

From: Barry Song

Date: Sun Nov 22 2026 - 20:53:37 EST


On Mon, Nov 16, 2026 at 4:14 PM Max Kellermann <max.kellermann@xxxxxxxxx> wrote:
>
> Rotating or onlining a memcg currently uses hlist_nulls_add_tail_rcu()
> which walks the destination bin to find its tail while holding
> `memcg_lru.lock` with interrupts disabled. With thousands of cgroups,
> this takes a long time and causes severe lock contention.
>
> Cache a pointer to each bin's last node, making tail insertion O(1).
> This requires remembering the bin number (but the new struct field
> occupies a hole, so it does not grow).
>
> In a micro benchmark that creates 1500 cgroups and repeatedly writes
> `1024M swappiness=0` to `memory.reclaim` in 8 threads, the request
> rate increases by a factor of 9.5 and the average duration of each
> write request is reduced by 89%.
>
> All writes return EAGAIN in both runs; the benchmark measures the cost
> of reclaim attempts that cannot meet their requested target, not
> reclaimed throughput.
>
> Fixes: e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists")
> Signed-off-by: Max Kellermann <max.kellermann@xxxxxxxxx>
> ---
> include/linux/mmzone.h | 4 ++++
> mm/vmscan.c | 24 ++++++++++++++++++++++--
> 2 files changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
> index c161d563ce8f..8382fc86cf0d 100644
> --- a/include/linux/mmzone.h
> +++ b/include/linux/mmzone.h
> @@ -570,6 +570,8 @@ struct lru_gen_folio {
> u8 gen;
> /* the list segment this lru_gen_folio belongs to */
> u8 seg;
> + /* the memcg bin, protected by memcg_lru.lock */
> + u8 bin;
> /* per-node lru_gen_folio list for global reclaim */
> struct hlist_nulls_node list;
> };
> @@ -661,6 +663,8 @@ struct lru_gen_memcg {
> unsigned long nr_memcgs[MEMCG_NR_GENS];
> /* per-node lru_gen_folio list for global reclaim */
> struct hlist_nulls_head fifo[MEMCG_NR_GENS][MEMCG_NR_BINS];
> + /* last entry in each bin, or NULL if empty */
> + struct hlist_nulls_node *tail[MEMCG_NR_GENS][MEMCG_NR_BINS];
> /* protects the above */
> spinlock_t lock;
> };
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 49ef29a86553..7cfaeca9a605 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -4434,18 +4434,36 @@ static void lru_gen_add_memcg(struct lru_gen_memcg *memcg_lru,
> int gen, int bin)
> {
> struct hlist_nulls_head *head = &memcg_lru->fifo[gen][bin];
> + struct hlist_nulls_node *last = memcg_lru->tail[gen][bin];
>
> lockdep_assert_held(&memcg_lru->lock);
>
> - hlist_nulls_add_tail_rcu(&lrugen->list, head);
> + lrugen->bin = bin;
> +
> + if (!last)
> + hlist_nulls_add_head_rcu(&lrugen->list, head);

So this is the case where the list is empty, right? Maybe add a
WARN_ON_ONCE() check before this code to make the assumption explicit
and catch potential violations in the future.

VM_WARN_ON_ONCE(!last != hlist_nulls_empty(head));


The patch itself looks correct to me.

Reviewed-by: Barry Song <baohua@xxxxxxxxxx>

> + else
> + hlist_nulls_add_behind_last_rcu(&lrugen->list, last);
> +
> + memcg_lru->tail[gen][bin] = &lrugen->list;
> }

Thanks,
Barry