Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Artem Novak <artem122novak@gmail.com>
To: wahrenst@gmx.net
Cc: bcm-kernel-feedback-list@broadcom.com,
	florian.fainelli@broadcom.com, hemanth.selam@gmail.com,
	john.garry@linux.dev, kraxel@redhat.com,
	linux-arm-kernel@lists.infradead.org, linux-mmc@vger.kernel.org,
	linux-rpi-kernel@lists.infradead.org, rjui@broadcom.com,
	sbranden@broadcom.com, ulfh@kernel.org,
	Artem Novak <artem122novak@gmail.com>
Subject: [PATCH v2] mmc: bcm2835: restore scatterlist after DMA setup failure
Date: Sun, 22 Nov 2026 01:58:14 -0600	[thread overview]
Message-ID: <20261122075814.162099-1-artem122novak@gmail.com> (raw)
In-Reply-To: <fcfc5d38-2755-445c-8444-bd4c5b9c524f@gmx.net>

The DMA preparation path shortens the final scatterlist entry
to reserve space for FIFO drain words.

If DMA mapping or descriptor preparation fails, the request
falls back to PIO, but the shortened scatterlist entry is left
unchanged. As a result, the PIO transfer may omit part of the
data, potentially causing corrupted or incomplete I/O.

Restore the entry's original length and clear drain_words
before returning to the fallback path.

This issue was found while investigating a kernel panic on
my Raspberry Pi server.

Fixes: 660fc733bd74 ("mmc: bcm2835: Add new driver for the sdhost controller.")
Signed-off-by: Artem Novak <artem122novak@gmail.com>
---
 drivers/mmc/host/bcm2835.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/mmc/host/bcm2835.c b/drivers/mmc/host/bcm2835.c
index efd1903c3ccd..49a975e984df 100644
--- a/drivers/mmc/host/bcm2835.c
+++ b/drivers/mmc/host/bcm2835.c
@@ -449,6 +449,8 @@ static
 void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
 {
 	int sg_len, dir_data, dir_slave;
+	struct scatterlist *trimmed_sg = NULL;
+	unsigned int original_len = 0;
 	struct dma_async_tx_descriptor *desc = NULL;
 	struct dma_chan *dma_chan;
 
@@ -480,6 +482,8 @@ void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
 		for_each_sg(data->sg, sg, data->sg_len, i) {
 			if (sg_is_last(sg)) {
 				WARN_ON(sg->length < len);
+				trimmed_sg = sg;
+				original_len = sg->length;
 				sg->length -= len;
 				host->drain_page = sg_page(sg);
 				host->drain_offset = sg->offset + sg->length;
@@ -495,14 +499,21 @@ void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
 				     &host->dma_cfg_tx);
 
 	sg_len = dma_map_sg(host->dma_dev, data->sg, data->sg_len, dir_data);
-	if (!sg_len)
+	if (!sg_len) {
+		if (trimmed_sg)
+			trimmed_sg->length = original_len;
+		host->drain_words = 0;
 		return;
+	}
 
 	desc = dmaengine_prep_slave_sg(dma_chan, data->sg, sg_len, dir_slave,
 				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 
 	if (!desc) {
 		dma_unmap_sg(host->dma_dev, data->sg, data->sg_len, dir_data);
+		if (trimmed_sg)
+			trimmed_sg->length = original_len;
+		host->drain_words = 0;
 		return;
 	}
 
-- 
2.56.1



  reply	other threads:[~2026-11-22  7:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14  5:10 [PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure Artem Novak
2026-11-14  9:50 ` Stefan Wahren
2026-11-14 18:16   ` Artem Novak
2026-11-14 18:32     ` Stefan Wahren
2026-11-22  7:58       ` Artem Novak [this message]
2026-11-22 12:39         ` [PATCH v2] " Stefan Wahren

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=20261122075814.162099-1-artem122novak@gmail.com \
    --to=artem122novak@gmail.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=hemanth.selam@gmail.com \
    --cc=john.garry@linux.dev \
    --cc=kraxel@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=rjui@broadcom.com \
    --cc=sbranden@broadcom.com \
    --cc=ulfh@kernel.org \
    --cc=wahrenst@gmx.net \
    /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