Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / kernel / bpf / reuseport_array.c
blob4838922f723ddab66879e17aa4c341dc6fe2d406
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018 Facebook
4 */
5 #include <linux/bpf.h>
6 #include <linux/err.h>
7 #include <linux/sock_diag.h>
8 #include <net/sock_reuseport.h>
10 struct reuseport_array {
11 struct bpf_map map;
12 struct sock __rcu *ptrs[];
15 static struct reuseport_array *reuseport_array(struct bpf_map *map)
17 return (struct reuseport_array *)map;
20 /* The caller must hold the reuseport_lock */
21 void bpf_sk_reuseport_detach(struct sock *sk)
23 uintptr_t sk_user_data;
25 write_lock_bh(&sk->sk_callback_lock);
26 sk_user_data = (uintptr_t)sk->sk_user_data;
27 if (sk_user_data & SK_USER_DATA_BPF) {
28 struct sock __rcu **socks;
30 socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
31 WRITE_ONCE(sk->sk_user_data, NULL);
33 * Do not move this NULL assignment outside of
34 * sk->sk_callback_lock because there is
35 * a race with reuseport_array_free()
36 * which does not hold the reuseport_lock.
38 RCU_INIT_POINTER(*socks, NULL);
40 write_unlock_bh(&sk->sk_callback_lock);
43 static int reuseport_array_alloc_check(union bpf_attr *attr)
45 if (attr->value_size != sizeof(u32) &&
46 attr->value_size != sizeof(u64))
47 return -EINVAL;
49 return array_map_alloc_check(attr);
52 static void *reuseport_array_lookup_elem(struct bpf_map *map, void *key)
54 struct reuseport_array *array = reuseport_array(map);
55 u32 index = *(u32 *)key;
57 if (unlikely(index >= array->map.max_entries))
58 return NULL;
60 return rcu_dereference(array->ptrs[index]);
63 /* Called from syscall only */
64 static int reuseport_array_delete_elem(struct bpf_map *map, void *key)
66 struct reuseport_array *array = reuseport_array(map);
67 u32 index = *(u32 *)key;
68 struct sock *sk;
69 int err;
71 if (index >= map->max_entries)
72 return -E2BIG;
74 if (!rcu_access_pointer(array->ptrs[index]))
75 return -ENOENT;
77 spin_lock_bh(&reuseport_lock);
79 sk = rcu_dereference_protected(array->ptrs[index],
80 lockdep_is_held(&reuseport_lock));
81 if (sk) {
82 write_lock_bh(&sk->sk_callback_lock);
83 WRITE_ONCE(sk->sk_user_data, NULL);
84 RCU_INIT_POINTER(array->ptrs[index], NULL);
85 write_unlock_bh(&sk->sk_callback_lock);
86 err = 0;
87 } else {
88 err = -ENOENT;
91 spin_unlock_bh(&reuseport_lock);
93 return err;
96 static void reuseport_array_free(struct bpf_map *map)
98 struct reuseport_array *array = reuseport_array(map);
99 struct sock *sk;
100 u32 i;
103 * ops->map_*_elem() will not be able to access this
104 * array now. Hence, this function only races with
105 * bpf_sk_reuseport_detach() which was triggerred by
106 * close() or disconnect().
108 * This function and bpf_sk_reuseport_detach() are
109 * both removing sk from "array". Who removes it
110 * first does not matter.
112 * The only concern here is bpf_sk_reuseport_detach()
113 * may access "array" which is being freed here.
114 * bpf_sk_reuseport_detach() access this "array"
115 * through sk->sk_user_data _and_ with sk->sk_callback_lock
116 * held which is enough because this "array" is not freed
117 * until all sk->sk_user_data has stopped referencing this "array".
119 * Hence, due to the above, taking "reuseport_lock" is not
120 * needed here.
124 * Since reuseport_lock is not taken, sk is accessed under
125 * rcu_read_lock()
127 rcu_read_lock();
128 for (i = 0; i < map->max_entries; i++) {
129 sk = rcu_dereference(array->ptrs[i]);
130 if (sk) {
131 write_lock_bh(&sk->sk_callback_lock);
133 * No need for WRITE_ONCE(). At this point,
134 * no one is reading it without taking the
135 * sk->sk_callback_lock.
137 sk->sk_user_data = NULL;
138 write_unlock_bh(&sk->sk_callback_lock);
139 RCU_INIT_POINTER(array->ptrs[i], NULL);
142 rcu_read_unlock();
145 * Once reaching here, all sk->sk_user_data is not
146 * referenceing this "array". "array" can be freed now.
148 bpf_map_area_free(array);
151 static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
153 int numa_node = bpf_map_attr_numa_node(attr);
154 struct reuseport_array *array;
155 u64 array_size;
157 if (!bpf_capable())
158 return ERR_PTR(-EPERM);
160 array_size = sizeof(*array);
161 array_size += (u64)attr->max_entries * sizeof(struct sock *);
163 /* allocate all map elements and zero-initialize them */
164 array = bpf_map_area_alloc(array_size, numa_node);
165 if (!array)
166 return ERR_PTR(-ENOMEM);
168 /* copy mandatory map attributes */
169 bpf_map_init_from_attr(&array->map, attr);
171 return &array->map;
174 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
175 void *value)
177 struct sock *sk;
178 int err;
180 if (map->value_size != sizeof(u64))
181 return -ENOSPC;
183 rcu_read_lock();
184 sk = reuseport_array_lookup_elem(map, key);
185 if (sk) {
186 *(u64 *)value = __sock_gen_cookie(sk);
187 err = 0;
188 } else {
189 err = -ENOENT;
191 rcu_read_unlock();
193 return err;
196 static int
197 reuseport_array_update_check(const struct reuseport_array *array,
198 const struct sock *nsk,
199 const struct sock *osk,
200 const struct sock_reuseport *nsk_reuse,
201 u32 map_flags)
203 if (osk && map_flags == BPF_NOEXIST)
204 return -EEXIST;
206 if (!osk && map_flags == BPF_EXIST)
207 return -ENOENT;
209 if (nsk->sk_protocol != IPPROTO_UDP && nsk->sk_protocol != IPPROTO_TCP)
210 return -ENOTSUPP;
212 if (nsk->sk_family != AF_INET && nsk->sk_family != AF_INET6)
213 return -ENOTSUPP;
215 if (nsk->sk_type != SOCK_STREAM && nsk->sk_type != SOCK_DGRAM)
216 return -ENOTSUPP;
219 * sk must be hashed (i.e. listening in the TCP case or binded
220 * in the UDP case) and
221 * it must also be a SO_REUSEPORT sk (i.e. reuse cannot be NULL).
223 * Also, sk will be used in bpf helper that is protected by
224 * rcu_read_lock().
226 if (!sock_flag(nsk, SOCK_RCU_FREE) || !sk_hashed(nsk) || !nsk_reuse)
227 return -EINVAL;
229 /* READ_ONCE because the sk->sk_callback_lock may not be held here */
230 if (READ_ONCE(nsk->sk_user_data))
231 return -EBUSY;
233 return 0;
237 * Called from syscall only.
238 * The "nsk" in the fd refcnt.
239 * The "osk" and "reuse" are protected by reuseport_lock.
241 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
242 void *value, u64 map_flags)
244 struct reuseport_array *array = reuseport_array(map);
245 struct sock *free_osk = NULL, *osk, *nsk;
246 struct sock_reuseport *reuse;
247 u32 index = *(u32 *)key;
248 uintptr_t sk_user_data;
249 struct socket *socket;
250 int err, fd;
252 if (map_flags > BPF_EXIST)
253 return -EINVAL;
255 if (index >= map->max_entries)
256 return -E2BIG;
258 if (map->value_size == sizeof(u64)) {
259 u64 fd64 = *(u64 *)value;
261 if (fd64 > S32_MAX)
262 return -EINVAL;
263 fd = fd64;
264 } else {
265 fd = *(int *)value;
268 socket = sockfd_lookup(fd, &err);
269 if (!socket)
270 return err;
272 nsk = socket->sk;
273 if (!nsk) {
274 err = -EINVAL;
275 goto put_file;
278 /* Quick checks before taking reuseport_lock */
279 err = reuseport_array_update_check(array, nsk,
280 rcu_access_pointer(array->ptrs[index]),
281 rcu_access_pointer(nsk->sk_reuseport_cb),
282 map_flags);
283 if (err)
284 goto put_file;
286 spin_lock_bh(&reuseport_lock);
288 * Some of the checks only need reuseport_lock
289 * but it is done under sk_callback_lock also
290 * for simplicity reason.
292 write_lock_bh(&nsk->sk_callback_lock);
294 osk = rcu_dereference_protected(array->ptrs[index],
295 lockdep_is_held(&reuseport_lock));
296 reuse = rcu_dereference_protected(nsk->sk_reuseport_cb,
297 lockdep_is_held(&reuseport_lock));
298 err = reuseport_array_update_check(array, nsk, osk, reuse, map_flags);
299 if (err)
300 goto put_file_unlock;
302 sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY |
303 SK_USER_DATA_BPF;
304 WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
305 rcu_assign_pointer(array->ptrs[index], nsk);
306 free_osk = osk;
307 err = 0;
309 put_file_unlock:
310 write_unlock_bh(&nsk->sk_callback_lock);
312 if (free_osk) {
313 write_lock_bh(&free_osk->sk_callback_lock);
314 WRITE_ONCE(free_osk->sk_user_data, NULL);
315 write_unlock_bh(&free_osk->sk_callback_lock);
318 spin_unlock_bh(&reuseport_lock);
319 put_file:
320 fput(socket->file);
321 return err;
324 /* Called from syscall */
325 static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
326 void *next_key)
328 struct reuseport_array *array = reuseport_array(map);
329 u32 index = key ? *(u32 *)key : U32_MAX;
330 u32 *next = (u32 *)next_key;
332 if (index >= array->map.max_entries) {
333 *next = 0;
334 return 0;
337 if (index == array->map.max_entries - 1)
338 return -ENOENT;
340 *next = index + 1;
341 return 0;
344 static int reuseport_array_map_btf_id;
345 const struct bpf_map_ops reuseport_array_ops = {
346 .map_meta_equal = bpf_map_meta_equal,
347 .map_alloc_check = reuseport_array_alloc_check,
348 .map_alloc = reuseport_array_alloc,
349 .map_free = reuseport_array_free,
350 .map_lookup_elem = reuseport_array_lookup_elem,
351 .map_get_next_key = reuseport_array_get_next_key,
352 .map_delete_elem = reuseport_array_delete_elem,
353 .map_btf_name = "reuseport_array",
354 .map_btf_id = &reuseport_array_map_btf_id,