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