Skip to contentSkip to conversation

net/smc: Fix for race in smc link group termination

RFC PATCH Superseded Wen Gu 2 participants 6 messages Dec 28, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wen Gu

author reviewer
#0 lore raw

cover net/smc: Fix for race in smc link group termination

We encountered some crashes recently and they are caused by the race between the access and free of link/link group in smc link group termination. The crashes can be reproduced in frequent abnormal link group termination, like set RNICs up/down.

This set of patches tries to fix this by extending the life cycle of link/link group to ensure that they won't be referred to after cleared or freed.

Best wishes,
Wen Gu

Wen Gu (2):
net/smc: Resolve the race between link group access and termination net/smc: Resolve the race between SMC-R link access and clear

 net/smc/smc.h      |  1 +
 net/smc/smc_core.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++------
 net/smc/smc_core.h |  7 +++++
 3 files changed, 79 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.3.1
author reviewer
#1 lore raw

RFC PATCH 1/2 net/smc: Resolve the race between link group access and termination

We encountered some crashes caused by the race between the access and the termination of link groups.

Here are some of panic stacks we met:

1) Race between smc_clc_wait_msg() and __smc_lgr_terminate()

 BUG: kernel NULL pointer dereference, address: 00000000000002f0
 Workqueue: smc_hs_wq smc_listen_work [smc]
 RIP: 0010:smc_clc_wait_msg+0x3eb/0x5c0 [smc]
 Call Trace:
  <TASK>
  ? smc_clc_send_accept+0x45/0xa0 [smc]
  ? smc_clc_send_accept+0x45/0xa0 [smc]
  smc_listen_work+0x783/0x1220 [smc]
  ? finish_task_switch+0xc4/0x2e0
  ? process_one_work+0x1ad/0x3c0
  process_one_work+0x1ad/0x3c0
  worker_thread+0x4c/0x390
  ? rescuer_thread+0x320/0x320
  kthread+0x149/0x190
  ? set_kthread_struct+0x40/0x40
  ret_from_fork+0x1f/0x30
  </TASK>

smc_listen_work() abnormal case like port error
---------------------------------------------------------------

                                | __smc_lgr_terminate()
                                |     |- smc_conn_kill()
                                |            |- smc_lgr_unregister_conn()
                                |                   |- set conn->lgr = NULL
smc_clc_wait_msg()              |
    |- access conn->lgr (panic) |

2) Race between smc_setsockopt() and __smc_lgr_terminate()

 BUG: kernel NULL pointer dereference, address: 00000000000002e8
 RIP: 0010:smc_setsockopt+0x17a/0x280 [smc]
 Call Trace:
  <TASK>
  __sys_setsockopt+0xfc/0x190
  __x64_sys_setsockopt+0x20/0x30
  do_syscall_64+0x34/0x90
  entry_SYSCALL_64_after_hwframe+0x44/0xae
  </TASK>

smc_setsockopt() abnormal case like port error
--------------------------------------------------------------

                                | __smc_lgr_terminate()
                                |     |- smc_conn_kill()
                                |            |- smc_lgr_unregister_conn()
                                |                   |- set conn->lgr = NULL
mod_delayed_work()              |
    |- access conn->lgr (panic) |

There are some other panic points and they are caused by the simmilar reason as described above, which is accessing link group after termination, thus getting a NULL pointer or invalid resource.

Currently, there seems to be no synchronization between the link group access and a sudden termination of it. This patch tries to fix this by introducing reference count of link group and not freeing link group until reference count is zero.

Link group might be referred to by link or smc connection. So the operation to the link group reference count can be concluded as follows:

object [hold or initialized as 1] [put]
--------------------------------------------------------------------

link group      smc_lgr_create()                   smc_lgr_free()
connections     smc_lgr_register_conn()            smc_conn_free()
links           smcr_link_init()                   smcr_link_clear()

Througth this way, we extend the life cycle of link group and ensure it is longer than the life cycle of connections and links above it, so that avoid invalid access to link group after its termination.

---
 net/smc/smc.h      |  1 +
 net/smc/smc_core.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 net/smc/smc_core.h |  3 +++
 3 files changed, 44 insertions(+), 5 deletions(-)
net/smc/smc.h +1 -0
index 1a4fc1c..3d0b8e3 100644
@@ -221,6 +221,7 @@ struct smc_connection {
 						 */
 	u64			peer_token;	/* SMC-D token of peer */
 	u8			killed : 1;	/* abnormal termination */
+	u8			freed : 1;	/* normal termiation */
 	u8			out_of_sync : 1; /* out of sync with peer */
 };
 
net/smc/smc_core.c +40 -5

109 lines not on this page · raw message

net/smc/smc_core.h +3 -0
index d63b082..51203b1 100644
@@ -249,6 +249,7 @@ struct smc_link_group {
 	u8			terminating : 1;/* lgr is terminating */
 	u8			freeing : 1;	/* lgr is being freed */
 
+	refcount_t		refcnt;		/* lgr reference count */
 	bool			is_smcd;	/* SMC-R or SMC-D */
 	u8			smc_version;
 	u8			negotiated_eid[SMC_MAX_EID_LEN];
@@ -470,6 +471,8 @@ static inline void smc_set_pci_values(struct pci_dev *pci_dev,
 
 void smc_lgr_cleanup_early(struct smc_connection *conn);
 void smc_lgr_terminate_sched(struct smc_link_group *lgr);
+void smc_lgr_hold(struct smc_link_group *lgr);
+void smc_lgr_put(struct smc_link_group *lgr);
 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport);
 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport);
 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid,
Signature · 1 line
1.8.3.1
author reviewer
#2 lore raw

RFC PATCH 2/2 net/smc: Resolve the race between SMC-R link access and clear

In reply to Wen Gu

We encountered some crashes caused by the race between SMC-R link access and link clear triggered by link group termination in abnormal case, like port error.

Here are some of panic stacks we met:

1) Race between smc_llc_flow_initiate() and smcr_link_clear()

 BUG: kernel NULL pointer dereference, address: 0000000000000000
 Workqueue: smc_hs_wq smc_listen_work [smc]
 RIP: 0010:smc_llc_flow_initiate+0x44/0x190 [smc]
 Call Trace:
  <TASK>
  ? __smc_buf_create+0x75a/0x950 [smc]
  smcr_lgr_reg_rmbs+0x2a/0xbf [smc]
  smc_listen_work+0xf72/0x1230 [smc]
  ? process_one_work+0x25c/0x600
  process_one_work+0x25c/0x600
  worker_thread+0x4f/0x3a0
  ? process_one_work+0x600/0x600
  kthread+0x15d/0x1a0
  ? set_kthread_struct+0x40/0x40
  ret_from_fork+0x1f/0x30
  </TASK>

smc_listen_work() __smc_lgr_terminate()
---------------------------------------------------------------

                                       | smc_lgr_free()
                                       |     |- smcr_link_clear()
                                       |            |- memset(lnk, 0)
smc_listen_rdma_reg()                  |
  |- smcr_lgr_reg_rmbs()               |
       |- smc_llc_flow_initiate()      |
            |- access lnk->lgr (panic) |

2) Race between smc_wr_tx_dismiss_slots() and smcr_link_clear()

 BUG: kernel NULL pointer dereference, address: 0000000000000000
 RIP: 0010:_find_first_bit+0x8/0x50
 Call Trace:
  <TASK>
  smc_wr_tx_dismiss_slots+0x34/0xc0 [smc]
  ? smc_cdc_tx_filter+0x10/0x10 [smc]
  smc_conn_free+0xd8/0x100 [smc]
  __smc_release+0xf1/0x140 [smc]
  smc_release+0x89/0x1b0 [smc]
  __sock_release+0x37/0xb0
  sock_close+0x14/0x20
  __fput+0xa9/0x260
  task_work_run+0x6b/0xb0
  do_exit+0x3ef/0xd40
  do_group_exit+0x47/0xb0
  __x64_sys_exit_group+0x14/0x20
  do_syscall_64+0x34/0x90
  entry_SYSCALL_64_after_hwframe+0x44/0xae
  </TASK>

smc_conn_free() __smc_lgr_terminate()
----------------------------------------------------------------

                                         | smc_lgr_free()
                                         |  |- smcr_link_clear()
                                         |      |- smc_wr_free_link_mem()
                                         |          |- lnk->wr_tx_mask = NULL;
smc_wr_tx_dismiss_slots()                |
  |- for_each_set_bit(link->wr_tx_mask)  |
            |- (panic)                   |

These crashes are caused by clearing SMC-R link resources when someone is still accessing to them. So this patch tries to fix it by introducing reference count of SMC-R links and ensuring that the sensitive resources of links are not cleared until reference count is zero.

The operation to the SMC-R link reference count can be concluded as follows:

object [hold or initialized as 1] [put]
--------------------------------------------------------------------

links           smcr_link_init()                   smcr_link_clear()
connections     smcr_lgr_conn_assign_link()        smc_conn_free()

Through this way, the clear of SMC-R links is later than the free of all the smc connections above it, thus avoiding the unsafe reference to SMC-R links.

---
 net/smc/smc_core.c | 43 +++++++++++++++++++++++++++++++++++--------
 net/smc/smc_core.h |  4 ++++
 2 files changed, 39 insertions(+), 8 deletions(-)
net/smc/smc_core.c +35 -8
index d72eb13..83a80e6 100644
@@ -155,6 +155,7 @@ static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
 	if (!conn->lnk)
 		return SMC_CLC_DECL_NOACTLINK;
 	atomic_inc(&conn->lnk->conn_cnt);
+	smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */
 	return 0;
 }
 
@@ -746,6 +747,8 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	}
 	get_device(&lnk->smcibdev->ibdev->dev);
 	atomic_inc(&lnk->smcibdev->lnk_cnt);
+	refcount_set(&lnk->refcnt, 1); /* link refcnt is set to 1 */
+	lnk->clearing = 0;
 	lnk->path_mtu = lnk->smcibdev->pattr[lnk->ibport - 1].active_mtu;
 	lnk->link_id = smcr_next_link_id(lgr);
 	lnk->lgr = lgr;
@@ -994,8 +997,12 @@ void smc_switch_link_and_count(struct smc_connection *conn,
 			       struct smc_link *to_lnk)
 {
 	atomic_dec(&conn->lnk->conn_cnt);
+	/* put old link, hold in smcr_lgr_conn_assign_link() */
+	smcr_link_put(conn->lnk);
 	conn->lnk = to_lnk;
 	atomic_inc(&conn->lnk->conn_cnt);
+	/* hold new link, put in smc_conn_free() */
+	smcr_link_hold(conn->lnk);
 }
 
 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
@@ -1126,9 +1133,9 @@ void smc_conn_free(struct smc_connection *conn)
 		/* smc connection wasn't registered to a link group
 		 * or has already been freed before.
 		 *
-		 * Judge these to ensure that lgr refcnt will be put
-		 * only once if connection has been registered to a
-		 * link group successfully.
+		 * Judge these to ensure that lgr/link refcnt will be
+		 * put only once if connection has been registered to
+		 * a link group successfully.
 		 */
 		return;
 
@@ -1153,6 +1160,8 @@ void smc_conn_free(struct smc_connection *conn)
 	if (!lgr->conns_num)
 		smc_lgr_schedule_free_work(lgr);
 lgr_put:
+	if (!lgr->is_smcd)
+		smcr_link_put(conn->lnk); /* link_hold in smcr_lgr_conn_assign_link() */
 	smc_lgr_put(lgr); /* lgr_hold in smc_lgr_register_conn() */
 }
 
@@ -1209,13 +1218,23 @@ static void smcr_rtoken_clear_link(struct smc_link *lnk)
 	}
 }
 
+void __smcr_link_clear(struct smc_link *lnk)
+{
+	smc_wr_free_link_mem(lnk);
+	smc_lgr_put(lnk->lgr);	/* lgr_hold in smcr_link_init() */
+	memset(lnk, 0, sizeof(struct smc_link));
+	lnk->state = SMC_LNK_UNUSED;
+}
+
 /* must be called under lgr->llc_conf_mutex lock */
 void smcr_link_clear(struct smc_link *lnk, bool log)
 {
 	struct smc_ib_device *smcibdev;
 
-	if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
+	if (lnk->clearing || !lnk->lgr ||
+	    lnk->state == SMC_LNK_UNUSED)
 		return;
+	lnk->clearing = 1;
 	lnk->peer_qpn = 0;
 	smc_llc_link_clear(lnk, log);
 	smcr_buf_unmap_lgr(lnk);
@@ -1224,15 +1243,23 @@ void smcr_link_clear(struct smc_link *lnk, bool log)
 	smc_wr_free_link(lnk);
 	smc_ib_destroy_queue_pair(lnk);
 	smc_ib_dealloc_protection_domain(lnk);
-	smc_wr_free_link_mem(lnk);
-	smc_lgr_put(lnk->lgr); /* lgr_hold in smcr_link_init() */
 	smc_ibdev_cnt_dec(lnk);
 	put_device(&lnk->smcibdev->ibdev->dev);
 	smcibdev = lnk->smcibdev;
-	memset(lnk, 0, sizeof(struct smc_link));
-	lnk->state = SMC_LNK_UNUSED;
 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
 		wake_up(&smcibdev->lnks_deleted);
+	smcr_link_put(lnk); /* theoretically last link_put */
+}
+
+void smcr_link_hold(struct smc_link *lnk)
+{
+	refcount_inc(&lnk->refcnt);
+}
+
+void smcr_link_put(struct smc_link *lnk)
+{
+	if (refcount_dec_and_test(&lnk->refcnt))
+		__smcr_link_clear(lnk);
 }
 
 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
net/smc/smc_core.h +4 -0
index 51203b1..e73217f 100644
@@ -137,6 +137,8 @@ struct smc_link {
 	u8			peer_link_uid[SMC_LGR_ID_SIZE]; /* peer uid */
 	u8			link_idx;	/* index in lgr link array */
 	u8			link_is_asym;	/* is link asymmetric? */
+	u8			clearing : 1;	/* link is being cleared */
+	refcount_t		refcnt;		/* link reference count */
 	struct smc_link_group	*lgr;		/* parent link group */
 	struct work_struct	link_down_wrk;	/* wrk to bring link down */
 	char			ibname[IB_DEVICE_NAME_MAX]; /* ib device name */
@@ -504,6 +506,8 @@ void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 		   u8 link_idx, struct smc_init_info *ini);
 void smcr_link_clear(struct smc_link *lnk, bool log);
+void smcr_link_hold(struct smc_link *lnk);
+void smcr_link_put(struct smc_link *lnk);
 void smc_switch_link_and_count(struct smc_connection *conn,
 			       struct smc_link *to_lnk);
 int smcr_buf_map_lgr(struct smc_link *lnk);
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [RFC PATCH net 2/2] net/smc: Resolve the race between SMC-R link access and clear

reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Wen,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on net/master]

url:    https://github.com/0day-ci/linux/commits/Wen-Gu/net-smc-Fix-for-race-in-smc-link-group-termination/20211228-172133
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net.git 79b69a83705e621b258ac6d8ae6d3bfdb4b930aa
config: i386-randconfig-a012-20211228 (https://download.01.org/0day-ci/archive/20211228/202112281902.funtfcTC-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 7171af744543433ac75b232eb7dfdaef7efd4d7a)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/effcb39cf4a6a501f01235f3ba3420c8736ce18a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Wen-Gu/net-smc-Fix-for-race-in-smc-link-group-termination/20211228-172133 git checkout effcb39cf4a6a501f01235f3ba3420c8736ce18a
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash net/smc/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> net/smc/smc_core.c:1221:6: warning: no previous prototype for function '__smcr_link_clear' [-Wmissing-prototypes]
   void __smcr_link_clear(struct smc_link *lnk)
        ^

net/smc/smc_core.c:1221:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __smcr_link_clear(struct smc_link *lnk)
^
static
1 warning generated.

vim +/__smcr_link_clear +1221 net/smc/smc_core.c

1220

> 1221	void __smcr_link_clear(struct smc_link *lnk)

1222 {
1223 smc_wr_free_link_mem(lnk);
1224 smc_lgr_put(lnk->lgr); /* lgr_hold in smcr_link_init() */
1225 memset(lnk, 0, sizeof(struct smc_link));
1226 lnk->state = SMC_LNK_UNUSED;
1227 }
1228

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

Re: [RFC PATCH net 2/2] net/smc: Resolve the race between SMC-R link access and clear

In reply to Wen Gu
reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/effcb39cf4a6a501f01235f3ba3420c8736ce18a git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…

Hi Wen,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on net/master]

url:    https://github.com/0day-ci/linux/commits/Wen-Gu/net-smc-Fix-for-race-in-smc-link-group-termination/20211228-172133
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net.git 79b69a83705e621b258ac6d8ae6d3bfdb4b930aa
config: i386-randconfig-r006-20211228 (https://download.01.org/0day-ci/archive/20211228/202112281939.XfmBhts7-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/effcb39cf4a6a501f01235f3ba3420c8736ce18a
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Wen-Gu/net-smc-Fix-for-race-in-smc-link-group-termination/20211228-172133 git checkout effcb39cf4a6a501f01235f3ba3420c8736ce18a
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash net/smc/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> net/smc/smc_core.c:1221:6: warning: no previous prototype for '__smcr_link_clear' [-Wmissing-prototypes]
    1221 | void __smcr_link_clear(struct smc_link *lnk)
         |      ^~~~~~~~~~~~~~~~~

vim +/__smcr_link_clear +1221 net/smc/smc_core.c

1220

> 1221	void __smcr_link_clear(struct smc_link *lnk)

1222 {
1223 smc_wr_free_link_mem(lnk);
1224 smc_lgr_put(lnk->lgr); /* lgr_hold in smcr_link_init() */
1225 memset(lnk, 0, sizeof(struct smc_link));
1226 lnk->state = SMC_LNK_UNUSED;
1227 }
1228

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author reviewer
#5 lore raw

Re: [RFC PATCH net 2/2] net/smc: Resolve the race between SMC-R link access and clear

In reply to Wen Gu

There is an auto build test WARNING in this patch, function __smcr_link_clear() should be declared as 'static'. I will send a v2 of this RFC patch set.

Quoted 222 lines · Wen Gu

We encountered some crashes caused by the race between SMC-R link access and link clear triggered by link group termination in abnormal case, like port error.

Here are some of panic stacks we met:

1) Race between smc_llc_flow_initiate() and smcr_link_clear()

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  Workqueue: smc_hs_wq smc_listen_work [smc]
  RIP: 0010:smc_llc_flow_initiate+0x44/0x190 [smc]
  Call Trace:
   <TASK>
   ? __smc_buf_create+0x75a/0x950 [smc]
   smcr_lgr_reg_rmbs+0x2a/0xbf [smc]
   smc_listen_work+0xf72/0x1230 [smc]
   ? process_one_work+0x25c/0x600
   process_one_work+0x25c/0x600
   worker_thread+0x4f/0x3a0
   ? process_one_work+0x600/0x600
   kthread+0x15d/0x1a0
   ? set_kthread_struct+0x40/0x40
   ret_from_fork+0x1f/0x30
   </TASK>

smc_listen_work() __smc_lgr_terminate()
---------------------------------------------------------------

                                        | smc_lgr_free()
                                        |     |- smcr_link_clear()
                                        |            |- memset(lnk, 0)
smc_listen_rdma_reg()                  |
   |- smcr_lgr_reg_rmbs()               |
        |- smc_llc_flow_initiate()      |
             |- access lnk->lgr (panic) |

2) Race between smc_wr_tx_dismiss_slots() and smcr_link_clear()

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  RIP: 0010:_find_first_bit+0x8/0x50
  Call Trace:
   <TASK>
   smc_wr_tx_dismiss_slots+0x34/0xc0 [smc]
   ? smc_cdc_tx_filter+0x10/0x10 [smc]
   smc_conn_free+0xd8/0x100 [smc]
   __smc_release+0xf1/0x140 [smc]
   smc_release+0x89/0x1b0 [smc]
   __sock_release+0x37/0xb0
   sock_close+0x14/0x20
   __fput+0xa9/0x260
   task_work_run+0x6b/0xb0
   do_exit+0x3ef/0xd40
   do_group_exit+0x47/0xb0
   __x64_sys_exit_group+0x14/0x20
   do_syscall_64+0x34/0x90
   entry_SYSCALL_64_after_hwframe+0x44/0xae
   </TASK>

smc_conn_free() __smc_lgr_terminate()
----------------------------------------------------------------

                                          | smc_lgr_free()
                                          |  |- smcr_link_clear()
                                          |      |- smc_wr_free_link_mem()
                                          |          |- lnk->wr_tx_mask = NULL;
smc_wr_tx_dismiss_slots()                |
   |- for_each_set_bit(link->wr_tx_mask)  |
             |- (panic)                   |

These crashes are caused by clearing SMC-R link resources when someone is still accessing to them. So this patch tries to fix it by introducing reference count of SMC-R links and ensuring that the sensitive resources of links are not cleared until reference count is zero.

The operation to the SMC-R link reference count can be concluded as follows:

object [hold or initialized as 1] [put]
--------------------------------------------------------------------

links           smcr_link_init()                   smcr_link_clear()
connections     smcr_lgr_conn_assign_link()        smc_conn_free()

Through this way, the clear of SMC-R links is later than the free of all the smc connections above it, thus avoiding the unsafe reference to SMC-R links.

---
  net/smc/smc_core.c | 43 +++++++++++++++++++++++++++++++++++--------
  net/smc/smc_core.h |  4 ++++
  2 files changed, 39 insertions(+), 8 deletions(-)
net/smc/smc_core.c +35 -8
index d72eb13..83a80e6 100644
@@ -155,6 +155,7 @@ static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
  	if (!conn->lnk)
  		return SMC_CLC_DECL_NOACTLINK;
  	atomic_inc(&conn->lnk->conn_cnt);
+	smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */
  	return 0;
  }
 
@@ -746,6 +747,8 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
  	}
  	get_device(&lnk->smcibdev->ibdev->dev);
  	atomic_inc(&lnk->smcibdev->lnk_cnt);
+	refcount_set(&lnk->refcnt, 1); /* link refcnt is set to 1 */
+	lnk->clearing = 0;
  	lnk->path_mtu = lnk->smcibdev->pattr[lnk->ibport - 1].active_mtu;
  	lnk->link_id = smcr_next_link_id(lgr);
  	lnk->lgr = lgr;
@@ -994,8 +997,12 @@ void smc_switch_link_and_count(struct smc_connection *conn,
  			       struct smc_link *to_lnk)
  {
  	atomic_dec(&conn->lnk->conn_cnt);
+	/* put old link, hold in smcr_lgr_conn_assign_link() */
+	smcr_link_put(conn->lnk);
  	conn->lnk = to_lnk;
  	atomic_inc(&conn->lnk->conn_cnt);
+	/* hold new link, put in smc_conn_free() */
+	smcr_link_hold(conn->lnk);
  }
 
  struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
@@ -1126,9 +1133,9 @@ void smc_conn_free(struct smc_connection *conn)
  		/* smc connection wasn't registered to a link group
  		 * or has already been freed before.
  		 *
-		 * Judge these to ensure that lgr refcnt will be put
-		 * only once if connection has been registered to a
-		 * link group successfully.
+		 * Judge these to ensure that lgr/link refcnt will be
+		 * put only once if connection has been registered to
+		 * a link group successfully.
  		 */
  		return;
 
@@ -1153,6 +1160,8 @@ void smc_conn_free(struct smc_connection *conn)
  	if (!lgr->conns_num)
  		smc_lgr_schedule_free_work(lgr);
  lgr_put:
+	if (!lgr->is_smcd)
+		smcr_link_put(conn->lnk); /* link_hold in smcr_lgr_conn_assign_link() */
  	smc_lgr_put(lgr); /* lgr_hold in smc_lgr_register_conn() */
  }
 
@@ -1209,13 +1218,23 @@ static void smcr_rtoken_clear_link(struct smc_link *lnk)
  	}
  }
 
+void __smcr_link_clear(struct smc_link *lnk)
+{
+	smc_wr_free_link_mem(lnk);
+	smc_lgr_put(lnk->lgr);	/* lgr_hold in smcr_link_init() */
+	memset(lnk, 0, sizeof(struct smc_link));
+	lnk->state = SMC_LNK_UNUSED;
+}
+
  /* must be called under lgr->llc_conf_mutex lock */
  void smcr_link_clear(struct smc_link *lnk, bool log)
  {
  	struct smc_ib_device *smcibdev;
 
-	if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
+	if (lnk->clearing || !lnk->lgr ||
+	    lnk->state == SMC_LNK_UNUSED)
  		return;
+	lnk->clearing = 1;
  	lnk->peer_qpn = 0;
  	smc_llc_link_clear(lnk, log);
  	smcr_buf_unmap_lgr(lnk);
@@ -1224,15 +1243,23 @@ void smcr_link_clear(struct smc_link *lnk, bool log)
  	smc_wr_free_link(lnk);
  	smc_ib_destroy_queue_pair(lnk);
  	smc_ib_dealloc_protection_domain(lnk);
-	smc_wr_free_link_mem(lnk);
-	smc_lgr_put(lnk->lgr); /* lgr_hold in smcr_link_init() */
  	smc_ibdev_cnt_dec(lnk);
  	put_device(&lnk->smcibdev->ibdev->dev);
  	smcibdev = lnk->smcibdev;
-	memset(lnk, 0, sizeof(struct smc_link));
-	lnk->state = SMC_LNK_UNUSED;
  	if (!atomic_dec_return(&smcibdev->lnk_cnt))
  		wake_up(&smcibdev->lnks_deleted);
+	smcr_link_put(lnk); /* theoretically last link_put */
+}
+
+void smcr_link_hold(struct smc_link *lnk)
+{
+	refcount_inc(&lnk->refcnt);
+}
+
+void smcr_link_put(struct smc_link *lnk)
+{
+	if (refcount_dec_and_test(&lnk->refcnt))
+		__smcr_link_clear(lnk);
  }
 
  static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
net/smc/smc_core.h +4 -0
index 51203b1..e73217f 100644
@@ -137,6 +137,8 @@ struct smc_link {
  	u8			peer_link_uid[SMC_LGR_ID_SIZE]; /* peer uid */
  	u8			link_idx;	/* index in lgr link array */
  	u8			link_is_asym;	/* is link asymmetric? */
+	u8			clearing : 1;	/* link is being cleared */
+	refcount_t		refcnt;		/* link reference count */
  	struct smc_link_group	*lgr;		/* parent link group */
  	struct work_struct	link_down_wrk;	/* wrk to bring link down */
  	char			ibname[IB_DEVICE_NAME_MAX]; /* ib device name */
@@ -504,6 +506,8 @@ void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
  int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
  		   u8 link_idx, struct smc_init_info *ini);
  void smcr_link_clear(struct smc_link *lnk, bool log);
+void smcr_link_hold(struct smc_link *lnk);
+void smcr_link_put(struct smc_link *lnk);
  void smc_switch_link_and_count(struct smc_connection *conn,
  			       struct smc_link *to_lnk);
  int smcr_buf_map_lgr(struct smc_link *lnk);