From patchwork Sun Jun 19 15:50:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12886755 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 D42F7C433EF for ; Sun, 19 Jun 2022 15:50:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A7A68D0008; Sun, 19 Jun 2022 11:50:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 530D78D0006; Sun, 19 Jun 2022 11:50:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AA4A8D0008; Sun, 19 Jun 2022 11:50:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 293378D0006 for ; Sun, 19 Jun 2022 11:50:50 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F002AA7A for ; Sun, 19 Jun 2022 15:50:49 +0000 (UTC) X-FDA: 79595423418.26.623180C Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf22.hostedemail.com (Postfix) with ESMTP id 9BA72C000A for ; Sun, 19 Jun 2022 15:50:49 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id 73-20020a17090a0fcf00b001eaee69f600so8134914pjz.1 for ; Sun, 19 Jun 2022 08:50:49 -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=WaE/WLiaH5o5ki3TY2BjGi6nEsbC9ev3Bayiew634jE=; b=lg4hLmrvx1aAxFRgX2lydGh2RYsS+K8/vHGdBT3GPJM2V/iumEKMc9+bns60t1/KuB J1TuHcq8XADBIaFEVzyfvThx9pJUKJuJOKByFLFtB3cvZod6/1K+3pO5KBTrbMAriRSp 0aA84OhCbo1cxE5mjGwVS8OjVkf8uyLxSG96ru3xWftJyjaRYeDWc//IT0jYdBNtRhPn WcloU6qqNmA+04BwAXt7Tyjzl/yvXQUfBqUwQO+iOJ6GZB91CgEPWKZXrsjb0Cphc5pp f7Z0OMdpQ6lWNQjBQSjPrk9BqXMbRzM5/bOul+57dEAkay+yUKbMpZ9MsZadWBKuXs7W AYJQ== 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=WaE/WLiaH5o5ki3TY2BjGi6nEsbC9ev3Bayiew634jE=; b=QAvGdInjRJxQgO6om8qjD5iQrYjn6pVYZMSlOS0Gr4A0sFEwdb4pD/y4is+6lDA/7K IhSdhIuKKoZRXx+8PE/z/cEtgnsYVIacaxITUoPpTBCpf2OL/VftfJuVUvSSWFjvVhSN sjqBNu5+Ow5pF8xGIRtJbYZphIXclVIwzMswa3niG4AVt1ePp7KLPt7M5TO8adgGNZqW ZC+cH4noq/nyW6KNq2xI35Q7bDuKM3I8m1hHa8vL/dHvRQZV1/nwHRACHNGTdsmsJFXe iwSmpfVJupluWjkcCEGDZgiKSY4XXlo1pgpaH1Gu1heQ32VDpgoRIwkgeV7zETjovj7v PBqQ== X-Gm-Message-State: AJIora/s3JCNVZpMMjJ0Id/brU0pMsHq1k7TiSx3Ddr7v7YbXtcOeKD4 EMotMgDm+PILTAO0cVNnZGE= X-Google-Smtp-Source: AGRyM1tw+4VoyM5S+fxLBlskF4kAB2xCBsbZchmj1iffm8dIiAU8+M1iozEcYYLSnyvhMrmeJK+cAw== X-Received: by 2002:a17:903:11cd:b0:167:90e6:5d83 with SMTP id q13-20020a17090311cd00b0016790e65d83mr19355523plh.136.1655653848680; Sun, 19 Jun 2022 08:50:48 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2b24:5400:4ff:fe09:b144]) by smtp.gmail.com with ESMTPSA id z10-20020a1709027e8a00b001690a7df347sm6381761pla.96.2022.06.19.08.50.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 08:50:47 -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, quentin@isovalent.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 03/10] mm, memcg: Add new helper obj_cgroup_from_current() Date: Sun, 19 Jun 2022 15:50:25 +0000 Message-Id: <20220619155032.32515-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220619155032.32515-1-laoar.shao@gmail.com> References: <20220619155032.32515-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655653849; a=rsa-sha256; cv=none; b=LGKr16bhPG8OgB7yR/Nr9R4XM55GJLdCdiIA355D26aokMmv5aKJOjhajFBkHHU/MrtCjs Trbh41zMZXp6crIjCwGSaXjIfHdt01wjk/kFXuTxLIwGeryviLXCyk2cRKeTJ7g287CQGp WvekK4RgYeyamH4o4XdnNPvpcHPILpU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lg4hLmrv; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 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=1655653849; 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=WaE/WLiaH5o5ki3TY2BjGi6nEsbC9ev3Bayiew634jE=; b=Y6LEWofv+WWUUA+gOpTLgBygSi2mwkWO9yGViXGvTmqJwKGWYu2hOGSMEpgrrhjU4SXkt2 kNDDmre57cwjDQKcf9U3mzHYkWHTOmnMSHEs4YNURGakjFukYTEZ9P0uIMfK4lw2iyCylv uI0qHrB1dK0jW1ZcDuaIsQ0moDdQXjM= X-Stat-Signature: sd8btri5u1xqj3kwkmakid438463zqmg X-Rspamd-Queue-Id: 9BA72C000A Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=lg4hLmrv; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam07 X-Rspam-User: X-HE-Tag: 1655653849-72757 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: The difference between get_obj_cgroup_from_current() and obj_cgroup_from_current() is that the later one doesn't add objcg's refcnt. Signed-off-by: Yafang Shao --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index cf074156c6ac..402b42670bcd 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1703,6 +1703,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 *obj_cgroup_from_current(void); 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 abec50f31fe6..350a7849dac3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2950,6 +2950,30 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) return objcg; } +__always_inline struct obj_cgroup *obj_cgroup_from_current(void) +{ + struct obj_cgroup *objcg = NULL; + struct mem_cgroup *memcg; + + if (memcg_kmem_bypass()) + return NULL; + + rcu_read_lock(); + if (unlikely(active_memcg())) + memcg = active_memcg(); + else + memcg = mem_cgroup_from_task(current); + + for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) { + objcg = rcu_dereference(memcg->objcg); + if (objcg) + break; + } + rcu_read_unlock(); + + return objcg; +} + static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) { mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);