From patchwork Thu Sep 5 00:30:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kinsey Ho X-Patchwork-Id: 13791567 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 91798CD4F4A for ; Thu, 5 Sep 2024 00:31:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C89A6B0200; Wed, 4 Sep 2024 20:31:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9794E6B0202; Wed, 4 Sep 2024 20:31:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A4A26B0201; Wed, 4 Sep 2024 20:31:56 -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 55BD76B01FF for ; Wed, 4 Sep 2024 20:31:56 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1DB5CAAE20 for ; Thu, 5 Sep 2024 00:31:56 +0000 (UTC) X-FDA: 82528807032.01.4E8AA0C Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 4E761180007 for ; Thu, 5 Sep 2024 00:31:54 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KQJgVTNS; spf=pass (imf24.hostedemail.com: domain of 39_vYZggKCJA427CyI1808805y.w86527EH-664Fuw4.8B0@flex--kinseyho.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=39_vYZggKCJA427CyI1808805y.w86527EH-664Fuw4.8B0@flex--kinseyho.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725496290; 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=Ac9f2JvhNnpWVv2ZKYegshHvMbbmkBcnbuU73G1cYt8=; b=JgY3eimbX+XLUlIVy0fI5C5o3rfKrTxE0MB9uj/CiCelXXwY5hcmrdFz4IpOzqLbouBsbc PvPiYzazTrtTk8/EflZOYZuNFaswlqdNg9Ih0d9XVBY2Yk8FgjOarcHfTNoSyLhgxFdpLp Jh8hGU8dQrAHRVdON8s0g1N/fBfmtYM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KQJgVTNS; spf=pass (imf24.hostedemail.com: domain of 39_vYZggKCJA427CyI1808805y.w86527EH-664Fuw4.8B0@flex--kinseyho.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=39_vYZggKCJA427CyI1808805y.w86527EH-664Fuw4.8B0@flex--kinseyho.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725496290; a=rsa-sha256; cv=none; b=BZlVaJCLBG1M9bW9jK0NwqyQ/oqc2XDD0ih/gLP2cpYZDfxk1iaQk9KOKb7+Oj3aflSMbw +aHY59pHDp0SUEtclTGsBLjdYIuYBvdRh2tRFFIdt2+cLhzlQStJ2Lt4fgzVaa+VOnlV5j cTpCwN8h0gIuhW8y5gS7rxfBMhUN6No= Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7d4fb78fe49so269305a12.2 for ; Wed, 04 Sep 2024 17:31:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725496313; x=1726101113; darn=kvack.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=Ac9f2JvhNnpWVv2ZKYegshHvMbbmkBcnbuU73G1cYt8=; b=KQJgVTNSP3dXfMyLA9BpXQRCX1gyUttYMBubbPC70gyVO+r/OrfLIsiottaZpQ/XcJ V/SjVhrBY3TCfR9ebtaLezxmA/TVuj2vqILAVLI7AeCzh1+FThkrim3x4IHX+x9GD/s2 d7GRLYrX6Uci08DtBbKHxBx+404qkitpdH9FUrqoXExn7HpzJ3KbjSLGsrMY24RBipgc j/Tx8+26tq1otlJEdqOdnFYx3ek0UAId+RFc/ynV1w5YKpOvFIvf+VWFlcZMa8bn7nj1 bxPKWizwucp6sW9x/KSoBCHqCEx1OvvcOMvv6V3E5Y7asz1iBQdfD6ZjG3E3Q4Z7m6ni 7ZRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725496313; x=1726101113; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=Ac9f2JvhNnpWVv2ZKYegshHvMbbmkBcnbuU73G1cYt8=; b=V309YqJQjLIA5vL4R1d4ufrI8nOrYYHoCbA1u+Gb7vMdA6h/aTDhvg/ZjOwxkHg5Ic IvfsQoiNVRj1186YAJbpgSEY45xk2gzZasDW6hciL4DxBIpP3ieQhlCy4pvAdBeZAdnz I3kJdGftOjyA1q4P2j91hDhsPVxvDv4AxDL0dGjfx738cAYf+do9v1NBjXcVO2n6U+zv WwjPmMx6/9jwoGVDLvEkKNLmSbq7vMjX5oi3DWa1ty4lqNZLZtgLGWgEWeJZ6J2rFq5r j4zpzti9JiVoQ2fmibEy5I85bxdNex/d8O2bnr8vWt1WEAf7BAqTVkNcRzv2vdQQF0Gq Ckhw== X-Gm-Message-State: AOJu0YwEOCLYkPWmQBB4THnhRLWPcFYx1uXSKY027q+JPRTQKSl8Vvsa LeQQk6RRVLTSKOCE3e9ke7/C4pPzQs3yir2XlV6JuPIFMhjKswZXkUpNiLdGxXz+iX/mxAD7uJq DwQlaLITBmg== X-Google-Smtp-Source: AGHT+IHuJuEjgofhNfeZ87JSGyMalBhwo29t0H9DuDw9GmIw/cnVXOLyLUXVF5+Ftrg6N3vF7BBT4Qhm6qOSmQ== X-Received: from kinseyct.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:46b]) (user=kinseyho job=sendgmr) by 2002:a63:f303:0:b0:7a1:f561:8c99 with SMTP id 41be03b00d2f7-7d4c1087417mr46719a12.6.1725496311779; Wed, 04 Sep 2024 17:31:51 -0700 (PDT) Date: Thu, 5 Sep 2024 00:30:52 +0000 In-Reply-To: <20240905003058.1859929-1-kinseyho@google.com> Mime-Version: 1.0 References: <20240905003058.1859929-1-kinseyho@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog Message-ID: <20240905003058.1859929-4-kinseyho@google.com> Subject: [PATCH mm-unstable v4 3/5] mm: increment gen # before restarting traversal From: Kinsey Ho To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Yosry Ahmed , Roman Gushchin , Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Tejun Heo , Zefan Li , mkoutny@suse.com, "T . J . Mercier" , Hugh Dickins , Kinsey Ho X-Rspam-User: X-Stat-Signature: z1j1e9m53dpu6afqy3qawxfq8gapfwor X-Rspamd-Queue-Id: 4E761180007 X-Rspamd-Server: rspam11 X-HE-Tag: 1725496314-692738 X-HE-Meta: U2FsdGVkX1/wXbXC2vcgMt8bK195oITni/tWFe330szvsy9QpBCpFrANqVLF9NkisRzabYocGI/e2BTHe8R0wmgNsmdttdAcn5cOUwcy65MoXIafZgS/gDJ58GE4TYAXYz9NsqGNA7bhV6QJJM84YaDpQcEmdeOOx+uq1EmTMdCszdS+OlKr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r1jikzSn9LEZWp8SGWDKmVEeIKzrpz2bc2T0/NKNRCIcE29lK/+HXn3kQdpduT9cHRgU0Tq6QbVnPE6/jFWcZUfYpBLwZoMLyXsoSgP9WjQDbR1B9uu7zHuUPEuA/MMwd4sH8/z0LKBd6/taBR34SOi3wR9IJBQc1hE9JrJd6GDuTpNGrDe5DVURxjR/6jroVa6VwAEmB7d/ClUAhEnPJBLml+Sw+ECWZB8ZWPlgPTyIvPcn/dV4V0lWcaewGVttUbU7DoQFP1Q0LTtvGw/rqhN5tf23F8g4DHnkoPfDiu/jGAnAs2DfJfVNoqBYGKUhmTHxEpieoDb4OVYP8HkIfEXjPKGLQnyMQ3RRWDRfFEu0VDoKLpmPmw+B0zPQBUO7MmGSJoOsYZIflzo0WV1TMslPRiYea+DlX4SSHUPj1xJZNlgQn/JYajbQrRIrpmqy6poV9v/MXY8H8ex8ftWcliJTKnvn 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: The generation number in struct mem_cgroup_reclaim_iter should be incremented on every round-trip. Currently, it is possible for a concurrent reclaimer to jump in at the end of the hierarchy, causing a traversal restart (resetting the iteration position) without incrementing the generation number. By resetting the position without incrementing the generation, it's possible for another ongoing mem_cgroup_iter() thread to walk the tree twice. Move the traversal restart such that the generation number is incremented before the restart. Signed-off-by: Kinsey Ho Reviewed-by: T.J. Mercier Cc: Johannes Weiner Cc: Michal Hocko Cc: Michal Koutný Cc: Muchun Song Cc: Roman Gushchin Cc: Shakeel Butt Cc: Tejun Heo Cc: Yosry Ahmed Cc: Zefan Li Cc: Hugh Dickins --- mm/memcontrol.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index cdd324812b55..c24ef6a106e0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -999,7 +999,7 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, root = root_mem_cgroup; rcu_read_lock(); - +restart: if (reclaim) { struct mem_cgroup_per_node *mz; @@ -1026,14 +1026,6 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, for (;;) { css = css_next_descendant_pre(css, &root->css); if (!css) { - /* - * Reclaimers share the hierarchy walk, and a - * new one might jump in right at the end of - * the hierarchy - make sure they see at least - * one group and restart from the beginning. - */ - if (!prev) - continue; break; } @@ -1056,8 +1048,18 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, */ (void)cmpxchg(&iter->position, pos, memcg); - if (!memcg) + if (!memcg) { iter->generation++; + + /* + * Reclaimers share the hierarchy walk, and a + * new one might jump in right at the end of + * the hierarchy - make sure they see at least + * one group and restart from the beginning. + */ + if (!prev) + goto restart; + } } out_unlock: