Skip to content

Commit 6eab7ac

Browse files
T.J. MercierAlexei Starovoitov
authored andcommitted
bpf: Add open coded dmabuf iterator
This 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. Signed-off-by: T.J. Mercier <tjmercier@google.com> Acked-by: Christian König <christian.koenig@amd.com> Acked-by: Song Liu <song@kernel.org> Link: https://lore.kernel.org/r/20250522230429.941193-4-tjmercier@google.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 76ea955 commit 6eab7ac

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

kernel/bpf/dmabuf_iter.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,51 @@ static int __init dmabuf_iter_init(void)
100100
}
101101

102102
late_initcall(dmabuf_iter_init);
103+
104+
struct bpf_iter_dmabuf {
105+
/*
106+
* opaque iterator state; having __u64 here allows to preserve correct
107+
* alignment requirements in vmlinux.h, generated from BTF
108+
*/
109+
__u64 __opaque[1];
110+
} __aligned(8);
111+
112+
/* Non-opaque version of bpf_iter_dmabuf */
113+
struct bpf_iter_dmabuf_kern {
114+
struct dma_buf *dmabuf;
115+
} __aligned(8);
116+
117+
__bpf_kfunc_start_defs();
118+
119+
__bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
120+
{
121+
struct bpf_iter_dmabuf_kern *kit = (void *)it;
122+
123+
BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
124+
BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
125+
126+
kit->dmabuf = NULL;
127+
return 0;
128+
}
129+
130+
__bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
131+
{
132+
struct bpf_iter_dmabuf_kern *kit = (void *)it;
133+
134+
if (kit->dmabuf)
135+
kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
136+
else
137+
kit->dmabuf = dma_buf_iter_begin();
138+
139+
return kit->dmabuf;
140+
}
141+
142+
__bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
143+
{
144+
struct bpf_iter_dmabuf_kern *kit = (void *)it;
145+
146+
if (kit->dmabuf)
147+
dma_buf_put(kit->dmabuf);
148+
}
149+
150+
__bpf_kfunc_end_defs();

kernel/bpf/helpers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,6 +3386,11 @@ BTF_ID_FLAGS(func, bpf_copy_from_user_dynptr, KF_SLEEPABLE)
33863386
BTF_ID_FLAGS(func, bpf_copy_from_user_str_dynptr, KF_SLEEPABLE)
33873387
BTF_ID_FLAGS(func, bpf_copy_from_user_task_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
33883388
BTF_ID_FLAGS(func, bpf_copy_from_user_task_str_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS)
3389+
#ifdef CONFIG_DMA_SHARED_BUFFER
3390+
BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW | KF_SLEEPABLE)
3391+
BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
3392+
BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE)
3393+
#endif
33893394
BTF_KFUNCS_END(common_btf_ids)
33903395

33913396
static const struct btf_kfunc_id_set common_kfunc_set = {

0 commit comments

Comments
 (0)