From patchwork Thu Aug 18 14:31:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12947994 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 5F7C6C00140 for ; Thu, 18 Aug 2022 21:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA1FA8D0002; Thu, 18 Aug 2022 17:32:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E4FEF8D0001; Thu, 18 Aug 2022 17:32:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF09D8D0002; Thu, 18 Aug 2022 17:32:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BCE718D0001 for ; Thu, 18 Aug 2022 17:32:35 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 958E7140671 for ; Thu, 18 Aug 2022 21:32:35 +0000 (UTC) X-FDA: 79814012670.18.8EDFF17 Received: by imf24.hostedemail.com (Postfix, from userid 200) id F0B2618003D; Thu, 18 Aug 2022 15:43:04 +0000 (UTC) Received: from mail-il1-f173.google.com (mail-il1-f173.google.com [209.85.166.173]) by imf24.hostedemail.com (Postfix) with ESMTP id 02A24180719 for ; Thu, 18 Aug 2022 15:42:57 +0000 (UTC) Received: by mail-il1-f173.google.com with SMTP id l15so996480ilf.4 for ; Thu, 18 Aug 2022 08:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=SdEsGdLtUTnGTQlmpkcqbLnnsMN1enZs7HVXG4Feokc=; b=iM7+xYE3ZpaZKh2j/9NZNfU6WfXX1qnhHz90ontLLo/0kh1EMI2GgT3hsaLXjYuqB+ Bc+3EgV3QlYwmbQSgP6DF4xyLZM3XeV8u8WuYlvq9gCHoYixiOEju6GCoB9t8bIaV/h0 8FIPglxdIU0iKsIttK5lqBYDQPvSCT6m7EJ3UB3fKymtdyT2oCkbs3nW8AuR5HmU6c4U plqqyHQwuiSWQJN/q+zwHtXRUvp4HuPyDXIB+i0qM1xVXS6Uwz7nDD7ePd9S4T3uG5Lr XMmyq9x38HyRsbixPhyelgKdWfrTLNyNj+YPuYZOVsEtiDUSthZTWGolXni2ZmEiLLAD tSiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=SdEsGdLtUTnGTQlmpkcqbLnnsMN1enZs7HVXG4Feokc=; b=cFqSg8BeDf/pEhwieFQ9gj3MZQ3SiExyQoz45X4zWKHLnwk3+EQwNMkJhtRU7kpCBA 3D0wNDWQOP3BaW9LtcYjw7qHlWU/jP9wygXEWy2bGmTM3W8gL0U8/xdLlmSWHGSgIj6F mgl8IsOZG+R2q39YrEx09EoWCEQrokHpBdc6j4H9RKW4C3QQUYmLmPZJm+pev95dlCrB vgruQRacFDDjpLPwBmY2eIu+s6puhsLQBWyEjcAFsDCOFd7jIFlic5wAzZr7N7iTpjhv a/OCDaQ3cltRNq7ji5YMU+2kFUBrJpoKHy4xNAedsF33DUT5psSET/gUkPZPGoSwREZ+ e7Ag== X-Gm-Message-State: ACgBeo3gctEeGEfctjsaaAwvNwwthdaIbkameBeZYn9eojyyX6js8hO1 T+nZcdi4oQNA38AsPbloo+mYHtYSOWVv2AVn X-Google-Smtp-Source: AA6agR6tGbyVsMQyI/lXKdwbyTP8XOf52SpT7zfV868FB9HDcvT6Z5ZuEY1X02mmUFcf4qavA1RCrg== X-Received: by 2002:a63:2208:0:b0:429:9444:85be with SMTP id i8-20020a632208000000b00429944485bemr2660287pgi.236.1660833128367; Thu, 18 Aug 2022 07:32:08 -0700 (PDT) Received: from vultr.guest ([45.32.72.237]) by smtp.gmail.com with ESMTPSA id h5-20020a63f905000000b003fdc16f5de2sm1379124pgi.15.2022.08.18.07.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 07:32:07 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org, tj@kernel.org, lizefan.x@bytedance.com Cc: cgroups@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next v2 10/12] mm, memcg: Add new helper get_obj_cgroup_from_cgroup Date: Thu, 18 Aug 2022 14:31:16 +0000 Message-Id: <20220818143118.17733-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220818143118.17733-1-laoar.shao@gmail.com> References: <20220818143118.17733-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660837383; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SdEsGdLtUTnGTQlmpkcqbLnnsMN1enZs7HVXG4Feokc=; b=qJmL93NnL5zX5lSKoB1Btx7WQImdI1yTsSCL5e+9wi63/cWC0vKvMZ3JeB73zoQyfBAfkT b4OQVDvqhY+bTGxaIAYCUP36DCZPqVOwwyczpZfxpujN9S/GjXUKvANWCPfAlVqBoj68N+ /ZfRXP8yuyVVxT6FFI15jhS+fBBRWXk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=iM7+xYE3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660837383; a=rsa-sha256; cv=none; b=ViNqkvz/7ysxxa5kOMRP6cN34Xie0Z5bW9J7Iv7zsDJpZ75DtMu8TFrxf4MXu0Nla7gYHd X4SFD1zWniLNnROgWZ8xOoYBoOhY8skr97a/VnIaPopWm3KOa2+euKXSYa6Do//zZjA4bf DX48VntpDyOGKXgL6PRQPBfqZLVlXHU= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 02A24180719 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=iM7+xYE3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspam-User: X-Stat-Signature: roaius3h58qqanuizty8onar8fmhw1zy X-HE-Tag: 1660837377-729775 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: We want to open a cgroup directory and pass the fd into kernel, and then in the kernel we can charge the allocated memory into the open cgroup if it has valid memory subsystem. In the bpf subsystem, the opened cgroup will be store as a struct obj_cgroup pointer, so a new helper get_obj_cgroup_from_cgroup() is introduced. It also add a comment on why the helper __get_obj_cgroup_from_memcg() must be protected by rcu read lock. Signed-off-by: Yafang Shao --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 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..0409cc4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2895,6 +2895,14 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p) return page_memcg_check(folio_page(folio, 0)); } +/* + * Pls. note that the memg->objcg can be freed after it is deferenced, + * that can happen when the memcg is changed from online to offline. + * So this helper must be protected by read rcu lock. + * + * After obj_cgroup_tryget(), it is safe to use the objcg outside of the rcu + * read-side critical section. + */ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) { struct obj_cgroup *objcg = NULL; @@ -2908,6 +2916,45 @@ 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; +} + +struct obj_cgroup *get_obj_cgroup_from_cgroup(struct cgroup *cgrp) +{ + struct cgroup_subsys_state *css; + struct mem_cgroup *memcg; + struct obj_cgroup *objcg; + + rcu_read_lock(); + css = rcu_dereference(cgrp->subsys[memory_cgrp_id]); + if (!css || !css_tryget_online(css)) { + rcu_read_unlock(); + return ERR_PTR(-EINVAL); + } + rcu_read_unlock(); + + memcg = mem_cgroup_from_css(css); + if (!memcg) { + css_put(css); + return ERR_PTR(-EINVAL); + } + + objcg = get_obj_cgroup_from_memcg(memcg); + css_put(css); + + return objcg; +} + __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) { struct obj_cgroup *objcg = NULL;