mm: memcontrol: fix excessive complexity in memory.stat reporting
Subsystems (2)
[email protected]
maintainerPATCH 7/15 mm: memcontrol: fix excessive complexity in memory.stat reporting
From: Johannes Weiner <[email protected]>
Subject: mm: memcontrol: fix excessive complexity in memory.stat reporting
We've seen memory.stat reads in top-level cgroups take up to fourteen seconds during a userspace bug that created tens of thousands of ghost cgroups pinned by lingering page cache.
Even with a more reasonable number of cgroups, aggregating memory.stat is unnecessarily heavy. The complexity is this:
nr_cgroups * nr_stat_items * nr_possible_cpus
where the stat items are ~70 at this point. With 128 cgroups and 128 CPUs
- decent, not enormous setups - reading the top-level memory.stat has to aggregate over a million per-cpu counters. This doesn't scale.
Instead of spreading the source of truth across all CPUs, use the per-cpu counters merely to batch updates to shared atomic counters.
This is the same as the per-cpu stocks we use for charging memory to the shared atomic page_counters, and also the way the global vmstat counters are implemented.
Vmstat has elaborate spilling thresholds that depend on the number of CPUs, amount of memory, and memory pressure - carefully balancing the cost of counter updates with the amount of per-cpu error. That's because the vmstat counters are system-wide, but also used for decisions inside the kernel (e.g. NR_FREE_PAGES in the allocator). Neither is true for the memory controller.
Use the same static batch size we already use for page_counter updates during charging. The per-cpu error in the stats will be 128k, which is an acceptable ratio of cores to memory accounting granularity.
---
include/linux/memcontrol.h | 96 +++++++++++++++++++++------------ mm/memcontrol.c | 101 +++++++++++++++++------------------ 2 files changed, 113 insertions(+), 84 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mm-memcontrol-fix-excessive-complexity-in-memorystat-reporting +62 -34
@@ -108,7 +108,10 @@ struct lruvec_stat {*/ struct mem_cgroup_per_node {struct lruvec lruvec; -struct lruvec_stat __percpu *lruvec_stat; + +struct lruvec_stat __percpu *lruvec_stat_cpu; +atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS ]; +unsigned long lru_zone_size[MAX_NR_ZONES ][NR_LRU_LISTS ];struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY +1 ];
@@ -227,10 +230,10 @@ struct mem_cgroup {spinlock_t move_lock;struct task_struct *move_lock_task;unsigned long move_lock_flags; -/* -* percpu counter. -*/ -struct mem_cgroup_stat_cpu __percpu *stat; + +struct mem_cgroup_stat_cpu __percpu *stat_cpu; +atomic_long_t stat[MEMCG_NR_STAT ]; +atomic_long_t events[MEMCG_NR_EVENTS ];unsigned long socket_pressure;
@@ -265,6 +268,12 @@ struct mem_cgroup {/* WARNING: nodeinfo must be the last member here */ }; +/* +* size of first charge trial. "32" comes from vmscan.c's magic value. +* TODO: maybe necessary to use big numbers in big irons. +*/ +#define MEMCG_CHARGE_BATCH 32U +extern struct mem_cgroup *root_mem_cgroup;static inline bool mem_cgroup_disabled (void )
@@ -485,32 +494,38 @@ void unlock_page_memcg(struct page *pagestatic inline unsigned long memcg_page_state (struct mem_cgroup *memcg,int idx) { -long val =0 ; -int cpu; - -for_each_possible_cpu (cpu) - val +=per_cpu (memcg->stat->count[idx], cpu); - -if (val <0 ) - val =0 ; - -return val; +long x =atomic_long_read (&memcg->stat[idx]); +#ifdef CONFIG_SMP +if (x <0 ) + x =0 ; +#endif +return x; }/* idx can be of type enum memcg_stat_item or node_stat_item */ static inline void __mod_memcg_state (struct mem_cgroup *memcg,int idx,int val) { -if (!mem_cgroup_disabled ()) -__this_cpu_add (memcg->stat->count[idx], val); +long x; + +if (mem_cgroup_disabled ()) +return ; + + x = val +__this_cpu_read (memcg->stat_cpu->count[idx]); +if (unlikely (abs (x) >MEMCG_CHARGE_BATCH )) { +atomic_long_add (x, &memcg->stat[idx]); + x =0 ; + } +__this_cpu_write (memcg->stat_cpu->count[idx], x); }/* idx can be of type enum memcg_stat_item or node_stat_item */ static inline void mod_memcg_state (struct mem_cgroup *memcg,int idx,int val) { -if (!mem_cgroup_disabled ()) -this_cpu_add (memcg->stat->count[idx], val); +preempt_disable (); +__mod_memcg_state (memcg, idx, val); +preempt_enable (); }/**
@@ -548,26 +563,25 @@ static inline unsigned long lruvec_page_ enum node_stat_item idx) {struct mem_cgroup_per_node *pn; -long val =0 ; -int cpu; +long x;if (mem_cgroup_disabled ())return node_page_state (lruvec_pgdat (lruvec), idx); pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); -for_each_possible_cpu (cpu) - val +=per_cpu (pn->lruvec_stat->count[idx], cpu); - -if (val <0 ) - val =0 ; - -return val; + x =atomic_long_read (&pn->lruvec_stat[idx]); +#ifdef CONFIG_SMP +if (x <0 ) + x =0 ; +#endif +return x; }static inline void __mod_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val) {struct mem_cgroup_per_node *pn; +long x;/* Update node */ __mod_node_page_state (lruvec_pgdat (lruvec), idx, val);
@@ -581,7 +595,12 @@ static inline void __mod_lruvec_state(st__mod_memcg_state (pn->memcg, idx, val);/* Update lruvec */ -__this_cpu_add (pn->lruvec_stat->count[idx], val); + x = val +__this_cpu_read (pn->lruvec_stat_cpu->count[idx]); +if (unlikely (abs (x) >MEMCG_CHARGE_BATCH )) { +atomic_long_add (x, &pn->lruvec_stat[idx]); + x =0 ; + } +__this_cpu_write (pn->lruvec_stat_cpu->count[idx], x); } static inline void mod_lruvec_state(struct lruvec *lruvec,
@@ -624,16 +643,25 @@ unsigned long mem_cgroup_soft_limit_reclstatic inline void __count_memcg_events (struct mem_cgroup *memcg,int idx,unsigned long count) { -if (!mem_cgroup_disabled ()) -__this_cpu_add (memcg->stat->events[idx], count); +unsigned long x; + +if (mem_cgroup_disabled ()) +return ; + + x = count +__this_cpu_read (memcg->stat_cpu->events[idx]); +if (unlikely (x >MEMCG_CHARGE_BATCH )) { +atomic_long_add (x, &memcg->events[idx]); + x =0 ; + } +__this_cpu_write (memcg->stat_cpu->events[idx], x); } -/* idx can be of type enum memcg_event_item or vm_event_item */ static inline void count_memcg_events (struct mem_cgroup *memcg,int idx,unsigned long count) { -if (!mem_cgroup_disabled ()) -this_cpu_add (memcg->stat->events[idx], count); +preempt_disable (); +__count_memcg_events (memcg, idx, count); +preempt_enable (); }/* idx can be of type enum memcg_event_item or vm_event_item */
mm/memcontrol.c renamed from mm/memcontrol.c~mm-memcontrol-fix-excessive-complexity-in-memorystat-reporting +51 -50
197 lines not on this page · raw message
_
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Johannes Weiner
maintainerQuoted 34 lines · On Thu, Nov 30, 2017 at 02:15:31PM -0800, [email protected] wrote:
(quoted diff) +27
-0@@ -1858,9 +1824,44 @@ static void drain_all_stock(struct mem_c static int memcg_hotplug_cpu_dead(unsigned int cpu) { struct memcg_stock_pcp *stock; + struct mem_cgroup *memcg; stock = &per_cpu(memcg_stock, cpu); drain_stock(stock); + + for_each_mem_cgroup(memcg) { + int i; + + for (i = 0; i < MEMCG_NR_STAT; i++) { + int nid; + long x; + + x = __this_cpu_xchg(memcg->stat_cpu->count[i], 0); + if (x) + atomic_long_add(x, &memcg->stat[i]); + + if (i >= NR_VM_NODE_STAT_ITEMS) + continue; + + for_each_node(nid) { + struct mem_cgroup_per_node *pn; + + pn = mem_cgroup_nodeinfo(memcg, nid); + x = __this_cpu_xchg(pn->lruvec_stat_cpu->count[i], 0); + if (x) + atomic_long_add(x, &pn->lruvec_stat[i]); + } + } + + for (i = 0; i < MEMCG_NR_EVENTS; i++) {
Review static void drain_all_stock(struct mem_c · line 1866
Quoted 10 lines
(quoted diff) +8
-0+ long x; + + x = __this_cpu_xchg(memcg->stat_cpu->events[i], 0); + if (x) + atomic_long_add(x, &memcg->events[i]); + } + } + return 0; }
The memcg cpu_dead callback can be called early during startup (CONFIG_DEBUG_HOTPLUG_CPU0) with preemption enabled, which triggers a warning in its __this_cpu_xchg() calls. But CPU locality is always guaranteed, which is the only thing we really care about here.
Using the preemption-safe this_cpu_xchg() addresses this problem.
---
Andrew, can you please merge this fixlet into the original patch?
mm/memcontrol.c +3 -3
@@ -1836,7 +1836,7 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu)int nid;long x; - x =__this_cpu_xchg (memcg->stat_cpu->count[i],0 ); + x =this_cpu_xchg (memcg->stat_cpu->count[i],0 );if (x)atomic_long_add (x, &memcg->stat[i]);
@@ -1847,7 +1847,7 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu)struct mem_cgroup_per_node *pn; pn =mem_cgroup_nodeinfo (memcg, nid); - x =__this_cpu_xchg (pn->lruvec_stat_cpu->count[i],0 ); + x =this_cpu_xchg (pn->lruvec_stat_cpu->count[i],0 );if (x)atomic_long_add (x, &pn->lruvec_stat[i]); }
@@ -1856,7 +1856,7 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu)for (i =0 ; i <MEMCG_NR_EVENTS ; i++) {long x; - x =__this_cpu_xchg (memcg->stat_cpu->events[i],0 ); + x =this_cpu_xchg (memcg->stat_cpu->events[i],0 );if (x)atomic_long_add (x, &memcg->events[i]); }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 11 lines · On Fri, 1 Dec 2017 13:57:50 +0000 Johannes Weiner <[email protected]> wrote:
The memcg
cpu_deadcallback can be called early during startup (CONFIG_DEBUG_HOTPLUG_CPU0) with preemption enabled, which triggers a warning in its__this_cpu_xchg()calls. But CPU locality is always guaranteed, which is the only thing we really care about here.Using the preemption-safe
this_cpu_xchg()addresses this problem.Signed-off-by Johannes Weiner <[email protected]>---
Andrew, can you please merge this fixlet into the original patch?
Did.
I see that lkp-robot identified a performance regression and pointed the finger at this patch?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Johannes Weiner
maintainerSorry, this email slipped through the cracks.
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Fri, 1 Dec 2017 13:57:50 +0000 Johannes Weiner <[email protected]> wrote:
The memcg
cpu_deadcallback can be called early during startup (CONFIG_DEBUG_HOTPLUG_CPU0) with preemption enabled, which triggers a warning in its__this_cpu_xchg()calls. But CPU locality is always guaranteed, which is the only thing we really care about here.Using the preemption-safe
this_cpu_xchg()addresses this problem.Signed-off-by Johannes Weiner <[email protected]>---
Quoted 6 lines
Quoted 1 line
Andrew, can you please merge this fixlet into the original patch?
Did.
I see that lkp-robot identified a performance regression and pointed the finger at this patch?
Right, it reports a perf drop in page fault stress tests, but that is to be the expected trade-off. Before, we'd do everything per cpu, and have to collapse all counters everytime somebody would read the stats. Now we fold them in batches, which introduces a periodic atomic when the batches are flushed (same frequency as we per-cpu cache charges for the atomic page_counter).
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>