[PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure
Artem Novak
artem122novak at gmail.com
Sat Nov 14 10:16:23 PST 2026
Greetings.
Yes. If DMA setup fails, the request falls back to PIO with a
shortened scatterlist entry.
This can cause the PIO transfer to omit part of the data,
potentially resulting in corrupted or incomplete I/O.
I had a kernel panic while running my Raspberry Pi server,
which made me find this problem.
For trimmed_sg:
I kept trimmed_sg to explicitly track the scatterlist entry whose
length was modified.
original_len alone tells us the previous length, but not which entry to restore.
Best regards,
Artem
On Sat, Nov 14, 2026 at 3:50 AM Stefan Wahren <wahrenst at gmx.net> wrote:
>
> Hi Aartem,
>
> Am 14.11.26 um 06:10 schrieb Artem Novak:
> > 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. Restore its original length and clear drain_words
> > before returning to the fallback path.
> >
> > Fixes: 660fc733bd74 ("mmc: bcm2835: Add new driver for the sdhost controller.")
> > Signed-off-by: Artem Novak <artem122novak at gmail.com>
> are there real potential negative consequences? I'm asking because there
> is a fixes tag.
> > ---
> > 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;
>
> Maybe I missed something, but why do we need trimmed_sg? Can't we just
> check original_len instead?
>
> Best regards
>
> > + 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;
> > }
> >
More information about the linux-arm-kernel
mailing list