From patchwork Fri Jul 19 13:06:40 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: 13737325 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 8EAD6C3DA5D for ; Fri, 19 Jul 2024 13:06:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E19C6B0088; Fri, 19 Jul 2024 09:06:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 192336B0089; Fri, 19 Jul 2024 09:06:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 032646B008C; Fri, 19 Jul 2024 09:06:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D9C366B0088 for ; Fri, 19 Jul 2024 09:06:49 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4BDBF1A068E for ; Fri, 19 Jul 2024 13:06:49 +0000 (UTC) X-FDA: 82356526938.20.9DDB887 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 842C780033 for ; Fri, 19 Jul 2024 13:06:47 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MQ0np2mw; spf=pass (imf30.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=1721394366; 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=DZ7SXWK1iEdG9+ToZC7vasIodyw+1eaFanvy9sk1xBs=; b=ZIuVNWu+4NzH9ASMJjjCF4MRq1sI/Q5gPCnz9TkMP0y7A9gakMwV9p43bYCPiyWPIGt8gY 0r9HxfMg7vOxwl0U4U4YTEA+D93G+/ybRAVujMCEPv2ROsNT5kXwxjfhjSM2kH2MWNqFdg vfPtadCoQSn5A/mxv7wpSmKE/SeQa0U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721394366; a=rsa-sha256; cv=none; b=jxIr6L7wsFwH9t98qTmetf3nrDzt7C4cHcqiOxT7VUSqiF2V+LXQB7snamvC+BOp1BTj8b yVY3EA1smiH9UhtLqpmwKMA0ncy07i5qCQdAy3bnZ+59/E0l1keimITLVyzw//gEF0UKZS +uhTyA0o19OYZHbEfLqHLDqCcWwas0M= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MQ0np2mw; spf=pass (imf30.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 3823B61C63; Fri, 19 Jul 2024 13:06:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69CA1C32782; Fri, 19 Jul 2024 13:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721394405; bh=w68mLylNhHyoPfXOqRfkFDQl+hrgCFvODmQQz+j9g6o=; h=Subject:From:To:Cc:Date:From; b=MQ0np2mwFUfXAsXQXXDysvbfJDePJh1kJjMUYbA4B1/v9x4R4D+TaMi2InZEQQB/x wuiZ2WwPlHbqwzdi5WsvsuGCN//SiLouyjIrZmmf3/C7GD4aXm0bn5pgnPRGOeSZ3U gnN86eXHo8iSPOk9S4JyOlsvlEk6l0deUK72VAfHKoDYOIU6W+jiOwOdXcYGVC4CrO PEVgh959xbZ9tGnUHS0kVq7CPqJHpFQwWnqAzP1e5/pqAXQHIy7ISP+Wu/DjUNCnzK PipSka+zQf8JV+GhB5QXbf3IgZe9H/R43lE2OxbG3JN66tarxU1gX7KmkqMMtQX4zG YykppGiug7MxA== Subject: [PATCH V8 1/2] cgroup/rstat: Avoid flushing if there is an ongoing overlapping flush 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, 19 Jul 2024 15:06:40 +0200 Message-ID: <172139415725.3084888.13770938453137383953.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 842C780033 X-Stat-Signature: x8aj5gc335xrqchu48js1b39ydia4kja X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1721394407-329515 X-HE-Meta: U2FsdGVkX1+FMmx/ToXLd5gbudHJq3OFbze6Rz13sn4LjgJtVUuggcZ+JZ7cvS38VPymGOlAjiu4F37kVzukNNf85PHSWUBuqOJ0W2/6fcfwGPlDGDjdMPQmFaGSkON066i8tMudWtlkF8h+EWmjnM/FYllxBy3oX0p99390EK/WzKrXISFi1jcZfVS+bV+HvWLYFcHFnZIHOfEruHoDblYFCu2zfZscuQjaFLiQcGa/vV996yG5sEUqkv+C5jRvq14s0EmzLOHF2f8+iGnCSewxJ9TSBHZzm+hIjmHBuDGrU9A6MHm960I6yCCmwBL9U0UnRpWJBpLBc5PWRXAbkrq5EhgtYtRPqEipO83FyxYH3TqC/YdY3Mni6PpFeVT+zOPHzo2IsZrf9PulYhTaW5kQQf0OLJZZxmLJquiDXh/9WXsjrU4S4aecEGST5eo4oVEr1keG8/51Wr21Pw9mMdQOnp8ku8d7oq5u9avhHuqDLNtzpYq/1dGIPQVXPY/IoGMK1GBc1nmEhs8JR0ylRwTstZ7sMul8+yR48c/vhyLmsHd/HSfK+LSxFEinKxVlCsmnhfHTtGSfVb+ZdoD4untJtitc6+V/jDnmPXVBIEwhjMJuKczKva5XAptQiaUYAg89XyO6gNIV3lsasVbdS2dGdpEkW/gFP4Un8E7drk2mjjuQo8MwK+QisD5/Y7NufHuEbgn/eb2r1MH1/Zxf8KsaJMUUgKFpMNc9hBmuNN5OARHsaZaID8v5mz/OHNmkWJO2F5roBRUz/mQn9onprGb6N9NJpM10kJ+GhbcKT8MtMXmKGYV1D7yPOZgUcJTiF/bQmWhsKcIdR5CdhLU9BUgcLL2ZgmG4tRS/MJVCI4V+kahb0tN8q5xf//KpjV//E5jjCv6qNbteGFlbXx4m8gzxQtO/GsA6sEd+EUMrwxJcgKRdFhV2ogrXI/lFjl/LoPvdtYW0DGKZOm/dNZC j9bx1Cm1 YaoaNeYZchxZ1UDcmoLRnJONrKkyl2kayIqgrbDreFF/6nSI4kZMHoC+LCE/OeulrKCO3FgWgqFsjPxMAYj9ooHQ5TVbO1qFiwi+0MbGJuxReMzLtFOnGHikRgjUHMolcDd3I/8dW0PbUffPDLMqdcc46NoWc1PS6N491Lc5g3NmGvAWVz0wmLedhzFwA4om+zWQnIB8X31P6vWiJAHuu8tSGw+aC3+sLPo8b7j66oazM0lYlHr0fl5YAJg== 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: This patch reintroduces and generalizes the "stats_flush_ongoing" concept to avoid redundant flushes if there is an ongoing flush, addressing lock contention issues on the global cgroup rstat lock. At Cloudflare, we observed significant performance degradation due to lock contention on the rstat lock, primarily caused by kswapd. The specific mem_cgroup_flush_stats() call inlined in shrink_node, which takes the rstat lock, is particularly problematic. On our 12 NUMA node machines, each with a kswapd kthread per NUMA node, we noted severe lock contention on the rstat lock, causing 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. Here's a brief overview of the issue: - __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. - balance_pgdat() has a NULL value in target_mem_cgroup, causing mem_cgroup_flush_stats() to flush with root_mem_cgroup. The kernel previously addressed this with a "stats_flush_ongoing" concept, which was removed in commit 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). This patch reintroduces and generalizes the concept to apply to all users of cgroup rstat, not just memcg. If there is an ongoing rstat flush and the current cgroup is a descendant, a new flush is unnecessary. To ensure callers still receive updated stats, they wait for the ongoing flush to complete before returning, but with a timeout, as stats may already be inaccurate due to continuous updates. Lock yielding causes complications for ongoing flushers. Therefore, we limit which cgroup can become ongoing flusher to top-level, as lock yielding allows others to obtain the lock without being the ongoing flusher, leading to a situation where a cgroup that isn't a descendant obtains the lock via yielding. Thus, we prefer an ongoing flusher with many descendants. If and when the lock yielding is removed, such as when changing this to a mutex, we can simplify this code. This change significantly reduces lock contention, especially in environments with multiple NUMA nodes, thereby improving overall system performance. Fixes: 7d7ef0a4686a ("mm: memcg: restore subtree stats flushing"). Signed-off-by: Jesper Dangaard Brouer --- V8: - Updated subject+desc based on Yosry's feedback - Explain lock yielding challenges in comments - Limit ongoing flushers to cgrp level 0 and 1 V7: https://lore.kernel.org/all/172070450139.2992819.13210624094367257881.stgit@firesoul V6: https://lore.kernel.org/all/172052399087.2357901.4955042377343593447.stgit@firesoul/ 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 | 114 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 12 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..eaa138f2da2f 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,32 @@ __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, true); + else + trace_cgroup_rstat_locked(cgrp, cpu_in_loop, false); + + return locked; +} + +static inline void __cgroup_rstat_lock(struct cgroup *cgrp, int cpu_in_loop, + bool already_contended) __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 (already_contended) /* Skip trylock if already contended */ + locked = __cgroup_rstat_trylock(cgrp, cpu_in_loop); + + if (!locked) { spin_lock_irq(&cgroup_rstat_lock); + trace_cgroup_rstat_locked(cgrp, cpu_in_loop, true); } - trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended); } static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) @@ -299,6 +316,72 @@ 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) +/** + * cgroup_rstat_trylock_flusher - Trylock that checks for on ongoing flusher + * @cgrp: target cgroup + * + * Function return value follow trylock semantics. Returning true when lock is + * obtained. Returning false when not locked and it detected flushing can be + * skipped as another ongoing flusher took care of the flush. + */ +static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) +{ + struct cgroup *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: + ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (ongoing && cgroup_is_descendant(cgrp, ongoing)) { + wait_for_completion_interruptible_timeout( + &ongoing->flush_done, MAX_WAIT); + /* TODO: Add tracepoint here */ + return false; + } + + locked = __cgroup_rstat_trylock(cgrp, -1); + if (!locked) { + /* Contended: Handle losing race for ongoing flusher */ + if (!ongoing && READ_ONCE(cgrp_rstat_ongoing_flusher)) + goto retry; + + __cgroup_rstat_lock(cgrp, -1, true); + } + /* + * Obtained lock, record this cgrp as the ongoing flusher. + * Due to lock yielding, we might obtain lock while another + * ongoing flusher (that isn't a parent) owns ongoing_flusher. + */ + ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (!ongoing) { + /* + * Limit to top-level as lock yielding allows others to obtain + * lock without being ongoing_flusher. Leading to cgroup that + * isn't descendant to obtain lock via yielding. So, prefer + * ongoing_flusher with many descendants. + */ + if (cgrp->level < 2) { + reinit_completion(&cgrp->flush_done); + WRITE_ONCE(cgrp_rstat_ongoing_flusher, cgrp); + } + } + return true; +} + +static void cgroup_rstat_unlock_flusher(struct cgroup *cgrp) +{ + 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 +411,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, false); } } } @@ -350,9 +433,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 +453,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, true); } /** @@ -379,7 +467,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 +489,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; } From patchwork Fri Jul 19 13:07:47 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: 13737326 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 B9594C3DA5D for ; Fri, 19 Jul 2024 13:07:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51AE66B008C; Fri, 19 Jul 2024 09:07:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C6886B0092; Fri, 19 Jul 2024 09:07:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38E2D6B0093; Fri, 19 Jul 2024 09:07:56 -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 190026B008C for ; Fri, 19 Jul 2024 09:07:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 8673E1C140C for ; Fri, 19 Jul 2024 13:07:55 +0000 (UTC) X-FDA: 82356529710.24.74A2EB9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id CAB4F20029 for ; Fri, 19 Jul 2024 13:07:53 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SxPWbaF1; spf=pass (imf03.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=1721394432; 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=DSnbZ3VpiEe0mvciQe4pVg3jZQWxApGTmQtkUiFIOKg=; b=ZTEiOryeJnR1F6m7OoUvOeRNcVzkaBz2PhPY8o6+txdXIodEi+I//k1fu0K57S//9ib/NU km+RrZsQIfcbPpCGefLSU4+8wcEid2qLjMe4XoogKoKMIF2IIgMwIa9ZxWYCa5rUtu6TPk 31zEqSDqqCQ0Xmy0DqTuQP6BdEDa1C4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721394432; a=rsa-sha256; cv=none; b=hgPDZr9IRJnv0IbwN+KDBIY2XXGdee32P6zcImuk34qiNBOjHmM2+1N0Nb66yDp3IXa0ID OpSvDId7+yxkpEqcUgXgE0n2SDNGKItIui9Jvj93dpASU3XP3zh/SqUSDk5npPXHZl32Zi l9Q/5MRQQMIjNgZYWFCHHz7XdeFuss4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SxPWbaF1; spf=pass (imf03.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 CD4C560AF9; Fri, 19 Jul 2024 13:07:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 192C4C32782; Fri, 19 Jul 2024 13:07:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721394472; bh=7zTaxivH6TZiJpCmsPOaYPHmaRAX9f1Y7IJuuZGbuPM=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=SxPWbaF1lpLtzt7stK2H9/YKhzURL4bsn/4B95hTLCDfIqZUOAzFF/I/f3pcg5dTl fqeRJQU74sYzfkaNxZl9T0eK0u4+841IfnsTiIrbvXFvuCPXDLv2WCwbaiZCY4gfNf NEne1nH3dn8AOGL8J82c9qjVv7rUygZp5ZzWw3Yw87giT0gJ7fog2sUQQgB5lF6qzY IKmiGhQiFzwLAq5XxcVxMFlNlc7y/n6w2/FDoYzk2FmBTY9wP+elnOYu/z7mhcGCc4 cr/emNtktmAENh5SKEA5xBkf/ASaq10pq8y8u6yp0vh4H3VRmnfrhVW/84QuloUBjZ rItxlvav9kqIA== Subject: [PATCH V8 2/2] cgroup/rstat: add tracepoints for ongoing flusher waits 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, 19 Jul 2024 15:07:47 +0200 Message-ID: <172139440730.3084888.16497707303868810863.stgit@firesoul> In-Reply-To: <172139415725.3084888.13770938453137383953.stgit@firesoul> References: <172139415725.3084888.13770938453137383953.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: CAB4F20029 X-Stat-Signature: jwmar74nxqk5gmnf9nah176geoyccw6w X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1721394473-376926 X-HE-Meta: U2FsdGVkX1+UcYF9AwK7MPYi5U5qhqljcN55CXJP8eGZs0lX8sOd27YMdYWcLMnGI8rVWjsUSWWvvbhSnA54jOTjgaPRF416EN40eBU6RzvDwGumxx9whN4Xhy7MrxNEapH7KAsAnEoyX07P7+S8tlbbQLTZjVJQFGAA7NliKHhXWLlJevvGeqBKjPCo6Z+nUlj3cUqESQ8yJUnuTzxT7Zm9EUnnpil3Wf58ZJ53GVCJVJ6gClJQdpnE2iJhaCK77qaDCQ8Rhqfgk0pgf1vLpfVRhT1+kYLRHbpy3nw+JjbJah0nI3Ia0eXZaYXPRe7d1kpEoaOFylMINsCcys4z/zzZ5i7mFNspHAZjv70L9UMcjqs9ry90bsptCGMAw6VUKpCLLfdbeMKAubltukRWv8NUloKgo5G/gZCP6DECGJFuDHuoG0miFSn527hHeRpPwoNvR7rDGenoN9yiJ23E/lGBUxLG5S4WZNLcDfwqfhjAIkTZtDlFNNZYDgX4TTLr108Rh9grRGTHY8QxQy72iTZfQzvG3TOiaA1Km5gUD3UigXlOTCoYrhxPsAZVDyr7nidKWeTSWzXUX8c781JkwwKhpunRq+ajK9obSZo50Mams6ub9mDhDMvQvB/7pDwfSnP5sXa4DKaw1XUyh0Af2aLsT9nUmCTgetCYdlzyJ13nvuM58slZfQ08/nqrsT3RpbgYsKECfPSymulIvWhjU4XvVh1CbmqlMOG5fx9FVeU1IsLnAkV8MGVqX8BXGvKq+CMgWuZmSeQ5nRQn0/KrTEuBrZb1jrVMQNpTdNCtt9du1e25KJE1IMZ3yfwR2ZeCo2wfeyRiGxf4vMRkJU32gEolwGdkLoZ0x4G1HIJqRe18zYToo2h4XXz4ytI5nvFGGKKjYxe3eAjBz/yZ5rm0OMemq1gd/tolZnZ6ulpC49nWRFta0dXnV5vcA4plA4pJ2aiZQkSrBKdkxDs8Ih5 6mB8L/F0 FHf6VJRoZLpT/Bc6ObPsj33eBmXHlm8Q4iW+G1e6KnBHJVo7xP2ePxSD4GNWWWNrCTpeUWQy8pOy+Td2QnHeXAskSI/cjusXZ66PaLZcyVfPJ6ZibQnVmoZnHRGCGmVE2ICF3NTbUbr0PxkmPrhNWh3YnXWl7YwZhXZxOgsVCPAXjuTKkIOP9BET3zt3p93JP/Z9xgvfUJXM/20Vdn5O0cNWcRz9YgjOXvvx2OulwI4JyNypz6BXYoVb0OQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000053, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: These tracepoints were practical to measure ongoing flusher wait time behavior and see that race do occur in production. Signed-off-by: Jesper Dangaard Brouer --- V8: Add TP for detecting ongoing_flusher yielding lock include/trace/events/cgroup.h | 56 +++++++++++++++++++++++++++++++++++++++++ kernel/cgroup/rstat.c | 18 +++++++++++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/include/trace/events/cgroup.h b/include/trace/events/cgroup.h index af2755bda6eb..81f57fa751c4 100644 --- a/include/trace/events/cgroup.h +++ b/include/trace/events/cgroup.h @@ -296,6 +296,62 @@ DEFINE_EVENT(cgroup_rstat, cgroup_rstat_cpu_unlock_fastpath, TP_ARGS(cgrp, cpu, contended) ); +DECLARE_EVENT_CLASS(cgroup_ongoing, + + TP_PROTO(struct cgroup *cgrp, struct cgroup *cgrp_ongoing, \ + long res, unsigned int race, ktime_t ts), + + TP_ARGS(cgrp, cgrp_ongoing, res, race, ts), + + TP_STRUCT__entry( + __field( int, root ) + __field( int, level ) + __field( u64, id ) + __field( u64, id_ongoing ) + __field( ktime_t, ts ) + __field( long, res ) + __field( u64, race ) + ), + + TP_fast_assign( + __entry->root = cgrp->root->hierarchy_id; + __entry->id = cgroup_id(cgrp); + __entry->level = cgrp->level; + __entry->id_ongoing = cgroup_id(cgrp_ongoing); + __entry->res = res; + __entry->race = race; + __entry->ts = ts; + ), + + TP_printk("root=%d id=%llu level=%d ongoing_flusher=%llu res=%ld race=%llu ts=%lld", + __entry->root, __entry->id, __entry->level, + __entry->id_ongoing, __entry->res, __entry->race, __entry->ts) +); + +DEFINE_EVENT(cgroup_ongoing, cgroup_ongoing_flusher, + + TP_PROTO(struct cgroup *cgrp, struct cgroup *cgrp_ongoing, \ + long res, unsigned int race, ktime_t ts), + + TP_ARGS(cgrp, cgrp_ongoing, res, race, ts) +); + +DEFINE_EVENT(cgroup_ongoing, cgroup_ongoing_flusher_wait, + + TP_PROTO(struct cgroup *cgrp, struct cgroup *cgrp_ongoing, \ + long res, unsigned int race, ktime_t ts), + + TP_ARGS(cgrp, cgrp_ongoing, res, race, ts) +); + +DEFINE_EVENT(cgroup_ongoing, cgroup_ongoing_flusher_yield, + + TP_PROTO(struct cgroup *cgrp, struct cgroup *cgrp_ongoing, \ + long res, unsigned int race, ktime_t ts), + + TP_ARGS(cgrp, cgrp_ongoing, res, race, ts) +); + #endif /* _TRACE_CGROUP_H */ /* This part must be outside protection */ diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index eaa138f2da2f..cf344c0e71b3 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -328,6 +328,7 @@ static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) { struct cgroup *ongoing; + unsigned int race = 0; bool locked; /* @@ -338,17 +339,25 @@ static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) retry: ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); if (ongoing && cgroup_is_descendant(cgrp, ongoing)) { - wait_for_completion_interruptible_timeout( + ktime_t ts = ktime_get_mono_fast_ns(); + long res = 0; + + trace_cgroup_ongoing_flusher(cgrp, ongoing, 0, race, ts); + + res = wait_for_completion_interruptible_timeout( &ongoing->flush_done, MAX_WAIT); - /* TODO: Add tracepoint here */ + trace_cgroup_ongoing_flusher_wait(cgrp, ongoing, res, race, ts); + return false; } locked = __cgroup_rstat_trylock(cgrp, -1); if (!locked) { /* Contended: Handle losing race for ongoing flusher */ - if (!ongoing && READ_ONCE(cgrp_rstat_ongoing_flusher)) + if (!ongoing && READ_ONCE(cgrp_rstat_ongoing_flusher)) { + race++; goto retry; + } __cgroup_rstat_lock(cgrp, -1, true); } @@ -369,6 +378,9 @@ static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) reinit_completion(&cgrp->flush_done); WRITE_ONCE(cgrp_rstat_ongoing_flusher, cgrp); } + } else { + /* Detect multiple flushers as ongoing yielded lock */ + trace_cgroup_ongoing_flusher_yield(cgrp, ongoing, 0, 0, 0); } return true; }