* [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo()
@ 2023-01-14 9:35 Kirill Tkhai
2023-01-14 9:59 ` Kuniyuki Iwashima
2023-01-16 11:30 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Kirill Tkhai @ 2023-01-14 9:35 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni, kuniyu, netdev, tkhai
After switching to TCP_ESTABLISHED or TCP_LISTEN sk_state, alive SOCK_STREAM
and SOCK_SEQPACKET sockets can't change it anymore (since commit 3ff8bff704f4
"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>
---
v2: Initialize "nr_fds = 0".
net/unix/af_unix.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index f0c2293f1d3b..009616fa0256 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -807,23 +807,23 @@ static int unix_count_nr_fds(struct sock *sk)
static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
{
struct sock *sk = sock->sk;
+ unsigned char s_state;
struct unix_sock *u;
- int nr_fds;
+ int nr_fds = 0;
if (sk) {
+ s_state = READ_ONCE(sk->sk_state);
u = unix_sk(sk);
- if (sock->type == SOCK_DGRAM) {
- nr_fds = atomic_read(&u->scm_stat.nr_fds);
- goto out_print;
- }
- unix_state_lock(sk);
- if (sk->sk_state != TCP_LISTEN)
+ /* SOCK_STREAM and SOCK_SEQPACKET sockets never change their
+ * sk_state after switching to TCP_ESTABLISHED or TCP_LISTEN.
+ * SOCK_DGRAM is ordinary. So, no lock is needed.
+ */
+ if (sock->type == SOCK_DGRAM || s_state == TCP_ESTABLISHED)
nr_fds = atomic_read(&u->scm_stat.nr_fds);
- else
+ else if (s_state == TCP_LISTEN)
nr_fds = unix_count_nr_fds(sk);
- unix_state_unlock(sk);
-out_print:
+
seq_printf(m, "scm_fds: %u\n", nr_fds);
}
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo()
2023-01-14 9:35 [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo() Kirill Tkhai
@ 2023-01-14 9:59 ` Kuniyuki Iwashima
2023-01-16 11:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Kuniyuki Iwashima @ 2023-01-14 9:59 UTC (permalink / raw)
To: tkhai; +Cc: davem, edumazet, kuba, kuniyu, netdev, pabeni
From: Kirill Tkhai <tkhai@ya.ru>
Date: Sat, 14 Jan 2023 12:35:02 +0300
> After switching to TCP_ESTABLISHED or TCP_LISTEN sk_state, alive SOCK_STREAM
> and SOCK_SEQPACKET sockets can't change it anymore (since commit 3ff8bff704f4
> "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>
> ---
> v2: Initialize "nr_fds = 0".
Yes, this is necessary because the new if-else does not cover
(SOCK_STREAM, TCP_CLOSE), and in such a case, nr_fds is
uninitialised val with the v1 patch.
This version looks good.
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
>
> net/unix/af_unix.c | 20 ++++++++++----------
> 1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index f0c2293f1d3b..009616fa0256 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -807,23 +807,23 @@ static int unix_count_nr_fds(struct sock *sk)
> static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
> {
> struct sock *sk = sock->sk;
> + unsigned char s_state;
> struct unix_sock *u;
> - int nr_fds;
> + int nr_fds = 0;
>
> if (sk) {
> + s_state = READ_ONCE(sk->sk_state);
> u = unix_sk(sk);
> - if (sock->type == SOCK_DGRAM) {
> - nr_fds = atomic_read(&u->scm_stat.nr_fds);
> - goto out_print;
> - }
>
> - unix_state_lock(sk);
> - if (sk->sk_state != TCP_LISTEN)
> + /* SOCK_STREAM and SOCK_SEQPACKET sockets never change their
> + * sk_state after switching to TCP_ESTABLISHED or TCP_LISTEN.
> + * SOCK_DGRAM is ordinary. So, no lock is needed.
> + */
> + if (sock->type == SOCK_DGRAM || s_state == TCP_ESTABLISHED)
> nr_fds = atomic_read(&u->scm_stat.nr_fds);
> - else
> + else if (s_state == TCP_LISTEN)
> nr_fds = unix_count_nr_fds(sk);
> - unix_state_unlock(sk);
> -out_print:
> +
> seq_printf(m, "scm_fds: %u\n", nr_fds);
> }
> }
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo()
2023-01-14 9:35 [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo() Kirill Tkhai
2023-01-14 9:59 ` Kuniyuki Iwashima
@ 2023-01-16 11:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-01-16 11:30 UTC (permalink / raw)
To: Kirill Tkhai; +Cc: davem, edumazet, kuba, pabeni, kuniyu, netdev
Hello:
This patch was applied to netdev/net-next.git (master)
by David S. Miller <davem@davemloft.net>:
On Sat, 14 Jan 2023 12:35:02 +0300 you wrote:
> After switching to TCP_ESTABLISHED or TCP_LISTEN sk_state, alive SOCK_STREAM
> and SOCK_SEQPACKET sockets can't change it anymore (since commit 3ff8bff704f4
> "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>
>
> [...]
Here is the summary with links:
- [net-next,v2] unix: Improve locking scheme in unix_show_fdinfo()
https://git.kernel.org/netdev/net-next/c/b27401a30ee4
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-01-16 11:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-14 9:35 [PATCH net-next v2] unix: Improve locking scheme in unix_show_fdinfo() Kirill Tkhai
2023-01-14 9:59 ` Kuniyuki Iwashima
2023-01-16 11:30 ` patchwork-bot+netdevbpf
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.