From patchwork Wed Oct 2 06:54: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: 13819405 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 2E2A3CF3198 for ; Wed, 2 Oct 2024 06:55:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 384356B0323; Wed, 2 Oct 2024 02:55:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 330996B0324; Wed, 2 Oct 2024 02:55:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 181896B0325; Wed, 2 Oct 2024 02:55:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CB1176B0324 for ; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5C7E0161A5A for ; Wed, 2 Oct 2024 06:55:04 +0000 (UTC) X-FDA: 82627750128.26.F1547E1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 9E75940003 for ; Wed, 2 Oct 2024 06:55:01 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pCj6O5yM; spf=pass (imf12.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=1727851974; 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=yJL5LxbownzeagD4KZq9kXv8efc3SGqxwFvj7Yx6zRKZFNTxk80zzhpjJSaGUW2SBJheSz 0uuXkXFDG3hkFzjd86IAG7JeCUdZM1AXrWhnHMTWNOlucPzuoVrdultokzztfnzUMwWnYE KVrRlv6KxKhD2uV2J6pNFCGMIHgKRi8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727851974; a=rsa-sha256; cv=none; b=e/2RQXfe/ys2cAos0bSP2qtpK7LAlnyvAg9HicFtxnZPDvuD89ydI1+qkPkDmZnF8XB5Y/ B/36+akCocI154FzqYmvpobDXjP/abOBklmWbqoGtJJesuc4CNEBJwlWtksNiw/gve9hq5 Mf6ZPQb6oGRG6fAPDHK0DjRQyWSbGAU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pCj6O5yM; spf=pass (imf12.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 5F6AB5C567B; Wed, 2 Oct 2024 06:54:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53748C4CED0; Wed, 2 Oct 2024 06:54:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727852100; bh=CZ1VN+wISqfF7U/NBBkytViSmRb/vkGLNUY76gXrLxU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pCj6O5yMWzyRyOC2sLEsE73j1nEyAtnVAWeVYuXspRu65PIzqY4xQgVm4L8nTDTqO kR4bMLfJz0yHTW7hgwhx0Kyi9RCgz7FEGVu/s6n2CDpActnGsj+FE3vL0vRuTx5ihi rMKduO0LhpTzkAHvr9vAIYmw4Y42AGhMUDBPaYw0qFuDSTs34ZCpM9FVGh4ero//3q ilPuxyLgN60d9WEwkyjzVzA+y/GDOmaZAUCaTDI0dzldxb64qWNFlpSXcdD+TCn8U9 FJotUbQWrdeqRWtPHA++WD1UpIfiZ60ZwVtEiNTk/kfge5P9SGIpWfPlcQYxbAbHzx bP/m61KKeHAoA== 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 Subject: [PATCH v3 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc Date: Tue, 1 Oct 2024 23:54:55 -0700 Message-ID: <20241002065456.1580143-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20241002065456.1580143-1-namhyung@kernel.org> References: <20241002065456.1580143-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9E75940003 X-Stat-Signature: 68jp4harm8dco547nw3r4e67w9wbescz X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1727852101-197830 X-HE-Meta: U2FsdGVkX1+ZFDetZx3ABc+S3arDgNJwtqaT+hLC98/K6c+YsGyv8tz+n7vE0U1wEHDn8YXJw39+0osu1tQ/EaSTKW9klvvurl84K2opC0b3otnPYU9Zh9VgpWJMzwfDYmi3qQ+L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+r24c03LDM9jnujExT6u0mLTDCCzuDMvK6HURWUXfLaIncugd7xIy7FwG8Ta4Am7SZROwe7fYq/oODZjw9v3aIiypDhy/Lc101/T0z9bzv0IQK0zalmzvgcFzCAPj4RadisZpYse+mEaB7r4SLfgjrUBknULq2UHt0ik= 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);