Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1648356pxv; Fri, 16 Jul 2021 14:23:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6BukEPdBAdcgdcvCIOg/mw6Z9E8NBIWC9NdezIZohWUyEQ44LKeOtDxjVsCmoJvElbngX X-Received: by 2002:a05:6638:24ce:: with SMTP id y14mr10400976jat.82.1626470582425; Fri, 16 Jul 2021 14:23:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626470582; cv=none; d=google.com; s=arc-20160816; b=LYs3DgRq4Sw6lpuZVAwq1qbdXkFKZ815MuKqkO0jm8OtAOphzy0cJgROFKa5dX6h6d h90jaV7C4E8n3hyrIFQKSwWRnfAhaN+TrPfnVhfQsVj4M/To6Ml0QnfBdT21uGzkohSF NHMn/SdZhbsauK37MoklsmunD2Y2eEK54RhT5NszY/80KZ4hbUrK6g2AXGUaqzcygBpi AI/gkEJKfNveQlIpP5H4+GdidwX9yUIJHGQjOBaCNNoOAqBli5Z1IDijLaOasURZTHbg kN1/UDq0Csdr7sKx7d8Lk/QcyeyXwNSRAgP1Cib4yLYuBG2m7LQ+Ha1XIMh/Y3+PymrL b38Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=fk4tAuPxyFd0FobDfqVGnEYc2NrI1vYM+OgM2ictzdU=; b=IPXttltMEsmgSYKeL4ios4Gx8+ZlAWRzu+AamRhaBbpLWfJsNU8pAHApK0Zx/9rSoE 0R2nffCRFnsphSrBXhCU3tuUraajsI1FyrGgtl9KbhF5HwblONFxbh2pzsOJ3NBB9GeA R226dK7WD8W25KwhAKt42sQS1fja3dBNyygjVcJ2DTpHkwb4qh9yzVHpHVP0j7ekT8at CDyiCuQbvToEkYwF6Mq3mbC1oUgLOW20fdE69I9ebo96ODK27F4B3EG7/psW3lH2GwfA A+5PNZdETFpXU8TzOnX5rIMaDaqu2EIVYuEjuhWujwo+pGNJtSh8MgzqXSDdb5qXjEP4 HhIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OXccDO+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r7si61244ill.122.2021.07.16.14.22.49; Fri, 16 Jul 2021 14:23:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OXccDO+9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235914AbhGPVYy (ORCPT + 99 others); Fri, 16 Jul 2021 17:24:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235924AbhGPVYs (ORCPT ); Fri, 16 Jul 2021 17:24:48 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD90BC061762 for ; Fri, 16 Jul 2021 14:21:51 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id j10-20020a056a00174ab029031e1e93e88dso7826328pfc.3 for ; Fri, 16 Jul 2021 14:21:51 -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=fk4tAuPxyFd0FobDfqVGnEYc2NrI1vYM+OgM2ictzdU=; b=OXccDO+93T+Duotqx23iOxbpOH2hC+t2wyXy/iHa5ryp9TQZRDeoCQhISflgiYg7tM M5RPsBIx4rTn+Esxk4YCBgUaupjIoT7QD7xFkyCVDOvHoFEEcFPiYTffQKjMcFmw4uWy QoEjHRAItFobqeqb2dfK4NviepC1QqWeio3br37scL6Ah/VHUDwACET67NB6gts4o4nE 7Iw/p5Klw84ubiS67x6n7NG5WEisOg/FoVaAjpQkWqppTH2F73WnfdUspG0maB7oYER0 ECDU32zKKRH2D6+oBmL/CuOeM0Yqgefpg+6EXVGy2KHYi4r36htIHXXorL+8WVsRmeLX l31w== 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=fk4tAuPxyFd0FobDfqVGnEYc2NrI1vYM+OgM2ictzdU=; b=WUFNWCc+3K4SF9eTxCUsxQbKYfyaBNw01SJaeDnhXI58RJEuJCGGm40Y1Xmf4QWFTJ nTkXooqbbtnE1fcSw6MqoU7EOBQT/of9Jqe/3AKQJjxmpt+GqS3bK3iu3wBZgORHJvqX UZV2q6y2cPdFe5eM04llcVhOPqkxYXTCrsSCtjK+PN81edFgUrC3HeWV+dOXylNBk6TB pqNgamyJlX3SbjO96CmyY4d/cdFQ6OiTE76Ou39wJBhM4ue8n04nuDMJEyxf9z6z9C5j tqS3IKOGwSi/8oFzFsw4xBNmZK/2Tj5OPFDmObXT+prd1Ocy5jRux7YMfS/jtarC3jup Gf6g== X-Gm-Message-State: AOAM5317+MElD+Q6CLwbci4HlBL+F1mDwidQWCw0saIHxQNCQQvJEE2U Gqe2AoeTETFV+qwnUdy5eEysKYFByu3PaQ== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:7bfd:347c:6059:d68a]) (user=shakeelb job=sendgmr) by 2002:a17:90b:15c9:: with SMTP id lh9mr10582743pjb.111.1626470511140; Fri, 16 Jul 2021 14:21:51 -0700 (PDT) Date: Fri, 16 Jul 2021 14:21:37 -0700 In-Reply-To: <20210716212137.1391164-1-shakeelb@google.com> Message-Id: <20210716212137.1391164-2-shakeelb@google.com> Mime-Version: 1.0 References: <20210716212137.1391164-1-shakeelb@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH v5 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=20Koutn=C3=BD?=" , Huang Ying , Hillf Danton , Yang Yingliang , Marek Szyprowski , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. However we would have to use irqsafe version of rstat flush but that is fine as this code path will be flushing for whole tree and do the work for everyone. No one will be waiting for that worker. Signed-off-by: Shakeel Butt --- Changes since v4: - Fixed the sleep-in-wrong context bug report by Yang Yingliang and Marek Szyprowski . Changes since v3: - Add back the sigoff 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..0a05ce5cd045 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_irqsafe(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; -- 2.32.0.402.g57bb445576-goog