Commit 205b20cc authored by Johannes Weiner's avatar Johannes Weiner Committed by Linus Torvalds

mm: memcontrol: make cgroup stats and events query API explicitly local

Patch series "mm: memcontrol: memory.stat cost & correctness".

The cgroup memory.stat file holds recursive statistics for the entire
subtree.  The current implementation does this tree walk on-demand
whenever the file is read.  This is giving us problems in production.

1. The cost of aggregating the statistics on-demand is high.  A lot of
   system service cgroups are mostly idle and their stats don't change
   between reads, yet we always have to check them.  There are also always
   some lazily-dying cgroups sitting around that are pinned by a handful
   of remaining page cache; the same applies to them.

   In an application that periodically monitors memory.stat in our
   fleet, we have seen the aggregation consume up to 5% CPU time.

2. When cgroups die and disappear from the cgroup tree, so do their
   accumulated vm events.  The result is that the event counters at
   higher-level cgroups can go backwards and confuse some of our
   automation, let alone people looking at the graphs over time.

To address both issues, this patch series changes the stat
implementation to spill counts upwards when the counters change.

The upward spilling is batched using the existing per-cpu cache.  In a
sparse file stress test with 5 level cgroup nesting, the additional cost
of the flushing was negligible (a little under 1% of CPU at 100% CPU
utilization, compared to the 5% of reading memory.stat during regular
operation).

This patch (of 4):

memcg_page_state(), lruvec_page_state(), memcg_sum_events() are
currently returning the state of the local memcg or lruvec, not the
recursive state.

In practice there is a demand for both versions, although the callers
that want the recursive counts currently sum them up by hand.

Per default, cgroups are considered recursive entities and generally we
expect more users of the recursive counters, with the local counts being
special cases.  To reflect that in the name, add a _local suffix to the
current implementations.

The following patch will re-incarnate these functions with recursive
semantics, but with an O(1) implementation.

[hannes@cmpxchg.org: fix bisection hole]
  Link: http://lkml.kernel.org/r/20190417160347.GC23013@cmpxchg.org
Link: http://lkml.kernel.org/r/20190412151507.2769-2-hannes@cmpxchg.orgSigned-off-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
Reviewed-by: default avatarShakeel Butt <shakeelb@google.com>
Reviewed-by: default avatarRoman Gushchin <guro@fb.com>
Cc: Michal Hocko <mhocko@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 6a024330
...@@ -554,7 +554,7 @@ void unlock_page_memcg(struct page *page); ...@@ -554,7 +554,7 @@ void unlock_page_memcg(struct page *page);
* 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.
* Keep in sync with memcg_exact_page_state(). * Keep in sync with memcg_exact_page_state().
*/ */
static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
int idx) int idx)
{ {
long x = atomic_long_read(&memcg->vmstats[idx]); long x = atomic_long_read(&memcg->vmstats[idx]);
...@@ -624,7 +624,7 @@ static inline void mod_memcg_page_state(struct page *page, ...@@ -624,7 +624,7 @@ static inline void mod_memcg_page_state(struct page *page,
mod_memcg_state(page->mem_cgroup, idx, val); mod_memcg_state(page->mem_cgroup, idx, val);
} }
static inline unsigned long lruvec_page_state(struct lruvec *lruvec, static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
enum node_stat_item idx) enum node_stat_item idx)
{ {
struct mem_cgroup_per_node *pn; struct mem_cgroup_per_node *pn;
...@@ -1011,7 +1011,7 @@ static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) ...@@ -1011,7 +1011,7 @@ static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
{ {
} }
static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
int idx) int idx)
{ {
return 0; return 0;
...@@ -1041,7 +1041,7 @@ static inline void mod_memcg_page_state(struct page *page, ...@@ -1041,7 +1041,7 @@ static inline void mod_memcg_page_state(struct page *page,
{ {
} }
static inline unsigned long lruvec_page_state(struct lruvec *lruvec, static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
enum node_stat_item idx) enum node_stat_item idx)
{ {
return node_page_state(lruvec_pgdat(lruvec), idx); return node_page_state(lruvec_pgdat(lruvec), idx);
......
...@@ -687,7 +687,7 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) ...@@ -687,7 +687,7 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
return mz; return mz;
} }
static unsigned long memcg_sum_events(struct mem_cgroup *memcg, static unsigned long memcg_events_local(struct mem_cgroup *memcg,
int event) int event)
{ {
return atomic_long_read(&memcg->vmevents[event]); return atomic_long_read(&memcg->vmevents[event]);
...@@ -1325,12 +1325,14 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) ...@@ -1325,12 +1325,14 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
if (memcg1_stats[i] == MEMCG_SWAP && !do_swap_account) if (memcg1_stats[i] == MEMCG_SWAP && !do_swap_account)
continue; continue;
pr_cont(" %s:%luKB", memcg1_stat_names[i], pr_cont(" %s:%luKB", memcg1_stat_names[i],
K(memcg_page_state(iter, memcg1_stats[i]))); K(memcg_page_state_local(iter,
memcg1_stats[i])));
} }
for (i = 0; i < NR_LRU_LISTS; i++) for (i = 0; i < NR_LRU_LISTS; i++)
pr_cont(" %s:%luKB", mem_cgroup_lru_names[i], pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
K(memcg_page_state(iter, NR_LRU_BASE + i))); K(memcg_page_state_local(iter,
NR_LRU_BASE + i)));
pr_cont("\n"); pr_cont("\n");
} }
...@@ -1396,13 +1398,13 @@ static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, ...@@ -1396,13 +1398,13 @@ static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
{ {
struct lruvec *lruvec = mem_cgroup_lruvec(NODE_DATA(nid), memcg); struct lruvec *lruvec = mem_cgroup_lruvec(NODE_DATA(nid), memcg);
if (lruvec_page_state(lruvec, NR_INACTIVE_FILE) || if (lruvec_page_state_local(lruvec, NR_INACTIVE_FILE) ||
lruvec_page_state(lruvec, NR_ACTIVE_FILE)) lruvec_page_state_local(lruvec, NR_ACTIVE_FILE))
return true; return true;
if (noswap || !total_swap_pages) if (noswap || !total_swap_pages)
return false; return false;
if (lruvec_page_state(lruvec, NR_INACTIVE_ANON) || if (lruvec_page_state_local(lruvec, NR_INACTIVE_ANON) ||
lruvec_page_state(lruvec, NR_ACTIVE_ANON)) lruvec_page_state_local(lruvec, NR_ACTIVE_ANON))
return true; return true;
return false; return false;
...@@ -2961,16 +2963,16 @@ static void accumulate_vmstats(struct mem_cgroup *memcg, ...@@ -2961,16 +2963,16 @@ static void accumulate_vmstats(struct mem_cgroup *memcg,
for_each_mem_cgroup_tree(mi, memcg) { for_each_mem_cgroup_tree(mi, memcg) {
for (i = 0; i < acc->vmstats_size; i++) for (i = 0; i < acc->vmstats_size; i++)
acc->vmstats[i] += memcg_page_state(mi, acc->vmstats[i] += memcg_page_state_local(mi,
acc->vmstats_array ? acc->vmstats_array[i] : i); acc->vmstats_array ? acc->vmstats_array[i] : i);
for (i = 0; i < acc->vmevents_size; i++) for (i = 0; i < acc->vmevents_size; i++)
acc->vmevents[i] += memcg_sum_events(mi, acc->vmevents[i] += memcg_events_local(mi,
acc->vmevents_array acc->vmevents_array
? acc->vmevents_array[i] : i); ? acc->vmevents_array[i] : i);
for (i = 0; i < NR_LRU_LISTS; i++) for (i = 0; i < NR_LRU_LISTS; i++)
acc->lru_pages[i] += memcg_page_state(mi, acc->lru_pages[i] += memcg_page_state_local(mi,
NR_LRU_BASE + i); NR_LRU_BASE + i);
} }
} }
...@@ -2983,10 +2985,10 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) ...@@ -2983,10 +2985,10 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
struct mem_cgroup *iter; struct mem_cgroup *iter;
for_each_mem_cgroup_tree(iter, memcg) { for_each_mem_cgroup_tree(iter, memcg) {
val += memcg_page_state(iter, MEMCG_CACHE); val += memcg_page_state_local(iter, MEMCG_CACHE);
val += memcg_page_state(iter, MEMCG_RSS); val += memcg_page_state_local(iter, MEMCG_RSS);
if (swap) if (swap)
val += memcg_page_state(iter, MEMCG_SWAP); val += memcg_page_state_local(iter, MEMCG_SWAP);
} }
} else { } else {
if (!swap) if (!swap)
...@@ -3328,7 +3330,7 @@ static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, ...@@ -3328,7 +3330,7 @@ static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
for_each_lru(lru) { for_each_lru(lru) {
if (!(BIT(lru) & lru_mask)) if (!(BIT(lru) & lru_mask))
continue; continue;
nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru); nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
} }
return nr; return nr;
} }
...@@ -3342,7 +3344,7 @@ static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, ...@@ -3342,7 +3344,7 @@ static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
for_each_lru(lru) { for_each_lru(lru) {
if (!(BIT(lru) & lru_mask)) if (!(BIT(lru) & lru_mask))
continue; continue;
nr += memcg_page_state(memcg, NR_LRU_BASE + lru); nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
} }
return nr; return nr;
} }
...@@ -3427,17 +3429,17 @@ static int memcg_stat_show(struct seq_file *m, void *v) ...@@ -3427,17 +3429,17 @@ static int memcg_stat_show(struct seq_file *m, void *v)
if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
continue; continue;
seq_printf(m, "%s %lu\n", memcg1_stat_names[i], seq_printf(m, "%s %lu\n", memcg1_stat_names[i],
memcg_page_state(memcg, memcg1_stats[i]) * memcg_page_state_local(memcg, memcg1_stats[i]) *
PAGE_SIZE); PAGE_SIZE);
} }
for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
seq_printf(m, "%s %lu\n", memcg1_event_names[i], seq_printf(m, "%s %lu\n", memcg1_event_names[i],
memcg_sum_events(memcg, memcg1_events[i])); memcg_events_local(memcg, memcg1_events[i]));
for (i = 0; i < NR_LRU_LISTS; i++) for (i = 0; i < NR_LRU_LISTS; i++)
seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i], seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
memcg_page_state(memcg, NR_LRU_BASE + i) * memcg_page_state_local(memcg, NR_LRU_BASE + i) *
PAGE_SIZE); PAGE_SIZE);
/* Hierarchical information */ /* Hierarchical information */
......
...@@ -346,7 +346,7 @@ unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone ...@@ -346,7 +346,7 @@ unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone
int zid; int zid;
if (!mem_cgroup_disabled()) if (!mem_cgroup_disabled())
lru_size = lruvec_page_state(lruvec, NR_LRU_BASE + lru); lru_size = lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
else else
lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru); lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
...@@ -2150,7 +2150,7 @@ static bool inactive_list_is_low(struct lruvec *lruvec, bool file, ...@@ -2150,7 +2150,7 @@ static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
* is being established. Disable active list protection to get * is being established. Disable active list protection to get
* rid of the stale workingset quickly. * rid of the stale workingset quickly.
*/ */
refaults = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE); refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE);
if (file && actual_reclaim && lruvec->refaults != refaults) { if (file && actual_reclaim && lruvec->refaults != refaults) {
inactive_ratio = 0; inactive_ratio = 0;
} else { } else {
...@@ -2912,7 +2912,7 @@ static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat) ...@@ -2912,7 +2912,7 @@ static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
struct lruvec *lruvec; struct lruvec *lruvec;
lruvec = mem_cgroup_lruvec(pgdat, memcg); lruvec = mem_cgroup_lruvec(pgdat, memcg);
refaults = lruvec_page_state(lruvec, WORKINGSET_ACTIVATE); refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE);
lruvec->refaults = refaults; lruvec->refaults = refaults;
} while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL))); } while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
} }
......
...@@ -430,9 +430,10 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker, ...@@ -430,9 +430,10 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
lruvec = mem_cgroup_lruvec(NODE_DATA(sc->nid), sc->memcg); lruvec = mem_cgroup_lruvec(NODE_DATA(sc->nid), sc->memcg);
for (pages = 0, i = 0; i < NR_LRU_LISTS; i++) for (pages = 0, i = 0; i < NR_LRU_LISTS; i++)
pages += lruvec_page_state(lruvec, NR_LRU_BASE + i); pages += lruvec_page_state_local(lruvec,
pages += lruvec_page_state(lruvec, NR_SLAB_RECLAIMABLE); NR_LRU_BASE + i);
pages += lruvec_page_state(lruvec, NR_SLAB_UNRECLAIMABLE); pages += lruvec_page_state_local(lruvec, NR_SLAB_RECLAIMABLE);
pages += lruvec_page_state_local(lruvec, NR_SLAB_UNRECLAIMABLE);
} else } else
#endif #endif
pages = node_present_pages(sc->nid); pages = node_present_pages(sc->nid);
......
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