From patchwork Wed Aug 10 15:18:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12940672 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 C33E9C00140 for ; Wed, 10 Aug 2022 15:19:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 598506B0075; Wed, 10 Aug 2022 11:19:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 520E06B0080; Wed, 10 Aug 2022 11:19:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39DB08E0001; Wed, 10 Aug 2022 11:19:11 -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 26E226B0075 for ; Wed, 10 Aug 2022 11:19:11 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ED5B38057B for ; Wed, 10 Aug 2022 15:19:10 +0000 (UTC) X-FDA: 79784041260.18.921DBDC Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf07.hostedemail.com (Postfix) with ESMTP id 9D33740177 for ; Wed, 10 Aug 2022 15:19:10 +0000 (UTC) Received: by mail-pg1-f174.google.com with SMTP id q16so14623364pgq.6 for ; Wed, 10 Aug 2022 08:19:10 -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=Clyu36Jre2FnqWFd0IUB464wOplrravQ4IfQGIbyf7A=; b=YEil+EOUwJvQIvFBvsL8BPyBTpmCr/QRfJvhndnQRuC2ZQ0+oQiSl9+jCjuJsBhbCT t9ZCx1gUCzZYRJYYHoPNdbmW/+0irKUw1pjI7jNKTtBINlmPaSWFy91p5RHjUFl6XNgM G1xgYpU4pkXPTphUNnsdL/jS18recBewUX0nzUnJ39ufiGnGv3a8FxQFoEMVMyp3kgSa 8T/+jAcFoP51aFusdVra1eIzo7qi0alJ1JstrFTAj/ESY+9O6OVrVAGYqLnrtB4TLz0C DfbqqKV2N0K862CAJDiK+bNn0Q5z9kegp3Ck5xrlgC3xb4U2WxfDoDWCvmvJf39os0HE xeww== 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=Clyu36Jre2FnqWFd0IUB464wOplrravQ4IfQGIbyf7A=; b=nwfu1+TKL7L5Qd+8vEDqVfjQ5iVnJp14dslmuNt+t//B7MKjYrkReYOznYhgGvETlw Po/+GJu+oiyR5m7F9Qs9L8zIhLckFDzaeOqwjHtXjvbkujqRmF2TX8U2XV61mLf48fMQ H5mCpMx2dmdEFPcot2fOsB+EddLIzNIS9p/U10TWnAoi5kvBM4/QvSGiLTi1GIqLwc8s jKlfaBRuCibB0aLW4mrYBFrfh7j35jwqTIMCkzs175UndWI0tqs3qZB3Ksw5cq44mlgJ zuVU/Zy0iS2xbZS1bR0BINEqTRB8cdB75bJrS/mikI5T3MNWKcSZ9epDj35+tIPeEy1f UCAg== X-Gm-Message-State: ACgBeo20rdti4H4qLlzs5PDYcYL8iNCANIMKTFpWWLqoaUGWJbV3U0G5 5Wk9qcBsbXhKekk5UCtgvuE= X-Google-Smtp-Source: AA6agR7Xkytp2XXN8I2Qcu6v3KLTmoDxQ3RCtj8xr4Gi8KDkaVZMWgLR+utyFISJ9K3PkHwewHU68g== X-Received: by 2002:a05:6a00:21c8:b0:52b:ffc0:15e7 with SMTP id t8-20020a056a0021c800b0052bffc015e7mr28079599pfj.29.1660144749771; Wed, 10 Aug 2022 08:19:09 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:5c3e:5400:4ff:fe19:c3bc]) by smtp.gmail.com with ESMTPSA id 85-20020a621558000000b0052b6ed5ca40sm2071935pfv.192.2022.08.10.08.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 08:19:08 -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 Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next 13/15] mm, memcg: Add new helper get_obj_cgroup_from_cgroup Date: Wed, 10 Aug 2022 15:18:38 +0000 Message-Id: <20220810151840.16394-14-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220810151840.16394-1-laoar.shao@gmail.com> References: <20220810151840.16394-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=1660144750; 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=Clyu36Jre2FnqWFd0IUB464wOplrravQ4IfQGIbyf7A=; b=lqaSbOr8RcrC51lUozHJJ0r/AZSQ+VL19tGANr88FUxtaG8xWx97fEBslR0/+KeEr61Y8L xu/iXpZwFdehRQ/rgG15onDX2e5SBDIRgq/oTc2uASEBe/XeIailV8vCCoI60e+h5eHPP/ M0g4Ll+epNDXkSsVqQEtRiaKY69IJhs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YEil+EOU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660144750; a=rsa-sha256; cv=none; b=ULuH4ebXrJHfd63VwAUUy3TpqqKniMJTTBydCkmak4cSpXtt2ifuQcbpXDixWTbCOIC6aU /D6uQQk0ilx/D0giFnKxPmJimou/jrZR1OL00tlUPmyUhErm7or9KnbXNzzHVoVa+ab9h7 4PkHTnTOzjZlyFlgAY5PP7Jty49iVUw= X-Stat-Signature: hm3u36no8ptup5kwqswm8h33z19yjqez X-Rspamd-Queue-Id: 9D33740177 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YEil+EOU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf07.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1660144750-296743 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: 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; +} + +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(); + cgroup_put(cgrp); + return ERR_PTR(-EINVAL); + } + rcu_read_unlock(); + cgroup_put(cgrp); + + 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;