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 CEB88C19F2D for ; Sat, 13 Aug 2022 18:30:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 509B36B0075; Sat, 13 Aug 2022 14:30:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B9226B0078; Sat, 13 Aug 2022 14:30:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 359948E0001; Sat, 13 Aug 2022 14:30:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 222C36B0075 for ; Sat, 13 Aug 2022 14:30:12 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id F00E4805CD for ; Sat, 13 Aug 2022 18:30:11 +0000 (UTC) X-FDA: 79795409022.01.1CF238A Received: from out1.migadu.com (out1.migadu.com [91.121.223.63]) by imf24.hostedemail.com (Postfix) with ESMTP id 7060E1801B3 for ; Sat, 13 Aug 2022 18:30:11 +0000 (UTC) Date: Sat, 13 Aug 2022 11:30:04 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1660415410; 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=E63Q1bPVq427Ovtaj+mb5uXGGsmTNyBJBlzhvoLTwWg=; b=XdvYQabcYG06stMpL7taZTyyG5P1TGqdnpJqO/xobn+zdaM0gHjKmJS1RiR8q8k7WW5y/z MWC+HwqyGdQgITH4nwfv+7w09IKR7j1EkyVrU+LSEsH2O7ic770M27T3OEBvmLVqryM345 2y46qaqEmEAroYsj4l2Fw6+DPT61iSw= 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 , Stanislav Fomichev , Hao Luo , jolsa@kernel.org, Johannes Weiner , Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , netdev , bpf , Linux MM Subject: Re: [PATCH bpf-next 13/15] mm, memcg: Add new helper get_obj_cgroup_from_cgroup Message-ID: References: <20220810151840.16394-1-laoar.shao@gmail.com> <20220810151840.16394-14-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-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=XdvYQabc; spf=pass (imf24.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.121.223.63 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=1660415411; a=rsa-sha256; cv=none; b=gqHTEhPPOeyMZLFiqtqcpuIOGzqZ2G2kkeCo/rLgknkfeD6pf4sPVTfaM/TGwOZ8uE8ji/ fH+hdJuZDu59LVY1t4N7VX+u6MPc7Kf+6wHQicjZJitQZ2Rsj9qbN42K7lkz5iBoWoOLdH WGKlPryIeIc6c3+BbyNcyJOByqeCSEs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660415411; 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=E63Q1bPVq427Ovtaj+mb5uXGGsmTNyBJBlzhvoLTwWg=; b=Yan5PMH7/4RdpUafHOBQI1hSNKm2SJuVf3mLEyN7L86dOkLsEXhF8BhdJF70g6pX8uSSdc 0PeyeBGgbTGDyLJeClR3QTAnf7PpHfacG6YjzTvAREPhL8gzOi8PrQlSOxf1XCpyEke//n dPX8U8omBA7fOZ7CxXqQWgsjyCCm4i0= Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=XdvYQabc; spf=pass (imf24.hostedemail.com: domain of roman.gushchin@linux.dev designates 91.121.223.63 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7060E1801B3 X-Stat-Signature: 74ee15sozcuxy8z4itcnte8pfiqpkgns X-Rspam-User: X-HE-Tag: 1660415411-38572 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, Aug 13, 2022 at 07:56:54AM +0800, Yafang Shao wrote: > On Sat, Aug 13, 2022 at 1:40 AM Roman Gushchin wrote: > > > > On Fri, Aug 12, 2022 at 08:35:19AM +0800, Yafang Shao wrote: > > > On Fri, Aug 12, 2022 at 12:16 AM Roman Gushchin > > > wrote: > > > > > > > > On Wed, Aug 10, 2022 at 03:18:38PM +0000, Yafang Shao wrote: > > > > > Introduce new helper get_obj_cgroup_from_cgroup() to get obj_cgroup from > > > > > a specific cgroup. > > > > > > > > > > Signed-off-by: Yafang Shao > > > > > --- > > > > > include/linux/memcontrol.h | 1 + > > > > > mm/memcontrol.c | 41 +++++++++++++++++++++++++++++++++++++++++ > > > > > 2 files changed, 42 insertions(+) > > > > > > > > > > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > > > > > index 2f0a611..901a921 100644 > > > > > --- a/include/linux/memcontrol.h > > > > > +++ b/include/linux/memcontrol.h > > > > > @@ -1713,6 +1713,7 @@ static inline void set_shrinker_bit(struct mem_cgroup *memcg, > > > > > 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 *get_obj_cgroup_from_cgroup(struct cgroup *cgrp); > > > > > 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 618c366..762cffa 100644 > > > > > --- a/mm/memcontrol.c > > > > > +++ b/mm/memcontrol.c > > > > > @@ -2908,6 +2908,47 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > return objcg; > > > > > } > > > > > > > > > > +static struct obj_cgroup *get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) > > > > > +{ > > > > > + struct obj_cgroup *objcg; > > > > > + > > > > > + if (memcg_kmem_bypass()) > > > > > + return NULL; > > > > > + > > > > > + rcu_read_lock(); > > > > > + objcg = __get_obj_cgroup_from_memcg(memcg); > > > > > + rcu_read_unlock(); > > > > > + return objcg; > > > > > > > > This code doesn't make sense to me. What does rcu read lock protect here? > > > > > > To protect rcu_dereference(memcg->objcg);. > > > Doesn't it need the read rcu lock ? > > > > No, it's not how rcu works. Please, take a look at the docs here: > > https://docs.kernel.org/RCU/whatisRCU.html#whatisrcu . > > In particular, it describes this specific case very well. > > > > In 2 words, you don't protect the rcu_dereference() call, you protect the pointer > > I just copied and pasted rcu_dereference(memcg->objcg) there to make it clear. > Actually it protects memcg->objcg, doesn't it ? > > > you get, cause it's valid only inside the rcu read section. After rcu_read_unlock() > > it might point at a random data, because the protected object can be already freed. > > > > Are you sure? > Can't the obj_cgroup_tryget(objcg) prevent it from being freed ? Ok, now I see where it comes from. You copy-pasted it from get_obj_cgroup_from_current()? There rcu read lock section protects memcg, not objcg. In your case you don't need it, because memcg is passed as a parameter to the function, so it's the duty of the caller to ensure the lifetime of memcg. Thanks!