Skip to contentSkip to conversation

mmc: sdhci: Clear unused bounce buffer at DMA mmap error path

PATCH Superseded Takashi Iwai 2 participants 3 messages Jun 11, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: sdhci: Clear unused bounce buffer at DMA mmap error path

When DMA-mapping of the bounce buffer fails, the driver tries to fall back, but it leaves the allocated host->bounce_buffer although its size is zero. Later on, the driver checks the use of bounce buffer with host->bounce_buffer pointer, and it tries to use the buffer incorrectly, resulting in Oops.

This patch clears the release the unused buffer and clears the bounce_buffer pointer for addressing the problem.

---
 drivers/mmc/host/sdhci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mmc/host/sdhci.c +5 -1
index bf238ade1602..5f467b98ca88 100644
@@ -4072,9 +4072,13 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
 					   bounce_size,
 					   DMA_BIDIRECTIONAL);
 	ret = dma_mapping_error(mmc_dev(mmc), host->bounce_addr);
-	if (ret)
+	if (ret) {
+		devm_kfree(mmc->parent, host->bounce_buffer);
+		host->bounce_buffer = NULL;
 		/* Again fall back to max_segs == 1 */
 		return;
+	}
+
 	host->bounce_buffer_size = bounce_size;
 
 	/* Lie about this since we're bouncing */
Signature · 1 line
2.26.2
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Takashi Iwai

When DMA-mapping of the bounce buffer fails, the driver tries to fall back, but it leaves the allocated host->bounce_buffer although its size is zero. Later on, the driver checks the use of bounce buffer with host->bounce_buffer pointer, and it tries to use the buffer incorrectly, resulting in Oops.

This patch clears the release the unused buffer and clears the bounce_buffer pointer for addressing the problem.

One minor comment below, otherwise:

Quoted 9 lines
---
 drivers/mmc/host/sdhci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mmc/host/sdhci.c +0 -0
index bf238ade1602..5f467b98ca88 100644

drivers/mmc/host/sdhci.c static void sdhci_allocate_bounce_buffer(struct sdhci_host *host) · line 4076

Quoted 6 lines
drivers/mmc/host/sdhci.c +2 -1
@@ -4072,9 +4072,13 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
 					   bounce_size,
 					   DMA_BIDIRECTIONAL);
 	ret = dma_mapping_error(mmc_dev(mmc), host->bounce_addr);
-	if (ret)
+	if (ret) {
+		devm_kfree(mmc->parent, host->bounce_buffer);

Everywhere else in this function mmc_dev(mmc) is used, so maybe use it here too instead of mmc->parent.

Quoted 9 lines
drivers/mmc/host/sdhci.c +3 -0
+		host->bounce_buffer = NULL;
 		/* Again fall back to max_segs == 1 */
 		return;
+	}
+
 	host->bounce_buffer_size = bounce_size;
 
 	/* Lie about this since we're bouncing */
#2 lore raw
Quoted 21 lines · Adrian Hunter
Quoted 10 lines · Takashi Iwai

When DMA-mapping of the bounce buffer fails, the driver tries to fall back, but it leaves the allocated host->bounce_buffer although its size is zero. Later on, the driver checks the use of bounce buffer with host->bounce_buffer pointer, and it tries to use the buffer incorrectly, resulting in Oops.

This patch clears the release the unused buffer and clears the bounce_buffer pointer for addressing the problem.

One minor comment below, otherwise:

Quoted 4 lines
---
 drivers/mmc/host/sdhci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 14 lines

drivers/mmc/host/sdhci.c static void sdhci_allocate_bounce_buffer(struct sdhci_host *host) · line 4076

Quoted 11 lines
drivers/mmc/host/sdhci.c +2 -1
index bf238ade1602..5f467b98ca88 100644
@@ -4072,9 +4072,13 @@ static void sdhci_allocate_bounce_buffer(struct sdhci_host *host)
 					   bounce_size,
 					   DMA_BIDIRECTIONAL);
 	ret = dma_mapping_error(mmc_dev(mmc), host->bounce_addr);
-	if (ret)
+	if (ret) {
+		devm_kfree(mmc->parent, host->bounce_buffer);

Everywhere else in this function mmc_dev(mmc) is used, so maybe use it here too instead of mmc->parent.

Doh, right, I wrote a patch based on the older kernel and refreshed afterwards, but I forgot correcting this place. Will resubmit the fixed version.

thanks,

Takashi