Linux IOMMU Development
 help / color / mirror / Atom feed
From: Catalin Marinas <catalin.marinas-5wv7dgnIgG8@public.gmane.org>
To: Robin Murphy <robin.murphy-5wv7dgnIgG8@public.gmane.org>
Cc: laurent.pinchart+renesas-ryLnwIuWjnjg/C1BVhZhaw@public.gmane.org,
	arnd-r2nGTMty4D4@public.gmane.org,
	will.deacon-5wv7dgnIgG8@public.gmane.org,
	iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
	djkurtz-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org,
	yong.wu-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
	thunder.leizhen-hv44wF8Li93QT0dZR+AlfA@public.gmane.org,
	yingjoe.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
	treding-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Subject: Re: [PATCH v4 2/4] iommu: Implement common IOMMU ops for DMA mapping
Date: Tue, 28 Jul 2015 17:45:27 +0100	[thread overview]
Message-ID: <20150728164526.GA536@e104818-lin.cambridge.arm.com> (raw)
In-Reply-To: <00fb13713dc8a69dc32b859f9311e70d78fb0c7a.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>

On Thu, Jul 16, 2015 at 07:40:13PM +0100, Robin Murphy wrote:
> +/**
> + * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space
> + * @dev: Device to allocate memory for. Must be a real device
> + *	 attached to an iommu_dma_domain
> + * @size: Size of buffer in bytes
> + * @gfp: Allocation flags
> + * @prot: IOMMU mapping flags
> + * @handle: Out argument for allocated DMA handle
> + * @flush_page: Arch callback to flush a single page from all caches to
> + *		ensure DMA visibility of __GFP_ZERO. May be NULL if not
> + *		required.
> + *
> + * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
> + * but an IOMMU which supports smaller pages might not map the whole thing.
> + *
> + * Return: Array of struct page pointers describing the buffer,
> + *	   or NULL on failure.
> + */
> +struct page **iommu_dma_alloc(struct device *dev, size_t size,
> +		gfp_t gfp, int prot, dma_addr_t *handle,
> +		void (*flush_page)(const void *, phys_addr_t))
> +{
> +	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
> +	struct iova_domain *iovad = domain->dma_api_cookie;
> +	struct iova *iova;
> +	struct page **pages;
> +	struct sg_table sgt;
> +	dma_addr_t dma_addr;
> +	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
> +
> +	*handle = DMA_ERROR_CODE;
> +
> +	pages = __iommu_dma_alloc_pages(count, gfp);
> +	if (!pages)
> +		return NULL;
> +
> +	iova = __alloc_iova(iovad, size, dev->coherent_dma_mask);
> +	if (!iova)
> +		goto out_free_pages;
> +
> +	size = iova_align(iovad, size);
> +	if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
> +		goto out_free_iova;
> +
> +	if (gfp & __GFP_ZERO) {
> +		struct sg_mapping_iter miter;
> +		/*
> +		 * The flushing provided by the SG_MITER_TO_SG flag only
> +		 * applies to highmem pages, so it won't do the job here.
> +		 */

The comment here is slightly wrong. The SG_MITER_FROM_SG flushing is
done by calling flush_kernel_dcache_page(). This function can be no-op
even on architectures implementing highmem. For example, on arm32 it
only does some flushing if there is a potential D-cache alias with user
space. The flush that you call below is for DMA operations, something
entirely different.

> +		sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG);
> +		while (sg_miter_next(&miter)) {
> +			memset(miter.addr, 0, PAGE_SIZE);

Isn't __GFP_ZERO already honoured by alloc_pages in
__iommu_dma_alloc_pages?

> +			if (flush_page)
> +				flush_page(miter.addr, page_to_phys(miter.page));

Could you instead do the check as !(prot & IOMEM_CACHE) so that it saves
architecture code from passing NULL when coherent?

I'm still not convinced we need this flushing here but I'll follow up in
patch 3/4.

-- 
Catalin

  parent reply	other threads:[~2015-07-28 16:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-16 18:40 [PATCH v4 0/4] arm64: IOMMU-backed DMA mapping Robin Murphy
     [not found] ` <cover.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2015-07-16 18:40   ` [PATCH v4 1/4] iommu/iova: Avoid over-allocating when size-aligned Robin Murphy
     [not found]     ` <35cb877828b570db84e89684ef76308cd1857f0c.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2015-07-28 13:31       ` David Woodhouse
2015-07-16 18:40   ` [PATCH v4 2/4] iommu: Implement common IOMMU ops for DMA mapping Robin Murphy
     [not found]     ` <00fb13713dc8a69dc32b859f9311e70d78fb0c7a.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2015-07-28 16:45       ` Catalin Marinas [this message]
2015-07-16 18:40   ` [PATCH v4 3/4] arm64: Add IOMMU dma_ops Robin Murphy
     [not found]     ` <f51a6dfad1367107b07ca43624a990a62f3d4f3c.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2015-07-28 17:27       ` Catalin Marinas
2015-07-16 18:40   ` [PATCH v4 4/4] arm64: Hook up " Robin Murphy
     [not found]     ` <fecbcc50e62ae86639c886ab86e4ec067d52b16a.1437070995.git.robin.murphy-5wv7dgnIgG8@public.gmane.org>
2015-07-28 17:30       ` Catalin Marinas
2015-07-20 15:26   ` [PATCH v4 0/4] arm64: IOMMU-backed DMA mapping Joerg Roedel
     [not found]     ` <20150720152637.GD10969-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2015-07-20 17:23       ` Robin Murphy
     [not found]         ` <55AD2E97.1040901-5wv7dgnIgG8@public.gmane.org>
2015-07-28 12:46           ` 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=20150728164526.GA536@e104818-lin.cambridge.arm.com \
    --to=catalin.marinas-5wv7dgnigg8@public.gmane.org \
    --cc=arnd-r2nGTMty4D4@public.gmane.org \
    --cc=djkurtz-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
    --cc=iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
    --cc=laurent.pinchart+renesas-ryLnwIuWjnjg/C1BVhZhaw@public.gmane.org \
    --cc=linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=robin.murphy-5wv7dgnIgG8@public.gmane.org \
    --cc=thunder.leizhen-hv44wF8Li93QT0dZR+AlfA@public.gmane.org \
    --cc=treding-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org \
    --cc=will.deacon-5wv7dgnIgG8@public.gmane.org \
    --cc=yingjoe.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
    --cc=yong.wu-NuS5LvNUpcJWk0Htik3J/w@public.gmane.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