From patchwork Wed Nov 29 03:21:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13472159 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C3FDC07CB1 for ; Wed, 29 Nov 2023 03:22:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 299D16B0399; Tue, 28 Nov 2023 22:22:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24E346B039A; Tue, 28 Nov 2023 22:22:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 09D126B039B; Tue, 28 Nov 2023 22:22:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EAEA76B0399 for ; Tue, 28 Nov 2023 22:22:08 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C5E4A120443 for ; Wed, 29 Nov 2023 03:22:08 +0000 (UTC) X-FDA: 81509543136.02.F00ED78 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 00B882001E for ; Wed, 29 Nov 2023 03:22:06 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=o+lJAqhS; spf=pass (imf13.hostedemail.com: domain of 3Xq5mZQoKCIc9z329lsxporzzrwp.nzxwty58-xxv6lnv.z2r@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Xq5mZQoKCIc9z329lsxporzzrwp.nzxwty58-xxv6lnv.z2r@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701228127; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yhGB2buQoBgcCIpoYKeC0LjcVeijrI8T84D/Lk2xUnw=; b=kqBVzzkL731QStEhwqvpzLYIAsXRAF7p66ENv43ElFetJC1CykfOzWWTa1CNsDYAHG3jL2 pQa8eBDz6C5yasdZBHtjq74Yj1dExfv29GX3DppsKxQ4AjcvW60lXgKXy/VG46hKMKHWkw SwsVksmJ4Xy6tGMACNdmeW/jq2gOY9g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701228127; a=rsa-sha256; cv=none; b=I5h2NvkN/rfQ0Y3rJDuSVI1TEN8qqsh60RqpjpBOiFcESesxM23UN2PU9/uLlorW3U14mY jWjghxy7FbJ3x4QeI5vZxMuu5hYjY2i1udlHu9tBCv6QGr/0hSzs1spuqFrEiYR6b3A3xb z5xhwQZeAyvayNTujnn8wgE9v4lHWec= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=o+lJAqhS; spf=pass (imf13.hostedemail.com: domain of 3Xq5mZQoKCIc9z329lsxporzzrwp.nzxwty58-xxv6lnv.z2r@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Xq5mZQoKCIc9z329lsxporzzrwp.nzxwty58-xxv6lnv.z2r@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5ca61d84dc3so82005827b3.0 for ; Tue, 28 Nov 2023 19:22:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701228126; x=1701832926; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yhGB2buQoBgcCIpoYKeC0LjcVeijrI8T84D/Lk2xUnw=; b=o+lJAqhSaRBkZR/qhR48AX1DmVWz1byMKqt0OeVlklTxuHM5qsx9Vzg4Q+PFYPKgTL OO7K1m+FqrC0KDLr0gUugkAKWpvVg1hj0TRQd0gn9B5oSpa0htU7bnna1nluG2Om+E7j KWOFQBfbhNRwalnafxTSzhRPmyzWhrSl6Ron9t/dnG0+FE33ZtVui4TB0iTDPYTkWUn8 dcp82UN4+kRTMe14SvUhYGN06lpWzsopwZ5Pf9Dnu4L1OZb8nTz2JwiAYDzUOqaHAw7G 7Gh1FvDug5iEfApCfPhWtXDXj1VTTVOfLZLIUdoH9qGZZC46HpX84vKXSnqW6F7ROQW6 SCSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701228126; x=1701832926; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yhGB2buQoBgcCIpoYKeC0LjcVeijrI8T84D/Lk2xUnw=; b=CO2sDt7tCXW6xas1O/0iPd6TyJooBoFPQTCJg0ZX8UIq2yjaQc3py0Zxyd6iJbVShs BhYiI1h6DypgLNyJH7JOfSw86UXSTDcWnMrcuyYruVDTl3+m7t5pRpSpwvNELktFuM2G 3b03QgY4zycS6VQ/1/ql3tD5mrTNw4ql8QgBuF9rKEwUVaVD1r60l/IPUsfpUG94yYYc wHZquu79Jja4vB7Qrxf6I9ZjYN+7R+LoVuTKMVNBbkFQchJbOPQSFQTa7QH+cwpI7x0D U24xnyFISO/LhgncEGSyXml4g0JCjop6XvX+i3O0SpSp1mh8oueowduaLmvN+MMQyAcY Zhmg== X-Gm-Message-State: AOJu0YxOLjF/AW+OuSFIPA5kXNN1P/Xm8FhLG5iEQp3fb/AZxYLBeZHd yQvkb7ea4c/P7qw8QpBbDQmGjecLv/BhhBUC X-Google-Smtp-Source: AGHT+IGQMvjzW97pO0CVzIRKmnSqmB117hXzR09KNowV7aCNCGSm6cUnDihjta6XjONTlKJWB5U7rHlCSdY/ECEH X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:690c:3183:b0:5cc:412c:27c7 with SMTP id fd3-20020a05690c318300b005cc412c27c7mr550939ywb.5.1701228126157; Tue, 28 Nov 2023 19:22:06 -0800 (PST) Date: Wed, 29 Nov 2023 03:21:51 +0000 In-Reply-To: <20231129032154.3710765-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231129032154.3710765-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231129032154.3710765-4-yosryahmed@google.com> Subject: [mm-unstable v4 3/5] mm: memcg: make stats flushing threshold per-memcg From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Stat-Signature: 4fi3msa54be3jm3gjx8rotpok4jzphgg X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 00B882001E X-Rspam-User: X-HE-Tag: 1701228126-431105 X-HE-Meta: U2FsdGVkX1/ISUXrUoPR1R5OP2ARGCbkQ/lLtJbHATV8k66O+LIfUzEpcxOIK3RvKBsytk1ffnMaCiUYCMIKzmahZYkQQKKe7HKGWzqAu0np1iAjvNCDm3d4kkks11iBz2kGfym2VOHXvkzjSxZtxrzgGeo4qgQzLBRsirj+V2FECjZi+57ZlGDGam6EclSK5+yRigSt5QZrIU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H3T3E61GehfclVLJHtJCA9sC0qEC83RR5qjs+QswtbV24MG9qVF0Dgy9x6m+lPqWSIgJ2q0cDpOgRwulDu1QDWp4Dh4ZklW8KQo8hrrTJCh60+4gRAdPUHW49lD7thpJ6Cm5hIl7IeEL3sP+1v1a7dil/nE26OhFjOvrddM3ZfPie+afjcKCgvpuJmIAxjUysoORCl8qbe0gicOY5PpXKq/fU4UZvTZZb+jShz6QRlyDYNl0/x/CHTKuWaE+MwlRIahMcLaa1ZLIxz5/oad+fmCc9VG27utt5ULjD9qzO2OCmcpKgIyaSFATdmwoHcRi1C8qd7ee5zyFpc1WQdlhv6i1oOPsp7f5SbLV9INp4AmZgIcC2KVPlBkM7DecuuSxWiAkg1Rw1ZIx3PnufXZ2tHfqyqggxMkFNTkUdNwIJgLhdTops0JuD4v8ndb9FbO6SyjomocOtRXcHM1hT9wtBHn//5In8jAYcaQYGFFHv8dUQ== 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: List-Subscribe: List-Unsubscribe: A global counter for the magnitude of memcg stats update is maintained on the memcg side to avoid invoking rstat flushes when the pending updates are not significant. This avoids unnecessary flushes, which are not very cheap even if there isn't a lot of stats to flush. It also avoids unnecessary lock contention on the underlying global rstat lock. Make this threshold per-memcg. The scheme is followed where percpu (now also per-memcg) counters are incremented in the update path, and only propagated to per-memcg atomics when they exceed a certain threshold. This provides two benefits: (a) On large machines with a lot of memcgs, the global threshold can be reached relatively fast, so guarding the underlying lock becomes less effective. Making the threshold per-memcg avoids this. (b) Having a global threshold makes it hard to do subtree flushes, as we cannot reset the global counter except for a full flush. Per-memcg counters removes this as a blocker from doing subtree flushes, which helps avoid unnecessary work when the stats of a small subtree are needed. Nothing is free, of course. This comes at a cost: (a) A new per-cpu counter per memcg, consuming NR_CPUS * NR_MEMCGS * 4 bytes. The extra memory usage is insigificant. (b) More work on the update side, although in the common case it will only be percpu counter updates. The amount of work scales with the number of ancestors (i.e. tree depth). This is not a new concept, adding a cgroup to the rstat tree involves a parent loop, so is charging. Testing results below show no significant regressions. (c) The error margin in the stats for the system as a whole increases from NR_CPUS * MEMCG_CHARGE_BATCH to NR_CPUS * MEMCG_CHARGE_BATCH * NR_MEMCGS. This is probably fine because we have a similar per-memcg error in charges coming from percpu stocks, and we have a periodic flusher that makes sure we always flush all the stats every 2s anyway. This patch was tested to make sure no significant regressions are introduced on the update path as follows. The following benchmarks were ran in a cgroup that is 2 levels deep (/sys/fs/cgroup/a/b/): (1) Running 22 instances of netperf on a 44 cpu machine with hyperthreading disabled. All instances are run in a level 2 cgroup, as well as netserver: # netserver -6 # netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K Averaging 20 runs, the numbers are as follows: Base: 40198.0 mbps Patched: 38629.7 mbps (-3.9%) The regression is minimal, especially for 22 instances in the same cgroup sharing all ancestors (so updating the same atomics). (2) will-it-scale page_fault tests. These tests (specifically per_process_ops in page_fault3 test) detected a 25.9% regression before for a change in the stats update path [1]. These are the numbers from 10 runs (+ is good) on a machine with 256 cpus: LABEL | MEAN | MEDIAN | STDDEV | ------------------------------+-------------+-------------+------------- page_fault1_per_process_ops | | | | (A) base | 270249.164 | 265437.000 | 13451.836 | (B) patched | 261368.709 | 255725.000 | 13394.767 | | -3.29% | -3.66% | | page_fault1_per_thread_ops | | | | (A) base | 242111.345 | 239737.000 | 10026.031 | (B) patched | 237057.109 | 235305.000 | 9769.687 | | -2.09% | -1.85% | | page_fault1_scalability | | | (A) base | 0.034387 | 0.035168 | 0.0018283 | (B) patched | 0.033988 | 0.034573 | 0.0018056 | | -1.16% | -1.69% | | page_fault2_per_process_ops | | | (A) base | 203561.836 | 203301.000 | 2550.764 | (B) patched | 197195.945 | 197746.000 | 2264.263 | | -3.13% | -2.73% | | page_fault2_per_thread_ops | | | (A) base | 171046.473 | 170776.000 | 1509.679 | (B) patched | 166626.327 | 166406.000 | 768.753 | | -2.58% | -2.56% | | page_fault2_scalability | | | (A) base | 0.054026 | 0.053821 | 0.00062121 | (B) patched | 0.053329 | 0.05306 | 0.00048394 | | -1.29% | -1.41% | | page_fault3_per_process_ops | | | (A) base | 1295807.782 | 1297550.000 | 5907.585 | (B) patched | 1275579.873 | 1273359.000 | 8759.160 | | -1.56% | -1.86% | | page_fault3_per_thread_ops | | | (A) base | 391234.164 | 390860.000 | 1760.720 | (B) patched | 377231.273 | 376369.000 | 1874.971 | | -3.58% | -3.71% | | page_fault3_scalability | | | (A) base | 0.60369 | 0.60072 | 0.0083029 | (B) patched | 0.61733 | 0.61544 | 0.009855 | | +2.26% | +2.45% | | All regressions seem to be minimal, and within the normal variance for the benchmark. The fix for [1] assumes that 3% is noise -- and there were no further practical complaints), so hopefully this means that such variations in these microbenchmarks do not reflect on practical workloads. (3) I also ran stress-ng in a nested cgroup and did not observe any obvious regressions. [1]https://lore.kernel.org/all/20190520063534.GB19312@shao2-debian/ Suggested-by: Johannes Weiner Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo Acked-by: Shakeel Butt --- mm/memcontrol.c | 50 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index cf05b97c1e824..93b483b379aa1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -631,6 +631,9 @@ struct memcg_vmstats_percpu { /* Cgroup1: threshold notifications & softlimit tree updates */ unsigned long nr_page_events; unsigned long targets[MEM_CGROUP_NTARGETS]; + + /* Stats updates since the last flush */ + unsigned int stats_updates; }; struct memcg_vmstats { @@ -645,6 +648,9 @@ struct memcg_vmstats { /* Pending child counts during tree propagation */ long state_pending[MEMCG_NR_STAT]; unsigned long events_pending[NR_MEMCG_EVENTS]; + + /* Stats updates since the last flush */ + atomic64_t stats_updates; }; /* @@ -664,9 +670,7 @@ struct memcg_vmstats { */ static void flush_memcg_stats_dwork(struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); -static DEFINE_PER_CPU(unsigned int, stats_updates); static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); -static atomic_t stats_flush_threshold = ATOMIC_INIT(0); static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -693,26 +697,37 @@ static void memcg_stats_unlock(void) preempt_enable_nested(); } + +static bool memcg_should_flush_stats(struct mem_cgroup *memcg) +{ + return atomic64_read(&memcg->vmstats->stats_updates) > + MEMCG_CHARGE_BATCH * num_online_cpus(); +} + static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) { + int cpu = smp_processor_id(); unsigned int x; if (!val) return; - cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); + cgroup_rstat_updated(memcg->css.cgroup, cpu); + + for (; memcg; memcg = parent_mem_cgroup(memcg)) { + x = __this_cpu_add_return(memcg->vmstats_percpu->stats_updates, + abs(val)); + + if (x < MEMCG_CHARGE_BATCH) + continue; - x = __this_cpu_add_return(stats_updates, abs(val)); - if (x > MEMCG_CHARGE_BATCH) { /* - * If stats_flush_threshold exceeds the threshold - * (>num_online_cpus()), cgroup stats update will be triggered - * in __mem_cgroup_flush_stats(). Increasing this var further - * is redundant and simply adds overhead in atomic update. + * If @memcg is already flush-able, increasing stats_updates is + * redundant. Avoid the overhead of the atomic update. */ - if (atomic_read(&stats_flush_threshold) <= num_online_cpus()) - atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold); - __this_cpu_write(stats_updates, 0); + if (!memcg_should_flush_stats(memcg)) + atomic64_add(x, &memcg->vmstats->stats_updates); + __this_cpu_write(memcg->vmstats_percpu->stats_updates, 0); } } @@ -731,13 +746,12 @@ static void do_flush_stats(void) cgroup_rstat_flush(root_mem_cgroup->css.cgroup); - atomic_set(&stats_flush_threshold, 0); atomic_set(&stats_flush_ongoing, 0); } void mem_cgroup_flush_stats(void) { - if (atomic_read(&stats_flush_threshold) > num_online_cpus()) + if (memcg_should_flush_stats(root_mem_cgroup)) do_flush_stats(); } @@ -751,8 +765,8 @@ void mem_cgroup_flush_stats_ratelimited(void) static void flush_memcg_stats_dwork(struct work_struct *w) { /* - * Always flush here so that flushing in latency-sensitive paths is - * as cheap as possible. + * Deliberately ignore memcg_should_flush_stats() here so that flushing + * in latency-sensitive paths is as cheap as possible. */ do_flush_stats(); queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); @@ -5809,6 +5823,10 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } } } + statc->stats_updates = 0; + /* We are in a per-cpu loop here, only do the atomic write once */ + if (atomic64_read(&memcg->vmstats->stats_updates)) + atomic64_set(&memcg->vmstats->stats_updates, 0); } #ifdef CONFIG_MMU