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