From: Bart Van Assche <bvanassche@acm.org>
To: Ulf Hansson <ulfh@kernel.org>
Cc: linux-mmc@vger.kernel.org,
"Adrian Hunter" <adrian.hunter@intel.com>,
"Christian Löhle" <CLoehle@hyperstone.com>,
linux-kernel@vger.kernel.org,
"Bart Van Assche" <bvanassche@acm.org>,
stable@vger.kernel.org
Subject: [PATCH v2] mmc: block: Wait for idle before switching partitions
Date: Mon, 30 Nov 2026 11:02:38 -0800 [thread overview]
Message-ID: <fab32f0412c96a10668e9c116abc19abc86710fc.1796065294.git.bvanassche@acm.org> (raw)
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").
In addition, if a pipelined or synchronous request passes the initial
part_curr check while the previous request is still in flight and that
previous request's error recovery subsequently calls mmc_blk_reset()
and fails during the wait, mmc_blk_rw_wait() still returns 0 and the
waiting request is issued without switching back to the requested
partition.
Fix both issues by:
- Waiting for idle before calling mmc_blk_part_switch() inside the
part_curr != part_type check in mmc_blk_mq_issue_rq(), so CMD6 is
never sent concurrently with an in-flight request or error recovery
and the hot path is unaffected.
- Calling mmc_blk_part_switch() again after mmc_blk_wait_for_idle() in
the MMC_ISSUE_SYNC path and after mmc_blk_rw_wait() in
mmc_blk_mq_issue_rw_rq(), so a partition change from the previous
request's error recovery is handled before issuing the new request.
- Annotating concurrent accesses to part_curr with READ_ONCE() and
WRITE_ONCE().
Fixes: 406e14808ee6 ("mmc: block: Remove error check of hw_reset on reset")
Cc: stable@vger.kernel.org
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
Changes compared to v1: fixed Sashiko findings.
drivers/mmc/core/block.c | 42 +++++++++++++++++++++++++++++++++++-----
1 file changed, 37 insertions(+), 5 deletions(-)
diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c
index a02bb2fee874..e9d355819961 100644
--- a/drivers/mmc/core/block.c
+++ b/drivers/mmc/core/block.c
@@ -947,7 +947,7 @@ static inline int mmc_blk_part_switch(struct mmc_card *card,
ret = mmc_blk_part_switch_post(card, main_md->part_curr);
}
- main_md->part_curr = part_type;
+ WRITE_ONCE(main_md->part_curr, part_type);
return ret;
}
@@ -1062,7 +1062,8 @@ static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
* upon failure it might not be, so set it to MMC_BLK_PART_INVALID
* in that case.
*/
- main_md->part_curr = err ? MMC_BLK_PART_INVALID : main_md->part_type;
+ WRITE_ONCE(main_md->part_curr,
+ err ? MMC_BLK_PART_INVALID : main_md->part_type);
if (err)
return err;
/* Ensure we switch back to the correct partition */
@@ -2319,6 +2320,19 @@ static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
if (err)
goto out_post_req;
+ /*
+ * Recovery of the previous request may have reset the card and
+ * changed part_curr while we were waiting.
+ */
+ err = mmc_blk_part_switch(mq->card, mq->blkdata->part_type);
+ if (err) {
+ if (prev_req)
+ mmc_blk_mq_post_req(mq, prev_req, true);
+ if (err == -EBUSY)
+ err = -EIO;
+ goto out_post_req;
+ }
+
mq->rw_wait = true;
err = mmc_start_request(host, &mqrq->brq.mrq);
@@ -2353,17 +2367,35 @@ 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;
- ret = mmc_blk_part_switch(card, md->part_type);
- if (ret)
- return MMC_REQ_FAILED_TO_START;
+ /*
+ * 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(READ_ONCE(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;
+ }
switch (mmc_issue_type(mq, req)) {
case MMC_ISSUE_SYNC:
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;
switch (req_op(req)) {
case REQ_OP_DRV_IN:
case REQ_OP_DRV_OUT:
reply other threads:[~2026-11-30 19:02 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fab32f0412c96a10668e9c116abc19abc86710fc.1796065294.git.bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=CLoehle@hyperstone.com \
--cc=adrian.hunter@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mmc@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=ulfh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®