From 517d6a19771bf4f3c7b5eb9d5b89d39c7efdacf3 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 26 Sep 2026 02:55:40 +1000 Subject: [PATCH 1/2] nvme-apple: Let post-M4 reset recovery finish while I/O is frozen After the I/O queue is re-created, reset_work unquiesces the I/O queues and waits in nvme_wait_freeze() for the requests that entered before the reset, while holding disable_lock. If one of them times out while the controller is CONNECTING, the timeout handler marks the reset aborted and queues recovery_work, which needs disable_lock to stop the controller and cancel the request, so neither side can proceed. A timeout during removal and apple_nvme_shutdown() also stop the controller under this lock, so they wait behind reset_work as well. Release disable_lock while waiting and take it again before resizing the tag set and publishing LIVE. If the reset was aborted, or the controller was removed or quarantined meanwhile, unfreeze and take the reset failure path. Releasing the lock alone does not let the wait finish. recovery_work stops the controller without unquiescing the queues, and the requests it cancels, which are retryable, are requeued onto the quiesced queues. An aborted initialization gets no follow-up reset, so after its stop, unquiesce the queues under the same lock: the requeued requests then reach the disabled queues and fail with BLK_STS_IOERR, as the PCI driver lets them fail after a timeout while connecting. The failure path removes the controller, so these requests could not have completed anyway. A controller whose stop fails is quarantined and keeps its requests until reboot, so the wait could still never finish. Wait in steps of the I/O timeout, and stop waiting once the controller is quarantined or the reset aborted. The failure path then stops the controller, or leaves a quarantined one as it is. Removing a controller that cannot be stopped still hangs: the quarantine keeps its requests, and del_gendisk() waits for them. That needs a separate decision. Legacy controllers are unchanged. Compile-tested only; not run on hardware. Fixes: 7296c5865423 ("nvme-apple: Serialize post-M4 disable, timeout recovery and reset") Assisted-by: LLM checkpatch sparse Co-authored-by: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 48 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 18b735f023381..5a09ba6cb370e 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -1305,7 +1305,19 @@ static void apple_nvme_recovery_work(struct work_struct *work) */ if (READ_ONCE(anv->quarantined) || READ_ONCE(anv->removing)) goto done; - ret = apple_nvme_disable(anv, false); + mutex_lock(&anv->disable_lock); + ret = apple_nvme_disable_locked(anv, false); + /* + * Without restart, this stop aborts an initialization, and no reset + * follows. The requests it cancelled have been requeued; let them + * reach the stopped queues and fail, so that reset_work does not wait + * for them forever. + */ + if (!ret && !restart) { + nvme_unquiesce_io_queues(&anv->ctrl); + nvme_unquiesce_admin_queue(&anv->ctrl); + } + mutex_unlock(&anv->disable_lock); if (ret) goto done; clear_bit(APPLE_NVME_RECOVERY_PENDING, &anv->recovery_flags); @@ -1486,6 +1498,19 @@ static void apple_nvme_init_queue(struct apple_nvme_queue *q) apple_nvme_enable_queue(q); } +/* + * Wait for the requests that entered before a post-M4 reset. Give up once + * the reset has been aborted, whose failure path stops the controller, or + * the controller has been quarantined, which keeps its requests. + */ +static void apple_nvme_wait_freeze(struct apple_nvme *anv) +{ + while (!nvme_wait_freeze_timeout(&anv->ctrl, NVME_IO_TIMEOUT)) { + if (READ_ONCE(anv->abort_reset) || READ_ONCE(anv->quarantined)) + return; + } +} + static void apple_nvme_reset_work(struct work_struct *work) { unsigned int nr_io_queues = 1; @@ -1793,7 +1818,26 @@ static void apple_nvme_reset_work(struct work_struct *work) anv->ctrl.queue_count = nr_io_queues + 1; nvme_unquiesce_io_queues(&anv->ctrl); - nvme_wait_freeze(&anv->ctrl); + if (phase_locked) { + /* + * Do not hold disable_lock while waiting for the requests that + * entered before the reset: if one of them times out, stopping + * the controller needs the lock. That stop fails the requests, + * unless it quarantines the controller, which + * apple_nvme_wait_freeze() gives up on. + */ + mutex_unlock(&anv->disable_lock); + apple_nvme_wait_freeze(anv); + mutex_lock(&anv->disable_lock); + if (READ_ONCE(anv->quarantined) || READ_ONCE(anv->removing) || + READ_ONCE(anv->abort_reset)) { + nvme_unfreeze(&anv->ctrl); + ret = -ECANCELED; + goto out; + } + } else { + nvme_wait_freeze(&anv->ctrl); + } blk_mq_update_nr_hw_queues(&anv->tagset, 1); nvme_unfreeze(&anv->ctrl); From f5be53a3aa91ac071d155e41e64903713177938c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 26 Sep 2026 02:56:41 +1000 Subject: [PATCH 2/2] nvme-apple: Release disable_lock before nvme_start_ctrl() reset_work publishes LIVE and then calls nvme_start_ctrl() with disable_lock held. When the controller reports supported asynchronous events in OAES, nvme_start_ctrl() sends Set Features (Asynchronous Event Configuration) and waits for it. If that command times out, the controller is LIVE, so the timeout handler moves it to RESETTING and queues recovery_work, which blocks on disable_lock while reset_work waits for the command that recovery_work would cancel. A timeout during removal blocks on the lock the same way. This driver has no submit_async_event() callback, so a controller that reported asynchronous events would also fault in nvme_async_event_work(). The OAES field of the post-M4 controllers has not been read here, though, so do not rely on the command never being sent: release disable_lock once LIVE has been published, before nvme_start_ctrl(). Nothing in nvme_start_ctrl() programs the controller, which is what the lock keeps apart from a stop. After the lock is released, a recovery can stop the controller before nvme_start_ctrl() unquiesces the I/O queues. The unquiesce then undoes the stop's quiesce, and requests fail on the stopped queues until the next reset instead of waiting for it. The PCI driver has the same window. A request can time out in it only while nvme_start_ctrl() waits for the command above: the requests that entered before the reset have completed by then, and later ones have been in flight for less than the I/O timeout. Legacy controllers never take disable_lock and are unchanged. Compile-tested only; not run on hardware. Fixes: 7296c5865423 ("nvme-apple: Serialize post-M4 disable, timeout recovery and reset") Assisted-by: LLM checkpatch sparse Co-authored-by: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/nvme/host/apple.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/nvme/host/apple.c b/drivers/nvme/host/apple.c index 5a09ba6cb370e..f2828beefd4e6 100644 --- a/drivers/nvme/host/apple.c +++ b/drivers/nvme/host/apple.c @@ -1848,11 +1848,19 @@ static void apple_nvme_reset_work(struct work_struct *work) goto out_remove_sq; } + /* + * nvme_start_ctrl() sends Set Features if the controller reports + * asynchronous events. If that times out, the controller is live and + * recovery_work needs disable_lock to stop it. As in pci.c, a stop + * that lands before nvme_start_ctrl() unquiesces the I/O queues loses + * its quiesce, and requests then fail on the stopped queues until the + * next reset instead of waiting for it. + */ + if (phase_locked) + mutex_unlock(&anv->disable_lock); nvme_start_ctrl(&anv->ctrl); dev_dbg(anv->dev, "ANS boot and NVMe init completed."); - if (phase_locked) - mutex_unlock(&anv->disable_lock); return; out_remove_sq: