Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016,2017 Facebook |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/bpf.h> |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 14 | #include <linux/btf.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 15 | #include <linux/err.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/mm.h> |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 18 | #include <linux/filter.h> |
Daniel Borkmann | 0cdf5640 | 2015-10-02 18:42:00 +0200 | [diff] [blame] | 19 | #include <linux/perf_event.h> |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 20 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 21 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 22 | #include "map_in_map.h" |
| 23 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 24 | #define ARRAY_CREATE_FLAG_MASK \ |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame^] | 25 | (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK) |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 26 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 27 | static void bpf_array_free_percpu(struct bpf_array *array) |
| 28 | { |
| 29 | int i; |
| 30 | |
Eric Dumazet | 32fff23 | 2018-02-22 08:33:24 -0800 | [diff] [blame] | 31 | for (i = 0; i < array->map.max_entries; i++) { |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 32 | free_percpu(array->pptrs[i]); |
Eric Dumazet | 32fff23 | 2018-02-22 08:33:24 -0800 | [diff] [blame] | 33 | cond_resched(); |
| 34 | } |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int bpf_array_alloc_percpu(struct bpf_array *array) |
| 38 | { |
| 39 | void __percpu *ptr; |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < array->map.max_entries; i++) { |
| 43 | ptr = __alloc_percpu_gfp(array->elem_size, 8, |
| 44 | GFP_USER | __GFP_NOWARN); |
| 45 | if (!ptr) { |
| 46 | bpf_array_free_percpu(array); |
| 47 | return -ENOMEM; |
| 48 | } |
| 49 | array->pptrs[i] = ptr; |
Eric Dumazet | 32fff23 | 2018-02-22 08:33:24 -0800 | [diff] [blame] | 50 | cond_resched(); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 56 | /* Called from syscall */ |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 57 | int array_map_alloc_check(union bpf_attr *attr) |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 58 | { |
| 59 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY; |
| 60 | int numa_node = bpf_map_attr_numa_node(attr); |
| 61 | |
| 62 | /* check sanity of attributes */ |
| 63 | if (attr->max_entries == 0 || attr->key_size != 4 || |
| 64 | attr->value_size == 0 || |
| 65 | attr->map_flags & ~ARRAY_CREATE_FLAG_MASK || |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame^] | 66 | !bpf_map_flags_access_ok(attr->map_flags) || |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 67 | (percpu && numa_node != NUMA_NO_NODE)) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | if (attr->value_size > KMALLOC_MAX_SIZE) |
| 71 | /* if value_size is bigger, the user space won't be able to |
| 72 | * access the elements. |
| 73 | */ |
| 74 | return -E2BIG; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 79 | static struct bpf_map *array_map_alloc(union bpf_attr *attr) |
| 80 | { |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 81 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY; |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 82 | int ret, numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 83 | u32 elem_size, index_mask, max_entries; |
| 84 | bool unpriv = !capable(CAP_SYS_ADMIN); |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 85 | u64 cost, array_size, mask64; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 86 | struct bpf_array *array; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 87 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 88 | elem_size = round_up(attr->value_size, 8); |
| 89 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 90 | max_entries = attr->max_entries; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 91 | |
Daniel Borkmann | bbeb6e4 | 2018-01-10 23:25:05 +0100 | [diff] [blame] | 92 | /* On 32 bit archs roundup_pow_of_two() with max_entries that has |
| 93 | * upper most bit set in u32 space is undefined behavior due to |
| 94 | * resulting 1U << 32, so do it manually here in u64 space. |
| 95 | */ |
| 96 | mask64 = fls_long(max_entries - 1); |
| 97 | mask64 = 1ULL << mask64; |
| 98 | mask64 -= 1; |
| 99 | |
| 100 | index_mask = mask64; |
| 101 | if (unpriv) { |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 102 | /* round up array size to nearest power of 2, |
| 103 | * since cpu will speculate within index_mask limits |
| 104 | */ |
| 105 | max_entries = index_mask + 1; |
Daniel Borkmann | bbeb6e4 | 2018-01-10 23:25:05 +0100 | [diff] [blame] | 106 | /* Check for overflows. */ |
| 107 | if (max_entries < attr->max_entries) |
| 108 | return ERR_PTR(-E2BIG); |
| 109 | } |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 110 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 111 | array_size = sizeof(*array); |
| 112 | if (percpu) |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 113 | array_size += (u64) max_entries * sizeof(void *); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 114 | else |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 115 | array_size += (u64) max_entries * elem_size; |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 116 | |
| 117 | /* make sure there is no u32 overflow later in round_up() */ |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 118 | cost = array_size; |
| 119 | if (cost >= U32_MAX - PAGE_SIZE) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 120 | return ERR_PTR(-ENOMEM); |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 121 | if (percpu) { |
| 122 | cost += (u64)attr->max_entries * elem_size * num_possible_cpus(); |
| 123 | if (cost >= U32_MAX - PAGE_SIZE) |
| 124 | return ERR_PTR(-ENOMEM); |
| 125 | } |
| 126 | cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; |
| 127 | |
| 128 | ret = bpf_map_precharge_memlock(cost); |
| 129 | if (ret < 0) |
| 130 | return ERR_PTR(ret); |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 131 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 132 | /* allocate all map elements and zero-initialize them */ |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 133 | array = bpf_map_area_alloc(array_size, numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 134 | if (!array) |
| 135 | return ERR_PTR(-ENOMEM); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 136 | array->index_mask = index_mask; |
| 137 | array->map.unpriv_array = unpriv; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 138 | |
| 139 | /* copy mandatory map attributes */ |
Jakub Kicinski | 3285264 | 2018-01-17 19:13:26 -0800 | [diff] [blame] | 140 | bpf_map_init_from_attr(&array->map, attr); |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 141 | array->map.pages = cost; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 142 | array->elem_size = elem_size; |
| 143 | |
Daniel Borkmann | 9c2d63b | 2018-02-16 01:10:29 +0100 | [diff] [blame] | 144 | if (percpu && bpf_array_alloc_percpu(array)) { |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 145 | bpf_map_area_free(array); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 146 | return ERR_PTR(-ENOMEM); |
| 147 | } |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 148 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 149 | return &array->map; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Called from syscall or from eBPF program */ |
| 153 | static void *array_map_lookup_elem(struct bpf_map *map, void *key) |
| 154 | { |
| 155 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 156 | u32 index = *(u32 *)key; |
| 157 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 158 | if (unlikely(index >= array->map.max_entries)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 159 | return NULL; |
| 160 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 161 | return array->value + array->elem_size * (index & array->index_mask); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 164 | static int array_map_direct_value_addr(const struct bpf_map *map, u64 *imm, |
| 165 | u32 off) |
| 166 | { |
| 167 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 168 | |
| 169 | if (map->max_entries != 1) |
| 170 | return -ENOTSUPP; |
| 171 | if (off >= map->value_size) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | *imm = (unsigned long)array->value; |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int array_map_direct_value_meta(const struct bpf_map *map, u64 imm, |
| 179 | u32 *off) |
| 180 | { |
| 181 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 182 | u64 base = (unsigned long)array->value; |
| 183 | u64 range = array->elem_size; |
| 184 | |
| 185 | if (map->max_entries != 1) |
| 186 | return -ENOTSUPP; |
| 187 | if (imm < base || imm >= base + range) |
| 188 | return -ENOENT; |
| 189 | |
| 190 | *off = imm - base; |
| 191 | return 0; |
| 192 | } |
| 193 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 194 | /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */ |
| 195 | static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) |
| 196 | { |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 197 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 198 | struct bpf_insn *insn = insn_buf; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 199 | u32 elem_size = round_up(map->value_size, 8); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 200 | const int ret = BPF_REG_0; |
| 201 | const int map_ptr = BPF_REG_1; |
| 202 | const int index = BPF_REG_2; |
| 203 | |
| 204 | *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value)); |
| 205 | *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 206 | if (map->unpriv_array) { |
| 207 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4); |
| 208 | *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask); |
| 209 | } else { |
| 210 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3); |
| 211 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 212 | |
| 213 | if (is_power_of_2(elem_size)) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 214 | *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size)); |
| 215 | } else { |
| 216 | *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size); |
| 217 | } |
| 218 | *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr); |
| 219 | *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1); |
| 220 | *insn++ = BPF_MOV64_IMM(ret, 0); |
| 221 | return insn - insn_buf; |
| 222 | } |
| 223 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 224 | /* Called from eBPF program */ |
| 225 | static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key) |
| 226 | { |
| 227 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 228 | u32 index = *(u32 *)key; |
| 229 | |
| 230 | if (unlikely(index >= array->map.max_entries)) |
| 231 | return NULL; |
| 232 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 233 | return this_cpu_ptr(array->pptrs[index & array->index_mask]); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 236 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value) |
| 237 | { |
| 238 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 239 | u32 index = *(u32 *)key; |
| 240 | void __percpu *pptr; |
| 241 | int cpu, off = 0; |
| 242 | u32 size; |
| 243 | |
| 244 | if (unlikely(index >= array->map.max_entries)) |
| 245 | return -ENOENT; |
| 246 | |
| 247 | /* per_cpu areas are zero-filled and bpf programs can only |
| 248 | * access 'value_size' of them, so copying rounded areas |
| 249 | * will not leak any kernel data |
| 250 | */ |
| 251 | size = round_up(map->value_size, 8); |
| 252 | rcu_read_lock(); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 253 | pptr = array->pptrs[index & array->index_mask]; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 254 | for_each_possible_cpu(cpu) { |
| 255 | bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size); |
| 256 | off += size; |
| 257 | } |
| 258 | rcu_read_unlock(); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 262 | /* Called from syscall */ |
| 263 | static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 264 | { |
| 265 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 266 | u32 index = key ? *(u32 *)key : U32_MAX; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 267 | u32 *next = (u32 *)next_key; |
| 268 | |
| 269 | if (index >= array->map.max_entries) { |
| 270 | *next = 0; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | if (index == array->map.max_entries - 1) |
| 275 | return -ENOENT; |
| 276 | |
| 277 | *next = index + 1; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /* Called from syscall or from eBPF program */ |
| 282 | static int array_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 283 | u64 map_flags) |
| 284 | { |
| 285 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 286 | u32 index = *(u32 *)key; |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 287 | char *val; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 288 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 289 | if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 290 | /* unknown flags */ |
| 291 | return -EINVAL; |
| 292 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 293 | if (unlikely(index >= array->map.max_entries)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 294 | /* all elements were pre-allocated, cannot insert a new one */ |
| 295 | return -E2BIG; |
| 296 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 297 | if (unlikely(map_flags & BPF_NOEXIST)) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 298 | /* all elements already exist */ |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 299 | return -EEXIST; |
| 300 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 301 | if (unlikely((map_flags & BPF_F_LOCK) && |
| 302 | !map_value_has_spin_lock(map))) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 306 | memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]), |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 307 | value, map->value_size); |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 308 | } else { |
| 309 | val = array->value + |
| 310 | array->elem_size * (index & array->index_mask); |
| 311 | if (map_flags & BPF_F_LOCK) |
| 312 | copy_map_value_locked(map, val, value, false); |
| 313 | else |
| 314 | copy_map_value(map, val, value); |
| 315 | } |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 319 | int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, |
| 320 | u64 map_flags) |
| 321 | { |
| 322 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 323 | u32 index = *(u32 *)key; |
| 324 | void __percpu *pptr; |
| 325 | int cpu, off = 0; |
| 326 | u32 size; |
| 327 | |
| 328 | if (unlikely(map_flags > BPF_EXIST)) |
| 329 | /* unknown flags */ |
| 330 | return -EINVAL; |
| 331 | |
| 332 | if (unlikely(index >= array->map.max_entries)) |
| 333 | /* all elements were pre-allocated, cannot insert a new one */ |
| 334 | return -E2BIG; |
| 335 | |
| 336 | if (unlikely(map_flags == BPF_NOEXIST)) |
| 337 | /* all elements already exist */ |
| 338 | return -EEXIST; |
| 339 | |
| 340 | /* the user space will provide round_up(value_size, 8) bytes that |
| 341 | * will be copied into per-cpu area. bpf programs can only access |
| 342 | * value_size of it. During lookup the same extra bytes will be |
| 343 | * returned or zeros which were zero-filled by percpu_alloc, |
| 344 | * so no kernel data leaks possible |
| 345 | */ |
| 346 | size = round_up(map->value_size, 8); |
| 347 | rcu_read_lock(); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 348 | pptr = array->pptrs[index & array->index_mask]; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 349 | for_each_possible_cpu(cpu) { |
| 350 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size); |
| 351 | off += size; |
| 352 | } |
| 353 | rcu_read_unlock(); |
| 354 | return 0; |
| 355 | } |
| 356 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 357 | /* Called from syscall or from eBPF program */ |
| 358 | static int array_map_delete_elem(struct bpf_map *map, void *key) |
| 359 | { |
| 360 | return -EINVAL; |
| 361 | } |
| 362 | |
| 363 | /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ |
| 364 | static void array_map_free(struct bpf_map *map) |
| 365 | { |
| 366 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 367 | |
| 368 | /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, |
| 369 | * so the programs (can be more than one that used this map) were |
| 370 | * disconnected from events. Wait for outstanding programs to complete |
| 371 | * and free the array |
| 372 | */ |
| 373 | synchronize_rcu(); |
| 374 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 375 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 376 | bpf_array_free_percpu(array); |
| 377 | |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 378 | bpf_map_area_free(array); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 381 | static void array_map_seq_show_elem(struct bpf_map *map, void *key, |
| 382 | struct seq_file *m) |
| 383 | { |
| 384 | void *value; |
| 385 | |
| 386 | rcu_read_lock(); |
| 387 | |
| 388 | value = array_map_lookup_elem(map, key); |
| 389 | if (!value) { |
| 390 | rcu_read_unlock(); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | seq_printf(m, "%u: ", *(u32 *)key); |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 395 | btf_type_seq_show(map->btf, map->btf_value_type_id, value, m); |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 396 | seq_puts(m, "\n"); |
| 397 | |
| 398 | rcu_read_unlock(); |
| 399 | } |
| 400 | |
Yonghong Song | c7b27c3 | 2018-08-29 14:43:13 -0700 | [diff] [blame] | 401 | static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key, |
| 402 | struct seq_file *m) |
| 403 | { |
| 404 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 405 | u32 index = *(u32 *)key; |
| 406 | void __percpu *pptr; |
| 407 | int cpu; |
| 408 | |
| 409 | rcu_read_lock(); |
| 410 | |
| 411 | seq_printf(m, "%u: {\n", *(u32 *)key); |
| 412 | pptr = array->pptrs[index & array->index_mask]; |
| 413 | for_each_possible_cpu(cpu) { |
| 414 | seq_printf(m, "\tcpu%d: ", cpu); |
| 415 | btf_type_seq_show(map->btf, map->btf_value_type_id, |
| 416 | per_cpu_ptr(pptr, cpu), m); |
| 417 | seq_puts(m, "\n"); |
| 418 | } |
| 419 | seq_puts(m, "}\n"); |
| 420 | |
| 421 | rcu_read_unlock(); |
| 422 | } |
| 423 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 424 | static int array_map_check_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 425 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 426 | const struct btf_type *key_type, |
| 427 | const struct btf_type *value_type) |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 428 | { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 429 | u32 int_data; |
| 430 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 431 | if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT) |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 432 | return -EINVAL; |
| 433 | |
| 434 | int_data = *(u32 *)(key_type + 1); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 435 | /* bpf array can only take a u32 key. This check makes sure |
| 436 | * that the btf matches the attr used during map_create. |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 437 | */ |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 438 | if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data)) |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 444 | const struct bpf_map_ops array_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 445 | .map_alloc_check = array_map_alloc_check, |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 446 | .map_alloc = array_map_alloc, |
| 447 | .map_free = array_map_free, |
| 448 | .map_get_next_key = array_map_get_next_key, |
| 449 | .map_lookup_elem = array_map_lookup_elem, |
| 450 | .map_update_elem = array_map_update_elem, |
| 451 | .map_delete_elem = array_map_delete_elem, |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 452 | .map_gen_lookup = array_map_gen_lookup, |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 453 | .map_direct_value_addr = array_map_direct_value_addr, |
| 454 | .map_direct_value_meta = array_map_direct_value_meta, |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 455 | .map_seq_show_elem = array_map_seq_show_elem, |
| 456 | .map_check_btf = array_map_check_btf, |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 457 | }; |
| 458 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 459 | const struct bpf_map_ops percpu_array_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 460 | .map_alloc_check = array_map_alloc_check, |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 461 | .map_alloc = array_map_alloc, |
| 462 | .map_free = array_map_free, |
| 463 | .map_get_next_key = array_map_get_next_key, |
| 464 | .map_lookup_elem = percpu_array_map_lookup_elem, |
| 465 | .map_update_elem = array_map_update_elem, |
| 466 | .map_delete_elem = array_map_delete_elem, |
Yonghong Song | c7b27c3 | 2018-08-29 14:43:13 -0700 | [diff] [blame] | 467 | .map_seq_show_elem = percpu_array_map_seq_show_elem, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 468 | .map_check_btf = array_map_check_btf, |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 469 | }; |
| 470 | |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 471 | static int fd_array_map_alloc_check(union bpf_attr *attr) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 472 | { |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 473 | /* only file descriptors can be stored in this type of map */ |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 474 | if (attr->value_size != sizeof(u32)) |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 475 | return -EINVAL; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame^] | 476 | /* Program read-only/write-only not supported for special maps yet. */ |
| 477 | if (attr->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) |
| 478 | return -EINVAL; |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 479 | return array_map_alloc_check(attr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 482 | static void fd_array_map_free(struct bpf_map *map) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 483 | { |
| 484 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 485 | int i; |
| 486 | |
| 487 | synchronize_rcu(); |
| 488 | |
| 489 | /* make sure it's empty */ |
| 490 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 491 | BUG_ON(array->ptrs[i] != NULL); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 492 | |
| 493 | bpf_map_area_free(array); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 496 | static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 497 | { |
Prashant Bhole | 3b4a63f | 2018-10-09 10:04:50 +0900 | [diff] [blame] | 498 | return ERR_PTR(-EOPNOTSUPP); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* only called from syscall */ |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 502 | int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value) |
| 503 | { |
| 504 | void **elem, *ptr; |
| 505 | int ret = 0; |
| 506 | |
| 507 | if (!map->ops->map_fd_sys_lookup_elem) |
| 508 | return -ENOTSUPP; |
| 509 | |
| 510 | rcu_read_lock(); |
| 511 | elem = array_map_lookup_elem(map, key); |
| 512 | if (elem && (ptr = READ_ONCE(*elem))) |
| 513 | *value = map->ops->map_fd_sys_lookup_elem(ptr); |
| 514 | else |
| 515 | ret = -ENOENT; |
| 516 | rcu_read_unlock(); |
| 517 | |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | /* only called from syscall */ |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 522 | int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file, |
| 523 | void *key, void *value, u64 map_flags) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 524 | { |
| 525 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 526 | void *new_ptr, *old_ptr; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 527 | u32 index = *(u32 *)key, ufd; |
| 528 | |
| 529 | if (map_flags != BPF_ANY) |
| 530 | return -EINVAL; |
| 531 | |
| 532 | if (index >= array->map.max_entries) |
| 533 | return -E2BIG; |
| 534 | |
| 535 | ufd = *(u32 *)value; |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 536 | new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 537 | if (IS_ERR(new_ptr)) |
| 538 | return PTR_ERR(new_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 539 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 540 | old_ptr = xchg(array->ptrs + index, new_ptr); |
| 541 | if (old_ptr) |
| 542 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 547 | static int fd_array_map_delete_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 548 | { |
| 549 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 550 | void *old_ptr; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 551 | u32 index = *(u32 *)key; |
| 552 | |
| 553 | if (index >= array->map.max_entries) |
| 554 | return -E2BIG; |
| 555 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 556 | old_ptr = xchg(array->ptrs + index, NULL); |
| 557 | if (old_ptr) { |
| 558 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } else { |
| 561 | return -ENOENT; |
| 562 | } |
| 563 | } |
| 564 | |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 565 | static void *prog_fd_array_get_ptr(struct bpf_map *map, |
| 566 | struct file *map_file, int fd) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 567 | { |
| 568 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 569 | struct bpf_prog *prog = bpf_prog_get(fd); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 570 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 571 | if (IS_ERR(prog)) |
| 572 | return prog; |
| 573 | |
| 574 | if (!bpf_prog_array_compatible(array, prog)) { |
| 575 | bpf_prog_put(prog); |
| 576 | return ERR_PTR(-EINVAL); |
| 577 | } |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 578 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 579 | return prog; |
| 580 | } |
| 581 | |
| 582 | static void prog_fd_array_put_ptr(void *ptr) |
| 583 | { |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 584 | bpf_prog_put(ptr); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 587 | static u32 prog_fd_array_sys_lookup_elem(void *ptr) |
| 588 | { |
| 589 | return ((struct bpf_prog *)ptr)->aux->id; |
| 590 | } |
| 591 | |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 592 | /* decrement refcnt of all bpf_progs that are stored in this map */ |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 593 | static void bpf_fd_array_map_clear(struct bpf_map *map) |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 594 | { |
| 595 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 596 | int i; |
| 597 | |
| 598 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 599 | fd_array_map_delete_elem(map, &i); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Yonghong Song | a7c19db | 2018-09-06 17:26:04 -0700 | [diff] [blame] | 602 | static void prog_array_map_seq_show_elem(struct bpf_map *map, void *key, |
| 603 | struct seq_file *m) |
| 604 | { |
| 605 | void **elem, *ptr; |
| 606 | u32 prog_id; |
| 607 | |
| 608 | rcu_read_lock(); |
| 609 | |
| 610 | elem = array_map_lookup_elem(map, key); |
| 611 | if (elem) { |
| 612 | ptr = READ_ONCE(*elem); |
| 613 | if (ptr) { |
| 614 | seq_printf(m, "%u: ", *(u32 *)key); |
| 615 | prog_id = prog_fd_array_sys_lookup_elem(ptr); |
| 616 | btf_type_seq_show(map->btf, map->btf_value_type_id, |
| 617 | &prog_id, m); |
| 618 | seq_puts(m, "\n"); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | rcu_read_unlock(); |
| 623 | } |
| 624 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 625 | const struct bpf_map_ops prog_array_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 626 | .map_alloc_check = fd_array_map_alloc_check, |
| 627 | .map_alloc = array_map_alloc, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 628 | .map_free = fd_array_map_free, |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 629 | .map_get_next_key = array_map_get_next_key, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 630 | .map_lookup_elem = fd_array_map_lookup_elem, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 631 | .map_delete_elem = fd_array_map_delete_elem, |
| 632 | .map_fd_get_ptr = prog_fd_array_get_ptr, |
| 633 | .map_fd_put_ptr = prog_fd_array_put_ptr, |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 634 | .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem, |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 635 | .map_release_uref = bpf_fd_array_map_clear, |
Yonghong Song | a7c19db | 2018-09-06 17:26:04 -0700 | [diff] [blame] | 636 | .map_seq_show_elem = prog_array_map_seq_show_elem, |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 637 | }; |
| 638 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 639 | static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file, |
| 640 | struct file *map_file) |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 641 | { |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 642 | struct bpf_event_entry *ee; |
| 643 | |
Daniel Borkmann | 858d68f | 2016-07-16 01:15:55 +0200 | [diff] [blame] | 644 | ee = kzalloc(sizeof(*ee), GFP_ATOMIC); |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 645 | if (ee) { |
| 646 | ee->event = perf_file->private_data; |
| 647 | ee->perf_file = perf_file; |
| 648 | ee->map_file = map_file; |
| 649 | } |
| 650 | |
| 651 | return ee; |
| 652 | } |
| 653 | |
| 654 | static void __bpf_event_entry_free(struct rcu_head *rcu) |
| 655 | { |
| 656 | struct bpf_event_entry *ee; |
| 657 | |
| 658 | ee = container_of(rcu, struct bpf_event_entry, rcu); |
| 659 | fput(ee->perf_file); |
| 660 | kfree(ee); |
| 661 | } |
| 662 | |
| 663 | static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee) |
| 664 | { |
| 665 | call_rcu(&ee->rcu, __bpf_event_entry_free); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 668 | static void *perf_event_fd_array_get_ptr(struct bpf_map *map, |
| 669 | struct file *map_file, int fd) |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 670 | { |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 671 | struct bpf_event_entry *ee; |
| 672 | struct perf_event *event; |
| 673 | struct file *perf_file; |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 674 | u64 value; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 675 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 676 | perf_file = perf_event_get(fd); |
| 677 | if (IS_ERR(perf_file)) |
| 678 | return perf_file; |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 679 | |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 680 | ee = ERR_PTR(-EOPNOTSUPP); |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 681 | event = perf_file->private_data; |
Yonghong Song | 9756263 | 2017-10-05 09:19:19 -0700 | [diff] [blame] | 682 | if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP) |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 683 | goto err_out; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 684 | |
Alexei Starovoitov | f91840a | 2017-06-02 21:03:52 -0700 | [diff] [blame] | 685 | ee = bpf_event_entry_gen(perf_file, map_file); |
| 686 | if (ee) |
| 687 | return ee; |
| 688 | ee = ERR_PTR(-ENOMEM); |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 689 | err_out: |
| 690 | fput(perf_file); |
| 691 | return ee; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static void perf_event_fd_array_put_ptr(void *ptr) |
| 695 | { |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 696 | bpf_event_entry_free_rcu(ptr); |
| 697 | } |
| 698 | |
| 699 | static void perf_event_fd_array_release(struct bpf_map *map, |
| 700 | struct file *map_file) |
| 701 | { |
| 702 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 703 | struct bpf_event_entry *ee; |
| 704 | int i; |
| 705 | |
| 706 | rcu_read_lock(); |
| 707 | for (i = 0; i < array->map.max_entries; i++) { |
| 708 | ee = READ_ONCE(array->ptrs[i]); |
| 709 | if (ee && ee->map_file == map_file) |
| 710 | fd_array_map_delete_elem(map, &i); |
| 711 | } |
| 712 | rcu_read_unlock(); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 715 | const struct bpf_map_ops perf_event_array_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 716 | .map_alloc_check = fd_array_map_alloc_check, |
| 717 | .map_alloc = array_map_alloc, |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 718 | .map_free = fd_array_map_free, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 719 | .map_get_next_key = array_map_get_next_key, |
| 720 | .map_lookup_elem = fd_array_map_lookup_elem, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 721 | .map_delete_elem = fd_array_map_delete_elem, |
| 722 | .map_fd_get_ptr = perf_event_fd_array_get_ptr, |
| 723 | .map_fd_put_ptr = perf_event_fd_array_put_ptr, |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 724 | .map_release = perf_event_fd_array_release, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 725 | .map_check_btf = map_check_no_btf, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 726 | }; |
| 727 | |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 728 | #ifdef CONFIG_CGROUPS |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 729 | static void *cgroup_fd_array_get_ptr(struct bpf_map *map, |
| 730 | struct file *map_file /* not used */, |
| 731 | int fd) |
| 732 | { |
| 733 | return cgroup_get_from_fd(fd); |
| 734 | } |
| 735 | |
| 736 | static void cgroup_fd_array_put_ptr(void *ptr) |
| 737 | { |
| 738 | /* cgroup_put free cgrp after a rcu grace period */ |
| 739 | cgroup_put(ptr); |
| 740 | } |
| 741 | |
| 742 | static void cgroup_fd_array_free(struct bpf_map *map) |
| 743 | { |
| 744 | bpf_fd_array_map_clear(map); |
| 745 | fd_array_map_free(map); |
| 746 | } |
| 747 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 748 | const struct bpf_map_ops cgroup_array_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 749 | .map_alloc_check = fd_array_map_alloc_check, |
| 750 | .map_alloc = array_map_alloc, |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 751 | .map_free = cgroup_fd_array_free, |
| 752 | .map_get_next_key = array_map_get_next_key, |
| 753 | .map_lookup_elem = fd_array_map_lookup_elem, |
| 754 | .map_delete_elem = fd_array_map_delete_elem, |
| 755 | .map_fd_get_ptr = cgroup_fd_array_get_ptr, |
| 756 | .map_fd_put_ptr = cgroup_fd_array_put_ptr, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 757 | .map_check_btf = map_check_no_btf, |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 758 | }; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 759 | #endif |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 760 | |
| 761 | static struct bpf_map *array_of_map_alloc(union bpf_attr *attr) |
| 762 | { |
| 763 | struct bpf_map *map, *inner_map_meta; |
| 764 | |
| 765 | inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd); |
| 766 | if (IS_ERR(inner_map_meta)) |
| 767 | return inner_map_meta; |
| 768 | |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 769 | map = array_map_alloc(attr); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 770 | if (IS_ERR(map)) { |
| 771 | bpf_map_meta_free(inner_map_meta); |
| 772 | return map; |
| 773 | } |
| 774 | |
| 775 | map->inner_map_meta = inner_map_meta; |
| 776 | |
| 777 | return map; |
| 778 | } |
| 779 | |
| 780 | static void array_of_map_free(struct bpf_map *map) |
| 781 | { |
| 782 | /* map->inner_map_meta is only accessed by syscall which |
| 783 | * is protected by fdget/fdput. |
| 784 | */ |
| 785 | bpf_map_meta_free(map->inner_map_meta); |
| 786 | bpf_fd_array_map_clear(map); |
| 787 | fd_array_map_free(map); |
| 788 | } |
| 789 | |
| 790 | static void *array_of_map_lookup_elem(struct bpf_map *map, void *key) |
| 791 | { |
| 792 | struct bpf_map **inner_map = array_map_lookup_elem(map, key); |
| 793 | |
| 794 | if (!inner_map) |
| 795 | return NULL; |
| 796 | |
| 797 | return READ_ONCE(*inner_map); |
| 798 | } |
| 799 | |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 800 | static u32 array_of_map_gen_lookup(struct bpf_map *map, |
| 801 | struct bpf_insn *insn_buf) |
| 802 | { |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 803 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 804 | u32 elem_size = round_up(map->value_size, 8); |
| 805 | struct bpf_insn *insn = insn_buf; |
| 806 | const int ret = BPF_REG_0; |
| 807 | const int map_ptr = BPF_REG_1; |
| 808 | const int index = BPF_REG_2; |
| 809 | |
| 810 | *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value)); |
| 811 | *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 812 | if (map->unpriv_array) { |
| 813 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6); |
| 814 | *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask); |
| 815 | } else { |
| 816 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5); |
| 817 | } |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 818 | if (is_power_of_2(elem_size)) |
| 819 | *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size)); |
| 820 | else |
| 821 | *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size); |
| 822 | *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr); |
| 823 | *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0); |
| 824 | *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1); |
| 825 | *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1); |
| 826 | *insn++ = BPF_MOV64_IMM(ret, 0); |
| 827 | |
| 828 | return insn - insn_buf; |
| 829 | } |
| 830 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 831 | const struct bpf_map_ops array_of_maps_map_ops = { |
Jakub Kicinski | ad46061 | 2018-01-17 19:13:25 -0800 | [diff] [blame] | 832 | .map_alloc_check = fd_array_map_alloc_check, |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 833 | .map_alloc = array_of_map_alloc, |
| 834 | .map_free = array_of_map_free, |
| 835 | .map_get_next_key = array_map_get_next_key, |
| 836 | .map_lookup_elem = array_of_map_lookup_elem, |
| 837 | .map_delete_elem = fd_array_map_delete_elem, |
| 838 | .map_fd_get_ptr = bpf_map_fd_get_ptr, |
| 839 | .map_fd_put_ptr = bpf_map_fd_put_ptr, |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 840 | .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, |
Daniel Borkmann | 7b0c2a0 | 2017-08-19 03:12:46 +0200 | [diff] [blame] | 841 | .map_gen_lookup = array_of_map_gen_lookup, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 842 | .map_check_btf = map_check_no_btf, |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 843 | }; |