From patchwork Tue Aug 27 23:07:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kinsey Ho X-Patchwork-Id: 13780209 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 775BDC5473F for ; Tue, 27 Aug 2024 23:11:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FFF96B008A; Tue, 27 Aug 2024 19:11:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 475696B008C; Tue, 27 Aug 2024 19:11:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C5FA6B0092; Tue, 27 Aug 2024 19:11:44 -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 0B0B26B008A for ; Tue, 27 Aug 2024 19:11:44 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A6BB2A19E0 for ; Tue, 27 Aug 2024 23:11:43 +0000 (UTC) X-FDA: 82499574486.28.6C6348C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf30.hostedemail.com (Postfix) with ESMTP id EA0B18000D for ; Tue, 27 Aug 2024 23:11:41 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uhpmkNJd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3LF3OZggKCF0FDIN9TCJBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--kinseyho.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3LF3OZggKCF0FDIN9TCJBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--kinseyho.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724800282; a=rsa-sha256; cv=none; b=gZJTGDEhSNigXwRY3Mb+GnNAKXlKOR4YVCMSYRTImSmI9etF6MjyjQQNYm39I/iAAEQtw7 cd6agpIQcyQp2/Ym1jbBwpc24+9BlTKiBNmZ6+RP5vqQnxweQywWS+IL+AWHoC0r0ArxD3 aEy626a13+0sPEZtnj2+CN6wjBYtg0o= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uhpmkNJd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3LF3OZggKCF0FDIN9TCJBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--kinseyho.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3LF3OZggKCF0FDIN9TCJBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--kinseyho.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724800282; 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=QXZSgIWrSh7o4TMYH5HXuFFT6CvWnPSrTikMMv9Xu6c=; b=1mLOGo3cxKKFMN4HdJ7ZYWOkhcuyGPUwEhhjXNIaihZPlRMz/IksXSjGzLcR4G711qzbw8 lCq3IUZAaTtQ507aoRkXHVC4lg9K31vMWLjT1hGlZjnrx14TSAQrNNxGs2iFk3gfj9vLrM hRaX0EyDzLphYKSRonaMuDGwo/g6GOg= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e13c3dee397so10563661276.3 for ; Tue, 27 Aug 2024 16:11:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724800301; x=1725405101; 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=QXZSgIWrSh7o4TMYH5HXuFFT6CvWnPSrTikMMv9Xu6c=; b=uhpmkNJdxQxSLn464NZHDJg9lsVhucwRlZbwiDHGsIGpXiEQwJMs/O7A6mY8JZnMRV jZMU2tSRgW1WmYzrKSGaZ7e+kZpUAQCtuPiL87e/vnLsSgo9d7B2YrjnMajRDZdkh7EJ csFOn9f55ZgxX4VcCkto4PxTXIX3O8q5enXZr1/XfBi8ODfgi8ZOjJLpo0gAudCQrHVA Jm4ck4vXNoA107o1CMCcyETCWq6/1CZooIbkAAYS90KpQ6I83yqc3XBNp4H0pmhA8s48 9lOfBFtGxwG/MzYKEtyViE2CWlcKCp8CNwtZDXpyNo6uKB6oh7H5aVfNDRcZEo5yIXbd k4jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724800301; x=1725405101; 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=QXZSgIWrSh7o4TMYH5HXuFFT6CvWnPSrTikMMv9Xu6c=; b=GocWSEZIu5R7pTRoaTzs+vJLuzWLabV2Kquet2eDj+r/aDzPf9AIwvoeY9OT7ZXvuP mvSm2q1mb0ilwrwG5uZRmeqL+ffIzxfWcCbfeVnd86b7nMIwnuTIM5PWkYYkINPqHunj qf/ZIu6aSncsXKtk9+y2eDIpJ/UD3X1LvZ1trQwwH5MUm7/eNWIyRcl7jrXdIYHejo/B fv9wLKycThxE/+1XYfuY779Y1CCSj7Eu8ScLQ87zDQnBjNezW+lv1cUnNzguJeZaa74V ubGJwIehlMSEB3fnH0iHYOSwRbCJkETukLkoCebER06KMgYr2W4y6IAN1tf57f0/YqAc w/Eg== X-Gm-Message-State: AOJu0YwAOYAoliBtgdVHtR0JOzP+JjPvL70iV2v0YQzUCQ4nGb4BY9+k sd+qoMjWt+3FiWTA1e0YmEpKfO1VDmYnDhENMQHIqJePJ6mVHo1qERmeT6feHLIyLwpHGeUg9K7 jv2aNx0tuoA== X-Google-Smtp-Source: AGHT+IH/OEMHPdWupnXyjW/u5NZJ+Iwna53tKUi6hE4QApyk+GLsJI4xuXktR0o4bdU85PWSweUUV8sgsDuT4w== X-Received: from kinseyct.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:46b]) (user=kinseyho job=sendgmr) by 2002:a25:abad:0:b0:e11:69f2:e39 with SMTP id 3f1490d57ef6-e1a4580c518mr380276.9.1724800300879; Tue, 27 Aug 2024 16:11:40 -0700 (PDT) Date: Tue, 27 Aug 2024 23:07:39 +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-3-kinseyho@google.com> Subject: [PATCH mm-unstable v3 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-Rspam-User: X-Rspamd-Queue-Id: EA0B18000D X-Rspamd-Server: rspam01 X-Stat-Signature: ebqi4fg58iuixpco8cs6njimmyd4bh85 X-HE-Tag: 1724800301-824657 X-HE-Meta: U2FsdGVkX18hQyONVPh3pYXtszZEHWkSH2ZkXOcgBhEm7xd9SMWdbcKxWtXcZ5+eyqYpkQzEhAQrwi5Xj4+zj26PXXU6izloNfKlM0tDS9jzSSEnJBofyiKgZ269z2Z8LkdX/Gwitw5m+MSfQaJgVLvo071+p2pfkd9MYmMO5cKo/ziROPo3+ITgyXk9Tqz0xtd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PhPJHo1o8CpbKlRLAU8RratBQuUQcBoAR9Jx0AzJ8dgHIoYs6hKvpOSUx+/UR7CgkK0CPmIzHf+lmo12bXrZ05wxMzeGfOoyeb6TWloXwO12fyAa0tBCCux9XYqynkVpoomwXi087nRjrdGIDQLIuD4iCrWLYlrI1RRe6tujhUMpC+e+4fGQgDH2fPJLcpT/5sOtHSh2Q4hHPrmjt3RcR9zV6UOQdNIQVz6ktk+N5p8ncRY1y4qo/qD3FroXy5dC49leEos6aFkGsKIYyv0rAAaii0tvkHDeg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Reviewed-by: T.J. Mercier --- mm/memcontrol.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 35431035e782..67b1994377b7 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1013,20 +1013,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 = READ_ONCE(iter->position); } else if (prev) { pos = prev; } @@ -1067,9 +1054,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++; }