From mboxrd@z Thu Jan 1 00:00:00 1970 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 139DAC2BD09 for ; Thu, 27 Jun 2024 10:34:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3FD9C6B0082; Thu, 27 Jun 2024 06:34:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3ACFB6B0083; Thu, 27 Jun 2024 06:34:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 274386B0088; Thu, 27 Jun 2024 06:34:32 -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 0A38C6B0082 for ; Thu, 27 Jun 2024 06:34:32 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B295641CE5 for ; Thu, 27 Jun 2024 10:34:31 +0000 (UTC) X-FDA: 82276309542.18.D9C6280 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf04.hostedemail.com (Postfix) with ESMTP id E01BC40018 for ; Thu, 27 Jun 2024 10:34:29 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=S7OAPQEQ; spf=pass (imf04.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719484461; a=rsa-sha256; cv=none; b=c4W1qGtWwwWwp6vzTeDgyTf/02FS8zXK+8QyQ72VCqm6Lx1/mL5pSwhqeTAQ7j6xOGCBZr zQqJk4230Z6hDedu1C+SaqWZPO90J8a+RA5h4OTwL5f8zX2DAYXnYUWzhaEy/GEK3+cXUr opU3VhHdHJgkTF4d8KUca3DMIL0+6Cs= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=S7OAPQEQ; spf=pass (imf04.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=yosryahmed@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=1719484461; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=j9FS9JzVBeWa6xY6RjccFGH3HEep7Fmdm+7GGppOyXg=; b=RICFHYAxe6mcDAMSJ4k2ja671o3EyhczzyfXcV5HQZs4bZwhl/9XdJraklJmYPiQai8Xr1 E/na8MrnOXNUzBN3MVb4SvZRcZglphUl9RpUA/o95giWEY2q2vozVNQpuyEFafk3E/2w++ pLq8NXJ9DDpjqmmjpNwR++FC6ZWGqp8= Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-a725a918edaso409857766b.3 for ; Thu, 27 Jun 2024 03:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1719484468; x=1720089268; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=j9FS9JzVBeWa6xY6RjccFGH3HEep7Fmdm+7GGppOyXg=; b=S7OAPQEQR0CG3IfGptSrAW9lKsRDPPQrnRDa4Zsoaj6mdXFrIRL9pa7y+tEz74cCae 3XqvKF3iptRgUFGLhpX1AtkrX2AVNENtZ6EIU9Im2jEiq2cT0hgZJtCQlalU+leunQLx KnuAgCyqUcAnHpiEhRGx6AQ3lD54bG17O11wgOzEn2DqJ3bDSw/ODcEPfheid9ZdS/Y9 +G99VU2qyv+cNh2eErK3nDuaii+3my3ky8kJs4OdHZ1yMP+FtmSJtTUK2/IX38ubY0dh 6ldGI70sOKLvjfuly4hyaSW381UtTzsJu22vutGldJsLBeb7A4vPWo/zTjNVbNEuUpxL j0QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719484468; x=1720089268; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j9FS9JzVBeWa6xY6RjccFGH3HEep7Fmdm+7GGppOyXg=; b=lgw4ZMw5BuhH1cPHhG1ambLiJ87UOSnCUIUBKJP6FmKGIM1SBcqmVA/grKYgJIfY0X oushrz6qG0+UNytpFeOZTB8yTX9TF6rgBpA48hUVXQjZtFP98O3UPTAn7NEfLuL+P2rb EhwzgQVkpzQz7RRX3/ImpyEN8tRvPKlmbvJEHwEH0DprCBTUie1KSgYnHo4/k/2euKOp rZEZ8y3sKEDFC2DBz3w+oZGD2jn5rNIqbgax8fFBIvYV6DgfbBj7C+MwDVuxAwoUcOFD 1t+6s1HTUhxnUjKbtJmEMYILrbVrG0EzoRzUhquh3Wp2rToSgv5Fnrxo3rCydwcb4WDY YGwg== X-Forwarded-Encrypted: i=1; AJvYcCXcx6AXeYDYbOX6JqcOnQYcf3L+6yV3bNsXnwpuTtdS5C3UpGKcd268+kZy1w106HrwDUXLUZooTwyJ2cQjZ6jmnC0= X-Gm-Message-State: AOJu0YwyUKng/H2wVVQedZK3wNie3fAvsvalnbTzXCs2oXl0RudKG0j0 gfRYw6Xr5xILR7aEFtip5vgba2qWe+HmoxqcyvfPAY9N6LNnzeLeshFY6wHex/hCHXLs3twPVWp ptyv63SlsrbzQcbn43h6w/CckjDXUBxX0BVey X-Google-Smtp-Source: AGHT+IGdO93khlibtkshuAazBmWw34RWyRGVw1+TAnqsxI8uf5V5MdIMHrT8t2WBfe+sceFvDcXuReAU2O9n7qvXycM= X-Received: by 2002:a17:906:d96e:b0:a6f:608e:c0d0 with SMTP id a640c23a62f3a-a7245ba3cc0mr1099324466b.25.1719484467646; Thu, 27 Jun 2024 03:34:27 -0700 (PDT) MIME-Version: 1.0 References: <171943667611.1638606.4158229160024621051.stgit@firesoul> <171943668946.1638606.1320095353103578332.stgit@firesoul> In-Reply-To: <171943668946.1638606.1320095353103578332.stgit@firesoul> From: Yosry Ahmed Date: Thu, 27 Jun 2024 03:33:49 -0700 Message-ID: Subject: Re: [PATCH V3 2/2] cgroup/rstat: Avoid thundering herd problem by kswapd across NUMA nodes To: Jesper Dangaard Brouer Cc: tj@kernel.org, cgroups@vger.kernel.org, shakeel.butt@linux.dev, hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: k9xeahg7k9gy9oy18gqtxbrw5xw7569f X-Rspamd-Queue-Id: E01BC40018 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1719484469-122408 X-HE-Meta: U2FsdGVkX18S5ZOnW2WjZLiJWSKC35FgY2QDJoxb58A1CsQrNRLjcSZ7X8wo2KwZxB5uijY1BIXqsG6oYtdhy/FnmcyU+b8Jz59PNYSrk5sn1z7YcBxfUONFP0x8jwugn5imzBss86iNXzLti9+fAlBSbuJ9y60u6FutHjKLOlqTmgeJ2ORY/Ew0ysTp8tiRbLdLomgfMkCCGfrs0VBLyf5o38UT1X5hP9/uRpBB1FU3w6aQFbReaP5axo0iy6VCs4Vl8lcgyE1p9gCE6V9s9A7BaBnfR78XjKX3yVOniW4ZI4S8nfLq/Cg5IYWRG/iao72EsmRJ1LZQ75QQtExGydKu+yn54sdgwfFGQfycVgsWUzAWrDDfj2hPyZD7mOnZFj91p5t7ups/qATiafwX48BxEw+QWHKDUbLV00MffHdY78NXzDB+uHGFDu32VPEaKMTJqwvYC1i2TDX0+QDIszUFjorkbQLtBKjUXR0YcXV29VcpsrTiHd1DHUd4fHZJDLTx1itMghK3AX8RniwltMQvruO4117P3Zdxu8nuQROH8Lyq2SjSEt3QhzyByMGl0DBg4+awq+UlOK3jTuY+TV58YlNpWgAHP/muOdjZTk/AuQ+qFX3PkBql9RF7bk7h3YVkYKgT/z/PPY5dB1QhzAWFwsHphOWyqHy4C6rDGNuJz/Qm0y9610m1zO6yLmqX0Boim45QADkBShBiPSDm5czFBGSfk+oyxMXwDTdsf2iIkt2GcK9f2AReecsXq/HFmEtl/amoYj+2FV/X0uKJTraKmHiYALDIMtddSiOHO8OpfzbF1dwUkxrIluORHEbCJIGMUPN5uJLQ+NJ2AcvAqkey7YO3FOAWZ3F2AMun2Ot0iASASWvB9joMEe5Dg/h+yR448wCrCcts7Blqdu6ag/sTRSwN195+TJ5fhQf/7AS7IVc6Cr9qOy0pqCjME35AELt58QKqRCGaYCieoqD 9qMMU+3W F4cLd+fDi9OidmGV4n1u+zZ46lliC5WfFFluXk84ChrieLOBCvR8ceORJwSYbRwqJid5FD4hSUXpirn0PSQk3zjYoZxWR8y+CDYKTuldjWRo8HUzsw+8u5wHpMcLH77DKHTdkyd1vimxxNbRy4773rrajOvcwTid2lt3E2UWwutm3cFvd3nfYTS8voczVlaC+7+DX69zi1PKKvcT/6jW26AtOtK57UTxgwcrR6VvJX19evYf4s5fNBaJBj+ekPCa796l8Gx4ifcS9aMY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000294, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Jun 26, 2024 at 2:18=E2=80=AFPM Jesper Dangaard Brouer wrote: > > Avoid lock contention on the global cgroup rstat lock caused by kswapd > starting on all NUMA nodes simultaneously. At Cloudflare, we observed > massive issues due to kswapd and the specific mem_cgroup_flush_stats() > call inlined in shrink_node, which takes the rstat lock. > > On our 12 NUMA node machines, each with a kswapd kthread per NUMA node, > we noted severe lock contention on the rstat lock. This contention > causes 12 CPUs to waste cycles spinning every time kswapd runs. > Fleet-wide stats (/proc/N/schedstat) for kthreads revealed that we are > burning an average of 20,000 CPU cores fleet-wide on kswapd, primarily > due to spinning on the rstat lock. > > To help reviewer follow code: When the Per-CPU-Pages (PCP) freelist is > empty, __alloc_pages_slowpath calls wake_all_kswapds(), causing all > kswapdN threads to wake up simultaneously. The kswapd thread invokes > shrink_node (via balance_pgdat) triggering the cgroup rstat flush > operation as part of its work. This results in kernel self-induced rstat > lock contention by waking up all kswapd threads simultaneously. > Leveraging this detail: balance_pgdat() have NULL value in > target_mem_cgroup, this cause mem_cgroup_flush_stats() to do flush with > root_mem_cgroup. > > To avoid this kind of thundering herd problem, kernel previously had a > "stats_flush_ongoing" concept, but this was removed as part of commit > 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). This patch > reintroduce and generalized the concept to apply to all users of cgroup > rstat, not just memcg. > > If there is an ongoing rstat flush, and current cgroup is a descendant, > then it is unnecessary to do the flush. For callers to still see updated > stats, wait for ongoing flusher to complete before returning, but add > timeout as stats are already inaccurate given updaters keeps running. > > Fixes: 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). > Signed-off-by: Jesper Dangaard Brouer > --- > V2: https://lore.kernel.org/all/171923011608.1500238.3591002573732683639.= stgit@firesoul/ > V1: https://lore.kernel.org/all/171898037079.1222367.13467317484793748519= .stgit@firesoul/ > RFC: https://lore.kernel.org/all/171895533185.1084853.3033751561302228252= .stgit@firesoul/ > > kernel/cgroup/rstat.c | 61 ++++++++++++++++++++++++++++++++++++++++---= ------ > 1 file changed, 50 insertions(+), 11 deletions(-) > > diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c > index 2a42be3a9bb3..f21e6b1109a4 100644 > --- a/kernel/cgroup/rstat.c > +++ b/kernel/cgroup/rstat.c > @@ -2,6 +2,7 @@ > #include "cgroup-internal.h" > > #include > +#include > > #include > #include > @@ -11,6 +12,8 @@ > > static DEFINE_SPINLOCK(cgroup_rstat_lock); > static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); > +static struct cgroup *cgrp_rstat_ongoing_flusher; > +static DECLARE_COMPLETION(cgrp_rstat_flusher_done); > > static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu); > > @@ -346,6 +349,44 @@ static void cgroup_rstat_flush_locked(struct cgroup = *cgrp) > } > } > > +#define MAX_WAIT msecs_to_jiffies(100) > +/* Trylock helper that also checks for on ongoing flusher */ > +static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) > +{ > +retry: > + bool locked =3D __cgroup_rstat_trylock(cgrp, -1); > + if (!locked) { > + struct cgroup *cgrp_ongoing; > + > + /* Lock is contended, lets check if ongoing flusher is al= ready > + * taking care of this, if we are a descendant. > + */ > + cgrp_ongoing =3D READ_ONCE(cgrp_rstat_ongoing_flusher); > + if (!cgrp_ongoing) > + goto retry; > + > + if (cgroup_is_descendant(cgrp, cgrp_ongoing)) { > + wait_for_completion_interruptible_timeout( > + &cgrp_rstat_flusher_done, MAX_WAIT); Thanks for sending this! The reason why I suggested that the completion live in struct cgroup is because there is a chance here that the flush completes and another irrelevant flush starts between reading cgrp_rstat_ongoing_flusher and calling wait_for_completion_interruptible_timeout(). This will cause the caller to wait for an irrelevant flush, which may be fine because today the caller would wait for the lock anyway. Just mentioning this in case you think this may happen enough to be a problem. Also, I like the idea of the timeout here, it bounds the flush wait time. I am wondering if there's a way to log something when the timeout is exceeded (which probably means flushing is taking too long), or maybe have a debug counter if we suspect this may spam the log.