From patchwork Wed Sep 25 22:30:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13812496 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 C8331CCFA04 for ; Wed, 25 Sep 2024 22:30:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FAD76B00B9; Wed, 25 Sep 2024 18:30:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5AB4A6B00BA; 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 472546B00BC; Wed, 25 Sep 2024 18:30:28 -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 2A6916B00B9 for ; Wed, 25 Sep 2024 18:30:28 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CEB80C06D7 for ; Wed, 25 Sep 2024 22:30:27 +0000 (UTC) X-FDA: 82604705694.09.33641BD Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id 3BBC9A000F for ; Wed, 25 Sep 2024 22:30:26 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Nog7Mu8r; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.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=1727303390; a=rsa-sha256; cv=none; b=4zzMTcFHsIMzqU+SAkQq4h3Zkca59udMCP7U2/BVpkWkKPyzcM7tDG45Ai3wPay8ioLRLx S/KaGpueVTJa+W0IryFE6hgPTtM79NmVvsGL3oW5yu5WZXmYE2V/3sdJmwnEoiJMc8I/ZE ZCkVHXarDE3sAknJgXKvp3iYEob+xdU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Nog7Mu8r; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.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=1727303390; 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=qJhXPLE2VVnI0XS5gBpcMazuTT3eS+3YvlO+XOk1KjQ=; b=l/kbDg7lIwu1jyXk5nOJS0YY0BZ6ik/INYijJNTU+KUKPXImCtsuMpxXQhf9OtpbjbKyhC otilYeYFPnb7arMjcpuiYAbl7PDJftJdUayuaq/P0U+xBSoenHuJ6gOtPlKyHRBgv6IOpP 8TCocXgk5ukR3s2DBySmX6zux7yUn0A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1447D5C5E31; Wed, 25 Sep 2024 22:30:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF73CC4CEC3; Wed, 25 Sep 2024 22:30:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727303424; bh=/wpNuyYlQgTLd8to7yv58RW9K/fnJNPPR7Yi9IPGjww=; h=From:To:Cc:Subject:Date:From; b=Nog7Mu8ryCZIbKiU+7l2FP2MewyEIKi52moHSJyUsIYX3gegdE3ejjnLfaq5M0o/b a6deA05wMrLbyO0tzRI82+imB0c5GsmMLOvgsVwTiuHyrAC3K4i8XgZrY0qFBFCh2A l7dwnt91wtbhHEinc9g9ObRIRZpZrQxJiDqwhaKE0Z//BwFYEmwdf+sN3mRnsXDwDL 8JpdKFXAq41biEuyplUCfv27kj6yZ6zSg56wxIFKxNIRbeJAMzV4pSHFQdjfz+H67s fRCcXQjwTn7KsM+dF00n2Hh+TmUxzVH7IykxOgE3lf21yWDlIiNm07GijRmr87+kbz k09AM+jhu3u5A== 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 0/3] bpf: Add slab iterator and kfunc (v1) Date: Wed, 25 Sep 2024 15:30:20 -0700 Message-ID: <20240925223023.735947-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 3BBC9A000F X-Rspamd-Server: rspam01 X-Stat-Signature: p645xrkt3oynw47ekwm9x91bzeuhebfq X-HE-Tag: 1727303426-152400 X-HE-Meta: U2FsdGVkX1/uFH7cJjMCS1pdYXSYwihwqHuqt22crfcjBbOqzoXrEztitG0ARQANRXn2dZjQ175rgNRTKJ2Hmhg1lAm2SKoCq4EGYrCCO8Aqb5LlP3lB0kGqCc+Rd6uLrXyX5C77iIt2G3qq4ruTUDTedR7y4TagNsjeGMVvDnE7a5O65rNRqzEeIPAF1rchGrI+m1T98l67UFXF0x/pMXB2eje3IVb/Tj9K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, 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. Maybe I need to call it kmem_cache iter but slab was short and easier to call. :) 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-v1' branch in https://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git Thanks, Namhyung Namhyung Kim (3): bpf: Add slab iterator mm/bpf: Add bpf_get_slab_cache() kfunc selftests/bpf: Add a test for slab_iter include/linux/btf_ids.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/helpers.c | 1 + kernel/bpf/slab_iter.c | 131 ++++++++++++++++++ mm/slab_common.c | 14 ++ .../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 +++++++++ 8 files changed, 281 insertions(+) create mode 100644 kernel/bpf/slab_iter.c create mode 100644 tools/testing/selftests/bpf/prog_tests/slab_iter.c create mode 100644 tools/testing/selftests/bpf/progs/slab_iter.c Acked-by: Roman Gushchin (mm/*) Acked-by: Vlastimil Babka #mm/slab