From: "T.J. Mercier" <tjmercier@google.com>
To: sumit.semwal@linaro.org, christian.koenig@amd.com,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, skhan@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-doc@vger.kernel.org, bpf@vger.kernel.org,
linux-kselftest@vger.kernel.org, android-mm@google.com,
simona@ffwll.ch, corbet@lwn.net, eddyz87@gmail.com,
song@kernel.org, yonghong.song@linux.dev,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me,
jolsa@kernel.org, mykolal@fb.com,
"T.J. Mercier" <tjmercier@google.com>
Subject: [PATCH 2/4] bpf: Add dmabuf iterator
Date: Mon, 14 Apr 2025 22:52:25 +0000 [thread overview]
Message-ID: <20250414225227.3642618-3-tjmercier@google.com> (raw)
In-Reply-To: <20250414225227.3642618-1-tjmercier@google.com>
The dmabuf iterator traverses the list of all DMA buffers. The list is
maintained only when CONFIG_DEBUG_FS is enabled.
DMA buffers are refcounted through their associated struct file. A
reference is taken on each buffer as the list is iterated to ensure each
buffer persists for the duration of the bpf program execution without
holding the list mutex.
Signed-off-by: T.J. Mercier <tjmercier@google.com>
---
include/linux/btf_ids.h | 1 +
kernel/bpf/Makefile | 3 +
kernel/bpf/dmabuf_iter.c | 130 +++++++++++++++++++++++++++++++++++++++
3 files changed, 134 insertions(+)
create mode 100644 kernel/bpf/dmabuf_iter.c
diff --git a/include/linux/btf_ids.h b/include/linux/btf_ids.h
index 139bdececdcf..899ead57d89d 100644
--- a/include/linux/btf_ids.h
+++ b/include/linux/btf_ids.h
@@ -284,5 +284,6 @@ extern u32 bpf_cgroup_btf_id[];
extern u32 bpf_local_storage_map_btf_id[];
extern u32 btf_bpf_map_id[];
extern u32 bpf_kmem_cache_btf_id[];
+extern u32 bpf_dmabuf_btf_id[];
#endif
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 70502f038b92..5b30d37ef055 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -53,6 +53,9 @@ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o
obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o
obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
+ifeq ($(CONFIG_DEBUG_FS),y)
+obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o
+endif
CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE)
CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE)
diff --git a/kernel/bpf/dmabuf_iter.c b/kernel/bpf/dmabuf_iter.c
new file mode 100644
index 000000000000..b4b8be1d6aa4
--- /dev/null
+++ b/kernel/bpf/dmabuf_iter.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Google LLC */
+#include <linux/bpf.h>
+#include <linux/btf_ids.h>
+#include <linux/dma-buf.h>
+#include <linux/kernel.h>
+#include <linux/seq_file.h>
+
+BTF_ID_LIST_GLOBAL_SINGLE(bpf_dmabuf_btf_id, struct, dma_buf)
+DEFINE_BPF_ITER_FUNC(dmabuf, struct bpf_iter_meta *meta, struct dma_buf *dmabuf)
+
+static void *dmabuf_iter_seq_start(struct seq_file *seq, loff_t *pos)
+{
+ struct dma_buf *dmabuf, *ret = NULL;
+
+ if (*pos) {
+ *pos = 0;
+ return NULL;
+ }
+ /* Look for the first buffer we can obtain a reference to.
+ * The list mutex does not protect a dmabuf's refcount, so it can be
+ * zeroed while we are iterating. Therefore we cannot call get_dma_buf()
+ * since the caller of this program may not already own a reference to
+ * the buffer.
+ */
+ mutex_lock(&dmabuf_debugfs_list_mutex);
+ list_for_each_entry(dmabuf, &dmabuf_debugfs_list, list_node) {
+ if (file_ref_get(&dmabuf->file->f_ref)) {
+ ret = dmabuf;
+ break;
+ }
+ }
+ mutex_unlock(&dmabuf_debugfs_list_mutex);
+
+ return ret;
+}
+
+static void *dmabuf_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+{
+ struct dma_buf *dmabuf = v, *ret = NULL;
+
+ ++*pos;
+
+ mutex_lock(&dmabuf_debugfs_list_mutex);
+ dma_buf_put(dmabuf);
+ while (!list_is_last(&dmabuf->list_node, &dmabuf_debugfs_list)) {
+ dmabuf = list_next_entry(dmabuf, list_node);
+ if (file_ref_get(&dmabuf->file->f_ref)) {
+ ret = dmabuf;
+ break;
+ }
+ }
+ mutex_unlock(&dmabuf_debugfs_list_mutex);
+
+ return ret;
+}
+
+struct bpf_iter__dmabuf {
+ __bpf_md_ptr(struct bpf_iter_meta *, meta);
+ __bpf_md_ptr(struct dma_buf *, dmabuf);
+};
+
+static int __dmabuf_seq_show(struct seq_file *seq, void *v, bool in_stop)
+{
+ struct bpf_iter_meta meta = {
+ .seq = seq,
+ };
+ struct bpf_iter__dmabuf ctx = {
+ .meta = &meta,
+ .dmabuf = v,
+ };
+ struct bpf_prog *prog = bpf_iter_get_info(&meta, in_stop);
+
+ if (prog)
+ return bpf_iter_run_prog(prog, &ctx);
+
+ return 0;
+}
+
+static int dmabuf_iter_seq_show(struct seq_file *seq, void *v)
+{
+ return __dmabuf_seq_show(seq, v, false);
+}
+
+static void dmabuf_iter_seq_stop(struct seq_file *seq, void *v)
+{
+ struct dma_buf *dmabuf = v;
+
+ if (dmabuf)
+ dma_buf_put(dmabuf);
+}
+
+static const struct seq_operations dmabuf_iter_seq_ops = {
+ .start = dmabuf_iter_seq_start,
+ .next = dmabuf_iter_seq_next,
+ .stop = dmabuf_iter_seq_stop,
+ .show = dmabuf_iter_seq_show,
+};
+
+static void bpf_iter_dmabuf_show_fdinfo(const struct bpf_iter_aux_info *aux,
+ struct seq_file *seq)
+{
+ seq_puts(seq, "dmabuf iter\n");
+}
+
+static const struct bpf_iter_seq_info dmabuf_iter_seq_info = {
+ .seq_ops = &dmabuf_iter_seq_ops,
+ .init_seq_private = NULL,
+ .fini_seq_private = NULL,
+ .seq_priv_size = 0,
+};
+
+static struct bpf_iter_reg bpf_dmabuf_reg_info = {
+ .target = "dmabuf",
+ .show_fdinfo = bpf_iter_dmabuf_show_fdinfo,
+ .ctx_arg_info_size = 1,
+ .ctx_arg_info = {
+ { offsetof(struct bpf_iter__dmabuf, dmabuf),
+ PTR_TO_BTF_ID_OR_NULL },
+ },
+ .seq_info = &dmabuf_iter_seq_info,
+};
+
+static int __init dmabuf_iter_init(void)
+{
+ bpf_dmabuf_reg_info.ctx_arg_info[0].btf_id = bpf_dmabuf_btf_id[0];
+ return bpf_iter_reg_target(&bpf_dmabuf_reg_info);
+}
+
+late_initcall(dmabuf_iter_init);
--
2.49.0.604.gff1f9ca942-goog
next prev parent reply other threads:[~2025-04-14 22:53 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-14 22:52 [PATCH 0/4] Replace CONFIG_DMABUF_SYSFS_STATS with BPF T.J. Mercier
2025-04-14 22:52 ` [PATCH 1/4] dma-buf: Rename and expose debugfs symbols T.J. Mercier
2025-04-14 22:52 ` T.J. Mercier [this message]
2025-04-16 2:48 ` [PATCH 2/4] bpf: Add dmabuf iterator kernel test robot
2025-04-16 16:28 ` T.J. Mercier
2025-04-16 22:02 ` Song Liu
2025-04-16 22:51 ` T.J. Mercier
2025-04-16 23:08 ` Song Liu
2025-04-16 23:40 ` T.J. Mercier
2025-04-17 1:25 ` Song Liu
2025-04-17 2:09 ` T.J. Mercier
2025-04-17 4:56 ` Song Liu
2025-04-17 16:04 ` T.J. Mercier
2025-04-17 20:26 ` Song Liu
2025-04-18 15:25 ` T.J. Mercier
2025-04-21 18:12 ` Song Liu
2025-04-21 17:58 ` Song Liu
2025-04-21 20:40 ` T.J. Mercier
2025-04-21 23:38 ` Alexei Starovoitov
2025-04-22 19:57 ` T.J. Mercier
2025-04-22 23:00 ` Alexei Starovoitov
2025-04-23 17:16 ` T.J. Mercier
2025-04-14 22:52 ` [PATCH 3/4] selftests/bpf: Add test for dmabuf_iter T.J. Mercier
2025-04-21 18:08 ` Song Liu
2025-04-14 22:52 ` [RFC PATCH 4/4] RFC: dma-buf: Remove DMA-BUF statistics T.J. Mercier
2025-04-15 9:03 ` [PATCH 0/4] Replace CONFIG_DMABUF_SYSFS_STATS with BPF Christian König
2025-04-15 18:13 ` T.J. Mercier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250414225227.3642618-3-tjmercier@google.com \
--to=tjmercier@google.com \
--cc=andrii@kernel.org \
--cc=android-mm@google.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=christian.koenig@amd.com \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=sdf@fomichev.me \
--cc=simona@ffwll.ch \
--cc=skhan@linuxfoundation.org \
--cc=song@kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox