mtip_hw_init() initializes port->svc_wait only after it has requested the interrupt and enabled interrupts on the HBA. The interrupt handler wakes port->svc_wait, e.g. from mtip_process_errors(), so an interrupt that arrives in between wakes an uninitialized waitqueue. Initialize the waitqueue immediately after allocating dd->port, before anything can reach it. Fixes: 60ec0eecfa89 ("mtip32xx: updates based on feedback") Signed-off-by: Bart Van Assche --- drivers/block/mtip32xx/mtip32xx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/block/mtip32xx/mtip32xx.c b/drivers/block/mtip32xx/mtip32xx.c index 0d71abbc334e..aa44b13d506b 100644 --- a/drivers/block/mtip32xx/mtip32xx.c +++ b/drivers/block/mtip32xx/mtip32xx.c @@ -2726,6 +2726,8 @@ static int mtip_hw_init(struct driver_data *dd) if (!dd->port) return -ENOMEM; + init_waitqueue_head(&dd->port->svc_wait); + /* Continue workqueue setup */ for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++) dd->work[i].port = dd->port; @@ -2815,8 +2817,6 @@ static int mtip_hw_init(struct driver_data *dd) writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, dd->mmio + HOST_CTL); - init_waitqueue_head(&dd->port->svc_wait); - if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) { rv = -EFAULT; goto out3; mtip_check_surprise_removal() calls blk_mark_disk_dead() from the interrupt handler, which acquires bd_holder_lock and calls into the holder, which may sleep. Only record the removal in mtip_check_surprise_removal(): replace the unsynchronized bool dd->sr with MTIP_DDF_DEAD_BIT so new commands fail immediately, and wake the service thread with MTIP_PF_SR_BIT. In mtip_exec_internal_command(), reset int_cmd->status before calling blk_execute_rq() and treat a non-zero return value as a failure too, since rejecting a reserved command with BLK_STS_IOERR leaves int_cmd->status untouched. In mtip_handle_surprise_removal(), quiesce the queue, wait for the interrupt handler with synchronize_irq(), flush isr_workq, abort commands in flight through blk_mq_complete_request(), unquiesce the queue, and only then mark the disk dead. Doing this in that order matters because blk_mark_disk_dead() acquires s_umount of a mounted filesystem, whose holder may be waiting for those commands. Clear the tag bit in port->cmds_to_issue before testing blk_mq_request_completed() in mtip_abort_cmd() so no stale bit remains for an already completed request. If MTIP_DDF_REMOVE_PENDING_BIT is set, wait for in-progress mtip_queue_rq() calls with synchronize_rcu() instead of quiescing and unquiescing the queue, and skip blk_mark_disk_dead() because mtip_pci_remove() is already tearing down the disk. Fixes: f55e017c6420 ("block: add a mark_dead holder operation") Reported-by: Josef Bacik Signed-off-by: Jens Axboe Signed-off-by: Bart Van Assche --- drivers/block/mtip32xx/mtip32xx.c | 65 +++++++++++++++++++++++++------ drivers/block/mtip32xx/mtip32xx.h | 9 +++-- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/drivers/block/mtip32xx/mtip32xx.c b/drivers/block/mtip32xx/mtip32xx.c index aa44b13d506b..24cef62136f1 100644 --- a/drivers/block/mtip32xx/mtip32xx.c +++ b/drivers/block/mtip32xx/mtip32xx.c @@ -134,15 +134,17 @@ static bool mtip_check_surprise_removal(struct driver_data *dd) { u16 vendor_id = 0; - if (dd->sr) + if (test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) return true; /* Read the vendorID from the configuration space */ pci_read_config_word(dd->pdev, 0x00, &vendor_id); if (vendor_id == 0xFFFF) { - dd->sr = true; - if (dd->disk) - blk_mark_disk_dead(dd->disk); + set_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag); + if (dd->port) { + set_bit(MTIP_PF_SR_BIT, &dd->port->flags); + wake_up_interruptible(&dd->port->svc_wait); + } return true; /* device removed */ } @@ -998,14 +1000,13 @@ static int mtip_exec_internal_command(struct mtip_port *port, /* Copy the command to the command table */ int_cmd = blk_mq_rq_to_pdu(rq); int_cmd->icmd = &icmd; + int_cmd->status = BLK_STS_OK; memcpy(int_cmd->command, fis, fis_len*4); rq->timeout = timeout; /* insert request and run queue */ - blk_execute_rq(rq, true); - - if (int_cmd->status) { + if (blk_execute_rq(rq, true) || int_cmd->status) { dev_err(&dd->pdev->dev, "Internal command [%02X] failed %d\n", fis->command, int_cmd->status); rv = -EIO; @@ -2439,8 +2440,11 @@ static bool mtip_abort_cmd(struct request *req, void *data) dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag); clear_bit(req->tag, dd->port->cmds_to_issue); + if (blk_mq_request_completed(req)) + return true; + cmd->status = BLK_STS_IOERR; - mtip_softirq_done_fn(req); + blk_mq_complete_request(req); return true; } @@ -2453,6 +2457,31 @@ static bool mtip_queue_cmd(struct request *req, void *data) return true; } +static void mtip_handle_surprise_removal(struct driver_data *dd) +{ + bool removing = test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); + + if (dd->queue) { + if (!removing) + blk_mq_quiesce_queue(dd->queue); + else + synchronize_rcu(); + + synchronize_irq(dd->pdev->irq); + if (dd->isr_workq) + flush_workqueue(dd->isr_workq); + + blk_mq_tagset_busy_iter(&dd->tags, mtip_abort_cmd, dd); + + if (!removing) + blk_mq_unquiesce_queue(dd->queue); + } + if (!removing && + test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) && + !test_and_set_bit(MTIP_DDF_DISK_DEAD_BIT, &dd->dd_flag)) + blk_mark_disk_dead(dd->disk); +} + /* * service thread to issue queued commands * @@ -2492,6 +2521,15 @@ static int mtip_service_thread(void *data) set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); + if (test_and_clear_bit(MTIP_PF_SR_BIT, &port->flags)) { + mtip_handle_surprise_removal(dd); + clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags); + clear_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags); + clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags); + clear_bit(MTIP_PF_REBUILD_BIT, &port->flags); + continue; + } + restart_eh: /* Demux bits: start with error handling */ if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) { @@ -2848,7 +2886,7 @@ static int mtip_standby_drive(struct driver_data *dd) { int rv = 0; - if (dd->sr || !dd->port) + if (test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag) || !dd->port) return -ENODEV; /* * Send standby immediate (E0h) to the drive so that it @@ -2875,7 +2913,7 @@ static int mtip_standby_drive(struct driver_data *dd) */ static int mtip_hw_exit(struct driver_data *dd) { - if (!dd->sr) { + if (!test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) { /* de-initialize the port. */ mtip_deinit_port(dd->port); @@ -3241,6 +3279,8 @@ static inline bool is_stopped(struct driver_data *dd, struct request *rq) return true; if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) return true; + if (test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) + return true; return false; } @@ -3279,6 +3319,9 @@ static blk_status_t mtip_issue_reserved_cmd(struct blk_mq_hw_ctx *hctx, dd->port->command_list + sizeof(struct mtip_cmd_hdr) * rq->tag; struct mtip_cmd_sg *command_sg; + if (test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) + return BLK_STS_IOERR; + if (mtip_commands_active(dd->port)) return BLK_STS_DEV_RESOURCE; @@ -3905,7 +3948,7 @@ static void mtip_pci_remove(struct pci_dev *pdev) kthread_stop(dd->mtip_svc_handler); } - if (!dd->sr) { + if (!test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) { /* * Explicitly wait here for IOs to quiesce, * as mtip_standby_drive usually won't wait for IOs. diff --git a/drivers/block/mtip32xx/mtip32xx.h b/drivers/block/mtip32xx/mtip32xx.h index 0963c07b5845..5bc86363a4c7 100644 --- a/drivers/block/mtip32xx/mtip32xx.h +++ b/drivers/block/mtip32xx/mtip32xx.h @@ -134,11 +134,13 @@ enum { MTIP_PF_ISSUE_CMDS_BIT = 5, MTIP_PF_REBUILD_BIT = 6, MTIP_PF_SVC_THD_STOP_BIT = 8, + MTIP_PF_SR_BIT = 11, /* surprise removal seen */ MTIP_PF_SVC_THD_WORK = ((1 << MTIP_PF_EH_ACTIVE_BIT) | (1 << MTIP_PF_ISSUE_CMDS_BIT) | (1 << MTIP_PF_REBUILD_BIT) | (1 << MTIP_PF_SVC_THD_STOP_BIT) | + (1 << MTIP_PF_SR_BIT) | (1 << MTIP_PF_TO_ACTIVE_BIT)), /* below are bit numbers in 'dd_flag' defined in driver_data */ @@ -150,12 +152,15 @@ enum { MTIP_DDF_RESUME_BIT = 6, MTIP_DDF_INIT_DONE_BIT = 7, MTIP_DDF_REBUILD_FAILED_BIT = 8, + MTIP_DDF_DEAD_BIT = 9, + MTIP_DDF_DISK_DEAD_BIT = 10, MTIP_DDF_STOP_IO = ((1 << MTIP_DDF_REMOVE_PENDING_BIT) | (1 << MTIP_DDF_SEC_LOCK_BIT) | (1 << MTIP_DDF_OVER_TEMP_BIT) | (1 << MTIP_DDF_WRITE_PROTECT_BIT) | - (1 << MTIP_DDF_REBUILD_FAILED_BIT)), + (1 << MTIP_DDF_REBUILD_FAILED_BIT) | + (1 << MTIP_DDF_DEAD_BIT)), }; @@ -449,8 +454,6 @@ struct driver_data { struct dentry *dfs_node; - bool sr; - int numa_node; /* NUMA support */ char workq_name[32]; mtip_pci_remove() relies on blk_mark_disk_dead(), which only stops new IO. del_gendisk() waits for the requests the driver still owns, and nothing completes them for a removed device: the service thread exits as soon as it sees MTIP_DDF_REMOVE_PENDING_BIT. The removal hangs. Keep the service thread running until kthread_stop() after del_gendisk() so it handles a removal seen while the disk is being deleted. Add dd->sr_mutex and hold it across mtip_handle_surprise_removal() to serialize the service thread against mtip_pci_remove(). In mtip_pci_remove(), abort the commands in flight via mtip_handle_surprise_removal() if MTIP_DDF_DEAD_BIT is already set, and otherwise acquire and release dd->sr_mutex before calling del_gendisk() to wait for any in-flight mtip_handle_surprise_removal() call that started before MTIP_DDF_REMOVE_PENDING_BIT was set. Drop MTIP_PF_REBUILD_BIT once removal is pending or mtip_ftl_rebuild_poll() returns <= 0 instead of looping on it. Fixes: e8b58ef09e84 ("mtip32xx: fix device removal") Reported-by: Josef Bacik Signed-off-by: Jens Axboe Signed-off-by: Bart Van Assche --- drivers/block/mtip32xx/mtip32xx.c | 26 ++++++++++++++++++++------ drivers/block/mtip32xx/mtip32xx.h | 1 + 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/block/mtip32xx/mtip32xx.c b/drivers/block/mtip32xx/mtip32xx.c index 24cef62136f1..2b8a60e35d56 100644 --- a/drivers/block/mtip32xx/mtip32xx.c +++ b/drivers/block/mtip32xx/mtip32xx.c @@ -2459,7 +2459,10 @@ static bool mtip_queue_cmd(struct request *req, void *data) static void mtip_handle_surprise_removal(struct driver_data *dd) { - bool removing = test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); + bool removing; + + guard(mutex)(&dd->sr_mutex); + removing = test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); if (dd->queue) { if (!removing) @@ -2515,10 +2518,6 @@ static int mtip_service_thread(void *data) test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags)) goto st_out; - if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, - &dd->dd_flag))) - goto st_out; - set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); if (test_and_clear_bit(MTIP_PF_SR_BIT, &port->flags)) { @@ -2599,7 +2598,9 @@ static int mtip_service_thread(void *data) } if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) { - if (mtip_ftl_rebuild_poll(dd) == 0) + if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, + &dd->dd_flag) || + mtip_ftl_rebuild_poll(dd) <= 0) clear_bit(MTIP_PF_REBUILD_BIT, &port->flags); } } @@ -3769,6 +3770,7 @@ static int mtip_pci_probe(struct pci_dev *pdev, if (!dd) return -ENOMEM; mutex_init(&dd->ioctl_mutex); + mutex_init(&dd->sr_mutex); /* Attach the private data to this PCI device. */ pci_set_drvdata(pdev, dd); @@ -3937,6 +3939,18 @@ static void mtip_pci_remove(struct pci_dev *pdev) set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); mutex_lock(&dd->ioctl_mutex); + /* + * Wait for any in-flight mtip_handle_surprise_removal() to complete + * before del_gendisk() tears down the queue, and abort any remaining + * requests if surprise removal was already detected. + */ + if (test_bit(MTIP_DDF_DEAD_BIT, &dd->dd_flag)) { + mtip_handle_surprise_removal(dd); + } else { + mutex_lock(&dd->sr_mutex); + mutex_unlock(&dd->sr_mutex); + } + if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) del_gendisk(dd->disk); diff --git a/drivers/block/mtip32xx/mtip32xx.h b/drivers/block/mtip32xx/mtip32xx.h index 5bc86363a4c7..6e8ea053d0b2 100644 --- a/drivers/block/mtip32xx/mtip32xx.h +++ b/drivers/block/mtip32xx/mtip32xx.h @@ -439,6 +439,7 @@ struct driver_data { struct blk_mq_tag_set tags; /* blk_mq tags */ struct mutex ioctl_mutex; + struct mutex sr_mutex; struct mtip_port *port; /* Pointer to the port data structure. */