From patchwork Tue Jul 13 20:24:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12375011 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=-21.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 8CDBDC07E96 for ; Tue, 13 Jul 2021 20:24:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 23DE761361 for ; Tue, 13 Jul 2021 20:24:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 23DE761361 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 5D1376B0036; Tue, 13 Jul 2021 16:24:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A8AF8D0003; Tue, 13 Jul 2021 16:24:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D5FD8D0002; Tue, 13 Jul 2021 16:24:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0215.hostedemail.com [216.40.44.215]) by kanga.kvack.org (Postfix) with ESMTP id 15FDC6B0036 for ; Tue, 13 Jul 2021 16:24:23 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1DE4A80EC91A for ; Tue, 13 Jul 2021 20:24:22 +0000 (UTC) X-FDA: 78358691964.15.2B50C5C Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf03.hostedemail.com (Postfix) with ESMTP id C235F30000BF for ; Tue, 13 Jul 2021 20:24:21 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id o15-20020a655bcf0000b029022c1a9c33b7so17735540pgr.18 for ; Tue, 13 Jul 2021 13:24:21 -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=EHAYE05GAu+niMbde+c2BkAYKkWBFNQ1NKRMcAVAVMc=; b=gKeP0lrlLzavl+bP2FEXh/IMOv/on+DOO4SjIuZd90lujGClYWiPwVADRjnqM6k4rL VUdsQJeumADj1m9LGp5Kb+Vgd1H8iFqcTZjsVOUDQ5e4PPf7bET8MCIOGQhMiyyaNm3X mdTHi7e0RJQ3gvmuWttCxxw9rfydRGVRrObaNXXYipMVOUlUldWoCgaXawR4RpU5uYZ1 sPyayHFWIth2GWlSa8CSPGgj83LtxlEG8em/J7DD/6RJL9K0eMVPM70QUVzBFimfvu6d 7KCHAomuOwUbrVoZ81kD/VN8s014fjFRhpGPkWmDe25p3XhCrRIuf0R1sdFj1nL3gqWq cN+g== 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=EHAYE05GAu+niMbde+c2BkAYKkWBFNQ1NKRMcAVAVMc=; b=IH3QUgyOlPXPT/cHGZeV0/Kqa/bcqpWjJNnJRbQb7pMWzh/ert+5qmghzeZuRpEbvs q4LZkpNdEmK+J4Vf50Dg/Y0bjEJoSmLbqIThku4pSsIU7vfkZzDPgi7jv6MXCmJ8yitf KmG9CWehF2dObnPlRp1paSgiJNu2ozsFpOGjBWSKtNutoZWY6FLnHW4lUgzpL3BzZezx FZ9BydRgZPH1Jxbyjr5BI2C2z43d8dmiYqFgEW99zDCSF0Kt3sQHAUOHOJuFGRCtU02l AJC6vwOubgzjIDPn5pBwOIhwn0he8uu6I15VSo2IvDnXHGO+MeSlnfxjhWENG4BRW+dY 1bmQ== X-Gm-Message-State: AOAM533+KZIEC9CqT9MSsdIgx7Us9WjSROz6mIyVKHK2j7uQwpjN19In sW0d4r/dZFEvdcPtDY9E/al00bTPcLFGdg== X-Google-Smtp-Source: ABdhPJxRmtyeKKnYhuV8eHDTnOI9kWYJGdkl7JZyqqiCjCELzK2xCP+sct+v8gi5+aV00J+f8BFa46imiHijYg== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:c6c:5381:cc6a:6baa]) (user=shakeelb job=sendgmr) by 2002:a17:90b:347:: with SMTP id fh7mr47998pjb.2.1626207860793; Tue, 13 Jul 2021 13:24:20 -0700 (PDT) Date: Tue, 13 Jul 2021 13:24:12 -0700 In-Reply-To: <20210713202412.248252-1-shakeelb@google.com> Message-Id: <20210713202412.248252-2-shakeelb@google.com> Mime-Version: 1.0 References: <20210713202412.248252-1-shakeelb@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH 2/2] memcg: infrastructure to flush 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 , Hillf Danton , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=gKeP0lrl; spf=pass (imf03.hostedemail.com: domain of 3dPbtYAgKCCcVKDNHHOEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--shakeelb.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3dPbtYAgKCCcVKDNHHOEJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--shakeelb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C235F30000BF X-Stat-Signature: wwptsjrrsdkofjejcjs8dbr1j754b9bk X-HE-Tag: 1626207861-243416 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. This patch uses following mechanisms to solve these challenges: 1. Periodically flush the stats from root memcg every 2 seconds. This will time limit the out of sync stats. 2. Asynchronously flush the stats after fixed number of stat updates. In the worst case the stat can be out of sync by O(nr_cpus * BATCH) for 2 seconds. 3. For avoiding thundering herd to flush the stats particularly from the memory reclaim context, introduce memcg local spinlock and let only one flusher active at a time. This could have been done through cgroup_rstat_lock lock but that lock is used by other subsystem and for userspace reading memcg stats. So, it is better to keep flushers introduced by this patch decoupled from cgroup_rstat_lock. --- Changes since v2: - Changed the subject of the patch - Added mechanism to bound errors to nr_cpus instead of nr_cgroups - memcg local lock to let one active flusher Changes since v1: - use system_unbound_wq for flushing the memcg stats include/linux/memcontrol.h | 6 ++++++ mm/memcontrol.c | 34 ++++++++++++++++++++++++++++++++++ mm/vmscan.c | 6 ++++++ 3 files changed, 46 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0bfa0409af22..fa095a94ae56 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -991,6 +991,8 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, return x; } +void mem_cgroup_flush_stats(void); + 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 +1402,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 848d711bf576..39a00991fc80 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -103,6 +103,14 @@ static bool do_memsw_account(void) return !cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_noswap; } +/* memcg and lruvec stats flushing */ +static void flush_memcg_stats_dwork(struct work_struct *w); +static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); +static void flush_memcg_stats_work(struct work_struct *w); +static DECLARE_WORK(stats_flush_work, flush_memcg_stats_work); +static DEFINE_PER_CPU(unsigned int, stats_flush_threshold); +static DEFINE_SPINLOCK(stats_flush_lock); + #define THRESHOLDS_EVENTS_TARGET 128 #define SOFTLIMIT_EVENTS_TARGET 1024 @@ -685,6 +693,8 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, /* Update lruvec */ __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); + if (!(__this_cpu_inc_return(stats_flush_threshold) % MEMCG_CHARGE_BATCH)) + queue_work(system_unbound_wq, &stats_flush_work); } /** @@ -5248,6 +5258,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_dwork, + 2UL*HZ); return 0; } @@ -5339,6 +5353,26 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) memcg_wb_domain_size_changed(memcg); } +void mem_cgroup_flush_stats(void) +{ + if (!spin_trylock(&stats_flush_lock)) + return; + + cgroup_rstat_flush(root_mem_cgroup->css.cgroup); + spin_unlock(&stats_flush_lock); +} + +static void flush_memcg_stats_dwork(struct work_struct *w) +{ + mem_cgroup_flush_stats(); + queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 2UL*HZ); +} + +static void flush_memcg_stats_work(struct work_struct *w) +{ + mem_cgroup_flush_stats(); +} + 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..1cc05ab8ca15 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2893,6 +2893,12 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) target_lruvec = mem_cgroup_lruvec(sc->target_mem_cgroup, pgdat); again: + /* + * Flush the memory cgroup stats, so that we read accurate per-memcg + * lruvec stats for heuristics. + */ + mem_cgroup_flush_stats(); + memset(&sc->nr, 0, sizeof(sc->nr)); nr_reclaimed = sc->nr_reclaimed;