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