From patchwork Thu Sep 21 08:10:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13393758 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 2AF50E706E3 for ; Thu, 21 Sep 2023 08:11:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CF6E6B018A; Thu, 21 Sep 2023 04:11:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 780996B0195; Thu, 21 Sep 2023 04:11:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6478F6B0198; Thu, 21 Sep 2023 04:11:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 50B306B018A for ; Thu, 21 Sep 2023 04:11:07 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 268101CADBD for ; Thu, 21 Sep 2023 08:11:07 +0000 (UTC) X-FDA: 81259884174.02.BA2D19F Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 4C4B94002A for ; Thu, 21 Sep 2023 08:11:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="2SHBAfY/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3l_oLZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3l_oLZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695283864; 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=FIm0UyACNB+n/akQREWxQWjORl9GVEfiVT/RezLXAow=; b=eQ+4VbxELzbf9C2Vzf/c+2ZTa2Vl04fOxL+0KF5HQS7e3MBcOkn+pG++hS+i8ymAyH9wS2 eyyXaQjaqGhNXcr/sDOIWc/Uxm6o0F0eMO66fuJAsbrDQY7RigUv2kwyzhmiAaU/3gDjQQ r5t0lCfh+x/xFY68y37z8cphYz3i6aI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="2SHBAfY/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3l_oLZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3l_oLZQoKCOokaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695283864; a=rsa-sha256; cv=none; b=E4RM+sdauMCn00Q8HhlAWD4L47wVMr0rEWTsb0t+9pdvyIQzZoxL/6xacT9cCATDIc5ouj jM/oKV/eEAMiJ92W2oxQeftL4Atvyfr9qaagBGEkO5wtzDxWnepK0KawMFFwMSnBvJUtFM PyD0Agwh0/ZmfxHGfJw1LoSyAbTA10c= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d81841ef79bso1087806276.1 for ; Thu, 21 Sep 2023 01:11:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695283863; x=1695888663; 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=FIm0UyACNB+n/akQREWxQWjORl9GVEfiVT/RezLXAow=; b=2SHBAfY/SuQLH1013sNn1FGjz7pjvWPm6Qitog3c6lZ9GFFnyMJgcnOcPId0BhOCFf 0N6wTE21ztM68MLWIgny84aoEHDKs+Qh4Es4Nn+rPC+qkHc2+0NUHMJDF4JsCOO2hORO 2dfznztSY5WWgvRSm1CJyvK7gSLMwjKeIycnJu2MhaIEYoW/1Uty9eEiE4aRFJTI4bzm HtOSfgFrdjAxr5VQNbbqpSBh1/CthEk6T2vfbiy8iqTZQb1m3f1x6XMHhRbEGja0MauM YTwMIDK7PIdO+++eishUYhEaCq2UQ8+vJfzwNwyiEWmHX9REUqKSTCME/RqSg+/3L/MK ztYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695283863; x=1695888663; 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=FIm0UyACNB+n/akQREWxQWjORl9GVEfiVT/RezLXAow=; b=bM3Wvbm2+lDYjlflT4gQroklMS/DIy+ILk1jo3/kMqmmsmieRDM1XuYE5SQe+UdDBk 5wwxgPGf4d/ZK89n5aPWxF3Ib+xpFaVW26yr1irmNyXrVF5i8PLf/0UxLyAeX2DBCaRG GiX6rcmPFPy6hMWIp02vIZjHo9lsDqBQPbiNhb1Wv+c4E3xcoWe5+H1LT4WZHW1m4H3W dbpj+qn83qA6AZki82sYnMxAycjKOYtBEh6Vv21Xz/AAfhTKXyNSlImqjhxwz79i56gf 6YOSKTUZYWPu4TPE555f5wmDct6DXT+9injqEfNf24moEXhyspaBeQo8xa1xmpv0A/7G CBOQ== X-Gm-Message-State: AOJu0Yw20j5Qp6Aryt+WTDexsXsdfn0+434mr49PebwYXei3zHOhEenk SzIVK383BD/b+KiFHU+D/ecAuQ39HepSnD0J X-Google-Smtp-Source: AGHT+IFPrnsBcOBrlhPC5hJYwVV2PE3AiCfsgKWx3CsvgDAaTduMINork77xXDD5F6NyG97FAk+4bBmhLTyqQlsm X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:4c8:0:b0:d0e:d67d:6617 with SMTP id 191-20020a2504c8000000b00d0ed67d6617mr67400ybe.4.1695283863247; Thu, 21 Sep 2023 01:11:03 -0700 (PDT) Date: Thu, 21 Sep 2023 08:10:53 +0000 In-Reply-To: <20230921081057.3440885-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230921081057.3440885-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230921081057.3440885-2-yosryahmed@google.com> Subject: [PATCH 1/5] mm: memcg: change flush_next_time to flush_last_time 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 , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 4C4B94002A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: u69ij7mzojnj7je6p8xyhugpoa5ft1iu X-HE-Tag: 1695283864-532977 X-HE-Meta: U2FsdGVkX18S6HOIQAalVy8YONKwSnMGva9c5xGMLSqGUvJ54GbEwwOXi5UyybJ2oFSGmx4r3uEdQLNIdI62tRSqkwP8ly7iTFQobVHy4j/9zuX42gu1BiL5q0Do3mu5iERht4JZGKLjD9d6bgyYysS89IrLPY5OCLMhOoGmvgSQcJlbHHx72EdtoS4+VkYspaRycmI1k63OtET6cx0ebjq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sZE4VToFpsBkJNUwqh9JvVZc5Znkjg7bdwckfXkBMNreFS/L6d+8wFu2PUi0obthok/lSLxilg72fN1ay+Me2RpB5MOBVAfsfjXBZ/BeFUBQaZsaLN0RJkDECZsJLFqGi9V3IkI89JfI8gqRjIdtVnnkCBwwKdtMoa3ARDzi92laIywkmAGRjL6GFWninX7s40yz6puW/qBkO2RKmyUAEo/uo2D4xWBdnXNSj8Fg6Lh9JlcAmHAlePgGG7X4NhbqzvM3UzMO2z8rVTdXLqvwA6xAfwpyYeqLgTWB2PFGpa2d36PbcpiAk1p9QTGqsoOmD/8M9Its2TwI3RUqfyvAhZp1tkKAq9t1N/vUYlkRzTrt2s3gt9VvzAg4WVKU8lOQwvnMVpw== 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: flush_next_time is an inaccurate name. It's not the next time that periodic flushing will happen, it's rather the next time that ratelimited flushing can happen if the periodic flusher is late. Simplify its semantics by just storing the timestamp of the last flush instead, flush_last_time. Move the 2*FLUSH_TIME addition to mem_cgroup_flush_stats_ratelimited(), and add a comment explaining it. This way, all the ratelimiting semantics live in one place. No functional change intended. Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 927c64d3cbcb..49562dfdeab2 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -590,7 +590,7 @@ 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_next_time; +static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -650,7 +650,7 @@ static void do_flush_stats(void) atomic_xchg(&stats_flush_ongoing, 1)) return; - WRITE_ONCE(flush_next_time, jiffies_64 + 2*FLUSH_TIME); + WRITE_ONCE(flush_last_time, jiffies_64); cgroup_rstat_flush(root_mem_cgroup->css.cgroup); @@ -666,7 +666,8 @@ void mem_cgroup_flush_stats(void) void mem_cgroup_flush_stats_ratelimited(void) { - if (time_after64(jiffies_64, READ_ONCE(flush_next_time))) + /* Only flush if the periodic flusher is one full cycle late */ + if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME)) mem_cgroup_flush_stats(); } From patchwork Thu Sep 21 08:10:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13393759 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 6B99AE706E5 for ; Thu, 21 Sep 2023 08:11:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D5716B0195; Thu, 21 Sep 2023 04:11:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C1F66B0198; Thu, 21 Sep 2023 04:11:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EC8B6B01F0; Thu, 21 Sep 2023 04:11:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 13BE86B0195 for ; Thu, 21 Sep 2023 04:11:08 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CFEBA8102F for ; Thu, 21 Sep 2023 08:11:07 +0000 (UTC) X-FDA: 81259884174.25.487737F Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf22.hostedemail.com (Postfix) with ESMTP id 22348C001D for ; Thu, 21 Sep 2023 08:11:05 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1KtSUNu1; spf=pass (imf22.hostedemail.com: domain of 3mfoLZQoKCOwmcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3mfoLZQoKCOwmcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@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=1695283866; 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=VUfaq4QWGo6Pgq9+Gc0pYjCdwcXd4ikm64XsTJ+V7zI=; b=G8d4TlZZ9LfjDppLt2/A/YEBMLnbaiuKcajYNMoFpBMvD28w+ocXo0r7Lehf6XgkSLBA4V rTxHrdZLUV5TPoCsNEzMCw5uEe70DjIatsxzytvlgxXFck218hALwcVhdKzTW6Ip4wGVYw ZHIBUQosjM/F9ChIW8KRNerH91kHxsI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695283866; a=rsa-sha256; cv=none; b=LZ+BwcoVDhunrgVM9a+FlDxbdZBS4ViKuStJSZ8mrr43zoQss5p3/2XRBnbgw9UuMAoKa0 l8oXRNOtCOcv5qGzc6AMqZFaGatAy0Ci6gBwWsyhb+aZjQYvOW8/ZsCKoMBkmy4zWoMrqR ff94Xb2iNyzIZEQ6g6MRNhYVu5N1a8w= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1KtSUNu1; spf=pass (imf22.hostedemail.com: domain of 3mfoLZQoKCOwmcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3mfoLZQoKCOwmcgfmOVaSRUccUZS.QcaZWbil-aaYjOQY.cfU@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59e78032ef9so10082157b3.2 for ; Thu, 21 Sep 2023 01:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695283865; x=1695888665; 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=VUfaq4QWGo6Pgq9+Gc0pYjCdwcXd4ikm64XsTJ+V7zI=; b=1KtSUNu1FsLC7ZYWkGZiLAVKN6Y8P1vkRyAg0llhiyZReNLYxhQP0199zs3psCZj9+ A1Zm7xp9wvMpeciLNuKqGlCaoo473FpEYlPU8M8hDegc450l5i3zpLQ3Kkou9St4soxI OHa8auqL4c7wmb+Ed/luhDGN11dYRqO9Nk+iaPKEHKm+DgeKag78YqoyNwygAayuRaTq LRaMqQccw43eJwTRfG5321GLtZCkGGO6a1Il0S/y1I5o0Ap1L2XkROFfS13Mem01SlMX G5Ftwd5iyuVbCuyd1OCbyUiOzq+AY8b2F9+WzrdM6bjJpp1oDS15xJVtDST6UFbn+fip xigA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695283865; x=1695888665; 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=VUfaq4QWGo6Pgq9+Gc0pYjCdwcXd4ikm64XsTJ+V7zI=; b=lkhdYX79RVEBX4pnIOK8CzwSUCnp5ynSKr4I8vWGnod9Pa0kBWrfuzQnUy5UoQpYhs NUZFVvLmrXarg96liCAmMBBxJiIHTiJN9v19dLmp+O4dIdgh6PUvCvnYt/e6lrDtzWzP xO0OYWuSlMLIvfEOd+pelNAT1ghZk7M8CBWK0C1dj7jhfOBLuHiz7fhF8Wr7fatRCwbS dFMjsvdUOx9BXY9X61yf6jlzvTp/2TK1VOgm7wJA9HaOOOjHLZCF3XP1WgXk5rgtMDRY o1DrgSz7ZbycvXPsHyhrvOEk6111y3qoNIDU3I1inMLpFALzwX0r0GTm6cUQQ9Gt5j8L s/9Q== X-Gm-Message-State: AOJu0YxU5QLNIPRQdJh+eIt3i/d3Lxa5onGv4KaMqfCdrEv32r1DzARx 5wrJ1hDIY6iBtDLD1L7uHNiwjfP5e9RstRvj X-Google-Smtp-Source: AGHT+IFBE/oPIuCJlO97tHApRysO3VNA0vvMKpAee95h86ybMubWU1dHAIDYmvCWr5CQMyHTNET+SuyM9GansnHQ X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a81:b656:0:b0:59b:f493:813c with SMTP id h22-20020a81b656000000b0059bf493813cmr74625ywk.9.1695283865279; Thu, 21 Sep 2023 01:11:05 -0700 (PDT) Date: Thu, 21 Sep 2023 08:10:54 +0000 In-Reply-To: <20230921081057.3440885-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230921081057.3440885-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230921081057.3440885-3-yosryahmed@google.com> Subject: [PATCH 2/5] mm: memcg: move vmstats structs definition above flushing code 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 , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 22348C001D X-Rspam-User: X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XEkVQsm3ah1oMLQp2sXU/cw+luu93lu86/UVgyZnKnzEPayzKE7YyCp2L32tHkrIi1TFKVxsAhcRNpU6p9biCxiMlTbZq830IygXHdbd5scmlkebS2FjBTWBjv7kRQ2Kdfuf61OaI4T8aezoa5opodeGOaqUb4kr1PoJ5sneXHY37C3/okKWMNvTLP8kAEHeUIneWaUoZpAxn7sMjNys92NhfO9GIdtJ79WYcuTboC+iSzLJyj3aictCPMO8X3cP4IKGjq1LqRHDgchh2GSZIzZmHvd4X1VIT8LzZbjJX1Lqj0jECE77wxsohKc7Kx6NF2KuOt7RN8Pn/dq/KLcr8hcLNTgtbIMTUW4xInXWY13z8xYx63Vov0l7V9bNSUYhmoMuIZjUCvxAnfXkfwgPkYUsBGKkg+FfbZqM5NDZYU7G+miPs6X731YfgasRD7NWQyNndwO5aDje5jPFNVs7nFMOUsJN+JerxgHQUOY+KNejzxeFhSHcww7UL+aU+iKNIyJqV4PMEjXjEb0aEqAyAZCD0MNyPYnF74CE0oRl5rPZSOcbH8RSTwLu1tXeFA27LQM9VBUyhv6Am+5E9LwbLexYxlvzfrp+ZOAeBhK4v9Qnrn5793AbVnD9uLTy 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 following patch will make use of those structs in the flushing code, so move their definitions (and a few other dependencies) a little bit up to reduce the diff noise in the following patch. No functional change intended. Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 146 ++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 49562dfdeab2..ef7ad66a9e4c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -570,6 +570,79 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) return mz; } +/* Subset of vm_event_item to report for memcg event stats */ +static const unsigned int memcg_vm_event_stat[] = { + PGPGIN, + PGPGOUT, + PGSCAN_KSWAPD, + PGSCAN_DIRECT, + PGSCAN_KHUGEPAGED, + PGSTEAL_KSWAPD, + PGSTEAL_DIRECT, + PGSTEAL_KHUGEPAGED, + PGFAULT, + PGMAJFAULT, + PGREFILL, + PGACTIVATE, + PGDEACTIVATE, + PGLAZYFREE, + PGLAZYFREED, +#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) + ZSWPIN, + ZSWPOUT, +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + THP_FAULT_ALLOC, + THP_COLLAPSE_ALLOC, + THP_SWPOUT, + THP_SWPOUT_FALLBACK, +#endif +}; + +#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) +static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; + +static void init_memcg_events(void) +{ + int i; + + for (i = 0; i < NR_MEMCG_EVENTS; ++i) + mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; +} + +static inline int memcg_events_index(enum vm_event_item idx) +{ + return mem_cgroup_events_index[idx] - 1; +} + +struct memcg_vmstats_percpu { + /* Local (CPU and cgroup) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_MEMCG_EVENTS]; + + /* Delta calculation for lockless upward propagation */ + long state_prev[MEMCG_NR_STAT]; + unsigned long events_prev[NR_MEMCG_EVENTS]; + + /* Cgroup1: threshold notifications & softlimit tree updates */ + unsigned long nr_page_events; + unsigned long targets[MEM_CGROUP_NTARGETS]; +}; + +struct memcg_vmstats { + /* Aggregated (CPU and subtree) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_MEMCG_EVENTS]; + + /* Non-hierarchical (CPU aggregated) page state & events */ + long state_local[MEMCG_NR_STAT]; + unsigned long events_local[NR_MEMCG_EVENTS]; + + /* Pending child counts during tree propagation */ + long state_pending[MEMCG_NR_STAT]; + unsigned long events_pending[NR_MEMCG_EVENTS]; +}; + /* * memcg and lruvec stats flushing * @@ -681,79 +754,6 @@ static void flush_memcg_stats_dwork(struct work_struct *w) queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); } -/* Subset of vm_event_item to report for memcg event stats */ -static const unsigned int memcg_vm_event_stat[] = { - PGPGIN, - PGPGOUT, - PGSCAN_KSWAPD, - PGSCAN_DIRECT, - PGSCAN_KHUGEPAGED, - PGSTEAL_KSWAPD, - PGSTEAL_DIRECT, - PGSTEAL_KHUGEPAGED, - PGFAULT, - PGMAJFAULT, - PGREFILL, - PGACTIVATE, - PGDEACTIVATE, - PGLAZYFREE, - PGLAZYFREED, -#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) - ZSWPIN, - ZSWPOUT, -#endif -#ifdef CONFIG_TRANSPARENT_HUGEPAGE - THP_FAULT_ALLOC, - THP_COLLAPSE_ALLOC, - THP_SWPOUT, - THP_SWPOUT_FALLBACK, -#endif -}; - -#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) -static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; - -static void init_memcg_events(void) -{ - int i; - - for (i = 0; i < NR_MEMCG_EVENTS; ++i) - mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; -} - -static inline int memcg_events_index(enum vm_event_item idx) -{ - return mem_cgroup_events_index[idx] - 1; -} - -struct memcg_vmstats_percpu { - /* Local (CPU and cgroup) page state & events */ - long state[MEMCG_NR_STAT]; - unsigned long events[NR_MEMCG_EVENTS]; - - /* Delta calculation for lockless upward propagation */ - long state_prev[MEMCG_NR_STAT]; - unsigned long events_prev[NR_MEMCG_EVENTS]; - - /* Cgroup1: threshold notifications & softlimit tree updates */ - unsigned long nr_page_events; - unsigned long targets[MEM_CGROUP_NTARGETS]; -}; - -struct memcg_vmstats { - /* Aggregated (CPU and subtree) page state & events */ - long state[MEMCG_NR_STAT]; - unsigned long events[NR_MEMCG_EVENTS]; - - /* Non-hierarchical (CPU aggregated) page state & events */ - long state_local[MEMCG_NR_STAT]; - unsigned long events_local[NR_MEMCG_EVENTS]; - - /* Pending child counts during tree propagation */ - long state_pending[MEMCG_NR_STAT]; - unsigned long events_pending[NR_MEMCG_EVENTS]; -}; - unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { long x = READ_ONCE(memcg->vmstats->state[idx]); From patchwork Thu Sep 21 08:10:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13393760 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 9DC3BE706E3 for ; Thu, 21 Sep 2023 08:11:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 278666B0198; Thu, 21 Sep 2023 04:11:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 22B616B01F0; Thu, 21 Sep 2023 04:11:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F206B6B01F7; Thu, 21 Sep 2023 04:11:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DCE9B6B0198 for ; Thu, 21 Sep 2023 04:11:09 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B599C1A1111 for ; Thu, 21 Sep 2023 08:11:09 +0000 (UTC) X-FDA: 81259884258.14.6BCF325 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.74]) by imf06.hostedemail.com (Postfix) with ESMTP id DD3FC180022 for ; Thu, 21 Sep 2023 08:11:07 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="n/BKYJqZ"; spf=pass (imf06.hostedemail.com: domain of 3mvoLZQoKCO0ndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com designates 209.85.210.74 as permitted sender) smtp.mailfrom=3mvoLZQoKCO0ndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@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=1695283867; 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=DUC4W/ANoWKKbgrvdNHuDLy6cXEfVkGWJByczmAAxsQ=; b=QKHze4eRoLzb58/RsDoKiOQWP8PvA11z/fe6nS7HFnYHIlpqBj49E4VVsJFFdPQ7jicJUI eySVsCL7HQ6rmO8KK18J3XCDkRLedSfKkfLc1jixyqgKy1jr9ufrhdq+QSsZna2C2uVDu4 1IEe4ZhGQuX2DvVnotGTENvaRDQG61Q= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="n/BKYJqZ"; spf=pass (imf06.hostedemail.com: domain of 3mvoLZQoKCO0ndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com designates 209.85.210.74 as permitted sender) smtp.mailfrom=3mvoLZQoKCO0ndhgnPWbTSVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695283867; a=rsa-sha256; cv=none; b=tBnaMceE4DlCgICleaOKom7PmM3eBryE1XC32/K61cdDj1+9byx9pB4o3kNg/3u94fIJGa v2+A08gEGzmR7HtoyQznCSTLD7MurM5v1u7U93b//Gab2hKaNZNxn9OHq2TKMgELotvyqY 2WBYGR2ick8eqBusl22lztjme2Ckm1Y= Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-6c0b345d6acso2031108a34.0 for ; Thu, 21 Sep 2023 01:11:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695283867; x=1695888667; 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=DUC4W/ANoWKKbgrvdNHuDLy6cXEfVkGWJByczmAAxsQ=; b=n/BKYJqZi++Yt/LZmyyEH2LxrJoyIF9bThzgzKCYZ1d3A3tmBNigcXGPUyNCve5SOi O6rPrOijW1LUobp4z/eOjGMUFEbnEEhed3rhsw9/kMGv8ktDhRbeFdCvHRYvkD2bkzZ+ 4nMj9vdqhWueUFG3eAd5e5cr6squRDyasjSjzwripezYjEEbtHHHfRiZAgKfk5sF9CY5 X6BIx94nn5vvsRJxImHNSyC4Dm8vDbrbia2WLI6GS2qheuonwWgU03JUxlEeNa6hFCKD mfZGN1ixtohEX0OiPl8lkNqnFJoIlCc88+VmheSPlrRlt9AQv1Z+trYPaqpgGa7HlkDK L+eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695283867; x=1695888667; 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=DUC4W/ANoWKKbgrvdNHuDLy6cXEfVkGWJByczmAAxsQ=; b=DMEf0B/ofGNpkKOibdsbgN5U5mGiGjCGB2TVSkIa+imX+gVITtU0f/q6JuwH1DdP94 Gixlg4VgB5okDNc4IJPlHBToRZo4cgJAx5lAUVIQDyXnpC4Q93Wef71KbOhWAhgCwMwZ vjOJ49fa3Qu40F2ufEavCdC5wWMKOtDc9yGpQmAWrbUvxfLFveWpRvx0y1hy9rhMJSaT 43Sfn7uDn5JVkeFuw0QTK9g0gicg7z9d3IaorZeLBC9CKfRsAHv+TqMAGfqzwUhKhbMu hl4frB5HebGX0+752U++QGSSwLqMFbKTnwiqxdVACtgwDoSkLdWud+lsOH4i71aUf7z2 JF6g== X-Gm-Message-State: AOJu0YwNUvfQ6El/4Qsl9MrQpRQ6vQ8IgYaqdHzS6LO1UIu7bn7qmQwz 97JVtj8MHMc6yV2HWA/UuNELSYk5TZyxDFSW X-Google-Smtp-Source: AGHT+IHWB2c9Yqrg5vaJ7T2d28Eo6cRgxwGr6tDusahnEBtVkRUK94znckmkqrIayxEXBXBNswuh+j1RV0RnYbJb X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6830:ca:b0:6bc:ac3d:2b77 with SMTP id x10-20020a05683000ca00b006bcac3d2b77mr1588187oto.2.1695283866981; Thu, 21 Sep 2023 01:11:06 -0700 (PDT) Date: Thu, 21 Sep 2023 08:10:55 +0000 In-Reply-To: <20230921081057.3440885-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230921081057.3440885-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230921081057.3440885-4-yosryahmed@google.com> Subject: [PATCH 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 , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: DD3FC180022 X-Rspam-User: X-Stat-Signature: guqquecjdrdndz9dzpxcajabfo3ueqcs X-Rspamd-Server: rspam01 X-HE-Tag: 1695283867-677848 X-HE-Meta: U2FsdGVkX1+VT+tvfDUw7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7HUxA1CK2Hpm0RAVzRVrkrrQPDev0lS6bLxhVMRbIfrEdGyDz3Uky+BbRXRp4z1fRSh5ydZopd8nHNeLmeAIBsFqULIluFdiFrw1Pt44m0afSlCn3XJiZyLlskDFLDXho2nYGKaZS+IWe58JrrvJDTJhTAZ/B0Lv7+fjRtPLevjOkiYl8eZIFeNEQjyODTRk+yI3gn1xkucmss39hQSd5QRPBufv/DKOWsKyd16bMfmgowrcboXCZw1PX1E1dpwKwZc+t6USUezfw0zqi/VJpBvYaPSwaaE5Jkn6toGeF7Wm2kxeqp+k/iSaifABFPqZLafypnCPkv0TZyW2UrsOq07myWKquzosQpaF+LclIRo09JEEy5pKwE8d2r2emY38/MS0lTDnz2KX/Y2ajswabei1EoRn6vBzly3Az2A/RPgce3mUqlrvbk9ThnBeeRO6eeSaCnNdjE3ebKACeUkMaCC+1ILKclNbwzEhzBdmPO3tiSadMUQlflv7WHYyuksI4cFFnMDBWFuaZkOPYjnyRHcI/GJGFy3GBhFIFGk1SpT 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: 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. (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 in a later patch shows this doesn't introduce 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. In a cgroup that is 4 levels deep (/sys/fs/cgroup/a/b/c/d), the following benchmarks were ran: (a) neper [1] with 1000 flows and 100 threads (single machine). The values in the table are the average of server and client throughputs in mbps after 30 iterations, each running for 30s: tcp_rr tcp_stream Base 9504218.56 357366.84 Patched 9656205.68 356978.39 Delta +1.6% -0.1% Standard Deviation 0.95% 1.03% An increase in the performance of tcp_rr doesn't really make sense, but it's probably in the noise. The same tests were ran with 1 flow and 1 thread but the throughput was too noisy to make any conclusions (the averages did not show regressions nonetheless). Looking at perf for one iteration of the above test, __mod_memcg_state() (which is where memcg_rstat_updated() is called) does not show up at all without this patch, but it shows up with this patch as 1.06% for tcp_rr and 0.36% for tcp_stream. (b) Running "stress-ng --vm 0 -t 1m --times --perf". I don't understand stress-ng very well, so I am not sure that's the best way to test this, but it spawns 384 workers and spits a lot of metrics which looks nice :) I picked a few ones that seem to be relevant to the stats update path. I also included cache misses as this patch introduce more atomics that may bounce between cpu caches: Metric Base Patched Delta Cache Misses 3.394 B/sec 3.433 B/sec +1.14% Cache L1D Read 0.148 T/sec 0.154 T/sec +4.05% Cache L1D Read Miss 20.430 B/sec 21.820 B/sec +6.8% Page Faults Total 4.304 M/sec 4.535 M/sec +5.4% Page Faults Minor 4.304 M/sec 4.535 M/sec +5.4% Page Faults Major 18.794 /sec 0.000 /sec Kmalloc 0.153 M/sec 0.152 M/sec -0.65% Kfree 0.152 M/sec 0.153 M/sec +0.65% MM Page Alloc 4.640 M/sec 4.898 M/sec +5.56% MM Page Free 4.639 M/sec 4.897 M/sec +5.56% Lock Contention Begin 0.362 M/sec 0.479 M/sec +32.32% Lock Contention End 0.362 M/sec 0.479 M/sec +32.32% page-cache add 238.057 /sec 0.000 /sec page-cache del 6.265 /sec 6.267 /sec -0.03% This is only using a single run in each case. I am not sure what to make out of most of these numbers, but they mostly seem in the noise (some better, some worse). The lock contention numbers are interesting. I am not sure if higher is better or worse here. No new locks or lock sections are introduced by this patch either way. Looking at perf, __mod_memcg_state() shows up as 0.00% with and without this patch. This is suspicious, but I verified while stress-ng is running that all the threads are in the right cgroup. [1]https://github.com/google/neper Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index ef7ad66a9e4c..c273c65bb642 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -627,6 +627,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 { @@ -641,6 +644,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; }; /* @@ -660,9 +666,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) @@ -689,26 +693,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); } } @@ -727,13 +742,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(); } @@ -747,8 +761,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); @@ -5622,6 +5636,9 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } } } + /* 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 From patchwork Thu Sep 21 08:10:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13393761 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 2D739E706E7 for ; Thu, 21 Sep 2023 08:11:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB2CD6B01F7; Thu, 21 Sep 2023 04:11:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C63DC6B01F9; Thu, 21 Sep 2023 04:11:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A66E06B01FB; Thu, 21 Sep 2023 04:11:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 923596B01F7 for ; Thu, 21 Sep 2023 04:11:11 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6BA3E16109A for ; Thu, 21 Sep 2023 08:11:11 +0000 (UTC) X-FDA: 81259884342.05.D17E841 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf09.hostedemail.com (Postfix) with ESMTP id A9CB1140020 for ; Thu, 21 Sep 2023 08:11:09 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BVZJrS9n; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3nPoLZQoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3nPoLZQoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695283869; 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=ap3WQf9RL9Y6q/AtYDIGLhJT98YcDdn8KWtOOFcbuqo=; b=hex0mehM8w2boyv+XCrlD9ZvOTRMnaW5/mmdDmoLLG9riQwcHkxL0k5jZjRlSS8ojjZJ2F nTCPwHBQ5AlKsGi5HcU+ZG15X/Kqfjj7PEpiFKyCvadizJ3HzXEYdh0xWbU62eW6Q2vjnm p65+Tww0gqQgEjAT2gUpugLbSvA1XWc= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BVZJrS9n; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3nPoLZQoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3nPoLZQoKCO8pfjipRYdVUXffXcV.TfdcZelo-ddbmRTb.fiX@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695283869; a=rsa-sha256; cv=none; b=f817tM4BPUdNzXRUOYHmqMGKrSB51BRFaiPjUCDAyTWYsN5y7iyUymN9ysLWzah3rwPZ5J FfJsGYBAevsmW6Sr0N6Z8tBgNoqfDdbpa9ZFSuhS+wcN9FvPeelyIe8oqDh3aY7JQUuipV kdpdFYDrJ1CHf2nOf4NBVUv4nEvrLHE= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59c15767524so10553707b3.0 for ; Thu, 21 Sep 2023 01:11:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695283869; x=1695888669; 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=ap3WQf9RL9Y6q/AtYDIGLhJT98YcDdn8KWtOOFcbuqo=; b=BVZJrS9nORw8w3QsuSO9GIrrSPJ9hC5vnEes6M8V5VbZoz1HNIcSKrdmJ/41SrspnY F8dGlEME9e/DUVASxPTxVy6mZkNTK27LRg6W20fu+3oI1eqb4K9NbrnHwjr3JUc39bb2 LO5VsY/YRBPWqXgyMaNRMAQDXXVLp3a7muK9gFZ3hFEh9eI89nvhufZoi5m2fqEN/hfF AkPRg2efUjhBDoipFZIBFqpIQuktcsHxhTwN9AvqwG424lk1/qxyuE9EgRUxbKkXnAto 16WdK4+EJpJFGiHQ72yv3Hk6r2oV3Jr98ZKaqoAJhi5evtdlbsGnOzipYm0Mgd53hcOj 1x4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695283869; x=1695888669; 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=ap3WQf9RL9Y6q/AtYDIGLhJT98YcDdn8KWtOOFcbuqo=; b=etRR6kFxdwZalrcAQESd13eLkff6LTB1G4CH/rpNaavMsiwdEq2WAsfjN4TodvtMkI vzw3VyuZ8OMDiGg1aIbh+gzUh1rb7QJ5h7Jma+h8tK/BCHcwVdCHxdzWq7r5SH9FLm3b rcuIMgIYeU9JJgeC4P01v5LbakI5vf/blHXlci41RrIlLBx8UQpLwpGtSY/SKZgi4WJB XUZSlqe6nhazw7UYrgFuOhqFWG432sWMFchOnhYNH24ZRHmA3MMkIHKj+g8Juop5kXRQ +z0yf8xnX8/82J6DUvtU+BE1fqFrp6DDOA1xnZyXsgZAdZ4jvJSz9Bm3AfTw1o3JB9Je TGDw== X-Gm-Message-State: AOJu0YxKPWwycEdNvmG9Da9e+P1iKA5m5b7IqLgT6t+RCYgGb4o+vTyd x27DfWI8LA2EH/xa8PxeW+mxjKnpPbJoSTcn X-Google-Smtp-Source: AGHT+IEctDDQF0RHmq3JRn62J6oQCd8whI4eyWsA+THF8MJIpkdXC4/+HB3bqA3JcJDKn3Dhk9UKpKL30JXlgF3v X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:504d:0:b0:d62:7f3f:621d with SMTP id e74-20020a25504d000000b00d627f3f621dmr75596ybb.11.1695283868835; Thu, 21 Sep 2023 01:11:08 -0700 (PDT) Date: Thu, 21 Sep 2023 08:10:56 +0000 In-Reply-To: <20230921081057.3440885-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230921081057.3440885-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230921081057.3440885-5-yosryahmed@google.com> Subject: [PATCH 4/5] mm: workingset: move the stats flush into workingset_test_recent() 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 , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A9CB1140020 X-Stat-Signature: 5rbhutqsf3wyi3nt398n5i5a8wawr8a1 X-Rspam-User: X-HE-Tag: 1695283869-623625 X-HE-Meta: U2FsdGV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YtavVOtdbJIB3h90JFKmQGARNmND5aQ310VZr98iSA6mdOGdjxJDv6BGQM4wcIXKS1EDTP74lOc2YP8dp3AI5QA3+H7fMsDrF5n08RwqPFCW4HXtfR0U/Trj/JZKJ9cAFurw7i5LNPR7G+Y5hiI34uM6bL1efhqYeSETNeofnZfrhXh9aStGelG01yItZQWvxdi3NGhnZayGvbr57d0KZ7m31vkzef3CeN6pwQbpvZY00mDKm3DqsJCxItvRThlM/NO64MuJVYml8r4OUYfq5Ve9AxHJBYyUqGxhPWvaq3NAXUDI37WTghNr/qErnocCXYSarrW2ePG5XmG5pR7JTw386epzVdwH0ozKyonm/VG1LOAOlBPqc8wsC9KMcoweSfzWONOguv4onrK2UFBb4bgrHmdJ9XpLvkH2ADlD77+NJ6wR4MjiiKEXzUEAg5bJ1pcITO6fAIopKMMWsniYNye/9hGQPpaZZ3UBjyzB+FCTTbq+v+fGHaJK/6krwRUA8Dp7dv3N51FRztcTu/L4PMXkXveKinKIJmV1yZcyJCENDFbRrecQBMNBu8gMdOQIhP+v7/BJy44LI6veAVYcMUjWmgKDQfAw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.004925, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The workingset code flushes the stats in workingset_refault() to get accurate stats of the eviction memcg. In preparation for more scoped flushed and passing the eviction memcg to the flush call, move the call to workingset_test_recent() where we have a pointer to the eviction memcg. The flush call is sleepable, and cannot be made in an rcu read section. Hence, minimize the rcu read section by also moving it into workingset_test_recent(). Furthermore, instead of holding the rcu read lock throughout workingset_test_recent(), only hold it briefly to get a ref on the eviction memcg. This allows us to make the flush call after we get the eviction memcg. As for workingset_refault(), nothing else there appears to be protected by rcu. The memcg of the faulted folio (which is not necessarily the same as the eviction memcg) is protected by the folio lock, which is held from all callsites. Add a VM_BUG_ON() to make sure this doesn't change from under us. No functional change intended. Signed-off-by: Yosry Ahmed --- mm/workingset.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index b192e44a0e7c..79b338996088 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -425,8 +425,15 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) struct pglist_data *pgdat; unsigned long eviction; - if (lru_gen_enabled()) - return lru_gen_test_recent(shadow, file, &eviction_lruvec, &eviction, workingset); + rcu_read_lock(); + + if (lru_gen_enabled()) { + bool recent = lru_gen_test_recent(shadow, file, + &eviction_lruvec, &eviction, + workingset); + rcu_read_unlock(); + return recent; + } unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<= bucket_order; @@ -451,6 +458,12 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) if (!mem_cgroup_disabled() && !eviction_memcg) return false; + css_get(&eviction_memcg->css); + rcu_read_unlock(); + + /* Flush stats (and potentially sleep) outside the RCU read section */ + mem_cgroup_flush_stats_ratelimited(); + eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -493,6 +506,7 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) } } + mem_cgroup_put(eviction_memcg); return refault_distance <= workingset_size; } @@ -519,19 +533,16 @@ void workingset_refault(struct folio *folio, void *shadow) return; } - /* Flush stats (and potentially sleep) before holding RCU read lock */ - mem_cgroup_flush_stats_ratelimited(); - - rcu_read_lock(); - /* * The activation decision for this folio is made at the level * where the eviction occurred, as that is where the LRU order * during folio reclaim is being determined. * * However, the cgroup that will own the folio is the one that - * is actually experiencing the refault event. + * is actually experiencing the refault event. Make sure the folio is + * locked to guarantee folio_memcg() stability throughout. */ + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); nr = folio_nr_pages(folio); memcg = folio_memcg(folio); pgdat = folio_pgdat(folio); @@ -540,7 +551,7 @@ void workingset_refault(struct folio *folio, void *shadow) mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); if (!workingset_test_recent(shadow, file, &workingset)) - goto out; + return; folio_set_active(folio); workingset_age_nonresident(lruvec, nr); @@ -556,8 +567,6 @@ void workingset_refault(struct folio *folio, void *shadow) lru_note_cost_refault(folio); mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr); } -out: - rcu_read_unlock(); } /** From patchwork Thu Sep 21 08:10:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13393762 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 351CDE706E5 for ; Thu, 21 Sep 2023 08:11:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D00A46B01FB; Thu, 21 Sep 2023 04:11:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB38C6B01FE; Thu, 21 Sep 2023 04:11:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADBCF6B01FF; Thu, 21 Sep 2023 04:11:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 958D06B01FB for ; Thu, 21 Sep 2023 04:11:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 650F412100E for ; Thu, 21 Sep 2023 08:11:13 +0000 (UTC) X-FDA: 81259884426.15.E037921 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf18.hostedemail.com (Postfix) with ESMTP id 91DBB1C0009 for ; Thu, 21 Sep 2023 08:11:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sM1NhMT9; spf=pass (imf18.hostedemail.com: domain of 3nvoLZQoKCPErhlkrTafXWZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3nvoLZQoKCPErhlkrTafXWZhhZeX.Vhfebgnq-ffdoTVd.hkZ@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=1695283871; 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=3167ObCwuPGy0ykS2mGpCnZZz/lnDX5wo2e/FWfZcMc=; b=f5c63QvYy3cHwhuFH/SBCEelGx5DUimx6LAWkz9uNSUnU6fRkE9e4RCRP2HOWmQaBX6c42 q9iGzdYmNoaHW/ld2HqvKmR0AW+O/r2J8dmMY2lPYF8146obRusMiGa9lKYgpMBFPF+Z/g +Vcpmbk/G7I9lcy0VSeP3sdWLB6qKi8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695283871; a=rsa-sha256; cv=none; b=CVJV1Vj8PXngukeZR5NNggNV9F0YaxZFqjSZ93EO5B3Wo/w2yTVQwNE3uwK2aQVI1yoc7w p2BKrXnh7pvgxdXNsc9beo+ZR9RCfgaw9Ybo4VHR3Y/lCEv7ivzw/SLUbZ5pYJT13gWb76 YaORwiUH1gWuhOX4M4bXrXyjuxcBnJo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sM1NhMT9; spf=pass (imf18.hostedemail.com: domain of 3nvoLZQoKCPErhlkrTafXWZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3nvoLZQoKCPErhlkrTafXWZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d81d85aae7cso3443001276.0 for ; Thu, 21 Sep 2023 01:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695283870; x=1695888670; 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=3167ObCwuPGy0ykS2mGpCnZZz/lnDX5wo2e/FWfZcMc=; b=sM1NhMT9Q1QiU5fVpdXXyvMGhdOPvs+ZLFZQiAgs1gP++oSYXq+J0HivrbUqHpc9zg OG0xQvChS0M9fWBZmLCCeX13HBm/C8E4G2H9t1rzTgzkeO0r9WpUuMIHy7OovvcSWg4+ 7MKXYQwJe7IT9mXr8p8gntJUHC2WZlpyiWC7+A6V8pKGYygJHA0adLT9r7hpROdY4FF1 bzPNgHyzVCiIoNmJHwNxaZzAgneXaLIY0lxaVAngNUUWmvIt7rQa/4+PQc6GCG0VyAiL t6Mt7SNF80XLXZTgDhdAypvTFqyvbg3K6sPQ5qcPTdSqO3OwelvTi5DNx5Gqn+voSEgM L2NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695283870; x=1695888670; 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=3167ObCwuPGy0ykS2mGpCnZZz/lnDX5wo2e/FWfZcMc=; b=DZCHOegFA84MmKgp2ZLKLMUKNG7D9UBnELw8bOPbNrQ0bME1mUXNZvF2AWHvim2TWZ bUko9pIdcOU9xuIdcvsveKH5fE/dAsmN+9hoa0S6XDnSc7F5KlzhpWVx5h1BRiEtVwkB rhUy+Dh+zi5QNSYJlcYgfrVHI9rdGlFIZzmKTnDU6gQbBqPURedSpjWQ4FnLBY1AKj8i QC54U5011GuC+TK+IEQrBCjRjCv3UV4ysG66M+GIrjzxlJj60i+FuXv+qOh0sOVlwXnt 9dA0KvS+YUenUN+kHY8qbnHCRkfaXbI2GnhnVXc9XofkWYvJYr4shNIq/Wta+T1idVgm PZFQ== X-Gm-Message-State: AOJu0YxQmn4GD8hgwSDbA/YLx0NyMenJdoZwGQXYr6GQTFZhsPayd1wp 1PDevt3xmcYW6CwNbd12Bo4VorVgcDGIAjK8 X-Google-Smtp-Source: AGHT+IEJ4fcoyEGYjUXnEO4k8S62qHUkVPnBwaKgs95Sypxteexrw0RSMgrAyDrgXkVHLcy4yrd/Nrxvhz1akcJT X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:e0c5:0:b0:d78:28d0:15bc with SMTP id x188-20020a25e0c5000000b00d7828d015bcmr99461ybg.4.1695283870798; Thu, 21 Sep 2023 01:11:10 -0700 (PDT) Date: Thu, 21 Sep 2023 08:10:57 +0000 In-Reply-To: <20230921081057.3440885-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230921081057.3440885-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.459.ge4e396fd5e-goog Message-ID: <20230921081057.3440885-6-yosryahmed@google.com> Subject: [PATCH 5/5] mm: memcg: restore subtree stats flushing 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 , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 91DBB1C0009 X-Rspam-User: X-Stat-Signature: 49zmnmsbqf4bw43ymgza9cjfcwre4bp5 X-Rspamd-Server: rspam03 X-HE-Tag: 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/6XpsxKfhqDvtQkHR58SCFQ3Pfe5BGgXKRSHcFtfZd+7i0AYSzysS1QMeTGcqTJZTSVCc5lKTmOFp2oB/cb8AwNIRfsfK1tapZ2ekAslrDhLqeJleeQQaDMLnhUqqijitbUNnILgCeeYxcAAr/Su6EMA6QTo1Hb6/ylsavWGVFavDwKwXno8lTDkvRdz7ql5fGlNZlm/3LuWtmTLG7D8XTlq+V+HLoXLGGUgiwgpHhW6n1eiwDUmEpOIfg6NsBZnQq3Wrr7rvV8tDF+gQf63k9LDYN4VkBr3s28hX95KBZvVQe+AvHor0UCSP2HzHMYEhlNrtBCwQG5Nf8YbaxTDFVv3k4AdJpOwX65OMgMnTmmuDxI8MrfiFNKZPKp8qBrXpKf7gajvQAws0rCPsVhb2RY8LkjPSiAf6B9FAmnl2gRSu0LGoEK+9uy5gc+CS2rl32c3e7p5Vt600+Ph0Qm1IR7e3eSuqc+TdIfPP862hp9L6P8V5jgmZFg3dZ+nhXmvGYhkXQD7OO/JiI9BCcYGCE14NsRlpmY3zXJyp1qSP7H1DB8KkOPS0EYosPNcpZrELmHuOb9VkMYUK5DJcUttAkmAZgq1oyThEOWSrJlDlSmpfK/YT3K2OTFLaibNJUmwpvmlzh+U6zksnJKdlnNqXQ0E 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: Stats flushing for memcg currently follows the following rules: - Always flush the entire memcg hierarchy (i.e. flush the root). - Only one flusher is allowed at a time. If someone else tries to flush concurrently, they skip and return immediately. - A periodic flusher flushes all the stats every 2 seconds. The reason this approach is followed is because all flushes are serialized by a global rstat spinlock. On the memcg side, flushing is invoked from userspace reads as well as in-kernel flushers (e.g. reclaim, refault, etc). This approach aims to avoid serializing all flushers on the global lock, which can cause a significant performance hit under high concurrency. This approach has the following problems: - Occasionally a userspace read of the stats of a non-root cgroup will be too expensive as it has to flush the entire hierarchy [1]. - Sometimes the stats accuracy are compromised if there is an ongoing flush, and we skip and return before the subtree of interest is actually flushed, yielding stale stats (by up to 2s due to periodic flushing). This is more visible when reading stats from userspace, but can also affect in-kernel flushers. The latter problem is particulary a concern when userspace reads stats after an event occurs, but gets stats from before the event. Examples: - When memory usage / pressure spikes, a userspace OOM handler may look at the stats of different memcgs to select a victim based on various heuristics (e.g. how much private memory will be freed by killing this). Reading stale stats from before the usage spike in this case may cause a wrongful OOM kill. - A proactive reclaimer may read the stats after writing to memory.reclaim to measure the success of the reclaim operation. Stale stats from before reclaim may give a false negative. - Reading the stats of a parent and a child memcg may be inconsistent (child larger than parent), if the flush doesn't happen when the parent is read, but happens when the child is read. As for in-kernel flushers, they will occasionally get stale stats. No regressions are currently known from this, but if there are regressions, they would be very difficult to debug and link to the source of the problem. This patch aims to fix these problems by restoring subtree flushing, and removing the unified/coalesced flushing logic that skips flushing if there is an ongoing flush. This change would introduce a significant regression with global stats flushing thresholds. With per-memcg stats flushing thresholds, this seems to perform really well. The thresholds protect the underlying lock from unnecessary contention. Add a mutex to protect the underlying rstat lock from excessive memcg flushing. The thresholds are re-checked after the mutex is grabbed to make sure that a concurrent flush did not already get the subtree we are trying to flush. A call to cgroup_rstat_flush() is not cheap, even if there are no pending updates. This patch was tested in two ways to ensure the latency of flushing is up to bar, on a machine with 384 cpus: - A synthetic test with 5000 concurrent workers in 500 cgroups doing allocations and reclaim, as well as 1000 readers for memory.stat (variation of [2]). No regressions were noticed in the total runtime. Note that significant regressions in this test are observed with global stats thresholds, but not with per-memcg thresholds. - A synthetic stress test for concurrently reading memcg stats while memory allocation/freeing workers are running in the background, provided by Wei Xu [3]. With 250k threads reading the stats every 100ms in 50k cgroups, 99.9% of reads take <= 50us. Less than 0.01% of reads take more than 1ms, and no reads take more than 100ms. [1] https://lore.kernel.org/lkml/CABWYdi0c6__rh-K7dcM_pkf9BJdTRtAU08M43KO9ME4-dsgfoQ@mail.gmail.com/ [2] https://lore.kernel.org/lkml/CAJD7tka13M-zVZTyQJYL1iUAYvuQ1fcHbCjcOBZcz6POYTV-4g@mail.gmail.com/ [3] https://lore.kernel.org/lkml/CAAPL-u9D2b=iF5Lf_cRnKxUfkiEe0AMDTu6yhrUAzX0b6a6rDg@mail.gmail.com/ Signed-off-by: Yosry Ahmed --- include/linux/memcontrol.h | 8 ++--- mm/memcontrol.c | 73 +++++++++++++++++++++++--------------- mm/vmscan.c | 2 +- mm/workingset.c | 10 ++++-- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 45d0c10e86cc..1b61a2707307 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1030,8 +1030,8 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, return x; } -void mem_cgroup_flush_stats(void); -void mem_cgroup_flush_stats_ratelimited(void); +void mem_cgroup_flush_stats(struct mem_cgroup *memcg); +void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val); @@ -1520,11 +1520,11 @@ 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 mem_cgroup_flush_stats(struct mem_cgroup *memcg) { } -static inline void mem_cgroup_flush_stats_ratelimited(void) +static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) { } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c273c65bb642..99cfba81684f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -666,7 +666,6 @@ 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 atomic_t stats_flush_ongoing = ATOMIC_INIT(0); static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -727,35 +726,45 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) } } -static void do_flush_stats(void) +static void do_flush_stats(struct mem_cgroup *memcg) { - /* - * We always flush the entire tree, so concurrent flushers can just - * skip. This avoids a thundering herd problem on the rstat global lock - * from memcg flushers (e.g. reclaim, refault, etc). - */ - if (atomic_read(&stats_flush_ongoing) || - atomic_xchg(&stats_flush_ongoing, 1)) - return; - - WRITE_ONCE(flush_last_time, jiffies_64); - - cgroup_rstat_flush(root_mem_cgroup->css.cgroup); + if (mem_cgroup_is_root(memcg)) + WRITE_ONCE(flush_last_time, jiffies_64); - atomic_set(&stats_flush_ongoing, 0); + cgroup_rstat_flush(memcg->css.cgroup); } -void mem_cgroup_flush_stats(void) +/* + * mem_cgroup_flush_stats - flush the stats of a memory cgroup subtree + * @memcg: root of the subtree to flush + * + * Flushing is serialized by the underlying global rstat lock. There is also a + * minimum amount of work to be done even if there are no stat updates to flush. + * Hence, we only flush the stats if the updates delta exceeds a threshold. This + * avoids unnecessary work and contention on the underlying lock. + */ +void mem_cgroup_flush_stats(struct mem_cgroup *memcg) { - if (memcg_should_flush_stats(root_mem_cgroup)) - do_flush_stats(); + static DEFINE_MUTEX(memcg_stats_flush_mutex); + + if (!memcg) + memcg = root_mem_cgroup; + + if (!memcg_should_flush_stats(memcg)) + return; + + mutex_lock(&memcg_stats_flush_mutex); + /* An overlapping flush may have occurred, check again after locking */ + if (memcg_should_flush_stats(memcg)) + do_flush_stats(memcg); + mutex_unlock(&memcg_stats_flush_mutex); } -void mem_cgroup_flush_stats_ratelimited(void) +void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) { /* Only flush if the periodic flusher is one full cycle late */ if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME)) - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); } static void flush_memcg_stats_dwork(struct work_struct *w) @@ -764,7 +773,7 @@ static void flush_memcg_stats_dwork(struct work_struct *w) * Deliberately ignore memcg_should_flush_stats() here so that flushing * in latency-sensitive paths is as cheap as possible. */ - do_flush_stats(); + do_flush_stats(root_mem_cgroup); queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); } @@ -1593,7 +1602,7 @@ static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) * * Current memory state: */ - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { u64 size; @@ -4035,7 +4044,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v) int nid; struct mem_cgroup *memcg = mem_cgroup_from_seq(m); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { seq_printf(m, "%s=%lu", stat->name, @@ -4116,7 +4125,7 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats)); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; @@ -4613,7 +4622,7 @@ void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); struct mem_cgroup *parent; - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); @@ -6640,7 +6649,7 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) int i; struct mem_cgroup *memcg = mem_cgroup_from_seq(m); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { int nid; @@ -7806,7 +7815,11 @@ bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) break; } - cgroup_rstat_flush(memcg->css.cgroup); + /* + * mem_cgroup_flush_stats() ignores small changes. Use + * do_flush_stats() directly to get accurate stats for charging. + */ + do_flush_stats(memcg); pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE; if (pages < max) continue; @@ -7871,8 +7884,10 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) static u64 zswap_current_read(struct cgroup_subsys_state *css, struct cftype *cft) { - cgroup_rstat_flush(css->cgroup); - return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B); + struct mem_cgroup *memcg = mem_cgroup_from_css(css); + + mem_cgroup_flush_stats(memcg); + return memcg_page_state(memcg, MEMCG_ZSWAP_B); } static int zswap_max_show(struct seq_file *m, void *v) diff --git a/mm/vmscan.c b/mm/vmscan.c index a4e44f1c97c1..60bead17b1f7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2246,7 +2246,7 @@ static void prepare_scan_control(pg_data_t *pgdat, struct scan_control *sc) * Flush the memory cgroup stats, so that we read accurate per-memcg * lruvec stats for heuristics. */ - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(sc->target_mem_cgroup); /* * Determine the scan balance between anon and file LRUs. diff --git a/mm/workingset.c b/mm/workingset.c index 79b338996088..141defbe3da2 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -461,8 +461,12 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) css_get(&eviction_memcg->css); rcu_read_unlock(); - /* Flush stats (and potentially sleep) outside the RCU read section */ - mem_cgroup_flush_stats_ratelimited(); + /* + * Flush stats (and potentially sleep) outside the RCU read section. + * XXX: With per-memcg flushing and thresholding, is ratelimiting + * still needed here? + */ + mem_cgroup_flush_stats_ratelimited(eviction_memcg); eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -673,7 +677,7 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker, struct lruvec *lruvec; int i; - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(sc->memcg); lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); for (pages = 0, i = 0; i < NR_LRU_LISTS; i++) pages += lruvec_page_state_local(lruvec,