From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80940C433E0 for ; Tue, 9 Feb 2021 16:33:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E8E1E64D9D for ; Tue, 9 Feb 2021 16:33:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E8E1E64D9D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D91A78D0001; Tue, 9 Feb 2021 11:33:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB7C26B007B; Tue, 9 Feb 2021 11:33:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 991958D0003; Tue, 9 Feb 2021 11:33:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id 726458D0002 for ; Tue, 9 Feb 2021 11:33:39 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AF117AC1C for ; Tue, 9 Feb 2021 16:33:37 +0000 (UTC) X-FDA: 77799275274.25.fang99_400b1cf27608 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 591211804E3CC for ; Tue, 9 Feb 2021 16:33:37 +0000 (UTC) X-HE-Tag: fang99_400b1cf27608 X-Filterd-Recvd-Size: 22154 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 16:33:35 +0000 (UTC) Received: by mail-qk1-f176.google.com with SMTP id h8so8290142qkk.6 for ; Tue, 09 Feb 2021 08:33:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q1ztIauIeB5U2a4fZy+spShvzlgM+Lo/zsMtRCcpKrs=; b=runw3XCF3XoPbEQhRxvXLAMw09Ba/TKXAy9YLY4BN1S+c4m3S8ez1jwAgZ7LVo/REQ w6LaVrDdQPudNzvsedEAzA/mmRpH1FkxNIy6+hKFZPn0JbFSgH98zUIJQN3APAeBLziD w1bhrSjUAVkoMIYv+JdvsDlVc1RH2ngKTuI53twZAmVhxsJ87ilm7bAb/fl6WPJZzyDG m38HH/oqYbtHQPtQgD/9dTP9bYtWqnHQfO7re8url7xQ3o9dOUanqI929tEpvwU5gKws cAlJcTWxVTMo+6yv99fk7z040HmgT3hoYctfI91KPTMKn7rdu+rkIw3ShNzJUGWONS+4 JGIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q1ztIauIeB5U2a4fZy+spShvzlgM+Lo/zsMtRCcpKrs=; b=bqm5/YXm3o5WWCw5FxS+M8pe/y/2PYTIm7NfhVw8BwFanG0IZ5c3XnaJ2U9sXTE+tS zVmpMNA3n9L+jCzpamwOROd5N4sSFcmvcRYaJzTEOJE2lq4YDgQG0b7MHucg7zVt/SUU Mt5uaZNXGOTLfbvKuNLj5agg1sym0wweHzQic2vwVnKzPBmjTzVj4GeHtUHEGKAMIOsh 8U0Th+86p+hae/1GqOEpiJrE+8wk/ScSzoBvE6qr9XtSTV39f9v5/tJ8oe0/83MaSgQ4 ublK6IoMEU02oPtlGSYgcejW52V3/DLhSh8iSLL0nN+6RTuXd+IaDOtbsLvwbNyxXLs3 H+XQ== X-Gm-Message-State: AOAM532iazZ9XloTNrgkmy/jt9KL+0XlmgoKFEpnwyfiVrgMihBDPPe2 IEF4xk1m71eYp/F0eQ2lYxVD0Q== X-Google-Smtp-Source: ABdhPJz6fekvspb/X4isM3lh/LskjDSD/IjGma6RozcdzjpRJ31PU/oBQ571U1xomMCBMbYqArRLlA== X-Received: by 2002:a37:ac0d:: with SMTP id e13mr1753762qkm.214.1612888415239; Tue, 09 Feb 2021 08:33:35 -0800 (PST) Received: from localhost (70.44.39.90.res-cmts.bus.ptd.net. [70.44.39.90]) by smtp.gmail.com with ESMTPSA id i5sm52527qkg.32.2021.02.09.08.33.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Feb 2021 08:33:34 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Tejun Heo , Michal Hocko , Roman Gushchin , Shakeel Butt , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v3 6/8] mm: memcontrol: switch to rstat Date: Tue, 9 Feb 2021 11:33:02 -0500 Message-Id: <20210209163304.77088-7-hannes@cmpxchg.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210209163304.77088-1-hannes@cmpxchg.org> References: <20210209163304.77088-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Replace the memory controller's custom hierarchical stats code with the generic rstat infrastructure provided by the cgroup core. The current implementation does batched upward propagation from the write side (i.e. as stats change). The per-cpu batches introduce an error, which is multiplied by the number of subgroups in a tree. In systems with many CPUs and sizable cgroup trees, the error can be large enough to confuse users (e.g. 32 batch pages * 32 CPUs * 32 subgroups results in an error of up to 128M per stat item). This can entirely swallow allocation bursts inside a workload that the user is expecting to see reflected in the statistics. In the past, we've done read-side aggregation, where a memory.stat read would have to walk the entire subtree and add up per-cpu counts. This became problematic with lazily-freed cgroups: we could have large subtrees where most cgroups were entirely idle. Hence the switch to change-driven upward propagation. Unfortunately, it needed to trade accuracy for speed due to the write side being so hot. Rstat combines the best of both worlds: from the write side, it cheaply maintains a queue of cgroups that have pending changes, so that the read side can do selective tree aggregation. This way the reported stats will always be precise and recent as can be, while the aggregation can skip over potentially large numbers of idle cgroups. The way rstat works is that it implements a tree for tracking cgroups with pending local changes, as well as a flush function that walks the tree upwards. The controller then drives this by 1) telling rstat when a local cgroup stat changes (e.g. mod_memcg_state) and 2) when a flush is required to get uptodate hierarchy stats for a given subtree (e.g. when memory.stat is read). The controller also provides a flush callback that is called during the rstat flush walk for each cgroup and aggregates its local per-cpu counters and propagates them upwards. This adds a second vmstats to struct mem_cgroup (MEMCG_NR_STAT + NR_VM_EVENT_ITEMS) to track pending subtree deltas during upward aggregation. It removes 3 words from the per-cpu data. It eliminates memcg_exact_page_state(), since memcg_page_state() is now exact. Signed-off-by: Johannes Weiner Reviewed-by: Roman Gushchin Acked-by: Michal Hocko Reviewed-by: Shakeel Butt --- include/linux/memcontrol.h | 67 ++++++----- mm/memcontrol.c | 223 +++++++++++++++---------------------- 2 files changed, 132 insertions(+), 158 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 20ecdfae3289..a8c7a0ccc759 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -76,10 +76,27 @@ enum mem_cgroup_events_target { }; =20 struct memcg_vmstats_percpu { - long stat[MEMCG_NR_STAT]; - unsigned long events[NR_VM_EVENT_ITEMS]; - unsigned long nr_page_events; - unsigned long targets[MEM_CGROUP_NTARGETS]; + /* Local (CPU and cgroup) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_VM_EVENT_ITEMS]; + + /* Delta calculation for lockless upward propagation */ + long state_prev[MEMCG_NR_STAT]; + unsigned long events_prev[NR_VM_EVENT_ITEMS]; + + /* Cgroup1: threshold notifications & softlimit tree updates */ + unsigned long nr_page_events; + unsigned long targets[MEM_CGROUP_NTARGETS]; +}; + +struct memcg_vmstats { + /* Aggregated (CPU and subtree) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_VM_EVENT_ITEMS]; + + /* Pending child counts during tree propagation */ + long state_pending[MEMCG_NR_STAT]; + unsigned long events_pending[NR_VM_EVENT_ITEMS]; }; =20 struct mem_cgroup_reclaim_iter { @@ -287,8 +304,8 @@ struct mem_cgroup { =20 MEMCG_PADDING(_pad1_); =20 - atomic_long_t vmstats[MEMCG_NR_STAT]; - atomic_long_t vmevents[NR_VM_EVENT_ITEMS]; + /* memory.stat */ + struct memcg_vmstats vmstats; =20 /* memory.events */ atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS]; @@ -315,10 +332,6 @@ struct mem_cgroup { atomic_t moving_account; struct task_struct *move_lock_task; =20 - /* Legacy local VM stats and events */ - struct memcg_vmstats_percpu __percpu *vmstats_local; - - /* Subtree VM stats and events (batched updates) */ struct memcg_vmstats_percpu __percpu *vmstats_percpu; =20 #ifdef CONFIG_CGROUP_WRITEBACK @@ -942,10 +955,6 @@ static inline void mod_memcg_lruvec_state(struct lru= vec *lruvec, local_irq_restore(flags); } =20 -unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, - gfp_t gfp_mask, - unsigned long *total_scanned); - void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item i= dx, unsigned long count); =20 @@ -1028,6 +1037,10 @@ static inline void memcg_memory_event_mm(struct mm= _struct *mm, void mem_cgroup_split_huge_fixup(struct page *head); #endif =20 +unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, + gfp_t gfp_mask, + unsigned long *total_scanned); + #else /* CONFIG_MEMCG */ =20 #define MEM_CGROUP_ID_SHIFT 0 @@ -1136,6 +1149,10 @@ static inline bool lruvec_holds_page_lru_lock(stru= ct page *page, return lruvec =3D=3D &pgdat->__lruvec; } =20 +static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page= *page) +{ +} + static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *me= mcg) { return NULL; @@ -1349,18 +1366,6 @@ static inline void mod_lruvec_kmem_state(void *p, = enum node_stat_item idx, mod_node_page_state(page_pgdat(page), idx, val); } =20 -static inline -unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, - gfp_t gfp_mask, - unsigned long *total_scanned) -{ - return 0; -} - -static inline void mem_cgroup_split_huge_fixup(struct page *head) -{ -} - static inline void count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, unsigned long count) @@ -1383,8 +1388,16 @@ void count_memcg_event_mm(struct mm_struct *mm, en= um vm_event_item idx) { } =20 -static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page= *page) +static inline void mem_cgroup_split_huge_fixup(struct page *head) +{ +} + +static inline +unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, + gfp_t gfp_mask, + unsigned long *total_scanned) { + return 0; } #endif /* CONFIG_MEMCG */ =20 diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2f97cb4cef6d..bc0979257551 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -757,6 +757,11 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup= _tree_per_node *mctz) return mz; } =20 +static void memcg_flush_vmstats(struct mem_cgroup *memcg) +{ + cgroup_rstat_flush(memcg->css.cgroup); +} + /** * __mod_memcg_state - update cgroup memory statistics * @memcg: the memory cgroup @@ -765,37 +770,17 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgrou= p_tree_per_node *mctz) */ void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) { - long x, threshold =3D MEMCG_CHARGE_BATCH; - if (mem_cgroup_disabled()) return; =20 - if (memcg_stat_item_in_bytes(idx)) - threshold <<=3D PAGE_SHIFT; - - x =3D val + __this_cpu_read(memcg->vmstats_percpu->stat[idx]); - if (unlikely(abs(x) > threshold)) { - struct mem_cgroup *mi; - - /* - * Batch local counters to keep them in sync with - * the hierarchical ones. - */ - __this_cpu_add(memcg->vmstats_local->stat[idx], x); - for (mi =3D memcg; mi; mi =3D parent_mem_cgroup(mi)) - atomic_long_add(x, &mi->vmstats[idx]); - x =3D 0; - } - __this_cpu_write(memcg->vmstats_percpu->stat[idx], x); + __this_cpu_add(memcg->vmstats_percpu->state[idx], val); + cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); } =20 -/* - * idx can be of type enum memcg_stat_item or node_stat_item. - * Keep in sync with memcg_exact_page_state(). - */ +/* idx can be of type enum memcg_stat_item or node_stat_item. */ static unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { - long x =3D atomic_long_read(&memcg->vmstats[idx]); + long x =3D READ_ONCE(memcg->vmstats.state[idx]); #ifdef CONFIG_SMP if (x < 0) x =3D 0; @@ -803,17 +788,14 @@ static unsigned long memcg_page_state(struct mem_cg= roup *memcg, int idx) return x; } =20 -/* - * idx can be of type enum memcg_stat_item or node_stat_item. - * Keep in sync with memcg_exact_page_state(). - */ +/* idx can be of type enum memcg_stat_item or node_stat_item. */ static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, in= t idx) { long x =3D 0; int cpu; =20 for_each_possible_cpu(cpu) - x +=3D per_cpu(memcg->vmstats_local->stat[idx], cpu); + x +=3D per_cpu(memcg->vmstats_percpu->state[idx], cpu); #ifdef CONFIG_SMP if (x < 0) x =3D 0; @@ -936,30 +918,16 @@ void __mod_lruvec_kmem_state(void *p, enum node_sta= t_item idx, int val) void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item i= dx, unsigned long count) { - unsigned long x; - if (mem_cgroup_disabled()) return; =20 - x =3D count + __this_cpu_read(memcg->vmstats_percpu->events[idx]); - if (unlikely(x > MEMCG_CHARGE_BATCH)) { - struct mem_cgroup *mi; - - /* - * Batch local counters to keep them in sync with - * the hierarchical ones. - */ - __this_cpu_add(memcg->vmstats_local->events[idx], x); - for (mi =3D memcg; mi; mi =3D parent_mem_cgroup(mi)) - atomic_long_add(x, &mi->vmevents[idx]); - x =3D 0; - } - __this_cpu_write(memcg->vmstats_percpu->events[idx], x); + __this_cpu_add(memcg->vmstats_percpu->events[idx], count); + cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); } =20 static unsigned long memcg_events(struct mem_cgroup *memcg, int event) { - return atomic_long_read(&memcg->vmevents[event]); + return READ_ONCE(memcg->vmstats.events[event]); } =20 static unsigned long memcg_events_local(struct mem_cgroup *memcg, int ev= ent) @@ -968,7 +936,7 @@ static unsigned long memcg_events_local(struct mem_cg= roup *memcg, int event) int cpu; =20 for_each_possible_cpu(cpu) - x +=3D per_cpu(memcg->vmstats_local->events[event], cpu); + x +=3D per_cpu(memcg->vmstats_percpu->events[event], cpu); return x; } =20 @@ -1631,6 +1599,7 @@ static char *memory_stat_format(struct mem_cgroup *= memcg) * * Current memory state: */ + memcg_flush_vmstats(memcg); =20 for (i =3D 0; i < ARRAY_SIZE(memory_stats); i++) { u64 size; @@ -2450,22 +2419,11 @@ static int memcg_hotplug_cpu_dead(unsigned int cp= u) drain_stock(stock); =20 for_each_mem_cgroup(memcg) { - struct memcg_vmstats_percpu *statc; int i; =20 - statc =3D per_cpu_ptr(memcg->vmstats_percpu, cpu); - - for (i =3D 0; i < MEMCG_NR_STAT; i++) { + for (i =3D 0; i < NR_VM_NODE_STAT_ITEMS; i++) { int nid; =20 - if (statc->stat[i]) { - mod_memcg_state(memcg, i, statc->stat[i]); - statc->stat[i] =3D 0; - } - - if (i >=3D NR_VM_NODE_STAT_ITEMS) - continue; - for_each_node(nid) { struct batched_lruvec_stat *lstatc; struct mem_cgroup_per_node *pn; @@ -2484,13 +2442,6 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu= ) } } } - - for (i =3D 0; i < NR_VM_EVENT_ITEMS; i++) { - if (statc->events[i]) { - count_memcg_events(memcg, i, statc->events[i]); - statc->events[i] =3D 0; - } - } } =20 return 0; @@ -3619,6 +3570,7 @@ static unsigned long mem_cgroup_usage(struct mem_cg= roup *memcg, bool swap) unsigned long val; =20 if (mem_cgroup_is_root(memcg)) { + memcg_flush_vmstats(memcg); val =3D memcg_page_state(memcg, NR_FILE_PAGES) + memcg_page_state(memcg, NR_ANON_MAPPED); if (swap) @@ -3683,26 +3635,15 @@ static u64 mem_cgroup_read_u64(struct cgroup_subs= ys_state *css, } } =20 -static void memcg_flush_percpu_vmstats(struct mem_cgroup *memcg) +static void memcg_flush_lruvec_page_state(struct mem_cgroup *memcg) { - unsigned long stat[MEMCG_NR_STAT] =3D {0}; - struct mem_cgroup *mi; - int node, cpu, i; - - for_each_online_cpu(cpu) - for (i =3D 0; i < MEMCG_NR_STAT; i++) - stat[i] +=3D per_cpu(memcg->vmstats_percpu->stat[i], cpu); - - for (mi =3D memcg; mi; mi =3D parent_mem_cgroup(mi)) - for (i =3D 0; i < MEMCG_NR_STAT; i++) - atomic_long_add(stat[i], &mi->vmstats[i]); + int node; =20 for_each_node(node) { struct mem_cgroup_per_node *pn =3D memcg->nodeinfo[node]; + unsigned long stat[NR_VM_NODE_STAT_ITEMS] =3D { 0 }; struct mem_cgroup_per_node *pi; - - for (i =3D 0; i < NR_VM_NODE_STAT_ITEMS; i++) - stat[i] =3D 0; + int cpu, i; =20 for_each_online_cpu(cpu) for (i =3D 0; i < NR_VM_NODE_STAT_ITEMS; i++) @@ -3715,25 +3656,6 @@ static void memcg_flush_percpu_vmstats(struct mem_= cgroup *memcg) } } =20 -static void memcg_flush_percpu_vmevents(struct mem_cgroup *memcg) -{ - unsigned long events[NR_VM_EVENT_ITEMS]; - struct mem_cgroup *mi; - int cpu, i; - - for (i =3D 0; i < NR_VM_EVENT_ITEMS; i++) - events[i] =3D 0; - - for_each_online_cpu(cpu) - for (i =3D 0; i < NR_VM_EVENT_ITEMS; i++) - events[i] +=3D per_cpu(memcg->vmstats_percpu->events[i], - cpu); - - for (mi =3D memcg; mi; mi =3D parent_mem_cgroup(mi)) - for (i =3D 0; i < NR_VM_EVENT_ITEMS; i++) - atomic_long_add(events[i], &mi->vmevents[i]); -} - #ifdef CONFIG_MEMCG_KMEM static int memcg_online_kmem(struct mem_cgroup *memcg) { @@ -4050,6 +3972,8 @@ static int memcg_numa_stat_show(struct seq_file *m,= void *v) int nid; struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); =20 + memcg_flush_vmstats(memcg); + for (stat =3D stats; stat < stats + ARRAY_SIZE(stats); stat++) { seq_printf(m, "%s=3D%lu", stat->name, mem_cgroup_nr_lru_pages(memcg, stat->lru_mask, @@ -4120,6 +4044,8 @@ static int memcg_stat_show(struct seq_file *m, void= *v) =20 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) !=3D ARRAY_SIZE(memcg1_stats= )); =20 + memcg_flush_vmstats(memcg); + for (i =3D 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; =20 @@ -4596,22 +4522,6 @@ struct wb_domain *mem_cgroup_wb_domain(struct bdi_= writeback *wb) return &memcg->cgwb_domain; } =20 -/* - * idx can be of type enum memcg_stat_item or node_stat_item. - * Keep in sync with memcg_exact_page(). - */ -static unsigned long memcg_exact_page_state(struct mem_cgroup *memcg, in= t idx) -{ - long x =3D atomic_long_read(&memcg->vmstats[idx]); - int cpu; - - for_each_online_cpu(cpu) - x +=3D per_cpu_ptr(memcg->vmstats_percpu, cpu)->stat[idx]; - if (x < 0) - x =3D 0; - return x; -} - /** * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg * @wb: bdi_writeback in question @@ -4637,13 +4547,14 @@ void mem_cgroup_wb_stats(struct bdi_writeback *wb= , unsigned long *pfilepages, struct mem_cgroup *memcg =3D mem_cgroup_from_css(wb->memcg_css); struct mem_cgroup *parent; =20 - *pdirty =3D memcg_exact_page_state(memcg, NR_FILE_DIRTY); + memcg_flush_vmstats(memcg); =20 - *pwriteback =3D memcg_exact_page_state(memcg, NR_WRITEBACK); - *pfilepages =3D memcg_exact_page_state(memcg, NR_INACTIVE_FILE) + - memcg_exact_page_state(memcg, NR_ACTIVE_FILE); - *pheadroom =3D PAGE_COUNTER_MAX; + *pdirty =3D memcg_page_state(memcg, NR_FILE_DIRTY); + *pwriteback =3D memcg_page_state(memcg, NR_WRITEBACK); + *pfilepages =3D memcg_page_state(memcg, NR_INACTIVE_FILE) + + memcg_page_state(memcg, NR_ACTIVE_FILE); =20 + *pheadroom =3D PAGE_COUNTER_MAX; while ((parent =3D parent_mem_cgroup(memcg))) { unsigned long ceiling =3D min(READ_ONCE(memcg->memory.max), READ_ONCE(memcg->memory.high)); @@ -5275,7 +5186,6 @@ static void __mem_cgroup_free(struct mem_cgroup *me= mcg) for_each_node(node) free_mem_cgroup_per_node_info(memcg, node); free_percpu(memcg->vmstats_percpu); - free_percpu(memcg->vmstats_local); kfree(memcg); } =20 @@ -5283,11 +5193,10 @@ static void mem_cgroup_free(struct mem_cgroup *me= mcg) { memcg_wb_domain_exit(memcg); /* - * Flush percpu vmstats and vmevents to guarantee the value correctness - * on parent's and all ancestor levels. + * Flush percpu lruvec stats to guarantee the value + * correctness on parent's and all ancestor levels. */ - memcg_flush_percpu_vmstats(memcg); - memcg_flush_percpu_vmevents(memcg); + memcg_flush_lruvec_page_state(memcg); __mem_cgroup_free(memcg); } =20 @@ -5314,11 +5223,6 @@ static struct mem_cgroup *mem_cgroup_alloc(void) goto fail; } =20 - memcg->vmstats_local =3D alloc_percpu_gfp(struct memcg_vmstats_percpu, - GFP_KERNEL_ACCOUNT); - if (!memcg->vmstats_local) - goto fail; - memcg->vmstats_percpu =3D alloc_percpu_gfp(struct memcg_vmstats_percpu, GFP_KERNEL_ACCOUNT); if (!memcg->vmstats_percpu) @@ -5518,6 +5422,62 @@ static void mem_cgroup_css_reset(struct cgroup_sub= sys_state *css) memcg_wb_domain_size_changed(memcg); } =20 +static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, = int cpu) +{ + struct mem_cgroup *memcg =3D mem_cgroup_from_css(css); + struct mem_cgroup *parent =3D parent_mem_cgroup(memcg); + struct memcg_vmstats_percpu *statc; + long delta, v; + int i; + + statc =3D per_cpu_ptr(memcg->vmstats_percpu, cpu); + + for (i =3D 0; i < MEMCG_NR_STAT; i++) { + /* + * Collect the aggregated propagation counts of groups + * below us. We're in a per-cpu loop here and this is + * a global counter, so the first cycle will get them. + */ + delta =3D memcg->vmstats.state_pending[i]; + if (delta) + memcg->vmstats.state_pending[i] =3D 0; + + /* Add CPU changes on this level since the last flush */ + v =3D READ_ONCE(statc->state[i]); + if (v !=3D statc->state_prev[i]) { + delta +=3D v - statc->state_prev[i]; + statc->state_prev[i] =3D v; + } + + if (!delta) + continue; + + /* Aggregate counts on this level and propagate upwards */ + memcg->vmstats.state[i] +=3D delta; + if (parent) + parent->vmstats.state_pending[i] +=3D delta; + } + + for (i =3D 0; i < NR_VM_EVENT_ITEMS; i++) { + delta =3D memcg->vmstats.events_pending[i]; + if (delta) + memcg->vmstats.events_pending[i] =3D 0; + + v =3D READ_ONCE(statc->events[i]); + if (v !=3D statc->events_prev[i]) { + delta +=3D v - statc->events_prev[i]; + statc->events_prev[i] =3D v; + } + + if (!delta) + continue; + + memcg->vmstats.events[i] +=3D delta; + if (parent) + parent->vmstats.events_pending[i] +=3D delta; + } +} + #ifdef CONFIG_MMU /* Handlers for move charge at task migration. */ static int mem_cgroup_do_precharge(unsigned long count) @@ -6571,6 +6531,7 @@ struct cgroup_subsys memory_cgrp_subsys =3D { .css_released =3D mem_cgroup_css_released, .css_free =3D mem_cgroup_css_free, .css_reset =3D mem_cgroup_css_reset, + .css_rstat_flush =3D mem_cgroup_css_rstat_flush, .can_attach =3D mem_cgroup_can_attach, .cancel_attach =3D mem_cgroup_cancel_attach, .post_attach =3D mem_cgroup_move_task, --=20 2.30.0