blob: 2c1790288138771a5d527ca5507fec7c9aca16f6 [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 | \
26 BPF_F_RDONLY | BPF_F_WRONLY)
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);
247 int numa_node = bpf_map_attr_numa_node(attr);
248
249 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
250 offsetof(struct htab_elem, hash_node.pprev));
251 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
252 offsetof(struct htab_elem, hash_node.pprev));
253
254 if (lru && !capable(CAP_SYS_ADMIN))
255 /* LRU implementation is much complicated than other
256 * maps. Hence, limit to CAP_SYS_ADMIN for now.
257 */
258 return -EPERM;
259
260 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
261 /* reserved bits should not be used */
262 return -EINVAL;
263
264 if (!lru && percpu_lru)
265 return -EINVAL;
266
267 if (lru && !prealloc)
268 return -ENOTSUPP;
269
270 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
271 return -EINVAL;
272
273 /* check sanity of attributes.
274 * value_size == 0 may be allowed in the future to use map as a set
275 */
276 if (attr->max_entries == 0 || attr->key_size == 0 ||
277 attr->value_size == 0)
278 return -EINVAL;
279
280 if (attr->key_size > MAX_BPF_STACK)
281 /* eBPF programs initialize keys on stack, so they cannot be
282 * larger than max stack size
283 */
284 return -E2BIG;
285
286 if (attr->value_size >= KMALLOC_MAX_SIZE -
287 MAX_BPF_STACK - sizeof(struct htab_elem))
288 /* if value_size is bigger, the user space won't be able to
289 * access the elements via bpf syscall. This check also makes
290 * sure that the elem_size doesn't overflow and it's
291 * kmalloc-able later in htab_map_update_elem()
292 */
293 return -E2BIG;
294
295 return 0;
296}
297
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800298static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
299{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800300 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
301 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
302 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
303 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800304 /* percpu_lru means each cpu has its own LRU list.
305 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
306 * the map's value itself is percpu. percpu_lru has
307 * nothing to do with the map's value.
308 */
309 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
310 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800311 struct bpf_htab *htab;
312 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800313 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800314
315 htab = kzalloc(sizeof(*htab), GFP_USER);
316 if (!htab)
317 return ERR_PTR(-ENOMEM);
318
Jakub Kicinskibd475642018-01-11 20:29:06 -0800319 bpf_map_init_from_attr(&htab->map, attr);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800320
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800321 if (percpu_lru) {
322 /* ensure each CPU's lru list has >=1 elements.
323 * since we are at it, make each lru list has the same
324 * number of elements.
325 */
326 htab->map.max_entries = roundup(attr->max_entries,
327 num_possible_cpus());
328 if (htab->map.max_entries < attr->max_entries)
329 htab->map.max_entries = rounddown(attr->max_entries,
330 num_possible_cpus());
331 }
332
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800333 /* hash table size must be power of 2 */
334 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
335
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800336 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800337 round_up(htab->map.key_size, 8);
338 if (percpu)
339 htab->elem_size += sizeof(void *);
340 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800341 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800342
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800343 err = -E2BIG;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800344 /* prevent zero size kmalloc and check for u32 overflow */
345 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800346 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800347 goto free_htab;
348
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800349 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
350 (u64) htab->elem_size * htab->map.max_entries;
351
352 if (percpu)
353 cost += (u64) round_up(htab->map.value_size, 8) *
354 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700355 else
356 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800357
358 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800359 /* make sure page count doesn't overflow */
360 goto free_htab;
361
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800362 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800363
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800364 /* if map size is larger than memlock limit, reject it early */
365 err = bpf_map_precharge_memlock(htab->map.pages);
366 if (err)
367 goto free_htab;
368
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800369 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100370 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700371 sizeof(struct bucket),
372 htab->map.numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100373 if (!htab->buckets)
374 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800375
Daniel Borkmannc0203472018-08-22 23:49:37 +0200376 htab->hashrnd = get_random_int();
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800377 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800378 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800379 raw_spin_lock_init(&htab->buckets[i].lock);
380 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800381
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800382 if (prealloc) {
383 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700384 if (err)
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700385 goto free_buckets;
386
387 if (!percpu && !lru) {
388 /* lru itself can remove the least used element, so
389 * there is no need for an extra elem during map_update.
390 */
391 err = alloc_extra_elems(htab);
392 if (err)
393 goto free_prealloc;
394 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700395 }
396
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800397 return &htab->map;
398
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700399free_prealloc:
400 prealloc_destroy(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800401free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100402 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800403free_htab:
404 kfree(htab);
405 return ERR_PTR(err);
406}
407
Daniel Borkmannc0203472018-08-22 23:49:37 +0200408static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800409{
Daniel Borkmannc0203472018-08-22 23:49:37 +0200410 return jhash(key, key_len, hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800411}
412
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800413static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800414{
415 return &htab->buckets[hash & (htab->n_buckets - 1)];
416}
417
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800418static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800419{
420 return &__select_bucket(htab, hash)->head;
421}
422
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800423/* this lookup function can only be called with bucket lock taken */
424static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800425 void *key, u32 key_size)
426{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800427 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800428 struct htab_elem *l;
429
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800430 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800431 if (l->hash == hash && !memcmp(&l->key, key, key_size))
432 return l;
433
434 return NULL;
435}
436
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800437/* can be called without bucket lock. it will repeat the loop in
438 * the unlikely event when elements moved from one bucket into another
439 * while link list is being walked
440 */
441static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
442 u32 hash, void *key,
443 u32 key_size, u32 n_buckets)
444{
445 struct hlist_nulls_node *n;
446 struct htab_elem *l;
447
448again:
449 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
450 if (l->hash == hash && !memcmp(&l->key, key, key_size))
451 return l;
452
453 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
454 goto again;
455
456 return NULL;
457}
458
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700459/* Called from syscall or from eBPF program directly, so
460 * arguments have to match bpf_map_lookup_elem() exactly.
461 * The return value is adjusted by BPF instructions
462 * in htab_map_gen_lookup().
463 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800464static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800465{
466 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800467 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800468 struct htab_elem *l;
469 u32 hash, key_size;
470
471 /* Must be called with rcu_read_lock. */
472 WARN_ON_ONCE(!rcu_read_lock_held());
473
474 key_size = map->key_size;
475
Daniel Borkmannc0203472018-08-22 23:49:37 +0200476 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800477
478 head = select_bucket(htab, hash);
479
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800480 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800481
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800482 return l;
483}
484
485static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
486{
487 struct htab_elem *l = __htab_map_lookup_elem(map, key);
488
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800489 if (l)
490 return l->key + round_up(map->key_size, 8);
491
492 return NULL;
493}
494
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700495/* inline bpf_map_lookup_elem() call.
496 * Instead of:
497 * bpf_prog
498 * bpf_map_lookup_elem
499 * map->ops->map_lookup_elem
500 * htab_map_lookup_elem
501 * __htab_map_lookup_elem
502 * do:
503 * bpf_prog
504 * __htab_map_lookup_elem
505 */
506static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
507{
508 struct bpf_insn *insn = insn_buf;
509 const int ret = BPF_REG_0;
510
Daniel Borkmann09772d92018-06-02 23:06:35 +0200511 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
512 (void *(*)(struct bpf_map *map, void *key))NULL));
513 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700514 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
515 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
516 offsetof(struct htab_elem, key) +
517 round_up(map->key_size, 8));
518 return insn - insn_buf;
519}
520
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800521static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
522{
523 struct htab_elem *l = __htab_map_lookup_elem(map, key);
524
525 if (l) {
526 bpf_lru_node_set_ref(&l->lru_node);
527 return l->key + round_up(map->key_size, 8);
528 }
529
530 return NULL;
531}
532
Martin KaFai Laucc555422017-08-31 23:27:12 -0700533static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
534 struct bpf_insn *insn_buf)
535{
536 struct bpf_insn *insn = insn_buf;
537 const int ret = BPF_REG_0;
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700538 const int ref_reg = BPF_REG_1;
Martin KaFai Laucc555422017-08-31 23:27:12 -0700539
Daniel Borkmann09772d92018-06-02 23:06:35 +0200540 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
541 (void *(*)(struct bpf_map *map, void *key))NULL));
542 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700543 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
544 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
545 offsetof(struct htab_elem, lru_node) +
546 offsetof(struct bpf_lru_node, ref));
547 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
Martin KaFai Laucc555422017-08-31 23:27:12 -0700548 *insn++ = BPF_ST_MEM(BPF_B, ret,
549 offsetof(struct htab_elem, lru_node) +
550 offsetof(struct bpf_lru_node, ref),
551 1);
552 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
553 offsetof(struct htab_elem, key) +
554 round_up(map->key_size, 8));
555 return insn - insn_buf;
556}
557
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800558/* It is called from the bpf_lru_list when the LRU needs to delete
559 * older elements from the htab.
560 */
561static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
562{
563 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800564 struct htab_elem *l = NULL, *tgt_l;
565 struct hlist_nulls_head *head;
566 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800567 unsigned long flags;
568 struct bucket *b;
569
570 tgt_l = container_of(node, struct htab_elem, lru_node);
571 b = __select_bucket(htab, tgt_l->hash);
572 head = &b->head;
573
574 raw_spin_lock_irqsave(&b->lock, flags);
575
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800576 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800577 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800578 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800579 break;
580 }
581
582 raw_spin_unlock_irqrestore(&b->lock, flags);
583
584 return l == tgt_l;
585}
586
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800587/* Called from syscall */
588static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
589{
590 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800591 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800592 struct htab_elem *l, *next_l;
593 u32 hash, key_size;
Teng Qin8fe45922017-04-24 19:00:37 -0700594 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800595
596 WARN_ON_ONCE(!rcu_read_lock_held());
597
598 key_size = map->key_size;
599
Teng Qin8fe45922017-04-24 19:00:37 -0700600 if (!key)
601 goto find_first_elem;
602
Daniel Borkmannc0203472018-08-22 23:49:37 +0200603 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800604
605 head = select_bucket(htab, hash);
606
607 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800608 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800609
Teng Qin8fe45922017-04-24 19:00:37 -0700610 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800611 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800612
613 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800614 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800615 struct htab_elem, hash_node);
616
617 if (next_l) {
618 /* if next elem in this hash list is non-zero, just return it */
619 memcpy(next_key, next_l->key, key_size);
620 return 0;
621 }
622
623 /* no more elements in this hash list, go to the next bucket */
624 i = hash & (htab->n_buckets - 1);
625 i++;
626
627find_first_elem:
628 /* iterate over buckets */
629 for (; i < htab->n_buckets; i++) {
630 head = select_bucket(htab, i);
631
632 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800633 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800634 struct htab_elem, hash_node);
635 if (next_l) {
636 /* if it's not empty, just return it */
637 memcpy(next_key, next_l->key, key_size);
638 return 0;
639 }
640 }
641
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800642 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800643 return -ENOENT;
644}
645
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800646static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800647{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800648 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
649 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800650 kfree(l);
651}
652
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800653static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800654{
655 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800656 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800657
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800658 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
659 * we're calling kfree, otherwise deadlock is possible if kprobes
660 * are placed somewhere inside of slub
661 */
662 preempt_disable();
663 __this_cpu_inc(bpf_prog_active);
664 htab_elem_free(htab, l);
665 __this_cpu_dec(bpf_prog_active);
666 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800667}
668
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800669static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800670{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700671 struct bpf_map *map = &htab->map;
672
673 if (map->ops->map_fd_put_ptr) {
674 void *ptr = fd_htab_map_get_ptr(map, l);
675
676 map->ops->map_fd_put_ptr(ptr);
677 }
678
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700679 if (htab_is_prealloc(htab)) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800680 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800681 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800682 atomic_dec(&htab->count);
683 l->htab = htab;
684 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800685 }
686}
687
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800688static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
689 void *value, bool onallcpus)
690{
691 if (!onallcpus) {
692 /* copy true value_size bytes */
693 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
694 } else {
695 u32 size = round_up(htab->map.value_size, 8);
696 int off = 0, cpu;
697
698 for_each_possible_cpu(cpu) {
699 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
700 value + off, size);
701 off += size;
702 }
703 }
704}
705
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200706static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
707{
708 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
709 BITS_PER_LONG == 64;
710}
711
712static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
713{
714 u32 size = htab->map.value_size;
715
716 if (percpu || fd_htab_map_needs_adjust(htab))
717 size = round_up(size, 8);
718 return size;
719}
720
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800721static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
722 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700723 bool percpu, bool onallcpus,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700724 struct htab_elem *old_elem)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800725{
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200726 u32 size = htab_size_value(htab, percpu);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700727 bool prealloc = htab_is_prealloc(htab);
728 struct htab_elem *l_new, **pl_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800729 void __percpu *pptr;
730
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800731 if (prealloc) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700732 if (old_elem) {
733 /* if we're updating the existing element,
734 * use per-cpu extra elems to avoid freelist_pop/push
735 */
736 pl_new = this_cpu_ptr(htab->extra_elems);
737 l_new = *pl_new;
738 *pl_new = old_elem;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700739 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700740 struct pcpu_freelist_node *l;
741
742 l = pcpu_freelist_pop(&htab->freelist);
743 if (!l)
744 return ERR_PTR(-E2BIG);
745 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800746 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700747 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700748 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
749 if (!old_elem) {
750 /* when map is full and update() is replacing
751 * old element, it's ok to allocate, since
752 * old element will be freed immediately.
753 * Otherwise return an error
754 */
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200755 l_new = ERR_PTR(-E2BIG);
756 goto dec_count;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700757 }
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700758 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
759 htab->map.numa_node);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200760 if (!l_new) {
761 l_new = ERR_PTR(-ENOMEM);
762 goto dec_count;
763 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800764 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800765
766 memcpy(l_new->key, key, key_size);
767 if (percpu) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800768 if (prealloc) {
769 pptr = htab_elem_get_ptr(l_new, key_size);
770 } else {
771 /* alloc_percpu zero-fills */
772 pptr = __alloc_percpu_gfp(size, 8,
773 GFP_ATOMIC | __GFP_NOWARN);
774 if (!pptr) {
775 kfree(l_new);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200776 l_new = ERR_PTR(-ENOMEM);
777 goto dec_count;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800778 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800779 }
780
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800781 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800782
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800783 if (!prealloc)
784 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800785 } else {
786 memcpy(l_new->key + round_up(key_size, 8), value, size);
787 }
788
789 l_new->hash = hash;
790 return l_new;
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200791dec_count:
792 atomic_dec(&htab->count);
793 return l_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800794}
795
796static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
797 u64 map_flags)
798{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800799 if (l_old && map_flags == BPF_NOEXIST)
800 /* elem already exists */
801 return -EEXIST;
802
803 if (!l_old && map_flags == BPF_EXIST)
804 /* elem doesn't exist, cannot update it */
805 return -ENOENT;
806
807 return 0;
808}
809
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800810/* Called from syscall or from eBPF program */
811static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
812 u64 map_flags)
813{
814 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800815 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800816 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800817 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800818 struct bucket *b;
819 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800820 int ret;
821
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800822 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800823 /* unknown flags */
824 return -EINVAL;
825
826 WARN_ON_ONCE(!rcu_read_lock_held());
827
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800828 key_size = map->key_size;
829
Daniel Borkmannc0203472018-08-22 23:49:37 +0200830 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800831
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800832 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800833 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800834
835 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800836 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800837
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800838 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800839
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800840 ret = check_flags(htab, l_old, map_flags);
841 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800842 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800843
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700844 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700845 l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800846 if (IS_ERR(l_new)) {
847 /* all pre-allocated elements are in use or memory exhausted */
848 ret = PTR_ERR(l_new);
849 goto err;
850 }
851
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800852 /* add new element to the head of the list, so that
853 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800854 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800855 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800856 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800857 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700858 if (!htab_is_prealloc(htab))
859 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800860 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800861 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800862err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800863 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800864 return ret;
865}
866
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800867static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
868 u64 map_flags)
869{
870 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
871 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800872 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800873 unsigned long flags;
874 struct bucket *b;
875 u32 key_size, hash;
876 int ret;
877
878 if (unlikely(map_flags > BPF_EXIST))
879 /* unknown flags */
880 return -EINVAL;
881
882 WARN_ON_ONCE(!rcu_read_lock_held());
883
884 key_size = map->key_size;
885
Daniel Borkmannc0203472018-08-22 23:49:37 +0200886 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800887
888 b = __select_bucket(htab, hash);
889 head = &b->head;
890
891 /* For LRU, we need to alloc before taking bucket's
892 * spinlock because getting free nodes from LRU may need
893 * to remove older elements from htab and this removal
894 * operation will need a bucket lock.
895 */
896 l_new = prealloc_lru_pop(htab, key, hash);
897 if (!l_new)
898 return -ENOMEM;
899 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
900
901 /* bpf_map_update_elem() can be called in_irq() */
902 raw_spin_lock_irqsave(&b->lock, flags);
903
904 l_old = lookup_elem_raw(head, hash, key, key_size);
905
906 ret = check_flags(htab, l_old, map_flags);
907 if (ret)
908 goto err;
909
910 /* add new element to the head of the list, so that
911 * concurrent search will find it before old elem
912 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800913 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800914 if (l_old) {
915 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800916 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800917 }
918 ret = 0;
919
920err:
921 raw_spin_unlock_irqrestore(&b->lock, flags);
922
923 if (ret)
924 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
925 else if (l_old)
926 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
927
928 return ret;
929}
930
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800931static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
932 void *value, u64 map_flags,
933 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800934{
935 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
936 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800937 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800938 unsigned long flags;
939 struct bucket *b;
940 u32 key_size, hash;
941 int ret;
942
943 if (unlikely(map_flags > BPF_EXIST))
944 /* unknown flags */
945 return -EINVAL;
946
947 WARN_ON_ONCE(!rcu_read_lock_held());
948
949 key_size = map->key_size;
950
Daniel Borkmannc0203472018-08-22 23:49:37 +0200951 hash = htab_map_hash(key, key_size, htab->hashrnd);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800952
953 b = __select_bucket(htab, hash);
954 head = &b->head;
955
956 /* bpf_map_update_elem() can be called in_irq() */
957 raw_spin_lock_irqsave(&b->lock, flags);
958
959 l_old = lookup_elem_raw(head, hash, key, key_size);
960
961 ret = check_flags(htab, l_old, map_flags);
962 if (ret)
963 goto err;
964
965 if (l_old) {
966 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800967 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
968 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800969 } else {
970 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700971 hash, true, onallcpus, NULL);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800972 if (IS_ERR(l_new)) {
973 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800974 goto err;
975 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800976 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800977 }
978 ret = 0;
979err:
980 raw_spin_unlock_irqrestore(&b->lock, flags);
981 return ret;
982}
983
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800984static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
985 void *value, u64 map_flags,
986 bool onallcpus)
987{
988 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
989 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800990 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800991 unsigned long flags;
992 struct bucket *b;
993 u32 key_size, hash;
994 int ret;
995
996 if (unlikely(map_flags > BPF_EXIST))
997 /* unknown flags */
998 return -EINVAL;
999
1000 WARN_ON_ONCE(!rcu_read_lock_held());
1001
1002 key_size = map->key_size;
1003
Daniel Borkmannc0203472018-08-22 23:49:37 +02001004 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001005
1006 b = __select_bucket(htab, hash);
1007 head = &b->head;
1008
1009 /* For LRU, we need to alloc before taking bucket's
1010 * spinlock because LRU's elem alloc may need
1011 * to remove older elem from htab and this removal
1012 * operation will need a bucket lock.
1013 */
1014 if (map_flags != BPF_EXIST) {
1015 l_new = prealloc_lru_pop(htab, key, hash);
1016 if (!l_new)
1017 return -ENOMEM;
1018 }
1019
1020 /* bpf_map_update_elem() can be called in_irq() */
1021 raw_spin_lock_irqsave(&b->lock, flags);
1022
1023 l_old = lookup_elem_raw(head, hash, key, key_size);
1024
1025 ret = check_flags(htab, l_old, map_flags);
1026 if (ret)
1027 goto err;
1028
1029 if (l_old) {
1030 bpf_lru_node_set_ref(&l_old->lru_node);
1031
1032 /* per-cpu hash map can update value in-place */
1033 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1034 value, onallcpus);
1035 } else {
1036 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1037 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001038 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001039 l_new = NULL;
1040 }
1041 ret = 0;
1042err:
1043 raw_spin_unlock_irqrestore(&b->lock, flags);
1044 if (l_new)
1045 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1046 return ret;
1047}
1048
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001049static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1050 void *value, u64 map_flags)
1051{
1052 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1053}
1054
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001055static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1056 void *value, u64 map_flags)
1057{
1058 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1059 false);
1060}
1061
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001062/* Called from syscall or from eBPF program */
1063static int htab_map_delete_elem(struct bpf_map *map, void *key)
1064{
1065 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001066 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001067 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001068 struct htab_elem *l;
1069 unsigned long flags;
1070 u32 hash, key_size;
1071 int ret = -ENOENT;
1072
1073 WARN_ON_ONCE(!rcu_read_lock_held());
1074
1075 key_size = map->key_size;
1076
Daniel Borkmannc0203472018-08-22 23:49:37 +02001077 hash = htab_map_hash(key, key_size, htab->hashrnd);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001078 b = __select_bucket(htab, hash);
1079 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001080
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001081 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001082
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001083 l = lookup_elem_raw(head, hash, key, key_size);
1084
1085 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001086 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001087 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001088 ret = 0;
1089 }
1090
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001091 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001092 return ret;
1093}
1094
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001095static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1096{
1097 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001098 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001099 struct bucket *b;
1100 struct htab_elem *l;
1101 unsigned long flags;
1102 u32 hash, key_size;
1103 int ret = -ENOENT;
1104
1105 WARN_ON_ONCE(!rcu_read_lock_held());
1106
1107 key_size = map->key_size;
1108
Daniel Borkmannc0203472018-08-22 23:49:37 +02001109 hash = htab_map_hash(key, key_size, htab->hashrnd);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001110 b = __select_bucket(htab, hash);
1111 head = &b->head;
1112
1113 raw_spin_lock_irqsave(&b->lock, flags);
1114
1115 l = lookup_elem_raw(head, hash, key, key_size);
1116
1117 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001118 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001119 ret = 0;
1120 }
1121
1122 raw_spin_unlock_irqrestore(&b->lock, flags);
1123 if (l)
1124 bpf_lru_push_free(&htab->lru, &l->lru_node);
1125 return ret;
1126}
1127
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001128static void delete_all_elements(struct bpf_htab *htab)
1129{
1130 int i;
1131
1132 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001133 struct hlist_nulls_head *head = select_bucket(htab, i);
1134 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001135 struct htab_elem *l;
1136
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001137 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1138 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001139 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001140 }
1141 }
1142}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001143
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001144/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1145static void htab_map_free(struct bpf_map *map)
1146{
1147 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1148
1149 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1150 * so the programs (can be more than one that used this map) were
1151 * disconnected from events. Wait for outstanding critical sections in
1152 * these programs to complete
1153 */
1154 synchronize_rcu();
1155
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001156 /* some of free_htab_elem() callbacks for elements of this map may
1157 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001158 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001159 rcu_barrier();
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001160 if (!htab_is_prealloc(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001161 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001162 else
1163 prealloc_destroy(htab);
1164
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001165 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001166 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001167 kfree(htab);
1168}
1169
Yonghong Song699c86d2018-08-09 08:55:20 -07001170static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1171 struct seq_file *m)
1172{
1173 void *value;
1174
1175 rcu_read_lock();
1176
1177 value = htab_map_lookup_elem(map, key);
1178 if (!value) {
1179 rcu_read_unlock();
1180 return;
1181 }
1182
1183 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1184 seq_puts(m, ": ");
1185 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1186 seq_puts(m, "\n");
1187
1188 rcu_read_unlock();
1189}
1190
Johannes Berg40077e02017-04-11 15:34:58 +02001191const struct bpf_map_ops htab_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001192 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001193 .map_alloc = htab_map_alloc,
1194 .map_free = htab_map_free,
1195 .map_get_next_key = htab_map_get_next_key,
1196 .map_lookup_elem = htab_map_lookup_elem,
1197 .map_update_elem = htab_map_update_elem,
1198 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001199 .map_gen_lookup = htab_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001200 .map_seq_show_elem = htab_map_seq_show_elem,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001201};
1202
Johannes Berg40077e02017-04-11 15:34:58 +02001203const struct bpf_map_ops htab_lru_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001204 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001205 .map_alloc = htab_map_alloc,
1206 .map_free = htab_map_free,
1207 .map_get_next_key = htab_map_get_next_key,
1208 .map_lookup_elem = htab_lru_map_lookup_elem,
1209 .map_update_elem = htab_lru_map_update_elem,
1210 .map_delete_elem = htab_lru_map_delete_elem,
Martin KaFai Laucc555422017-08-31 23:27:12 -07001211 .map_gen_lookup = htab_lru_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001212 .map_seq_show_elem = htab_map_seq_show_elem,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001213};
1214
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001215/* Called from eBPF program */
1216static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1217{
1218 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1219
1220 if (l)
1221 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1222 else
1223 return NULL;
1224}
1225
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001226static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1227{
1228 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1229
1230 if (l) {
1231 bpf_lru_node_set_ref(&l->lru_node);
1232 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1233 }
1234
1235 return NULL;
1236}
1237
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001238int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1239{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001240 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001241 struct htab_elem *l;
1242 void __percpu *pptr;
1243 int ret = -ENOENT;
1244 int cpu, off = 0;
1245 u32 size;
1246
1247 /* per_cpu areas are zero-filled and bpf programs can only
1248 * access 'value_size' of them, so copying rounded areas
1249 * will not leak any kernel data
1250 */
1251 size = round_up(map->value_size, 8);
1252 rcu_read_lock();
1253 l = __htab_map_lookup_elem(map, key);
1254 if (!l)
1255 goto out;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001256 if (htab_is_lru(htab))
1257 bpf_lru_node_set_ref(&l->lru_node);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001258 pptr = htab_elem_get_ptr(l, map->key_size);
1259 for_each_possible_cpu(cpu) {
1260 bpf_long_memcpy(value + off,
1261 per_cpu_ptr(pptr, cpu), size);
1262 off += size;
1263 }
1264 ret = 0;
1265out:
1266 rcu_read_unlock();
1267 return ret;
1268}
1269
1270int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1271 u64 map_flags)
1272{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001273 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001274 int ret;
1275
1276 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001277 if (htab_is_lru(htab))
1278 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1279 map_flags, true);
1280 else
1281 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1282 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001283 rcu_read_unlock();
1284
1285 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001286}
1287
Yonghong Songc7b27c32018-08-29 14:43:13 -07001288static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1289 struct seq_file *m)
1290{
1291 struct htab_elem *l;
1292 void __percpu *pptr;
1293 int cpu;
1294
1295 rcu_read_lock();
1296
1297 l = __htab_map_lookup_elem(map, key);
1298 if (!l) {
1299 rcu_read_unlock();
1300 return;
1301 }
1302
1303 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1304 seq_puts(m, ": {\n");
1305 pptr = htab_elem_get_ptr(l, map->key_size);
1306 for_each_possible_cpu(cpu) {
1307 seq_printf(m, "\tcpu%d: ", cpu);
1308 btf_type_seq_show(map->btf, map->btf_value_type_id,
1309 per_cpu_ptr(pptr, cpu), m);
1310 seq_puts(m, "\n");
1311 }
1312 seq_puts(m, "}\n");
1313
1314 rcu_read_unlock();
1315}
1316
Johannes Berg40077e02017-04-11 15:34:58 +02001317const struct bpf_map_ops htab_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001318 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001319 .map_alloc = htab_map_alloc,
1320 .map_free = htab_map_free,
1321 .map_get_next_key = htab_map_get_next_key,
1322 .map_lookup_elem = htab_percpu_map_lookup_elem,
1323 .map_update_elem = htab_percpu_map_update_elem,
1324 .map_delete_elem = htab_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001325 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001326};
1327
Johannes Berg40077e02017-04-11 15:34:58 +02001328const struct bpf_map_ops htab_lru_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001329 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001330 .map_alloc = htab_map_alloc,
1331 .map_free = htab_map_free,
1332 .map_get_next_key = htab_map_get_next_key,
1333 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1334 .map_update_elem = htab_lru_percpu_map_update_elem,
1335 .map_delete_elem = htab_lru_map_delete_elem,
Yonghong Songc7b27c32018-08-29 14:43:13 -07001336 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001337};
1338
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001339static int fd_htab_map_alloc_check(union bpf_attr *attr)
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001340{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001341 if (attr->value_size != sizeof(u32))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001342 return -EINVAL;
1343 return htab_map_alloc_check(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001344}
1345
1346static void fd_htab_map_free(struct bpf_map *map)
1347{
1348 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1349 struct hlist_nulls_node *n;
1350 struct hlist_nulls_head *head;
1351 struct htab_elem *l;
1352 int i;
1353
1354 for (i = 0; i < htab->n_buckets; i++) {
1355 head = select_bucket(htab, i);
1356
1357 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1358 void *ptr = fd_htab_map_get_ptr(map, l);
1359
1360 map->ops->map_fd_put_ptr(ptr);
1361 }
1362 }
1363
1364 htab_map_free(map);
1365}
1366
1367/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001368int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1369{
1370 void **ptr;
1371 int ret = 0;
1372
1373 if (!map->ops->map_fd_sys_lookup_elem)
1374 return -ENOTSUPP;
1375
1376 rcu_read_lock();
1377 ptr = htab_map_lookup_elem(map, key);
1378 if (ptr)
1379 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1380 else
1381 ret = -ENOENT;
1382 rcu_read_unlock();
1383
1384 return ret;
1385}
1386
1387/* only called from syscall */
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001388int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1389 void *key, void *value, u64 map_flags)
1390{
1391 void *ptr;
1392 int ret;
1393 u32 ufd = *(u32 *)value;
1394
1395 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1396 if (IS_ERR(ptr))
1397 return PTR_ERR(ptr);
1398
1399 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1400 if (ret)
1401 map->ops->map_fd_put_ptr(ptr);
1402
1403 return ret;
1404}
1405
1406static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1407{
1408 struct bpf_map *map, *inner_map_meta;
1409
1410 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1411 if (IS_ERR(inner_map_meta))
1412 return inner_map_meta;
1413
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001414 map = htab_map_alloc(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001415 if (IS_ERR(map)) {
1416 bpf_map_meta_free(inner_map_meta);
1417 return map;
1418 }
1419
1420 map->inner_map_meta = inner_map_meta;
1421
1422 return map;
1423}
1424
1425static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1426{
1427 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1428
1429 if (!inner_map)
1430 return NULL;
1431
1432 return READ_ONCE(*inner_map);
1433}
1434
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001435static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1436 struct bpf_insn *insn_buf)
1437{
1438 struct bpf_insn *insn = insn_buf;
1439 const int ret = BPF_REG_0;
1440
Daniel Borkmann09772d92018-06-02 23:06:35 +02001441 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1442 (void *(*)(struct bpf_map *map, void *key))NULL));
1443 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001444 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1445 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1446 offsetof(struct htab_elem, key) +
1447 round_up(map->key_size, 8));
1448 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1449
1450 return insn - insn_buf;
1451}
1452
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001453static void htab_of_map_free(struct bpf_map *map)
1454{
1455 bpf_map_meta_free(map->inner_map_meta);
1456 fd_htab_map_free(map);
1457}
1458
Johannes Berg40077e02017-04-11 15:34:58 +02001459const struct bpf_map_ops htab_of_maps_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001460 .map_alloc_check = fd_htab_map_alloc_check,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001461 .map_alloc = htab_of_map_alloc,
1462 .map_free = htab_of_map_free,
1463 .map_get_next_key = htab_map_get_next_key,
1464 .map_lookup_elem = htab_of_map_lookup_elem,
1465 .map_delete_elem = htab_map_delete_elem,
1466 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1467 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001468 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001469 .map_gen_lookup = htab_of_map_gen_lookup,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001470 .map_check_btf = map_check_no_btf,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001471};