Skip to content

Commit b27401a

Browse files
tkhaidavem330
authored andcommitted
unix: Improve locking scheme in unix_show_fdinfo()
After switching to TCP_ESTABLISHED or TCP_LISTEN sk_state, alive SOCK_STREAM and SOCK_SEQPACKET sockets can't change it anymore (since commit 3ff8bff "unix: Fix race in SOCK_SEQPACKET's unix_dgram_sendmsg()"). Thus, we do not need to take lock here. Signed-off-by: Kirill Tkhai <tkhai@ya.ru> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 40ea3ee commit b27401a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

net/unix/af_unix.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -807,23 +807,23 @@ static int unix_count_nr_fds(struct sock *sk)
807807
static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
808808
{
809809
struct sock *sk = sock->sk;
810+
unsigned char s_state;
810811
struct unix_sock *u;
811-
int nr_fds;
812+
int nr_fds = 0;
812813

813814
if (sk) {
815+
s_state = READ_ONCE(sk->sk_state);
814816
u = unix_sk(sk);
815-
if (sock->type == SOCK_DGRAM) {
816-
nr_fds = atomic_read(&u->scm_stat.nr_fds);
817-
goto out_print;
818-
}
819817

820-
unix_state_lock(sk);
821-
if (sk->sk_state != TCP_LISTEN)
818+
/* SOCK_STREAM and SOCK_SEQPACKET sockets never change their
819+
* sk_state after switching to TCP_ESTABLISHED or TCP_LISTEN.
820+
* SOCK_DGRAM is ordinary. So, no lock is needed.
821+
*/
822+
if (sock->type == SOCK_DGRAM || s_state == TCP_ESTABLISHED)
822823
nr_fds = atomic_read(&u->scm_stat.nr_fds);
823-
else
824+
else if (s_state == TCP_LISTEN)
824825
nr_fds = unix_count_nr_fds(sk);
825-
unix_state_unlock(sk);
826-
out_print:
826+
827827
seq_printf(m, "scm_fds: %u\n", nr_fds);
828828
}
829829
}

0 commit comments

Comments
 (0)