memcg: reduce cacheline contention from lruvec stat
Subsystems (3)
v3 cover memcg: reduce cacheline contention from lruvec stat
Memcg lruvec stat updates read fields of struct mem_cgroup_per_node and struct lruvec that can share a 128-byte cacheline pair with lines the lru_lock holder writes constantly (the LRU list heads, lru_lock or lru_zone_size[]), depending on where struct mem_cgroup_per_node lands in its slab. On CPUs that do spatial prefetching (eg Intel CPUs), this makes the stat updates contend with the lru_lock holder.
Patch 1 rearranges struct mem_cgroup_per_node so that its read-mostly line is paired with a cold line, and patch 2 makes the folio and kmem stat updates use the memcg and node they already know instead of going through the lruvec, whose pgdat field shares a cacheline pair with lru_lock or lru_zone_size[].
Results on a 2-socket Skylake (80 CPUs), average pages per second for threads faulting in and freeing memory in a single memcg:
base patch 1 patch 1+2
shmem, 20 threads 4.74M 5.59M 5.77M shmem, 80 threads 7.94M 8.66M 8.82M anon, 20 threads 3.04M 3.08M 3.10M
Changelog
---------
v2: https://lore.kernel.org/linux-mm/[email protected]/
Changes from v2 -> v3:
- Add in a comment about memcg_pn_cold placement after memcg_pn_read_mostly (Shakeel)
- Add Shakeel's Acked-bys
v1: https://lore.kernel.org/linux-mm/[email protected]/
Changes from v1 -> v2:
- Replace the lruvec_stats_percpu pointers in struct mem_cgroup with a rearranged struct mem_cgroup_per_node layout (Shakeel)
- Move lruvec_stat_mod_folio()/mod_lruvec_kmem_state() change into its own patch (Shakeel)
Joanne Koong (2):
memcg: reduce cacheline contention between lruvec stat updates and LRU
lists
memcg: don't go through the lruvec for folio and kmem lruvec stat
updates
include/linux/memcontrol.h | 29 ++++++++++------ mm/memcontrol.c | 71 ++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 53 deletions(-)
base-commit: fcec4f4bcc1101dbd7e3aff267856b3bbfe128c6
Signature · 1 line
2.52.0
PATCH v3 1/2 memcg: reduce 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. 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 Intel CPUs do spatial prefetching [1] and fetch cachelines 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.
When this happens, every stat update also pulls in a copy of the LRU list heads cacheline, which is written to constantly by other CPUs under the lru_lock. The stat update has to fetch it from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads 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 moving the memcg_pn_cold group, which is only touched when the memcg is reparented or freed, right after memcg_pn_read_mostly, so that the read-mostly cacheline is paired with the cold cacheline instead of the LRU list heads.
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.59M (+18%) shmem, 80 threads 7.94M 8.66M (+9%) anon, 20 threads 3.04M 3.08M (+1%)
[1] https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf
(bottom of page 62/800)
--- include/linux/memcontrol.h | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)
include/linux/memcontrol.h +18 -11
@@ -95,6 +95,24 @@ struct mem_cgroup_per_node {__cacheline_group_end_aligned (memcg_pn_read_mostly); +/* +* Touched only when the memcg is reparented or freed. Placed right +* after memcg_pn_read_mostly because CPUs with spatial prefetching +* (eg most Intel CPUs) fetch cachelines in 128-byte aligned pairs. +* This keeps the read-mostly line, which every lruvec stat update +* reads, from being paired with the LRU list heads, which are written +* constantly under lru_lock. +*/ +__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 +132,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.52.0
Re: [PATCH v3 1/2] memcg: reduce cacheline contention between lruvec stat updates and LRU lists
Quoted 45 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. 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 Intel CPUs do spatial prefetching [1] and fetch cachelines 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.When this happens, every stat update also pulls in a copy of the LRU list heads cacheline, which is written to constantly by other CPUs under the
lru_lock. The stat update has to fetch it from another CPU, which is unnecessary overhead since the stat update doesn't use the LRU list heads at 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.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_nodeas the most contended cacheline (over a third of all HITMs and 5x more than thelru_lockcacheline) 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 moving the
memcg_pn_coldgroup, which is only touched when the memcg is reparented or freed, right aftermemcg_pn_read_mostly, so that the read-mostly cacheline is paired with the cold cacheline instead of the LRU list heads.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.59M (+18%) shmem, 80 threads 7.94M 8.66M (+9%) anon, 20 threads 3.04M 3.08M (+1%)
Quoted 7 lines
[1] https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf
(bottom of page 62/800)
Thanks!
Quoted 53 lines
--- include/linux/memcontrol.h | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)include/linux/memcontrol.h +18
-11@@ -95,6 +95,24 @@ struct mem_cgroup_per_node {__cacheline_group_end_aligned (memcg_pn_read_mostly); +/* +* Touched only when the memcg is reparented or freed. Placed right +* after memcg_pn_read_mostly because CPUs with spatial prefetching +* (eg most Intel CPUs) fetch cachelines in 128-byte aligned pairs. +* This keeps the read-mostly line, which every lruvec stat update +* reads, from being paired with the LRU list heads, which are written +* constantly under lru_lock. +*/ +__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 +132,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.52.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v3 2/2 memcg: don't go through the lruvec for folio and kmem lruvec stat updates
In reply to Joanne Koonglruvec_stat_mod_folio() and mod_lruvec_kmem_state() already know the memcg and the node, but look up the lruvec with mem_cgroup_lruvec() and update the stats through mod_lruvec_state(), which reads lruvec->pgdat and mem_cgroup_per_node->memcg to get them back. lruvec->pgdat shares a 128-byte cacheline pair with lines that the lru_lock holder writes constantly (lru_lock or lru_zone_size[], depending on the alignment of struct mem_cgroup_per_node), so on CPUs that do spatial prefetching these per-folio stat updates contend with the lru_lock holder.
Pass the memcg and node they already know to mod_memcg_node_state() instead.
On top of the mem_cgroup_per_node rearrangement, on a 2-socket Skylake (80 CPUs) with threads faulting in and freeing shmem in a single memcg, this improves throughput by about 3% at 20 threads (5.59M -> 5.77M pages/sec) and 2% at 80 threads (8.66M -> 8.82M).
--- mm/memcontrol.c | 71 ++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 42 deletions(-)
mm/memcontrol.c +29 -42
@@ -587,7 +587,7 @@ unsigned long lruvec_page_state_local(const struct lruvec *lruvec,return x; } -static void __mod_memcg_lruvec_state (struct mem_cgroup_per_node *pn, +static void __mod_memcg_lruvec_state (struct mem_cgroup *memcg,int nid,enum node_stat_item idx,long val); void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,
@@ -596,16 +596,11 @@ void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,int nid;for_each_node (nid) { -struct lruvec *child_lruvec =mem_cgroup_lruvec (memcg,NODE_DATA (nid)); -struct lruvec *parent_lruvec =mem_cgroup_lruvec (parent,NODE_DATA (nid)); -unsigned long value =lruvec_page_state_local (child_lruvec, idx); -struct mem_cgroup_per_node *child_pn, *parent_pn; +struct lruvec *lruvec =mem_cgroup_lruvec (memcg,NODE_DATA (nid)); +unsigned long value =lruvec_page_state_local (lruvec, idx); - child_pn = container_of(child_lruvec, struct mem_cgroup_per_node, lruvec); - parent_pn = container_of(parent_lruvec, struct mem_cgroup_per_node, lruvec); - -__mod_memcg_lruvec_state (child_pn, idx, -value); -__mod_memcg_lruvec_state (parent_pn, idx, value); +__mod_memcg_lruvec_state (memcg, nid, idx, -value); +__mod_memcg_lruvec_state (parent, nid, idx, value); } }
@@ -990,10 +985,10 @@ void reparent_memcg_state_local(struct mem_cgroup *memcg, }#endif -static void __mod_memcg_lruvec_state (struct mem_cgroup_per_node *pn, +static void __mod_memcg_lruvec_state (struct mem_cgroup *memcg,int nid,enum node_stat_item idx,long val) { -struct mem_cgroup *memcg = pn->memcg; +struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];int i =memcg_stats_index (idx);int cpu;
@@ -1015,21 +1010,23 @@ static void __mod_memcg_lruvec_state(struct mem_cgroup_per_node *pn,put_cpu (); } +static void mod_memcg_node_state (struct mem_cgroup *memcg,int nid, +enum node_stat_item idx,int val) +{ +bool rcu_locked = false; + + memcg =get_non_dying_memcg_start (memcg, &rcu_locked); +__mod_memcg_lruvec_state (memcg, nid, idx, val); +get_non_dying_memcg_end (rcu_locked); +} +void mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val) { -struct pglist_data *pgdat =lruvec_pgdat (lruvec);struct mem_cgroup_per_node *pn; -struct mem_cgroup *memcg; -bool rcu_locked = false; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - memcg =get_non_dying_memcg_start (pn->memcg, &rcu_locked); - pn = memcg->nodeinfo[pgdat->node_id]; - -__mod_memcg_lruvec_state (pn, idx, val); - -get_non_dying_memcg_end (rcu_locked); +mod_memcg_node_state (pn->memcg,lruvec_pgdat (lruvec)->node_id, idx, val); }/**
@@ -1058,19 +1055,13 @@ void lruvec_stat_mod_folio(struct folio *folio, enum node_stat_item idx, {struct mem_cgroup *memcg;pg_data_t *pgdat =folio_pgdat (folio); -struct lruvec *lruvec; + +mod_node_page_state (pgdat, idx, val);rcu_read_lock (); memcg =folio_memcg (folio); -/* Untracked pages have no memcg, no lruvec. Update only the node */ -if (!memcg) { -rcu_read_unlock (); -mod_node_page_state (pgdat, idx, val); -return ; - } - - lruvec =mem_cgroup_lruvec (memcg, pgdat); -mod_lruvec_state (lruvec, idx, val); +if (memcg) +mod_memcg_node_state (memcg, pgdat->node_id, idx, val);rcu_read_unlock (); }EXPORT_SYMBOL (lruvec_stat_mod_folio);
@@ -1079,23 +1070,19 @@ void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) {pg_data_t *pgdat =page_pgdat (virt_to_page (p));struct mem_cgroup *memcg; -struct lruvec *lruvec; + +mod_node_page_state (pgdat, idx, val);rcu_read_lock (); memcg =mem_cgroup_from_virt (p);/* -* Untracked pages have no memcg, no lruvec. Update only the -* node. If we reparent the slab objects to the root memcg, -* when we free the slab object, we need to update the per-memcg -* vmstats to keep it correct for the root memcg. +* If we reparent the slab objects to the root memcg, when we free the +* slab object, we need to update the per-memcg vmstats to keep it +* correct for the root memcg. */ -if (!memcg) { -mod_node_page_state (pgdat, idx, val); - }else { - lruvec =mem_cgroup_lruvec (memcg, pgdat); -mod_lruvec_state (lruvec, idx, val); - } +if (memcg) +mod_memcg_node_state (memcg, pgdat->node_id, idx, val);rcu_read_unlock (); }
Signature · 1 line
2.52.0
Re: [PATCH v3 2/2] memcg: don't go through the lruvec for folio and kmem lruvec stat updates
Quoted 12 lines · Joanne Koong
lruvec_stat_mod_folio()andmod_lruvec_kmem_state()already know the memcg and the node, but look up the lruvec withmem_cgroup_lruvec()and update the stats throughmod_lruvec_state(), which readslruvec->pgdatandmem_cgroup_per_node->memcgto get them back.lruvec->pgdatshares a 128-byte cacheline pair with lines that thelru_lockholder writes constantly (lru_lockor lru_zone_size[], depending on the alignment of structmem_cgroup_per_node), so on CPUs that do spatial prefetching these per-folio stat updates contend with thelru_lockholder.Pass the memcg and node they already know to
mod_memcg_node_state()instead.
Quoted 7 lines
On top of the
mem_cgroup_per_noderearrangement, on a 2-socket Skylake (80 CPUs) with threads faulting in and freeing shmem in a single memcg, this improves throughput by about 3% at 20 threads (5.59M -> 5.77M pages/sec) and 2% at 80 threads (8.66M -> 8.82M).Acked-by Shakeel Butt <[email protected]>Signed-off-by Joanne Koong <[email protected]>
Thanks!
Quoted 141 lines
--- mm/memcontrol.c | 71 ++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 42 deletions(-)mm/memcontrol.c +29
-42@@ -587,7 +587,7 @@ unsigned long lruvec_page_state_local(const struct lruvec *lruvec,return x; } -static void __mod_memcg_lruvec_state (struct mem_cgroup_per_node *pn, +static void __mod_memcg_lruvec_state (struct mem_cgroup *memcg,int nid,enum node_stat_item idx,long val); void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,@@ -596,16 +596,11 @@ void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg,int nid;for_each_node (nid) { -struct lruvec *child_lruvec =mem_cgroup_lruvec (memcg,NODE_DATA (nid)); -struct lruvec *parent_lruvec =mem_cgroup_lruvec (parent,NODE_DATA (nid)); -unsigned long value =lruvec_page_state_local (child_lruvec, idx); -struct mem_cgroup_per_node *child_pn, *parent_pn; +struct lruvec *lruvec =mem_cgroup_lruvec (memcg,NODE_DATA (nid)); +unsigned long value =lruvec_page_state_local (lruvec, idx); - child_pn = container_of(child_lruvec, struct mem_cgroup_per_node, lruvec); - parent_pn = container_of(parent_lruvec, struct mem_cgroup_per_node, lruvec); - -__mod_memcg_lruvec_state (child_pn, idx, -value); -__mod_memcg_lruvec_state (parent_pn, idx, value); +__mod_memcg_lruvec_state (memcg, nid, idx, -value); +__mod_memcg_lruvec_state (parent, nid, idx, value); } }@@ -990,10 +985,10 @@ void reparent_memcg_state_local(struct mem_cgroup *memcg, }#endif -static void __mod_memcg_lruvec_state (struct mem_cgroup_per_node *pn, +static void __mod_memcg_lruvec_state (struct mem_cgroup *memcg,int nid,enum node_stat_item idx,long val) { -struct mem_cgroup *memcg = pn->memcg; +struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];int i =memcg_stats_index (idx);int cpu;@@ -1015,21 +1010,23 @@ static void __mod_memcg_lruvec_state(struct mem_cgroup_per_node *pn,put_cpu (); } +static void mod_memcg_node_state (struct mem_cgroup *memcg,int nid, +enum node_stat_item idx,int val) +{ +bool rcu_locked = false; + + memcg =get_non_dying_memcg_start (memcg, &rcu_locked); +__mod_memcg_lruvec_state (memcg, nid, idx, val); +get_non_dying_memcg_end (rcu_locked); +} +void mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val) { -struct pglist_data *pgdat =lruvec_pgdat (lruvec);struct mem_cgroup_per_node *pn; -struct mem_cgroup *memcg; -bool rcu_locked = false; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - memcg =get_non_dying_memcg_start (pn->memcg, &rcu_locked); - pn = memcg->nodeinfo[pgdat->node_id]; - -__mod_memcg_lruvec_state (pn, idx, val); - -get_non_dying_memcg_end (rcu_locked); +mod_memcg_node_state (pn->memcg,lruvec_pgdat (lruvec)->node_id, idx, val); }/** @@ -1058,19 +1055,13 @@ void lruvec_stat_mod_folio(struct folio *folio, enum node_stat_item idx, {struct mem_cgroup *memcg;pg_data_t *pgdat =folio_pgdat (folio); -struct lruvec *lruvec; + +mod_node_page_state (pgdat, idx, val);rcu_read_lock (); memcg =folio_memcg (folio); -/* Untracked pages have no memcg, no lruvec. Update only the node */ -if (!memcg) { -rcu_read_unlock (); -mod_node_page_state (pgdat, idx, val); -return ; - } - - lruvec =mem_cgroup_lruvec (memcg, pgdat); -mod_lruvec_state (lruvec, idx, val); +if (memcg) +mod_memcg_node_state (memcg, pgdat->node_id, idx, val);rcu_read_unlock (); }EXPORT_SYMBOL (lruvec_stat_mod_folio);@@ -1079,23 +1070,19 @@ void mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) {pg_data_t *pgdat =page_pgdat (virt_to_page (p));struct mem_cgroup *memcg; -struct lruvec *lruvec; + +mod_node_page_state (pgdat, idx, val);rcu_read_lock (); memcg =mem_cgroup_from_virt (p);/* -* Untracked pages have no memcg, no lruvec. Update only the -* node. If we reparent the slab objects to the root memcg, -* when we free the slab object, we need to update the per-memcg -* vmstats to keep it correct for the root memcg. +* If we reparent the slab objects to the root memcg, when we free the +* slab object, we need to update the per-memcg vmstats to keep it +* correct for the root memcg. */ -if (!memcg) { -mod_node_page_state (pgdat, idx, val); - }else { - lruvec =mem_cgroup_lruvec (memcg, pgdat); -mod_lruvec_state (lruvec, idx, val); - } +if (memcg) +mod_memcg_node_state (memcg, pgdat->node_id, idx, val);rcu_read_unlock (); }Signature · 1 line
2.52.0
Signature · 2 lines
Michal Hocko SUSE Labs
Andrew Morton
maintainerQuoted 12 lines · On Fri, 23 Oct 2026 12:54:15 -0700 Joanne Koong <[email protected]> wrote:
Memcg lruvec stat updates read fields of struct
mem_cgroup_per_nodeand struct lruvec that can share a 128-byte cacheline pair with lines thelru_lockholder writes constantly (the LRU list heads,lru_lockor lru_zone_size[]), depending on where structmem_cgroup_per_nodelands in its slab. On CPUs that do spatial prefetching (eg Intel CPUs), this makes the stat updates contend with thelru_lockholder.Patch 1 rearranges struct
mem_cgroup_per_nodeso that its read-mostly line is paired with a cold line, and patch 2 makes the folio and kmem stat updates use the memcg and node they already know instead of going through the lruvec, whose pgdat field shares a cacheline pair withlru_lockor lru_zone_size[].
Quoted 7 lines
Results on a 2-socket Skylake (80 CPUs), average pages per second for threads faulting in and freeing memory in a single memcg:
base patch 1 patch 1+2
shmem, 20 threads 4.74M 5.59M 5.77M shmem, 80 threads 7.94M 8.66M 8.82M anon, 20 threads 3.04M 3.08M 3.10M
Thanks. A local AI review pointed out a thing:
One limitation worth mentioning in the [1/2] changelog: the slab uses SLAB_HWCACHE_ALIGN, which need not align each object to a 128-byte boundary. If an object starts 64 bytes into such a pair, its first line can still be paired with the end of the preceding object rather than its own cold line. This isn't a correctness issue, and the measured Skylake improvement may well be real, but the layout doesn't guarantee that every read-mostly line pairs with a cold line.
Quoted 25 lines · Andrew Morton
Quoted 12 lines · On Fri, 23 Oct 2026 12:54:15 -0700 Joanne Koong <[email protected]> wrote:
Memcg lruvec stat updates read fields of struct
mem_cgroup_per_nodeand struct lruvec that can share a 128-byte cacheline pair with lines thelru_lockholder writes constantly (the LRU list heads,lru_lockor lru_zone_size[]), depending on where structmem_cgroup_per_nodelands in its slab. On CPUs that do spatial prefetching (eg Intel CPUs), this makes the stat updates contend with thelru_lockholder.Patch 1 rearranges struct
mem_cgroup_per_nodeso that its read-mostly line is paired with a cold line, and patch 2 makes the folio and kmem stat updates use the memcg and node they already know instead of going through the lruvec, whose pgdat field shares a cacheline pair withlru_lockor lru_zone_size[].Quoted 7 lines
Results on a 2-socket Skylake (80 CPUs), average pages per second for threads faulting in and freeing memory in a single memcg:
base patch 1 patch 1+2 shmem, 20 threads 4.74M 5.59M 5.77M shmem, 80 threads 7.94M 8.66M 8.82M anon, 20 threads 3.04M 3.08M 3.10MThanks. A local AI review pointed out a thing:
Quoted 8 lines
One limitation worth mentioning in the [1/2] changelog: the slab uses
SLAB_HWCACHE_ALIGN, which need not align each object to a 128-byte boundary. If an object starts 64 bytes into such a pair, its first line can still be paired with the end of the preceding object rather than its own cold line. This isn't a correctness issue, and the measured Skylake improvement may well be real, but the layout doesn't guarantee that every read-mostly line pairs with a cold line.
Hi Andrew,
Thanks for taking a look at this series!
The AI review's commentary is correct but it doesn't seem to matter in practice. In the cases where the read-mostly line isn't paired with the cold line, it's paired with the line that holds the end of lru_zone_size[] and the reclaim iterator, which are also rarely written outside of reclaim so it doesn't see much contention. In my benchmarking runs, the memcgs are freshly created and contain a mix of both alignments. Before the changes in this series, the per-memcg throughput was bimodal depending on the alignment, but afterwards, the throughput is uniform across memcgs.
Here's an updated version of the "Fix this by moving the memcg_pn_cold group..." paragraph in patch 1 that mentions this:
Fix this by moving the memcg_pn_cold group, which is only touched when the memcg is reparented or freed, right after memcg_pn_read_mostly. SLAB_HWCACHE_ALIGN only aligns objects to a cacheline, and the struct is 576 bytes, so depending on where an object lands, its read-mostly cacheline is now paired either with its cold cacheline or with the last cacheline of the preceding object, which holds the end of lru_zone_size[] and the reclaim iterator. Both are rarely written outside of reclaim, so either way the read-mostly cacheline is no longer paired with the LRU list heads.
Would you be able to swap it in or would you prefer that I send a v4?
Thanks,
Joanne
Andrew Morton
maintainerQuoted 13 lines · On Tue, 27 Oct 2026 15:57:43 -0700 Joanne Koong <[email protected]> wrote:
Here's an updated version of the "Fix this by moving the
memcg_pn_coldgroup..." paragraph in patch 1 that mentions this:Fix this by moving the
memcg_pn_coldgroup, which is only touched when the memcg is reparented or freed, right aftermemcg_pn_read_mostly.SLAB_HWCACHE_ALIGNonly aligns objects to a cacheline, and the struct is 576 bytes, so depending on where an object lands, its read-mostly cacheline is now paired either with its cold cacheline or with the last cacheline of the preceding object, which holds the end of lru_zone_size[] and the reclaim iterator. Both are rarely written outside of reclaim, so either way the read-mostly cacheline is no longer paired with the LRU list heads.
Quoted 1 line
Would you be able to swap it in or would you prefer that I send a v4?
Thanks. I'll take care of this when I process the patchset. Which will be quite soon now we're all merged up.
Quoted 16 lines · Andrew Morton
Quoted 13 lines · On Tue, 27 Oct 2026 15:57:43 -0700 Joanne Koong <[email protected]> wrote:
Here's an updated version of the "Fix this by moving the
memcg_pn_coldgroup..." paragraph in patch 1 that mentions this:Fix this by moving the
memcg_pn_coldgroup, which is only touched when the memcg is reparented or freed, right aftermemcg_pn_read_mostly.SLAB_HWCACHE_ALIGNonly aligns objects to a cacheline, and the struct is 576 bytes, so depending on where an object lands, its read-mostly cacheline is now paired either with its cold cacheline or with the last cacheline of the preceding object, which holds the end of lru_zone_size[] and the reclaim iterator. Both are rarely written outside of reclaim, so either way the read-mostly cacheline is no longer paired with the LRU list heads.
Quoted 4 lines
Quoted 1 line
Would you be able to swap it in or would you prefer that I send a v4?
Thanks. I'll take care of this when I process the patchset. Which will be quite soon now we're all merged up.
Sounds great. Thanks, Andrew!