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