All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [v2] mmc: sh_mmcif: rework sg_miter handling
@ 2026-11-19  9:39 Arnd Bergmann
       [not found] ` <sashiko-outbox-222892@kernel.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2026-11-19  9:39 UTC (permalink / raw)
  To: Wolfram Sang, Ulf Hansson, Linus Walleij
  Cc: Arnd Bergmann, Geert Uytterhoeven, Frank Li, Adrian Hunter,
	Matthew Wilcox (Oracle), Samarth Narang, Pan Chuang, linux-mmc

From: Arnd Bergmann <arnd@arndb.de>

This driver is the only one that uses sg_miter_next() to get a persistent
mapping across interrupts, which prevents us from converting the internal
kmap() to a local mapping using SG_MITER_LOCAL flag.

Linus previously attempted to use atomic mappings here, which failed
for the same reason. I gave it another try, with a mapping that is only
active during a single sh_mmcif_mread_block()/sh_mmcif_mwrite_block() call.

This is mainly a revert of the patch that fixed the command 18 timeout,
but tries to solve the problem by updating and checking data->bytes_xfered
to keep track of when the transfer is finished.

Instead of calling sg_miter_stop() on error or completion, this now
always stops the iterator after the data transfer and restarts it during
the following interrupt.

Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Acked-by: Linus Walleij <linusw@kernel.org>
Fixes: 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
v2: address incorrect accounting problem [sashiko-bot]
---
 drivers/mmc/host/sh_mmcif.c | 72 +++++++++++++++----------------------
 1 file changed, 29 insertions(+), 43 deletions(-)

diff --git a/drivers/mmc/host/sh_mmcif.c b/drivers/mmc/host/sh_mmcif.c
index aa5a19d16c7f..84d91771cd25 100644
--- a/drivers/mmc/host/sh_mmcif.c
+++ b/drivers/mmc/host/sh_mmcif.c
@@ -604,7 +604,7 @@ static void sh_mmcif_single_read(struct sh_mmcif_host *host,
 			   BLOCK_SIZE_MASK) + 3;
 
 	sg_miter_start(&host->sg_miter, data->sg, data->sg_len,
-		       SG_MITER_TO_SG);
+		       SG_MITER_LOCAL | SG_MITER_TO_SG);
 
 	host->wait_for = MMCIF_WAIT_FOR_READ;
 
@@ -621,7 +621,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
 	int i;
 
 	if (host->sd_error) {
-		sg_miter_stop(sgm);
 		data->error = sh_mmcif_error_manage(host);
 		dev_dbg(dev, "%s(): %d\n", __func__, data->error);
 		return false;
@@ -629,7 +628,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
 
 	if (!sg_miter_next(sgm)) {
 		/* This should not happen on single blocks */
-		sg_miter_stop(sgm);
 		return false;
 	}
 
@@ -638,6 +636,8 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
 	for (i = 0; i < host->blocksize / 4; i++)
 		*p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
 
+	data->bytes_xfered += 4 * i;
+	sgm->consumed = 4 * i;
 	sg_miter_stop(&host->sg_miter);
 
 	/* buffer read end */
@@ -650,7 +650,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
 static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
 				struct mmc_request *mrq)
 {
-	struct sg_mapping_iter *sgm = &host->sg_miter;
 	struct mmc_data *data = mrq->data;
 
 	if (!data->sg_len || !data->sg->length)
@@ -659,14 +658,8 @@ static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
 	host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
 		BLOCK_SIZE_MASK;
 
-	sg_miter_start(sgm, data->sg, data->sg_len,
-		       SG_MITER_TO_SG);
-
-	/* Advance to the first sglist entry */
-	if (!sg_miter_next(sgm)) {
-		sg_miter_stop(sgm);
-		return;
-	}
+	sg_miter_start(&host->sg_miter, data->sg, data->sg_len,
+		       SG_MITER_LOCAL | SG_MITER_TO_SG);
 
 	host->wait_for = MMCIF_WAIT_FOR_MREAD;
 
@@ -682,27 +675,26 @@ static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
 	int i;
 
 	if (host->sd_error) {
-		sg_miter_stop(sgm);
 		data->error = sh_mmcif_error_manage(host);
 		dev_dbg(dev, "%s(): %d\n", __func__, data->error);
 		return false;
 	}
 
+	if (!sg_miter_next(sgm))
+		return false;
+
 	p = sgm->addr;
 
 	for (i = 0; i < host->blocksize / 4; i++)
 		*p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
 
-	sgm->consumed = host->blocksize;
+	data->bytes_xfered += 4 * i;
+	sgm->consumed = 4 * i;
+	sg_miter_stop(sgm);
 
 	sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
 
-	if (!sg_miter_next(sgm)) {
-		sg_miter_stop(sgm);
-		return false;
-	}
-
-	return true;
+	return data->bytes_xfered < data->blksz * data->blocks;
 }
 
 static void sh_mmcif_single_write(struct sh_mmcif_host *host,
@@ -714,7 +706,7 @@ static void sh_mmcif_single_write(struct sh_mmcif_host *host,
 			   BLOCK_SIZE_MASK) + 3;
 
 	sg_miter_start(&host->sg_miter, data->sg, data->sg_len,
-		       SG_MITER_FROM_SG);
+		       SG_MITER_LOCAL | SG_MITER_FROM_SG);
 
 	host->wait_for = MMCIF_WAIT_FOR_WRITE;
 
@@ -731,7 +723,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
 	int i;
 
 	if (host->sd_error) {
-		sg_miter_stop(sgm);
 		data->error = sh_mmcif_error_manage(host);
 		dev_dbg(dev, "%s(): %d\n", __func__, data->error);
 		return false;
@@ -739,7 +730,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
 
 	if (!sg_miter_next(sgm)) {
 		/* This should not happen on single blocks */
-		sg_miter_stop(sgm);
 		return false;
 	}
 
@@ -748,6 +738,8 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
 	for (i = 0; i < host->blocksize / 4; i++)
 		sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
 
+	data->bytes_xfered += 4 * i;
+	sgm->consumed = 4 * i;
 	sg_miter_stop(&host->sg_miter);
 
 	/* buffer write end */
@@ -760,7 +752,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
 static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
 				struct mmc_request *mrq)
 {
-	struct sg_mapping_iter *sgm = &host->sg_miter;
 	struct mmc_data *data = mrq->data;
 
 	if (!data->sg_len || !data->sg->length)
@@ -769,14 +760,8 @@ static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
 	host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
 		BLOCK_SIZE_MASK;
 
-	sg_miter_start(sgm, data->sg, data->sg_len,
-		       SG_MITER_FROM_SG);
-
-	/* Advance to the first sglist entry */
-	if (!sg_miter_next(sgm)) {
-		sg_miter_stop(sgm);
-		return;
-	}
+	sg_miter_start(&host->sg_miter, data->sg, data->sg_len,
+		       SG_MITER_LOCAL | SG_MITER_FROM_SG);
 
 	host->wait_for = MMCIF_WAIT_FOR_MWRITE;
 
@@ -792,27 +777,26 @@ static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
 	int i;
 
 	if (host->sd_error) {
-		sg_miter_stop(sgm);
 		data->error = sh_mmcif_error_manage(host);
 		dev_dbg(dev, "%s(): %d\n", __func__, data->error);
 		return false;
 	}
 
+	if (!sg_miter_next(sgm))
+		return false;
+
 	p = sgm->addr;
 
 	for (i = 0; i < host->blocksize / 4; i++)
 		sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
 
-	sgm->consumed = host->blocksize;
-
-	if (!sg_miter_next(sgm)) {
-		sg_miter_stop(sgm);
-		return false;
-	}
+	data->bytes_xfered += 4 * i;
+	sgm->consumed = 4 * i;
+	sg_miter_stop(sgm);
 
 	sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
 
-	return true;
+	return data->bytes_xfered < data->blksz * data->blocks;
 }
 
 static void sh_mmcif_get_response(struct sh_mmcif_host *host,
@@ -1194,6 +1178,8 @@ static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
 			dmaengine_terminate_sync(host->chan_rx);
 		else
 			dmaengine_terminate_sync(host->chan_tx);
+	} else {
+		data->bytes_xfered = data->blksz * data->blocks;
 	}
 
 	/*
@@ -1269,6 +1255,7 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
 		break;
 	case MMCIF_WAIT_FOR_STOP:
 		if (host->sd_error) {
+			mrq->data->bytes_xfered = 0;
 			mrq->stop->error = sh_mmcif_error_manage(host);
 			dev_dbg(dev, "%s(): %d\n", __func__, mrq->stop->error);
 			break;
@@ -1279,6 +1266,7 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
 	case MMCIF_WAIT_FOR_READ_END:
 	case MMCIF_WAIT_FOR_WRITE_END:
 		if (host->sd_error) {
+			mrq->data->bytes_xfered = 0;
 			mrq->data->error = sh_mmcif_error_manage(host);
 			dev_dbg(dev, "%s(): %d\n", __func__, mrq->data->error);
 		}
@@ -1296,9 +1284,6 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
 
 	if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
 		struct mmc_data *data = mrq->data;
-		if (!mrq->cmd->error && data && !data->error)
-			data->bytes_xfered =
-				data->blocks * data->blksz;
 
 		if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
 			sh_mmcif_stop_cmd(host, mrq);
@@ -1397,6 +1382,7 @@ static void sh_mmcif_timeout_work(struct work_struct *work)
 	case MMCIF_WAIT_FOR_WRITE:
 	case MMCIF_WAIT_FOR_READ_END:
 	case MMCIF_WAIT_FOR_WRITE_END:
+		mrq->data->bytes_xfered = 0;
 		mrq->data->error = sh_mmcif_error_manage(host);
 		break;
 	default:
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] [v2] mmc: sh_mmcif: rework sg_miter handling
       [not found] ` <sashiko-outbox-222892@kernel.org>
@ 2026-11-19 15:21   ` Arnd Bergmann
  0 siblings, 0 replies; 2+ messages in thread
From: Arnd Bergmann @ 2026-11-19 15:21 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: Linus Walleij, Geert Uytterhoeven, Wolfram Sang, Ulf Hansson,
	linux-mmc @ vger . kernel . org

On Thu, Nov 19, 2026, at 10:59, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] mmc: sh_mmcif: data->bytes_xfered not reset on multi-block
>   PIO and stop errors
> - [Medium] mmc: sh_mmcif: out-of-bounds sgm->consumed in
>   sh_mmcif_read_block()
> --
>
> commit 65703f98092b0e4db22d64298984b4fb57add099
> Author: Arnd Bergmann <arnd@arndb.de>
>
> mmc: sh_mmcif: rework sg_miter handling
>
> This commit reworks PIO scatterlist mapping in sh_mmcif to use
> SG_MITER_LOCAL, stopping the iterator after each block and tracking
> completion via data->bytes_xfered.
>
>> diff --git a/drivers/mmc/host/sh_mmcif.c b/drivers/mmc/host/sh_mmcif.c
>> index 51be5dc82f6e6..8a0ccab9861d5 100644
>> --- a/drivers/mmc/host/sh_mmcif.c
>> +++ b/drivers/mmc/host/sh_mmcif.c
>> @@ -604,7 +604,7 @@ static void sh_mmcif_single_read(struct sh_mmcif_host *host,
>>  			   BLOCK_SIZE_MASK) + 3;
>>  
>>  	sg_miter_start(&host->sg_miter, data->sg, data->sg_len,
>> -		       SG_MITER_TO_SG);
>> +		       SG_MITER_LOCAL | SG_MITER_TO_SG);
>>  
>>  	host->wait_for = MMCIF_WAIT_FOR_READ;
>>  
>> @@ -621,7 +621,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
>>  	int i;
>>  
>>  	if (host->sd_error) {
>> -		sg_miter_stop(sgm);
>>  		data->error = sh_mmcif_error_manage(host);
>>  		dev_dbg(dev, "%s(): %d\n", __func__, data->error);
>>  		return false;
>> @@ -629,7 +628,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
>>  
>>  	if (!sg_miter_next(sgm)) {
>>  		/* This should not happen on single blocks */
>> -		sg_miter_stop(sgm);
>>  		return false;
>>  	}
>>  
>> @@ -638,6 +636,8 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
>>  	for (i = 0; i < host->blocksize / 4; i++)
>>  		*p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
>>  
>> +	data->bytes_xfered += 4 * i;
>> +	sgm->consumed = 4 * i;
>>  	sg_miter_stop(&host->sg_miter);
>
> [Severity: Medium]
> Can setting sgm->consumed and data->bytes_xfered to 4 * i exceed
> sgm->length on single-block transfers whose data->blksz is not a multiple
> of 4?

I've gone back to reporting data->blksz for the single transfers now,
which I think is what we expect to transfer here. It is a bit odd that
nothing actually compares host->blocksize and data->blksz, maybe
there should be a check here as the loop could otherwise go long
or short and both of those would presumably be bad.

> sh_mmcif_single_read() and sh_mmcif_single_write() add 3 to
> host->blocksize so that host->blocksize / 4 rounds up the 32-bit FIFO word
> count.  When data->blksz is not divisible by 4, 4 * i is larger than
> data->blksz (and sgm->length), which triggers
> WARN_ON(miter->consumed > miter->length) in sg_miter_stop() and
> over-reports data->bytes_xfered in both sh_mmcif_read_block() and
> sh_mmcif_write_block().

Exactly, except that host->blocksize is not even set to data->blksz
in the first place.

>> @@ -1397,6 +1382,7 @@ static void sh_mmcif_timeout_work(struct work_struct *work)
>>  	case MMCIF_WAIT_FOR_WRITE:
>>  	case MMCIF_WAIT_FOR_READ_END:
>>  	case MMCIF_WAIT_FOR_WRITE_END:
>> +		mrq->data->bytes_xfered = 0;
>>  		mrq->data->error = sh_mmcif_error_manage(host);
>>  		break;
>>  	default:
>
> [Severity: Medium]
> Does this leave mrq->data->bytes_xfered non-zero when a multi-block PIO
> transfer hits host->sd_error or when MMCIF_WAIT_FOR_STOP times out?
>
> sh_mmcif_mread_block() and sh_mmcif_mwrite_block() increment
> data->bytes_xfered as each block is read from or written into the
> MMCIF_CE_DATA FIFO, but their host->sd_error paths do not reset
> data->bytes_xfered to 0 if an error interrupt arrives on a later block.
> Likewise, case MMCIF_WAIT_FOR_STOP in sh_mmcif_timeout_work() does not
> reset mrq->data->bytes_xfered to 0, unlike MMCIF_WAIT_FOR_STOP in
> sh_mmcif_irqt().
>
> When data->bytes_xfered remains non-zero on error, mmc_blk_mq_rw_recovery()
> and mmc_blk_mq_complete_rq() can treat those unverified or aborted blocks
> as completed with BLK_STS_OK.

This was intentional on my end: if something has already been transferred
through registers, I would count it. Since I already changed the other
instances, I have changed this one as well now, and just unconditionally
reset 'mrq->data->bytes_xfered = 0' in the timeout handler. No idea if
this is actually better or worse.

     Arnd

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19 15:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  9:39 [PATCH] [v2] mmc: sh_mmcif: rework sg_miter handling Arnd Bergmann
     [not found] ` <sashiko-outbox-222892@kernel.org>
2026-11-19 15:21   ` Arnd Bergmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.