1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
5 #include <linux/filter.h>
6 #include <linux/kernel.h>
8 struct bpf_iter_seq_map_info
{
12 static void *bpf_map_seq_start(struct seq_file
*seq
, loff_t
*pos
)
14 struct bpf_iter_seq_map_info
*info
= seq
->private;
17 map
= bpf_map_get_curr_or_next(&info
->mid
);
25 static void *bpf_map_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
27 struct bpf_iter_seq_map_info
*info
= seq
->private;
32 bpf_map_put((struct bpf_map
*)v
);
33 map
= bpf_map_get_curr_or_next(&info
->mid
);
40 struct bpf_iter__bpf_map
{
41 __bpf_md_ptr(struct bpf_iter_meta
*, meta
);
42 __bpf_md_ptr(struct bpf_map
*, map
);
45 DEFINE_BPF_ITER_FUNC(bpf_map
, struct bpf_iter_meta
*meta
, struct bpf_map
*map
)
47 static int __bpf_map_seq_show(struct seq_file
*seq
, void *v
, bool in_stop
)
49 struct bpf_iter__bpf_map ctx
;
50 struct bpf_iter_meta meta
;
51 struct bpf_prog
*prog
;
57 prog
= bpf_iter_get_info(&meta
, in_stop
);
59 ret
= bpf_iter_run_prog(prog
, &ctx
);
64 static int bpf_map_seq_show(struct seq_file
*seq
, void *v
)
66 return __bpf_map_seq_show(seq
, v
, false);
69 static void bpf_map_seq_stop(struct seq_file
*seq
, void *v
)
72 (void)__bpf_map_seq_show(seq
, v
, true);
74 bpf_map_put((struct bpf_map
*)v
);
77 static const struct seq_operations bpf_map_seq_ops
= {
78 .start
= bpf_map_seq_start
,
79 .next
= bpf_map_seq_next
,
80 .stop
= bpf_map_seq_stop
,
81 .show
= bpf_map_seq_show
,
84 static const struct bpf_iter_reg bpf_map_reg_info
= {
86 .seq_ops
= &bpf_map_seq_ops
,
87 .init_seq_private
= NULL
,
88 .fini_seq_private
= NULL
,
89 .seq_priv_size
= sizeof(struct bpf_iter_seq_map_info
),
90 .ctx_arg_info_size
= 1,
92 { offsetof(struct bpf_iter__bpf_map
, map
),
93 PTR_TO_BTF_ID_OR_NULL
},
97 static int __init
bpf_map_iter_init(void)
99 return bpf_iter_reg_target(&bpf_map_reg_info
);
102 late_initcall(bpf_map_iter_init
);