From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=199.89.1.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796065371; b=MJvtQH6hz1UjvbxJr2MgqcegvzlN1C+DN8OaVEsxoonxWuDYRp2KyUinjrOYj6ccBlvE kyXwEl+umejNy220PFob8OyfZUn6ghBwdxpJSfoT3fEPNRCRUW8vmRV7rbodij/WT9OAF e77wKO7poPrIeQJHRx+0Qvz9ryzUyZBpLUnNeFQ2Q370WXqjIQ3Ta1eIugsQWVmB/0y33 9HYuGcLxyiQpAVFuUhLgogMSfVE0ntb1MPu9V7QHuYSoOrThiPICUjiPYhl9+ESoGEMp4 BtMFgnAQy7US6knLEJgAvWv2fpbQ9tz96GOXrtgzJOfqtSFP0eiRvFcZI070fceMTzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796065371; h=DMARC-Filter:Received:DKIM-Signature:X-Virus-Scanned:Received: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=vR3hwIYcYkurpG1I1E1YPmc2Ds8rt3y+t+eo+Ye5szc=; b=iUhUoPQh+hs4uPZJHGeuRXNsLmO+v1TEK4LMOOlEeqUMzv99Hlq+QrbRxSk3pDrRwGm7 TErEhvsuxVIJgi3od3fZDN1DVt0GLjbqQIhKVqfwNv/0fDTMB1m2sXSLePo3uyiaeNUNZ JuZdsfGMjVsGhc4QcEBHVRkZGIFMRN1B2ROiswSqS1GEBX+uxHPEesRrkb2zHQNzMH7K0 Pvn99yEU00PnG7Zz9m1digzbvgsvQWSxiFTtIh4PLFpQR3yYUIpATg6YdQT/37RjM6AiW GQbWCq4nMLxahxQytWW8duom6mPeZedOfLmw8LvtK56RtDb+HkstMX2j00tfre1PL+Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=acm.org; spf=pass smtp.mailfrom=acm.org; dkim=pass header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=WhfJQFdV; arc=none smtp.remote-ip=199.89.1.16 Received: from 013.lax.mailroute.net (013.lax.mailroute.net [199.89.1.16]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BBFF606A31C; Mon, 30 Nov 2026 19:02:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2BBFF606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=acm.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=acm.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=acm.org header.i=@acm.org header.a=rsa-sha256 header.s=mr01 header.b=WhfJQFdV Received: from localhost (localhost [127.0.0.1]) by 013.lax.mailroute.net (Postfix) with ESMTP id 4jdYLf3PqgzlfvqR; Mon, 30 Nov 2026 19:02:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:mime-version:x-mailer:message-id:date :date:subject:subject:from:from:received:received; s=mr01; t= 1796065365; x=1798657366; bh=vR3hwIYcYkurpG1I1E1YPmc2Ds8rt3y+t+e o+Ye5szc=; b=WhfJQFdV9gdyrcPE9IDNgKpJUfZ2PmcYxRMkOOBcJt2lV8HlyBj FdaXyLCoMeI87tQmasuMKJauIuSkhLSYsrPrS4V3sIdsHXdrNL2hCBBRcNCXmAmj kuOW/uSHpXSD5VLBKAvPw+26/rMQ7zkIkKhdDz9u3w4ojI6sKyDZngTrYwYRDqOR 9JfRQS9a6uuG6ivLHnJJmPWE7jBMMD7TQ/kxltFRDLghJCK18mzm7wR/MoOsR57v 6UNRKLyejw0GmqdTRThr2J9ZjHwIyxdO2mimv+ofaxx/XLfA7uAiH/RI1i9xcrB2 9o9GwD5oY9HaT+v1/2VZmVvnrEuABPKjM0A== X-Virus-Scanned: by MailRoute Received: from 013.lax.mailroute.net ([127.0.0.1]) by localhost (013.lax [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id uBfHFA-PP0Lz; Mon, 30 Nov 2026 19:02:45 +0000 (UTC) Received: from bvanassche.c.googlers.com.com (57.237.82.34.bc.googleusercontent.com [34.82.237.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bvanassche@acm.org) by 013.lax.mailroute.net (Postfix) with ESMTPSA id 4jdYLY3Ys4zlfvqJ; Mon, 30 Nov 2026 19:02:45 +0000 (UTC) From: Bart Van Assche To: Ulf Hansson Cc: linux-mmc@vger.kernel.org, Adrian Hunter , =?UTF-8?q?Christian=20L=C3=B6hle?= , linux-kernel@vger.kernel.org, Bart Van Assche , stable@vger.kernel.org Subject: [PATCH v2] mmc: block: Wait for idle before switching partitions Date: Mon, 30 Nov 2026 11:02:38 -0800 Message-ID: X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 !=3D 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 --- 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 =3D mmc_blk_part_switch_post(card, main_md->part_curr); } =20 - main_md->part_curr =3D part_type; + WRITE_ONCE(main_md->part_curr, part_type); return ret; } =20 @@ -1062,7 +1062,8 @@ static int mmc_blk_reset(struct mmc_blk_data *md, s= truct mmc_host *host, * upon failure it might not be, so set it to MMC_BLK_PART_INVALID * in that case. */ - main_md->part_curr =3D 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; =20 + /* + * Recovery of the previous request may have reset the card and + * changed part_curr while we were waiting. + */ + err =3D 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 =3D=3D -EBUSY) + err =3D -EIO; + goto out_post_req; + } + mq->rw_wait =3D true; =20 err =3D mmc_start_request(host, &mqrq->brq.mrq); @@ -2353,17 +2367,35 @@ enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_qu= eue *mq, struct request *req) struct mmc_blk_data *md =3D mq->blkdata; struct mmc_card *card =3D md->queue.card; struct mmc_host *host =3D card->host; + struct mmc_blk_data *main_md =3D dev_get_drvdata(&card->dev); int ret; =20 - ret =3D 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) !=3D md->part_type)) { + ret =3D mmc_blk_wait_for_idle(mq, host); + if (ret) + return MMC_REQ_BUSY; + ret =3D mmc_blk_part_switch(card, md->part_type); + if (ret) + return MMC_REQ_FAILED_TO_START; + } =20 switch (mmc_issue_type(mq, req)) { case MMC_ISSUE_SYNC: ret =3D mmc_blk_wait_for_idle(mq, host); if (ret) return MMC_REQ_BUSY; + ret =3D 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: