Skip to content

Commit 8a35fc7

Browse files
q2venSasha Levin
authored andcommitted
af_unix: Annotate data-race of sk->sk_state in unix_stream_connect().
[ Upstream commit a9bf9c7 ] As small optimisation, unix_stream_connect() prefetches the client's sk->sk_state without unix_state_lock() and checks if it's TCP_CLOSE. Later, sk->sk_state is checked again under unix_state_lock(). Let's use READ_ONCE() for the first check and TCP_CLOSE directly for the second check. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 581264e commit 8a35fc7

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

net/unix/af_unix.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
14691469
struct sk_buff *skb = NULL;
14701470
long timeo;
14711471
int err;
1472-
int st;
14731472

14741473
err = unix_validate_addr(sunaddr, addr_len);
14751474
if (err)
@@ -1553,9 +1552,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
15531552
15541553
Well, and we have to recheck the state after socket locked.
15551554
*/
1556-
st = sk->sk_state;
1557-
1558-
switch (st) {
1555+
switch (READ_ONCE(sk->sk_state)) {
15591556
case TCP_CLOSE:
15601557
/* This is ok... continue with connect */
15611558
break;
@@ -1570,7 +1567,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
15701567

15711568
unix_state_lock_nested(sk, U_LOCK_SECOND);
15721569

1573-
if (sk->sk_state != st) {
1570+
if (sk->sk_state != TCP_CLOSE) {
15741571
unix_state_unlock(sk);
15751572
unix_state_unlock(other);
15761573
sock_put(other);

0 commit comments

Comments
 (0)