From mboxrd@z Thu Jan 1 00:00:00 1970 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 6F952C3DA59 for ; Wed, 17 Jul 2024 00:31:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F0826B0082; Tue, 16 Jul 2024 20:31:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A0296B0083; Tue, 16 Jul 2024 20:31:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 740CC6B0085; Tue, 16 Jul 2024 20:31:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5166F6B0082 for ; Tue, 16 Jul 2024 20:31:11 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DE01E1207C2 for ; Wed, 17 Jul 2024 00:31:10 +0000 (UTC) X-FDA: 82347365100.13.CD33831 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by imf02.hostedemail.com (Postfix) with ESMTP id EE2638000D for ; Wed, 17 Jul 2024 00:31:08 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="nI0e/AuY"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of yosryahmed@google.com designates 209.85.167.48 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721176240; a=rsa-sha256; cv=none; b=0JQwPBiRF93NJMdBAzvdjG7lHPZjv65xXdGDy+tTCl1S9Q05nxJOK7rJwAckBBaNRyfddn jOh6GZr4FNMKgefVRksBILBliLOZTgME4CMyIRqcs0J5/7rYHIwKI3y5w0A75GunweTp7I tqxbsZk4QzhY3gE0SDoQQx9KsDdvpBE= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="nI0e/AuY"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of yosryahmed@google.com designates 209.85.167.48 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721176240; 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=/4bwsrtlDHHMtKZGxfkYdtqrb5XDD2o5DixhixOo4gU=; b=Jo0GMXQJS/O36uZf1UH3BbJoDaIy0O/UEe2+0P8B8PR5608WUQrGJQaNKkZTN6ZKuhYTmX onSZcVqQUuEryLgOzx1JUEXbBfWgawTltKngp81oKaqGcyU113eswFMJoa/z3BN90ToSYw l6NxKvWYfgxhurKsERhHZbiUxXrcQ9I= Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-52e9c55febcso8025601e87.2 for ; Tue, 16 Jul 2024 17:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721176267; x=1721781067; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=/4bwsrtlDHHMtKZGxfkYdtqrb5XDD2o5DixhixOo4gU=; b=nI0e/AuYq6W9DGhZdkpBMozqvnQhTM7GE6ufTKhKWh9aOp2InoQhMmkC/Clb8FKmOQ dCdv0D00O0h+N06xy1wzozXgvZPyrL3vIpOx+vPktBZ6ybnvhc+qEvZo59htg2HUHUFh XbIOIpPHNydrwDcch5ulpWd3e9KPWyjuIctG4ZaS4OZ/1Zi0b0bopy6eUth9TsYtBbQv OVEou4pb+viMAW66jK8YTzXs3V220r29TvOfDrUZtSMPNOZR3VA4opKWOlgNtF5QT0wN Pc7er33hhwxocbQz6iOIK2Ve7m8XKWPZDs776gkO8Vg77cn6BmzORawPcmQ47phkPcf/ N2rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721176267; x=1721781067; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/4bwsrtlDHHMtKZGxfkYdtqrb5XDD2o5DixhixOo4gU=; b=iWdu9i86PbHsRPvooTittkwxOKzcu1xGqdlsmPzAYQIWLktdBTrcNU4cUkzMUFR2rW NntnCgIKDYmJvKyr1sQ2/S+Ygr/yUkbRn342VfGCMhhDX4js7W/ZXPVo/EpIkAYURDn3 EerhlFg5qwfOTRFLamWThDo39u7d+cPmnTImRUr3JV9dbSip9lFm+HNjALb5/xDdG8te L3KcbyVnhl71JeexoWZL91ZPCOHBUNg8UwrjXjaVojLblIjPjAI/NZBxYYhJZ3hxbkug Uc4s5XCkN7HClr1lO33HYLtm6rQhmg1rhAuoQVCryCDON3WzJa2p5bFmT3BRakESHnY3 rojw== X-Forwarded-Encrypted: i=1; AJvYcCV55F2j2Pzwi3AEU99XXq0VCltmHxXGDPWo3qkp8jInXIY2Mm8RXH+fwK83fSF8vaGJEH7DnD94Km+azVLoOw0QdNQ= X-Gm-Message-State: AOJu0Yyd4RnbcPc8f0MNhwRSlw+0eOQ/E/W8SYgD9WBXisuk0Kq+l2md o3+O+9rVU54WNJcKGSiJlG99Bi516ZATrTuB5Ne/pEFnYMEnfc+a3FnyrDXSZ7OJJn26h4Ngt2V 4584XGrvZMy0ZXIQkmi1kHiPUfonAz2FaJ/dE X-Google-Smtp-Source: AGHT+IFJm5p6IKAa3mU6ONozlPoYypzzzR92UyGaQvJEkEpub9FMJBzxYpvlKVYAVIcUsZf+eJzDbzuq8bxIvG1ImYk= X-Received: by 2002:a05:6512:3a81:b0:516:d219:3779 with SMTP id 2adb3069b0e04-52ee5445f73mr52423e87.58.1721176265812; Tue, 16 Jul 2024 17:31:05 -0700 (PDT) MIME-Version: 1.0 References: <172070450139.2992819.13210624094367257881.stgit@firesoul> In-Reply-To: <172070450139.2992819.13210624094367257881.stgit@firesoul> From: Yosry Ahmed Date: Tue, 16 Jul 2024 17:30:27 -0700 Message-ID: Subject: Re: [PATCH V7 1/2] cgroup/rstat: Avoid thundering herd problem by kswapd across NUMA nodes To: Jesper Dangaard Brouer Cc: tj@kernel.org, cgroups@vger.kernel.org, shakeel.butt@linux.dev, hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: EE2638000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: us9tawxg7jrok6zs3q7qqtz4i51uwdgq X-HE-Tag: 1721176268-869613 X-HE-Meta: U2FsdGVkX18MCHz93pOuSHC/w1py9+bz0GkitS+lG0ytWs9GV+o2S/8qQ/zbu+HGehb9RSHZAACJU2vcq7derxaBji1azt0XuZ3hXqvK9tfDBSPimw2v2NV58v8+Q9RX081alOi10X8/1bfO5EOAQEJBu/wVVqySuIzCp4QblAqSBdFUmsrKN3VmxPmTZc+DT6mq5tCG899BeiLIfsZZ74N+HlkLgcRaVUtGflvl4++1tAARUQOCCnQof1UEVteRlU2LdyW42dqsZy1JKJVaf97Pp30faUnV2rxWUlEXZY8FPWFHJuZ20zpPFNXtvE6Hlb18IH4VFTaEI5/HkOPUFusWgEAN2tqXvZiGKSpW4yMiru5XK19Ii+jHfdFyzqN1CSr0fDxa8uIAZma8KOmjEQaMzqx2nou2/DvCZp2B0Q627nMf3gN/EEY4HDbAJvKZQxosz/dltbfD3vf/HcbV6eqJDEZZ2GT8Gjd+uorVTcXtPfyoQjGE7tkzIESD3rjCBr3PWE6/77F+9O7st8hLgDz/KErMLRyd6WRFCeWaEhlQ0DoCYy146nqkmT+xr9dSirqfxJu4itM1AcEtoh4PxBMOrsLCiqh9LjPgTCAXk4UmKyx7uRIRdJfxB2alR4kTNu7ZKnfu5cEwS3DK6c9ifHjMe/4y6uLIPOoO9VGKmO7VwQiWzhcNXnSe9YqAdLdwbiIKCY1bOhcHyvwynfwZTNTA74UrjH2I34sv1V7Z3OudJx6uXe9YF1xK7PylRkb79EoEVyRGXvdhRkM550MRMU1Q9ElnpQK8VG4cMoJSmgWJCLZ+j0Ebkvpa5t5edr732RPXEbJa4RT5qBdG+2gNRxJZoq+VqDdaWNay3pQYpl4B+20Ppw7CXZgqSXX1brkzaZel7j+d/UKVj4p61b85/e74rKcNxCVPFLHouzVbSFbTH83CzNbmDqAkoehKripPl5DN17N+aj0NyIq3g+d mKyW6Iw8 MpprTCs9qgfc7zO0IIprXIvliQ5fJF8FadQFcsR+ecM3cifQEFPqk+hd8OV8B226+S+8XK0d3D1gEXwUoQd9O/bHqr0RB8SPP3ZyD9twXJYTqhDYTAidWXaftf6E5P3fHx9ZzcPB0/UkUZb2MuX+FLuSiymY6Rdcs+Kzwh8Ce1LU8DlA597fUPaamDIxn7xbyhyq2ihFC3nCJipnP9HmYcP2waWzdxgKMqCpOkWm9NRjINeo= 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: On Thu, Jul 11, 2024 at 6:28=E2=80=AFAM Jesper Dangaard Brouer wrote: > > 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. Leveragin= g > 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 Thanks for working on this, Jesper! I love the data you collected here! I think the commit subject and message should be changed to better describe the patch. This is a patch that exclusively modifies cgroup code, yet the subject is about kswapd. This change affects all users of rstat flushing. I think a better subject would be: "cgroup/rstat: avoid flushing if there is an ongoing overlapping flush" or similar. The commit message should first describe the cgroup change, and then use kswapd as a brief example/illustration of how the problem manifests in practice. You should also include a brief summary of the numbers you collected from prod. > --- > 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 =3D 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_l= oop) > +static inline bool __cgroup_rstat_trylock(struct cgroup *cgrp, int cpu_i= n_loop) > +{ > + bool locked; > + > + locked =3D 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_l= oop, > + bool check_contention) > __acquires(&cgroup_rstat_lock) > { > - bool contended; > + bool locked =3D false; > > - contended =3D !spin_trylock_irq(&cgroup_rstat_lock); > - if (contended) { > - trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, cont= ended); > + if (check_contention) > + locked =3D __cgroup_rstat_trylock(cgrp, cpu_in_loop); > + > + if (!locked) { > spin_lock_irq(&cgroup_rstat_lock); > + trace_cgroup_rstat_locked(cgrp, cpu_in_loop, check_conten= tion); > } > - trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended); > } I will let Tejun and others weigh in about what tracepoints we should have going forward and how they should be structured (which also includes patch 2). I see the tremendous value they have to collect data from prod, but I do not see similar existing tracepoints, and I am not sure if the branching here could have an effect when the tracepoints are off. > > static inline void __cgroup_rstat_unlock(struct cgroup *cgrp, int cpu_in= _loop) > @@ -299,6 +316,53 @@ static inline void __cgroup_rstat_unlock(struct cgro= up *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 nit: I think commonly the comment would start on a new line after /*. > + * we are a descendant skip work, but wait for ongoing flusher > + * to complete work. > + */ > +retry: > + ongoing =3D 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 =3D __cgroup_rstat_trylock(cgrp, -1); > + if (!locked) { > + /* Contended: Handle loosing race for ongoing flusher */ nit: losing > + 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 */ Do we want a comment here to explain why there could be an existing ongoing flusher (i.e. due to multiple ongoing flushers)? I think it's not super obvious. > + ongoing =3D READ_ONCE(cgrp_rstat_ongoing_flusher); > + if (!ongoing) { > + reinit_completion(&cgrp->flush_done); > + WRITE_ONCE(cgrp_rstat_ongoing_flusher, cgrp); > + } > + return true; /* locked */ Would it be better to explain the return value of the function in the comment above it? > +} > + > +static void cgroup_rstat_unlock_flusher(struct cgroup *cgrp) > +{ > + /* Detect if we are the ongoing flusher */ I think this is a bit obvious. > + if (cgrp =3D=3D 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 *c= grp) > { > 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; > } > > >