]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/bpf/hashtab.c
4b7c76765d9d6998cdbb273ced0d30770d3f6b1e
[linux.git] / kernel / bpf / hashtab.c
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2  * Copyright (c) 2016 Facebook
3  *
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/btf.h>
15 #include <linux/jhash.h>
16 #include <linux/filter.h>
17 #include <linux/rculist_nulls.h>
18 #include <linux/random.h>
19 #include <uapi/linux/btf.h>
20 #include "percpu_freelist.h"
21 #include "bpf_lru_list.h"
22 #include "map_in_map.h"
23
24 #define HTAB_CREATE_FLAG_MASK                                           \
25         (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |    \
26          BPF_F_RDONLY | BPF_F_WRONLY | BPF_F_ZERO_SEED)
27
28 struct bucket {
29         struct hlist_nulls_head head;
30         raw_spinlock_t lock;
31 };
32
33 struct bpf_htab {
34         struct bpf_map map;
35         struct bucket *buckets;
36         void *elems;
37         union {
38                 struct pcpu_freelist freelist;
39                 struct bpf_lru lru;
40         };
41         struct htab_elem *__percpu *extra_elems;
42         atomic_t count; /* number of elements in this hashtable */
43         u32 n_buckets;  /* number of hash buckets */
44         u32 elem_size;  /* size of each element in bytes */
45         u32 hashrnd;
46 };
47
48 /* each htab element is struct htab_elem + key + value */
49 struct htab_elem {
50         union {
51                 struct hlist_nulls_node hash_node;
52                 struct {
53                         void *padding;
54                         union {
55                                 struct bpf_htab *htab;
56                                 struct pcpu_freelist_node fnode;
57                         };
58                 };
59         };
60         union {
61                 struct rcu_head rcu;
62                 struct bpf_lru_node lru_node;
63         };
64         u32 hash;
65         char key[0] __aligned(8);
66 };
67
68 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
69
70 static bool htab_is_lru(const struct bpf_htab *htab)
71 {
72         return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
73                 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
74 }
75
76 static bool htab_is_percpu(const struct bpf_htab *htab)
77 {
78         return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
79                 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
80 }
81
82 static bool htab_is_prealloc(const struct bpf_htab *htab)
83 {
84         return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
85 }
86
87 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
88                                      void __percpu *pptr)
89 {
90         *(void __percpu **)(l->key + key_size) = pptr;
91 }
92
93 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
94 {
95         return *(void __percpu **)(l->key + key_size);
96 }
97
98 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
99 {
100         return *(void **)(l->key + roundup(map->key_size, 8));
101 }
102
103 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
104 {
105         return (struct htab_elem *) (htab->elems + i * htab->elem_size);
106 }
107
108 static void htab_free_elems(struct bpf_htab *htab)
109 {
110         int i;
111
112         if (!htab_is_percpu(htab))
113                 goto free_elems;
114
115         for (i = 0; i < htab->map.max_entries; i++) {
116                 void __percpu *pptr;
117
118                 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
119                                          htab->map.key_size);
120                 free_percpu(pptr);
121                 cond_resched();
122         }
123 free_elems:
124         bpf_map_area_free(htab->elems);
125 }
126
127 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
128                                           u32 hash)
129 {
130         struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
131         struct htab_elem *l;
132
133         if (node) {
134                 l = container_of(node, struct htab_elem, lru_node);
135                 memcpy(l->key, key, htab->map.key_size);
136                 return l;
137         }
138
139         return NULL;
140 }
141
142 static int prealloc_init(struct bpf_htab *htab)
143 {
144         u32 num_entries = htab->map.max_entries;
145         int err = -ENOMEM, i;
146
147         if (!htab_is_percpu(htab) && !htab_is_lru(htab))
148                 num_entries += num_possible_cpus();
149
150         htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
151                                          htab->map.numa_node);
152         if (!htab->elems)
153                 return -ENOMEM;
154
155         if (!htab_is_percpu(htab))
156                 goto skip_percpu_elems;
157
158         for (i = 0; i < num_entries; i++) {
159                 u32 size = round_up(htab->map.value_size, 8);
160                 void __percpu *pptr;
161
162                 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
163                 if (!pptr)
164                         goto free_elems;
165                 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
166                                   pptr);
167                 cond_resched();
168         }
169
170 skip_percpu_elems:
171         if (htab_is_lru(htab))
172                 err = bpf_lru_init(&htab->lru,
173                                    htab->map.map_flags & BPF_F_NO_COMMON_LRU,
174                                    offsetof(struct htab_elem, hash) -
175                                    offsetof(struct htab_elem, lru_node),
176                                    htab_lru_map_delete_node,
177                                    htab);
178         else
179                 err = pcpu_freelist_init(&htab->freelist);
180
181         if (err)
182                 goto free_elems;
183
184         if (htab_is_lru(htab))
185                 bpf_lru_populate(&htab->lru, htab->elems,
186                                  offsetof(struct htab_elem, lru_node),
187                                  htab->elem_size, num_entries);
188         else
189                 pcpu_freelist_populate(&htab->freelist,
190                                        htab->elems + offsetof(struct htab_elem, fnode),
191                                        htab->elem_size, num_entries);
192
193         return 0;
194
195 free_elems:
196         htab_free_elems(htab);
197         return err;
198 }
199
200 static void prealloc_destroy(struct bpf_htab *htab)
201 {
202         htab_free_elems(htab);
203
204         if (htab_is_lru(htab))
205                 bpf_lru_destroy(&htab->lru);
206         else
207                 pcpu_freelist_destroy(&htab->freelist);
208 }
209
210 static int alloc_extra_elems(struct bpf_htab *htab)
211 {
212         struct htab_elem *__percpu *pptr, *l_new;
213         struct pcpu_freelist_node *l;
214         int cpu;
215
216         pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
217                                   GFP_USER | __GFP_NOWARN);
218         if (!pptr)
219                 return -ENOMEM;
220
221         for_each_possible_cpu(cpu) {
222                 l = pcpu_freelist_pop(&htab->freelist);
223                 /* pop will succeed, since prealloc_init()
224                  * preallocated extra num_possible_cpus elements
225                  */
226                 l_new = container_of(l, struct htab_elem, fnode);
227                 *per_cpu_ptr(pptr, cpu) = l_new;
228         }
229         htab->extra_elems = pptr;
230         return 0;
231 }
232
233 /* Called from syscall */
234 static int htab_map_alloc_check(union bpf_attr *attr)
235 {
236         bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
237                        attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
238         bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
239                     attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
240         /* percpu_lru means each cpu has its own LRU list.
241          * it is different from BPF_MAP_TYPE_PERCPU_HASH where
242          * the map's value itself is percpu.  percpu_lru has
243          * nothing to do with the map's value.
244          */
245         bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
246         bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
247         bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
248         int numa_node = bpf_map_attr_numa_node(attr);
249
250         BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
251                      offsetof(struct htab_elem, hash_node.pprev));
252         BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
253                      offsetof(struct htab_elem, hash_node.pprev));
254
255         if (lru && !capable(CAP_SYS_ADMIN))
256                 /* LRU implementation is much complicated than other
257                  * maps.  Hence, limit to CAP_SYS_ADMIN for now.
258                  */
259                 return -EPERM;
260
261         if (zero_seed && !capable(CAP_SYS_ADMIN))
262                 /* Guard against local DoS, and discourage production use. */
263                 return -EPERM;
264
265         if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
266                 /* reserved bits should not be used */
267                 return -EINVAL;
268
269         if (!lru && percpu_lru)
270                 return -EINVAL;
271
272         if (lru && !prealloc)
273                 return -ENOTSUPP;
274
275         if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
276                 return -EINVAL;
277
278         /* check sanity of attributes.
279          * value_size == 0 may be allowed in the future to use map as a set
280          */
281         if (attr->max_entries == 0 || attr->key_size == 0 ||
282             attr->value_size == 0)
283                 return -EINVAL;
284
285         if (attr->key_size > MAX_BPF_STACK)
286                 /* eBPF programs initialize keys on stack, so they cannot be
287                  * larger than max stack size
288                  */
289                 return -E2BIG;
290
291         if (attr->value_size >= KMALLOC_MAX_SIZE -
292             MAX_BPF_STACK - sizeof(struct htab_elem))
293                 /* if value_size is bigger, the user space won't be able to
294                  * access the elements via bpf syscall. This check also makes
295                  * sure that the elem_size doesn't overflow and it's
296                  * kmalloc-able later in htab_map_update_elem()
297                  */
298                 return -E2BIG;
299
300         return 0;
301 }
302
303 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
304 {
305         bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
306                        attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
307         bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
308                     attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
309         /* percpu_lru means each cpu has its own LRU list.
310          * it is different from BPF_MAP_TYPE_PERCPU_HASH where
311          * the map's value itself is percpu.  percpu_lru has
312          * nothing to do with the map's value.
313          */
314         bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
315         bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
316         struct bpf_htab *htab;
317         int err, i;
318         u64 cost;
319
320         htab = kzalloc(sizeof(*htab), GFP_USER);
321         if (!htab)
322                 return ERR_PTR(-ENOMEM);
323
324         bpf_map_init_from_attr(&htab->map, attr);
325
326         if (percpu_lru) {
327                 /* ensure each CPU's lru list has >=1 elements.
328                  * since we are at it, make each lru list has the same
329                  * number of elements.
330                  */
331                 htab->map.max_entries = roundup(attr->max_entries,
332                                                 num_possible_cpus());
333                 if (htab->map.max_entries < attr->max_entries)
334                         htab->map.max_entries = rounddown(attr->max_entries,
335                                                           num_possible_cpus());
336         }
337
338         /* hash table size must be power of 2 */
339         htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
340
341         htab->elem_size = sizeof(struct htab_elem) +
342                           round_up(htab->map.key_size, 8);
343         if (percpu)
344                 htab->elem_size += sizeof(void *);
345         else
346                 htab->elem_size += round_up(htab->map.value_size, 8);
347
348         err = -E2BIG;
349         /* prevent zero size kmalloc and check for u32 overflow */
350         if (htab->n_buckets == 0 ||
351             htab->n_buckets > U32_MAX / sizeof(struct bucket))
352                 goto free_htab;
353
354         cost = (u64) htab->n_buckets * sizeof(struct bucket) +
355                (u64) htab->elem_size * htab->map.max_entries;
356
357         if (percpu)
358                 cost += (u64) round_up(htab->map.value_size, 8) *
359                         num_possible_cpus() * htab->map.max_entries;
360         else
361                cost += (u64) htab->elem_size * num_possible_cpus();
362
363         if (cost >= U32_MAX - PAGE_SIZE)
364                 /* make sure page count doesn't overflow */
365                 goto free_htab;
366
367         htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
368
369         /* if map size is larger than memlock limit, reject it early */
370         err = bpf_map_precharge_memlock(htab->map.pages);
371         if (err)
372                 goto free_htab;
373
374         err = -ENOMEM;
375         htab->buckets = bpf_map_area_alloc(htab->n_buckets *
376                                            sizeof(struct bucket),
377                                            htab->map.numa_node);
378         if (!htab->buckets)
379                 goto free_htab;
380
381         if (htab->map.map_flags & BPF_F_ZERO_SEED)
382                 htab->hashrnd = 0;
383         else
384                 htab->hashrnd = get_random_int();
385
386         for (i = 0; i < htab->n_buckets; i++) {
387                 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
388                 raw_spin_lock_init(&htab->buckets[i].lock);
389         }
390
391         if (prealloc) {
392                 err = prealloc_init(htab);
393                 if (err)
394                         goto free_buckets;
395
396                 if (!percpu && !lru) {
397                         /* lru itself can remove the least used element, so
398                          * there is no need for an extra elem during map_update.
399                          */
400                         err = alloc_extra_elems(htab);
401                         if (err)
402                                 goto free_prealloc;
403                 }
404         }
405
406         return &htab->map;
407
408 free_prealloc:
409         prealloc_destroy(htab);
410 free_buckets:
411         bpf_map_area_free(htab->buckets);
412 free_htab:
413         kfree(htab);
414         return ERR_PTR(err);
415 }
416
417 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
418 {
419         return jhash(key, key_len, hashrnd);
420 }
421
422 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
423 {
424         return &htab->buckets[hash & (htab->n_buckets - 1)];
425 }
426
427 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
428 {
429         return &__select_bucket(htab, hash)->head;
430 }
431
432 /* this lookup function can only be called with bucket lock taken */
433 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
434                                          void *key, u32 key_size)
435 {
436         struct hlist_nulls_node *n;
437         struct htab_elem *l;
438
439         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
440                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
441                         return l;
442
443         return NULL;
444 }
445
446 /* can be called without bucket lock. it will repeat the loop in
447  * the unlikely event when elements moved from one bucket into another
448  * while link list is being walked
449  */
450 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
451                                                u32 hash, void *key,
452                                                u32 key_size, u32 n_buckets)
453 {
454         struct hlist_nulls_node *n;
455         struct htab_elem *l;
456
457 again:
458         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
459                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
460                         return l;
461
462         if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
463                 goto again;
464
465         return NULL;
466 }
467
468 /* Called from syscall or from eBPF program directly, so
469  * arguments have to match bpf_map_lookup_elem() exactly.
470  * The return value is adjusted by BPF instructions
471  * in htab_map_gen_lookup().
472  */
473 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
474 {
475         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
476         struct hlist_nulls_head *head;
477         struct htab_elem *l;
478         u32 hash, key_size;
479
480         /* Must be called with rcu_read_lock. */
481         WARN_ON_ONCE(!rcu_read_lock_held());
482
483         key_size = map->key_size;
484
485         hash = htab_map_hash(key, key_size, htab->hashrnd);
486
487         head = select_bucket(htab, hash);
488
489         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
490
491         return l;
492 }
493
494 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
495 {
496         struct htab_elem *l = __htab_map_lookup_elem(map, key);
497
498         if (l)
499                 return l->key + round_up(map->key_size, 8);
500
501         return NULL;
502 }
503
504 /* inline bpf_map_lookup_elem() call.
505  * Instead of:
506  * bpf_prog
507  *   bpf_map_lookup_elem
508  *     map->ops->map_lookup_elem
509  *       htab_map_lookup_elem
510  *         __htab_map_lookup_elem
511  * do:
512  * bpf_prog
513  *   __htab_map_lookup_elem
514  */
515 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
516 {
517         struct bpf_insn *insn = insn_buf;
518         const int ret = BPF_REG_0;
519
520         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
521                      (void *(*)(struct bpf_map *map, void *key))NULL));
522         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
523         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
524         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
525                                 offsetof(struct htab_elem, key) +
526                                 round_up(map->key_size, 8));
527         return insn - insn_buf;
528 }
529
530 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
531 {
532         struct htab_elem *l = __htab_map_lookup_elem(map, key);
533
534         if (l) {
535                 bpf_lru_node_set_ref(&l->lru_node);
536                 return l->key + round_up(map->key_size, 8);
537         }
538
539         return NULL;
540 }
541
542 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
543                                    struct bpf_insn *insn_buf)
544 {
545         struct bpf_insn *insn = insn_buf;
546         const int ret = BPF_REG_0;
547         const int ref_reg = BPF_REG_1;
548
549         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
550                      (void *(*)(struct bpf_map *map, void *key))NULL));
551         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
552         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
553         *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
554                               offsetof(struct htab_elem, lru_node) +
555                               offsetof(struct bpf_lru_node, ref));
556         *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
557         *insn++ = BPF_ST_MEM(BPF_B, ret,
558                              offsetof(struct htab_elem, lru_node) +
559                              offsetof(struct bpf_lru_node, ref),
560                              1);
561         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
562                                 offsetof(struct htab_elem, key) +
563                                 round_up(map->key_size, 8));
564         return insn - insn_buf;
565 }
566
567 /* It is called from the bpf_lru_list when the LRU needs to delete
568  * older elements from the htab.
569  */
570 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
571 {
572         struct bpf_htab *htab = (struct bpf_htab *)arg;
573         struct htab_elem *l = NULL, *tgt_l;
574         struct hlist_nulls_head *head;
575         struct hlist_nulls_node *n;
576         unsigned long flags;
577         struct bucket *b;
578
579         tgt_l = container_of(node, struct htab_elem, lru_node);
580         b = __select_bucket(htab, tgt_l->hash);
581         head = &b->head;
582
583         raw_spin_lock_irqsave(&b->lock, flags);
584
585         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
586                 if (l == tgt_l) {
587                         hlist_nulls_del_rcu(&l->hash_node);
588                         break;
589                 }
590
591         raw_spin_unlock_irqrestore(&b->lock, flags);
592
593         return l == tgt_l;
594 }
595
596 /* Called from syscall */
597 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
598 {
599         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
600         struct hlist_nulls_head *head;
601         struct htab_elem *l, *next_l;
602         u32 hash, key_size;
603         int i = 0;
604
605         WARN_ON_ONCE(!rcu_read_lock_held());
606
607         key_size = map->key_size;
608
609         if (!key)
610                 goto find_first_elem;
611
612         hash = htab_map_hash(key, key_size, htab->hashrnd);
613
614         head = select_bucket(htab, hash);
615
616         /* lookup the key */
617         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
618
619         if (!l)
620                 goto find_first_elem;
621
622         /* key was found, get next key in the same bucket */
623         next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
624                                   struct htab_elem, hash_node);
625
626         if (next_l) {
627                 /* if next elem in this hash list is non-zero, just return it */
628                 memcpy(next_key, next_l->key, key_size);
629                 return 0;
630         }
631
632         /* no more elements in this hash list, go to the next bucket */
633         i = hash & (htab->n_buckets - 1);
634         i++;
635
636 find_first_elem:
637         /* iterate over buckets */
638         for (; i < htab->n_buckets; i++) {
639                 head = select_bucket(htab, i);
640
641                 /* pick first element in the bucket */
642                 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
643                                           struct htab_elem, hash_node);
644                 if (next_l) {
645                         /* if it's not empty, just return it */
646                         memcpy(next_key, next_l->key, key_size);
647                         return 0;
648                 }
649         }
650
651         /* iterated over all buckets and all elements */
652         return -ENOENT;
653 }
654
655 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
656 {
657         if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
658                 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
659         kfree(l);
660 }
661
662 static void htab_elem_free_rcu(struct rcu_head *head)
663 {
664         struct htab_elem *l = container_of(head, struct htab_elem, rcu);
665         struct bpf_htab *htab = l->htab;
666
667         /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
668          * we're calling kfree, otherwise deadlock is possible if kprobes
669          * are placed somewhere inside of slub
670          */
671         preempt_disable();
672         __this_cpu_inc(bpf_prog_active);
673         htab_elem_free(htab, l);
674         __this_cpu_dec(bpf_prog_active);
675         preempt_enable();
676 }
677
678 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
679 {
680         struct bpf_map *map = &htab->map;
681
682         if (map->ops->map_fd_put_ptr) {
683                 void *ptr = fd_htab_map_get_ptr(map, l);
684
685                 map->ops->map_fd_put_ptr(ptr);
686         }
687
688         if (htab_is_prealloc(htab)) {
689                 pcpu_freelist_push(&htab->freelist, &l->fnode);
690         } else {
691                 atomic_dec(&htab->count);
692                 l->htab = htab;
693                 call_rcu(&l->rcu, htab_elem_free_rcu);
694         }
695 }
696
697 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
698                             void *value, bool onallcpus)
699 {
700         if (!onallcpus) {
701                 /* copy true value_size bytes */
702                 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
703         } else {
704                 u32 size = round_up(htab->map.value_size, 8);
705                 int off = 0, cpu;
706
707                 for_each_possible_cpu(cpu) {
708                         bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
709                                         value + off, size);
710                         off += size;
711                 }
712         }
713 }
714
715 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
716 {
717         return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
718                BITS_PER_LONG == 64;
719 }
720
721 static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
722 {
723         u32 size = htab->map.value_size;
724
725         if (percpu || fd_htab_map_needs_adjust(htab))
726                 size = round_up(size, 8);
727         return size;
728 }
729
730 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
731                                          void *value, u32 key_size, u32 hash,
732                                          bool percpu, bool onallcpus,
733                                          struct htab_elem *old_elem)
734 {
735         u32 size = htab_size_value(htab, percpu);
736         bool prealloc = htab_is_prealloc(htab);
737         struct htab_elem *l_new, **pl_new;
738         void __percpu *pptr;
739
740         if (prealloc) {
741                 if (old_elem) {
742                         /* if we're updating the existing element,
743                          * use per-cpu extra elems to avoid freelist_pop/push
744                          */
745                         pl_new = this_cpu_ptr(htab->extra_elems);
746                         l_new = *pl_new;
747                         *pl_new = old_elem;
748                 } else {
749                         struct pcpu_freelist_node *l;
750
751                         l = pcpu_freelist_pop(&htab->freelist);
752                         if (!l)
753                                 return ERR_PTR(-E2BIG);
754                         l_new = container_of(l, struct htab_elem, fnode);
755                 }
756         } else {
757                 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
758                         if (!old_elem) {
759                                 /* when map is full and update() is replacing
760                                  * old element, it's ok to allocate, since
761                                  * old element will be freed immediately.
762                                  * Otherwise return an error
763                                  */
764                                 l_new = ERR_PTR(-E2BIG);
765                                 goto dec_count;
766                         }
767                 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
768                                      htab->map.numa_node);
769                 if (!l_new) {
770                         l_new = ERR_PTR(-ENOMEM);
771                         goto dec_count;
772                 }
773         }
774
775         memcpy(l_new->key, key, key_size);
776         if (percpu) {
777                 if (prealloc) {
778                         pptr = htab_elem_get_ptr(l_new, key_size);
779                 } else {
780                         /* alloc_percpu zero-fills */
781                         pptr = __alloc_percpu_gfp(size, 8,
782                                                   GFP_ATOMIC | __GFP_NOWARN);
783                         if (!pptr) {
784                                 kfree(l_new);
785                                 l_new = ERR_PTR(-ENOMEM);
786                                 goto dec_count;
787                         }
788                 }
789
790                 pcpu_copy_value(htab, pptr, value, onallcpus);
791
792                 if (!prealloc)
793                         htab_elem_set_ptr(l_new, key_size, pptr);
794         } else {
795                 memcpy(l_new->key + round_up(key_size, 8), value, size);
796         }
797
798         l_new->hash = hash;
799         return l_new;
800 dec_count:
801         atomic_dec(&htab->count);
802         return l_new;
803 }
804
805 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
806                        u64 map_flags)
807 {
808         if (l_old && map_flags == BPF_NOEXIST)
809                 /* elem already exists */
810                 return -EEXIST;
811
812         if (!l_old && map_flags == BPF_EXIST)
813                 /* elem doesn't exist, cannot update it */
814                 return -ENOENT;
815
816         return 0;
817 }
818
819 /* Called from syscall or from eBPF program */
820 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
821                                 u64 map_flags)
822 {
823         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
824         struct htab_elem *l_new = NULL, *l_old;
825         struct hlist_nulls_head *head;
826         unsigned long flags;
827         struct bucket *b;
828         u32 key_size, hash;
829         int ret;
830
831         if (unlikely(map_flags > BPF_EXIST))
832                 /* unknown flags */
833                 return -EINVAL;
834
835         WARN_ON_ONCE(!rcu_read_lock_held());
836
837         key_size = map->key_size;
838
839         hash = htab_map_hash(key, key_size, htab->hashrnd);
840
841         b = __select_bucket(htab, hash);
842         head = &b->head;
843
844         /* bpf_map_update_elem() can be called in_irq() */
845         raw_spin_lock_irqsave(&b->lock, flags);
846
847         l_old = lookup_elem_raw(head, hash, key, key_size);
848
849         ret = check_flags(htab, l_old, map_flags);
850         if (ret)
851                 goto err;
852
853         l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
854                                 l_old);
855         if (IS_ERR(l_new)) {
856                 /* all pre-allocated elements are in use or memory exhausted */
857                 ret = PTR_ERR(l_new);
858                 goto err;
859         }
860
861         /* add new element to the head of the list, so that
862          * concurrent search will find it before old elem
863          */
864         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
865         if (l_old) {
866                 hlist_nulls_del_rcu(&l_old->hash_node);
867                 if (!htab_is_prealloc(htab))
868                         free_htab_elem(htab, l_old);
869         }
870         ret = 0;
871 err:
872         raw_spin_unlock_irqrestore(&b->lock, flags);
873         return ret;
874 }
875
876 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
877                                     u64 map_flags)
878 {
879         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
880         struct htab_elem *l_new, *l_old = NULL;
881         struct hlist_nulls_head *head;
882         unsigned long flags;
883         struct bucket *b;
884         u32 key_size, hash;
885         int ret;
886
887         if (unlikely(map_flags > BPF_EXIST))
888                 /* unknown flags */
889                 return -EINVAL;
890
891         WARN_ON_ONCE(!rcu_read_lock_held());
892
893         key_size = map->key_size;
894
895         hash = htab_map_hash(key, key_size, htab->hashrnd);
896
897         b = __select_bucket(htab, hash);
898         head = &b->head;
899
900         /* For LRU, we need to alloc before taking bucket's
901          * spinlock because getting free nodes from LRU may need
902          * to remove older elements from htab and this removal
903          * operation will need a bucket lock.
904          */
905         l_new = prealloc_lru_pop(htab, key, hash);
906         if (!l_new)
907                 return -ENOMEM;
908         memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
909
910         /* bpf_map_update_elem() can be called in_irq() */
911         raw_spin_lock_irqsave(&b->lock, flags);
912
913         l_old = lookup_elem_raw(head, hash, key, key_size);
914
915         ret = check_flags(htab, l_old, map_flags);
916         if (ret)
917                 goto err;
918
919         /* add new element to the head of the list, so that
920          * concurrent search will find it before old elem
921          */
922         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
923         if (l_old) {
924                 bpf_lru_node_set_ref(&l_new->lru_node);
925                 hlist_nulls_del_rcu(&l_old->hash_node);
926         }
927         ret = 0;
928
929 err:
930         raw_spin_unlock_irqrestore(&b->lock, flags);
931
932         if (ret)
933                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
934         else if (l_old)
935                 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
936
937         return ret;
938 }
939
940 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
941                                          void *value, u64 map_flags,
942                                          bool onallcpus)
943 {
944         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
945         struct htab_elem *l_new = NULL, *l_old;
946         struct hlist_nulls_head *head;
947         unsigned long flags;
948         struct bucket *b;
949         u32 key_size, hash;
950         int ret;
951
952         if (unlikely(map_flags > BPF_EXIST))
953                 /* unknown flags */
954                 return -EINVAL;
955
956         WARN_ON_ONCE(!rcu_read_lock_held());
957
958         key_size = map->key_size;
959
960         hash = htab_map_hash(key, key_size, htab->hashrnd);
961
962         b = __select_bucket(htab, hash);
963         head = &b->head;
964
965         /* bpf_map_update_elem() can be called in_irq() */
966         raw_spin_lock_irqsave(&b->lock, flags);
967
968         l_old = lookup_elem_raw(head, hash, key, key_size);
969
970         ret = check_flags(htab, l_old, map_flags);
971         if (ret)
972                 goto err;
973
974         if (l_old) {
975                 /* per-cpu hash map can update value in-place */
976                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
977                                 value, onallcpus);
978         } else {
979                 l_new = alloc_htab_elem(htab, key, value, key_size,
980                                         hash, true, onallcpus, NULL);
981                 if (IS_ERR(l_new)) {
982                         ret = PTR_ERR(l_new);
983                         goto err;
984                 }
985                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
986         }
987         ret = 0;
988 err:
989         raw_spin_unlock_irqrestore(&b->lock, flags);
990         return ret;
991 }
992
993 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
994                                              void *value, u64 map_flags,
995                                              bool onallcpus)
996 {
997         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
998         struct htab_elem *l_new = NULL, *l_old;
999         struct hlist_nulls_head *head;
1000         unsigned long flags;
1001         struct bucket *b;
1002         u32 key_size, hash;
1003         int ret;
1004
1005         if (unlikely(map_flags > BPF_EXIST))
1006                 /* unknown flags */
1007                 return -EINVAL;
1008
1009         WARN_ON_ONCE(!rcu_read_lock_held());
1010
1011         key_size = map->key_size;
1012
1013         hash = htab_map_hash(key, key_size, htab->hashrnd);
1014
1015         b = __select_bucket(htab, hash);
1016         head = &b->head;
1017
1018         /* For LRU, we need to alloc before taking bucket's
1019          * spinlock because LRU's elem alloc may need
1020          * to remove older elem from htab and this removal
1021          * operation will need a bucket lock.
1022          */
1023         if (map_flags != BPF_EXIST) {
1024                 l_new = prealloc_lru_pop(htab, key, hash);
1025                 if (!l_new)
1026                         return -ENOMEM;
1027         }
1028
1029         /* bpf_map_update_elem() can be called in_irq() */
1030         raw_spin_lock_irqsave(&b->lock, flags);
1031
1032         l_old = lookup_elem_raw(head, hash, key, key_size);
1033
1034         ret = check_flags(htab, l_old, map_flags);
1035         if (ret)
1036                 goto err;
1037
1038         if (l_old) {
1039                 bpf_lru_node_set_ref(&l_old->lru_node);
1040
1041                 /* per-cpu hash map can update value in-place */
1042                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1043                                 value, onallcpus);
1044         } else {
1045                 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1046                                 value, onallcpus);
1047                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1048                 l_new = NULL;
1049         }
1050         ret = 0;
1051 err:
1052         raw_spin_unlock_irqrestore(&b->lock, flags);
1053         if (l_new)
1054                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1055         return ret;
1056 }
1057
1058 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1059                                        void *value, u64 map_flags)
1060 {
1061         return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1062 }
1063
1064 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1065                                            void *value, u64 map_flags)
1066 {
1067         return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1068                                                  false);
1069 }
1070
1071 /* Called from syscall or from eBPF program */
1072 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1073 {
1074         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1075         struct hlist_nulls_head *head;
1076         struct bucket *b;
1077         struct htab_elem *l;
1078         unsigned long flags;
1079         u32 hash, key_size;
1080         int ret = -ENOENT;
1081
1082         WARN_ON_ONCE(!rcu_read_lock_held());
1083
1084         key_size = map->key_size;
1085
1086         hash = htab_map_hash(key, key_size, htab->hashrnd);
1087         b = __select_bucket(htab, hash);
1088         head = &b->head;
1089
1090         raw_spin_lock_irqsave(&b->lock, flags);
1091
1092         l = lookup_elem_raw(head, hash, key, key_size);
1093
1094         if (l) {
1095                 hlist_nulls_del_rcu(&l->hash_node);
1096                 free_htab_elem(htab, l);
1097                 ret = 0;
1098         }
1099
1100         raw_spin_unlock_irqrestore(&b->lock, flags);
1101         return ret;
1102 }
1103
1104 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1105 {
1106         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1107         struct hlist_nulls_head *head;
1108         struct bucket *b;
1109         struct htab_elem *l;
1110         unsigned long flags;
1111         u32 hash, key_size;
1112         int ret = -ENOENT;
1113
1114         WARN_ON_ONCE(!rcu_read_lock_held());
1115
1116         key_size = map->key_size;
1117
1118         hash = htab_map_hash(key, key_size, htab->hashrnd);
1119         b = __select_bucket(htab, hash);
1120         head = &b->head;
1121
1122         raw_spin_lock_irqsave(&b->lock, flags);
1123
1124         l = lookup_elem_raw(head, hash, key, key_size);
1125
1126         if (l) {
1127                 hlist_nulls_del_rcu(&l->hash_node);
1128                 ret = 0;
1129         }
1130
1131         raw_spin_unlock_irqrestore(&b->lock, flags);
1132         if (l)
1133                 bpf_lru_push_free(&htab->lru, &l->lru_node);
1134         return ret;
1135 }
1136
1137 static void delete_all_elements(struct bpf_htab *htab)
1138 {
1139         int i;
1140
1141         for (i = 0; i < htab->n_buckets; i++) {
1142                 struct hlist_nulls_head *head = select_bucket(htab, i);
1143                 struct hlist_nulls_node *n;
1144                 struct htab_elem *l;
1145
1146                 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1147                         hlist_nulls_del_rcu(&l->hash_node);
1148                         htab_elem_free(htab, l);
1149                 }
1150         }
1151 }
1152
1153 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1154 static void htab_map_free(struct bpf_map *map)
1155 {
1156         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1157
1158         /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1159          * so the programs (can be more than one that used this map) were
1160          * disconnected from events. Wait for outstanding critical sections in
1161          * these programs to complete
1162          */
1163         synchronize_rcu();
1164
1165         /* some of free_htab_elem() callbacks for elements of this map may
1166          * not have executed. Wait for them.
1167          */
1168         rcu_barrier();
1169         if (!htab_is_prealloc(htab))
1170                 delete_all_elements(htab);
1171         else
1172                 prealloc_destroy(htab);
1173
1174         free_percpu(htab->extra_elems);
1175         bpf_map_area_free(htab->buckets);
1176         kfree(htab);
1177 }
1178
1179 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1180                                    struct seq_file *m)
1181 {
1182         void *value;
1183
1184         rcu_read_lock();
1185
1186         value = htab_map_lookup_elem(map, key);
1187         if (!value) {
1188                 rcu_read_unlock();
1189                 return;
1190         }
1191
1192         btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1193         seq_puts(m, ": ");
1194         btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1195         seq_puts(m, "\n");
1196
1197         rcu_read_unlock();
1198 }
1199
1200 const struct bpf_map_ops htab_map_ops = {
1201         .map_alloc_check = htab_map_alloc_check,
1202         .map_alloc = htab_map_alloc,
1203         .map_free = htab_map_free,
1204         .map_get_next_key = htab_map_get_next_key,
1205         .map_lookup_elem = htab_map_lookup_elem,
1206         .map_update_elem = htab_map_update_elem,
1207         .map_delete_elem = htab_map_delete_elem,
1208         .map_gen_lookup = htab_map_gen_lookup,
1209         .map_seq_show_elem = htab_map_seq_show_elem,
1210 };
1211
1212 const struct bpf_map_ops htab_lru_map_ops = {
1213         .map_alloc_check = htab_map_alloc_check,
1214         .map_alloc = htab_map_alloc,
1215         .map_free = htab_map_free,
1216         .map_get_next_key = htab_map_get_next_key,
1217         .map_lookup_elem = htab_lru_map_lookup_elem,
1218         .map_update_elem = htab_lru_map_update_elem,
1219         .map_delete_elem = htab_lru_map_delete_elem,
1220         .map_gen_lookup = htab_lru_map_gen_lookup,
1221         .map_seq_show_elem = htab_map_seq_show_elem,
1222 };
1223
1224 /* Called from eBPF program */
1225 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1226 {
1227         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1228
1229         if (l)
1230                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1231         else
1232                 return NULL;
1233 }
1234
1235 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1236 {
1237         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1238
1239         if (l) {
1240                 bpf_lru_node_set_ref(&l->lru_node);
1241                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1242         }
1243
1244         return NULL;
1245 }
1246
1247 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1248 {
1249         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1250         struct htab_elem *l;
1251         void __percpu *pptr;
1252         int ret = -ENOENT;
1253         int cpu, off = 0;
1254         u32 size;
1255
1256         /* per_cpu areas are zero-filled and bpf programs can only
1257          * access 'value_size' of them, so copying rounded areas
1258          * will not leak any kernel data
1259          */
1260         size = round_up(map->value_size, 8);
1261         rcu_read_lock();
1262         l = __htab_map_lookup_elem(map, key);
1263         if (!l)
1264                 goto out;
1265         if (htab_is_lru(htab))
1266                 bpf_lru_node_set_ref(&l->lru_node);
1267         pptr = htab_elem_get_ptr(l, map->key_size);
1268         for_each_possible_cpu(cpu) {
1269                 bpf_long_memcpy(value + off,
1270                                 per_cpu_ptr(pptr, cpu), size);
1271                 off += size;
1272         }
1273         ret = 0;
1274 out:
1275         rcu_read_unlock();
1276         return ret;
1277 }
1278
1279 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1280                            u64 map_flags)
1281 {
1282         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1283         int ret;
1284
1285         rcu_read_lock();
1286         if (htab_is_lru(htab))
1287                 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1288                                                         map_flags, true);
1289         else
1290                 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1291                                                     true);
1292         rcu_read_unlock();
1293
1294         return ret;
1295 }
1296
1297 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1298                                           struct seq_file *m)
1299 {
1300         struct htab_elem *l;
1301         void __percpu *pptr;
1302         int cpu;
1303
1304         rcu_read_lock();
1305
1306         l = __htab_map_lookup_elem(map, key);
1307         if (!l) {
1308                 rcu_read_unlock();
1309                 return;
1310         }
1311
1312         btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1313         seq_puts(m, ": {\n");
1314         pptr = htab_elem_get_ptr(l, map->key_size);
1315         for_each_possible_cpu(cpu) {
1316                 seq_printf(m, "\tcpu%d: ", cpu);
1317                 btf_type_seq_show(map->btf, map->btf_value_type_id,
1318                                   per_cpu_ptr(pptr, cpu), m);
1319                 seq_puts(m, "\n");
1320         }
1321         seq_puts(m, "}\n");
1322
1323         rcu_read_unlock();
1324 }
1325
1326 const struct bpf_map_ops htab_percpu_map_ops = {
1327         .map_alloc_check = htab_map_alloc_check,
1328         .map_alloc = htab_map_alloc,
1329         .map_free = htab_map_free,
1330         .map_get_next_key = htab_map_get_next_key,
1331         .map_lookup_elem = htab_percpu_map_lookup_elem,
1332         .map_update_elem = htab_percpu_map_update_elem,
1333         .map_delete_elem = htab_map_delete_elem,
1334         .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1335 };
1336
1337 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1338         .map_alloc_check = htab_map_alloc_check,
1339         .map_alloc = htab_map_alloc,
1340         .map_free = htab_map_free,
1341         .map_get_next_key = htab_map_get_next_key,
1342         .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1343         .map_update_elem = htab_lru_percpu_map_update_elem,
1344         .map_delete_elem = htab_lru_map_delete_elem,
1345         .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1346 };
1347
1348 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1349 {
1350         if (attr->value_size != sizeof(u32))
1351                 return -EINVAL;
1352         return htab_map_alloc_check(attr);
1353 }
1354
1355 static void fd_htab_map_free(struct bpf_map *map)
1356 {
1357         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1358         struct hlist_nulls_node *n;
1359         struct hlist_nulls_head *head;
1360         struct htab_elem *l;
1361         int i;
1362
1363         for (i = 0; i < htab->n_buckets; i++) {
1364                 head = select_bucket(htab, i);
1365
1366                 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1367                         void *ptr = fd_htab_map_get_ptr(map, l);
1368
1369                         map->ops->map_fd_put_ptr(ptr);
1370                 }
1371         }
1372
1373         htab_map_free(map);
1374 }
1375
1376 /* only called from syscall */
1377 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1378 {
1379         void **ptr;
1380         int ret = 0;
1381
1382         if (!map->ops->map_fd_sys_lookup_elem)
1383                 return -ENOTSUPP;
1384
1385         rcu_read_lock();
1386         ptr = htab_map_lookup_elem(map, key);
1387         if (ptr)
1388                 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1389         else
1390                 ret = -ENOENT;
1391         rcu_read_unlock();
1392
1393         return ret;
1394 }
1395
1396 /* only called from syscall */
1397 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1398                                 void *key, void *value, u64 map_flags)
1399 {
1400         void *ptr;
1401         int ret;
1402         u32 ufd = *(u32 *)value;
1403
1404         ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1405         if (IS_ERR(ptr))
1406                 return PTR_ERR(ptr);
1407
1408         ret = htab_map_update_elem(map, key, &ptr, map_flags);
1409         if (ret)
1410                 map->ops->map_fd_put_ptr(ptr);
1411
1412         return ret;
1413 }
1414
1415 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1416 {
1417         struct bpf_map *map, *inner_map_meta;
1418
1419         inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1420         if (IS_ERR(inner_map_meta))
1421                 return inner_map_meta;
1422
1423         map = htab_map_alloc(attr);
1424         if (IS_ERR(map)) {
1425                 bpf_map_meta_free(inner_map_meta);
1426                 return map;
1427         }
1428
1429         map->inner_map_meta = inner_map_meta;
1430
1431         return map;
1432 }
1433
1434 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1435 {
1436         struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
1437
1438         if (!inner_map)
1439                 return NULL;
1440
1441         return READ_ONCE(*inner_map);
1442 }
1443
1444 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1445                                   struct bpf_insn *insn_buf)
1446 {
1447         struct bpf_insn *insn = insn_buf;
1448         const int ret = BPF_REG_0;
1449
1450         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1451                      (void *(*)(struct bpf_map *map, void *key))NULL));
1452         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1453         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1454         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1455                                 offsetof(struct htab_elem, key) +
1456                                 round_up(map->key_size, 8));
1457         *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1458
1459         return insn - insn_buf;
1460 }
1461
1462 static void htab_of_map_free(struct bpf_map *map)
1463 {
1464         bpf_map_meta_free(map->inner_map_meta);
1465         fd_htab_map_free(map);
1466 }
1467
1468 const struct bpf_map_ops htab_of_maps_map_ops = {
1469         .map_alloc_check = fd_htab_map_alloc_check,
1470         .map_alloc = htab_of_map_alloc,
1471         .map_free = htab_of_map_free,
1472         .map_get_next_key = htab_map_get_next_key,
1473         .map_lookup_elem = htab_of_map_lookup_elem,
1474         .map_delete_elem = htab_map_delete_elem,
1475         .map_fd_get_ptr = bpf_map_fd_get_ptr,
1476         .map_fd_put_ptr = bpf_map_fd_put_ptr,
1477         .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1478         .map_gen_lookup = htab_of_map_gen_lookup,
1479         .map_check_btf = map_check_no_btf,
1480 };