blob: 5cd8f52772790992aee5205ebc63966fcdcd89bf [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002/* Copyright (c) 2017 Facebook
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07003 */
4#include <linux/slab.h>
5#include <linux/bpf.h>
Alexei Starovoitov40ec00a2021-07-14 17:54:12 -07006#include <linux/btf.h>
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007
8#include "map_in_map.h"
9
10struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
11{
12 struct bpf_map *inner_map, *inner_map_meta;
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010013 u32 inner_map_meta_size;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070014 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
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070021 /* 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
Martin KaFai Lauf4d05252020-08-27 18:18:06 -070027 if (!inner_map->ops->map_meta_equal) {
28 fdput(f);
29 return ERR_PTR(-ENOTSUPP);
30 }
31
Alexei Starovoitovd83525c2019-01-31 15:40:04 -080032 if (map_value_has_spin_lock(inner_map)) {
33 fdput(f);
34 return ERR_PTR(-ENOTSUPP);
35 }
36
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010037 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);
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070043 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;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070052 inner_map_meta->max_entries = inner_map->max_entries;
Yonghong Songa115d0e2019-02-27 13:22:56 -080053 inner_map_meta->spin_lock_off = inner_map->spin_lock_off;
Alexei Starovoitov68134662021-07-14 17:54:10 -070054 inner_map_meta->timer_off = inner_map->timer_off;
Alexei Starovoitov40ec00a2021-07-14 17:54:12 -070055 if (inner_map->btf) {
56 btf_get(inner_map->btf);
57 inner_map_meta->btf = inner_map->btf;
58 }
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070059
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010060 /* Misc members not needed in bpf_map_meta_equal() check. */
61 inner_map_meta->ops = inner_map->ops;
62 if (inner_map->ops == &array_map_ops) {
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -070063 inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
Daniel Borkmann9d5564d2019-01-17 16:34:45 +010064 container_of(inner_map_meta, struct bpf_array, map)->index_mask =
65 container_of(inner_map, struct bpf_array, map)->index_mask;
66 }
67
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070068 fdput(f);
69 return inner_map_meta;
70}
71
72void bpf_map_meta_free(struct bpf_map *map_meta)
73{
Alexei Starovoitov40ec00a2021-07-14 17:54:12 -070074 btf_put(map_meta->btf);
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070075 kfree(map_meta);
76}
77
78bool bpf_map_meta_equal(const struct bpf_map *meta0,
79 const struct bpf_map *meta1)
80{
81 /* No need to compare ops because it is covered by map_type */
82 return meta0->map_type == meta1->map_type &&
83 meta0->key_size == meta1->key_size &&
84 meta0->value_size == meta1->value_size &&
Alexei Starovoitov68134662021-07-14 17:54:10 -070085 meta0->timer_off == meta1->timer_off &&
Martin KaFai Lau134fede2020-08-27 18:18:13 -070086 meta0->map_flags == meta1->map_flags;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070087}
88
89void *bpf_map_fd_get_ptr(struct bpf_map *map,
90 struct file *map_file /* not used */,
91 int ufd)
92{
Martin KaFai Lauf4d05252020-08-27 18:18:06 -070093 struct bpf_map *inner_map, *inner_map_meta;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070094 struct fd f;
95
96 f = fdget(ufd);
97 inner_map = __bpf_map_get(f);
98 if (IS_ERR(inner_map))
99 return inner_map;
100
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700101 inner_map_meta = map->inner_map_meta;
102 if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800103 bpf_map_inc(inner_map);
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700104 else
105 inner_map = ERR_PTR(-EINVAL);
106
107 fdput(f);
108 return inner_map;
109}
110
111void bpf_map_fd_put_ptr(void *ptr)
112{
113 /* ptr->ops->map_free() has to go through one
114 * rcu grace period by itself.
115 */
116 bpf_map_put(ptr);
117}
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700118
119u32 bpf_map_fd_sys_lookup_elem(void *ptr)
120{
121 return ((struct bpf_map *)ptr)->id;
122}