History

bpf: net: Avoid copying sk_user_data of reuseport_array during sk_clone

It makes little sense for copying sk_user_data of reuseport_array during sk_clone_lock(). This patch reuses the SK_USER_DATA_NOCOPY bit introduced in commit f1ff5ce2cd5e ("net, sk_msg: Clear sk_user_data pointer on clone if tagged"). It is used to mark the sk_user_data is not supposed to be copied to its clone. Although the cloned sk's sk_user_data will not be used/freed in bpf_sk_reuseport_detach(), this change can still allow the cloned sk's sk_user_data to be used by some other means. Freeing the reuseport_array's sk_user_data does not require a rcu grace period. Thus, the existing rcu_assign_sk_user_data_nocopy() is not used.

Martin KaFai Lau committed 6 years ago

Commit f3dda7a1 parent 365f9ae1 file · +9 −4Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/reuseport_array.c+9−4
@@ -20,11 +20,14 @@ static struct reuseport_array *reuseport_array(struct bpf_map *map) · 19 unmodified lines
2020 /* The caller must hold the reuseport_lock */
2121 void bpf_sk_reuseport_detach(struct sock *sk)
2222 {
23− struct sock __rcu **socks;
23+ uintptr_t sk_user_data;
2424
2525 write_lock_bh(&sk->sk_callback_lock);
26− socks = sk->sk_user_data;
27− if (socks) {
26+ sk_user_data = (uintptr_t)sk->sk_user_data;
27+ if (sk_user_data) {
28+ struct sock __rcu **socks;
29+
30+ socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
2831 WRITE_ONCE(sk->sk_user_data, NULL);
2932 /*
3033 * Do not move this NULL assignment outside of
@@ -252,6 +255,7 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, · 221 unmodified lines
252255 struct sock *free_osk = NULL, *osk, *nsk;
253256 struct sock_reuseport *reuse;
254257 u32 index = *(u32 *)key;
258+ uintptr_t sk_user_data;
255259 struct socket *socket;
256260 int err, fd;
257261
@@ -305,7 +309,8 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, · 47 unmodified lines
305309 if (err)
306310 goto put_file_unlock;
307311
308− WRITE_ONCE(nsk->sk_user_data, &array->ptrs[index]);
312+ sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY;
313+ WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
309314 rcu_assign_pointer(array->ptrs[index], nsk);
310315 free_osk = osk;
311316 err = 0;