Skip to content

Commit 9cee7e8

Browse files
yosrym93akpm00
authored andcommitted
mm: memcg: optimize parent iteration in memcg_rstat_updated()
In memcg_rstat_updated(), we iterate the memcg being updated and its parents to update memcg->vmstats_percpu->stats_updates in the fast path (i.e. no atomic updates). According to my math, this is 3 memory loads (and potentially 3 cache misses) per memcg: - Load the address of memcg->vmstats_percpu. - Load vmstats_percpu->stats_updates (based on some percpu calculation). - Load the address of the parent memcg. Avoid most of the cache misses by caching a pointer from each struct memcg_vmstats_percpu to its parent on the corresponding CPU. In this case, for the first memcg we have 2 memory loads (same as above): - Load the address of memcg->vmstats_percpu. - Load vmstats_percpu->stats_updates (based on some percpu calculation). Then for each additional memcg, we need a single load to get the parent's stats_updates directly. This reduces the number of loads from O(3N) to O(2+N) -- where N is the number of memcgs we need to iterate. Additionally, stash a pointer to memcg->vmstats in each struct memcg_vmstats_percpu such that we can access the atomic counter that all CPUs fold into, memcg->vmstats->stats_updates. memcg_should_flush_stats() is changed to memcg_vmstats_needs_flush() to accept a struct memcg_vmstats pointer accordingly. In struct memcg_vmstats_percpu, make sure both pointers together with stats_updates live on the same cacheline. Finally, update mem_cgroup_alloc() to take in a parent pointer and initialize the new cache pointers on each CPU. The percpu loop in mem_cgroup_alloc() may look concerning, but there are multiple similar loops in the cgroup creation path (e.g. cgroup_rstat_init()), most of which are hidden within alloc_percpu(). According to Oliver's testing [1], this fixes multiple 30-38% regressions in vm-scalability, will-it-scale-tlb_flush2, and will-it-scale-fallocate1. This comes at a cost of 2 more pointers per CPU (<2KB on a machine with 128 CPUs). [1] https://lore.kernel.org/lkml/ZbDJsfsZt2ITyo61@xsang-OptiPlex-9020/ [yosryahmed@google.com: fix struct memcg_vmstats_percpu size and alignment] Link: https://lkml.kernel.org/r/20240203044612.1234216-1-yosryahmed@google.com Link: https://lkml.kernel.org/r/20240124100023.660032-1-yosryahmed@google.com Signed-off-by: Yosry Ahmed <yosryahmed@google.com> Fixes: 8d59d22 ("mm: memcg: make stats flushing threshold per-memcg") Tested-by: kernel test robot <oliver.sang@intel.com> Reported-by: kernel test robot <oliver.sang@intel.com> Closes: https://lore.kernel.org/oe-lkp/202401221624.cb53a8ca-oliver.sang@intel.com Acked-by: Shakeel Butt <shakeelb@google.com> Acked-by: 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> Cc: Greg Thelen <gthelen@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 67b8bcb commit 9cee7e8

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

mm/memcontrol.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,15 @@ static inline int memcg_events_index(enum vm_event_item idx)
621621
}
622622

623623
struct memcg_vmstats_percpu {
624+
/* Stats updates since the last flush */
625+
unsigned int stats_updates;
626+
627+
/* Cached pointers for fast iteration in memcg_rstat_updated() */
628+
struct memcg_vmstats_percpu *parent;
629+
struct memcg_vmstats *vmstats;
630+
631+
/* The above should fit a single cacheline for memcg_rstat_updated() */
632+
624633
/* Local (CPU and cgroup) page state & events */
625634
long state[MEMCG_NR_STAT];
626635
unsigned long events[NR_MEMCG_EVENTS];
@@ -632,10 +641,7 @@ struct memcg_vmstats_percpu {
632641
/* Cgroup1: threshold notifications & softlimit tree updates */
633642
unsigned long nr_page_events;
634643
unsigned long targets[MEM_CGROUP_NTARGETS];
635-
636-
/* Stats updates since the last flush */
637-
unsigned int stats_updates;
638-
};
644+
} ____cacheline_aligned;
639645

640646
struct memcg_vmstats {
641647
/* Aggregated (CPU and subtree) page state & events */
@@ -698,36 +704,35 @@ static void memcg_stats_unlock(void)
698704
}
699705

700706

701-
static bool memcg_should_flush_stats(struct mem_cgroup *memcg)
707+
static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats)
702708
{
703-
return atomic64_read(&memcg->vmstats->stats_updates) >
709+
return atomic64_read(&vmstats->stats_updates) >
704710
MEMCG_CHARGE_BATCH * num_online_cpus();
705711
}
706712

707713
static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
708714
{
715+
struct memcg_vmstats_percpu *statc;
709716
int cpu = smp_processor_id();
710-
unsigned int x;
711717

712718
if (!val)
713719
return;
714720

715721
cgroup_rstat_updated(memcg->css.cgroup, cpu);
716-
717-
for (; memcg; memcg = parent_mem_cgroup(memcg)) {
718-
x = __this_cpu_add_return(memcg->vmstats_percpu->stats_updates,
719-
abs(val));
720-
721-
if (x < MEMCG_CHARGE_BATCH)
722+
statc = this_cpu_ptr(memcg->vmstats_percpu);
723+
for (; statc; statc = statc->parent) {
724+
statc->stats_updates += abs(val);
725+
if (statc->stats_updates < MEMCG_CHARGE_BATCH)
722726
continue;
723727

724728
/*
725729
* If @memcg is already flush-able, increasing stats_updates is
726730
* redundant. Avoid the overhead of the atomic update.
727731
*/
728-
if (!memcg_should_flush_stats(memcg))
729-
atomic64_add(x, &memcg->vmstats->stats_updates);
730-
__this_cpu_write(memcg->vmstats_percpu->stats_updates, 0);
732+
if (!memcg_vmstats_needs_flush(statc->vmstats))
733+
atomic64_add(statc->stats_updates,
734+
&statc->vmstats->stats_updates);
735+
statc->stats_updates = 0;
731736
}
732737
}
733738

@@ -756,7 +761,7 @@ void mem_cgroup_flush_stats(struct mem_cgroup *memcg)
756761
if (!memcg)
757762
memcg = root_mem_cgroup;
758763

759-
if (memcg_should_flush_stats(memcg))
764+
if (memcg_vmstats_needs_flush(memcg->vmstats))
760765
do_flush_stats(memcg);
761766
}
762767

@@ -770,7 +775,7 @@ void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg)
770775
static void flush_memcg_stats_dwork(struct work_struct *w)
771776
{
772777
/*
773-
* Deliberately ignore memcg_should_flush_stats() here so that flushing
778+
* Deliberately ignore memcg_vmstats_needs_flush() here so that flushing
774779
* in latency-sensitive paths is as cheap as possible.
775780
*/
776781
do_flush_stats(root_mem_cgroup);
@@ -5477,10 +5482,11 @@ static void mem_cgroup_free(struct mem_cgroup *memcg)
54775482
__mem_cgroup_free(memcg);
54785483
}
54795484

5480-
static struct mem_cgroup *mem_cgroup_alloc(void)
5485+
static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
54815486
{
5487+
struct memcg_vmstats_percpu *statc, *pstatc;
54825488
struct mem_cgroup *memcg;
5483-
int node;
5489+
int node, cpu;
54845490
int __maybe_unused i;
54855491
long error = -ENOMEM;
54865492

@@ -5504,6 +5510,14 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
55045510
if (!memcg->vmstats_percpu)
55055511
goto fail;
55065512

5513+
for_each_possible_cpu(cpu) {
5514+
if (parent)
5515+
pstatc = per_cpu_ptr(parent->vmstats_percpu, cpu);
5516+
statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
5517+
statc->parent = parent ? pstatc : NULL;
5518+
statc->vmstats = memcg->vmstats;
5519+
}
5520+
55075521
for_each_node(node)
55085522
if (alloc_mem_cgroup_per_node_info(memcg, node))
55095523
goto fail;
@@ -5549,7 +5563,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
55495563
struct mem_cgroup *memcg, *old_memcg;
55505564

55515565
old_memcg = set_active_memcg(parent);
5552-
memcg = mem_cgroup_alloc();
5566+
memcg = mem_cgroup_alloc(parent);
55535567
set_active_memcg(old_memcg);
55545568
if (IS_ERR(memcg))
55555569
return ERR_CAST(memcg);

0 commit comments

Comments
 (0)