net/smc: fix kernel panic caused by race of smc_sock
Dust Li
maintainercover net/smc: fix kernel panic caused by race of smc_sock
This patchset fixes the race between smc_release triggered by close(2) and cdc_handle triggered by underlaying RDMA device.
The race is caused because the smc_connection may been released before the pending tx CDC messages got its CQEs. In order to fix this, I add a counter to track how many pending WRs we have posted through the smc_connection, and only release the smc_connection after there is no pending WRs on the connection.
The first patch prevents posting WR on a QP that is not in RTS state. This patch is needed because if we post WR on a QP that is not in RTS state, ib_post_send() may success but no CQE will return, and that will confuse the counter tracking the pending WRs.
The second patch add a counter to track how many WRs were posted through the smc_connection, and don't reset the QP on link destroying to prevent leak of the counter.
Dust Li (2):
net/smc: don't send CDC/LLC message if link not ready net/smc: fix kernel panic caused by race of smc_sock
net/smc/smc.h | 5 +++++ net/smc/smc_cdc.c | 52 +++++++++++++++++++++------------------------- net/smc/smc_cdc.h | 2 +- net/smc/smc_core.c | 27 ++++++++++++++++++------ net/smc/smc_core.h | 6 ++++++ net/smc/smc_ib.c | 4 ++-- net/smc/smc_ib.h | 1 + net/smc/smc_llc.c | 2 +- net/smc/smc_wr.c | 45 +++++---------------------------------- net/smc/smc_wr.h | 5 ++--- 10 files changed, 68 insertions(+), 81 deletions(-)
Signature · 1 line
2.19.1.3.ge56e4f7
Dust Li
maintainerPATCH 1/2 net/smc: don't send CDC/LLC message if link not ready
We found smc_llc_send_link_delete_all() sometimes wait for 2s timeout when testing with RDMA link up/down. It is possible when a smc_link is in ACTIVATING state, the underlaying QP is still in RESET or RTR state, which cannot send any messages out.
smc_llc_send_link_delete_all() use smc_link_usable() to checks whether the link is usable, if the QP is still in RESET or RTR state, but the smc_link is in ACTIVATING, this LLC message will always fail without any CQE entering the CQ, and we will always wait 2s before timeout.
Since we cannot send any messages through the QP before the QP enter RTS. I add a wrapper smc_link_sendable() which checks the state of QP along with the link state. And replace smc_link_usable() with smc_link_sendable() in all LLC & CDC message sending routine.
--- net/smc/smc_core.c | 2 +- net/smc/smc_core.h | 6 ++++++ net/smc/smc_llc.c | 2 +- net/smc/smc_wr.c | 4 ++-- net/smc/smc_wr.h | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-)
net/smc/smc_core.c +1 -1
@@ -647,7 +647,7 @@ static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)for (i =0 ; i <SMC_LINKS_PER_LGR_MAX ; i++) {struct smc_link *lnk = &lgr->lnk[i]; -if (smc_link_usable (lnk)) +if (smc_link_sendable (lnk)) lnk->state =SMC_LNK_INACTIVE ; }wake_up_all (&lgr->llc_msg_waiter);
net/smc/smc_core.h +6 -0
@@ -415,6 +415,12 @@ static inline bool smc_link_usable(struct smc_link *lnk)return true; } +static inline bool smc_link_sendable (struct smc_link *lnk) +{ +return smc_link_usable (lnk) && + lnk->qp_attr.cur_qp_state ==IB_QPS_RTS ; +} +static inline bool smc_link_active (struct smc_link *lnk) {return lnk->state ==SMC_LNK_ACTIVE ;
net/smc/smc_llc.c +1 -1
@@ -1630,7 +1630,7 @@ void smc_llc_send_link_delete_all(struct smc_link_group *lgr, bool ord, u32 rsn) delllc.reason =htonl (rsn);for (i =0 ; i <SMC_LINKS_PER_LGR_MAX ; i++) { -if (!smc_link_usable (&lgr->lnk[i])) +if (!smc_link_sendable (&lgr->lnk[i]))continue ;if (!smc_llc_send_message_wait (&lgr->lnk[i], &delllc))break ;
net/smc/smc_wr.c +2 -2
@@ -188,7 +188,7 @@ void smc_wr_tx_cq_handler(struct ib_cq *ib_cq, void *cq_context)static inline int smc_wr_tx_get_free_slot_index (struct smc_link *link,u32 *idx) { *idx = link->wr_tx_cnt; -if (!smc_link_usable (link)) +if (!smc_link_sendable (link))return -ENOLINK ;for_each_clear_bit (*idx, link->wr_tx_mask, link->wr_tx_cnt) {if (!test_and_set_bit (*idx, link->wr_tx_mask))
@@ -231,7 +231,7 @@ int smc_wr_tx_get_free_slot(struct smc_link *link, }else { rc =wait_event_interruptible_timeout ( link->wr_tx_wait, - !smc_link_usable (link) || + !smc_link_sendable (link) || lgr->terminating || (smc_wr_tx_get_free_slot_index (link, &idx) != -EBUSY ),SMC_WR_TX_WAIT_FREE_SLOT_TIME );
net/smc/smc_wr.h +1 -1
@@ -62,7 +62,7 @@ static inline void smc_wr_tx_set_wr_id(atomic_long_t *wr_tx_id, long val)static inline bool smc_wr_tx_link_hold (struct smc_link *link) { -if (!smc_link_usable (link)) +if (!smc_link_sendable (link))return false;atomic_inc (&link->wr_tx_refcnt);return true;
Signature · 1 line
2.19.1.3.ge56e4f7
Re: [PATCH net 1/2] net/smc: don't send CDC/LLC message if link not ready
Quoted 5 lines · Dust Li
We found
smc_llc_send_link_delete_all()sometimes wait for 2s timeout when testing with RDMA link up/down. It is possible when asmc_linkis in ACTIVATING state, the underlaying QP is still in RESET or RTR state, which cannot send any messages out.
I see your point, but why do you needed to introduce a new wrapper instead of extending the existing smc_link_usable() wrapper?
With that and without any comments the reader of the code does not know why there are 2 different functions and what is the reason for having two of them.
dust.li
maintainerRe: [PATCH net 1/2] net/smc: don't send CDC/LLC message if link not ready
Quoted 11 lines · Karsten Graul
Quoted 5 lines · Dust Li
We found
smc_llc_send_link_delete_all()sometimes wait for 2s timeout when testing with RDMA link up/down. It is possible when asmc_linkis in ACTIVATING state, the underlaying QP is still in RESET or RTR state, which cannot send any messages out.I see your point, but why do you needed to introduce a new wrapper instead of extending the existing
smc_link_usable()wrapper?
With that and without any comments the reader of the code does not know why there are 2 different functions and what is the reason for having two of them.
Sorry for that, I should add some comments for those wrappers to better explain that.
The reason we need two wrappers here is because the QP state for the client side is not turned into RTS directly, but seperated into two stages. In the middle on RTR to RTS, we still need smc_link_usable().
For example:
For the client side in first contact, the QP is still in RTR before it receives the LLC_CONFIRM message. So for functions like smc_llc_wait() or smc_llc_event_handler(), we can't use smc_link_sendable(), or we can't even receive LLC_CONFIRM message anymore.
So the meaning for those 2 wrappers should be:
smc_link_usable(): the link is ready to receive RDMA messages smc_link_sendable(): the link is ready to send RDMA messages
For those places that need to send any CDC or LLC message, should go for smc_link_sendable(), otherwise, use smc_link_usable() instead.
I saw David has already applied this to net, should I send another patch to add some comments ?
Re: [PATCH net 1/2] net/smc: don't send CDC/LLC message if link not ready
Quoted 4 lines · dust.li
Quoted 1 line · Karsten Graul
On 28/12/2021 10:03, Dust Li wrote:
I saw David has already applied this to net, should I send another patch to add some comments ?
You could send a follow-on patch with your additional information, which I find is very helpful! Thanks.
Signature · 1 line
Karsten
dust.li
maintainerRe: [PATCH net 1/2] net/smc: don't send CDC/LLC message if link not ready
Quoted 8 lines · Karsten Graul
Quoted 4 lines · dust.li
Quoted 1 line · Karsten Graul
On 28/12/2021 10:03, Dust Li wrote:
I saw David has already applied this to net, should I send another patch to add some comments ?
You could send a follow-on patch with your additional information, which I find is very helpful! Thanks.
Sure, will do
Quoted 3 lines
Signature · 2 lines
-- Karsten
Re: [PATCH net 1/2] net/smc: don't send CDC/LLC message if link not ready
Quoted 11 lines · dust.li
Quoted 8 lines · Karsten Graul
Quoted 4 lines · dust.li
Quoted 1 line · Karsten Graul
On 28/12/2021 10:03, Dust Li wrote:
I saw David has already applied this to net, should I send another patch to add some comments ?
You could send a follow-on patch with your additional information, which I find is very helpful! Thanks.
Sure, will do
Thank you!
Dust Li
maintainerPATCH 1/1 net/smc: add comments for smc_link_{usable|sendable}
In reply to Dust LiAdd comments for both smc_link_sendable() and smc_link_usable() to help better distinguish and use them.
No function changes.
--- net/smc/smc_core.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
net/smc/smc_core.h +16 -1
@@ -407,7 +407,13 @@ static inline struct smc_connection *smc_lgr_find_conn(return res; } -/* returns true if the specified link is usable */ +/* +* Returns true if the specified link is usable. +* +* usable means the link is ready to receive RDMA messages, map memory +* on the link, etc. This doesn't ensure we are able to send RDMA messages +* on this link, if sending RDMA messages is needed, use smc_link_sendable() +*/ static inline bool smc_link_usable (struct smc_link *lnk) {if (lnk->state ==SMC_LNK_UNUSED || lnk->state ==SMC_LNK_INACTIVE )
@@ -415,6 +421,15 @@ static inline bool smc_link_usable(struct smc_link *lnk)return true; } +/* +* Returns true if the specified link is ready to receive AND send RDMA +* messages. +* +* For the client side in first contact, the underlying QP may still in +* RESET or RTR when the link state is ACTIVATING, checks in smc_link_usable() +* is not strong enough. For those places that need to send any CDC or LLC +* messages, use smc_link_sendable(), otherwise, use smc_link_usable() instead +*/ static inline bool smc_link_sendable (struct smc_link *lnk) { return smc_link_usable(lnk) &&
Signature · 1 line
2.19.1.3.ge56e4f7
Re: [PATCH net] net/smc: add comments for smc_link_{usable|sendable}
This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Quoted 8 lines · On Fri, 31 Dec 2021 14:08:53 +0800 you wrote:
Add comments for both
smc_link_sendable()andsmc_link_usable()to help better distinguish and use them.No function changes.
Signed-off-by Dust Li <[email protected]>[...]
Here is the summary with links:
- [net] net/smc: add comments for smc_link_{usable|sendable}
https://git.kernel.org/netdev/net-next/c/1f52a9380ff1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Dust Li
maintainerPATCH 2/2 net/smc: fix kernel panic caused by race of smc_sock
In reply to Dust LiA crash occurs when smc_cdc_tx_handler() tries to access smc_sock but smc_release() has already freed it.
[ 4570.695099] BUG: unable to handle page fault for address: 000000002eae9e88 [ 4570.696048] #PF: supervisor write access in kernel mode [ 4570.696728] #PF: error_code(0x0002) - not-present page [ 4570.697401] PGD 0 P4D 0 [ 4570.697716] Oops: 0002 [#1] PREEMPT SMP NOPTI [ 4570.698228] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.16.0-rc4+ #111 [ 4570.699013] Hardware name: Alibaba Cloud Alibaba Cloud ECS, BIOS 8c24b4c 04/0 [ 4570.699933] RIP: 0010:_raw_spin_lock+0x1a/0x30 <...> [ 4570.711446] Call Trace: [ 4570.711746] <IRQ> [ 4570.711992] smc_cdc_tx_handler+0x41/0xc0 [ 4570.712470] smc_wr_tx_tasklet_fn+0x213/0x560 [ 4570.712981] ? smc_cdc_tx_dismisser+0x10/0x10 [ 4570.713489] tasklet_action_common.isra.17+0x66/0x140 [ 4570.714083] __do_softirq+0x123/0x2f4 [ 4570.714521] irq_exit_rcu+0xc4/0xf0 [ 4570.714934] common_interrupt+0xba/0xe0
Though smc_cdc_tx_handler() checked the existence of smc connection, smc_release() may have already dismissed and released the smc socket before smc_cdc_tx_handler() further visits it.
smc_cdc_tx_handler() |smc_release()
if (!conn) |
|
|smc_cdc_tx_dismiss_slots()
| smc_cdc_tx_dismisser()
|
|sock_put(&smc->sk) <- last sock_put,
| smc_sock freed
bh_lock_sock(&smc->sk) (panic) |To make sure we won't receive any CDC messages after we free the smc_sock, add a refcount on the smc_connection for inflight CDC message(posted to the QP but haven't received related CQE), and don't release the smc_connection until all the inflight CDC messages haven been done, for both success or failed ones.
Using refcount on CDC messages brings another problem: when the link is going to be destroyed, smcr_link_clear() will reset the QP, which then remove all the pending CQEs related to the QP in the CQ. To make sure all the CQEs will always come back so the refcount on the smc_connection can always reach 0, smc_ib_modify_qp_reset() was replaced by smc_ib_modify_qp_error().
And remove the timeout in smc_wr_tx_wait_no_pending_sends() since we need to wait for all pending WQEs done, or we may encounter use-after- free when handling CQEs.
For IB device removal routine, we need to wait for all the QPs on that device been destroyed before we can destroy CQs on the device, or the refcount on smc_connection won't reach 0 and smc_sock cannot be released.
--- net/smc/smc.h | 5 +++++ net/smc/smc_cdc.c | 52 +++++++++++++++++++++------------------------- net/smc/smc_cdc.h | 2 +- net/smc/smc_core.c | 25 +++++++++++++++++----- net/smc/smc_ib.c | 4 ++-- net/smc/smc_ib.h | 1 + net/smc/smc_wr.c | 41 +++--------------------------------- net/smc/smc_wr.h | 3 +-- 8 files changed, 57 insertions(+), 76 deletions(-)
net/smc/smc.h +5 -0
@@ -185,6 +185,11 @@ struct smc_connection {u16 tx_cdc_seq;/* sequence # for CDC send */ u16 tx_cdc_seq_fin;/* sequence # - tx completed */ spinlock_t send_lock;/* protect wr_sends */ +atomic_t cdc_pend_tx_wr;/* number of pending tx CDC wqe +* - inc when post wqe, +* - dec on polled tx cqe +*/ +wait_queue_head_t cdc_pend_tx_wq;/* wakeup on no cdc_pend_tx_wr*/ struct delayed_work tx_work;/* retry of smc_cdc_msg_send */ u32 tx_off;/* base offset in peer rmb */
net/smc/smc_cdc.c +24 -28
90 lines not on this page · raw message
net/smc/smc_cdc.h +1 -1
@@ -291,7 +291,7 @@ int smc_cdc_get_free_slot(struct smc_connection *conn,struct smc_wr_buf **wr_buf, struct smc_rdma_wr **wr_rdma_buf, struct smc_cdc_tx_pend **pend); -void smc_cdc_tx_dismiss_slots (struct smc_connection *conn); +void smc_cdc_wait_pend_tx_wr (struct smc_connection *conn);int smc_cdc_msg_send (struct smc_connection *conn,struct smc_wr_buf *wr_buf,struct smc_cdc_tx_pend *pend);int smc_cdc_get_slot_and_msg_send (struct smc_connection *conn);
net/smc/smc_core.c +20 -5
@@ -1127,7 +1127,7 @@ void smc_conn_free(struct smc_connection *conn)smc_ism_unset_conn (conn);tasklet_kill (&conn->rx_tsklet); }else { -smc_cdc_tx_dismiss_slots (conn); +smc_cdc_wait_pend_tx_wr (conn);if (current_work () != &conn->abort_work)cancel_work_sync (&conn->abort_work); }
@@ -1204,7 +1204,7 @@ void smcr_link_clear(struct smc_link *lnk, bool log)smc_llc_link_clear (lnk, log);smcr_buf_unmap_lgr (lnk);smcr_rtoken_clear_link (lnk); -smc_ib_modify_qp_reset (lnk); +smc_ib_modify_qp_error (lnk);smc_wr_free_link (lnk);smc_ib_destroy_queue_pair (lnk);smc_ib_dealloc_protection_domain (lnk);
@@ -1336,7 +1336,7 @@ static void smc_conn_kill(struct smc_connection *conn, bool soft)else tasklet_unlock_wait (&conn->rx_tsklet); }else { -smc_cdc_tx_dismiss_slots (conn); +smc_cdc_wait_pend_tx_wr (conn); }smc_lgr_unregister_conn (conn);smc_close_active_abort (smc);
@@ -1459,11 +1459,16 @@ void smc_smcd_terminate_all(struct smcd_dev *smcd)/* Called when an SMCR device is removed or the smc module is unloaded. * If smcibdev is given, all SMCR link groups using this device are terminated. * If smcibdev is NULL, all SMCR link groups are terminated. +* +* We must wait here for QPs been destroyed before we destroy the CQs, +* or we won't received any CQEs and cdc_pend_tx_wr cannot reach 0 thus +* smc_sock cannot be released. */ void smc_smcr_terminate_all (struct smc_ib_device *smcibdev) {struct smc_link_group *lgr, *lg;LIST_HEAD (lgr_free_list); +LIST_HEAD (lgr_linkdown_list);int i;spin_lock_bh (&smc_lgr_list.lock);
@@ -1475,7 +1480,7 @@ void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)list_for_each_entry_safe (lgr, lg, &smc_lgr_list.list, list) {for (i =0 ; i <SMC_LINKS_PER_LGR_MAX ; i++) {if (lgr->lnk[i].smcibdev == smcibdev) -smcr_link_down_cond_sched (&lgr->lnk[i]); +list_move_tail (&lgr->list, &lgr_linkdown_list); } } }
@@ -1487,6 +1492,16 @@ void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)__smc_lgr_terminate (lgr, false); } +list_for_each_entry_safe (lgr, lg, &lgr_linkdown_list, list) { +for (i =0 ; i <SMC_LINKS_PER_LGR_MAX ; i++) { +if (lgr->lnk[i].smcibdev == smcibdev) { +mutex_lock (&lgr->llc_conf_mutex); +smcr_link_down_cond (&lgr->lnk[i]); +mutex_unlock (&lgr->llc_conf_mutex); + } + } + } +if (smcibdev) {if (atomic_read (&smcibdev->lnk_cnt)) wait_event(smcibdev->lnks_deleted,
@@ -1586,7 +1601,6 @@ static void smcr_link_down(struct smc_link *lnk)if (!lgr || lnk->state ==SMC_LNK_UNUSED ||list_empty (&lgr->list))return ; -smc_ib_modify_qp_reset (lnk); to_lnk =smc_switch_conns (lgr, lnk, true);if (!to_lnk) {/* no backup link available */ smcr_link_clear (lnk, true);
@@ -1824,6 +1838,7 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini) conn->local_tx_ctrl.common.type =SMC_CDC_MSG_TYPE ; conn->local_tx_ctrl.len =SMC_WR_TX_SIZE ; conn->urg_state =SMC_URG_READ ; +init_waitqueue_head (&conn->cdc_pend_tx_wq);INIT_WORK (&smc->conn.abort_work, smc_conn_abort_work);if (ini->is_smcd) { conn->rx_off =sizeof (struct smcd_cdc_msg );
net/smc/smc_ib.c +2 -2
@@ -109,12 +109,12 @@ int smc_ib_modify_qp_rts(struct smc_link *lnk) IB_QP_MAX_QP_RD_ATOMIC); } -int smc_ib_modify_qp_reset (struct smc_link *lnk) +int smc_ib_modify_qp_error (struct smc_link *lnk) {struct ib_qp_attr qp_attr;memset (&qp_attr,0 ,sizeof (qp_attr)); - qp_attr.qp_state =IB_QPS_RESET ; + qp_attr.qp_state =IB_QPS_ERR ;return ib_modify_qp (lnk->roce_qp, &qp_attr,IB_QP_STATE ); }
net/smc/smc_ib.h +1 -0
@@ -90,6 +90,7 @@ int smc_ib_create_queue_pair(struct smc_link *lnk);int smc_ib_ready_link (struct smc_link *lnk);int smc_ib_modify_qp_rts (struct smc_link *lnk);int smc_ib_modify_qp_reset (struct smc_link *lnk); +int smc_ib_modify_qp_error (struct smc_link *lnk);long smc_ib_setup_per_ibdev (struct smc_ib_device *smcibdev);int smc_ib_get_memory_region (struct ib_pd *pd,int access_flags,struct smc_buf_desc *buf_slot,u8 link_idx);
net/smc/smc_wr.c +3 -38
72 lines not on this page · raw message
net/smc/smc_wr.h +1 -2
@@ -22,7 +22,6 @@#define SMC_WR_BUF_CNT 16/* # of ctrl buffers per link */ #define SMC_WR_TX_WAIT_FREE_SLOT_TIME (10 * HZ) -#define SMC_WR_TX_WAIT_PENDING_TIME (5 * HZ)#define SMC_WR_TX_SIZE 44/* actual size of wr_send data (<=SMC_WR_BUF_SIZE) */
@@ -130,7 +129,7 @@ void smc_wr_tx_dismiss_slots(struct smc_link *lnk, u8 wr_rx_hdr_type,smc_wr_tx_filter filter, smc_wr_tx_dismisser dismisser, unsigned long data); -int smc_wr_tx_wait_no_pending_sends (struct smc_link *link); +void smc_wr_tx_wait_no_pending_sends (struct smc_link *link);int smc_wr_rx_register_handler (struct smc_wr_rx_handler *handler);int smc_wr_rx_post_init (struct smc_link *link);
Signature · 1 line
2.19.1.3.ge56e4f7
Quoted 2 lines · Dust Li
A crash occurs when
smc_cdc_tx_handler()tries to accesssmc_sockbutsmc_release()has already freed it.
I am not sure about what happened here.
Your patch removes the whole dismisser concept that was introduced to solve exactly the problem you describe. And you implemented a different approach.
In theory, when smc_cdc_tx_handler() is called but the connection is already freed than the connection should have gone through smc_cdc_tx_dismiss_slots(), called by smc_conn_kill() or smc_conn_free(). If that happened there would be no access to an already freed address in smc_cdc_tx_handler().
Can you explain why the code reached smc_cdc_tx_handler() with cdcpend->conn pointing to a connection that is already freed? I think if there is a bug it should be fixed instead of replacing the code by a new construct.
Thoughts?
dust.li
maintainerQuoted 13 lines · Karsten Graul
Quoted 2 lines · Dust Li
A crash occurs when
smc_cdc_tx_handler()tries to accesssmc_sockbutsmc_release()has already freed it.I am not sure about what happened here.
Your patch removes the whole dismisser concept that was introduced to solve exactly the problem you describe. And you implemented a different approach.In theory, when
smc_cdc_tx_handler()is called but the connection is already freed than the connection should have gone throughsmc_cdc_tx_dismiss_slots(), called bysmc_conn_kill()orsmc_conn_free(). If that happened there would be no access to an already freed address insmc_cdc_tx_handler().
Quoted 5 lines
Can you explain why the code reached
smc_cdc_tx_handler()withcdcpend->connpointing to a connection that is already freed? I think if there is a bug it should be fixed instead of replacing the code by a new construct.Thoughts?
Yes, at first we do try to fix this on the original path, but finally failed. that's why we turned into this way.
This bug can be reproduced in our environment pretty fast running the following test:
server:
smc_run nginx
client:
while true; do smc_run wrk -c 1000 -t 4 -d 20 http://smc-server; done
The reason is smc_cdc_tx_handler() checks whether cdcpend->conn == NULL or not, and will access to the connection if it's not NULL. But for short TCP flows(transfered to SMC flow), it is likely to close() the connection very soon. Since smc_cdc_tx_handler() is running in the soft IRQ context, and close(2) is running in the process context. There is a chance of race as describe below:
smc_cdc_tx_handler() |smc_release()
if (!conn) |
|
|smc_cdc_tx_dismiss_slots()
| smc_cdc_tx_dismisser()
|
|sock_put(&smc->sk) <- last sock_put,
| smc_sock freed
bh_lock_sock(&smc->sk) (panic) |If the check at the left passed, and then the application closed the smc_socket and dismissed the cdcpend with smc_connection. smc_cdc_tx_handler() still think the smc_sock is alive but actually it's already released, and further bh_lock_sock(&smc->sk) will cause the kernel panic.
As long as the check in smc_cdc_tx_handler() is not synchronized with the smc_release(), this is inevitable. And unfortunately, I haven't found a good way to synchronized them with the dismisser.
So my final solution is to remove the dissmiser and introduce the refcount for tx cdc messages to protect the visit from smc_cdc_tx_handler() from smc_release().
With the refcount, we can make sure the smc_conn_free() will wait for all pending tx cdc messages done by the underlying RDMA device and smc_cdc_tx_handler() is always safe to visit the smc_sock.
With this patchset, I run the wrk/nginx test cases for 2 days without any panic(No link down/up is performed) any more.
This series was applied to netdev/net.git (master) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net.git (master) by David S. Miller <[email protected]>:
Quoted 10 lines · On Tue, 28 Dec 2021 17:03:23 +0800 you wrote:
This patchset fixes the race between
smc_releasetriggered by close(2) andcdc_handletriggered by underlaying RDMA device.The race is caused because the
smc_connectionmay been released before the pending tx CDC messages got its CQEs. In order to fix this, I add a counter to track how many pending WRs we have posted through thesmc_connection, and only release thesmc_connectionafter there is no pending WRs on the connection.[...]
Here is the summary with links:
- [net,1/2] net/smc: don't send CDC/LLC message if link not ready
https://git.kernel.org/netdev/net/c/90cee52f2e78 - [net,2/2] net/smc: fix kernel panic caused by race of smc_sock https://git.kernel.org/netdev/net/c/349d43127dac
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html