blob: afe5bab376c9811c7b82f56bc0b93ce69b6a579b [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"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080019
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080020struct bucket {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080021 struct hlist_nulls_head head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080022 raw_spinlock_t lock;
23};
24
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080025struct bpf_htab {
26 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080027 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080028 void *elems;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080029 union {
30 struct pcpu_freelist freelist;
31 struct bpf_lru lru;
32 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070033 void __percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080034 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080035 u32 n_buckets; /* number of hash buckets */
36 u32 elem_size; /* size of each element in bytes */
37};
38
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070039enum extra_elem_state {
40 HTAB_NOT_AN_EXTRA_ELEM = 0,
41 HTAB_EXTRA_ELEM_FREE,
42 HTAB_EXTRA_ELEM_USED
43};
44
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080045/* each htab element is struct htab_elem + key + value */
46struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080047 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080048 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080049 struct {
50 void *padding;
51 union {
52 struct bpf_htab *htab;
53 struct pcpu_freelist_node fnode;
54 };
55 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080056 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070057 union {
58 struct rcu_head rcu;
59 enum extra_elem_state state;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080060 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070061 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080062 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080063 char key[0] __aligned(8);
64};
65
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080066static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
67
68static bool htab_is_lru(const struct bpf_htab *htab)
69{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080070 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72}
73
74static bool htab_is_percpu(const struct bpf_htab *htab)
75{
76 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
77 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080078}
79
Alexei Starovoitov6c905982016-03-07 21:57:15 -080080static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
81 void __percpu *pptr)
82{
83 *(void __percpu **)(l->key + key_size) = pptr;
84}
85
86static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
87{
88 return *(void __percpu **)(l->key + key_size);
89}
90
91static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
92{
93 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
94}
95
96static void htab_free_elems(struct bpf_htab *htab)
97{
98 int i;
99
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800100 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800101 goto free_elems;
102
103 for (i = 0; i < htab->map.max_entries; i++) {
104 void __percpu *pptr;
105
106 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
107 htab->map.key_size);
108 free_percpu(pptr);
109 }
110free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100111 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800112}
113
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800114static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
115 u32 hash)
116{
117 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
118 struct htab_elem *l;
119
120 if (node) {
121 l = container_of(node, struct htab_elem, lru_node);
122 memcpy(l->key, key, htab->map.key_size);
123 return l;
124 }
125
126 return NULL;
127}
128
129static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800130{
131 int err = -ENOMEM, i;
132
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100133 htab->elems = bpf_map_area_alloc(htab->elem_size *
134 htab->map.max_entries);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800135 if (!htab->elems)
136 return -ENOMEM;
137
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800138 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800139 goto skip_percpu_elems;
140
141 for (i = 0; i < htab->map.max_entries; i++) {
142 u32 size = round_up(htab->map.value_size, 8);
143 void __percpu *pptr;
144
145 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
146 if (!pptr)
147 goto free_elems;
148 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
149 pptr);
150 }
151
152skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800153 if (htab_is_lru(htab))
154 err = bpf_lru_init(&htab->lru,
155 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
156 offsetof(struct htab_elem, hash) -
157 offsetof(struct htab_elem, lru_node),
158 htab_lru_map_delete_node,
159 htab);
160 else
161 err = pcpu_freelist_init(&htab->freelist);
162
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800163 if (err)
164 goto free_elems;
165
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800166 if (htab_is_lru(htab))
167 bpf_lru_populate(&htab->lru, htab->elems,
168 offsetof(struct htab_elem, lru_node),
169 htab->elem_size, htab->map.max_entries);
170 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800171 pcpu_freelist_populate(&htab->freelist,
172 htab->elems + offsetof(struct htab_elem, fnode),
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800173 htab->elem_size, htab->map.max_entries);
174
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800175 return 0;
176
177free_elems:
178 htab_free_elems(htab);
179 return err;
180}
181
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800182static void prealloc_destroy(struct bpf_htab *htab)
183{
184 htab_free_elems(htab);
185
186 if (htab_is_lru(htab))
187 bpf_lru_destroy(&htab->lru);
188 else
189 pcpu_freelist_destroy(&htab->freelist);
190}
191
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700192static int alloc_extra_elems(struct bpf_htab *htab)
193{
194 void __percpu *pptr;
195 int cpu;
196
197 pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
198 if (!pptr)
199 return -ENOMEM;
200
201 for_each_possible_cpu(cpu) {
202 ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
203 HTAB_EXTRA_ELEM_FREE;
204 }
205 htab->extra_elems = pptr;
206 return 0;
207}
208
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800209/* Called from syscall */
210static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
211{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800212 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
213 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
214 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
215 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800216 /* percpu_lru means each cpu has its own LRU list.
217 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
218 * the map's value itself is percpu. percpu_lru has
219 * nothing to do with the map's value.
220 */
221 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
222 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800223 struct bpf_htab *htab;
224 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800225 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800226
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800227 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
228 offsetof(struct htab_elem, hash_node.pprev));
229 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
230 offsetof(struct htab_elem, hash_node.pprev));
231
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800232 if (lru && !capable(CAP_SYS_ADMIN))
233 /* LRU implementation is much complicated than other
234 * maps. Hence, limit to CAP_SYS_ADMIN for now.
235 */
236 return ERR_PTR(-EPERM);
237
238 if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800239 /* reserved bits should not be used */
240 return ERR_PTR(-EINVAL);
241
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800242 if (!lru && percpu_lru)
243 return ERR_PTR(-EINVAL);
244
245 if (lru && !prealloc)
246 return ERR_PTR(-ENOTSUPP);
247
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800248 htab = kzalloc(sizeof(*htab), GFP_USER);
249 if (!htab)
250 return ERR_PTR(-ENOMEM);
251
252 /* mandatory map attributes */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800253 htab->map.map_type = attr->map_type;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800254 htab->map.key_size = attr->key_size;
255 htab->map.value_size = attr->value_size;
256 htab->map.max_entries = attr->max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800257 htab->map.map_flags = attr->map_flags;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800258
259 /* check sanity of attributes.
260 * value_size == 0 may be allowed in the future to use map as a set
261 */
262 err = -EINVAL;
263 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
264 htab->map.value_size == 0)
265 goto free_htab;
266
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800267 if (percpu_lru) {
268 /* ensure each CPU's lru list has >=1 elements.
269 * since we are at it, make each lru list has the same
270 * number of elements.
271 */
272 htab->map.max_entries = roundup(attr->max_entries,
273 num_possible_cpus());
274 if (htab->map.max_entries < attr->max_entries)
275 htab->map.max_entries = rounddown(attr->max_entries,
276 num_possible_cpus());
277 }
278
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800279 /* hash table size must be power of 2 */
280 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
281
282 err = -E2BIG;
283 if (htab->map.key_size > MAX_BPF_STACK)
284 /* eBPF programs initialize keys on stack, so they cannot be
285 * larger than max stack size
286 */
287 goto free_htab;
288
Michal Hocko7984c272017-01-10 16:57:30 -0800289 if (htab->map.value_size >= KMALLOC_MAX_SIZE -
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800290 MAX_BPF_STACK - sizeof(struct htab_elem))
291 /* if value_size is bigger, the user space won't be able to
292 * access the elements via bpf syscall. This check also makes
293 * sure that the elem_size doesn't overflow and it's
294 * kmalloc-able later in htab_map_update_elem()
295 */
296 goto free_htab;
297
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800298 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
299 /* make sure the size for pcpu_alloc() is reasonable */
300 goto free_htab;
301
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800302 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800303 round_up(htab->map.key_size, 8);
304 if (percpu)
305 htab->elem_size += sizeof(void *);
306 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800307 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800308
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800309 /* prevent zero size kmalloc and check for u32 overflow */
310 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800311 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800312 goto free_htab;
313
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800314 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
315 (u64) htab->elem_size * htab->map.max_entries;
316
317 if (percpu)
318 cost += (u64) round_up(htab->map.value_size, 8) *
319 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700320 else
321 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800322
323 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800324 /* make sure page count doesn't overflow */
325 goto free_htab;
326
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800327 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800328
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800329 /* if map size is larger than memlock limit, reject it early */
330 err = bpf_map_precharge_memlock(htab->map.pages);
331 if (err)
332 goto free_htab;
333
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800334 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100335 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
336 sizeof(struct bucket));
337 if (!htab->buckets)
338 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800339
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800340 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800341 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800342 raw_spin_lock_init(&htab->buckets[i].lock);
343 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800344
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800345 if (!percpu && !lru) {
346 /* lru itself can remove the least used element, so
347 * there is no need for an extra elem during map_update.
348 */
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700349 err = alloc_extra_elems(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800350 if (err)
351 goto free_buckets;
352 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800353
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800354 if (prealloc) {
355 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700356 if (err)
357 goto free_extra_elems;
358 }
359
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800360 return &htab->map;
361
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700362free_extra_elems:
363 free_percpu(htab->extra_elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800364free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100365 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800366free_htab:
367 kfree(htab);
368 return ERR_PTR(err);
369}
370
371static inline u32 htab_map_hash(const void *key, u32 key_len)
372{
373 return jhash(key, key_len, 0);
374}
375
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800376static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800377{
378 return &htab->buckets[hash & (htab->n_buckets - 1)];
379}
380
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800381static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800382{
383 return &__select_bucket(htab, hash)->head;
384}
385
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800386/* this lookup function can only be called with bucket lock taken */
387static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800388 void *key, u32 key_size)
389{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800390 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800391 struct htab_elem *l;
392
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800393 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800394 if (l->hash == hash && !memcmp(&l->key, key, key_size))
395 return l;
396
397 return NULL;
398}
399
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800400/* can be called without bucket lock. it will repeat the loop in
401 * the unlikely event when elements moved from one bucket into another
402 * while link list is being walked
403 */
404static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
405 u32 hash, void *key,
406 u32 key_size, u32 n_buckets)
407{
408 struct hlist_nulls_node *n;
409 struct htab_elem *l;
410
411again:
412 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
413 if (l->hash == hash && !memcmp(&l->key, key, key_size))
414 return l;
415
416 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
417 goto again;
418
419 return NULL;
420}
421
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800422/* Called from syscall or from eBPF program */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800423static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800424{
425 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800426 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800427 struct htab_elem *l;
428 u32 hash, key_size;
429
430 /* Must be called with rcu_read_lock. */
431 WARN_ON_ONCE(!rcu_read_lock_held());
432
433 key_size = map->key_size;
434
435 hash = htab_map_hash(key, key_size);
436
437 head = select_bucket(htab, hash);
438
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800439 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800440
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800441 return l;
442}
443
444static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
445{
446 struct htab_elem *l = __htab_map_lookup_elem(map, key);
447
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800448 if (l)
449 return l->key + round_up(map->key_size, 8);
450
451 return NULL;
452}
453
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800454static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
455{
456 struct htab_elem *l = __htab_map_lookup_elem(map, key);
457
458 if (l) {
459 bpf_lru_node_set_ref(&l->lru_node);
460 return l->key + round_up(map->key_size, 8);
461 }
462
463 return NULL;
464}
465
466/* It is called from the bpf_lru_list when the LRU needs to delete
467 * older elements from the htab.
468 */
469static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
470{
471 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800472 struct htab_elem *l = NULL, *tgt_l;
473 struct hlist_nulls_head *head;
474 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800475 unsigned long flags;
476 struct bucket *b;
477
478 tgt_l = container_of(node, struct htab_elem, lru_node);
479 b = __select_bucket(htab, tgt_l->hash);
480 head = &b->head;
481
482 raw_spin_lock_irqsave(&b->lock, flags);
483
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800484 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800485 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800486 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800487 break;
488 }
489
490 raw_spin_unlock_irqrestore(&b->lock, flags);
491
492 return l == tgt_l;
493}
494
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800495/* Called from syscall */
496static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
497{
498 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800499 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800500 struct htab_elem *l, *next_l;
501 u32 hash, key_size;
502 int i;
503
504 WARN_ON_ONCE(!rcu_read_lock_held());
505
506 key_size = map->key_size;
507
508 hash = htab_map_hash(key, key_size);
509
510 head = select_bucket(htab, hash);
511
512 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800513 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800514
515 if (!l) {
516 i = 0;
517 goto find_first_elem;
518 }
519
520 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800521 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800522 struct htab_elem, hash_node);
523
524 if (next_l) {
525 /* if next elem in this hash list is non-zero, just return it */
526 memcpy(next_key, next_l->key, key_size);
527 return 0;
528 }
529
530 /* no more elements in this hash list, go to the next bucket */
531 i = hash & (htab->n_buckets - 1);
532 i++;
533
534find_first_elem:
535 /* iterate over buckets */
536 for (; i < htab->n_buckets; i++) {
537 head = select_bucket(htab, i);
538
539 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800540 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800541 struct htab_elem, hash_node);
542 if (next_l) {
543 /* if it's not empty, just return it */
544 memcpy(next_key, next_l->key, key_size);
545 return 0;
546 }
547 }
548
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800549 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800550 return -ENOENT;
551}
552
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800553static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800554{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800555 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
556 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800557 kfree(l);
558}
559
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800560static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800561{
562 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800563 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800564
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800565 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
566 * we're calling kfree, otherwise deadlock is possible if kprobes
567 * are placed somewhere inside of slub
568 */
569 preempt_disable();
570 __this_cpu_inc(bpf_prog_active);
571 htab_elem_free(htab, l);
572 __this_cpu_dec(bpf_prog_active);
573 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800574}
575
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800576static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800577{
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700578 if (l->state == HTAB_EXTRA_ELEM_USED) {
579 l->state = HTAB_EXTRA_ELEM_FREE;
580 return;
581 }
582
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800583 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
584 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800585 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800586 atomic_dec(&htab->count);
587 l->htab = htab;
588 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800589 }
590}
591
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800592static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
593 void *value, bool onallcpus)
594{
595 if (!onallcpus) {
596 /* copy true value_size bytes */
597 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
598 } else {
599 u32 size = round_up(htab->map.value_size, 8);
600 int off = 0, cpu;
601
602 for_each_possible_cpu(cpu) {
603 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
604 value + off, size);
605 off += size;
606 }
607 }
608}
609
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800610static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
611 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700612 bool percpu, bool onallcpus,
613 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800614{
615 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800616 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800617 struct htab_elem *l_new;
618 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700619 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800620
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800621 if (prealloc) {
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800622 struct pcpu_freelist_node *l;
623
624 l = pcpu_freelist_pop(&htab->freelist);
625 if (!l)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700626 err = -E2BIG;
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800627 else
628 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800629 } else {
630 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
631 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700632 err = -E2BIG;
633 } else {
634 l_new = kmalloc(htab->elem_size,
635 GFP_ATOMIC | __GFP_NOWARN);
636 if (!l_new)
637 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800638 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700639 }
640
641 if (err) {
642 if (!old_elem_exists)
643 return ERR_PTR(err);
644
645 /* if we're updating the existing element and the hash table
646 * is full, use per-cpu extra elems
647 */
648 l_new = this_cpu_ptr(htab->extra_elems);
649 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
650 return ERR_PTR(-E2BIG);
651 l_new->state = HTAB_EXTRA_ELEM_USED;
652 } else {
653 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800654 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800655
656 memcpy(l_new->key, key, key_size);
657 if (percpu) {
658 /* round up value_size to 8 bytes */
659 size = round_up(size, 8);
660
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800661 if (prealloc) {
662 pptr = htab_elem_get_ptr(l_new, key_size);
663 } else {
664 /* alloc_percpu zero-fills */
665 pptr = __alloc_percpu_gfp(size, 8,
666 GFP_ATOMIC | __GFP_NOWARN);
667 if (!pptr) {
668 kfree(l_new);
669 return ERR_PTR(-ENOMEM);
670 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800671 }
672
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800673 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800674
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800675 if (!prealloc)
676 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800677 } else {
678 memcpy(l_new->key + round_up(key_size, 8), value, size);
679 }
680
681 l_new->hash = hash;
682 return l_new;
683}
684
685static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
686 u64 map_flags)
687{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800688 if (l_old && map_flags == BPF_NOEXIST)
689 /* elem already exists */
690 return -EEXIST;
691
692 if (!l_old && map_flags == BPF_EXIST)
693 /* elem doesn't exist, cannot update it */
694 return -ENOENT;
695
696 return 0;
697}
698
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800699/* Called from syscall or from eBPF program */
700static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
701 u64 map_flags)
702{
703 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800704 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800705 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800706 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800707 struct bucket *b;
708 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800709 int ret;
710
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800711 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800712 /* unknown flags */
713 return -EINVAL;
714
715 WARN_ON_ONCE(!rcu_read_lock_held());
716
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800717 key_size = map->key_size;
718
719 hash = htab_map_hash(key, key_size);
720
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800721 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800722 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800723
724 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800725 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800726
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800727 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800728
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800729 ret = check_flags(htab, l_old, map_flags);
730 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800731 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800732
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700733 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
734 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800735 if (IS_ERR(l_new)) {
736 /* all pre-allocated elements are in use or memory exhausted */
737 ret = PTR_ERR(l_new);
738 goto err;
739 }
740
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800741 /* add new element to the head of the list, so that
742 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800743 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800744 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800745 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800746 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800747 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800748 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800749 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800750err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800751 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800752 return ret;
753}
754
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800755static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
756 u64 map_flags)
757{
758 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
759 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800760 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800761 unsigned long flags;
762 struct bucket *b;
763 u32 key_size, hash;
764 int ret;
765
766 if (unlikely(map_flags > BPF_EXIST))
767 /* unknown flags */
768 return -EINVAL;
769
770 WARN_ON_ONCE(!rcu_read_lock_held());
771
772 key_size = map->key_size;
773
774 hash = htab_map_hash(key, key_size);
775
776 b = __select_bucket(htab, hash);
777 head = &b->head;
778
779 /* For LRU, we need to alloc before taking bucket's
780 * spinlock because getting free nodes from LRU may need
781 * to remove older elements from htab and this removal
782 * operation will need a bucket lock.
783 */
784 l_new = prealloc_lru_pop(htab, key, hash);
785 if (!l_new)
786 return -ENOMEM;
787 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
788
789 /* bpf_map_update_elem() can be called in_irq() */
790 raw_spin_lock_irqsave(&b->lock, flags);
791
792 l_old = lookup_elem_raw(head, hash, key, key_size);
793
794 ret = check_flags(htab, l_old, map_flags);
795 if (ret)
796 goto err;
797
798 /* add new element to the head of the list, so that
799 * concurrent search will find it before old elem
800 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800801 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800802 if (l_old) {
803 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800804 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800805 }
806 ret = 0;
807
808err:
809 raw_spin_unlock_irqrestore(&b->lock, flags);
810
811 if (ret)
812 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
813 else if (l_old)
814 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
815
816 return ret;
817}
818
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800819static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
820 void *value, u64 map_flags,
821 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800822{
823 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
824 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800825 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800826 unsigned long flags;
827 struct bucket *b;
828 u32 key_size, hash;
829 int ret;
830
831 if (unlikely(map_flags > BPF_EXIST))
832 /* unknown flags */
833 return -EINVAL;
834
835 WARN_ON_ONCE(!rcu_read_lock_held());
836
837 key_size = map->key_size;
838
839 hash = htab_map_hash(key, key_size);
840
841 b = __select_bucket(htab, hash);
842 head = &b->head;
843
844 /* bpf_map_update_elem() can be called in_irq() */
845 raw_spin_lock_irqsave(&b->lock, flags);
846
847 l_old = lookup_elem_raw(head, hash, key, key_size);
848
849 ret = check_flags(htab, l_old, map_flags);
850 if (ret)
851 goto err;
852
853 if (l_old) {
854 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800855 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
856 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800857 } else {
858 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700859 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800860 if (IS_ERR(l_new)) {
861 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800862 goto err;
863 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800864 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800865 }
866 ret = 0;
867err:
868 raw_spin_unlock_irqrestore(&b->lock, flags);
869 return ret;
870}
871
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800872static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
873 void *value, u64 map_flags,
874 bool onallcpus)
875{
876 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
877 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800878 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800879 unsigned long flags;
880 struct bucket *b;
881 u32 key_size, hash;
882 int ret;
883
884 if (unlikely(map_flags > BPF_EXIST))
885 /* unknown flags */
886 return -EINVAL;
887
888 WARN_ON_ONCE(!rcu_read_lock_held());
889
890 key_size = map->key_size;
891
892 hash = htab_map_hash(key, key_size);
893
894 b = __select_bucket(htab, hash);
895 head = &b->head;
896
897 /* For LRU, we need to alloc before taking bucket's
898 * spinlock because LRU's elem alloc may need
899 * to remove older elem from htab and this removal
900 * operation will need a bucket lock.
901 */
902 if (map_flags != BPF_EXIST) {
903 l_new = prealloc_lru_pop(htab, key, hash);
904 if (!l_new)
905 return -ENOMEM;
906 }
907
908 /* bpf_map_update_elem() can be called in_irq() */
909 raw_spin_lock_irqsave(&b->lock, flags);
910
911 l_old = lookup_elem_raw(head, hash, key, key_size);
912
913 ret = check_flags(htab, l_old, map_flags);
914 if (ret)
915 goto err;
916
917 if (l_old) {
918 bpf_lru_node_set_ref(&l_old->lru_node);
919
920 /* per-cpu hash map can update value in-place */
921 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
922 value, onallcpus);
923 } else {
924 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
925 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800926 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800927 l_new = NULL;
928 }
929 ret = 0;
930err:
931 raw_spin_unlock_irqrestore(&b->lock, flags);
932 if (l_new)
933 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
934 return ret;
935}
936
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800937static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
938 void *value, u64 map_flags)
939{
940 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
941}
942
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800943static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
944 void *value, u64 map_flags)
945{
946 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
947 false);
948}
949
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800950/* Called from syscall or from eBPF program */
951static int htab_map_delete_elem(struct bpf_map *map, void *key)
952{
953 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800954 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800955 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800956 struct htab_elem *l;
957 unsigned long flags;
958 u32 hash, key_size;
959 int ret = -ENOENT;
960
961 WARN_ON_ONCE(!rcu_read_lock_held());
962
963 key_size = map->key_size;
964
965 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800966 b = __select_bucket(htab, hash);
967 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800968
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800969 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800970
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800971 l = lookup_elem_raw(head, hash, key, key_size);
972
973 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800974 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800975 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800976 ret = 0;
977 }
978
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800979 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800980 return ret;
981}
982
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800983static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
984{
985 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800986 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800987 struct bucket *b;
988 struct htab_elem *l;
989 unsigned long flags;
990 u32 hash, key_size;
991 int ret = -ENOENT;
992
993 WARN_ON_ONCE(!rcu_read_lock_held());
994
995 key_size = map->key_size;
996
997 hash = htab_map_hash(key, key_size);
998 b = __select_bucket(htab, hash);
999 head = &b->head;
1000
1001 raw_spin_lock_irqsave(&b->lock, flags);
1002
1003 l = lookup_elem_raw(head, hash, key, key_size);
1004
1005 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001006 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001007 ret = 0;
1008 }
1009
1010 raw_spin_unlock_irqrestore(&b->lock, flags);
1011 if (l)
1012 bpf_lru_push_free(&htab->lru, &l->lru_node);
1013 return ret;
1014}
1015
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001016static void delete_all_elements(struct bpf_htab *htab)
1017{
1018 int i;
1019
1020 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001021 struct hlist_nulls_head *head = select_bucket(htab, i);
1022 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001023 struct htab_elem *l;
1024
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001025 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1026 hlist_nulls_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +01001027 if (l->state != HTAB_EXTRA_ELEM_USED)
1028 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001029 }
1030 }
1031}
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001032/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1033static void htab_map_free(struct bpf_map *map)
1034{
1035 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1036
1037 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1038 * so the programs (can be more than one that used this map) were
1039 * disconnected from events. Wait for outstanding critical sections in
1040 * these programs to complete
1041 */
1042 synchronize_rcu();
1043
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001044 /* some of free_htab_elem() callbacks for elements of this map may
1045 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001046 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001047 rcu_barrier();
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001048 if (htab->map.map_flags & BPF_F_NO_PREALLOC)
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001049 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001050 else
1051 prealloc_destroy(htab);
1052
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001053 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001054 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001055 kfree(htab);
1056}
1057
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001058static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001059 .map_alloc = htab_map_alloc,
1060 .map_free = htab_map_free,
1061 .map_get_next_key = htab_map_get_next_key,
1062 .map_lookup_elem = htab_map_lookup_elem,
1063 .map_update_elem = htab_map_update_elem,
1064 .map_delete_elem = htab_map_delete_elem,
1065};
1066
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001067static struct bpf_map_type_list htab_type __ro_after_init = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001068 .ops = &htab_ops,
1069 .type = BPF_MAP_TYPE_HASH,
1070};
1071
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001072static const struct bpf_map_ops htab_lru_ops = {
1073 .map_alloc = htab_map_alloc,
1074 .map_free = htab_map_free,
1075 .map_get_next_key = htab_map_get_next_key,
1076 .map_lookup_elem = htab_lru_map_lookup_elem,
1077 .map_update_elem = htab_lru_map_update_elem,
1078 .map_delete_elem = htab_lru_map_delete_elem,
1079};
1080
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001081static struct bpf_map_type_list htab_lru_type __ro_after_init = {
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001082 .ops = &htab_lru_ops,
1083 .type = BPF_MAP_TYPE_LRU_HASH,
1084};
1085
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001086/* Called from eBPF program */
1087static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1088{
1089 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1090
1091 if (l)
1092 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1093 else
1094 return NULL;
1095}
1096
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001097static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1098{
1099 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1100
1101 if (l) {
1102 bpf_lru_node_set_ref(&l->lru_node);
1103 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1104 }
1105
1106 return NULL;
1107}
1108
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001109int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1110{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001111 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001112 struct htab_elem *l;
1113 void __percpu *pptr;
1114 int ret = -ENOENT;
1115 int cpu, off = 0;
1116 u32 size;
1117
1118 /* per_cpu areas are zero-filled and bpf programs can only
1119 * access 'value_size' of them, so copying rounded areas
1120 * will not leak any kernel data
1121 */
1122 size = round_up(map->value_size, 8);
1123 rcu_read_lock();
1124 l = __htab_map_lookup_elem(map, key);
1125 if (!l)
1126 goto out;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001127 if (htab_is_lru(htab))
1128 bpf_lru_node_set_ref(&l->lru_node);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001129 pptr = htab_elem_get_ptr(l, map->key_size);
1130 for_each_possible_cpu(cpu) {
1131 bpf_long_memcpy(value + off,
1132 per_cpu_ptr(pptr, cpu), size);
1133 off += size;
1134 }
1135 ret = 0;
1136out:
1137 rcu_read_unlock();
1138 return ret;
1139}
1140
1141int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1142 u64 map_flags)
1143{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001144 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001145 int ret;
1146
1147 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001148 if (htab_is_lru(htab))
1149 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1150 map_flags, true);
1151 else
1152 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1153 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001154 rcu_read_unlock();
1155
1156 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001157}
1158
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001159static const struct bpf_map_ops htab_percpu_ops = {
1160 .map_alloc = htab_map_alloc,
1161 .map_free = htab_map_free,
1162 .map_get_next_key = htab_map_get_next_key,
1163 .map_lookup_elem = htab_percpu_map_lookup_elem,
1164 .map_update_elem = htab_percpu_map_update_elem,
1165 .map_delete_elem = htab_map_delete_elem,
1166};
1167
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001168static struct bpf_map_type_list htab_percpu_type __ro_after_init = {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001169 .ops = &htab_percpu_ops,
1170 .type = BPF_MAP_TYPE_PERCPU_HASH,
1171};
1172
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001173static const struct bpf_map_ops htab_lru_percpu_ops = {
1174 .map_alloc = htab_map_alloc,
1175 .map_free = htab_map_free,
1176 .map_get_next_key = htab_map_get_next_key,
1177 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1178 .map_update_elem = htab_lru_percpu_map_update_elem,
1179 .map_delete_elem = htab_lru_map_delete_elem,
1180};
1181
Daniel Borkmannc78f8bd2017-02-16 22:24:48 +01001182static struct bpf_map_type_list htab_lru_percpu_type __ro_after_init = {
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001183 .ops = &htab_lru_percpu_ops,
1184 .type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
1185};
1186
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001187static int __init register_htab_map(void)
1188{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +01001189 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001190 bpf_register_map_type(&htab_percpu_type);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001191 bpf_register_map_type(&htab_lru_type);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001192 bpf_register_map_type(&htab_lru_percpu_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001193 return 0;
1194}
1195late_initcall(register_htab_map);