From patchwork Thu Oct 10 23:25:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13831300 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 363A2D2444F for ; Thu, 10 Oct 2024 23:25:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 975DE6B0083; Thu, 10 Oct 2024 19:25:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 925216B0088; Thu, 10 Oct 2024 19:25:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EC286B0089; Thu, 10 Oct 2024 19:25:10 -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 612EB6B0083 for ; Thu, 10 Oct 2024 19:25:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5D56E1204CE for ; Thu, 10 Oct 2024 23:25:06 +0000 (UTC) X-FDA: 82659275538.14.3169983 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id 2E0E64000D for ; Thu, 10 Oct 2024 23:25:06 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=giVnXvp+; spf=pass (imf01.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=1728602570; 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=3qn98BjlzK/JNVd9yn0wW08ygBS7HuacPHCR7lzW/Zc=; b=fO6Fqr+2iC5PhH/PT1Bzkdwkvpao1Uv6cVfx2Zwc1YDP971lXhGMwzB6wMYmJnmHjkQqRO 8+KmVBbqt96SqbvPDnqncpzTqArJQQ8BrX9l95sLUXXG6YumdJhA3N9hIbFoEtuS9X4cYI XmMVGsVz1JVqWRCYiETpejj+Jfihqi8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728602570; a=rsa-sha256; cv=none; b=GrUIZsSUNbnaP0C5IteEk2z7k1vWR5nsufp15mcoazbnENySMz7AdBpNObD7hoKB4rnQ9s Q67E1je478P7T3pfjt5wIpbI7G89gZglA16YstJye02+YX5N1G1OGXrCJa3uvCSckCamxA 6m+Y0B8yyLNdxthHrKiF0bWx9CS/P10= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=giVnXvp+; spf=pass (imf01.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 F11595C5FF6; Thu, 10 Oct 2024 23:25:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C3C1C4CEC5; Thu, 10 Oct 2024 23:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728602707; bh=SkeUhfgqXNji7w4xxQutN92ENbwE2DcNcHbbwYFp+6c=; h=From:To:Cc:Subject:Date:From; b=giVnXvp+04utXzc16EEnQh68Lf1FrjoJCHHdyNXB+Rz0+ecy3XTuXiE2xvypxdgqu 2MMZkEahxm8oMQEBMwnVGTf+VsHNh2hK3xoA75I2YLTqnesMiBeCs9/9Ad6Ht/xdqr U4hzC2UgVZ1wmjT6D0Tbl4k9N2uYkwON+D2wNB5sygNUpRMkdHNwRveHWsMRrmElks O0TK8Ato9cWzyOAOqiQcIJUMlLWsO3BUWRhIx+MoYifYNS7XFi5Jy/PhyAheSLhVaQ A7Ai8un3gjGgF3B5dZvcM1skdqwqkXEw/Rd4Cy7soqGqjJb8MQfsNBgvPvsCVUNKkv GbH44iX92QfgQ== 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 , "Paul E. McKenney" Subject: [PATCH v5 bpf-next 0/3] bpf: Add kmem_cache iterator and kfunc Date: Thu, 10 Oct 2024 16:25:02 -0700 Message-ID: <20241010232505.1339892-1-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2E0E64000D X-Stat-Signature: hf4pwrhcaow1fkdif1651akgq6hxehyo X-HE-Tag: 1728602706-512118 X-HE-Meta: U2FsdGVkX187O3s2v5Z8KFrPvk50BYXCrMIv4ZtwH4gGbEEH7LTohmEJO0/QtE2G7tLhmfmDwzd/rRxgw+cDrfTv2PJ10Aq3pntkyYZxHVKZBk192ehZPeN6a8GChUSBjMNUkeRpTL+8DlEC4fn1G/qonQ+K+Pb5bLn7v/akO19LJPIjk9pugu8ashCUQ/JT9OVM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3PPDoABRXqknQLV+Iapv7Wxxcej2Rz0sTBGrxAF7MYSP7GhS6lzNW6yd259QshlXFLN0rqh3EJeZ8hkZNYwS 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. v5 changes) * set PTR_UNTRUSTED for return value of bpf_get_kmem_cache() (Alexei) * add KF_RCU_PROTECTED to bpf_get_kmem_cache(). See below. (Song) * add WARN_ON_ONCE and comment in kmem_cache_iter_seq_next() (Song) * change kmem_cache_iter_seq functions not to call BPF on intermediate stop * add a subtest to compare the kmem cache info with /proc/slabinfo (Alexei) v4: https://lore.kernel.org/lkml/20241002180956.1781008-1-namhyung@kernel.org * 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. Actualy I'm not sure about the RCU lock - IIUC it doesn't protect the kmem_cache itself but kmem_cache_destroy() calls some RCU barrier functions, so having RCU read lock would protect the object from going away by kfree_rcu() or something and then kmem_cache. But please correct me if I'm wrong. 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-v5' 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 | 175 ++++++++++++++++++ kernel/bpf/verifier.c | 5 + mm/slab_common.c | 19 ++ .../bpf/prog_tests/kmem_cache_iter.c | 115 ++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 + .../selftests/bpf/progs/kmem_cache_iter.c | 95 ++++++++++ 9 files changed, 419 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: 5bd48a3a14df4b3ee1be0757efcc0f40d4f57b35