Merge tag 'metag-for-v4.11-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.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/jhash.h>
15 #include <linux/filter.h>
16 #include <linux/rculist_nulls.h>
17 #include "percpu_freelist.h"
18 #include "bpf_lru_list.h"
19
20 struct bucket {
21         struct hlist_nulls_head head;
22         raw_spinlock_t lock;
23 };
24
25 struct bpf_htab {
26         struct bpf_map map;
27         struct bucket *buckets;
28         void *elems;
29         union {
30                 struct pcpu_freelist freelist;
31                 struct bpf_lru lru;
32         };
33         struct htab_elem *__percpu *extra_elems;
34         atomic_t count; /* number of elements in this hashtable */
35         u32 n_buckets;  /* number of hash buckets */
36         u32 elem_size;  /* size of each element in bytes */
37 };
38
39 /* each htab element is struct htab_elem + key + value */
40 struct htab_elem {
41         union {
42                 struct hlist_nulls_node hash_node;
43                 struct {
44                         void *padding;
45                         union {
46                                 struct bpf_htab *htab;
47                                 struct pcpu_freelist_node fnode;
48                         };
49                 };
50         };
51         union {
52                 struct rcu_head rcu;
53                 struct bpf_lru_node lru_node;
54         };
55         u32 hash;
56         char key[0] __aligned(8);
57 };
58
59 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
60
61 static bool htab_is_lru(const struct bpf_htab *htab)
62 {
63         return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
64                 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
65 }
66
67 static bool htab_is_percpu(const struct bpf_htab *htab)
68 {
69         return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
70                 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
71 }
72
73 static bool htab_is_prealloc(const struct bpf_htab *htab)
74 {
75         return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
76 }
77
78 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
79                                      void __percpu *pptr)
80 {
81         *(void __percpu **)(l->key + key_size) = pptr;
82 }
83
84 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
85 {
86         return *(void __percpu **)(l->key + key_size);
87 }
88
89 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
90 {
91         return (struct htab_elem *) (htab->elems + i * htab->elem_size);
92 }
93
94 static void htab_free_elems(struct bpf_htab *htab)
95 {
96         int i;
97
98         if (!htab_is_percpu(htab))
99                 goto free_elems;
100
101         for (i = 0; i < htab->map.max_entries; i++) {
102                 void __percpu *pptr;
103
104                 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
105                                          htab->map.key_size);
106                 free_percpu(pptr);
107         }
108 free_elems:
109         bpf_map_area_free(htab->elems);
110 }
111
112 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
113                                           u32 hash)
114 {
115         struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
116         struct htab_elem *l;
117
118         if (node) {
119                 l = container_of(node, struct htab_elem, lru_node);
120                 memcpy(l->key, key, htab->map.key_size);
121                 return l;
122         }
123
124         return NULL;
125 }
126
127 static int prealloc_init(struct bpf_htab *htab)
128 {
129         u32 num_entries = htab->map.max_entries;
130         int err = -ENOMEM, i;
131
132         if (!htab_is_percpu(htab) && !htab_is_lru(htab))
133                 num_entries += num_possible_cpus();
134
135         htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries);
136         if (!htab->elems)
137                 return -ENOMEM;
138
139         if (!htab_is_percpu(htab))
140                 goto skip_percpu_elems;
141
142         for (i = 0; i < num_entries; i++) {
143                 u32 size = round_up(htab->map.value_size, 8);
144                 void __percpu *pptr;
145
146                 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
147                 if (!pptr)
148                         goto free_elems;
149                 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
150                                   pptr);
151         }
152
153 skip_percpu_elems:
154         if (htab_is_lru(htab))
155                 err = bpf_lru_init(&htab->lru,
156                                    htab->map.map_flags & BPF_F_NO_COMMON_LRU,
157                                    offsetof(struct htab_elem, hash) -
158                                    offsetof(struct htab_elem, lru_node),
159                                    htab_lru_map_delete_node,
160                                    htab);
161         else
162                 err = pcpu_freelist_init(&htab->freelist);
163
164         if (err)
165                 goto free_elems;
166
167         if (htab_is_lru(htab))
168                 bpf_lru_populate(&htab->lru, htab->elems,
169                                  offsetof(struct htab_elem, lru_node),
170                                  htab->elem_size, num_entries);
171         else
172                 pcpu_freelist_populate(&htab->freelist,
173                                        htab->elems + offsetof(struct htab_elem, fnode),
174                                        htab->elem_size, num_entries);
175
176         return 0;
177
178 free_elems:
179         htab_free_elems(htab);
180         return err;
181 }
182
183 static void prealloc_destroy(struct bpf_htab *htab)
184 {
185         htab_free_elems(htab);
186
187         if (htab_is_lru(htab))
188                 bpf_lru_destroy(&htab->lru);
189         else
190                 pcpu_freelist_destroy(&htab->freelist);
191 }
192
193 static int alloc_extra_elems(struct bpf_htab *htab)
194 {
195         struct htab_elem *__percpu *pptr, *l_new;
196         struct pcpu_freelist_node *l;
197         int cpu;
198
199         pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
200                                   GFP_USER | __GFP_NOWARN);
201         if (!pptr)
202                 return -ENOMEM;
203
204         for_each_possible_cpu(cpu) {
205                 l = pcpu_freelist_pop(&htab->freelist);
206                 /* pop will succeed, since prealloc_init()
207                  * preallocated extra num_possible_cpus elements
208                  */
209                 l_new = container_of(l, struct htab_elem, fnode);
210                 *per_cpu_ptr(pptr, cpu) = l_new;
211         }
212         htab->extra_elems = pptr;
213         return 0;
214 }
215
216 /* Called from syscall */
217 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
218 {
219         bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
220                        attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
221         bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
222                     attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
223         /* percpu_lru means each cpu has its own LRU list.
224          * it is different from BPF_MAP_TYPE_PERCPU_HASH where
225          * the map's value itself is percpu.  percpu_lru has
226          * nothing to do with the map's value.
227          */
228         bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
229         bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
230         struct bpf_htab *htab;
231         int err, i;
232         u64 cost;
233
234         BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
235                      offsetof(struct htab_elem, hash_node.pprev));
236         BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
237                      offsetof(struct htab_elem, hash_node.pprev));
238
239         if (lru && !capable(CAP_SYS_ADMIN))
240                 /* LRU implementation is much complicated than other
241                  * maps.  Hence, limit to CAP_SYS_ADMIN for now.
242                  */
243                 return ERR_PTR(-EPERM);
244
245         if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
246                 /* reserved bits should not be used */
247                 return ERR_PTR(-EINVAL);
248
249         if (!lru && percpu_lru)
250                 return ERR_PTR(-EINVAL);
251
252         if (lru && !prealloc)
253                 return ERR_PTR(-ENOTSUPP);
254
255         htab = kzalloc(sizeof(*htab), GFP_USER);
256         if (!htab)
257                 return ERR_PTR(-ENOMEM);
258
259         /* mandatory map attributes */
260         htab->map.map_type = attr->map_type;
261         htab->map.key_size = attr->key_size;
262         htab->map.value_size = attr->value_size;
263         htab->map.max_entries = attr->max_entries;
264         htab->map.map_flags = attr->map_flags;
265
266         /* check sanity of attributes.
267          * value_size == 0 may be allowed in the future to use map as a set
268          */
269         err = -EINVAL;
270         if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
271             htab->map.value_size == 0)
272                 goto free_htab;
273
274         if (percpu_lru) {
275                 /* ensure each CPU's lru list has >=1 elements.
276                  * since we are at it, make each lru list has the same
277                  * number of elements.
278                  */
279                 htab->map.max_entries = roundup(attr->max_entries,
280                                                 num_possible_cpus());
281                 if (htab->map.max_entries < attr->max_entries)
282                         htab->map.max_entries = rounddown(attr->max_entries,
283                                                           num_possible_cpus());
284         }
285
286         /* hash table size must be power of 2 */
287         htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
288
289         err = -E2BIG;
290         if (htab->map.key_size > MAX_BPF_STACK)
291                 /* eBPF programs initialize keys on stack, so they cannot be
292                  * larger than max stack size
293                  */
294                 goto free_htab;
295
296         if (htab->map.value_size >= KMALLOC_MAX_SIZE -
297             MAX_BPF_STACK - sizeof(struct htab_elem))
298                 /* if value_size is bigger, the user space won't be able to
299                  * access the elements via bpf syscall. This check also makes
300                  * sure that the elem_size doesn't overflow and it's
301                  * kmalloc-able later in htab_map_update_elem()
302                  */
303                 goto free_htab;
304
305         if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
306                 /* make sure the size for pcpu_alloc() is reasonable */
307                 goto free_htab;
308
309         htab->elem_size = sizeof(struct htab_elem) +
310                           round_up(htab->map.key_size, 8);
311         if (percpu)
312                 htab->elem_size += sizeof(void *);
313         else
314                 htab->elem_size += round_up(htab->map.value_size, 8);
315
316         /* prevent zero size kmalloc and check for u32 overflow */
317         if (htab->n_buckets == 0 ||
318             htab->n_buckets > U32_MAX / sizeof(struct bucket))
319                 goto free_htab;
320
321         cost = (u64) htab->n_buckets * sizeof(struct bucket) +
322                (u64) htab->elem_size * htab->map.max_entries;
323
324         if (percpu)
325                 cost += (u64) round_up(htab->map.value_size, 8) *
326                         num_possible_cpus() * htab->map.max_entries;
327         else
328                cost += (u64) htab->elem_size * num_possible_cpus();
329
330         if (cost >= U32_MAX - PAGE_SIZE)
331                 /* make sure page count doesn't overflow */
332                 goto free_htab;
333
334         htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
335
336         /* if map size is larger than memlock limit, reject it early */
337         err = bpf_map_precharge_memlock(htab->map.pages);
338         if (err)
339                 goto free_htab;
340
341         err = -ENOMEM;
342         htab->buckets = bpf_map_area_alloc(htab->n_buckets *
343                                            sizeof(struct bucket));
344         if (!htab->buckets)
345                 goto free_htab;
346
347         for (i = 0; i < htab->n_buckets; i++) {
348                 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
349                 raw_spin_lock_init(&htab->buckets[i].lock);
350         }
351
352         if (prealloc) {
353                 err = prealloc_init(htab);
354                 if (err)
355                         goto free_buckets;
356
357                 if (!percpu && !lru) {
358                         /* lru itself can remove the least used element, so
359                          * there is no need for an extra elem during map_update.
360                          */
361                         err = alloc_extra_elems(htab);
362                         if (err)
363                                 goto free_prealloc;
364                 }
365         }
366
367         return &htab->map;
368
369 free_prealloc:
370         prealloc_destroy(htab);
371 free_buckets:
372         bpf_map_area_free(htab->buckets);
373 free_htab:
374         kfree(htab);
375         return ERR_PTR(err);
376 }
377
378 static inline u32 htab_map_hash(const void *key, u32 key_len)
379 {
380         return jhash(key, key_len, 0);
381 }
382
383 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
384 {
385         return &htab->buckets[hash & (htab->n_buckets - 1)];
386 }
387
388 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
389 {
390         return &__select_bucket(htab, hash)->head;
391 }
392
393 /* this lookup function can only be called with bucket lock taken */
394 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
395                                          void *key, u32 key_size)
396 {
397         struct hlist_nulls_node *n;
398         struct htab_elem *l;
399
400         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
401                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
402                         return l;
403
404         return NULL;
405 }
406
407 /* can be called without bucket lock. it will repeat the loop in
408  * the unlikely event when elements moved from one bucket into another
409  * while link list is being walked
410  */
411 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
412                                                u32 hash, void *key,
413                                                u32 key_size, u32 n_buckets)
414 {
415         struct hlist_nulls_node *n;
416         struct htab_elem *l;
417
418 again:
419         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
420                 if (l->hash == hash && !memcmp(&l->key, key, key_size))
421                         return l;
422
423         if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
424                 goto again;
425
426         return NULL;
427 }
428
429 /* Called from syscall or from eBPF program */
430 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
431 {
432         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
433         struct hlist_nulls_head *head;
434         struct htab_elem *l;
435         u32 hash, key_size;
436
437         /* Must be called with rcu_read_lock. */
438         WARN_ON_ONCE(!rcu_read_lock_held());
439
440         key_size = map->key_size;
441
442         hash = htab_map_hash(key, key_size);
443
444         head = select_bucket(htab, hash);
445
446         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
447
448         return l;
449 }
450
451 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
452 {
453         struct htab_elem *l = __htab_map_lookup_elem(map, key);
454
455         if (l)
456                 return l->key + round_up(map->key_size, 8);
457
458         return NULL;
459 }
460
461 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
462 {
463         struct htab_elem *l = __htab_map_lookup_elem(map, key);
464
465         if (l) {
466                 bpf_lru_node_set_ref(&l->lru_node);
467                 return l->key + round_up(map->key_size, 8);
468         }
469
470         return NULL;
471 }
472
473 /* It is called from the bpf_lru_list when the LRU needs to delete
474  * older elements from the htab.
475  */
476 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
477 {
478         struct bpf_htab *htab = (struct bpf_htab *)arg;
479         struct htab_elem *l = NULL, *tgt_l;
480         struct hlist_nulls_head *head;
481         struct hlist_nulls_node *n;
482         unsigned long flags;
483         struct bucket *b;
484
485         tgt_l = container_of(node, struct htab_elem, lru_node);
486         b = __select_bucket(htab, tgt_l->hash);
487         head = &b->head;
488
489         raw_spin_lock_irqsave(&b->lock, flags);
490
491         hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
492                 if (l == tgt_l) {
493                         hlist_nulls_del_rcu(&l->hash_node);
494                         break;
495                 }
496
497         raw_spin_unlock_irqrestore(&b->lock, flags);
498
499         return l == tgt_l;
500 }
501
502 /* Called from syscall */
503 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
504 {
505         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
506         struct hlist_nulls_head *head;
507         struct htab_elem *l, *next_l;
508         u32 hash, key_size;
509         int i;
510
511         WARN_ON_ONCE(!rcu_read_lock_held());
512
513         key_size = map->key_size;
514
515         hash = htab_map_hash(key, key_size);
516
517         head = select_bucket(htab, hash);
518
519         /* lookup the key */
520         l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
521
522         if (!l) {
523                 i = 0;
524                 goto find_first_elem;
525         }
526
527         /* key was found, get next key in the same bucket */
528         next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
529                                   struct htab_elem, hash_node);
530
531         if (next_l) {
532                 /* if next elem in this hash list is non-zero, just return it */
533                 memcpy(next_key, next_l->key, key_size);
534                 return 0;
535         }
536
537         /* no more elements in this hash list, go to the next bucket */
538         i = hash & (htab->n_buckets - 1);
539         i++;
540
541 find_first_elem:
542         /* iterate over buckets */
543         for (; i < htab->n_buckets; i++) {
544                 head = select_bucket(htab, i);
545
546                 /* pick first element in the bucket */
547                 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
548                                           struct htab_elem, hash_node);
549                 if (next_l) {
550                         /* if it's not empty, just return it */
551                         memcpy(next_key, next_l->key, key_size);
552                         return 0;
553                 }
554         }
555
556         /* iterated over all buckets and all elements */
557         return -ENOENT;
558 }
559
560 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
561 {
562         if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
563                 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
564         kfree(l);
565 }
566
567 static void htab_elem_free_rcu(struct rcu_head *head)
568 {
569         struct htab_elem *l = container_of(head, struct htab_elem, rcu);
570         struct bpf_htab *htab = l->htab;
571
572         /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
573          * we're calling kfree, otherwise deadlock is possible if kprobes
574          * are placed somewhere inside of slub
575          */
576         preempt_disable();
577         __this_cpu_inc(bpf_prog_active);
578         htab_elem_free(htab, l);
579         __this_cpu_dec(bpf_prog_active);
580         preempt_enable();
581 }
582
583 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
584 {
585         if (htab_is_prealloc(htab)) {
586                 pcpu_freelist_push(&htab->freelist, &l->fnode);
587         } else {
588                 atomic_dec(&htab->count);
589                 l->htab = htab;
590                 call_rcu(&l->rcu, htab_elem_free_rcu);
591         }
592 }
593
594 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
595                             void *value, bool onallcpus)
596 {
597         if (!onallcpus) {
598                 /* copy true value_size bytes */
599                 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
600         } else {
601                 u32 size = round_up(htab->map.value_size, 8);
602                 int off = 0, cpu;
603
604                 for_each_possible_cpu(cpu) {
605                         bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
606                                         value + off, size);
607                         off += size;
608                 }
609         }
610 }
611
612 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
613                                          void *value, u32 key_size, u32 hash,
614                                          bool percpu, bool onallcpus,
615                                          struct htab_elem *old_elem)
616 {
617         u32 size = htab->map.value_size;
618         bool prealloc = htab_is_prealloc(htab);
619         struct htab_elem *l_new, **pl_new;
620         void __percpu *pptr;
621
622         if (prealloc) {
623                 if (old_elem) {
624                         /* if we're updating the existing element,
625                          * use per-cpu extra elems to avoid freelist_pop/push
626                          */
627                         pl_new = this_cpu_ptr(htab->extra_elems);
628                         l_new = *pl_new;
629                         *pl_new = old_elem;
630                 } else {
631                         struct pcpu_freelist_node *l;
632
633                         l = pcpu_freelist_pop(&htab->freelist);
634                         if (!l)
635                                 return ERR_PTR(-E2BIG);
636                         l_new = container_of(l, struct htab_elem, fnode);
637                 }
638         } else {
639                 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
640                         if (!old_elem) {
641                                 /* when map is full and update() is replacing
642                                  * old element, it's ok to allocate, since
643                                  * old element will be freed immediately.
644                                  * Otherwise return an error
645                                  */
646                                 atomic_dec(&htab->count);
647                                 return ERR_PTR(-E2BIG);
648                         }
649                 l_new = kmalloc(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN);
650                 if (!l_new)
651                         return ERR_PTR(-ENOMEM);
652         }
653
654         memcpy(l_new->key, key, key_size);
655         if (percpu) {
656                 /* round up value_size to 8 bytes */
657                 size = round_up(size, 8);
658
659                 if (prealloc) {
660                         pptr = htab_elem_get_ptr(l_new, key_size);
661                 } else {
662                         /* alloc_percpu zero-fills */
663                         pptr = __alloc_percpu_gfp(size, 8,
664                                                   GFP_ATOMIC | __GFP_NOWARN);
665                         if (!pptr) {
666                                 kfree(l_new);
667                                 return ERR_PTR(-ENOMEM);
668                         }
669                 }
670
671                 pcpu_copy_value(htab, pptr, value, onallcpus);
672
673                 if (!prealloc)
674                         htab_elem_set_ptr(l_new, key_size, pptr);
675         } else {
676                 memcpy(l_new->key + round_up(key_size, 8), value, size);
677         }
678
679         l_new->hash = hash;
680         return l_new;
681 }
682
683 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
684                        u64 map_flags)
685 {
686         if (l_old && map_flags == BPF_NOEXIST)
687                 /* elem already exists */
688                 return -EEXIST;
689
690         if (!l_old && map_flags == BPF_EXIST)
691                 /* elem doesn't exist, cannot update it */
692                 return -ENOENT;
693
694         return 0;
695 }
696
697 /* Called from syscall or from eBPF program */
698 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
699                                 u64 map_flags)
700 {
701         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
702         struct htab_elem *l_new = NULL, *l_old;
703         struct hlist_nulls_head *head;
704         unsigned long flags;
705         struct bucket *b;
706         u32 key_size, hash;
707         int ret;
708
709         if (unlikely(map_flags > BPF_EXIST))
710                 /* unknown flags */
711                 return -EINVAL;
712
713         WARN_ON_ONCE(!rcu_read_lock_held());
714
715         key_size = map->key_size;
716
717         hash = htab_map_hash(key, key_size);
718
719         b = __select_bucket(htab, hash);
720         head = &b->head;
721
722         /* bpf_map_update_elem() can be called in_irq() */
723         raw_spin_lock_irqsave(&b->lock, flags);
724
725         l_old = lookup_elem_raw(head, hash, key, key_size);
726
727         ret = check_flags(htab, l_old, map_flags);
728         if (ret)
729                 goto err;
730
731         l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
732                                 l_old);
733         if (IS_ERR(l_new)) {
734                 /* all pre-allocated elements are in use or memory exhausted */
735                 ret = PTR_ERR(l_new);
736                 goto err;
737         }
738
739         /* add new element to the head of the list, so that
740          * concurrent search will find it before old elem
741          */
742         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
743         if (l_old) {
744                 hlist_nulls_del_rcu(&l_old->hash_node);
745                 if (!htab_is_prealloc(htab))
746                         free_htab_elem(htab, l_old);
747         }
748         ret = 0;
749 err:
750         raw_spin_unlock_irqrestore(&b->lock, flags);
751         return ret;
752 }
753
754 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
755                                     u64 map_flags)
756 {
757         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
758         struct htab_elem *l_new, *l_old = NULL;
759         struct hlist_nulls_head *head;
760         unsigned long flags;
761         struct bucket *b;
762         u32 key_size, hash;
763         int ret;
764
765         if (unlikely(map_flags > BPF_EXIST))
766                 /* unknown flags */
767                 return -EINVAL;
768
769         WARN_ON_ONCE(!rcu_read_lock_held());
770
771         key_size = map->key_size;
772
773         hash = htab_map_hash(key, key_size);
774
775         b = __select_bucket(htab, hash);
776         head = &b->head;
777
778         /* For LRU, we need to alloc before taking bucket's
779          * spinlock because getting free nodes from LRU may need
780          * to remove older elements from htab and this removal
781          * operation will need a bucket lock.
782          */
783         l_new = prealloc_lru_pop(htab, key, hash);
784         if (!l_new)
785                 return -ENOMEM;
786         memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
787
788         /* bpf_map_update_elem() can be called in_irq() */
789         raw_spin_lock_irqsave(&b->lock, flags);
790
791         l_old = lookup_elem_raw(head, hash, key, key_size);
792
793         ret = check_flags(htab, l_old, map_flags);
794         if (ret)
795                 goto err;
796
797         /* add new element to the head of the list, so that
798          * concurrent search will find it before old elem
799          */
800         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
801         if (l_old) {
802                 bpf_lru_node_set_ref(&l_new->lru_node);
803                 hlist_nulls_del_rcu(&l_old->hash_node);
804         }
805         ret = 0;
806
807 err:
808         raw_spin_unlock_irqrestore(&b->lock, flags);
809
810         if (ret)
811                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
812         else if (l_old)
813                 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
814
815         return ret;
816 }
817
818 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
819                                          void *value, u64 map_flags,
820                                          bool onallcpus)
821 {
822         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
823         struct htab_elem *l_new = NULL, *l_old;
824         struct hlist_nulls_head *head;
825         unsigned long flags;
826         struct bucket *b;
827         u32 key_size, hash;
828         int ret;
829
830         if (unlikely(map_flags > BPF_EXIST))
831                 /* unknown flags */
832                 return -EINVAL;
833
834         WARN_ON_ONCE(!rcu_read_lock_held());
835
836         key_size = map->key_size;
837
838         hash = htab_map_hash(key, key_size);
839
840         b = __select_bucket(htab, hash);
841         head = &b->head;
842
843         /* bpf_map_update_elem() can be called in_irq() */
844         raw_spin_lock_irqsave(&b->lock, flags);
845
846         l_old = lookup_elem_raw(head, hash, key, key_size);
847
848         ret = check_flags(htab, l_old, map_flags);
849         if (ret)
850                 goto err;
851
852         if (l_old) {
853                 /* per-cpu hash map can update value in-place */
854                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
855                                 value, onallcpus);
856         } else {
857                 l_new = alloc_htab_elem(htab, key, value, key_size,
858                                         hash, true, onallcpus, NULL);
859                 if (IS_ERR(l_new)) {
860                         ret = PTR_ERR(l_new);
861                         goto err;
862                 }
863                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
864         }
865         ret = 0;
866 err:
867         raw_spin_unlock_irqrestore(&b->lock, flags);
868         return ret;
869 }
870
871 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
872                                              void *value, u64 map_flags,
873                                              bool onallcpus)
874 {
875         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
876         struct htab_elem *l_new = NULL, *l_old;
877         struct hlist_nulls_head *head;
878         unsigned long flags;
879         struct bucket *b;
880         u32 key_size, hash;
881         int ret;
882
883         if (unlikely(map_flags > BPF_EXIST))
884                 /* unknown flags */
885                 return -EINVAL;
886
887         WARN_ON_ONCE(!rcu_read_lock_held());
888
889         key_size = map->key_size;
890
891         hash = htab_map_hash(key, key_size);
892
893         b = __select_bucket(htab, hash);
894         head = &b->head;
895
896         /* For LRU, we need to alloc before taking bucket's
897          * spinlock because LRU's elem alloc may need
898          * to remove older elem from htab and this removal
899          * operation will need a bucket lock.
900          */
901         if (map_flags != BPF_EXIST) {
902                 l_new = prealloc_lru_pop(htab, key, hash);
903                 if (!l_new)
904                         return -ENOMEM;
905         }
906
907         /* bpf_map_update_elem() can be called in_irq() */
908         raw_spin_lock_irqsave(&b->lock, flags);
909
910         l_old = lookup_elem_raw(head, hash, key, key_size);
911
912         ret = check_flags(htab, l_old, map_flags);
913         if (ret)
914                 goto err;
915
916         if (l_old) {
917                 bpf_lru_node_set_ref(&l_old->lru_node);
918
919                 /* per-cpu hash map can update value in-place */
920                 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
921                                 value, onallcpus);
922         } else {
923                 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
924                                 value, onallcpus);
925                 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
926                 l_new = NULL;
927         }
928         ret = 0;
929 err:
930         raw_spin_unlock_irqrestore(&b->lock, flags);
931         if (l_new)
932                 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
933         return ret;
934 }
935
936 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
937                                        void *value, u64 map_flags)
938 {
939         return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
940 }
941
942 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
943                                            void *value, u64 map_flags)
944 {
945         return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
946                                                  false);
947 }
948
949 /* Called from syscall or from eBPF program */
950 static int htab_map_delete_elem(struct bpf_map *map, void *key)
951 {
952         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
953         struct hlist_nulls_head *head;
954         struct bucket *b;
955         struct htab_elem *l;
956         unsigned long flags;
957         u32 hash, key_size;
958         int ret = -ENOENT;
959
960         WARN_ON_ONCE(!rcu_read_lock_held());
961
962         key_size = map->key_size;
963
964         hash = htab_map_hash(key, key_size);
965         b = __select_bucket(htab, hash);
966         head = &b->head;
967
968         raw_spin_lock_irqsave(&b->lock, flags);
969
970         l = lookup_elem_raw(head, hash, key, key_size);
971
972         if (l) {
973                 hlist_nulls_del_rcu(&l->hash_node);
974                 free_htab_elem(htab, l);
975                 ret = 0;
976         }
977
978         raw_spin_unlock_irqrestore(&b->lock, flags);
979         return ret;
980 }
981
982 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
983 {
984         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
985         struct hlist_nulls_head *head;
986         struct bucket *b;
987         struct htab_elem *l;
988         unsigned long flags;
989         u32 hash, key_size;
990         int ret = -ENOENT;
991
992         WARN_ON_ONCE(!rcu_read_lock_held());
993
994         key_size = map->key_size;
995
996         hash = htab_map_hash(key, key_size);
997         b = __select_bucket(htab, hash);
998         head = &b->head;
999
1000         raw_spin_lock_irqsave(&b->lock, flags);
1001
1002         l = lookup_elem_raw(head, hash, key, key_size);
1003
1004         if (l) {
1005                 hlist_nulls_del_rcu(&l->hash_node);
1006                 ret = 0;
1007         }
1008
1009         raw_spin_unlock_irqrestore(&b->lock, flags);
1010         if (l)
1011                 bpf_lru_push_free(&htab->lru, &l->lru_node);
1012         return ret;
1013 }
1014
1015 static void delete_all_elements(struct bpf_htab *htab)
1016 {
1017         int i;
1018
1019         for (i = 0; i < htab->n_buckets; i++) {
1020                 struct hlist_nulls_head *head = select_bucket(htab, i);
1021                 struct hlist_nulls_node *n;
1022                 struct htab_elem *l;
1023
1024                 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1025                         hlist_nulls_del_rcu(&l->hash_node);
1026                         htab_elem_free(htab, l);
1027                 }
1028         }
1029 }
1030 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1031 static void htab_map_free(struct bpf_map *map)
1032 {
1033         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1034
1035         /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1036          * so the programs (can be more than one that used this map) were
1037          * disconnected from events. Wait for outstanding critical sections in
1038          * these programs to complete
1039          */
1040         synchronize_rcu();
1041
1042         /* some of free_htab_elem() callbacks for elements of this map may
1043          * not have executed. Wait for them.
1044          */
1045         rcu_barrier();
1046         if (!htab_is_prealloc(htab))
1047                 delete_all_elements(htab);
1048         else
1049                 prealloc_destroy(htab);
1050
1051         free_percpu(htab->extra_elems);
1052         bpf_map_area_free(htab->buckets);
1053         kfree(htab);
1054 }
1055
1056 static const struct bpf_map_ops htab_ops = {
1057         .map_alloc = htab_map_alloc,
1058         .map_free = htab_map_free,
1059         .map_get_next_key = htab_map_get_next_key,
1060         .map_lookup_elem = htab_map_lookup_elem,
1061         .map_update_elem = htab_map_update_elem,
1062         .map_delete_elem = htab_map_delete_elem,
1063 };
1064
1065 static struct bpf_map_type_list htab_type __ro_after_init = {
1066         .ops = &htab_ops,
1067         .type = BPF_MAP_TYPE_HASH,
1068 };
1069
1070 static const struct bpf_map_ops htab_lru_ops = {
1071         .map_alloc = htab_map_alloc,
1072         .map_free = htab_map_free,
1073         .map_get_next_key = htab_map_get_next_key,
1074         .map_lookup_elem = htab_lru_map_lookup_elem,
1075         .map_update_elem = htab_lru_map_update_elem,
1076         .map_delete_elem = htab_lru_map_delete_elem,
1077 };
1078
1079 static struct bpf_map_type_list htab_lru_type __ro_after_init = {
1080         .ops = &htab_lru_ops,
1081         .type = BPF_MAP_TYPE_LRU_HASH,
1082 };
1083
1084 /* Called from eBPF program */
1085 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1086 {
1087         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1088
1089         if (l)
1090                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1091         else
1092                 return NULL;
1093 }
1094
1095 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1096 {
1097         struct htab_elem *l = __htab_map_lookup_elem(map, key);
1098
1099         if (l) {
1100                 bpf_lru_node_set_ref(&l->lru_node);
1101                 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1102         }
1103
1104         return NULL;
1105 }
1106
1107 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1108 {
1109         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1110         struct htab_elem *l;
1111         void __percpu *pptr;
1112         int ret = -ENOENT;
1113         int cpu, off = 0;
1114         u32 size;
1115
1116         /* per_cpu areas are zero-filled and bpf programs can only
1117          * access 'value_size' of them, so copying rounded areas
1118          * will not leak any kernel data
1119          */
1120         size = round_up(map->value_size, 8);
1121         rcu_read_lock();
1122         l = __htab_map_lookup_elem(map, key);
1123         if (!l)
1124                 goto out;
1125         if (htab_is_lru(htab))
1126                 bpf_lru_node_set_ref(&l->lru_node);
1127         pptr = htab_elem_get_ptr(l, map->key_size);
1128         for_each_possible_cpu(cpu) {
1129                 bpf_long_memcpy(value + off,
1130                                 per_cpu_ptr(pptr, cpu), size);
1131                 off += size;
1132         }
1133         ret = 0;
1134 out:
1135         rcu_read_unlock();
1136         return ret;
1137 }
1138
1139 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1140                            u64 map_flags)
1141 {
1142         struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1143         int ret;
1144
1145         rcu_read_lock();
1146         if (htab_is_lru(htab))
1147                 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1148                                                         map_flags, true);
1149         else
1150                 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1151                                                     true);
1152         rcu_read_unlock();
1153
1154         return ret;
1155 }
1156
1157 static const struct bpf_map_ops htab_percpu_ops = {
1158         .map_alloc = htab_map_alloc,
1159         .map_free = htab_map_free,
1160         .map_get_next_key = htab_map_get_next_key,
1161         .map_lookup_elem = htab_percpu_map_lookup_elem,
1162         .map_update_elem = htab_percpu_map_update_elem,
1163         .map_delete_elem = htab_map_delete_elem,
1164 };
1165
1166 static struct bpf_map_type_list htab_percpu_type __ro_after_init = {
1167         .ops = &htab_percpu_ops,
1168         .type = BPF_MAP_TYPE_PERCPU_HASH,
1169 };
1170
1171 static const struct bpf_map_ops htab_lru_percpu_ops = {
1172         .map_alloc = htab_map_alloc,
1173         .map_free = htab_map_free,
1174         .map_get_next_key = htab_map_get_next_key,
1175         .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1176         .map_update_elem = htab_lru_percpu_map_update_elem,
1177         .map_delete_elem = htab_lru_map_delete_elem,
1178 };
1179
1180 static struct bpf_map_type_list htab_lru_percpu_type __ro_after_init = {
1181         .ops = &htab_lru_percpu_ops,
1182         .type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
1183 };
1184
1185 static int __init register_htab_map(void)
1186 {
1187         bpf_register_map_type(&htab_type);
1188         bpf_register_map_type(&htab_percpu_type);
1189         bpf_register_map_type(&htab_lru_type);
1190         bpf_register_map_type(&htab_lru_percpu_type);
1191         return 0;
1192 }
1193 late_initcall(register_htab_map);