Skip to contentSkip to conversation

net/smc: bugfixs for smc-r

PATCH In review D. Wythe 5 participants 38 messages Oct 11, 2023 – Oct 31, 2023 4 messages not in the archive

Subsystems (5)
lore
Share & tools
lore

D. Wythe

author maintainer
#0 lore raw

cover net/smc: bugfixs for smc-r

From: "D. Wythe" <[email protected]>

This patches contains bugfix following:

1. hung state
2. sock leak
3. potential panic

We have been testing these patches for some time, but if you have any questions, please let us know.

Thanks,
D. Wythe

D. Wythe (5):
net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT net/smc: fix incorrect barrier usage
net/smc: allow cdc msg send rather than drop it with NULL sndbuf_desc net/smc: protect connection state transitions in listen work net/smc: put sk reference if close work was canceled

 net/smc/af_smc.c    |  9 +++++++--
 net/smc/smc.h       |  5 +++++
 net/smc/smc_cdc.c   | 11 +++++------
 net/smc/smc_close.c |  5 +++--
 net/smc/smc_core.c  | 21 +++++++++++++--------
 5 files changed, 33 insertions(+), 18 deletions(-)
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 1/5 net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

From: "D. Wythe" <[email protected]>

Considering scenario:

smc_cdc_rx_handler_rwwi
__smc_release
sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD) __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&

	smc_close_sent_any_close(conn)) {
	sk->sk_state = SMC_CLOSED;
} else {
	/* just shutdown, but not yet closed locally */
	sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

---
 net/smc/af_smc.c    | 4 ++--
 net/smc/smc.h       | 5 +++++
 net/smc/smc_cdc.c   | 2 +-
 net/smc/smc_close.c | 2 +-
 4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
 
 	if (!smc->use_fallback) {
 		rc = smc_close_active(smc);
-		sock_set_flag(sk, SOCK_DEAD);
+		smc_sock_set_flag(sk, SOCK_DEAD);
 		sk->sk_shutdown |= SHUTDOWN_MASK;
 	} else {
 		if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (new_clcsock)
 			sock_release(new_clcsock);
 		new_sk->sk_state = SMC_CLOSED;
-		sock_set_flag(new_sk, SOCK_DEAD);
+		smc_sock_set_flag(new_sk, SOCK_DEAD);
 		sock_put(new_sk); /* final */
 		*new_smc = NULL;
 		goto out;
net/smc/smc.h +5 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
 
+static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
+{
+	set_bit(flag, &sk->sk_flags);
+}
+
 #endif	/* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 		smc->sk.sk_shutdown |= RCV_SHUTDOWN;
 		if (smc->clcsock && smc->clcsock->sk)
 			smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-		sock_set_flag(&smc->sk, SOCK_DONE);
+		smc_sock_set_flag(&smc->sk, SOCK_DONE);
 		sock_hold(&smc->sk); /* sock_put in close_work */
 		if (!queue_work(smc_close_wq, &conn->close_work))
 			sock_put(&smc->sk);
net/smc/smc_close.c +1 -1
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
 		break;
 	}
 
-	sock_set_flag(sk, SOCK_DEAD);
+	smc_sock_set_flag(sk, SOCK_DEAD);
 	sk->sk_state_change(sk);
 
 	if (release_clcsock) {
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 11 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

smc_cdc_rx_handler_rwwi
__smc_release
sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD) __set_bit(DONE)

If I understand correctly, both operations should hold sock_lock, that means thay should not race, have I missed something ?

Quoted 13 lines

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&

	smc_close_sent_any_close(conn)) {
	sk->sk_state = SMC_CLOSED;
} else {
	/* just shutdown, but not yet closed locally */
	sk->sk_state = SMC_APPFINCLOSEWAIT;
}
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

You should add a fixes tag.

Quoted 72 lines

---

net/smc/af_smc.c    | 4 ++--
net/smc/smc.h       | 5 +++++
net/smc/smc_cdc.c   | 2 +-
net/smc/smc_close.c | 2 +-
4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
 
 	if (!smc->use_fallback) {
 		rc = smc_close_active(smc);
-		sock_set_flag(sk, SOCK_DEAD);
+		smc_sock_set_flag(sk, SOCK_DEAD);
 		sk->sk_shutdown |= SHUTDOWN_MASK;
 	} else {
 		if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
 		if (new_clcsock)
 			sock_release(new_clcsock);
 		new_sk->sk_state = SMC_CLOSED;
-		sock_set_flag(new_sk, SOCK_DEAD);
+		smc_sock_set_flag(new_sk, SOCK_DEAD);
 		sock_put(new_sk); /* final */
 		*new_smc = NULL;
 		goto out;
net/smc/smc.h +5 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
 
+static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
+{
+	set_bit(flag, &sk->sk_flags);
+}
+
 #endif	/* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 		smc->sk.sk_shutdown |= RCV_SHUTDOWN;
 		if (smc->clcsock && smc->clcsock->sk)
 			smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-		sock_set_flag(&smc->sk, SOCK_DONE);
+		smc_sock_set_flag(&smc->sk, SOCK_DONE);
 		sock_hold(&smc->sk); /* sock_put in close_work */
 		if (!queue_work(smc_close_wq, &conn->close_work))
 			sock_put(&smc->sk);
net/smc/smc_close.c +1 -1
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
 		break;
 	}
 
-	sock_set_flag(sk, SOCK_DEAD);
+	smc_sock_set_flag(sk, SOCK_DEAD);
 	sk->sk_state_change(sk);
 
 	if (release_clcsock) {
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

In reply to D. Wythe
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

smc_cdc_rx_handler_rwwi
__smc_release
sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD) __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&

	smc_close_sent_any_close(conn)) {
	sk->sk_state = SMC_CLOSED;
} else {
	/* just shutdown, but not yet closed locally */
	sk->sk_state = SMC_APPFINCLOSEWAIT;
}
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Quoted 70 lines
---
  net/smc/af_smc.c    | 4 ++--
  net/smc/smc.h       | 5 +++++
  net/smc/smc_cdc.c   | 2 +-
  net/smc/smc_close.c | 2 +-
  4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
 
  	if (!smc->use_fallback) {
  		rc = smc_close_active(smc);
-		sock_set_flag(sk, SOCK_DEAD);
+		smc_sock_set_flag(sk, SOCK_DEAD);
  		sk->sk_shutdown |= SHUTDOWN_MASK;
  	} else {
  		if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
  		if (new_clcsock)
  			sock_release(new_clcsock);
  		new_sk->sk_state = SMC_CLOSED;
-		sock_set_flag(new_sk, SOCK_DEAD);
+		smc_sock_set_flag(new_sk, SOCK_DEAD);
  		sock_put(new_sk); /* final */
  		*new_smc = NULL;
  		goto out;
net/smc/smc.h +5 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
  int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
  int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
 
+static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
+{
+	set_bit(flag, &sk->sk_flags);
+}
+
  #endif	/* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  		smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  		if (smc->clcsock && smc->clcsock->sk)
  			smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-		sock_set_flag(&smc->sk, SOCK_DONE);
+		smc_sock_set_flag(&smc->sk, SOCK_DONE);
  		sock_hold(&smc->sk); /* sock_put in close_work */
  		if (!queue_work(smc_close_wq, &conn->close_work))
  			sock_put(&smc->sk);
net/smc/smc_close.c +1 -1
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
  		break;
  	}
 
-	sock_set_flag(sk, SOCK_DEAD);
+	smc_sock_set_flag(sk, SOCK_DEAD);
  	sk->sk_state_change(sk);
 
  	if (release_clcsock) {
author maintainer
#4 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 27 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) && smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 5 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Note :  bh_lock_sock and lock_sock are not mutually exclusive. They are actually used for different purposes and contexts.

Quoted 68 lines
Quoted 68 lines

---
  net/smc/af_smc.c    | 4 ++--
  net/smc/smc.h       | 5 +++++
  net/smc/smc_cdc.c   | 2 +-
  net/smc/smc_close.c | 2 +-

  4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
         if (!smc->use_fallback) {
           rc = smc_close_active(smc);
-        sock_set_flag(sk, SOCK_DEAD);
+        smc_sock_set_flag(sk, SOCK_DEAD);
           sk->sk_shutdown |= SHUTDOWN_MASK;
       } else {
           if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct smc_sock 
 *lsmc, struct smc_sock **new_smc)
           if (new_clcsock)
               sock_release(new_clcsock);
           new_sk->sk_state = SMC_CLOSED;
-        sock_set_flag(new_sk, SOCK_DEAD);
+        smc_sock_set_flag(new_sk, SOCK_DEAD);

          sock_put(new_sk); /* final */
          *new_smc = NULL;
          goto out;

net/smc/smc.h +4 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
   int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
   int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
   +static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
+{
+    set_bit(flag, &sk->sk_flags);
+}
+
   #endif    /* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct 
 smc_sock *smc,
           smc->sk.sk_shutdown |= RCV_SHUTDOWN;
           if (smc->clcsock && smc->clcsock->sk)
               smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-        sock_set_flag(&smc->sk, SOCK_DONE);
+        smc_sock_set_flag(&smc->sk, SOCK_DONE);

          sock_hold(&smc->sk); /* sock_put in close_work */ if (!queue_work(smc_close_wq, &conn->close_work)) sock_put(&smc->sk);

net/smc/smc_close.c +1 -0
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
           break;
       }
   -    sock_set_flag(sk, SOCK_DEAD);
+    smc_sock_set_flag(sk, SOCK_DEAD);
       sk->sk_state_change(sk);
         if (release_clcsock) {

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 57 lines · D. Wythe
Quoted 27 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) && smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 5 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 3 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Quoted 70 lines
Quoted 69 lines
Quoted 69 lines

---
  net/smc/af_smc.c    | 4 ++--
  net/smc/smc.h       | 5 +++++
  net/smc/smc_cdc.c   | 2 +-
  net/smc/smc_close.c | 2 +-

  4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
         if (!smc->use_fallback) {
           rc = smc_close_active(smc);
-        sock_set_flag(sk, SOCK_DEAD);
+        smc_sock_set_flag(sk, SOCK_DEAD);
           sk->sk_shutdown |= SHUTDOWN_MASK;
       } else {
           if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct smc_sock 
 *lsmc, struct smc_sock **new_smc)
           if (new_clcsock)
               sock_release(new_clcsock);
           new_sk->sk_state = SMC_CLOSED;
-        sock_set_flag(new_sk, SOCK_DEAD);
+        smc_sock_set_flag(new_sk, SOCK_DEAD);

          sock_put(new_sk); /* final */
          *new_smc = NULL;
          goto out;

net/smc/smc.h +4 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
   int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
   int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
   +static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)
+{
+    set_bit(flag, &sk->sk_flags);
+}
+
   #endif    /* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct 
 smc_sock *smc,
           smc->sk.sk_shutdown |= RCV_SHUTDOWN;
           if (smc->clcsock && smc->clcsock->sk)
               smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-        sock_set_flag(&smc->sk, SOCK_DONE);
+        smc_sock_set_flag(&smc->sk, SOCK_DONE);

          sock_hold(&smc->sk); /* sock_put in close_work */ if (!queue_work(smc_close_wq, &conn->close_work)) sock_put(&smc->sk);

net/smc/smc_close.c +1 -0
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
           break;
       }
   -    sock_set_flag(sk, SOCK_DEAD);
+    smc_sock_set_flag(sk, SOCK_DEAD);
       sk->sk_state_change(sk);
         if (release_clcsock) {
maintainer
#7 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 75 lines · Wenjia Zhang
Quoted 60 lines · D. Wythe
Quoted 27 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) && smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()

    spin_lock_bh(&sk->sk_lock.slock);
    ...
    sk->sk_lock.owned = 1;
    // here the spinlock is released
    spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
                                                   bh_lock_sock(&smc->sk);
                                                   smc_cdc_msg_recv_action(smc, cdc);
                                                       sock_set_flag(&smc->sk, SOCK_DONE);
                                                   bh_unlock_sock(&smc->sk);

    sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE)

release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()

         bh_lock_sock_nested(sk);
         tcp_segs_in(tcp_sk(sk), skb);
         ret = 0;
         if (!sock_owned_by_user(sk)) {
                 ret = tcp_v4_do_rcv(sk, skb);
         } else {
                 if (tcp_add_backlog(sk, skb, &drop_reason))
                         goto discard_and_relse;
         }
         bh_unlock_sock(sk);

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

Quoted 74 lines
Quoted 74 lines
Quoted 73 lines
Quoted 73 lines

---
  net/smc/af_smc.c    | 4 ++--
  net/smc/smc.h       | 5 +++++
  net/smc/smc_cdc.c   | 2 +-
  net/smc/smc_close.c | 2 +-

  4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
         if (!smc->use_fallback) {
           rc = smc_close_active(smc);
-        sock_set_flag(sk, SOCK_DEAD);
+        smc_sock_set_flag(sk, SOCK_DEAD);
           sk->sk_shutdown |= SHUTDOWN_MASK;
       } else {
           if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct
 smc_sock *lsmc, struct smc_sock **new_smc)
           if (new_clcsock)
               sock_release(new_clcsock);
           new_sk->sk_state = SMC_CLOSED;
-        sock_set_flag(new_sk, SOCK_DEAD);
+        smc_sock_set_flag(new_sk, SOCK_DEAD);

          sock_put(new_sk); /* final */
          *new_smc = NULL;
          goto out;

net/smc/smc.h +0 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,

  int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
  int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info);
  +static inline void smc_sock_set_flag(struct sock *sk, enum sock_flags flag)

net/smc/smc.h +4 -0
+{
+    set_bit(flag, &sk->sk_flags);
+}
+

  #endif    /* __SMC_H */

net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct
 smc_sock *smc,
           smc->sk.sk_shutdown |= RCV_SHUTDOWN;
           if (smc->clcsock && smc->clcsock->sk)
               smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-        sock_set_flag(&smc->sk, SOCK_DONE);
+        smc_sock_set_flag(&smc->sk, SOCK_DONE);

          sock_hold(&smc->sk); /* sock_put in close_work */ if (!queue_work(smc_close_wq, &conn->close_work)) sock_put(&smc->sk);

net/smc/smc_close.c +1 -0
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
           break;
       }
   -    sock_set_flag(sk, SOCK_DEAD);
+    smc_sock_set_flag(sk, SOCK_DEAD);
       sk->sk_state_change(sk);
         if (release_clcsock) {
#8 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 134 lines · Dust Li
Quoted 75 lines · Wenjia Zhang
Quoted 60 lines · D. Wythe
Quoted 27 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&
    smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()

     spin_lock_bh(&sk->sk_lock.slock);
     ...
     sk->sk_lock.owned = 1;
     // here the spinlock is released
     spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
                                                    bh_lock_sock(&smc->sk);
                                                    smc_cdc_msg_recv_action(smc, cdc);
                                                        sock_set_flag(&smc->sk, SOCK_DONE);
                                                    bh_unlock_sock(&smc->sk);

     sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE)

release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()

          bh_lock_sock_nested(sk);
          tcp_segs_in(tcp_sk(sk), skb);
          ret = 0;
          if (!sock_owned_by_user(sk)) {
                  ret = tcp_v4_do_rcv(sk, skb);
          } else {
                  if (tcp_add_backlog(sk, skb, &drop_reason))
                          goto discard_and_relse;
          }
          bh_unlock_sock(sk);
Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

Maybe I'm the only one who is getting stuck in the problem. I'll aprieciate if you can help me get out :P

Thanks,
Wenjia

Quoted 76 lines
Quoted 76 lines
Quoted 76 lines
Quoted 75 lines
Quoted 75 lines
---
   net/smc/af_smc.c    | 4 ++--
   net/smc/smc.h       | 5 +++++
   net/smc/smc_cdc.c   | 2 +-
   net/smc/smc_close.c | 2 +-
   4 files changed, 9 insertions(+), 4 deletions(-)
net/smc/af_smc.c +2 -2
index bacdd97..5ad2a9f 100644
@@ -275,7 +275,7 @@ static int __smc_release(struct smc_sock *smc)
         if (!smc->use_fallback) {
           rc = smc_close_active(smc);
-        sock_set_flag(sk, SOCK_DEAD);
+        smc_sock_set_flag(sk, SOCK_DEAD);
           sk->sk_shutdown |= SHUTDOWN_MASK;
       } else {
           if (sk->sk_state != SMC_CLOSED) {
@@ -1742,7 +1742,7 @@ static int smc_clcsock_accept(struct
 smc_sock *lsmc, struct smc_sock **new_smc)
           if (new_clcsock)
               sock_release(new_clcsock);
           new_sk->sk_state = SMC_CLOSED;
-        sock_set_flag(new_sk, SOCK_DEAD);
+        smc_sock_set_flag(new_sk, SOCK_DEAD);
           sock_put(new_sk); /* final */
           *new_smc = NULL;

          goto out;

net/smc/smc.h +0 -0
index 24745fd..e377980 100644
@@ -377,4 +377,9 @@ void smc_fill_gid_list(struct smc_link_group *lgr,
   int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct
 genl_info *info);
   int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct
 genl_info *info);
   +static inline void smc_sock_set_flag(struct sock *sk, enum

sock_flags flag)

net/smc/smc.h +4 -0
+{
+    set_bit(flag, &sk->sk_flags);
+}
+
   #endif    /* __SMC_H */
net/smc/smc_cdc.c +1 -1
index 89105e9..01bdb79 100644
@@ -385,7 +385,7 @@ static void smc_cdc_msg_recv_action(struct
 smc_sock *smc,
           smc->sk.sk_shutdown |= RCV_SHUTDOWN;
           if (smc->clcsock && smc->clcsock->sk)
               smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
-        sock_set_flag(&smc->sk, SOCK_DONE);
+        smc_sock_set_flag(&smc->sk, SOCK_DONE);
           sock_hold(&smc->sk); /* sock_put in close_work */
           if (!queue_work(smc_close_wq, &conn->close_work))

              sock_put(&smc->sk);

net/smc/smc_close.c +1 -0
index dbdf03e..449ef45 100644
@@ -173,7 +173,7 @@ void smc_close_active_abort(struct smc_sock *smc)
           break;
       }
   -    sock_set_flag(sk, SOCK_DEAD);
+    smc_sock_set_flag(sk, SOCK_DEAD);
       sk->sk_state_change(sk);
         if (release_clcsock) {
maintainer
#9 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 170 lines · Wenjia Zhang
Quoted 134 lines · Dust Li
Quoted 75 lines · Wenjia Zhang
Quoted 60 lines · D. Wythe
Quoted 27 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&
    smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()

     spin_lock_bh(&sk->sk_lock.slock);
     ...
     sk->sk_lock.owned = 1;
     // here the spinlock is released
     spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
                                                    bh_lock_sock(&smc->sk);
                                                    smc_cdc_msg_recv_action(smc, cdc);
                                                        sock_set_flag(&smc->sk, SOCK_DONE);
                                                    bh_unlock_sock(&smc->sk);

     sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE)

release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()

          bh_lock_sock_nested(sk);
          tcp_segs_in(tcp_sk(sk), skb);
          ret = 0;
          if (!sock_owned_by_user(sk)) {
                  ret = tcp_v4_do_rcv(sk, skb);
          } else {
                  if (tcp_add_backlog(sk, skb, &drop_reason))
                          goto discard_and_relse;
          }
          bh_unlock_sock(sk);
Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

I agree with you on this. I also don't see using atomic can solve the problem of overwriting the DEAD flag with DONE.

I think we need some mechanisms to ensure that sk_flags and other struct sock related fields are not modified simultaneously.

Best regards,
Dust

author maintainer
#10 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 169 lines · Dust Li
Quoted 164 lines · Wenjia Zhang
Quoted 129 lines · Dust Li
Quoted 71 lines · Wenjia Zhang
Quoted 57 lines · D. Wythe
Quoted 26 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&
    smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()

      spin_lock_bh(&sk->sk_lock.slock);
      ...
      sk->sk_lock.owned = 1;
      // here the spinlock is released
      spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
                                                     bh_lock_sock(&smc->sk);
                                                     smc_cdc_msg_recv_action(smc, cdc);
                                                         sock_set_flag(&smc->sk, SOCK_DONE);
                                                     bh_unlock_sock(&smc->sk);

      sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE)

release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()

           bh_lock_sock_nested(sk);
           tcp_segs_in(tcp_sk(sk), skb);
           ret = 0;
           if (!sock_owned_by_user(sk)) {
                   ret = tcp_v4_do_rcv(sk, skb);
           } else {
                   if (tcp_add_backlog(sk, skb, &drop_reason))
                           goto discard_and_relse;
           }
           bh_unlock_sock(sk);
Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

Quoted 8 lines

I agree with you on this. I also don't see using atomic can solve the problem of overwriting the DEAD flag with DONE.

I think we need some mechanisms to ensure that sk_flags and other struct sock related fields are not modified simultaneously.

Best regards,
Dust

It seems that everyone has agrees on that case 2 is impossible. I'm a bit confused, why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. What mechanism prevents their parallel execution?

Best wishes,
D. Wythe

Quoted 1 line
maintainer
#11 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 178 lines · D. Wythe
Quoted 175 lines · Dust Li
Quoted 164 lines · Wenjia Zhang
Quoted 129 lines · Dust Li
Quoted 71 lines · Wenjia Zhang
Quoted 57 lines · D. Wythe
Quoted 26 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                smc_cdc_rx_handler_rwwi
__smc_release
                sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&
    smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
    /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()

      spin_lock_bh(&sk->sk_lock.slock);
      ...
      sk->sk_lock.owned = 1;
      // here the spinlock is released
      spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
                                                     bh_lock_sock(&smc->sk);
                                                     smc_cdc_msg_recv_action(smc, cdc);
                                                         sock_set_flag(&smc->sk, SOCK_DONE);
                                                     bh_unlock_sock(&smc->sk);

      sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE)

release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()

           bh_lock_sock_nested(sk);
           tcp_segs_in(tcp_sk(sk), skb);
           ret = 0;
           if (!sock_owned_by_user(sk)) {
                   ret = tcp_v4_do_rcv(sk, skb);
           } else {
                   if (tcp_add_backlog(sk, skb, &drop_reason))
                           goto discard_and_relse;
           }
           bh_unlock_sock(sk);
Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

I agree with you on this. I also don't see using atomic can solve the problem of overwriting the DEAD flag with DONE.

I think we need some mechanisms to ensure that sk_flags and other struct sock related fields are not modified simultaneously.

Quoted 8 lines
Quoted 2 lines

Best regards,
Dust

It seems that everyone has agrees on that case 2 is impossible. I'm a bit confused, why that
sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. What mechanism
prevents their parallel execution?

Upon reviewing the code again, I realize that my previous understanding was incorrect. I mistakenly believed that the DEAD and DONE flags would overwrite each other, without realizing that sk_flags is actually used as a bitmap.

So, I think you are right, using atomic will ensure that the DEAD flag is always set.

Best regards,
Dust

#12 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

In reply to D. Wythe
Quoted 185 lines · D. Wythe
Quoted 173 lines · Dust Li
Quoted 168 lines · Wenjia Zhang
Quoted 133 lines · Dust Li
Quoted 71 lines · Wenjia Zhang
Quoted 57 lines · D. Wythe
Quoted 26 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                  smc_cdc_rx_handler_rwwi
__smc_release
                  sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) && smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
      /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

      PROCESS CONTEXT                                 INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()
      spin_lock_bh(&sk->sk_lock.slock);
      ...
      sk->sk_lock.owned = 1;
      // here the spinlock is released spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()

bh_lock_sock(&smc->sk);

smc_cdc_msg_recv_action(smc, cdc);

sock_set_flag(&smc->sk, SOCK_DONE);

bh_unlock_sock(&smc->sk);

      sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE) release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()
           bh_lock_sock_nested(sk); tcp_segs_in(tcp_sk(sk), skb);
           ret = 0;
           if (!sock_owned_by_user(sk)) {
                   ret = tcp_v4_do_rcv(sk, skb);
           } else {
                   if (tcp_add_backlog(sk, skb, &drop_reason)) goto discard_and_relse;
           }
           bh_unlock_sock(sk);

Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

Quoted 8 lines

I agree with you on this. I also don't see using atomic can solve the problem of overwriting the DEAD flag with DONE.

I think we need some mechanisms to ensure that sk_flags and other struct sock related fields are not modified simultaneously.

Best regards,
Dust

Quoted 9 lines

It seems that everyone has agrees on that case 2 is impossible. I'm a bit confused, why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. What mechanism prevents their parallel execution?

Best wishes,
D. Wythe

Quoted 1 line

In the smc_cdc_rx_handler(), if bh_lock_sock() is got, how could the sock_set_flag(DEAD) in the __smc_release() modify the flag concurrently? As I said, that could be just kind of lapse of my thought, but I still want to make it clarify.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

Quoted 222 lines · D. Wythe
Quoted 188 lines · Wenjia Zhang
Quoted 185 lines · D. Wythe
Quoted 169 lines · Dust Li
Quoted 164 lines · Wenjia Zhang
Quoted 129 lines · Dust Li
Quoted 71 lines · Wenjia Zhang
Quoted 57 lines · D. Wythe
Quoted 26 lines · Wenjia Zhang
Quoted 24 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

                  smc_cdc_rx_handler_rwwi
__smc_release
                  sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD)            __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED  will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) && smc_close_sent_any_close(conn)) { sk->sk_state = SMC_CLOSED;
} else {
      /* just shutdown, but not yet closed locally */ sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Quoted 7 lines
Quoted 3 lines

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

I didn't really understand the scenario. What is smc_cdc_rx_handler_rwwi()? What does it do? Don't it get the lock during the runtime?

Hi Wenjia,

Sorry for that, It is not smc_cdc_rx_handler_rwwi() but smc_cdc_rx_handler();

Following is a more specific description of the issues

lock_sock()
__smc_release

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

Note : |bh_lock_sock|and |lock_sock|are not mutually exclusive. They are actually used for different purposes and contexts.

ok, that's true that |bh_lock_sock|and |lock_sock|are not really mutually exclusive. However, since bh_lock_sock() is used, this scenario you described above should not happen, because that gets the sk_lock.slock. Following this scenarios, IMO, only the following situation can happen.

lock_sock()
__smc_release

Quoted 8 lines

smc_cdc_rx_handler()
smc_cdc_msg_recv()
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
sock_set_flag(DEAD)
release_sock()

Hi wenjia,

I think I know what D. Wythe means now, and I think he is right on this.

IIUC, in process context, lock_sock() won't respect bh_lock_sock() if it acquires the lock before bh_lock_sock(). This is how the sock lock works.

      PROCESS CONTEXT INTERRUPT CONTEXT
------------------------------------------------------------------------
lock_sock()
      spin_lock_bh(&sk->sk_lock.slock);
      ...
      sk->sk_lock.owned = 1;
      // here the spinlock is released
      spin_unlock_bh(&sk->sk_lock.slock);
__smc_release()
bh_lock_sock(&smc->sk);
smc_cdc_msg_recv_action(smc, cdc);
sock_set_flag(&smc->sk, SOCK_DONE);
bh_unlock_sock(&smc->sk);

      sock_set_flag(DEAD)  <-- Can be before or after sock_set_flag(DONE) release_sock()

The bh_lock_sock() only spins on sk->sk_lock.slock, which is already released after lock_sock() return. Therefor, there is actually no lock between
the code after lock_sock() and before release_sock() with bh_lock_sock()...bh_unlock_sock(). Thus, sock_set_flag(DEAD) won't respect bh_lock_sock() at all, and might be before or after sock_set_flag(DONE).

Actually, in TCP, the interrupt context will check sock_owned_by_user(). If it returns true, the softirq just defer the process to backlog, and process that in release_sock(). Which avoid the race between softirq and process when visiting the 'struct sock'.

tcp_v4_rcv()
           bh_lock_sock_nested(sk); tcp_segs_in(tcp_sk(sk), skb);
           ret = 0;
           if (!sock_owned_by_user(sk)) {
                   ret = tcp_v4_do_rcv(sk, skb);
           } else {
                   if (tcp_add_backlog(sk, skb, &drop_reason)) goto discard_and_relse;
           }
           bh_unlock_sock(sk);

Quoted 6 lines

But in SMC we don't have a backlog, that means fields in 'struct sock' might all have race, and this sock_set_flag() is just one of the cases.

Best regards,
Dust

I agree on your description above.
Sure, the following case 1) can also happen

case 1)
-------
lock_sock()
__smc_release

sock_set_flag(DEAD)
bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE)
bh_unlock_sock()
release_sock()

case 2)
-------
lock_sock()
__smc_release

bh_lock_sock()
smc_cdc_msg_recv_action()
sock_set_flag(DONE) sock_set_flag(DEAD)
__set_bit __set_bit
bh_unlock_sock()
release_sock()

Quoted 4 lines

My point here is that case2) can never happen. i.e that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. Thus, how could the atomic set help make sure that the Dead flag would not be overwritten with DONE?

Quoted 8 lines

I agree with you on this. I also don't see using atomic can solve the problem of overwriting the DEAD flag with DONE.

I think we need some mechanisms to ensure that sk_flags and other struct sock related fields are not modified simultaneously.

Best regards,
Dust

It seems that everyone has agrees on that case 2 is impossible. I'm a bit confused, why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently. What mechanism prevents their parallel execution?

Quoted 6 lines
Quoted 5 lines

Best wishes,
D. Wythe

Quoted 1 line

In the smc_cdc_rx_handler(), if bh_lock_sock() is got, how could the sock_set_flag(DEAD) in the __smc_release() modify the flag concurrently? As I said, that could be just kind of lapse of my thought, but I still want to make it clarify.

#define bh_lock_sock(__sk) spin_lock(&((__sk)->sk_lock.slock))

static inline void lock_sock(struct sock *sk)
{
    lock_sock_nested(sk, 0);
}

void lock_sock_nested(struct sock *sk, int subclass) {
    might_sleep();
spin_lock_bh(&sk->sk_lock.slock);
    if (sk->sk_lock.owned)
        __lock_sock(sk);
    sk->sk_lock.owned = 1;

*/spin_unlock(&sk->sk_lock.slock);/*
    /*
     * The sk_lock has mutex_lock() semantics here:
     */
    mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_); local_bh_enable();
}

Quoted 3 lines

It seems that you believe bh_lock_sock() will block the execution of __smc_release(), indicating that you think the spin on slock will block the execution of __smc_release(). So, you assume that __smc_release() must also spin on slock, right?

That is right what I mean.

Quoted 3 lines

However, lock_sock() releases the slock before returning. You can see it in code above. In other words, __smc_release() will not spin on slock. This means that bh_lock_sock() will not block the execution of __smc_release().

Do you mean that the spin_unlock you marked in the code above is to release the socket spin lock from __smc_release()?

Quoted 9 lines

Hoping this will helps

Best wishes,
D. Wythe

Reviewed-by on v1 1/5
#15 lore raw

Re: [PATCH net 1/5] net/smc: fix dangling sock under state SMC_APPFINCLOSEWAIT

In reply to D. Wythe
Quoted 27 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Considering scenario:

smc_cdc_rx_handler_rwwi
__smc_release
sock_set_flag
smc_close_active()
sock_set_flag

__set_bit(DEAD) __set_bit(DONE)

Dues to __set_bit is not atomic, the DEAD or DONE might be lost. if the DEAD flag lost, the state SMC_CLOSED will be never be reached in smc_close_passive_work:

if (sock_flag(sk, SOCK_DEAD) &&

	smc_close_sent_any_close(conn)) {
	sk->sk_state = SMC_CLOSED;
} else {
	/* just shutdown, but not yet closed locally */
	sk->sk_state = SMC_APPFINCLOSEWAIT;
}

Replace sock_set_flags or __set_bit to set_bit will fix this problem. Since set_bit is atomic.

Quoted 8 lines
---
  net/smc/af_smc.c    | 4 ++--
  net/smc/smc.h       | 5 +++++
  net/smc/smc_cdc.c   | 2 +-
  net/smc/smc_close.c | 2 +-
  4 files changed, 9 insertions(+), 4 deletions(-)
author maintainer
#16 lore raw

PATCH 2/5 net/smc: fix incorrect barrier usage

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

This patch add explicit CPU barrier to ensure memory consistency rather than compiler barrier.

Besides, the atomicity between READ_ONCE and cmpxhcg cannot be guaranteed, so we need to use atomic ops. The simple way is to replace READ_ONCE with xchg.

Fixes 475f9ff63ee8 ("net/smc: fix application data exception")

Links: https://lore.kernel.org/netdev/[email protected]/T/

---
 net/smc/smc_core.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
net/smc/smc_core.c +13 -8
index d520ee6..cc7d72e 100644
@@ -1133,9 +1133,10 @@ static void smcr_buf_unuse(struct smc_buf_desc *buf_desc, bool is_rmb,
 
 		smc_buf_free(lgr, is_rmb, buf_desc);
 	} else {
-		/* memzero_explicit provides potential memory barrier semantics */
-		memzero_explicit(buf_desc->cpu_addr, buf_desc->len);
-		WRITE_ONCE(buf_desc->used, 0);
+		memset(buf_desc->cpu_addr, 0, buf_desc->len);
+		/* make sure buf_desc->used not be reordered ahead */
+		smp_mb__before_atomic();
+		xchg(&buf_desc->used, 0);
 	}
 }
 
@@ -1146,17 +1147,21 @@ static void smc_buf_unuse(struct smc_connection *conn,
 		if (!lgr->is_smcd && conn->sndbuf_desc->is_vm) {
 			smcr_buf_unuse(conn->sndbuf_desc, false, lgr);
 		} else {
-			memzero_explicit(conn->sndbuf_desc->cpu_addr, conn->sndbuf_desc->len);
-			WRITE_ONCE(conn->sndbuf_desc->used, 0);
+			memset(conn->sndbuf_desc->cpu_addr, 0, conn->sndbuf_desc->len);
+			/* make sure buf_desc->used not be reordered ahead */
+			smp_mb__before_atomic();
+			xchg(&conn->sndbuf_desc->used, 0);
 		}
 	}
 	if (conn->rmb_desc) {
 		if (!lgr->is_smcd) {
 			smcr_buf_unuse(conn->rmb_desc, true, lgr);
 		} else {
-			memzero_explicit(conn->rmb_desc->cpu_addr,
-					 conn->rmb_desc->len + sizeof(struct smcd_cdc_msg));
-			WRITE_ONCE(conn->rmb_desc->used, 0);
+			memset(conn->rmb_desc->cpu_addr, 0,
+			       conn->rmb_desc->len + sizeof(struct smcd_cdc_msg));
+			/* make sure buf_desc->used not be reordered ahead */
+			smp_mb__before_atomic();
+			xchg(&conn->rmb_desc->used, 0);
 		}
 	}
 }
Signature · 1 line
1.8.3.1
maintainer
#17 lore raw

Re: [PATCH net 2/5] net/smc: fix incorrect barrier usage

Quoted 5 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This patch add explicit CPU barrier to ensure memory consistency rather than compiler barrier.

Quoted 7 lines

Besides, the atomicity between READ_ONCE and cmpxhcg cannot be guaranteed, so we need to use atomic ops. The simple way is to replace READ_ONCE with xchg.

Fixes 475f9ff63ee8 ("net/smc: fix application data exception")

^^^
I did not Co-develop this, nor did I provide an explicit Signed-off-by. Please don't add Signed-off-by statements which have not been explicitly agreed on.

author maintainer
#18 lore raw

Re: [PATCH net 2/5] net/smc: fix incorrect barrier usage

Quoted 3 lines · Heiko Carstens

I did not Co-develop this, nor did I provide an explicit Signed-off-by. Please don't add Signed-off-by statements which have not been explicitly agreed on.

Sorry for that, I have used the wrong tag, Reported by might be more appropriate . I will remove this in the next version, sorry to bother you again.

Best wishes,
D. Wythe

author maintainer
#19 lore raw

PATCH 3/5 net/smc: allow cdc msg send rather than drop it with NULL sndbuf_desc

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

This patch re-fix the issues memtianed by commit 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

Blocking sending message do solve the issues though, but it also prevents the peer to receive the final message. Besides, in logic, whether the sndbuf_desc is NULL or not have no impact on the processing of cdc message sending.

Hence that, this patch allow the cdc message sending but to check the sndbuf_desc with care in smc_cdc_tx_handler().

Fixes 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()")
---
 net/smc/smc_cdc.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
net/smc/smc_cdc.c +4 -5
index 01bdb79..3c06625 100644
@@ -28,13 +28,15 @@ static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
 {
 	struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
 	struct smc_connection *conn = cdcpend->conn;
+	struct smc_buf_desc *sndbuf_desc;
 	struct smc_sock *smc;
 	int diff;
 
+	sndbuf_desc = conn->sndbuf_desc;
 	smc = container_of(conn, struct smc_sock, conn);
 	bh_lock_sock(&smc->sk);
-	if (!wc_status) {
-		diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
+	if (!wc_status && sndbuf_desc) {
+		diff = smc_curs_diff(sndbuf_desc->len,
 				     &cdcpend->conn->tx_curs_fin,
 				     &cdcpend->cursor);
 		/* sndbuf_space is decreased in smc_sendmsg */
@@ -114,9 +116,6 @@ int smc_cdc_msg_send(struct smc_connection *conn,
 	union smc_host_cursor cfed;
 	int rc;
 
-	if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
-		return -ENOBUFS;
-
 	smc_cdc_add_pending_send(conn, pend);
 
 	conn->tx_cdc_seq++;
Signature · 1 line
1.8.3.1
#20 lore raw

Re: [PATCH net 3/5] net/smc: allow cdc msg send rather than drop it with NULL sndbuf_desc

Quoted 10 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This patch re-fix the issues memtianed by commit 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

Blocking sending message do solve the issues though, but it also prevents the peer to receive the final message. Besides, in logic, whether the sndbuf_desc is NULL or not have no impact on the processing of cdc message sending.

Agree.

Quoted 18 lines

Hence that, this patch allow the cdc message sending but to check the sndbuf_desc with care in smc_cdc_tx_handler().

Fixes 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()")
---
  net/smc/smc_cdc.c | 9 ++++-----
  1 file changed, 4 insertions(+), 5 deletions(-)
net/smc/smc_cdc.c +1 -0
index 01bdb79..3c06625 100644
@@ -28,13 +28,15 @@ static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
  {
  	struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
  	struct smc_connection *conn = cdcpend->conn;
+	struct smc_buf_desc *sndbuf_desc;

net/smc/smc_cdc.c static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, · line 39

Quoted 10 lines
net/smc/smc_cdc.c +3 -2
  	struct smc_sock *smc;
  	int diff;
 
+	sndbuf_desc = conn->sndbuf_desc;
  	smc = container_of(conn, struct smc_sock, conn);
  	bh_lock_sock(&smc->sk);
-	if (!wc_status) {
-		diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
+	if (!wc_status && sndbuf_desc) {
+		diff = smc_curs_diff(sndbuf_desc->len,

How could this guarantee that the sndbuf_desc would not be NULL?

Quoted 13 lines
net/smc/smc_cdc.c +0 -3
  				     &cdcpend->conn->tx_curs_fin,
  				     &cdcpend->cursor);
  		/* sndbuf_space is decreased in smc_sendmsg */
@@ -114,9 +116,6 @@ int smc_cdc_msg_send(struct smc_connection *conn,
  	union smc_host_cursor cfed;
  	int rc;
 
-	if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
-		return -ENOBUFS;
-
  	smc_cdc_add_pending_send(conn, pend);
 
  	conn->tx_cdc_seq++;
author maintainer
#21 lore raw

Re: [PATCH net 3/5] net/smc: allow cdc msg send rather than drop it with NULL sndbuf_desc

Quoted 36 lines · Wenjia Zhang
Quoted 10 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This patch re-fix the issues memtianed by commit 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

Blocking sending message do solve the issues though, but it also prevents the peer to receive the final message. Besides, in logic, whether the sndbuf_desc is NULL or not have no impact on the processing of cdc message sending.

Agree.

Quoted 21 lines

Hence that, this patch allow the cdc message sending but to check the sndbuf_desc with care in smc_cdc_tx_handler().

Fixes 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()")

---
  net/smc/smc_cdc.c | 9 ++++-----

  1 file changed, 4 insertions(+), 5 deletions(-)
net/smc/smc_cdc.c +1 -0
index 01bdb79..3c06625 100644
@@ -28,13 +28,15 @@ static void smc_cdc_tx_handler(struct 
 smc_wr_tx_pend_priv *pnd_snd,
   {
       struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
       struct smc_connection *conn = cdcpend->conn;
+    struct smc_buf_desc *sndbuf_desc;
       struct smc_sock *smc;
       int diff;
Quoted 9 lines

net/smc/smc_cdc.c static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, · line 39

Quoted 7 lines
net/smc/smc_cdc.c +2 -2
   +    sndbuf_desc = conn->sndbuf_desc;
       smc = container_of(conn, struct smc_sock, conn);
       bh_lock_sock(&smc->sk);
-    if (!wc_status) {
-        diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
+    if (!wc_status && sndbuf_desc) {
+        diff = smc_curs_diff(sndbuf_desc->len,

How could this guarantee that the sndbuf_desc would not be NULL?

It can not guarantee he sndbuf_desc would not be NULL, but it will prevents the smc_cdc_tx_handler() to access a NULL sndbuf_desc. So that we can avoid the panic descried in commit 22a825c541d7
("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

Quoted 11 lines
Quoted 11 lines
net/smc/smc_cdc.c +0 -0
 &cdcpend->conn->tx_curs_fin,
                        &cdcpend->cursor);

          /* sndbuf_space is decreased in smc_sendmsg */

net/smc/smc_cdc.c +0 -2
@@ -114,9 +116,6 @@ int smc_cdc_msg_send(struct smc_connection *conn,
       union smc_host_cursor cfed;
       int rc;
   -    if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
-        return -ENOBUFS;
-
       smc_cdc_add_pending_send(conn, pend);
         conn->tx_cdc_seq++;
Reviewed-by
#22 lore raw

Re: [PATCH net 3/5] net/smc: allow cdc msg send rather than drop it with NULL sndbuf_desc

Quoted 49 lines · D. Wythe
Quoted 36 lines · Wenjia Zhang
Quoted 10 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This patch re-fix the issues memtianed by commit 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

Blocking sending message do solve the issues though, but it also prevents the peer to receive the final message. Besides, in logic, whether the sndbuf_desc is NULL or not have no impact on the processing of cdc message sending.

Agree.

Quoted 21 lines

Hence that, this patch allow the cdc message sending but to check the sndbuf_desc with care in smc_cdc_tx_handler().

Fixes 22a825c541d7 ("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()")

---
  net/smc/smc_cdc.c | 9 ++++-----

  1 file changed, 4 insertions(+), 5 deletions(-)
net/smc/smc_cdc.c +1 -0
index 01bdb79..3c06625 100644
@@ -28,13 +28,15 @@ static void smc_cdc_tx_handler(struct 
 smc_wr_tx_pend_priv *pnd_snd,
   {
       struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
       struct smc_connection *conn = cdcpend->conn;
+    struct smc_buf_desc *sndbuf_desc;
       struct smc_sock *smc;
       int diff;
Quoted 9 lines

net/smc/smc_cdc.c static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, · line 39

Quoted 7 lines
net/smc/smc_cdc.c +2 -2
   +    sndbuf_desc = conn->sndbuf_desc;
       smc = container_of(conn, struct smc_sock, conn);
       bh_lock_sock(&smc->sk);
-    if (!wc_status) {
-        diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
+    if (!wc_status && sndbuf_desc) {
+        diff = smc_curs_diff(sndbuf_desc->len,

How could this guarantee that the sndbuf_desc would not be NULL?

Quoted 5 lines

It can not guarantee he sndbuf_desc would not be NULL, but it will prevents the smc_cdc_tx_handler() to access a NULL sndbuf_desc. So that we can avoid the panic descried in commit 22a825c541d7
("net/smc: fix NULL sndbuf_desc in smc_cdc_tx_handler()").

got it, thanks!

Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
net/smc/smc_cdc.c +0 -0
 &cdcpend->conn->tx_curs_fin,
                        &cdcpend->cursor);

          /* sndbuf_space is decreased in smc_sendmsg */

net/smc/smc_cdc.c +0 -2
@@ -114,9 +116,6 @@ int smc_cdc_msg_send(struct smc_connection *conn,
       union smc_host_cursor cfed;
       int rc;
   -    if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
-        return -ENOBUFS;
-
       smc_cdc_add_pending_send(conn, pend);
         conn->tx_cdc_seq++;
author maintainer
#23 lore raw

PATCH 4/5 net/smc: protect connection state transitions in listen work

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

Consider the following scenario:

smc_close_passive_work
smc_listen_out_connected

				lock_sock()
if (state  == SMC_INIT)
				if (state  == SMC_INIT)
					state = SMC_APPCLOSEWAIT1;
	state = SMC_ACTIVE
				release_sock()

This would cause the state machine of the connection to be corrupted. Also, this issue can occur in smc_listen_out_err().

To solve this problem, we can protect the state transitions under the lock of sock to avoid collision.

Fixes 3b2dec2603d5 ("net/smc: restructure client and server code in af_smc")
---
 net/smc/af_smc.c | 5 +++++
 1 file changed, 5 insertions(+)
net/smc/af_smc.c +5 -0
index 5ad2a9f..3bb8265 100644
@@ -1926,8 +1926,10 @@ static void smc_listen_out_connected(struct smc_sock *new_smc)
 {
 	struct sock *newsmcsk = &new_smc->sk;
 
+	lock_sock(newsmcsk);
 	if (newsmcsk->sk_state == SMC_INIT)
 		newsmcsk->sk_state = SMC_ACTIVE;
+	release_sock(newsmcsk);
 
 	smc_listen_out(new_smc);
 }
@@ -1939,9 +1941,12 @@ static void smc_listen_out_err(struct smc_sock *new_smc)
 	struct net *net = sock_net(newsmcsk);
 
 	this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
+
+	lock_sock(newsmcsk);
 	if (newsmcsk->sk_state == SMC_INIT)
 		sock_put(&new_smc->sk); /* passive closing */
 	newsmcsk->sk_state = SMC_CLOSED;
+	release_sock(newsmcsk);
 
 	smc_listen_out(new_smc);
 }
Signature · 1 line
1.8.3.1
#24 lore raw

Re: [PATCH net 4/5] net/smc: protect connection state transitions in listen work

Quoted 13 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Consider the following scenario:

smc_close_passive_work
smc_listen_out_connected

				lock_sock()
if (state  == SMC_INIT)
				if (state  == SMC_INIT)
					state = SMC_APPCLOSEWAIT1;
	state = SMC_ACTIVE
				release_sock()
Quoted 6 lines

This would cause the state machine of the connection to be corrupted. Also, this issue can occur in smc_listen_out_err().

To solve this problem, we can protect the state transitions under the lock of sock to avoid collision.

To this fix, I have to repeat the question from Alexandra.
Did the scenario occur in real life? Or is it just kind of potencial problem you found during the code review?

If it is the former one, could you please show us the corresponding message, e.g. from dmesg? If it is the latter one, I'd like to deal with it more carefully. Going from this scenario, I noticed that there could also be other similar places where we need to make sure that no race happens. Thus, it would make more sense to find a systematic approach.

Quoted 34 lines
Fixes 3b2dec2603d5 ("net/smc: restructure client and server code in af_smc")
---
  net/smc/af_smc.c | 5 +++++
  1 file changed, 5 insertions(+)
net/smc/af_smc.c +5 -0
index 5ad2a9f..3bb8265 100644
@@ -1926,8 +1926,10 @@ static void smc_listen_out_connected(struct smc_sock *new_smc)
  {
  	struct sock *newsmcsk = &new_smc->sk;
 
+	lock_sock(newsmcsk);
  	if (newsmcsk->sk_state == SMC_INIT)
  		newsmcsk->sk_state = SMC_ACTIVE;
+	release_sock(newsmcsk);
 
  	smc_listen_out(new_smc);
  }
@@ -1939,9 +1941,12 @@ static void smc_listen_out_err(struct smc_sock *new_smc)
  	struct net *net = sock_net(newsmcsk);
 
  	this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
+
+	lock_sock(newsmcsk);
  	if (newsmcsk->sk_state == SMC_INIT)
  		sock_put(&new_smc->sk); /* passive closing */
  	newsmcsk->sk_state = SMC_CLOSED;
+	release_sock(newsmcsk);
 
  	smc_listen_out(new_smc);
  }
author maintainer
#25 lore raw

Re: [PATCH net 4/5] net/smc: protect connection state transitions in listen work

Quoted 16 lines · Wenjia Zhang
Quoted 13 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Consider the following scenario:

                smc_close_passive_work
smc_listen_out_connected
                lock_sock()
if (state  == SMC_INIT)
                if (state  == SMC_INIT)
                    state = SMC_APPCLOSEWAIT1;
    state = SMC_ACTIVE
                release_sock()

Quoted 9 lines
Quoted 6 lines

This would cause the state machine of the connection to be corrupted. Also, this issue can occur in smc_listen_out_err().

To solve this problem, we can protect the state transitions under the lock of sock to avoid collision.

To this fix, I have to repeat the question from Alexandra.
Did the scenario occur in real life? Or is it just kind of potencial problem you found during the code review?

Hi Wenjia,

This is a real issue that occurred in our environment rather than being obtained from code reviews. Unfortunately, since this patch does not cause panic, but rather potential reference leaks, so it is difficult for me to provide a very intuitive error message.

Quoted 2 lines

If it is the former one, could you please show us the corresponding message, e.g. from dmesg? If it is the latter one, I'd like to deal with it more carefully. Going from this scenario, I noticed that there could also be other similar places where we need to make sure that no race happens. Thus, it would make more sense to find a systematic approach.

We agree that we should deal with it with more care, In fact, this issue is very complex and we may spend a lot of time discussing it. Therefore, I suggest that we can temporarily drop it so that we can quickly accept the patch we have already agreed on. I will send those patches separately in the future.

Best Wishes,
D. Wythe

Quoted 32 lines
Quoted 32 lines
Fixes 3b2dec2603d5 ("net/smc: restructure client and server code in af_smc")

---
  net/smc/af_smc.c | 5 +++++

  1 file changed, 5 insertions(+)
net/smc/af_smc.c +3 -0
index 5ad2a9f..3bb8265 100644
@@ -1926,8 +1926,10 @@ static void smc_listen_out_connected(struct 
 smc_sock *new_smc)
   {
       struct sock *newsmcsk = &new_smc->sk;
   +    lock_sock(newsmcsk);
       if (newsmcsk->sk_state == SMC_INIT)
           newsmcsk->sk_state = SMC_ACTIVE;
+    release_sock(newsmcsk);
         smc_listen_out(new_smc);
   }
@@ -1939,9 +1941,12 @@ static void smc_listen_out_err(struct smc_sock 
 *new_smc)
       struct net *net = sock_net(newsmcsk);
 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
+
+    lock_sock(newsmcsk);
       if (newsmcsk->sk_state == SMC_INIT)

          sock_put(&new_smc->sk); /* passive closing */ newsmcsk->sk_state = SMC_CLOSED;
+    release_sock(newsmcsk); smc_listen_out(new_smc);
  }

author maintainer
#26 lore raw

PATCH 5/5 net/smc: put sk reference if close work was canceled

In reply to D. Wythe

From: "D. Wythe" <[email protected]>

Note that we always hold a reference to sock when attempting to submit close_work. Therefore, if we have successfully canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")
---
 net/smc/smc_close.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct smc_sock *smc)
 	struct sock *sk = &smc->sk;
 
 	release_sock(sk);
-	cancel_work_sync(&smc->conn.close_work);
+	if (cancel_work_sync(&smc->conn.close_work))
+		sock_put(sk);
 	cancel_delayed_work_sync(&smc->conn.tx_work);
 	lock_sock(sk);
 }
Signature · 1 line
1.8.3.1
maintainer Reviewed-by on v1 5/5
#27 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 9 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Note that we always hold a reference to sock when attempting to submit close_work. Therefore, if we have successfully canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

Best regards,
Dust

Quoted 20 lines

---
net/smc/smc_close.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct smc_sock *smc)
 	struct sock *sk = &smc->sk;
 
 	release_sock(sk);
-	cancel_work_sync(&smc->conn.close_work);
+	if (cancel_work_sync(&smc->conn.close_work))
+		sock_put(sk);
 	cancel_delayed_work_sync(&smc->conn.tx_work);
 	lock_sock(sk);
 }
Signature · 1 line
1.8.3.1
#28 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

In reply to D. Wythe
Quoted 4 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Quoted 20 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")
---
  net/smc/smc_close.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct smc_sock *smc)
  	struct sock *sk = &smc->sk;
 
  	release_sock(sk);
-	cancel_work_sync(&smc->conn.close_work);
+	if (cancel_work_sync(&smc->conn.close_work))
+		sock_put(sk);
  	cancel_delayed_work_sync(&smc->conn.tx_work);
  	lock_sock(sk);
  }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#30 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct smc_sock 
 *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
       cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
author maintainer
#31 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
                                                               if (cancel_work_sync())        // successfully cancel before execution sock_put()                        //  need to put it since we already hold a ref before   queue_work()

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 20 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
       cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
#32 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 69 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

smc_close_cancel_work
smc_cdc_msg_recv_action

Quoted 7 lines

sock_hold
queue_work
                                                               if
(cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

I think here two Problems need to be clarified:

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

Quoted 23 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 20 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
       cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
author maintainer
#33 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 78 lines · Wenjia Zhang
Quoted 65 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

Quoted 10 lines

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

Quoted 7 lines

ha, I already thought you might ask such question:P

I think here two Problems need to be clarified:

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(), then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 1 line

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it, but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

2. If the queue_work returns true, the work should be or is being executed

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Best wishes,
D. Wythe

Quoted 24 lines
Quoted 24 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
 cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
#34 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 83 lines · D. Wythe
Quoted 80 lines · Wenjia Zhang
Quoted 65 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

Quoted 10 lines

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

Quoted 8 lines
Quoted 5 lines

I think here two Problems need to be clarified:

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

No, the logic of the inference is very vague to me. My understand is completely different. That is what I read from the kernel code. They are not *completely* exclusive, because while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock. (This is actually my main point of my argument for these fixes, and I didn't see any clarify from you). However, while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock, this is just like what you showed in the code in lock_sock. Once it gets the ownership, it release the spinlock.

Quoted 17 lines

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(), then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 1 line

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it, but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

Quoted 8 lines

2. If the queue_work returns true, the work should be or is being executed

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Best wishes,
D. Wythe

If you say the thread is not gauranteed to be waken up in then queue_work to execute the work, please explain what the kick_pool function does.

However, the spin_lock understanding is still the key problem in the cases. As I said, if it is not get clarify, we don't really need to go on to disucss this.

Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
 cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
author maintainer
#35 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 88 lines · Wenjia Zhang
Quoted 85 lines · D. Wythe
Quoted 82 lines · Wenjia Zhang
Quoted 65 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

Quoted 10 lines

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

I think here two Problems need to be clarified:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

No, the logic of the inference is very vague to me. My understand is completely different. That is what I read from the kernel code. They are not *completely* exclusive, because while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock. (This is actually my main point of my argument for these fixes, and I didn't see any clarify from you). However, while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock, this is just like what you showed in the code in lock_sock. Once it gets the ownership, it release the spinlock.

“ while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock,  ”

You already got that, so why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently ?

Quoted 19 lines
Quoted 19 lines

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(), then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 1 line

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it, but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

2. If the queue_work returns true, the work should be or is being executed

Quoted 7 lines
Quoted 6 lines

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Best wishes,
D. Wythe

If you say the thread is not gauranteed to be waken up in then queue_work to execute the work, please explain what the kick_pool function does.

I never said that.

Quoted 28 lines

However, the spin_lock understanding is still the key problem in the cases. As I said, if it is not get clarify, we don't really need to go on to disucss this.

Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
 cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
#36 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 95 lines · D. Wythe
Quoted 92 lines · Wenjia Zhang
Quoted 89 lines · D. Wythe
Quoted 85 lines · Wenjia Zhang
Quoted 65 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

Quoted 10 lines

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

I think here two Problems need to be clarified:

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

No, the logic of the inference is very vague to me. My understand is completely different. That is what I read from the kernel code. They are not *completely* exclusive, because while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock. (This is actually my main point of my argument for these fixes, and I didn't see any clarify from you). However, while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock, this is just like what you showed in the code in lock_sock. Once it gets the ownership, it release the spinlock.

“ while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock,  ”

You already got that, so why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently ?

Then I'd ask how do you understand this sentence I wrote? "while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock."

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(), then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 1 line

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it, but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

2. If the queue_work returns true, the work should be or is being executed

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Best wishes,
D. Wythe

If you say the thread is not gauranteed to be waken up in then queue_work to execute the work, please explain what the kick_pool function does.

I never said that.

What do you understand on the kick_pool there?

Quoted 29 lines
Quoted 28 lines

However, the spin_lock understanding is still the key problem in the cases. As I said, if it is not get clarify, we don't really need to go on to disucss this.

Quoted 25 lines
Quoted 24 lines
Quoted 24 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Fixes 42bfba9eaa33 ("net/smc: immediate termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +2 -1
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void smc_close_cancel_work(struct 
 smc_sock *smc)
       struct sock *sk = &smc->sk;
         release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
 cancel_delayed_work_sync(&smc->conn.tx_work);
       lock_sock(sk);
   }
maintainer
#37 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 130 lines · Wenjia Zhang
Quoted 127 lines · D. Wythe
Quoted 109 lines · Wenjia Zhang
Quoted 102 lines · D. Wythe
Quoted 90 lines · Wenjia Zhang
Quoted 78 lines · D. Wythe
Quoted 54 lines · Wenjia Zhang
Quoted 49 lines · D. Wythe
Quoted 7 lines · Wenjia Zhang
Quoted 4 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Note that we always hold a reference to sock when attempting to submit close_work.

Quoted 8 lines

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already
released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 2 lines

Best wishes,
D. Wythe

Quoted 9 lines

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution,
the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

smc_close_cancel_work
smc_cdc_msg_recv_action

Quoted 7 lines

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution sock_put()                        //  need to put it
since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

Quoted 8 lines

I think here two Problems need to be clarified:

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Quoted 4 lines

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

Quoted 11 lines

No, the logic of the inference is very vague to me. My understand is completely different. That is what I read from the kernel code. They are not *completely* exclusive, because while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock. (This is actually my main point of my argument for these fixes, and I didn't see any clarify from you). However, while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock, this is just like what you showed in the code in lock_sock. Once it gets the ownership, it release the spinlock.

“ while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock,  ”

Quoted 7 lines
Quoted 3 lines

You already got that, so why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently ?

Then I'd ask how do you understand this sentence I wrote? "while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock."

That's correct, but the reverse is not true. i.e. if the process context hold the lock, the botton half context can still acquire the lock.

Best regards,
Dust

Quoted 80 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(),
then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 4 lines

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it,
but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

2. If the queue_work returns true, the work should be or is being executed

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Best wishes,
D. Wythe

If you say the thread is not gauranteed to be waken up in then queue_work to execute the work, please explain what the kick_pool function does.

I never said that.

What do you understand on the kick_pool there?

Quoted 33 lines
Quoted 32 lines

However, the spin_lock understanding is still the key problem in the cases. As I said, if it is not get clarify, we don't really need to go on to disucss this.

Quoted 27 lines
Quoted 26 lines
Quoted 26 lines
Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 23 lines
Fixes 42bfba9eaa33 ("net/smc: immediate

termination for SMCD link groups")

---
  net/smc/smc_close.c | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
net/smc/smc_close.c +0 -0
index 449ef45..10219f5 100644
@@ -116,7 +116,8 @@ static void

smc_close_cancel_work(struct smc_sock
*smc)
      struct sock *sk = &smc->sk;
        release_sock(sk);
-    cancel_work_sync(&smc->conn.close_work);
+    if (cancel_work_sync(&smc->conn.close_work))
+        sock_put(sk);
cancel_delayed_work_sync(&smc->conn.tx_work);
      lock_sock(sk);
  }

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#39 lore raw

Re: [PATCH net 5/5] net/smc: put sk reference if close work was canceled

Quoted 111 lines · D. Wythe
Quoted 108 lines · Wenjia Zhang
Quoted 104 lines · D. Wythe
Quoted 88 lines · Wenjia Zhang
Quoted 85 lines · D. Wythe
Quoted 82 lines · Wenjia Zhang
Quoted 65 lines · D. Wythe
Quoted 51 lines · Wenjia Zhang
Quoted 48 lines · D. Wythe
Quoted 5 lines · Wenjia Zhang
Quoted 2 lines · D. Wythe

From: "D. Wythe" <[email protected]>

Quoted 9 lines
Quoted 2 lines

Note that we always hold a reference to sock when attempting to submit close_work.

yes
Therefore, if we have successfully

Quoted 3 lines

canceled close_work from pending, we MUST release that reference to avoid potential leaks.

Isn't the corresponding reference already released inside the smc_close_passive_work()?

Hi Wenjia,

If we successfully cancel the close work from the pending state, it means that smc_close_passive_work() has never been executed.

You can find more details here.

/**
* cancel_work_sync - cancel a work and wait for it to finish
* @work:the work to cancel
*
* Cancel @work and wait for its execution to finish. This function
* can be used even if the work re-queues itself or migrates to
* another workqueue. On return from this function, @work is
* guaranteed to be not pending or executing on any CPU. *
* cancel_work_sync(&delayed_work->work) must not be used for
* delayed_work's. Use cancel_delayed_work_sync() instead. *
* The caller must ensure that the workqueue on which @work was last
* queued can't be destroyed before this function returns. *
* Return:
* %true if @work was pending, %false otherwise.
*/
boolcancel_work_sync(structwork_struct *work)
{
return__cancel_work_timer(work, false);
}

Quoted 4 lines
Quoted 2 lines

Best wishes,
D. Wythe

As I understand, queue_work() would wake up the work if the work is not already on the queue. And the sock_hold() is just prio to the queue_work(). That means, cancel_work_sync() would cancel the work either before its execution or after. If your fix refers to the former case, at this moment, I don't think the reference can be hold, thus it is unnecessary to put it.

Quoted 1 line

I am quite confuse about why you think when we cancel the work before its execution, the reference can not be hold ?

Perhaps the following diagram can describe the problem in better way :

Quoted 10 lines

smc_close_cancel_work
smc_cdc_msg_recv_action

sock_hold
queue_work
if (cancel_work_sync())        // successfully cancel before execution
sock_put()                        //  need to put it since we already hold a ref before   queue_work()

ha, I already thought you might ask such question:P

I think here two Problems need to be clarified:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

1) Do you think the bh_lock_sock/bh_unlock_sock in the smc_cdc_msg_recv does not protect the smc_cdc_msg_recv_action() from cancel_work_sync()? Maybe that would go back to the discussion in the other patch on the behaviors of the locks.

Yes. bh_lock_sock/bh_unlock_sock can not block code execution protected by lock_sock/unlock(). That is to say, they are not exclusive.

No, the logic of the inference is very vague to me. My understand is completely different. That is what I read from the kernel code. They are not *completely* exclusive, because while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock. (This is actually my main point of my argument for these fixes, and I didn't see any clarify from you). However, while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock, this is just like what you showed in the code in lock_sock. Once it gets the ownership, it release the spinlock.

“ while the process context holds the lock by lock_sock, the bottom half context can still get it by bh_lock_sock,  ”

You already got that, so why that sock_set_flag(DONE) and sock_set_flag(DEAD) can not happen concurrently ?

Quoted 8 lines
Quoted 2 lines

Then I'd ask how do you understand this sentence I wrote? "while the bottom half context holds the lock i.e. bh_lock_sock, the process context can not get the lock by lock_sock."

Quoted 1 line

That's also true.  I have no questions on it.  They are asymmetrical.

But we cannot guarantee that the interrupt context always holds the lock before the process context, that's why i think that sock_set_flag(DONE) and sock_set_flag(DEAD) can run concurrently.

ok, I have to agree with that. I did too much focus on this case :(
So I think the approach of the 1st patch is also appropriate. Thank you for taking time to let me out!

Quoted 42 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

We can use a very simple example to infer that since bh_lock_sock is type of spin-lock, if bh_lock_sock/bh_unlock_sock can block lock_sock/unlock(), then lock_sock/unlock() can also block bh_lock_sock/bh_unlock_sock.

If this is true, when the process context already lock_sock(), the interrupt context must wait for the process to call release_sock(). Obviously, this is very unreasonable.

Quoted 1 line

2) If the queue_work returns true, as I said in the last main, the work should be (being) executed. How could the cancel_work_sync() cancel the work before execution successgully?

No, that's not true. In fact, if queue_work returns true, it simply means that we have added the task to the queue and may schedule a worker to execute it, but it does not guarantee that the task will be executed or is being executed when it returns true,
the task might still in the list and waiting some worker to execute it.

We can make a simple inference,

1. A known fact is that if no special flag (WORK_UNBOUND) is given, tasks submitted will eventually be executed on the CPU where they were submitted.

2. If the queue_work returns true, the work should be or is being executed

If all of the above are true, when we invoke queue_work in an interrupt context, does it mean that the submitted task will be executed in the interrupt context?

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Best wishes,
D. Wythe

If you say the thread is not gauranteed to be waken up in then queue_work to execute the work, please explain what the kick_pool function does.

I never said that.

What do you understand on the kick_pool there?

I think this simple logic-code graph can totally explain my point of view in clear.

My key point is queue_work can not guarantee the work_1 is executed or being executed, the work_1 might still be in the list ( before executed ) .

The kick_pool() might wake up the 'a_idle_worker' from schedule(), and then the work_1 can be executed soon. But we can not said that the  work_1 is already executed or being executed.

Quoted 7 lines

In fact, we can invoke cancel_work_syn() to delete the work_1 from the list to avoid to be executed, when the a_idle_worker_main has not delete(or pop) the work_1 yet.

Besides, there is a upper limit to the number of idle workers. If the current number of work_x being executed exceeds this number, the work_1 must wait until there are idle_workers available. In that case, we can not said that the  work_1 is already executed or being executed as well.

I do agree with this explaination. My thought was that cancel_work_syn() deleting the work_1 from the list to avoid to be executed would rarely happen, as I was focusing the scenario above. Since we have the agreement on the locks now, I agree that would happen.

Thanks again!
Here you are:

#40 lore raw
In reply to D. Wythe

The subject of the thread says 'smc-r', but some of the changes affect smc-d alike, don't they?

Quoted 8 lines · D. Wythe

From: "D. Wythe" <[email protected]>

This patches contains bugfix following:

1. hung state
2. sock leak
3. potential panic

I may be helpful for the reviewers, when you point out, which patch fixes which problem.

Were they all found by code reviews?
Or did some occur in real life? If so, then what were the symptoms?
A description of the symptoms is helpful for somebody who is debugging and wants to check whether the issue was already fixed upstream.

author maintainer
#41 lore raw
Quoted 2 lines · Alexandra Winter

The subject of the thread says 'smc-r', but some of the changes affect smc-d alike, don't they?

Yes, sorry for this mistake, it should be bugfix for smc.

Quoted 11 lines · D. Wythe
Quoted 8 lines

From: "D. Wythe" <[email protected]>

This patches contains bugfix following:

1. hung state
2. sock leak
3. potential panic

Quoted 6 lines

I may be helpful for the reviewers, when you point out, which patch fixes which problem.

Were they all found by code reviews?
Or did some occur in real life? If so, then what were the symptoms?
A description of the symptoms is helpful for somebody who is debugging and wants to check whether the issue was already fixed upstream.

Hi Alexandra,

Except for the issue with the barrier, which was feedback from the review, all other issues have actually occurred in our environment and have been verified through internal testing. However, most of these issues are caused by reference leakage rather than panic, so it is difficult to provide a representative phenomenon. But what you said is do necessary, so I will post some phenomena in the next version, such as

lsmod | grep smc
or
smcss - a

In that case, we can found  the issues of reference residue or the connection residue. Hope it can be helpful to you.

Thanks,
D. Wythe