From patchwork Fri Jun 21 14:32:50 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: 13707672 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 95480C27C4F for ; Fri, 21 Jun 2024 14:33:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1862E6B0424; Fri, 21 Jun 2024 10:33:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1388B6B0423; Fri, 21 Jun 2024 10:33:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF24D6B0424; Fri, 21 Jun 2024 10:33:03 -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 C75596B041C for ; Fri, 21 Jun 2024 10:33:03 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7724AA1011 for ; Fri, 21 Jun 2024 14:33:03 +0000 (UTC) X-FDA: 82255137846.02.C4F7B49 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id 2C96D4000F for ; Fri, 21 Jun 2024 14:33:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Lw7RZSee; spf=pass (imf01.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=1718980376; a=rsa-sha256; cv=none; b=hrVezozXUG0+XydkRdB8IGLst0Cqh1vGj4NmPTjAX9hpiwraNBgf5cXMWO2mrSetu0fC8K 4F9sa4t5exw2tuZ/1XMek5oPHketVGKt4xHyHd7sdwKcjRNR+m5bjFJm+pP7lNHW240M+U Risypd0/vFYHyXOt5Wbq2ucUW8XoPKQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Lw7RZSee; spf=pass (imf01.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=1718980376; 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=VKQiM9BUvYn+Etz2SYrnAijmhFpRRt1VG27jhCZ5u3s=; b=o3/5hiAdSkgQJ0C4crIitCcjywqJHO/NLZNqtowkfL1iKZt3QDVkS/dacSNdgsdHfb+frv OO1C8XbArRB9wkWptt9UK/TzOLwCqp25MlZtIJ/qER8pKcoeYwizThoFDAQn7fIOV0IFOQ aD394CnSPNLJy/E9k4a+ij/fQGY9DgQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 783C6CE2DD3; Fri, 21 Jun 2024 14:32:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44796C4AF08; Fri, 21 Jun 2024 14:32:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718980375; bh=ez03aVKBlqGthUjZy/ZRNjr5TOr2Y00yZXdbgrk//tQ=; h=Subject:From:To:Cc:Date:From; b=Lw7RZSeeaO9duH9TTQvuqZ1NRkCsIavqwGDeNGjGj1lPcgKSG1tMmPb86gwmYTBEp QBBUS9aGRHw3hTTB+q+2eYwpmJDQMr5koen1v0X0zEO0El95ER1+Qw41KR+GLGpYLH sYIm37RwkhMnY/w6GkRTYLy0ksOaHWIpS5eQePfEAFOs8S+5j5p4O2T/aUz8iegk9L GlIpFuIJahJkUbE3fXuQNmH2OCuAkCBhIveOnA9kPe+fAVwnAHo1cNUPV3oRjJx2Kq rn6/SfVxO8alI3p8CrTDRMxmrpJU2Lu286EXChTP3D3SoLcv/g0HpR1mk35X1FeK2b OOhC//SFZB/xQ== Subject: [PATCH v1] 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: Fri, 21 Jun 2024 16:32:50 +0200 Message-ID: <171898037079.1222367.13467317484793748519.stgit@firesoul> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Stat-Signature: gbi67nxitge96a8bnb8cpghyg3dtfthu X-Rspamd-Queue-Id: 2C96D4000F X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1718980380-356354 X-HE-Meta: U2FsdGVkX19UGWuT2uwWTZgY3DvliPQcTKh8xNT4FZWxniohRU6AOKGHF2G4HbVRKpoaBtxV/RTyyXNDmisRzCJk0JEzPxxWuTHUHGh4v4Jnu/Y189gDDYH3P6ryrYZtGj7sRQL68LFebGVS/QqjpaL15f7xNidpMDxne2HRA3BXAyy28mdl1A8+Z+bFp/yZbkQpzYxYSM0yFVv9L1ehMB/mMoKQjoeLWdalwG6TQfA8cjYTIFBuDBBHoWLlR/lkJArQPJtlz0lkm1TBCnV4GS3F2Qcoixc4juKquN4KdAvw80wlNqVDdxfUTrYc68xn7NV5X6aR3tIzV5BMUnxoybpSIHAlQzWRqeJtGA6Kg6carkR8rZP1MUjaX7I2VV7iXyT/3IrhayhmeLWkp31tV9F8P4a5uH2T0AADqn5NdkZCfShWxedyLNOIdfXJvbF200mYiCUzLmGlrZJVfYk9stJ1kRfFq9UsvxHk13Lhqv7OFMPs6HBPRgQ9QrSCela87hf+rVXIsFxFWRF3p+2v8jE/QLTItfLbhfclFse8r7fw4nQUVLOYzLX/JN2jOGrsmyMhKTPJxXoofGcJMOkmEm8EE5C826EJtpzXuizdb0EJsfLWSLaUUABWSj1msBcuwDsW3zJDgIEnvmZxvH7nvojnAEW9Kqv2a66jzVizGU8auUNUNOVUzPCjnyDPAOoFs3eOr01zWuL6+8Ca51blWlcTcA9h1CPrS/LkQvaUEvihESyeM8mEZHYq8aYaiI1bd/5uUf1chS4N43rOGa68v0RkkBT30XgWgjMMnG5H9aTiWKM3h39RpzeoTzFqfYxO+Rvi/i17uTctpkg0h/gi4SG6jlM5zI2Uof1h6HR/1s4d5rdIwUOZcPNGwIN/t2CFdsTiGHB+c8KnXeXxBufDH4rKtJHVsobP5LJjGcd+Mmr/emwWDrcKBt1wVXUVq06yItrh+AdeGORVsvUhSXC Xre4MbvT 2WeLBbGgC3y8HcXWA7SMDt/o6iD0HafGWktTVM1+CbNOjpgPr7wbup84dRu3hmDQDkMM8dFYs7pX7iZ2qNj0+oDLH6A+dr3d2WmXdTD5fbiN0nTi35pQlMDsiAhEHrtEsoi5bor8tyQ1h67BrvouBDQH1jnde8+2NxoLH8jF9yopvSijAakgbZqEOLz6T3RTrXHECaefiT184bQjper7OMbuZ3b8GzeMDAY2iGewZhFH6SD4jRMj60hZXLw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000024, 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 --- include/linux/cgroup.h | 5 +++++ kernel/cgroup/rstat.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 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..5aba95e92d31 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); } @@ -379,6 +404,9 @@ void cgroup_rstat_flush_hold(struct cgroup *cgrp) void cgroup_rstat_flush_release(struct cgroup *cgrp) __releases(&cgroup_rstat_lock) { + if (cgroup_is_root(cgrp)) + atomic_set(&root_rstat_flush_ongoing, 0); + __cgroup_rstat_unlock(cgrp, -1); }