0001
0002
0003 #include <linux/bpf.h>
0004 #include <linux/fs.h>
0005 #include <linux/filter.h>
0006 #include <linux/kernel.h>
0007 #include <linux/btf_ids.h>
0008
0009 struct bpf_iter_seq_map_info {
0010 u32 map_id;
0011 };
0012
0013 static void *bpf_map_seq_start(struct seq_file *seq, loff_t *pos)
0014 {
0015 struct bpf_iter_seq_map_info *info = seq->private;
0016 struct bpf_map *map;
0017
0018 map = bpf_map_get_curr_or_next(&info->map_id);
0019 if (!map)
0020 return NULL;
0021
0022 if (*pos == 0)
0023 ++*pos;
0024 return map;
0025 }
0026
0027 static void *bpf_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
0028 {
0029 struct bpf_iter_seq_map_info *info = seq->private;
0030
0031 ++*pos;
0032 ++info->map_id;
0033 bpf_map_put((struct bpf_map *)v);
0034 return bpf_map_get_curr_or_next(&info->map_id);
0035 }
0036
0037 struct bpf_iter__bpf_map {
0038 __bpf_md_ptr(struct bpf_iter_meta *, meta);
0039 __bpf_md_ptr(struct bpf_map *, map);
0040 };
0041
0042 DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)
0043
0044 static int __bpf_map_seq_show(struct seq_file *seq, void *v, bool in_stop)
0045 {
0046 struct bpf_iter__bpf_map ctx;
0047 struct bpf_iter_meta meta;
0048 struct bpf_prog *prog;
0049 int ret = 0;
0050
0051 ctx.meta = &meta;
0052 ctx.map = v;
0053 meta.seq = seq;
0054 prog = bpf_iter_get_info(&meta, in_stop);
0055 if (prog)
0056 ret = bpf_iter_run_prog(prog, &ctx);
0057
0058 return ret;
0059 }
0060
0061 static int bpf_map_seq_show(struct seq_file *seq, void *v)
0062 {
0063 return __bpf_map_seq_show(seq, v, false);
0064 }
0065
0066 static void bpf_map_seq_stop(struct seq_file *seq, void *v)
0067 {
0068 if (!v)
0069 (void)__bpf_map_seq_show(seq, v, true);
0070 else
0071 bpf_map_put((struct bpf_map *)v);
0072 }
0073
0074 static const struct seq_operations bpf_map_seq_ops = {
0075 .start = bpf_map_seq_start,
0076 .next = bpf_map_seq_next,
0077 .stop = bpf_map_seq_stop,
0078 .show = bpf_map_seq_show,
0079 };
0080
0081 BTF_ID_LIST(btf_bpf_map_id)
0082 BTF_ID(struct, bpf_map)
0083
0084 static const struct bpf_iter_seq_info bpf_map_seq_info = {
0085 .seq_ops = &bpf_map_seq_ops,
0086 .init_seq_private = NULL,
0087 .fini_seq_private = NULL,
0088 .seq_priv_size = sizeof(struct bpf_iter_seq_map_info),
0089 };
0090
0091 static struct bpf_iter_reg bpf_map_reg_info = {
0092 .target = "bpf_map",
0093 .ctx_arg_info_size = 1,
0094 .ctx_arg_info = {
0095 { offsetof(struct bpf_iter__bpf_map, map),
0096 PTR_TO_BTF_ID_OR_NULL },
0097 },
0098 .seq_info = &bpf_map_seq_info,
0099 };
0100
0101 static int bpf_iter_attach_map(struct bpf_prog *prog,
0102 union bpf_iter_link_info *linfo,
0103 struct bpf_iter_aux_info *aux)
0104 {
0105 u32 key_acc_size, value_acc_size, key_size, value_size;
0106 struct bpf_map *map;
0107 bool is_percpu = false;
0108 int err = -EINVAL;
0109
0110 if (!linfo->map.map_fd)
0111 return -EBADF;
0112
0113 map = bpf_map_get_with_uref(linfo->map.map_fd);
0114 if (IS_ERR(map))
0115 return PTR_ERR(map);
0116
0117 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
0118 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
0119 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
0120 is_percpu = true;
0121 else if (map->map_type != BPF_MAP_TYPE_HASH &&
0122 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
0123 map->map_type != BPF_MAP_TYPE_ARRAY)
0124 goto put_map;
0125
0126 key_acc_size = prog->aux->max_rdonly_access;
0127 value_acc_size = prog->aux->max_rdwr_access;
0128 key_size = map->key_size;
0129 if (!is_percpu)
0130 value_size = map->value_size;
0131 else
0132 value_size = round_up(map->value_size, 8) * num_possible_cpus();
0133
0134 if (key_acc_size > key_size || value_acc_size > value_size) {
0135 err = -EACCES;
0136 goto put_map;
0137 }
0138
0139 aux->map = map;
0140 return 0;
0141
0142 put_map:
0143 bpf_map_put_with_uref(map);
0144 return err;
0145 }
0146
0147 static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
0148 {
0149 bpf_map_put_with_uref(aux->map);
0150 }
0151
0152 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
0153 struct seq_file *seq)
0154 {
0155 seq_printf(seq, "map_id:\t%u\n", aux->map->id);
0156 }
0157
0158 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
0159 struct bpf_link_info *info)
0160 {
0161 info->iter.map.map_id = aux->map->id;
0162 return 0;
0163 }
0164
0165 DEFINE_BPF_ITER_FUNC(bpf_map_elem, struct bpf_iter_meta *meta,
0166 struct bpf_map *map, void *key, void *value)
0167
0168 static const struct bpf_iter_reg bpf_map_elem_reg_info = {
0169 .target = "bpf_map_elem",
0170 .attach_target = bpf_iter_attach_map,
0171 .detach_target = bpf_iter_detach_map,
0172 .show_fdinfo = bpf_iter_map_show_fdinfo,
0173 .fill_link_info = bpf_iter_map_fill_link_info,
0174 .ctx_arg_info_size = 2,
0175 .ctx_arg_info = {
0176 { offsetof(struct bpf_iter__bpf_map_elem, key),
0177 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
0178 { offsetof(struct bpf_iter__bpf_map_elem, value),
0179 PTR_TO_BUF | PTR_MAYBE_NULL },
0180 },
0181 };
0182
0183 static int __init bpf_map_iter_init(void)
0184 {
0185 int ret;
0186
0187 bpf_map_reg_info.ctx_arg_info[0].btf_id = *btf_bpf_map_id;
0188 ret = bpf_iter_reg_target(&bpf_map_reg_info);
0189 if (ret)
0190 return ret;
0191
0192 return bpf_iter_reg_target(&bpf_map_elem_reg_info);
0193 }
0194
0195 late_initcall(bpf_map_iter_init);