[PATCH 0/6] memcg: group struct fields by access pattern

Shakeel Butt posted 6 patches 2 weeks, 6 days ago
include/linux/memcontrol.h | 169 ++++++++++++++++++++++---------------
mm/memcontrol.c            | 122 ++++++++++++++++++++++++--
2 files changed, 213 insertions(+), 78 deletions(-)
[PATCH 0/6] memcg: group struct fields by access pattern
Posted by Shakeel Butt 2 weeks, 6 days ago
Every so often we get a memcg performance regression caused by nothing
more than a field moving. Someone adds a field, removes one, or puts a
few behind a config option. The layout shifts, fields with different
access patterns land on the same cache line, and a bot reports a
regression.

Two examples. commit 98c9daf5ae6b ("mm: memcg: guard memcg1-specific
members of struct mem_cgroup_per_node") moved lruvec next to
lru_zone_size[] and needed commit f59adcf59332 ("mm: memcg: add
cacheline padding after lruvec in mem_cgroup_per_node") to fix it.
commit c1afbd5de131 ("mm/memcontrol: avoid false sharing between
vmstats and events") had to add ____cacheline_aligned_in_smp for the
same reason.

Each fix was correct but nothing stops the next field addition
from undoing it.

This series makes the layout a contract the compiler checks, the same
way struct net_device does it. Fields are sorted into named cache line
groups by access pattern, and memcg_struct_check() verifies at build
time that every field sits in its group. A field added in the wrong
place now breaks the build instead of quietly costing a few percent.

struct mem_cgroup gets three groups:

  memcg_write_hot     written on the charge, reclaim and socket paths
  memcg_cold          only the cgroup control paths touch these
  memcg_read_mostly   set when the memcg is created, then only read

Testing
=======

The cgroup selftests give identical results with and without the series.

For performance, two identical 30 core Xeon machines each ran both
kernels, with the boot order swapped between them so that machine and
order effects cancel. The useful tests run two workloads at once in one
cgroup, because false sharing only shows up when one side reads a field
that the other side writes.

  slab allocs + page faults, slab side           +1.2%
  page faults + memory.stat readers, fault side  +1.3%
  page faults + memory.stat readers, reader side +0.8%
  everything else                                no change

No test regressed. The gains are small but the point of the series is
the build time contract.

Shakeel Butt (6):
  memcg: move per-node objcg to the read-mostly fields
  memcg: split mem_cgroup_private_id into two fields
  memcg: group the write-hot fields of struct mem_cgroup
  memcg: group the cold fields of struct mem_cgroup
  memcg: group the read-mostly fields of struct mem_cgroup
  memcg: group the fields of struct mem_cgroup_per_node

 include/linux/memcontrol.h | 169 ++++++++++++++++++++++---------------
 mm/memcontrol.c            | 122 ++++++++++++++++++++++++--
 2 files changed, 213 insertions(+), 78 deletions(-)


base-commit: 817d340204c513316223ba084615f5906ac49ddb
-- 
2.53.0-Meta
Re: [PATCH 0/6] memcg: group struct fields by access pattern
Posted by Andrew Morton 2 weeks, 6 days ago
On Fri,  4 Sep 2026 20:05:16 -0700 Shakeel Butt <shakeel.butt@linux.dev> wrote:

> Every so often we get a memcg performance regression caused by nothing
> more than a field moving. Someone adds a field, removes one, or puts a
> few behind a config option. The layout shifts, fields with different
> access patterns land on the same cache line, and a bot reports a
> regression.
> 
> ...
>
> This series makes the layout a contract the compiler checks, the same
> way struct net_device does it. Fields are sorted into named cache line
> groups by access pattern, and memcg_struct_check() verifies at build
> time that every field sits in its group. A field added in the wrong
> place now breaks the build instead of quietly costing a few percent.

Sounds smart.

Significant repair work was needed for the struct mem_cgroup_per_node
and struct mem_cgroup alterations, due to the below pending changes.  I
think I got it all, please check.

--- linux-7.3-rc1/include/linux/memcontrol.h	2026-08-30 04:44:06.000000000 -0700
+++ 25/include/linux/memcontrol.h	2026-09-05 16:31:03.119571612 -0700
@@ -95,25 +95,12 @@ struct mem_cgroup_per_node {
 	struct lruvec_stats			*lruvec_stats;
 	struct shrinker_info __rcu	*shrinker_info;
 
-#ifdef CONFIG_MEMCG_V1
-	/*
-	 * Memcg-v1 only stuff in middle as buffer between read mostly fields
-	 * and update often fields to avoid false sharing. If v1 stuff is
-	 * not present, an explicit padding is needed.
-	 */
-
-	struct rb_node		tree_node;	/* RB tree node */
-	unsigned long		usage_in_excess;/* Set to the value by which */
-						/* the soft limit is exceeded*/
-	bool			on_tree;
-#else
 	CACHELINE_PADDING(_pad1_);
-#endif
 
 	/* Fields which get updated often at the end. */
 	struct lruvec		lruvec;
 	CACHELINE_PADDING(_pad2_);
-	unsigned long		lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
+	long			lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
 	struct mem_cgroup_reclaim_iter	iter;
 
 	/*
@@ -293,8 +280,6 @@ struct mem_cgroup {
 
 	struct memcg1_events_percpu __percpu *events_percpu;
 
-	unsigned long soft_limit;
-
 	/* protected by memcg_oom_lock */
 	bool oom_lock;
 	int under_oom;