|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright (c) 2025 Isovalent */ |
| 3 | + |
| 4 | +#include <linux/bpf.h> |
| 5 | + |
| 6 | +#define MAX_INSN_ARRAY_ENTRIES 256 |
| 7 | + |
| 8 | +struct bpf_insn_array { |
| 9 | + struct bpf_map map; |
| 10 | + atomic_t used; |
| 11 | + long *ips; |
| 12 | + DECLARE_FLEX_ARRAY(struct bpf_insn_array_value, values); |
| 13 | +}; |
| 14 | + |
| 15 | +#define cast_insn_array(MAP_PTR) \ |
| 16 | + container_of((MAP_PTR), struct bpf_insn_array, map) |
| 17 | + |
| 18 | +#define INSN_DELETED ((u32)-1) |
| 19 | + |
| 20 | +static inline u32 insn_array_alloc_size(u32 max_entries) |
| 21 | +{ |
| 22 | + const u32 base_size = sizeof(struct bpf_insn_array); |
| 23 | + const u32 entry_size = sizeof(struct bpf_insn_array_value); |
| 24 | + |
| 25 | + return base_size + max_entries * (entry_size + sizeof(long)); |
| 26 | +} |
| 27 | + |
| 28 | +static int insn_array_alloc_check(union bpf_attr *attr) |
| 29 | +{ |
| 30 | + u32 value_size = sizeof(struct bpf_insn_array_value); |
| 31 | + |
| 32 | + if (attr->max_entries == 0 || attr->key_size != 4 || |
| 33 | + attr->value_size != value_size || attr->map_flags != 0) |
| 34 | + return -EINVAL; |
| 35 | + |
| 36 | + if (attr->max_entries > MAX_INSN_ARRAY_ENTRIES) |
| 37 | + return -E2BIG; |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static void insn_array_free(struct bpf_map *map) |
| 43 | +{ |
| 44 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 45 | + |
| 46 | + bpf_map_area_free(insn_array); |
| 47 | +} |
| 48 | + |
| 49 | +static struct bpf_map *insn_array_alloc(union bpf_attr *attr) |
| 50 | +{ |
| 51 | + u64 size = insn_array_alloc_size(attr->max_entries); |
| 52 | + struct bpf_insn_array *insn_array; |
| 53 | + |
| 54 | + insn_array = bpf_map_area_alloc(size, NUMA_NO_NODE); |
| 55 | + if (!insn_array) |
| 56 | + return ERR_PTR(-ENOMEM); |
| 57 | + |
| 58 | + /* ips are allocated right after the insn_array->values[] array */ |
| 59 | + insn_array->ips = (void *)&insn_array->values[attr->max_entries]; |
| 60 | + |
| 61 | + bpf_map_init_from_attr(&insn_array->map, attr); |
| 62 | + |
| 63 | + return &insn_array->map; |
| 64 | +} |
| 65 | + |
| 66 | +static void *insn_array_lookup_elem(struct bpf_map *map, void *key) |
| 67 | +{ |
| 68 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 69 | + u32 index = *(u32 *)key; |
| 70 | + |
| 71 | + if (unlikely(index >= insn_array->map.max_entries)) |
| 72 | + return NULL; |
| 73 | + |
| 74 | + return &insn_array->values[index]; |
| 75 | +} |
| 76 | + |
| 77 | +static long insn_array_update_elem(struct bpf_map *map, void *key, void *value, u64 map_flags) |
| 78 | +{ |
| 79 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 80 | + u32 index = *(u32 *)key; |
| 81 | + struct bpf_insn_array_value val = {}; |
| 82 | + |
| 83 | + if (unlikely(index >= insn_array->map.max_entries)) |
| 84 | + return -E2BIG; |
| 85 | + |
| 86 | + if (unlikely(map_flags & BPF_NOEXIST)) |
| 87 | + return -EEXIST; |
| 88 | + |
| 89 | + copy_map_value(map, &val, value); |
| 90 | + if (val.jitted_off || val.xlated_off) |
| 91 | + return -EINVAL; |
| 92 | + |
| 93 | + insn_array->values[index].orig_off = val.orig_off; |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +static long insn_array_delete_elem(struct bpf_map *map, void *key) |
| 99 | +{ |
| 100 | + return -EINVAL; |
| 101 | +} |
| 102 | + |
| 103 | +static int insn_array_check_btf(const struct bpf_map *map, |
| 104 | + const struct btf *btf, |
| 105 | + const struct btf_type *key_type, |
| 106 | + const struct btf_type *value_type) |
| 107 | +{ |
| 108 | + if (!btf_type_is_i32(key_type)) |
| 109 | + return -EINVAL; |
| 110 | + |
| 111 | + if (!btf_type_is_i64(value_type)) |
| 112 | + return -EINVAL; |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +static u64 insn_array_mem_usage(const struct bpf_map *map) |
| 118 | +{ |
| 119 | + return insn_array_alloc_size(map->max_entries); |
| 120 | +} |
| 121 | + |
| 122 | +BTF_ID_LIST_SINGLE(insn_array_btf_ids, struct, bpf_insn_array) |
| 123 | + |
| 124 | +const struct bpf_map_ops insn_array_map_ops = { |
| 125 | + .map_alloc_check = insn_array_alloc_check, |
| 126 | + .map_alloc = insn_array_alloc, |
| 127 | + .map_free = insn_array_free, |
| 128 | + .map_get_next_key = bpf_array_get_next_key, |
| 129 | + .map_lookup_elem = insn_array_lookup_elem, |
| 130 | + .map_update_elem = insn_array_update_elem, |
| 131 | + .map_delete_elem = insn_array_delete_elem, |
| 132 | + .map_check_btf = insn_array_check_btf, |
| 133 | + .map_mem_usage = insn_array_mem_usage, |
| 134 | + .map_btf_id = &insn_array_btf_ids[0], |
| 135 | +}; |
| 136 | + |
| 137 | +static inline bool is_frozen(struct bpf_map *map) |
| 138 | +{ |
| 139 | + guard(mutex)(&map->freeze_mutex); |
| 140 | + |
| 141 | + return map->frozen; |
| 142 | +} |
| 143 | + |
| 144 | +static bool is_insn_array(const struct bpf_map *map) |
| 145 | +{ |
| 146 | + return map->map_type == BPF_MAP_TYPE_INSN_ARRAY; |
| 147 | +} |
| 148 | + |
| 149 | +static inline bool valid_offsets(const struct bpf_insn_array *insn_array, |
| 150 | + const struct bpf_prog *prog) |
| 151 | +{ |
| 152 | + u32 off; |
| 153 | + int i; |
| 154 | + |
| 155 | + for (i = 0; i < insn_array->map.max_entries; i++) { |
| 156 | + off = insn_array->values[i].orig_off; |
| 157 | + |
| 158 | + if (off >= prog->len) |
| 159 | + return false; |
| 160 | + |
| 161 | + if (off > 0) { |
| 162 | + if (prog->insnsi[off-1].code == (BPF_LD | BPF_DW | BPF_IMM)) |
| 163 | + return false; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return true; |
| 168 | +} |
| 169 | + |
| 170 | +int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog) |
| 171 | +{ |
| 172 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 173 | + struct bpf_insn_array_value *values = insn_array->values; |
| 174 | + int i; |
| 175 | + |
| 176 | + if (!is_frozen(map)) |
| 177 | + return -EINVAL; |
| 178 | + |
| 179 | + if (!valid_offsets(insn_array, prog)) |
| 180 | + return -EINVAL; |
| 181 | + |
| 182 | + /* |
| 183 | + * There can be only one program using the map |
| 184 | + */ |
| 185 | + if (atomic_xchg(&insn_array->used, 1)) |
| 186 | + return -EBUSY; |
| 187 | + |
| 188 | + /* |
| 189 | + * Reset all the map indexes to the original values. This is needed, |
| 190 | + * e.g., when a replay of verification with different log level should |
| 191 | + * be performed. |
| 192 | + */ |
| 193 | + for (i = 0; i < map->max_entries; i++) |
| 194 | + values[i].xlated_off = values[i].orig_off; |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
| 198 | + |
| 199 | +int bpf_insn_array_ready(struct bpf_map *map) |
| 200 | +{ |
| 201 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 202 | + int i; |
| 203 | + |
| 204 | + for (i = 0; i < map->max_entries; i++) { |
| 205 | + if (insn_array->values[i].xlated_off == INSN_DELETED) |
| 206 | + continue; |
| 207 | + if (!insn_array->ips[i]) |
| 208 | + return -EFAULT; |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | +} |
| 213 | + |
| 214 | +void bpf_insn_array_release(struct bpf_map *map) |
| 215 | +{ |
| 216 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 217 | + |
| 218 | + atomic_set(&insn_array->used, 0); |
| 219 | +} |
| 220 | + |
| 221 | +void bpf_insn_array_adjust(struct bpf_map *map, u32 off, u32 len) |
| 222 | +{ |
| 223 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 224 | + int i; |
| 225 | + |
| 226 | + if (len <= 1) |
| 227 | + return; |
| 228 | + |
| 229 | + for (i = 0; i < map->max_entries; i++) { |
| 230 | + if (insn_array->values[i].xlated_off <= off) |
| 231 | + continue; |
| 232 | + if (insn_array->values[i].xlated_off == INSN_DELETED) |
| 233 | + continue; |
| 234 | + insn_array->values[i].xlated_off += len - 1; |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +void bpf_insn_array_adjust_after_remove(struct bpf_map *map, u32 off, u32 len) |
| 239 | +{ |
| 240 | + struct bpf_insn_array *insn_array = cast_insn_array(map); |
| 241 | + int i; |
| 242 | + |
| 243 | + for (i = 0; i < map->max_entries; i++) { |
| 244 | + if (insn_array->values[i].xlated_off < off) |
| 245 | + continue; |
| 246 | + if (insn_array->values[i].xlated_off == INSN_DELETED) |
| 247 | + continue; |
| 248 | + if (insn_array->values[i].xlated_off < off + len) |
| 249 | + insn_array->values[i].xlated_off = INSN_DELETED; |
| 250 | + else |
| 251 | + insn_array->values[i].xlated_off -= len; |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +/* |
| 256 | + * This function is called by JITs. The image is the real program |
| 257 | + * image, the offsets array set up the xlated -> jitted mapping. |
| 258 | + */ |
| 259 | +void bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image) |
| 260 | +{ |
| 261 | + struct bpf_insn_array *insn_array; |
| 262 | + struct bpf_map *map; |
| 263 | + u32 xlated_off; |
| 264 | + int i, j; |
| 265 | + |
| 266 | + if (!offsets || !image) |
| 267 | + return; |
| 268 | + |
| 269 | + for (i = 0; i < prog->aux->used_map_cnt; i++) { |
| 270 | + map = prog->aux->used_maps[i]; |
| 271 | + if (!is_insn_array(map)) |
| 272 | + continue; |
| 273 | + |
| 274 | + insn_array = cast_insn_array(map); |
| 275 | + for (j = 0; j < map->max_entries; j++) { |
| 276 | + xlated_off = insn_array->values[j].xlated_off; |
| 277 | + if (xlated_off == INSN_DELETED) |
| 278 | + continue; |
| 279 | + if (xlated_off < prog->aux->subprog_start) |
| 280 | + continue; |
| 281 | + xlated_off -= prog->aux->subprog_start; |
| 282 | + if (xlated_off >= prog->len) |
| 283 | + continue; |
| 284 | + |
| 285 | + insn_array->values[j].jitted_off = offsets[xlated_off]; |
| 286 | + insn_array->ips[j] = (long)(image + offsets[xlated_off]); |
| 287 | + } |
| 288 | + } |
| 289 | +} |
0 commit comments