From patchwork Tue Jun 15 17:44:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12322557 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 998CCC48BDF for ; Tue, 15 Jun 2021 17:44:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 41F3E611CE for ; Tue, 15 Jun 2021 17:44:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 41F3E611CE Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B37F56B006E; Tue, 15 Jun 2021 13:44:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC09A6B0071; Tue, 15 Jun 2021 13:44:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EC1A6B0072; Tue, 15 Jun 2021 13:44:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0119.hostedemail.com [216.40.44.119]) by kanga.kvack.org (Postfix) with ESMTP id 586E66B006E for ; Tue, 15 Jun 2021 13:44:46 -0400 (EDT) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id D266E180AD804 for ; Tue, 15 Jun 2021 17:44:45 +0000 (UTC) X-FDA: 78256683330.34.AC3FBFE Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf28.hostedemail.com (Postfix) with ESMTP id 465C020010A8 for ; Tue, 15 Jun 2021 17:44:36 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id 61-20020aed21430000b029024e7e455d67so4267436qtc.16 for ; Tue, 15 Jun 2021 10:44:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=20nqL6qcCVh1AvFA3adHVcn9UAqdKdvXk0+eH6fWh7o=; b=m+Dyg5uwVyznaSJVLrsdBn75XEh0xyv7liBZGwWfTPbsybpDB18UQGHlxrGfJcAbAu PvQd0xgxx5xVpV4T430nt56XZeWq/mlgdOxaJVTcwl2F2BJiX75apbPEH89ycqsNK8Ey P3RqZTILjTqeJKydGAmZK4F5I1+GJBuiHgvxiCF6Ec3O78MK8hu9jaePxEJg8e93uIYv 8a8fh88cW+K3tWGH6qd8tiP/HJfiQUm8+HXThHY+ec99TaD8lp+zfY2Ld1jZMlY/nrmV zYMnFPA0waN5TKV6PcO5B/Cvw/wopjfrfphFlEZqBb2eSdsVLDOqq4II863RtZ/ifn80 1q+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=20nqL6qcCVh1AvFA3adHVcn9UAqdKdvXk0+eH6fWh7o=; b=M7HU1WPrFZIW/Rl/x8IN3pzQCbb5QhUu6gKp+mWrqHepcsILKu5pxOZ1gU+k718dF6 gmUDb5KIcK+TU3Yh1KCYxOSweiFt6XQS1KVY1kIhkkS5oCU71Od6q/VjXlCYDLDF9rc1 4kHFQj0HsfklPhG6JJbwDGWlkTA6mcASFdt9BYvZQVHKIzI6PWZO5MpuI4nJKWYpSmQF PAT//zvCjQ3kCzgqzCoAK+AtULeWw8EU9NoxYvFc0h24qIQ1EKD7Zuj/1gIM3sEJHyKh N2jIPEDubCdQKzolJRAQvOPO3dTPynmYZyvKkWPijZz4188LxyHzTmQqArQPDdQJV8Qa h6Ew== X-Gm-Message-State: AOAM533HVMAnNWrcVu8sQQxE4kOzKoMm89PaT8jW15qpNx3eZxttlqQ9 AkD6n6hObQahQu7UmFgzPhXxaNxWDhICng== X-Google-Smtp-Source: ABdhPJydvun+o6IPzXg8Ul67P4VrrhW7vltIRPiCs1YmM0dbnynwRa4fSol1BQuApDKPWYqGIdY7S4ePTHTpZQ== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:5fdb:fdf0:b7e3:fb19]) (user=shakeelb job=sendgmr) by 2002:a05:620a:e09:: with SMTP id y9mr778121qkm.359.1623779084624; Tue, 15 Jun 2021 10:44:44 -0700 (PDT) Date: Tue, 15 Jun 2021 10:44:34 -0700 Message-Id: <20210615174435.4174364-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.272.g935e593368-goog Subject: [PATCH v2 1/2] memcg: switch lruvec stats to rstat From: Shakeel Butt To: Tejun Heo , Johannes Weiner , Muchun Song Cc: Michal Hocko , Roman Gushchin , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Huang Ying , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=m+Dyg5uw; spf=pass (imf28.hostedemail.com: domain of 3DOfIYAgKCAs3slvppwmrzzrwp.nzxwty58-xxv6lnv.z2r@flex--shakeelb.bounces.google.com designates 209.85.160.202 as permitted sender) smtp.mailfrom=3DOfIYAgKCAs3slvppwmrzzrwp.nzxwty58-xxv6lnv.z2r@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 465C020010A8 X-Stat-Signature: hamggoqxkore69rmqd6yqu94y6z3df76 X-HE-Tag: 1623779076-843709 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: The commit 2d146aa3aa84 ("mm: memcontrol: switch to rstat") 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 patch adds the periodic flushing of the stats and thus remove the need to synchronously flushing the stats in the performance critical code paths. 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. [1] https://lore.kernel.org/linux-mm/20210311004449.1170308-1-ying.huang@intel.com/ Signed-off-by: Shakeel Butt --- Changes since v1: - no changes include/linux/memcontrol.h | 42 +++++++------ mm/memcontrol.c | 118 +++++++++++++------------------------ 2 files changed, 60 insertions(+), 100 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index bfe5c486f4ad..0bfa0409af22 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -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; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index dce5154fbb8f..6e24fd8c5301 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -671,46 +671,20 @@ static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) return x; } -static struct mem_cgroup_per_node * -parent_nodeinfo(struct mem_cgroup_per_node *pn, int nid) -{ - struct mem_cgroup *parent; - - parent = parent_mem_cgroup(pn->memcg); - if (!parent) - return NULL; - return parent->nodeinfo[nid]; -} - void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val) { struct mem_cgroup_per_node *pn; struct mem_cgroup *memcg; - long x, threshold = MEMCG_CHARGE_BATCH; pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); memcg = pn->memcg; - /* Update memcg */ - __mod_memcg_state(memcg, idx, val); - /* Update lruvec */ - __this_cpu_add(pn->lruvec_stat_local->count[idx], val); + __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); - if (vmstat_item_in_bytes(idx)) - threshold <<= PAGE_SHIFT; - - x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]); - if (unlikely(abs(x) > threshold)) { - pg_data_t *pgdat = lruvec_pgdat(lruvec); - struct mem_cgroup_per_node *pi; - - for (pi = pn; pi; pi = parent_nodeinfo(pi, pgdat->node_id)) - atomic_long_add(x, &pi->lruvec_stat[idx]); - x = 0; - } - __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x); + /* Update memcg */ + __mod_memcg_state(memcg, idx, val); } /** @@ -2289,40 +2263,13 @@ static void drain_all_stock(struct mem_cgroup *root_memcg) mutex_unlock(&percpu_charge_mutex); } -static void memcg_flush_lruvec_page_state(struct mem_cgroup *memcg, int cpu) -{ - int nid; - - for_each_node(nid) { - struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; - unsigned long stat[NR_VM_NODE_STAT_ITEMS]; - struct batched_lruvec_stat *lstatc; - int i; - - lstatc = per_cpu_ptr(pn->lruvec_stat_cpu, cpu); - for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { - stat[i] = lstatc->count[i]; - lstatc->count[i] = 0; - } - - do { - for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) - atomic_long_add(stat[i], &pn->lruvec_stat[i]); - } while ((pn = parent_nodeinfo(pn, nid))); - } -} - 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) - memcg_flush_lruvec_page_state(memcg, cpu); - return 0; } @@ -5126,17 +5073,9 @@ static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) if (!pn) return 1; - pn->lruvec_stat_local = alloc_percpu_gfp(struct lruvec_stat, - GFP_KERNEL_ACCOUNT); - if (!pn->lruvec_stat_local) { - kfree(pn); - return 1; - } - - pn->lruvec_stat_cpu = alloc_percpu_gfp(struct batched_lruvec_stat, - GFP_KERNEL_ACCOUNT); - if (!pn->lruvec_stat_cpu) { - free_percpu(pn->lruvec_stat_local); + pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu, + GFP_KERNEL_ACCOUNT); + if (!pn->lruvec_stats_percpu) { kfree(pn); return 1; } @@ -5157,8 +5096,7 @@ static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) if (!pn) return; - free_percpu(pn->lruvec_stat_cpu); - free_percpu(pn->lruvec_stat_local); + free_percpu(pn->lruvec_stats_percpu); kfree(pn); } @@ -5174,15 +5112,7 @@ static void __mem_cgroup_free(struct mem_cgroup *memcg) static void mem_cgroup_free(struct mem_cgroup *memcg) { - int cpu; - memcg_wb_domain_exit(memcg); - /* - * Flush percpu lruvec stats to guarantee the value - * correctness on parent's and all ancestor levels. - */ - for_each_online_cpu(cpu) - memcg_flush_lruvec_page_state(memcg, cpu); __mem_cgroup_free(memcg); } @@ -5415,7 +5345,7 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) struct mem_cgroup *parent = parent_mem_cgroup(memcg); struct memcg_vmstats_percpu *statc; long delta, v; - int i; + int i, nid; statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); @@ -5463,6 +5393,36 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) if (parent) parent->vmstats.events_pending[i] += delta; } + + for_each_node_state(nid, N_MEMORY) { + struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; + struct mem_cgroup_per_node *ppn = NULL; + struct lruvec_stats_percpu *lstatc; + + if (parent) + ppn = parent->nodeinfo[nid]; + + lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); + + for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { + delta = pn->lruvec_stats.state_pending[i]; + if (delta) + pn->lruvec_stats.state_pending[i] = 0; + + v = READ_ONCE(lstatc->state[i]); + if (v != lstatc->state_prev[i]) { + delta += v - lstatc->state_prev[i]; + lstatc->state_prev[i] = v; + } + + if (!delta) + continue; + + pn->lruvec_stats.state[i] += delta; + if (ppn) + ppn->lruvec_stats.state_pending[i] += delta; + } + } } #ifdef CONFIG_MMU @@ -6396,6 +6356,8 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) int i; struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + cgroup_rstat_flush(memcg->css.cgroup); + for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { int nid; From patchwork Tue Jun 15 17:44:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12322559 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 0DEE8C48BE5 for ; Tue, 15 Jun 2021 17:44:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B448061009 for ; Tue, 15 Jun 2021 17:44:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B448061009 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5931B6B0071; Tue, 15 Jun 2021 13:44:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5430F6B0072; Tue, 15 Jun 2021 13:44:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 395706B0073; Tue, 15 Jun 2021 13:44:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0043.hostedemail.com [216.40.44.43]) by kanga.kvack.org (Postfix) with ESMTP id 01E666B0071 for ; Tue, 15 Jun 2021 13:44:53 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 888C5181AC9CB for ; Tue, 15 Jun 2021 17:44:53 +0000 (UTC) X-FDA: 78256683666.16.A5730E8 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf26.hostedemail.com (Postfix) with ESMTP id A60A74000290 for ; Tue, 15 Jun 2021 17:44:47 +0000 (UTC) Received: by mail-qk1-f202.google.com with SMTP id a193-20020a3766ca0000b02903a9be00d619so28693315qkc.12 for ; Tue, 15 Jun 2021 10:44:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Iz29C08sFJm0taW2hiHDiec7o6H7fLe3J2KrZkSw4Wg=; b=tTnedSnwGXolczu44KDqPFYzqUhFG0Wfi6rTaBDUakFr3yiBFcBdAiG2yl48Dj/0TT PIASE5VNvQzGGumlUkwgy7qHxEFNiof1XX8Zzygi2ufxh9tS3vC83PdFfIt6kagXcJ7J XVyk4y2+4bWvYs6DN/mqlg9MxaJaBU98PI+7PChmapHsOl7F/Kui8Jb7npr1Fn/ck9ce zy/zNnetTlXf2jLLCF/Z1zWGeegyrUOEiLUSYsD58bqx6JM+YfsoTwwlgZUx0XfxNsxl atdHQ8ebsTUCq64FQEZd3/HcuYlIAlyORh53Rk3bQpGFM7RovXaARV3vOBDPiVb+SKz0 r8wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Iz29C08sFJm0taW2hiHDiec7o6H7fLe3J2KrZkSw4Wg=; b=fOjK7Dhl3FWbEaPhBp0y8c21ZRXuEfkAENcyG4xM4gr3p5WbpSZtf9juZsXg6G9RDv 2VU9i2uUE2KnnVwfeyqMUxHbCi7DoPsf8AbsRz7HNGECerc3mOAVAvwJcTnDA7aP91Rl eW89hgpMCgRtAycL2FAZBKWZIDxBMe1/sJoFzi5MEdF9JpwRLK3sSgp71tJWN4nh7wQE Gci4hbiTTEEIsPZfzmYWvzAVewd9Qnxcnm/xq1dGMotc0nm4AIfyABkj2wrTybqg9ku5 k+k94ijGy4CUV0vbhTL/sZ6NZcxlJjOyuKgzFpSGQg/cU2qsTYnKDYeR6VdGPKKcRYPP 1pPA== X-Gm-Message-State: AOAM532gh6mvCitGY2PaZloJHSN52mwr2Tyx23oFeFR1M9UqdMmAhdUw G9fvEnX7g9doAfoCrA1F1jIuefjGj84BuQ== X-Google-Smtp-Source: ABdhPJyFzjQESlViPskRerATPZ7JDg0A4sdXnLQRMhiPcO3tV6YS+CvI4rOWOOZlFz/96Qio2VI9e23HetYs2A== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:5fdb:fdf0:b7e3:fb19]) (user=shakeelb job=sendgmr) by 2002:a05:620a:142f:: with SMTP id k15mr795933qkj.45.1623779092427; Tue, 15 Jun 2021 10:44:52 -0700 (PDT) Date: Tue, 15 Jun 2021 10:44:35 -0700 In-Reply-To: <20210615174435.4174364-1-shakeelb@google.com> Message-Id: <20210615174435.4174364-2-shakeelb@google.com> Mime-Version: 1.0 References: <20210615174435.4174364-1-shakeelb@google.com> X-Mailer: git-send-email 2.32.0.272.g935e593368-goog Subject: [PATCH v2 2/2] memcg: periodically flush the memcg stats From: Shakeel Butt To: Tejun Heo , Johannes Weiner , Muchun Song Cc: Michal Hocko , Roman Gushchin , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Huang Ying , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=tTnedSnw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3FOfIYAgKCBMB0t3xx4uz77z4x.v75416DG-553Etv3.7Az@flex--shakeelb.bounces.google.com designates 209.85.222.202 as permitted sender) smtp.mailfrom=3FOfIYAgKCBMB0t3xx4uz77z4x.v75416DG-553Etv3.7Az@flex--shakeelb.bounces.google.com X-Stat-Signature: bbj7qsczkbjr3ognyrkyxy43zgdmkjgs X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: A60A74000290 X-HE-Tag: 1623779087-868111 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: 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. Instead of synchronously flushing the stats, this patch adds support of asynchronous periodic flushing of the memcg stats. For now the flushing period is hardcoded to 2*HZ but that can be changed later through maybe sysctl if need arise. This patch does add the explicit flushing in the kswapd thread as the number of kswapd threads which corresponds to the number of nodes on realistic machines are usually low. Signed-off-by: Shakeel Butt --- Changes since v1: - use system_unbound_wq for flushing the memcg stats include/linux/memcontrol.h | 10 ++++++++++ mm/memcontrol.c | 14 ++++++++++++++ mm/vmscan.c | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0bfa0409af22..f34214382a1c 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -991,6 +991,12 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, return x; } +static inline void mem_cgroup_flush_stats(void) +{ + if (!mem_cgroup_disabled()) + cgroup_rstat_flush(root_mem_cgroup->css.cgroup); +} + 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 +1406,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) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 6e24fd8c5301..5910658bac84 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -97,6 +97,10 @@ bool cgroup_memory_noswap __ro_after_init; static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq); #endif +/* Periodically flush memcg and lruvec stats. */ +static void flush_memcg_stats(struct work_struct *w); +static DECLARE_DEFERRABLE_WORK(stats_flush, flush_memcg_stats); + /* Whether legacy memory+swap accounting is active */ static bool do_memsw_account(void) { @@ -5248,6 +5252,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, 2UL*HZ); + return 0; } @@ -5339,6 +5347,12 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) memcg_wb_domain_size_changed(memcg); } +static void flush_memcg_stats(struct work_struct *w) +{ + cgroup_rstat_flush(root_mem_cgroup->css.cgroup); + queue_delayed_work(system_unbound_wq, &stats_flush, 2UL*HZ); +} + static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) { struct mem_cgroup *memcg = mem_cgroup_from_css(css); diff --git a/mm/vmscan.c b/mm/vmscan.c index a7602f71ec04..7bf9a4241dd9 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3893,6 +3893,12 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) sc.may_writepage = !laptop_mode && !nr_boost_reclaim; sc.may_swap = !nr_boost_reclaim; + /* + * Flush the memory cgroup stats, so that we read accurate + * per-memcg lruvec stats for heuristics later. + */ + mem_cgroup_flush_stats(); + /* * Do some background aging of the anon list, to give * pages a chance to be referenced before reclaiming. All