From mboxrd@z Thu Jan 1 00:00:00 1970 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 6F5E4C3DA63 for ; Wed, 24 Jul 2024 22:38:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B89C36B0085; Wed, 24 Jul 2024 18:38:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B39E06B0088; Wed, 24 Jul 2024 18:38:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A27D36B0089; Wed, 24 Jul 2024 18:38:51 -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 849CC6B0085 for ; Wed, 24 Jul 2024 18:38:51 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id ECD33A3F05 for ; Wed, 24 Jul 2024 22:38:50 +0000 (UTC) X-FDA: 82376112420.24.4302453 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) by imf17.hostedemail.com (Postfix) with ESMTP id 3642E4001E for ; Wed, 24 Jul 2024 22:38:48 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rzRNgrUt; spf=pass (imf17.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.181 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721860665; 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=Fa12xhcwGYWhKFU8YlB18e+bkbQbKpTyxv5JolLajyI=; b=OPWn1/eBEzmuWiyGAvorbvTuk+5AUdqCHD4reC1fWbZyEaSzjdTlBchjkqWJq2OzCvtj03 fp27E6wztFqmCdEtb0gNdjvBTaYXYtnsv/J+nO21rzf1QLIthqCUfWh0j94L7FQbAjBH1H RBY70ruwlM4fOd08WCMK7n9DaTFmAug= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rzRNgrUt; spf=pass (imf17.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.218.175.181 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721860665; a=rsa-sha256; cv=none; b=Kh/BQ2WUQSCPcGJv6jeNgG23kPcy96vhv1TUU9IenOsD62slTLOQcUMG9raZUlBILTmzIq mVLqtUSYnsh0J0jGw/n5YyAdfZQNgAkb7nmfh9tUUVE8lJARi1mzG+gScvbXcQLObq+WNr HEXr4zUwT1gByrv62I7Ccz4PChS4tnw= Date: Wed, 24 Jul 2024 22:38:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1721860727; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Fa12xhcwGYWhKFU8YlB18e+bkbQbKpTyxv5JolLajyI=; b=rzRNgrUta71PEusaTqtjES9ZeQ2BsHFx5T0w/qmy0FzuUvsUQrlE1gMAhDgUM1RdPfbsGa eio+MV6v96WLahF445gldpaoeRQz6TJdZNQZvQEDSmP5cSJ5bHpaPiXadDBDbNsHfRU+df AAmuKB+h+0dUE702S4EynQXbJLeF23Q= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: "Vlastimil Babka (SUSE)" Cc: Muchun Song , hannes@cmpxchg.org, mhocko@kernel.org, shakeel.butt@linux.dev, muchun.song@linux.dev, akpm@linux-foundation.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm: kmem: add lockdep assertion to obj_cgroup_memcg Message-ID: References: <20240724095307.81264-1-songmuchun@bytedance.com> <610dc6fa-6681-4c9e-bffb-ef6d299dd169@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <610dc6fa-6681-4c9e-bffb-ef6d299dd169@kernel.org> X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3642E4001E X-Stat-Signature: m5szuzoqdwfz9pc8ojjkrkyoo6pz6pr4 X-Rspam-User: X-HE-Tag: 1721860728-362217 X-HE-Meta: U2FsdGVkX18Clr7fKNWf4p425AFKAPqspBlNHx2bMD4U7lGLBG+gLYse2Ssco+JxGor7G2WK4q7Lfj0GaLhbnKQQUhokEI5qTAGZ08mAoGdIpqQ8Avj1oXCCccLXedx5ioVaSHbUSLRQx11ZSWT3+0z/Op0swEQ0cTmv/ggwUcw4vXRwrVna2vEy6udv+N4jFGJ8TiRflJ0N9pjjj3rSpsWWcBI/w0Mr10y3GUHTtcxTUxd8ytxqBaCaGbWH86rdsPng5n7X8nYUcUvHXtkRe9uzZg5tUSN/K9rwX3LHffmi/XD167MJAJrZzdNoey+CI5M3fGoR54ZNR0jVYCjOmd3TwIhZYmenXcXrtOHmDCY/F9TWh1hvSCvyvr2PKi+JPQe82oegjyxH054gBdnWYhjepbx/p52443ea5DkZ7uCI/39zbgJVjq4dg8RqADzuPxGYWP8POojWjNj+FMTG+V6FPz1w5aCk7snsEGnuj/5eVAYkDeDbXIUqmKyR811tc+Bw0V9P3uQwHcZXjMJICebet5sq8Hac3C6/vgyfq/XhuVMw2tMhjmI2EGekRY1kEy8Imx47eKdkIgWkiDH/msZrlLqagZ+81fnnf6XA24VF1oF/sHBa4bcSULkmqt4o+rfCtUFLEGjELRAko3zi3mG0wo1T8n/w/U1LzFVugh/zUzJzt5xdxV68Uip+Ii+GC+R4naa7cOv9IRNuwpp20gKtzarJD6NJg7OvTD5O3WqPO/oxlHPdlVfs9xOY6P2Sb708xZEKB3PObjxhKvfuiLyp4uYFV9tBFTSU51rc7Y/XGP/ljRO7MK2fKTY+ZhIgGGflfD5rgqnvTr9RNdS5S0jCIBIICdh/SeHsJZEwuL1SoAM7QL4ip4R+hdokICk77QJrALjkLnQ9ZWTQMtj/Pl+XU866nw1VnE57W5USqGctZu6K0Xz1X2uvlMtS1IOsxhr2J1Grx9F5kTmNeYM VPm6K7MZ v8KCnMw5k+0+hp8BPZjvnJZ441uKrLvrQnBOEQyjg3klZYGrIC/kTj8bzdL2dH2N2Y4W315PReFQ+bbe/ILRoomaoUNEXhEhUBF0LyFgaZ8aC6KcyzJKTe9L905+GKaoyKd+s8YDO2Z48Ta7pRZpe/MoJ+KcjkXlSY1hdduaKwwRx14skIog6l0k6qnjDiAZidjPTLg/kMZXEW8d3cbaocmwrvVCaaJ3/C7tb+cRt4rDPxKhqgJ9WKH6QYag1BbTSrvWyeCvM1Xqlvs76+CD6QXBWb8aZNE2a4OOh5QuwW6GNv7f8tD8A4ZdHfALIVJ/R3BX7RXlaDOaxG/Q= 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: On Wed, Jul 24, 2024 at 05:20:09PM +0200, Vlastimil Babka (SUSE) wrote: > On 7/24/24 11:53 AM, Muchun Song wrote: > > The obj_cgroup_memcg() is supposed to safe to prevent the returned > > memory cgroup from being freed only when the caller is holding the > > rcu read lock or objcg_lock or cgroup_mutex. It is very easy to > > ignore thoes conditions when users call some upper APIs which call > > obj_cgroup_memcg() internally like mem_cgroup_from_slab_obj() (See > > the link below). So it is better to add lockdep assertion to > > obj_cgroup_memcg() to find those issues ASAP. > > > > Because there is no user of obj_cgroup_memcg() holding objcg_lock > > to make the returned memory cgroup safe, do not add objcg_lock > > assertion (We should export objcg_lock if we really want to do). > > Additionally, this is some internal implementation detail of memcg > > and should not be accessible outside memcg code. > > > > Some users like __mem_cgroup_uncharge() do not care the lifetime > > of the returned memory cgroup, which just want to know if the > > folio is charged to a memory cgroup, therefore, they do not need > > to hold the needed locks. In which case, introduce a new helper > > folio_memcg_charged() to do this. Compare it to folio_memcg(), it > > could eliminate a memory access of objcg->memcg for kmem, actually, > > a really small gain. > > > > Link: https://lore.kernel.org/all/20240718083607.42068-1-songmuchun@bytedance.com/ > > Signed-off-by: Muchun Song > > --- > > v2: > > - Remove mention of objcg_lock in obj_cgroup_memcg()(Shakeel Butt). > > > > include/linux/memcontrol.h | 20 +++++++++++++++++--- > > mm/memcontrol.c | 6 +++--- > > 2 files changed, 20 insertions(+), 6 deletions(-) > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > index fc94879db4dff..742351945f683 100644 > > --- a/include/linux/memcontrol.h > > +++ b/include/linux/memcontrol.h > > @@ -360,11 +360,11 @@ static inline bool folio_memcg_kmem(struct folio *folio); > > * After the initialization objcg->memcg is always pointing at > > * a valid memcg, but can be atomically swapped to the parent memcg. > > * > > - * The caller must ensure that the returned memcg won't be released: > > - * e.g. acquire the rcu_read_lock or css_set_lock. > > + * The caller must ensure that the returned memcg won't be released. > > */ > > static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg) > > { > > + WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_is_held(&cgroup_mutex)); > > Maybe lockdep_assert_once() would be a better fit? 100%.