blob: 343fb5394c95b21fdaa4103ae0287458d15938df [file] [log] [blame]
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov6c905982016-03-07 21:57:15 -08002 * Copyright (c) 2016 Facebook
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08003 *
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 Starovoitov4fe84352017-03-07 20:00:13 -080016#include <linux/rculist_nulls.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080017#include "percpu_freelist.h"
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080018#include "bpf_lru_list.h"
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070019#include "map_in_map.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080020
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080021struct bucket {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080022 struct hlist_nulls_head head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080023 raw_spinlock_t lock;
24};
25
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080026struct bpf_htab {
27 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080028 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080029 void *elems;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080030 union {
31 struct pcpu_freelist freelist;
32 struct bpf_lru lru;
33 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070034 void __percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080035 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080036 u32 n_buckets; /* number of hash buckets */
37 u32 elem_size; /* size of each element in bytes */
38};
39
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070040enum extra_elem_state {
41 HTAB_NOT_AN_EXTRA_ELEM = 0,
42 HTAB_EXTRA_ELEM_FREE,
43 HTAB_EXTRA_ELEM_USED
44};
45
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080046/* each htab element is struct htab_elem + key + value */
47struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080048 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080049 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080050 struct {
51 void *padding;
52 union {
53 struct bpf_htab *htab;
54 struct pcpu_freelist_node fnode;
55 };
56 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080057 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070058 union {
59 struct rcu_head rcu;
60 enum extra_elem_state state;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080061 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070062 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080063 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080064 char key[0] __aligned(8);
65};
66
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080067static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
68
69static bool htab_is_lru(const struct bpf_htab *htab)
70{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080071 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
72 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
73}
74
75static bool htab_is_percpu(const struct bpf_htab *htab)
76{
77 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
78 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080079}
80
Alexei Starovoitov6c905982016-03-07 21:57:15 -080081static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
82 void __percpu *pptr)
83{
84 *(void __percpu **)(l->key + key_size) = pptr;
85}
86
87static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
88{
89 return *(void __percpu **)(l->key + key_size);
90}
91
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070092static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
93{
94 return *(void **)(l->key + roundup(map->key_size, 8));
95}
96
Alexei Starovoitov6c905982016-03-07 21:57:15 -080097static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
98{
99 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
100}
101
102static void htab_free_elems(struct bpf_htab *htab)
103{
104 int i;
105
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800106 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800107 goto free_elems;
108
109 for (i = 0; i < htab->map.max_entries; i++) {
110 void __percpu *pptr;
111
112 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
113 htab->map.key_size);
114 free_percpu(pptr);
115 }
116free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100117 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800118}
119
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800120static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
121 u32 hash)
122{
123 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
124 struct htab_elem *l;
125
126 if (node) {
127 l = container_of(node, struct htab_elem, lru_node);
128 memcpy(l->key, key, htab->map.key_size);
129 return l;
130 }
131
132 return NULL;
133}
134
135static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800136{
137 int err = -ENOMEM, i;
138
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100139 htab->elems = bpf_map_area_alloc(htab->elem_size *
140 htab->map.max_entries);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800141 if (!htab->elems)
142 return -ENOMEM;
143
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800144 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800145 goto skip_percpu_elems;
146
147 for (i = 0; i < htab->map.max_entries; i++) {
148 u32 size = round_up(htab->map.value_size, 8);
149 void __percpu *pptr;
150
151 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
152 if (!pptr)
153 goto free_elems;
154 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
155 pptr);
156 }
157
158skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800159 if (htab_is_lru(htab))
160 err = bpf_lru_init(&htab->lru,
161 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
162 offsetof(struct htab_elem, hash) -
163 offsetof(struct htab_elem, lru_node),
164 htab_lru_map_delete_node,
165 htab);
166 else
167 err = pcpu_freelist_init(&htab->freelist);
168
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800169 if (err)
170 goto free_elems;
171
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800172 if (htab_is_lru(htab))
173 bpf_lru_populate(&htab->lru, htab->elems,
174 offsetof(struct htab_elem, lru_node),
175 htab->elem_size, htab->map.max_entries);
176 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800177 pcpu_freelist_populate(&htab->freelist,
178 htab->elems + offsetof(struct htab_elem, fnode),
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800179 htab->elem_size, htab->map.max_entries);
180
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800181 return 0;
182
183free_elems:
184 htab_free_elems(htab);
185 return err;
186}
187
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800188static void prealloc_destroy(struct bpf_htab *htab)
189{
190 htab_free_elems(htab);
191
192 if (htab_is_lru(htab))
193 bpf_lru_destroy(&htab->lru);
194 else
195 pcpu_freelist_destroy(&htab->freelist);
196}
197
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700198static int alloc_extra_elems(struct bpf_htab *htab)
199{
200 void __percpu *pptr;
201 int cpu;
202
203 pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
204 if (!pptr)
205 return -ENOMEM;
206
207 for_each_possible_cpu(cpu) {
208 ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
209 HTAB_EXTRA_ELEM_FREE;
210 }
211 htab->extra_elems = pptr;
212 return 0;
213}
214
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800215/* Called from syscall */
216static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
217{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800218 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
219 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
220 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
221 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800222 /* percpu_lru means each cpu has its own LRU list.
223 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
224 * the map's value itself is percpu. percpu_lru has
225 * nothing to do with the map's value.
226 */
227 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
228 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800229 struct bpf_htab *htab;
230 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800231 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800232
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800233 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
234 offsetof(struct htab_elem, hash_node.pprev));
235 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
236 offsetof(struct htab_elem, hash_node.pprev));
237
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800238 if (lru && !capable(CAP_SYS_ADMIN))
239 /* LRU implementation is much complicated than other
240 * maps. Hence, limit to CAP_SYS_ADMIN for now.
241 */
242 return ERR_PTR(-EPERM);
243
244 if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800245 /* reserved bits should not be used */
246 return ERR_PTR(-EINVAL);
247
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800248 if (!lru && percpu_lru)
249 return ERR_PTR(-EINVAL);
250
251 if (lru && !prealloc)
252 return ERR_PTR(-ENOTSUPP);
253
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800254 htab = kzalloc(sizeof(*htab), GFP_USER);
255 if (!htab)
256 return ERR_PTR(-ENOMEM);
257
258 /* mandatory map attributes */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800259 htab->map.map_type = attr->map_type;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800260 htab->map.key_size = attr->key_size;
261 htab->map.value_size = attr->value_size;
262 htab->map.max_entries = attr->max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800263 htab->map.map_flags = attr->map_flags;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800264
265 /* check sanity of attributes.
266 * value_size == 0 may be allowed in the future to use map as a set
267 */
268 err = -EINVAL;
269 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
270 htab->map.value_size == 0)
271 goto free_htab;
272
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800273 if (percpu_lru) {
274 /* ensure each CPU's lru list has >=1 elements.
275 * since we are at it, make each lru list has the same
276 * number of elements.
277 */
278 htab->map.max_entries = roundup(attr->max_entries,
279 num_possible_cpus());
280 if (htab->map.max_entries < attr->max_entries)
281 htab->map.max_entries = rounddown(attr->max_entries,
282 num_possible_cpus());
283 }
284
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800285 /* hash table size must be power of 2 */
286 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
287
288 err = -E2BIG;
289 if (htab->map.key_size > MAX_BPF_STACK)
290 /* eBPF programs initialize keys on stack, so they cannot be
291 * larger than max stack size
292 */
293 goto free_htab;
294
Michal Hocko7984c272017-01-10 16:57:30 -0800295 if (htab->map.value_size >= KMALLOC_MAX_SIZE -
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800296 MAX_BPF_STACK - sizeof(struct htab_elem))
297 /* if value_size is bigger, the user space won't be able to
298 * access the elements via bpf syscall. This check also makes
299 * sure that the elem_size doesn't overflow and it's
300 * kmalloc-able later in htab_map_update_elem()
301 */
302 goto free_htab;
303
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800304 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
305 /* make sure the size for pcpu_alloc() is reasonable */
306 goto free_htab;
307
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800308 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800309 round_up(htab->map.key_size, 8);
310 if (percpu)
311 htab->elem_size += sizeof(void *);
312 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800313 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800314
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800315 /* prevent zero size kmalloc and check for u32 overflow */
316 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800317 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800318 goto free_htab;
319
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800320 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
321 (u64) htab->elem_size * htab->map.max_entries;
322
323 if (percpu)
324 cost += (u64) round_up(htab->map.value_size, 8) *
325 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700326 else
327 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800328
329 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800330 /* make sure page count doesn't overflow */
331 goto free_htab;
332
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800333 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800334
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800335 /* if map size is larger than memlock limit, reject it early */
336 err = bpf_map_precharge_memlock(htab->map.pages);
337 if (err)
338 goto free_htab;
339
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800340 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100341 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
342 sizeof(struct bucket));
343 if (!htab->buckets)
344 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800345
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800346 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800347 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800348 raw_spin_lock_init(&htab->buckets[i].lock);
349 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800350
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800351 if (!percpu && !lru) {
352 /* lru itself can remove the least used element, so
353 * there is no need for an extra elem during map_update.
354 */
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700355 err = alloc_extra_elems(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800356 if (err)
357 goto free_buckets;
358 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800359
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800360 if (prealloc) {
361 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700362 if (err)
363 goto free_extra_elems;
364 }
365
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800366 return &htab->map;
367
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700368free_extra_elems:
369 free_percpu(htab->extra_elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800370free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100371 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800372free_htab:
373 kfree(htab);
374 return ERR_PTR(err);
375}
376
377static inline u32 htab_map_hash(const void *key, u32 key_len)
378{
379 return jhash(key, key_len, 0);
380}
381
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800382static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800383{
384 return &htab->buckets[hash & (htab->n_buckets - 1)];
385}
386
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800387static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800388{
389 return &__select_bucket(htab, hash)->head;
390}
391
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800392/* this lookup function can only be called with bucket lock taken */
393static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800394 void *key, u32 key_size)
395{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800396 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800397 struct htab_elem *l;
398
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800399 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800400 if (l->hash == hash && !memcmp(&l->key, key, key_size))
401 return l;
402
403 return NULL;
404}
405
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800406/* can be called without bucket lock. it will repeat the loop in
407 * the unlikely event when elements moved from one bucket into another
408 * while link list is being walked
409 */
410static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
411 u32 hash, void *key,
412 u32 key_size, u32 n_buckets)
413{
414 struct hlist_nulls_node *n;
415 struct htab_elem *l;
416
417again:
418 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
419 if (l->hash == hash && !memcmp(&l->key, key, key_size))
420 return l;
421
422 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
423 goto again;
424
425 return NULL;
426}
427
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700428/* Called from syscall or from eBPF program directly, so
429 * arguments have to match bpf_map_lookup_elem() exactly.
430 * The return value is adjusted by BPF instructions
431 * in htab_map_gen_lookup().
432 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800433static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800434{
435 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800436 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800437 struct htab_elem *l;
438 u32 hash, key_size;
439
440 /* Must be called with rcu_read_lock. */
441 WARN_ON_ONCE(!rcu_read_lock_held());
442
443 key_size = map->key_size;
444
445 hash = htab_map_hash(key, key_size);
446
447 head = select_bucket(htab, hash);
448
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800449 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800450
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800451 return l;
452}
453
454static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
455{
456 struct htab_elem *l = __htab_map_lookup_elem(map, key);
457
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800458 if (l)
459 return l->key + round_up(map->key_size, 8);
460
461 return NULL;
462}
463
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700464/* inline bpf_map_lookup_elem() call.
465 * Instead of:
466 * bpf_prog
467 * bpf_map_lookup_elem
468 * map->ops->map_lookup_elem
469 * htab_map_lookup_elem
470 * __htab_map_lookup_elem
471 * do:
472 * bpf_prog
473 * __htab_map_lookup_elem
474 */
475static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
476{
477 struct bpf_insn *insn = insn_buf;
478 const int ret = BPF_REG_0;
479
480 *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem);
481 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
482 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
483 offsetof(struct htab_elem, key) +
484 round_up(map->key_size, 8));
485 return insn - insn_buf;
486}
487
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800488static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
489{
490 struct htab_elem *l = __htab_map_lookup_elem(map, key);
491
492 if (l) {
493 bpf_lru_node_set_ref(&l->lru_node);
494 return l->key + round_up(map->key_size, 8);
495 }
496
497 return NULL;
498}
499
500/* It is called from the bpf_lru_list when the LRU needs to delete
501 * older elements from the htab.
502 */
503static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
504{
505 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800506 struct htab_elem *l = NULL, *tgt_l;
507 struct hlist_nulls_head *head;
508 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800509 unsigned long flags;
510 struct bucket *b;
511
512 tgt_l = container_of(node, struct htab_elem, lru_node);
513 b = __select_bucket(htab, tgt_l->hash);
514 head = &b->head;
515
516 raw_spin_lock_irqsave(&b->lock, flags);
517
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800518 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800519 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800520 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800521 break;
522 }
523
524 raw_spin_unlock_irqrestore(&b->lock, flags);
525
526 return l == tgt_l;
527}
528
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800529/* Called from syscall */
530static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
531{
532 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800533 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800534 struct htab_elem *l, *next_l;
535 u32 hash, key_size;
536 int i;
537
538 WARN_ON_ONCE(!rcu_read_lock_held());
539
540 key_size = map->key_size;
541
542 hash = htab_map_hash(key, key_size);
543
544 head = select_bucket(htab, hash);
545
546 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800547 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800548
549 if (!l) {
550 i = 0;
551 goto find_first_elem;
552 }
553
554 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800555 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800556 struct htab_elem, hash_node);
557
558 if (next_l) {
559 /* if next elem in this hash list is non-zero, just return it */
560 memcpy(next_key, next_l->key, key_size);
561 return 0;
562 }
563
564 /* no more elements in this hash list, go to the next bucket */
565 i = hash & (htab->n_buckets - 1);
566 i++;
567
568find_first_elem:
569 /* iterate over buckets */
570 for (; i < htab->n_buckets; i++) {
571 head = select_bucket(htab, i);
572
573 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800574 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800575 struct htab_elem, hash_node);
576 if (next_l) {
577 /* if it's not empty, just return it */
578 memcpy(next_key, next_l->key, key_size);
579 return 0;
580 }
581 }
582
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800583 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800584 return -ENOENT;
585}
586
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800587static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800588{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800589 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
590 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800591 kfree(l);
592}
593
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800594static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800595{
596 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800597 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800598
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800599 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
600 * we're calling kfree, otherwise deadlock is possible if kprobes
601 * are placed somewhere inside of slub
602 */
603 preempt_disable();
604 __this_cpu_inc(bpf_prog_active);
605 htab_elem_free(htab, l);
606 __this_cpu_dec(bpf_prog_active);
607 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800608}
609
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800610static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800611{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700612 struct bpf_map *map = &htab->map;
613
614 if (map->ops->map_fd_put_ptr) {
615 void *ptr = fd_htab_map_get_ptr(map, l);
616
617 map->ops->map_fd_put_ptr(ptr);
618 }
619
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700620 if (l->state == HTAB_EXTRA_ELEM_USED) {
621 l->state = HTAB_EXTRA_ELEM_FREE;
622 return;
623 }
624
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800625 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
626 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800627 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800628 atomic_dec(&htab->count);
629 l->htab = htab;
630 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800631 }
632}
633
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800634static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
635 void *value, bool onallcpus)
636{
637 if (!onallcpus) {
638 /* copy true value_size bytes */
639 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
640 } else {
641 u32 size = round_up(htab->map.value_size, 8);
642 int off = 0, cpu;
643
644 for_each_possible_cpu(cpu) {
645 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
646 value + off, size);
647 off += size;
648 }
649 }
650}
651
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800652static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
653 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700654 bool percpu, bool onallcpus,
655 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800656{
657 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800658 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800659 struct htab_elem *l_new;
660 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700661 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800662
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800663 if (prealloc) {
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800664 struct pcpu_freelist_node *l;
665
666 l = pcpu_freelist_pop(&htab->freelist);
667 if (!l)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700668 err = -E2BIG;
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800669 else
670 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800671 } else {
672 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
673 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700674 err = -E2BIG;
675 } else {
676 l_new = kmalloc(htab->elem_size,
677 GFP_ATOMIC | __GFP_NOWARN);
678 if (!l_new)
679 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800680 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700681 }
682
683 if (err) {
684 if (!old_elem_exists)
685 return ERR_PTR(err);
686
687 /* if we're updating the existing element and the hash table
688 * is full, use per-cpu extra elems
689 */
690 l_new = this_cpu_ptr(htab->extra_elems);
691 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
692 return ERR_PTR(-E2BIG);
693 l_new->state = HTAB_EXTRA_ELEM_USED;
694 } else {
695 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800696 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800697
698 memcpy(l_new->key, key, key_size);
699 if (percpu) {
700 /* round up value_size to 8 bytes */
701 size = round_up(size, 8);
702
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800703 if (prealloc) {
704 pptr = htab_elem_get_ptr(l_new, key_size);
705 } else {
706 /* alloc_percpu zero-fills */
707 pptr = __alloc_percpu_gfp(size, 8,
708 GFP_ATOMIC | __GFP_NOWARN);
709 if (!pptr) {
710 kfree(l_new);
711 return ERR_PTR(-ENOMEM);
712 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800713 }
714
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800715 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800716
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800717 if (!prealloc)
718 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800719 } else {
720 memcpy(l_new->key + round_up(key_size, 8), value, size);
721 }
722
723 l_new->hash = hash;
724 return l_new;
725}
726
727static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
728 u64 map_flags)
729{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800730 if (l_old && map_flags == BPF_NOEXIST)
731 /* elem already exists */
732 return -EEXIST;
733
734 if (!l_old && map_flags == BPF_EXIST)
735 /* elem doesn't exist, cannot update it */
736 return -ENOENT;
737
738 return 0;
739}
740
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800741/* Called from syscall or from eBPF program */
742static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
743 u64 map_flags)
744{
745 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800746 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800747 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800748 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800749 struct bucket *b;
750 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800751 int ret;
752
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800753 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800754 /* unknown flags */
755 return -EINVAL;
756
757 WARN_ON_ONCE(!rcu_read_lock_held());
758
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800759 key_size = map->key_size;
760
761 hash = htab_map_hash(key, key_size);
762
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800763 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800764 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800765
766 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800767 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800768
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800769 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800770
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800771 ret = check_flags(htab, l_old, map_flags);
772 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800773 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800774
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700775 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
776 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800777 if (IS_ERR(l_new)) {
778 /* all pre-allocated elements are in use or memory exhausted */
779 ret = PTR_ERR(l_new);
780 goto err;
781 }
782
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800783 /* add new element to the head of the list, so that
784 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800785 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800786 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800787 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800788 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800789 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800790 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800791 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800792err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800793 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800794 return ret;
795}
796
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800797static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
798 u64 map_flags)
799{
800 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
801 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800802 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800803 unsigned long flags;
804 struct bucket *b;
805 u32 key_size, hash;
806 int ret;
807
808 if (unlikely(map_flags > BPF_EXIST))
809 /* unknown flags */
810 return -EINVAL;
811
812 WARN_ON_ONCE(!rcu_read_lock_held());
813
814 key_size = map->key_size;
815
816 hash = htab_map_hash(key, key_size);
817
818 b = __select_bucket(htab, hash);
819 head = &b->head;
820
821 /* For LRU, we need to alloc before taking bucket's
822 * spinlock because getting free nodes from LRU may need
823 * to remove older elements from htab and this removal
824 * operation will need a bucket lock.
825 */
826 l_new = prealloc_lru_pop(htab, key, hash);
827 if (!l_new)
828 return -ENOMEM;
829 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
830
831 /* bpf_map_update_elem() can be called in_irq() */
832 raw_spin_lock_irqsave(&b->lock, flags);
833
834 l_old = lookup_elem_raw(head, hash, key, key_size);
835
836 ret = check_flags(htab, l_old, map_flags);
837 if (ret)
838 goto err;
839
840 /* add new element to the head of the list, so that
841 * concurrent search will find it before old elem
842 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800843 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800844 if (l_old) {
845 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800846 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800847 }
848 ret = 0;
849
850err:
851 raw_spin_unlock_irqrestore(&b->lock, flags);
852
853 if (ret)
854 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
855 else if (l_old)
856 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
857
858 return ret;
859}
860
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800861static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
862 void *value, u64 map_flags,
863 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800864{
865 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
866 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800867 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800868 unsigned long flags;
869 struct bucket *b;
870 u32 key_size, hash;
871 int ret;
872
873 if (unlikely(map_flags > BPF_EXIST))
874 /* unknown flags */
875 return -EINVAL;
876
877 WARN_ON_ONCE(!rcu_read_lock_held());
878
879 key_size = map->key_size;
880
881 hash = htab_map_hash(key, key_size);
882
883 b = __select_bucket(htab, hash);
884 head = &b->head;
885
886 /* bpf_map_update_elem() can be called in_irq() */
887 raw_spin_lock_irqsave(&b->lock, flags);
888
889 l_old = lookup_elem_raw(head, hash, key, key_size);
890
891 ret = check_flags(htab, l_old, map_flags);
892 if (ret)
893 goto err;
894
895 if (l_old) {
896 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800897 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
898 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800899 } else {
900 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700901 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800902 if (IS_ERR(l_new)) {
903 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800904 goto err;
905 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800906 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800907 }
908 ret = 0;
909err:
910 raw_spin_unlock_irqrestore(&b->lock, flags);
911 return ret;
912}
913
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800914static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
915 void *value, u64 map_flags,
916 bool onallcpus)
917{
918 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
919 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800920 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800921 unsigned long flags;
922 struct bucket *b;
923 u32 key_size, hash;
924 int ret;
925
926 if (unlikely(map_flags > BPF_EXIST))
927 /* unknown flags */
928 return -EINVAL;
929
930 WARN_ON_ONCE(!rcu_read_lock_held());
931
932 key_size = map->key_size;
933
934 hash = htab_map_hash(key, key_size);
935
936 b = __select_bucket(htab, hash);
937 head = &b->head;
938
939 /* For LRU, we need to alloc before taking bucket's
940 * spinlock because LRU's elem alloc may need
941 * to remove older elem from htab and this removal
942 * operation will need a bucket lock.
943 */
944 if (map_flags != BPF_EXIST) {
945 l_new = prealloc_lru_pop(htab, key, hash);
946 if (!l_new)
947 return -ENOMEM;
948 }
949
950 /* bpf_map_update_elem() can be called in_irq() */
951 raw_spin_lock_irqsave(&b->lock, flags);
952
953 l_old = lookup_elem_raw(head, hash, key, key_size);
954
955 ret = check_flags(htab, l_old, map_flags);
956 if (ret)
957 goto err;
958
959 if (l_old) {
960 bpf_lru_node_set_ref(&l_old->lru_node);
961
962 /* per-cpu hash map can update value in-place */
963 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
964 value, onallcpus);
965 } else {
966 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
967 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800968 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800969 l_new = NULL;
970 }
971 ret = 0;
972err:
973 raw_spin_unlock_irqrestore(&b->lock, flags);
974 if (l_new)
975 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
976 return ret;
977}
978
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800979static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
980 void *value, u64 map_flags)
981{
982 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
983}
984
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800985static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
986 void *value, u64 map_flags)
987{
988 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
989 false);
990}
991
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800992/* Called from syscall or from eBPF program */
993static int htab_map_delete_elem(struct bpf_map *map, void *key)
994{
995 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800996 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800997 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800998 struct htab_elem *l;
999 unsigned long flags;
1000 u32 hash, key_size;
1001 int ret = -ENOENT;
1002
1003 WARN_ON_ONCE(!rcu_read_lock_held());
1004
1005 key_size = map->key_size;
1006
1007 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001008 b = __select_bucket(htab, hash);
1009 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001010
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001011 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001012
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001013 l = lookup_elem_raw(head, hash, key, key_size);
1014
1015 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001016 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001017 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001018 ret = 0;
1019 }
1020
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001021 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001022 return ret;
1023}
1024
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001025static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1026{
1027 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001028 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001029 struct bucket *b;
1030 struct htab_elem *l;
1031 unsigned long flags;
1032 u32 hash, key_size;
1033 int ret = -ENOENT;
1034
1035 WARN_ON_ONCE(!rcu_read_lock_held());
1036
1037 key_size = map->key_size;
1038
1039 hash = htab_map_hash(key, key_size);
1040 b = __select_bucket(htab, hash);
1041 head = &b->head;
1042
1043 raw_spin_lock_irqsave(&b->lock, flags);
1044
1045 l = lookup_elem_raw(head, hash, key, key_size);
1046
1047 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001048 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001049 ret = 0;
1050 }
1051
1052 raw_spin_unlock_irqrestore(&b->lock, flags);
1053 if (l)
1054 bpf_lru_push_free(&htab->lru, &l->lru_node);
1055 return ret;
1056}
1057
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001058static void delete_all_elements(struct bpf_htab *htab)
1059{
1060 int i;
1061
1062 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001063 struct hlist_nulls_head *head = select_bucket(htab, i);
1064 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001065 struct htab_elem *l;
1066
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001067 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1068 hlist_nulls_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +01001069 if (l->state != HTAB_EXTRA_ELEM_USED)
1070 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001071 }
1072 }
1073}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001074
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001075/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1076static void htab_map_free(struct bpf_map *map)
1077{
1078 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1079
1080 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1081 * so the programs (can be more than one that used this map) were
1082 * disconnected from events. Wait for outstanding critical sections in
1083 * these programs to complete
1084 */
1085 synchronize_rcu();
1086
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001087 /* some of free_htab_elem() callbacks for elements of this map may
1088 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001089 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001090 rcu_barrier();
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001091 if (htab->map.map_flags & BPF_F_NO_PREALLOC)
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001092 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001093 else
1094 prealloc_destroy(htab);
1095
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001096 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001097 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001098 kfree(htab);
1099}
1100
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001101static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001102 .map_alloc = htab_map_alloc,
1103 .map_free = htab_map_free,
1104 .map_get_next_key = htab_map_get_next_key,
1105 .map_lookup_elem = htab_map_lookup_elem,
1106 .map_update_elem = htab_map_update_elem,
1107 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001108 .map_gen_lookup = htab_map_gen_lookup,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001109};
1110
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001111static struct bpf_map_type_list htab_type __ro_after_init = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001112 .ops = &htab_ops,
1113 .type = BPF_MAP_TYPE_HASH,
1114};
1115
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001116static const struct bpf_map_ops htab_lru_ops = {
1117 .map_alloc = htab_map_alloc,
1118 .map_free = htab_map_free,
1119 .map_get_next_key = htab_map_get_next_key,
1120 .map_lookup_elem = htab_lru_map_lookup_elem,
1121 .map_update_elem = htab_lru_map_update_elem,
1122 .map_delete_elem = htab_lru_map_delete_elem,
1123};
1124
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001125static struct bpf_map_type_list htab_lru_type __ro_after_init = {
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001126 .ops = &htab_lru_ops,
1127 .type = BPF_MAP_TYPE_LRU_HASH,
1128};
1129
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001130/* Called from eBPF program */
1131static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1132{
1133 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1134
1135 if (l)
1136 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1137 else
1138 return NULL;
1139}
1140
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001141static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1142{
1143 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1144
1145 if (l) {
1146 bpf_lru_node_set_ref(&l->lru_node);
1147 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1148 }
1149
1150 return NULL;
1151}
1152
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001153int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1154{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001155 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001156 struct htab_elem *l;
1157 void __percpu *pptr;
1158 int ret = -ENOENT;
1159 int cpu, off = 0;
1160 u32 size;
1161
1162 /* per_cpu areas are zero-filled and bpf programs can only
1163 * access 'value_size' of them, so copying rounded areas
1164 * will not leak any kernel data
1165 */
1166 size = round_up(map->value_size, 8);
1167 rcu_read_lock();
1168 l = __htab_map_lookup_elem(map, key);
1169 if (!l)
1170 goto out;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001171 if (htab_is_lru(htab))
1172 bpf_lru_node_set_ref(&l->lru_node);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001173 pptr = htab_elem_get_ptr(l, map->key_size);
1174 for_each_possible_cpu(cpu) {
1175 bpf_long_memcpy(value + off,
1176 per_cpu_ptr(pptr, cpu), size);
1177 off += size;
1178 }
1179 ret = 0;
1180out:
1181 rcu_read_unlock();
1182 return ret;
1183}
1184
1185int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1186 u64 map_flags)
1187{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001188 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001189 int ret;
1190
1191 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001192 if (htab_is_lru(htab))
1193 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1194 map_flags, true);
1195 else
1196 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1197 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001198 rcu_read_unlock();
1199
1200 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001201}
1202
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001203static const struct bpf_map_ops htab_percpu_ops = {
1204 .map_alloc = htab_map_alloc,
1205 .map_free = htab_map_free,
1206 .map_get_next_key = htab_map_get_next_key,
1207 .map_lookup_elem = htab_percpu_map_lookup_elem,
1208 .map_update_elem = htab_percpu_map_update_elem,
1209 .map_delete_elem = htab_map_delete_elem,
1210};
1211
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001212static struct bpf_map_type_list htab_percpu_type __ro_after_init = {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001213 .ops = &htab_percpu_ops,
1214 .type = BPF_MAP_TYPE_PERCPU_HASH,
1215};
1216
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001217static const struct bpf_map_ops htab_lru_percpu_ops = {
1218 .map_alloc = htab_map_alloc,
1219 .map_free = htab_map_free,
1220 .map_get_next_key = htab_map_get_next_key,
1221 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1222 .map_update_elem = htab_lru_percpu_map_update_elem,
1223 .map_delete_elem = htab_lru_map_delete_elem,
1224};
1225
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001226static struct bpf_map_type_list htab_lru_percpu_type __ro_after_init = {
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001227 .ops = &htab_lru_percpu_ops,
1228 .type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
1229};
1230
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001231static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr)
1232{
1233 struct bpf_map *map;
1234
1235 if (attr->value_size != sizeof(u32))
1236 return ERR_PTR(-EINVAL);
1237
1238 /* pointer is stored internally */
1239 attr->value_size = sizeof(void *);
1240 map = htab_map_alloc(attr);
1241 attr->value_size = sizeof(u32);
1242
1243 return map;
1244}
1245
1246static void fd_htab_map_free(struct bpf_map *map)
1247{
1248 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1249 struct hlist_nulls_node *n;
1250 struct hlist_nulls_head *head;
1251 struct htab_elem *l;
1252 int i;
1253
1254 for (i = 0; i < htab->n_buckets; i++) {
1255 head = select_bucket(htab, i);
1256
1257 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1258 void *ptr = fd_htab_map_get_ptr(map, l);
1259
1260 map->ops->map_fd_put_ptr(ptr);
1261 }
1262 }
1263
1264 htab_map_free(map);
1265}
1266
1267/* only called from syscall */
1268int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1269 void *key, void *value, u64 map_flags)
1270{
1271 void *ptr;
1272 int ret;
1273 u32 ufd = *(u32 *)value;
1274
1275 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1276 if (IS_ERR(ptr))
1277 return PTR_ERR(ptr);
1278
1279 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1280 if (ret)
1281 map->ops->map_fd_put_ptr(ptr);
1282
1283 return ret;
1284}
1285
1286static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1287{
1288 struct bpf_map *map, *inner_map_meta;
1289
1290 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1291 if (IS_ERR(inner_map_meta))
1292 return inner_map_meta;
1293
1294 map = fd_htab_map_alloc(attr);
1295 if (IS_ERR(map)) {
1296 bpf_map_meta_free(inner_map_meta);
1297 return map;
1298 }
1299
1300 map->inner_map_meta = inner_map_meta;
1301
1302 return map;
1303}
1304
1305static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1306{
1307 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1308
1309 if (!inner_map)
1310 return NULL;
1311
1312 return READ_ONCE(*inner_map);
1313}
1314
1315static void htab_of_map_free(struct bpf_map *map)
1316{
1317 bpf_map_meta_free(map->inner_map_meta);
1318 fd_htab_map_free(map);
1319}
1320
1321static const struct bpf_map_ops htab_of_map_ops = {
1322 .map_alloc = htab_of_map_alloc,
1323 .map_free = htab_of_map_free,
1324 .map_get_next_key = htab_map_get_next_key,
1325 .map_lookup_elem = htab_of_map_lookup_elem,
1326 .map_delete_elem = htab_map_delete_elem,
1327 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1328 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1329};
1330
1331static struct bpf_map_type_list htab_of_map_type __ro_after_init = {
1332 .ops = &htab_of_map_ops,
1333 .type = BPF_MAP_TYPE_HASH_OF_MAPS,
1334};
1335
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001336static int __init register_htab_map(void)
1337{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001338 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001339 bpf_register_map_type(&htab_percpu_type);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001340 bpf_register_map_type(&htab_lru_type);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001341 bpf_register_map_type(&htab_lru_percpu_type);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001342 bpf_register_map_type(&htab_of_map_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001343 return 0;
1344}
1345late_initcall(register_htab_map);