All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag
       [not found] <CGME20180611051352eucas1p1f256a2a303da85a1e0923f012d709e39@eucas1p1.samsung.com>
@ 2018-06-11  5:13 ` Marek Szyprowski
  2018-06-11  7:55   ` Geert Uytterhoeven
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Szyprowski @ 2018-06-11  5:13 UTC (permalink / raw)
  To: linux-arm-kernel

dma_alloc_*() buffers might be exposed to userspace via mmap() call, so
they should be cleared on allocation. In case of IOMMU-based dma-mapping
implementation such buffer clearing was missing in the code path for
DMA_ATTR_FORCE_CONTIGUOUS flag handling. This patch fixes this issue. For
more information on clearing buffers allocated by dma_alloc_* functions,
see commit 44176bb38fa4 ("arm64: dma-mapping: always clear allocated
buffers").

Fixes: 44176bb38fa4 ("arm64: Add support for DMA_ATTR_FORCE_CONTIGUOUS to IOMMU")
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
---
 arch/arm64/mm/dma-mapping.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
index 632d32109755..aa0037a3185f 100644
--- a/arch/arm64/mm/dma-mapping.c
+++ b/arch/arm64/mm/dma-mapping.c
@@ -629,13 +629,14 @@ static void *__iommu_alloc_attrs(struct device *dev, size_t size,
 						    size >> PAGE_SHIFT);
 			return NULL;
 		}
-		if (!coherent)
-			__dma_flush_area(page_to_virt(page), iosize);
-
 		addr = dma_common_contiguous_remap(page, size, VM_USERMAP,
 						   prot,
 						   __builtin_return_address(0));
-		if (!addr) {
+		if (addr) {
+			memset(addr, 0, size);
+			if (!coherent)
+				__dma_flush_area(page_to_virt(page), iosize);
+		} else {
 			iommu_dma_unmap_page(dev, *handle, iosize, 0, attrs);
 			dma_release_from_contiguous(dev, page,
 						    size >> PAGE_SHIFT);
-- 
2.17.1

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

* [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag
  2018-06-11  5:13 ` [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag Marek Szyprowski
@ 2018-06-11  7:55   ` Geert Uytterhoeven
  2018-06-11 12:31     ` Will Deacon
  0 siblings, 1 reply; 5+ messages in thread
From: Geert Uytterhoeven @ 2018-06-11  7:55 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Marek,

Thanks for your patch!

On Mon, Jun 11, 2018 at 7:14 AM Marek Szyprowski
<m.szyprowski@samsung.com> wrote:
> dma_alloc_*() buffers might be exposed to userspace via mmap() call, so
> they should be cleared on allocation. In case of IOMMU-based dma-mapping
> implementation such buffer clearing was missing in the code path for
> DMA_ATTR_FORCE_CONTIGUOUS flag handling. This patch fixes this issue. For

Is it? The memory is allocated using dma_alloc_from_contiguous(..., gfp),
and __iommu_alloc_attrs() has

        /*
         * Some drivers rely on this, and we probably don't want the
         * possibility of stale kernel data being read by devices anyway.
         */
        gfp |= __GFP_ZERO;

at the top, before the allocation.

If cma_alloc() (called from dma_alloc_from_contiguous()) doesn't honor
__GFP_ZERO, I think cma_alloc() should be fixed instead.

> more information on clearing buffers allocated by dma_alloc_* functions,
> see commit 44176bb38fa4 ("arm64: dma-mapping: always clear allocated

6829e274a623

> buffers").
>
> Fixes: 44176bb38fa4 ("arm64: Add support for DMA_ATTR_FORCE_CONTIGUOUS to IOMMU")
> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag
  2018-06-11  7:55   ` Geert Uytterhoeven
@ 2018-06-11 12:31     ` Will Deacon
  2018-06-12  7:31       ` Marek Szyprowski
  0 siblings, 1 reply; 5+ messages in thread
From: Will Deacon @ 2018-06-11 12:31 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Jun 11, 2018 at 09:55:54AM +0200, Geert Uytterhoeven wrote:
> Hi Marek,
> 
> Thanks for your patch!
> 
> On Mon, Jun 11, 2018 at 7:14 AM Marek Szyprowski
> <m.szyprowski@samsung.com> wrote:
> > dma_alloc_*() buffers might be exposed to userspace via mmap() call, so
> > they should be cleared on allocation. In case of IOMMU-based dma-mapping
> > implementation such buffer clearing was missing in the code path for
> > DMA_ATTR_FORCE_CONTIGUOUS flag handling. This patch fixes this issue. For
> 
> Is it? The memory is allocated using dma_alloc_from_contiguous(..., gfp),
> and __iommu_alloc_attrs() has
> 
>         /*
>          * Some drivers rely on this, and we probably don't want the
>          * possibility of stale kernel data being read by devices anyway.
>          */
>         gfp |= __GFP_ZERO;
> 
> at the top, before the allocation.
> 
> If cma_alloc() (called from dma_alloc_from_contiguous()) doesn't honor
> __GFP_ZERO, I think cma_alloc() should be fixed instead.

Agreed. We tried to fix this in 7132813c3845 ("arm64: Honor __GFP_ZERO in
dma allocations"). Has something broken that?

Will

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

* [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag
  2018-06-11 12:31     ` Will Deacon
@ 2018-06-12  7:31       ` Marek Szyprowski
  2018-06-12  7:36         ` Geert Uytterhoeven
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Szyprowski @ 2018-06-12  7:31 UTC (permalink / raw)
  To: linux-arm-kernel



On 2018-06-11 14:31, Will Deacon wrote:
> On Mon, Jun 11, 2018 at 09:55:54AM +0200, Geert Uytterhoeven wrote:
>> Hi Marek,
>>
>> Thanks for your patch!
>>
>> On Mon, Jun 11, 2018 at 7:14 AM Marek Szyprowski
>> <m.szyprowski@samsung.com> wrote:
>>> dma_alloc_*() buffers might be exposed to userspace via mmap() call, so
>>> they should be cleared on allocation. In case of IOMMU-based dma-mapping
>>> implementation such buffer clearing was missing in the code path for
>>> DMA_ATTR_FORCE_CONTIGUOUS flag handling. This patch fixes this issue. For
>> Is it? The memory is allocated using dma_alloc_from_contiguous(..., gfp),
>> and __iommu_alloc_attrs() has
>>
>>          /*
>>           * Some drivers rely on this, and we probably don't want the
>>           * possibility of stale kernel data being read by devices anyway.
>>           */
>>          gfp |= __GFP_ZERO;
>>
>> at the top, before the allocation.
>>
>> If cma_alloc() (called from dma_alloc_from_contiguous()) doesn't honor
>> __GFP_ZERO, I think cma_alloc() should be fixed instead.
> Agreed. We tried to fix this in 7132813c3845 ("arm64: Honor __GFP_ZERO in
> dma allocations"). Has something broken that?

The code for handling DMA_ATTR_FORCE_CONTIGUOUS flag in arm64 
dma-mapping/iommu
implementation has been added later and assumed that __GFP_ZERO flag is
handled by dma_alloc_from_contiguous(). This is sadly not true, so the 
buffer
allocated this way is not cleared.

In case of ARM (32bit) the newly allocated buffer is always cleared directly
after calling dma_alloc_from_contiguous() function.

I agree that adding handling of __GFP_ZERO flag to 
dma_alloc_from_contiguous()
or rather cma_alloc() is a better idea, but might have some side effects, so
such change is not a good candidate for -rc cycle. I will prepare such 
patch,
but for now this one is imho a bit less invasive approach.

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland

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

* [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag
  2018-06-12  7:31       ` Marek Szyprowski
@ 2018-06-12  7:36         ` Geert Uytterhoeven
  0 siblings, 0 replies; 5+ messages in thread
From: Geert Uytterhoeven @ 2018-06-12  7:36 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Marek,

On Tue, Jun 12, 2018 at 9:31 AM Marek Szyprowski
<m.szyprowski@samsung.com> wrote:
> On 2018-06-11 14:31, Will Deacon wrote:
> > On Mon, Jun 11, 2018 at 09:55:54AM +0200, Geert Uytterhoeven wrote:
> >> On Mon, Jun 11, 2018 at 7:14 AM Marek Szyprowski
> >> <m.szyprowski@samsung.com> wrote:
> >>> dma_alloc_*() buffers might be exposed to userspace via mmap() call, so
> >>> they should be cleared on allocation. In case of IOMMU-based dma-mapping
> >>> implementation such buffer clearing was missing in the code path for
> >>> DMA_ATTR_FORCE_CONTIGUOUS flag handling. This patch fixes this issue. For
> >> Is it? The memory is allocated using dma_alloc_from_contiguous(..., gfp),
> >> and __iommu_alloc_attrs() has
> >>
> >>          /*
> >>           * Some drivers rely on this, and we probably don't want the
> >>           * possibility of stale kernel data being read by devices anyway.
> >>           */
> >>          gfp |= __GFP_ZERO;
> >>
> >> at the top, before the allocation.
> >>
> >> If cma_alloc() (called from dma_alloc_from_contiguous()) doesn't honor
> >> __GFP_ZERO, I think cma_alloc() should be fixed instead.
> > Agreed. We tried to fix this in 7132813c3845 ("arm64: Honor __GFP_ZERO in
> > dma allocations"). Has something broken that?
>
> The code for handling DMA_ATTR_FORCE_CONTIGUOUS flag in arm64
> dma-mapping/iommu
> implementation has been added later and assumed that __GFP_ZERO flag is
> handled by dma_alloc_from_contiguous(). This is sadly not true, so the
> buffer
> allocated this way is not cleared.
>
> In case of ARM (32bit) the newly allocated buffer is always cleared directly
> after calling dma_alloc_from_contiguous() function.

JFTR, mips and generic dma_direct_alloc() also do that, extensa does not.

> I agree that adding handling of __GFP_ZERO flag to
> dma_alloc_from_contiguous()
> or rather cma_alloc() is a better idea, but might have some side effects, so
> such change is not a good candidate for -rc cycle. I will prepare such
> patch,
> but for now this one is imho a bit less invasive approach.

OK, fine for me.
Thanks!

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

end of thread, other threads:[~2018-06-12  7:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20180611051352eucas1p1f256a2a303da85a1e0923f012d709e39@eucas1p1.samsung.com>
2018-06-11  5:13 ` [PATCH] arm64: dma-mapping: clear buffers allocated with FORCE_CONTIGUOUS flag Marek Szyprowski
2018-06-11  7:55   ` Geert Uytterhoeven
2018-06-11 12:31     ` Will Deacon
2018-06-12  7:31       ` Marek Szyprowski
2018-06-12  7:36         ` Geert Uytterhoeven

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.