From patchwork Tue Aug 27 23:07:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kinsey Ho X-Patchwork-Id: 13780210 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 AE171C54743 for ; Tue, 27 Aug 2024 23:11:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B53B96B0092; Tue, 27 Aug 2024 19:11:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADE656B0093; Tue, 27 Aug 2024 19:11:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9300C6B0095; Tue, 27 Aug 2024 19:11:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6D79C6B0092 for ; Tue, 27 Aug 2024 19:11:46 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 33221A70D3 for ; Tue, 27 Aug 2024 23:11:46 +0000 (UTC) X-FDA: 82499574612.11.F97B2D6 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf04.hostedemail.com (Postfix) with ESMTP id 6A70F40022 for ; Tue, 27 Aug 2024 23:11:44 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Rawjuc2t; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Ll3OZggKCF8HFKPBVELDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--kinseyho.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3Ll3OZggKCF8HFKPBVELDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--kinseyho.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724800233; a=rsa-sha256; cv=none; b=qyd/DLtzrPKgerK5kKls5uaSoXI+qxLttg2Vp01BuDDyK7FXnN+xcsR2YCr0I4dG1svtqo Cl97od7b7Zd9Iw77On/PTmEmQTByn7uLh0w+u5t9mGwWqPtKWkHu4iZhH4t9EknJuQTcmi /r7IdZ1s+6zjdqOe9G7JB6s4fjPKuaw= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Rawjuc2t; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Ll3OZggKCF8HFKPBVELDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--kinseyho.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3Ll3OZggKCF8HFKPBVELDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--kinseyho.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724800233; 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=pVbBxbMJe3JYZfDSh84GXFqWqtxHIvJybjs9yUa4Qwg=; b=4Q6g+dGvueEFM+ERJF7IYtRexRcuENQ4zTg6vjTTcWiia3h86wsxoWA5ll3MX92urmw+No 6Nm9eFqi2opiwZVqcLkdnA/lt9IZUJzJwjdxL6HgNFtcIaUEMcLNgQahwLZkkAUWnPiExM FKMbo06FfgQNfnLtotZfa56ZKpmXea4= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7cd98f27becso5963654a12.3 for ; Tue, 27 Aug 2024 16:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724800303; x=1725405103; 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=pVbBxbMJe3JYZfDSh84GXFqWqtxHIvJybjs9yUa4Qwg=; b=Rawjuc2tfwhCKjHDsXxXBGKUmpOO8csO5hcok6hwDJkJwDkpQmJpY368y7FipAeg5R BQqs3jCRnHQbc7mR7Wtlfaibhv8ZXkyjuQk7FVR3IKSprHaB3oB+UtAqgOpFgzHjJVZP JpNmCyJVxNhgj1vVvRt46Z292P4ETKePJXbae8/60H2uDd5HKGeXhNNempAgYAbEW424 SWHgrH1ATEK04CVGC/RxukKpdOXHwM/pckZ8z5xXBsGJ76gFdR1EGlfP+fNjYb0R5uE1 sTnsLgvh6ERna2Qd/eIrEpsGl9xIBzeY+Jon4bLuy98Dv15xIMJxkZ400WkhO2CMC1hX K5QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724800303; x=1725405103; 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=pVbBxbMJe3JYZfDSh84GXFqWqtxHIvJybjs9yUa4Qwg=; b=ki/gMFpJnn3pN03P0jOZSQ+TUJtppjk4Z5uZ32zNw0dzNDNRZ2I0/Yw2NL6BWNJcEV 0teSzFwHeLI4L2CsYQG8S86cNpEF3uMH2Ze9crBUX0XZ3QN3WFyy6oW2+qW2dzyqnAt1 G56GH2Izl4PXa+RrrrrWyIB0k0Dbb21c/OohDOYLid0U94IrIDHA0rrLJz3+zfzkCwaK gxyFVvyjge30nvOPt8LjyMVMm/6XFf8PHN3syD40TxcCRPbmqQiaTuCm0cbejMP3i8mH 22mpCIxahyNRMlqNk7LRIx5wLCMsRaq+tGuNTmW0cH8kv6mBBtbnSy5qyZW+WoO7sCot 0lAA== X-Gm-Message-State: AOJu0YyRRDyn78wUrg2ZVBLE7rPCvfSYOgW2C4PBZYmFEGKWV1xSN/Aw gyR5YKSt//vLLeIimPxZMS6/wtoToLSw/cGgviVWaMgRzaRBUoOhg02yGtqqNAavNZUHKtbqap2 1q6LlCLJ5IQ== X-Google-Smtp-Source: AGHT+IFdVh/6ZDHdjnuvg4eof/eCc2TmxxMkSBRAT5EDf73D7CKtBh/Kuj5HdytE0rc9XtN+OFRBe12aA8e0gw== X-Received: from kinseyct.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:46b]) (user=kinseyho job=sendgmr) by 2002:a63:d70c:0:b0:7cb:c8c3:3811 with SMTP id 41be03b00d2f7-7d2228e5badmr309a12.5.1724800302880; Tue, 27 Aug 2024 16:11:42 -0700 (PDT) Date: Tue, 27 Aug 2024 23:07:40 +0000 In-Reply-To: <20240827230753.2073580-1-kinseyho@google.com> Mime-Version: 1.0 References: <20240827230753.2073580-1-kinseyho@google.com> X-Mailer: git-send-email 2.46.0.295.g3b9ea8a38a-goog Message-ID: <20240827230753.2073580-4-kinseyho@google.com> Subject: [PATCH mm-unstable v3 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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6A70F40022 X-Stat-Signature: m4t4wqxqpmasihytff7ddhec7fm7a5fz X-Rspam-User: X-HE-Tag: 1724800304-972282 X-HE-Meta: U2FsdGVkX1/qOq+Zv81dQCSSVwEcnij49dRzZuudchGfP0r5FKv1f5bncQW2pnXhdbXmwYC+/gO037ylYc/FpwjnDYHhDnU0FqSlmzMgqwrUgBq8wE7dy4ROQk7fd03R7caCvTirii6vKTnQU9AaRChMLJV58vyXDGKrK2RmoNeo5GJM2v0/SRTydqkw2aJwEg3CGQvuCwcv567f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COcTDa/aHXmRTaU4b6H34/bp3bCtMh4Jrsg/gVIHvg7pQvzqUhf6k6D8bpbbaotqL08FUUhxIUWuHQUYZOj33dUyf4nav46rBgNaxLOh0DisnDGAXtV/KbIwG9GAqW8SxWJDEVL8wfC2L7Q1Jg1r0s/xzvyJczYJDAxrFEph372rg7sSTkZP/b1uerTKbovtIMea/Se/04hKGlqbmccqGiRN0A+8kGhim/4FBO3t/kXwlNCi9I4lJ9oIPJoOlS68D3iZybqEQUjAow== 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 --- mm/memcontrol.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 67b1994377b7..51b194a4c375 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -997,7 +997,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; @@ -1024,14 +1024,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; } @@ -1054,8 +1046,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: