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