* [PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure
@ 2026-11-14 5:10 Artem Novak
2026-11-14 9:50 ` Stefan Wahren
0 siblings, 1 reply; 6+ messages in thread
From: Artem Novak @ 2026-11-14 5:10 UTC (permalink / raw)
To: Ulf Hansson
Cc: linux-mmc, linux-rpi-kernel, linux-arm-kernel, Florian Fainelli,
bcm-kernel-feedback-list, Ray Jui, Scott Branden, Hemanth Selam,
John Garry, Gerd Hoffmann, Eric Anholt, Stefan Wahren,
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@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.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure
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
0 siblings, 1 reply; 6+ messages in thread
From: Stefan Wahren @ 2026-11-14 9:50 UTC (permalink / raw)
To: Artem Novak, Ulf Hansson
Cc: linux-mmc, linux-rpi-kernel, linux-arm-kernel, Florian Fainelli,
bcm-kernel-feedback-list, Ray Jui, Scott Branden, Hemanth Selam,
John Garry, Gerd Hoffmann
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@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;
> }
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure
2026-11-14 9:50 ` Stefan Wahren
@ 2026-11-14 18:16 ` Artem Novak
2026-11-14 18:32 ` Stefan Wahren
0 siblings, 1 reply; 6+ messages in thread
From: Artem Novak @ 2026-11-14 18:16 UTC (permalink / raw)
To: Stefan Wahren
Cc: Ulf Hansson, linux-mmc, linux-rpi-kernel, linux-arm-kernel,
Florian Fainelli, bcm-kernel-feedback-list, Ray Jui,
Scott Branden, Hemanth Selam, John Garry, Gerd Hoffmann
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@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@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;
> > }
> >
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] mmc: bcm2835: restore scatterlist after DMA setup failure
2026-11-14 18:16 ` Artem Novak
@ 2026-11-14 18:32 ` Stefan Wahren
2026-11-22 7:58 ` [PATCH v2] " Artem Novak
0 siblings, 1 reply; 6+ messages in thread
From: Stefan Wahren @ 2026-11-14 18:32 UTC (permalink / raw)
To: Artem Novak
Cc: Ulf Hansson, linux-mmc, linux-rpi-kernel, linux-arm-kernel,
Florian Fainelli, bcm-kernel-feedback-list, Ray Jui,
Scott Branden, Hemanth Selam, John Garry, Gerd Hoffmann
Hi Artem,
Am 14.11.26 um 19:16 schrieb Artem Novak:
> 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.
please add these important information into the commit message. Just a
hint for the future, please try to avoid top-posting on the kernel
mailing lists.
> 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.
Sorry, I didn't look properly. Please ignore this part.
Thanks
>
> Best regards,
> Artem
>
> On Sat, Nov 14, 2026 at 3:50 AM Stefan Wahren <wahrenst@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@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;
>>> }
>>>
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2] mmc: bcm2835: restore scatterlist after DMA setup failure
2026-11-14 18:32 ` Stefan Wahren
@ 2026-11-22 7:58 ` Artem Novak
2026-11-22 12:39 ` Stefan Wahren
0 siblings, 1 reply; 6+ messages in thread
From: Artem Novak @ 2026-11-22 7:58 UTC (permalink / raw)
To: wahrenst
Cc: bcm-kernel-feedback-list, florian.fainelli, hemanth.selam,
john.garry, kraxel, linux-arm-kernel, linux-mmc, linux-rpi-kernel,
rjui, sbranden, ulfh, 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. 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
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v2] mmc: bcm2835: restore scatterlist after DMA setup failure
2026-11-22 7:58 ` [PATCH v2] " Artem Novak
@ 2026-11-22 12:39 ` Stefan Wahren
0 siblings, 0 replies; 6+ messages in thread
From: Stefan Wahren @ 2026-11-22 12:39 UTC (permalink / raw)
To: Artem Novak
Cc: bcm-kernel-feedback-list, florian.fainelli, hemanth.selam,
john.garry, kraxel, linux-arm-kernel, linux-mmc, linux-rpi-kernel,
rjui, sbranden, ulfh
Am 22.11.26 um 08:58 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. 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>
Reviewed-by: Stefan Wahren <wahrenst@gmx.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-22 12:39 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2] " Artem Novak
2026-11-22 12:39 ` Stefan Wahren
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox