Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | * General Public License for more details. |
| 11 | */ |
| 12 | #include <linux/bpf.h> |
| 13 | #include <linux/err.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/mm.h> |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 16 | #include <linux/filter.h> |
Daniel Borkmann | 0cdf5640 | 2015-10-02 18:42:00 +0200 | [diff] [blame] | 17 | #include <linux/perf_event.h> |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 18 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 19 | static void bpf_array_free_percpu(struct bpf_array *array) |
| 20 | { |
| 21 | int i; |
| 22 | |
Eric Dumazet | 2a8bc53 | 2018-03-08 16:17:36 +0100 | [diff] [blame^] | 23 | for (i = 0; i < array->map.max_entries; i++) { |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 24 | free_percpu(array->pptrs[i]); |
Eric Dumazet | 2a8bc53 | 2018-03-08 16:17:36 +0100 | [diff] [blame^] | 25 | cond_resched(); |
| 26 | } |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int bpf_array_alloc_percpu(struct bpf_array *array) |
| 30 | { |
| 31 | void __percpu *ptr; |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < array->map.max_entries; i++) { |
| 35 | ptr = __alloc_percpu_gfp(array->elem_size, 8, |
| 36 | GFP_USER | __GFP_NOWARN); |
| 37 | if (!ptr) { |
| 38 | bpf_array_free_percpu(array); |
| 39 | return -ENOMEM; |
| 40 | } |
| 41 | array->pptrs[i] = ptr; |
Eric Dumazet | 2a8bc53 | 2018-03-08 16:17:36 +0100 | [diff] [blame^] | 42 | cond_resched(); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 48 | /* Called from syscall */ |
| 49 | static struct bpf_map *array_map_alloc(union bpf_attr *attr) |
| 50 | { |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 51 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY; |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 52 | u32 elem_size, index_mask, max_entries; |
| 53 | bool unpriv = !capable(CAP_SYS_ADMIN); |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 54 | u64 cost, array_size, mask64; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 55 | struct bpf_array *array; |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 56 | int ret; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 57 | |
| 58 | /* check sanity of attributes */ |
| 59 | if (attr->max_entries == 0 || attr->key_size != 4 || |
Alexei Starovoitov | 823707b | 2016-03-07 21:57:16 -0800 | [diff] [blame] | 60 | attr->value_size == 0 || attr->map_flags) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 61 | return ERR_PTR(-EINVAL); |
| 62 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 63 | if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1)) |
| 64 | /* if value_size is bigger, the user space won't be able to |
| 65 | * access the elements. |
| 66 | */ |
| 67 | return ERR_PTR(-E2BIG); |
| 68 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 69 | elem_size = round_up(attr->value_size, 8); |
| 70 | |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 71 | max_entries = attr->max_entries; |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 72 | |
Daniel Borkmann | 820ef2a | 2018-01-10 23:25:05 +0100 | [diff] [blame] | 73 | /* On 32 bit archs roundup_pow_of_two() with max_entries that has |
| 74 | * upper most bit set in u32 space is undefined behavior due to |
| 75 | * resulting 1U << 32, so do it manually here in u64 space. |
| 76 | */ |
| 77 | mask64 = fls_long(max_entries - 1); |
| 78 | mask64 = 1ULL << mask64; |
| 79 | mask64 -= 1; |
| 80 | |
| 81 | index_mask = mask64; |
| 82 | if (unpriv) { |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 83 | /* round up array size to nearest power of 2, |
| 84 | * since cpu will speculate within index_mask limits |
| 85 | */ |
| 86 | max_entries = index_mask + 1; |
Daniel Borkmann | 820ef2a | 2018-01-10 23:25:05 +0100 | [diff] [blame] | 87 | /* Check for overflows. */ |
| 88 | if (max_entries < attr->max_entries) |
| 89 | return ERR_PTR(-E2BIG); |
| 90 | } |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 91 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 92 | array_size = sizeof(*array); |
| 93 | if (percpu) |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 94 | array_size += (u64) max_entries * sizeof(void *); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 95 | else |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 96 | array_size += (u64) max_entries * elem_size; |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 97 | |
| 98 | /* make sure there is no u32 overflow later in round_up() */ |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 99 | cost = array_size; |
| 100 | if (cost >= U32_MAX - PAGE_SIZE) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 101 | return ERR_PTR(-ENOMEM); |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 102 | if (percpu) { |
| 103 | cost += (u64)attr->max_entries * elem_size * num_possible_cpus(); |
| 104 | if (cost >= U32_MAX - PAGE_SIZE) |
| 105 | return ERR_PTR(-ENOMEM); |
| 106 | } |
| 107 | cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; |
| 108 | |
| 109 | ret = bpf_map_precharge_memlock(cost); |
| 110 | if (ret < 0) |
| 111 | return ERR_PTR(ret); |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 112 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 113 | /* allocate all map elements and zero-initialize them */ |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 114 | array = bpf_map_area_alloc(array_size); |
| 115 | if (!array) |
| 116 | return ERR_PTR(-ENOMEM); |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 117 | array->index_mask = index_mask; |
| 118 | array->map.unpriv_array = unpriv; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 119 | |
| 120 | /* copy mandatory map attributes */ |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 121 | array->map.map_type = attr->map_type; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 122 | array->map.key_size = attr->key_size; |
| 123 | array->map.value_size = attr->value_size; |
| 124 | array->map.max_entries = attr->max_entries; |
Daniel Borkmann | 816cfeb | 2018-03-08 16:17:32 +0100 | [diff] [blame] | 125 | array->map.map_flags = attr->map_flags; |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 126 | array->map.pages = cost; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 127 | array->elem_size = elem_size; |
| 128 | |
Daniel Borkmann | 422baf6 | 2018-03-08 16:17:33 +0100 | [diff] [blame] | 129 | if (percpu && |
| 130 | (elem_size > PCPU_MIN_UNIT_SIZE || |
| 131 | bpf_array_alloc_percpu(array))) { |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 132 | bpf_map_area_free(array); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 133 | return ERR_PTR(-ENOMEM); |
| 134 | } |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 135 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 136 | return &array->map; |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Called from syscall or from eBPF program */ |
| 140 | static void *array_map_lookup_elem(struct bpf_map *map, void *key) |
| 141 | { |
| 142 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 143 | u32 index = *(u32 *)key; |
| 144 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 145 | if (unlikely(index >= array->map.max_entries)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 146 | return NULL; |
| 147 | |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 148 | return array->value + array->elem_size * (index & array->index_mask); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 151 | /* Called from eBPF program */ |
| 152 | static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key) |
| 153 | { |
| 154 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 155 | u32 index = *(u32 *)key; |
| 156 | |
| 157 | if (unlikely(index >= array->map.max_entries)) |
| 158 | return NULL; |
| 159 | |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 160 | return this_cpu_ptr(array->pptrs[index & array->index_mask]); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 163 | int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value) |
| 164 | { |
| 165 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 166 | u32 index = *(u32 *)key; |
| 167 | void __percpu *pptr; |
| 168 | int cpu, off = 0; |
| 169 | u32 size; |
| 170 | |
| 171 | if (unlikely(index >= array->map.max_entries)) |
| 172 | return -ENOENT; |
| 173 | |
| 174 | /* per_cpu areas are zero-filled and bpf programs can only |
| 175 | * access 'value_size' of them, so copying rounded areas |
| 176 | * will not leak any kernel data |
| 177 | */ |
| 178 | size = round_up(map->value_size, 8); |
| 179 | rcu_read_lock(); |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 180 | pptr = array->pptrs[index & array->index_mask]; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 181 | for_each_possible_cpu(cpu) { |
| 182 | bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size); |
| 183 | off += size; |
| 184 | } |
| 185 | rcu_read_unlock(); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 189 | /* Called from syscall */ |
| 190 | static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 191 | { |
| 192 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 193 | u32 index = *(u32 *)key; |
| 194 | u32 *next = (u32 *)next_key; |
| 195 | |
| 196 | if (index >= array->map.max_entries) { |
| 197 | *next = 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | if (index == array->map.max_entries - 1) |
| 202 | return -ENOENT; |
| 203 | |
| 204 | *next = index + 1; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* Called from syscall or from eBPF program */ |
| 209 | static int array_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 210 | u64 map_flags) |
| 211 | { |
| 212 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 213 | u32 index = *(u32 *)key; |
| 214 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 215 | if (unlikely(map_flags > BPF_EXIST)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 216 | /* unknown flags */ |
| 217 | return -EINVAL; |
| 218 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 219 | if (unlikely(index >= array->map.max_entries)) |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 220 | /* all elements were pre-allocated, cannot insert a new one */ |
| 221 | return -E2BIG; |
| 222 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 223 | if (unlikely(map_flags == BPF_NOEXIST)) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 224 | /* all elements already exist */ |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 225 | return -EEXIST; |
| 226 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 227 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 228 | memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]), |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 229 | value, map->value_size); |
| 230 | else |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 231 | memcpy(array->value + |
| 232 | array->elem_size * (index & array->index_mask), |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 233 | value, map->value_size); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 237 | int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value, |
| 238 | u64 map_flags) |
| 239 | { |
| 240 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 241 | u32 index = *(u32 *)key; |
| 242 | void __percpu *pptr; |
| 243 | int cpu, off = 0; |
| 244 | u32 size; |
| 245 | |
| 246 | if (unlikely(map_flags > BPF_EXIST)) |
| 247 | /* unknown flags */ |
| 248 | return -EINVAL; |
| 249 | |
| 250 | if (unlikely(index >= array->map.max_entries)) |
| 251 | /* all elements were pre-allocated, cannot insert a new one */ |
| 252 | return -E2BIG; |
| 253 | |
| 254 | if (unlikely(map_flags == BPF_NOEXIST)) |
| 255 | /* all elements already exist */ |
| 256 | return -EEXIST; |
| 257 | |
| 258 | /* the user space will provide round_up(value_size, 8) bytes that |
| 259 | * will be copied into per-cpu area. bpf programs can only access |
| 260 | * value_size of it. During lookup the same extra bytes will be |
| 261 | * returned or zeros which were zero-filled by percpu_alloc, |
| 262 | * so no kernel data leaks possible |
| 263 | */ |
| 264 | size = round_up(map->value_size, 8); |
| 265 | rcu_read_lock(); |
Alexei Starovoitov | a9bfac14 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 266 | pptr = array->pptrs[index & array->index_mask]; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 267 | for_each_possible_cpu(cpu) { |
| 268 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size); |
| 269 | off += size; |
| 270 | } |
| 271 | rcu_read_unlock(); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 275 | /* Called from syscall or from eBPF program */ |
| 276 | static int array_map_delete_elem(struct bpf_map *map, void *key) |
| 277 | { |
| 278 | return -EINVAL; |
| 279 | } |
| 280 | |
| 281 | /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ |
| 282 | static void array_map_free(struct bpf_map *map) |
| 283 | { |
| 284 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 285 | |
| 286 | /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, |
| 287 | * so the programs (can be more than one that used this map) were |
| 288 | * disconnected from events. Wait for outstanding programs to complete |
| 289 | * and free the array |
| 290 | */ |
| 291 | synchronize_rcu(); |
| 292 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 293 | if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 294 | bpf_array_free_percpu(array); |
| 295 | |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 296 | bpf_map_area_free(array); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 299 | static const struct bpf_map_ops array_ops = { |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 300 | .map_alloc = array_map_alloc, |
| 301 | .map_free = array_map_free, |
| 302 | .map_get_next_key = array_map_get_next_key, |
| 303 | .map_lookup_elem = array_map_lookup_elem, |
| 304 | .map_update_elem = array_map_update_elem, |
| 305 | .map_delete_elem = array_map_delete_elem, |
| 306 | }; |
| 307 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 308 | static struct bpf_map_type_list array_type __read_mostly = { |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 309 | .ops = &array_ops, |
| 310 | .type = BPF_MAP_TYPE_ARRAY, |
| 311 | }; |
| 312 | |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 313 | static const struct bpf_map_ops percpu_array_ops = { |
| 314 | .map_alloc = array_map_alloc, |
| 315 | .map_free = array_map_free, |
| 316 | .map_get_next_key = array_map_get_next_key, |
| 317 | .map_lookup_elem = percpu_array_map_lookup_elem, |
| 318 | .map_update_elem = array_map_update_elem, |
| 319 | .map_delete_elem = array_map_delete_elem, |
| 320 | }; |
| 321 | |
| 322 | static struct bpf_map_type_list percpu_array_type __read_mostly = { |
| 323 | .ops = &percpu_array_ops, |
| 324 | .type = BPF_MAP_TYPE_PERCPU_ARRAY, |
| 325 | }; |
| 326 | |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 327 | static int __init register_array_map(void) |
| 328 | { |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 329 | bpf_register_map_type(&array_type); |
Alexei Starovoitov | a10423b | 2016-02-01 22:39:54 -0800 | [diff] [blame] | 330 | bpf_register_map_type(&percpu_array_type); |
Alexei Starovoitov | 28fbcfa | 2014-11-13 17:36:46 -0800 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | late_initcall(register_array_map); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 334 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 335 | static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 336 | { |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 337 | /* only file descriptors can be stored in this type of map */ |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 338 | if (attr->value_size != sizeof(u32)) |
| 339 | return ERR_PTR(-EINVAL); |
| 340 | return array_map_alloc(attr); |
| 341 | } |
| 342 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 343 | static void fd_array_map_free(struct bpf_map *map) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 344 | { |
| 345 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 346 | int i; |
| 347 | |
| 348 | synchronize_rcu(); |
| 349 | |
| 350 | /* make sure it's empty */ |
| 351 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 352 | BUG_ON(array->ptrs[i] != NULL); |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 353 | |
| 354 | bpf_map_area_free(array); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 357 | static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 358 | { |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /* only called from syscall */ |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 363 | int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file, |
| 364 | void *key, void *value, u64 map_flags) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 365 | { |
| 366 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 367 | void *new_ptr, *old_ptr; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 368 | u32 index = *(u32 *)key, ufd; |
| 369 | |
| 370 | if (map_flags != BPF_ANY) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | if (index >= array->map.max_entries) |
| 374 | return -E2BIG; |
| 375 | |
| 376 | ufd = *(u32 *)value; |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 377 | new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 378 | if (IS_ERR(new_ptr)) |
| 379 | return PTR_ERR(new_ptr); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 380 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 381 | old_ptr = xchg(array->ptrs + index, new_ptr); |
| 382 | if (old_ptr) |
| 383 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 388 | static int fd_array_map_delete_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 389 | { |
| 390 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 391 | void *old_ptr; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 392 | u32 index = *(u32 *)key; |
| 393 | |
| 394 | if (index >= array->map.max_entries) |
| 395 | return -E2BIG; |
| 396 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 397 | old_ptr = xchg(array->ptrs + index, NULL); |
| 398 | if (old_ptr) { |
| 399 | map->ops->map_fd_put_ptr(old_ptr); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | } else { |
| 402 | return -ENOENT; |
| 403 | } |
| 404 | } |
| 405 | |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 406 | static void *prog_fd_array_get_ptr(struct bpf_map *map, |
| 407 | struct file *map_file, int fd) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 408 | { |
| 409 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 410 | struct bpf_prog *prog = bpf_prog_get(fd); |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 411 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 412 | if (IS_ERR(prog)) |
| 413 | return prog; |
| 414 | |
| 415 | if (!bpf_prog_array_compatible(array, prog)) { |
| 416 | bpf_prog_put(prog); |
| 417 | return ERR_PTR(-EINVAL); |
| 418 | } |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 419 | |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 420 | return prog; |
| 421 | } |
| 422 | |
| 423 | static void prog_fd_array_put_ptr(void *ptr) |
| 424 | { |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 425 | bpf_prog_put(ptr); |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 428 | /* decrement refcnt of all bpf_progs that are stored in this map */ |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 429 | void bpf_fd_array_map_clear(struct bpf_map *map) |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 430 | { |
| 431 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 432 | int i; |
| 433 | |
| 434 | for (i = 0; i < array->map.max_entries; i++) |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 435 | fd_array_map_delete_elem(map, &i); |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static const struct bpf_map_ops prog_array_ops = { |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 439 | .map_alloc = fd_array_map_alloc, |
| 440 | .map_free = fd_array_map_free, |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 441 | .map_get_next_key = array_map_get_next_key, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 442 | .map_lookup_elem = fd_array_map_lookup_elem, |
Wang Nan | 2a36f0b | 2015-08-06 07:02:33 +0000 | [diff] [blame] | 443 | .map_delete_elem = fd_array_map_delete_elem, |
| 444 | .map_fd_get_ptr = prog_fd_array_get_ptr, |
| 445 | .map_fd_put_ptr = prog_fd_array_put_ptr, |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | static struct bpf_map_type_list prog_array_type __read_mostly = { |
| 449 | .ops = &prog_array_ops, |
| 450 | .type = BPF_MAP_TYPE_PROG_ARRAY, |
| 451 | }; |
| 452 | |
| 453 | static int __init register_prog_array_map(void) |
| 454 | { |
| 455 | bpf_register_map_type(&prog_array_type); |
| 456 | return 0; |
| 457 | } |
| 458 | late_initcall(register_prog_array_map); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 459 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 460 | static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file, |
| 461 | struct file *map_file) |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 462 | { |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 463 | struct bpf_event_entry *ee; |
| 464 | |
Daniel Borkmann | 858d68f | 2016-07-16 01:15:55 +0200 | [diff] [blame] | 465 | ee = kzalloc(sizeof(*ee), GFP_ATOMIC); |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 466 | if (ee) { |
| 467 | ee->event = perf_file->private_data; |
| 468 | ee->perf_file = perf_file; |
| 469 | ee->map_file = map_file; |
| 470 | } |
| 471 | |
| 472 | return ee; |
| 473 | } |
| 474 | |
| 475 | static void __bpf_event_entry_free(struct rcu_head *rcu) |
| 476 | { |
| 477 | struct bpf_event_entry *ee; |
| 478 | |
| 479 | ee = container_of(rcu, struct bpf_event_entry, rcu); |
| 480 | fput(ee->perf_file); |
| 481 | kfree(ee); |
| 482 | } |
| 483 | |
| 484 | static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee) |
| 485 | { |
| 486 | call_rcu(&ee->rcu, __bpf_event_entry_free); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 489 | static void *perf_event_fd_array_get_ptr(struct bpf_map *map, |
| 490 | struct file *map_file, int fd) |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 491 | { |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 492 | const struct perf_event_attr *attr; |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 493 | struct bpf_event_entry *ee; |
| 494 | struct perf_event *event; |
| 495 | struct file *perf_file; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 496 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 497 | perf_file = perf_event_get(fd); |
| 498 | if (IS_ERR(perf_file)) |
| 499 | return perf_file; |
Alexei Starovoitov | e03e7ee | 2016-01-25 20:59:49 -0800 | [diff] [blame] | 500 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 501 | event = perf_file->private_data; |
| 502 | ee = ERR_PTR(-EINVAL); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 503 | |
| 504 | attr = perf_event_attrs(event); |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 505 | if (IS_ERR(attr) || attr->inherit) |
| 506 | goto err_out; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 507 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 508 | switch (attr->type) { |
| 509 | case PERF_TYPE_SOFTWARE: |
| 510 | if (attr->config != PERF_COUNT_SW_BPF_OUTPUT) |
| 511 | goto err_out; |
| 512 | /* fall-through */ |
| 513 | case PERF_TYPE_RAW: |
| 514 | case PERF_TYPE_HARDWARE: |
| 515 | ee = bpf_event_entry_gen(perf_file, map_file); |
| 516 | if (ee) |
| 517 | return ee; |
| 518 | ee = ERR_PTR(-ENOMEM); |
| 519 | /* fall-through */ |
| 520 | default: |
| 521 | break; |
| 522 | } |
Alexei Starovoitov | 62544ce | 2015-10-22 17:10:14 -0700 | [diff] [blame] | 523 | |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 524 | err_out: |
| 525 | fput(perf_file); |
| 526 | return ee; |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void perf_event_fd_array_put_ptr(void *ptr) |
| 530 | { |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 531 | bpf_event_entry_free_rcu(ptr); |
| 532 | } |
| 533 | |
| 534 | static void perf_event_fd_array_release(struct bpf_map *map, |
| 535 | struct file *map_file) |
| 536 | { |
| 537 | struct bpf_array *array = container_of(map, struct bpf_array, map); |
| 538 | struct bpf_event_entry *ee; |
| 539 | int i; |
| 540 | |
| 541 | rcu_read_lock(); |
| 542 | for (i = 0; i < array->map.max_entries; i++) { |
| 543 | ee = READ_ONCE(array->ptrs[i]); |
| 544 | if (ee && ee->map_file == map_file) |
| 545 | fd_array_map_delete_elem(map, &i); |
| 546 | } |
| 547 | rcu_read_unlock(); |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static const struct bpf_map_ops perf_event_array_ops = { |
| 551 | .map_alloc = fd_array_map_alloc, |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 552 | .map_free = fd_array_map_free, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 553 | .map_get_next_key = array_map_get_next_key, |
| 554 | .map_lookup_elem = fd_array_map_lookup_elem, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 555 | .map_delete_elem = fd_array_map_delete_elem, |
| 556 | .map_fd_get_ptr = perf_event_fd_array_get_ptr, |
| 557 | .map_fd_put_ptr = perf_event_fd_array_put_ptr, |
Daniel Borkmann | 3b1efb1 | 2016-06-15 22:47:14 +0200 | [diff] [blame] | 558 | .map_release = perf_event_fd_array_release, |
Kaixu Xia | ea317b2 | 2015-08-06 07:02:34 +0000 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | static struct bpf_map_type_list perf_event_array_type __read_mostly = { |
| 562 | .ops = &perf_event_array_ops, |
| 563 | .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, |
| 564 | }; |
| 565 | |
| 566 | static int __init register_perf_event_array_map(void) |
| 567 | { |
| 568 | bpf_register_map_type(&perf_event_array_type); |
| 569 | return 0; |
| 570 | } |
| 571 | late_initcall(register_perf_event_array_map); |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 572 | |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 573 | #ifdef CONFIG_CGROUPS |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 574 | static void *cgroup_fd_array_get_ptr(struct bpf_map *map, |
| 575 | struct file *map_file /* not used */, |
| 576 | int fd) |
| 577 | { |
| 578 | return cgroup_get_from_fd(fd); |
| 579 | } |
| 580 | |
| 581 | static void cgroup_fd_array_put_ptr(void *ptr) |
| 582 | { |
| 583 | /* cgroup_put free cgrp after a rcu grace period */ |
| 584 | cgroup_put(ptr); |
| 585 | } |
| 586 | |
| 587 | static void cgroup_fd_array_free(struct bpf_map *map) |
| 588 | { |
| 589 | bpf_fd_array_map_clear(map); |
| 590 | fd_array_map_free(map); |
| 591 | } |
| 592 | |
| 593 | static const struct bpf_map_ops cgroup_array_ops = { |
| 594 | .map_alloc = fd_array_map_alloc, |
| 595 | .map_free = cgroup_fd_array_free, |
| 596 | .map_get_next_key = array_map_get_next_key, |
| 597 | .map_lookup_elem = fd_array_map_lookup_elem, |
| 598 | .map_delete_elem = fd_array_map_delete_elem, |
| 599 | .map_fd_get_ptr = cgroup_fd_array_get_ptr, |
| 600 | .map_fd_put_ptr = cgroup_fd_array_put_ptr, |
| 601 | }; |
| 602 | |
| 603 | static struct bpf_map_type_list cgroup_array_type __read_mostly = { |
| 604 | .ops = &cgroup_array_ops, |
| 605 | .type = BPF_MAP_TYPE_CGROUP_ARRAY, |
| 606 | }; |
| 607 | |
| 608 | static int __init register_cgroup_array_map(void) |
| 609 | { |
| 610 | bpf_register_map_type(&cgroup_array_type); |
| 611 | return 0; |
| 612 | } |
| 613 | late_initcall(register_cgroup_array_map); |
| 614 | #endif |