Revert "iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper"
Subsystems (4)
PATCH 1/1 Revert "iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper"
This reverts commit 1acce29926d93f3eba427696fe3da6451504dae2.
Before this commit, dma_iova_destroy() unmapped through __iommu_dma_iova_unlink(..., free_iova=true), which set iotlb_gather.queued from cookie->fq_domain, skipped iommu_iotlb_sync() and freed the IOVA with the queued gather, exactly like __iommu_dma_unmap(). After it, dma_iova_destroy() calls dma_iova_unlink() and then dma_iova_free():
- dma_iova_unlink() never marks its gather as queued 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 through free_iova_fast() and never through queue_iova().
DMA-FQ domains therefore get strict invalidation on this path. That includes the block layer's blk_rq_dma_map() path, which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O.
hw/sw details: Qualcomm Glymur CRD: SMMUv3 with no RANGE_INV,
4K pages; NVMe SSD on PCIe Gen5 x4
Fio sequential read/write numbers:
Domain seq write seq read DMA (strict) 2694 MiB/s 2693 MiB/s DMA-FQ (lazy), flush queue 2707 MiB/s 2803 MiB/s
bypassed (this regression)
DMA-FQ (lazy), flush queue 10.6 GiB/s 11.9 GiB/s
used by dma_iova_destroy()
--- drivers/iommu/dma-iommu.c | 57 ++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 22 deletions(-)
drivers/iommu/dma-iommu.c +35 -22
@@ -2101,20 +2101,10 @@ 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. -*/ -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) +static void __iommu_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, +bool free_iova) {struct iommu_domain *domain =iommu_get_dma_domain (dev);struct iommu_dma_cookie *cookie = domain->iova_cookie;
@@ -2130,13 +2120,35 @@ void dma_iova_unlink(struct device *dev, struct dma_iova_state *state,iommu_dma_iova_unlink_range_slow (dev, addr, size, dir, attrs);iommu_iotlb_gather_init (&iotlb_gather); + iotlb_gather.queued = free_iova &&READ_ONCE (cookie->fq_domain); size =iova_align (iovad, size + iova_start_pad); addr -= iova_start_pad; unmapped =iommu_unmap_fast (domain, addr, size, &iotlb_gather);WARN_ON (unmapped != size); -iommu_iotlb_sync (domain, &iotlb_gather); +if (!iotlb_gather.queued) +iommu_iotlb_sync (domain, &iotlb_gather); +if (free_iova) +iommu_dma_free_iova (domain, addr, size, &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) +{ +__iommu_dma_iova_unlink (dev, state, offset, size, dir, attrs, false); }EXPORT_SYMBOL_GPL (dma_iova_unlink);
@@ -2157,13 +2169,14 @@ void dma_iova_destroy(struct device *dev, struct dma_iova_state *state, unsigned long attrs) {if (mapped_len) -dma_iova_unlink (dev, state,0 , mapped_len, dir, attrs); - -/* -* 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); +__iommu_dma_iova_unlink (dev, state,0 , mapped_len, dir, attrs, + true); +else +/* +* We can be here if first call to dma_iova_link() failed and +* there is nothing to unlink, so let's be more clear. +*/ +dma_iova_free (dev, state); }EXPORT_SYMBOL_GPL (dma_iova_destroy);
Signature · 1 line
2.34.1
Quoted 26 lines · Sibi Sankar
This reverts commit
1acce29926d93f3eba427696fe3da6451504dae2.Before this commit,
dma_iova_destroy()unmapped through__iommu_dma_iova_unlink(..., free_iova=true), which set iotlb_gather.queued fromcookie->fq_domain, skippediommu_iotlb_sync()and freed the IOVA with the queued gather, exactly like__iommu_dma_unmap(). After it,dma_iova_destroy()callsdma_iova_unlink()and thendma_iova_free():-
dma_iova_unlink()never marks its gather as queued and always callsiommu_iotlb_sync()afteriommu_unmap_fast().
-dma_iova_free()passes a NULL gather toiommu_dma_free_iova(), so the IOVA goes straight back to the allocator throughfree_iova_fast()and never throughqueue_iova().DMA-FQ domains therefore get strict invalidation on this path. That includes the block layer's
blk_rq_dma_map()path, which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O.hw/sw details: Qualcomm Glymur CRD: SMMUv3 with no
RANGE_INV,
4K pages; NVMe SSD on PCIe Gen5 x4Fio sequential read/write numbers:
Domain seq write seq read DMA (strict) 2694 MiB/s 2693 MiB/s DMA-FQ (lazy), flush queue 2707 MiB/s 2803 MiB/s
Quoted 7 lines
bypassed (this regression)
DMA-FQ (lazy), flush queue 10.6 GiB/s 11.9 GiB/s
used bydma_iova_destroy()Fixes 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper")Cc [email protected] # v7.3+Signed-off-by Sibi Sankar <[email protected]>
The performance data clearly shows that the flush queue was there for the reason. Together with this revert it would probably make sense to apply the v1 of the reverted patch:
https://lore.kernel.org/all/[email protected]/
Leon, do You see any other option?
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Quoted 36 lines · Marek Szyprowski
Quoted 26 lines · Sibi Sankar
This reverts commit
1acce29926d93f3eba427696fe3da6451504dae2.Before this commit,
dma_iova_destroy()unmapped through__iommu_dma_iova_unlink(..., free_iova=true), which set iotlb_gather.queued fromcookie->fq_domain, skippediommu_iotlb_sync()and freed the IOVA with the queued gather, exactly like__iommu_dma_unmap(). After it,dma_iova_destroy()callsdma_iova_unlink()and thendma_iova_free():-
dma_iova_unlink()never marks its gather as queued and always callsiommu_iotlb_sync()afteriommu_unmap_fast().
-dma_iova_free()passes a NULL gather toiommu_dma_free_iova(), so the IOVA goes straight back to the allocator throughfree_iova_fast()and never throughqueue_iova().DMA-FQ domains therefore get strict invalidation on this path. That includes the block layer's
blk_rq_dma_map()path, which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O.hw/sw details: Qualcomm Glymur CRD: SMMUv3 with no
RANGE_INV,
4K pages; NVMe SSD on PCIe Gen5 x4Fio sequential read/write numbers:
Domain seq write seq read DMA (strict) 2694 MiB/s 2693 MiB/s DMA-FQ (lazy), flush queue 2707 MiB/s 2803 MiB/sQuoted 7 lines
bypassed (this regression)
DMA-FQ (lazy), flush queue 10.6 GiB/s 11.9 GiB/s
used bydma_iova_destroy()Fixes 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper")Cc [email protected] # v7.3+Signed-off-by Sibi Sankar <[email protected]>
Quoted 7 lines
The performance data clearly shows that the flush queue was there for the reason. Together with this revert it would probably make sense to apply the v1 of the reverted patch:
https://lore.kernel.org/all/[email protected]/
Leon, do You see any other option?
Oh, there are a couple of issues here too, and reverting isn't fixing the main problem with this..
The issue is the assumption we can separate the sync from the free:
if (!iotlb_gather.queued)
iommu_iotlb_sync(domain, &iotlb_gather);
if (free_iova)
iommu_dma_free_iova(domain, addr, size, &iotlb_gather);We cannot. The gather must always be retired, either be calling iommu_iotlb_sync() or by calling iommu_dma_free_iova(). It can never just be left dangling.
Further you can't call iommu_dma_free_iova() with a NULL gather once unlink has been called. That risks things out of order where the rbtree can show the freed iova before the associated queued flush has happened. That is a correctness issue.
I suppose the resolution here is to feed the gather up the call chain and allow it to be retired correctly instead of trying to short circuit it inside the API.
Jason
Quoted 59 lines · Jason Gunthorpe
Quoted 36 lines · Marek Szyprowski
Quoted 26 lines · Sibi Sankar
This reverts commit
1acce29926d93f3eba427696fe3da6451504dae2.Before this commit,
dma_iova_destroy()unmapped through__iommu_dma_iova_unlink(..., free_iova=true), which set iotlb_gather.queued fromcookie->fq_domain, skippediommu_iotlb_sync()and freed the IOVA with the queued gather, exactly like__iommu_dma_unmap(). After it,dma_iova_destroy()callsdma_iova_unlink()and thendma_iova_free():-
dma_iova_unlink()never marks its gather as queued and always callsiommu_iotlb_sync()afteriommu_unmap_fast().
-dma_iova_free()passes a NULL gather toiommu_dma_free_iova(), so the IOVA goes straight back to the allocator throughfree_iova_fast()and never throughqueue_iova().DMA-FQ domains therefore get strict invalidation on this path. That includes the block layer's
blk_rq_dma_map()path, which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O.hw/sw details: Qualcomm Glymur CRD: SMMUv3 with no
RANGE_INV,
4K pages; NVMe SSD on PCIe Gen5 x4Fio sequential read/write numbers:
Domain seq write seq read DMA (strict) 2694 MiB/s 2693 MiB/s DMA-FQ (lazy), flush queue 2707 MiB/s 2803 MiB/sQuoted 7 lines
bypassed (this regression)
DMA-FQ (lazy), flush queue 10.6 GiB/s 11.9 GiB/s
used bydma_iova_destroy()Fixes 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper")Cc [email protected] # v7.3+Signed-off-by Sibi Sankar <[email protected]>Quoted 7 lines
The performance data clearly shows that the flush queue was there for the reason. Together with this revert it would probably make sense to apply the v1 of the reverted patch:
https://lore.kernel.org/all/[email protected]/
Leon, do You see any other option?
Oh, there are a couple of issues here too, and reverting isn't fixing the main problem with this..
The issue is the assumption we can separate the sync from the free:
if (!iotlb_gather.queued) iommu_iotlb_sync(domain, &iotlb_gather); if (free_iova) iommu_dma_free_iova(domain, addr, size, &iotlb_gather);We cannot. The gather must always be retired, either be calling
iommu_iotlb_sync()or by callingiommu_dma_free_iova(). It can never just be left dangling.
Quoted 8 lines
Further you can't call
iommu_dma_free_iova()with a NULL gather once unlink has been called. That risks things out of order where the rbtree can show the freed iova before the associated queued flush has happened. That is a correctness issue.I suppose the resolution here is to feed the gather up the call chain and allow it to be retired correctly instead of trying to short circuit it inside the API.
I recall trying to cache the gather operation in an earlier revisions of link/unlink series, but that approach was not well received.
Thanks
Quoted 3 lines
Jason
Quoted 36 lines · Marek Szyprowski
Quoted 26 lines · Sibi Sankar
This reverts commit
1acce29926d93f3eba427696fe3da6451504dae2.Before this commit,
dma_iova_destroy()unmapped through__iommu_dma_iova_unlink(..., free_iova=true), which set iotlb_gather.queued fromcookie->fq_domain, skippediommu_iotlb_sync()and freed the IOVA with the queued gather, exactly like__iommu_dma_unmap(). After it,dma_iova_destroy()callsdma_iova_unlink()and thendma_iova_free():-
dma_iova_unlink()never marks its gather as queued and always callsiommu_iotlb_sync()afteriommu_unmap_fast().
-dma_iova_free()passes a NULL gather toiommu_dma_free_iova(), so the IOVA goes straight back to the allocator throughfree_iova_fast()and never throughqueue_iova().DMA-FQ domains therefore get strict invalidation on this path. That includes the block layer's
blk_rq_dma_map()path, which NVMe uses for all data transfers, so iommu.strict=0 has no effect on NVMe I/O.hw/sw details: Qualcomm Glymur CRD: SMMUv3 with no
RANGE_INV,
4K pages; NVMe SSD on PCIe Gen5 x4Fio sequential read/write numbers:
Domain seq write seq read DMA (strict) 2694 MiB/s 2693 MiB/s DMA-FQ (lazy), flush queue 2707 MiB/s 2803 MiB/sQuoted 7 lines
bypassed (this regression)
DMA-FQ (lazy), flush queue 10.6 GiB/s 11.9 GiB/s
used bydma_iova_destroy()Fixes 1acce29926d9 ("iommu/dma: simplify dma_iova_destroy() and drop the free_iova helper")Cc [email protected] # v7.3+Signed-off-by Sibi Sankar <[email protected]>
Quoted 7 lines
The performance data clearly shows that the flush queue was there for the reason. Together with this revert it would probably make sense to apply the v1 of the reverted patch:
https://lore.kernel.org/all/[email protected]/
Leon, do You see any other option?
These numbers shows clearly that we had iotlb_gather.queued != 0 and never performed iommu_iotlb_sync(domain, &iotlb_gather); while destroying mapped IOVA.
I agree with you, v1 is the easiest solution, however my preferred one is to cache gather when it is created and keep it in dma_iova_state structure for whole life cycle of that struct.
Thanks
Quoted 7 lines
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland