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