From patchwork Fri Jun 4 01:56:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12298593 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=unavailable 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 003D2C47097 for ; Fri, 4 Jun 2021 02:03:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 91F2C613FA for ; Fri, 4 Jun 2021 02:03:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 91F2C613FA 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 17F216B0036; Thu, 3 Jun 2021 22:03:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 130B96B006C; Thu, 3 Jun 2021 22:03:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB1C6B006E; Thu, 3 Jun 2021 22:03:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0183.hostedemail.com [216.40.44.183]) by kanga.kvack.org (Postfix) with ESMTP id BC92E6B0036 for ; Thu, 3 Jun 2021 22:03:19 -0400 (EDT) Received: from smtpin35.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5C131EFBE for ; Fri, 4 Jun 2021 02:03:19 +0000 (UTC) X-FDA: 78214394118.35.BE0D4F5 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf04.hostedemail.com (Postfix) with ESMTP id 664B1351C for ; Fri, 4 Jun 2021 02:03:05 +0000 (UTC) Received: by mail-pl1-f202.google.com with SMTP id x15-20020a170902e04fb02900f5295925dbso3434521plx.9 for ; Thu, 03 Jun 2021 19:03:13 -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=uh6gy4nZthBorhbRuZ4nfT30dGL+1kaEEjjmFJIUwIQ=; b=h7Se6qB/0cnnCs0FMOVwiNEYheVr2wFbzsatoLiYi1xkHnTgTljxG000R1EPRE5ksS U7kt5w744nMgfzzUltQKJlatxrPjqhQrX854XWGGw9u8WKzNKIw1cNdCphoH0IGL1TmS am3i4nOxfIT/O1nwzP4vL2sQFhbvB+BgMpB1gFQ0atXxtderGtY9fbqR3UkgUh7C9zLd XAX7q51IotP73K7SCw3RpR8taqAuzA9JYPJcC+onu5jJV0p06AfWOxlwtJIuhJWLJmHs ABfgbKc+cD+knNVJI6OUd42yvxwL65tayeQ0AD9qpxyRMjLJAGgL9eOixg6HpzneCLeW 89Pw== 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=uh6gy4nZthBorhbRuZ4nfT30dGL+1kaEEjjmFJIUwIQ=; b=MdeqMLL9tX4HjkXasCwPBPm9CEA5L8jTnYJ9E66f6EJZ1l7ID/codywYn/lX6ggSeX V+oPexyM8XMOlKaQUiy3AIbw1Nwn5E4aj/IAKe6WctDkSKilwbbl/W/1QrnRw4Qx27CM EBCdxHzGnPxuamHySRRMy1UH3LSoZsIl4kS0K4gOZgKxc3g4uDACI0e8z8YSeIRFxPCE +WfnN+CWgcIo0Z7KsP7IhX5J1zbrOSxqy7tXPmP211hVbhAuzMg8qU/aqlXVJW3VJ687 XbIzwsQFD7qeekhHb/al04oQ5GpiD2/uXjHGrEtY6E/u60I42cfGV3POnbNVb9gcau3I 8wTA== X-Gm-Message-State: AOAM5314fQkJtnFKGqnqunfvj+W0RWVB+n8AkdIfQ4FyCeD9oG4nNcH5 G5DdvrBA2FH39WmaC+xYjjo3wDeHAPIERg== X-Google-Smtp-Source: ABdhPJwz56m8TYUYhjbxzv/3S7nXuqm0Lfx263QneYGQ/2VvVbc6MN0OrIO1+l3D4W6MehEckGkiUK+GgvaeqQ== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:1d16:daf:7a47:a348]) (user=shakeelb job=sendgmr) by 2002:a63:f40d:: with SMTP id g13mr2462876pgi.290.1622771828563; Thu, 03 Jun 2021 18:57:08 -0700 (PDT) Date: Thu, 3 Jun 2021 18:56:40 -0700 In-Reply-To: <20210604015640.2586269-1-shakeelb@google.com> Message-Id: <20210604015640.2586269-2-shakeelb@google.com> Mime-Version: 1.0 References: <20210604015640.2586269-1-shakeelb@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH 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: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b="h7Se6qB/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3dIi5YAgKCHcncVfZZgWbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--shakeelb.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3dIi5YAgKCHcncVfZZgWbjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--shakeelb.bounces.google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 664B1351C X-Stat-Signature: b77wr5sfdeaj5ye8rbo4s1fn3rn17qnr X-HE-Tag: 1622772185-649354 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 --- 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 81d65d32ec2a..222c00e76ef9 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); @@ -1394,6 +1400,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 d48f727bec05..6c8578faa8b4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -96,6 +96,10 @@ bool cgroup_memory_noswap __read_mostly; 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) { @@ -5230,6 +5234,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))) + schedule_delayed_work(&stats_flush, round_jiffies(2UL*HZ)); + return 0; } @@ -5321,6 +5329,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); + schedule_delayed_work(&stats_flush, round_jiffies(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 60a19fd6ea3f..16546a5be922 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3872,6 +3872,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