From: Tony Lu <tonylu@linux.alibaba.com> Date: 2021-11-25 06:22:58
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(-)
--
2.32.0.3.g01195cf9f
From: Tony Lu <tonylu@linux.alibaba.com> Date: 2021-11-25 06:23:16
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.
Link: https://lore.kernel.org/linux-s390/1f67548e-cbf6-0dce-82b5-10288a4583bd@linux.ibm.com/
Fixes: 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")
Suggested-by: Karsten Graul <redacted>
Signed-off-by: Tony Lu <tonylu@linux.alibaba.com>
Reviewed-by: Wen Gu <guwen@linux.alibaba.com>
---
net/smc/smc_close.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
@@ -195,6 +195,7 @@ int smc_close_active(struct smc_sock *smc)intold_state;longtimeout;intrc=0;+intrc1=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,*preventpeerfromenteringTIME_WAITstate.*/-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 */gotoagain;
From: Tony Lu <tonylu@linux.alibaba.com> Date: 2021-11-25 06:24:22
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().
Link: https://lore.kernel.org/linux-s390/1f67548e-cbf6-0dce-82b5-10288a4583bd@linux.ibm.com/
Fixes: 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")
Signed-off-by: Tony Lu <tonylu@linux.alibaba.com>
Reviewed-by: Wen Gu <guwen@linux.alibaba.com>
---
net/smc/af_smc.c | 8 ++++++++
1 file changed, 8 insertions(+)
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)structsmc_sock*smc;intrc=-EINVAL;intrc1=0;+intold_state;smc=smc_sk(sk);
@@ -2398,7 +2399,12 @@ static int smc_shutdown(struct socket *sock, int how)}switch(how){caseSHUT_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)+gotoout_no_shutdown;+break;caseSHUT_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;
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.
Link: https://lore.kernel.org/linux-s390/1f67548e-cbf6-0dce-82b5-10288a4583bd@linux.ibm.com/
Fixes: 606a63c9783a ("net/smc: Ensure the active closing peer first closes clcsock")
Suggested-by: Karsten Graul <redacted>
Signed-off-by: Tony Lu <tonylu@linux.alibaba.com>
Reviewed-by: Wen Gu <guwen@linux.alibaba.com>
---
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)structsmc_sock*smc;intrc=-EINVAL;intrc1=0;+intold_state;
Reverse Christmas tree formatting, please.
quoted hunk
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 hunk
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;
@@ -2373,6 +2373,7 @@ static int smc_shutdown(struct socket *sock, int how)structsmc_sock*smc;intrc=-EINVAL;intrc1=0;+intold_state;
Reverse Christmas tree formatting, please.
Sorry for that, I will fix it in the next patch.
quoted
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?
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
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;