From: yong.wu@mediatek.com (Yong Wu)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2] iommu/io-pgtable-arm: Don't use dma_to_phys()
Date: Fri, 18 Sep 2015 16:55:50 +0800 [thread overview]
Message-ID: <1442566550.8145.156.camel@mhfsdcap03> (raw)
In-Reply-To: <59f4ebbf06e75a6176a366495211afd16d0048a3.1442507940.git.robin.murphy@arm.com>
On Thu, 2015-09-17 at 17:42 +0100, Robin Murphy wrote:
> In checking whether DMA addresses differ from physical addresses, using
> dma_to_phys() is actually the wrong thing to do, since it may hide any
> DMA offset, which is precisely one of the things we are checking for.
> Simply casting between the two address types, whilst ugly, is in fact
> the appropriate course of action. Further care (and ugliness) is also
> necessary in the comparison to avoid truncation if phys_addr_t and
> dma_addr_t differ in size.
>
> We can also reject any device with a fixed DMA offset up-front at page
> table creation, leaving the allocation-time check for the more subtle
> cases like bounce buffering due to an incorrect DMA mask.
>
> Furthermore, we can then fix the hackish KConfig dependency so that
> architectures without a dma_to_phys() implementation may still
> COMPILE_TEST (or even use!) the code. The true dependency is on the
> DMA API, so use the appropriate symbol for that.
>
> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> ---
[...]
>
> static bool selftest_running = false;
>
> -static dma_addr_t __arm_lpae_dma_addr(struct device *dev, void *pages)
> +static dma_addr_t __arm_lpae_dma_addr(void *pages)
> {
> - return phys_to_dma(dev, virt_to_phys(pages));
> + return (dma_addr_t)virt_to_phys(pages);
> }
>
> static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
> @@ -223,10 +223,10 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
> goto out_free;
> /*
> * We depend on the IOMMU being able to work with any physical
> - * address directly, so if the DMA layer suggests it can't by
> - * giving us back some translation, that bodes very badly...
> + * address directly, so if the DMA layer suggests otherwise by
> + * translating or truncating them, that bodes very badly...
> */
> - if (dma != __arm_lpae_dma_addr(dev, pages))
> + if (dma != virt_to_phys(pages))
Could I ask why not use __arm_lpae_dma_addr(pages) here?
dma is dma_addr_t.
> goto out_unmap;
> }
>
> @@ -243,10 +243,8 @@ out_free:
> static void __arm_lpae_free_pages(void *pages, size_t size,
> struct io_pgtable_cfg *cfg)
> {
> - struct device *dev = cfg->iommu_dev;
> -
> if (!selftest_running)
> - dma_unmap_single(dev, __arm_lpae_dma_addr(dev, pages),
> + dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
> size, DMA_TO_DEVICE);
> free_pages_exact(pages, size);
> }
> @@ -254,12 +252,11 @@ static void __arm_lpae_free_pages(void *pages, size_t size,
> static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
> struct io_pgtable_cfg *cfg)
> {
> - struct device *dev = cfg->iommu_dev;
> -
> *ptep = pte;
>
> if (!selftest_running)
> - dma_sync_single_for_device(dev, __arm_lpae_dma_addr(dev, ptep),
> + dma_sync_single_for_device(cfg->iommu_dev,
> + __arm_lpae_dma_addr(ptep),
> sizeof(pte), DMA_TO_DEVICE);
> }
>
> @@ -629,6 +626,11 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
> if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
> return NULL;
>
> + if (cfg->iommu_dev->dma_pfn_offset) {
> + dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
> + return NULL;
> + }
> +
> data = kmalloc(sizeof(*data), GFP_KERNEL);
> if (!data)
> return NULL;
next prev parent reply other threads:[~2015-09-18 8:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-17 14:22 [PATCH] iommu/io-pgtable-arm: Don't use dma_to_phys() Robin Murphy
2015-09-17 14:52 ` Will Deacon
2015-09-17 15:53 ` Robin Murphy
2015-09-17 16:42 ` [PATCH v2] " Robin Murphy
2015-09-18 8:55 ` Yong Wu [this message]
2015-09-18 11:04 ` Robin Murphy
2015-09-18 12:29 ` Russell King - ARM Linux
2015-09-22 13:25 ` Yong Wu
2015-09-22 16:23 ` Robin Murphy
2015-09-22 16:26 ` Will Deacon
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=1442566550.8145.156.camel@mhfsdcap03 \
--to=yong.wu@mediatek.com \
--cc=linux-arm-kernel@lists.infradead.org \
/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