]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mm/memcontrol: hoist pstatc_pcpu assignment out of CPU loop
authorHui Zhu <zhuhui@kylinos.cn>
Wed, 29 Apr 2026 08:42:16 +0000 (16:42 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 May 2026 04:05:00 +0000 (21:05 -0700)
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.

Link: https://lore.kernel.org/20260429084216.186238-1-hui.zhu@linux.dev
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
Reviewed-by: SeongJae Park <sj@kernel.org>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/memcontrol.c

index 177732fef010bc7a13b2ef38777d591062f63d60..2bc9a7238939af1b9487e0e8b5288684ccb1672b 100644 (file)
@@ -4002,11 +4002,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;
        }