From patchwork Fri Sep 2 02:30:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12963570 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 16758C6FA82 for ; Fri, 2 Sep 2022 02:30:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F65F800A1; Thu, 1 Sep 2022 22:30:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A6148008D; Thu, 1 Sep 2022 22:30:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7FA66800A1; Thu, 1 Sep 2022 22:30:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 6A0BE8008D for ; Thu, 1 Sep 2022 22:30:33 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 377F61A1167 for ; Fri, 2 Sep 2022 02:30:33 +0000 (UTC) X-FDA: 79865566746.14.2B611C2 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf03.hostedemail.com (Postfix) with ESMTP id E57EA2004D for ; Fri, 2 Sep 2022 02:30:32 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id 78so731409pgb.13 for ; Thu, 01 Sep 2022 19:30:32 -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:subject:date; bh=oTzSf85iWJHNEVVdQJQawTYzDnCEuMbWuvl4YMwFJHo=; b=HpH5nbR7f38apaxWi0KaD3SeNAKzaoXicey2rg/miPioqCgeCWVOK43hCtIftBuo1N QbqGHEF6XUCu0BCMIRlFwsthhpD00GqnpDf3e3iIVAx6t7zqW0H7lguDqeHmf9F9kjxZ S6VeDMuodddj2nFJ9QdZANE0ynOkPTs8CSWtXQtVPUlS/WeUrrRH8JcbisME3QJuXhjN PBriHpMPWSoNwoECmcNJcBgaMUza/8shHez4l6SJ+A6QdqVQeyaccN2u0IE0CWhjJnBn +56WGiPw4jZ5f7N+AU/OOQCp50Ok1xUJm48Hr0097jDlSOabNoWqfp7K4Ct7NsN5+B+u K5ZQ== 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 :subject:date; bh=oTzSf85iWJHNEVVdQJQawTYzDnCEuMbWuvl4YMwFJHo=; b=o1BqWuSj764FxuRiSNGuqPtE07/J+bJqE2IgysoX8UXateOWAr69vj1aUCZcMjBGkR g1OEA6ekECucZLZGhixvMc1PaTTOq7R8iKPrvaXAOEI4X4YFG6kCg9pE296imJ4qG0Jn 7o6I5j+DdAnUug8EA5ON3UDFrrP8Ul9VV0nyXioo3fKcSo9H412qIFBlFm/0ZWav5w1n KG9EnIoHqC0Q9az2JUfLwzQW4n4d9TXxBy0WgLshuJC/3m7CI1J10eMPdEGeavKgASsg ocrdeB2V0k3/BL0d5WITlDxwpna15FvCIu2Qrge4HAoQEJMYmYpbY4JUAOPdZmRlcR13 9EbQ== X-Gm-Message-State: ACgBeo3LPTRuEvxcNo5IBOPfW4/uc7n1aEcNSDRkHVzICQo/ZfuHp854 EYnzJV4m/XLkS7n/csCjNxA= X-Google-Smtp-Source: AA6agR7Ygx377TAYolYTUSgnk0C2dXozijEaoUr+0SdI+Cp29mIWbr48/yrAJDnYVJ/ugfGgDTLCgQ== X-Received: by 2002:a65:6047:0:b0:42b:313e:d331 with SMTP id a7-20020a656047000000b0042b313ed331mr29312059pgp.179.1662085832022; Thu, 01 Sep 2022 19:30:32 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:50ea:5400:4ff:fe1f:fbe2]) by smtp.gmail.com with ESMTPSA id j4-20020a170902da8400b0017297a6b39dsm269719plx.265.2022.09.01.19.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 19:30:31 -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 v3 10/13] mm, memcg: Add new helper get_obj_cgroup_from_cgroup Date: Fri, 2 Sep 2022 02:30:00 +0000 Message-Id: <20220902023003.47124-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220902023003.47124-1-laoar.shao@gmail.com> References: <20220902023003.47124-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662085832; a=rsa-sha256; cv=none; b=pKnlmaoqYfKLqm68Z0urKhynz1lvxsPxcj3cdbm2WstPS/beL8zmL1jn+RYN4uJXsdu6vh fbJl0jJTmp8zs0k2rOPhl3k/5jFUCJhTHI5yLpOJzXGqejSFa/+A50GWpA4L/CJZfBa2F1 mKTnmRbLfdOcvGO0IgeXLix7OqorDM8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=HpH5nbR7; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662085832; 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=oTzSf85iWJHNEVVdQJQawTYzDnCEuMbWuvl4YMwFJHo=; b=8K0O6/JzTn4BH4BAJeYPtfZvXpIYIbwiCuIgOtiVOVxLU0tMn6vndpzl1njT7H/2KlDAAM PMc76iZZFTeKrqdozeMbWOHYO/IWj/gNLwswef7TeQBv4S6rgWAm/iOsJJgjvZI6uS/ySZ Sf4cJ4l0lmtUbK8Uz+ew85wU6WR65fI= X-Rspam-User: X-Stat-Signature: cfteaqkka4fa9e6o9q6bjzqgwr81d8u6 X-Rspamd-Queue-Id: E57EA2004D X-Rspamd-Server: rspam06 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=HpH5nbR7; spf=pass (imf03.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1662085832-669633 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. Signed-off-by: Yafang Shao --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 6040b5c..7a7f252 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1734,6 +1734,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 b69979c..4e3b51e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2940,6 +2940,7 @@ static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) { struct obj_cgroup *objcg = NULL; + WARN_ON_ONCE(!rcu_read_lock_held()); for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { objcg = rcu_dereference(memcg->objcg); if (objcg && obj_cgroup_tryget(objcg)) @@ -2949,6 +2950,53 @@ 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; +} + +/** + * get_obj_cgroup_from_cgroup: Obtain a reference on given cgroup's objcg. + * @cgrp: cgroup from which objcg should be extracted. It can't be NULL. + * The memory subsystem of this cgroup must be enabled, otherwise + * -EINVAL will be returned. + * On success, the objcg will be returned. + * On failure, -EINVAL will be returned. + */ +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(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;