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