mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master] mci: core: allocate memory used for DMA with dma_alloc
Date: Tue, 23 Apr 2024 08:04:35 +0200	[thread overview]
Message-ID: <20240423060435.1514644-1-a.fatoum@pengutronix.de> (raw)

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@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




             reply	other threads:[~2024-04-23  6:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-23  6:04 Ahmad Fatoum [this message]
2024-04-23  6:26 ` Sascha Hauer

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=20240423060435.1514644-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=enrico.scholz@sigma-chemnitz.de \
    /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