From: Walter Wu <walter-zh.wu@mediatek.com>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: wsd_upstream <wsd_upstream@mediatek.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Linux IOMMU <iommu@lists.linux-foundation.org>,
linux-mediatek@lists.infradead.org,
Matthias Brugger <matthias.bgg@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Robin Murphy <robin.murphy@arm.com>,
Christoph Hellwig <hch@lst.de>,
Linux ARM <linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH] dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING
Date: Tue, 2 Nov 2021 11:21:16 +0800 [thread overview]
Message-ID: <c9f74d817aa1ae1cceaee9ec226d39bbdf2c5c48.camel@mediatek.com> (raw)
In-Reply-To: <CAMj1kXFrUrThJFDJ0Q9qjDemcEovQVMtO20KbdnLccp7VfyVwA@mail.gmail.com>
On Mon, 2021-11-01 at 15:17 +0100, Ard Biesheuvel wrote:
> On Mon, 1 Nov 2021 at 13:21, Walter Wu <walter-zh.wu@mediatek.com>
> wrote:
> >
> > Hi Ard,
> >
> > On Mon, 2021-11-01 at 09:34 +0100, Ard Biesheuvel wrote:
> > > On Mon, 1 Nov 2021 at 04:17, Walter Wu <walter-zh.wu@mediatek.com
> > > >
> > > wrote:
> > > >
> > > > DMA_ATTR_NO_KERNEL_MAPPING is to avoid creating a kernel
> > > > mapping
> > > > for the allocated buffer, but current implementation is that
> > > > PTE of allocated buffer in kernel page table is valid. So we
> > > > should set invalid for PTE of allocate buffer so that there are
> > > > no kernel mapping for the allocated buffer.
> > > >
> > > > In some cases, we don't hope the allocated buffer to be read
> > > > by cpu or speculative execution, so we use
> > > > DMA_ATTR_NO_KERNEL_MAPPING
> > > > to get no kernel mapping in order to achieve this goal.
> > > >
> > > > Signed-off-by: Walter Wu <walter-zh.wu@mediatek.com>
> > > > Cc: Christoph Hellwig <hch@lst.de>
> > > > Cc: Marek Szyprowski <m.szyprowski@samsung.com>
> > > > Cc: Robin Murphy <robin.murphy@arm.com>
> > > > Cc: Matthias Brugger <matthias.bgg@gmail.com>
> > > > Cc: Andrew Morton <akpm@linux-foundation.org>
> > > > ---
> > > > kernel/dma/direct.c | 8 ++++++++
> > > > 1 file changed, 8 insertions(+)
> > > >
> > > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> > > > index 4c6c5e0635e3..aa10b4c5d762 100644
> > > > --- a/kernel/dma/direct.c
> > > > +++ b/kernel/dma/direct.c
> > > > @@ -13,6 +13,7 @@
> > > > #include <linux/vmalloc.h>
> > > > #include <linux/set_memory.h>
> > > > #include <linux/slab.h>
> > > > +#include <asm/cacheflush.h>
> > > > #include "direct.h"
> > > >
> > > > /*
> > > > @@ -169,6 +170,9 @@ void *dma_direct_alloc(struct device *dev,
> > > > size_t size,
> > > > if (!PageHighMem(page))
> > > > arch_dma_prep_coherent(page, size);
> > > > *dma_handle = phys_to_dma_direct(dev,
> > > > page_to_phys(page));
> > > > + /* remove kernel mapping for pages */
> > > > + set_memory_valid((unsigned
> > > > long)phys_to_virt(dma_to_phys(dev, *dma_handle)),
> > > > + size >> PAGE_SHIFT, 0);
> > >
> > > This only works if the memory is mapped at page granularity in
> > > the
> > > linear region, and you cannot rely on that. Many architectures
> > > prefer
> > > block mappings for the linear region, and arm64 will only use
> > > page
> > > mappings if rodata=full is set (which is set by default but can
> > > be
> > > overridden on the kernel command line)
> > >
> >
> > We mainly want to solve arm64 arch.
>
> Solve what?
Our platform is arch64. We need a dynamic allocated buffer from CMA is
not to read by CPU peculative execution, so we need to remove its
kernel mapping.
> > RODATA_FULL_DEFAULT_ENABLED should
> > be the arm64 config. If we use CONFIG_RODATA_FULL_DEFAULT_ENABLED
> > to
> > check whether it call set_memory_valid(). It should avoid other
> > architectures. Do you think this method is work?
> >
>
> No. Try it with rodata=off (or rodata=on for that matter) and see
> what happens.
>
I try with rodata=[off/on] on qemu, it looks like boot pass. see below.
[ 0.000000] Kernel command line: root=/dev/ram0 rw rootfstype=ext4
rodata=off console=ttyAMA0 rdinit=qemu/ramdisk.img earlyprintk=serial
I also try with rodata=off on our platform and do stress test, it looks
like pass.
Thanks.
Walter
> > Thanks for your explaination and suggestion.
> >
>
> YW
>
> >
> > >
> > > > /* return the page pointer as the opaque cookie
> > > > */
> > > > return page;
> > > > }
> > > > @@ -278,6 +282,10 @@ void dma_direct_free(struct device *dev,
> > > > size_t size,
> > > >
> > > > if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
> > > > !force_dma_unencrypted(dev) &&
> > > > !is_swiotlb_for_alloc(dev)) {
> > > > + size = PAGE_ALIGN(size);
> > > > + /* create kernel mapping for pages */
> > > > + set_memory_valid((unsigned
> > > > long)phys_to_virt(dma_to_phys(dev, dma_addr)),
> > > > + size >> PAGE_SHIFT, 1);
> > > > /* cpu_addr is a struct page cookie, not a
> > > > kernel
> > > > address */
> > > > dma_free_contiguous(dev, cpu_addr, size);
> > > > return;
> > > > --
> > > > 2.18.0
> > > >
> > > >
> > > > _______________________________________________
> > > > linux-arm-kernel mailing list
> > > > linux-arm-kernel@lists.infradead.org
> > > >
> >
> >
https://urldefense.com/v3/__http://lists.infradead.org/mailman/listinfo/linux-arm-kernel__;!!CTRNKA9wMg0ARbw!16dLCjnvtRkaRLeCO9AQ7Fund5XL0FicZmeVaU3-WkFymr-0lbITfzwrvoJpiHlqnqIu-g$
> > > >
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
next prev parent reply other threads:[~2021-11-02 3:21 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-01 3:15 [PATCH] dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING Walter Wu
2021-11-01 6:10 ` kernel test robot
2021-11-01 8:34 ` Ard Biesheuvel
2021-11-01 12:20 ` Walter Wu
2021-11-01 14:17 ` Ard Biesheuvel
2021-11-02 3:21 ` Walter Wu [this message]
2021-11-02 6:43 ` Christoph Hellwig
2021-11-01 10:29 ` Robin Murphy
2021-11-01 12:07 ` Walter Wu
2021-11-02 6:41 ` Christoph Hellwig
2021-11-02 7:08 ` Walter Wu
2021-11-02 7:26 ` Walter Wu
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=c9f74d817aa1ae1cceaee9ec226d39bbdf2c5c48.camel@mediatek.com \
--to=walter-zh.wu@mediatek.com \
--cc=akpm@linux-foundation.org \
--cc=ardb@kernel.org \
--cc=hch@lst.de \
--cc=iommu@lists.linux-foundation.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=robin.murphy@arm.com \
--cc=wsd_upstream@mediatek.com \
/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