1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
5 #include <linux/filter.h>
6 #include <linux/kernel.h>
7 #include <linux/btf_ids.h>
9 struct bpf_iter_seq_prog_info
{
13 static void *bpf_prog_seq_start(struct seq_file
*seq
, loff_t
*pos
)
15 struct bpf_iter_seq_prog_info
*info
= seq
->private;
16 struct bpf_prog
*prog
;
18 prog
= bpf_prog_get_curr_or_next(&info
->prog_id
);
27 static void *bpf_prog_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
29 struct bpf_iter_seq_prog_info
*info
= seq
->private;
33 bpf_prog_put((struct bpf_prog
*)v
);
34 return bpf_prog_get_curr_or_next(&info
->prog_id
);
37 struct bpf_iter__bpf_prog
{
38 __bpf_md_ptr(struct bpf_iter_meta
*, meta
);
39 __bpf_md_ptr(struct bpf_prog
*, prog
);
42 DEFINE_BPF_ITER_FUNC(bpf_prog
, struct bpf_iter_meta
*meta
, struct bpf_prog
*prog
)
44 static int __bpf_prog_seq_show(struct seq_file
*seq
, void *v
, bool in_stop
)
46 struct bpf_iter__bpf_prog ctx
;
47 struct bpf_iter_meta meta
;
48 struct bpf_prog
*prog
;
54 prog
= bpf_iter_get_info(&meta
, in_stop
);
56 ret
= bpf_iter_run_prog(prog
, &ctx
);
61 static int bpf_prog_seq_show(struct seq_file
*seq
, void *v
)
63 return __bpf_prog_seq_show(seq
, v
, false);
66 static void bpf_prog_seq_stop(struct seq_file
*seq
, void *v
)
69 (void)__bpf_prog_seq_show(seq
, v
, true);
71 bpf_prog_put((struct bpf_prog
*)v
);
74 static const struct seq_operations bpf_prog_seq_ops
= {
75 .start
= bpf_prog_seq_start
,
76 .next
= bpf_prog_seq_next
,
77 .stop
= bpf_prog_seq_stop
,
78 .show
= bpf_prog_seq_show
,
81 BTF_ID_LIST(btf_bpf_prog_id
)
82 BTF_ID(struct, bpf_prog
)
84 static const struct bpf_iter_seq_info bpf_prog_seq_info
= {
85 .seq_ops
= &bpf_prog_seq_ops
,
86 .init_seq_private
= NULL
,
87 .fini_seq_private
= NULL
,
88 .seq_priv_size
= sizeof(struct bpf_iter_seq_prog_info
),
91 static struct bpf_iter_reg bpf_prog_reg_info
= {
93 .ctx_arg_info_size
= 1,
95 { offsetof(struct bpf_iter__bpf_prog
, prog
),
96 PTR_TO_BTF_ID_OR_NULL
},
98 .seq_info
= &bpf_prog_seq_info
,
101 static int __init
bpf_prog_iter_init(void)
103 bpf_prog_reg_info
.ctx_arg_info
[0].btf_id
= *btf_bpf_prog_id
;
104 return bpf_iter_reg_target(&bpf_prog_reg_info
);
107 late_initcall(bpf_prog_iter_init
);