blob: 583df5cb302d7f37f54822be1b7e2fd667e57003 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov6c905982016-03-07 21:57:15 -08003 * Copyright (c) 2016 Facebook
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08004 */
5#include <linux/bpf.h>
Yonghong Song699c86d2018-08-09 08:55:20 -07006#include <linux/btf.h>
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08007#include <linux/jhash.h>
8#include <linux/filter.h>
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08009#include <linux/rculist_nulls.h>
Daniel Borkmannc0203472018-08-22 23:49:37 +020010#include <linux/random.h>
Yonghong Song699c86d2018-08-09 08:55:20 -070011#include <uapi/linux/btf.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080012#include "percpu_freelist.h"
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080013#include "bpf_lru_list.h"
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070014#include "map_in_map.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080015
Chenbo Feng6e71b042017-10-18 13:00:22 -070016#define HTAB_CREATE_FLAG_MASK \
17 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
Daniel Borkmann591fe982019-04-09 23:20:05 +020018 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070019
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 Starovoitov8c290e62017-03-21 19:05:04 -070033 struct htab_elem *__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 */
Daniel Borkmannc0203472018-08-22 23:49:37 +020037 u32 hashrnd;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080038};
39
40/* each htab element is struct htab_elem + key + value */
41struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080042 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080043 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080044 struct {
45 void *padding;
46 union {
47 struct bpf_htab *htab;
48 struct pcpu_freelist_node fnode;
49 };
50 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080051 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070052 union {
53 struct rcu_head rcu;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080054 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070055 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080056 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080057 char key[0] __aligned(8);
58};
59
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080060static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
61
62static bool htab_is_lru(const struct bpf_htab *htab)
63{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080064 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
66}
67
68static bool htab_is_percpu(const struct bpf_htab *htab)
69{
70 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080072}
73
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070074static bool htab_is_prealloc(const struct bpf_htab *htab)
75{
76 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
77}
78
Alexei Starovoitov6c905982016-03-07 21:57:15 -080079static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
80 void __percpu *pptr)
81{
82 *(void __percpu **)(l->key + key_size) = pptr;
83}
84
85static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
86{
87 return *(void __percpu **)(l->key + key_size);
88}
89
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070090static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
91{
92 return *(void **)(l->key + roundup(map->key_size, 8));
93}
94
Alexei Starovoitov6c905982016-03-07 21:57:15 -080095static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
96{
97 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
98}
99
100static void htab_free_elems(struct bpf_htab *htab)
101{
102 int i;
103
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800104 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800105 goto free_elems;
106
107 for (i = 0; i < htab->map.max_entries; i++) {
108 void __percpu *pptr;
109
110 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
111 htab->map.key_size);
112 free_percpu(pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800113 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800114 }
115free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100116 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800117}
118
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800119static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
120 u32 hash)
121{
122 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
123 struct htab_elem *l;
124
125 if (node) {
126 l = container_of(node, struct htab_elem, lru_node);
127 memcpy(l->key, key, htab->map.key_size);
128 return l;
129 }
130
131 return NULL;
132}
133
134static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800135{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700136 u32 num_entries = htab->map.max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800137 int err = -ENOMEM, i;
138
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700139 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
140 num_entries += num_possible_cpus();
141
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700142 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
143 htab->map.numa_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800144 if (!htab->elems)
145 return -ENOMEM;
146
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800147 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800148 goto skip_percpu_elems;
149
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700150 for (i = 0; i < num_entries; i++) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800151 u32 size = round_up(htab->map.value_size, 8);
152 void __percpu *pptr;
153
154 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
155 if (!pptr)
156 goto free_elems;
157 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
158 pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800159 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800160 }
161
162skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800163 if (htab_is_lru(htab))
164 err = bpf_lru_init(&htab->lru,
165 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
166 offsetof(struct htab_elem, hash) -
167 offsetof(struct htab_elem, lru_node),
168 htab_lru_map_delete_node,
169 htab);
170 else
171 err = pcpu_freelist_init(&htab->freelist);
172
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800173 if (err)
174 goto free_elems;
175
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800176 if (htab_is_lru(htab))
177 bpf_lru_populate(&htab->lru, htab->elems,
178 offsetof(struct htab_elem, lru_node),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700179 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800180 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800181 pcpu_freelist_populate(&htab->freelist,
182 htab->elems + offsetof(struct htab_elem, fnode),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700183 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800184
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800185 return 0;
186
187free_elems:
188 htab_free_elems(htab);
189 return err;
190}
191
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800192static void prealloc_destroy(struct bpf_htab *htab)
193{
194 htab_free_elems(htab);
195
196 if (htab_is_lru(htab))
197 bpf_lru_destroy(&htab->lru);
198 else
199 pcpu_freelist_destroy(&htab->freelist);
200}
201
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700202static int alloc_extra_elems(struct bpf_htab *htab)
203{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700204 struct htab_elem *__percpu *pptr, *l_new;
205 struct pcpu_freelist_node *l;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700206 int cpu;
207
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700208 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
209 GFP_USER | __GFP_NOWARN);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700210 if (!pptr)
211 return -ENOMEM;
212
213 for_each_possible_cpu(cpu) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700214 l = pcpu_freelist_pop(&htab->freelist);
215 /* pop will succeed, since prealloc_init()
216 * preallocated extra num_possible_cpus elements
217 */
218 l_new = container_of(l, struct htab_elem, fnode);
219 *per_cpu_ptr(pptr, cpu) = l_new;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700220 }
221 htab->extra_elems = pptr;
222 return 0;
223}
224
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800225/* Called from syscall */
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800226static int htab_map_alloc_check(union bpf_attr *attr)
227{
228 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
229 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
230 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
231 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
232 /* percpu_lru means each cpu has its own LRU list.
233 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
234 * the map's value itself is percpu. percpu_lru has
235 * nothing to do with the map's value.
236 */
237 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
238 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000239 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800240 int numa_node = bpf_map_attr_numa_node(attr);
241
242 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
243 offsetof(struct htab_elem, hash_node.pprev));
244 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
245 offsetof(struct htab_elem, hash_node.pprev));
246
247 if (lru && !capable(CAP_SYS_ADMIN))
248 /* LRU implementation is much complicated than other
249 * maps. Hence, limit to CAP_SYS_ADMIN for now.
250 */
251 return -EPERM;
252
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000253 if (zero_seed && !capable(CAP_SYS_ADMIN))
254 /* Guard against local DoS, and discourage production use. */
255 return -EPERM;
256
Daniel Borkmann591fe982019-04-09 23:20:05 +0200257 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
258 !bpf_map_flags_access_ok(attr->map_flags))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800259 return -EINVAL;
260
261 if (!lru && percpu_lru)
262 return -EINVAL;
263
264 if (lru && !prealloc)
265 return -ENOTSUPP;
266
267 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
268 return -EINVAL;
269
270 /* check sanity of attributes.
271 * value_size == 0 may be allowed in the future to use map as a set
272 */
273 if (attr->max_entries == 0 || attr->key_size == 0 ||
274 attr->value_size == 0)
275 return -EINVAL;
276
277 if (attr->key_size > MAX_BPF_STACK)
278 /* eBPF programs initialize keys on stack, so they cannot be
279 * larger than max stack size
280 */
281 return -E2BIG;
282
283 if (attr->value_size >= KMALLOC_MAX_SIZE -
284 MAX_BPF_STACK - sizeof(struct htab_elem))
285 /* if value_size is bigger, the user space won't be able to
286 * access the elements via bpf syscall. This check also makes
287 * sure that the elem_size doesn't overflow and it's
288 * kmalloc-able later in htab_map_update_elem()
289 */
290 return -E2BIG;
291
292 return 0;
293}
294
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800295static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
296{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800297 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
298 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
299 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
300 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800301 /* percpu_lru means each cpu has its own LRU list.
302 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
303 * the map's value itself is percpu. percpu_lru has
304 * nothing to do with the map's value.
305 */
306 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
307 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800308 struct bpf_htab *htab;
309 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800310 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800311
312 htab = kzalloc(sizeof(*htab), GFP_USER);
313 if (!htab)
314 return ERR_PTR(-ENOMEM);
315
Jakub Kicinskibd475642018-01-11 20:29:06 -0800316 bpf_map_init_from_attr(&htab->map, attr);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800317
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800318 if (percpu_lru) {
319 /* ensure each CPU's lru list has >=1 elements.
320 * since we are at it, make each lru list has the same
321 * number of elements.
322 */
323 htab->map.max_entries = roundup(attr->max_entries,
324 num_possible_cpus());
325 if (htab->map.max_entries < attr->max_entries)
326 htab->map.max_entries = rounddown(attr->max_entries,
327 num_possible_cpus());
328 }
329
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800330 /* hash table size must be power of 2 */
331 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
332
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800333 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800334 round_up(htab->map.key_size, 8);
335 if (percpu)
336 htab->elem_size += sizeof(void *);
337 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800338 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800339
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800340 err = -E2BIG;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800341 /* prevent zero size kmalloc and check for u32 overflow */
342 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800343 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800344 goto free_htab;
345
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800346 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
347 (u64) htab->elem_size * htab->map.max_entries;
348
349 if (percpu)
350 cost += (u64) round_up(htab->map.value_size, 8) *
351 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700352 else
353 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800354
355 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800356 /* make sure page count doesn't overflow */
357 goto free_htab;
358
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800359 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800360
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800361 /* if map size is larger than memlock limit, reject it early */
362 err = bpf_map_precharge_memlock(htab->map.pages);
363 if (err)
364 goto free_htab;
365
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800366 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100367 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700368 sizeof(struct bucket),
369 htab->map.numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100370 if (!htab->buckets)
371 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800372
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000373 if (htab->map.map_flags & BPF_F_ZERO_SEED)
374 htab->hashrnd = 0;
375 else
376 htab->hashrnd = get_random_int();
377
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800378 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800379 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800380 raw_spin_lock_init(&htab->buckets[i].lock);
381 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800382
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800383 if (prealloc) {
384 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700385 if (err)
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700386 goto free_buckets;
387
388 if (!percpu && !lru) {
389 /* lru itself can remove the least used element, so
390 * there is no need for an extra elem during map_update.
391 */
392 err = alloc_extra_elems(htab);
393 if (err)
394 goto free_prealloc;
395 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700396 }
397
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800398 return &htab->map;
399
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700400free_prealloc:
401 prealloc_destroy(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800402free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100403 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800404free_htab:
405 kfree(htab);
406 return ERR_PTR(err);
407}
408
Daniel Borkmannc0203472018-08-22 23:49:37 +0200409static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800410{
Daniel Borkmannc0203472018-08-22 23:49:37 +0200411 return jhash(key, key_len, hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800412}
413
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800414static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800415{
416 return &htab->buckets[hash & (htab->n_buckets - 1)];
417}
418
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800419static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800420{
421 return &__select_bucket(htab, hash)->head;
422}
423
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800424/* this lookup function can only be called with bucket lock taken */
425static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800426 void *key, u32 key_size)
427{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800428 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800429 struct htab_elem *l;
430
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800431 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800432 if (l->hash == hash && !memcmp(&l->key, key, key_size))
433 return l;
434
435 return NULL;
436}
437
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800438/* can be called without bucket lock. it will repeat the loop in
439 * the unlikely event when elements moved from one bucket into another
440 * while link list is being walked
441 */
442static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
443 u32 hash, void *key,
444 u32 key_size, u32 n_buckets)
445{
446 struct hlist_nulls_node *n;
447 struct htab_elem *l;
448
449again:
450 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
451 if (l->hash == hash && !memcmp(&l->key, key, key_size))
452 return l;
453
454 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
455 goto again;
456
457 return NULL;
458}
459
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700460/* Called from syscall or from eBPF program directly, so
461 * arguments have to match bpf_map_lookup_elem() exactly.
462 * The return value is adjusted by BPF instructions
463 * in htab_map_gen_lookup().
464 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800465static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800466{
467 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800468 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800469 struct htab_elem *l;
470 u32 hash, key_size;
471
472 /* Must be called with rcu_read_lock. */
473 WARN_ON_ONCE(!rcu_read_lock_held());
474
475 key_size = map->key_size;
476
Daniel Borkmannc0203472018-08-22 23:49:37 +0200477 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800478
479 head = select_bucket(htab, hash);
480
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800481 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800482
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800483 return l;
484}
485
486static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
487{
488 struct htab_elem *l = __htab_map_lookup_elem(map, key);
489
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800490 if (l)
491 return l->key + round_up(map->key_size, 8);
492
493 return NULL;
494}
495
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700496/* inline bpf_map_lookup_elem() call.
497 * Instead of:
498 * bpf_prog
499 * bpf_map_lookup_elem
500 * map->ops->map_lookup_elem
501 * htab_map_lookup_elem
502 * __htab_map_lookup_elem
503 * do:
504 * bpf_prog
505 * __htab_map_lookup_elem
506 */
507static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
508{
509 struct bpf_insn *insn = insn_buf;
510 const int ret = BPF_REG_0;
511
Daniel Borkmann09772d92018-06-02 23:06:35 +0200512 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
513 (void *(*)(struct bpf_map *map, void *key))NULL));
514 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700515 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
516 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
517 offsetof(struct htab_elem, key) +
518 round_up(map->key_size, 8));
519 return insn - insn_buf;
520}
521
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200522static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
523 void *key, const bool mark)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800524{
525 struct htab_elem *l = __htab_map_lookup_elem(map, key);
526
527 if (l) {
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200528 if (mark)
529 bpf_lru_node_set_ref(&l->lru_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800530 return l->key + round_up(map->key_size, 8);
531 }
532
533 return NULL;
534}
535
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200536static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
537{
538 return __htab_lru_map_lookup_elem(map, key, true);
539}
540
541static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
542{
543 return __htab_lru_map_lookup_elem(map, key, false);
544}
545
Martin KaFai Laucc555422017-08-31 23:27:12 -0700546static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
547 struct bpf_insn *insn_buf)
548{
549 struct bpf_insn *insn = insn_buf;
550 const int ret = BPF_REG_0;
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700551 const int ref_reg = BPF_REG_1;
Martin KaFai Laucc555422017-08-31 23:27:12 -0700552
Daniel Borkmann09772d92018-06-02 23:06:35 +0200553 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
554 (void *(*)(struct bpf_map *map, void *key))NULL));
555 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700556 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
557 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
558 offsetof(struct htab_elem, lru_node) +
559 offsetof(struct bpf_lru_node, ref));
560 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
Martin KaFai Laucc555422017-08-31 23:27:12 -0700561 *insn++ = BPF_ST_MEM(BPF_B, ret,
562 offsetof(struct htab_elem, lru_node) +
563 offsetof(struct bpf_lru_node, ref),
564 1);
565 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
566 offsetof(struct htab_elem, key) +
567 round_up(map->key_size, 8));
568 return insn - insn_buf;
569}
570
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800571/* It is called from the bpf_lru_list when the LRU needs to delete
572 * older elements from the htab.
573 */
574static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
575{
576 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800577 struct htab_elem *l = NULL, *tgt_l;
578 struct hlist_nulls_head *head;
579 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800580 unsigned long flags;
581 struct bucket *b;
582
583 tgt_l = container_of(node, struct htab_elem, lru_node);
584 b = __select_bucket(htab, tgt_l->hash);
585 head = &b->head;
586
587 raw_spin_lock_irqsave(&b->lock, flags);
588
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800589 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800590 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800591 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800592 break;
593 }
594
595 raw_spin_unlock_irqrestore(&b->lock, flags);
596
597 return l == tgt_l;
598}
599
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800600/* Called from syscall */
601static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
602{
603 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800604 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800605 struct htab_elem *l, *next_l;
606 u32 hash, key_size;
Teng Qin8fe45922017-04-24 19:00:37 -0700607 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800608
609 WARN_ON_ONCE(!rcu_read_lock_held());
610
611 key_size = map->key_size;
612
Teng Qin8fe45922017-04-24 19:00:37 -0700613 if (!key)
614 goto find_first_elem;
615
Daniel Borkmannc0203472018-08-22 23:49:37 +0200616 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800617
618 head = select_bucket(htab, hash);
619
620 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800621 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800622
Teng Qin8fe45922017-04-24 19:00:37 -0700623 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800624 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800625
626 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800627 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800628 struct htab_elem, hash_node);
629
630 if (next_l) {
631 /* if next elem in this hash list is non-zero, just return it */
632 memcpy(next_key, next_l->key, key_size);
633 return 0;
634 }
635
636 /* no more elements in this hash list, go to the next bucket */
637 i = hash & (htab->n_buckets - 1);
638 i++;
639
640find_first_elem:
641 /* iterate over buckets */
642 for (; i < htab->n_buckets; i++) {
643 head = select_bucket(htab, i);
644
645 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800646 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800647 struct htab_elem, hash_node);
648 if (next_l) {
649 /* if it's not empty, just return it */
650 memcpy(next_key, next_l->key, key_size);
651 return 0;
652 }
653 }
654
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800655 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800656 return -ENOENT;
657}
658
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800659static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800660{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800661 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
662 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800663 kfree(l);
664}
665
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800666static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800667{
668 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800669 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800670
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800671 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
672 * we're calling kfree, otherwise deadlock is possible if kprobes
673 * are placed somewhere inside of slub
674 */
675 preempt_disable();
676 __this_cpu_inc(bpf_prog_active);
677 htab_elem_free(htab, l);
678 __this_cpu_dec(bpf_prog_active);
679 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800680}
681
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800682static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800683{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700684 struct bpf_map *map = &htab->map;
685
686 if (map->ops->map_fd_put_ptr) {
687 void *ptr = fd_htab_map_get_ptr(map, l);
688
689 map->ops->map_fd_put_ptr(ptr);
690 }
691
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700692 if (htab_is_prealloc(htab)) {
Alexei Starovoitova89fac52019-01-30 18:12:43 -0800693 __pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800694 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800695 atomic_dec(&htab->count);
696 l->htab = htab;
697 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800698 }
699}
700
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800701static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
702 void *value, bool onallcpus)
703{
704 if (!onallcpus) {
705 /* copy true value_size bytes */
706 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
707 } else {
708 u32 size = round_up(htab->map.value_size, 8);
709 int off = 0, cpu;
710
711 for_each_possible_cpu(cpu) {
712 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
713 value + off, size);
714 off += size;
715 }
716 }
717}
718
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200719static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
720{
721 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
722 BITS_PER_LONG == 64;
723}
724
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800725static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
726 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700727 bool percpu, bool onallcpus,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700728 struct htab_elem *old_elem)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800729{
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800730 u32 size = htab->map.value_size;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700731 bool prealloc = htab_is_prealloc(htab);
732 struct htab_elem *l_new, **pl_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800733 void __percpu *pptr;
734
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800735 if (prealloc) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700736 if (old_elem) {
737 /* if we're updating the existing element,
738 * use per-cpu extra elems to avoid freelist_pop/push
739 */
740 pl_new = this_cpu_ptr(htab->extra_elems);
741 l_new = *pl_new;
742 *pl_new = old_elem;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700743 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700744 struct pcpu_freelist_node *l;
745
Alexei Starovoitova89fac52019-01-30 18:12:43 -0800746 l = __pcpu_freelist_pop(&htab->freelist);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700747 if (!l)
748 return ERR_PTR(-E2BIG);
749 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800750 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700751 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700752 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
753 if (!old_elem) {
754 /* when map is full and update() is replacing
755 * old element, it's ok to allocate, since
756 * old element will be freed immediately.
757 * Otherwise return an error
758 */
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200759 l_new = ERR_PTR(-E2BIG);
760 goto dec_count;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700761 }
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700762 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
763 htab->map.numa_node);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200764 if (!l_new) {
765 l_new = ERR_PTR(-ENOMEM);
766 goto dec_count;
767 }
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800768 check_and_init_map_lock(&htab->map,
769 l_new->key + round_up(key_size, 8));
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800770 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800771
772 memcpy(l_new->key, key, key_size);
773 if (percpu) {
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800774 size = round_up(size, 8);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800775 if (prealloc) {
776 pptr = htab_elem_get_ptr(l_new, key_size);
777 } else {
778 /* alloc_percpu zero-fills */
779 pptr = __alloc_percpu_gfp(size, 8,
780 GFP_ATOMIC | __GFP_NOWARN);
781 if (!pptr) {
782 kfree(l_new);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200783 l_new = ERR_PTR(-ENOMEM);
784 goto dec_count;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800785 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800786 }
787
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800788 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800789
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800790 if (!prealloc)
791 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800792 } else if (fd_htab_map_needs_adjust(htab)) {
793 size = round_up(size, 8);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800794 memcpy(l_new->key + round_up(key_size, 8), value, size);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800795 } else {
796 copy_map_value(&htab->map,
797 l_new->key + round_up(key_size, 8),
798 value);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800799 }
800
801 l_new->hash = hash;
802 return l_new;
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200803dec_count:
804 atomic_dec(&htab->count);
805 return l_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800806}
807
808static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
809 u64 map_flags)
810{
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800811 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800812 /* elem already exists */
813 return -EEXIST;
814
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800815 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800816 /* elem doesn't exist, cannot update it */
817 return -ENOENT;
818
819 return 0;
820}
821
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800822/* Called from syscall or from eBPF program */
823static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
824 u64 map_flags)
825{
826 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800827 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800828 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800829 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800830 struct bucket *b;
831 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800832 int ret;
833
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800834 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800835 /* unknown flags */
836 return -EINVAL;
837
838 WARN_ON_ONCE(!rcu_read_lock_held());
839
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800840 key_size = map->key_size;
841
Daniel Borkmannc0203472018-08-22 23:49:37 +0200842 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800843
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800844 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800845 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800846
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800847 if (unlikely(map_flags & BPF_F_LOCK)) {
848 if (unlikely(!map_value_has_spin_lock(map)))
849 return -EINVAL;
850 /* find an element without taking the bucket lock */
851 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
852 htab->n_buckets);
853 ret = check_flags(htab, l_old, map_flags);
854 if (ret)
855 return ret;
856 if (l_old) {
857 /* grab the element lock and update value in place */
858 copy_map_value_locked(map,
859 l_old->key + round_up(key_size, 8),
860 value, false);
861 return 0;
862 }
863 /* fall through, grab the bucket lock and lookup again.
864 * 99.9% chance that the element won't be found,
865 * but second lookup under lock has to be done.
866 */
867 }
868
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800869 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800870 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800871
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800872 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800873
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800874 ret = check_flags(htab, l_old, map_flags);
875 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800876 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800877
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800878 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
879 /* first lookup without the bucket lock didn't find the element,
880 * but second lookup with the bucket lock found it.
881 * This case is highly unlikely, but has to be dealt with:
882 * grab the element lock in addition to the bucket lock
883 * and update element in place
884 */
885 copy_map_value_locked(map,
886 l_old->key + round_up(key_size, 8),
887 value, false);
888 ret = 0;
889 goto err;
890 }
891
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700892 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700893 l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800894 if (IS_ERR(l_new)) {
895 /* all pre-allocated elements are in use or memory exhausted */
896 ret = PTR_ERR(l_new);
897 goto err;
898 }
899
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800900 /* add new element to the head of the list, so that
901 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800902 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800903 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800904 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800905 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700906 if (!htab_is_prealloc(htab))
907 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800908 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800909 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800910err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800911 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800912 return ret;
913}
914
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800915static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
916 u64 map_flags)
917{
918 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
919 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800920 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -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
Daniel Borkmannc0203472018-08-22 23:49:37 +0200934 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800935
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 getting free nodes from LRU may need
941 * to remove older elements from htab and this removal
942 * operation will need a bucket lock.
943 */
944 l_new = prealloc_lru_pop(htab, key, hash);
945 if (!l_new)
946 return -ENOMEM;
947 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
948
949 /* bpf_map_update_elem() can be called in_irq() */
950 raw_spin_lock_irqsave(&b->lock, flags);
951
952 l_old = lookup_elem_raw(head, hash, key, key_size);
953
954 ret = check_flags(htab, l_old, map_flags);
955 if (ret)
956 goto err;
957
958 /* add new element to the head of the list, so that
959 * concurrent search will find it before old elem
960 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800961 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800962 if (l_old) {
963 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800964 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800965 }
966 ret = 0;
967
968err:
969 raw_spin_unlock_irqrestore(&b->lock, flags);
970
971 if (ret)
972 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
973 else if (l_old)
974 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
975
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 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800982{
983 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
984 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800985 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800986 unsigned long flags;
987 struct bucket *b;
988 u32 key_size, hash;
989 int ret;
990
991 if (unlikely(map_flags > BPF_EXIST))
992 /* unknown flags */
993 return -EINVAL;
994
995 WARN_ON_ONCE(!rcu_read_lock_held());
996
997 key_size = map->key_size;
998
Daniel Borkmannc0203472018-08-22 23:49:37 +0200999 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001000
1001 b = __select_bucket(htab, hash);
1002 head = &b->head;
1003
1004 /* bpf_map_update_elem() can be called in_irq() */
1005 raw_spin_lock_irqsave(&b->lock, flags);
1006
1007 l_old = lookup_elem_raw(head, hash, key, key_size);
1008
1009 ret = check_flags(htab, l_old, map_flags);
1010 if (ret)
1011 goto err;
1012
1013 if (l_old) {
1014 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -08001015 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1016 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001017 } else {
1018 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001019 hash, true, onallcpus, NULL);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001020 if (IS_ERR(l_new)) {
1021 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001022 goto err;
1023 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001024 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001025 }
1026 ret = 0;
1027err:
1028 raw_spin_unlock_irqrestore(&b->lock, flags);
1029 return ret;
1030}
1031
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001032static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1033 void *value, u64 map_flags,
1034 bool onallcpus)
1035{
1036 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1037 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001038 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001039 unsigned long flags;
1040 struct bucket *b;
1041 u32 key_size, hash;
1042 int ret;
1043
1044 if (unlikely(map_flags > BPF_EXIST))
1045 /* unknown flags */
1046 return -EINVAL;
1047
1048 WARN_ON_ONCE(!rcu_read_lock_held());
1049
1050 key_size = map->key_size;
1051
Daniel Borkmannc0203472018-08-22 23:49:37 +02001052 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001053
1054 b = __select_bucket(htab, hash);
1055 head = &b->head;
1056
1057 /* For LRU, we need to alloc before taking bucket's
1058 * spinlock because LRU's elem alloc may need
1059 * to remove older elem from htab and this removal
1060 * operation will need a bucket lock.
1061 */
1062 if (map_flags != BPF_EXIST) {
1063 l_new = prealloc_lru_pop(htab, key, hash);
1064 if (!l_new)
1065 return -ENOMEM;
1066 }
1067
1068 /* bpf_map_update_elem() can be called in_irq() */
1069 raw_spin_lock_irqsave(&b->lock, flags);
1070
1071 l_old = lookup_elem_raw(head, hash, key, key_size);
1072
1073 ret = check_flags(htab, l_old, map_flags);
1074 if (ret)
1075 goto err;
1076
1077 if (l_old) {
1078 bpf_lru_node_set_ref(&l_old->lru_node);
1079
1080 /* per-cpu hash map can update value in-place */
1081 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1082 value, onallcpus);
1083 } else {
1084 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1085 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001086 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001087 l_new = NULL;
1088 }
1089 ret = 0;
1090err:
1091 raw_spin_unlock_irqrestore(&b->lock, flags);
1092 if (l_new)
1093 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1094 return ret;
1095}
1096
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001097static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1098 void *value, u64 map_flags)
1099{
1100 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1101}
1102
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001103static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1104 void *value, u64 map_flags)
1105{
1106 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1107 false);
1108}
1109
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001110/* Called from syscall or from eBPF program */
1111static int htab_map_delete_elem(struct bpf_map *map, void *key)
1112{
1113 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001114 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001115 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001116 struct htab_elem *l;
1117 unsigned long flags;
1118 u32 hash, key_size;
1119 int ret = -ENOENT;
1120
1121 WARN_ON_ONCE(!rcu_read_lock_held());
1122
1123 key_size = map->key_size;
1124
Daniel Borkmannc0203472018-08-22 23:49:37 +02001125 hash = htab_map_hash(key, key_size, htab->hashrnd);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001126 b = __select_bucket(htab, hash);
1127 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001128
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001129 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001130
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001131 l = lookup_elem_raw(head, hash, key, key_size);
1132
1133 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001134 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001135 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001136 ret = 0;
1137 }
1138
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001139 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001140 return ret;
1141}
1142
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001143static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1144{
1145 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001146 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001147 struct bucket *b;
1148 struct htab_elem *l;
1149 unsigned long flags;
1150 u32 hash, key_size;
1151 int ret = -ENOENT;
1152
1153 WARN_ON_ONCE(!rcu_read_lock_held());
1154
1155 key_size = map->key_size;
1156
Daniel Borkmannc0203472018-08-22 23:49:37 +02001157 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001158 b = __select_bucket(htab, hash);
1159 head = &b->head;
1160
1161 raw_spin_lock_irqsave(&b->lock, flags);
1162
1163 l = lookup_elem_raw(head, hash, key, key_size);
1164
1165 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001166 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001167 ret = 0;
1168 }
1169
1170 raw_spin_unlock_irqrestore(&b->lock, flags);
1171 if (l)
1172 bpf_lru_push_free(&htab->lru, &l->lru_node);
1173 return ret;
1174}
1175
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001176static void delete_all_elements(struct bpf_htab *htab)
1177{
1178 int i;
1179
1180 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001181 struct hlist_nulls_head *head = select_bucket(htab, i);
1182 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001183 struct htab_elem *l;
1184
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001185 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1186 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001187 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001188 }
1189 }
1190}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001191
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001192/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1193static void htab_map_free(struct bpf_map *map)
1194{
1195 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1196
1197 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1198 * so the programs (can be more than one that used this map) were
1199 * disconnected from events. Wait for outstanding critical sections in
1200 * these programs to complete
1201 */
1202 synchronize_rcu();
1203
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001204 /* some of free_htab_elem() callbacks for elements of this map may
1205 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001206 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001207 rcu_barrier();
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001208 if (!htab_is_prealloc(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001209 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001210 else
1211 prealloc_destroy(htab);
1212
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001213 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001214 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001215 kfree(htab);
1216}
1217
Yonghong Song699c86d2018-08-09 08:55:20 -07001218static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1219 struct seq_file *m)
1220{
1221 void *value;
1222
1223 rcu_read_lock();
1224
1225 value = htab_map_lookup_elem(map, key);
1226 if (!value) {
1227 rcu_read_unlock();
1228 return;
1229 }
1230
1231 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1232 seq_puts(m, ": ");
1233 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1234 seq_puts(m, "\n");
1235
1236 rcu_read_unlock();
1237}
1238
Johannes Berg40077e02017-04-11 15:34:58 +02001239const struct bpf_map_ops htab_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001240 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001241 .map_alloc = htab_map_alloc,
1242 .map_free = htab_map_free,
1243 .map_get_next_key = htab_map_get_next_key,
1244 .map_lookup_elem = htab_map_lookup_elem,
1245 .map_update_elem = htab_map_update_elem,
1246 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001247 .map_gen_lookup = htab_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001248 .map_seq_show_elem = htab_map_seq_show_elem,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001249};
1250
Johannes Berg40077e02017-04-11 15:34:58 +02001251const struct bpf_map_ops htab_lru_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001252 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001253 .map_alloc = htab_map_alloc,
1254 .map_free = htab_map_free,
1255 .map_get_next_key = htab_map_get_next_key,
1256 .map_lookup_elem = htab_lru_map_lookup_elem,
Daniel Borkmann50b045a2019-05-14 01:18:56 +02001257 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001258 .map_update_elem = htab_lru_map_update_elem,
1259 .map_delete_elem = htab_lru_map_delete_elem,
Martin KaFai Laucc555422017-08-31 23:27:12 -07001260 .map_gen_lookup = htab_lru_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001261 .map_seq_show_elem = htab_map_seq_show_elem,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001262};
1263
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001264/* Called from eBPF program */
1265static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1266{
1267 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1268
1269 if (l)
1270 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1271 else
1272 return NULL;
1273}
1274
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001275static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1276{
1277 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1278
1279 if (l) {
1280 bpf_lru_node_set_ref(&l->lru_node);
1281 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1282 }
1283
1284 return NULL;
1285}
1286
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001287int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1288{
1289 struct htab_elem *l;
1290 void __percpu *pptr;
1291 int ret = -ENOENT;
1292 int cpu, off = 0;
1293 u32 size;
1294
1295 /* per_cpu areas are zero-filled and bpf programs can only
1296 * access 'value_size' of them, so copying rounded areas
1297 * will not leak any kernel data
1298 */
1299 size = round_up(map->value_size, 8);
1300 rcu_read_lock();
1301 l = __htab_map_lookup_elem(map, key);
1302 if (!l)
1303 goto out;
Daniel Borkmann50b045a2019-05-14 01:18:56 +02001304 /* We do not mark LRU map element here in order to not mess up
1305 * eviction heuristics when user space does a map walk.
1306 */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001307 pptr = htab_elem_get_ptr(l, map->key_size);
1308 for_each_possible_cpu(cpu) {
1309 bpf_long_memcpy(value + off,
1310 per_cpu_ptr(pptr, cpu), size);
1311 off += size;
1312 }
1313 ret = 0;
1314out:
1315 rcu_read_unlock();
1316 return ret;
1317}
1318
1319int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1320 u64 map_flags)
1321{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001322 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001323 int ret;
1324
1325 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001326 if (htab_is_lru(htab))
1327 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1328 map_flags, true);
1329 else
1330 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1331 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001332 rcu_read_unlock();
1333
1334 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001335}
1336
Yonghong Songc7b27c32018-08-29 14:43:13 -07001337static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1338 struct seq_file *m)
1339{
1340 struct htab_elem *l;
1341 void __percpu *pptr;
1342 int cpu;
1343
1344 rcu_read_lock();
1345
1346 l = __htab_map_lookup_elem(map, key);
1347 if (!l) {
1348 rcu_read_unlock();
1349 return;
1350 }
1351
1352 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1353 seq_puts(m, ": {\n");
1354 pptr = htab_elem_get_ptr(l, map->key_size);
1355 for_each_possible_cpu(cpu) {
1356 seq_printf(m, "\tcpu%d: ", cpu);
1357 btf_type_seq_show(map->btf, map->btf_value_type_id,
1358 per_cpu_ptr(pptr, cpu), m);
1359 seq_puts(m, "\n");
1360 }
1361 seq_puts(m, "}\n");
1362
1363 rcu_read_unlock();
1364}
1365
Johannes Berg40077e02017-04-11 15:34:58 +02001366const struct bpf_map_ops htab_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001367 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001368 .map_alloc = htab_map_alloc,
1369 .map_free = htab_map_free,
1370 .map_get_next_key = htab_map_get_next_key,
1371 .map_lookup_elem = htab_percpu_map_lookup_elem,
1372 .map_update_elem = htab_percpu_map_update_elem,
1373 .map_delete_elem = htab_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001374 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001375};
1376
Johannes Berg40077e02017-04-11 15:34:58 +02001377const struct bpf_map_ops htab_lru_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001378 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001379 .map_alloc = htab_map_alloc,
1380 .map_free = htab_map_free,
1381 .map_get_next_key = htab_map_get_next_key,
1382 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1383 .map_update_elem = htab_lru_percpu_map_update_elem,
1384 .map_delete_elem = htab_lru_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001385 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001386};
1387
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001388static int fd_htab_map_alloc_check(union bpf_attr *attr)
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001389{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001390 if (attr->value_size != sizeof(u32))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001391 return -EINVAL;
1392 return htab_map_alloc_check(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001393}
1394
1395static void fd_htab_map_free(struct bpf_map *map)
1396{
1397 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1398 struct hlist_nulls_node *n;
1399 struct hlist_nulls_head *head;
1400 struct htab_elem *l;
1401 int i;
1402
1403 for (i = 0; i < htab->n_buckets; i++) {
1404 head = select_bucket(htab, i);
1405
1406 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1407 void *ptr = fd_htab_map_get_ptr(map, l);
1408
1409 map->ops->map_fd_put_ptr(ptr);
1410 }
1411 }
1412
1413 htab_map_free(map);
1414}
1415
1416/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001417int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1418{
1419 void **ptr;
1420 int ret = 0;
1421
1422 if (!map->ops->map_fd_sys_lookup_elem)
1423 return -ENOTSUPP;
1424
1425 rcu_read_lock();
1426 ptr = htab_map_lookup_elem(map, key);
1427 if (ptr)
1428 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1429 else
1430 ret = -ENOENT;
1431 rcu_read_unlock();
1432
1433 return ret;
1434}
1435
1436/* only called from syscall */
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001437int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1438 void *key, void *value, u64 map_flags)
1439{
1440 void *ptr;
1441 int ret;
1442 u32 ufd = *(u32 *)value;
1443
1444 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1445 if (IS_ERR(ptr))
1446 return PTR_ERR(ptr);
1447
1448 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1449 if (ret)
1450 map->ops->map_fd_put_ptr(ptr);
1451
1452 return ret;
1453}
1454
1455static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1456{
1457 struct bpf_map *map, *inner_map_meta;
1458
1459 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1460 if (IS_ERR(inner_map_meta))
1461 return inner_map_meta;
1462
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001463 map = htab_map_alloc(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001464 if (IS_ERR(map)) {
1465 bpf_map_meta_free(inner_map_meta);
1466 return map;
1467 }
1468
1469 map->inner_map_meta = inner_map_meta;
1470
1471 return map;
1472}
1473
1474static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1475{
1476 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1477
1478 if (!inner_map)
1479 return NULL;
1480
1481 return READ_ONCE(*inner_map);
1482}
1483
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001484static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1485 struct bpf_insn *insn_buf)
1486{
1487 struct bpf_insn *insn = insn_buf;
1488 const int ret = BPF_REG_0;
1489
Daniel Borkmann09772d92018-06-02 23:06:35 +02001490 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1491 (void *(*)(struct bpf_map *map, void *key))NULL));
1492 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001493 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1494 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1495 offsetof(struct htab_elem, key) +
1496 round_up(map->key_size, 8));
1497 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1498
1499 return insn - insn_buf;
1500}
1501
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001502static void htab_of_map_free(struct bpf_map *map)
1503{
1504 bpf_map_meta_free(map->inner_map_meta);
1505 fd_htab_map_free(map);
1506}
1507
Johannes Berg40077e02017-04-11 15:34:58 +02001508const struct bpf_map_ops htab_of_maps_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001509 .map_alloc_check = fd_htab_map_alloc_check,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001510 .map_alloc = htab_of_map_alloc,
1511 .map_free = htab_of_map_free,
1512 .map_get_next_key = htab_map_get_next_key,
1513 .map_lookup_elem = htab_of_map_lookup_elem,
1514 .map_delete_elem = htab_map_delete_elem,
1515 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1516 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001517 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001518 .map_gen_lookup = htab_of_map_gen_lookup,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001519 .map_check_btf = map_check_no_btf,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001520};