From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796176343; b=UdHwue74yOdOGB8bh1nZnAN7uoFTl7snIymgyw0Vn3r+zN0PeX2INnBn8gpYm9kNq3sZ FKXCF2JmhhDu1mxC55XKg3Q8JPUg7TjWNIhamQRzZAaCr6h3xLCaomvWUNNzQONSUSgF8 9FyuJKts4GxezvuqBq+ZJySrEJDzEaomGCu1dJygbbrCafuCWIfW/603XPLsbBOlSyKZk pMjq+FOK45pMohUoEHlbvso9+LOb2He4tiwGbDxEpkmXNw8I+5QUAu5TKphhVNyWp/lii 2+G8zlPV4C6ED5fJD7AcOHvPozK/tq3rNxciHm0TGOq0MhDk3+1xo/DnUdsh9Swn/JQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796176343; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=nXoY+vW3kBHbr2V+TrG++tEHu8BfZEXPzELmq570NLA=; b=Jj2AJtc5j3TB1wGpRLQh8X3UTjqTvd/aXOPssW40FNybnw5+2V/fWMLxg5ETbb4jXOAx E0lhQ2ucJOSm5lFjnqdIm305yjbGcaB556M0C2FXitmkUmVF3C08cIvQZ1Yl48CoJigIB IuIt6VH2BMr+2daAuplWTs8/sO66LY6KlHpXn/HlcX+PGAcfizUpwd131ovhrPU5zrFXk 3ImiglkFOZk5bgW8tjwYyOUCK7X61FRt3CYSDeAh5moODzuhyKU6IqGi2yl0a0YV5uf4K go6la57p9n2gSkQ00Gv5e/nLeW5sP+0Eh7YYbzKgVPHd4v9PLIiFLo3M79Lc7TE8vSw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=a6mPBULm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC991606A31A for ; Wed, 2 Dec 2026 01:52:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=a6mPBULm Received: by smtp.kernel.org (Postfix) with ESMTPSA id A89711F00893; Wed, 2 Dec 2026 01:52:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796176343; bh=nXoY+vW3kBHbr2V+TrG++tEHu8BfZEXPzELmq570NLA=; h=Date:To:From:In-Reply-To:References:Subject; b=a6mPBULmiWAq8EmdNFmuYz1eUJ3LkhEBOzZ5yLpAXhewN7jwuLa7rF9YIA/9i402/ 71VcwAQ6XrPCJmu7HLEhH13iP7XqZuIRLvwkIakv2yUkClYuWXhP6EfcVFs5Yo5hwO 8dUgqklRATSSds5Z+mCGElUNyxmIhqFX/Rn4NwF0= Date: Tue, 01 Dec 2026 17:52:23 -0800 To: mm-commits@vger.kernel.org,yuanchu@google.com,weixugc@google.com, vbabka@kernel.org,surenb@google.com,shakeel.butt@linux.dev,rppt@kernel.org, ridong.chen@linux.dev,qi.zheng@linux.dev,paulmck@kernel.org,mhocko@suse.com, ljs@kernel.org,liam@infradead.org,kasong@tencent.com,hannes@cmpxchg.org, david@kernel.org,baoquan.he@linux.dev,baolin.wang@linux.alibaba.com, baohua@kernel.org,axelrasmussen@google.com,max.kellermann@ionos.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261123135200.4086519-4-max.kellermann@ionos.com> References: <20261123135200.4086519-4-max.kellermann@ionos.com> Subject: + mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time.patch added to mm-new branch Message-Id: <20261202015223.A89711F00893@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/mglru: cache the last memcg node, make tail insertion constant time has been added to the -mm mm-new branch. Its filename is mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Max Kellermann Subject: mm/mglru: cache the last memcg node, make tail insertion constant time Date: Mon, 23 Nov 2026 14:51:58 +0100 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. Link: https://patch.msgid.link/20261123135200.4086519-4-max.kellermann@ionos.com Fixes: e4dde56cd208 ("mm: multi-gen LRU: per-node lru_gen_folio lists") Signed-off-by: Max Kellermann Signed-off-by: Andrew Morton Reviewed-by: Barry Song Reviewed-by: Baolin Wang Cc: Kairui Song Cc: Qi Zheng Cc: Shakeel Butt Cc: Axel Rasmussen Cc: Yuanchu Xie Cc: Wei Xu Cc: Baoquan He Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Johannes Weiner Cc: "Paul E. McKenney" Cc: Ridong Chen include/linux/mmzone.h | 4 ++++ mm/vmscan.c | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) --- a/include/linux/mmzone.h~mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time +++ a/include/linux/mmzone.h @@ -622,6 +622,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; }; @@ -715,6 +717,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; }; --- a/mm/vmscan.c~mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time +++ a/mm/vmscan.c @@ -4722,18 +4722,38 @@ static void lru_gen_add_memcg(struct 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); + VM_WARN_ON_ONCE((!last) != hlist_nulls_empty(head)); + + lrugen->bin = bin; + + if (!last) + hlist_nulls_add_head_rcu(&lrugen->list, head); + else + hlist_nulls_add_behind_last_rcu(&lrugen->list, last); + + memcg_lru->tail[gen][bin] = &lrugen->list; } static void lru_gen_del_memcg(struct lru_gen_memcg *memcg_lru, struct lru_gen_folio *lrugen, int gen) { + const int bin = lrugen->bin; + struct hlist_nulls_head *head = &memcg_lru->fifo[gen][bin]; + lockdep_assert_held(&memcg_lru->lock); + /* pprev points to head->first for the only entry, otherwise prev->next. */ + if (memcg_lru->tail[gen][bin] == &lrugen->list) + memcg_lru->tail[gen][bin] = lrugen->list.pprev == &head->first + ? NULL + : container_of(lrugen->list.pprev, struct hlist_nulls_node, next); + + /* Keep next intact for readers that still hold this entry. */ hlist_nulls_del_init_rcu(&lrugen->list); } @@ -6401,8 +6421,10 @@ void lru_gen_init_pgdat(struct pglist_da spin_lock_init(&pgdat->memcg_lru.lock); for (i = 0; i < MEMCG_NR_GENS; i++) { - for (j = 0; j < MEMCG_NR_BINS; j++) + for (j = 0; j < MEMCG_NR_BINS; j++) { INIT_HLIST_NULLS_HEAD(&pgdat->memcg_lru.fifo[i][j], i); + pgdat->memcg_lru.tail[i][j] = NULL; + } } } _ Patches currently in -mm which might be from max.kellermann@ionos.com are include-linux-rculist_nulls-add-hlist_nulls_add_behind_last_rcu.patch mm-vmscan-move-code-to-lru_gen_adddel_memcg.patch mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time.patch mm-mglru-avoid-redundant-rotations-of-young-memcgs.patch mm-mglru-reschedule-between-memcg-reclaim-attempts.patch