From patchwork Wed Oct 2 18:09:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13820194 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 BE82CCEACF3 for ; Wed, 2 Oct 2024 18:10:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 523426B046B; Wed, 2 Oct 2024 14:10:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D3C66B046C; Wed, 2 Oct 2024 14:10:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 374C16B046D; Wed, 2 Oct 2024 14:10:01 -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 0BE926B046B for ; Wed, 2 Oct 2024 14:10:01 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9D8A0AC4DC for ; Wed, 2 Oct 2024 18:10:00 +0000 (UTC) X-FDA: 82629450960.06.15655BF Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf17.hostedemail.com (Postfix) with ESMTP id 175DC40009 for ; Wed, 2 Oct 2024 18:09:58 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gci2uiPh; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.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=1727892504; a=rsa-sha256; cv=none; b=RT0w369gGls9nw0Pn1EuKOvVhOvhuRTpOleMIiQFWXhWZ9bu2W+cCAha2YyCTpWZII9bfS pSaF4XJZtNICN9cDZ9GdDE0WAuGR2n0iENfYVEyVaxh7B5ByBLkbWLXp7/xiOv6vaJa3U2 Rx9FLTVQZuGMKdlQolB7Bp3BICo5tkk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gci2uiPh; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf17.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=1727892504; 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:references:dkim-signature; bh=aWpMj0cbr/6anlgoF75nxSXDKfdtyDtRZhIadtRqvhY=; b=mMCF9tNsnIRM2NR7VOEM7sKycLeU4gM0HucjfNpb0n2Gaxo+T21GvkjOYM4toydpi457BU T3NH+MOMHbLtL837wyLeXyI35QFXtIteXdJYMEZJwH59b/FOFsKMouIq5g+rL1UDYtwam2 64XvXMFLMfJ03onrmKd1wNiy1zTzwjk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id C2865A427CA; Wed, 2 Oct 2024 18:09:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED49DC4CEC2; Wed, 2 Oct 2024 18:09:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727892597; bh=5eYoXNjoAptQV3+YkIGOlz/sVoK3uR5EITHgEaQvhsQ=; h=From:To:Cc:Subject:Date:From; b=gci2uiPhUT9t0xewWeL2xmBbcV81KvRX7tw5ZQp8LokAgu2jik94G7BYkQEBZwvsl RZPAq/xaJ+r46ovuf7f90fWc3HSytNNmngedjWqdm0PN7eAuFQUKmhRPveD7KUjGm4 D0XOI8pPiyU2SGVIKpkWZNsbSkAL+VktDknCRNWdZIeMwbQcUe7PkyHytG4a3amxBn VS7xWrmj9+4WrWlZ3h0YLyzFZlmZOeDsf+6hFXUG0Pv3XmDyl0aZdiNzqNefCDMvKA VeHJQiJfFJ1kpg/KYtYNqLNORNkuYhn8QuNaEOtK3ktRrlVNVfWMavF3BylAoBgy7J 9QJMe9Cw4n43w== 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 0/3] bpf: Add kmem_cache iterator and kfunc Date: Wed, 2 Oct 2024 11:09:53 -0700 Message-ID: <20241002180956.1781008-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog MIME-Version: 1.0 X-Rspamd-Queue-Id: 175DC40009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: wczmruaxi3dkcj3ih1seia6xyb3if94b X-HE-Tag: 1727892598-991616 X-HE-Meta: U2FsdGVkX1+8a+LChqhGRNp/U/C1zzcb7veJRWuq40g0PLFcyEwl/DzgL59Wqz1+hA2MikxTONK44Hx67d1lfOHML+dWtRj/oIvU7HT3mQKitfBgIUEL/CGzb0II1wPLT0usXZxnuSkNYTLfkNd7nyLysYMsuYiamH2XxSLxu1i1N8xAzqJIaUQfoVceUqCj5zpkcZ16kkrheozIRxMQUQb+TxUi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ynpJVJp2Y7t5lyMdzKqReJcfGAmJH7vz8 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: Hello, I'm proposing a new iterator and a kfunc for the slab memory allocator to get information of each kmem_cache like in /proc/slabinfo or /sys/kernel/slab in more flexible way. v4 changes) * skip kmem_cache_destroy() in kmem_cache_iter_seq_stop() if possible (Vlastimil) * fix a bug in the kmem_cache_iter_seq_start() for the last entry v3: https://lore.kernel.org/lkml/20241002065456.1580143-1-namhyung@kernel.org/ * rework kmem_cache_iter not to hold slab_mutex when running BPF (Alexei) * add virt_addr_valid() check (Alexei) * fix random test failure by running test with the current task (Hyeonggon) v2: https://lore.kernel.org/lkml/20240927184133.968283-1-namhyung@kernel.org/ * rename it to "kmem_cache_iter" * fix a build issue * add Acked-by's from Roman and Vlastimil (Thanks!) * add error codes in the test for debugging v1: https://lore.kernel.org/lkml/20240925223023.735947-1-namhyung@kernel.org/ My use case is `perf lock contention` tool which shows contended locks but many of them are not global locks and don't have symbols. If it can tranlate the address of the lock in a slab object to the name of the slab, it'd be much more useful. I'm not aware of type information in slab yet, but I was told there's a work to associate BTF ID with it. It'd be definitely helpful to my use case. Probably we need another kfunc to get the start address of the object or the offset in the object from an address if the type info is available. But I want to start with a simple thing first. The kmem_cache_iter iterates kmem_cache objects under slab_mutex and will be useful for userspace to prepare some work for specific slabs like setting up filters in advance. And the bpf_get_kmem_cache() kfunc will return a pointer to a slab from the address of a lock. And the test code is to read from the iterator and make sure it finds a slab cache of the task_struct for the current task. The code is available at 'bpf/slab-iter-v4' branch in https://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git Thanks, Namhyung Namhyung Kim (3): bpf: Add kmem_cache iterator mm/bpf: Add bpf_get_kmem_cache() kfunc selftests/bpf: Add a test for kmem_cache_iter include/linux/btf_ids.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/helpers.c | 1 + kernel/bpf/kmem_cache_iter.c | 174 ++++++++++++++++++ mm/slab_common.c | 19 ++ .../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 +++++++ 8 files changed, 333 insertions(+) create mode 100644 kernel/bpf/kmem_cache_iter.c 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 base-commit: 9502a7de5a61bec3bda841a830560c5d6d40ecac