^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (c) 2020 Facebook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/btf_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct bpf_iter_seq_map_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) u32 map_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static void *bpf_map_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct bpf_iter_seq_map_info *info = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct bpf_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) map = bpf_map_get_curr_or_next(&info->map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (*pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void *bpf_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct bpf_iter_seq_map_info *info = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ++info->map_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) bpf_map_put((struct bpf_map *)v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return bpf_map_get_curr_or_next(&info->map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct bpf_iter__bpf_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __bpf_md_ptr(struct bpf_iter_meta *, meta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __bpf_md_ptr(struct bpf_map *, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) DEFINE_BPF_ITER_FUNC(bpf_map, struct bpf_iter_meta *meta, struct bpf_map *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int __bpf_map_seq_show(struct seq_file *seq, void *v, bool in_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct bpf_iter__bpf_map ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct bpf_iter_meta meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct bpf_prog *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ctx.meta = &meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ctx.map = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) meta.seq = seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) prog = bpf_iter_get_info(&meta, in_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = bpf_iter_run_prog(prog, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int bpf_map_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return __bpf_map_seq_show(seq, v, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void bpf_map_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (void)__bpf_map_seq_show(seq, v, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) bpf_map_put((struct bpf_map *)v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static const struct seq_operations bpf_map_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .start = bpf_map_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .next = bpf_map_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .stop = bpf_map_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .show = bpf_map_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) BTF_ID_LIST(btf_bpf_map_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) BTF_ID(struct, bpf_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static const struct bpf_iter_seq_info bpf_map_seq_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .seq_ops = &bpf_map_seq_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .init_seq_private = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .fini_seq_private = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .seq_priv_size = sizeof(struct bpf_iter_seq_map_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static struct bpf_iter_reg bpf_map_reg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .target = "bpf_map",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .ctx_arg_info_size = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .ctx_arg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) { offsetof(struct bpf_iter__bpf_map, map),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) PTR_TO_BTF_ID_OR_NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .seq_info = &bpf_map_seq_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int bpf_iter_attach_map(struct bpf_prog *prog,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) union bpf_iter_link_info *linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct bpf_iter_aux_info *aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u32 key_acc_size, value_acc_size, key_size, value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct bpf_map *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) bool is_percpu = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!linfo->map.map_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) map = bpf_map_get_with_uref(linfo->map.map_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (IS_ERR(map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return PTR_ERR(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) is_percpu = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) else if (map->map_type != BPF_MAP_TYPE_HASH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) map->map_type != BPF_MAP_TYPE_LRU_HASH &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) map->map_type != BPF_MAP_TYPE_ARRAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto put_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) key_acc_size = prog->aux->max_rdonly_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) value_acc_size = prog->aux->max_rdwr_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) key_size = map->key_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!is_percpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) value_size = map->value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) value_size = round_up(map->value_size, 8) * num_possible_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (key_acc_size > key_size || value_acc_size > value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto put_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) aux->map = map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) put_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) bpf_map_put_with_uref(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void bpf_iter_detach_map(struct bpf_iter_aux_info *aux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) bpf_map_put_with_uref(aux->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) seq_printf(seq, "map_id:\t%u\n", aux->map->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct bpf_link_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) info->iter.map.map_id = aux->map->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) DEFINE_BPF_ITER_FUNC(bpf_map_elem, struct bpf_iter_meta *meta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct bpf_map *map, void *key, void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static const struct bpf_iter_reg bpf_map_elem_reg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .target = "bpf_map_elem",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .attach_target = bpf_iter_attach_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .detach_target = bpf_iter_detach_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .show_fdinfo = bpf_iter_map_show_fdinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .fill_link_info = bpf_iter_map_fill_link_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .ctx_arg_info_size = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .ctx_arg_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) { offsetof(struct bpf_iter__bpf_map_elem, key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) PTR_TO_RDONLY_BUF_OR_NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) { offsetof(struct bpf_iter__bpf_map_elem, value),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) PTR_TO_RDWR_BUF_OR_NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int __init bpf_map_iter_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) bpf_map_reg_info.ctx_arg_info[0].btf_id = *btf_bpf_map_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = bpf_iter_reg_target(&bpf_map_reg_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return bpf_iter_reg_target(&bpf_map_elem_reg_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) late_initcall(bpf_map_iter_init);