From patchwork Wed Sep 4 19:41: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: 13791305 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 80E34CD4F24 for ; Wed, 4 Sep 2024 19:41:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFA928D0007; Wed, 4 Sep 2024 15:41:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B84508D0006; Wed, 4 Sep 2024 15:41:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FE388D0007; Wed, 4 Sep 2024 15:41:17 -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 789C28D0006 for ; Wed, 4 Sep 2024 15:41:17 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0640BAADED for ; Wed, 4 Sep 2024 19:41:17 +0000 (UTC) X-FDA: 82528074594.02.68C26E6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 55B5940008 for ; Wed, 4 Sep 2024 19:41:15 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RxzGiTn6; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.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=1725478804; a=rsa-sha256; cv=none; b=fNwUWmo+MMK93I25LE0FqZwf4+5OMZY12dCnxhfZmEfbhEIIrL0O2OWKXPVOF0kwaDg5zi +UzoDBbWaPtJGXaZ7jpD2cUqb5G13VlvargOfcE6PVlMMP2EL6hq0ve3TL9Cfgzqx5+a0N pAXue2q1YBQcjAlOOS5pngxch+55SLU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RxzGiTn6; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.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=1725478804; 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=72RAVrM/erT4Doj2IjtGxpAwBB2DtTwoG5I6vu4twxw=; b=lq7J2h7Ov+GUUoD1rkVO/4RIdlfwQ1WwSkScV7NN+fwI7M5yZMk09EKx1iWZ3nZ2REgjbe ZIh/y1B++VH7pEzcpmYjN4kIPk7VNMmbcXEpDx31YSDUtnAAX6eJARruWNhjXIwWiiiSwN kfU/rd67z+9dcIZNMf03+4a2k24dcOo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 033B95C577B; Wed, 4 Sep 2024 19:41:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C209DC4CEC2; Wed, 4 Sep 2024 19:41:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725478874; bh=+P1nXsv5c2AU6eGz3Z1uqksdsy9OIBytD2R938NaV0U=; h=Subject:From:To:Cc:Date:From; b=RxzGiTn6T/ENHaJKp9kSrjXY1MNB45MpzR3mJjZinobgAl31xETgNcf+uafQb2nSc tNCHvvwATPHV3ryifB905e91EPvdqWYQ68M1jPdecqvjChsUHOYWp5CUKXwIwD07rk tuYyNnNypJbS7g1TmVqRs1pChvkLkZ6uPsUxFaKQ4VpPUojT/EGFhWBkpuvH2gT5B4 InxlXfo+erWk7eutXvczgPBsDcYIKENWKK6CqhJkVRJCXIFFFoDvirUBlPFqVxACyc 2Oez+Gkxb1kLp168M1USm1DbAJTHmxBx42mwk4ZXWZCkjV2oAEHXqvc5QmtyZ5n1Rl FszWEQ5syzSbg== Subject: [PATCH V10] cgroup/rstat: Avoid flushing if there is an ongoing root 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, 04 Sep 2024 21:41:09 +0200 Message-ID: <172547884995.206112.808619042206173396.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 55B5940008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: mm9m8zbg49ywtc6hrzc6p5qhfjnnh91h X-HE-Tag: 1725478875-738878 X-HE-Meta: U2FsdGVkX1+DeXf6/FxR8U/cIzN7nsAHebbhiEPwQBCmmOhcjGFtIkNwAvPd91+0pTUCyTa63aUtiJUbNNKCt41CC+kUAF0HiTAvO8CkA3zta+JAACOfcKPoNf/7R5v8uotuf+WMJr/84jaXxOdrZ2E141ZnDyO/X8KG5UfJad6rUC8hZF+lLdp2sslj24xlIp2n84rR5OyTKr9SS6shORfwuHH+i09IpecNexD3L3PPz56IbD1gJh3JmRjr4uQT+wdGL+c1a93X+phroW29q0GfThPUAJQAoYeG+K6i46e7+WILRQMRk53qbcBtEqN3sZ0ZhXyF3t7yugU4BYaD2lK2CduOWfZ2BYQ91Oo6Hy/lNLfjBGEnguhj1rGmxJgKl4db9evsOgUmWNUhegVJuQcSb25PVZ++2IykvzKN8T/IcqP0mAokuIuuPSS4Lxb9PWAbwcuBPSXiscH9rsTLyhUMrRFfwX5j3JxVcBOSEaHLNPwISjXDXY1uWTYJeuXvzWvKDkAZPXS70eErn9LEDwDJYmsAbB7FlBC8f3jN1jb9iyASVN+Mf50G5zKZ/aMH1BHdvKNk2BtrbzdFAxOQwL7I9xPZJd/+1Vml3iuHCpYrUuhUBONWFi+QmoLdyzEmZLeJvKsO8xDDPf/pLZqoxJZC8XX8HwbI+GDtV34Guv/C52NlE1Vg1ezbVlB3s2My2SF19mftsxDa51TwuhDWwv0McLiBcQ2t3bslbeDuUNV7lrjGcXMZToBhVYmlP8f858jhsCuF+UV9VAA2m8seIS60AFSgRNdIi/zsm673q6joOvhUhNpZgy/tR4zGl1fIGpdJAwdPLuCyQ2LmPfqNX1MF4E3c4E0UVWjMfEyEptrQlNxQ/vvdnxxwkI1a9HGz7ZDLGqT0c0lYxYyNxA0wygT1k3/hC5nSWIC8QqYh9pwOMMnhLqwqjQS9i4ObJa4Kdmwem80nKrhic2gy2WM gA2BHd58 BXb63TYmQ38wlAh4Nki1O449nXxvrkVsa/oGwmYNhqQf/SPblnDsIV2i43onZ63KOvIe4j+/k+E5He5uLUrQ0bM5eX7gwGeQXI6Z5UXSkvmMrCze2fas0bPuBL+uHRJc1xY+8DcSNWBuY+AodXFFNizwuEGp3eR9hQReiML1y6i73eLjz/AIdkYUTMgdXrBnnObCDa9eWPG4pLn9bmoKz17YvzxFzXfKYDb31cHGFhh9P5DadDg0pBRRGgg== 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 at cgroup root level, addressing production 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. In this patch only a root cgroup can become the ongoing flusher, as this solves the production issue. Letting other levels becoming ongoing flusher cause root cgroup to contend on the lock again. Some in-kernel users of the cgroup_rstat_flush() API depend on waiting for the flush to complete before continuing. This patch introduce the call cgroup_rstat_flush_relaxed() and use it in those cases that can live with slightly inaccurate flushes. 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: https://lore.kernel.org/all/172245504313.3147408.12138439169548255896.stgit@firesoul/ 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/ block/blk-cgroup.c | 2 + include/linux/cgroup.h | 1 + kernel/cgroup/rstat.c | 92 ++++++++++++++++++++++++++++++++++++++++++++---- mm/memcontrol.c | 2 + 4 files changed, 88 insertions(+), 9 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 37e6cc91d576..e12061fc3832 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1200,7 +1200,7 @@ static int blkcg_print_stat(struct seq_file *sf, void *v) if (!seq_css(sf)->parent) blkcg_fill_root_iostats(); else - cgroup_rstat_flush(blkcg->css.cgroup); + cgroup_rstat_flush_relaxed(blkcg->css.cgroup); rcu_read_lock(); hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) { diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 2150ca60394b..4f11b5721500 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -691,6 +691,7 @@ void cgroup_rstat_updated(struct cgroup *cgrp, int cpu); void cgroup_rstat_flush(struct cgroup *cgrp); void cgroup_rstat_flush_hold(struct cgroup *cgrp); void cgroup_rstat_flush_release(struct cgroup *cgrp); +void cgroup_rstat_flush_relaxed(struct cgroup *cgrp); /* * Basic resource stats. diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index a06b45272411..91559ba3b187 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -11,6 +11,8 @@ 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 struct task_struct *cgrp_rstat_ongoing_flusher_ID = NULL; static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu); @@ -299,6 +301,67 @@ static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in_loop) spin_unlock_irq(&cgroup_rstat_lock); } +static inline bool cgroup_is_root(struct cgroup *cgrp) +{ + return cgroup_parent(cgrp) == NULL; +} + +/** + * 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 is taking care of the flush. + * + * For callers that depend on flush completing before returning a strict option + * is provided. + */ +static bool cgroup_rstat_trylock_flusher(struct cgroup *cgrp, bool strict) +{ + struct cgroup *ongoing; + + if (strict) + goto lock; + + /* + * Check if ongoing flusher is already taking care of this. Descendant + * check is necessary due to cgroup v1 supporting multiple root's. + */ + ongoing = READ_ONCE(cgrp_rstat_ongoing_flusher); + if (ongoing && cgroup_is_descendant(cgrp, ongoing)) + return false; + + /* Grab right to be ongoing flusher */ + if (!ongoing && cgroup_is_root(cgrp)) { + struct cgroup *old; + + old = cmpxchg(&cgrp_rstat_ongoing_flusher, NULL, cgrp); + if (old) { + /* Lost race for being ongoing flusher */ + if (cgroup_is_descendant(cgrp, old)) + return false; + } + /* Due to lock yield combined with strict mode record ID */ + WRITE_ONCE(cgrp_rstat_ongoing_flusher_ID, current); + } +lock: + __cgroup_rstat_lock(cgrp, -1); + + return true; +} + +static void cgroup_rstat_unlock_flusher(struct cgroup *cgrp) +{ + if (cgrp == READ_ONCE(cgrp_rstat_ongoing_flusher) && + READ_ONCE(cgrp_rstat_ongoing_flusher_ID) == current) { + WRITE_ONCE(cgrp_rstat_ongoing_flusher_ID, NULL); + WRITE_ONCE(cgrp_rstat_ongoing_flusher, NULL); + } + + __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) @@ -333,6 +396,17 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) } } +static void __cgroup_rstat_flush(struct cgroup *cgrp, bool strict) +{ + might_sleep(); + + if (!cgroup_rstat_trylock_flusher(cgrp, strict)) + return; + + cgroup_rstat_flush_locked(cgrp); + cgroup_rstat_unlock_flusher(cgrp); +} + /** * cgroup_rstat_flush - flush stats in @cgrp's subtree * @cgrp: target cgroup @@ -348,11 +422,12 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp) */ __bpf_kfunc void cgroup_rstat_flush(struct cgroup *cgrp) { - might_sleep(); + __cgroup_rstat_flush(cgrp, true); +} - __cgroup_rstat_lock(cgrp, -1); - cgroup_rstat_flush_locked(cgrp); - __cgroup_rstat_unlock(cgrp, -1); +void cgroup_rstat_flush_relaxed(struct cgroup *cgrp) +{ + __cgroup_rstat_flush(cgrp, false); } /** @@ -368,8 +443,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, false)) + cgroup_rstat_flush_locked(cgrp); + else + __cgroup_rstat_lock(cgrp, -1); } /** @@ -379,7 +457,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) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 71fe2a95b8bd..be2022f27b5f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -876,7 +876,7 @@ static void do_flush_stats(struct mem_cgroup *memcg) if (mem_cgroup_is_root(memcg)) WRITE_ONCE(flush_last_time, jiffies_64); - cgroup_rstat_flush(memcg->css.cgroup); + cgroup_rstat_flush_relaxed(memcg->css.cgroup); } /*