From patchwork Wed Jul 31 19:47:09 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: 13749139 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 06877C3DA64 for ; Wed, 31 Jul 2024 19:47:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 356436B007B; Wed, 31 Jul 2024 15:47:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 306916B0083; Wed, 31 Jul 2024 15:47:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CDE96B0085; Wed, 31 Jul 2024 15:47:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F3DF76B007B for ; Wed, 31 Jul 2024 15:47:21 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 45E0D1607B3 for ; Wed, 31 Jul 2024 19:47:21 +0000 (UTC) X-FDA: 82401081882.23.15941A7 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf17.hostedemail.com (Postfix) with ESMTP id 072114001B for ; Wed, 31 Jul 2024 19:47:18 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=APTeW19P; spf=pass (imf17.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=1722455184; 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=m3YeLEJy4Q2A9NTQRuJ9MYOr1kTkenW+D2AD9MESqf4=; b=8AmpWY1eqqDWZCwSKowI9YP5b6/aY5G/PWjbADpqcpUfriEOWsxDGdh1+zUOMRFsfTCdwG 8ktQHQ1Y7wiR9kOOAl2pg6wHue8Qo0xlhZZQ17cymK5NiqXCkaqTkiAZ2M18Nn2xBAenG9 h8I215KenzfkZk4qklL/mU8qZmOtOPI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722455184; a=rsa-sha256; cv=none; b=GjhK1NMgRnqLRDeq2rqHPOXe3XwazvpRF2zgFHOWiLxp4qelAz975IMWvJkur7aghW51qP lBGk0qTiDG+aOXAgizuOclNFqBB6/wiw+SXYHXBfnclE1o3OdKpCcX6IngCzhVHoO/x5Mq 6BsoMydkKQhG5iG8L7rEytjtALPR4eM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=APTeW19P; spf=pass (imf17.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 3F70DCE180D; Wed, 31 Jul 2024 19:47:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15C0AC4AF0F; Wed, 31 Jul 2024 19:47:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722455234; bh=nvNratIWItKZQVYz3quVJHi2Shdz8vQJ6LbiC1lFAVk=; h=Subject:From:To:Cc:Date:From; b=APTeW19PzPCi5eC6RjUHc7sgg8yj6sOUBYMNWig/HpsXD4Elo2dXQu4UE/GRlXWNy EQPkPw46Kg5yp9BH3CU2AjNNhPdiR9TvqjmBHvUQ9V3wi2ELi0lZSB7pJaDeEZ3kEP mLPeLhDtuuv75ElAqL2vWDHQT6SY7Bg7PDHI7Pymgvkvv9o8aKpXinXzXi/8CbDrU8 9cZsT9k5tcXmZTOyYPvremMOVmXm8+iEIpU/WvOzEV9oyaEUkUN0DJbgeiMDzX0EKW 2B5q4MiWfQbm5c20dNlnBp1p4M3gtlANrKTl/gjMb7nMO1vwfic2kzz3xx0p7LSTRO O7C2rNK7RGMnQ== Subject: [PATCH V9 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: Wed, 31 Jul 2024 21:47:09 +0200 Message-ID: <172245504313.3147408.12138439169548255896.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 072114001B X-Stat-Signature: zk6xs9s4pinycsphauhkkr154hkh7uue X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1722455238-202036 X-HE-Meta: U2FsdGVkX18u6P928MOpCMZtrTXjhaomcbwakSQIUzTzkmUbeVs1ycBna2Zf6fKc8XL9vPBRdsT1GyFg3QZ7HhsM4S9vYPQi0LBS6P5lnirRBqCaCpsvdd2zj0uMj9gXftBW2c/y4hRok/5nBWYAdLd3IVLZHBTZo7RBv5g9uMaQYJFMmDNB6SC6RviBswCQsxJMiMK3r1xgcdOPysSTxVq79wsQdeRWVD50N4LAjSjyyH5BxwoL3rN16gAbeDzQJTVHkz/AKFu8sBat1w3FejnlMG/Csu0oDhmRFLeXdDb0daEwBfk8+Mnd9zoKTF5ns1GIirYBEeNhYC8vgP8U2rEwb1ld3Ot9dAUMTJWHQOfz05CPQTHpnC0dTzrF11DbnLmEHyf5H+mBnlAbHygkap1w/Bf2iogcjMIx8WE6tbhyPUPuV+1TbUEsaIopzgJ6P3nJ/EHzD9GZN+jaUO89JXdwVMOQWAu8oZRpjl3H4YSbuRGxmMcVXGLu+A3J2x6zVz32HzXSvGxo5qGjbfULpa6BGAB2GcTHHI+1TkOzMUjlp8rQ1xoVVFdDr/NtN0GqV8NAYDxpiPd+Dt+ogrrmvytYkz2vTiwfBNZzZ3FQ/uksRhIDomwFwm4dwlMorplAEehNhyND8TZRU34UIwSZp5IkKq9VOUvT5AwZXfRDmgQTqqJjQRBgnLDWuRHiAV6obrYLuR+pp9bYNsd/+7ey5EeA8qx/W8eqLDghWFp205bKr2DRALqihYS41kX3MIGTb7QE+i1rqRdoxfbkDNI/FpPHMI+1Snj2SAhW5NOFJNFaDwF3BuPjoo4Q9+F0IDv1PthzfF59J5J7cYBa2WRlymAVp4V/eUoZbANLHMkeK+siI2mcqgs7nPg1iJXfilQVgYmOdZxhBaTyQMGtXR36PX4UiRzE4ix3XKWyXfqEmmJNVs5aJCeLMk/arJ/K75C0xiJKWiWok+xbSB+maG8 dSMXIjXS LIvtsNgXKChitylFAuHxMUQPBiFAxCWn8yjY799N9q8MBeDtvFa4SCovKZP6MZ7PJPuXSk532qLGdMsnzuzZ+fljXTQi5Zmi/Nw7wNwl7d50QWDpYfOzL2BSsvF/xhfdXozysHAScxzyM1IAF/kQ7MMwQemTW/guRQJAHoqtbehOiPzHjkJDtevXz6Y3gpHfbw4NnzMHMJz/XC+hUO87xxnl16KR8RhlmIZBIRbyMkWNywLBYh3zF59TZLg== 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 --- V9: - Fix logic for 'already_contended' - Didn't address the refcnt suggestion, need more input from experts - I'll deploy this ASAP to my production experiment hosts and report back V8: https://lore.kernel.org/all/172139415725.3084888.13770938453137383953.stgit@firesoul 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 | 113 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 103 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..463f9807ec7e 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,71 @@ 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. + */ + if (!READ_ONCE(cgrp_rstat_ongoing_flusher)) { + /* + * 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 +410,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 +432,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 +452,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 +466,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 +488,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; } From patchwork Wed Jul 31 19:47:24 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: 13749140 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 A53F4C52D54 for ; Wed, 31 Jul 2024 19:47:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26B6A6B0085; Wed, 31 Jul 2024 15:47:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 21BA36B0088; Wed, 31 Jul 2024 15:47:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E3996B0089; Wed, 31 Jul 2024 15:47:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E3CF76B0085 for ; Wed, 31 Jul 2024 15:47:32 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6385FA5E75 for ; Wed, 31 Jul 2024 19:47:32 +0000 (UTC) X-FDA: 82401082344.23.92D7F2A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 8D3144000F for ; Wed, 31 Jul 2024 19:47:30 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=u0L820fc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722455246; a=rsa-sha256; cv=none; b=gEYQHoNQDs0oaoRM1lp61IUlS8XlvTQUJFXrlsPw3hZr1RgAt11n2cUj5LCcU3inLLbLI+ VDGlirpQ7eWb6+KPVXSev5jI5ynntH7j6a39BetYBmhZaKuUoGF4XwaKsVynLkePXNe7Z9 qiMeNNjuGKRS5j1q8fugL+6xIN1D3Gg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=u0L820fc; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722455246; 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=QEBgh6a+BlMKGpUab0Qd+RjfNoGtp/N4LwMuZJAvxD8=; b=aA0pj52/B+mQc5j9m9TOCeg/ojcdXzrfk9BUbx1NWr+307gpWW+HGhBd3Q1BvHA3FwNG81 AoIRYHQAgPjE6RGqsiltRY98MoJcxEf1ZhzkYkWWHLQHRVsTkpo2ypSU/njgi/ufAC8GmO v08eIjTgZhs9a7coxtv7vkzKCGWU72A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7339A62607; Wed, 31 Jul 2024 19:47:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1F23C116B1; Wed, 31 Jul 2024 19:47:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722455249; bh=Q2e7po8J+L6F35gasVB8tGKFXWuPCBzWtFxt9x2NrGQ=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=u0L820fc2XQ8WzMEYAVX39p5MrG9oz4sqnYgcqtpSKKKwTVDAkf1Zzcl+eDq7UdJ1 WMW2zjUp0/RoJJYfCqHiyIazcRKWwvd+rDfCKgwRdJBM6MRWBk9Gk/PcYdmrHlzhck eWDkClLNs1bEYw7hEh1zwQGThMZg8UxS3yG8nVGvPg03NloQr+uZ9ns4UHiCmJFcgL fGkHJ3wCd9FvOYqCEo8vek0nchdAxcv05Zb5PyaHOB9C5LSGgpvBidbGVCSyOoCTKK JkBLFvGHe+Tkt/67tH8jiu8AGCeXq2SDv8zTwYana19BSEEecUNZs/nBSdhiUCdq0p NodnBpuSKnjMA== Subject: [PATCH V9 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: Wed, 31 Jul 2024 21:47:24 +0200 Message-ID: <172245523597.3147408.4165443154847854225.stgit@firesoul> In-Reply-To: <172245504313.3147408.12138439169548255896.stgit@firesoul> References: <172245504313.3147408.12138439169548255896.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 8D3144000F X-Rspamd-Server: rspam01 X-Stat-Signature: 4f49eyu8hb8cktcp9nko6a31yd99mn1g X-HE-Tag: 1722455250-315288 X-HE-Meta: U2FsdGVkX18sfDNFRU37XDzLzvmdbbepuE8iTLzVC8crm6iVsPY2M+bfCB8YbygdzYJubBqmAtCM/FvwK77IRX51znxLKdQInXZ3LF6/jyCDrPGdvkhw9MkIOW0aXJ5HdFvxQQYCPXlGgTy4XQ0pBjy8V7ZuOUcEU5Yw35wG8tioE5zqk85+Lyz6x6z0Va+VjeH7cDt8TtpsUrq1TyFuFm3/RnH2FTeuHvJebM+2RbfrMoanow3gmdT30SgZju5O6vrIbriLC6lAaPKKkhWzWnv+Cegku1lI5tL7oy3Mmu4OgbvY2edhn+Z50EXM3VxYohebcXttRiysIEXw2rcHwEZuHaa8LwVdStvNMojsvGFsK+JmrnLs4YBR3+UZ68rFLr8GHveeYD7rqWT5GCXwYWcaf+Mb5Kg6ca4uOorJDZ6jPXivzcHJcJhlNRbKmnR9locceBmzPHzy4elg8Eac7fQmCtF8WiBgDnqT87cWnR9JqBdLxlvMrtns0qhDtLVjaZ074lJPTcLpkhIXZ+O7Jj0iaq6vHXsMErZD5JvjoExJOrwo8J3/BgesbXNBsuL3LCGFufHq9/+oQuVGhkwAe0dUuX6C/46mpMH/NpUA0KS3K/UCDsak3b6qOZZ1qyrDEdFdzBqVtf9Te5aZIn3EXcFMBpQrUGWQDdYx6ZDFebgwGWTekTYqouxQlQS6gVRZf/cNhMl9cRVv5z05EnaAflyUY3zPhCsQp2vnQtJm4QiWRswIzELVgx8h5Z6SRhXejwzLMRZ74Y+FXDKvftWpT++NxvM/Ldgb2ZEaBK/5IpctsIoeA4Mys+RTf2miQJwtZiz8Gz+04dWUhh64Pc7C8jMwk57hK9mIRJkqtqmY1bEFR8pJki9esrEUe9OseQH4Bsv2O4z5YFHhHPgkPmmCY2AwuqXQXOoOFMywXlQYvvasj2/O65TCm6tKSVBy+NpTwFspRLAYzQdPVuM8E+S fDWtVJk8 +25RCWYgoy3IVGx1J9Vbpd/QcVLhXS2Ccy30qdzNYFyd6ftPoESmoCXxWhPezBKPwHAbxQ19lCBafg9J5pViqKSxHv0yYhcLlhgu5Q0OK4ZtiLptt7GmJXjGpeYoCes8Zim8nl60yN5dizfuayx94dkIWPxZAv4Oqq+4fL7IBGmgTyiPervfYUEz/6So1ctH2Ps7h0EmiMFcqiqvVNbOymuFeiZgHfwSx0qysy+u5x4rxJsMvwaFs3vgAoQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000033, 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 --- include/trace/events/cgroup.h | 56 +++++++++++++++++++++++++++++++++++++++++ kernel/cgroup/rstat.c | 21 ++++++++++++--- 2 files changed, 73 insertions(+), 4 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 463f9807ec7e..c343506b2c7b 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); } @@ -357,7 +366,8 @@ static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp) * Due to lock yielding, we might obtain lock while another * ongoing flusher (that isn't a parent) owns ongoing_flusher. */ - if (!READ_ONCE(cgrp_rstat_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 @@ -368,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; }