Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: Make locking in sock_bindtoindex optional

The sock_bindtoindex intended for kernel wide usage however
it will lock the socket regardless of the context. This modification
relax this behavior optionally: locking the socket will be optional
by calling the sock_bindtoindex with lock_sk = true.

The modification applied to all users of the sock_bindtoindex.

Signed-off-by: Ferenc Fejes <fejes@inf.elte.hu>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/bee6355da40d9e991b2f2d12b67d55ebb5f5b207.1590871065.git.fejes@inf.elte.hu

authored by

Ferenc Fejes and committed by
Alexei Starovoitov
8ea204c2 bb2359f4

+9 -7
+1 -1
include/net/sock.h
··· 2690 2690 2691 2691 void sock_def_readable(struct sock *sk); 2692 2692 2693 - int sock_bindtoindex(struct sock *sk, int ifindex); 2693 + int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk); 2694 2694 void sock_enable_timestamps(struct sock *sk); 2695 2695 void sock_no_linger(struct sock *sk); 2696 2696 void sock_set_keepalive(struct sock *sk);
+6 -4
net/core/sock.c
··· 594 594 return ret; 595 595 } 596 596 597 - int sock_bindtoindex(struct sock *sk, int ifindex) 597 + int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk) 598 598 { 599 599 int ret; 600 600 601 - lock_sock(sk); 601 + if (lock_sk) 602 + lock_sock(sk); 602 603 ret = sock_bindtoindex_locked(sk, ifindex); 603 - release_sock(sk); 604 + if (lock_sk) 605 + release_sock(sk); 604 606 605 607 return ret; 606 608 } ··· 648 646 goto out; 649 647 } 650 648 651 - return sock_bindtoindex(sk, index); 649 + return sock_bindtoindex(sk, index, true); 652 650 out: 653 651 #endif 654 652
+1 -1
net/ipv4/udp_tunnel.c
··· 22 22 goto error; 23 23 24 24 if (cfg->bind_ifindex) { 25 - err = sock_bindtoindex(sock->sk, cfg->bind_ifindex); 25 + err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true); 26 26 if (err < 0) 27 27 goto error; 28 28 }
+1 -1
net/ipv6/ip6_udp_tunnel.c
··· 30 30 goto error; 31 31 } 32 32 if (cfg->bind_ifindex) { 33 - err = sock_bindtoindex(sock->sk, cfg->bind_ifindex); 33 + err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true); 34 34 if (err < 0) 35 35 goto error; 36 36 }