linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
@ 2025-06-12 11:26 Masami Hiramatsu (Google)
  2025-06-19  2:05 ` Masami Hiramatsu
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Masami Hiramatsu (Google) @ 2025-06-12 11:26 UTC (permalink / raw)
  To: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: mhiramat, Tomasz Figa, Sergey Senozhatsky, linux-mmc,
	linux-kernel, linux-arm-kernel, linux-mediatek

From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

If msdc_prepare_data() fails to map the DMA region, the request is
not prepared for data receiving, but msdc_start_data() proceeds
the DMA with previous setting.
Since this will lead a memory corruption, we have to stop the
request operation soon after the msdc_prepare_data() fails to
prepare it.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 drivers/mmc/host/mtk-sd.c |   17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index 3594010bc229..56b4999fe58f 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -834,6 +834,11 @@ static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
 	}
 }
 
+static bool msdc_data_prepared(struct mmc_data *data)
+{
+	return data->host_cookie & MSDC_PREPARE_FLAG;
+}
+
 static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
 {
 	if (data->host_cookie & MSDC_ASYNC_FLAG)
@@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
 	WARN_ON(!host->hsq_en && host->mrq);
 	host->mrq = mrq;
 
-	if (mrq->data)
+	if (mrq->data) {
 		msdc_prepare_data(host, mrq->data);
+		if (!msdc_data_prepared(mrq->data)) {
+			/*
+			 * Failed to prepare DMA area, fail fast before
+			 * starting any commands.
+			 */
+			mrq->cmd->error = -ENOSPC;
+			mmc_request_done(mmc_from_priv(host), mrq);
+			return;
+		}
+	}
 
 	/* if SBC is required, we have HW option and SW option.
 	 * if HW option is enabled, and SBC does not have "special" flags,


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
  2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
@ 2025-06-19  2:05 ` Masami Hiramatsu
  2025-06-19 11:20 ` Ulf Hansson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2025-06-19  2:05 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno, Tomasz Figa, Sergey Senozhatsky,
	linux-mmc, linux-kernel, linux-arm-kernel, linux-mediatek

Ping?

I think there will be another bug which leaks the resources
to cause this failure case. But anyway, without this fix,
if DMA preparation fails but runs a DMA, it will overwrite
the previous read buffer memory.

Thank you,

On Thu, 12 Jun 2025 20:26:10 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> If msdc_prepare_data() fails to map the DMA region, the request is
> not prepared for data receiving, but msdc_start_data() proceeds
> the DMA with previous setting.
> Since this will lead a memory corruption, we have to stop the
> request operation soon after the msdc_prepare_data() fails to
> prepare it.
> 
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> ---
>  drivers/mmc/host/mtk-sd.c |   17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index 3594010bc229..56b4999fe58f 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -834,6 +834,11 @@ static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
>  	}
>  }
>  
> +static bool msdc_data_prepared(struct mmc_data *data)
> +{
> +	return data->host_cookie & MSDC_PREPARE_FLAG;
> +}
> +
>  static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
>  {
>  	if (data->host_cookie & MSDC_ASYNC_FLAG)
> @@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>  	WARN_ON(!host->hsq_en && host->mrq);
>  	host->mrq = mrq;
>  
> -	if (mrq->data)
> +	if (mrq->data) {
>  		msdc_prepare_data(host, mrq->data);
> +		if (!msdc_data_prepared(mrq->data)) {
> +			/*
> +			 * Failed to prepare DMA area, fail fast before
> +			 * starting any commands.
> +			 */
> +			mrq->cmd->error = -ENOSPC;
> +			mmc_request_done(mmc_from_priv(host), mrq);
> +			return;
> +		}
> +	}
>  
>  	/* if SBC is required, we have HW option and SW option.
>  	 * if HW option is enabled, and SBC does not have "special" flags,
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
  2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
  2025-06-19  2:05 ` Masami Hiramatsu
@ 2025-06-19 11:20 ` Ulf Hansson
  2025-06-25  3:56 ` Sergey Senozhatsky
  2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
  3 siblings, 0 replies; 9+ messages in thread
From: Ulf Hansson @ 2025-06-19 11:20 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Chaotian Jing, Matthias Brugger, AngeloGioacchino Del Regno,
	Tomasz Figa, Sergey Senozhatsky, linux-mmc, linux-kernel,
	linux-arm-kernel, linux-mediatek

On Thu, 12 Jun 2025 at 13:26, Masami Hiramatsu (Google)
<mhiramat@kernel.org> wrote:
>
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
>
> If msdc_prepare_data() fails to map the DMA region, the request is
> not prepared for data receiving, but msdc_start_data() proceeds
> the DMA with previous setting.
> Since this will lead a memory corruption, we have to stop the
> request operation soon after the msdc_prepare_data() fails to
> prepare it.
>
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Applied to fixes and by adding a fixes/stable tag, thanks!

Kind regards
Uffe


> ---
>  drivers/mmc/host/mtk-sd.c |   17 ++++++++++++++++-
>  1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index 3594010bc229..56b4999fe58f 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -834,6 +834,11 @@ static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
>         }
>  }
>
> +static bool msdc_data_prepared(struct mmc_data *data)
> +{
> +       return data->host_cookie & MSDC_PREPARE_FLAG;
> +}
> +
>  static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
>  {
>         if (data->host_cookie & MSDC_ASYNC_FLAG)
> @@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>         WARN_ON(!host->hsq_en && host->mrq);
>         host->mrq = mrq;
>
> -       if (mrq->data)
> +       if (mrq->data) {
>                 msdc_prepare_data(host, mrq->data);
> +               if (!msdc_data_prepared(mrq->data)) {
> +                       /*
> +                        * Failed to prepare DMA area, fail fast before
> +                        * starting any commands.
> +                        */
> +                       mrq->cmd->error = -ENOSPC;
> +                       mmc_request_done(mmc_from_priv(host), mrq);
> +                       return;
> +               }
> +       }
>
>         /* if SBC is required, we have HW option and SW option.
>          * if HW option is enabled, and SBC does not have "special" flags,
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
  2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
  2025-06-19  2:05 ` Masami Hiramatsu
  2025-06-19 11:20 ` Ulf Hansson
@ 2025-06-25  3:56 ` Sergey Senozhatsky
  2025-06-25  4:13   ` Sergey Senozhatsky
  2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
  3 siblings, 1 reply; 9+ messages in thread
From: Sergey Senozhatsky @ 2025-06-25  3:56 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno, Tomasz Figa, Sergey Senozhatsky,
	linux-mmc, linux-kernel, linux-arm-kernel, linux-mediatek

On (25/06/12 20:26), Masami Hiramatsu (Google) wrote:
[..]
> +static bool msdc_data_prepared(struct mmc_data *data)
> +{
> +	return data->host_cookie & MSDC_PREPARE_FLAG;
> +}
> +
>  static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
>  {
>  	if (data->host_cookie & MSDC_ASYNC_FLAG)
> @@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>  	WARN_ON(!host->hsq_en && host->mrq);
>  	host->mrq = mrq;
>  
> -	if (mrq->data)
> +	if (mrq->data) {
>  		msdc_prepare_data(host, mrq->data);
> +		if (!msdc_data_prepared(mrq->data)) {
> +			/*
> +			 * Failed to prepare DMA area, fail fast before
> +			 * starting any commands.
> +			 */
> +			mrq->cmd->error = -ENOSPC;
> +			mmc_request_done(mmc_from_priv(host), mrq);

Do we end up having a stale/dangling host->mrq pointer here?

> +			return;
> +		}
> +	}
>  
>  	/* if SBC is required, we have HW option and SW option.
>  	 * if HW option is enabled, and SBC does not have "special" flags,
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
  2025-06-25  3:56 ` Sergey Senozhatsky
@ 2025-06-25  4:13   ` Sergey Senozhatsky
  2025-06-25  4:42     ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Sergey Senozhatsky @ 2025-06-25  4:13 UTC (permalink / raw)
  To: Masami Hiramatsu (Google), Ulf Hansson
  Cc: Chaotian Jing, Matthias Brugger, AngeloGioacchino Del Regno,
	Tomasz Figa, linux-mmc, linux-kernel, linux-arm-kernel,
	linux-mediatek, Sergey Senozhatsky

On (25/06/25 12:56), Sergey Senozhatsky wrote:
> On (25/06/12 20:26), Masami Hiramatsu (Google) wrote:
> [..]
> > @@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
> >  	WARN_ON(!host->hsq_en && host->mrq);
> >  	host->mrq = mrq;
> >  
> > -	if (mrq->data)
> > +	if (mrq->data) {
> >  		msdc_prepare_data(host, mrq->data);
> > +		if (!msdc_data_prepared(mrq->data)) {
> > +			/*
> > +			 * Failed to prepare DMA area, fail fast before
> > +			 * starting any commands.
> > +			 */
> > +			mrq->cmd->error = -ENOSPC;
> > +			mmc_request_done(mmc_from_priv(host), mrq);
> 
> Do we end up having a stale/dangling host->mrq pointer here?

Something like this maybe?

---

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index b12cfb9a5e5f..46bb770ace41 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -1498,6 +1498,7 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
 			 */
 			mrq->cmd->error = -ENOSPC;
 			mmc_request_done(mmc_from_priv(host), mrq);
+			host->mrq = NULL;
 			return;
 		}
 	}

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
  2025-06-25  4:13   ` Sergey Senozhatsky
@ 2025-06-25  4:42     ` Masami Hiramatsu
  0 siblings, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2025-06-25  4:42 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Ulf Hansson, Chaotian Jing, Matthias Brugger,
	AngeloGioacchino Del Regno, Tomasz Figa, linux-mmc, linux-kernel,
	linux-arm-kernel, linux-mediatek

On Wed, 25 Jun 2025 13:13:18 +0900
Sergey Senozhatsky <senozhatsky@chromium.org> wrote:

> On (25/06/25 12:56), Sergey Senozhatsky wrote:
> > On (25/06/12 20:26), Masami Hiramatsu (Google) wrote:
> > [..]
> > > @@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
> > >  	WARN_ON(!host->hsq_en && host->mrq);
> > >  	host->mrq = mrq;
> > >  
> > > -	if (mrq->data)
> > > +	if (mrq->data) {
> > >  		msdc_prepare_data(host, mrq->data);
> > > +		if (!msdc_data_prepared(mrq->data)) {
> > > +			/*
> > > +			 * Failed to prepare DMA area, fail fast before
> > > +			 * starting any commands.
> > > +			 */
> > > +			mrq->cmd->error = -ENOSPC;
> > > +			mmc_request_done(mmc_from_priv(host), mrq);
> > 
> > Do we end up having a stale/dangling host->mrq pointer here?
> 
> Something like this maybe?

Good catch! I thought it is cleared in mmc_request_done(), but not.
I agree we need to clean it up since it is set in msdc_ops_request().

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thanks,

> 
> ---
> 
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index b12cfb9a5e5f..46bb770ace41 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -1498,6 +1498,7 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>  			 */
>  			mrq->cmd->error = -ENOSPC;
>  			mmc_request_done(mmc_from_priv(host), mrq);
> +			host->mrq = NULL;
>  			return;
>  		}
>  	}


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH] mtk-sd: reset host->mrq on prepare_data() error
  2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
                   ` (2 preceding siblings ...)
  2025-06-25  3:56 ` Sergey Senozhatsky
@ 2025-06-25  5:20 ` Sergey Senozhatsky
  2025-06-25  8:22   ` Masami Hiramatsu
  2025-06-25 12:45   ` Ulf Hansson
  3 siblings, 2 replies; 9+ messages in thread
From: Sergey Senozhatsky @ 2025-06-25  5:20 UTC (permalink / raw)
  To: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: Masami Hiramatsu, Tomasz Figa, linux-mmc, linux-kernel,
	linux-arm-kernel, linux-mediatek, Sergey Senozhatsky

Do not leave host with dangling ->mrq pointer if we hit
the msdc_prepare_data() error out path.

Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
 drivers/mmc/host/mtk-sd.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index b12cfb9a5e5f..d7020e06dd55 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -1492,6 +1492,7 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
 	if (mrq->data) {
 		msdc_prepare_data(host, mrq->data);
 		if (!msdc_data_prepared(mrq->data)) {
+			host->mrq = NULL;
 			/*
 			 * Failed to prepare DMA area, fail fast before
 			 * starting any commands.
-- 
2.50.0.714.g196bf9f422-goog


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: reset host->mrq on prepare_data() error
  2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
@ 2025-06-25  8:22   ` Masami Hiramatsu
  2025-06-25 12:45   ` Ulf Hansson
  1 sibling, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2025-06-25  8:22 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno, Masami Hiramatsu, Tomasz Figa,
	linux-mmc, linux-kernel, linux-arm-kernel, linux-mediatek

On Wed, 25 Jun 2025 14:20:37 +0900
Sergey Senozhatsky <senozhatsky@chromium.org> wrote:

> Do not leave host with dangling ->mrq pointer if we hit
> the msdc_prepare_data() error out path.
> 
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thanks for fixing it.

> ---
>  drivers/mmc/host/mtk-sd.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index b12cfb9a5e5f..d7020e06dd55 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -1492,6 +1492,7 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>  	if (mrq->data) {
>  		msdc_prepare_data(host, mrq->data);
>  		if (!msdc_data_prepared(mrq->data)) {
> +			host->mrq = NULL;
>  			/*
>  			 * Failed to prepare DMA area, fail fast before
>  			 * starting any commands.
> -- 
> 2.50.0.714.g196bf9f422-goog
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] mtk-sd: reset host->mrq on prepare_data() error
  2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
  2025-06-25  8:22   ` Masami Hiramatsu
@ 2025-06-25 12:45   ` Ulf Hansson
  1 sibling, 0 replies; 9+ messages in thread
From: Ulf Hansson @ 2025-06-25 12:45 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Chaotian Jing, Matthias Brugger, AngeloGioacchino Del Regno,
	Masami Hiramatsu, Tomasz Figa, linux-mmc, linux-kernel,
	linux-arm-kernel, linux-mediatek

On Wed, 25 Jun 2025 at 07:21, Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> Do not leave host with dangling ->mrq pointer if we hit
> the msdc_prepare_data() error out path.
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>

Applied for fixes, thanks!

Kind regards
Uffe

> ---
>  drivers/mmc/host/mtk-sd.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index b12cfb9a5e5f..d7020e06dd55 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -1492,6 +1492,7 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
>         if (mrq->data) {
>                 msdc_prepare_data(host, mrq->data);
>                 if (!msdc_data_prepared(mrq->data)) {
> +                       host->mrq = NULL;
>                         /*
>                          * Failed to prepare DMA area, fail fast before
>                          * starting any commands.
> --
> 2.50.0.714.g196bf9f422-goog
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-06-25 12:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
2025-06-19  2:05 ` Masami Hiramatsu
2025-06-19 11:20 ` Ulf Hansson
2025-06-25  3:56 ` Sergey Senozhatsky
2025-06-25  4:13   ` Sergey Senozhatsky
2025-06-25  4:42     ` Masami Hiramatsu
2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
2025-06-25  8:22   ` Masami Hiramatsu
2025-06-25 12:45   ` Ulf Hansson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).