linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data
@ 2025-06-05  1:07 Masami Hiramatsu (Google)
  2025-06-05  1:14 ` Sergey Senozhatsky
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2025-06-05  1:07 UTC (permalink / raw)
  To: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: mhiramat, Sergey Senozhatsky, linux-mmc, linux-kernel,
	linux-arm-kernel, linux-mediatek

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

mtk-msdc driver causes a kernel crash after swiotlb buffer is full.

---
mtk-msdc 11240000.mmc: swiotlb buffer is full (sz: 16384 bytes), total 32768 (slots), used 32732 (slots)
mtk-msdc 11240000.mmc: msdc_track_cmd_data: cmd=18 arg=0397A6F8; host->error=0x00000004
Unable to handle kernel paging request at virtual address ffffffffc0001fc0
---

When swiotlb buffer is full, the dma_map_sg() returns 0 to
msdc_prepare_data(), but it does not check it and sets the
MSDC_PREPARE_FLAG.

swiotlb_tbl_map_single() /* prints "swiotlb buffer is full" */
  <-swiotlb_map()
    <-dma_direct_map_page()
      <-dma_direct_map_sg()
        <-__dma_map_sg_attrs()
          <-dma_map_sg_attrs()
            <-dma_map_sg()  /* returns 0 (pages mapped) */
              <-msdc_prepare_data()

Then, the msdc_unprepare_data() checks MSDC_PREPARE_FLAG and calls
dma_unmap_sg() with unmapped pages. It causes a page fault.

To fix this problem, Do not set MSDC_PREPARE_FLAG if dma_map_sg()
fails because this is not prepared.

Fixes: 208489032bdd ("mmc: mediatek: Add Mediatek MMC driver")
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 drivers/mmc/host/mtk-sd.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index 345ea91629e0..3594010bc229 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -827,9 +827,10 @@ static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
 static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
 {
 	if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
-		data->host_cookie |= MSDC_PREPARE_FLAG;
 		data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
 					    mmc_get_dma_dir(data));
+		if (data->sg_count)
+			data->host_cookie |= MSDC_PREPARE_FLAG;
 	}
 }
 



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

* Re: [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data
  2025-06-05  1:07 [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data Masami Hiramatsu (Google)
@ 2025-06-05  1:14 ` Sergey Senozhatsky
  2025-06-05  5:56 ` AngeloGioacchino Del Regno
  2025-06-09 14:25 ` Ulf Hansson
  2 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2025-06-05  1:14 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno, Sergey Senozhatsky, linux-mmc,
	linux-kernel, linux-arm-kernel, linux-mediatek

On (25/06/05 10:07), Masami Hiramatsu (Google) wrote:
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> mtk-msdc driver causes a kernel crash after swiotlb buffer is full.
> 
> ---
> mtk-msdc 11240000.mmc: swiotlb buffer is full (sz: 16384 bytes), total 32768 (slots), used 32732 (slots)
> mtk-msdc 11240000.mmc: msdc_track_cmd_data: cmd=18 arg=0397A6F8; host->error=0x00000004
> Unable to handle kernel paging request at virtual address ffffffffc0001fc0
> ---
> 
> When swiotlb buffer is full, the dma_map_sg() returns 0 to
> msdc_prepare_data(), but it does not check it and sets the
> MSDC_PREPARE_FLAG.
> 
> swiotlb_tbl_map_single() /* prints "swiotlb buffer is full" */
>   <-swiotlb_map()
>     <-dma_direct_map_page()
>       <-dma_direct_map_sg()
>         <-__dma_map_sg_attrs()
>           <-dma_map_sg_attrs()
>             <-dma_map_sg()  /* returns 0 (pages mapped) */
>               <-msdc_prepare_data()
> 
> Then, the msdc_unprepare_data() checks MSDC_PREPARE_FLAG and calls
> dma_unmap_sg() with unmapped pages. It causes a page fault.
> 
> To fix this problem, Do not set MSDC_PREPARE_FLAG if dma_map_sg()
> fails because this is not prepared.
> 
> Fixes: 208489032bdd ("mmc: mediatek: Add Mediatek MMC driver")
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Tested-by: Sergey Senozhatsky <senozhatsky@chromium.org>


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

* Re: [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data
  2025-06-05  1:07 [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data Masami Hiramatsu (Google)
  2025-06-05  1:14 ` Sergey Senozhatsky
@ 2025-06-05  5:56 ` AngeloGioacchino Del Regno
  2025-06-09 14:25 ` Ulf Hansson
  2 siblings, 0 replies; 5+ messages in thread
From: AngeloGioacchino Del Regno @ 2025-06-05  5:56 UTC (permalink / raw)
  To: Masami Hiramatsu (Google), Chaotian Jing, Ulf Hansson,
	Matthias Brugger
  Cc: Sergey Senozhatsky, linux-mmc, linux-kernel, linux-arm-kernel,
	linux-mediatek

Il 05/06/25 03:07, Masami Hiramatsu (Google) ha scritto:
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> mtk-msdc driver causes a kernel crash after swiotlb buffer is full.
> 
> ---
> mtk-msdc 11240000.mmc: swiotlb buffer is full (sz: 16384 bytes), total 32768 (slots), used 32732 (slots)
> mtk-msdc 11240000.mmc: msdc_track_cmd_data: cmd=18 arg=0397A6F8; host->error=0x00000004
> Unable to handle kernel paging request at virtual address ffffffffc0001fc0
> ---
> 
> When swiotlb buffer is full, the dma_map_sg() returns 0 to
> msdc_prepare_data(), but it does not check it and sets the
> MSDC_PREPARE_FLAG.
> 
> swiotlb_tbl_map_single() /* prints "swiotlb buffer is full" */
>    <-swiotlb_map()
>      <-dma_direct_map_page()
>        <-dma_direct_map_sg()
>          <-__dma_map_sg_attrs()
>            <-dma_map_sg_attrs()
>              <-dma_map_sg()  /* returns 0 (pages mapped) */
>                <-msdc_prepare_data()
> 
> Then, the msdc_unprepare_data() checks MSDC_PREPARE_FLAG and calls
> dma_unmap_sg() with unmapped pages. It causes a page fault.
> 
> To fix this problem, Do not set MSDC_PREPARE_FLAG if dma_map_sg()
> fails because this is not prepared.
> 
> Fixes: 208489032bdd ("mmc: mediatek: Add Mediatek MMC driver")
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>



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

* Re: [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data
  2025-06-05  1:07 [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data Masami Hiramatsu (Google)
  2025-06-05  1:14 ` Sergey Senozhatsky
  2025-06-05  5:56 ` AngeloGioacchino Del Regno
@ 2025-06-09 14:25 ` Ulf Hansson
  2025-06-10  4:41   ` Masami Hiramatsu
  2 siblings, 1 reply; 5+ messages in thread
From: Ulf Hansson @ 2025-06-09 14:25 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: Chaotian Jing, Matthias Brugger, AngeloGioacchino Del Regno,
	Sergey Senozhatsky, linux-mmc, linux-kernel, linux-arm-kernel,
	linux-mediatek

On Thu, 5 Jun 2025 at 03:07, Masami Hiramatsu (Google)
<mhiramat@kernel.org> wrote:
>
> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
>
> mtk-msdc driver causes a kernel crash after swiotlb buffer is full.
>
> ---
> mtk-msdc 11240000.mmc: swiotlb buffer is full (sz: 16384 bytes), total 32768 (slots), used 32732 (slots)
> mtk-msdc 11240000.mmc: msdc_track_cmd_data: cmd=18 arg=0397A6F8; host->error=0x00000004
> Unable to handle kernel paging request at virtual address ffffffffc0001fc0
> ---
>
> When swiotlb buffer is full, the dma_map_sg() returns 0 to
> msdc_prepare_data(), but it does not check it and sets the
> MSDC_PREPARE_FLAG.
>
> swiotlb_tbl_map_single() /* prints "swiotlb buffer is full" */
>   <-swiotlb_map()
>     <-dma_direct_map_page()
>       <-dma_direct_map_sg()
>         <-__dma_map_sg_attrs()
>           <-dma_map_sg_attrs()
>             <-dma_map_sg()  /* returns 0 (pages mapped) */
>               <-msdc_prepare_data()
>
> Then, the msdc_unprepare_data() checks MSDC_PREPARE_FLAG and calls
> dma_unmap_sg() with unmapped pages. It causes a page fault.
>
> To fix this problem, Do not set MSDC_PREPARE_FLAG if dma_map_sg()
> fails because this is not prepared.
>
> Fixes: 208489032bdd ("mmc: mediatek: Add Mediatek MMC driver")
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Applied for fixes and by adding a stable-tag, thanks!

Note that it looked like the patch was not entirely correctly
formatted, but I fixed it up when applying. Please have a look at the
fixes branch to make sure things look okay to you.

Kind regards
Uffe



> ---
>  drivers/mmc/host/mtk-sd.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index 345ea91629e0..3594010bc229 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -827,9 +827,10 @@ static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
>  static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
>  {
>         if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
> -               data->host_cookie |= MSDC_PREPARE_FLAG;
>                 data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
>                                             mmc_get_dma_dir(data));
> +               if (data->sg_count)
> +                       data->host_cookie |= MSDC_PREPARE_FLAG;
>         }
>  }
>
>


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

* Re: [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data
  2025-06-09 14:25 ` Ulf Hansson
@ 2025-06-10  4:41   ` Masami Hiramatsu
  0 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu @ 2025-06-10  4:41 UTC (permalink / raw)
  To: Ulf Hansson
  Cc: Chaotian Jing, Matthias Brugger, AngeloGioacchino Del Regno,
	Sergey Senozhatsky, linux-mmc, linux-kernel, linux-arm-kernel,
	linux-mediatek

On Mon, 9 Jun 2025 16:25:24 +0200
Ulf Hansson <ulf.hansson@linaro.org> wrote:

> On Thu, 5 Jun 2025 at 03:07, Masami Hiramatsu (Google)
> <mhiramat@kernel.org> wrote:
> >
> > From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> >
> > mtk-msdc driver causes a kernel crash after swiotlb buffer is full.
> >
> > ---
> > mtk-msdc 11240000.mmc: swiotlb buffer is full (sz: 16384 bytes), total 32768 (slots), used 32732 (slots)
> > mtk-msdc 11240000.mmc: msdc_track_cmd_data: cmd=18 arg=0397A6F8; host->error=0x00000004
> > Unable to handle kernel paging request at virtual address ffffffffc0001fc0
> > ---
> >
> > When swiotlb buffer is full, the dma_map_sg() returns 0 to
> > msdc_prepare_data(), but it does not check it and sets the
> > MSDC_PREPARE_FLAG.
> >
> > swiotlb_tbl_map_single() /* prints "swiotlb buffer is full" */
> >   <-swiotlb_map()
> >     <-dma_direct_map_page()
> >       <-dma_direct_map_sg()
> >         <-__dma_map_sg_attrs()
> >           <-dma_map_sg_attrs()
> >             <-dma_map_sg()  /* returns 0 (pages mapped) */
> >               <-msdc_prepare_data()
> >
> > Then, the msdc_unprepare_data() checks MSDC_PREPARE_FLAG and calls
> > dma_unmap_sg() with unmapped pages. It causes a page fault.
> >
> > To fix this problem, Do not set MSDC_PREPARE_FLAG if dma_map_sg()
> > fails because this is not prepared.
> >
> > Fixes: 208489032bdd ("mmc: mediatek: Add Mediatek MMC driver")
> > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Applied for fixes and by adding a stable-tag, thanks!
> 
> Note that it looked like the patch was not entirely correctly
> formatted, but I fixed it up when applying. Please have a look at the
> fixes branch to make sure things look okay to you.

Thank for applying and fixing!

Thank you,

> 
> Kind regards
> Uffe
> 
> 
> 
> > ---
> >  drivers/mmc/host/mtk-sd.c |    3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> > index 345ea91629e0..3594010bc229 100644
> > --- a/drivers/mmc/host/mtk-sd.c
> > +++ b/drivers/mmc/host/mtk-sd.c
> > @@ -827,9 +827,10 @@ static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
> >  static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
> >  {
> >         if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
> > -               data->host_cookie |= MSDC_PREPARE_FLAG;
> >                 data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
> >                                             mmc_get_dma_dir(data));
> > +               if (data->sg_count)
> > +                       data->host_cookie |= MSDC_PREPARE_FLAG;
> >         }
> >  }
> >
> >


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


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

end of thread, other threads:[~2025-06-10  4:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-05  1:07 [PATCH] mtk-sd: Fix a pagefault in dma_unmap_sg() for not prepared data Masami Hiramatsu (Google)
2025-06-05  1:14 ` Sergey Senozhatsky
2025-06-05  5:56 ` AngeloGioacchino Del Regno
2025-06-09 14:25 ` Ulf Hansson
2025-06-10  4:41   ` Masami Hiramatsu

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).