Commit 5b3be698 authored by Shakeel Butt's avatar Shakeel Butt Committed by Linus Torvalds

memcg: better bounds on the memcg stats updates

Commit 11192d9c ("memcg: flush stats only if updated") added
tracking of memcg stats updates which is used by the readers to flush
only if the updates are over a certain threshold.  However each
individual update can correspond to a large value change for a given
stat.  For example adding or removing a hugepage to an LRU changes the
stat by thp_nr_pages (512 on x86_64).

Treating the update related to THP as one can keep the stat off, in
theory, by (thp_nr_pages * nr_cpus * CHARGE_BATCH) before flush.

To handle such scenarios, this patch adds consideration of the stat
update value as well instead of just the update event.  In addition let
the asyn flusher unconditionally flush the stats to put time limit on
the stats skew and hopefully a lot less readers would need to flush.

Link: https://lkml.kernel.org/r/20211118065350.697046-1-shakeelb@google.comSigned-off-by: default avatarShakeel Butt <shakeelb@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: "Michal Koutný" <mkoutny@suse.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent b6bf9abb
...@@ -629,11 +629,17 @@ static DEFINE_SPINLOCK(stats_flush_lock); ...@@ -629,11 +629,17 @@ static DEFINE_SPINLOCK(stats_flush_lock);
static DEFINE_PER_CPU(unsigned int, stats_updates); static DEFINE_PER_CPU(unsigned int, stats_updates);
static atomic_t stats_flush_threshold = ATOMIC_INIT(0); static atomic_t stats_flush_threshold = ATOMIC_INIT(0);
static inline void memcg_rstat_updated(struct mem_cgroup *memcg) static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
{ {
unsigned int x;
cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id());
if (!(__this_cpu_inc_return(stats_updates) % MEMCG_CHARGE_BATCH))
atomic_inc(&stats_flush_threshold); x = __this_cpu_add_return(stats_updates, abs(val));
if (x > MEMCG_CHARGE_BATCH) {
atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold);
__this_cpu_write(stats_updates, 0);
}
} }
static void __mem_cgroup_flush_stats(void) static void __mem_cgroup_flush_stats(void)
...@@ -656,7 +662,7 @@ void mem_cgroup_flush_stats(void) ...@@ -656,7 +662,7 @@ void mem_cgroup_flush_stats(void)
static void flush_memcg_stats_dwork(struct work_struct *w) static void flush_memcg_stats_dwork(struct work_struct *w)
{ {
mem_cgroup_flush_stats(); __mem_cgroup_flush_stats();
queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 2UL*HZ); queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 2UL*HZ);
} }
...@@ -672,7 +678,7 @@ void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) ...@@ -672,7 +678,7 @@ void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
return; return;
__this_cpu_add(memcg->vmstats_percpu->state[idx], val); __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
memcg_rstat_updated(memcg); memcg_rstat_updated(memcg, val);
} }
/* idx can be of type enum memcg_stat_item or node_stat_item. */ /* idx can be of type enum memcg_stat_item or node_stat_item. */
...@@ -705,7 +711,7 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, ...@@ -705,7 +711,7 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
/* Update lruvec */ /* Update lruvec */
__this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val);
memcg_rstat_updated(memcg); memcg_rstat_updated(memcg, val);
} }
/** /**
...@@ -789,7 +795,7 @@ void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, ...@@ -789,7 +795,7 @@ void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
return; return;
__this_cpu_add(memcg->vmstats_percpu->events[idx], count); __this_cpu_add(memcg->vmstats_percpu->events[idx], count);
memcg_rstat_updated(memcg); memcg_rstat_updated(memcg, count);
} }
static unsigned long memcg_events(struct mem_cgroup *memcg, int event) static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment