bpf: rework memlock-based memory accounting for maps
[linux-2.6-microblaze.git] / kernel / bpf / xskmap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* XSKMAP used for AF_XDP sockets
3  * Copyright(c) 2018 Intel Corporation.
4  */
5
6 #include <linux/bpf.h>
7 #include <linux/capability.h>
8 #include <net/xdp_sock.h>
9 #include <linux/slab.h>
10 #include <linux/sched.h>
11
12 struct xsk_map {
13         struct bpf_map map;
14         struct xdp_sock **xsk_map;
15         struct list_head __percpu *flush_list;
16 };
17
18 static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
19 {
20         int cpu, err = -EINVAL;
21         struct xsk_map *m;
22         u64 cost;
23
24         if (!capable(CAP_NET_ADMIN))
25                 return ERR_PTR(-EPERM);
26
27         if (attr->max_entries == 0 || attr->key_size != 4 ||
28             attr->value_size != 4 ||
29             attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY))
30                 return ERR_PTR(-EINVAL);
31
32         m = kzalloc(sizeof(*m), GFP_USER);
33         if (!m)
34                 return ERR_PTR(-ENOMEM);
35
36         bpf_map_init_from_attr(&m->map, attr);
37
38         cost = (u64)m->map.max_entries * sizeof(struct xdp_sock *);
39         cost += sizeof(struct list_head) * num_possible_cpus();
40         if (cost >= U32_MAX - PAGE_SIZE)
41                 goto free_m;
42
43         /* Notice returns -EPERM on if map size is larger than memlock limit */
44         err = bpf_map_charge_init(&m->map.memory,
45                                   round_up(cost, PAGE_SIZE) >> PAGE_SHIFT);
46         if (err)
47                 goto free_m;
48
49         err = -ENOMEM;
50
51         m->flush_list = alloc_percpu(struct list_head);
52         if (!m->flush_list)
53                 goto free_charge;
54
55         for_each_possible_cpu(cpu)
56                 INIT_LIST_HEAD(per_cpu_ptr(m->flush_list, cpu));
57
58         m->xsk_map = bpf_map_area_alloc(m->map.max_entries *
59                                         sizeof(struct xdp_sock *),
60                                         m->map.numa_node);
61         if (!m->xsk_map)
62                 goto free_percpu;
63         return &m->map;
64
65 free_percpu:
66         free_percpu(m->flush_list);
67 free_charge:
68         bpf_map_charge_finish(&m->map.memory);
69 free_m:
70         kfree(m);
71         return ERR_PTR(err);
72 }
73
74 static void xsk_map_free(struct bpf_map *map)
75 {
76         struct xsk_map *m = container_of(map, struct xsk_map, map);
77         int i;
78
79         bpf_clear_redirect_map(map);
80         synchronize_net();
81
82         for (i = 0; i < map->max_entries; i++) {
83                 struct xdp_sock *xs;
84
85                 xs = m->xsk_map[i];
86                 if (!xs)
87                         continue;
88
89                 sock_put((struct sock *)xs);
90         }
91
92         free_percpu(m->flush_list);
93         bpf_map_area_free(m->xsk_map);
94         kfree(m);
95 }
96
97 static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
98 {
99         struct xsk_map *m = container_of(map, struct xsk_map, map);
100         u32 index = key ? *(u32 *)key : U32_MAX;
101         u32 *next = next_key;
102
103         if (index >= m->map.max_entries) {
104                 *next = 0;
105                 return 0;
106         }
107
108         if (index == m->map.max_entries - 1)
109                 return -ENOENT;
110         *next = index + 1;
111         return 0;
112 }
113
114 struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
115 {
116         struct xsk_map *m = container_of(map, struct xsk_map, map);
117         struct xdp_sock *xs;
118
119         if (key >= map->max_entries)
120                 return NULL;
121
122         xs = READ_ONCE(m->xsk_map[key]);
123         return xs;
124 }
125
126 int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
127                        struct xdp_sock *xs)
128 {
129         struct xsk_map *m = container_of(map, struct xsk_map, map);
130         struct list_head *flush_list = this_cpu_ptr(m->flush_list);
131         int err;
132
133         err = xsk_rcv(xs, xdp);
134         if (err)
135                 return err;
136
137         if (!xs->flush_node.prev)
138                 list_add(&xs->flush_node, flush_list);
139
140         return 0;
141 }
142
143 void __xsk_map_flush(struct bpf_map *map)
144 {
145         struct xsk_map *m = container_of(map, struct xsk_map, map);
146         struct list_head *flush_list = this_cpu_ptr(m->flush_list);
147         struct xdp_sock *xs, *tmp;
148
149         list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
150                 xsk_flush(xs);
151                 __list_del(xs->flush_node.prev, xs->flush_node.next);
152                 xs->flush_node.prev = NULL;
153         }
154 }
155
156 static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
157 {
158         return ERR_PTR(-EOPNOTSUPP);
159 }
160
161 static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
162                                u64 map_flags)
163 {
164         struct xsk_map *m = container_of(map, struct xsk_map, map);
165         u32 i = *(u32 *)key, fd = *(u32 *)value;
166         struct xdp_sock *xs, *old_xs;
167         struct socket *sock;
168         int err;
169
170         if (unlikely(map_flags > BPF_EXIST))
171                 return -EINVAL;
172         if (unlikely(i >= m->map.max_entries))
173                 return -E2BIG;
174         if (unlikely(map_flags == BPF_NOEXIST))
175                 return -EEXIST;
176
177         sock = sockfd_lookup(fd, &err);
178         if (!sock)
179                 return err;
180
181         if (sock->sk->sk_family != PF_XDP) {
182                 sockfd_put(sock);
183                 return -EOPNOTSUPP;
184         }
185
186         xs = (struct xdp_sock *)sock->sk;
187
188         if (!xsk_is_setup_for_bpf_map(xs)) {
189                 sockfd_put(sock);
190                 return -EOPNOTSUPP;
191         }
192
193         sock_hold(sock->sk);
194
195         old_xs = xchg(&m->xsk_map[i], xs);
196         if (old_xs)
197                 sock_put((struct sock *)old_xs);
198
199         sockfd_put(sock);
200         return 0;
201 }
202
203 static int xsk_map_delete_elem(struct bpf_map *map, void *key)
204 {
205         struct xsk_map *m = container_of(map, struct xsk_map, map);
206         struct xdp_sock *old_xs;
207         int k = *(u32 *)key;
208
209         if (k >= map->max_entries)
210                 return -EINVAL;
211
212         old_xs = xchg(&m->xsk_map[k], NULL);
213         if (old_xs)
214                 sock_put((struct sock *)old_xs);
215
216         return 0;
217 }
218
219 const struct bpf_map_ops xsk_map_ops = {
220         .map_alloc = xsk_map_alloc,
221         .map_free = xsk_map_free,
222         .map_get_next_key = xsk_map_get_next_key,
223         .map_lookup_elem = xsk_map_lookup_elem,
224         .map_update_elem = xsk_map_update_elem,
225         .map_delete_elem = xsk_map_delete_elem,
226         .map_check_btf = map_check_no_btf,
227 };