|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright (c) 2025 Google LLC */ |
| 3 | +#include <linux/bpf.h> |
| 4 | +#include <linux/btf_ids.h> |
| 5 | +#include <linux/dma-buf.h> |
| 6 | +#include <linux/kernel.h> |
| 7 | +#include <linux/seq_file.h> |
| 8 | + |
| 9 | +static void *dmabuf_iter_seq_start(struct seq_file *seq, loff_t *pos) |
| 10 | +{ |
| 11 | + if (*pos) |
| 12 | + return NULL; |
| 13 | + |
| 14 | + return dma_buf_iter_begin(); |
| 15 | +} |
| 16 | + |
| 17 | +static void *dmabuf_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 18 | +{ |
| 19 | + struct dma_buf *dmabuf = v; |
| 20 | + |
| 21 | + ++*pos; |
| 22 | + |
| 23 | + return dma_buf_iter_next(dmabuf); |
| 24 | +} |
| 25 | + |
| 26 | +struct bpf_iter__dmabuf { |
| 27 | + __bpf_md_ptr(struct bpf_iter_meta *, meta); |
| 28 | + __bpf_md_ptr(struct dma_buf *, dmabuf); |
| 29 | +}; |
| 30 | + |
| 31 | +static int __dmabuf_seq_show(struct seq_file *seq, void *v, bool in_stop) |
| 32 | +{ |
| 33 | + struct bpf_iter_meta meta = { |
| 34 | + .seq = seq, |
| 35 | + }; |
| 36 | + struct bpf_iter__dmabuf ctx = { |
| 37 | + .meta = &meta, |
| 38 | + .dmabuf = v, |
| 39 | + }; |
| 40 | + struct bpf_prog *prog = bpf_iter_get_info(&meta, in_stop); |
| 41 | + |
| 42 | + if (prog) |
| 43 | + return bpf_iter_run_prog(prog, &ctx); |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static int dmabuf_iter_seq_show(struct seq_file *seq, void *v) |
| 49 | +{ |
| 50 | + return __dmabuf_seq_show(seq, v, false); |
| 51 | +} |
| 52 | + |
| 53 | +static void dmabuf_iter_seq_stop(struct seq_file *seq, void *v) |
| 54 | +{ |
| 55 | + struct dma_buf *dmabuf = v; |
| 56 | + |
| 57 | + if (dmabuf) |
| 58 | + dma_buf_put(dmabuf); |
| 59 | +} |
| 60 | + |
| 61 | +static const struct seq_operations dmabuf_iter_seq_ops = { |
| 62 | + .start = dmabuf_iter_seq_start, |
| 63 | + .next = dmabuf_iter_seq_next, |
| 64 | + .stop = dmabuf_iter_seq_stop, |
| 65 | + .show = dmabuf_iter_seq_show, |
| 66 | +}; |
| 67 | + |
| 68 | +static void bpf_iter_dmabuf_show_fdinfo(const struct bpf_iter_aux_info *aux, |
| 69 | + struct seq_file *seq) |
| 70 | +{ |
| 71 | + seq_puts(seq, "dmabuf iter\n"); |
| 72 | +} |
| 73 | + |
| 74 | +static const struct bpf_iter_seq_info dmabuf_iter_seq_info = { |
| 75 | + .seq_ops = &dmabuf_iter_seq_ops, |
| 76 | + .init_seq_private = NULL, |
| 77 | + .fini_seq_private = NULL, |
| 78 | + .seq_priv_size = 0, |
| 79 | +}; |
| 80 | + |
| 81 | +static struct bpf_iter_reg bpf_dmabuf_reg_info = { |
| 82 | + .target = "dmabuf", |
| 83 | + .feature = BPF_ITER_RESCHED, |
| 84 | + .show_fdinfo = bpf_iter_dmabuf_show_fdinfo, |
| 85 | + .ctx_arg_info_size = 1, |
| 86 | + .ctx_arg_info = { |
| 87 | + { offsetof(struct bpf_iter__dmabuf, dmabuf), |
| 88 | + PTR_TO_BTF_ID_OR_NULL }, |
| 89 | + }, |
| 90 | + .seq_info = &dmabuf_iter_seq_info, |
| 91 | +}; |
| 92 | + |
| 93 | +DEFINE_BPF_ITER_FUNC(dmabuf, struct bpf_iter_meta *meta, struct dma_buf *dmabuf) |
| 94 | +BTF_ID_LIST_SINGLE(bpf_dmabuf_btf_id, struct, dma_buf) |
| 95 | + |
| 96 | +static int __init dmabuf_iter_init(void) |
| 97 | +{ |
| 98 | + bpf_dmabuf_reg_info.ctx_arg_info[0].btf_id = bpf_dmabuf_btf_id[0]; |
| 99 | + return bpf_iter_reg_target(&bpf_dmabuf_reg_info); |
| 100 | +} |
| 101 | + |
| 102 | +late_initcall(dmabuf_iter_init); |
0 commit comments