[PATCH master] mci: core: allocate memory used for DMA with dma_alloc

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Apr 22 23:04:35 PDT 2024


Memory allocated by normal malloc may not fulfill the alignment
requirements for DMA.

This fixes memory corruption observed on the i.MX8MP when the
DMA-enabled eSDHC driver attempts to probe an eMMC.

This issues always existed, but only after commit 65ef5d885263
("ARM64: let 'end' point after the range in cache functions"), the whole
512 bytes were getting invalidated, which corrupted the TLSF malloc
header of the block after it.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/mmc.c         |  9 +++++----
 drivers/mci/mci-core.c | 12 ++++++------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/commands/mmc.c b/commands/mmc.c
index fa01b89cdf42..041a721d3661 100644
--- a/commands/mmc.c
+++ b/commands/mmc.c
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
+#include <dma.h>
 
 static int mmc_enh_area_setmax(struct mci *mci, u8 *ext_csd)
 {
@@ -115,7 +116,7 @@ static int do_mmc_enh_area(int argc, char *argv[])
 	if (ret)
 		goto error;
 
-	free(ext_csd);
+	dma_free(ext_csd);
 
 	if (set_completed) {
 		ret = mmc_partitioning_complete(mci);
@@ -127,7 +128,7 @@ static int do_mmc_enh_area(int argc, char *argv[])
 	return COMMAND_SUCCESS;
 
 error:
-	free(ext_csd);
+	dma_free(ext_csd);
 	return COMMAND_ERROR;
 }
 
@@ -183,12 +184,12 @@ static int do_mmc_write_reliability(int argc, char *argv[])
 		}
 	}
 
-	free(ext_csd);
+	dma_free(ext_csd);
 
 	return COMMAND_SUCCESS;
 
 error:
-	free(ext_csd);
+	dma_free(ext_csd);
 	return COMMAND_ERROR;
 }
 
diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 6d4abe321bfa..1d383e6449e9 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -545,12 +545,12 @@ u8 *mci_get_ext_csd(struct mci *mci)
 	u8 *ext_csd;
 	int ret;
 
-	ext_csd = xmalloc(512);
+	ext_csd = dma_alloc(512);
 
 	ret = mci_send_ext_csd(mci, ext_csd);
 	if (ret) {
 		printf("Failure to read EXT_CSD register\n");
-		free(ext_csd);
+		dma_free(ext_csd);
 		return ERR_PTR(-EIO);
 	}
 
@@ -666,7 +666,7 @@ static int mmc_change_freq(struct mci *mci)
 	char cardtype;
 	int err;
 
-	mci->ext_csd = xmalloc(512);
+	mci->ext_csd = dma_alloc(512);
 	mci->card_caps = 0;
 
 	/* Only version 4 supports high-speed */
@@ -1124,7 +1124,7 @@ static int mmc_compare_ext_csds(struct mci *mci, enum mci_bus_width bus_width)
 	if (bus_width == MMC_BUS_WIDTH_1)
 		return 0;
 
-	bw_ext_csd = xmalloc(512);
+	bw_ext_csd = dma_alloc(512);
 	err = mci_send_ext_csd(mci, bw_ext_csd);
 	if (err) {
 		dev_info(&mci->dev, "mci_send_ext_csd failed with %d\n", err);
@@ -1173,7 +1173,7 @@ static int mmc_compare_ext_csds(struct mci *mci, enum mci_bus_width bus_width)
 				0 : -EINVAL;
 
 out:
-	free(bw_ext_csd);
+	dma_free(bw_ext_csd);
 	return err;
 }
 
@@ -2220,7 +2220,7 @@ static int mci_get_partition_setting_completed(struct mci *mci)
 
 	ret = ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED];
 
-	free(ext_csd);
+	dma_free(ext_csd);
 
 	return ret;
 }
-- 
2.39.2




More information about the barebox mailing list