Skip to contentSkip to conversation

nvmet-tcp: fixup hang in nvmet_tcp_listen_data_ready()

PATCH Applied [email protected] 6 participants 7 messages Aug 18, 2025 – Jan 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-tcp: fixup hang in nvmet_tcp_listen_data_ready()

From: Hannes Reinecke <[email protected]>

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

---
 drivers/nvme/target/tcp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +4 -5
index 470bf37e5a63..2e9a3e698b70 100644
@@ -1992,14 +1992,13 @@ static void nvmet_tcp_listen_data_ready(struct sock *sk)
 
 	trace_sk_data_ready(sk);
 
+	if (sk->sk_state != TCP_LISTEN)
+		return;
+
 	read_lock_bh(&sk->sk_callback_lock);
 	port = sk->sk_user_data;
-	if (!port)
-		goto out;
-
-	if (sk->sk_state == TCP_LISTEN)
+	if (port)
 		queue_work(nvmet_wq, &port->accept_work);
-out:
 	read_unlock_bh(&sk->sk_callback_lock);
 }
 
Signature · 1 line
2.43.0
#1 lore raw

Hi Hannes

Thanks for the patch fix.
Could you also add the bug report link to this patch so that we can get more info, like the reproducer?

Quoted 41 lines · On Mon, Aug 18, 2025 at 5:38 PM <[email protected]> wrote:

From: Hannes Reinecke <[email protected]>

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

---
 drivers/nvme/target/tcp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +4 -6
index 470bf37e5a63..2e9a3e698b70 100644
@@ -1992,14 +1992,13 @@ static void nvmet_tcp_listen_data_ready(struct sock *sk)
 
        trace_sk_data_ready(sk);
 
+       if (sk->sk_state != TCP_LISTEN)
+               return;
+
        read_lock_bh(&sk->sk_callback_lock);
        port = sk->sk_user_data;
-       if (!port)
-               goto out;
-
-       if (sk->sk_state == TCP_LISTEN)
+       if (port)
                queue_work(nvmet_wq, &port->accept_work);
-out:
        read_unlock_bh(&sk->sk_callback_lock);
 }
 
--

2.43.0

Signature · 2 lines
Best Regards,
  Yi Zhang
#2 lore raw
In reply to [email protected]
Quoted 7 lines · hare

From: Hannes Reinecke <[email protected]>

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

Ping, I guess this patch has been forgotten

Maurizio

Quoted 30 lines
---
 drivers/nvme/target/tcp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +4 -5
index 470bf37e5a63..2e9a3e698b70 100644
@@ -1992,14 +1992,13 @@ static void nvmet_tcp_listen_data_ready(struct sock *sk)
 
 	trace_sk_data_ready(sk);
 
+	if (sk->sk_state != TCP_LISTEN)
+		return;
+
 	read_lock_bh(&sk->sk_callback_lock);
 	port = sk->sk_user_data;
-	if (!port)
-		goto out;
-
-	if (sk->sk_state == TCP_LISTEN)
+	if (port)
 		queue_work(nvmet_wq, &port->accept_work);
-out:
 	read_unlock_bh(&sk->sk_callback_lock);
 }
 
#3 lore raw
Quoted 4 lines · Maurizio Lombardi
Quoted 2 lines · hare

From: Hannes Reinecke <[email protected]>

Quoted 9 lines
Quoted 5 lines

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

Ping, I guess this patch has been forgotten

Here is the patch fixed bug report:
https://lore.kernel.org/linux-nvme/CAHj4cs-zu7eVB78yUpFjVe2UqMWFkLk8p+DaS3qj+uiGCXBAoA@mail.gmail.com/

Quoted 34 lines

Maurizio

Quoted 30 lines
---
 drivers/nvme/target/tcp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +4 -5
index 470bf37e5a63..2e9a3e698b70 100644
@@ -1992,14 +1992,13 @@ static void nvmet_tcp_listen_data_ready(struct sock *sk)
 
      trace_sk_data_ready(sk);
 
+     if (sk->sk_state != TCP_LISTEN)
+             return;
+
      read_lock_bh(&sk->sk_callback_lock);
      port = sk->sk_user_data;
-     if (!port)
-             goto out;
-
-     if (sk->sk_state == TCP_LISTEN)
+     if (port)
              queue_work(nvmet_wq, &port->accept_work);
-out:
      read_unlock_bh(&sk->sk_callback_lock);
 }
 
Signature · 2 lines
Best Regards,
  Yi Zhang
#4 lore raw
In reply to [email protected]

Sagi? Could you review this please?

Thanks,
Maurizio

po 18. 8. 2025 v 15:41 odesílatel <[email protected]> napsal:

Quoted 42 lines

From: Hannes Reinecke <[email protected]>

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

---
 drivers/nvme/target/tcp.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/nvme/target/tcp.c +4 -6
index 470bf37e5a63..2e9a3e698b70 100644
@@ -1992,14 +1992,13 @@ static void nvmet_tcp_listen_data_ready(struct sock *sk)
 
        trace_sk_data_ready(sk);
 
+       if (sk->sk_state != TCP_LISTEN)
+               return;
+
        read_lock_bh(&sk->sk_callback_lock);
        port = sk->sk_user_data;
-       if (!port)
-               goto out;
-
-       if (sk->sk_state == TCP_LISTEN)
+       if (port)
                queue_work(nvmet_wq, &port->accept_work);
-out:
        read_unlock_bh(&sk->sk_callback_lock);
 }
 
--

2.43.0

maintainer Applied
#6 lore raw
In reply to [email protected]
Quoted 7 lines · On Mon, Aug 18, 2025 at 11:32:45AM +0200, [email protected] wrote:

From: Hannes Reinecke <[email protected]>

When the socket is closed while in TCP_LISTEN a callback is run to flush all outstanding packets, which in turns calls nvmet_tcp_listen_data_ready() with the sk_callback_lock held. So we need to check if we are in TCP_LISTEN before attempting to get the sk_callback_lock() to avoid a deadlock.

Thanks, applied to nvme-6.19.