From patchwork Fri Sep 27 18:41:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13814483 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 D30FBCDD1D2 for ; Fri, 27 Sep 2024 18:41:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2034C6B0126; Fri, 27 Sep 2024 14:41:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B2EA6B0127; Fri, 27 Sep 2024 14:41:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07B376B0128; Fri, 27 Sep 2024 14:41:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id DD05A6B0126 for ; Fri, 27 Sep 2024 14:41:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 61650A129D for ; Fri, 27 Sep 2024 18:41:38 +0000 (UTC) X-FDA: 82611386676.24.72925D5 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf06.hostedemail.com (Postfix) with ESMTP id B975418000C for ; Fri, 27 Sep 2024 18:41:36 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="K15c/Fa4"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.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=1727462479; a=rsa-sha256; cv=none; b=PbWrLTPiDbD73Rnuh5sr0xEkPE4j9Cc4QMp9UNgOMKdNfNuDfOmrFNaxYB92/1w4dZZrJQ vw1Jli0aBLNyvKKdEAbTkx5VTqQ462H3NIlVXU27Da0CPYxqc6xWU0ptUZLgdp9qyJs5FK ebZrnD1M4gOZpMzajvwTJgd7YzP+OdQ= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="K15c/Fa4"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.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=1727462479; 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=54EAhOmuO/R4cNVQhUa2/jSTtN3uRkmgUp+xJpe8Akw=; b=1f/K7BJH4mwhtajteay/eBe/8D/cLLmvBBPiVRh2hadXtxEtr75iPi/MFMN+RFdkrrAYdk 85M4TS9VFzsIC1ethYm0hJYeJG200vO9e1a0O6jrEhOcTKYmzNROsTWVbpwv+zIW/ypc8w 7LtjWOtoQemvyjXPOsf4RB8z4yyD8go= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id B6984A459D7; Fri, 27 Sep 2024 18:41:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A11AC4CED1; Fri, 27 Sep 2024 18:41:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727462495; bh=djFsDLX/zPhywfaWe9kuIOBlVP1tbaN3Cb2qCVNHtag=; h=From:To:Cc:Subject:Date:From; b=K15c/Fa4IssMrnMcNRtDZ4ZfJY2zLaXDrpGgEnHpYKLoiLIuQzcNqrj1tG7KIjkZz n7sigAgo8SL8tdVuT2SHNI3ncG1dPKKLs7J6uysXouXstWbSwMgtjLIYm9D1LE4joJ pnY8kqsiunrDXDfewGRp4JTB1gYaowM8E/WDFHhaNJsl93YmCrl9gMK/8YEAYYOMwg cuyBbOGQ+SL08sVCMjiJw9NWd4xLVfKLM4oAphLEAw/EnCISkPoW8Ds5WOU/8o0ktU CgsotGk/rxKx7MJgsRExdnv/Co9p8I1aKxETboVQa9qHoU2oHruIQHN0nKydDhYHpA 2LkyMOcgAhWCQ== 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 0/3] bpf: Add kmem_cache iterator and kfunc (v2) Date: Fri, 27 Sep 2024 11:41:30 -0700 Message-ID: <20240927184133.968283-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: jdohgqogy4rof1ajh4pja9a8c6o3m91a X-Rspamd-Queue-Id: B975418000C X-Rspamd-Server: rspam02 X-HE-Tag: 1727462496-178988 X-HE-Meta: U2FsdGVkX1/n2NpsqvNqjEDguy8U4DMgwfylsAnkCnXKl0duCgwy1vTnvuZlmdHU0EJYuQgYm2Vc1DmYf9XWjAP6IuZ0W2n3HHdeNhPETzMcf5svhpM0yhzDl/Sv18wLgRy0l9DUtKf0GmkriBye81L66aKsJ3W1X9kPTQpn/tkHVyZipDInZbaWCrI6pBUAN4C5ii+kdL6XH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Wb9Vg1r6R6djB32ksE/kfl9WZi/J0gs3a/g7YvkF70Z6SIv8AFPfghCxvmn 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. v2 changes) * 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 slab_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_slab_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-v2' 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 | 131 ++++++++++++++++++ mm/slab_common.c | 16 +++ .../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, 287 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