bpf: move memory size checks to bpf_map_charge_init()
[linux-2.6-microblaze.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_ACCESS_MASK | 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             !bpf_map_flags_access_ok(attr->map_flags))
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 map size is larger than memlock limit, reject it */
364         err = bpf_map_charge_init(&htab->map.memory, cost);
365         if (err)
366                 goto free_htab;
367
368         err = -ENOMEM;
369         htab->buckets = bpf_map_area_alloc(htab->n_buckets *
370                                            sizeof(struct bucket),
371                                            htab->map.numa_node);
372         if (!htab->buckets)
373                 goto free_charge;
374
375         if (htab->map.map_flags & BPF_F_ZERO_SEED)
376                 htab->hashrnd = 0;
377         else
378                 htab->hashrnd = get_random_int();
379
380         for (i = 0; i < htab->n_buckets; i++) {
381                 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
382                 raw_spin_lock_init(&htab->buckets[i].lock);
383         }
384
385         if (prealloc) {
386                 err = prealloc_init(htab);
387                 if (err)
388                         goto free_buckets;
389
390                 if (!percpu && !lru) {
391                         /* lru itself can remove the least used element, so
392                          * there is no need for an extra elem during map_update.
393                          */
394                         err = alloc_extra_elems(htab);
395                         if (err)
396                                 goto free_prealloc;
397                 }
398         }
399
400         return &htab->map;
401
402 free_prealloc:
403         prealloc_destroy(htab);
404 free_buckets:
405         bpf_map_area_free(htab->buckets);
406 free_charge:
407         bpf_map_charge_finish(&htab->map.memory);
408 free_htab:
409         kfree(htab);
410         return ERR_PTR(err);
411 }
412
413 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
414 {
415         return jhash(key, key_len, hashrnd);
416 }
417
418 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
419 {
420         return &htab->buckets[hash & (htab->n_buckets - 1)];
421 }
422
423 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
424 {
425         return &__select_bucket(htab, hash)->head;
426 }
427
428 /* this lookup function can only be called with bucket lock taken */
429 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
430                                          void *key, u32 key_size)
431 {
432         struct hlist_nulls_node *n;
433         struct htab_elem *l;
434
435         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
436                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
437                         return l;
438
439         return NULL;
440 }
441
442 /* can be called without bucket lock. it will repeat the loop in
443  * the unlikely event when elements moved from one bucket into another
444  * while link list is being walked
445  */
446 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
447                                                u32 hash, void *key,
448                                                u32 key_size, u32 n_buckets)
449 {
450         struct hlist_nulls_node *n;
451         struct htab_elem *l;
452
453 again:
454         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
455                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
456                         return l;
457
458         if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
459                 goto again;
460
461         return NULL;
462 }
463
464 /* Called from syscall or from eBPF program directly, so
465  * arguments have to match bpf_map_lookup_elem() exactly.
466  * The return value is adjusted by BPF instructions
467  * in htab_map_gen_lookup().
468  */
469 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
470 {
471         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
472         struct hlist_nulls_head *head;
473         struct htab_elem *l;
474         u32 hash, key_size;
475
476         /* Must be called with rcu_read_lock. */
477         WARN_ON_ONCE(!rcu_read_lock_held());
478
479         key_size = map->key_size;
480
481         hash = htab_map_hash(key, key_size, htab->hashrnd);
482
483         head = select_bucket(htab, hash);
484
485         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
486
487         return l;
488 }
489
490 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
491 {
492         struct htab_elem *l = __htab_map_lookup_elem(map, key);
493
494         if (l)
495                 return l->key + round_up(map->key_size, 8);
496
497         return NULL;
498 }
499
500 /* inline bpf_map_lookup_elem() call.
501  * Instead of:
502  * bpf_prog
503  *   bpf_map_lookup_elem
504  *     map->ops->map_lookup_elem
505  *       htab_map_lookup_elem
506  *         __htab_map_lookup_elem
507  * do:
508  * bpf_prog
509  *   __htab_map_lookup_elem
510  */
511 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
512 {
513         struct bpf_insn *insn = insn_buf;
514         const int ret = BPF_REG_0;
515
516         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
517                      (void *(*)(struct bpf_map *map, void *key))NULL));
518         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
519         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
520         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
521                                 offsetof(struct htab_elem, key) +
522                                 round_up(map->key_size, 8));
523         return insn - insn_buf;
524 }
525
526 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
527                                                         void *key, const bool mark)
528 {
529         struct htab_elem *l = __htab_map_lookup_elem(map, key);
530
531         if (l) {
532                 if (mark)
533                         bpf_lru_node_set_ref(&l->lru_node);
534                 return l->key + round_up(map->key_size, 8);
535         }
536
537         return NULL;
538 }
539
540 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
541 {
542         return __htab_lru_map_lookup_elem(map, key, true);
543 }
544
545 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
546 {
547         return __htab_lru_map_lookup_elem(map, key, false);
548 }
549
550 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
551                                    struct bpf_insn *insn_buf)
552 {
553         struct bpf_insn *insn = insn_buf;
554         const int ret = BPF_REG_0;
555         const int ref_reg = BPF_REG_1;
556
557         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
558                      (void *(*)(struct bpf_map *map, void *key))NULL));
559         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
560         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
561         *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
562                               offsetof(struct htab_elem, lru_node) +
563                               offsetof(struct bpf_lru_node, ref));
564         *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
565         *insn++ = BPF_ST_MEM(BPF_B, ret,
566                              offsetof(struct htab_elem, lru_node) +
567                              offsetof(struct bpf_lru_node, ref),
568                              1);
569         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
570                                 offsetof(struct htab_elem, key) +
571                                 round_up(map->key_size, 8));
572         return insn - insn_buf;
573 }
574
575 /* It is called from the bpf_lru_list when the LRU needs to delete
576  * older elements from the htab.
577  */
578 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
579 {
580         struct bpf_htab *htab = (struct bpf_htab *)arg;
581         struct htab_elem *l = NULL, *tgt_l;
582         struct hlist_nulls_head *head;
583         struct hlist_nulls_node *n;
584         unsigned long flags;
585         struct bucket *b;
586
587         tgt_l = container_of(node, struct htab_elem, lru_node);
588         b = __select_bucket(htab, tgt_l->hash);
589         head = &b->head;
590
591         raw_spin_lock_irqsave(&b->lock, flags);
592
593         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
594                 if (l == tgt_l) {
595                         hlist_nulls_del_rcu(&l->hash_node);
596                         break;
597                 }
598
599         raw_spin_unlock_irqrestore(&b->lock, flags);
600
601         return l == tgt_l;
602 }
603
604 /* Called from syscall */
605 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
606 {
607         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
608         struct hlist_nulls_head *head;
609         struct htab_elem *l, *next_l;
610         u32 hash, key_size;
611         int i = 0;
612
613         WARN_ON_ONCE(!rcu_read_lock_held());
614
615         key_size = map->key_size;
616
617         if (!key)
618                 goto find_first_elem;
619
620         hash = htab_map_hash(key, key_size, htab->hashrnd);
621
622         head = select_bucket(htab, hash);
623
624         /* lookup the key */
625         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
626
627         if (!l)
628                 goto find_first_elem;
629
630         /* key was found, get next key in the same bucket */
631         next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
632                                   struct htab_elem, hash_node);
633
634         if (next_l) {
635                 /* if next elem in this hash list is non-zero, just return it */
636                 memcpy(next_key, next_l->key, key_size);
637                 return 0;
638         }
639
640         /* no more elements in this hash list, go to the next bucket */
641         i = hash & (htab->n_buckets - 1);
642         i++;
643
644 find_first_elem:
645         /* iterate over buckets */
646         for (; i < htab->n_buckets; i++) {
647                 head = select_bucket(htab, i);
648
649                 /* pick first element in the bucket */
650                 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
651                                           struct htab_elem, hash_node);
652                 if (next_l) {
653                         /* if it's not empty, just return it */
654                         memcpy(next_key, next_l->key, key_size);
655                         return 0;
656                 }
657         }
658
659         /* iterated over all buckets and all elements */
660         return -ENOENT;
661 }
662
663 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
664 {
665         if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
666                 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
667         kfree(l);
668 }
669
670 static void htab_elem_free_rcu(struct rcu_head *head)
671 {
672         struct htab_elem *l = container_of(head, struct htab_elem, rcu);
673         struct bpf_htab *htab = l->htab;
674
675         /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
676          * we're calling kfree, otherwise deadlock is possible if kprobes
677          * are placed somewhere inside of slub
678          */
679         preempt_disable();
680         __this_cpu_inc(bpf_prog_active);
681         htab_elem_free(htab, l);
682         __this_cpu_dec(bpf_prog_active);
683         preempt_enable();
684 }
685
686 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
687 {
688         struct bpf_map *map = &htab->map;
689
690         if (map->ops->map_fd_put_ptr) {
691                 void *ptr = fd_htab_map_get_ptr(map, l);
692
693                 map->ops->map_fd_put_ptr(ptr);
694         }
695
696         if (htab_is_prealloc(htab)) {
697                 __pcpu_freelist_push(&htab->freelist, &l->fnode);
698         } else {
699                 atomic_dec(&htab->count);
700                 l->htab = htab;
701                 call_rcu(&l->rcu, htab_elem_free_rcu);
702         }
703 }
704
705 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
706                             void *value, bool onallcpus)
707 {
708         if (!onallcpus) {
709                 /* copy true value_size bytes */
710                 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
711         } else {
712                 u32 size = round_up(htab->map.value_size, 8);
713                 int off = 0, cpu;
714
715                 for_each_possible_cpu(cpu) {
716                         bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
717                                         value + off, size);
718                         off += size;
719                 }
720         }
721 }
722
723 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
724 {
725         return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
726                BITS_PER_LONG == 64;
727 }
728
729 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
730                                          void *value, u32 key_size, u32 hash,
731                                          bool percpu, bool onallcpus,
732                                          struct htab_elem *old_elem)
733 {
734         u32 size = htab->map.value_size;
735         bool prealloc = htab_is_prealloc(htab);
736         struct htab_elem *l_new, **pl_new;
737         void __percpu *pptr;
738
739         if (prealloc) {
740                 if (old_elem) {
741                         /* if we're updating the existing element,
742                          * use per-cpu extra elems to avoid freelist_pop/push
743                          */
744                         pl_new = this_cpu_ptr(htab->extra_elems);
745                         l_new = *pl_new;
746                         *pl_new = old_elem;
747                 } else {
748                         struct pcpu_freelist_node *l;
749
750                         l = __pcpu_freelist_pop(&htab->freelist);
751                         if (!l)
752                                 return ERR_PTR(-E2BIG);
753                         l_new = container_of(l, struct htab_elem, fnode);
754                 }
755         } else {
756                 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
757                         if (!old_elem) {
758                                 /* when map is full and update() is replacing
759                                  * old element, it's ok to allocate, since
760                                  * old element will be freed immediately.
761                                  * Otherwise return an error
762                                  */
763                                 l_new = ERR_PTR(-E2BIG);
764                                 goto dec_count;
765                         }
766                 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
767                                      htab->map.numa_node);
768                 if (!l_new) {
769                         l_new = ERR_PTR(-ENOMEM);
770                         goto dec_count;
771                 }
772                 check_and_init_map_lock(&htab->map,
773                                         l_new->key + round_up(key_size, 8));
774         }
775
776         memcpy(l_new->key, key, key_size);
777         if (percpu) {
778                 size = round_up(size, 8);
779                 if (prealloc) {
780                         pptr = htab_elem_get_ptr(l_new, key_size);
781                 } else {
782                         /* alloc_percpu zero-fills */
783                         pptr = __alloc_percpu_gfp(size, 8,
784                                                   GFP_ATOMIC | __GFP_NOWARN);
785                         if (!pptr) {
786                                 kfree(l_new);
787                                 l_new = ERR_PTR(-ENOMEM);
788                                 goto dec_count;
789                         }
790                 }
791
792                 pcpu_copy_value(htab, pptr, value, onallcpus);
793
794                 if (!prealloc)
795                         htab_elem_set_ptr(l_new, key_size, pptr);
796         } else if (fd_htab_map_needs_adjust(htab)) {
797                 size = round_up(size, 8);
798                 memcpy(l_new->key + round_up(key_size, 8), value, size);
799         } else {
800                 copy_map_value(&htab->map,
801                                l_new->key + round_up(key_size, 8),
802                                value);
803         }
804
805         l_new->hash = hash;
806         return l_new;
807 dec_count:
808         atomic_dec(&htab->count);
809         return l_new;
810 }
811
812 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
813                        u64 map_flags)
814 {
815         if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
816                 /* elem already exists */
817                 return -EEXIST;
818
819         if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
820                 /* elem doesn't exist, cannot update it */
821                 return -ENOENT;
822
823         return 0;
824 }
825
826 /* Called from syscall or from eBPF program */
827 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
828                                 u64 map_flags)
829 {
830         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
831         struct htab_elem *l_new = NULL, *l_old;
832         struct hlist_nulls_head *head;
833         unsigned long flags;
834         struct bucket *b;
835         u32 key_size, hash;
836         int ret;
837
838         if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
839                 /* unknown flags */
840                 return -EINVAL;
841
842         WARN_ON_ONCE(!rcu_read_lock_held());
843
844         key_size = map->key_size;
845
846         hash = htab_map_hash(key, key_size, htab->hashrnd);
847
848         b = __select_bucket(htab, hash);
849         head = &b->head;
850
851         if (unlikely(map_flags & BPF_F_LOCK)) {
852                 if (unlikely(!map_value_has_spin_lock(map)))
853                         return -EINVAL;
854                 /* find an element without taking the bucket lock */
855                 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
856                                               htab->n_buckets);
857                 ret = check_flags(htab, l_old, map_flags);
858                 if (ret)
859                         return ret;
860                 if (l_old) {
861                         /* grab the element lock and update value in place */
862                         copy_map_value_locked(map,
863                                               l_old->key + round_up(key_size, 8),
864                                               value, false);
865                         return 0;
866                 }
867                 /* fall through, grab the bucket lock and lookup again.
868                  * 99.9% chance that the element won't be found,
869                  * but second lookup under lock has to be done.
870                  */
871         }
872
873         /* bpf_map_update_elem() can be called in_irq() */
874         raw_spin_lock_irqsave(&b->lock, flags);
875
876         l_old = lookup_elem_raw(head, hash, key, key_size);
877
878         ret = check_flags(htab, l_old, map_flags);
879         if (ret)
880                 goto err;
881
882         if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
883                 /* first lookup without the bucket lock didn't find the element,
884                  * but second lookup with the bucket lock found it.
885                  * This case is highly unlikely, but has to be dealt with:
886                  * grab the element lock in addition to the bucket lock
887                  * and update element in place
888                  */
889                 copy_map_value_locked(map,
890                                       l_old->key + round_up(key_size, 8),
891                                       value, false);
892                 ret = 0;
893                 goto err;
894         }
895
896         l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
897                                 l_old);
898         if (IS_ERR(l_new)) {
899                 /* all pre-allocated elements are in use or memory exhausted */
900                 ret = PTR_ERR(l_new);
901                 goto err;
902         }
903
904         /* add new element to the head of the list, so that
905          * concurrent search will find it before old elem
906          */
907         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
908         if (l_old) {
909                 hlist_nulls_del_rcu(&l_old->hash_node);
910                 if (!htab_is_prealloc(htab))
911                         free_htab_elem(htab, l_old);
912         }
913         ret = 0;
914 err:
915         raw_spin_unlock_irqrestore(&b->lock, flags);
916         return ret;
917 }
918
919 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
920                                     u64 map_flags)
921 {
922         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
923         struct htab_elem *l_new, *l_old = NULL;
924         struct hlist_nulls_head *head;
925         unsigned long flags;
926         struct bucket *b;
927         u32 key_size, hash;
928         int ret;
929
930         if (unlikely(map_flags > BPF_EXIST))
931                 /* unknown flags */
932                 return -EINVAL;
933
934         WARN_ON_ONCE(!rcu_read_lock_held());
935
936         key_size = map->key_size;
937
938         hash = htab_map_hash(key, key_size, htab->hashrnd);
939
940         b = __select_bucket(htab, hash);
941         head = &b->head;
942
943         /* For LRU, we need to alloc before taking bucket's
944          * spinlock because getting free nodes from LRU may need
945          * to remove older elements from htab and this removal
946          * operation will need a bucket lock.
947          */
948         l_new = prealloc_lru_pop(htab, key, hash);
949         if (!l_new)
950                 return -ENOMEM;
951         memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
952
953         /* bpf_map_update_elem() can be called in_irq() */
954         raw_spin_lock_irqsave(&b->lock, flags);
955
956         l_old = lookup_elem_raw(head, hash, key, key_size);
957
958         ret = check_flags(htab, l_old, map_flags);
959         if (ret)
960                 goto err;
961
962         /* add new element to the head of the list, so that
963          * concurrent search will find it before old elem
964          */
965         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
966         if (l_old) {
967                 bpf_lru_node_set_ref(&l_new->lru_node);
968                 hlist_nulls_del_rcu(&l_old->hash_node);
969         }
970         ret = 0;
971
972 err:
973         raw_spin_unlock_irqrestore(&b->lock, flags);
974
975         if (ret)
976                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
977         else if (l_old)
978                 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
979
980         return ret;
981 }
982
983 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
984                                          void *value, u64 map_flags,
985                                          bool onallcpus)
986 {
987         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
988         struct htab_elem *l_new = NULL, *l_old;
989         struct hlist_nulls_head *head;
990         unsigned long flags;
991         struct bucket *b;
992         u32 key_size, hash;
993         int ret;
994
995         if (unlikely(map_flags > BPF_EXIST))
996                 /* unknown flags */
997                 return -EINVAL;
998
999         WARN_ON_ONCE(!rcu_read_lock_held());
1000
1001         key_size = map->key_size;
1002
1003         hash = htab_map_hash(key, key_size, htab->hashrnd);
1004
1005         b = __select_bucket(htab, hash);
1006         head = &b->head;
1007
1008         /* bpf_map_update_elem() can be called in_irq() */
1009         raw_spin_lock_irqsave(&b->lock, flags);
1010
1011         l_old = lookup_elem_raw(head, hash, key, key_size);
1012
1013         ret = check_flags(htab, l_old, map_flags);
1014         if (ret)
1015                 goto err;
1016
1017         if (l_old) {
1018                 /* per-cpu hash map can update value in-place */
1019                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1020                                 value, onallcpus);
1021         } else {
1022                 l_new = alloc_htab_elem(htab, key, value, key_size,
1023                                         hash, true, onallcpus, NULL);
1024                 if (IS_ERR(l_new)) {
1025                         ret = PTR_ERR(l_new);
1026                         goto err;
1027                 }
1028                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1029         }
1030         ret = 0;
1031 err:
1032         raw_spin_unlock_irqrestore(&b->lock, flags);
1033         return ret;
1034 }
1035
1036 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1037                                              void *value, u64 map_flags,
1038                                              bool onallcpus)
1039 {
1040         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1041         struct htab_elem *l_new = NULL, *l_old;
1042         struct hlist_nulls_head *head;
1043         unsigned long flags;
1044         struct bucket *b;
1045         u32 key_size, hash;
1046         int ret;
1047
1048         if (unlikely(map_flags > BPF_EXIST))
1049                 /* unknown flags */
1050                 return -EINVAL;
1051
1052         WARN_ON_ONCE(!rcu_read_lock_held());
1053
1054         key_size = map->key_size;
1055
1056         hash = htab_map_hash(key, key_size, htab->hashrnd);
1057
1058         b = __select_bucket(htab, hash);
1059         head = &b->head;
1060
1061         /* For LRU, we need to alloc before taking bucket's
1062          * spinlock because LRU's elem alloc may need
1063          * to remove older elem from htab and this removal
1064          * operation will need a bucket lock.
1065          */
1066         if (map_flags != BPF_EXIST) {
1067                 l_new = prealloc_lru_pop(htab, key, hash);
1068                 if (!l_new)
1069                         return -ENOMEM;
1070         }
1071
1072         /* bpf_map_update_elem() can be called in_irq() */
1073         raw_spin_lock_irqsave(&b->lock, flags);
1074
1075         l_old = lookup_elem_raw(head, hash, key, key_size);
1076
1077         ret = check_flags(htab, l_old, map_flags);
1078         if (ret)
1079                 goto err;
1080
1081         if (l_old) {
1082                 bpf_lru_node_set_ref(&l_old->lru_node);
1083
1084                 /* per-cpu hash map can update value in-place */
1085                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1086                                 value, onallcpus);
1087         } else {
1088                 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1089                                 value, onallcpus);
1090                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1091                 l_new = NULL;
1092         }
1093         ret = 0;
1094 err:
1095         raw_spin_unlock_irqrestore(&b->lock, flags);
1096         if (l_new)
1097                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1098         return ret;
1099 }
1100
1101 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1102                                        void *value, u64 map_flags)
1103 {
1104         return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1105 }
1106
1107 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1108                                            void *value, u64 map_flags)
1109 {
1110         return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1111                                                  false);
1112 }
1113
1114 /* Called from syscall or from eBPF program */
1115 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1116 {
1117         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1118         struct hlist_nulls_head *head;
1119         struct bucket *b;
1120         struct htab_elem *l;
1121         unsigned long flags;
1122         u32 hash, key_size;
1123         int ret = -ENOENT;
1124
1125         WARN_ON_ONCE(!rcu_read_lock_held());
1126
1127         key_size = map->key_size;
1128
1129         hash = htab_map_hash(key, key_size, htab->hashrnd);
1130         b = __select_bucket(htab, hash);
1131         head = &b->head;
1132
1133         raw_spin_lock_irqsave(&b->lock, flags);
1134
1135         l = lookup_elem_raw(head, hash, key, key_size);
1136
1137         if (l) {
1138                 hlist_nulls_del_rcu(&l->hash_node);
1139                 free_htab_elem(htab, l);
1140                 ret = 0;
1141         }
1142
1143         raw_spin_unlock_irqrestore(&b->lock, flags);
1144         return ret;
1145 }
1146
1147 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1148 {
1149         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1150         struct hlist_nulls_head *head;
1151         struct bucket *b;
1152         struct htab_elem *l;
1153         unsigned long flags;
1154         u32 hash, key_size;
1155         int ret = -ENOENT;
1156
1157         WARN_ON_ONCE(!rcu_read_lock_held());
1158
1159         key_size = map->key_size;
1160
1161         hash = htab_map_hash(key, key_size, htab->hashrnd);
1162         b = __select_bucket(htab, hash);
1163         head = &b->head;
1164
1165         raw_spin_lock_irqsave(&b->lock, flags);
1166
1167         l = lookup_elem_raw(head, hash, key, key_size);
1168
1169         if (l) {
1170                 hlist_nulls_del_rcu(&l->hash_node);
1171                 ret = 0;
1172         }
1173
1174         raw_spin_unlock_irqrestore(&b->lock, flags);
1175         if (l)
1176                 bpf_lru_push_free(&htab->lru, &l->lru_node);
1177         return ret;
1178 }
1179
1180 static void delete_all_elements(struct bpf_htab *htab)
1181 {
1182         int i;
1183
1184         for (i = 0; i < htab->n_buckets; i++) {
1185                 struct hlist_nulls_head *head = select_bucket(htab, i);
1186                 struct hlist_nulls_node *n;
1187                 struct htab_elem *l;
1188
1189                 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1190                         hlist_nulls_del_rcu(&l->hash_node);
1191                         htab_elem_free(htab, l);
1192                 }
1193         }
1194 }
1195
1196 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1197 static void htab_map_free(struct bpf_map *map)
1198 {
1199         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1200
1201         /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1202          * so the programs (can be more than one that used this map) were
1203          * disconnected from events. Wait for outstanding critical sections in
1204          * these programs to complete
1205          */
1206         synchronize_rcu();
1207
1208         /* some of free_htab_elem() callbacks for elements of this map may
1209          * not have executed. Wait for them.
1210          */
1211         rcu_barrier();
1212         if (!htab_is_prealloc(htab))
1213                 delete_all_elements(htab);
1214         else
1215                 prealloc_destroy(htab);
1216
1217         free_percpu(htab->extra_elems);
1218         bpf_map_area_free(htab->buckets);
1219         kfree(htab);
1220 }
1221
1222 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1223                                    struct seq_file *m)
1224 {
1225         void *value;
1226
1227         rcu_read_lock();
1228
1229         value = htab_map_lookup_elem(map, key);
1230         if (!value) {
1231                 rcu_read_unlock();
1232                 return;
1233         }
1234
1235         btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1236         seq_puts(m, ": ");
1237         btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1238         seq_puts(m, "\n");
1239
1240         rcu_read_unlock();
1241 }
1242
1243 const struct bpf_map_ops htab_map_ops = {
1244         .map_alloc_check = htab_map_alloc_check,
1245         .map_alloc = htab_map_alloc,
1246         .map_free = htab_map_free,
1247         .map_get_next_key = htab_map_get_next_key,
1248         .map_lookup_elem = htab_map_lookup_elem,
1249         .map_update_elem = htab_map_update_elem,
1250         .map_delete_elem = htab_map_delete_elem,
1251         .map_gen_lookup = htab_map_gen_lookup,
1252         .map_seq_show_elem = htab_map_seq_show_elem,
1253 };
1254
1255 const struct bpf_map_ops htab_lru_map_ops = {
1256         .map_alloc_check = htab_map_alloc_check,
1257         .map_alloc = htab_map_alloc,
1258         .map_free = htab_map_free,
1259         .map_get_next_key = htab_map_get_next_key,
1260         .map_lookup_elem = htab_lru_map_lookup_elem,
1261         .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1262         .map_update_elem = htab_lru_map_update_elem,
1263         .map_delete_elem = htab_lru_map_delete_elem,
1264         .map_gen_lookup = htab_lru_map_gen_lookup,
1265         .map_seq_show_elem = htab_map_seq_show_elem,
1266 };
1267
1268 /* Called from eBPF program */
1269 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1270 {
1271         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1272
1273         if (l)
1274                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1275         else
1276                 return NULL;
1277 }
1278
1279 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1280 {
1281         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1282
1283         if (l) {
1284                 bpf_lru_node_set_ref(&l->lru_node);
1285                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1286         }
1287
1288         return NULL;
1289 }
1290
1291 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1292 {
1293         struct htab_elem *l;
1294         void __percpu *pptr;
1295         int ret = -ENOENT;
1296         int cpu, off = 0;
1297         u32 size;
1298
1299         /* per_cpu areas are zero-filled and bpf programs can only
1300          * access 'value_size' of them, so copying rounded areas
1301          * will not leak any kernel data
1302          */
1303         size = round_up(map->value_size, 8);
1304         rcu_read_lock();
1305         l = __htab_map_lookup_elem(map, key);
1306         if (!l)
1307                 goto out;
1308         /* We do not mark LRU map element here in order to not mess up
1309          * eviction heuristics when user space does a map walk.
1310          */
1311         pptr = htab_elem_get_ptr(l, map->key_size);
1312         for_each_possible_cpu(cpu) {
1313                 bpf_long_memcpy(value + off,
1314                                 per_cpu_ptr(pptr, cpu), size);
1315                 off += size;
1316         }
1317         ret = 0;
1318 out:
1319         rcu_read_unlock();
1320         return ret;
1321 }
1322
1323 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1324                            u64 map_flags)
1325 {
1326         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1327         int ret;
1328
1329         rcu_read_lock();
1330         if (htab_is_lru(htab))
1331                 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1332                                                         map_flags, true);
1333         else
1334                 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1335                                                     true);
1336         rcu_read_unlock();
1337
1338         return ret;
1339 }
1340
1341 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1342                                           struct seq_file *m)
1343 {
1344         struct htab_elem *l;
1345         void __percpu *pptr;
1346         int cpu;
1347
1348         rcu_read_lock();
1349
1350         l = __htab_map_lookup_elem(map, key);
1351         if (!l) {
1352                 rcu_read_unlock();
1353                 return;
1354         }
1355
1356         btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1357         seq_puts(m, ": {\n");
1358         pptr = htab_elem_get_ptr(l, map->key_size);
1359         for_each_possible_cpu(cpu) {
1360                 seq_printf(m, "\tcpu%d: ", cpu);
1361                 btf_type_seq_show(map->btf, map->btf_value_type_id,
1362                                   per_cpu_ptr(pptr, cpu), m);
1363                 seq_puts(m, "\n");
1364         }
1365         seq_puts(m, "}\n");
1366
1367         rcu_read_unlock();
1368 }
1369
1370 const struct bpf_map_ops htab_percpu_map_ops = {
1371         .map_alloc_check = htab_map_alloc_check,
1372         .map_alloc = htab_map_alloc,
1373         .map_free = htab_map_free,
1374         .map_get_next_key = htab_map_get_next_key,
1375         .map_lookup_elem = htab_percpu_map_lookup_elem,
1376         .map_update_elem = htab_percpu_map_update_elem,
1377         .map_delete_elem = htab_map_delete_elem,
1378         .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1379 };
1380
1381 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1382         .map_alloc_check = htab_map_alloc_check,
1383         .map_alloc = htab_map_alloc,
1384         .map_free = htab_map_free,
1385         .map_get_next_key = htab_map_get_next_key,
1386         .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1387         .map_update_elem = htab_lru_percpu_map_update_elem,
1388         .map_delete_elem = htab_lru_map_delete_elem,
1389         .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1390 };
1391
1392 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1393 {
1394         if (attr->value_size != sizeof(u32))
1395                 return -EINVAL;
1396         return htab_map_alloc_check(attr);
1397 }
1398
1399 static void fd_htab_map_free(struct bpf_map *map)
1400 {
1401         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1402         struct hlist_nulls_node *n;
1403         struct hlist_nulls_head *head;
1404         struct htab_elem *l;
1405         int i;
1406
1407         for (i = 0; i < htab->n_buckets; i++) {
1408                 head = select_bucket(htab, i);
1409
1410                 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1411                         void *ptr = fd_htab_map_get_ptr(map, l);
1412
1413                         map->ops->map_fd_put_ptr(ptr);
1414                 }
1415         }
1416
1417         htab_map_free(map);
1418 }
1419
1420 /* only called from syscall */
1421 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1422 {
1423         void **ptr;
1424         int ret = 0;
1425
1426         if (!map->ops->map_fd_sys_lookup_elem)
1427                 return -ENOTSUPP;
1428
1429         rcu_read_lock();
1430         ptr = htab_map_lookup_elem(map, key);
1431         if (ptr)
1432                 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1433         else
1434                 ret = -ENOENT;
1435         rcu_read_unlock();
1436
1437         return ret;
1438 }
1439
1440 /* only called from syscall */
1441 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1442                                 void *key, void *value, u64 map_flags)
1443 {
1444         void *ptr;
1445         int ret;
1446         u32 ufd = *(u32 *)value;
1447
1448         ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1449         if (IS_ERR(ptr))
1450                 return PTR_ERR(ptr);
1451
1452         ret = htab_map_update_elem(map, key, &ptr, map_flags);
1453         if (ret)
1454                 map->ops->map_fd_put_ptr(ptr);
1455
1456         return ret;
1457 }
1458
1459 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1460 {
1461         struct bpf_map *map, *inner_map_meta;
1462
1463         inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1464         if (IS_ERR(inner_map_meta))
1465                 return inner_map_meta;
1466
1467         map = htab_map_alloc(attr);
1468         if (IS_ERR(map)) {
1469                 bpf_map_meta_free(inner_map_meta);
1470                 return map;
1471         }
1472
1473         map->inner_map_meta = inner_map_meta;
1474
1475         return map;
1476 }
1477
1478 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1479 {
1480         struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
1481
1482         if (!inner_map)
1483                 return NULL;
1484
1485         return READ_ONCE(*inner_map);
1486 }
1487
1488 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1489                                   struct bpf_insn *insn_buf)
1490 {
1491         struct bpf_insn *insn = insn_buf;
1492         const int ret = BPF_REG_0;
1493
1494         BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1495                      (void *(*)(struct bpf_map *map, void *key))NULL));
1496         *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1497         *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1498         *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1499                                 offsetof(struct htab_elem, key) +
1500                                 round_up(map->key_size, 8));
1501         *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1502
1503         return insn - insn_buf;
1504 }
1505
1506 static void htab_of_map_free(struct bpf_map *map)
1507 {
1508         bpf_map_meta_free(map->inner_map_meta);
1509         fd_htab_map_free(map);
1510 }
1511
1512 const struct bpf_map_ops htab_of_maps_map_ops = {
1513         .map_alloc_check = fd_htab_map_alloc_check,
1514         .map_alloc = htab_of_map_alloc,
1515         .map_free = htab_of_map_free,
1516         .map_get_next_key = htab_map_get_next_key,
1517         .map_lookup_elem = htab_of_map_lookup_elem,
1518         .map_delete_elem = htab_map_delete_elem,
1519         .map_fd_get_ptr = bpf_map_fd_get_ptr,
1520         .map_fd_put_ptr = bpf_map_fd_put_ptr,
1521         .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1522         .map_gen_lookup = htab_of_map_gen_lookup,
1523         .map_check_btf = map_check_no_btf,
1524 };