Skip to content

Commit 942238f

Browse files
q2venPaolo Abeni
authored andcommitted
af_unix: Annodate data-races around sk->sk_state for writers.
sk->sk_state is changed under unix_state_lock(), but it's read locklessly in many places. This patch adds WRITE_ONCE() on the writer side. We will add READ_ONCE() to the lockless readers in the following patches. Fixes: 83301b5 ("af_unix: Set TCP_ESTABLISHED for datagram sockets too") Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 26bfb8b commit 942238f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

net/unix/af_unix.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
616616
u->path.dentry = NULL;
617617
u->path.mnt = NULL;
618618
state = sk->sk_state;
619-
sk->sk_state = TCP_CLOSE;
619+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
620620

621621
skpair = unix_peer(sk);
622622
unix_peer(sk) = NULL;
@@ -738,7 +738,8 @@ static int unix_listen(struct socket *sock, int backlog)
738738
if (backlog > sk->sk_max_ack_backlog)
739739
wake_up_interruptible_all(&u->peer_wait);
740740
sk->sk_max_ack_backlog = backlog;
741-
sk->sk_state = TCP_LISTEN;
741+
WRITE_ONCE(sk->sk_state, TCP_LISTEN);
742+
742743
/* set credentials so connect can copy them */
743744
init_peercred(sk);
744745
err = 0;
@@ -1401,7 +1402,8 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
14011402
if (err)
14021403
goto out_unlock;
14031404

1404-
sk->sk_state = other->sk_state = TCP_ESTABLISHED;
1405+
WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
1406+
WRITE_ONCE(other->sk_state, TCP_ESTABLISHED);
14051407
} else {
14061408
/*
14071409
* 1003.1g breaking connected state with AF_UNSPEC
@@ -1418,7 +1420,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
14181420

14191421
unix_peer(sk) = other;
14201422
if (!other)
1421-
sk->sk_state = TCP_CLOSE;
1423+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
14221424
unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
14231425

14241426
unix_state_double_unlock(sk, other);
@@ -1639,7 +1641,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
16391641
copy_peercred(sk, other);
16401642

16411643
sock->state = SS_CONNECTED;
1642-
sk->sk_state = TCP_ESTABLISHED;
1644+
WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
16431645
sock_hold(newsk);
16441646

16451647
smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
@@ -2050,7 +2052,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
20502052
unix_peer(sk) = NULL;
20512053
unix_dgram_peer_wake_disconnect_wakeup(sk, other);
20522054

2053-
sk->sk_state = TCP_CLOSE;
2055+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
20542056
unix_state_unlock(sk);
20552057

20562058
unix_dgram_disconnected(sk, other);

0 commit comments

Comments
 (0)