Skip to contentSkip to conversation

Fixes for clcsock shutdown behaviors

PATCH Superseded Tony Lu 2 participants 6 messages Nov 25, 2021

Subsystems (5)
lore
Share & tools
lore

Tony Lu

author reviewer
#0 lore raw

cover Fixes for clcsock shutdown behaviors

These patches fix issues when calling kernel_sock_shutdown(). To make the solution clear, I split it into two patches.

Patch 1 keeps the return code of smc_close_final() in smc_close_active(), which is more important than kernel_sock_shutdown().

Patch 2 doesn't call clcsock shutdown twice when applications call smc_shutdown(). It should be okay to call kernel_sock_shutdown() twice, I decide to avoid it for slightly speed up releasing socket.

Tony Lu (2):
net/smc: Keep smc_close_final rc during active close net/smc: Don't call clcsock shutdown twice when smc shutdown

 net/smc/af_smc.c    | 8 ++++++++
 net/smc/smc_close.c | 8 ++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
author reviewer
#1 lore raw

PATCH 1/2 net/smc: Keep smc_close_final rc during active close

When smc_close_final() returns error, the return code overwrites by kernel_sock_shutdown() in smc_close_active(). The return code of smc_close_final() is more important than kernel_sock_shutdown(), and it will pass to userspace directly.

Fix it by keeping both return codes, if smc_close_final() raises an error, return it or kernel_sock_shutdown()'s.

Fixes 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")
---
 net/smc/smc_close.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/smc/smc_close.c +6 -2
index 3715d2f5ad55..292e4d904ab6 100644
@@ -195,6 +195,7 @@ int smc_close_active(struct smc_sock *smc)
 	int old_state;
 	long timeout;
 	int rc = 0;
+	int rc1 = 0;
 
 	timeout = current->flags & PF_EXITING ?
 		  0 : sock_flag(sk, SOCK_LINGER) ?
@@ -232,8 +233,11 @@ int smc_close_active(struct smc_sock *smc)
 			/* actively shutdown clcsock before peer close it,
 			 * prevent peer from entering TIME_WAIT state.
 			 */
-			if (smc->clcsock && smc->clcsock->sk)
-				rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
+			if (smc->clcsock && smc->clcsock->sk) {
+				rc1 = kernel_sock_shutdown(smc->clcsock,
+							   SHUT_RDWR);
+				rc = rc ? rc : rc1;
+			}
 		} else {
 			/* peer event has changed the state */
 			goto again;
Signature · 1 line
2.32.0.3.g01195cf9f
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] net/smc: Keep smc_close_final rc during active close

Quoted 8 lines · Tony Lu

When smc_close_final() returns error, the return code overwrites by kernel_sock_shutdown() in smc_close_active(). The return code of smc_close_final() is more important than kernel_sock_shutdown(), and it will pass to userspace directly.

Fix it by keeping both return codes, if smc_close_final() raises an error, return it or kernel_sock_shutdown()'s.

Quoted 6 lines
Fixes 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")

---

author reviewer
#3 lore raw

PATCH 2/2 net/smc: Don't call clcsock shutdown twice when smc shutdown

In reply to Tony Lu

When applications call shutdown() with SHUT_RDWR in userspace, smc_close_active() calls kernel_sock_shutdown(), and it is called twice in smc_shutdown().

This fixes this by checking sk_state before do clcsock shutdown, and avoids missing the application's call of smc_shutdown().

Fixes 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")
---
 net/smc/af_smc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/smc/af_smc.c +8 -0
index 4b62c925a13e..7b04cb4d15f4 100644
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)
 	struct smc_sock *smc;
 	int rc = -EINVAL;
 	int rc1 = 0;
+	int old_state;
 
 	smc = smc_sk(sk);
 
@@ -2398,7 +2399,12 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	switch (how) {
 	case SHUT_RDWR:		/* shutdown in both directions */
+		old_state = sk->sk_state;
 		rc = smc_close_active(smc);
+		if (old_state == SMC_ACTIVE &&
+		    sk->sk_state == SMC_PEERCLOSEWAIT1)
+			goto out_no_shutdown;
+
 		break;
 	case SHUT_WR:
 		rc = smc_close_shutdown_write(smc);
@@ -2410,6 +2416,8 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	if (smc->clcsock)
 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
+
+out_no_shutdown:
 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
 	sk->sk_shutdown |= how + 1;
 
Signature · 1 line
2.32.0.3.g01195cf9f
#4 lore raw

Re: [PATCH net 2/2] net/smc: Don't call clcsock shutdown twice when smc shutdown

net/smc/af_smc.c static int smc_shutdown(struct socket *sock, int how) · line 2376

Quoted 9 lines · Tony Lu
net/smc/af_smc.c +1 -0
index 4b62c925a13e..7b04cb4d15f4 100644
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)
 	struct smc_sock *smc;
 	int rc = -EINVAL;
 	int rc1 = 0;
+	int old_state;

Reverse Christmas tree formatting, please.

net/smc/af_smc.c static int smc_shutdown(struct socket *sock, int how) · line 2407

Quoted 13 lines
net/smc/af_smc.c +5 -0
 	smc = smc_sk(sk);
 
@@ -2398,7 +2399,12 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	switch (how) {
 	case SHUT_RDWR:		/* shutdown in both directions */
+		old_state = sk->sk_state;
 		rc = smc_close_active(smc);
+		if (old_state == SMC_ACTIVE &&
+		    sk->sk_state == SMC_PEERCLOSEWAIT1)
+			goto out_no_shutdown;
+

I would prefer a new "bool do_shutdown" instead of a goto for this skip of the shutdown. What do you think?

Quoted 13 lines
net/smc/af_smc.c +2 -0
 		break;
 	case SHUT_WR:
 		rc = smc_close_shutdown_write(smc);
@@ -2410,6 +2416,8 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	if (smc->clcsock)
 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
+
+out_no_shutdown:
 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
 	sk->sk_shutdown |= how + 1;
 
Signature · 1 line
Karsten
author reviewer
#5 lore raw

Re: [PATCH net 2/2] net/smc: Don't call clcsock shutdown twice when smc shutdown

Quoted 12 lines · Karsten Graul

net/smc/af_smc.c static int smc_shutdown(struct socket *sock, int how) · line 2376

Quoted 9 lines · Tony Lu
net/smc/af_smc.c +1 -0
index 4b62c925a13e..7b04cb4d15f4 100644
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)
 	struct smc_sock *smc;
 	int rc = -EINVAL;
 	int rc1 = 0;
+	int old_state;

Reverse Christmas tree formatting, please.

Sorry for that, I will fix it in the next patch.

Quoted 4 lines
Quoted 4 lines
net/smc/af_smc.c +0 -0
 	smc = smc_sk(sk);
 
Quoted 13 lines

net/smc/af_smc.c static int smc_shutdown(struct socket *sock, int how) · line 2407

Quoted 10 lines
net/smc/af_smc.c +5 -0
@@ -2398,7 +2399,12 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	switch (how) {
 	case SHUT_RDWR:		/* shutdown in both directions */
+		old_state = sk->sk_state;
 		rc = smc_close_active(smc);
+		if (old_state == SMC_ACTIVE &&
+		    sk->sk_state == SMC_PEERCLOSEWAIT1)
+			goto out_no_shutdown;
+

I would prefer a new "bool do_shutdown" instead of a goto for this skip of the shutdown. What do you think?

I agree with you, I'd like bool condition rather than goto, which will disturb the continuity of reading code.

I will fix it soon. Thank you.

Tony Lu

Quoted 17 lines
Quoted 14 lines
net/smc/af_smc.c +2 -0
 		break;
 	case SHUT_WR:
 		rc = smc_close_shutdown_write(smc);
@@ -2410,6 +2416,8 @@ static int smc_shutdown(struct socket *sock, int how)
 	}
 	if (smc->clcsock)
 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
+
+out_no_shutdown:
 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
 	sk->sk_shutdown |= how + 1;
 
Signature · 1 line
Karsten