From patchwork Wed Oct 2 06:54:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13819403 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 0EE06CF3199 for ; Wed, 2 Oct 2024 06:55:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24AE36B031C; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F6FF6B031F; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA3E46B031C; Wed, 2 Oct 2024 02:55:03 -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 AF0466B0315 for ; Wed, 2 Oct 2024 02:55:03 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 665D116198B for ; Wed, 2 Oct 2024 06:55:03 +0000 (UTC) X-FDA: 82627750086.02.C8523BF Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id AF8EA140006 for ; Wed, 2 Oct 2024 06:55:00 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vlm7lIwr; spf=pass (imf23.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=1727851973; 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=MLtkiGV4MD1yMU8jgglHJy3X/2Yb9MXKwmwxeJSMn1c=; b=HzS061QP00vRcuCUOicSGIXTE5KEtWpO+yY0JSaW8SEXTU8sUwjmYvn+mLx9aLZ+KqARmV lefGj+NGpRen6IY+c8Q9SahL233CSo+rt3Jo/RZXCqCJtazh9bE1FcDbE9qfS5e86amCX9 nc+vZW7mFCxovtqH740v6SKBR6+k3dg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727851973; a=rsa-sha256; cv=none; b=MHaVLBKBjGvY80WByzRMRhWq9bXJq0jVPI1qCGepSTKALHwE1NKe2WAMNpeB3Z5eBf3udX noHX9Ab8UW7s4s8p5nAcHDaymhIfrw+N1O6dCl/XG+kXhvCamCmpKkuKG+l47LhCKSdxNm z3JIP/jeAdo8bQq1Ya+RR/rWdkPdeZM= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vlm7lIwr; spf=pass (imf23.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 3F35E5C5653; Wed, 2 Oct 2024 06:54:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4FE8CC4CED4; Wed, 2 Oct 2024 06:54:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727852099; bh=Aek5cZQ6n85ip3bRtfPexSBg6htaWNu16xazCDWdW4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vlm7lIwr/dhafAwMaF9u4SxUvkeHiao0dELBvsNjPyceEnzkv9XU0OVWndLR1ySRn jat6+BuMZBfvf6xkXuqC1/4CXf7fMpCc1hipaPvz3/RzIiO7BjvhTe9uoBs9QxTOEu B6z5PH+RnI5sJ2p5Hw1DsyTzmJRsXRuX+Wa6iWAl4QDy5IY0GUsW+7bPYKi8QXhjnj /c/LhPqLupR+UrpI5mzVp4TderYm917KdTqoUwoXhPuXYNZ38QIOtjBQ03FizsF+3d 8msmT0qfuG3rGem3XoWDS5MYCg1QKwJfMQodctnyfZHnFA/S41NhdDPRfwPK7BXN/b FcYIAlRI3U8YQ== 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 1/3] bpf: Add kmem_cache iterator Date: Tue, 1 Oct 2024 23:54:54 -0700 Message-ID: <20241002065456.1580143-2-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AF8EA140006 X-Stat-Signature: 3xy5xzwa58ergroksh6h9d65s5jmquti X-HE-Tag: 1727852100-634433 X-HE-Meta: U2FsdGVkX19PMpDdU+3CycXqXYkqUayOm362ypuGKC7mrAH6T56Ob7oulRm+/3yeDa/R9ng1ht+ltnIeomB7XlWL8RKyNOW5iqFPZXQW1bbLlVNbQm8Ihxq/Rrdkvv7xG8JpTK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j/8+b3gs62NKolwcNyojb9Iq5zYOD6C11zBoVX/IVI7zydf/USIE7j2HI9Uj9hbgNiUkMxklinR/26zuq31jxWX5tPAA72F2Wt0/SfjRN5uj1rIF+ie62S1NPMSz2s= 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 new "kmem_cache" iterator will traverse the list of slab caches and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it. Now the iteration grabs the slab_mutex only if it traverse the list and releases the mutex when it runs the BPF program. The kmem_cache entry is protected by a refcount during the execution. It includes the internal "mm/slab.h" header to access kmem_cache, slab_caches and slab_mutex. Hope it's ok to mm folks. Signed-off-by: Namhyung Kim --- I've removed the Acked-by's from Roman and Vlastimil since it's changed not to hold the slab_mutex and to manage the refcount. Please review this change again! include/linux/btf_ids.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/kmem_cache_iter.c | 165 +++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 kernel/bpf/kmem_cache_iter.c base-commit: 9502a7de5a61bec3bda841a830560c5d6d40ecac diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index c0e3e1426a82f5c4..139bdececdcfaefb 100644 --- a/include/linux/btf_ids.h +++ b/include/linux/btf_ids.h @@ -283,5 +283,6 @@ extern u32 btf_tracing_ids[]; extern u32 bpf_cgroup_btf_id[]; extern u32 bpf_local_storage_map_btf_id[]; extern u32 btf_bpf_map_id[]; +extern u32 bpf_kmem_cache_btf_id[]; #endif diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 9b9c151b5c826b31..105328f0b9c04e37 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -52,3 +52,4 @@ obj-$(CONFIG_BPF_PRELOAD) += preload/ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o +obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o diff --git a/kernel/bpf/kmem_cache_iter.c b/kernel/bpf/kmem_cache_iter.c new file mode 100644 index 0000000000000000..a77c08b82c6bc965 --- /dev/null +++ b/kernel/bpf/kmem_cache_iter.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Google */ +#include +#include +#include +#include +#include + +#include "../../mm/slab.h" /* kmem_cache, slab_caches and slab_mutex */ + +struct bpf_iter__kmem_cache { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kmem_cache *, s); +}; + +static void *kmem_cache_iter_seq_start(struct seq_file *seq, loff_t *pos) +{ + loff_t cnt = 0; + struct kmem_cache *s = NULL; + + mutex_lock(&slab_mutex); + + /* + * Find an entry at the given position in the slab_caches list instead + * of keeping a reference (of the last visited entry, if any) out of + * slab_mutex. It might miss something if one is deleted in the middle + * while it releases the lock. But it should be rare and there's not + * much we can do about it. + */ + list_for_each_entry(s, &slab_caches, list) { + if (cnt == *pos) { + /* + * Make sure this entry remains in the list by getting + * a new reference count. Note that boot_cache entries + * have a negative refcount, so don't touch them. + */ + if (s->refcount > 0) + s->refcount++; + break; + } + + cnt++; + } + mutex_unlock(&slab_mutex); + + if (cnt != *pos) + return NULL; + + ++*pos; + return s; +} + +static void kmem_cache_iter_seq_stop(struct seq_file *seq, void *v) +{ + struct bpf_iter_meta meta; + struct bpf_iter__kmem_cache ctx = { + .meta = &meta, + .s = v, + }; + struct bpf_prog *prog; + bool destroy = false; + + meta.seq = seq; + prog = bpf_iter_get_info(&meta, true); + if (prog) + bpf_iter_run_prog(prog, &ctx); + + mutex_lock(&slab_mutex); + if (ctx.s && ctx.s->refcount > 0) + destroy = true; + mutex_unlock(&slab_mutex); + + if (destroy) + kmem_cache_destroy(ctx.s); +} + +static void *kmem_cache_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct kmem_cache *s = v; + struct kmem_cache *next = NULL; + bool destroy = false; + + ++*pos; + + mutex_lock(&slab_mutex); + + if (list_last_entry(&slab_caches, struct kmem_cache, list) != s) { + next = list_next_entry(s, list); + if (next->refcount > 0) + next->refcount++; + } + + /* Skip kmem_cache_destroy() for active entries */ + if (s->refcount > 1) + s->refcount--; + else if (s->refcount == 1) + destroy = true; + + mutex_unlock(&slab_mutex); + + if (destroy) + kmem_cache_destroy(s); + + return next; +} + +static int kmem_cache_iter_seq_show(struct seq_file *seq, void *v) +{ + struct bpf_iter_meta meta; + struct bpf_iter__kmem_cache ctx = { + .meta = &meta, + .s = v, + }; + struct bpf_prog *prog; + int ret = 0; + + meta.seq = seq; + prog = bpf_iter_get_info(&meta, false); + if (prog) + ret = bpf_iter_run_prog(prog, &ctx); + + return ret; +} + +static const struct seq_operations kmem_cache_iter_seq_ops = { + .start = kmem_cache_iter_seq_start, + .next = kmem_cache_iter_seq_next, + .stop = kmem_cache_iter_seq_stop, + .show = kmem_cache_iter_seq_show, +}; + +BTF_ID_LIST_GLOBAL_SINGLE(bpf_kmem_cache_btf_id, struct, kmem_cache) + +static const struct bpf_iter_seq_info kmem_cache_iter_seq_info = { + .seq_ops = &kmem_cache_iter_seq_ops, +}; + +static void bpf_iter_kmem_cache_show_fdinfo(const struct bpf_iter_aux_info *aux, + struct seq_file *seq) +{ + seq_puts(seq, "kmem_cache iter\n"); +} + +DEFINE_BPF_ITER_FUNC(kmem_cache, struct bpf_iter_meta *meta, + struct kmem_cache *s) + +static struct bpf_iter_reg bpf_kmem_cache_reg_info = { + .target = "kmem_cache", + .feature = BPF_ITER_RESCHED, + .show_fdinfo = bpf_iter_kmem_cache_show_fdinfo, + .ctx_arg_info_size = 1, + .ctx_arg_info = { + { offsetof(struct bpf_iter__kmem_cache, s), + PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, + }, + .seq_info = &kmem_cache_iter_seq_info, +}; + +static int __init bpf_kmem_cache_iter_init(void) +{ + bpf_kmem_cache_reg_info.ctx_arg_info[0].btf_id = bpf_kmem_cache_btf_id[0]; + return bpf_iter_reg_target(&bpf_kmem_cache_reg_info); +} + +late_initcall(bpf_kmem_cache_iter_init); 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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W1gd99a9M06qTxlJrf+kSI9sJSNoDcd7rS3rGRMwEaFEWGLWFQNRxEuFX9zDIKfLJw+5WXF/sZ3WgXKqu4xLVJJJv2TX4jyytcY6elgvHqpwkluucRQBgcjmi3njuO+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); From patchwork Wed Oct 2 06:54:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13819404 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 43C7CCF3198 for ; Wed, 2 Oct 2024 06:55:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6A1B6B0321; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D28256B0325; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1DF06B0323; Wed, 2 Oct 2024 02:55:04 -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 770BC6B031F for ; Wed, 2 Oct 2024 02:55:04 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0A7A081A28 for ; Wed, 2 Oct 2024 06:55:04 +0000 (UTC) X-FDA: 82627750128.07.015482D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 5E95818000A for ; Wed, 2 Oct 2024 06:55:02 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WBVxxspY; spf=pass (imf06.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=1727851932; 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=f0m9MJsP55KrK3+a3K8sFov9FGQ4S5Wxs3ZBzUE7qQg=; b=74z9dfwoedNxr9+KlBlsdnDieUFwZHD6l8eXXO1quJkn/4FTB3l9cAUssf1Fk9wktc1nHh 7TU3dkhBD4YBR0u2r66U4y5NMfkQYMb2uZvSsFRqQaMhQvkql5+g+1TxPj4gqW4wozu9QZ Uzj/DAqO0jKk2BmPLTltJeX3bn/4CSs= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WBVxxspY; spf=pass (imf06.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727851932; a=rsa-sha256; cv=none; b=FFAVxjFTD5eG1ZAnlsIrVwcK56BneDBsqnL3f371xDRRITLDPNtPTNDfxjm1Ocv/XTz8Ht wPXpavGE5/iUz7jmuZCwIlIKmyv4cI5h47smYpHSM2VeqLKkMtfxec4vNRbueRKBb7vnNQ DGhevWaqwtaa0dfv18ODrRaOdWpH9TY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5B6FE5C48B2; Wed, 2 Oct 2024 06:54:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51B76C4CED1; Wed, 2 Oct 2024 06:55:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727852101; bh=ePjQez0zpwMreMUAuz+KvIM2b9Vii7HFddbSYrTFhpc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WBVxxspYRqfrBYxPvoZg4GwqodyrcUmbXOFf9ME/YUAtHdpPJYt/6Dk3G0HgWrJoV fKSUVHbwcvkLgmG86/v+P3h2vTQvZKFWsjdzg1CZK31PljMEhfdhrE4B4aYxMswVgW wo9sLBmW1TNfXviivZg2dtva9N8yp6Mbqvm9Wr+8fHKmb1Q59TGOHqwS32JZL5WKg8 jkS1HVE2a4KAY4NAEz2ibBChxUJh8WjgSeMT0FNB0uzZr2uxQX5cDgsq1AOVdQOQDe 7VZtd/UiT3dg+JdpcOTZ5K8JP5WcAHfy4lEP3qWOZ06Us4Zp0XWBYh4kHfZLGks+kq 5TBJIDo2rWOxw== 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 3/3] selftests/bpf: Add a test for kmem_cache_iter Date: Tue, 1 Oct 2024 23:54:56 -0700 Message-ID: <20241002065456.1580143-4-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-Server: rspam06 X-Rspamd-Queue-Id: 5E95818000A X-Stat-Signature: fupkiefwhhr7tjtpamfy69htkj79x94c X-Rspam-User: X-HE-Tag: 1727852102-337903 X-HE-Meta: U2FsdGVkX18hCEdh8LnbPCjJh5okDRHQxF5fUWqNtHgjTLykHnxDBbhXpK9cLDOZbfHHwAjVrc137EgndOhGsG/d7vuswqLEtw4KJKD9p8DS4TyuwNIttWHTXe8op1bY/ewC98PFRaGds72nfrJm6qzgKhbG4N5uBR+uXzw4nT0z6vH5yU9ptBLe7S5I0ecuTly3PVdAUwR7Wyq+kWn8Yd1ucZFOeUBQw5KafTPO0wGoN4j9odIexpszfv/cCspqmKxuXuro3SGcfO0raQnkvOFtpbrioVUrV4YoCx5MqnX7l/l2K8TU+YSjjqAxTCnBTuWnXvfXkQEgsMjo0SrCeVSlclZcbpwoO9obaxgUSsUBHRo3erncj+q8ecu/7wbTvslMt28Swc1Zdwi1LrVE+R28QCh2sV6vvkYQ2QAtK8lVlrqKeipTmd+70lU48KiFsdjUvWGjHwtLN4DeOkYovZ9Rj9BLtIgbjOGaK7GyrGbcrGW7KX7Vqz4B4wetG1m2FMb0C8pWO0OqybAfesqdoPAhRmnjrKqVonH/kxmFqgBAm4OeGn/xeGXOozXp0XdH3VEClibGQFZWvz6357hRf+9xTFbKmEtq2t6JkNGWLYYi6rY4bRm0MhBlzUxw3DM5Amvb8HS9ZsmEBE2AQlRATuj6OR3YWIduvGAVW8Kh8NFuM46eLTox+4EzDygCfokohQz1/UiVr46JBYW4FWWohZUg4+oX+bTL2NLzcIZ0SXJjz/A5t78ZsDsYIjjB4s94o6hSIh+WeHrliEThYLdnqp6IqnOLn+l67PY0/Pr5v/otcU8HqilSqHMr1a2mYIT6oS0uXyYgxmOfwo1Z9V1bjDu4djlY6hQX9X3cQP6VfBh+gcBH4k+RALUDBt6RLUdE4Axlgf5NKcbErA0XikuAzq0FcpG2aPVa74MGAIb3HCXd0tw4JpHrusl8uvh8kGuEKdxRLgx6Z68DFJ++T+8 R8mIbLnK csZyqmt1b60WpAgacViAu3nCqzfTkjMcn4bYma0zv1gldaegzPAMtElHYcP7/Dmo5Y7LtIyzkwFG5vdaAZoW9nA296Em15+bWtBVwoh8sA2tgapJ8L7Cf+QOaXoNeLNw/LQ+puqlCDS2h7U0HUPv8on4pM8Vo63JRQtgqU6Nl0WST0cBNZhq3WfAy/qBLqrKN9ST2dPkvTKHXYeqQutt7rjTWgIx2Ud2/let4fAwXNevFZmV/xFZ/dy08UtrmLSktqZLbuGkF+0OBf5A3Kgw8KNRaCp50NjOlE+RsFb1Gd8QW1/3zvhw/YlUUAsK7VDAG4F4YBhT5nd9lQi+CA+nFEQiJ2raAzbXax8AouTtf7m1JPKR7HGhssvvbr4BtHs0dijnSqrziRVMJKI0= 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 test traverses all slab caches using the kmem_cache_iter and check if current task's pointer is from "task_struct" slab cache. Signed-off-by: Namhyung Kim --- .../bpf/prog_tests/kmem_cache_iter.c | 64 ++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 ++ .../selftests/bpf/progs/kmem_cache_iter.c | 66 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c create mode 100644 tools/testing/selftests/bpf/progs/kmem_cache_iter.c diff --git a/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c new file mode 100644 index 0000000000000000..3965e2924ac82d91 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google */ + +#include +#include +#include +#include "kmem_cache_iter.skel.h" + +static void test_kmem_cache_iter_check_task(struct kmem_cache_iter *skel) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .flags = 0, /* run it with the current task */ + ); + int prog_fd = bpf_program__fd(skel->progs.check_task_struct); + + /* get task_struct and check it if's from a slab cache */ + bpf_prog_test_run_opts(prog_fd, &opts); + + /* the BPF program should set 'found' variable */ + ASSERT_EQ(skel->bss->found, 1, "found task_struct"); +} + +void test_kmem_cache_iter(void) +{ + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + struct kmem_cache_iter *skel = NULL; + union bpf_iter_link_info linfo = {}; + struct bpf_link *link; + char buf[1024]; + int iter_fd; + + skel = kmem_cache_iter__open_and_load(); + if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load")) + return; + + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + + link = bpf_program__attach_iter(skel->progs.slab_info_collector, &opts); + if (!ASSERT_OK_PTR(link, "attach_iter")) + goto destroy; + + iter_fd = bpf_iter_create(bpf_link__fd(link)); + if (!ASSERT_GE(iter_fd, 0, "iter_create")) + goto free_link; + + memset(buf, 0, sizeof(buf)); + while (read(iter_fd, buf, sizeof(buf) > 0)) { + /* read out all contents */ + printf("%s", buf); + } + + /* next reads should return 0 */ + ASSERT_EQ(read(iter_fd, buf, sizeof(buf)), 0, "read"); + + test_kmem_cache_iter_check_task(skel); + + close(iter_fd); + +free_link: + bpf_link__destroy(link); +destroy: + kmem_cache_iter__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/bpf_iter.h b/tools/testing/selftests/bpf/progs/bpf_iter.h index c41ee80533ca219a..3305dc3a74b32481 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter.h +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h @@ -24,6 +24,7 @@ #define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used #define BTF_F_ZERO BTF_F_ZERO___not_used #define bpf_iter__ksym bpf_iter__ksym___not_used +#define bpf_iter__kmem_cache bpf_iter__kmem_cache___not_used #include "vmlinux.h" #undef bpf_iter_meta #undef bpf_iter__bpf_map @@ -48,6 +49,7 @@ #undef BTF_F_PTR_RAW #undef BTF_F_ZERO #undef bpf_iter__ksym +#undef bpf_iter__kmem_cache struct bpf_iter_meta { struct seq_file *seq; @@ -165,3 +167,8 @@ struct bpf_iter__ksym { struct bpf_iter_meta *meta; struct kallsym_iter *ksym; }; + +struct bpf_iter__kmem_cache { + struct bpf_iter_meta *meta; + struct kmem_cache *s; +} __attribute__((preserve_access_index)); diff --git a/tools/testing/selftests/bpf/progs/kmem_cache_iter.c b/tools/testing/selftests/bpf/progs/kmem_cache_iter.c new file mode 100644 index 0000000000000000..3f6ec15a1bf6344c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kmem_cache_iter.c @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google */ + +#include "bpf_iter.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +#define SLAB_NAME_MAX 256 + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(void *)); + __uint(value_size, SLAB_NAME_MAX); + __uint(max_entries, 1024); +} slab_hash SEC(".maps"); + +extern struct kmem_cache *bpf_get_kmem_cache(__u64 addr) __ksym; + +/* result, will be checked by userspace */ +int found; + +SEC("iter/kmem_cache") +int slab_info_collector(struct bpf_iter__kmem_cache *ctx) +{ + struct seq_file *seq = ctx->meta->seq; + struct kmem_cache *s = ctx->s; + + if (s) { + char name[SLAB_NAME_MAX]; + + /* + * To make sure if the slab_iter implements the seq interface + * properly and it's also useful for debugging. + */ + BPF_SEQ_PRINTF(seq, "%s: %u\n", s->name, s->object_size); + + bpf_probe_read_kernel_str(name, sizeof(name), s->name); + bpf_map_update_elem(&slab_hash, &s, name, BPF_NOEXIST); + } + + return 0; +} + +SEC("raw_tp/bpf_test_finish") +int BPF_PROG(check_task_struct) +{ + __u64 curr = bpf_get_current_task(); + struct kmem_cache *s; + char *name; + + s = bpf_get_kmem_cache(curr); + if (s == NULL) { + found = -1; + return 0; + } + + name = bpf_map_lookup_elem(&slab_hash, &s); + if (name && !bpf_strncmp(name, 11, "task_struct")) + found = 1; + else + found = -2; + + return 0; +}