blob: b80f42adf0686ae5e31071e86a06de02c41a9f5a [file] [log] [blame]
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov6c905982016-03-07 21:57:15 -08002 * Copyright (c) 2016 Facebook
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/bpf.h>
14#include <linux/jhash.h>
15#include <linux/filter.h>
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080016#include <linux/rculist_nulls.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080017#include "percpu_freelist.h"
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080018#include "bpf_lru_list.h"
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070019#include "map_in_map.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080020
Chenbo Feng6e71b042017-10-18 13:00:22 -070021#define HTAB_CREATE_FLAG_MASK \
22 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
23 BPF_F_RDONLY | BPF_F_WRONLY)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070024
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080025struct bucket {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080026 struct hlist_nulls_head head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080027 raw_spinlock_t lock;
28};
29
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080030struct bpf_htab {
31 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080032 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080033 void *elems;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080034 union {
35 struct pcpu_freelist freelist;
36 struct bpf_lru lru;
37 };
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070038 struct htab_elem *__percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080039 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080040 u32 n_buckets; /* number of hash buckets */
41 u32 elem_size; /* size of each element in bytes */
42};
43
44/* each htab element is struct htab_elem + key + value */
45struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080046 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080047 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080048 struct {
49 void *padding;
50 union {
51 struct bpf_htab *htab;
52 struct pcpu_freelist_node fnode;
53 };
54 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080055 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070056 union {
57 struct rcu_head rcu;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080058 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070059 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080060 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080061 char key[0] __aligned(8);
62};
63
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080064static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
65
66static bool htab_is_lru(const struct bpf_htab *htab)
67{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080068 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
69 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
70}
71
72static bool htab_is_percpu(const struct bpf_htab *htab)
73{
74 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
75 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080076}
77
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070078static bool htab_is_prealloc(const struct bpf_htab *htab)
79{
80 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
81}
82
Alexei Starovoitov6c905982016-03-07 21:57:15 -080083static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
84 void __percpu *pptr)
85{
86 *(void __percpu **)(l->key + key_size) = pptr;
87}
88
89static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
90{
91 return *(void __percpu **)(l->key + key_size);
92}
93
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070094static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
95{
96 return *(void **)(l->key + roundup(map->key_size, 8));
97}
98
Alexei Starovoitov6c905982016-03-07 21:57:15 -080099static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
100{
101 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
102}
103
104static void htab_free_elems(struct bpf_htab *htab)
105{
106 int i;
107
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800108 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800109 goto free_elems;
110
111 for (i = 0; i < htab->map.max_entries; i++) {
112 void __percpu *pptr;
113
114 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
115 htab->map.key_size);
116 free_percpu(pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800117 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800118 }
119free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100120 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800121}
122
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800123static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
124 u32 hash)
125{
126 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
127 struct htab_elem *l;
128
129 if (node) {
130 l = container_of(node, struct htab_elem, lru_node);
131 memcpy(l->key, key, htab->map.key_size);
132 return l;
133 }
134
135 return NULL;
136}
137
138static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800139{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700140 u32 num_entries = htab->map.max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800141 int err = -ENOMEM, i;
142
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700143 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
144 num_entries += num_possible_cpus();
145
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700146 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
147 htab->map.numa_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800148 if (!htab->elems)
149 return -ENOMEM;
150
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800151 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800152 goto skip_percpu_elems;
153
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700154 for (i = 0; i < num_entries; i++) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800155 u32 size = round_up(htab->map.value_size, 8);
156 void __percpu *pptr;
157
158 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
159 if (!pptr)
160 goto free_elems;
161 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
162 pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800163 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800164 }
165
166skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800167 if (htab_is_lru(htab))
168 err = bpf_lru_init(&htab->lru,
169 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
170 offsetof(struct htab_elem, hash) -
171 offsetof(struct htab_elem, lru_node),
172 htab_lru_map_delete_node,
173 htab);
174 else
175 err = pcpu_freelist_init(&htab->freelist);
176
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800177 if (err)
178 goto free_elems;
179
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800180 if (htab_is_lru(htab))
181 bpf_lru_populate(&htab->lru, htab->elems,
182 offsetof(struct htab_elem, lru_node),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700183 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800184 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800185 pcpu_freelist_populate(&htab->freelist,
186 htab->elems + offsetof(struct htab_elem, fnode),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700187 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800188
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800189 return 0;
190
191free_elems:
192 htab_free_elems(htab);
193 return err;
194}
195
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800196static void prealloc_destroy(struct bpf_htab *htab)
197{
198 htab_free_elems(htab);
199
200 if (htab_is_lru(htab))
201 bpf_lru_destroy(&htab->lru);
202 else
203 pcpu_freelist_destroy(&htab->freelist);
204}
205
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700206static int alloc_extra_elems(struct bpf_htab *htab)
207{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700208 struct htab_elem *__percpu *pptr, *l_new;
209 struct pcpu_freelist_node *l;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700210 int cpu;
211
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700212 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
213 GFP_USER | __GFP_NOWARN);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700214 if (!pptr)
215 return -ENOMEM;
216
217 for_each_possible_cpu(cpu) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700218 l = pcpu_freelist_pop(&htab->freelist);
219 /* pop will succeed, since prealloc_init()
220 * preallocated extra num_possible_cpus elements
221 */
222 l_new = container_of(l, struct htab_elem, fnode);
223 *per_cpu_ptr(pptr, cpu) = l_new;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700224 }
225 htab->extra_elems = pptr;
226 return 0;
227}
228
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800229/* Called from syscall */
230static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
231{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800232 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
233 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
234 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
235 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800236 /* percpu_lru means each cpu has its own LRU list.
237 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
238 * the map's value itself is percpu. percpu_lru has
239 * nothing to do with the map's value.
240 */
241 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
242 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700243 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800244 struct bpf_htab *htab;
245 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800246 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800247
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800248 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
249 offsetof(struct htab_elem, hash_node.pprev));
250 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
251 offsetof(struct htab_elem, hash_node.pprev));
252
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800253 if (lru && !capable(CAP_SYS_ADMIN))
254 /* LRU implementation is much complicated than other
255 * maps. Hence, limit to CAP_SYS_ADMIN for now.
256 */
257 return ERR_PTR(-EPERM);
258
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700259 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800260 /* reserved bits should not be used */
261 return ERR_PTR(-EINVAL);
262
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800263 if (!lru && percpu_lru)
264 return ERR_PTR(-EINVAL);
265
266 if (lru && !prealloc)
267 return ERR_PTR(-ENOTSUPP);
268
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700269 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
270 return ERR_PTR(-EINVAL);
271
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800272 /* check sanity of attributes.
273 * value_size == 0 may be allowed in the future to use map as a set
274 */
275 if (attr->max_entries == 0 || attr->key_size == 0 ||
276 attr->value_size == 0)
277 return ERR_PTR(-EINVAL);
278
279 if (attr->key_size > MAX_BPF_STACK)
280 /* eBPF programs initialize keys on stack, so they cannot be
281 * larger than max stack size
282 */
283 return ERR_PTR(-E2BIG);
284
285 if (attr->value_size >= KMALLOC_MAX_SIZE -
286 MAX_BPF_STACK - sizeof(struct htab_elem))
287 /* if value_size is bigger, the user space won't be able to
288 * access the elements via bpf syscall. This check also makes
289 * sure that the elem_size doesn't overflow and it's
290 * kmalloc-able later in htab_map_update_elem()
291 */
292 return ERR_PTR(-E2BIG);
293
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800294 htab = kzalloc(sizeof(*htab), GFP_USER);
295 if (!htab)
296 return ERR_PTR(-ENOMEM);
297
298 /* mandatory map attributes */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800299 htab->map.map_type = attr->map_type;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800300 htab->map.key_size = attr->key_size;
301 htab->map.value_size = attr->value_size;
302 htab->map.max_entries = attr->max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800303 htab->map.map_flags = attr->map_flags;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700304 htab->map.numa_node = numa_node;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800305
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800306 if (percpu_lru) {
307 /* ensure each CPU's lru list has >=1 elements.
308 * since we are at it, make each lru list has the same
309 * number of elements.
310 */
311 htab->map.max_entries = roundup(attr->max_entries,
312 num_possible_cpus());
313 if (htab->map.max_entries < attr->max_entries)
314 htab->map.max_entries = rounddown(attr->max_entries,
315 num_possible_cpus());
316 }
317
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800318 /* hash table size must be power of 2 */
319 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
320
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800321 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800322 round_up(htab->map.key_size, 8);
323 if (percpu)
324 htab->elem_size += sizeof(void *);
325 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800326 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800327
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800328 err = -E2BIG;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800329 /* prevent zero size kmalloc and check for u32 overflow */
330 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800331 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800332 goto free_htab;
333
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800334 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
335 (u64) htab->elem_size * htab->map.max_entries;
336
337 if (percpu)
338 cost += (u64) round_up(htab->map.value_size, 8) *
339 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700340 else
341 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800342
343 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800344 /* make sure page count doesn't overflow */
345 goto free_htab;
346
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800347 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800348
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800349 /* if map size is larger than memlock limit, reject it early */
350 err = bpf_map_precharge_memlock(htab->map.pages);
351 if (err)
352 goto free_htab;
353
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800354 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100355 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700356 sizeof(struct bucket),
357 htab->map.numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100358 if (!htab->buckets)
359 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800360
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800361 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800362 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800363 raw_spin_lock_init(&htab->buckets[i].lock);
364 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800365
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800366 if (prealloc) {
367 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700368 if (err)
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700369 goto free_buckets;
370
371 if (!percpu && !lru) {
372 /* lru itself can remove the least used element, so
373 * there is no need for an extra elem during map_update.
374 */
375 err = alloc_extra_elems(htab);
376 if (err)
377 goto free_prealloc;
378 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700379 }
380
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800381 return &htab->map;
382
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700383free_prealloc:
384 prealloc_destroy(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800385free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100386 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800387free_htab:
388 kfree(htab);
389 return ERR_PTR(err);
390}
391
392static inline u32 htab_map_hash(const void *key, u32 key_len)
393{
394 return jhash(key, key_len, 0);
395}
396
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800397static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800398{
399 return &htab->buckets[hash & (htab->n_buckets - 1)];
400}
401
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800402static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800403{
404 return &__select_bucket(htab, hash)->head;
405}
406
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800407/* this lookup function can only be called with bucket lock taken */
408static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800409 void *key, u32 key_size)
410{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800411 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800412 struct htab_elem *l;
413
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800414 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800415 if (l->hash == hash && !memcmp(&l->key, key, key_size))
416 return l;
417
418 return NULL;
419}
420
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800421/* can be called without bucket lock. it will repeat the loop in
422 * the unlikely event when elements moved from one bucket into another
423 * while link list is being walked
424 */
425static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
426 u32 hash, void *key,
427 u32 key_size, u32 n_buckets)
428{
429 struct hlist_nulls_node *n;
430 struct htab_elem *l;
431
432again:
433 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
434 if (l->hash == hash && !memcmp(&l->key, key, key_size))
435 return l;
436
437 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
438 goto again;
439
440 return NULL;
441}
442
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700443/* Called from syscall or from eBPF program directly, so
444 * arguments have to match bpf_map_lookup_elem() exactly.
445 * The return value is adjusted by BPF instructions
446 * in htab_map_gen_lookup().
447 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800448static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800449{
450 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800451 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800452 struct htab_elem *l;
453 u32 hash, key_size;
454
455 /* Must be called with rcu_read_lock. */
456 WARN_ON_ONCE(!rcu_read_lock_held());
457
458 key_size = map->key_size;
459
460 hash = htab_map_hash(key, key_size);
461
462 head = select_bucket(htab, hash);
463
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800464 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800465
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800466 return l;
467}
468
469static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
470{
471 struct htab_elem *l = __htab_map_lookup_elem(map, key);
472
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800473 if (l)
474 return l->key + round_up(map->key_size, 8);
475
476 return NULL;
477}
478
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700479/* inline bpf_map_lookup_elem() call.
480 * Instead of:
481 * bpf_prog
482 * bpf_map_lookup_elem
483 * map->ops->map_lookup_elem
484 * htab_map_lookup_elem
485 * __htab_map_lookup_elem
486 * do:
487 * bpf_prog
488 * __htab_map_lookup_elem
489 */
490static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
491{
492 struct bpf_insn *insn = insn_buf;
493 const int ret = BPF_REG_0;
494
495 *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem);
496 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
497 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
498 offsetof(struct htab_elem, key) +
499 round_up(map->key_size, 8));
500 return insn - insn_buf;
501}
502
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800503static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
504{
505 struct htab_elem *l = __htab_map_lookup_elem(map, key);
506
507 if (l) {
508 bpf_lru_node_set_ref(&l->lru_node);
509 return l->key + round_up(map->key_size, 8);
510 }
511
512 return NULL;
513}
514
Martin KaFai Laucc555422017-08-31 23:27:12 -0700515static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
516 struct bpf_insn *insn_buf)
517{
518 struct bpf_insn *insn = insn_buf;
519 const int ret = BPF_REG_0;
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700520 const int ref_reg = BPF_REG_1;
Martin KaFai Laucc555422017-08-31 23:27:12 -0700521
522 *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem);
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700523 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
524 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
525 offsetof(struct htab_elem, lru_node) +
526 offsetof(struct bpf_lru_node, ref));
527 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
Martin KaFai Laucc555422017-08-31 23:27:12 -0700528 *insn++ = BPF_ST_MEM(BPF_B, ret,
529 offsetof(struct htab_elem, lru_node) +
530 offsetof(struct bpf_lru_node, ref),
531 1);
532 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
533 offsetof(struct htab_elem, key) +
534 round_up(map->key_size, 8));
535 return insn - insn_buf;
536}
537
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800538/* It is called from the bpf_lru_list when the LRU needs to delete
539 * older elements from the htab.
540 */
541static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
542{
543 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800544 struct htab_elem *l = NULL, *tgt_l;
545 struct hlist_nulls_head *head;
546 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800547 unsigned long flags;
548 struct bucket *b;
549
550 tgt_l = container_of(node, struct htab_elem, lru_node);
551 b = __select_bucket(htab, tgt_l->hash);
552 head = &b->head;
553
554 raw_spin_lock_irqsave(&b->lock, flags);
555
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800556 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800557 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800558 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800559 break;
560 }
561
562 raw_spin_unlock_irqrestore(&b->lock, flags);
563
564 return l == tgt_l;
565}
566
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800567/* Called from syscall */
568static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
569{
570 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800571 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800572 struct htab_elem *l, *next_l;
573 u32 hash, key_size;
Teng Qin8fe45922017-04-24 19:00:37 -0700574 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800575
576 WARN_ON_ONCE(!rcu_read_lock_held());
577
578 key_size = map->key_size;
579
Teng Qin8fe45922017-04-24 19:00:37 -0700580 if (!key)
581 goto find_first_elem;
582
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800583 hash = htab_map_hash(key, key_size);
584
585 head = select_bucket(htab, hash);
586
587 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800588 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800589
Teng Qin8fe45922017-04-24 19:00:37 -0700590 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800591 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800592
593 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800594 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800595 struct htab_elem, hash_node);
596
597 if (next_l) {
598 /* if next elem in this hash list is non-zero, just return it */
599 memcpy(next_key, next_l->key, key_size);
600 return 0;
601 }
602
603 /* no more elements in this hash list, go to the next bucket */
604 i = hash & (htab->n_buckets - 1);
605 i++;
606
607find_first_elem:
608 /* iterate over buckets */
609 for (; i < htab->n_buckets; i++) {
610 head = select_bucket(htab, i);
611
612 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800613 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800614 struct htab_elem, hash_node);
615 if (next_l) {
616 /* if it's not empty, just return it */
617 memcpy(next_key, next_l->key, key_size);
618 return 0;
619 }
620 }
621
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800622 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800623 return -ENOENT;
624}
625
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800626static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800627{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800628 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
629 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800630 kfree(l);
631}
632
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800633static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800634{
635 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800636 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800637
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800638 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
639 * we're calling kfree, otherwise deadlock is possible if kprobes
640 * are placed somewhere inside of slub
641 */
642 preempt_disable();
643 __this_cpu_inc(bpf_prog_active);
644 htab_elem_free(htab, l);
645 __this_cpu_dec(bpf_prog_active);
646 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800647}
648
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800649static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800650{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700651 struct bpf_map *map = &htab->map;
652
653 if (map->ops->map_fd_put_ptr) {
654 void *ptr = fd_htab_map_get_ptr(map, l);
655
656 map->ops->map_fd_put_ptr(ptr);
657 }
658
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700659 if (htab_is_prealloc(htab)) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800660 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800661 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800662 atomic_dec(&htab->count);
663 l->htab = htab;
664 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800665 }
666}
667
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800668static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
669 void *value, bool onallcpus)
670{
671 if (!onallcpus) {
672 /* copy true value_size bytes */
673 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
674 } else {
675 u32 size = round_up(htab->map.value_size, 8);
676 int off = 0, cpu;
677
678 for_each_possible_cpu(cpu) {
679 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
680 value + off, size);
681 off += size;
682 }
683 }
684}
685
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200686static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
687{
688 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
689 BITS_PER_LONG == 64;
690}
691
692static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
693{
694 u32 size = htab->map.value_size;
695
696 if (percpu || fd_htab_map_needs_adjust(htab))
697 size = round_up(size, 8);
698 return size;
699}
700
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800701static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
702 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700703 bool percpu, bool onallcpus,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700704 struct htab_elem *old_elem)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800705{
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200706 u32 size = htab_size_value(htab, percpu);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700707 bool prealloc = htab_is_prealloc(htab);
708 struct htab_elem *l_new, **pl_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800709 void __percpu *pptr;
710
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800711 if (prealloc) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700712 if (old_elem) {
713 /* if we're updating the existing element,
714 * use per-cpu extra elems to avoid freelist_pop/push
715 */
716 pl_new = this_cpu_ptr(htab->extra_elems);
717 l_new = *pl_new;
718 *pl_new = old_elem;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700719 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700720 struct pcpu_freelist_node *l;
721
722 l = pcpu_freelist_pop(&htab->freelist);
723 if (!l)
724 return ERR_PTR(-E2BIG);
725 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800726 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700727 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700728 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
729 if (!old_elem) {
730 /* when map is full and update() is replacing
731 * old element, it's ok to allocate, since
732 * old element will be freed immediately.
733 * Otherwise return an error
734 */
735 atomic_dec(&htab->count);
736 return ERR_PTR(-E2BIG);
737 }
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700738 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
739 htab->map.numa_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700740 if (!l_new)
741 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800742 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800743
744 memcpy(l_new->key, key, key_size);
745 if (percpu) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800746 if (prealloc) {
747 pptr = htab_elem_get_ptr(l_new, key_size);
748 } else {
749 /* alloc_percpu zero-fills */
750 pptr = __alloc_percpu_gfp(size, 8,
751 GFP_ATOMIC | __GFP_NOWARN);
752 if (!pptr) {
753 kfree(l_new);
754 return ERR_PTR(-ENOMEM);
755 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800756 }
757
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800758 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800759
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800760 if (!prealloc)
761 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800762 } else {
763 memcpy(l_new->key + round_up(key_size, 8), value, size);
764 }
765
766 l_new->hash = hash;
767 return l_new;
768}
769
770static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
771 u64 map_flags)
772{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800773 if (l_old && map_flags == BPF_NOEXIST)
774 /* elem already exists */
775 return -EEXIST;
776
777 if (!l_old && map_flags == BPF_EXIST)
778 /* elem doesn't exist, cannot update it */
779 return -ENOENT;
780
781 return 0;
782}
783
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800784/* Called from syscall or from eBPF program */
785static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
786 u64 map_flags)
787{
788 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800789 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800790 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800791 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800792 struct bucket *b;
793 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800794 int ret;
795
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800796 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800797 /* unknown flags */
798 return -EINVAL;
799
800 WARN_ON_ONCE(!rcu_read_lock_held());
801
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800802 key_size = map->key_size;
803
804 hash = htab_map_hash(key, key_size);
805
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800806 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800807 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800808
809 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800810 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800811
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800812 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800813
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800814 ret = check_flags(htab, l_old, map_flags);
815 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800816 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800817
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700818 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700819 l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800820 if (IS_ERR(l_new)) {
821 /* all pre-allocated elements are in use or memory exhausted */
822 ret = PTR_ERR(l_new);
823 goto err;
824 }
825
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800826 /* add new element to the head of the list, so that
827 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800828 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800829 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800830 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800831 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700832 if (!htab_is_prealloc(htab))
833 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800834 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800835 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800836err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800837 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800838 return ret;
839}
840
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800841static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
842 u64 map_flags)
843{
844 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
845 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800846 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800847 unsigned long flags;
848 struct bucket *b;
849 u32 key_size, hash;
850 int ret;
851
852 if (unlikely(map_flags > BPF_EXIST))
853 /* unknown flags */
854 return -EINVAL;
855
856 WARN_ON_ONCE(!rcu_read_lock_held());
857
858 key_size = map->key_size;
859
860 hash = htab_map_hash(key, key_size);
861
862 b = __select_bucket(htab, hash);
863 head = &b->head;
864
865 /* For LRU, we need to alloc before taking bucket's
866 * spinlock because getting free nodes from LRU may need
867 * to remove older elements from htab and this removal
868 * operation will need a bucket lock.
869 */
870 l_new = prealloc_lru_pop(htab, key, hash);
871 if (!l_new)
872 return -ENOMEM;
873 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
874
875 /* bpf_map_update_elem() can be called in_irq() */
876 raw_spin_lock_irqsave(&b->lock, flags);
877
878 l_old = lookup_elem_raw(head, hash, key, key_size);
879
880 ret = check_flags(htab, l_old, map_flags);
881 if (ret)
882 goto err;
883
884 /* add new element to the head of the list, so that
885 * concurrent search will find it before old elem
886 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800887 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800888 if (l_old) {
889 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800890 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800891 }
892 ret = 0;
893
894err:
895 raw_spin_unlock_irqrestore(&b->lock, flags);
896
897 if (ret)
898 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
899 else if (l_old)
900 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
901
902 return ret;
903}
904
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800905static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
906 void *value, u64 map_flags,
907 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800908{
909 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
910 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800911 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800912 unsigned long flags;
913 struct bucket *b;
914 u32 key_size, hash;
915 int ret;
916
917 if (unlikely(map_flags > BPF_EXIST))
918 /* unknown flags */
919 return -EINVAL;
920
921 WARN_ON_ONCE(!rcu_read_lock_held());
922
923 key_size = map->key_size;
924
925 hash = htab_map_hash(key, key_size);
926
927 b = __select_bucket(htab, hash);
928 head = &b->head;
929
930 /* bpf_map_update_elem() can be called in_irq() */
931 raw_spin_lock_irqsave(&b->lock, flags);
932
933 l_old = lookup_elem_raw(head, hash, key, key_size);
934
935 ret = check_flags(htab, l_old, map_flags);
936 if (ret)
937 goto err;
938
939 if (l_old) {
940 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800941 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
942 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800943 } else {
944 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700945 hash, true, onallcpus, NULL);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800946 if (IS_ERR(l_new)) {
947 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800948 goto err;
949 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800950 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800951 }
952 ret = 0;
953err:
954 raw_spin_unlock_irqrestore(&b->lock, flags);
955 return ret;
956}
957
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800958static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
959 void *value, u64 map_flags,
960 bool onallcpus)
961{
962 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
963 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800964 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800965 unsigned long flags;
966 struct bucket *b;
967 u32 key_size, hash;
968 int ret;
969
970 if (unlikely(map_flags > BPF_EXIST))
971 /* unknown flags */
972 return -EINVAL;
973
974 WARN_ON_ONCE(!rcu_read_lock_held());
975
976 key_size = map->key_size;
977
978 hash = htab_map_hash(key, key_size);
979
980 b = __select_bucket(htab, hash);
981 head = &b->head;
982
983 /* For LRU, we need to alloc before taking bucket's
984 * spinlock because LRU's elem alloc may need
985 * to remove older elem from htab and this removal
986 * operation will need a bucket lock.
987 */
988 if (map_flags != BPF_EXIST) {
989 l_new = prealloc_lru_pop(htab, key, hash);
990 if (!l_new)
991 return -ENOMEM;
992 }
993
994 /* bpf_map_update_elem() can be called in_irq() */
995 raw_spin_lock_irqsave(&b->lock, flags);
996
997 l_old = lookup_elem_raw(head, hash, key, key_size);
998
999 ret = check_flags(htab, l_old, map_flags);
1000 if (ret)
1001 goto err;
1002
1003 if (l_old) {
1004 bpf_lru_node_set_ref(&l_old->lru_node);
1005
1006 /* per-cpu hash map can update value in-place */
1007 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1008 value, onallcpus);
1009 } else {
1010 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1011 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001012 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001013 l_new = NULL;
1014 }
1015 ret = 0;
1016err:
1017 raw_spin_unlock_irqrestore(&b->lock, flags);
1018 if (l_new)
1019 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1020 return ret;
1021}
1022
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001023static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1024 void *value, u64 map_flags)
1025{
1026 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1027}
1028
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001029static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1030 void *value, u64 map_flags)
1031{
1032 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1033 false);
1034}
1035
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001036/* Called from syscall or from eBPF program */
1037static int htab_map_delete_elem(struct bpf_map *map, void *key)
1038{
1039 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001040 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001041 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001042 struct htab_elem *l;
1043 unsigned long flags;
1044 u32 hash, key_size;
1045 int ret = -ENOENT;
1046
1047 WARN_ON_ONCE(!rcu_read_lock_held());
1048
1049 key_size = map->key_size;
1050
1051 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001052 b = __select_bucket(htab, hash);
1053 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001054
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001055 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001056
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001057 l = lookup_elem_raw(head, hash, key, key_size);
1058
1059 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001060 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001061 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001062 ret = 0;
1063 }
1064
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001065 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001066 return ret;
1067}
1068
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001069static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1070{
1071 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001072 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001073 struct bucket *b;
1074 struct htab_elem *l;
1075 unsigned long flags;
1076 u32 hash, key_size;
1077 int ret = -ENOENT;
1078
1079 WARN_ON_ONCE(!rcu_read_lock_held());
1080
1081 key_size = map->key_size;
1082
1083 hash = htab_map_hash(key, key_size);
1084 b = __select_bucket(htab, hash);
1085 head = &b->head;
1086
1087 raw_spin_lock_irqsave(&b->lock, flags);
1088
1089 l = lookup_elem_raw(head, hash, key, key_size);
1090
1091 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001092 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001093 ret = 0;
1094 }
1095
1096 raw_spin_unlock_irqrestore(&b->lock, flags);
1097 if (l)
1098 bpf_lru_push_free(&htab->lru, &l->lru_node);
1099 return ret;
1100}
1101
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001102static void delete_all_elements(struct bpf_htab *htab)
1103{
1104 int i;
1105
1106 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001107 struct hlist_nulls_head *head = select_bucket(htab, i);
1108 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001109 struct htab_elem *l;
1110
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001111 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1112 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001113 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001114 }
1115 }
1116}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001117
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001118/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1119static void htab_map_free(struct bpf_map *map)
1120{
1121 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1122
1123 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1124 * so the programs (can be more than one that used this map) were
1125 * disconnected from events. Wait for outstanding critical sections in
1126 * these programs to complete
1127 */
1128 synchronize_rcu();
1129
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001130 /* some of free_htab_elem() callbacks for elements of this map may
1131 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001132 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001133 rcu_barrier();
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001134 if (!htab_is_prealloc(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001135 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001136 else
1137 prealloc_destroy(htab);
1138
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001139 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001140 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001141 kfree(htab);
1142}
1143
Johannes Berg40077e02017-04-11 15:34:58 +02001144const struct bpf_map_ops htab_map_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001145 .map_alloc = htab_map_alloc,
1146 .map_free = htab_map_free,
1147 .map_get_next_key = htab_map_get_next_key,
1148 .map_lookup_elem = htab_map_lookup_elem,
1149 .map_update_elem = htab_map_update_elem,
1150 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001151 .map_gen_lookup = htab_map_gen_lookup,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001152};
1153
Johannes Berg40077e02017-04-11 15:34:58 +02001154const struct bpf_map_ops htab_lru_map_ops = {
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001155 .map_alloc = htab_map_alloc,
1156 .map_free = htab_map_free,
1157 .map_get_next_key = htab_map_get_next_key,
1158 .map_lookup_elem = htab_lru_map_lookup_elem,
1159 .map_update_elem = htab_lru_map_update_elem,
1160 .map_delete_elem = htab_lru_map_delete_elem,
Martin KaFai Laucc555422017-08-31 23:27:12 -07001161 .map_gen_lookup = htab_lru_map_gen_lookup,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001162};
1163
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001164/* Called from eBPF program */
1165static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1166{
1167 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1168
1169 if (l)
1170 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1171 else
1172 return NULL;
1173}
1174
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001175static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1176{
1177 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1178
1179 if (l) {
1180 bpf_lru_node_set_ref(&l->lru_node);
1181 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1182 }
1183
1184 return NULL;
1185}
1186
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001187int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1188{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001189 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001190 struct htab_elem *l;
1191 void __percpu *pptr;
1192 int ret = -ENOENT;
1193 int cpu, off = 0;
1194 u32 size;
1195
1196 /* per_cpu areas are zero-filled and bpf programs can only
1197 * access 'value_size' of them, so copying rounded areas
1198 * will not leak any kernel data
1199 */
1200 size = round_up(map->value_size, 8);
1201 rcu_read_lock();
1202 l = __htab_map_lookup_elem(map, key);
1203 if (!l)
1204 goto out;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001205 if (htab_is_lru(htab))
1206 bpf_lru_node_set_ref(&l->lru_node);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001207 pptr = htab_elem_get_ptr(l, map->key_size);
1208 for_each_possible_cpu(cpu) {
1209 bpf_long_memcpy(value + off,
1210 per_cpu_ptr(pptr, cpu), size);
1211 off += size;
1212 }
1213 ret = 0;
1214out:
1215 rcu_read_unlock();
1216 return ret;
1217}
1218
1219int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1220 u64 map_flags)
1221{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001222 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001223 int ret;
1224
1225 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001226 if (htab_is_lru(htab))
1227 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1228 map_flags, true);
1229 else
1230 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1231 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001232 rcu_read_unlock();
1233
1234 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001235}
1236
Johannes Berg40077e02017-04-11 15:34:58 +02001237const struct bpf_map_ops htab_percpu_map_ops = {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001238 .map_alloc = htab_map_alloc,
1239 .map_free = htab_map_free,
1240 .map_get_next_key = htab_map_get_next_key,
1241 .map_lookup_elem = htab_percpu_map_lookup_elem,
1242 .map_update_elem = htab_percpu_map_update_elem,
1243 .map_delete_elem = htab_map_delete_elem,
1244};
1245
Johannes Berg40077e02017-04-11 15:34:58 +02001246const struct bpf_map_ops htab_lru_percpu_map_ops = {
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001247 .map_alloc = htab_map_alloc,
1248 .map_free = htab_map_free,
1249 .map_get_next_key = htab_map_get_next_key,
1250 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1251 .map_update_elem = htab_lru_percpu_map_update_elem,
1252 .map_delete_elem = htab_lru_map_delete_elem,
1253};
1254
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001255static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr)
1256{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001257 if (attr->value_size != sizeof(u32))
1258 return ERR_PTR(-EINVAL);
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +02001259 return htab_map_alloc(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001260}
1261
1262static void fd_htab_map_free(struct bpf_map *map)
1263{
1264 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1265 struct hlist_nulls_node *n;
1266 struct hlist_nulls_head *head;
1267 struct htab_elem *l;
1268 int i;
1269
1270 for (i = 0; i < htab->n_buckets; i++) {
1271 head = select_bucket(htab, i);
1272
1273 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1274 void *ptr = fd_htab_map_get_ptr(map, l);
1275
1276 map->ops->map_fd_put_ptr(ptr);
1277 }
1278 }
1279
1280 htab_map_free(map);
1281}
1282
1283/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001284int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1285{
1286 void **ptr;
1287 int ret = 0;
1288
1289 if (!map->ops->map_fd_sys_lookup_elem)
1290 return -ENOTSUPP;
1291
1292 rcu_read_lock();
1293 ptr = htab_map_lookup_elem(map, key);
1294 if (ptr)
1295 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1296 else
1297 ret = -ENOENT;
1298 rcu_read_unlock();
1299
1300 return ret;
1301}
1302
1303/* only called from syscall */
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001304int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1305 void *key, void *value, u64 map_flags)
1306{
1307 void *ptr;
1308 int ret;
1309 u32 ufd = *(u32 *)value;
1310
1311 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1312 if (IS_ERR(ptr))
1313 return PTR_ERR(ptr);
1314
1315 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1316 if (ret)
1317 map->ops->map_fd_put_ptr(ptr);
1318
1319 return ret;
1320}
1321
1322static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1323{
1324 struct bpf_map *map, *inner_map_meta;
1325
1326 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1327 if (IS_ERR(inner_map_meta))
1328 return inner_map_meta;
1329
1330 map = fd_htab_map_alloc(attr);
1331 if (IS_ERR(map)) {
1332 bpf_map_meta_free(inner_map_meta);
1333 return map;
1334 }
1335
1336 map->inner_map_meta = inner_map_meta;
1337
1338 return map;
1339}
1340
1341static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1342{
1343 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1344
1345 if (!inner_map)
1346 return NULL;
1347
1348 return READ_ONCE(*inner_map);
1349}
1350
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001351static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1352 struct bpf_insn *insn_buf)
1353{
1354 struct bpf_insn *insn = insn_buf;
1355 const int ret = BPF_REG_0;
1356
1357 *insn++ = BPF_EMIT_CALL((u64 (*)(u64, u64, u64, u64, u64))__htab_map_lookup_elem);
1358 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1359 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1360 offsetof(struct htab_elem, key) +
1361 round_up(map->key_size, 8));
1362 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1363
1364 return insn - insn_buf;
1365}
1366
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001367static void htab_of_map_free(struct bpf_map *map)
1368{
1369 bpf_map_meta_free(map->inner_map_meta);
1370 fd_htab_map_free(map);
1371}
1372
Johannes Berg40077e02017-04-11 15:34:58 +02001373const struct bpf_map_ops htab_of_maps_map_ops = {
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001374 .map_alloc = htab_of_map_alloc,
1375 .map_free = htab_of_map_free,
1376 .map_get_next_key = htab_map_get_next_key,
1377 .map_lookup_elem = htab_of_map_lookup_elem,
1378 .map_delete_elem = htab_map_delete_elem,
1379 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1380 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001381 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001382 .map_gen_lookup = htab_of_map_gen_lookup,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001383};