All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
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
Subject: + mm-mglru-cache-the-last-memcg-node-make-tail-insertion-constant-time.patch added to mm-new branch
Date: Tue, 01 Dec 2026 17:52:23 -0800	[thread overview]
Message-ID: <20261202015223.A89711F00893@smtp.kernel.org> (raw)
In-Reply-To: <20261123135200.4086519-4-max.kellermann@ionos.com>


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 <max.kellermann@ionos.com>
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 <max.kellermann@ionos.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Barry Song <baohua@kernel.org>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Kairui Song <kasong@tencent.com>
Cc: Qi Zheng <qi.zheng@linux.dev>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Yuanchu Xie <yuanchu@google.com>
Cc: Wei Xu <weixugc@google.com>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: David Hildenbrand <david@kernel.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Ridong Chen <ridong.chen@linux.dev>

 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


  reply	other threads:[~2026-12-02  1:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:51 [PATCH v4 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
2026-11-23 13:51 ` [PATCH v4 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_last_rcu() Max Kellermann
2026-12-02  1:52   ` + include-linux-rculist_nulls-add-hlist_nulls_add_behind_last_rcu.patch added to mm-new branch Andrew Morton
2026-11-23 13:51 ` [PATCH v4 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg() Max Kellermann
2026-11-23 21:22   ` Barry Song
2026-12-02  1:52   ` + mm-vmscan-move-code-to-lru_gen_adddel_memcg.patch added to mm-new branch Andrew Morton
2026-11-23 13:51 ` [PATCH v4 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time Max Kellermann
2026-12-02  1:52   ` Andrew Morton [this message]
2026-11-23 13:51 ` [PATCH v4 4/5] mm/mglru: avoid redundant rotations of young memcgs Max Kellermann
2026-11-23 21:23   ` Barry Song
2026-12-02  1:52   ` + mm-mglru-avoid-redundant-rotations-of-young-memcgs.patch added to mm-new branch Andrew Morton
2026-11-23 13:52 ` [PATCH v4 5/5] mm/mglru: reschedule between memcg reclaim attempts Max Kellermann
2026-12-02  1:52   ` + mm-mglru-reschedule-between-memcg-reclaim-attempts.patch added to mm-new branch Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202015223.A89711F00893@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=ljs@kernel.org \
    --cc=max.kellermann@ionos.com \
    --cc=mhocko@suse.com \
    --cc=mm-commits@vger.kernel.org \
    --cc=paulmck@kernel.org \
    --cc=qi.zheng@linux.dev \
    --cc=ridong.chen@linux.dev \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=yuanchu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.