From patchwork Fri Jul 29 15:23:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12932542 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 AD4D4C19F2C for ; Fri, 29 Jul 2022 15:23:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B72D6B0083; Fri, 29 Jul 2022 11:23:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4655E6B0085; Fri, 29 Jul 2022 11:23:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3071E8E0001; Fri, 29 Jul 2022 11:23:48 -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 201566B0083 for ; Fri, 29 Jul 2022 11:23:48 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id ECE00141610 for ; Fri, 29 Jul 2022 15:23:47 +0000 (UTC) X-FDA: 79740507294.08.E119E23 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf21.hostedemail.com (Postfix) with ESMTP id ED44F1C00D5 for ; Fri, 29 Jul 2022 15:23:46 +0000 (UTC) Received: by mail-pj1-f53.google.com with SMTP id w17-20020a17090a8a1100b001f326c73df6so3943240pjn.3 for ; Fri, 29 Jul 2022 08:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pNoGfDj1V1TnWI6qBdBSp31pmoo67RNKkb0LUb2vNLg=; b=L5yhSIt8gjsSmoj1o/cBW9AzyQHq5mIC+9at26SQ6hcp6bPNjtuU8B409uMpIMZulG QPxCn+2t9lkamdw4jzEf0G7zH9NHRcpwqv16qIJXXhJYDy5yKOj47BUrDDSVxTl9UkwT v9/v1FmerqehsorOy6r6RTfjjTPHyTpxQIEeQPNzVU2JKPvYvoBgpEtGQZpUiAJ1nqLi 3wkIv92UxZWulnBVltosq9eXBkLyzfnsQMRY6U+plLOjEM5r9evSaEkkJ1O4Vvl56u0l znIJhCMahnLYJnPpQcR8ECUaR1SL/ePMj83pAVrBVaC8Der/7J67vC6fQmZFhSNf1Wn4 c3/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pNoGfDj1V1TnWI6qBdBSp31pmoo67RNKkb0LUb2vNLg=; b=U/CtZwe3aVndtttDdOjO36Oqvuwaxt5mQBWXDWHFCsBZBiYR77oOkLda45CjJnyqs4 xhqtxNnnfGWeZnAuFvnkIDK1tqcvDzAIoU8WqUD0RB6E1adjiwQ3YFkbqhR4y2VBpGNc dibs8s9PljHs4Wb01qVLEXzpGipVSQXcTfNpYLgD6xFxIjuk+lCx3b4/N6Qtywf9Qpth MDB0RkthM7w9wFHEuSEByp/1ufHWRSG2Dsy6Qa9+nR/uefSpQRMVNwB/oz0B4RKCpDhM PjssxC2qyqp22MMEJXvB7W8M/ZRjhoSF642oPFAi/pnBhvNbLQIK/IM3J10WMDO8/NXO Suiw== X-Gm-Message-State: ACgBeo2gl7qzc+hwaX0oaCdQCJAZOOX5YCQFzJBXxUxF5Y9/ltQeXxbq 9LLGBGapzy9cbmmfEdTL41E= X-Google-Smtp-Source: AA6agR6NHf7CpfGmFU7zV1EhZPCbkoe+KGvzl+qrAN+Jzzpvjgr18XBqTeZdgzhLr65Hfxz2R6a99Q== X-Received: by 2002:a17:902:e5cd:b0:16d:b92c:6848 with SMTP id u13-20020a170902e5cd00b0016db92c6848mr4335575plf.56.1659108226544; Fri, 29 Jul 2022 08:23:46 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2912:5400:4ff:fe16:4344]) by smtp.gmail.com with ESMTPSA id b12-20020a1709027e0c00b0016d3a354cffsm3714219plm.89.2022.07.29.08.23.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 08:23:45 -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: [RFC PATCH bpf-next 12/15] mm, memcg: Add new helper get_obj_cgroup_from_cgroup Date: Fri, 29 Jul 2022 15:23:13 +0000 Message-Id: <20220729152316.58205-13-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220729152316.58205-1-laoar.shao@gmail.com> References: <20220729152316.58205-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=L5yhSIt8; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659108227; a=rsa-sha256; cv=none; b=PsKhT9Yr01WXtzetb14HLNcPh/G0H7nXoleFsGUTOPZEOJYozA9wKCwmJd+MuhmqtCOy1f iwzofnAhuOnuhBqUv2WrmdGYgTQPhi+rOni6FJItovpkzetpS6VnROO9DGITZjixNV+sT1 RQCyaiZn4aPXm22QCJsSWI+gAzIIZWI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659108227; 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=pNoGfDj1V1TnWI6qBdBSp31pmoo67RNKkb0LUb2vNLg=; b=lB8nMum191d6RYk4+ofJo4Udw9DBamrBpQB0YoZ1G5xuHRDPwSSEkQow2pj/b6UwK43asm GByWIIoJuGalbb4N0ZA9QVfg2ehzVWcL5orUcRKI/qCDDeruyr8aaSGpu4Nxswsv/8Mz3v F6+uvcdbE0pXjwMPDYgmiOwzhqWb3kw= Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=L5yhSIt8; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspam-User: X-Stat-Signature: 7mmk4nziwxd597rbz94uupan1nysymym X-Rspamd-Queue-Id: ED44F1C00D5 X-HE-Tag: 1659108226-277174 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 2f0a611f12e5..901a9219fe9a 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1713,6 +1713,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 *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 618c366a2f07..762cffae0320 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;