Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -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/jhash.h> |
| 15 | #include <linux/filter.h> |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 16 | #include "percpu_freelist.h" |
Chenbo Feng | 4672ded | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 17 | #define HTAB_CREATE_FLAG_MASK \ |
| 18 | (BPF_F_NO_PREALLOC | BPF_F_RDONLY | BPF_F_WRONLY) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 19 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 20 | struct bucket { |
| 21 | struct hlist_head head; |
| 22 | raw_spinlock_t lock; |
| 23 | }; |
| 24 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 25 | struct bpf_htab { |
| 26 | struct bpf_map map; |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 27 | struct bucket *buckets; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 28 | void *elems; |
| 29 | struct pcpu_freelist freelist; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 30 | void __percpu *extra_elems; |
tom.leiming@gmail.com | 6591f1e | 2015-12-29 22:40:25 +0800 | [diff] [blame] | 31 | atomic_t count; /* number of elements in this hashtable */ |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 32 | u32 n_buckets; /* number of hash buckets */ |
| 33 | u32 elem_size; /* size of each element in bytes */ |
| 34 | }; |
| 35 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 36 | enum extra_elem_state { |
| 37 | HTAB_NOT_AN_EXTRA_ELEM = 0, |
| 38 | HTAB_EXTRA_ELEM_FREE, |
| 39 | HTAB_EXTRA_ELEM_USED |
| 40 | }; |
| 41 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 42 | /* each htab element is struct htab_elem + key + value */ |
| 43 | struct htab_elem { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 44 | union { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 45 | struct hlist_node hash_node; |
| 46 | struct bpf_htab *htab; |
| 47 | struct pcpu_freelist_node fnode; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 48 | }; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 49 | union { |
| 50 | struct rcu_head rcu; |
| 51 | enum extra_elem_state state; |
| 52 | }; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 53 | u32 hash; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 54 | char key[0] __aligned(8); |
| 55 | }; |
| 56 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 57 | static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size, |
| 58 | void __percpu *pptr) |
| 59 | { |
| 60 | *(void __percpu **)(l->key + key_size) = pptr; |
| 61 | } |
| 62 | |
| 63 | static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size) |
| 64 | { |
| 65 | return *(void __percpu **)(l->key + key_size); |
| 66 | } |
| 67 | |
| 68 | static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i) |
| 69 | { |
| 70 | return (struct htab_elem *) (htab->elems + i * htab->elem_size); |
| 71 | } |
| 72 | |
| 73 | static void htab_free_elems(struct bpf_htab *htab) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH) |
| 78 | goto free_elems; |
| 79 | |
| 80 | for (i = 0; i < htab->map.max_entries; i++) { |
| 81 | void __percpu *pptr; |
| 82 | |
| 83 | pptr = htab_elem_get_ptr(get_htab_elem(htab, i), |
| 84 | htab->map.key_size); |
| 85 | free_percpu(pptr); |
| 86 | } |
| 87 | free_elems: |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 88 | bpf_map_area_free(htab->elems); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int prealloc_elems_and_freelist(struct bpf_htab *htab) |
| 92 | { |
| 93 | int err = -ENOMEM, i; |
| 94 | |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 95 | htab->elems = bpf_map_area_alloc(htab->elem_size * |
| 96 | htab->map.max_entries); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 97 | if (!htab->elems) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH) |
| 101 | goto skip_percpu_elems; |
| 102 | |
| 103 | for (i = 0; i < htab->map.max_entries; i++) { |
| 104 | u32 size = round_up(htab->map.value_size, 8); |
| 105 | void __percpu *pptr; |
| 106 | |
| 107 | pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN); |
| 108 | if (!pptr) |
| 109 | goto free_elems; |
| 110 | htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size, |
| 111 | pptr); |
| 112 | } |
| 113 | |
| 114 | skip_percpu_elems: |
| 115 | err = pcpu_freelist_init(&htab->freelist); |
| 116 | if (err) |
| 117 | goto free_elems; |
| 118 | |
| 119 | pcpu_freelist_populate(&htab->freelist, htab->elems, htab->elem_size, |
| 120 | htab->map.max_entries); |
| 121 | return 0; |
| 122 | |
| 123 | free_elems: |
| 124 | htab_free_elems(htab); |
| 125 | return err; |
| 126 | } |
| 127 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 128 | static int alloc_extra_elems(struct bpf_htab *htab) |
| 129 | { |
| 130 | void __percpu *pptr; |
| 131 | int cpu; |
| 132 | |
| 133 | pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN); |
| 134 | if (!pptr) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | for_each_possible_cpu(cpu) { |
| 138 | ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state = |
| 139 | HTAB_EXTRA_ELEM_FREE; |
| 140 | } |
| 141 | htab->extra_elems = pptr; |
| 142 | return 0; |
| 143 | } |
| 144 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 145 | /* Called from syscall */ |
| 146 | static struct bpf_map *htab_map_alloc(union bpf_attr *attr) |
| 147 | { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 148 | bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_HASH; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 149 | struct bpf_htab *htab; |
| 150 | int err, i; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 151 | u64 cost; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 152 | |
Chenbo Feng | 4672ded | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 153 | if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK) |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 154 | /* reserved bits should not be used */ |
| 155 | return ERR_PTR(-EINVAL); |
| 156 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 157 | htab = kzalloc(sizeof(*htab), GFP_USER); |
| 158 | if (!htab) |
| 159 | return ERR_PTR(-ENOMEM); |
| 160 | |
| 161 | /* mandatory map attributes */ |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 162 | htab->map.map_type = attr->map_type; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 163 | htab->map.key_size = attr->key_size; |
| 164 | htab->map.value_size = attr->value_size; |
| 165 | htab->map.max_entries = attr->max_entries; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 166 | htab->map.map_flags = attr->map_flags; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 167 | |
| 168 | /* check sanity of attributes. |
| 169 | * value_size == 0 may be allowed in the future to use map as a set |
| 170 | */ |
| 171 | err = -EINVAL; |
| 172 | if (htab->map.max_entries == 0 || htab->map.key_size == 0 || |
| 173 | htab->map.value_size == 0) |
| 174 | goto free_htab; |
| 175 | |
| 176 | /* hash table size must be power of 2 */ |
| 177 | htab->n_buckets = roundup_pow_of_two(htab->map.max_entries); |
| 178 | |
| 179 | err = -E2BIG; |
| 180 | if (htab->map.key_size > MAX_BPF_STACK) |
| 181 | /* eBPF programs initialize keys on stack, so they cannot be |
| 182 | * larger than max stack size |
| 183 | */ |
| 184 | goto free_htab; |
| 185 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 186 | if (htab->map.value_size >= (1 << (KMALLOC_SHIFT_MAX - 1)) - |
| 187 | MAX_BPF_STACK - sizeof(struct htab_elem)) |
| 188 | /* if value_size is bigger, the user space won't be able to |
| 189 | * access the elements via bpf syscall. This check also makes |
| 190 | * sure that the elem_size doesn't overflow and it's |
| 191 | * kmalloc-able later in htab_map_update_elem() |
| 192 | */ |
| 193 | goto free_htab; |
| 194 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 195 | if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE) |
| 196 | /* make sure the size for pcpu_alloc() is reasonable */ |
| 197 | goto free_htab; |
| 198 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 199 | htab->elem_size = sizeof(struct htab_elem) + |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 200 | round_up(htab->map.key_size, 8); |
| 201 | if (percpu) |
| 202 | htab->elem_size += sizeof(void *); |
| 203 | else |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 204 | htab->elem_size += round_up(htab->map.value_size, 8); |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 205 | |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 206 | /* prevent zero size kmalloc and check for u32 overflow */ |
| 207 | if (htab->n_buckets == 0 || |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 208 | htab->n_buckets > U32_MAX / sizeof(struct bucket)) |
Alexei Starovoitov | daaf427 | 2014-11-18 17:32:16 -0800 | [diff] [blame] | 209 | goto free_htab; |
| 210 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 211 | cost = (u64) htab->n_buckets * sizeof(struct bucket) + |
| 212 | (u64) htab->elem_size * htab->map.max_entries; |
| 213 | |
| 214 | if (percpu) |
| 215 | cost += (u64) round_up(htab->map.value_size, 8) * |
| 216 | num_possible_cpus() * htab->map.max_entries; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 217 | else |
| 218 | cost += (u64) htab->elem_size * num_possible_cpus(); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 219 | |
| 220 | if (cost >= U32_MAX - PAGE_SIZE) |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 221 | /* make sure page count doesn't overflow */ |
| 222 | goto free_htab; |
| 223 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 224 | htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 225 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 226 | /* if map size is larger than memlock limit, reject it early */ |
| 227 | err = bpf_map_precharge_memlock(htab->map.pages); |
| 228 | if (err) |
| 229 | goto free_htab; |
| 230 | |
Alexei Starovoitov | 01b3f52 | 2015-11-29 16:59:35 -0800 | [diff] [blame] | 231 | err = -ENOMEM; |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 232 | htab->buckets = bpf_map_area_alloc(htab->n_buckets * |
| 233 | sizeof(struct bucket)); |
| 234 | if (!htab->buckets) |
| 235 | goto free_htab; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 236 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 237 | for (i = 0; i < htab->n_buckets; i++) { |
| 238 | INIT_HLIST_HEAD(&htab->buckets[i].head); |
| 239 | raw_spin_lock_init(&htab->buckets[i].lock); |
| 240 | } |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 241 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 242 | if (!percpu) { |
| 243 | err = alloc_extra_elems(htab); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 244 | if (err) |
| 245 | goto free_buckets; |
| 246 | } |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 247 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 248 | if (!(attr->map_flags & BPF_F_NO_PREALLOC)) { |
| 249 | err = prealloc_elems_and_freelist(htab); |
| 250 | if (err) |
| 251 | goto free_extra_elems; |
| 252 | } |
| 253 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 254 | return &htab->map; |
| 255 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 256 | free_extra_elems: |
| 257 | free_percpu(htab->extra_elems); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 258 | free_buckets: |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 259 | bpf_map_area_free(htab->buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 260 | free_htab: |
| 261 | kfree(htab); |
| 262 | return ERR_PTR(err); |
| 263 | } |
| 264 | |
| 265 | static inline u32 htab_map_hash(const void *key, u32 key_len) |
| 266 | { |
| 267 | return jhash(key, key_len, 0); |
| 268 | } |
| 269 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 270 | static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 271 | { |
| 272 | return &htab->buckets[hash & (htab->n_buckets - 1)]; |
| 273 | } |
| 274 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 275 | static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash) |
| 276 | { |
| 277 | return &__select_bucket(htab, hash)->head; |
| 278 | } |
| 279 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 280 | static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash, |
| 281 | void *key, u32 key_size) |
| 282 | { |
| 283 | struct htab_elem *l; |
| 284 | |
| 285 | hlist_for_each_entry_rcu(l, head, hash_node) |
| 286 | if (l->hash == hash && !memcmp(&l->key, key, key_size)) |
| 287 | return l; |
| 288 | |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | /* Called from syscall or from eBPF program */ |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 293 | static void *__htab_map_lookup_elem(struct bpf_map *map, void *key) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 294 | { |
| 295 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 296 | struct hlist_head *head; |
| 297 | struct htab_elem *l; |
| 298 | u32 hash, key_size; |
| 299 | |
| 300 | /* Must be called with rcu_read_lock. */ |
| 301 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 302 | |
| 303 | key_size = map->key_size; |
| 304 | |
| 305 | hash = htab_map_hash(key, key_size); |
| 306 | |
| 307 | head = select_bucket(htab, hash); |
| 308 | |
| 309 | l = lookup_elem_raw(head, hash, key, key_size); |
| 310 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 311 | return l; |
| 312 | } |
| 313 | |
| 314 | static void *htab_map_lookup_elem(struct bpf_map *map, void *key) |
| 315 | { |
| 316 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 317 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 318 | if (l) |
| 319 | return l->key + round_up(map->key_size, 8); |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | /* Called from syscall */ |
| 325 | static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 326 | { |
| 327 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 328 | struct hlist_head *head; |
| 329 | struct htab_elem *l, *next_l; |
| 330 | u32 hash, key_size; |
Teng Qin | fcbc8d0 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 331 | int i = 0; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 332 | |
| 333 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 334 | |
| 335 | key_size = map->key_size; |
| 336 | |
Teng Qin | fcbc8d0 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 337 | if (!key) |
| 338 | goto find_first_elem; |
| 339 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 340 | hash = htab_map_hash(key, key_size); |
| 341 | |
| 342 | head = select_bucket(htab, hash); |
| 343 | |
| 344 | /* lookup the key */ |
| 345 | l = lookup_elem_raw(head, hash, key, key_size); |
| 346 | |
Teng Qin | fcbc8d0 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 347 | if (!l) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 348 | goto find_first_elem; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 349 | |
| 350 | /* key was found, get next key in the same bucket */ |
| 351 | next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)), |
| 352 | struct htab_elem, hash_node); |
| 353 | |
| 354 | if (next_l) { |
| 355 | /* if next elem in this hash list is non-zero, just return it */ |
| 356 | memcpy(next_key, next_l->key, key_size); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /* no more elements in this hash list, go to the next bucket */ |
| 361 | i = hash & (htab->n_buckets - 1); |
| 362 | i++; |
| 363 | |
| 364 | find_first_elem: |
| 365 | /* iterate over buckets */ |
| 366 | for (; i < htab->n_buckets; i++) { |
| 367 | head = select_bucket(htab, i); |
| 368 | |
| 369 | /* pick first element in the bucket */ |
| 370 | next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)), |
| 371 | struct htab_elem, hash_node); |
| 372 | if (next_l) { |
| 373 | /* if it's not empty, just return it */ |
| 374 | memcpy(next_key, next_l->key, key_size); |
| 375 | return 0; |
| 376 | } |
| 377 | } |
| 378 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 379 | /* iterated over all buckets and all elements */ |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 380 | return -ENOENT; |
| 381 | } |
| 382 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 383 | static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 384 | { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 385 | if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) |
| 386 | free_percpu(htab_elem_get_ptr(l, htab->map.key_size)); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 387 | kfree(l); |
| 388 | } |
| 389 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 390 | static void htab_elem_free_rcu(struct rcu_head *head) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 391 | { |
| 392 | struct htab_elem *l = container_of(head, struct htab_elem, rcu); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 393 | struct bpf_htab *htab = l->htab; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 394 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 395 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering while |
| 396 | * we're calling kfree, otherwise deadlock is possible if kprobes |
| 397 | * are placed somewhere inside of slub |
| 398 | */ |
| 399 | preempt_disable(); |
| 400 | __this_cpu_inc(bpf_prog_active); |
| 401 | htab_elem_free(htab, l); |
| 402 | __this_cpu_dec(bpf_prog_active); |
| 403 | preempt_enable(); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 406 | static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 407 | { |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 408 | if (l->state == HTAB_EXTRA_ELEM_USED) { |
| 409 | l->state = HTAB_EXTRA_ELEM_FREE; |
| 410 | return; |
| 411 | } |
| 412 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 413 | if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) { |
| 414 | pcpu_freelist_push(&htab->freelist, &l->fnode); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 415 | } else { |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 416 | atomic_dec(&htab->count); |
| 417 | l->htab = htab; |
| 418 | call_rcu(&l->rcu, htab_elem_free_rcu); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, |
| 423 | void *value, u32 key_size, u32 hash, |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 424 | bool percpu, bool onallcpus, |
| 425 | bool old_elem_exists) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 426 | { |
| 427 | u32 size = htab->map.value_size; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 428 | bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 429 | struct htab_elem *l_new; |
| 430 | void __percpu *pptr; |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 431 | int err = 0; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 432 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 433 | if (prealloc) { |
| 434 | l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist); |
| 435 | if (!l_new) |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 436 | err = -E2BIG; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 437 | } else { |
| 438 | if (atomic_inc_return(&htab->count) > htab->map.max_entries) { |
| 439 | atomic_dec(&htab->count); |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 440 | err = -E2BIG; |
| 441 | } else { |
| 442 | l_new = kmalloc(htab->elem_size, |
| 443 | GFP_ATOMIC | __GFP_NOWARN); |
| 444 | if (!l_new) |
| 445 | return ERR_PTR(-ENOMEM); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 446 | } |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (err) { |
| 450 | if (!old_elem_exists) |
| 451 | return ERR_PTR(err); |
| 452 | |
| 453 | /* if we're updating the existing element and the hash table |
| 454 | * is full, use per-cpu extra elems |
| 455 | */ |
| 456 | l_new = this_cpu_ptr(htab->extra_elems); |
| 457 | if (l_new->state != HTAB_EXTRA_ELEM_FREE) |
| 458 | return ERR_PTR(-E2BIG); |
| 459 | l_new->state = HTAB_EXTRA_ELEM_USED; |
| 460 | } else { |
| 461 | l_new->state = HTAB_NOT_AN_EXTRA_ELEM; |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 462 | } |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 463 | |
| 464 | memcpy(l_new->key, key, key_size); |
| 465 | if (percpu) { |
| 466 | /* round up value_size to 8 bytes */ |
| 467 | size = round_up(size, 8); |
| 468 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 469 | if (prealloc) { |
| 470 | pptr = htab_elem_get_ptr(l_new, key_size); |
| 471 | } else { |
| 472 | /* alloc_percpu zero-fills */ |
| 473 | pptr = __alloc_percpu_gfp(size, 8, |
| 474 | GFP_ATOMIC | __GFP_NOWARN); |
| 475 | if (!pptr) { |
| 476 | kfree(l_new); |
| 477 | return ERR_PTR(-ENOMEM); |
| 478 | } |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 481 | if (!onallcpus) { |
| 482 | /* copy true value_size bytes */ |
| 483 | memcpy(this_cpu_ptr(pptr), value, htab->map.value_size); |
| 484 | } else { |
| 485 | int off = 0, cpu; |
| 486 | |
| 487 | for_each_possible_cpu(cpu) { |
| 488 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), |
| 489 | value + off, size); |
| 490 | off += size; |
| 491 | } |
| 492 | } |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 493 | if (!prealloc) |
| 494 | htab_elem_set_ptr(l_new, key_size, pptr); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 495 | } else { |
| 496 | memcpy(l_new->key + round_up(key_size, 8), value, size); |
| 497 | } |
| 498 | |
| 499 | l_new->hash = hash; |
| 500 | return l_new; |
| 501 | } |
| 502 | |
| 503 | static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old, |
| 504 | u64 map_flags) |
| 505 | { |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 506 | if (l_old && map_flags == BPF_NOEXIST) |
| 507 | /* elem already exists */ |
| 508 | return -EEXIST; |
| 509 | |
| 510 | if (!l_old && map_flags == BPF_EXIST) |
| 511 | /* elem doesn't exist, cannot update it */ |
| 512 | return -ENOENT; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 517 | /* Called from syscall or from eBPF program */ |
| 518 | static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 519 | u64 map_flags) |
| 520 | { |
| 521 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 522 | struct htab_elem *l_new = NULL, *l_old; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 523 | struct hlist_head *head; |
| 524 | unsigned long flags; |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 525 | struct bucket *b; |
| 526 | u32 key_size, hash; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 527 | int ret; |
| 528 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 529 | if (unlikely(map_flags > BPF_EXIST)) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 530 | /* unknown flags */ |
| 531 | return -EINVAL; |
| 532 | |
| 533 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 534 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 535 | key_size = map->key_size; |
| 536 | |
| 537 | hash = htab_map_hash(key, key_size); |
| 538 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 539 | b = __select_bucket(htab, hash); |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 540 | head = &b->head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 541 | |
| 542 | /* bpf_map_update_elem() can be called in_irq() */ |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 543 | raw_spin_lock_irqsave(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 544 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 545 | l_old = lookup_elem_raw(head, hash, key, key_size); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 546 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 547 | ret = check_flags(htab, l_old, map_flags); |
| 548 | if (ret) |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 549 | goto err; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 550 | |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 551 | l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false, |
| 552 | !!l_old); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 553 | if (IS_ERR(l_new)) { |
| 554 | /* all pre-allocated elements are in use or memory exhausted */ |
| 555 | ret = PTR_ERR(l_new); |
| 556 | goto err; |
| 557 | } |
| 558 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 559 | /* add new element to the head of the list, so that |
| 560 | * concurrent search will find it before old elem |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 561 | */ |
| 562 | hlist_add_head_rcu(&l_new->hash_node, head); |
| 563 | if (l_old) { |
| 564 | hlist_del_rcu(&l_old->hash_node); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 565 | free_htab_elem(htab, l_old); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 566 | } |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 567 | ret = 0; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 568 | err: |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 569 | raw_spin_unlock_irqrestore(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 570 | return ret; |
| 571 | } |
| 572 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 573 | static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 574 | void *value, u64 map_flags, |
| 575 | bool onallcpus) |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 576 | { |
| 577 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 578 | struct htab_elem *l_new = NULL, *l_old; |
| 579 | struct hlist_head *head; |
| 580 | unsigned long flags; |
| 581 | struct bucket *b; |
| 582 | u32 key_size, hash; |
| 583 | int ret; |
| 584 | |
| 585 | if (unlikely(map_flags > BPF_EXIST)) |
| 586 | /* unknown flags */ |
| 587 | return -EINVAL; |
| 588 | |
| 589 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 590 | |
| 591 | key_size = map->key_size; |
| 592 | |
| 593 | hash = htab_map_hash(key, key_size); |
| 594 | |
| 595 | b = __select_bucket(htab, hash); |
| 596 | head = &b->head; |
| 597 | |
| 598 | /* bpf_map_update_elem() can be called in_irq() */ |
| 599 | raw_spin_lock_irqsave(&b->lock, flags); |
| 600 | |
| 601 | l_old = lookup_elem_raw(head, hash, key, key_size); |
| 602 | |
| 603 | ret = check_flags(htab, l_old, map_flags); |
| 604 | if (ret) |
| 605 | goto err; |
| 606 | |
| 607 | if (l_old) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 608 | void __percpu *pptr = htab_elem_get_ptr(l_old, key_size); |
| 609 | u32 size = htab->map.value_size; |
| 610 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 611 | /* per-cpu hash map can update value in-place */ |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 612 | if (!onallcpus) { |
| 613 | memcpy(this_cpu_ptr(pptr), value, size); |
| 614 | } else { |
| 615 | int off = 0, cpu; |
| 616 | |
| 617 | size = round_up(size, 8); |
| 618 | for_each_possible_cpu(cpu) { |
| 619 | bpf_long_memcpy(per_cpu_ptr(pptr, cpu), |
| 620 | value + off, size); |
| 621 | off += size; |
| 622 | } |
| 623 | } |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 624 | } else { |
| 625 | l_new = alloc_htab_elem(htab, key, value, key_size, |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 626 | hash, true, onallcpus, false); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 627 | if (IS_ERR(l_new)) { |
| 628 | ret = PTR_ERR(l_new); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 629 | goto err; |
| 630 | } |
| 631 | hlist_add_head_rcu(&l_new->hash_node, head); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 632 | } |
| 633 | ret = 0; |
| 634 | err: |
| 635 | raw_spin_unlock_irqrestore(&b->lock, flags); |
| 636 | return ret; |
| 637 | } |
| 638 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 639 | static int htab_percpu_map_update_elem(struct bpf_map *map, void *key, |
| 640 | void *value, u64 map_flags) |
| 641 | { |
| 642 | return __htab_percpu_map_update_elem(map, key, value, map_flags, false); |
| 643 | } |
| 644 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 645 | /* Called from syscall or from eBPF program */ |
| 646 | static int htab_map_delete_elem(struct bpf_map *map, void *key) |
| 647 | { |
| 648 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 649 | struct hlist_head *head; |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 650 | struct bucket *b; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 651 | struct htab_elem *l; |
| 652 | unsigned long flags; |
| 653 | u32 hash, key_size; |
| 654 | int ret = -ENOENT; |
| 655 | |
| 656 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 657 | |
| 658 | key_size = map->key_size; |
| 659 | |
| 660 | hash = htab_map_hash(key, key_size); |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 661 | b = __select_bucket(htab, hash); |
| 662 | head = &b->head; |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 663 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 664 | raw_spin_lock_irqsave(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 665 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 666 | l = lookup_elem_raw(head, hash, key, key_size); |
| 667 | |
| 668 | if (l) { |
| 669 | hlist_del_rcu(&l->hash_node); |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 670 | free_htab_elem(htab, l); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 671 | ret = 0; |
| 672 | } |
| 673 | |
tom.leiming@gmail.com | 688ecfe | 2015-12-29 22:40:27 +0800 | [diff] [blame] | 674 | raw_spin_unlock_irqrestore(&b->lock, flags); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static void delete_all_elements(struct bpf_htab *htab) |
| 679 | { |
| 680 | int i; |
| 681 | |
| 682 | for (i = 0; i < htab->n_buckets; i++) { |
| 683 | struct hlist_head *head = select_bucket(htab, i); |
| 684 | struct hlist_node *n; |
| 685 | struct htab_elem *l; |
| 686 | |
| 687 | hlist_for_each_entry_safe(l, n, head, hash_node) { |
| 688 | hlist_del_rcu(&l->hash_node); |
Daniel Borkmann | 483bed2 | 2016-11-04 00:01:19 +0100 | [diff] [blame] | 689 | if (l->state != HTAB_EXTRA_ELEM_USED) |
| 690 | htab_elem_free(htab, l); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 694 | /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ |
| 695 | static void htab_map_free(struct bpf_map *map) |
| 696 | { |
| 697 | struct bpf_htab *htab = container_of(map, struct bpf_htab, map); |
| 698 | |
| 699 | /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, |
| 700 | * so the programs (can be more than one that used this map) were |
| 701 | * disconnected from events. Wait for outstanding critical sections in |
| 702 | * these programs to complete |
| 703 | */ |
| 704 | synchronize_rcu(); |
| 705 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 706 | /* some of free_htab_elem() callbacks for elements of this map may |
| 707 | * not have executed. Wait for them. |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 708 | */ |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 709 | rcu_barrier(); |
| 710 | if (htab->map.map_flags & BPF_F_NO_PREALLOC) { |
| 711 | delete_all_elements(htab); |
| 712 | } else { |
| 713 | htab_free_elems(htab); |
| 714 | pcpu_freelist_destroy(&htab->freelist); |
| 715 | } |
Alexei Starovoitov | a6ed3ea | 2016-08-05 14:01:27 -0700 | [diff] [blame] | 716 | free_percpu(htab->extra_elems); |
Daniel Borkmann | 251d00b | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 717 | bpf_map_area_free(htab->buckets); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 718 | kfree(htab); |
| 719 | } |
| 720 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 721 | static const struct bpf_map_ops htab_ops = { |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 722 | .map_alloc = htab_map_alloc, |
| 723 | .map_free = htab_map_free, |
| 724 | .map_get_next_key = htab_map_get_next_key, |
| 725 | .map_lookup_elem = htab_map_lookup_elem, |
| 726 | .map_update_elem = htab_map_update_elem, |
| 727 | .map_delete_elem = htab_map_delete_elem, |
| 728 | }; |
| 729 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 730 | static struct bpf_map_type_list htab_type __read_mostly = { |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 731 | .ops = &htab_ops, |
| 732 | .type = BPF_MAP_TYPE_HASH, |
| 733 | }; |
| 734 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 735 | /* Called from eBPF program */ |
| 736 | static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key) |
| 737 | { |
| 738 | struct htab_elem *l = __htab_map_lookup_elem(map, key); |
| 739 | |
| 740 | if (l) |
| 741 | return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); |
| 742 | else |
| 743 | return NULL; |
| 744 | } |
| 745 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 746 | int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value) |
| 747 | { |
| 748 | struct htab_elem *l; |
| 749 | void __percpu *pptr; |
| 750 | int ret = -ENOENT; |
| 751 | int cpu, off = 0; |
| 752 | u32 size; |
| 753 | |
| 754 | /* per_cpu areas are zero-filled and bpf programs can only |
| 755 | * access 'value_size' of them, so copying rounded areas |
| 756 | * will not leak any kernel data |
| 757 | */ |
| 758 | size = round_up(map->value_size, 8); |
| 759 | rcu_read_lock(); |
| 760 | l = __htab_map_lookup_elem(map, key); |
| 761 | if (!l) |
| 762 | goto out; |
| 763 | pptr = htab_elem_get_ptr(l, map->key_size); |
| 764 | for_each_possible_cpu(cpu) { |
| 765 | bpf_long_memcpy(value + off, |
| 766 | per_cpu_ptr(pptr, cpu), size); |
| 767 | off += size; |
| 768 | } |
| 769 | ret = 0; |
| 770 | out: |
| 771 | rcu_read_unlock(); |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, |
| 776 | u64 map_flags) |
| 777 | { |
Sasha Levin | 6bbd9a0 | 2016-02-19 13:53:10 -0500 | [diff] [blame] | 778 | int ret; |
| 779 | |
| 780 | rcu_read_lock(); |
| 781 | ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true); |
| 782 | rcu_read_unlock(); |
| 783 | |
| 784 | return ret; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 787 | static const struct bpf_map_ops htab_percpu_ops = { |
| 788 | .map_alloc = htab_map_alloc, |
| 789 | .map_free = htab_map_free, |
| 790 | .map_get_next_key = htab_map_get_next_key, |
| 791 | .map_lookup_elem = htab_percpu_map_lookup_elem, |
| 792 | .map_update_elem = htab_percpu_map_update_elem, |
| 793 | .map_delete_elem = htab_map_delete_elem, |
| 794 | }; |
| 795 | |
| 796 | static struct bpf_map_type_list htab_percpu_type __read_mostly = { |
| 797 | .ops = &htab_percpu_ops, |
| 798 | .type = BPF_MAP_TYPE_PERCPU_HASH, |
| 799 | }; |
| 800 | |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 801 | static int __init register_htab_map(void) |
| 802 | { |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 803 | bpf_register_map_type(&htab_type); |
Alexei Starovoitov | 824bd0c | 2016-02-01 22:39:53 -0800 | [diff] [blame] | 804 | bpf_register_map_type(&htab_percpu_type); |
Alexei Starovoitov | 0f8e4bd | 2014-11-13 17:36:45 -0800 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | late_initcall(register_htab_map); |