mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: Marek Szyprowski <m.szyprowski@samsung.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
	Leon Romanovsky <leon@kernel.org>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Sibi Sankar <sibi.sankar@oss.qualcomm.com>,
	Honglei Huang <honghuan@amd.com>,
	prakash.gupta@oss.qualcomm.com, stable@vger.kernel.org
Subject: Re: [PATCH v2] iommu/dma: restore flush queue use in dma_iova_destroy()
Date: Fri, 27 Nov 2026 17:30:26 +0000	[thread overview]
Message-ID: <90e159b3-ef50-4d03-937a-ced5896b152a@arm.com> (raw)
In-Reply-To: <20261127171529.2917893-1-m.szyprowski@samsung.com>

On 27/11/2026 5:15 pm, Marek Szyprowski wrote:
> Commit 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop
> the free_iova helper") turned dma_iova_destroy() into dma_iova_unlink()
> followed by dma_iova_free(). Before it, the destroy path marked its
> IOTLB gather as queued for DMA-FQ domains, skipped iommu_iotlb_sync()
> and handed the IOVA over to the flush queue, exactly like
> __iommu_dma_unmap(). After it:
> 
>    - dma_iova_unlink() never queues its gather and always calls
>      iommu_iotlb_sync() after iommu_unmap_fast(),
>    - dma_iova_free() passes a NULL gather to iommu_dma_free_iova(), so
>      the IOVA goes straight back to the allocator via free_iova_fast().
> 
> DMA-FQ domains therefore get strict invalidation on this path. This
> includes blk_rq_dma_map(), which NVMe uses for all data transfers, so
> iommu.strict=0 has no effect on NVMe I/O. On a Qualcomm Glymur CRD
> (SMMUv3 without RANGE_INV, 4K pages, NVMe on PCIe Gen5 x4) sequential
> fio throughput drops from 10.6/11.9 GiB/s (write/read) to the strict
> mode level of ~2.7 GiB/s.
> 
> Fix this by letting dma_iova_destroy() unmap the linked range into its
> own gather, queued for flush queue domains like in __iommu_dma_unmap(),
> and retire that gather together with the IOVA free: either by
> iommu_iotlb_sync(), or by handing the IOVA space (and any page table
> pages released by the unmap) over to the flush queue via queue_iova().
> The whole reservation is still freed, keeping the size fix of the
> mentioned commit.
> 
> A gather must always be retired, either by iommu_iotlb_sync() or by
> handing it to the flush queue together with the IOVA, and the IOVA must
> never be returned to the allocator while a queued invalidation for it is
> still pending. To keep that obvious, the internal unlink helper only
> unmaps into a gather supplied by its caller, and each caller retires it
> right away: dma_iova_destroy() syncs or queues it together with the IOVA
> free, while dma_iova_unlink() always uses a never queued gather and
> syncs it, as the unlinked range remains owned by the caller and may be
> linked again immediately.
> 
> Consequently dma_iova_free(), and dma_iova_destroy() with nothing
> mapped, keep returning the IOVA directly to the allocator: no queued
> gather was ever created for it, so there is no deferred invalidation to
> wait for.

 From a quick skim, this looks about right to me. Note that a single 
gather in the dma_iova_state itself would never work anyway, as multiple 
threads are still free to unlink non-overlapping parts of the range 
simultaneously, so gathering always has to be per-call.

Acked-by: Robin Murphy <reobin.murphy@arm.com>

> Fixes: 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper")
> Reported-by: Sibi Sankar <sibi.sankar@oss.qualcomm.com>
> Closes: https://lore.kernel.org/all/20261125135120.1126571-1-sibi.sankar@oss.qualcomm.com/
> Cc: stable@vger.kernel.org # v7.3+
> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
> The series has been developped with an aid of LLM, build tested on arm64
> and x86_64. Sibi, it would be great if you could rerun your fio test on
> top of it.
> 
> Best regards
> Marek Szyprowski, PhD
> Samsung R&D Institute Poland
> 
> 
> Changelog:
> v2:
> - dma_iova_free() and dma_iova_destroy() with nothing mapped return the
>    IOVA directly to the allocator instead of the flush queue, as there is
>    no deferred invalidation to wait for (pointed out by Sashiko review)
> - dropped DMA_IOVA_USE_FQ flag and flags related rework (pointed out by
>    Jason)
> 
> v1: https://lore.kernel.org/all/20261127115849.2780625-1-m.szyprowski@samsung.com/
> ---
>   drivers/iommu/dma-iommu.c | 100 +++++++++++++++++++++++++++-----------
>   1 file changed, 72 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
> index 78742aeba932..44fa02c35bad 100644
> --- a/drivers/iommu/dma-iommu.c
> +++ b/drivers/iommu/dma-iommu.c
> @@ -1857,6 +1857,19 @@ bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state,
>   }
>   EXPORT_SYMBOL_GPL(dma_iova_try_alloc);
>   
> +static void __dma_iova_free(struct device *dev, struct dma_iova_state *state,
> +		struct iommu_iotlb_gather *gather)
> +{
> +	struct iommu_domain *domain = iommu_get_dma_domain(dev);
> +	struct iommu_dma_cookie *cookie = domain->iova_cookie;
> +	struct iova_domain *iovad = &cookie->iovad;
> +	size_t iova_start_pad = iova_offset(iovad, state->addr);
> +	size_t size = dma_iova_size(state);
> +
> +	iommu_dma_free_iova(domain, state->addr - iova_start_pad,
> +			iova_align(iovad, size + iova_start_pad), gather);
> +}
> +
>   /**
>    * dma_iova_free - Free an IOVA space
>    * @dev: Device to free the IOVA space for
> @@ -1871,14 +1884,12 @@ EXPORT_SYMBOL_GPL(dma_iova_try_alloc);
>    */
>   void dma_iova_free(struct device *dev, struct dma_iova_state *state)
>   {
> -	struct iommu_domain *domain = iommu_get_dma_domain(dev);
> -	struct iommu_dma_cookie *cookie = domain->iova_cookie;
> -	struct iova_domain *iovad = &cookie->iovad;
> -	size_t iova_start_pad = iova_offset(iovad, state->addr);
> -	size_t size = dma_iova_size(state);
> -
> -	iommu_dma_free_iova(domain, state->addr - iova_start_pad,
> -			iova_align(iovad, size + iova_start_pad), NULL);
> +	/*
> +	 * Nothing is linked anymore and every dma_iova_unlink() has already
> +	 * invalidated the IOTLB synchronously, so there is no deferred
> +	 * invalidation to wait for and the IOVA can be reused right away.
> +	 */
> +	__dma_iova_free(dev, state, NULL);
>   }
>   EXPORT_SYMBOL_GPL(dma_iova_free);
>   
> @@ -2097,27 +2108,20 @@ static void iommu_dma_iova_unlink_range_slow(struct device *dev,
>   		arch_sync_dma_flush();
>   }
>   
> -/**
> - * dma_iova_unlink - Unlink a range of IOVA space
> - * @dev: DMA device
> - * @state: IOVA state
> - * @offset: offset into the IOVA state to unlink
> - * @size: size of the buffer
> - * @dir: DMA direction
> - * @attrs: attributes of mapping properties
> - *
> - * Unlink a range of IOVA space for the given IOVA state.
> +/*
> + * Unmap a range of IOVA space into @gather. The caller owns @gather and must
> + * retire it, either by iommu_iotlb_sync() or, for a queued gather, by handing
> + * it over to the flush queue together with the IOVA via iommu_dma_free_iova().
>    */
> -void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
> +static void __dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
>   		size_t offset, size_t size, enum dma_data_direction dir,
> -		unsigned long attrs)
> +		unsigned long attrs, struct iommu_iotlb_gather *gather)
>   {
>   	struct iommu_domain *domain = iommu_get_dma_domain(dev);
>   	struct iommu_dma_cookie *cookie = domain->iova_cookie;
>   	struct iova_domain *iovad = &cookie->iovad;
>   	dma_addr_t addr = state->addr + offset;
>   	size_t iova_start_pad = iova_offset(iovad, addr);
> -	struct iommu_iotlb_gather iotlb_gather;
>   	size_t unmapped;
>   
>   	if ((state->__size & DMA_IOVA_USE_SWIOTLB) ||
> @@ -2125,14 +2129,38 @@ void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
>   	     !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))))
>   		iommu_dma_iova_unlink_range_slow(dev, addr, size, dir, attrs);
>   
> -	iommu_iotlb_gather_init(&iotlb_gather);
> -
>   	size = iova_align(iovad, size + iova_start_pad);
>   	addr -= iova_start_pad;
> -	unmapped = iommu_unmap_fast(domain, addr, size, &iotlb_gather);
> +	unmapped = iommu_unmap_fast(domain, addr, size, gather);
>   	WARN_ON(unmapped != size);
> +}
>   
> -	iommu_iotlb_sync(domain, &iotlb_gather);
> +/**
> + * dma_iova_unlink - Unlink a range of IOVA space
> + * @dev: DMA device
> + * @state: IOVA state
> + * @offset: offset into the IOVA state to unlink
> + * @size: size of the buffer
> + * @dir: DMA direction
> + * @attrs: attributes of mapping properties
> + *
> + * Unlink a range of IOVA space for the given IOVA state.
> + */
> +void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,
> +		size_t offset, size_t size, enum dma_data_direction dir,
> +		unsigned long attrs)
> +{
> +	struct iommu_iotlb_gather iotlb_gather;
> +
> +	/*
> +	 * The IOVA range stays owned by the caller and may be linked again
> +	 * right away, so the IOTLB has to be invalidated synchronously even
> +	 * for flush queue domains. The gather is therefore never queued and
> +	 * is retired right here.
> +	 */
> +	iommu_iotlb_gather_init(&iotlb_gather);
> +	__dma_iova_unlink(dev, state, offset, size, dir, attrs, &iotlb_gather);
> +	iommu_iotlb_sync(iommu_get_dma_domain(dev), &iotlb_gather);
>   }
>   EXPORT_SYMBOL_GPL(dma_iova_unlink);
>   
> @@ -2152,14 +2180,30 @@ void dma_iova_destroy(struct device *dev, struct dma_iova_state *state,
>   		size_t mapped_len, enum dma_data_direction dir,
>   		unsigned long attrs)
>   {
> -	if (mapped_len)
> -		dma_iova_unlink(dev, state, 0, mapped_len, dir, attrs);
> +	struct iommu_domain *domain = iommu_get_dma_domain(dev);
> +	struct iommu_iotlb_gather iotlb_gather;
> +
> +	iommu_iotlb_gather_init(&iotlb_gather);
>   
>   	/*
>   	 * We can be here if the first call to dma_iova_link() failed and
>   	 * there is nothing to unlink, so let's be more clear.
>   	 */
> -	dma_iova_free(dev, state);
> +	if (mapped_len) {
> +		iotlb_gather.queued = READ_ONCE(domain->iova_cookie->fq_domain);
> +		__dma_iova_unlink(dev, state, 0, mapped_len, dir, attrs,
> +				&iotlb_gather);
> +	}
> +
> +	/*
> +	 * Retire the gather together with the IOVA, like __iommu_dma_unmap()
> +	 * does: either invalidate the IOTLB now, or hand the IOVA space and
> +	 * any page table pages released by the unmap over to the flush queue,
> +	 * so the IOVA cannot be reused before the deferred invalidation.
> +	 */
> +	if (!iotlb_gather.queued)
> +		iommu_iotlb_sync(domain, &iotlb_gather);
> +	__dma_iova_free(dev, state, &iotlb_gather);
>   }
>   EXPORT_SYMBOL_GPL(dma_iova_destroy);
>   


      reply	other threads:[~2026-11-27 17:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20261127171534eucas1p1adc65490fcfb09b573d27aaa834f2912@eucas1p1.samsung.com>
2026-11-27 17:15 ` Marek Szyprowski
2026-11-27 17:30   ` Robin Murphy [this message]

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=90e159b3-ef50-4d03-937a-ced5896b152a@arm.com \
    --to=robin.murphy@arm.com \
    --cc=honghuan@amd.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=prakash.gupta@oss.qualcomm.com \
    --cc=sibi.sankar@oss.qualcomm.com \
    --cc=stable@vger.kernel.org \
    --cc=will@kernel.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

all inboxes | Powered by JetHome®