From patchwork Wed Sep 25 22:30:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13812497 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 9B8ACCCFA06 for ; Wed, 25 Sep 2024 22:30:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07FC46B00BA; Wed, 25 Sep 2024 18:30:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F22BF6B00BC; Wed, 25 Sep 2024 18:30:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D99D46B00BD; Wed, 25 Sep 2024 18:30:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BF6046B00BA for ; Wed, 25 Sep 2024 18:30:28 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3DDF480A9F for ; Wed, 25 Sep 2024 22:30:28 +0000 (UTC) X-FDA: 82604705736.26.6D664C3 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id A82D020002 for ; Wed, 25 Sep 2024 22:30:26 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hP3uzsJa; spf=pass (imf03.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 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=1727303305; 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=sFYt/nEkx0UumJD8E9UZ/5jRTJPx7XWT9TtPePAHNMs=; b=adVLpFyZEpvtq6xZSNrjuguxAqlQRd6GtidQzahUDw1B1A2zkrNmBoByT9qx8vWHMGK3N/ NFwKq5O71/uRChcu0uXGe5TkKdohGfhWojbrZMV4EjpFRk60wog+O7QqlYcf0wokfpuOVR mSbRg3ZJFhfp7kVl8pJDT6rJqL+krrM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727303305; a=rsa-sha256; cv=none; b=DplKZHww8Fg/2//hinIx2ouoEuXLO6/oCniz5Gg474ZGgxsB0mLoijhz9vtLxEpKCqW3/8 YimfhqOPZPjC+TgSP7aCnTtlODKxkHAkM6sv0Bjo+kIXHpmM4YTSaNPaOddgK3hfib2XFT df0uYCvO5taFVkiBjRrPk1f2xfc6+l8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hP3uzsJa; spf=pass (imf03.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 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 nyc.source.kernel.org (Postfix) with ESMTP id 9BE16A4497D; Wed, 25 Sep 2024 22:30:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD8CEC4CED0; Wed, 25 Sep 2024 22:30:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727303425; bh=IZbnpYx7HLrnxLUhGSarNuN3sSbeCYpJAfQP4eedA0Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hP3uzsJaqEqSbMN7cXerNzoM3ytZTbTgvmYg9KxlAbUC1BGyI+422hsFAGVd87g0t cDpwrOhWNY8UsLK/+XC0deTSKHwKUcsG8EttSMg6ER6dzJ94mtkN8L57W8ytbGzC+Y KlJsOcoLrYvzifR4kKqaHRNWOr8IcpaS2mq6BMYmQzZcT5HR5X3cFs1alBeQlSNbdc +KAVT4Vzj5Ef3dLH73ZZrsReLp4Xh2pFGsvK1DYEaqUSxqfiuf6trArB9ktUz/ITxA oYx4Jo3yk1MFiV6icw6J3rmuc8+x4t14fYCwe4Z3cNeddfvknOVGXV+oqFAD8oGw2C /rEPG0rwhjSkw== 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 Subject: [RFC/PATCH bpf-next 1/3] bpf: Add slab iterator Date: Wed, 25 Sep 2024 15:30:21 -0700 Message-ID: <20240925223023.735947-2-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog In-Reply-To: <20240925223023.735947-1-namhyung@kernel.org> References: <20240925223023.735947-1-namhyung@kernel.org> MIME-Version: 1.0 X-Stat-Signature: hwqdd4ijkoxrc3jxfywho4dnjzk39mif X-Rspamd-Queue-Id: A82D020002 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727303426-62894 X-HE-Meta: U2FsdGVkX1/aXzG5ZszZAGeupxPCoE500p2MeLAhproe/6wkmgBqLLWOr/ujl0PSqd4uCSCWuV70FEwebr7on1x8ITj5Js70xS9A/QTyBi3MVnGtjyupkgjb7/aFGrp4VT/TBROkgKtUVk5MEjeZoOIhAz+Dfd3uBwtY6gE4Zw7bifmZijCEFU684FZPYL94rjLf7FKnn9m9z4lUAUoPBu8OFxAyriKjFe1+nfQ4QoDLaIHkkUUKkFfuLbIwBQ5oew0XPHLapevktw4LMrDwz+nukYFAjVLPIfy/fGG+vLC1PpFIb7rqvdtJH+P7Vv1VgV3Ty7SylE9LYb8xq3hUgcIbfW3kkOytmJH0fkH81d/U6wi86kysnEUaLWlryXfgntEY7bvAQ+atSUz6AIGw/kQSP5UKrqtYJFVO3tIr9Pq5ZNuXjcvyP08sBY1MY8/iXahSbIfGiR7r6b0bRjTBXoiJ+GeRCYCaY1TFyFIYnKuBqn9oLoI3lMnk6yqoSgHLfdcRK93bDBQ8rfCRL6VS/eqM2b5Ed/BKuuZ1Kh9JGhZpCIVPRuv2NVCgKwTLv8elzXl26zofyu1Fs14pdVp6WVKHhLys6nxU8bXjUU35AFjPh5sJoPds7kSqIeJU63jyi0ziezmFr2WE6Xz4XilZ8/bqD1rXwyqi8p1uOaO2OLEjy8IqBBc76mwICyfPcwPIZgwv7ob61vMnfX7UCCEI1w8iWBtsb53nUjo+B/fx5N7ksZBijcT7ti8eGEgDVsVMAO9ZJ71QfmIEqeIZpoWs21+H2XXckN3sBD31CmtW/Cd/Gk3HPX0oCWsfB+gjlzCWFo+TRWV813XMYEx0NaTpf8Ql+lrseDLgb7Yzs+RfnT1Uv9EBi02Qj5DNMnOVtaky1jOgI8TOdk5F20Aox6dSEBTrAxs6D2w5kX4pt9Dn8CflLSrNMpa9zjBaSWcBoMXQgIPim3zuOaXXYltbG+b 9QbHrgc6 TAdsU4vEVBedB7TvtG5VhlGbEmRYmrcJ4k7Mr3KbVfhlzCSukmdv/SVJHKqWNBA9hORDIvHU1NNrGj84usj/BeIcF3U7Qs4kZHL2MP8FIzbil9VDl10XHOcCEXMI5IaKRoC63Y64SjnbWbbupc0VQ1ICDPDpvo9jWAKgL0bAwHaX1Ww17ltw8pcfl4XLxBoSex2Y/2JZsBmKDXDFGKG+1OaB4S7MvjGdO4S7MeKMb7EU8nARrQm5YuuVRBmIbcpSwbd5HAnjpF3b/GjN65R6ZjIy3urIg5EAbmhjTt0PIlfWoqbL1PV99ToxulJ8wfINfm3lrzuOr8kUMxjvK2TLCjWSX4g== 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 "slab" iterator will traverse the list of slab caches (kmem_cache) and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it. The iteration will be done with slab_mutex held but it'd break and return to user if the BPF program emits data to seq buffer more than the buffer size given by the user. IOW the whole iteration would be protected by the slab_mutex as long as it won't emit anything. 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 --- include/linux/btf_ids.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/slab_iter.c | 131 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 kernel/bpf/slab_iter.c diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h index c0e3e1426a82f5c4..1474ab7f44a9cff6 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_slab_btf_id[]; #endif diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 9b9c151b5c826b31..e18b09069349e1e9 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) += slab_iter.o diff --git a/kernel/bpf/slab_iter.c b/kernel/bpf/slab_iter.c new file mode 100644 index 0000000000000000..bf1e50bd7497220e --- /dev/null +++ b/kernel/bpf/slab_iter.c @@ -0,0 +1,131 @@ +// 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__slab { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kmem_cache *, s); +}; + +static void *slab_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) + break; + + cnt++; + } + + if (cnt != *pos) + return NULL; + + ++*pos; + return s; +} + +static void slab_iter_seq_stop(struct seq_file *seq, void *v) +{ + struct bpf_iter_meta meta; + struct bpf_iter__slab ctx = { + .meta = &meta, + .s = v, + }; + struct bpf_prog *prog; + + meta.seq = seq; + prog = bpf_iter_get_info(&meta, true); + if (prog) + bpf_iter_run_prog(prog, &ctx); + + mutex_unlock(&slab_mutex); +} + +static void *slab_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct kmem_cache *s = v; + + ++*pos; + + if (list_last_entry(&slab_caches, struct kmem_cache, list) == s) + return NULL; + + return list_next_entry(s, list); +} + +static int slab_iter_seq_show(struct seq_file *seq, void *v) +{ + struct bpf_iter_meta meta; + struct bpf_iter__slab 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 slab_iter_seq_ops = { + .start = slab_iter_seq_start, + .next = slab_iter_seq_next, + .stop = slab_iter_seq_stop, + .show = slab_iter_seq_show, +}; + +BTF_ID_LIST_GLOBAL_SINGLE(bpf_slab_btf_id, struct, kmem_cache) + +static const struct bpf_iter_seq_info slab_iter_seq_info = { + .seq_ops = &slab_iter_seq_ops, +}; + +static void bpf_iter_slab_show_fdinfo(const struct bpf_iter_aux_info *aux, + struct seq_file *seq) +{ + seq_puts(seq, "slab iter\n"); +} + +DEFINE_BPF_ITER_FUNC(slab, struct bpf_iter_meta *meta, + struct kmem_cache *s) + +static struct bpf_iter_reg bpf_slab_reg_info = { + .target = "slab", + .feature = BPF_ITER_RESCHED, + .show_fdinfo = bpf_iter_slab_show_fdinfo, + .ctx_arg_info_size = 1, + .ctx_arg_info = { + { offsetof(struct bpf_iter__slab, s), + PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED }, + }, + .seq_info = &slab_iter_seq_info, +}; + +static int __init bpf_slab_iter_init(void) +{ + bpf_slab_reg_info.ctx_arg_info[0].btf_id = bpf_slab_btf_id[0]; + return bpf_iter_reg_target(&bpf_slab_reg_info); +} + +late_initcall(bpf_slab_iter_init); From patchwork Wed Sep 25 22:30:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13812498 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 A2FE8CCFA04 for ; Wed, 25 Sep 2024 22:30:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ADE386B00BC; Wed, 25 Sep 2024 18:30:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A184B6B00BD; Wed, 25 Sep 2024 18:30:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8444B6B00BF; Wed, 25 Sep 2024 18:30:29 -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 64F826B00BC for ; Wed, 25 Sep 2024 18:30:29 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1E276C1139 for ; Wed, 25 Sep 2024 22:30:29 +0000 (UTC) X-FDA: 82604705778.07.2A04DB3 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf20.hostedemail.com (Postfix) with ESMTP id 8E4D61C000B for ; Wed, 25 Sep 2024 22:30:27 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p1oejC7+; spf=pass (imf20.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 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=1727303366; 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=voPevcXBHx1UrvcL2F8jRJp/SZBhn5cwavBu4O2d+Jc=; b=07vTI01iPOhXBS4qXhc6kBbcwk5Aa76xzdL+tQ20H5dkPsQu313ai6cBOfSxa51pBtvHbv HfcojPOpes9CTxXMSPTLF4aSS5i7JuHJzN5JhhF0uAApVooHXe3+4U/z7KPvKESHfIy3hH XLehaFU43A8l2KU1L1/zBTcXhmrPtFI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p1oejC7+; spf=pass (imf20.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 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=1727303366; a=rsa-sha256; cv=none; b=6f8b3GDSqvDBzr4//v2MKMiDZX1tjgU06Vy2p6UgkKNfM1KZqsK8YHGQo5lzqle7gsJrhe dXMtkv0hLYnTsy7sCEF0Fu0orB891ZDz9rciBG0lNoKrRi8hWclOrowC1ImvWohQpyN3OY NYvRikn1605RXdMsTNvNVsanXKfJNug= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 748FDA4497A; Wed, 25 Sep 2024 22:30:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A91D4C4AF0D; Wed, 25 Sep 2024 22:30:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727303426; bh=p9xJJCK3sjdnEPQSrpwEL4Bd6PM0VmkqDDJfyawq95E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p1oejC7+XVCwNRAWMWb4liG+RlJRSHTe6i7jZnnI1bsrdWYUvIbiYRhAcYn7mK9A6 Rb6y1UTGncPTi2bKRvBzAX3S29OJmrzbFtf7uooMNt3kb1kihPEz63vK+n5Q/rDBa+ QiV/ajK12l2NTZoqyuuczq0zNb6+Hm1CM3Am1n8TZYMmCU3JaDoOzrhlsJmWKwJ3PY 6fGuKaZLfIj2SPcMsxEFeRfCgIHQ7G+lRIVWwVdQbmFZMdUw0X5UwV4Hpma8I9kJSa 4P+1U360/3PhTx5bYR5nlRI1rWBbeAE0K2/ODtT9NWqFl18NqTXV/QetfcyQSILH2I 9Z+4pLAf+dk3w== 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 Subject: [RFC/PATCH bpf-next 2/3] mm/bpf: Add bpf_get_slab_cache() kfunc Date: Wed, 25 Sep 2024 15:30:22 -0700 Message-ID: <20240925223023.735947-3-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog In-Reply-To: <20240925223023.735947-1-namhyung@kernel.org> References: <20240925223023.735947-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 8E4D61C000B X-Stat-Signature: bsuyz4q6jztfcadhrc4nq4h7ze93xprt X-HE-Tag: 1727303427-280109 X-HE-Meta: U2FsdGVkX1/1yoKQqAKoEe/gANfSgd49phauvGJUHRdzTRC8RWWG1aH5HoUbfDfQ8KbOP8cEdwIWGa78ikxwzCa9AjpdVawO0rQpFlISbDfsaBrC7l664EqmlD3jKQohRU2q5wyGn3xwkBkNlACg+0lEewL1eOA0vVbfISjd6WXyNbW7R5EiDo9/7VC+rAe2tIhJVivK2RbAKz/12TN6g8oyxTpNPERXycAD1pYdsJqc2jH8xQn3CPsE06BAJmPonxd4WEKRde+HZJ9NIb0MzafpYkeuipCY4Z7vCCxKpG/283VVS19SlvCK1Wf6MhTHFeuogFs8at8a/VXTvBGb4Kdyfk7q8HnjYrVPLnmbDTP3G9YhgWff9Lv/06a+6L7WshCCT2nVVvAKSG+6Sqae6orPN3VqNcX3PxlJ7yuRxW+IusKqNPbgWRY6/iJ5cjx84JU1WqPbvLmUTeJmjqQR/QL8VSqdhFVaydDzJkrYQLYA9qoH6XKdMnbZ23NhKKKg29W1K1kHsKB/3GByfNYXatf2yVv0B6E7/pQFklZPDaBjW8hHjVpdqT2qgqxleszrf/pXXoExGagcTvmtI49v+cBQ8Fqlg19T5PnEPooWxdvZmFNukmrzcXTVTUzcwrNUUAjKkMPnCxOl+eqxfworHQbEKPd1nkQWnBxVX9/Ckfg4gEcIjUSiBSJmNbxIdGatRKKMzTENdA2OysXJpbV2bSVI4ZUdIq0Qm9MEU+ugwoexUY0g7S3j9881CROg4qO17WB+8KCwn4vXxOi4qBDxws4XyggKe8dCfMJQVVDBPHcHHi1wkXizfzypNBI7FftsbgyJ5xWlL9Fb/rX0V04gto3w3HH2zQUcauOZ7cT/6iGg6Vm+e061IaP2SMnSRCR95/a3EICtiA1+DDAYkneJTW0SyEszFBCHBxlDofU+DOvrGxiPrxwl1izRmwbJA3pxh1WrAoyRxtMv+w1jnMu YQBptILo C9HOIk4NuDhXuMY0fJ/1u4H9CdXGrEPtgdVzb/wluughHvarLmPeNLKT0YJPDnUAXhyFpkGT8gLEjXdcAgSMsSH1C6SFkc5JpyPOXMHXcrCNFxPux0RZqFwgpW5QneLPVCkYUaKA4LrfJPuW6pX2ljaFFydvnczV3TkNfTECFy1frAL4ucTH37CX+hNTQA4FEX2HgFGbukazz3lGPfwnF+FlFmCFVVLbgVKivoMfQCxnWkSSNRNc49VbsRmJ8WO9QaRe/tCf8J+iFw2WaX6hoUMYcffOJ2pail2v0YvLHPVtvUflsYHq/fe6jEBdzDs0gtEQxwBrj/QRUu80tKZ0dak0NGcO2V6OESKjt 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_slab_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 Signed-off-by: Namhyung Kim --- kernel/bpf/helpers.c | 1 + mm/slab_common.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1a43d06eab286c26..03db007a247175c4 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_slab_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..a87adcf182f49fc4 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1322,6 +1322,20 @@ size_t ksize(const void *objp) } EXPORT_SYMBOL(ksize); +#ifdef CONFIG_BPF_SYSCALL +__bpf_kfunc_start_defs(); + +__bpf_kfunc struct kmem_cache *bpf_get_slab_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); From patchwork Wed Sep 25 22:30:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13812499 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 0ECB8CCFA06 for ; Wed, 25 Sep 2024 22:30:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3A9C6B00C0; Wed, 25 Sep 2024 18:30:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC3486B00BF; Wed, 25 Sep 2024 18:30:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3DAB6B00C0; Wed, 25 Sep 2024 18:30:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9B86C6B00BD for ; Wed, 25 Sep 2024 18:30:30 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 473E3806B0 for ; Wed, 25 Sep 2024 22:30:30 +0000 (UTC) X-FDA: 82604705820.09.EB524C3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id 8540840018 for ; Wed, 25 Sep 2024 22:30:28 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EZsDwet5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of namhyung@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=namhyung@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727303412; a=rsa-sha256; cv=none; b=dQjUUH0k0WbbNCe6EoM7xnFg90hsadVM7VSQwDuDv8bkWP2f8X9LuTXeLubjPpa2STIE2h wy66yXTPl8FjiavdP9AQayrMNfxUpIBTNdZ07BSpohdFtOP5mBlmKr4CHFJXeTEg5BYYk7 WQtxU4Het9GS5EcbEv8s2fI32Ora3Yk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EZsDwet5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of namhyung@kernel.org designates 139.178.84.217 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=1727303412; 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=o96elNwAqn+Wezce+ChfUz4XcVcycSVS58NfHbiUEFU=; b=2ZExa1DQavn78JYJUkVGs8cwFT3WPZziEDWn4RStst3xW6VVW/bxK/QSEyKgeAWlVFDntN p4n+mxHJ6//nnhj+pi0Dk/NkuAUw8BifIHRIUbArfB7NF9k6kTj37oua6Y0fI0jZI486Ip yGSRCEScjnBsZHD8s6LXc8KQUR7Zcro= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9FFA75C5E75; Wed, 25 Sep 2024 22:30:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87B17C4CECD; Wed, 25 Sep 2024 22:30:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727303427; bh=F0K+S6VZ06qBNMDXblIhDt1PDc7u3rtDzvje3N0WCyk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EZsDwet5lz1zxqaEpGuxsFM2AJIGYVu0wR8Fx4aoPVtBDiXpyXhUR9ER1ai+Q1eGs 5kmLrSdWia5+BA/FXlCLaefbVT0KDVM4BPenIWg8jIGrp1Vs0j3lVqq7iX51Dwp4Yp Q+UBtdYTE7BOYfrB1xbAhHSuFnO8CTo9FPkJP+1ZjACDAHNr6VS6wcOlGN5vzibLWo kq68VeU/qIDJWg3GeM+5xE9HlOK2yPd7QWWXkT31X2QG/WBzTLEEBuRa2YS+7N3Pk0 4nIxGd/JqIn0aypxOPksiiahT/XXzdveSurDU8E1egdGUN+lI8yzg0lbIq0J6qcHOE 1vr7YVNJkG2nw== 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 Subject: [RFC/PATCH bpf-next 3/3] selftests/bpf: Add a test for slab_iter Date: Wed, 25 Sep 2024 15:30:23 -0700 Message-ID: <20240925223023.735947-4-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog In-Reply-To: <20240925223023.735947-1-namhyung@kernel.org> References: <20240925223023.735947-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 1hwfj531kee5gehgf3qgffbkhr1exkbf X-Rspamd-Queue-Id: 8540840018 X-Rspamd-Server: rspam02 X-HE-Tag: 1727303428-662593 X-HE-Meta: U2FsdGVkX18kn2IKvz0WfRN6uUAgTtCiG3HJwFtEICggMrBi7DFbo99OeTZ437Iq9ZxUQGLPyUjxLfQJNnX9qTVdq4PcoxK8UJYH6AyHGU6FlTviN8cY6p6BmAcRmBpetluw0BY6nMqbgYFP3Vwhg59f3tXKeqki8X5nHX0OSWVia9TSfarE8VlGXKju+7iXE7JkWYghyk125/rO/jpencAkWjNCrO9tg4y83iWmFk/OsKFVhQD0OaSbrgpi1ljYimBIyuEJFrJUE/wFrKpJUccgDUZHqO+L4C8yPRszf7prN8FV47W3qvwoBueGiZt0FNnj/Y1GIXzs++Z6w5x6PKd5QZNvyBzyuOIHInu+m4bMYRZXNdlqQLzSW6CG8tsFbyOTuSM5hbEnHzHhqBnUmB4hmOKaUPoUk1E2RJ+qmPqEvfQQMSBvWqfirzzrvYQndZezcLlB/2abNlfLjHWUdp4FYsuVyNnKBEfJGYi0RwPnkFRCCgBfMs1JjaAt0/uroSnnyIell64HWJogoS5MC54VpNPWyvIBTrsqaE4ntXZ+dKIOpEjPQGUarogb3+/AVgOHgzwbSv9MkbRTd6mFz0hFuX3juqgrlUQq9eAqGmLcTB6zZRAkLgUhvvNdnmpi6PJMtE9FY5Wy+ThYbBuCwmmRmK7WYanG57qHxibu2P0/yxT346w7jI4OJoERT15tZm2YgsuqSXu1auZMVpsx55d3V1K7eRR8GyyQ1mZiTcOAlFR+ZzANU3HFepsO2hoGabX+4hME5P47EninifxoZOZ08XfxgcgZ4/APtkQi2GuT9ZdAc19Dxpqo3j3GDGd3p4UFKZi0dKPpOrF5JlYfaAOzxW7r7Z9FMf0lctmu13fZRz2lSJCEYjuaTFxfR/gWF7Gu4qa0dHjoB53V6jczPklKK+27TXRuWuitLzhq5GkOzHoFhip8xQV09FpsdUbYmnXWCw0HivVS6x+vHko 368JP5Cq VZ60rkkCFrtxUWTCHYL/aWcZJ0T9SKF78v0KsLhoPyxGBiinsS3Rb+lc2OYXvVeMKmlkB8Nmq2lZbk21ZSlLMG6Y86oyzm5fkCnrzbiErbnC7LgF0HWDBZi/YuQCifW5ssqp96EmLIs/haVoIQONYxbXGeInSTZgItO8fHiNpllE3ad/acBQsmP5gB8zqWcy0S8DwfhE9sTEnfX0Ioy3os/iBY2YIMt/6WNDjTk2phwVFlDrqcQ+hVyq24AmUvkeUOnazmII6Kzkhuv/mXYO8huf0tuADwJnK9L7jRMOFNdjqRALiiAWC2DKtv/ZNRnNPQApVqngzZ+NKjNCUGJc7uVohDvtcDciLyu/Zc1v+94Iy6nd+TPUjoBEMzaPPOzXZNZXNZDAhaeTT+R8= 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 slab_iter and check if current task's pointer is from "task_struct" slab cache. Signed-off-by: Namhyung Kim --- .../selftests/bpf/prog_tests/slab_iter.c | 64 +++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 ++ tools/testing/selftests/bpf/progs/slab_iter.c | 62 ++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/slab_iter.c create mode 100644 tools/testing/selftests/bpf/progs/slab_iter.c diff --git a/tools/testing/selftests/bpf/prog_tests/slab_iter.c b/tools/testing/selftests/bpf/prog_tests/slab_iter.c new file mode 100644 index 0000000000000000..e461f6e703d67dc8 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/slab_iter.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google */ + +#include +#include +#include +#include "slab_iter.skel.h" + +static void test_slab_iter_check_task(struct slab_iter *skel) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts, + .flags = BPF_F_TEST_RUN_ON_CPU, + ); + 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_slab_iter(void) +{ + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + struct slab_iter *skel = NULL; + union bpf_iter_link_info linfo = {}; + struct bpf_link *link; + char buf[1024]; + int iter_fd; + + skel = slab_iter__open_and_load(); + if (!ASSERT_OK_PTR(skel, "slab_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_slab_iter_check_task(skel); + + close(iter_fd); + +free_link: + bpf_link__destroy(link); +destroy: + slab_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..33ea7181e1f03560 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__slab bpf_iter__slab___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__slab 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__slab { + struct bpf_iter_meta *meta; + struct kmem_cache *s; +} __attribute__((preserve_access_index)); diff --git a/tools/testing/selftests/bpf/progs/slab_iter.c b/tools/testing/selftests/bpf/progs/slab_iter.c new file mode 100644 index 0000000000000000..f806365506851774 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/slab_iter.c @@ -0,0 +1,62 @@ +// 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_slab_cache(__u64 addr) __ksym; + +/* result, will be checked by userspace */ +int found; + +SEC("iter/slab") +int slab_info_collector(struct bpf_iter__slab *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_slab_cache(curr); + if (s == NULL) + return 0; + + name = bpf_map_lookup_elem(&slab_hash, &s); + if (name && !bpf_strncmp(name, 11, "task_struct")) + found = 1; + + return 0; +}