Skip to contentSkip to conversation

RDMA/siw: Fix connection failure handling

PATCH Applied Bernard Metzler 3 participants 5 messages Sep 5, 2023 – Sep 11, 2023

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

PATCH v2 1/1 RDMA/siw: Fix connection failure handling

In case immediate MPA request processing fails, the newly created endpoint unlinks the listening endpoint and is ready to be dropped. This special case was not handled correctly by the code handling the later TCP socket close, causing a NULL dereference crash in siw_cm_work_handler() when dereferencing a NULL listener. We now also cancel the useless MPA timeout, if immediate MPA request processing fails.

This patch furthermore simplifies MPA processing in general:
Scheduling a useless TCP socket read in sk_data_ready() upcall is now surpressed, if the socket is already moved out of TCP_ESTABLISHED state.

Fixes 6c52fdc244b5 ("rdma/siw: connection management")

---
ChangeLog v1->v2:
- Move debug message to now conditional listener drop

---
 drivers/infiniband/sw/siw/siw_cm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/infiniband/sw/siw/siw_cm.c +12 -4
index a2605178f4ed..43e776073f49 100644
@@ -976,6 +976,7 @@ static void siw_accept_newconn(struct siw_cep *cep)
 			siw_cep_put(cep);
 			new_cep->listen_cep = NULL;
 			if (rv) {
+				siw_cancel_mpatimer(new_cep);
 				siw_cep_set_free(new_cep);
 				goto error;
 			}
@@ -1100,9 +1101,12 @@ static void siw_cm_work_handler(struct work_struct *w)
 				/*
 				 * Socket close before MPA request received.
 				 */
-				siw_dbg_cep(cep, "no mpareq: drop listener\n");
-				siw_cep_put(cep->listen_cep);
-				cep->listen_cep = NULL;
+				if (cep->listen_cep) {
+					siw_dbg_cep(cep,
+						"no mpareq: drop listener\n");
+					siw_cep_put(cep->listen_cep);
+					cep->listen_cep = NULL;
+				}
 			}
 		}
 		release_cep = 1;
@@ -1227,7 +1231,11 @@ static void siw_cm_llp_data_ready(struct sock *sk)
 	if (!cep)
 		goto out;
 
-	siw_dbg_cep(cep, "state: %d\n", cep->state);
+	siw_dbg_cep(cep, "cep state: %d, socket state %d\n",
+		    cep->state, sk->sk_state);
+
+	if (sk->sk_state != TCP_ESTABLISHED)
+		goto out;
 
 	switch (cep->state) {
 	case SIW_EPSTATE_RDMA_MODE:
Signature · 1 line
2.38.1
#1 lore raw

Hi,

Quoted 28 lines · Bernard Metzler

In case immediate MPA request processing fails, the newly created endpoint unlinks the listening endpoint and is ready to be dropped. This special case was not handled correctly by the code handling the later TCP socket close, causing a NULL dereference crash in siw_cm_work_handler() when dereferencing a NULL listener. We now also cancel the useless MPA timeout, if immediate MPA request processing fails.

This patch furthermore simplifies MPA processing in general:
Scheduling a useless TCP socket read in sk_data_ready() upcall is now surpressed, if the socket is already moved out of TCP_ESTABLISHED state.

Fixes 6c52fdc244b5 ("rdma/siw: connection management")

---
ChangeLog v1->v2:
- Move debug message to now conditional listener drop

---
  drivers/infiniband/sw/siw/siw_cm.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
drivers/infiniband/sw/siw/siw_cm.c +0 -0
index a2605178f4ed..43e776073f49 100644

drivers/infiniband/sw/siw/siw_cm.c static void siw_accept_newconn(struct siw_cep *cep) · line 979

Quoted 4 lines
drivers/infiniband/sw/siw/siw_cm.c +1 -0
@@ -976,6 +976,7 @@ static void siw_accept_newconn(struct siw_cep *cep)
  			siw_cep_put(cep);
  			new_cep->listen_cep = NULL;
  			if (rv) {
+				siw_cancel_mpatimer(new_cep);

One question, why siw_cm_work_handler set cep->mpa_timer to NULL instead of call siw_cancel_mpatimer like above? Could it be memory leak issue for cep->mpa_timer? Let's say mpa_timer is set to NULL before  call siw_cancel_mpatimer.

Quoted 20 lines
drivers/infiniband/sw/siw/siw_cm.c +6 -3
  				siw_cep_set_free(new_cep);
  				goto error;
  			}
@@ -1100,9 +1101,12 @@ static void siw_cm_work_handler(struct work_struct *w)
  				/*
  				 * Socket close before MPA request received.
  				 */
-				siw_dbg_cep(cep, "no mpareq: drop listener\n");
-				siw_cep_put(cep->listen_cep);
-				cep->listen_cep = NULL;
+				if (cep->listen_cep) {
+					siw_dbg_cep(cep,
+						"no mpareq: drop listener\n");
+					siw_cep_put(cep->listen_cep);
+					cep->listen_cep = NULL;
+				}
  			}
  		}
  		release_cep = 1;

drivers/infiniband/sw/siw/siw_cm.c static void siw_cm_llp_data_ready(struct sock *sk) · line 1239

Quoted 10 lines
drivers/infiniband/sw/siw/siw_cm.c +5 -1
@@ -1227,7 +1231,11 @@ static void siw_cm_llp_data_ready(struct sock *sk)
  	if (!cep)
  		goto out;
 
-	siw_dbg_cep(cep, "state: %d\n", cep->state);
+	siw_dbg_cep(cep, "cep state: %d, socket state %d\n",
+		    cep->state, sk->sk_state);
+
+	if (sk->sk_state != TCP_ESTABLISHED)
+		goto out;
 

Maybe split above to another patch makes more sense, just my $0.02.

Thanks,
Guoqing

#2 lore raw
Quoted 38 lines

-----Original Message-----
From: Guoqing Jiang <[email protected]>
Sent: Friday, 8 September 2023 03:35
To: Bernard Metzler <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v2] RDMA/siw: Fix connection failure handling

Hi,

Quoted 16 lines · Bernard Metzler

In case immediate MPA request processing fails, the newly created endpoint unlinks the listening endpoint and is ready to be dropped. This special case was not handled correctly by the code handling the later TCP socket close, causing a NULL dereference crash in siw_cm_work_handler() when dereferencing a NULL listener. We now also cancel the useless MPA timeout, if immediate MPA request processing fails.

This patch furthermore simplifies MPA processing in general:
Scheduling a useless TCP socket read in sk_data_ready() upcall is now surpressed, if the socket is already moved out of TCP_ESTABLISHED state.

Fixes 6c52fdc244b5 ("rdma/siw: connection management")

Review

Quoted 8 lines

---
ChangeLog v1->v2:
- Move debug message to now conditional listener drop

---
  drivers/infiniband/sw/siw/siw_cm.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/infiniband/sw/siw/siw_cm.c

Quoted 13 lines

drivers/infiniband/sw/siw/siw_cm.c static void siw_accept_newconn(struct siw_cep *cep) · line 979

Quoted 8 lines

index a2605178f4ed..43e776073f49 100644

drivers/infiniband/sw/siw/siw_cm.c +1 -0
@@ -976,6 +976,7 @@ static void siw_accept_newconn(struct siw_cep *cep)
  			siw_cep_put(cep);
  			new_cep->listen_cep = NULL;
  			if (rv) {
+				siw_cancel_mpatimer(new_cep);

One question, why siw_cm_work_handler set cep->mpa_timer to NULL instead of call
siw_cancel_mpatimer like above? Could it be memory leak issue for cep->mpa_timer?

Thanks for reviewing.

Above in the proposed patch, we cancel a pending
MPA timer.

You are referring to siw_cm.c:1115

        case SIW_CM_WORK_MPATIMEOUT:
                cep->mpa_timer = NULL;

We explicitly set it NULL here since we are handling the timeout, so the MPA timer has fired and we cannot cancel it anymore. At the end of siw_cm_work_handler(), any work, including the MPA timeout, gets recycled to the cep's list of free work items via siw_put_work(). This list is recycled only at the end of the cep's lifetime.

Quoted 26 lines

Let's say mpa_timer is set to NULL before  call siw_cancel_mpatimer.

drivers/infiniband/sw/siw/siw_cm.c static void siw_cm_work_handler(struct work_struct

Quoted 4 lines
drivers/infiniband/sw/siw/siw_cm.c +0 -0
  				siw_cep_set_free(new_cep);
  				goto error;
  			}
@@ -1100,9 +1101,12 @@ static void siw_cm_work_handler(struct work_struct

*w)

Quoted 19 lines
drivers/infiniband/sw/siw/siw_cm.c +6 -3
  				/*
  				 * Socket close before MPA request received.
  				 */
-				siw_dbg_cep(cep, "no mpareq: drop listener\n");
-				siw_cep_put(cep->listen_cep);
-				cep->listen_cep = NULL;
+				if (cep->listen_cep) {
+					siw_dbg_cep(cep,
+						"no mpareq: drop listener\n");
+					siw_cep_put(cep->listen_cep);
+					cep->listen_cep = NULL;
+				}
  			}
  		}
  		release_cep = 1;
@@ -1227,7 +1231,11 @@ static void siw_cm_llp_data_ready(struct sock *sk)
  	if (!cep)
  		goto out;
 
Quoted 10 lines

drivers/infiniband/sw/siw/siw_cm.c static void siw_cm_llp_data_ready(struct sock *sk) · line 1239

Quoted 7 lines
drivers/infiniband/sw/siw/siw_cm.c +5 -1
-	siw_dbg_cep(cep, "state: %d\n", cep->state);
+	siw_dbg_cep(cep, "cep state: %d, socket state %d\n",
+		    cep->state, sk->sk_state);
+
+	if (sk->sk_state != TCP_ESTABLISHED)
+		goto out;
 

Maybe split above to another patch makes more sense, just my $0.02.

I preferred to have it in one patch, since it was all triggered by your patch b056327bee09
'RDMA/siw: Balance the reference of cep->kref in the error path'

Alarmed by that, I re-tested many error cases including a failing MPA request processing, which triggered this NULL bug. It also showed me this useless extra read event processing when the TCP socket is closing while the endpoint is still in MPA handshake mode.

But you are right, it is not necessarily related. I can split it.

Quoted 2 lines

Thanks,
Guoqing

#3 lore raw
Quoted 58 lines · Bernard Metzler
Quoted 36 lines

-----Original Message-----
From: Guoqing Jiang <[email protected]>
Sent: Friday, 8 September 2023 03:35
To: Bernard Metzler <[email protected]>; [email protected]
Cc: [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v2] RDMA/siw: Fix connection failure handling

Hi,

Quoted 16 lines · Bernard Metzler

In case immediate MPA request processing fails, the newly created endpoint unlinks the listening endpoint and is ready to be dropped. This special case was not handled correctly by the code handling the later TCP socket close, causing a NULL dereference crash in siw_cm_work_handler() when dereferencing a NULL listener. We now also cancel the useless MPA timeout, if immediate MPA request processing fails.

This patch furthermore simplifies MPA processing in general:
Scheduling a useless TCP socket read in sk_data_ready() upcall is now surpressed, if the socket is already moved out of TCP_ESTABLISHED state.

Fixes 6c52fdc244b5 ("rdma/siw: connection management")

Review

Quoted 8 lines

---
ChangeLog v1->v2:
- Move debug message to now conditional listener drop

---
   drivers/infiniband/sw/siw/siw_cm.c | 16 ++++++++++++----
   1 file changed, 12 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/infiniband/sw/siw/siw_cm.c

Quoted 12 lines

drivers/infiniband/sw/siw/siw_cm.c static void siw_accept_newconn(struct siw_cep *cep) · line 979

Quoted 8 lines

index a2605178f4ed..43e776073f49 100644

drivers/infiniband/sw/siw/siw_cm.c +1 -0
@@ -976,6 +976,7 @@ static void siw_accept_newconn(struct siw_cep *cep)
   			siw_cep_put(cep);
   			new_cep->listen_cep = NULL;
   			if (rv) {
+				siw_cancel_mpatimer(new_cep);

One question, why siw_cm_work_handler set cep->mpa_timer to NULL instead of call
siw_cancel_mpatimer like above? Could it be memory leak issue for cep->mpa_timer?

Thanks for reviewing.

Above in the proposed patch, we cancel a pending
MPA timer.

You are referring to siw_cm.c:1115

         case SIW_CM_WORK_MPATIMEOUT:
                 cep->mpa_timer = NULL;
Quoted 7 lines

We explicitly set it NULL here since we are handling the timeout, so the MPA timer has fired and we cannot cancel it anymore. At the end of siw_cm_work_handler(), any work, including the MPA timeout, gets recycled to the cep's list of free work items via siw_put_work(). This list is recycled only at the end of the cep's lifetime.

Got it, and thanks for detailed explanation!

Quoted 35 lines
Quoted 26 lines

Let's say mpa_timer is set to NULL before  call siw_cancel_mpatimer.

drivers/infiniband/sw/siw/siw_cm.c static void siw_cm_work_handler(struct work_struct

Quoted 4 lines
drivers/infiniband/sw/siw/siw_cm.c +0 -0
   				siw_cep_set_free(new_cep);
   				goto error;
   			}
@@ -1100,9 +1101,12 @@ static void siw_cm_work_handler(struct work_struct

*w)

Quoted 19 lines
drivers/infiniband/sw/siw/siw_cm.c +6 -3
   				/*
   				 * Socket close before MPA request received.
   				 */
-				siw_dbg_cep(cep, "no mpareq: drop listener\n");
-				siw_cep_put(cep->listen_cep);
-				cep->listen_cep = NULL;
+				if (cep->listen_cep) {
+					siw_dbg_cep(cep,
+						"no mpareq: drop listener\n");
+					siw_cep_put(cep->listen_cep);
+					cep->listen_cep = NULL;
+				}
   			}
   		}
   		release_cep = 1;
@@ -1227,7 +1231,11 @@ static void siw_cm_llp_data_ready(struct sock *sk)
   	if (!cep)
   		goto out;
 
Quoted 9 lines

drivers/infiniband/sw/siw/siw_cm.c static void siw_cm_llp_data_ready(struct sock *sk) · line 1239

Quoted 7 lines
drivers/infiniband/sw/siw/siw_cm.c +5 -1
-	siw_dbg_cep(cep, "state: %d\n", cep->state);
+	siw_dbg_cep(cep, "cep state: %d, socket state %d\n",
+		    cep->state, sk->sk_state);
+
+	if (sk->sk_state != TCP_ESTABLISHED)
+		goto out;
 

Maybe split above to another patch makes more sense, just my $0.02.

Quoted 8 lines

I preferred to have it in one patch, since it was all triggered by your patch b056327bee09
'RDMA/siw: Balance the reference of cep->kref in the error path'

Alarmed by that, I re-tested many error cases including a failing MPA request processing, which triggered this NULL bug. It also showed me this useless extra read event processing when the TCP socket is closing while the endpoint is still in MPA handshake mode.

Good to know the background :).

Quoted 1 line

But you are right, it is not necessarily related. I can split it.

With or without the split, Acked-by: Guoqing Jiang <[email protected]>

Thanks,
Guoqing

#4 lore raw
In reply to Bernard Metzler
Quoted 10 lines · Bernard Metzler

In case immediate MPA request processing fails, the newly created endpoint unlinks the listening endpoint and is ready to be dropped. This special case was not handled correctly by the code handling the later TCP socket close, causing a NULL dereference crash in siw_cm_work_handler() when dereferencing a NULL listener. We now also cancel the useless MPA timeout, if immediate MPA request processing fails.

[...]

Applied, thanks!

[1/1] RDMA/siw: Fix connection failure handling

      https://git.kernel.org/rdma/rdma/c/53a3f777049771

Best regards,

Signature · 1 line
Leon Romanovsky <[email protected]>