Replace CONFIG_DMABUF_SYSFS_STATS with BPF
Subsystems (6)
v3 cover Replace CONFIG_DMABUF_SYSFS_STATS with BPF
Until CONFIG_DMABUF_SYSFS_STATS was added [1] it was only possible to perform per-buffer accounting with debugfs which is not suitable for production environments. Eventually we discovered the overhead with per-buffer sysfs file creation/removal was significantly impacting allocation and free times, and exacerbated kernfs lock contention. [2] dma_buf_stats_setup() is responsible for 39% of single-page buffer creation duration, or 74% of single-page dma_buf_export() duration when stressing dmabuf allocations and frees.
I prototyped a change from per-buffer to per-exporter statistics with a RCU protected list of exporter allocations that accommodates most (but not all) of our use-cases and avoids almost all of the sysfs overhead. While that adds less overhead than per-buffer sysfs, and less even than the maintenance of the dmabuf debugfs_list, it's still *additional* overhead on top of the debugfs_list and doesn't give us per-buffer info.
This series uses the existing dmabuf debugfs_list to implement a BPF dmabuf iterator, which adds no overhead to buffer allocation/free and provides per-buffer info. The list has been moved outside of CONFIG_DEBUG_FS scope so that it is always populated. The BPF program loaded by userspace that extracts per-buffer information gets to define its own interface which avoids the lack of ABI stability with debugfs.
This will allow us to replace our use of CONFIG_DMABUF_SYSFS_STATS, and the plan is to remove it from the kernel after the next longterm stable release.
[1] https://lore.kernel.org/linux-media/[email protected]
[2] https://lore.kernel.org/all/[email protected]
v1: https://lore.kernel.org/all/[email protected]
v1 -> v2:
Make the DMA buffer list independent of CONFIG_DEBUG_FS per Christian König Add CONFIG_DMA_SHARED_BUFFER check to kernel/bpf/Makefile per kernel test robot Use BTF_ID_LIST_SINGLE instead of BTF_ID_LIST_GLOBAL_SINGLE per Song Liu Fixup comment style, mixing code/declarations, and use ASSERT_OK_FD in selftest per Song Liu Add BPF_ITER_RESCHED feature to bpf_dmabuf_reg_info per Alexei Starovoitov Add open-coded iterator and selftest per Alexei Starovoitov
Add a second test buffer from the system dmabuf heap to selftests
Use the BPF program we'll use in production for selftest per Alexei Starovoitov
https://r.android.com/c/platform/system/bpfprogs/+/3616123/2/dmabufIter.c
https://r.android.com/c/platform/system/memory/libmeminfo/+/3614259/1/libdmabufinfo/dmabuf_bpf_stats.cpp
v2: https://lore.kernel.org/all/[email protected]
v2 -> v3:
Rebase onto bpf-next/master
Move get_next_dmabuf() into drivers/dma-buf/dma-buf.c, along with the
new get_first_dmabuf(). This avoids having to expose the dmabuf list
and mutex to the rest of the kernel, and keeps the dmabuf mutex
operations near each other in the same file. (Christian König)
Add Christian's RB to dma-buf: Rename debugfs symbols
Drop RFC: dma-buf: Remove DMA-BUF statistics
T.J. Mercier (5):
dma-buf: Rename debugfs symbols
bpf: Add dmabuf iterator
bpf: Add open coded dmabuf iterator selftests/bpf: Add test for dmabuf_iter selftests/bpf: Add test for open coded dmabuf_iter
drivers/dma-buf/dma-buf.c | 94 +++++-- include/linux/dma-buf.h | 5 +- kernel/bpf/Makefile | 3 + kernel/bpf/dmabuf_iter.c | 149 ++++++++++ kernel/bpf/helpers.c | 5 + .../testing/selftests/bpf/bpf_experimental.h | 5 + tools/testing/selftests/bpf/config | 3 + .../selftests/bpf/prog_tests/dmabuf_iter.c | 258 ++++++++++++++++++ .../testing/selftests/bpf/progs/dmabuf_iter.c | 91 ++++++ 9 files changed, 591 insertions(+), 22 deletions(-) create mode 100644 kernel/bpf/dmabuf_iter.c create mode 100644 tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c create mode 100644 tools/testing/selftests/bpf/progs/dmabuf_iter.c
base-commit: 43745d11bfd9683abdf08ad7a5cc403d6a9ffd15
Signature · 1 line
2.49.0.1045.g170613ef41-goog
PATCH v3 1/5 dma-buf: Rename debugfs symbols
Rename the debugfs list and mutex so it's clear they are now usable without the need for CONFIG_DEBUG_FS. The list will always be populated to support the creation of a BPF iterator for dmabufs.
--- drivers/dma-buf/dma-buf.c | 40 +++++++++++++++------------------------ include/linux/dma-buf.h | 2 -- 2 files changed, 15 insertions(+), 27 deletions(-)
drivers/dma-buf/dma-buf.c +15 -25
@@ -35,35 +35,25 @@static inline int is_dma_buf_file (struct file *); -#if IS_ENABLED (CONFIG_DEBUG_FS ) -static DEFINE_MUTEX (debugfs_list_mutex); -static LIST_HEAD (debugfs_list); +static DEFINE_MUTEX (dmabuf_list_mutex); +static LIST_HEAD (dmabuf_list); -static void __dma_buf_debugfs_list_add (struct dma_buf *dmabuf) +static void __dma_buf_list_add (struct dma_buf *dmabuf) { -mutex_lock (&debugfs_list_mutex); -list_add (&dmabuf->list_node, &debugfs_list); -mutex_unlock (&debugfs_list_mutex); +mutex_lock (&dmabuf_list_mutex); +list_add (&dmabuf->list_node, &dmabuf_list); +mutex_unlock (&dmabuf_list_mutex); } -static void __dma_buf_debugfs_list_del (struct dma_buf *dmabuf) +static void __dma_buf_list_del (struct dma_buf *dmabuf) {if (!dmabuf)return ; -mutex_lock (&debugfs_list_mutex); +mutex_lock (&dmabuf_list_mutex);list_del (&dmabuf->list_node); -mutex_unlock (&debugfs_list_mutex); +mutex_unlock (&dmabuf_list_mutex); } -#else -static void __dma_buf_debugfs_list_add (struct dma_buf *dmabuf) -{ -} - -static void __dma_buf_debugfs_list_del (struct dma_buf *dmabuf) -{ -} -#endif static char *dmabuffs_dname (struct dentry *dentry,char *buffer,int buflen) {
@@ -115,7 +105,7 @@ static int dma_buf_file_release(struct inode *inode, struct file *file)if (!is_dma_buf_file (file))return -EINVAL ; -__dma_buf_debugfs_list_del (file->private_data); +__dma_buf_list_del (file->private_data);return 0 ; }
@@ -689,7 +679,7 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) file->f_path.dentry->d_fsdata = dmabuf; dmabuf->file = file; -__dma_buf_debugfs_list_add (dmabuf); +__dma_buf_list_add (dmabuf);return dmabuf;
@@ -1630,7 +1620,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)size_t size =0 ;int ret; - ret =mutex_lock_interruptible (&debugfs_list_mutex); + ret =mutex_lock_interruptible (&dmabuf_list_mutex);if (ret)return ret;
@@ -1639,7 +1629,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)seq_printf (s,"%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\tname\n" ,"size" ,"flags" ,"mode" ,"count" ,"ino" ); -list_for_each_entry (buf_obj, &debugfs_list, list_node) { +list_for_each_entry (buf_obj, &dmabuf_list, list_node) { ret =dma_resv_lock_interruptible (buf_obj->resv,NULL );if (ret)
@@ -1676,11 +1666,11 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)seq_printf (s,"\nTotal %d objects, %zu bytes\n" , count, size); -mutex_unlock (&debugfs_list_mutex); +mutex_unlock (&dmabuf_list_mutex);return 0 ; error_unlock: -mutex_unlock (&debugfs_list_mutex); +mutex_unlock (&dmabuf_list_mutex);return ret; }
include/linux/dma-buf.h +0 -2
@@ -370,10 +370,8 @@ struct dma_buf {*/ struct module *owner; -#if IS_ENABLED (CONFIG_DEBUG_FS )/** @list_node: node for dma_buf accounting and debugging. */ struct list_head list_node; -#endif /** @priv: exporter specific private data for this buffer object. */ void *priv;
Signature · 1 line
2.49.0.1045.g170613ef41-goog
PATCH v3 2/5 bpf: Add dmabuf iterator
In reply to T.J. MercierThe dmabuf iterator traverses the list of all DMA buffers.
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.
--- drivers/dma-buf/dma-buf.c | 64 ++++++++++++++++++++++++ include/linux/dma-buf.h | 3 ++ kernel/bpf/Makefile | 3 ++ kernel/bpf/dmabuf_iter.c | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 kernel/bpf/dmabuf_iter.c
drivers/dma-buf/dma-buf.c +64 -0
@@ -19,7 +19,9 @@#include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/seq_file.h> #include <linux/sync_file.h> #include <linux/poll.h>
@@ -55,6 +57,68 @@ static void __dma_buf_list_del(struct dma_buf *dmabuf)mutex_unlock (&dmabuf_list_mutex); } +/** +* get_first_dmabuf - begin iteration through global list of DMA-bufs +* +* Returns the first buffer in the global list of DMA-bufs that's not in the +* process of being destroyed. Increments that buffer's reference count to +* prevent buffer destruction. Callers must release the reference, either by +* continuing iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no active buffers are present. +*/ +struct dma_buf *get_first_dmabuf (void ) +{ +struct dma_buf *ret =NULL , *dmabuf; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +list_for_each_entry (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} + +/** +* get_next_dmabuf - continue iteration through global list of DMA-bufs +* @dmabuf: [in] pointer to dma_buf +* +* Decrements the reference count on the provided buffer. Returns the next +* buffer from the remainder of the global list of DMA-bufs with its reference +* count incremented. Callers must release the reference, either by continuing +* iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no additional active buffers are present. +*/ +struct dma_buf *get_next_dmabuf (struct dma_buf *dmabuf) +{ +struct dma_buf *ret =NULL ; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +dma_buf_put (dmabuf); +list_for_each_entry_continue (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} +static char *dmabuffs_dname (struct dentry *dentry,char *buffer,int buflen) {struct dma_buf *dmabuf;
include/linux/dma-buf.h +3 -0
@@ -568,6 +568,9 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)get_file (dmabuf->file); } +struct dma_buf *get_first_dmabuf (void ); +struct dma_buf *get_next_dmabuf (struct dma_buf *dmbuf); +/** * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings. * @dmabuf: the DMA-buf to check
kernel/bpf/Makefile +3 -0
@@ -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_DMA_SHARED_BUFFER),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)
kernel/bpf/dmabuf_iter.c new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
2.49.0.1045.g170613ef41-goog
Christian König
maintainerRe: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator
Quoted 31 lines · T.J. Mercier
The dmabuf iterator traverses the list of all DMA buffers.
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 <[email protected]>--- drivers/dma-buf/dma-buf.c | 64 ++++++++++++++++++++++++ include/linux/dma-buf.h | 3 ++ kernel/bpf/Makefile | 3 ++ kernel/bpf/dmabuf_iter.c | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 kernel/bpf/dmabuf_iter.cdrivers/dma-buf/dma-buf.c +2
-0@@ -19,7 +19,9 @@#include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/seq_file.h> #include <linux/sync_file.h> #include <linux/poll.h>
drivers/dma-buf/dma-buf.c static void __dma_buf_list_del(struct dma_buf *dmabuf) · line 61
Quoted 5 lines
drivers/dma-buf/dma-buf.c +2
-0@@ -55,6 +57,68 @@ static void __dma_buf_list_del(struct dma_buf *dmabuf)mutex_unlock (&dmabuf_list_mutex); } +/** +* get_first_dmabuf - begin iteration through global list of DMA-bufs
As far as I can see that looks really good.
The only thing I'm questioning a little bit is that the name get_first_dmabuf() just doesn't sound so well to me.
I'm a fan of keeping the object you work with first in the naming and it should somehow express that this iters over the global list of all buffers. Maybe something like dmabuf_get_first_globally or dmabuf_get_first_instance.
Feel free to add my rb if any of those suggestions are used, but I'm completely open other ideas as well.
Regards,
Christian.
Quoted 199 lines
drivers/dma-buf/dma-buf.c +60
-0+* +* Returns the first buffer in the global list of DMA-bufs that's not in the +* process of being destroyed. Increments that buffer's reference count to +* prevent buffer destruction. Callers must release the reference, either by +* continuing iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no active buffers are present. +*/ +struct dma_buf *get_first_dmabuf (void ) +{ +struct dma_buf *ret =NULL , *dmabuf; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +list_for_each_entry (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} + +/** +* get_next_dmabuf - continue iteration through global list of DMA-bufs +* @dmabuf: [in] pointer to dma_buf +* +* Decrements the reference count on the provided buffer. Returns the next +* buffer from the remainder of the global list of DMA-bufs with its reference +* count incremented. Callers must release the reference, either by continuing +* iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no additional active buffers are present. +*/ +struct dma_buf *get_next_dmabuf (struct dma_buf *dmabuf) +{ +struct dma_buf *ret =NULL ; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +dma_buf_put (dmabuf); +list_for_each_entry_continue (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} +static char *dmabuffs_dname (struct dentry *dentry,char *buffer,int buflen) {struct dma_buf *dmabuf;include/linux/dma-buf.h +3
-0@@ -568,6 +568,9 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)get_file (dmabuf->file); } +struct dma_buf *get_first_dmabuf (void ); +struct dma_buf *get_next_dmabuf (struct dma_buf *dmbuf); +/** * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings. * @dmabuf: the DMA-buf to check kernel/bpf/Makefile +3
-0@@ -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_DMA_SHARED_BUFFER),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)kernel/bpf/dmabuf_iter.c new file +102
-0@@ -0,0 +1,102 @@ +// 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_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) +{ +if (*pos) +return NULL ; + +return get_first_dmabuf (); +} + +static void *dmabuf_iter_seq_next (struct seq_file *seq,void *v,loff_t *pos) +{ +struct dma_buf *dmabuf = v; + + ++*pos; + +return get_next_dmabuf (dmabuf); +} + +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" , + .feature =BPF_ITER_RESCHED , + .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);
Re: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator
Quoted 41 lines · Christian König
Quoted 31 lines · T.J. Mercier
The dmabuf iterator traverses the list of all DMA buffers.
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 <[email protected]>--- drivers/dma-buf/dma-buf.c | 64 ++++++++++++++++++++++++ include/linux/dma-buf.h | 3 ++ kernel/bpf/Makefile | 3 ++ kernel/bpf/dmabuf_iter.c | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 kernel/bpf/dmabuf_iter.cdrivers/dma-buf/dma-buf.c +2
-0@@ -19,7 +19,9 @@#include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/seq_file.h> #include <linux/sync_file.h> #include <linux/poll.h> drivers/dma-buf/dma-buf.c static void __dma_buf_list_del(struct dma_buf *dmabuf) · line 61
Quoted 5 lines
drivers/dma-buf/dma-buf.c +2
-0@@ -55,6 +57,68 @@ static void __dma_buf_list_del(struct dma_buf *dmabuf)mutex_unlock (&dmabuf_list_mutex); } +/** +* get_first_dmabuf - begin iteration through global list of DMA-bufs As far as I can see that looks really good.
Quoted 9 lines
The only thing I'm questioning a little bit is that the name
get_first_dmabuf()just doesn't sound so well to me.I'm a fan of keeping the object you work with first in the naming and it should somehow express that this iters over the global list of all buffers. Maybe something like
dmabuf_get_first_globallyordmabuf_get_first_instance.Feel free to add my rb if any of those suggestions are used, but I'm completely open other ideas as well.
Regards,
Christian.
Yeah you're right. "first" is actually a little misleading too, since the most recently exported buffer will be at the list head, not the oldest buffer. But buffer age or ordering doesn't really matter here as long as we get through all of them.
So I'm thinking dma_buf_iter_begin() and dma_buf_iter_next() would be better names. Similar to seq_start / seq_next or bpf's iter_<type>_new / iter_<type>_next.
Quoted 200 lines
Quoted 199 lines
drivers/dma-buf/dma-buf.c +60
-0+* +* Returns the first buffer in the global list of DMA-bufs that's not in the +* process of being destroyed. Increments that buffer's reference count to +* prevent buffer destruction. Callers must release the reference, either by +* continuing iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no active buffers are present. +*/ +struct dma_buf *get_first_dmabuf (void ) +{ +struct dma_buf *ret =NULL , *dmabuf; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +list_for_each_entry (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} + +/** +* get_next_dmabuf - continue iteration through global list of DMA-bufs +* @dmabuf: [in] pointer to dma_buf +* +* Decrements the reference count on the provided buffer. Returns the next +* buffer from the remainder of the global list of DMA-bufs with its reference +* count incremented. Callers must release the reference, either by continuing +* iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no additional active buffers are present. +*/ +struct dma_buf *get_next_dmabuf (struct dma_buf *dmabuf) +{ +struct dma_buf *ret =NULL ; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +dma_buf_put (dmabuf); +list_for_each_entry_continue (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} +static char *dmabuffs_dname (struct dentry *dentry,char *buffer,int buflen) {struct dma_buf *dmabuf;include/linux/dma-buf.h +3
-0@@ -568,6 +568,9 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)get_file (dmabuf->file); } +struct dma_buf *get_first_dmabuf (void ); +struct dma_buf *get_next_dmabuf (struct dma_buf *dmbuf); +/** * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings. * @dmabuf: the DMA-buf to check kernel/bpf/Makefile +3
-0@@ -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_DMA_SHARED_BUFFER),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)kernel/bpf/dmabuf_iter.c new file +102
-0@@ -0,0 +1,102 @@ +// 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_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) +{ +if (*pos) +return NULL ; + +return get_first_dmabuf (); +} + +static void *dmabuf_iter_seq_next (struct seq_file *seq,void *v,loff_t *pos) +{ +struct dma_buf *dmabuf = v; + + ++*pos; + +return get_next_dmabuf (dmabuf); +} + +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" , + .feature =BPF_ITER_RESCHED , + .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);
Christian König
maintainerRe: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator
Quoted 51 lines · T.J. Mercier
Quoted 41 lines · Christian König
Quoted 31 lines · T.J. Mercier
The dmabuf iterator traverses the list of all DMA buffers.
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 <[email protected]>--- drivers/dma-buf/dma-buf.c | 64 ++++++++++++++++++++++++ include/linux/dma-buf.h | 3 ++ kernel/bpf/Makefile | 3 ++ kernel/bpf/dmabuf_iter.c | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 kernel/bpf/dmabuf_iter.cdrivers/dma-buf/dma-buf.c +2
-0@@ -19,7 +19,9 @@#include <linux/anon_inodes.h> #include <linux/export.h> #include <linux/debugfs.h> +#include <linux/list.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/seq_file.h> #include <linux/sync_file.h> #include <linux/poll.h> drivers/dma-buf/dma-buf.c static void __dma_buf_list_del(struct dma_buf *dmabuf) · line 61
Quoted 5 lines
drivers/dma-buf/dma-buf.c +2
-0@@ -55,6 +57,68 @@ static void __dma_buf_list_del(struct dma_buf *dmabuf)mutex_unlock (&dmabuf_list_mutex); } +/** +* get_first_dmabuf - begin iteration through global list of DMA-bufs As far as I can see that looks really good.
Quoted 9 lines
The only thing I'm questioning a little bit is that the name
get_first_dmabuf()just doesn't sound so well to me.I'm a fan of keeping the object you work with first in the naming and it should somehow express that this iters over the global list of all buffers. Maybe something like
dmabuf_get_first_globallyordmabuf_get_first_instance.Feel free to add my rb if any of those suggestions are used, but I'm completely open other ideas as well.
Regards,
Christian.
Quoted 8 lines
Yeah you're right. "first" is actually a little misleading too, since the most recently exported buffer will be at the list head, not the oldest buffer. But buffer age or ordering doesn't really matter here as long as we get through all of them.
So I'm thinking
dma_buf_iter_begin()anddma_buf_iter_next()would be better names. Similar toseq_start/seq_nextor bpf's iter_<type>_new / iter_<type>_next.
Yeah, dma_buf_iter_begin/next works for me as well.
Feel free to add my rb when you use those names.
Regards,
Christian.
Quoted 201 lines
Quoted 201 lines
Quoted 200 lines
drivers/dma-buf/dma-buf.c +60
-0+* +* Returns the first buffer in the global list of DMA-bufs that's not in the +* process of being destroyed. Increments that buffer's reference count to +* prevent buffer destruction. Callers must release the reference, either by +* continuing iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no active buffers are present. +*/ +struct dma_buf *get_first_dmabuf (void ) +{ +struct dma_buf *ret =NULL , *dmabuf; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +list_for_each_entry (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} + +/** +* get_next_dmabuf - continue iteration through global list of DMA-bufs +* @dmabuf: [in] pointer to dma_buf +* +* Decrements the reference count on the provided buffer. Returns the next +* buffer from the remainder of the global list of DMA-bufs with its reference +* count incremented. Callers must release the reference, either by continuing +* iteration with get_next_dmabuf(), or with dma_buf_put(). +* +* Returns NULL If no additional active buffers are present. +*/ +struct dma_buf *get_next_dmabuf (struct dma_buf *dmabuf) +{ +struct dma_buf *ret =NULL ; + +/* +* The list mutex does not protect a dmabuf's refcount, so it can be +* zeroed while we are iterating. We cannot call get_dma_buf() since the +* caller may not already own a reference to the buffer. +*/ +mutex_lock (&dmabuf_list_mutex); +dma_buf_put (dmabuf); +list_for_each_entry_continue (dmabuf, &dmabuf_list, list_node) { +if (file_ref_get (&dmabuf->file->f_ref)) { + ret = dmabuf; +break ; + } + } +mutex_unlock (&dmabuf_list_mutex); +return ret; +} +static char *dmabuffs_dname (struct dentry *dentry,char *buffer,int buflen) {struct dma_buf *dmabuf;include/linux/dma-buf.h +3
-0@@ -568,6 +568,9 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)get_file (dmabuf->file); } +struct dma_buf *get_first_dmabuf (void ); +struct dma_buf *get_next_dmabuf (struct dma_buf *dmbuf); +/** * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings. * @dmabuf: the DMA-buf to check kernel/bpf/Makefile +3
-0@@ -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_DMA_SHARED_BUFFER),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)kernel/bpf/dmabuf_iter.c new file +102
-0@@ -0,0 +1,102 @@ +// 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_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) +{ +if (*pos) +return NULL ; + +return get_first_dmabuf (); +} + +static void *dmabuf_iter_seq_next (struct seq_file *seq,void *v,loff_t *pos) +{ +struct dma_buf *dmabuf = v; + + ++*pos; + +return get_next_dmabuf (dmabuf); +} + +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" , + .feature =BPF_ITER_RESCHED , + .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);
Re: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator
In reply to T.J. MercierQuoted 11 lines · T.J. Mercier
+/**
+ *get_first_dmabuf- begin iteration through global list of DMA-bufs
+ *
+ * Returns the first buffer in the global list of DMA-bufs that's not in the
+ * process of being destroyed. Increments that buffer's reference count to
+ * prevent buffer destruction. Callers must release the reference, either by
+ * continuing iteration withget_next_dmabuf(), or withdma_buf_put().
+ *
+ * Returns NULL If no active buffers are present.
+ */
kdoc wants to see 'Return:'.
See errors in BPF CI.
And patch 5 shouldn't be using /** for plain comments.
pw-bot: cr
Re: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator
Quoted 14 lines · Alexei Starovoitov
Quoted 11 lines · T.J. Mercier
+/**
+ *get_first_dmabuf- begin iteration through global list of DMA-bufs
+ *
+ * Returns the first buffer in the global list of DMA-bufs that's not in the
+ * process of being destroyed. Increments that buffer's reference count to
+ * prevent buffer destruction. Callers must release the reference, either by
+ * continuing iteration withget_next_dmabuf(), or withdma_buf_put().
+ *
+ * Returns NULL If no active buffers are present.
+ */
Quoted 5 lines
kdoc wants to see 'Return:'.
See errors in BPF CI.
And patch 5 shouldn't be using /** for plain comments.
Thanks, I found the CI errors, fixed, and verified with scripts/kernel-doc. I didn't receive emails about them though, not sure if I should have.
Quoted 1 line
pw-bot: cr
PATCH v3 3/5 bpf: Add open coded dmabuf iterator
In reply to T.J. MercierThis open coded iterator allows for more flexibility when creating BPF programs. It can support output in formats other than text. With an open coded iterator, a single BPF program can traverse multiple kernel data structures (now including dmabufs), allowing for more efficient analysis of kernel data compared to multiple reads from procfs, sysfs, or multiple traditional BPF iterator invocations.
--- kernel/bpf/dmabuf_iter.c | 47 ++++++++++++++++++++++++++++++++++++++++ kernel/bpf/helpers.c | 5 +++++ 2 files changed, 52 insertions(+)
kernel/bpf/dmabuf_iter.c +47 -0
@@ -100,3 +100,50 @@ static int __init dmabuf_iter_init(void) }late_initcall (dmabuf_iter_init); + +struct bpf_iter_dmabuf { +/* opaque iterator state; having __u64 here allows to preserve correct +* alignment requirements in vmlinux.h, generated from BTF +*/ +__u64 __opaque[1 ]; +}__aligned(8) ; + +/* Non-opaque version of bpf_iter_dmabuf */ +struct bpf_iter_dmabuf_kern { +struct dma_buf *dmabuf; +}__aligned(8) ; + +__bpf_kfunc_start_defs (); + +__bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) +{ +struct bpf_iter_dmabuf_kern *kit = (void *)it; + +BUILD_BUG_ON (sizeof (*kit) >sizeof (*it)); + BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it)); + + kit->dmabuf =NULL ; +return 0 ; +} + +__bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) +{ +struct bpf_iter_dmabuf_kern *kit = (void *)it; + +if (kit->dmabuf) + kit->dmabuf =get_next_dmabuf (kit->dmabuf); +else + kit->dmabuf =get_first_dmabuf (); + +return kit->dmabuf; +} + +__bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) +{ +struct bpf_iter_dmabuf_kern *kit = (void *)it; + +if (kit->dmabuf) +dma_buf_put (kit->dmabuf); +} + +__bpf_kfunc_end_defs ();
kernel/bpf/helpers.c +5 -0
@@ -3346,6 +3346,11 @@ BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLE BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_local_irq_save) BTF_ID_FLAGS(func, bpf_local_irq_restore) +#ifdef CONFIG_DMA_SHARED_BUFFER +BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) +#endif BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.49.0.1045.g170613ef41-goog
PATCH v3 4/5 selftests/bpf: Add test for dmabuf_iter
In reply to T.J. MercierThis test creates a udmabuf, and a dmabuf from the system dmabuf heap, and uses a BPF program that prints dmabuf metadata with the new dmabuf_iter to verify they can be found.
--- tools/testing/selftests/bpf/config | 3 + .../selftests/bpf/prog_tests/dmabuf_iter.c | 224 ++++++++++++++++++ .../testing/selftests/bpf/progs/dmabuf_iter.c | 53 +++++ 3 files changed, 280 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c create mode 100644 tools/testing/selftests/bpf/progs/dmabuf_iter.c
tools/testing/selftests/bpf/config +3 -0
@@ -22,6 +22,8 @@ CONFIG_CRYPTO_AES=y CONFIG_DEBUG_INFO=y CONFIG_DEBUG_INFO_BTF=y CONFIG_DEBUG_INFO_DWARF4=y +CONFIG_DMABUF_HEAPS=y +CONFIG_DMABUF_HEAPS_SYSTEM=y CONFIG_DUMMY=y CONFIG_DYNAMIC_FTRACE=y CONFIG_FPROBE=y
@@ -106,6 +108,7 @@ CONFIG_SECURITY=y CONFIG_SECURITYFS=y CONFIG_SYN_COOKIES=y CONFIG_TEST_BPF=m +CONFIG_UDMABUF=y CONFIG_USERFAULTFD=y CONFIG_VSOCKETS=y CONFIG_VXLAN=y
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c new file +224 -0
224 lines not on this page · raw message
tools/testing/selftests/bpf/progs/dmabuf_iter.c new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.49.0.1045.g170613ef41-goog
PATCH v3 5/5 selftests/bpf: Add test for open coded dmabuf_iter
In reply to T.J. MercierUse the same test buffers as the traditional iterator and a new BPF map to verify the test buffers can be found with the open coded dmabuf iterator.
--- .../testing/selftests/bpf/bpf_experimental.h | 5 ++ .../selftests/bpf/prog_tests/dmabuf_iter.c | 52 +++++++++++++++---- .../testing/selftests/bpf/progs/dmabuf_iter.c | 38 ++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/bpf_experimental.h +5 -0
@@ -591,4 +591,9 @@ extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksymextern struct kmem_cache *bpf_iter_kmem_cache_next (struct bpf_iter_kmem_cache *it)__weak __ksym; extern void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it) __weak __ksym; +struct bpf_iter_dmabuf ; +extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) __weak __ksym; +extern struct dma_buf *bpf_iter_dmabuf_next (struct bpf_iter_dmabuf *it)__weak __ksym; +extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym; +#endif
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +43 -9
114 lines not on this page · raw message
tools/testing/selftests/bpf/progs/dmabuf_iter.c +38 -0
@@ -9,6 +9,13 @@ char _license[] SEC("license") = "GPL"; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, DMA_BUF_NAME_LEN); + __type(value, bool); + __uint(max_entries, 5); +} testbuf_hash SEC(".maps"); +/** * Fields output by this iterator are delimited by newlines. Convert any * newlines in user-provided printed strings to spaces.
@@ -51,3 +58,34 @@ int dmabuf_collector(struct bpf_iter__dmabuf *ctx)BPF_SEQ_PRINTF (seq,"%lu\n%llu\n%s\n%s\n" , inode, size, name, exporter);return 0 ; } + +SEC("syscall") +int iter_dmabuf_for_each (const void *ctx) +{ +struct dma_buf *d; + +bpf_for_each (dmabuf, d) { +char name[DMA_BUF_NAME_LEN ]; +const char *pname; +bool *found; + +if (bpf_core_read (&pname,sizeof (pname), &d->name)) +return 1 ; + +/* Buffers are not required to be named */ +if (!pname) +continue ; + +if (bpf_probe_read_kernel (name,sizeof (name), pname)) +return 1 ; + + found =bpf_map_lookup_elem (&testbuf_hash, name); +if (found) { +bool t = true; + +bpf_map_update_elem (&testbuf_hash, name, &t,BPF_EXIST ); + } + } + +return 0 ; +}
Signature · 1 line
2.49.0.1045.g170613ef41-goog