blob: d6110042e0d9d00860b1da592b61efc3dbca2094 [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>
Yonghong Song699c86d2018-08-09 08:55:20 -070018#include <uapi/linux/btf.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080019#include "percpu_freelist.h"
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080020#include "bpf_lru_list.h"
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070021#include "map_in_map.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080022
Chenbo Feng6e71b042017-10-18 13:00:22 -070023#define HTAB_CREATE_FLAG_MASK \
24 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
25 BPF_F_RDONLY | BPF_F_WRONLY)
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070026
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080027struct bucket {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080028 struct hlist_nulls_head head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080029 raw_spinlock_t lock;
30};
31
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080032struct bpf_htab {
33 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080034 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080035 void *elems;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080036 union {
37 struct pcpu_freelist freelist;
38 struct bpf_lru lru;
39 };
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070040 struct htab_elem *__percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080041 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080042 u32 n_buckets; /* number of hash buckets */
43 u32 elem_size; /* size of each element in bytes */
44};
45
46/* each htab element is struct htab_elem + key + value */
47struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080048 union {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -080049 struct hlist_nulls_node hash_node;
Alexei Starovoitov9f691542017-03-07 20:00:12 -080050 struct {
51 void *padding;
52 union {
53 struct bpf_htab *htab;
54 struct pcpu_freelist_node fnode;
55 };
56 };
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080057 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070058 union {
59 struct rcu_head rcu;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080060 struct bpf_lru_node lru_node;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070061 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080062 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080063 char key[0] __aligned(8);
64};
65
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080066static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
67
68static bool htab_is_lru(const struct bpf_htab *htab)
69{
Martin KaFai Lau8f844932016-11-11 10:55:10 -080070 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72}
73
74static bool htab_is_percpu(const struct bpf_htab *htab)
75{
76 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
77 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -080078}
79
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070080static bool htab_is_prealloc(const struct bpf_htab *htab)
81{
82 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
83}
84
Alexei Starovoitov6c905982016-03-07 21:57:15 -080085static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
86 void __percpu *pptr)
87{
88 *(void __percpu **)(l->key + key_size) = pptr;
89}
90
91static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
92{
93 return *(void __percpu **)(l->key + key_size);
94}
95
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -070096static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
97{
98 return *(void **)(l->key + roundup(map->key_size, 8));
99}
100
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800101static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
102{
103 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
104}
105
106static void htab_free_elems(struct bpf_htab *htab)
107{
108 int i;
109
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800110 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800111 goto free_elems;
112
113 for (i = 0; i < htab->map.max_entries; i++) {
114 void __percpu *pptr;
115
116 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
117 htab->map.key_size);
118 free_percpu(pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800119 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800120 }
121free_elems:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100122 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800123}
124
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800125static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
126 u32 hash)
127{
128 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
129 struct htab_elem *l;
130
131 if (node) {
132 l = container_of(node, struct htab_elem, lru_node);
133 memcpy(l->key, key, htab->map.key_size);
134 return l;
135 }
136
137 return NULL;
138}
139
140static int prealloc_init(struct bpf_htab *htab)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800141{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700142 u32 num_entries = htab->map.max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800143 int err = -ENOMEM, i;
144
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700145 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
146 num_entries += num_possible_cpus();
147
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700148 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
149 htab->map.numa_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800150 if (!htab->elems)
151 return -ENOMEM;
152
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800153 if (!htab_is_percpu(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800154 goto skip_percpu_elems;
155
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700156 for (i = 0; i < num_entries; i++) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800157 u32 size = round_up(htab->map.value_size, 8);
158 void __percpu *pptr;
159
160 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
161 if (!pptr)
162 goto free_elems;
163 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
164 pptr);
Eric Dumazet9147efc2017-12-12 14:22:39 -0800165 cond_resched();
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800166 }
167
168skip_percpu_elems:
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800169 if (htab_is_lru(htab))
170 err = bpf_lru_init(&htab->lru,
171 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
172 offsetof(struct htab_elem, hash) -
173 offsetof(struct htab_elem, lru_node),
174 htab_lru_map_delete_node,
175 htab);
176 else
177 err = pcpu_freelist_init(&htab->freelist);
178
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800179 if (err)
180 goto free_elems;
181
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800182 if (htab_is_lru(htab))
183 bpf_lru_populate(&htab->lru, htab->elems,
184 offsetof(struct htab_elem, lru_node),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700185 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800186 else
Alexei Starovoitov9f691542017-03-07 20:00:12 -0800187 pcpu_freelist_populate(&htab->freelist,
188 htab->elems + offsetof(struct htab_elem, fnode),
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700189 htab->elem_size, num_entries);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800190
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800191 return 0;
192
193free_elems:
194 htab_free_elems(htab);
195 return err;
196}
197
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800198static void prealloc_destroy(struct bpf_htab *htab)
199{
200 htab_free_elems(htab);
201
202 if (htab_is_lru(htab))
203 bpf_lru_destroy(&htab->lru);
204 else
205 pcpu_freelist_destroy(&htab->freelist);
206}
207
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700208static int alloc_extra_elems(struct bpf_htab *htab)
209{
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700210 struct htab_elem *__percpu *pptr, *l_new;
211 struct pcpu_freelist_node *l;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700212 int cpu;
213
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700214 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
215 GFP_USER | __GFP_NOWARN);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700216 if (!pptr)
217 return -ENOMEM;
218
219 for_each_possible_cpu(cpu) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700220 l = pcpu_freelist_pop(&htab->freelist);
221 /* pop will succeed, since prealloc_init()
222 * preallocated extra num_possible_cpus elements
223 */
224 l_new = container_of(l, struct htab_elem, fnode);
225 *per_cpu_ptr(pptr, cpu) = l_new;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700226 }
227 htab->extra_elems = pptr;
228 return 0;
229}
230
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800231/* Called from syscall */
Jakub Kicinski9328e0d2018-01-11 20:29:05 -0800232static int htab_map_alloc_check(union bpf_attr *attr)
233{
234 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
235 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
236 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
237 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
238 /* percpu_lru means each cpu has its own LRU list.
239 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
240 * the map's value itself is percpu. percpu_lru has
241 * nothing to do with the map's value.
242 */
243 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
244 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
245 int numa_node = bpf_map_attr_numa_node(attr);
246
247 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
248 offsetof(struct htab_elem, hash_node.pprev));
249 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
250 offsetof(struct htab_elem, hash_node.pprev));
251
252 if (lru && !capable(CAP_SYS_ADMIN))
253 /* LRU implementation is much complicated than other
254 * maps. Hence, limit to CAP_SYS_ADMIN for now.
255 */
256 return -EPERM;
257
258 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
259 /* reserved bits should not be used */
260 return -EINVAL;
261
262 if (!lru && percpu_lru)
263 return -EINVAL;
264
265 if (lru && !prealloc)
266 return -ENOTSUPP;
267
268 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
269 return -EINVAL;
270
271 /* check sanity of attributes.
272 * value_size == 0 may be allowed in the future to use map as a set
273 */
274 if (attr->max_entries == 0 || attr->key_size == 0 ||
275 attr->value_size == 0)
276 return -EINVAL;
277
278 if (attr->key_size > MAX_BPF_STACK)
279 /* eBPF programs initialize keys on stack, so they cannot be
280 * larger than max stack size
281 */
282 return -E2BIG;
283
284 if (attr->value_size >= KMALLOC_MAX_SIZE -
285 MAX_BPF_STACK - sizeof(struct htab_elem))
286 /* if value_size is bigger, the user space won't be able to
287 * access the elements via bpf syscall. This check also makes
288 * sure that the elem_size doesn't overflow and it's
289 * kmalloc-able later in htab_map_update_elem()
290 */
291 return -E2BIG;
292
293 return 0;
294}
295
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800296static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
297{
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800298 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
299 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
300 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
301 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800302 /* percpu_lru means each cpu has its own LRU list.
303 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
304 * the map's value itself is percpu. percpu_lru has
305 * nothing to do with the map's value.
306 */
307 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
308 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800309 struct bpf_htab *htab;
310 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800311 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800312
313 htab = kzalloc(sizeof(*htab), GFP_USER);
314 if (!htab)
315 return ERR_PTR(-ENOMEM);
316
Jakub Kicinskibd475642018-01-11 20:29:06 -0800317 bpf_map_init_from_attr(&htab->map, attr);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800318
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800319 if (percpu_lru) {
320 /* ensure each CPU's lru list has >=1 elements.
321 * since we are at it, make each lru list has the same
322 * number of elements.
323 */
324 htab->map.max_entries = roundup(attr->max_entries,
325 num_possible_cpus());
326 if (htab->map.max_entries < attr->max_entries)
327 htab->map.max_entries = rounddown(attr->max_entries,
328 num_possible_cpus());
329 }
330
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800331 /* hash table size must be power of 2 */
332 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
333
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800334 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800335 round_up(htab->map.key_size, 8);
336 if (percpu)
337 htab->elem_size += sizeof(void *);
338 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800339 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800340
Jakub Kicinskidaffc5a2018-01-11 20:29:04 -0800341 err = -E2BIG;
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800342 /* prevent zero size kmalloc and check for u32 overflow */
343 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800344 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800345 goto free_htab;
346
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800347 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
348 (u64) htab->elem_size * htab->map.max_entries;
349
350 if (percpu)
351 cost += (u64) round_up(htab->map.value_size, 8) *
352 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700353 else
354 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800355
356 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800357 /* make sure page count doesn't overflow */
358 goto free_htab;
359
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800360 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800361
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800362 /* if map size is larger than memlock limit, reject it early */
363 err = bpf_map_precharge_memlock(htab->map.pages);
364 if (err)
365 goto free_htab;
366
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800367 err = -ENOMEM;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100368 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700369 sizeof(struct bucket),
370 htab->map.numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100371 if (!htab->buckets)
372 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800373
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800374 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800375 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800376 raw_spin_lock_init(&htab->buckets[i].lock);
377 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800378
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800379 if (prealloc) {
380 err = prealloc_init(htab);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700381 if (err)
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700382 goto free_buckets;
383
384 if (!percpu && !lru) {
385 /* lru itself can remove the least used element, so
386 * there is no need for an extra elem during map_update.
387 */
388 err = alloc_extra_elems(htab);
389 if (err)
390 goto free_prealloc;
391 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700392 }
393
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800394 return &htab->map;
395
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700396free_prealloc:
397 prealloc_destroy(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800398free_buckets:
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100399 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800400free_htab:
401 kfree(htab);
402 return ERR_PTR(err);
403}
404
405static inline u32 htab_map_hash(const void *key, u32 key_len)
406{
407 return jhash(key, key_len, 0);
408}
409
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800410static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800411{
412 return &htab->buckets[hash & (htab->n_buckets - 1)];
413}
414
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800415static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800416{
417 return &__select_bucket(htab, hash)->head;
418}
419
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800420/* this lookup function can only be called with bucket lock taken */
421static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800422 void *key, u32 key_size)
423{
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800424 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800425 struct htab_elem *l;
426
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800427 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800428 if (l->hash == hash && !memcmp(&l->key, key, key_size))
429 return l;
430
431 return NULL;
432}
433
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800434/* can be called without bucket lock. it will repeat the loop in
435 * the unlikely event when elements moved from one bucket into another
436 * while link list is being walked
437 */
438static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
439 u32 hash, void *key,
440 u32 key_size, u32 n_buckets)
441{
442 struct hlist_nulls_node *n;
443 struct htab_elem *l;
444
445again:
446 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
447 if (l->hash == hash && !memcmp(&l->key, key, key_size))
448 return l;
449
450 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
451 goto again;
452
453 return NULL;
454}
455
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700456/* Called from syscall or from eBPF program directly, so
457 * arguments have to match bpf_map_lookup_elem() exactly.
458 * The return value is adjusted by BPF instructions
459 * in htab_map_gen_lookup().
460 */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800461static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800462{
463 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800464 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800465 struct htab_elem *l;
466 u32 hash, key_size;
467
468 /* Must be called with rcu_read_lock. */
469 WARN_ON_ONCE(!rcu_read_lock_held());
470
471 key_size = map->key_size;
472
473 hash = htab_map_hash(key, key_size);
474
475 head = select_bucket(htab, hash);
476
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800477 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800478
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800479 return l;
480}
481
482static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
483{
484 struct htab_elem *l = __htab_map_lookup_elem(map, key);
485
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800486 if (l)
487 return l->key + round_up(map->key_size, 8);
488
489 return NULL;
490}
491
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700492/* inline bpf_map_lookup_elem() call.
493 * Instead of:
494 * bpf_prog
495 * bpf_map_lookup_elem
496 * map->ops->map_lookup_elem
497 * htab_map_lookup_elem
498 * __htab_map_lookup_elem
499 * do:
500 * bpf_prog
501 * __htab_map_lookup_elem
502 */
503static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
504{
505 struct bpf_insn *insn = insn_buf;
506 const int ret = BPF_REG_0;
507
Daniel Borkmann09772d92018-06-02 23:06:35 +0200508 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
509 (void *(*)(struct bpf_map *map, void *key))NULL));
510 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -0700511 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
512 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
513 offsetof(struct htab_elem, key) +
514 round_up(map->key_size, 8));
515 return insn - insn_buf;
516}
517
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800518static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
519{
520 struct htab_elem *l = __htab_map_lookup_elem(map, key);
521
522 if (l) {
523 bpf_lru_node_set_ref(&l->lru_node);
524 return l->key + round_up(map->key_size, 8);
525 }
526
527 return NULL;
528}
529
Martin KaFai Laucc555422017-08-31 23:27:12 -0700530static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
531 struct bpf_insn *insn_buf)
532{
533 struct bpf_insn *insn = insn_buf;
534 const int ret = BPF_REG_0;
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700535 const int ref_reg = BPF_REG_1;
Martin KaFai Laucc555422017-08-31 23:27:12 -0700536
Daniel Borkmann09772d92018-06-02 23:06:35 +0200537 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
538 (void *(*)(struct bpf_map *map, void *key))NULL));
539 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Martin KaFai Laubb9b9f82017-08-31 23:27:13 -0700540 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
541 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
542 offsetof(struct htab_elem, lru_node) +
543 offsetof(struct bpf_lru_node, ref));
544 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
Martin KaFai Laucc555422017-08-31 23:27:12 -0700545 *insn++ = BPF_ST_MEM(BPF_B, ret,
546 offsetof(struct htab_elem, lru_node) +
547 offsetof(struct bpf_lru_node, ref),
548 1);
549 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
550 offsetof(struct htab_elem, key) +
551 round_up(map->key_size, 8));
552 return insn - insn_buf;
553}
554
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800555/* It is called from the bpf_lru_list when the LRU needs to delete
556 * older elements from the htab.
557 */
558static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
559{
560 struct bpf_htab *htab = (struct bpf_htab *)arg;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800561 struct htab_elem *l = NULL, *tgt_l;
562 struct hlist_nulls_head *head;
563 struct hlist_nulls_node *n;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800564 unsigned long flags;
565 struct bucket *b;
566
567 tgt_l = container_of(node, struct htab_elem, lru_node);
568 b = __select_bucket(htab, tgt_l->hash);
569 head = &b->head;
570
571 raw_spin_lock_irqsave(&b->lock, flags);
572
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800573 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800574 if (l == tgt_l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800575 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800576 break;
577 }
578
579 raw_spin_unlock_irqrestore(&b->lock, flags);
580
581 return l == tgt_l;
582}
583
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800584/* Called from syscall */
585static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
586{
587 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800588 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800589 struct htab_elem *l, *next_l;
590 u32 hash, key_size;
Teng Qin8fe45922017-04-24 19:00:37 -0700591 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800592
593 WARN_ON_ONCE(!rcu_read_lock_held());
594
595 key_size = map->key_size;
596
Teng Qin8fe45922017-04-24 19:00:37 -0700597 if (!key)
598 goto find_first_elem;
599
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800600 hash = htab_map_hash(key, key_size);
601
602 head = select_bucket(htab, hash);
603
604 /* lookup the key */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800605 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800606
Teng Qin8fe45922017-04-24 19:00:37 -0700607 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800608 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800609
610 /* key was found, get next key in the same bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800611 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800612 struct htab_elem, hash_node);
613
614 if (next_l) {
615 /* if next elem in this hash list is non-zero, just return it */
616 memcpy(next_key, next_l->key, key_size);
617 return 0;
618 }
619
620 /* no more elements in this hash list, go to the next bucket */
621 i = hash & (htab->n_buckets - 1);
622 i++;
623
624find_first_elem:
625 /* iterate over buckets */
626 for (; i < htab->n_buckets; i++) {
627 head = select_bucket(htab, i);
628
629 /* pick first element in the bucket */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800630 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800631 struct htab_elem, hash_node);
632 if (next_l) {
633 /* if it's not empty, just return it */
634 memcpy(next_key, next_l->key, key_size);
635 return 0;
636 }
637 }
638
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800639 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800640 return -ENOENT;
641}
642
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800643static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800644{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800645 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
646 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800647 kfree(l);
648}
649
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800650static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800651{
652 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800653 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800654
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800655 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
656 * we're calling kfree, otherwise deadlock is possible if kprobes
657 * are placed somewhere inside of slub
658 */
659 preempt_disable();
660 __this_cpu_inc(bpf_prog_active);
661 htab_elem_free(htab, l);
662 __this_cpu_dec(bpf_prog_active);
663 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800664}
665
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800666static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800667{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700668 struct bpf_map *map = &htab->map;
669
670 if (map->ops->map_fd_put_ptr) {
671 void *ptr = fd_htab_map_get_ptr(map, l);
672
673 map->ops->map_fd_put_ptr(ptr);
674 }
675
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700676 if (htab_is_prealloc(htab)) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800677 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800678 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800679 atomic_dec(&htab->count);
680 l->htab = htab;
681 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800682 }
683}
684
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800685static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
686 void *value, bool onallcpus)
687{
688 if (!onallcpus) {
689 /* copy true value_size bytes */
690 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
691 } else {
692 u32 size = round_up(htab->map.value_size, 8);
693 int off = 0, cpu;
694
695 for_each_possible_cpu(cpu) {
696 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
697 value + off, size);
698 off += size;
699 }
700 }
701}
702
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200703static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
704{
705 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
706 BITS_PER_LONG == 64;
707}
708
709static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
710{
711 u32 size = htab->map.value_size;
712
713 if (percpu || fd_htab_map_needs_adjust(htab))
714 size = round_up(size, 8);
715 return size;
716}
717
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800718static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
719 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700720 bool percpu, bool onallcpus,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700721 struct htab_elem *old_elem)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800722{
Daniel Borkmanncd36c3a2017-08-23 00:06:09 +0200723 u32 size = htab_size_value(htab, percpu);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700724 bool prealloc = htab_is_prealloc(htab);
725 struct htab_elem *l_new, **pl_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800726 void __percpu *pptr;
727
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800728 if (prealloc) {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700729 if (old_elem) {
730 /* if we're updating the existing element,
731 * use per-cpu extra elems to avoid freelist_pop/push
732 */
733 pl_new = this_cpu_ptr(htab->extra_elems);
734 l_new = *pl_new;
735 *pl_new = old_elem;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700736 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700737 struct pcpu_freelist_node *l;
738
739 l = pcpu_freelist_pop(&htab->freelist);
740 if (!l)
741 return ERR_PTR(-E2BIG);
742 l_new = container_of(l, struct htab_elem, fnode);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800743 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700744 } else {
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700745 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
746 if (!old_elem) {
747 /* when map is full and update() is replacing
748 * old element, it's ok to allocate, since
749 * old element will be freed immediately.
750 * Otherwise return an error
751 */
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200752 l_new = ERR_PTR(-E2BIG);
753 goto dec_count;
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700754 }
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700755 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
756 htab->map.numa_node);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200757 if (!l_new) {
758 l_new = ERR_PTR(-ENOMEM);
759 goto dec_count;
760 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800761 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800762
763 memcpy(l_new->key, key, key_size);
764 if (percpu) {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800765 if (prealloc) {
766 pptr = htab_elem_get_ptr(l_new, key_size);
767 } else {
768 /* alloc_percpu zero-fills */
769 pptr = __alloc_percpu_gfp(size, 8,
770 GFP_ATOMIC | __GFP_NOWARN);
771 if (!pptr) {
772 kfree(l_new);
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200773 l_new = ERR_PTR(-ENOMEM);
774 goto dec_count;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800775 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800776 }
777
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800778 pcpu_copy_value(htab, pptr, value, onallcpus);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800779
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800780 if (!prealloc)
781 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800782 } else {
783 memcpy(l_new->key + round_up(key_size, 8), value, size);
784 }
785
786 l_new->hash = hash;
787 return l_new;
Mauricio Vasquez Bed2b82c2018-06-29 14:48:20 +0200788dec_count:
789 atomic_dec(&htab->count);
790 return l_new;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800791}
792
793static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
794 u64 map_flags)
795{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800796 if (l_old && map_flags == BPF_NOEXIST)
797 /* elem already exists */
798 return -EEXIST;
799
800 if (!l_old && map_flags == BPF_EXIST)
801 /* elem doesn't exist, cannot update it */
802 return -ENOENT;
803
804 return 0;
805}
806
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800807/* Called from syscall or from eBPF program */
808static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
809 u64 map_flags)
810{
811 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800812 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800813 struct hlist_nulls_head *head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800814 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800815 struct bucket *b;
816 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800817 int ret;
818
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800819 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800820 /* unknown flags */
821 return -EINVAL;
822
823 WARN_ON_ONCE(!rcu_read_lock_held());
824
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800825 key_size = map->key_size;
826
827 hash = htab_map_hash(key, key_size);
828
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800829 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800830 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800831
832 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800833 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800834
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800835 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800836
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800837 ret = check_flags(htab, l_old, map_flags);
838 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800839 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800840
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700841 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700842 l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800843 if (IS_ERR(l_new)) {
844 /* all pre-allocated elements are in use or memory exhausted */
845 ret = PTR_ERR(l_new);
846 goto err;
847 }
848
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800849 /* add new element to the head of the list, so that
850 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800851 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800852 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800853 if (l_old) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800854 hlist_nulls_del_rcu(&l_old->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700855 if (!htab_is_prealloc(htab))
856 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800857 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800858 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800859err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800860 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800861 return ret;
862}
863
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800864static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
865 u64 map_flags)
866{
867 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
868 struct htab_elem *l_new, *l_old = NULL;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800869 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800870 unsigned long flags;
871 struct bucket *b;
872 u32 key_size, hash;
873 int ret;
874
875 if (unlikely(map_flags > BPF_EXIST))
876 /* unknown flags */
877 return -EINVAL;
878
879 WARN_ON_ONCE(!rcu_read_lock_held());
880
881 key_size = map->key_size;
882
883 hash = htab_map_hash(key, key_size);
884
885 b = __select_bucket(htab, hash);
886 head = &b->head;
887
888 /* For LRU, we need to alloc before taking bucket's
889 * spinlock because getting free nodes from LRU may need
890 * to remove older elements from htab and this removal
891 * operation will need a bucket lock.
892 */
893 l_new = prealloc_lru_pop(htab, key, hash);
894 if (!l_new)
895 return -ENOMEM;
896 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
897
898 /* bpf_map_update_elem() can be called in_irq() */
899 raw_spin_lock_irqsave(&b->lock, flags);
900
901 l_old = lookup_elem_raw(head, hash, key, key_size);
902
903 ret = check_flags(htab, l_old, map_flags);
904 if (ret)
905 goto err;
906
907 /* add new element to the head of the list, so that
908 * concurrent search will find it before old elem
909 */
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800910 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800911 if (l_old) {
912 bpf_lru_node_set_ref(&l_new->lru_node);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800913 hlist_nulls_del_rcu(&l_old->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -0800914 }
915 ret = 0;
916
917err:
918 raw_spin_unlock_irqrestore(&b->lock, flags);
919
920 if (ret)
921 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
922 else if (l_old)
923 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
924
925 return ret;
926}
927
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800928static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
929 void *value, u64 map_flags,
930 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800931{
932 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
933 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800934 struct hlist_nulls_head *head;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800935 unsigned long flags;
936 struct bucket *b;
937 u32 key_size, hash;
938 int ret;
939
940 if (unlikely(map_flags > BPF_EXIST))
941 /* unknown flags */
942 return -EINVAL;
943
944 WARN_ON_ONCE(!rcu_read_lock_held());
945
946 key_size = map->key_size;
947
948 hash = htab_map_hash(key, key_size);
949
950 b = __select_bucket(htab, hash);
951 head = &b->head;
952
953 /* bpf_map_update_elem() can be called in_irq() */
954 raw_spin_lock_irqsave(&b->lock, flags);
955
956 l_old = lookup_elem_raw(head, hash, key, key_size);
957
958 ret = check_flags(htab, l_old, map_flags);
959 if (ret)
960 goto err;
961
962 if (l_old) {
963 /* per-cpu hash map can update value in-place */
Martin KaFai Laufd91de72016-11-11 10:55:08 -0800964 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
965 value, onallcpus);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800966 } else {
967 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700968 hash, true, onallcpus, NULL);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800969 if (IS_ERR(l_new)) {
970 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800971 goto err;
972 }
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800973 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800974 }
975 ret = 0;
976err:
977 raw_spin_unlock_irqrestore(&b->lock, flags);
978 return ret;
979}
980
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800981static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
982 void *value, u64 map_flags,
983 bool onallcpus)
984{
985 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
986 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov4fe84352017-03-07 20:00:13 -0800987 struct hlist_nulls_head *head;
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800988 unsigned long flags;
989 struct bucket *b;
990 u32 key_size, hash;
991 int ret;
992
993 if (unlikely(map_flags > BPF_EXIST))
994 /* unknown flags */
995 return -EINVAL;
996
997 WARN_ON_ONCE(!rcu_read_lock_held());
998
999 key_size = map->key_size;
1000
1001 hash = htab_map_hash(key, key_size);
1002
1003 b = __select_bucket(htab, hash);
1004 head = &b->head;
1005
1006 /* For LRU, we need to alloc before taking bucket's
1007 * spinlock because LRU's elem alloc may need
1008 * to remove older elem from htab and this removal
1009 * operation will need a bucket lock.
1010 */
1011 if (map_flags != BPF_EXIST) {
1012 l_new = prealloc_lru_pop(htab, key, hash);
1013 if (!l_new)
1014 return -ENOMEM;
1015 }
1016
1017 /* bpf_map_update_elem() can be called in_irq() */
1018 raw_spin_lock_irqsave(&b->lock, flags);
1019
1020 l_old = lookup_elem_raw(head, hash, key, key_size);
1021
1022 ret = check_flags(htab, l_old, map_flags);
1023 if (ret)
1024 goto err;
1025
1026 if (l_old) {
1027 bpf_lru_node_set_ref(&l_old->lru_node);
1028
1029 /* per-cpu hash map can update value in-place */
1030 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1031 value, onallcpus);
1032 } else {
1033 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1034 value, onallcpus);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001035 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001036 l_new = NULL;
1037 }
1038 ret = 0;
1039err:
1040 raw_spin_unlock_irqrestore(&b->lock, flags);
1041 if (l_new)
1042 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1043 return ret;
1044}
1045
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001046static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1047 void *value, u64 map_flags)
1048{
1049 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1050}
1051
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001052static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1053 void *value, u64 map_flags)
1054{
1055 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1056 false);
1057}
1058
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001059/* Called from syscall or from eBPF program */
1060static int htab_map_delete_elem(struct bpf_map *map, void *key)
1061{
1062 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001063 struct hlist_nulls_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001064 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001065 struct htab_elem *l;
1066 unsigned long flags;
1067 u32 hash, key_size;
1068 int ret = -ENOENT;
1069
1070 WARN_ON_ONCE(!rcu_read_lock_held());
1071
1072 key_size = map->key_size;
1073
1074 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001075 b = __select_bucket(htab, hash);
1076 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001077
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001078 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001079
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001080 l = lookup_elem_raw(head, hash, key, key_size);
1081
1082 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001083 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001084 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001085 ret = 0;
1086 }
1087
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +08001088 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001089 return ret;
1090}
1091
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001092static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1093{
1094 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001095 struct hlist_nulls_head *head;
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001096 struct bucket *b;
1097 struct htab_elem *l;
1098 unsigned long flags;
1099 u32 hash, key_size;
1100 int ret = -ENOENT;
1101
1102 WARN_ON_ONCE(!rcu_read_lock_held());
1103
1104 key_size = map->key_size;
1105
1106 hash = htab_map_hash(key, key_size);
1107 b = __select_bucket(htab, hash);
1108 head = &b->head;
1109
1110 raw_spin_lock_irqsave(&b->lock, flags);
1111
1112 l = lookup_elem_raw(head, hash, key, key_size);
1113
1114 if (l) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001115 hlist_nulls_del_rcu(&l->hash_node);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001116 ret = 0;
1117 }
1118
1119 raw_spin_unlock_irqrestore(&b->lock, flags);
1120 if (l)
1121 bpf_lru_push_free(&htab->lru, &l->lru_node);
1122 return ret;
1123}
1124
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001125static void delete_all_elements(struct bpf_htab *htab)
1126{
1127 int i;
1128
1129 for (i = 0; i < htab->n_buckets; i++) {
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001130 struct hlist_nulls_head *head = select_bucket(htab, i);
1131 struct hlist_nulls_node *n;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001132 struct htab_elem *l;
1133
Alexei Starovoitov4fe84352017-03-07 20:00:13 -08001134 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1135 hlist_nulls_del_rcu(&l->hash_node);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001136 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001137 }
1138 }
1139}
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001140
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001141/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1142static void htab_map_free(struct bpf_map *map)
1143{
1144 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1145
1146 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1147 * so the programs (can be more than one that used this map) were
1148 * disconnected from events. Wait for outstanding critical sections in
1149 * these programs to complete
1150 */
1151 synchronize_rcu();
1152
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001153 /* some of free_htab_elem() callbacks for elements of this map may
1154 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001155 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001156 rcu_barrier();
Alexei Starovoitov8c290e62017-03-21 19:05:04 -07001157 if (!htab_is_prealloc(htab))
Alexei Starovoitov6c905982016-03-07 21:57:15 -08001158 delete_all_elements(htab);
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001159 else
1160 prealloc_destroy(htab);
1161
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -07001162 free_percpu(htab->extra_elems);
Daniel Borkmannd407bd22017-01-18 15:14:17 +01001163 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001164 kfree(htab);
1165}
1166
Yonghong Song699c86d2018-08-09 08:55:20 -07001167static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1168 struct seq_file *m)
1169{
1170 void *value;
1171
1172 rcu_read_lock();
1173
1174 value = htab_map_lookup_elem(map, key);
1175 if (!value) {
1176 rcu_read_unlock();
1177 return;
1178 }
1179
1180 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1181 seq_puts(m, ": ");
1182 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1183 seq_puts(m, "\n");
1184
1185 rcu_read_unlock();
1186}
1187
1188static int htab_map_check_btf(const struct bpf_map *map, const struct btf *btf,
1189 u32 btf_key_id, u32 btf_value_id)
1190{
1191 const struct btf_type *key_type, *value_type;
1192 u32 key_size, value_size;
1193
1194 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1195 if (!key_type || key_size != map->key_size)
1196 return -EINVAL;
1197
1198 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1199 if (!value_type || value_size != map->value_size)
1200 return -EINVAL;
1201
1202 return 0;
1203}
1204
Johannes Berg40077e02017-04-11 15:34:58 +02001205const struct bpf_map_ops htab_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001206 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001207 .map_alloc = htab_map_alloc,
1208 .map_free = htab_map_free,
1209 .map_get_next_key = htab_map_get_next_key,
1210 .map_lookup_elem = htab_map_lookup_elem,
1211 .map_update_elem = htab_map_update_elem,
1212 .map_delete_elem = htab_map_delete_elem,
Alexei Starovoitov9015d2f2017-03-15 18:26:43 -07001213 .map_gen_lookup = htab_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001214 .map_seq_show_elem = htab_map_seq_show_elem,
1215 .map_check_btf = htab_map_check_btf,
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001216};
1217
Johannes Berg40077e02017-04-11 15:34:58 +02001218const struct bpf_map_ops htab_lru_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001219 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001220 .map_alloc = htab_map_alloc,
1221 .map_free = htab_map_free,
1222 .map_get_next_key = htab_map_get_next_key,
1223 .map_lookup_elem = htab_lru_map_lookup_elem,
1224 .map_update_elem = htab_lru_map_update_elem,
1225 .map_delete_elem = htab_lru_map_delete_elem,
Martin KaFai Laucc555422017-08-31 23:27:12 -07001226 .map_gen_lookup = htab_lru_map_gen_lookup,
Yonghong Song699c86d2018-08-09 08:55:20 -07001227 .map_seq_show_elem = htab_map_seq_show_elem,
1228 .map_check_btf = htab_map_check_btf,
Martin KaFai Lau29ba7322016-11-11 10:55:09 -08001229};
1230
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001231/* Called from eBPF program */
1232static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1233{
1234 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1235
1236 if (l)
1237 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1238 else
1239 return NULL;
1240}
1241
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001242static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1243{
1244 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1245
1246 if (l) {
1247 bpf_lru_node_set_ref(&l->lru_node);
1248 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1249 }
1250
1251 return NULL;
1252}
1253
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001254int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1255{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001256 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001257 struct htab_elem *l;
1258 void __percpu *pptr;
1259 int ret = -ENOENT;
1260 int cpu, off = 0;
1261 u32 size;
1262
1263 /* per_cpu areas are zero-filled and bpf programs can only
1264 * access 'value_size' of them, so copying rounded areas
1265 * will not leak any kernel data
1266 */
1267 size = round_up(map->value_size, 8);
1268 rcu_read_lock();
1269 l = __htab_map_lookup_elem(map, key);
1270 if (!l)
1271 goto out;
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001272 if (htab_is_lru(htab))
1273 bpf_lru_node_set_ref(&l->lru_node);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001274 pptr = htab_elem_get_ptr(l, map->key_size);
1275 for_each_possible_cpu(cpu) {
1276 bpf_long_memcpy(value + off,
1277 per_cpu_ptr(pptr, cpu), size);
1278 off += size;
1279 }
1280 ret = 0;
1281out:
1282 rcu_read_unlock();
1283 return ret;
1284}
1285
1286int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1287 u64 map_flags)
1288{
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001289 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001290 int ret;
1291
1292 rcu_read_lock();
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001293 if (htab_is_lru(htab))
1294 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1295 map_flags, true);
1296 else
1297 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1298 true);
Sasha Levin6bbd9a02016-02-19 13:53:10 -05001299 rcu_read_unlock();
1300
1301 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001302}
1303
Johannes Berg40077e02017-04-11 15:34:58 +02001304const struct bpf_map_ops htab_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001305 .map_alloc_check = htab_map_alloc_check,
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -08001306 .map_alloc = htab_map_alloc,
1307 .map_free = htab_map_free,
1308 .map_get_next_key = htab_map_get_next_key,
1309 .map_lookup_elem = htab_percpu_map_lookup_elem,
1310 .map_update_elem = htab_percpu_map_update_elem,
1311 .map_delete_elem = htab_map_delete_elem,
1312};
1313
Johannes Berg40077e02017-04-11 15:34:58 +02001314const struct bpf_map_ops htab_lru_percpu_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001315 .map_alloc_check = htab_map_alloc_check,
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001316 .map_alloc = htab_map_alloc,
1317 .map_free = htab_map_free,
1318 .map_get_next_key = htab_map_get_next_key,
1319 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1320 .map_update_elem = htab_lru_percpu_map_update_elem,
1321 .map_delete_elem = htab_lru_map_delete_elem,
1322};
1323
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001324static int fd_htab_map_alloc_check(union bpf_attr *attr)
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001325{
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001326 if (attr->value_size != sizeof(u32))
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001327 return -EINVAL;
1328 return htab_map_alloc_check(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001329}
1330
1331static void fd_htab_map_free(struct bpf_map *map)
1332{
1333 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1334 struct hlist_nulls_node *n;
1335 struct hlist_nulls_head *head;
1336 struct htab_elem *l;
1337 int i;
1338
1339 for (i = 0; i < htab->n_buckets; i++) {
1340 head = select_bucket(htab, i);
1341
1342 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1343 void *ptr = fd_htab_map_get_ptr(map, l);
1344
1345 map->ops->map_fd_put_ptr(ptr);
1346 }
1347 }
1348
1349 htab_map_free(map);
1350}
1351
1352/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001353int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1354{
1355 void **ptr;
1356 int ret = 0;
1357
1358 if (!map->ops->map_fd_sys_lookup_elem)
1359 return -ENOTSUPP;
1360
1361 rcu_read_lock();
1362 ptr = htab_map_lookup_elem(map, key);
1363 if (ptr)
1364 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1365 else
1366 ret = -ENOENT;
1367 rcu_read_unlock();
1368
1369 return ret;
1370}
1371
1372/* only called from syscall */
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001373int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1374 void *key, void *value, u64 map_flags)
1375{
1376 void *ptr;
1377 int ret;
1378 u32 ufd = *(u32 *)value;
1379
1380 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1381 if (IS_ERR(ptr))
1382 return PTR_ERR(ptr);
1383
1384 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1385 if (ret)
1386 map->ops->map_fd_put_ptr(ptr);
1387
1388 return ret;
1389}
1390
1391static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1392{
1393 struct bpf_map *map, *inner_map_meta;
1394
1395 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1396 if (IS_ERR(inner_map_meta))
1397 return inner_map_meta;
1398
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001399 map = htab_map_alloc(attr);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001400 if (IS_ERR(map)) {
1401 bpf_map_meta_free(inner_map_meta);
1402 return map;
1403 }
1404
1405 map->inner_map_meta = inner_map_meta;
1406
1407 return map;
1408}
1409
1410static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1411{
1412 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1413
1414 if (!inner_map)
1415 return NULL;
1416
1417 return READ_ONCE(*inner_map);
1418}
1419
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001420static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1421 struct bpf_insn *insn_buf)
1422{
1423 struct bpf_insn *insn = insn_buf;
1424 const int ret = BPF_REG_0;
1425
Daniel Borkmann09772d92018-06-02 23:06:35 +02001426 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1427 (void *(*)(struct bpf_map *map, void *key))NULL));
1428 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001429 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1430 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1431 offsetof(struct htab_elem, key) +
1432 round_up(map->key_size, 8));
1433 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1434
1435 return insn - insn_buf;
1436}
1437
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001438static void htab_of_map_free(struct bpf_map *map)
1439{
1440 bpf_map_meta_free(map->inner_map_meta);
1441 fd_htab_map_free(map);
1442}
1443
Johannes Berg40077e02017-04-11 15:34:58 +02001444const struct bpf_map_ops htab_of_maps_map_ops = {
Jakub Kicinski9328e0d2018-01-11 20:29:05 -08001445 .map_alloc_check = fd_htab_map_alloc_check,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001446 .map_alloc = htab_of_map_alloc,
1447 .map_free = htab_of_map_free,
1448 .map_get_next_key = htab_map_get_next_key,
1449 .map_lookup_elem = htab_of_map_lookup_elem,
1450 .map_delete_elem = htab_map_delete_elem,
1451 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1452 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -07001453 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +02001454 .map_gen_lookup = htab_of_map_gen_lookup,
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001455};