From patchwork Tue Aug 13 20:47:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kinsey Ho X-Patchwork-Id: 13762482 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 A59B1C52D7C for ; Tue, 13 Aug 2024 20:48:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E9EC6B0096; Tue, 13 Aug 2024 16:48:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 124BB6B0095; Tue, 13 Aug 2024 16:48:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F08A66B0096; Tue, 13 Aug 2024 16:48:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CE7496B0093 for ; Tue, 13 Aug 2024 16:48:01 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8DC73A0880 for ; Tue, 13 Aug 2024 20:48:01 +0000 (UTC) X-FDA: 82448409162.22.10A3199 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf20.hostedemail.com (Postfix) with ESMTP id CD4ED1C000C for ; Tue, 13 Aug 2024 20:47:59 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LES5v7eB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3fsa7ZggKCDcdbglXrahZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--kinseyho.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3fsa7ZggKCDcdbglXrahZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--kinseyho.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723582067; a=rsa-sha256; cv=none; b=vFXN2djx6IRKJlTsgqVQxdxibjDxyx9lEyjO9BzqQBQyzVRQScilyPUZ8/i1SaJ/zMWW12 Z408QNHkcbfo8ywjY4+x6iNKp3NJ2+1uREVn8WEG/TPYZ1lEfdUgHzsWymSvARbkuN6rtb 0lgIUhROAyoOJ4N7n8G1u2eQc0/hiQo= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LES5v7eB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3fsa7ZggKCDcdbglXrahZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--kinseyho.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3fsa7ZggKCDcdbglXrahZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--kinseyho.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723582067; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7PkbmuKxOROTjGI4E3wXgUmmHtkXl9USrDaeWbBH7v4=; b=I7sN4wt/HAa0FkVrzEyvlmWlrhLcsik1syAv/LyObxua2+y3mqUP4ntLeJizeGlA6sUoFK BQ4qcSMtEDZMVF+n6rkLXo+vCt9i53AQKSKFJKjWPZuNFmbl2LN5twBGXZQzTihJEHCOd3 NUALkEoPDzKT0MYtqF7izkLM2IQQT+Y= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7bbe0ab18caso5679826a12.0 for ; Tue, 13 Aug 2024 13:47:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723582078; x=1724186878; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7PkbmuKxOROTjGI4E3wXgUmmHtkXl9USrDaeWbBH7v4=; b=LES5v7eBGr+UisWSeCLiIJ8U3A8s+zL2KsMJd/mSh8qQrCXgjXlBScQ9UxgdoGHjLk l4C31rvSLyHojIvtaIAphWwIYzw7KUza0l3rbjSsWF4NYoy3T5qO+Rdo5sIGLD7OGeLI KrcTVjJurMiZs0njXDINz5f6WlHSdjXwfO+wxtAgkAnXYVN+lyIUknBI0hj1NUbCkLVQ BThN5mkp88OYrIdZR1wCxkyvnBFbU10tKtqLjPWOAXcO+pY7C5H4WP1QcvfeMHr4p5TI a9QqcjSCmLqfhqfYElbLiXLnRh1A9MrzxUtAhsReF9/NlHSZ6tQAMCsCA9ZaXFvq0tcE T12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723582078; x=1724186878; h=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=7PkbmuKxOROTjGI4E3wXgUmmHtkXl9USrDaeWbBH7v4=; b=Q8oBEJEC4ySNade5R8VGwCdUk1FS2Xk27TxqWtetf25oqY7akcy0uLo3Ww9VfvOGus siaXpcUs98Aj9qoYXgl3QU+E1k4LeYE9y+oeUxxJvwo4aULIlaiPPRHLTeC/SiiQr2yx Kt09p1oP48OFYQ0c7H9L/Tent+GNlvboyAtmDuGIyadq/ID9L0hevuieMLjODFO+lzHa CgrJkSGP9Lg40nk+GxWuA1RFto22MNNyP6tz+uXPUtXtFtt4Aqnr0DYcDvXOosPqRnWU cNLlmEWQWDja7UIsNQVKRZFqv2PwCwyyQzdD9y0SVVT2kjg/JRKKu4OtUIroZXR9IIvT mVQA== X-Gm-Message-State: AOJu0YwmFODeQulBijNrrFJUhfjZ6t/3FXSnwGBaJWRylDE20fblSAFC ti2jYQFtcwxZIT6g237CQ+IbMSRqzJLZQjBLzvDC/IuSJkWNYLKj3iifMoO3MAXgFw4MlCSGIbP IG6Z7ysUcQw== X-Google-Smtp-Source: AGHT+IHAciPzqd2lPyRlBVSYjYEEfNedI8L6N+K0qntudc/ZA3AgPVxsZZlT71exmwJpvjWHZeV3opJ32aRJGw== X-Received: from kinseyct.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:46b]) (user=kinseyho job=sendgmr) by 2002:a05:6a02:58d:b0:6ea:a4f3:14a3 with SMTP id 41be03b00d2f7-7c6a56d0242mr754a12.4.1723582078235; Tue, 13 Aug 2024 13:47:58 -0700 (PDT) Date: Tue, 13 Aug 2024 20:47:13 +0000 In-Reply-To: <20240813204716.842811-1-kinseyho@google.com> Mime-Version: 1.0 References: <20240813204716.842811-1-kinseyho@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813204716.842811-4-kinseyho@google.com> Subject: [PATCH mm-unstable v2 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, Kinsey Ho X-Rspam-User: X-Rspamd-Queue-Id: CD4ED1C000C X-Rspamd-Server: rspam01 X-Stat-Signature: ffms6sxyxukzhcmjtcwzr7iyxu8juhnx X-HE-Tag: 1723582079-626707 X-HE-Meta: U2FsdGVkX1+1Ai3XbrOiE3Wr2EciBf416u4CaJfv1z7FE8HkiOf1CNvHeYoKpXAUFQVj3BVlgQXslGa4WcjDAs2bzw/BB0HEdSYBXxX59byjlmtcVX3c+FkaYUD6y7m6yZ8FjvRyYJ2yZCOMWcV8B2qfF94KMdCfPMGQuE0p9FzVIaZHPfPLHb5qJ4oSD9sQ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Qn9igpw4Jw9rl7g3F5YaeHriCG4Q+GkJKcwNFBnFO9IPyvqjF2R/jGVUQc3DewPsNaESx1KNOtBgDQ3WsJubDE9aMv4ngfhdH8DROUIVoMpoBTivnfuhMpM2yqG8NDXSJd2t1wOnmq/hkvPZ9AQ5ENhuStEK7B7ti798sHqcg50p2G5beV+AeSS0cVH/o2+jmd8fo4U9pG24ZK31m4AjpyiWPRbKFezFvIgWsbXAcPdqyfRJnncy6iA== 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 --- mm/memcontrol.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 1688aae3b1b4..937b7efc41ca 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1036,7 +1036,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; @@ -1063,14 +1063,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; } @@ -1093,8 +1085,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: