From patchwork Mon Jun 24 11:55:32 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: 13709387 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 851B9C2BD09 for ; Mon, 24 Jun 2024 11:55:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D16826B00BA; Mon, 24 Jun 2024 07:55:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC5896B00BD; Mon, 24 Jun 2024 07:55:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8DCD6B00C0; Mon, 24 Jun 2024 07:55:44 -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 955DD6B00BA for ; Mon, 24 Jun 2024 07:55:44 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0A54280EA5 for ; Mon, 24 Jun 2024 11:55:44 +0000 (UTC) X-FDA: 82265627808.29.F16D8E6 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf20.hostedemail.com (Postfix) with ESMTP id 9FB2B1C0008 for ; Mon, 24 Jun 2024 11:55:41 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YNAnJsSh; spf=pass (imf20.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=1719230129; 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=dUJ86ENtDzyXljtOAtEED3UCddwu/4dHNlbFxXoPiAA=; b=aDcqakEDzEEd99ObeodYAe1AEQGBhzN5rXC7sHTOOV9yqyc8pZF757+itE0gboH/3hNZ9i ksz01DkSUPpguv1K7vKkbCxKy9LVyXHlHqQbp4jyXXvos2X2VNsX4uGcGOagJDt2+FMBu3 AwC9LsAg/98ascZvFNUPlMbN6gfQifU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719230129; a=rsa-sha256; cv=none; b=wT9wQdG3YmI3qtFkgm0OyZ6MWAhLhDxm4TJTO5usbws9ygQcXSRaAb9vwXAMb+aGhJnrTo N1mLsRv+hs1rjJ4BH5lzQ/hqwW2e3vj7yrd6OXigj/aG4QC0h0GaeP5a3QMJo3Xeq06TeO tNqqnD4AdwWSe+eG8yJ852TlmkoHQiw= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YNAnJsSh; spf=pass (imf20.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 6DC8BCE1103; Mon, 24 Jun 2024 11:55:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 337C8C2BBFC; Mon, 24 Jun 2024 11:55:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719230137; bh=VIXNictcRYphYWAXSJWwF0b89V514+SWvTQS5dHJjfU=; h=Subject:From:To:Cc:Date:From; b=YNAnJsShLYovUOxxo9TvhReIZ64ptq5VYWGZncUaUQiFCGiYc0aDPGDMKZ8sZSskU B0WDDpKePg628LWl9yUW8+tpiIeWeXq5nnA40/QNjwd7xcjKtdGT2cdzdjSO1HOqCU 0ybDO1XU41O8mgWmnBJvXxrWGGRR1slfZaRWbmMcwH1y+Jgrs+m1Pm3gHZVrUH6oHo AtjSmzSSuFJexRBpEMMJuYUSr8cZHUZMcSokwnk6HvDwm89qSX+WmiEqxGNelE8TjU z88tmNYTBayn4Rk8bWFp6aEtwpc+kSfSIEo0AVgUztvwQcyks3WH0/EejtNjaSveEu zmBDXa7wVyOCw== Subject: [PATCH V2] 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: Mon, 24 Jun 2024 13:55:32 +0200 Message-ID: <171923011608.1500238.3591002573732683639.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9FB2B1C0008 X-Stat-Signature: hnjd8tma4omucz31ihsjyb3smphr1j6a X-HE-Tag: 1719230141-173584 X-HE-Meta: U2FsdGVkX1/2vKajlnMAHW4Zz0pLFtxKfPQeJLK2oEZCDPFqaax4dwnRJcC2sQPaX9X5Yv0T+s5jw9HFe1Nu7Z06+vb4tEWy+QuDLYV9eo8ZFkfDRePf6Adaa/ry7EiTfeGy1KE7y8LTT2IrGwb69ZWRg9swG+vBqTBNYW2DkQbI0b075Oi04x6NOTEF5oPPGJZDjQHVeIBF85IGx7o4JGTJNXv91wteqixUJsv5lw0HJ4BEK2lPSmqMUXxpGR77yuTQYz47QxPSf3h9pWODWinbxHwlepPUtNYJ8pmbLzeWZ8Rh5GvRbfvk8VMB6sIqoje2lHH1ihMILodo3SwUXd6uwVlPR0tPG0W+qMYnyNzq/KAespjHAwkGeL+OSWl7CUNrHKrUTOY3xjiEce/KI2wHfaTLK3uPpruTDr0eq9qvjTV2got8jFFMKatcryKZJ0sZjNLgq7OaIWwC3IonQO86uwMevEX0//ZP1eo1o17Vw9x1bjb3wJf0M19DX5Dwg62bdwUV2E4lLtA9ZFv7bz1GybzhrhOIQYjDmvv00XmZ4qCZQJMdPPEH95kH6RdiYIkVyjKHPtYz9v/PpJHApwMeo4tnpiYU5XYLV5W+lQKVW+F9LMPXpqZVdQW/l1GZier4b9Td+OCiJoRpvymXHz3KoZWkxRG7wp2s0DOXz3ubDbSHoPpmb0AGVMtnan4yDcMwbvMRATKEZTqOS4320otOit6h6dnMRQMer+2G+uDGfLWo390GfcMuXC0L8W8ZO4R8E5xuNrQwp2zp4vSLCbt2dpYie1ZnPOJ8suzP+7BfPSXzdfNYVX3H6VHNPQNxPvD331SYW7TrS26j5huIGrkYfCDYOaq+Vm8cmVg+GtaWVfTxcfBoFOfjYib5H+ncsXneWFX5iRzoP8eNSzPZVGF8W11AFkYoqxDoni3o/Pui2BSMRIjd+tfXVx7BRSrxu97zUDRxzNz70cDd4xO 8QKL7sZG LKzMyBtIxRsJ03i3hmoYJWE3GLSSAegzytTHQekhCjpWjRe7ufqj2tWBv7u2mpWh0FOD56ugmwQEGcGRQ491e6/f8vTJxkIAXKEsV/8ovzoLq3QIgOqzaMYEONeNXzO63QmkcY+esyuTjCCgfdC0xsdc9CKuaIBtOLKTkj1De275PdjqtWHNfI2PYPpTSh9r9OydKIF1BvQQ8VA7HuXftrQOE5ZKEK5IdK1+5yhqBv51JqfXv1bceEh66MQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, 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. 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 resolve the kswapd issue, we generalized the "stats_flush_ongoing" concept to apply to all users of cgroup rstat, not just memcg. This concept was originally reverted in commit 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). If there is an ongoing rstat flush, limited to the root cgroup, the flush is skipped. This is effective as kswapd operates on the root tree, sufficiently mitigating the thundering herd problem. This lowers contention on the global rstat lock, although limited to the root cgroup. Flushing cgroup subtree's can still lead to lock contention. Fixes: 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). Signed-off-by: Jesper Dangaard Brouer --- 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.h | 5 +++++ kernel/cgroup/rstat.c | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 2150ca60394b..ad41cca5c3b6 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -499,6 +499,11 @@ static inline struct cgroup *cgroup_parent(struct cgroup *cgrp) return NULL; } +static inline bool cgroup_is_root(struct cgroup *cgrp) +{ + return cgroup_parent(cgrp) == NULL; +} + /** * cgroup_is_descendant - test ancestry * @cgrp: the cgroup to be tested diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index fb8b49437573..2591840b6dc1 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -11,6 +11,7 @@ static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); +static atomic_t root_rstat_flush_ongoing = ATOMIC_INIT(0); static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu); @@ -350,8 +351,25 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) { might_sleep(); + /* + * This avoids thundering herd problem on global rstat lock. When an + * ongoing flush of the entire tree is in progress, then skip flush. + */ + if (atomic_read(&root_rstat_flush_ongoing)) + return; + + /* Grab right to be ongoing flusher, return if loosing race */ + if (cgroup_is_root(cgrp) && + atomic_xchg(&root_rstat_flush_ongoing, 1)) + return; + __cgroup_rstat_lock(cgrp, -1); + cgroup_rstat_flush_locked(cgrp); + + if (cgroup_is_root(cgrp)) + atomic_set(&root_rstat_flush_ongoing, 0); + __cgroup_rstat_unlock(cgrp, -1); } @@ -362,13 +380,20 @@ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) * Flush stats in @cgrp's subtree and prevent further flushes. Must be * paired with cgroup_rstat_flush_release(). * + * Current invariant, not called with root cgrp. + * * This function may block. */ void cgroup_rstat_flush_hold(struct cgroup *cgrp) __acquires(&cgroup_rstat_lock) { might_sleep(); + __cgroup_rstat_lock(cgrp, -1); + + if (atomic_read(&root_rstat_flush_ongoing)) + return; + cgroup_rstat_flush_locked(cgrp); }