Similar to the earlier patch that avoids sock_setsockopt() from taking sk lock when called from bpf. This patch changes do_tcp_setsockopt() to use the {lock,release}_sock_sockopt(). This patch also changes do_tcp_setsockopt() to check optval.is_bpf when passing the cap_net_admin arg to tcp_set_congestion_control(..., cap_net_admin). It is the same as how bpf_setsockopt(TCP_CONGESTION) is calling tcp_set_congestion_control() now. Signed-off-by: Martin KaFai Lau <kafai@xxxxxx> --- net/ipv4/tcp.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ba2bdc811374..7f8d81befa8e 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -3462,11 +3462,12 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, return -EFAULT; name[val] = 0; - lock_sock(sk); - err = tcp_set_congestion_control(sk, name, true, - ns_capable(sock_net(sk)->user_ns, - CAP_NET_ADMIN)); - release_sock(sk); + lock_sock_sockopt(sk, optval); + err = tcp_set_congestion_control(sk, name, !optval.is_bpf, + optval.is_bpf ? + true : ns_capable(sock_net(sk)->user_ns, + CAP_NET_ADMIN)); + release_sock_sockopt(sk, optval); return err; } case TCP_ULP: { @@ -3482,9 +3483,9 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, return -EFAULT; name[val] = 0; - lock_sock(sk); + lock_sock_sockopt(sk, optval); err = tcp_set_ulp(sk, name); - release_sock(sk); + release_sock_sockopt(sk, optval); return err; } case TCP_FASTOPEN_KEY: { @@ -3517,7 +3518,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, if (copy_from_sockptr(&val, optval, sizeof(val))) return -EFAULT; - lock_sock(sk); + lock_sock_sockopt(sk, optval); switch (optname) { case TCP_MAXSEG: @@ -3739,7 +3740,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, break; } - release_sock(sk); + release_sock_sockopt(sk, optval); return err; } -- 2.30.2