memcg: switch lruvec stats to rstat
Subsystems (5)
PATCH v4 1/2 memcg: switch lruvec stats to rstat
The commit 2d146aa3aa84 ("mm: memcontrol: switch to rstat") switched memcg stats to rstat infrastructure but skipped the conversion of the lruvec stats as such stats are read in the performance critical code paths and flushing stats may have impacted the performances of the applications. This patch converts the lruvec stats to rstat and later patches add mechanisms to keep the performance impact to minimum.
The rstat conversion comes with the price i.e. memory cost. Effectively this patch reverts the savings done by the commit f3344adf38bd ("mm:
memcontrol: optimize per-lruvec stats counter memory usage"). However this cost is justified due to negative impact of the inaccurate lruvec stats on many heuristics. One such case is reported in [1].
The memory reclaim code is filled with plethora of heuristics and many of those heuristics reads the lruvec stats. So, inaccurate stats can make such heuristics ineffective. [1] reports the impact of inaccurate lruvec stats on the "cache trim mode" heuristic. Inaccurate lruvec stats can impact the deactivation and aging anon heuristics as well.
---
Changes since v3:
- Add back the signoff
Changes since v2:
- updated commit message
Changes since v1:
- None
include/linux/memcontrol.h | 42 +++++++------- mm/memcontrol.c | 114 +++++++++++++------------------------ 2 files changed, 58 insertions(+), 98 deletions(-)
include/linux/memcontrol.h +20 -22
@@ -105,14 +105,6 @@ struct mem_cgroup_reclaim_iter {unsigned int generation; }; -struct lruvec_stat { -long count[NR_VM_NODE_STAT_ITEMS ]; -}; - -struct batched_lruvec_stat { -s32 count[NR_VM_NODE_STAT_ITEMS ]; -}; -/* * Bitmap and deferred work of shrinker::id corresponding to memcg-aware * shrinkers, which have elements charged to this memcg.
@@ -123,24 +115,30 @@ struct shrinker_info {unsigned long *map; }; +struct lruvec_stats_percpu { +/* Local (CPU and cgroup) state */ +long state[NR_VM_NODE_STAT_ITEMS ]; + +/* Delta calculation for lockless upward propagation */ +long state_prev[NR_VM_NODE_STAT_ITEMS ]; +}; + +struct lruvec_stats { +/* Aggregated (CPU and subtree) state */ +long state[NR_VM_NODE_STAT_ITEMS ]; + +/* Pending child counts during tree propagation */ +long state_pending[NR_VM_NODE_STAT_ITEMS ]; +}; +/* * per-node information in memory controller. */ struct mem_cgroup_per_node {struct lruvec lruvec; -/* -* Legacy local VM stats. This should be struct lruvec_stat and -* cannot be optimized to struct batched_lruvec_stat. Because -* the threshold of the lruvec_stat_cpu can be as big as -* MEMCG_CHARGE_BATCH * PAGE_SIZE. It can fit into s32. But this -* filed has no upper limit. -*/ -struct lruvec_stat __percpu *lruvec_stat_local; - -/* Subtree VM stats (batched updates) */ -struct batched_lruvec_stat __percpu *lruvec_stat_cpu; -atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS ]; +struct lruvec_stats_percpu __percpu *lruvec_stats_percpu; +struct lruvec_stats lruvec_stats;unsigned long lru_zone_size[MAX_NR_ZONES ][NR_LRU_LISTS ];
@@ -965,7 +963,7 @@ static inline unsigned long lruvec_page_state(struct lruvec *lruvec,return node_page_state (lruvec_pgdat (lruvec), idx); pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); - x =atomic_long_read (&pn->lruvec_stat[idx]); + x =READ_ONCE (pn->lruvec_stats.state[idx]);#ifdef CONFIG_SMP if (x <0 ) x =0 ;
@@ -985,7 +983,7 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);for_each_possible_cpu (cpu) - x +=per_cpu (pn->lruvec_stat_local->count[idx], cpu); + x +=per_cpu (pn->lruvec_stats_percpu->state[idx], cpu);#ifdef CONFIG_SMP if (x <0 ) x =0 ;
mm/memcontrol.c +38 -76
181 lines not on this page · raw message
Signature · 1 line
2.32.0.93.g670b81a890-goog
PATCH v4 2/2 memcg: infrastructure to flush memcg stats
At the moment memcg stats are read in four contexts:
1. memcg stat user interfaces
2. dirty throttling
3. page fault
4. memory reclaim
Currently the kernel flushes the stats for first two cases. Flushing the stats for remaining two casese may have performance impact. Always flushing the memcg stats on the page fault code path may negatively impacts the performance of the applications. In addition flushing in the memory reclaim code path, though treated as slowpath, can become the source of contention for the global lock taken for stat flushing because when system or memcg is under memory pressure, many tasks may enter the reclaim path.
This patch uses following mechanisms to solve these challenges:
1. Periodically flush the stats from root memcg every 2 seconds. This will time limit the out of sync stats.
2. Asynchronously flush the stats after fixed number of stat updates. In the worst case the stat can be out of sync by O(nr_cpus * BATCH) for 2 seconds.
3. For avoiding thundering herd to flush the stats particularly from the memory reclaim context, introduce memcg local spinlock and let only one flusher active at a time. This could have been done through cgroup_rstat_lock lock but that lock is used by other subsystem and for userspace reading memcg stats. So, it is better to keep flushers introduced by this patch decoupled from cgroup_rstat_lock.
---
Changes since v3:
- Add back the sigoff
Changes since v2:
- Changed the subject of the patch
- Added mechanism to bound errors to nr_cpus instead of nr_cgroups
- memcg local lock to let one active flusher
Changes since v1:
- use system_unbound_wq for flushing the memcg stats
include/linux/memcontrol.h | 6 ++++++ mm/memcontrol.c | 34 ++++++++++++++++++++++++++++++++++ mm/vmscan.c | 6 ++++++ 3 files changed, 46 insertions(+)
include/linux/memcontrol.h +6 -0
@@ -991,6 +991,8 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,return x; } +void mem_cgroup_flush_stats (void ); +void __mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val);void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val);
@@ -1400,6 +1402,10 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,return node_page_state (lruvec_pgdat (lruvec), idx); } +static inline void mem_cgroup_flush_stats (void ) +{ +} +static inline void __mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val) {
mm/memcontrol.c +34 -0
@@ -103,6 +103,14 @@ static bool do_memsw_account(void)return !cgroup_subsys_on_dfl (memory_cgrp_subsys) && !cgroup_memory_noswap; } +/* memcg and lruvec stats flushing */ +static void flush_memcg_stats_dwork (struct work_struct *w); +static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); +static void flush_memcg_stats_work (struct work_struct *w); +static DECLARE_WORK(stats_flush_work, flush_memcg_stats_work); +static DEFINE_PER_CPU(unsigned int, stats_flush_threshold); +static DEFINE_SPINLOCK (stats_flush_lock); +#define THRESHOLDS_EVENTS_TARGET 128#define SOFTLIMIT_EVENTS_TARGET 1024
@@ -685,6 +693,8 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,/* Update lruvec */ __this_cpu_add (pn->lruvec_stats_percpu->state[idx], val); +if (!(__this_cpu_inc_return (stats_flush_threshold) %MEMCG_CHARGE_BATCH )) +queue_work (system_unbound_wq, &stats_flush_work); }/**
@@ -5248,6 +5258,10 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)/* Online state pins memcg ID, memcg ID pins CSS */ refcount_set (&memcg->id.ref,1 );css_get (css); + +if (unlikely (mem_cgroup_is_root (memcg))) +queue_delayed_work (system_unbound_wq, &stats_flush_dwork, +2UL *HZ );return 0 ; }
@@ -5339,6 +5353,26 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)memcg_wb_domain_size_changed (memcg); } +void mem_cgroup_flush_stats (void ) +{ +if (!spin_trylock (&stats_flush_lock)) +return ; + +cgroup_rstat_flush (root_mem_cgroup->css.cgroup); +spin_unlock (&stats_flush_lock); +} + +static void flush_memcg_stats_dwork (struct work_struct *w) +{ +mem_cgroup_flush_stats (); +queue_delayed_work (system_unbound_wq, &stats_flush_dwork,2UL *HZ ); +} + +static void flush_memcg_stats_work (struct work_struct *w) +{ +mem_cgroup_flush_stats (); +} +static void mem_cgroup_css_rstat_flush (struct cgroup_subsys_state *css,int cpu) {struct mem_cgroup *memcg =mem_cgroup_from_css (css);
mm/vmscan.c +6 -0
@@ -2893,6 +2893,12 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) target_lruvec =mem_cgroup_lruvec (sc->target_mem_cgroup, pgdat); again: +/* +* Flush the memory cgroup stats, so that we read accurate per-memcg +* lruvec stats for heuristics. +*/ +mem_cgroup_flush_stats (); +memset (&sc->nr,0 ,sizeof (sc->nr)); nr_reclaimed = sc->nr_reclaimed;
Signature · 1 line
2.32.0.93.g670b81a890-goog
Re: [PATCH v4 2/2] memcg: infrastructure to flush memcg stats
Hi,
Quoted 25 lines · Shakeel Butt
At the moment memcg stats are read in four contexts:
1. memcg stat user interfaces
2. dirty throttling
3. page fault
4. memory reclaimCurrently the kernel flushes the stats for first two cases. Flushing the stats for remaining two casese may have performance impact. Always flushing the memcg stats on the page fault code path may negatively impacts the performance of the applications. In addition flushing in the memory reclaim code path, though treated as slowpath, can become the source of contention for the global lock taken for stat flushing because when system or memcg is under memory pressure, many tasks may enter the reclaim path.
This patch uses following mechanisms to solve these challenges:
1. Periodically flush the stats from root memcg every 2 seconds. This will time limit the out of sync stats.
2. Asynchronously flush the stats after fixed number of stat updates. In the worst case the stat can be out of sync by
O(nr_cpus * BATCH)for 2 seconds.
Quoted 8 lines
3. For avoiding thundering herd to flush the stats particularly from the memory reclaim context, introduce memcg local spinlock and let only one flusher active at a time. This could have been done through
cgroup_rstat_locklock but that lock is used by other subsystem and for userspace reading memcg stats. So, it is better to keep flushers introduced by this patch decoupled fromcgroup_rstat_lock.Signed-off-by Shakeel Butt <[email protected]>
This patch landed in today's linux-next (next-20210716) as commit 42265e014ac7 ("memcg: infrastructure to flush memcg stats"). On my test system's I found that it triggers a kernel BUG on all ARM64 boards:
BUG: sleeping function called from invalid context at
kernel/cgroup/rstat.c:200
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 7, name:
kworker/u8:0
3 locks held by kworker/u8:0/7:
#0: ffff00004000c938 ((wq_completion)events_unbound){+.+.}-{0:0}, at:
process_one_work+0x200/0x718
#1: ffff80001334bdd0 ((stats_flush_dwork).work){+.+.}-{0:0}, at:
process_one_work+0x200/0x718
#2: ffff8000124f6d40 (stats_flush_lock){+.+.}-{2:2}, at:
mem_cgroup_flush_stats+0x20/0x48
CPU: 2 PID: 7 Comm: kworker/u8:0 Tainted: G W 5.14.0-rc1+ #3713
Hardware name: Raspberry Pi 4 Model B (DT)
Workqueue: events_unbound flush_memcg_stats_dwork
Call trace:
dump_backtrace+0x0/0x1d0
show_stack+0x14/0x20
dump_stack_lvl+0x88/0xb0
dump_stack+0x14/0x2c
___might_sleep+0x1dc/0x200
__might_sleep+0x4c/0x88
cgroup_rstat_flush+0x2c/0x58
mem_cgroup_flush_stats+0x34/0x48
flush_memcg_stats_dwork+0xc/0x38
process_one_work+0x2a8/0x718
worker_thread+0x48/0x460
kthread+0x12c/0x160
ret_from_fork+0x10/0x18This can be also reproduced with QEmu. Please let me know if I can help fixing this issue.
Quoted 114 lines
---
Changes since v3:
- Add back the sigoffChanges since v2:
- Changed the subject of the patch
- Added mechanism to bound errors tonr_cpusinstead ofnr_cgroups
- memcg local lock to let one active flusherChanges since v1:
- usesystem_unbound_wqfor flushing the memcg statsinclude/linux/memcontrol.h | 6 ++++++ mm/memcontrol.c | 34 ++++++++++++++++++++++++++++++++++ mm/vmscan.c | 6 ++++++ 3 files changed, 46 insertions(+)include/linux/memcontrol.h +6
-0@@ -991,6 +991,8 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,return x; } +void mem_cgroup_flush_stats (void ); +void __mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val);void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val);@@ -1400,6 +1402,10 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,return node_page_state (lruvec_pgdat (lruvec), idx); } +static inline void mem_cgroup_flush_stats (void ) +{ +} +static inline void __mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val) {mm/memcontrol.c +34
-0@@ -103,6 +103,14 @@ static bool do_memsw_account(void)return !cgroup_subsys_on_dfl (memory_cgrp_subsys) && !cgroup_memory_noswap; } +/* memcg and lruvec stats flushing */ +static void flush_memcg_stats_dwork (struct work_struct *w); +static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); +static void flush_memcg_stats_work (struct work_struct *w); +static DECLARE_WORK(stats_flush_work, flush_memcg_stats_work); +static DEFINE_PER_CPU(unsigned int, stats_flush_threshold); +static DEFINE_SPINLOCK (stats_flush_lock); +#define THRESHOLDS_EVENTS_TARGET 128#define SOFTLIMIT_EVENTS_TARGET 1024@@ -685,6 +693,8 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,/* Update lruvec */ __this_cpu_add (pn->lruvec_stats_percpu->state[idx], val); +if (!(__this_cpu_inc_return (stats_flush_threshold) %MEMCG_CHARGE_BATCH )) +queue_work (system_unbound_wq, &stats_flush_work); }/** @@ -5248,6 +5258,10 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)/* Online state pins memcg ID, memcg ID pins CSS */ refcount_set (&memcg->id.ref,1 );css_get (css); + +if (unlikely (mem_cgroup_is_root (memcg))) +queue_delayed_work (system_unbound_wq, &stats_flush_dwork, +2UL *HZ );return 0 ; }@@ -5339,6 +5353,26 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)memcg_wb_domain_size_changed (memcg); } +void mem_cgroup_flush_stats (void ) +{ +if (!spin_trylock (&stats_flush_lock)) +return ; + +cgroup_rstat_flush (root_mem_cgroup->css.cgroup); +spin_unlock (&stats_flush_lock); +} + +static void flush_memcg_stats_dwork (struct work_struct *w) +{ +mem_cgroup_flush_stats (); +queue_delayed_work (system_unbound_wq, &stats_flush_dwork,2UL *HZ ); +} + +static void flush_memcg_stats_work (struct work_struct *w) +{ +mem_cgroup_flush_stats (); +} +static void mem_cgroup_css_rstat_flush (struct cgroup_subsys_state *css,int cpu) {struct mem_cgroup *memcg =mem_cgroup_from_css (css);mm/vmscan.c +0
-0@@ -2893,6 +2893,12 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) target_lruvec =mem_cgroup_lruvec (sc->target_mem_cgroup, pgdat);
mm/vmscan.c static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) · line 2904
Quoted 10 lines
mm/vmscan.c +6
-0again: +/* +* Flush the memory cgroup stats, so that we read accurate per-memcg +* lruvec stats for heuristics. +*/ +mem_cgroup_flush_stats (); +memset (&sc->nr,0 ,sizeof (sc->nr)); nr_reclaimed = sc->nr_reclaimed;
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [PATCH v4 2/2] memcg: infrastructure to flush memcg stats
Hi Marek
Quoted 71 lines · Marek Szyprowski
Hi,
Quoted 25 lines · Shakeel Butt
At the moment memcg stats are read in four contexts:
1. memcg stat user interfaces
2. dirty throttling
3. page fault
4. memory reclaimCurrently the kernel flushes the stats for first two cases. Flushing the stats for remaining two casese may have performance impact. Always flushing the memcg stats on the page fault code path may negatively impacts the performance of the applications. In addition flushing in the memory reclaim code path, though treated as slowpath, can become the source of contention for the global lock taken for stat flushing because when system or memcg is under memory pressure, many tasks may enter the reclaim path.
This patch uses following mechanisms to solve these challenges:
1. Periodically flush the stats from root memcg every 2 seconds. This will time limit the out of sync stats.
2. Asynchronously flush the stats after fixed number of stat updates. In the worst case the stat can be out of sync by
O(nr_cpus * BATCH)for 2 seconds.Quoted 8 lines
3. For avoiding thundering herd to flush the stats particularly from the memory reclaim context, introduce memcg local spinlock and let only one flusher active at a time. This could have been done through
cgroup_rstat_locklock but that lock is used by other subsystem and for userspace reading memcg stats. So, it is better to keep flushers introduced by this patch decoupled fromcgroup_rstat_lock.Signed-off-by Shakeel Butt <[email protected]>This patch landed in today's linux-next (next-20210716) as commit
42265e014ac7("memcg: infrastructure to flush memcg stats"). On my test system's I found that it triggers a kernel BUG on all ARM64 boards:BUG: sleeping function called from invalid context at
kernel/cgroup/rstat.c:200
in_atomic(): 1,irqs_disabled(): 0,non_block: 0, pid: 7, name:
kworker/u8:0
3 locks held by kworker/u8:0/7:#0: ffff00004000c938 ((wq_completion)events_unbound){+.+.}-{0:0}, at: process_one_work+0x200/0x718 #1: ffff80001334bdd0 ((stats_flush_dwork).work){+.+.}-{0:0}, at: process_one_work+0x200/0x718 #2: ffff8000124f6d40 (stats_flush_lock){+.+.}-{2:2}, at: mem_cgroup_flush_stats+0x20/0x48 CPU: 2 PID: 7 Comm: kworker/u8:0 Tainted: G W 5.14.0-rc1+ #3713 Hardware name: Raspberry Pi 4 Model B (DT) Workqueue: events_unbound flush_memcg_stats_dwork Call trace: dump_backtrace+0x0/0x1d0 show_stack+0x14/0x20 dump_stack_lvl+0x88/0xb0 dump_stack+0x14/0x2c ___might_sleep+0x1dc/0x200 __might_sleep+0x4c/0x88 cgroup_rstat_flush+0x2c/0x58 mem_cgroup_flush_stats+0x34/0x48 flush_memcg_stats_dwork+0xc/0x38 process_one_work+0x2a8/0x718 worker_thread+0x48/0x460 kthread+0x12c/0x160 ret_from_fork+0x10/0x18
Quoted 3 lines
This can be also reproduced with QEmu. Please let me know if I can help fixing this issue.
Thanks for the report. The issue can be fixed by changing cgroup_rstat_flush() to cgroup_rstat_flush_irqsafe() in mem_cgroup_flush_stats(). I will send out the updated patch in a couple of hours after a bit more testing.
Marek Szyprowski
Tested-byRe: [PATCH v4 2/2] memcg: infrastructure to flush memcg stats
Hi,
Quoted 73 lines · Shakeel Butt
Hi Marek
Quoted 69 lines · Marek Szyprowski
Hi,
Quoted 25 lines · Shakeel Butt
At the moment memcg stats are read in four contexts:
1. memcg stat user interfaces
2. dirty throttling
3. page fault
4. memory reclaimCurrently the kernel flushes the stats for first two cases. Flushing the stats for remaining two casese may have performance impact. Always flushing the memcg stats on the page fault code path may negatively impacts the performance of the applications. In addition flushing in the memory reclaim code path, though treated as slowpath, can become the source of contention for the global lock taken for stat flushing because when system or memcg is under memory pressure, many tasks may enter the reclaim path.
This patch uses following mechanisms to solve these challenges:
1. Periodically flush the stats from root memcg every 2 seconds. This will time limit the out of sync stats.
2. Asynchronously flush the stats after fixed number of stat updates. In the worst case the stat can be out of sync by
O(nr_cpus * BATCH)for 2 seconds.Quoted 8 lines
3. For avoiding thundering herd to flush the stats particularly from the memory reclaim context, introduce memcg local spinlock and let only one flusher active at a time. This could have been done through
cgroup_rstat_locklock but that lock is used by other subsystem and for userspace reading memcg stats. So, it is better to keep flushers introduced by this patch decoupled fromcgroup_rstat_lock.Signed-off-by Shakeel Butt <[email protected]>This patch landed in today's linux-next (next-20210716) as commit
42265e014ac7("memcg: infrastructure to flush memcg stats"). On my test system's I found that it triggers a kernel BUG on all ARM64 boards:BUG: sleeping function called from invalid context at
kernel/cgroup/rstat.c:200
in_atomic(): 1,irqs_disabled(): 0,non_block: 0, pid: 7, name:
kworker/u8:0
3 locks held by kworker/u8:0/7:#0: ffff00004000c938 ((wq_completion)events_unbound){+.+.}-{0:0}, at: process_one_work+0x200/0x718 #1: ffff80001334bdd0 ((stats_flush_dwork).work){+.+.}-{0:0}, at: process_one_work+0x200/0x718 #2: ffff8000124f6d40 (stats_flush_lock){+.+.}-{2:2}, at: mem_cgroup_flush_stats+0x20/0x48 CPU: 2 PID: 7 Comm: kworker/u8:0 Tainted: G W 5.14.0-rc1+ #3713 Hardware name: Raspberry Pi 4 Model B (DT) Workqueue: events_unbound flush_memcg_stats_dwork Call trace: dump_backtrace+0x0/0x1d0 show_stack+0x14/0x20 dump_stack_lvl+0x88/0xb0 dump_stack+0x14/0x2c ___might_sleep+0x1dc/0x200 __might_sleep+0x4c/0x88 cgroup_rstat_flush+0x2c/0x58 mem_cgroup_flush_stats+0x34/0x48 flush_memcg_stats_dwork+0xc/0x38 process_one_work+0x2a8/0x718 worker_thread+0x48/0x460 kthread+0x12c/0x160 ret_from_fork+0x10/0x18
Quoted 7 lines
Quoted 3 lines
This can be also reproduced with QEmu. Please let me know if I can help fixing this issue.
Thanks for the report. The issue can be fixed by changing
cgroup_rstat_flush()tocgroup_rstat_flush_irqsafe()inmem_cgroup_flush_stats(). I will send out the updated patch in a couple of hours after a bit more testing.
Right, this fixes the issue on my test systems. Feel free to add:
to the fixup patch if the target kernel tree won't be rebased and the original patch (42265e014ac7) stays.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland