From patchwork Thu Oct 10 23:25:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13831302 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 60985D2444F for ; Thu, 10 Oct 2024 23:25:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AA836B0089; Thu, 10 Oct 2024 19:25:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 30CE66B008A; Thu, 10 Oct 2024 19:25:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3F626B008C; Thu, 10 Oct 2024 19:25:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D99186B0089 for ; Thu, 10 Oct 2024 19:25:11 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 733591C6964 for ; Thu, 10 Oct 2024 23:25:07 +0000 (UTC) X-FDA: 82659275580.12.B3608A8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 11DB5C0006 for ; Thu, 10 Oct 2024 23:25:08 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LpjMyrxb; spf=pass (imf10.hostedemail.com: domain of namhyung@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=namhyung@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728602572; 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=PJEKwEWnujsI7OvXqbMa/wPYsumJ28JqxOqu+XT5ozs=; b=V1jOCX4DB9X2L62m6i1h7kYIkZtcIQtLSJSIAvta8fAN8WjgbRC9QF9BX4kV4qujMximvM qEre1see9IuQU/Tvu2UrN0HnZ5PR5MZcW+dusCqLiOIdpWvbUyf9s3KwuTItD/OSB052qK XjFkq8Jo3XwRcKVg5OGEfQlJhLFmE9w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728602572; a=rsa-sha256; cv=none; b=vMIEAwDibt4OFs8dV1t1nKKD1w0er4Lel6BPVDBbgmW9Sp4FL6VheQelsPP51DcBLabjNQ QvdDXiyEahzw+GuqCupcn7XhgteXfpJ5tjiH1nk+MmtBizFwXndaFUmyE3SONLu6ypHpk+ ptGZbQKIsz+NnTxV6nFiQ2CPNrOphPo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LpjMyrxb; spf=pass (imf10.hostedemail.com: domain of namhyung@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=namhyung@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E58655C6013; Thu, 10 Oct 2024 23:25:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D8B8C4CECC; Thu, 10 Oct 2024 23:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728602709; bh=oQNc8mViY1+Zpg9pTngTgTZBRAhT3aWMoYlhdEdPulk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LpjMyrxbIkU8p8Pos+uewoY7l6slYSFawh+sAhm4DKth60xZfVibOZUz7Q6yJfaS9 3BieWcZ1AB31oU4P2cdt6jVckI/XGUIuNal+JYlW6EC5bjS5GyD2crZULXYCqB2AvS pAP9K0fAZg0LENhrccEFmhwVCDT33GDgLUwbdYYl90qz//WMQXC6O6DBYyBtfdZ4ob AfzoKOLuZH8Wi8V4Aejb9+FIDypglbDiCktzmpuiuVcWORss5rG7yW1GO2Si5l022x tNa6F5diiFsS75Ryvb3II3cIsRUVM9gOgqJNWNvqiIreIfJMsGK78mMda+iZFqGik2 LR0aLVQRbFOEA== From: Namhyung Kim To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , LKML , bpf@vger.kernel.org, Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, Arnaldo Carvalho de Melo , Kees Cook , "Paul E. McKenney" Subject: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc Date: Thu, 10 Oct 2024 16:25:04 -0700 Message-ID: <20241010232505.1339892-3-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241010232505.1339892-1-namhyung@kernel.org> References: <20241010232505.1339892-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 11DB5C0006 X-Stat-Signature: mb85j7wq41fajfxqgi4ami9qpj7ms677 X-HE-Tag: 1728602708-981692 X-HE-Meta: U2FsdGVkX1/yqdsfBTeuGkwmDP+uK2o2F942FahSpq8Imp4rBBteXjEw1HSAIsTddq4jHrE5WdN12YadImwLWz4QY9PPm+JeeOkb1eqyc6q+N1yJUnq5NSNBfg72UE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FXuLxh+WylFVIgT1Rocg/U+rfUyl5Y9eUA1Ay2K65G414zrsFGUdZTDGJEOnhs0q7uVfiNLjN4/sRi9kL09BHFam2sjuGopvDKjcU9EUVpmJA1Ti/6t64QjXtjF/Zm9fx+66eKvBXq8PGWrBOSKkTHh045PN5K5r4YZw/cb/SzDWP75kTCf5QQ1nkIYlI6ZvyfE= 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: List-Subscribe: List-Unsubscribe: The bpf_get_kmem_cache() is to get a slab cache information from a virtual address like virt_to_cache(). If the address is a pointer to a slab object, it'd return a valid kmem_cache pointer, otherwise NULL is returned. It doesn't grab a reference count of the kmem_cache so the caller is responsible to manage the access. The returned point is marked as PTR_UNTRUSTED. And the kfunc has KF_RCU_PROTECTED as the slab object might be protected by RCU. The intended use case for now is to symbolize locks in slab objects from the lock contention tracepoints. Suggested-by: Vlastimil Babka Acked-by: Roman Gushchin (mm/*) Acked-by: Vlastimil Babka #mm/slab Signed-off-by: Namhyung Kim --- kernel/bpf/helpers.c | 1 + kernel/bpf/verifier.c | 5 +++++ mm/slab_common.c | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 4053f279ed4cc7ab..7bfef9378ab21267 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3090,6 +3090,7 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_get_kmem_cache, KF_RCU_PROTECTED) BTF_KFUNCS_END(common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index cfc62e0776bff2c8..f514247ba8ba8a57 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11259,6 +11259,7 @@ enum special_kfunc_type { KF_bpf_preempt_enable, KF_bpf_iter_css_task_new, KF_bpf_session_cookie, + KF_bpf_get_kmem_cache, }; BTF_SET_START(special_kfunc_set) @@ -11324,6 +11325,7 @@ BTF_ID(func, bpf_session_cookie) #else BTF_ID_UNUSED #endif +BTF_ID(func, bpf_get_kmem_cache) static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) { @@ -12834,6 +12836,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0].type = PTR_TO_BTF_ID; regs[BPF_REG_0].btf_id = ptr_type_id; + if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) + regs[BPF_REG_0].type |= PTR_UNTRUSTED; + if (is_iter_next_kfunc(&meta)) { struct bpf_reg_state *cur_iter; diff --git a/mm/slab_common.c b/mm/slab_common.c index 7443244656150325..5484e1cd812f698e 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1322,6 +1322,25 @@ size_t ksize(const void *objp) } EXPORT_SYMBOL(ksize); +#ifdef CONFIG_BPF_SYSCALL +#include + +__bpf_kfunc_start_defs(); + +__bpf_kfunc struct kmem_cache *bpf_get_kmem_cache(u64 addr) +{ + struct slab *slab; + + if (!virt_addr_valid(addr)) + return NULL; + + slab = virt_to_slab((void *)(long)addr); + return slab ? slab->slab_cache : NULL; +} + +__bpf_kfunc_end_defs(); +#endif /* CONFIG_BPF_SYSCALL */ + /* Tracepoints definitions. */ EXPORT_TRACEPOINT_SYMBOL(kmalloc); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);