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