When a command sent during mwifiex_init_fw() fails, the command response handler recycles the command node. mwifiex_insert_cmd_to_free_q() wakes the thread waiting in mwifiex_send_cmd() before mwifiex_clean_cmd_node() invokes the cmdrsp_complete() callback. The woken firmware callback thread returns an error from mwifiex_init_fw() and takes the error path in _mwifiex_fw_dpc(). That path calls if_ops->unregister_dev() before mwifiex_terminate_workqueue(). For USB, mwifiex_unregister_dev() calls mwifiex_usb_free(), which frees card->rx_cmd.urb and sets it to NULL. If the main workqueue thread has not yet reached mwifiex_clean_cmd_node(), the subsequent cmdrsp_complete() callback, mwifiex_usb_cmd_event_complete(), resubmits the command RX URB. Since rx_cmd_urb_pending is already zero and the URB context still has a valid skb, mwifiex_usb_submit_rx_urb() passes a NULL urb to usb_fill_bulk_urb() and the write of urb->dev faults in the workqueue. Order the teardown so that nothing can touch the RX URBs after they have been released, without destroying the workqueue while URB completion handlers may still queue work on it: - set MWIFIEX_SURPRISE_REMOVED so newly queued main work returns early; - cancel_work_sync() the main work, draining a mwifiex_main_process() that could still resubmit the command RX URB; - shut the firmware down and release the command buffers, which may also recycle a pending command while the URBs are still valid; - unregister the device, which frees the URBs and, for PCIe/SDIO, tears down the interrupt; - destroy the work queues last, after every completion handler has been quiesced by the device unregister. The failure was observed when the firmware returned an error for an initialization command during probe. This change is compile-tested only; the race window was not reproduced on demand. Fixes: f8c095f67962 ("mwifiex: handle race during mwifiex_usb_disconnect") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- v2: - Keep the work queues alive until after unregister_dev() so that URB completion handlers cannot queue work on a destroyed workqueue; drain the main work with cancel_work_sync() and destroy the queues last. drivers/net/wireless/marvell/mwifiex/main.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/net/wireless/marvell/mwifiex/main.c b/drivers/net/wireless/marvell/mwifiex/main.c index c8df3edff675..a3982af50855 100644 --- a/drivers/net/wireless/marvell/mwifiex/main.c +++ b/drivers/net/wireless/marvell/mwifiex/main.c @@ -655,13 +655,9 @@ static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context) if (adapter->if_ops.disable_int) adapter->if_ops.disable_int(adapter); err_dnld_fw: - mwifiex_dbg(adapter, ERROR, - "info: %s: unregister device\n", __func__); - if (adapter->if_ops.unregister_dev) - adapter->if_ops.unregister_dev(adapter); - set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags); - mwifiex_terminate_workqueue(adapter); + /* Wait for a main process that may still resubmit the command RX URB. */ + cancel_work_sync(&adapter->main_work); if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) { pr_debug("info: %s: shutdown mwifiex\n", __func__); @@ -669,6 +665,17 @@ static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context) mwifiex_free_cmd_buffers(adapter); } + mwifiex_dbg(adapter, ERROR, + "info: %s: unregister device\n", __func__); + if (adapter->if_ops.unregister_dev) + adapter->if_ops.unregister_dev(adapter); + + /* + * Destroy the workqueue last: URB completion handlers queue main work, + * so it must stay alive until the device has been unregistered. + */ + mwifiex_terminate_workqueue(adapter); + init_failed = true; done: if (adapter->firmware) { -- 2.34.1