bpf: Fix elem_size not being set for inner maps
[platform/kernel/linux-starfive.git] / kernel / bpf / map_in_map.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include <linux/slab.h>
5 #include <linux/bpf.h>
6 #include <linux/btf.h>
7
8 #include "map_in_map.h"
9
10 struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
11 {
12         struct bpf_map *inner_map, *inner_map_meta;
13         u32 inner_map_meta_size;
14         struct fd f;
15
16         f = fdget(inner_map_ufd);
17         inner_map = __bpf_map_get(f);
18         if (IS_ERR(inner_map))
19                 return inner_map;
20
21         /* Does not support >1 level map-in-map */
22         if (inner_map->inner_map_meta) {
23                 fdput(f);
24                 return ERR_PTR(-EINVAL);
25         }
26
27         if (!inner_map->ops->map_meta_equal) {
28                 fdput(f);
29                 return ERR_PTR(-ENOTSUPP);
30         }
31
32         if (map_value_has_spin_lock(inner_map)) {
33                 fdput(f);
34                 return ERR_PTR(-ENOTSUPP);
35         }
36
37         inner_map_meta_size = sizeof(*inner_map_meta);
38         /* In some cases verifier needs to access beyond just base map. */
39         if (inner_map->ops == &array_map_ops)
40                 inner_map_meta_size = sizeof(struct bpf_array);
41
42         inner_map_meta = kzalloc(inner_map_meta_size, GFP_USER);
43         if (!inner_map_meta) {
44                 fdput(f);
45                 return ERR_PTR(-ENOMEM);
46         }
47
48         inner_map_meta->map_type = inner_map->map_type;
49         inner_map_meta->key_size = inner_map->key_size;
50         inner_map_meta->value_size = inner_map->value_size;
51         inner_map_meta->map_flags = inner_map->map_flags;
52         inner_map_meta->max_entries = inner_map->max_entries;
53         inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
54         inner_map_meta->timer_off = inner_map->timer_off;
55         inner_map_meta->kptr_off_tab = bpf_map_copy_kptr_off_tab(inner_map);
56         if (inner_map->btf) {
57                 btf_get(inner_map->btf);
58                 inner_map_meta->btf = inner_map->btf;
59         }
60
61         /* Misc members not needed in bpf_map_meta_equal() check. */
62         inner_map_meta->ops = inner_map->ops;
63         if (inner_map->ops == &array_map_ops) {
64                 struct bpf_array *inner_array_meta =
65                         container_of(inner_map_meta, struct bpf_array, map);
66                 struct bpf_array *inner_array = container_of(inner_map, struct bpf_array, map);
67
68                 inner_array_meta->index_mask = inner_array->index_mask;
69                 inner_array_meta->elem_size = inner_array->elem_size;
70                 inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
71         }
72
73         fdput(f);
74         return inner_map_meta;
75 }
76
77 void bpf_map_meta_free(struct bpf_map *map_meta)
78 {
79         bpf_map_free_kptr_off_tab(map_meta);
80         btf_put(map_meta->btf);
81         kfree(map_meta);
82 }
83
84 bool bpf_map_meta_equal(const struct bpf_map *meta0,
85                         const struct bpf_map *meta1)
86 {
87         /* No need to compare ops because it is covered by map_type */
88         return meta0->map_type == meta1->map_type &&
89                 meta0->key_size == meta1->key_size &&
90                 meta0->value_size == meta1->value_size &&
91                 meta0->timer_off == meta1->timer_off &&
92                 meta0->map_flags == meta1->map_flags &&
93                 bpf_map_equal_kptr_off_tab(meta0, meta1);
94 }
95
96 void *bpf_map_fd_get_ptr(struct bpf_map *map,
97                          struct file *map_file /* not used */,
98                          int ufd)
99 {
100         struct bpf_map *inner_map, *inner_map_meta;
101         struct fd f;
102
103         f = fdget(ufd);
104         inner_map = __bpf_map_get(f);
105         if (IS_ERR(inner_map))
106                 return inner_map;
107
108         inner_map_meta = map->inner_map_meta;
109         if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
110                 bpf_map_inc(inner_map);
111         else
112                 inner_map = ERR_PTR(-EINVAL);
113
114         fdput(f);
115         return inner_map;
116 }
117
118 void bpf_map_fd_put_ptr(void *ptr)
119 {
120         /* ptr->ops->map_free() has to go through one
121          * rcu grace period by itself.
122          */
123         bpf_map_put(ptr);
124 }
125
126 u32 bpf_map_fd_sys_lookup_elem(void *ptr)
127 {
128         return ((struct bpf_map *)ptr)->id;
129 }