From patchwork Thu Jul 11 13:28:42 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: 13730729 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 EF3A0C3DA41 for ; Thu, 11 Jul 2024 13:28:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71E0D6B009F; Thu, 11 Jul 2024 09:28:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A7E66B00A0; Thu, 11 Jul 2024 09:28:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5467E6B00A1; Thu, 11 Jul 2024 09:28:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 32AD96B009F for ; Thu, 11 Jul 2024 09:28:56 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CDD5CC0634 for ; Thu, 11 Jul 2024 13:28:55 +0000 (UTC) X-FDA: 82327552230.18.1CDFAB2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 5F30518002F for ; Thu, 11 Jul 2024 13:28:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WNRBesbM; spf=pass (imf24.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=1720704518; 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=xOt7+Dfr+GtDvbnGtJ+kam3CKpHUtzq6d9HIMVfCZHk=; b=I0ayu2XM3b4+yfvnv6lNv506fFSNhe3jsjnR5Al9sdm7nyUyHn8m89BKjhYsD85S0Kfiwd lj15oBFV50iTHUmjfDR8hUpJlLbeIMUiklLEWScOa2AUCngQvgIA9YqqFBB5vialA4TEoe IbcNlojlIIhUdr77Vzejv/38jT2RnkA= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WNRBesbM; spf=pass (imf24.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=1720704518; a=rsa-sha256; cv=none; b=NON2HmQjtYxm5XOYXifDe6quZ++uruurKIBdUeNWNkM2j8/ddjrsdUO3Um9/9jWjmzI3x+ Xq5RQDaX96BEQmk7yxUcbVJNkDIaQoieEG8M5Mqw/XnJGpLemHK0110j+XVKqSe5zlCceG LN7bmm65Nt6LvoMdIDztd297j79/Tb0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 30F72CE1872; Thu, 11 Jul 2024 13:28:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECFA9C116B1; Thu, 11 Jul 2024 13:28:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720704527; bh=Eb9o1Di+c/CTeNyBD8sT9k+z0JOCVeAcAfUHZWxXvBM=; h=Subject:From:To:Cc:Date:From; b=WNRBesbMcICpfsr2BZ/g2mnbaAzrpvsuLnSDEaprlkMUKyKqo5cH/x2jQPCTuiPfI zLQnrYDv+33cPLokYY8sSI5CPNtD3ehmfAE1PJpvCDqjE2vLpX3NrlBxNMJ15/rft0 zz0bOvk/s2+M6Krb/alRcZG4UeQj5+IJ/55iQIei8wHDFpoKeXGTcqV3AYuSN5UuVK aUfu7tvQNfnc1kfgsYoGsjsqDq0GjafkdUKRUcG/h8Gnv6SPgm6AOyvXhADvxAQjl9 m6j2+DIkhJ0ClTolOmMKDRI0aDQgj2mwrXRcfpzyApXQzQ5kckfXCpehkUKq6Iu8uw nFknN7UIze/bA== Subject: [PATCH V7 1/2] 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: Thu, 11 Jul 2024 15:28:42 +0200 Message-ID: <172070450139.2992819.13210624094367257881.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5F30518002F X-Stat-Signature: 8ap3qce6s94xm6sxikjsjtg3ansr9azf X-HE-Tag: 1720704532-275199 X-HE-Meta: U2FsdGVkX18Gj2t3rxws4RtOYZcemEJOO9VFP1v9uafRd+7OQW6V42Ca7agsFjxEhqIrT0HtnMcnJVg8ugDYDQjfB4Txm4ZbOVjoWyXkBdmtps9cSRaaCN9W4Rjh2TJZ/7KXmhFhVTQuTU5cZeiehbfsHiiE1E0YKWEO9c8IvicI4PVK4WE2DjYwR8g9+sHTqgo2miRjmYJcGaB8kfnnXgIRFyd5kYEt+5Z9XktEBFIWETP3mC3P/xM4knXQ+jKvfTl0KLKz40HyvedZWT+KzAQSnu1VjQbvwAL0lWzg+1z6Ch6p9Jn543vG5S4EO/gWuTTfGUhheLJkT/n/I7Y4minAT8h5grQBapkAgss/bMsGqYDrJY3NfWvVnBxa7XZ4kS7VhnoHSukrFDHvWCIyH1rgSN537LABWBq1U1yen6GGSb8t8YsA+Yjwro7GnqNDk1HTlfSsXdugfANba+tZrPsNCaF9nXMoT2wS4M2UeLhepmDm2T5qnIGTQLW+BLEET3Qe8QdIlaVv4KgUIn+eq7n8w9sAcvkIbcxa2B9XbgZ+irZsTIQJQ3K2qfF3O+/iCsdhXmAMvhUMwniyHO9LxvnXp2h2sO2Hsv4fRg+1xs++i+/ns9hmZsNLvJDf0DqmSEVc+d2uajNdEU3KGpeb9+G39fQCYp1U4NQxAIhc2HC3MpH3er9hjH+R23E562wH4fwG7dQ+pOJ4DF6Aeh0IkAsjvd2112BKxnIkQhMQohtSOvlnBW3zjJRm9iDKmfYHzLJNNvj5jU/Qco344B6OgCnmDZsrxgAHhX4gXAjFNrU8To9toPeeqJROkxqMGS+kI5MLBOIJwscv4ajUjZPqmNtKfY257TjuavL+ADoXawgPBluRhw0QOAeDRjg0kT3doxaHGivhSx8qK90ve9vj38tIjw5fZ7XMl4hew6u0SCL4driuGV3/pGTXoB9HJZciAnSeKYjrQXpj7CNeHwm 4YHAvOOV 7uBu9l8NAos9LKo2rZ2Tz/UoZBhiVUguDyT6TCIcEHvHlInCRVQqe1vE6wDXWGNV7Jdf6wivCGAyAA+A+6l4DiZ3gTNDoZVkzGhYwfX3yv8ipRK7Kpy6s6PAChh4vrotlYPk/F1SYAPDAvVovH3YQT6zeArHEzb9mJxkSwthvhXIEA1flbPDc+4aAeXTptZ/hlda4QQBSzAnr+yo/9gkZMMtjRTiU7H+6p1IZLiolqQtqSHdwmhDuqQlIAA== 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 --- 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 | 95 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 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..fe2a81a310bb 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 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, check_contention); } - 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,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 *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 loosing race for ongoing flusher */ + if (!ongoing && READ_ONCE(cgrp_rstat_ongoing_flusher)) + goto retry; + + __cgroup_rstat_lock(cgrp, -1, false); + } + /* Obtained lock, record this cgrp as the ongoing flusher */ + ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (!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 +392,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 +414,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 +434,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 +448,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 +470,8 @@ int cgroup_rstat_init(struct cgroup *cgrp) u64_stats_init(&rstatc->bsync); } + init_completion(&cgrp->flush_done); + return 0; } From patchwork Thu Jul 11 13:29:04 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: 13730730 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 23434C3DA49 for ; Thu, 11 Jul 2024 13:29:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 808276B00A1; Thu, 11 Jul 2024 09:29:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 790F06B00A2; Thu, 11 Jul 2024 09:29:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 632C66B00A3; Thu, 11 Jul 2024 09:29:12 -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 419116B00A1 for ; Thu, 11 Jul 2024 09:29:12 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E2ADEA0607 for ; Thu, 11 Jul 2024 13:29:11 +0000 (UTC) X-FDA: 82327552902.30.41B82F0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 339CF18000A for ; Thu, 11 Jul 2024 13:29:10 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lnXshWEc; spf=pass (imf06.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=1720704518; 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=VigyS0kQQ56bwb0M/vpZa26Meg9GoOnOTXPJC3GA0/s=; b=hb8LgBPnXAyoBIC+z/Xsa7R+Y/hjDjh26V72ya7gnQFpafW3AqokqkaMqEVn1PcQfyDwis gRmO1kBEGz5avesG780tAtASFF1TrxunkcGe2icmixtad4YRvk10GMt2UAJm1erUedOpp9 Lz3h2cPIg6md7MYjJ0gG+WOygXcRvbY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720704518; a=rsa-sha256; cv=none; b=XsAAWkBXzLdg1SgV7tkQnO6ZAsFpJOpwhsFKoHcIEnr5yfvJzNhJOh36X0NZmQxkB3uywH FbEn7DxY6dpHKlhh1W5IPzLxCXTTghl+YII+O7WIfTJWIXUNJbunJuIcs8KILcrXkKnger oY5wpU/bCFrCDM+33Js0MPEipxxn4lI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lnXshWEc; spf=pass (imf06.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 05F9661A8E; Thu, 11 Jul 2024 13:29:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37D8DC116B1; Thu, 11 Jul 2024 13:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720704548; bh=71qZnHa/lOQXOs6SPxp3YR6prqZbbSZTednAdP42Nvw=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=lnXshWEcQmP4VRNf41OlqGBQFa9tJ3wyNmFf9hqXdO/3++NfHoBJ/Xy2ewfxM9vGh 0YKn9zTu2OBXDdqWDy554AlBqmCzJ/jpuYHq6ZR6TaxKsaLBwdhv+Z3QzwgojDV0uL xMCfwonkVgOttj0Mje6clsRQaE7t9BJFHmc0LZOUxmWBOi+5kaK3z4qnY+Gw4ON8pU E/U13b99c/k9Pp3fMGlMx2dJIhNcLln28AcKkW65HrF94toRmbo1Afx8nPIyW4DJ/9 PVn6gvsm3eX2vSqplagOMgS4ez0szl1yoBs8MHwkcJwguZXFkmnTgynhN7gZbUcpM3 dUxY/zrrEeZfA== Subject: [PATCH V7 2/2 RFC] cgroup/rstat: add tracepoint 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: Thu, 11 Jul 2024 15:29:04 +0200 Message-ID: <172070452878.2992819.10548676901200594081.stgit@firesoul> In-Reply-To: <172070450139.2992819.13210624094367257881.stgit@firesoul> References: <172070450139.2992819.13210624094367257881.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 339CF18000A X-Stat-Signature: 9cr67aediz8jqz6zw9qeqzawfmcsrdyx X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1720704550-443569 X-HE-Meta: U2FsdGVkX19v7vGGJc0G+O88vyvIJM0Rk2bxK6BB1xKqhXAVXZRTrPsKzorfncWxdiK8Do5Z1SEH18aah3isOyQl+jbO45HLaxS+WDLHBjKUtkxwEhnzw2DZ6GoQRNlJbx00f2mwBiJN3qNCcH/aMUb0NOfm+hZHRORsSeuqz2i+OqAVGOR5Qt+O/qx77gB/wPfQmRnOpWWk+N6A5kaAMfz/unVsnLZre6fidOMAK4aDX8Vib1YTLv7Q58cyRQ8BUFwQT616NJPuP1tFMpyBp644DZ2blh26yXUCh5NignEV1mVdbCasH6lUIgQ+kjPQjUrVh/pirD4JDxX9CvqPizq/XUUizBsj0xdruQ0i9HZscsX4Y3+whoGbTbrYSWzlLotolTkMUj+VRsm2GrvRrVdK7klbzm2wK/6UvckVe4JAsnbY1Qyv6+9YZ7g0dQK5xiBBtGvx+FU4XIz/T5tO9Mnkp2/j4NQFMulnoYk6SDvA0eBkpgKL4EzAiSU8aFUL2NUdrgwtPe/kOCFnPV6JyVt+MfV/nwQr2FrSzSXV3Zn3lgtjNKOnPmoqvRFS+y7lbYiyhPZl4S/pr8K9BSylvaCV53lQ+Rq3SQSsqkImcCyvRZPbGbyVKaRsH9Zq9xmA2BRn/rXnROzSJxe2eDLUBGmy18xmrq+2qwm9q/+IpQPZrAuT/20nSvwUojXcIYtCqJjUg0fawEZlbI4Wbotx15+79ar8xkvls7onrGJzJHHzaZPsIj9qruMmK8CIRtMpqxEiORvExOz+KxWPfI10eb2kfCrWhIz4nw/7hahLz9ATKwxsn5xADE5jN60JrvFjcdagxLFH5f9oIZbP76UVm/kBjg+WV+XgrVUGlKobuLYCpfrA1Y08EYG5Pp3Cx5lH9ws1JNEK/DdXROHZ/15nKjrm7w+t6i5YxFGcPmNsecw1ummWNHqtQZHPEAO1kIb52zR0VbHRbhEWWJiqzof xXklPYht 8hLZdNnJP0W5l50/812XxJfj3ts42hh328vOPXldMfuVw4jrgxW6EmIAVuXQK4Cj1xEqtlBfxot58jXsNq3EVpr0hFx5mNNAmktD8a9/b3V5ETd2qz1a7hVEukglMNpTfZIm3lm+3kRMrgU2n91a1AL3a93/O7bfZPMR/ePlq8qcVrprdmSR1IhXl2BQe3GBeSP6f51WOyG4GVGXjc9BfTTsFx1Emi81ghcY8MIoLr4V823C6mmpmQHfhXQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000721, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: I'll be using this tracepoint in production and will report back on findings, e.g. measuring how often the race for ongoing flusher happens. Tthen we can decide if it is worth to keep/apply this patch. Signed-off-by: Jesper Dangaard Brouer --- include/trace/events/cgroup.h | 49 +++++++++++++++++++++++++++++++++++++++++ kernel/cgroup/rstat.c | 15 ++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/include/trace/events/cgroup.h b/include/trace/events/cgroup.h index af2755bda6eb..c8d84e9a08dc 100644 --- a/include/trace/events/cgroup.h +++ b/include/trace/events/cgroup.h @@ -296,6 +296,55 @@ 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) +); + + #endif /* _TRACE_CGROUP_H */ /* This part must be outside protection */ diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index fe2a81a310bb..2d7af86e31c8 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -321,6 +321,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; /* Check if ongoing flusher is already taking care of this, if @@ -330,17 +331,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 loosing 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, false); }