From patchwork Wed Oct 2 18:09:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13820196 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 7D9AECEACF3 for ; Wed, 2 Oct 2024 18:10:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39FB46B02AF; Wed, 2 Oct 2024 14:10:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3297C6B02B7; Wed, 2 Oct 2024 14:10:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 153A36B02B0; Wed, 2 Oct 2024 14:10:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D0DED6B02AE for ; Wed, 2 Oct 2024 14:10:02 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 731B4140D65 for ; Wed, 2 Oct 2024 18:10:02 +0000 (UTC) X-FDA: 82629451044.06.634BF06 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id C196F40019 for ; Wed, 2 Oct 2024 18:10:00 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C1nN9bS1; spf=pass (imf27.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=1727892473; 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=/gFwJL66H9iO1jq0RcGmk7K6IL+6CFpwiDSLj/EXxAo=; b=RwtNb7nr5PpL/Da/RHKFyf8RVixtMNPynTVjqcQcvpnmm1mKMP9tifL4SoFf+WlZPZ1eb6 DLHq+iGbJ9y8XNjSqLlpaALV66Hslb6iV+AAH7NqIq+xsQJvXyrOnm2Wvhflt1GMHK6FU0 CWlWb6XtVY58Y/oDXhqVcHgluXh9bB4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727892473; a=rsa-sha256; cv=none; b=UsE/z9QssCOts2csEgkwouluG1C5QOPFWdy02HC/9G1OLi/KZHI+1ZSNukDe2HmeuX+lUS 7ofcBqYvcl7carGDYLEcVZdsuojHML/wQ7tzqkXYB0eCfcQwdE+ljt83UcC4o74m2Bb8aG kw0stQc0+L1qGzR7JRLw4pjwtXCtmxU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C1nN9bS1; spf=pass (imf27.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 BD3505C3E58; Wed, 2 Oct 2024 18:09:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3F17C4CED6; Wed, 2 Oct 2024 18:09:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727892599; bh=CZ1VN+wISqfF7U/NBBkytViSmRb/vkGLNUY76gXrLxU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C1nN9bS1juVNiiTLMsoIIP2DGHBhXblmGXHhsKBD+j6XPJwI5tpUtFSZYdSkMTJVU PBXFgrsz8B8ZV+VT0cgqgK6qrFs2w4+ycrTH0yA9gyW/m5l7V02/XBNfHa+8kFJAxc 4lDiS2LMUucmUhiFgs7PefmznRoDDt92VSgPcL4FERTeuDorLGV9p9f336azJI+4UE OA/Xcg20450+n41GGYuAvcr+sjyEwfRG4b5943hnrFLr1Jc36oc3uHQDVlH90Kgvc4 otDwmKd42es+dgexigXc+VKAxyPI3gA5HxIY2GFemE/uxwLmPRtnBaR2WSVkvv29E2 eedyeP/3Sdorw== 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 Subject: [PATCH v4 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc Date: Wed, 2 Oct 2024 11:09:55 -0700 Message-ID: <20241002180956.1781008-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20241002180956.1781008-1-namhyung@kernel.org> References: <20241002180956.1781008-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C196F40019 X-Stat-Signature: ogmjw4z9aiss3utzp8hgddojm86s3ckm X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1727892600-696600 X-HE-Meta: U2FsdGVkX1/30dgTIDKInSRtjRrdlB5NNn3xuvz0t1ey+WZyBQcN268Eb2ZIptyrRetf14Cr4vM5WlttOSyOjbcYm78ViBIlKeQKxqr1xygrdmJD8hTAiRrkVDXWynK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PTLXlBTozFwHSGM9q1pvuSeik0hqGgeeke78nAiHrNR6RVaWGrMuFb9idTEeOW86+oWHHVE/LlGoSBSQVN3occuQz8HS3UCV73NqfdbAEr7MKKRqIg6yRWARDybP2uSx1bxATdlRw8zwoW5slBSkwHezNAI1M81KsB13zNLrJaNBInz8= 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 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 + mm/slab_common.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 4053f279ed4cc7ab..3709fb14288105c6 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_RET_NULL) BTF_KFUNCS_END(common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { 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);