From patchwork Tue Aug 13 20:47:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kinsey Ho X-Patchwork-Id: 13762481 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 6E039C52D7B for ; Tue, 13 Aug 2024 20:48:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98CD86B008A; Tue, 13 Aug 2024 16:47:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 914B76B0092; Tue, 13 Aug 2024 16:47:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B7F96B0093; Tue, 13 Aug 2024 16:47:59 -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 56C996B008A for ; Tue, 13 Aug 2024 16:47:59 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 000A01C406D for ; Tue, 13 Aug 2024 20:47:58 +0000 (UTC) X-FDA: 82448409078.08.CFB6E1F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 4BA5940011 for ; Tue, 13 Aug 2024 20:47:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AuNXGrO8; spf=pass (imf04.hostedemail.com: domain of 3fMa7ZggKCDUbZejVpYfXffXcV.TfdcZelo-ddbmRTb.fiX@flex--kinseyho.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3fMa7ZggKCDUbZejVpYfXffXcV.TfdcZelo-ddbmRTb.fiX@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=1723581983; 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=hmvEk3aWaELcFNHaifCCcbwnogY+IyR2g5JVb5or6kw=; b=LPU2Sqtp2YtrKhdy8CfM9hF5musq+J03YFPl8YDrI24MArj945TyIE2DIcQPhGVFuEse7K Oofbu3ALawp8ukdrmK+3gUd7ZSTIWeOUi/wF+by5DDv/3/SyZizFMnyk7mcfYrNYWrCSHW YsrJnCKtzpl9KxEyrh2tv1pducH3xPI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AuNXGrO8; spf=pass (imf04.hostedemail.com: domain of 3fMa7ZggKCDUbZejVpYfXffXcV.TfdcZelo-ddbmRTb.fiX@flex--kinseyho.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3fMa7ZggKCDUbZejVpYfXffXcV.TfdcZelo-ddbmRTb.fiX@flex--kinseyho.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723581983; a=rsa-sha256; cv=none; b=NwftUoAFmcw1qVwjfMQfCkYiumTW1qbqUrjPrj1fksUjBTEggdX/dQZ6Qv9nv8Wt/cT3H0 APGsxNap7uxQwv0dVkzpct9Ym/2qlF09EvXpQKeWCiJiNJXRmFrrOWBw3DF9xqSCuKkQ5D tcGhQQAsiyOwiWuutSLJoT+M6FF+GaA= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-69a0536b23aso129522217b3.3 for ; Tue, 13 Aug 2024 13:47:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723582076; x=1724186876; 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=hmvEk3aWaELcFNHaifCCcbwnogY+IyR2g5JVb5or6kw=; b=AuNXGrO8cQ5eShkAgFv8GVsTn4f7oaqa1W4oqSSY2pU2IKBQJQ/+WWBAITI2dPr5is n/rl1F4jvQKit5yNZo/mVWf8kcRb8bo3RL1iUmBpn//bZrE6Bdv78LTqMpAPHIavYLis JCNkutTPwIYj3q31ZLEy9VhktHeP24NxmrLjRjXlgvboU4QEQrXY2iD30m+77zdqOtmu 96Ac4wA8jx+Uthx+G2YFl1kWwyO11y7kHogcdYy1FS6+bCNXtVwTgbAwUplUQzOK2C67 vBM7XWDO1tj+BWIIwohzlblM4mX1DZoIQqqxqrat3tuVzUIrDNHuC9Yoqr3oHppkdHrC UOOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723582076; x=1724186876; 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=hmvEk3aWaELcFNHaifCCcbwnogY+IyR2g5JVb5or6kw=; b=c7eP+rGtCc6kkkV2TsfcOhs/lM/w533WoQ9g4h0KTDk0JjBA5lw/686G2+gIX8w7PG FnxZ1fy+jpTrQFyjo1W6+zUjrtitxuhcOSZ2srPrd8CwPph+zLFeOwnBQwmDbhgy/Tmb pvx+UhNlt6j0t+HMpM2eASuFL0eozCFZ/VQpkoSG61DHLgegeKYx1gqGe9BqOR/un6Jx rQzRXSJFfo1AwSukJcgyDUXE7OF1BU3J3DvrGXwl2wiPkZIjNcMhywuciKq877G48kDe sAMi+dIQrX5VA0c3L/zYbEAey8NWdqLQkrdpN8+Auyf3oboqAa+L/HP6vDY52BovIi5d dx2A== X-Gm-Message-State: AOJu0YzpR5mA7Chak9mzKztqAdIWbclR8U0Lo/2qFhLa/Xz+eJmn4e05 jqwHzdyF8d14udSHkIx5UUEucbWkiXblwJ/Hjd4EADGurrSd2xFgh2HaQYau7DzuZ11T4gxUQdb YfzcDnQZ+SA== X-Google-Smtp-Source: AGHT+IHiDHCjFRbPR5+mkcQhIrf9lDLp4M5EcHeImCSP6xD1TY/GSvpgSst1fxEwRAEkFihKnMepRjSKepm4bA== X-Received: from kinseyct.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:46b]) (user=kinseyho job=sendgmr) by 2002:a25:c788:0:b0:e03:62dc:63de with SMTP id 3f1490d57ef6-e1155ada202mr39205276.6.1723582076373; Tue, 13 Aug 2024 13:47:56 -0700 (PDT) Date: Tue, 13 Aug 2024 20:47:12 +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-3-kinseyho@google.com> Subject: [PATCH mm-unstable v2 2/5] mm: don't hold css->refcnt during 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: rspam06 X-Rspamd-Queue-Id: 4BA5940011 X-Stat-Signature: pgpcjdy8eqzbds9waf75reeorihnxrim X-Rspam-User: X-HE-Tag: 1723582077-306441 X-HE-Meta: U2FsdGVkX1/ASkTHlUlIZj326KjYZT7WOEXm5UyD28BfA4R8D2Sp5hxnSTiUJ7MrVx2yZbVRGt2dnRAKl/xloFCx5g76dYRcBoPfjJ9cvoE2kcGb6Bc0W90RPr+1fv8LUGSp9/rpCg7NrRLG0tQkH80/E73imMVhSsSih0g8L3fanq4c+8BkUfbxOPf3CweHizdAUoXXkvHLMLKnl/FYsqhp/ZJ94QJDTnQQyiTWOYubdC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Aj9K+52nzYB3o/cTE+lcYrxgPb5lxytvwXWtLAyJ/LFT9H1uN81ZmF8W2o9DasRhAwt5yBupZT50QSaazZxJvmMB2qfE+Wj73h68PSmTO7d1YIJ4NmfRa28Uj8wPS8+/SS4AaM+gD9dZFwFryMOp0a9UqoW1salZNJtGvUw7o5XyfmIwOlv7TX3gwBieY1MuEPQDl/A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000008, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To obtain the pointer to the next memcg position, mem_cgroup_iter() currently holds css->refcnt during memcg traversal only to put css->refcnt at the end of the routine. This isn't necessary as an rcu_read_lock is already held throughout the function. The use of the RCU read lock with css_next_descendant_pre() guarantees that sibling linkage is safe without holding a ref on the passed-in @css. Remove css->refcnt usage during traversal by leveraging RCU. Signed-off-by: Kinsey Ho --- include/linux/memcontrol.h | 2 +- mm/memcontrol.c | 18 +----------------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 90ecd2dbca06..1aaed2f1f6ae 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -75,7 +75,7 @@ struct lruvec_stats_percpu; struct lruvec_stats; struct mem_cgroup_reclaim_iter { - struct mem_cgroup *position; + struct mem_cgroup __rcu *position; /* scan generation, increased every round-trip */ unsigned int generation; }; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index dacf4fec4541..1688aae3b1b4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1052,20 +1052,7 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, else if (reclaim->generation != iter->generation) goto out_unlock; - while (1) { - pos = READ_ONCE(iter->position); - if (!pos || css_tryget(&pos->css)) - break; - /* - * css reference reached zero, so iter->position will - * be cleared by ->css_released. However, we should not - * rely on this happening soon, because ->css_released - * is called from a work queue, and by busy-waiting we - * might block it. So we clear iter->position right - * away. - */ - (void)cmpxchg(&iter->position, pos, NULL); - } + pos = rcu_dereference(iter->position); } else if (prev) { pos = prev; } @@ -1106,9 +1093,6 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, */ (void)cmpxchg(&iter->position, pos, memcg); - if (pos) - css_put(&pos->css); - if (!memcg) iter->generation++; }