[PATCH] mmc: block: Wait for idle before switching partitions
From: Bart Van Assche
Date: Sun Nov 29 2026 - 20:16:49 EST
mmc_blk_mq_issue_rq() calls mmc_blk_part_switch() before it waits for
the previous request. That is safe as long as part_curr matches the
partition of the request queue since no command is sent in that case.
Since commit 406e14808ee6 ("mmc: block: Remove error check of hw_reset
on reset") mmc_blk_reset() sets part_curr to MMC_BLK_PART_INVALID if
resetting the card failed. From then on every request submitted to the
same queue sends CMD6 from mmc_blk_part_switch() while the previous
request may still be in flight or while the mmc_complete work is still
running error recovery for it. Two contexts then use the host at the
same time, sdhci_send_command() overwrites host->cmd, the completion of
the overwritten command is lost and its submitter sleeps forever in
mmc_wait_for_req_done(). This has been observed with kernel v5.15.167:
WARNING: drivers/mmc/host/sdhci.c:1612 sdhci_send_command+0x7b0/0xf0c
Workqueue: kblockd blk_mq_run_work_fn
sdhci_send_command
sdhci_send_command_retry
sdhci_request
mmc_start_request
mmc_wait_for_req
mmc_wait_for_cmd
__mmc_switch
mmc_switch
mmc_blk_mq_issue_rq
mmc_mq_queue_rq
[ ... ]
INFO: task kworker/0:2H:8115 blocked for more than 122 seconds.
Workqueue: mmc_complete mmc_blk_mq_complete_work
wait_for_completion
mmc_wait_for_req_done
mmc_wait_for_cmd
mmc_blk_fix_state
mmc_blk_mq_rw_recovery
mmc_blk_mq_poll_completion
mmc_blk_mq_complete_work
Kernel panic - not syncing: hung_task: blocked tasks
mmc_blk_reset() also modifies part_curr if resetting the card succeeded:
it sets part_curr to the main partition before switching back to the
partition of the request queue. Hence the same race can happen briefly
for request queues of other partitions, e.g. boot partitions. That
variant has existed since commit 81196976ed94 ("mmc: block: Add blk-mq
support").
Fix this by waiting until the previous request has been completed,
including its error recovery, before switching partitions. Only do this
if a partition switch is required such that the hot path is unaffected.
Note: part_curr is read without holding a lock. If error recovery
modifies part_curr after the new check and before mmc_blk_part_switch()
reads it again, CMD6 is still sent while error recovery is in progress.
Leave that narrow window open since closing it requires waiting
unconditionally, which would slow down the hot path.
Fixes: 406e14808ee6 ("mmc: block: Remove error check of hw_reset on reset")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
drivers/mmc/core/block.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
index a02bb2fee874..726815a7b527 100644
--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -2353,8 +2353,24 @@ enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
struct mmc_blk_data *md = mq->blkdata;
struct mmc_card *card = md->queue.card;
struct mmc_host *host = card->host;
+ struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
int ret;
+ /*
+ * mmc_blk_reset() sets part_curr to MMC_BLK_PART_INVALID if resetting
+ * the card failed and to the main partition if resetting the card
+ * succeeded. In both cases part_curr may differ from the partition of
+ * this queue and the partition switch below sends CMD6 to the card.
+ * Wait until the previous request has completed and its error recovery
+ * has finished before doing so, since the host can only process one
+ * request at a time.
+ */
+ if (unlikely(main_md->part_curr != md->part_type)) {
+ ret = mmc_blk_wait_for_idle(mq, host);
+ if (ret)
+ return MMC_REQ_BUSY;
+ }
+
ret = mmc_blk_part_switch(card, md->part_type);
if (ret)
return MMC_REQ_FAILED_TO_START;