* [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu()
2026-11-16 8:13 [PATCH v3 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
@ 2026-11-16 8:13 ` Max Kellermann
2026-11-17 8:14 ` sashiko-bot
2026-11-16 8:14 ` [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg() Max Kellermann
` (3 subsequent siblings)
4 siblings, 1 reply; 15+ messages in thread
From: Max Kellermann @ 2026-11-16 8:13 UTC (permalink / raw)
To: akpm, kasong, qi.zheng, shakeel.butt, baohua, axelrasmussen,
yuanchu, weixugc, baoquan.he, baolin.wang, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
Cc: Max Kellermann
Move some code from hlist_nulls_add_tail_rcu() to the new function
hlist_nulls_add_behind_rcu() for callers that already know the last
node of an `hlist_nulls` list, to allow constant-time appends.
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
include/linux/rculist_nulls.h | 36 +++++++++++++++++++++++++++++------
1 file changed, 30 insertions(+), 6 deletions(-)
diff --git a/include/linux/rculist_nulls.h b/include/linux/rculist_nulls.h
index a97c3bcb1656..ffbf5ed4ff19 100644
--- a/include/linux/rculist_nulls.h
+++ b/include/linux/rculist_nulls.h
@@ -115,6 +115,33 @@ static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
WRITE_ONCE(first->pprev, &n->next);
}
+/**
+ * hlist_nulls_add_behind_last_rcu - append after the last list entry
+ * @n: the new element to add to the hash list.
+ * @last: the last element in the list.
+ *
+ * Adds the specified element after @last while permitting racing RCU
+ * traversals. @last must be the last list entry, with its next
+ * pointer holding the terminating nulls marker.
+ *
+ * The caller must take whatever precautions are necessary
+ * (such as holding appropriate locks) to avoid racing
+ * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
+ * or hlist_nulls_del_rcu(), running on this same list.
+ * However, it is perfectly legal to run concurrently with
+ * the _rcu list-traversal primitives, such as
+ * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
+ * problems on Alpha CPUs. Regardless of the type of CPU, the
+ * list-traversal primitive must be guarded by rcu_read_lock().
+ */
+static inline void hlist_nulls_add_behind_last_rcu(struct hlist_nulls_node *n,
+ struct hlist_nulls_node *last)
+{
+ WRITE_ONCE(n->next, last->next);
+ WRITE_ONCE(n->pprev, &last->next);
+ rcu_assign_pointer(hlist_nulls_next_rcu(last), n);
+}
+
/**
* hlist_nulls_add_tail_rcu
* @n: the element to add to the hash list.
@@ -143,13 +170,10 @@ static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
for (i = h->first; !is_a_nulls(i); i = i->next)
last = i;
- if (last) {
- WRITE_ONCE(n->next, last->next);
- WRITE_ONCE(n->pprev, &last->next);
- rcu_assign_pointer(hlist_nulls_next_rcu(last), n);
- } else {
+ if (last)
+ hlist_nulls_add_behind_last_rcu(n, last);
+ else
hlist_nulls_add_head_rcu(n, h);
- }
}
/* after that hlist_nulls_del will work */
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu()
2026-11-16 8:13 ` [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu() Max Kellermann
@ 2026-11-17 8:14 ` sashiko-bot
2026-11-19 19:55 ` Barry Song
0 siblings, 1 reply; 15+ messages in thread
From: sashiko-bot @ 2026-11-17 8:14 UTC (permalink / raw)
To: Max Kellermann, akpm, kasong, qi.zheng, shakeel.butt, baohua,
axelrasmussen, yuanchu, weixugc, baoquan.he, baolin.wang, david,
ljs, liam, vbabka, rppt, surenb, mhocko, hannes, linux-mm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] rculist_nulls: commit message mismatches
hlist_nulls_add_behind_last_rcu()
--
commit 8be860a8b1d9e4863e4165df073c841c355f9d25
Author: Max Kellermann <max.kellermann@ionos.com>
> include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu()
>
> Move some code from hlist_nulls_add_tail_rcu() to the new function
> hlist_nulls_add_behind_rcu() for callers that already know the last
> node of an `hlist_nulls` list, to allow constant-time appends.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
[Severity: Low]
The commit subject and message refer to hlist_nulls_add_behind_rcu(),
whereas the helper added by the patch is named
hlist_nulls_add_behind_last_rcu().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116081403.216452-1-max.kellermann@ionos.com?part=1
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu()
2026-11-17 8:14 ` sashiko-bot
@ 2026-11-19 19:55 ` Barry Song
0 siblings, 0 replies; 15+ messages in thread
From: Barry Song @ 2026-11-19 19:55 UTC (permalink / raw)
To: sashiko-reviews
Cc: Max Kellermann, akpm, kasong, qi.zheng, shakeel.butt,
axelrasmussen, yuanchu, weixugc, baoquan.he, baolin.wang, david,
ljs, liam, vbabka, rppt, surenb, mhocko, hannes, linux-mm
On Tue, Nov 17, 2026 at 4:14 PM <sashiko-bot@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Low] rculist_nulls: commit message mismatches
> hlist_nulls_add_behind_last_rcu()
> --
>
> commit 8be860a8b1d9e4863e4165df073c841c355f9d25
> Author: Max Kellermann <max.kellermann@ionos.com>
>
> > include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu()
> >
> > Move some code from hlist_nulls_add_tail_rcu() to the new function
> > hlist_nulls_add_behind_rcu() for callers that already know the last
> > node of an `hlist_nulls` list, to allow constant-time appends.
> >
> > Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
>
> [Severity: Low]
> The commit subject and message refer to hlist_nulls_add_behind_rcu(),
> whereas the helper added by the patch is named
> hlist_nulls_add_behind_last_rcu().
With the commit message fixed,
Reviewed-by: Barry Song <baohua@kernel.org>
I recall that Paul also gave his Reviewed-by tag earlier. Should we
pick it up as well?
https://lore.kernel.org/linux-mm/7037ee68-5dcb-46ed-a73f-ed6ea58915f4@paulmck-laptop/
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261116081403.216452-1-max.kellermann@ionos.com?part=1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg()
2026-11-16 8:13 [PATCH v3 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
2026-11-16 8:13 ` [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu() Max Kellermann
@ 2026-11-16 8:14 ` Max Kellermann
2026-11-23 1:35 ` Barry Song
2026-11-23 9:40 ` Baolin Wang
2026-11-16 8:14 ` [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time Max Kellermann
` (2 subsequent siblings)
4 siblings, 2 replies; 15+ messages in thread
From: Max Kellermann @ 2026-11-16 8:14 UTC (permalink / raw)
To: akpm, kasong, qi.zheng, shakeel.butt, baohua, axelrasmussen,
yuanchu, weixugc, baoquan.he, baolin.wang, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
Cc: Max Kellermann
Preparation for the following patch that adds tail caching.
For simplicity, hlist_nulls_del_init_rcu() is now always used, which
is also valid during rotation, because the entry is immediately
reinserted under `memcg_lru.lock`.
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
mm/vmscan.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index e772eacff3f8..49ef29a86553 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4429,6 +4429,26 @@ enum {
MEMCG_LRU_YOUNG,
};
+static void lru_gen_add_memcg(struct lru_gen_memcg *memcg_lru,
+ struct lru_gen_folio *lrugen,
+ int gen, int bin)
+{
+ struct hlist_nulls_head *head = &memcg_lru->fifo[gen][bin];
+
+ lockdep_assert_held(&memcg_lru->lock);
+
+ hlist_nulls_add_tail_rcu(&lrugen->list, head);
+}
+
+static void lru_gen_del_memcg(struct lru_gen_memcg *memcg_lru,
+ struct lru_gen_folio *lrugen,
+ int gen)
+{
+ lockdep_assert_held(&memcg_lru->lock);
+
+ hlist_nulls_del_init_rcu(&lrugen->list);
+}
+
static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
{
int seg;
@@ -4455,8 +4475,8 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
WRITE_ONCE(lruvec->lrugen.seg, seg);
WRITE_ONCE(lruvec->lrugen.gen, new);
- hlist_nulls_del_rcu(&lruvec->lrugen.list);
- hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[new][bin]);
+ lru_gen_del_memcg(&pgdat->memcg_lru, &lruvec->lrugen, old);
+ lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, new, bin);
pgdat->memcg_lru.nr_memcgs[old]--;
pgdat->memcg_lru.nr_memcgs[new]++;
@@ -4487,7 +4507,7 @@ void lru_gen_online_memcg(struct mem_cgroup *memcg)
lruvec->lrugen.gen = gen;
- hlist_nulls_add_tail_rcu(&lruvec->lrugen.list, &pgdat->memcg_lru.fifo[gen][bin]);
+ lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, gen, bin);
pgdat->memcg_lru.nr_memcgs[gen]++;
spin_unlock_irq(&pgdat->memcg_lru.lock);
@@ -4510,7 +4530,7 @@ void lru_gen_release_memcg(struct mem_cgroup *memcg)
gen = lruvec->lrugen.gen;
- hlist_nulls_del_init_rcu(&lruvec->lrugen.list);
+ lru_gen_del_memcg(&pgdat->memcg_lru, &lruvec->lrugen, gen);
pgdat->memcg_lru.nr_memcgs[gen]--;
if (!pgdat->memcg_lru.nr_memcgs[gen] && gen == get_memcg_gen(pgdat->memcg_lru.seq))
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg()
2026-11-16 8:14 ` [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg() Max Kellermann
@ 2026-11-23 1:35 ` Barry Song
2026-11-23 9:40 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Barry Song @ 2026-11-23 1:35 UTC (permalink / raw)
To: Max Kellermann
Cc: akpm, kasong, qi.zheng, shakeel.butt, axelrasmussen, yuanchu,
weixugc, baoquan.he, baolin.wang, david, ljs, liam, vbabka, rppt,
surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On Mon, Nov 16, 2026 at 4:14 PM Max Kellermann <max.kellermann@ionos.com> wrote:
>
> Preparation for the following patch that adds tail caching.
>
> For simplicity, hlist_nulls_del_init_rcu() is now always used, which
> is also valid during rotation, because the entry is immediately
> reinserted under `memcg_lru.lock`.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
Reviewed-by: Barry Song <baohua@kernel.org>
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg()
2026-11-16 8:14 ` [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg() Max Kellermann
2026-11-23 1:35 ` Barry Song
@ 2026-11-23 9:40 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Baolin Wang @ 2026-11-23 9:40 UTC (permalink / raw)
To: Max Kellermann, akpm, kasong, qi.zheng, shakeel.butt, baohua,
axelrasmussen, yuanchu, weixugc, baoquan.he, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On 11/16/26 4:14 PM, Max Kellermann wrote:
> Preparation for the following patch that adds tail caching.
>
> For simplicity, hlist_nulls_del_init_rcu() is now always used, which
> is also valid during rotation, because the entry is immediately
> reinserted under `memcg_lru.lock`.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time
2026-11-16 8:13 [PATCH v3 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
2026-11-16 8:13 ` [PATCH v3 1/5] include/linux/rculist_nulls: add hlist_nulls_add_behind_rcu() Max Kellermann
2026-11-16 8:14 ` [PATCH v3 2/5] mm/vmscan: move code to lru_gen_{add,del}_memcg() Max Kellermann
@ 2026-11-16 8:14 ` Max Kellermann
2026-11-23 1:53 ` Barry Song
2026-11-23 9:59 ` Baolin Wang
2026-11-16 8:14 ` [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs Max Kellermann
2026-11-16 8:14 ` [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts Max Kellermann
4 siblings, 2 replies; 15+ messages in thread
From: Max Kellermann @ 2026-11-16 8:14 UTC (permalink / raw)
To: akpm, kasong, qi.zheng, shakeel.butt, baohua, axelrasmussen,
yuanchu, weixugc, baoquan.he, baolin.wang, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
Cc: Max Kellermann
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@ionos.com>
---
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);
+ 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);
}
@@ -6106,8 +6124,10 @@ void lru_gen_init_pgdat(struct pglist_data *pgdat)
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;
+ }
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time
2026-11-16 8:14 ` [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time Max Kellermann
@ 2026-11-23 1:53 ` Barry Song
2026-11-23 9:59 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Barry Song @ 2026-11-23 1:53 UTC (permalink / raw)
To: Max Kellermann
Cc: akpm, kasong, qi.zheng, shakeel.butt, axelrasmussen, yuanchu,
weixugc, baoquan.he, baolin.wang, david, ljs, liam, vbabka, rppt,
surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On Mon, Nov 16, 2026 at 4:14 PM Max Kellermann <max.kellermann@ionos.com> 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@ionos.com>
> ---
> 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@kernel.org>
> + else
> + hlist_nulls_add_behind_last_rcu(&lrugen->list, last);
> +
> + memcg_lru->tail[gen][bin] = &lrugen->list;
> }
Thanks,
Barry
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time
2026-11-16 8:14 ` [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time Max Kellermann
2026-11-23 1:53 ` Barry Song
@ 2026-11-23 9:59 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Baolin Wang @ 2026-11-23 9:59 UTC (permalink / raw)
To: Max Kellermann, akpm, kasong, qi.zheng, shakeel.butt, baohua,
axelrasmussen, yuanchu, weixugc, baoquan.he, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On 11/16/26 4:14 PM, Max Kellermann 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")
I don't think this fixes anything, right?
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> ---
Thanks. We hit the same issue in our stress tests and confirmed that
this optimization works.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs
2026-11-16 8:13 [PATCH v3 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
` (2 preceding siblings ...)
2026-11-16 8:14 ` [PATCH v3 3/5] mm/mglru: cache the last memcg node, make tail insertion constant time Max Kellermann
@ 2026-11-16 8:14 ` Max Kellermann
2026-11-23 2:01 ` Barry Song
2026-11-16 8:14 ` [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts Max Kellermann
4 siblings, 1 reply; 15+ messages in thread
From: Max Kellermann @ 2026-11-16 8:14 UTC (permalink / raw)
To: akpm, kasong, qi.zheng, shakeel.butt, baohua, axelrasmussen,
yuanchu, weixugc, baoquan.he, baolin.wang, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
Cc: Max Kellermann
Concurrent reclaimers can request promotion after another reclaimer
has already moved a memcg into the young generation. Requeueing it
into a random bin again adds list mutations without advancing the
old-generation scan.
Therefore, skip `MEMCG_LRU_YOUNG` when the memcg is already in the
default segment of the current young generation.
In my micro benchmark, this increases the request rate by 36% and
reduces the average duration of each write request by 27%.
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
mm/vmscan.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 7cfaeca9a605..6b7c9f118e18 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4472,7 +4472,6 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
int seg;
int old, new;
unsigned long flags;
- int bin = get_random_u32_below(MEMCG_NR_BINS);
struct pglist_data *pgdat = lruvec_pgdat(lruvec);
spin_lock_irqsave(&pgdat->memcg_lru.lock, flags);
@@ -4490,11 +4489,19 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
else
VM_WARN_ON_ONCE(true);
+ if (op == MEMCG_LRU_YOUNG && old == new && lruvec->lrugen.seg == seg)
+ /*
+ * This memcg is already in the young generation and
+ * reshuffling it again cannot help the scan of the
+ * old generation.
+ */
+ goto unlock;
+
WRITE_ONCE(lruvec->lrugen.seg, seg);
WRITE_ONCE(lruvec->lrugen.gen, new);
lru_gen_del_memcg(&pgdat->memcg_lru, &lruvec->lrugen, old);
- lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, new, bin);
+ lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, new, get_random_u32_below(MEMCG_NR_BINS));
pgdat->memcg_lru.nr_memcgs[old]--;
pgdat->memcg_lru.nr_memcgs[new]++;
@@ -4502,6 +4509,7 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq))
WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
+unlock:
spin_unlock_irqrestore(&pgdat->memcg_lru.lock, flags);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs
2026-11-16 8:14 ` [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs Max Kellermann
@ 2026-11-23 2:01 ` Barry Song
0 siblings, 0 replies; 15+ messages in thread
From: Barry Song @ 2026-11-23 2:01 UTC (permalink / raw)
To: Max Kellermann
Cc: akpm, kasong, qi.zheng, shakeel.butt, axelrasmussen, yuanchu,
weixugc, baoquan.he, baolin.wang, david, ljs, liam, vbabka, rppt,
surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On Mon, Nov 16, 2026 at 4:14 PM Max Kellermann <max.kellermann@ionos.com> wrote:
>
> Concurrent reclaimers can request promotion after another reclaimer
> has already moved a memcg into the young generation. Requeueing it
> into a random bin again adds list mutations without advancing the
> old-generation scan.
>
> Therefore, skip `MEMCG_LRU_YOUNG` when the memcg is already in the
> default segment of the current young generation.
>
> In my micro benchmark, this increases the request rate by 36% and
> reduces the average duration of each write request by 27%.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> ---
> mm/vmscan.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 7cfaeca9a605..6b7c9f118e18 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -4472,7 +4472,6 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
> int seg;
> int old, new;
> unsigned long flags;
> - int bin = get_random_u32_below(MEMCG_NR_BINS);
> struct pglist_data *pgdat = lruvec_pgdat(lruvec);
>
> spin_lock_irqsave(&pgdat->memcg_lru.lock, flags);
> @@ -4490,11 +4489,19 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
> else
> VM_WARN_ON_ONCE(true);
>
> + if (op == MEMCG_LRU_YOUNG && old == new && lruvec->lrugen.seg == seg)
> + /*
> + * This memcg is already in the young generation and
> + * reshuffling it again cannot help the scan of the
> + * old generation.
> + */
> + goto unlock;
> +
> WRITE_ONCE(lruvec->lrugen.seg, seg);
> WRITE_ONCE(lruvec->lrugen.gen, new);
>
> lru_gen_del_memcg(&pgdat->memcg_lru, &lruvec->lrugen, old);
> - lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, new, bin);
> + lru_gen_add_memcg(&pgdat->memcg_lru, &lruvec->lrugen, new, get_random_u32_below(MEMCG_NR_BINS));
Hi Max,
I know it was my suggestion to avoid unnecessary calls to
get_random_u32_below() unless we actually need them.
You then pointed out that this would move get_random_u32_below()
inside the spinlock, which I had overlooked. Sorry about that.
Have you tested whether calling get_random_u32_below() under the
spinlock increases lock contention?
>
> pgdat->memcg_lru.nr_memcgs[old]--;
> pgdat->memcg_lru.nr_memcgs[new]++;
> @@ -4502,6 +4509,7 @@ static void lru_gen_rotate_memcg(struct lruvec *lruvec, int op)
> if (!pgdat->memcg_lru.nr_memcgs[old] && old == get_memcg_gen(pgdat->memcg_lru.seq))
> WRITE_ONCE(pgdat->memcg_lru.seq, pgdat->memcg_lru.seq + 1);
>
> +unlock:
> spin_unlock_irqrestore(&pgdat->memcg_lru.lock, flags);
> }
Thanks,
Barry
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts
2026-11-16 8:13 [PATCH v3 0/5] mm: multi-gen LRU: reduce memcg rotation overhead Max Kellermann
` (3 preceding siblings ...)
2026-11-16 8:14 ` [PATCH v3 4/5] mm/mglru: avoid redundant rotations of young memcgs Max Kellermann
@ 2026-11-16 8:14 ` Max Kellermann
2026-11-23 2:24 ` Barry Song
2026-11-23 10:09 ` Baolin Wang
4 siblings, 2 replies; 15+ messages in thread
From: Max Kellermann @ 2026-11-16 8:14 UTC (permalink / raw)
To: akpm, kasong, qi.zheng, shakeel.butt, baohua, axelrasmussen,
yuanchu, weixugc, baoquan.he, baolin.wang, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
Cc: Max Kellermann
shrink_one() can return immediately for empty or protected memcgs, and
try_to_shrink_lruvec() can break out before reaching its
cond_resched(). A scan through many memcgs can repeatedly rotate
entries without giving other runnable tasks a scheduling opportunity.
Call cond_resched_tasks_rcu_qs() (as the existing shrink_lruvec() loop
does) after shrink_one(), while the memcg reference still pins the
cursor and before reacquiring the RCU read lock. Also call it between
bins and nulls-marker retries, covering scans that skip every entry.
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
mm/vmscan.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 6b7c9f118e18..a0d76e5a10c0 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -5400,6 +5400,9 @@ static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
gen = get_memcg_gen(READ_ONCE(pgdat->memcg_lru.seq));
bin = first_bin = get_random_u32_below(MEMCG_NR_BINS);
restart:
+ /* Yield between bins/retries that contain only skipped entries. */
+ cond_resched_tasks_rcu_qs();
+
op = 0;
memcg = NULL;
@@ -5430,6 +5433,14 @@ static void shrink_many(struct pglist_data *pgdat, struct scan_control *sc)
op = shrink_one(lruvec, sc);
+ /*
+ * Empty or protected memcgs can bypass the
+ * rescheduling points in page reclaim. The memcg
+ * reference pins our cursor while we yield outside
+ * the RCU read-side critical section.
+ */
+ cond_resched_tasks_rcu_qs();
+
rcu_read_lock();
if (should_abort_scan(lruvec, sc))
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts
2026-11-16 8:14 ` [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts Max Kellermann
@ 2026-11-23 2:24 ` Barry Song
2026-11-23 10:09 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Barry Song @ 2026-11-23 2:24 UTC (permalink / raw)
To: Max Kellermann
Cc: akpm, kasong, qi.zheng, shakeel.butt, axelrasmussen, yuanchu,
weixugc, baoquan.he, baolin.wang, david, ljs, liam, vbabka, rppt,
surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On Mon, Nov 16, 2026 at 4:14 PM Max Kellermann <max.kellermann@ionos.com> wrote:
>
> shrink_one() can return immediately for empty or protected memcgs, and
> try_to_shrink_lruvec() can break out before reaching its
> cond_resched(). A scan through many memcgs can repeatedly rotate
> entries without giving other runnable tasks a scheduling opportunity.
>
> Call cond_resched_tasks_rcu_qs() (as the existing shrink_lruvec() loop
> does) after shrink_one(), while the memcg reference still pins the
> cursor and before reacquiring the RCU read lock. Also call it between
> bins and nulls-marker retries, covering scans that skip every entry.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> ---
Reviewed-by: Barry Song <baohua@kernel.org>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts
2026-11-16 8:14 ` [PATCH v3 5/5] mm/mglru: reschedule between memcg reclaim attempts Max Kellermann
2026-11-23 2:24 ` Barry Song
@ 2026-11-23 10:09 ` Baolin Wang
1 sibling, 0 replies; 15+ messages in thread
From: Baolin Wang @ 2026-11-23 10:09 UTC (permalink / raw)
To: Max Kellermann, akpm, kasong, qi.zheng, shakeel.butt, baohua,
axelrasmussen, yuanchu, weixugc, baoquan.he, david, ljs, liam,
vbabka, rppt, surenb, mhocko, hannes, linux-mm, linux-kernel, rcu
On 11/16/26 4:14 PM, Max Kellermann wrote:
> shrink_one() can return immediately for empty or protected memcgs, and
> try_to_shrink_lruvec() can break out before reaching its
> cond_resched(). A scan through many memcgs can repeatedly rotate
> entries without giving other runnable tasks a scheduling opportunity.
>
> Call cond_resched_tasks_rcu_qs() (as the existing shrink_lruvec() loop
> does) after shrink_one(), while the memcg reference still pins the
> cursor and before reacquiring the RCU read lock. Also call it between
> bins and nulls-marker retries, covering scans that skip every entry.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> ---
LGTM.
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
^ permalink raw reply [flat|nested] 15+ messages in thread