Skip to contentSkip to conversation

mm/memcontrol: hoist pstatc_pcpu assignment out of CPU loop

PATCH Applied Hui Zhu 4 participants 4 messages Apr 29 – Apr 30

Subsystems (3)
lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH 1/1 mm/memcontrol: hoist pstatc_pcpu assignment out of CPU loop

From: Hui Zhu <[email protected]>

In mem_cgroup_alloc(), the assignment of pstatc_pcpu is invariant with respect to the for_each_possible_cpu() loop: both the 'parent' pointer and 'parent->vmstats_percpu' remain constant throughout all iterations.

The original code redundantly re-evaluated the 'if (parent)' condition and reassigned pstatc_pcpu on every CPU iteration, then repeated the same ternary check 'parent ? pstatc_pcpu : NULL' when storing into statc->parent_pcpu.

Move the single conditional assignment of pstatc_pcpu to before the loop, resolving both the loop-invariant placement issue and the duplicated null check. On systems with a large number of possible CPUs, this eliminates repeated branch evaluation with no functional change.

No functional change intended.

---
 mm/memcontrol.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/memcontrol.c +2 -3
index c3d98ab41f1f..4f4a60e57a08 100644
@@ -3993,11 +3993,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
 	if (!memcg1_alloc_events(memcg))
 		goto fail;
 
+	pstatc_pcpu = parent ? parent->vmstats_percpu : NULL;
 	for_each_possible_cpu(cpu) {
-		if (parent)
-			pstatc_pcpu = parent->vmstats_percpu;
 		statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
-		statc->parent_pcpu = parent ? pstatc_pcpu : NULL;
+		statc->parent_pcpu = pstatc_pcpu;
 		statc->vmstats = memcg->vmstats;
 	}
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 27 lines · On Wed, 29 Apr 2026 16:42:16 +0800 Hui Zhu <[email protected]> wrote:

From: Hui Zhu <[email protected]>

In mem_cgroup_alloc(), the assignment of pstatc_pcpu is invariant with respect to the for_each_possible_cpu() loop: both the 'parent' pointer and 'parent->vmstats_percpu' remain constant throughout all iterations.

The original code redundantly re-evaluated the 'if (parent)' condition and reassigned pstatc_pcpu on every CPU iteration, then repeated the same ternary check 'parent ? pstatc_pcpu : NULL' when storing into statc->parent_pcpu.

Move the single conditional assignment of pstatc_pcpu to before the loop, resolving both the loop-invariant placement issue and the duplicated null check. On systems with a large number of possible CPUs, this eliminates repeated branch evaluation with no functional change.

No functional change intended.

...

mm/memcontrol.c +0 -0
@@ -3993,11 +3993,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
 	if (!memcg1_alloc_events(memcg))
 		goto fail;

mm/memcontrol.c static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent) · line 4001

Quoted 10 lines
mm/memcontrol.c +2 -3
 
+	pstatc_pcpu = parent ? parent->vmstats_percpu : NULL;
 	for_each_possible_cpu(cpu) {
-		if (parent)
-			pstatc_pcpu = parent->vmstats_percpu;
 		statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
-		statc->parent_pcpu = parent ? pstatc_pcpu : NULL;
+		statc->parent_pcpu = pstatc_pcpu;
 		statc->vmstats = memcg->vmstats;
 	}

lgtm.

I expected this to make no change to generated code but it actually reduces memcontrol.o text by nearly 300 bytes (x86_64 allmodconfig).

Reviewed-by on v1 1/1
#2 lore raw
In reply to Hui Zhu
Quoted 12 lines · On Wed, 29 Apr 2026 16:42:16 +0800 Hui Zhu <[email protected]> wrote:

From: Hui Zhu <[email protected]>

In mem_cgroup_alloc(), the assignment of pstatc_pcpu is invariant with respect to the for_each_possible_cpu() loop: both the 'parent' pointer and 'parent->vmstats_percpu' remain constant throughout all iterations.

The original code redundantly re-evaluated the 'if (parent)' condition and reassigned pstatc_pcpu on every CPU iteration, then repeated the same ternary check 'parent ? pstatc_pcpu : NULL' when storing into statc->parent_pcpu.

Quoted 7 lines

Move the single conditional assignment of pstatc_pcpu to before the loop, resolving both the loop-invariant placement issue and the duplicated null check. On systems with a large number of possible CPUs, this eliminates repeated branch evaluation with no functional change.

No functional change intended.

Makes sense and looks good to me.

Quoted 2 lines

Thanks,
SJ

[...]

maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Hui Zhu
Quoted 18 lines · Hui Zhu

From: Hui Zhu <[email protected]>

In mem_cgroup_alloc(), the assignment of pstatc_pcpu is invariant with respect to the for_each_possible_cpu() loop: both the 'parent' pointer and 'parent->vmstats_percpu' remain constant throughout all iterations.

The original code redundantly re-evaluated the 'if (parent)' condition and reassigned pstatc_pcpu on every CPU iteration, then repeated the same ternary check 'parent ? pstatc_pcpu : NULL' when storing into statc->parent_pcpu.

Move the single conditional assignment of pstatc_pcpu to before the loop, resolving both the loop-invariant placement issue and the duplicated null check. On systems with a large number of possible CPUs, this eliminates repeated branch evaluation with no functional change.

Quoted 3 lines

No functional change intended.