From patchwork Fri Sep 27 18:41:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13814484 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 6EA88CDD1D4 for ; Fri, 27 Sep 2024 18:41:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E39886B0127; Fri, 27 Sep 2024 14:41:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE85F6B0128; Fri, 27 Sep 2024 14:41:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3D616B0129; Fri, 27 Sep 2024 14:41:40 -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 A43906B0127 for ; Fri, 27 Sep 2024 14:41:40 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5B49DC1395 for ; Fri, 27 Sep 2024 18:41:40 +0000 (UTC) X-FDA: 82611386760.28.6D3B226 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id C1A43100008 for ; Fri, 27 Sep 2024 18:41:38 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="h/xmwgNC"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=namhyung@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727462461; a=rsa-sha256; cv=none; b=ioUfjR+1VNKMLrh9Jmy4329mrrRP7rZnPNPA1FhHzLhEnjIkSscOLkyNPilW2JNLC3XtG0 6n2NniLQB9aTc6XqgKCA7tGcwJ/7o+Hm2y6ir+LeOxn3tZKjiMD6VNGnsG2aqQHvJD//T0 J/WnYKUt2JMjgA4e2po+qAegRoc77DQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="h/xmwgNC"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=namhyung@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727462461; 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=viKyFMixliHnI2HInYy9r5fic0KTNf5AsKWSi3DHVDs=; b=1rrXVASjOxylDeKbgqy2uE05XDuiNet0Ewf26ud8Q64wbAjzzPNIxc82GKgWp0gHw4B9eL Cby3SFZtZywPYY9vgOrNCW9A103PQeEK0KklIth+qW5yQ7x39doFiD2qouvrk9Jgau1UDz u3ie5snwJxpAdkKplo9IO47ElDmFD3U= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id D38B8A459D8; Fri, 27 Sep 2024 18:41:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1612C4CED9; Fri, 27 Sep 2024 18:41:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727462497; bh=aGXxmsEp/uzql1BOkNU+QldkkvWzmazSesv6pu9JlCg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h/xmwgNC9gCQMLx3EopU51gjZjDEszjSx7m0YzwM9dxl9jGiQIfZfu7fXfcf2beZz d/SmetuGMSmmNIkLuEdEl53xnFkV5+JkvmJD79VTWBih+Z720cMEeOFnbFQ21+Gb5M ZbMqLldLREXQzUUeKjYRGs5aEqw86o+UBP2TIqbLSO80yAV65emdDN7Tiy3gkHXRcP fRxziCdTpjXwWpzBaEGpvJiZt+d5v3MoGa6drDAMezJzaQg9D8YiepRoq7StCxX3ZO +2ATJyOcO4sK/7+bL5IhOebBEP3qkQwwSS7I8sIMgi9HAlDmMxpDg5ZdIRXCzsrOEi pYWhKpTE5LbEA== 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: [RFC/PATCH bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc Date: Fri, 27 Sep 2024 11:41:32 -0700 Message-ID: <20240927184133.968283-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20240927184133.968283-1-namhyung@kernel.org> References: <20240927184133.968283-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: C1A43100008 X-Rspamd-Server: rspam01 X-Stat-Signature: hwom1oroj539k6yrhakzj3b8ixazedit X-HE-Tag: 1727462498-760129 X-HE-Meta: U2FsdGVkX1+HSWd9JHuBWlCxdc2/UlmB17mUC6JMBkRv7fOSCKIvIpuQEu2MTB3NDGALw5pa0xuuCogh0ze63R4E1ALGL/UuFrAqWv6TUvRtgCbCjkK+tRtV7FwGojFWvQpP9G70Cc6Cwi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obijkgODfUVNfjSSQXqBZ1fo8+ZosNHpeSCHHD5iVpJlKmq6mmZuzoj9DFAIFD1cskX6pTQcDUNGUAaODuwpkKAQy/2xXJ4aHu5hQZDlBbmtgeKc9uYdyi2FTOFF3Gap01zWrQPiqNDcavJs+r1qYzrwQaCW 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 | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1a43d06eab286c26..bbc5800ec3afc899 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..e648b05a635b94bf 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1322,6 +1322,22 @@ 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; + + 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);