memcg: fix cacheline contention between lruvec stat updates and LRU lists
Subsystems (3)
PATCH 1/1 memcg: fix cacheline contention between lruvec stat updates and LRU lists
Every memcg lruvec (per-node) stat update reads the memcg and lruvec_stats_percpu pointers in the first 64 bytes of struct mem_cgroup_per_node, and the ones that go through mod_lruvec_state() (such as lruvec_stat_mod_folio()) also read lruvec->pgdat. These updates are extremely frequent (eg these updates happen several times for every page that's faulted in, freed, dirtied, or written back).
The memcg_pn_read_mostly cacheline group keeps those read-mostly fields on a different cacheline from the lruvec, but on CPUs that do spatial prefetching [1] (which is most Intel CPUs / modern CPUs), cachelines are fetched in 128-byte aligned pairs. Depending on where struct mem_cgroup_per_node lands in its slab, that cacheline can be paired with the cacheline holding the LRU list heads, and the lruvec->pgdat cacheline with the one holding lru_lock.
When this happens, every stat update also pulls in a copy of the LRU list heads and lru_lock cachelines, which are written to constantly by other CPUs under the lru_lock. The stat update has to fetch them from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads or lru_lock at all. But even worse, the lru_lock holder then has to invalidate those copies and wait for the acknowledgements to come back before it can do its next write, which makes the lru_lock critical section longer for everyone waiting on the lock.
This was found when analyzing the output of perf c2c for a benchmark where many threads fault in pages in one memcg. It showed the first 64 bytes of mem_cgroup_per_node as the most contended cacheline (over a third of all HITMs and 5x more than the lru_lock cacheline) even though there are no writes to it. A quick experiment adding extra padding so that the cacheline no longer shared a pairing with the LRU list heads showed that this removed most of the contention, which confirmed the cause.
Fix this by not touching struct mem_cgroup_per_node on the main stat update paths and instead going through the memcg. The memcg keeps an array of pointers to the per-cpu lruvec stats of each node, pointed to from the same cacheline as vmstats_percpu, which the update path already reads. lruvec_stat_mod_folio() and mod_lruvec_kmem_state() pass the memcg and node they already know instead of going through the lruvec and fetching mem_cgroup_per_node->memcg and lruvec->pgdat. Both the memcg and lruvec_stats_percpu reads need to go, since either one alone still pulls in the cacheline.
Results from a multi-threaded fault/free benchmark in a single memcg, run on an Intel Skylake machine (80 threads, 2 NUMA nodes) with threads pinned to CPUs showed average pages faulted in (and freed) per second across all threads, over 7 freshly created memcgs per run:
before after shmem, 20 threads 4.74M 5.68M (+20%) shmem, 80 threads 7.94M 8.81M (+11%) anon, 20 threads 3.04M 3.13M (+3%) anon, 80 threads 3.23M 3.30M (+2%)
perf c2c showed HITMs on the read-mostly line from the stat update paths dropped from ~2350 to ~10.
[1] https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf
(bottom of page 62/800)
---
An alternative would be to turn memcg->nodeinfo[] into an array of {mem_cgroup_per_node, stats} pointer pairs, which saves one load, but this would require quite a bit of churn (would need to modify every nodeinfo call site). Happy to switch to this for v2 if this is preferred.
include/linux/memcontrol.h | 8 +++- mm/memcontrol.c | 88 +++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 49 deletions(-)
include/linux/memcontrol.h +7 -1
@@ -88,7 +88,6 @@ struct mem_cgroup_per_node {struct mem_cgroup *memcg;/* Back pointer, we cannot */ /* use container_of */ -struct lruvec_stats_percpu __percpu *lruvec_stats_percpu;struct lruvec_stats *lruvec_stats;struct shrinker_info __rcu *shrinker_info;struct obj_cgroup __rcu *objcg;
@@ -314,6 +313,13 @@ struct mem_cgroup {__cacheline_group_begin_aligned (memcg_read_mostly);/* Read on every stat update */ struct memcg_vmstats_percpu __percpu *vmstats_percpu; +/* +* Per-CPU lruvec stats of each node, indexed by node id. Points to an +* array after nodeinfo[] in the same allocation. Kept here instead of +* in mem_cgroup_per_node, to avoid sharing a cacheline pair with the +* very frequently written LRU lists. +*/ +struct lruvec_stats_percpu __percpu **lruvec_stats_percpu;/* memory.stat */ struct memcg_vmstats *vmstats;
mm/memcontrol.c +40 -48
199 lines not on this page · raw message
Signature · 1 line
2.52.0
Shakeel Butt
maintainerQuoted 15 lines · Joanne Koong
Every memcg lruvec (per-node) stat update reads the memcg and
lruvec_stats_percpupointers in the first 64 bytes of structmem_cgroup_per_node, and the ones that go throughmod_lruvec_state()(such aslruvec_stat_mod_folio()) also readlruvec->pgdat. These updates are extremely frequent (eg these updates happen several times for every page that's faulted in, freed, dirtied, or written back).The
memcg_pn_read_mostlycacheline group keeps those read-mostly fields on a different cacheline from the lruvec, but on CPUs that do spatial prefetching [1] (which is most Intel CPUs / modern CPUs), cachelines are fetched in 128-byte aligned pairs. Depending on where structmem_cgroup_per_nodelands in its slab, that cacheline can be paired with the cacheline holding the LRU list heads, and thelruvec->pgdatcacheline with the one holdinglru_lock.
Quoted 9 lines
When this happens, every stat update also pulls in a copy of the LRU list heads and
lru_lockcachelines, which are written to constantly by other CPUs under thelru_lock. The stat update has to fetch them from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads orlru_lockat all. But even worse, thelru_lockholder then has to invalidate those copies and wait for the acknowledgements to come back before it can do its next write, which makes thelru_lockcritical section longer for everyone waiting on the lock.
Can you please reproduce on a non-intel machine?
Second, can you try the following patch instead of your on the intel machine?
From a747ceeb4e3f10fb0c3e7daf5660076a49b77126 Mon Sep 17 00:00:00 2001
From: Shakeel Butt <[email protected]>
Date: Tue, 20 Oct 2026 15:33:26 -0700
Subject: [PATCH] memcg: rearrage mem_cgroup_per_node
--- include/linux/memcontrol.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
include/linux/memcontrol.h +11 -11
@@ -95,6 +95,17 @@ struct mem_cgroup_per_node {__cacheline_group_end_aligned (memcg_pn_read_mostly); +/* Touched only when the memcg is reparented or freed. */ +__cacheline_group_begin_aligned (memcg_pn_cold); +/* +* orig_objcg preserves a pointer (and a reference) to the original +* objcg until the end of life of memcg. +*/ +struct obj_cgroup *orig_objcg; +/* list of inherited objcgs, protected by objcg_lock */ +struct list_head objcg_list; +__cacheline_group_end_aligned (memcg_pn_cold); +/* * Keep lruvec on its own lines. Sharing them with lru_zone_size[] * regressed, see commit f59adcf59332 ("mm: memcg: add cacheline
@@ -114,17 +125,6 @@ struct mem_cgroup_per_node {atomic_t slab_unreclaimable;#endif __cacheline_group_end_aligned (memcg_pn_write_hot); - -/* Touched only when the memcg is reparented or freed. */ -__cacheline_group_begin_aligned (memcg_pn_cold); -/* -* orig_objcg preserves a pointer (and a reference) to the original -* objcg until the end of life of memcg. -*/ -struct obj_cgroup *orig_objcg; -/* list of inherited objcgs, protected by objcg_lock */ -struct list_head objcg_list; -__cacheline_group_end_aligned (memcg_pn_cold); };struct mem_cgroup_threshold {
Signature · 1 line
2.53.0-Meta
Shakeel Butt
maintainerQuoted 26 lines · Shakeel Butt
Quoted 15 lines · Joanne Koong
Every memcg lruvec (per-node) stat update reads the memcg and
lruvec_stats_percpupointers in the first 64 bytes of structmem_cgroup_per_node, and the ones that go throughmod_lruvec_state()(such aslruvec_stat_mod_folio()) also readlruvec->pgdat. These updates are extremely frequent (eg these updates happen several times for every page that's faulted in, freed, dirtied, or written back).The
memcg_pn_read_mostlycacheline group keeps those read-mostly fields on a different cacheline from the lruvec, but on CPUs that do spatial prefetching [1] (which is most Intel CPUs / modern CPUs), cachelines are fetched in 128-byte aligned pairs. Depending on where structmem_cgroup_per_nodelands in its slab, that cacheline can be paired with the cacheline holding the LRU list heads, and thelruvec->pgdatcacheline with the one holdinglru_lock.Quoted 9 lines
When this happens, every stat update also pulls in a copy of the LRU list heads and
lru_lockcachelines, which are written to constantly by other CPUs under thelru_lock. The stat update has to fetch them from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads orlru_lockat all. But even worse, thelru_lockholder then has to invalidate those copies and wait for the acknowledgements to come back before it can do its next write, which makes thelru_lockcritical section longer for everyone waiting on the lock.
Quoted 4 lines
Can you please reproduce on a non-intel machine?
Second, can you try the following patch instead of your on the intel machine?
I see there is also removal of access of lruvec->pgdat from stats update function. Let's break up the patch into two. One will remove lruvec->pgdat access and the other one rearrange the fields in mem_cgroup_per_node (I prefer rearraging than removing the lruvec stat pointer).
Quoted 28 lines · Shakeel Butt
Quoted 26 lines · Shakeel Butt
Quoted 24 lines · Joanne Koong
Every memcg lruvec (per-node) stat update reads the memcg and
lruvec_stats_percpupointers in the first 64 bytes of structmem_cgroup_per_node, and the ones that go throughmod_lruvec_state()(such aslruvec_stat_mod_folio()) also readlruvec->pgdat. These updates are extremely frequent (eg these updates happen several times for every page that's faulted in, freed, dirtied, or written back).The
memcg_pn_read_mostlycacheline group keeps those read-mostly fields on a different cacheline from the lruvec, but on CPUs that do spatial prefetching [1] (which is most Intel CPUs / modern CPUs), cachelines are fetched in 128-byte aligned pairs. Depending on where structmem_cgroup_per_nodelands in its slab, that cacheline can be paired with the cacheline holding the LRU list heads, and thelruvec->pgdatcacheline with the one holdinglru_lock.When this happens, every stat update also pulls in a copy of the LRU list heads and
lru_lockcachelines, which are written to constantly by other CPUs under thelru_lock. The stat update has to fetch them from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads orlru_lockat all. But even worse, thelru_lockholder then has to invalidate those copies and wait for the acknowledgements to come back before it can do its next write, which makes thelru_lockcritical section longer for everyone waiting on the lock.
Quoted 1 line
Quoted 1 line
Can you please reproduce on a non-intel machine?
I ran this on an arm64 machine but the results don't reproduce, so this looks specific to intel's prefetcher.
Quoted 9 lines
Quoted 3 lines
Second, can you try the following patch instead of your on the intel machine?
I see there is also removal of access of
lruvec->pgdatfrom stats update function. Let's break up the patch into two. One will removelruvec->pgdataccess and the other one rearrange the fields inmem_cgroup_per_node(I prefer rearraging than removing the lruvec stat pointer).
Sounds good, I'll go the layout rearranging route then. I verified on the Intel machine that rearranging + the pgdat change gives the same results as this v1. Do you want me to include your rearrange patch in v2 (with your authorship and signed-off-by) or do you prefer to post it yourself and I'll send the pgdat change on top?
Thanks,
Joanne
Shakeel Butt
maintainerQuoted 2 lines · Joanne Koong
Quoted 1 line · Shakeel Butt
[...]
Quoted 6 lines
Sounds good, I'll go the layout rearranging route then. I verified on the Intel machine that rearranging + the pgdat change gives the same results as this v1. Do you want me to include your rearrange patch in v2 (with your authorship and signed-off-by) or do you prefer to post it yourself and I'll send the pgdat change on top?
You can take the patch and the ownership/authorship as well and post both patches.
Shakeel