From patchwork Tue Jul 9 11:20:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesper Dangaard Brouer X-Patchwork-Id: 13727696 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 9A7F4C3DA41 for ; Tue, 9 Jul 2024 11:21:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 269D56B00A1; Tue, 9 Jul 2024 07:21:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 217BA6B00A2; Tue, 9 Jul 2024 07:21:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DF596B00A3; Tue, 9 Jul 2024 07:21:01 -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 E4C9B6B00A1 for ; Tue, 9 Jul 2024 07:21:00 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 923FC817F0 for ; Tue, 9 Jul 2024 11:21:00 +0000 (UTC) X-FDA: 82319972280.05.D488BDD Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 47FCA80004 for ; Tue, 9 Jul 2024 11:20:57 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fKVJSBeu; spf=pass (imf30.hostedemail.com: domain of hawk@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720524044; 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: references:dkim-signature; bh=OX4IiHhv6fDy26wlvi1eoBXQqGq8CZ9v71pcfMDHmXU=; b=a4Rg0JDkWldjTRU64c1+4hPSC1P/DtYi9DMrKWyBBCEkrI3x+B90iUajK0tt3jDcUNOmmM oa8p+i3uEb/hZP9mL8y78NYW8n8KjoqYnjDr6dRUwj5ulOeeQoUDoVGa7qei73cs5LeAoE oGvw6uGFsbk626qZVk1J77TRIXbKhDo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fKVJSBeu; spf=pass (imf30.hostedemail.com: domain of hawk@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720524044; a=rsa-sha256; cv=none; b=rdfnBwhZav/pcCPWkLmAAxn9ZjUZZM9lelWsmsrAfwWSLNq8KmIwSj8yjrnPbrjWjiLapx Sknqt80ctVXXph3WVtcr+CyBranza91/qEn4INTTLIIy1JvfXMPRlXVydSIKrRUfllBxnv kPZg9ukWIsXBbiK6yiWxG4X3R9OPb10= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 5B614CE0C57; Tue, 9 Jul 2024 11:20:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2CE6C4AF0A; Tue, 9 Jul 2024 11:20:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720524053; bh=uzbwOmcXWAlo++p7zYdgl6a5JDjwKTDeVm3y97WXpRs=; h=Subject:From:To:Cc:Date:From; b=fKVJSBeu3Vmjb5cOTxW9wcwD7v5mXK3Lsj6k9G/5/AtIReEs3tHgrFjN8367bxsrB t5aWZDqo6wDTUYRHvSGTX2MEgDpS7zbiwT+a9Nhy+XMqZxmclzP6aX4C+EEQefSPeg E/vKIo6nm4Px2pkODEjDc5pvuio8AYaWYj1rsYyyvg+73Va8NR8j/eI6jJM8VZg+nV OxSSWo4VzDCvFq7Ikd7SqLnup3V21zqQGRnPVYPsx2ujaWguj4xe11DgvUkmkbBsIy sXnhpjyVJpMu1lOJNDGMHMoJVqIsEzVyv/5GkHA27SvByv+RC/gJKmmSD7VWxn77HZ VedNpi0qPK1Jw== Subject: [PATCH V6] cgroup/rstat: Avoid thundering herd problem by kswapd across NUMA nodes From: Jesper Dangaard Brouer To: tj@kernel.org, cgroups@vger.kernel.org, yosryahmed@google.com, shakeel.butt@linux.dev Cc: Jesper Dangaard Brouer , hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Date: Tue, 09 Jul 2024 13:20:48 +0200 Message-ID: <172052399087.2357901.4955042377343593447.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 47FCA80004 X-Stat-Signature: no5n8th8zuzmj6mtgk468a3yykdwni5f X-HE-Tag: 1720524057-504545 X-HE-Meta: U2FsdGVkX1/DZ1yb3u83xfeuMr86rl65iXwL4LktIWCVpaHsbir/G4CCTHilDB7PrfeF6COOJ9jI8xzW8lownt7OHtjSCYqjDlhZLnR3hWVwU7alaiUheZScss6MqrtiUvegapXZCgKbjfV+yMJnupWR3bFu1qSkWT5bqaf8j7TaT/WQuBtG0Zz9B/LWajebTGs7i1vzts6wZJbbGsP3T4rPIEdB9tB5V3zoAwX0j9B6vskC2jBg/Asydfl4hHiFQUTC5aSOdUUV2gTMHvYpuh9VbvNRatsZandrD/4YmXm39sfcqAr6tEcFJ8ES2jwELKXWEVZmxm8kellqzNAaClOB0GAHjhUSHYzw20KGu6uL14moP6TbDnvWrJjLjVn/FVgH5K82bF2VSzi20vGPl74gIUCWaonxRXPKgtvKg675G16QuAOhzfTG0002dIQF6M029MpKNWC2SFgvER0vRzK3x7SVo3G41y8xrn1nr9/rsv9Ge9welKApoeN9F2S54XCZERbdmWWaS+5NyS3bnjfGlAg6VN9vqYMYYGFLBXQpZP5dW6gRdt1shTypmBVrzDB5lJm4EcopgqfSzymiMSzcU+7fgu/dAtqVJxdNz+LQk6pvOsSado5sclGD9CeSunTPiW82AaiNg6odDUc1FL+iq+CMyXOLLyNGJhv1yyZe0pVA+fwYWRTasL/JlxhFkPYSVmhWY5UmepgcCuGr3r7BRRPwsZFxMmi04Pabv1DwM8jX8VWF9kkEgMYXeZzwApFYnSVd/CSzNeDA9ErxZ/zAqpRQo4rPXtMTyP3Qo8AksxqXcsLcO8O8lGPecGmasGxV/rsx56qEQJEf0/DO7EaVH4awN1STm/FOZzGQkQWr77c1wlIIgn127Sd9Otu78ZFPXs/bf8/s0RiviaEi+6xtDvFaZO6ohox572iywnYQ8Ofbz8C4qh1FjW+NBDhUhrGxSoplBYcNzpKrqJA UqQPd8Qi QZ88KPwYW7MA0zlTBEFdEjpkcQhRHtLqOwuuPB7Xdwj++bfAl0xr4w8C2CENOtDpa0r3lZiB//7FI0jatDBkfo7Co4shMN2sK2zElrbEtZY8ryVxipNbZ5ANGlgqU0cjmt2r3HJ//Iq2XvTRmBv9k9ypbgkxAOmlQZemO404Di585PJ3Gzfe2MezpQevRwiE89P6NPnTH4DT1rNPcmhjBZ9kg0Pssm4rC+AvVAc2hlcaRLtrada5iuVv2Ag== 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: 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. Help reviewers follow code: __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 --- V5: https://lore.kernel.org/all/171956951930.1897969.8709279863947931285.stgit@firesoul/ V4: https://lore.kernel.org/all/171952312320.1810550.13209360603489797077.stgit@firesoul/ V3: https://lore.kernel.org/all/171943668946.1638606.1320095353103578332.stgit@firesoul/ 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/ include/linux/cgroup-defs.h | 2 + kernel/cgroup/rstat.c | 95 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 84 insertions(+), 13 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index b36690ca0d3f..a33b37514c29 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -548,6 +548,8 @@ struct cgroup { #ifdef CONFIG_BPF_SYSCALL struct bpf_local_storage __rcu *bpf_cgrp_storage; #endif + /* completion queue for cgrp_rstat_ongoing_flusher */ + struct completion flush_done; /* All ancestors including self */ struct cgroup *ancestors[]; diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index fb8b49437573..d0b9b8bf275d 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,7 @@ static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); +static struct cgroup *cgrp_rstat_ongoing_flusher = NULL; static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu); @@ -279,17 +281,30 @@ __bpf_hook_end(); * value -1 is used when obtaining the main lock else this is the CPU * number processed last. */ -static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop) +static inline bool __cgroup_rstat_trylock(struct cgroup *cgrp, int cpu_in_loop) +{ + bool locked; + + locked = spin_trylock_irq(&cgroup_rstat_lock); + if (!locked) + trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, !locked); + + return locked; +} + +static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop, + bool check_contention) __acquires(&cgroup_rstat_lock) { - bool contended; + bool locked = false; - contended = !spin_trylock_irq(&cgroup_rstat_lock); - if (contended) { - trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, contended); + if (check_contention) + locked = __cgroup_rstat_trylock(cgrp, cpu_in_loop); + + if (!locked) spin_lock_irq(&cgroup_rstat_lock); - } - trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended); + + trace_cgroup_rstat_locked(cgrp, cpu_in_loop, !locked); } static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) @@ -299,6 +314,53 @@ static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) spin_unlock_irq(&cgroup_rstat_lock); } +#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) +{ + struct cgroup *cgrp_ongoing; + bool locked; + + /* Check if ongoing flusher is already taking care of this, if + * we are a descendant skip work, but wait for ongoing flusher + * to complete work. + */ +retry: + cgrp_ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (cgrp_ongoing && cgroup_is_descendant(cgrp, cgrp_ongoing)) { + wait_for_completion_interruptible_timeout( + &cgrp_ongoing->flush_done, MAX_WAIT); + /* TODO: Add tracepoint here */ + return false; + } + + locked = __cgroup_rstat_trylock(cgrp, -1); + if (!locked) { + /* Contended: Handle loosing race for ongoing flusher */ + if (!cgrp_ongoing && READ_ONCE(cgrp_rstat_ongoing_flusher)) + goto retry; + + __cgroup_rstat_lock(cgrp, -1, false); + } + /* Obtained lock, record this cgrp as the ongoing flusher */ + cgrp_ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (!cgrp_ongoing) { + reinit_completion(&cgrp->flush_done); + WRITE_ONCE(cgrp_rstat_ongoing_flusher, cgrp); + } + return true; /* locked */ +} + +static void cgroup_rstat_unlock_flusher(struct cgroup *cgrp) +{ + /* Detect if we are the ongoing flusher */ + if (cgrp == READ_ONCE(cgrp_rstat_ongoing_flusher)) { + WRITE_ONCE(cgrp_rstat_ongoing_flusher, NULL); + complete_all(&cgrp->flush_done); + } + __cgroup_rstat_unlock(cgrp, -1); +} + /* see cgroup_rstat_flush() */ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) __releases(&cgroup_rstat_lock) __acquires(&cgroup_rstat_lock) @@ -328,7 +390,7 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) __cgroup_rstat_unlock(cgrp, cpu); if (!cond_resched()) cpu_relax(); - __cgroup_rstat_lock(cgrp, cpu); + __cgroup_rstat_lock(cgrp, cpu, true); } } } @@ -350,9 +412,11 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) { might_sleep(); - __cgroup_rstat_lock(cgrp, -1); + if (!cgroup_rstat_trylock_flusher(cgrp)) + return; + cgroup_rstat_flush_locked(cgrp); - __cgroup_rstat_unlock(cgrp, -1); + cgroup_rstat_unlock_flusher(cgrp); } /** @@ -368,8 +432,11 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) __acquires(&cgroup_rstat_lock) { might_sleep(); - __cgroup_rstat_lock(cgrp, -1); - cgroup_rstat_flush_locked(cgrp); + + if (cgroup_rstat_trylock_flusher(cgrp)) + cgroup_rstat_flush_locked(cgrp); + else + __cgroup_rstat_lock(cgrp, -1, false); } /** @@ -379,7 +446,7 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) void cgroup_rstat_flush_release(struct cgroup *cgrp) __releases(&cgroup_rstat_lock) { - __cgroup_rstat_unlock(cgrp, -1); + cgroup_rstat_unlock_flusher(cgrp); } int cgroup_rstat_init(struct cgroup *cgrp) @@ -401,6 +468,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; }