blob: 0f2708fde5f7b162ca5bf7b1da2945415a4ba67b [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>
Yonghong Song699c86d2018-08-09 08:55:20 -070014#include <linux/btf.h>
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080015#include <linux/jhash.h>
16#include <linux/filter.h>
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080017#include <linux/rculist_nulls.h>
Daniel Borkmannc0203472018-08-22 23:49:37 +020018#include <linux/random.h>
Yonghong Song699c86d2018-08-09 08:55:20 -070019#include <uapi/linux/btf.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080020#include "percpu_freelist.h"
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080021#include "bpf_lru_list.h"
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070022#include "map_in_map.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080023
Chenbo Feng6e71b042017-10-18 13:00:22 -070024#define HTAB_CREATE_FLAG_MASK \
25 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
Daniel Borkmann591fe982019-04-09 23:20:05 +020026 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070027
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080028struct bucket {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080029 struct hlist_nulls_head head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080030 raw_spinlock_t lock;
31};
32
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080033struct bpf_htab {
34 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080035 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080036 void *elems;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080037 union {
38 struct pcpu_freelist freelist;
39 struct bpf_lru lru;
40 };
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070041 struct htab_elem *__percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080042 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080043 u32 n_buckets; /* number of hash buckets */
44 u32 elem_size; /* size of each element in bytes */
Daniel Borkmannc0203472018-08-22 23:49:37 +020045 u32 hashrnd;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080046};
47
48/* each htab element is struct htab_elem + key + value */
49struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080050 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080051 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080052 struct {
53 void *padding;
54 union {
55 struct bpf_htab *htab;
56 struct pcpu_freelist_node fnode;
57 };
58 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080059 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070060 union {
61 struct rcu_head rcu;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080062 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070063 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080064 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080065 char key[0] __aligned(8);
66};
67
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080068static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
69
70static bool htab_is_lru(const struct bpf_htab *htab)
71{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080072 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
73 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
74}
75
76static bool htab_is_percpu(const struct bpf_htab *htab)
77{
78 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
79 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080080}
81
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070082static bool htab_is_prealloc(const struct bpf_htab *htab)
83{
84 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
85}
86
Alexei Starovoitov6c905982016-03-07 21:57:15 -080087static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
88 void __percpu *pptr)
89{
90 *(void __percpu **)(l->key + key_size) = pptr;
91}
92
93static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
94{
95 return *(void __percpu **)(l->key + key_size);
96}
97
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070098static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
99{
100 return *(void **)(l->key + roundup(map->key_size, 8));
101}
102
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800103static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
104{
105 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
106}
107
108static void htab_free_elems(struct bpf_htab *htab)
109{
110 int i;
111
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800112 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800113 goto free_elems;
114
115 for (i = 0; i < htab->map.max_entries; i++) {
116 void __percpu *pptr;
117
118 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
119 htab->map.key_size);
120 free_percpu(pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800121 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800122 }
123free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100124 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800125}
126
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800127static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
128 u32 hash)
129{
130 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
131 struct htab_elem *l;
132
133 if (node) {
134 l = container_of(node, struct htab_elem, lru_node);
135 memcpy(l->key, key, htab->map.key_size);
136 return l;
137 }
138
139 return NULL;
140}
141
142static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800143{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700144 u32 num_entries = htab->map.max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800145 int err = -ENOMEM, i;
146
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700147 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
148 num_entries += num_possible_cpus();
149
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700150 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
151 htab->map.numa_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800152 if (!htab->elems)
153 return -ENOMEM;
154
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800155 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800156 goto skip_percpu_elems;
157
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700158 for (i = 0; i < num_entries; i++) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800159 u32 size = round_up(htab->map.value_size, 8);
160 void __percpu *pptr;
161
162 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
163 if (!pptr)
164 goto free_elems;
165 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
166 pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800167 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800168 }
169
170skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800171 if (htab_is_lru(htab))
172 err = bpf_lru_init(&htab->lru,
173 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
174 offsetof(struct htab_elem, hash) -
175 offsetof(struct htab_elem, lru_node),
176 htab_lru_map_delete_node,
177 htab);
178 else
179 err = pcpu_freelist_init(&htab->freelist);
180
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800181 if (err)
182 goto free_elems;
183
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800184 if (htab_is_lru(htab))
185 bpf_lru_populate(&htab->lru, htab->elems,
186 offsetof(struct htab_elem, lru_node),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700187 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800188 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800189 pcpu_freelist_populate(&htab->freelist,
190 htab->elems + offsetof(struct htab_elem, fnode),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700191 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800192
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800193 return 0;
194
195free_elems:
196 htab_free_elems(htab);
197 return err;
198}
199
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800200static void prealloc_destroy(struct bpf_htab *htab)
201{
202 htab_free_elems(htab);
203
204 if (htab_is_lru(htab))
205 bpf_lru_destroy(&htab->lru);
206 else
207 pcpu_freelist_destroy(&htab->freelist);
208}
209
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700210static int alloc_extra_elems(struct bpf_htab *htab)
211{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700212 struct htab_elem *__percpu *pptr, *l_new;
213 struct pcpu_freelist_node *l;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700214 int cpu;
215
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700216 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
217 GFP_USER | __GFP_NOWARN);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700218 if (!pptr)
219 return -ENOMEM;
220
221 for_each_possible_cpu(cpu) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700222 l = pcpu_freelist_pop(&htab->freelist);
223 /* pop will succeed, since prealloc_init()
224 * preallocated extra num_possible_cpus elements
225 */
226 l_new = container_of(l, struct htab_elem, fnode);
227 *per_cpu_ptr(pptr, cpu) = l_new;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700228 }
229 htab->extra_elems = pptr;
230 return 0;
231}
232
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800233/* Called from syscall */
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800234static int htab_map_alloc_check(union bpf_attr *attr)
235{
236 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
237 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
238 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
239 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
240 /* percpu_lru means each cpu has its own LRU list.
241 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
242 * the map's value itself is percpu. percpu_lru has
243 * nothing to do with the map's value.
244 */
245 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
246 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000247 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800248 int numa_node = bpf_map_attr_numa_node(attr);
249
250 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
251 offsetof(struct htab_elem, hash_node.pprev));
252 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
253 offsetof(struct htab_elem, hash_node.pprev));
254
255 if (lru && !capable(CAP_SYS_ADMIN))
256 /* LRU implementation is much complicated than other
257 * maps. Hence, limit to CAP_SYS_ADMIN for now.
258 */
259 return -EPERM;
260
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000261 if (zero_seed && !capable(CAP_SYS_ADMIN))
262 /* Guard against local DoS, and discourage production use. */
263 return -EPERM;
264
Daniel Borkmann591fe982019-04-09 23:20:05 +0200265 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
266 !bpf_map_flags_access_ok(attr->map_flags))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800267 return -EINVAL;
268
269 if (!lru && percpu_lru)
270 return -EINVAL;
271
272 if (lru && !prealloc)
273 return -ENOTSUPP;
274
275 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
276 return -EINVAL;
277
278 /* check sanity of attributes.
279 * value_size == 0 may be allowed in the future to use map as a set
280 */
281 if (attr->max_entries == 0 || attr->key_size == 0 ||
282 attr->value_size == 0)
283 return -EINVAL;
284
285 if (attr->key_size > MAX_BPF_STACK)
286 /* eBPF programs initialize keys on stack, so they cannot be
287 * larger than max stack size
288 */
289 return -E2BIG;
290
291 if (attr->value_size >= KMALLOC_MAX_SIZE -
292 MAX_BPF_STACK - sizeof(struct htab_elem))
293 /* if value_size is bigger, the user space won't be able to
294 * access the elements via bpf syscall. This check also makes
295 * sure that the elem_size doesn't overflow and it's
296 * kmalloc-able later in htab_map_update_elem()
297 */
298 return -E2BIG;
299
300 return 0;
301}
302
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800303static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
304{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800305 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
306 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
307 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
308 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800309 /* percpu_lru means each cpu has its own LRU list.
310 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
311 * the map's value itself is percpu. percpu_lru has
312 * nothing to do with the map's value.
313 */
314 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
315 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800316 struct bpf_htab *htab;
317 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800318 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800319
320 htab = kzalloc(sizeof(*htab), GFP_USER);
321 if (!htab)
322 return ERR_PTR(-ENOMEM);
323
Jakub Kicinskibd475642018-01-11 20:29:06 -0800324 bpf_map_init_from_attr(&htab->map, attr);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800325
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800326 if (percpu_lru) {
327 /* ensure each CPU's lru list has >=1 elements.
328 * since we are at it, make each lru list has the same
329 * number of elements.
330 */
331 htab->map.max_entries = roundup(attr->max_entries,
332 num_possible_cpus());
333 if (htab->map.max_entries < attr->max_entries)
334 htab->map.max_entries = rounddown(attr->max_entries,
335 num_possible_cpus());
336 }
337
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800338 /* hash table size must be power of 2 */
339 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
340
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800341 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800342 round_up(htab->map.key_size, 8);
343 if (percpu)
344 htab->elem_size += sizeof(void *);
345 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800346 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800347
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800348 err = -E2BIG;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800349 /* prevent zero size kmalloc and check for u32 overflow */
350 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800351 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800352 goto free_htab;
353
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800354 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
355 (u64) htab->elem_size * htab->map.max_entries;
356
357 if (percpu)
358 cost += (u64) round_up(htab->map.value_size, 8) *
359 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700360 else
361 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800362
363 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800364 /* make sure page count doesn't overflow */
365 goto free_htab;
366
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800367 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800368
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800369 /* if map size is larger than memlock limit, reject it early */
370 err = bpf_map_precharge_memlock(htab->map.pages);
371 if (err)
372 goto free_htab;
373
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800374 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100375 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700376 sizeof(struct bucket),
377 htab->map.numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100378 if (!htab->buckets)
379 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800380
Lorenz Bauer96b3b6c2018-11-16 11:41:08 +0000381 if (htab->map.map_flags & BPF_F_ZERO_SEED)
382 htab->hashrnd = 0;
383 else
384 htab->hashrnd = get_random_int();
385
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800386 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800387 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800388 raw_spin_lock_init(&htab->buckets[i].lock);
389 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800390
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800391 if (prealloc) {
392 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700393 if (err)
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700394 goto free_buckets;
395
396 if (!percpu && !lru) {
397 /* lru itself can remove the least used element, so
398 * there is no need for an extra elem during map_update.
399 */
400 err = alloc_extra_elems(htab);
401 if (err)
402 goto free_prealloc;
403 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700404 }
405
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800406 return &htab->map;
407
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700408free_prealloc:
409 prealloc_destroy(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800410free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100411 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800412free_htab:
413 kfree(htab);
414 return ERR_PTR(err);
415}
416
Daniel Borkmannc0203472018-08-22 23:49:37 +0200417static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800418{
Daniel Borkmannc0203472018-08-22 23:49:37 +0200419 return jhash(key, key_len, hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800420}
421
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800422static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800423{
424 return &htab->buckets[hash & (htab->n_buckets - 1)];
425}
426
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800427static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800428{
429 return &__select_bucket(htab, hash)->head;
430}
431
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800432/* this lookup function can only be called with bucket lock taken */
433static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800434 void *key, u32 key_size)
435{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800436 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800437 struct htab_elem *l;
438
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800439 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800440 if (l->hash == hash && !memcmp(&l->key, key, key_size))
441 return l;
442
443 return NULL;
444}
445
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800446/* can be called without bucket lock. it will repeat the loop in
447 * the unlikely event when elements moved from one bucket into another
448 * while link list is being walked
449 */
450static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
451 u32 hash, void *key,
452 u32 key_size, u32 n_buckets)
453{
454 struct hlist_nulls_node *n;
455 struct htab_elem *l;
456
457again:
458 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
459 if (l->hash == hash && !memcmp(&l->key, key, key_size))
460 return l;
461
462 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
463 goto again;
464
465 return NULL;
466}
467
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700468/* Called from syscall or from eBPF program directly, so
469 * arguments have to match bpf_map_lookup_elem() exactly.
470 * The return value is adjusted by BPF instructions
471 * in htab_map_gen_lookup().
472 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800473static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800474{
475 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800476 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800477 struct htab_elem *l;
478 u32 hash, key_size;
479
480 /* Must be called with rcu_read_lock. */
481 WARN_ON_ONCE(!rcu_read_lock_held());
482
483 key_size = map->key_size;
484
Daniel Borkmannc0203472018-08-22 23:49:37 +0200485 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800486
487 head = select_bucket(htab, hash);
488
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800489 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800490
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800491 return l;
492}
493
494static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
495{
496 struct htab_elem *l = __htab_map_lookup_elem(map, key);
497
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800498 if (l)
499 return l->key + round_up(map->key_size, 8);
500
501 return NULL;
502}
503
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700504/* inline bpf_map_lookup_elem() call.
505 * Instead of:
506 * bpf_prog
507 * bpf_map_lookup_elem
508 * map->ops->map_lookup_elem
509 * htab_map_lookup_elem
510 * __htab_map_lookup_elem
511 * do:
512 * bpf_prog
513 * __htab_map_lookup_elem
514 */
515static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
516{
517 struct bpf_insn *insn = insn_buf;
518 const int ret = BPF_REG_0;
519
Daniel Borkmann09772d92018-06-02 23:06:35 +0200520 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
521 (void *(*)(struct bpf_map *map, void *key))NULL));
522 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700523 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
524 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
525 offsetof(struct htab_elem, key) +
526 round_up(map->key_size, 8));
527 return insn - insn_buf;
528}
529
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200530static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
531 void *key, const bool mark)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800532{
533 struct htab_elem *l = __htab_map_lookup_elem(map, key);
534
535 if (l) {
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200536 if (mark)
537 bpf_lru_node_set_ref(&l->lru_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800538 return l->key + round_up(map->key_size, 8);
539 }
540
541 return NULL;
542}
543
Daniel Borkmann50b045a2019-05-14 01:18:56 +0200544static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
545{
546 return __htab_lru_map_lookup_elem(map, key, true);
547}
548
549static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
550{
551 return __htab_lru_map_lookup_elem(map, key, false);
552}
553
Martin KaFai Laucc555422017-08-31 23:27:12 -0700554static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
555 struct bpf_insn *insn_buf)
556{
557 struct bpf_insn *insn = insn_buf;
558 const int ret = BPF_REG_0;
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700559 const int ref_reg = BPF_REG_1;
Martin KaFai Laucc555422017-08-31 23:27:12 -0700560
Daniel Borkmann09772d92018-06-02 23:06:35 +0200561 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
562 (void *(*)(struct bpf_map *map, void *key))NULL));
563 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700564 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
565 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
566 offsetof(struct htab_elem, lru_node) +
567 offsetof(struct bpf_lru_node, ref));
568 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
Martin KaFai Laucc555422017-08-31 23:27:12 -0700569 *insn++ = BPF_ST_MEM(BPF_B, ret,
570 offsetof(struct htab_elem, lru_node) +
571 offsetof(struct bpf_lru_node, ref),
572 1);
573 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
574 offsetof(struct htab_elem, key) +
575 round_up(map->key_size, 8));
576 return insn - insn_buf;
577}
578
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800579/* It is called from the bpf_lru_list when the LRU needs to delete
580 * older elements from the htab.
581 */
582static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
583{
584 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800585 struct htab_elem *l = NULL, *tgt_l;
586 struct hlist_nulls_head *head;
587 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800588 unsigned long flags;
589 struct bucket *b;
590
591 tgt_l = container_of(node, struct htab_elem, lru_node);
592 b = __select_bucket(htab, tgt_l->hash);
593 head = &b->head;
594
595 raw_spin_lock_irqsave(&b->lock, flags);
596
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800597 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800598 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800599 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800600 break;
601 }
602
603 raw_spin_unlock_irqrestore(&b->lock, flags);
604
605 return l == tgt_l;
606}
607
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800608/* Called from syscall */
609static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
610{
611 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800612 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800613 struct htab_elem *l, *next_l;
614 u32 hash, key_size;
Teng Qin8fe45922017-04-24 19:00:37 -0700615 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800616
617 WARN_ON_ONCE(!rcu_read_lock_held());
618
619 key_size = map->key_size;
620
Teng Qin8fe45922017-04-24 19:00:37 -0700621 if (!key)
622 goto find_first_elem;
623
Daniel Borkmannc0203472018-08-22 23:49:37 +0200624 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800625
626 head = select_bucket(htab, hash);
627
628 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800629 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800630
Teng Qin8fe45922017-04-24 19:00:37 -0700631 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800632 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800633
634 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800635 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800636 struct htab_elem, hash_node);
637
638 if (next_l) {
639 /* if next elem in this hash list is non-zero, just return it */
640 memcpy(next_key, next_l->key, key_size);
641 return 0;
642 }
643
644 /* no more elements in this hash list, go to the next bucket */
645 i = hash & (htab->n_buckets - 1);
646 i++;
647
648find_first_elem:
649 /* iterate over buckets */
650 for (; i < htab->n_buckets; i++) {
651 head = select_bucket(htab, i);
652
653 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800654 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800655 struct htab_elem, hash_node);
656 if (next_l) {
657 /* if it's not empty, just return it */
658 memcpy(next_key, next_l->key, key_size);
659 return 0;
660 }
661 }
662
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800663 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800664 return -ENOENT;
665}
666
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800667static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800668{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800669 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
670 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800671 kfree(l);
672}
673
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800674static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800675{
676 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800677 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800678
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800679 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
680 * we're calling kfree, otherwise deadlock is possible if kprobes
681 * are placed somewhere inside of slub
682 */
683 preempt_disable();
684 __this_cpu_inc(bpf_prog_active);
685 htab_elem_free(htab, l);
686 __this_cpu_dec(bpf_prog_active);
687 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800688}
689
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800690static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800691{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700692 struct bpf_map *map = &htab->map;
693
694 if (map->ops->map_fd_put_ptr) {
695 void *ptr = fd_htab_map_get_ptr(map, l);
696
697 map->ops->map_fd_put_ptr(ptr);
698 }
699
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700700 if (htab_is_prealloc(htab)) {
Alexei Starovoitova89fac52019-01-30 18:12:43 -0800701 __pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800702 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800703 atomic_dec(&htab->count);
704 l->htab = htab;
705 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800706 }
707}
708
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800709static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
710 void *value, bool onallcpus)
711{
712 if (!onallcpus) {
713 /* copy true value_size bytes */
714 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
715 } else {
716 u32 size = round_up(htab->map.value_size, 8);
717 int off = 0, cpu;
718
719 for_each_possible_cpu(cpu) {
720 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
721 value + off, size);
722 off += size;
723 }
724 }
725}
726
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200727static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
728{
729 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
730 BITS_PER_LONG == 64;
731}
732
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800733static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
734 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700735 bool percpu, bool onallcpus,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700736 struct htab_elem *old_elem)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800737{
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800738 u32 size = htab->map.value_size;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700739 bool prealloc = htab_is_prealloc(htab);
740 struct htab_elem *l_new, **pl_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800741 void __percpu *pptr;
742
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800743 if (prealloc) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700744 if (old_elem) {
745 /* if we're updating the existing element,
746 * use per-cpu extra elems to avoid freelist_pop/push
747 */
748 pl_new = this_cpu_ptr(htab->extra_elems);
749 l_new = *pl_new;
750 *pl_new = old_elem;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700751 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700752 struct pcpu_freelist_node *l;
753
Alexei Starovoitova89fac52019-01-30 18:12:43 -0800754 l = __pcpu_freelist_pop(&htab->freelist);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700755 if (!l)
756 return ERR_PTR(-E2BIG);
757 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800758 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700759 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700760 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
761 if (!old_elem) {
762 /* when map is full and update() is replacing
763 * old element, it's ok to allocate, since
764 * old element will be freed immediately.
765 * Otherwise return an error
766 */
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200767 l_new = ERR_PTR(-E2BIG);
768 goto dec_count;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700769 }
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700770 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
771 htab->map.numa_node);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200772 if (!l_new) {
773 l_new = ERR_PTR(-ENOMEM);
774 goto dec_count;
775 }
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800776 check_and_init_map_lock(&htab->map,
777 l_new->key + round_up(key_size, 8));
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800778 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800779
780 memcpy(l_new->key, key, key_size);
781 if (percpu) {
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800782 size = round_up(size, 8);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800783 if (prealloc) {
784 pptr = htab_elem_get_ptr(l_new, key_size);
785 } else {
786 /* alloc_percpu zero-fills */
787 pptr = __alloc_percpu_gfp(size, 8,
788 GFP_ATOMIC | __GFP_NOWARN);
789 if (!pptr) {
790 kfree(l_new);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200791 l_new = ERR_PTR(-ENOMEM);
792 goto dec_count;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800793 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800794 }
795
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800796 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800797
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800798 if (!prealloc)
799 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800800 } else if (fd_htab_map_needs_adjust(htab)) {
801 size = round_up(size, 8);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800802 memcpy(l_new->key + round_up(key_size, 8), value, size);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800803 } else {
804 copy_map_value(&htab->map,
805 l_new->key + round_up(key_size, 8),
806 value);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800807 }
808
809 l_new->hash = hash;
810 return l_new;
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200811dec_count:
812 atomic_dec(&htab->count);
813 return l_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800814}
815
816static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
817 u64 map_flags)
818{
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800819 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800820 /* elem already exists */
821 return -EEXIST;
822
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800823 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800824 /* elem doesn't exist, cannot update it */
825 return -ENOENT;
826
827 return 0;
828}
829
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800830/* Called from syscall or from eBPF program */
831static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
832 u64 map_flags)
833{
834 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800835 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800836 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800837 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800838 struct bucket *b;
839 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800840 int ret;
841
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800842 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800843 /* unknown flags */
844 return -EINVAL;
845
846 WARN_ON_ONCE(!rcu_read_lock_held());
847
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800848 key_size = map->key_size;
849
Daniel Borkmannc0203472018-08-22 23:49:37 +0200850 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800851
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800852 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800853 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800854
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800855 if (unlikely(map_flags & BPF_F_LOCK)) {
856 if (unlikely(!map_value_has_spin_lock(map)))
857 return -EINVAL;
858 /* find an element without taking the bucket lock */
859 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
860 htab->n_buckets);
861 ret = check_flags(htab, l_old, map_flags);
862 if (ret)
863 return ret;
864 if (l_old) {
865 /* grab the element lock and update value in place */
866 copy_map_value_locked(map,
867 l_old->key + round_up(key_size, 8),
868 value, false);
869 return 0;
870 }
871 /* fall through, grab the bucket lock and lookup again.
872 * 99.9% chance that the element won't be found,
873 * but second lookup under lock has to be done.
874 */
875 }
876
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800877 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800878 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800879
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800880 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800881
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800882 ret = check_flags(htab, l_old, map_flags);
883 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800884 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800885
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800886 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
887 /* first lookup without the bucket lock didn't find the element,
888 * but second lookup with the bucket lock found it.
889 * This case is highly unlikely, but has to be dealt with:
890 * grab the element lock in addition to the bucket lock
891 * and update element in place
892 */
893 copy_map_value_locked(map,
894 l_old->key + round_up(key_size, 8),
895 value, false);
896 ret = 0;
897 goto err;
898 }
899
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700900 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700901 l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800902 if (IS_ERR(l_new)) {
903 /* all pre-allocated elements are in use or memory exhausted */
904 ret = PTR_ERR(l_new);
905 goto err;
906 }
907
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800908 /* add new element to the head of the list, so that
909 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800910 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800911 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800912 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800913 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700914 if (!htab_is_prealloc(htab))
915 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800916 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800917 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800918err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800919 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800920 return ret;
921}
922
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800923static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
924 u64 map_flags)
925{
926 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
927 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800928 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800929 unsigned long flags;
930 struct bucket *b;
931 u32 key_size, hash;
932 int ret;
933
934 if (unlikely(map_flags > BPF_EXIST))
935 /* unknown flags */
936 return -EINVAL;
937
938 WARN_ON_ONCE(!rcu_read_lock_held());
939
940 key_size = map->key_size;
941
Daniel Borkmannc0203472018-08-22 23:49:37 +0200942 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800943
944 b = __select_bucket(htab, hash);
945 head = &b->head;
946
947 /* For LRU, we need to alloc before taking bucket's
948 * spinlock because getting free nodes from LRU may need
949 * to remove older elements from htab and this removal
950 * operation will need a bucket lock.
951 */
952 l_new = prealloc_lru_pop(htab, key, hash);
953 if (!l_new)
954 return -ENOMEM;
955 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
956
957 /* bpf_map_update_elem() can be called in_irq() */
958 raw_spin_lock_irqsave(&b->lock, flags);
959
960 l_old = lookup_elem_raw(head, hash, key, key_size);
961
962 ret = check_flags(htab, l_old, map_flags);
963 if (ret)
964 goto err;
965
966 /* add new element to the head of the list, so that
967 * concurrent search will find it before old elem
968 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800969 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800970 if (l_old) {
971 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800972 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800973 }
974 ret = 0;
975
976err:
977 raw_spin_unlock_irqrestore(&b->lock, flags);
978
979 if (ret)
980 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
981 else if (l_old)
982 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
983
984 return ret;
985}
986
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800987static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
988 void *value, u64 map_flags,
989 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800990{
991 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
992 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800993 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800994 unsigned long flags;
995 struct bucket *b;
996 u32 key_size, hash;
997 int ret;
998
999 if (unlikely(map_flags > BPF_EXIST))
1000 /* unknown flags */
1001 return -EINVAL;
1002
1003 WARN_ON_ONCE(!rcu_read_lock_held());
1004
1005 key_size = map->key_size;
1006
Daniel Borkmannc0203472018-08-22 23:49:37 +02001007 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001008
1009 b = __select_bucket(htab, hash);
1010 head = &b->head;
1011
1012 /* bpf_map_update_elem() can be called in_irq() */
1013 raw_spin_lock_irqsave(&b->lock, flags);
1014
1015 l_old = lookup_elem_raw(head, hash, key, key_size);
1016
1017 ret = check_flags(htab, l_old, map_flags);
1018 if (ret)
1019 goto err;
1020
1021 if (l_old) {
1022 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -08001023 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1024 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001025 } else {
1026 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001027 hash, true, onallcpus, NULL);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001028 if (IS_ERR(l_new)) {
1029 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001030 goto err;
1031 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001032 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001033 }
1034 ret = 0;
1035err:
1036 raw_spin_unlock_irqrestore(&b->lock, flags);
1037 return ret;
1038}
1039
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001040static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1041 void *value, u64 map_flags,
1042 bool onallcpus)
1043{
1044 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1045 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001046 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001047 unsigned long flags;
1048 struct bucket *b;
1049 u32 key_size, hash;
1050 int ret;
1051
1052 if (unlikely(map_flags > BPF_EXIST))
1053 /* unknown flags */
1054 return -EINVAL;
1055
1056 WARN_ON_ONCE(!rcu_read_lock_held());
1057
1058 key_size = map->key_size;
1059
Daniel Borkmannc0203472018-08-22 23:49:37 +02001060 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001061
1062 b = __select_bucket(htab, hash);
1063 head = &b->head;
1064
1065 /* For LRU, we need to alloc before taking bucket's
1066 * spinlock because LRU's elem alloc may need
1067 * to remove older elem from htab and this removal
1068 * operation will need a bucket lock.
1069 */
1070 if (map_flags != BPF_EXIST) {
1071 l_new = prealloc_lru_pop(htab, key, hash);
1072 if (!l_new)
1073 return -ENOMEM;
1074 }
1075
1076 /* bpf_map_update_elem() can be called in_irq() */
1077 raw_spin_lock_irqsave(&b->lock, flags);
1078
1079 l_old = lookup_elem_raw(head, hash, key, key_size);
1080
1081 ret = check_flags(htab, l_old, map_flags);
1082 if (ret)
1083 goto err;
1084
1085 if (l_old) {
1086 bpf_lru_node_set_ref(&l_old->lru_node);
1087
1088 /* per-cpu hash map can update value in-place */
1089 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1090 value, onallcpus);
1091 } else {
1092 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1093 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001094 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001095 l_new = NULL;
1096 }
1097 ret = 0;
1098err:
1099 raw_spin_unlock_irqrestore(&b->lock, flags);
1100 if (l_new)
1101 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1102 return ret;
1103}
1104
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001105static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1106 void *value, u64 map_flags)
1107{
1108 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1109}
1110
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001111static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1112 void *value, u64 map_flags)
1113{
1114 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1115 false);
1116}
1117
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001118/* Called from syscall or from eBPF program */
1119static int htab_map_delete_elem(struct bpf_map *map, void *key)
1120{
1121 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001122 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001123 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001124 struct htab_elem *l;
1125 unsigned long flags;
1126 u32 hash, key_size;
1127 int ret = -ENOENT;
1128
1129 WARN_ON_ONCE(!rcu_read_lock_held());
1130
1131 key_size = map->key_size;
1132
Daniel Borkmannc0203472018-08-22 23:49:37 +02001133 hash = htab_map_hash(key, key_size, htab->hashrnd);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001134 b = __select_bucket(htab, hash);
1135 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001136
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001137 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001138
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001139 l = lookup_elem_raw(head, hash, key, key_size);
1140
1141 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001142 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001143 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001144 ret = 0;
1145 }
1146
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001147 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001148 return ret;
1149}
1150
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001151static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1152{
1153 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001154 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001155 struct bucket *b;
1156 struct htab_elem *l;
1157 unsigned long flags;
1158 u32 hash, key_size;
1159 int ret = -ENOENT;
1160
1161 WARN_ON_ONCE(!rcu_read_lock_held());
1162
1163 key_size = map->key_size;
1164
Daniel Borkmannc0203472018-08-22 23:49:37 +02001165 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001166 b = __select_bucket(htab, hash);
1167 head = &b->head;
1168
1169 raw_spin_lock_irqsave(&b->lock, flags);
1170
1171 l = lookup_elem_raw(head, hash, key, key_size);
1172
1173 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001174 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001175 ret = 0;
1176 }
1177
1178 raw_spin_unlock_irqrestore(&b->lock, flags);
1179 if (l)
1180 bpf_lru_push_free(&htab->lru, &l->lru_node);
1181 return ret;
1182}
1183
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001184static void delete_all_elements(struct bpf_htab *htab)
1185{
1186 int i;
1187
1188 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001189 struct hlist_nulls_head *head = select_bucket(htab, i);
1190 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001191 struct htab_elem *l;
1192
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001193 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1194 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001195 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001196 }
1197 }
1198}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001199
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001200/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1201static void htab_map_free(struct bpf_map *map)
1202{
1203 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1204
1205 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1206 * so the programs (can be more than one that used this map) were
1207 * disconnected from events. Wait for outstanding critical sections in
1208 * these programs to complete
1209 */
1210 synchronize_rcu();
1211
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001212 /* some of free_htab_elem() callbacks for elements of this map may
1213 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001214 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001215 rcu_barrier();
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001216 if (!htab_is_prealloc(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001217 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001218 else
1219 prealloc_destroy(htab);
1220
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001221 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001222 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001223 kfree(htab);
1224}
1225
Yonghong Song699c86d2018-08-09 08:55:20 -07001226static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1227 struct seq_file *m)
1228{
1229 void *value;
1230
1231 rcu_read_lock();
1232
1233 value = htab_map_lookup_elem(map, key);
1234 if (!value) {
1235 rcu_read_unlock();
1236 return;
1237 }
1238
1239 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1240 seq_puts(m, ": ");
1241 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1242 seq_puts(m, "\n");
1243
1244 rcu_read_unlock();
1245}
1246
Johannes Berg40077e02017-04-11 15:34:58 +02001247const struct bpf_map_ops htab_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001248 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001249 .map_alloc = htab_map_alloc,
1250 .map_free = htab_map_free,
1251 .map_get_next_key = htab_map_get_next_key,
1252 .map_lookup_elem = htab_map_lookup_elem,
1253 .map_update_elem = htab_map_update_elem,
1254 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001255 .map_gen_lookup = htab_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001256 .map_seq_show_elem = htab_map_seq_show_elem,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001257};
1258
Johannes Berg40077e02017-04-11 15:34:58 +02001259const struct bpf_map_ops htab_lru_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001260 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001261 .map_alloc = htab_map_alloc,
1262 .map_free = htab_map_free,
1263 .map_get_next_key = htab_map_get_next_key,
1264 .map_lookup_elem = htab_lru_map_lookup_elem,
Daniel Borkmann50b045a2019-05-14 01:18:56 +02001265 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001266 .map_update_elem = htab_lru_map_update_elem,
1267 .map_delete_elem = htab_lru_map_delete_elem,
Martin KaFai Laucc555422017-08-31 23:27:12 -07001268 .map_gen_lookup = htab_lru_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001269 .map_seq_show_elem = htab_map_seq_show_elem,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001270};
1271
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001272/* Called from eBPF program */
1273static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1274{
1275 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1276
1277 if (l)
1278 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1279 else
1280 return NULL;
1281}
1282
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001283static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1284{
1285 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1286
1287 if (l) {
1288 bpf_lru_node_set_ref(&l->lru_node);
1289 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1290 }
1291
1292 return NULL;
1293}
1294
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001295int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1296{
1297 struct htab_elem *l;
1298 void __percpu *pptr;
1299 int ret = -ENOENT;
1300 int cpu, off = 0;
1301 u32 size;
1302
1303 /* per_cpu areas are zero-filled and bpf programs can only
1304 * access 'value_size' of them, so copying rounded areas
1305 * will not leak any kernel data
1306 */
1307 size = round_up(map->value_size, 8);
1308 rcu_read_lock();
1309 l = __htab_map_lookup_elem(map, key);
1310 if (!l)
1311 goto out;
Daniel Borkmann50b045a2019-05-14 01:18:56 +02001312 /* We do not mark LRU map element here in order to not mess up
1313 * eviction heuristics when user space does a map walk.
1314 */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001315 pptr = htab_elem_get_ptr(l, map->key_size);
1316 for_each_possible_cpu(cpu) {
1317 bpf_long_memcpy(value + off,
1318 per_cpu_ptr(pptr, cpu), size);
1319 off += size;
1320 }
1321 ret = 0;
1322out:
1323 rcu_read_unlock();
1324 return ret;
1325}
1326
1327int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1328 u64 map_flags)
1329{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001330 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001331 int ret;
1332
1333 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001334 if (htab_is_lru(htab))
1335 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1336 map_flags, true);
1337 else
1338 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1339 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001340 rcu_read_unlock();
1341
1342 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001343}
1344
Yonghong Songc7b27c32018-08-29 14:43:13 -07001345static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1346 struct seq_file *m)
1347{
1348 struct htab_elem *l;
1349 void __percpu *pptr;
1350 int cpu;
1351
1352 rcu_read_lock();
1353
1354 l = __htab_map_lookup_elem(map, key);
1355 if (!l) {
1356 rcu_read_unlock();
1357 return;
1358 }
1359
1360 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1361 seq_puts(m, ": {\n");
1362 pptr = htab_elem_get_ptr(l, map->key_size);
1363 for_each_possible_cpu(cpu) {
1364 seq_printf(m, "\tcpu%d: ", cpu);
1365 btf_type_seq_show(map->btf, map->btf_value_type_id,
1366 per_cpu_ptr(pptr, cpu), m);
1367 seq_puts(m, "\n");
1368 }
1369 seq_puts(m, "}\n");
1370
1371 rcu_read_unlock();
1372}
1373
Johannes Berg40077e02017-04-11 15:34:58 +02001374const struct bpf_map_ops htab_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001375 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001376 .map_alloc = htab_map_alloc,
1377 .map_free = htab_map_free,
1378 .map_get_next_key = htab_map_get_next_key,
1379 .map_lookup_elem = htab_percpu_map_lookup_elem,
1380 .map_update_elem = htab_percpu_map_update_elem,
1381 .map_delete_elem = htab_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001382 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001383};
1384
Johannes Berg40077e02017-04-11 15:34:58 +02001385const struct bpf_map_ops htab_lru_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001386 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001387 .map_alloc = htab_map_alloc,
1388 .map_free = htab_map_free,
1389 .map_get_next_key = htab_map_get_next_key,
1390 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1391 .map_update_elem = htab_lru_percpu_map_update_elem,
1392 .map_delete_elem = htab_lru_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001393 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001394};
1395
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001396static int fd_htab_map_alloc_check(union bpf_attr *attr)
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001397{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001398 if (attr->value_size != sizeof(u32))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001399 return -EINVAL;
1400 return htab_map_alloc_check(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001401}
1402
1403static void fd_htab_map_free(struct bpf_map *map)
1404{
1405 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1406 struct hlist_nulls_node *n;
1407 struct hlist_nulls_head *head;
1408 struct htab_elem *l;
1409 int i;
1410
1411 for (i = 0; i < htab->n_buckets; i++) {
1412 head = select_bucket(htab, i);
1413
1414 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1415 void *ptr = fd_htab_map_get_ptr(map, l);
1416
1417 map->ops->map_fd_put_ptr(ptr);
1418 }
1419 }
1420
1421 htab_map_free(map);
1422}
1423
1424/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001425int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1426{
1427 void **ptr;
1428 int ret = 0;
1429
1430 if (!map->ops->map_fd_sys_lookup_elem)
1431 return -ENOTSUPP;
1432
1433 rcu_read_lock();
1434 ptr = htab_map_lookup_elem(map, key);
1435 if (ptr)
1436 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1437 else
1438 ret = -ENOENT;
1439 rcu_read_unlock();
1440
1441 return ret;
1442}
1443
1444/* only called from syscall */
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001445int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1446 void *key, void *value, u64 map_flags)
1447{
1448 void *ptr;
1449 int ret;
1450 u32 ufd = *(u32 *)value;
1451
1452 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1453 if (IS_ERR(ptr))
1454 return PTR_ERR(ptr);
1455
1456 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1457 if (ret)
1458 map->ops->map_fd_put_ptr(ptr);
1459
1460 return ret;
1461}
1462
1463static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1464{
1465 struct bpf_map *map, *inner_map_meta;
1466
1467 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1468 if (IS_ERR(inner_map_meta))
1469 return inner_map_meta;
1470
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001471 map = htab_map_alloc(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001472 if (IS_ERR(map)) {
1473 bpf_map_meta_free(inner_map_meta);
1474 return map;
1475 }
1476
1477 map->inner_map_meta = inner_map_meta;
1478
1479 return map;
1480}
1481
1482static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1483{
1484 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1485
1486 if (!inner_map)
1487 return NULL;
1488
1489 return READ_ONCE(*inner_map);
1490}
1491
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001492static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1493 struct bpf_insn *insn_buf)
1494{
1495 struct bpf_insn *insn = insn_buf;
1496 const int ret = BPF_REG_0;
1497
Daniel Borkmann09772d92018-06-02 23:06:35 +02001498 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1499 (void *(*)(struct bpf_map *map, void *key))NULL));
1500 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001501 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1502 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1503 offsetof(struct htab_elem, key) +
1504 round_up(map->key_size, 8));
1505 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1506
1507 return insn - insn_buf;
1508}
1509
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001510static void htab_of_map_free(struct bpf_map *map)
1511{
1512 bpf_map_meta_free(map->inner_map_meta);
1513 fd_htab_map_free(map);
1514}
1515
Johannes Berg40077e02017-04-11 15:34:58 +02001516const struct bpf_map_ops htab_of_maps_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001517 .map_alloc_check = fd_htab_map_alloc_check,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001518 .map_alloc = htab_of_map_alloc,
1519 .map_free = htab_of_map_free,
1520 .map_get_next_key = htab_map_get_next_key,
1521 .map_lookup_elem = htab_of_map_lookup_elem,
1522 .map_delete_elem = htab_map_delete_elem,
1523 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1524 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001525 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001526 .map_gen_lookup = htab_of_map_gen_lookup,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001527 .map_check_btf = map_check_no_btf,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001528};