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 39ED6C433EF for ; Sun, 26 Jun 2022 01:52:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A738F6B0071; Sat, 25 Jun 2022 21:52:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FB066B0072; Sat, 25 Jun 2022 21:52:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89D066B0073; Sat, 25 Jun 2022 21:52:15 -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 77F9F6B0071 for ; Sat, 25 Jun 2022 21:52:15 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 496B233BD0 for ; Sun, 26 Jun 2022 01:52:15 +0000 (UTC) X-FDA: 79618711830.23.20F052B Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by imf18.hostedemail.com (Postfix) with ESMTP id 747D71C002B for ; Sun, 26 Jun 2022 01:52:14 +0000 (UTC) Date: Sat, 25 Jun 2022 18:52:05 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1656208333; 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=Bx3abD8MY8AFNkt9IMrGyI8BpvZiqLUJO1kMPiRlUrc=; b=mZ9bljj+Q9GANyah9tPGURJT0uBGi3FRPJSOYvX2zXYmqxGfO14xlRc8JV8SQNpAY0hQ0Z pU9MfKOCIInMClWkmaIUxKDe7bHmpzZmB54cclnlPVyclbo+haMl/LUYLAtYaxlN32XVR4 PxbH1S8vpBHzN6hRcsDSKIblSHYqOfg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Yafang Shao Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , Quentin Monnet , Johannes Weiner , Michal Hocko , Shakeel Butt , songmuchun@bytedance.com, Andrew Morton , Christoph Lameter , penberg@kernel.org, David Rientjes , iamjoonsoo.kim@lge.com, Vlastimil Babka , Linux MM , bpf Subject: Re: [RFC PATCH bpf-next 03/10] mm, memcg: Add new helper obj_cgroup_from_current() Message-ID: References: <20220619155032.32515-1-laoar.shao@gmail.com> <20220619155032.32515-4-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656208335; a=rsa-sha256; cv=none; b=FLB7w0TcSBwbRMGevNMLkjAE19Pmxpx88SCg60QainGQIkHi2e2CeeS6SKOg62Z5UgDbB4 MC1TFNwd+ZE4fzvzHRwF767Wde3/1s/inhdb+l5oxMNRo6K3iRfcYIQYfY/M5M1FVHwPmh O1xUyVDJdb3b5ABRn/UwwttWGqNaiRQ= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mZ9bljj+; spf=pass (imf18.hostedemail.com: domain of roman.gushchin@linux.dev designates 188.165.223.204 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=1656208335; 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=Bx3abD8MY8AFNkt9IMrGyI8BpvZiqLUJO1kMPiRlUrc=; b=nvMlzPUqNJWXv5Lr0Il4tx/g1svyAgLFMeV1GR7t7NE0et20CqivVgNHoXZt3Oiu2DExXa Bt2kdfeyvfKItAEf8x5dk47rSCOuuvL8DkCHSDvFrb8EVHWf5DA+BihaZ60poY2JSdPaJj xc4bVenpFQgfe5t6V8FlUP1d+GxNLAk= X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 747D71C002B Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mZ9bljj+; spf=pass (imf18.hostedemail.com: domain of roman.gushchin@linux.dev designates 188.165.223.204 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Stat-Signature: eqxdxdebdkgfzhk6anfswam54rwxw4ij X-HE-Tag: 1656208334-91519 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: On Sat, Jun 25, 2022 at 09:54:17PM +0800, Yafang Shao wrote: > On Thu, Jun 23, 2022 at 11:01 AM Roman Gushchin > wrote: > > > > On Sun, Jun 19, 2022 at 03:50:25PM +0000, Yafang Shao wrote: > > > The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() > > > is that the later one doesn't add objcg's refcnt. > > > > > > Signed-off-by: Yafang Shao > > > --- > > > include/linux/memcontrol.h | 1 + > > > mm/memcontrol.c | 24 ++++++++++++++++++++++++ > > > 2 files changed, 25 insertions(+) > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > index cf074156c6ac..402b42670bcd 100644 > > > --- a/include/linux/memcontrol.h > > > +++ b/include/linux/memcontrol.h > > > @@ -1703,6 +1703,7 @@ bool mem_cgroup_kmem_disabled(void); > > > int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order); > > > void __memcg_kmem_uncharge_page(struct page *page, int order); > > > > > > +struct obj_cgroup *obj_cgroup_from_current(void); > > > struct obj_cgroup *get_obj_cgroup_from_current(void); > > > struct obj_cgroup *get_obj_cgroup_from_page(struct page *page); > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > index abec50f31fe6..350a7849dac3 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) > > > return objcg; > > > } > > > > > > +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) > > > +{ > > > + struct obj_cgroup *objcg = NULL; > > > + struct mem_cgroup *memcg; > > > + > > > + if (memcg_kmem_bypass()) > > > + return NULL; > > > + > > > + rcu_read_lock(); > > > + if (unlikely(active_memcg())) > > > + memcg = active_memcg(); > > > + else > > > + memcg = mem_cgroup_from_task(current); > > > + > > > + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { > > > + objcg = rcu_dereference(memcg->objcg); > > > + if (objcg) > > > + break; > > > + } > > > + rcu_read_unlock(); > > > > Hm, what prevents the objcg from being released here? Under which conditions > > it's safe to call it? > > obj_cgroup_from_current() is used when we know the objcg's refcnt has > already been incremented. > For example in my case, it is called after we have already call get_ > parent_mem_cgroup(). > I should add a comment or a WARN_ON() in this function. Yes, it's very confusing, please add a big comment explaining under which conditions it's safe to call it. Thanks!