From patchwork Wed Oct 2 06:54: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: 13819402 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 B2AB4CF3195 for ; Wed, 2 Oct 2024 06:55:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 356A46B02DD; Wed, 2 Oct 2024 02:55:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DFCA6B0309; Wed, 2 Oct 2024 02:55:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 133FF6B0307; 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 C0C026B02D5 for ; Wed, 2 Oct 2024 02:55:02 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1C662ACF74 for ; Wed, 2 Oct 2024 06:55:02 +0000 (UTC) X-FDA: 82627750044.16.2374130 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf15.hostedemail.com (Postfix) with ESMTP id 89077A0002 for ; Wed, 2 Oct 2024 06:54:59 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cnrpS7VG; spf=pass (imf15.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=1727851971; 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=5NncNmsHHIk++jzKcXxeYn+yzIfDcuXRMs/grziV9+A=; b=fsr6ITyyiRTyjT8qu5EHNQYC2hZ/lTUBZQ6p40X8300BINwKKsZqC8wDZwPsUMVVEef7Nb tUs2eGQYm7awvN7vpfCKBpLLt6gTSW7UA5lfcxP6Ach1B5MBrHtun0UUhwxwFY+bIBZ8A8 iJoXaVV/pm6DLIkhNlL30f80iR0HCKQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727851971; a=rsa-sha256; cv=none; b=5BB7biR5zMfU6P8msBoyHLoFGraT+uCfdUzAwZy6Y4PCMfju3EV6rC4BX8fjNtbDrhXvQG j1Grc2Cq6kdreioVkvS2EUxC5RfjbiRooIyY3fi7VMJMaXcWWfsP9ctwsNdEcRmyaxlArK msw70x2A5T5njGrpDwus9KBaxSUu71M= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cnrpS7VG; spf=pass (imf15.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 2679AA43792; Wed, 2 Oct 2024 06:54:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4FA26C4CEC5; Wed, 2 Oct 2024 06:54:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727852098; bh=ikD39xTNdK1VPFn+b9rmFZukD7yDctdhfRaFL9vvfSI=; h=From:To:Cc:Subject:Date:From; b=cnrpS7VGnmvG/4KZpDiB9fb65Tzxt2vVukuCUEeV9RoTxMgqZ14jQpjTlxG3e/lR3 6Qwo9CPncVy4LKXq34pt3A8nt0Ge/KLoZYtfXsPXwuKImbCw0rtqszcsYCzYSVkXDI k8O89gKWHdEITW4akKeMJJywZ/1zkP3RFCRtIg/LnPpmmh+DqrCLJTwYvapeUODaPU noSjA9/gw6+dcA45pyNizHifqg6orWWVEHlO1ehFRQKAX84u2BEwmUTckS4WCoyv6O ZJkhMWZ8vLyFm/yq7If+hk0Pz4+USYnn5mJ7JgPmfsueCWWRilqIoyK+cKVFp0ilc4 srq2PzqcIFJXQ== 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 0/3] bpf: Add kmem_cache iterator and kfunc Date: Tue, 1 Oct 2024 23:54:53 -0700 Message-ID: <20241002065456.1580143-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 89077A0002 X-Stat-Signature: b6fat7p3swbdhoqomhs1dmubondf8gxz X-HE-Tag: 1727852099-4821 X-HE-Meta: U2FsdGVkX1+LJXO0+V47hRu1hF21ib6MLGDKnQUcr1ozRe+6q7XLoLN7DrozM5wwrULKtBvzvxcr84O+CtSF7bhiHWV9cwBie8ySOYDsOKxbpl4VkmIbI7T5dnMeSPkicDXsAsp2VsbiZ+R9hmhSGX77scNaaS0lQ0yn2QWx9CAh5d7a93bv3DXxMZ0xW8kTi5MFtlkn+syqu4uau3fLBQ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/aXQ7aR+X2HjouWdk3oPSfqF/l5VUevIeodpJ9ruWoN/v6wo1+QrwmF4Hd 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. v3 changes) * 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-v3' 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 | 165 ++++++++++++++++++ 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, 324 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