From patchwork Fri Jun 28 10:12:57 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: 13715929 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 3D055C2BBCA for ; Fri, 28 Jun 2024 10:13:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A47376B009E; Fri, 28 Jun 2024 06:13:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F71D6B00A2; Fri, 28 Jun 2024 06:13:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8BDCF6B00A3; Fri, 28 Jun 2024 06:13:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6EEAC6B009E for ; Fri, 28 Jun 2024 06:13:06 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA3791A1AC0 for ; Fri, 28 Jun 2024 10:13:05 +0000 (UTC) X-FDA: 82279884330.21.7BC30A0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 1E41F4000B for ; Fri, 28 Jun 2024 10:13:03 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QfhbMerd; spf=pass (imf12.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 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=1719569566; 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=8S/FZsnA73GHWn4murhxlaOy9C6JFFIFA3Cx4vjFYgI=; b=4prtATJviXQTGux4QXs/1Dallms1/ZuW2NwRAdxR+2lkJbWkGo5uisxr0AhucM9IPEcCpC RdnDqStUi4UG1jwtFICud5h+gg+IgVKfnCw+rQwE1cAmUz3gLruVcScbWZ1Ab2kTm7G1WR RgLcjqDUAe6sFqeag1idzweiGv/ndRY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719569566; a=rsa-sha256; cv=none; b=sCIzFJ6JPkDsP1qZrG42+4zwKNw61x1O7E6S7yRW8sBIHT/rFxf9hidYNjQbHUO0fArAz9 HG9x8TciNEB/KjIIdRA36F3NZPRtAoiGYFfmH14b4fkkLYtzFnUnNq1hkxBSKUoomGJRUN CP8vJsxwg3a+LnRmPDZlOtswFOvwDRU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QfhbMerd; spf=pass (imf12.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1416D620D9; Fri, 28 Jun 2024 10:13:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CA0CC116B1; Fri, 28 Jun 2024 10:12:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719569582; bh=aEN4UjGbMyC6DjslP84ymi+R6PLjp4YmG/9QHdQTNu8=; h=Subject:From:To:Cc:Date:From; b=QfhbMerdzgimj3QDKCj3H0751pP7YHmcbybQrl38GUsCeLnFopVKIdskKdYaA82TC dF/zsfqhuaJDOtfP1yuDzIHbgVOi7KTRNn5vHqisQqXQg0KXKrQvzsaMEnrQHnXR12 asEyBtuCiFlLF2Kj0D8NxLdT/tPlnbSW8iDCMDANKfKK6x1lW9/u8Z8VGKkWZ/zXvx SLLdlmq8A1/R6HNwD//C88045UEPPqeDO/3iM3X7QlHQ0RYjyBQBZMmp1xlGPiifAV zjvkRAE8sX66vQB+ebWkAxhbgz0Oijhzn5KIyynMmKKdGPDMemIDg/rvuMi/vE50aA GpZaS6IsGn1+A== Subject: [PATCH V5] 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: Fri, 28 Jun 2024 12:12:57 +0200 Message-ID: <171956951930.1897969.8709279863947931285.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1E41F4000B X-Stat-Signature: 91k7ntz8z4mds4uebojdqhz8cjbt7e9n X-HE-Tag: 1719569583-348933 X-HE-Meta: U2FsdGVkX1+r5P3iFFTFNaP8uuQt7GrWvCzzpiS+EV8lEYPSr3Bj2mwfTtYzhPUhdT/Zb2rFQeHzaQ5CN3ZvInO04GaR2Ym5iOcf8V4fIfiiqTAGz5dud/xMNyjJY8aA/WTNIDGJk4akhArR0jY17tAonALDu0OH1/bzzkTg5X0Um1q4BBBVabyM45GXsUbRMHfoi5E580KOaj+hSt7fQMMrmj2vc4NVHyZAA13GXWQfZu5QNNiIztLV5Ij0UDrrkBl9g7dFnMmCtZnPrh+2Eo4/nn3xTCtuAo3YORJH8SWmBNre+16c+isRgv1hG8FqmqEmzyfPLSF+/oQ4nSLpflyUhP/w3NW5bZwU/UonsB64b4EjomxjzxN0mfB3du0480nQzMWI4fXwnmlwPKblrgVAfTUV6G6qrbDX3r7uzuzmkmVcZDrggR3M6qmdls13ZAE3mkHIPjqFCQZnRW+e1yjqho1w1ZaOskqFlYVZkp9gXmjpOLs6ySrAkIoDDyYy0964vMKB6wvn1Q7BbnnZ5FHHGyq7naytfgHc3P8UmrCd/1oNagaOwPoYNhpHa+AA0IKqe431JGQ7tPEarKWkHDvYTndm5MPWGbSr5r3DCB29qwfo1y47BfCYi3IUfK/cv/8ewKfLgGmAy6ab5VrKNBgesT4EqlA6hd2SXaIklUtholOPcnQBtwf+djhx+TUFoe1SLj+eIgxSjWYO9fVEofakUKlxcUnbB1FSgcsGi0QwPa7wNbBUZsxuF6RM7b2Nfy31tWO+G7D0vSW6+iqaQMZvpekMgAiU1B+e1qY0fKbNhIdmlqtLTdfJcn3QC5NMawjSzyhGUSY167MEP9Fc9PF/gweSDfFW9nGjGOkUmJOCu1pLECiO/CFPb91VH4jo+F+mSbo39Bn1RoM8p83aPJwloapPisQid3OnDeuXiNd+r97AB3A4wJj/Q+rNPSqAHu45XkVGSQYc7gy9nG1 Ky/Qs88m ilfjIA4LkhltygYP6S6cQzx0kFT2+Q0xRaW0Zp6M+B7u/vRYu/KzOkXWb1cITGjdj1yhdkiBeQW9ouIdvW84JpAbyCqrLGT0qwcsLcAOVEMXCJn0x4o4qnEDmRsQ0mw7GvCV++LPPBQ9WQHyIgEg7VGPLaCyr+XUMx7vKQLPY9UK7eloH+/7IDCfkFIb5ENHMJWfIqidLvjShPYpkLDCRf8OnLRres6l8VPsmixToiHDcYhpJcj5tyJi+I6IO0ZOkHtMvWBVKqlZ5+CwZLLVFaE+35Y2Aix6h8pcj 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: Dropped trylock helper 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 | 57 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 5 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..e9d3e2aff698 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); @@ -299,6 +301,44 @@ 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; + + /* 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. + */ + 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; + } + + __cgroup_rstat_lock(cgrp, -1); + /* Obtained lock, record this cgrp as the ongoing flusher */ + if (!READ_ONCE(cgrp_rstat_ongoing_flusher)) { + 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) @@ -350,9 +390,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 +410,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); } /** @@ -379,7 +424,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 +446,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; }