Skip to content

Commit 8ea204c

Browse files
SPYFFAlexei Starovoitov
authored andcommitted
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/bee6355da40d9e991b2f2d12b67d55ebb5f5b207.1590871065.git.fejes@inf.elte.hu
1 parent bb2359f commit 8ea204c

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

include/net/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
26902690

26912691
void sock_def_readable(struct sock *sk);
26922692

2693-
int sock_bindtoindex(struct sock *sk, int ifindex);
2693+
int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk);
26942694
void sock_enable_timestamps(struct sock *sk);
26952695
void sock_no_linger(struct sock *sk);
26962696
void sock_set_keepalive(struct sock *sk);

net/core/sock.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,15 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
594594
return ret;
595595
}
596596

597-
int sock_bindtoindex(struct sock *sk, int ifindex)
597+
int sock_bindtoindex(struct sock *sk, int ifindex, bool lock_sk)
598598
{
599599
int ret;
600600

601-
lock_sock(sk);
601+
if (lock_sk)
602+
lock_sock(sk);
602603
ret = sock_bindtoindex_locked(sk, ifindex);
603-
release_sock(sk);
604+
if (lock_sk)
605+
release_sock(sk);
604606

605607
return ret;
606608
}
@@ -646,7 +648,7 @@ static int sock_setbindtodevice(struct sock *sk, char __user *optval,
646648
goto out;
647649
}
648650

649-
return sock_bindtoindex(sk, index);
651+
return sock_bindtoindex(sk, index, true);
650652
out:
651653
#endif
652654

net/ipv4/udp_tunnel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
2222
goto error;
2323

2424
if (cfg->bind_ifindex) {
25-
err = sock_bindtoindex(sock->sk, cfg->bind_ifindex);
25+
err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true);
2626
if (err < 0)
2727
goto error;
2828
}

net/ipv6/ip6_udp_tunnel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
3030
goto error;
3131
}
3232
if (cfg->bind_ifindex) {
33-
err = sock_bindtoindex(sock->sk, cfg->bind_ifindex);
33+
err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true);
3434
if (err < 0)
3535
goto error;
3636
}

0 commit comments

Comments
 (0)