Batch IOTLB/dev-IOTLB invalidation
Subsystems (2)
v3 cover Batch IOTLB/dev-IOTLB invalidation
IOTLB and dev-IOTLB invalidation operations are performance-critical. The current implementation in the VT-d driver submits these commands individually, leading to some inefficiencies due to the IOMMU programming and invalidation command processing overhead for each operation.
This patch series enhances the efficiency of Queue Invalidation (QI) operations by adding support for batch processing. Microbenchmarks show that with a DSA device working in SVA, batching IOTLB and dev-IOTLB invalidations can decrease the time spent in qi_submit_sync() by roughly more than 800 cycles.
Changelog
v3:
* Rebased on 6.11-rc3.
* Updated commit messages which are revised by Baolu.
* Dropped the refactoring quirk_extra_dev_tlb_flush() patch.
* Added "Add qi_batch for dmar_domain" patch which is provided by Baolu.
v2:
* Rebased on 6.11-rc2.
* Updated commit messages.
* Added changes of refactoring IOTLB/Dev-IOTLB invalidation logic and quirk_extra_dev_tlb_flush() logic.
v1:
https://lore.kernel.org/linux-iommu/[email protected]/
Lu Baolu (1):
iommu/vt-d: Add qi_batch for dmar_domain
Tina Zhang (3):
iommu/vt-d: Factor out invalidation descriptor composition iommu/vt-d: Refactor IOTLB and Dev-IOTLB flush for batching iommu/vt-d: Introduce batched cache invalidation
drivers/iommu/intel/cache.c | 239 ++++++++++++++++++++++++++--------- drivers/iommu/intel/dmar.c | 93 +------------- drivers/iommu/intel/iommu.c | 6 +- drivers/iommu/intel/iommu.h | 126 ++++++++++++++++++ drivers/iommu/intel/nested.c | 1 + drivers/iommu/intel/svm.c | 5 +- 6 files changed, 316 insertions(+), 154 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/4 iommu/vt-d: Factor out invalidation descriptor composition
Separate the logic for constructing IOTLB and device TLB invalidation descriptors from the qi_flush interfaces. New helpers, qi_desc(), are introduced to encapsulate this common functionality.
Moving descriptor composition code to new helpers enables its reuse in the upcoming qi_batch interfaces.
No functional changes are intended.
--- drivers/iommu/intel/dmar.c | 93 ++---------------------------- drivers/iommu/intel/iommu.h | 109 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 87 deletions(-)
drivers/iommu/intel/dmar.c +6 -87
@@ -1526,24 +1526,9 @@ void qi_flush_context(struct intel_iommu *iommu, u16 did, u16 sid, u8 fm,void qi_flush_iotlb (struct intel_iommu *iommu,u16 did,u64 addr,unsigned int size_order,u64 type) { -u8 dw =0 , dr =0 ; -struct qi_desc desc; -int ih =0 ; - -if (cap_write_drain (iommu->cap)) - dw =1 ; - -if (cap_read_drain (iommu->cap)) - dr =1 ; - - desc.qw0 =QI_IOTLB_DID (did) |QI_IOTLB_DR (dr) |QI_IOTLB_DW (dw) - |QI_IOTLB_GRAN (type) |QI_IOTLB_TYPE ; - desc.qw1 =QI_IOTLB_ADDR (addr) |QI_IOTLB_IH (ih) - |QI_IOTLB_AM (size_order); - desc.qw2 =0 ; - desc.qw3 =0 ; +qi_desc_iotlb (iommu, did, addr, size_order, type, &desc);qi_submit_sync (iommu, &desc,1 ,0 ); }
@@ -1561,20 +1546,7 @@ void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,if (!(iommu->gcmd &DMA_GCMD_TE ))return ; -if (mask) { - addr |= (1ULL << (VTD_PAGE_SHIFT + mask -1 )) -1 ; - desc.qw1 =QI_DEV_IOTLB_ADDR (addr) |QI_DEV_IOTLB_SIZE ; - }else - desc.qw1 =QI_DEV_IOTLB_ADDR (addr); - -if (qdep >=QI_DEV_IOTLB_MAX_INVS ) - qdep =0 ; - - desc.qw0 =QI_DEV_IOTLB_SID (sid) |QI_DEV_IOTLB_QDEP (qdep) | -QI_DIOTLB_TYPE |QI_DEV_IOTLB_PFSID (pfsid); - desc.qw2 =0 ; - desc.qw3 =0 ; - +qi_desc_dev_iotlb (sid, pfsid, qdep, addr, mask, &desc);qi_submit_sync (iommu, &desc,1 ,0 ); }
@@ -1594,28 +1566,7 @@ void qi_flush_piotlb(struct intel_iommu *iommu, u16 did, u32 pasid, u64 addr,return ; } -if (npages ==-1 ) { - desc.qw0 =QI_EIOTLB_PASID (pasid) | -QI_EIOTLB_DID (did) | -QI_EIOTLB_GRAN (QI_GRAN_NONG_PASID ) | -QI_EIOTLB_TYPE ; - desc.qw1 =0 ; - }else { -int mask =ilog2 (__roundup_pow_of_two (npages)); -unsigned long align = (1ULL << (VTD_PAGE_SHIFT + mask)); - -if (WARN_ON_ONCE (!IS_ALIGNED (addr, align))) - addr =ALIGN_DOWN (addr, align); - - desc.qw0 =QI_EIOTLB_PASID (pasid) | -QI_EIOTLB_DID (did) | -QI_EIOTLB_GRAN (QI_GRAN_PSI_PASID ) | -QI_EIOTLB_TYPE ; - desc.qw1 =QI_EIOTLB_ADDR (addr) | -QI_EIOTLB_IH (ih) | -QI_EIOTLB_AM (mask); - } - +qi_desc_piotlb (did, pasid, addr, npages, ih, &desc);qi_submit_sync (iommu, &desc,1 ,0 ); }
@@ -1623,7 +1574,6 @@ void qi_flush_piotlb(struct intel_iommu *iommu, u16 did, u32 pasid, u64 addr,void qi_flush_dev_iotlb_pasid (struct intel_iommu *iommu,u16 sid,u16 pfsid,u32 pasid,u16 qdep,u64 addr,unsigned int size_order) { -unsigned long mask =1UL << (VTD_PAGE_SHIFT + size_order -1 );struct qi_desc desc = {.qw1 =0 , .qw2 =0 , .qw3 =0 };/*
@@ -1635,40 +1585,9 @@ void qi_flush_dev_iotlb_pasid(struct intel_iommu *iommu, u16 sid, u16 pfsid,if (!(iommu->gcmd &DMA_GCMD_TE ))return ; - desc.qw0 =QI_DEV_EIOTLB_PASID (pasid) |QI_DEV_EIOTLB_SID (sid) | -QI_DEV_EIOTLB_QDEP (qdep) |QI_DEIOTLB_TYPE | -QI_DEV_IOTLB_PFSID (pfsid); - -/* -* If S bit is 0, we only flush a single page. If S bit is set, -* The least significant zero bit indicates the invalidation address -* range. VT-d spec 6.5.2.6. -* e.g. address bit 12[0] indicates 8KB, 13[0] indicates 16KB. -* size order = 0 is PAGE_SIZE 4KB -* Max Invs Pending (MIP) is set to 0 for now until we have DIT in -* ECAP. -*/ -if (!IS_ALIGNED (addr,VTD_PAGE_SIZE << size_order)) -pr_warn_ratelimited ("Invalidate non-aligned address %llx, order %d\n" , - addr, size_order); - -/* Take page address */ - desc.qw1 =QI_DEV_EIOTLB_ADDR (addr); - -if (size_order) { -/* -* Existing 0s in address below size_order may be the least -* significant bit, we must set them to 1s to avoid having -* smaller size than desired. -*/ - desc.qw1 |=GENMASK_ULL (size_order +VTD_PAGE_SHIFT -1 , -VTD_PAGE_SHIFT ); -/* Clear size_order bit to indicate size */ - desc.qw1 &= ~mask; -/* Set the S bit to indicate flushing more than 1 page */ - desc.qw1 |=QI_DEV_EIOTLB_SIZE ; - } - +qi_desc_dev_iotlb_pasid (sid, pfsid, pasid, + qdep, addr, size_order, + &desc);qi_submit_sync (iommu, &desc,1 ,0 ); }
drivers/iommu/intel/iommu.h +109 -0
115 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/4 iommu/vt-d: Refactor IOTLB and Dev-IOTLB flush for batching
In reply to Tina ZhangExtracts IOTLB and Dev-IOTLB invalidation logic from cache tag flush interfaces into dedicated helper functions. It prepares the codebase for upcoming changes to support batched cache invalidations.
To enable direct use of qi_flush helpers in the new functions, iommu->flush.flush_iotlb and quirk_extra_dev_tlb_flush() are opened up.
No functional changes are intended.
--- drivers/iommu/intel/cache.c | 142 ++++++++++++++++++++---------------- drivers/iommu/intel/iommu.c | 5 +- drivers/iommu/intel/iommu.h | 3 + 3 files changed, 83 insertions(+), 67 deletions(-)
drivers/iommu/intel/cache.c +78 -64
@@ -255,6 +255,78 @@ static unsigned long calculate_psi_aligned_address(unsigned long start,return ALIGN_DOWN (start,VTD_PAGE_SIZE << mask); } +static void cache_tag_flush_iotlb (struct dmar_domain *domain,struct cache_tag *tag, +unsigned long addr,unsigned long pages, +unsigned long mask,int ih) +{ +struct intel_iommu *iommu = tag->iommu; +u64 type =DMA_TLB_PSI_FLUSH ; + +if (domain->use_first_level) { +qi_flush_piotlb (iommu, tag->domain_id, tag->pasid, addr, pages, ih); +return ; + } + +/* +* Fallback to domain selective flush if no PSI support or the size +* is too big. +*/ +if (!cap_pgsel_inv (iommu->cap) || + mask >cap_max_amask_val (iommu->cap) || pages ==-1 ) { + addr =0 ; + mask =0 ; + ih =0 ; + type =DMA_TLB_DSI_FLUSH ; + } + +if (ecap_qis (iommu->ecap)) +qi_flush_iotlb (iommu, tag->domain_id, addr | ih, mask, type); +else +__iommu_flush_iotlb (iommu, tag->domain_id, addr | ih, mask, type); +} + +static void cache_tag_flush_devtlb_psi (struct dmar_domain *domain,struct cache_tag *tag, +unsigned long addr,unsigned long mask) +{ +struct intel_iommu *iommu = tag->iommu; +struct device_domain_info *info; +u16 sid; + + info =dev_iommu_priv_get (tag->dev); + sid =PCI_DEVID (info->bus, info->devfn); + +if (tag->pasid ==IOMMU_NO_PASID ) { +qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep, + addr, mask); +if (info->dtlb_extra_inval) +qi_flush_dev_iotlb (iommu, sid, info->pfsid, + info->ats_qdep, addr, mask); +return ; + } + +qi_flush_dev_iotlb_pasid (iommu, sid, info->pfsid, tag->pasid, + info->ats_qdep, addr, mask); +if (info->dtlb_extra_inval) +qi_flush_dev_iotlb_pasid (iommu, sid, info->pfsid, tag->pasid, + info->ats_qdep, addr, mask); +} + +static void cache_tag_flush_devtlb_all (struct dmar_domain *domain,struct cache_tag *tag) +{ +struct intel_iommu *iommu = tag->iommu; +struct device_domain_info *info; +u16 sid; + + info =dev_iommu_priv_get (tag->dev); + sid =PCI_DEVID (info->bus, info->devfn); + +qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , +MAX_AGAW_PFN_WIDTH ); +if (info->dtlb_extra_inval) +qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , +MAX_AGAW_PFN_WIDTH ); +} +/* * Invalidates a range of IOVA from @start (inclusive) to @end (inclusive) * when the memory mappings in the target domain have been modified.
@@ -270,30 +342,10 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,spin_lock_irqsave (&domain->cache_lock, flags);list_for_each_entry (tag, &domain->cache_tags, node) { -struct intel_iommu *iommu = tag->iommu; -struct device_domain_info *info; -u16 sid; -switch (tag->type) {case CACHE_TAG_IOTLB :case CACHE_TAG_NESTING_IOTLB : -if (domain->use_first_level) { -qi_flush_piotlb (iommu, tag->domain_id, - tag->pasid, addr, pages, ih); - }else { -/* -* Fallback to domain selective flush if no -* PSI support or the size is too big. -*/ -if (!cap_pgsel_inv (iommu->cap) || - mask >cap_max_amask_val (iommu->cap)) - iommu->flush.flush_iotlb (iommu, tag->domain_id, -0 ,0 ,DMA_TLB_DSI_FLUSH ); -else - iommu->flush.flush_iotlb (iommu, tag->domain_id, - addr | ih, mask, -DMA_TLB_PSI_FLUSH ); - } +cache_tag_flush_iotlb (domain, tag, addr, pages, mask, ih);break ;case CACHE_TAG_NESTING_DEVTLB :/*
@@ -307,18 +359,7 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start, mask =MAX_AGAW_PFN_WIDTH ; fallthrough;case CACHE_TAG_DEVTLB : - info =dev_iommu_priv_get (tag->dev); - sid =PCI_DEVID (info->bus, info->devfn); - -if (tag->pasid ==IOMMU_NO_PASID ) -qi_flush_dev_iotlb (iommu, sid, info->pfsid, - info->ats_qdep, addr, mask); -else -qi_flush_dev_iotlb_pasid (iommu, sid, info->pfsid, - tag->pasid, info->ats_qdep, - addr, mask); - -quirk_extra_dev_tlb_flush (info, addr, mask, tag->pasid, info->ats_qdep); +cache_tag_flush_devtlb_psi (domain, tag, addr, mask);break ; }
@@ -338,29 +379,14 @@ void cache_tag_flush_all(struct dmar_domain *domain)spin_lock_irqsave (&domain->cache_lock, flags);list_for_each_entry (tag, &domain->cache_tags, node) { -struct intel_iommu *iommu = tag->iommu; -struct device_domain_info *info; -u16 sid; -switch (tag->type) {case CACHE_TAG_IOTLB :case CACHE_TAG_NESTING_IOTLB : -if (domain->use_first_level) -qi_flush_piotlb (iommu, tag->domain_id, - tag->pasid,0 ,-1 ,0 ); -else - iommu->flush.flush_iotlb (iommu, tag->domain_id, -0 ,0 ,DMA_TLB_DSI_FLUSH ); +cache_tag_flush_iotlb (domain, tag,0 ,-1 ,0 ,0 );break ;case CACHE_TAG_DEVTLB :case CACHE_TAG_NESTING_DEVTLB : - info =dev_iommu_priv_get (tag->dev); - sid =PCI_DEVID (info->bus, info->devfn); - -qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep, -0 ,MAX_AGAW_PFN_WIDTH ); -quirk_extra_dev_tlb_flush (info,0 ,MAX_AGAW_PFN_WIDTH , -IOMMU_NO_PASID , info->ats_qdep); +cache_tag_flush_devtlb_all (domain, tag);break ; }
@@ -399,20 +425,8 @@ void cache_tag_flush_range_np(struct dmar_domain *domain, unsigned long start, }if (tag->type ==CACHE_TAG_IOTLB || - tag->type ==CACHE_TAG_NESTING_IOTLB ) { -/* -* Fallback to domain selective flush if no -* PSI support or the size is too big. -*/ -if (!cap_pgsel_inv (iommu->cap) || - mask >cap_max_amask_val (iommu->cap)) - iommu->flush.flush_iotlb (iommu, tag->domain_id, -0 ,0 ,DMA_TLB_DSI_FLUSH ); -else - iommu->flush.flush_iotlb (iommu, tag->domain_id, - addr, mask, -DMA_TLB_PSI_FLUSH ); - } + tag->type ==CACHE_TAG_NESTING_IOTLB ) +cache_tag_flush_iotlb (domain, tag, addr, pages, mask,0 );trace_cache_tag_flush_range_np (tag, start, end, addr, pages, mask); }
drivers/iommu/intel/iommu.c +2 -3
@@ -1184,9 +1184,8 @@ static void __iommu_flush_context(struct intel_iommu *iommu,raw_spin_unlock_irqrestore (&iommu->register_lock, flag); } -/* return value determine if we need a write buffer flush */ -static void __iommu_flush_iotlb (struct intel_iommu *iommu,u16 did, -u64 addr,unsigned int size_order,u64 type) +void __iommu_flush_iotlb (struct intel_iommu *iommu,u16 did,u64 addr, +unsigned int size_order,u64 type) {int tlb_offset =ecap_iotlb_offset (iommu->ecap);u64 val =0 , val_iva =0 ;
drivers/iommu/intel/iommu.h +3 -0
@@ -1206,6 +1206,9 @@ void qi_flush_pasid_cache(struct intel_iommu *iommu, u16 did, u64 granu,int qi_submit_sync (struct intel_iommu *iommu,struct qi_desc *desc,unsigned int count,unsigned long options); + +void __iommu_flush_iotlb (struct intel_iommu *iommu,u16 did,u64 addr, +unsigned int size_order,u64 type);/* * Options used in qi_submit_sync: * QI_OPT_WAIT_DRAIN - Wait for PRQ drain completion, spec 6.5.2.8.
Signature · 1 line
2.34.1
PATCH v3 3/4 iommu/vt-d: Add qi_batch for dmar_domain
In reply to Tina ZhangFrom: Lu Baolu <[email protected]>
Introduces a qi_batch structure to hold batched cache invalidation descriptors on a per-dmar_domain basis. A fixed-size descriptor array is used for simplicity. The qi_batch is allocated when the first cache tag is added to the domain and freed during iommu_free_domain().
--- drivers/iommu/intel/cache.c | 7 +++++++ drivers/iommu/intel/iommu.c | 1 + drivers/iommu/intel/iommu.h | 14 ++++++++++++++ drivers/iommu/intel/nested.c | 1 + drivers/iommu/intel/svm.c | 5 ++++- 5 files changed, 27 insertions(+), 1 deletion(-)
drivers/iommu/intel/cache.c +7 -0
@@ -190,6 +190,13 @@ int cache_tag_assign_domain(struct dmar_domain *domain,u16 did =domain_get_id_for_dev (domain, dev);int ret; +/* domain->qi_bach will be freed in iommu_free_domain() path. */ +if (!domain->qi_batch) { + domain->qi_batch =kzalloc (sizeof (struct qi_batch ),GFP_KERNEL ); +if (!domain->qi_batch) +return -ENOMEM ; + } + ret =__cache_tag_assign_domain (domain, did, dev, pasid);if (ret || domain->domain.type !=IOMMU_DOMAIN_NESTED )return ret;
drivers/iommu/intel/iommu.c +1 -0
@@ -1586,6 +1586,7 @@ static void domain_exit(struct dmar_domain *domain)if (WARN_ON (!list_empty (&domain->devices)))return ; +kfree (domain->qi_batch);kfree (domain); }
drivers/iommu/intel/iommu.h +14 -0
@@ -584,6 +584,19 @@ struct iommu_domain_info {* to VT-d spec, section 9.3 */ }; +/* +* We start simply by using a fixed size for the batched descriptors. This +* size is currently sufficient for our needs. Future improvements could +* involve dynamically allocating the batch buffer based on actual demand, +* allowing us to adjust the batch size for optimal performance in different +* scenarios. +*/ +#define QI_MAX_BATCHED_DESC_COUNT 16 +struct qi_batch { +struct qi_desc descs[QI_MAX_BATCHED_DESC_COUNT ]; +unsigned int index; +}; +struct dmar_domain {int nid;/* node id */ struct xarray iommu_array;/* Attached IOMMU array */
@@ -608,6 +621,7 @@ struct dmar_domain {spinlock_t cache_lock;/* Protect the cache tag list */ struct list_head cache_tags;/* Cache tag list */ +struct qi_batch *qi_batch;/* Batched QI descriptors */ int iommu_superpage;/* Level of superpages supported: 0 == 4KiB (no superpages), 1 == 2MiB,
drivers/iommu/intel/nested.c +1 -0
@@ -83,6 +83,7 @@ static void intel_nested_domain_free(struct iommu_domain *domain)spin_lock (&s2_domain->s1_lock);list_del (&dmar_domain->s2_link);spin_unlock (&s2_domain->s1_lock); +kfree (dmar_domain->qi_batch);kfree (dmar_domain); }
drivers/iommu/intel/svm.c +4 -1
@@ -184,7 +184,10 @@ static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)static void intel_mm_free_notifier (struct mmu_notifier *mn) { -kfree (container_of(mn ,struct dmar_domain ,notifier )); + struct dmar_domain *domain = container_of(mn, struct dmar_domain, notifier); + +kfree (domain->qi_batch); +kfree (domain); }static const struct mmu_notifier_ops intel_mmuops = {
Signature · 1 line
2.34.1
PATCH v3 4/4 iommu/vt-d: Introduce batched cache invalidation
In reply to Tina ZhangConverts IOTLB and Dev-IOTLB invalidation to a batched model. Cache tag invalidation requests for a domain are now accumulated in a qi_batch structure before being flushed in bulk. It replaces the previous per- request qi_flush approach with a more efficient batching mechanism.
--- drivers/iommu/intel/cache.c | 122 +++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 15 deletions(-)
drivers/iommu/intel/cache.c +107 -15
@@ -262,6 +262,79 @@ static unsigned long calculate_psi_aligned_address(unsigned long start,return ALIGN_DOWN (start,VTD_PAGE_SIZE << mask); } +static void qi_batch_flush_descs (struct intel_iommu *iommu,struct qi_batch *batch) +{ +if (!iommu || !batch->index) +return ; + +qi_submit_sync (iommu, batch->descs, batch->index,0 ); + +/* Reset the index value and clean the whole batch buffer. */ +memset (batch,0 ,sizeof (*batch)); +} + +static void qi_batch_increment_index (struct intel_iommu *iommu,struct qi_batch *batch) +{ +if (++batch->index ==QI_MAX_BATCHED_DESC_COUNT ) +qi_batch_flush_descs (iommu, batch); +} + +static void qi_batch_add_iotlb (struct intel_iommu *iommu,u16 did,u64 addr, +unsigned int size_order,u64 type, +struct qi_batch *batch) +{ +qi_desc_iotlb (iommu, did, addr, size_order, type, &(batch->descs[batch->index])); +qi_batch_increment_index (iommu, batch); +} + +static void qi_batch_add_dev_iotlb (struct intel_iommu *iommu,u16 sid,u16 pfsid, +u16 qdep,u64 addr,unsigned int mask, +struct qi_batch *batch) +{ +/* +* According to VT-d spec, software is recommended to not submit any Device-TLB +* invalidation requests while address remapping hardware is disabled. +*/ +if (!(iommu->gcmd &DMA_GCMD_TE )) +return ; + +qi_desc_dev_iotlb (sid, pfsid, qdep, addr, mask, &(batch->descs[batch->index])); +qi_batch_increment_index (iommu, batch); +} + +static void qi_batch_add_piotlb (struct intel_iommu *iommu,u16 did,u32 pasid, +u64 addr,unsigned long npages,bool ih, +struct qi_batch *batch) +{ +/* +* npages == -1 means a PASID-selective invalidation, otherwise, +* a positive value for Page-selective-within-PASID invalidation. +* 0 is not a valid input. +*/ +if (!npages) +return ; + +qi_desc_piotlb (did, pasid, addr, npages, ih, &(batch->descs[batch->index])); +qi_batch_increment_index (iommu, batch); +} + +static void qi_batch_add_pasid_dev_iotlb (struct intel_iommu *iommu,u16 sid,u16 pfsid, +u32 pasid,u16 qdep,u64 addr, +unsigned int size_order,struct qi_batch *batch) +{ +/* +* According to VT-d spec, software is recommended to not submit any +* Device-TLB invalidation requests while address remapping hardware +* is disabled. +*/ +if (!(iommu->gcmd &DMA_GCMD_TE )) +return ; + +qi_desc_dev_iotlb_pasid (sid, pfsid, pasid, qdep, addr, size_order, + &(batch->descs[batch->index])); +qi_batch_increment_index (iommu, batch); +} +static void cache_tag_flush_iotlb (struct dmar_domain *domain,struct cache_tag *tag,unsigned long addr,unsigned long pages,unsigned long mask,int ih)
@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag *u64 type =DMA_TLB_PSI_FLUSH ;if (domain->use_first_level) { -qi_flush_piotlb (iommu, tag->domain_id, tag->pasid, addr, pages, ih); +qi_batch_add_piotlb (iommu, tag->domain_id, tag->pasid, addr, + pages, ih, domain->qi_batch);return ; }
@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * }if (ecap_qis (iommu->ecap)) -qi_flush_iotlb (iommu, tag->domain_id, addr | ih, mask, type); +qi_batch_add_iotlb (iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);else __iommu_flush_iotlb (iommu, tag->domain_id, addr | ih, mask, type); }
@@ -303,19 +378,20 @@ static void cache_tag_flush_devtlb_psi(struct dmar_domain *domain, struct cache_ sid =PCI_DEVID (info->bus, info->devfn);if (tag->pasid ==IOMMU_NO_PASID ) { -qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep, - addr, mask); +qi_batch_add_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep, + addr, mask, domain->qi_batch);if (info->dtlb_extra_inval) -qi_flush_dev_iotlb (iommu, sid, info->pfsid, - info->ats_qdep, addr, mask); +qi_batch_add_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep, + addr, mask, domain->qi_batch);return ; } -qi_flush_dev_iotlb_pasid (iommu, sid, info->pfsid, tag->pasid, - info->ats_qdep, addr, mask); +qi_batch_add_pasid_dev_iotlb (iommu, sid, info->pfsid, tag->pasid, + info->ats_qdep, addr, mask, domain->qi_batch);if (info->dtlb_extra_inval) -qi_flush_dev_iotlb_pasid (iommu, sid, info->pfsid, tag->pasid, - info->ats_qdep, addr, mask); +qi_batch_add_pasid_dev_iotlb (iommu, sid, info->pfsid, tag->pasid, + info->ats_qdep, addr, mask, + domain->qi_batch); }static void cache_tag_flush_devtlb_all (struct dmar_domain *domain,struct cache_tag *tag)
@@ -327,11 +403,11 @@ static void cache_tag_flush_devtlb_all(struct dmar_domain *domain, struct cache_ info =dev_iommu_priv_get (tag->dev); sid =PCI_DEVID (info->bus, info->devfn); -qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , -MAX_AGAW_PFN_WIDTH ); +qi_batch_add_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , +MAX_AGAW_PFN_WIDTH , domain->qi_batch);if (info->dtlb_extra_inval) -qi_flush_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , -MAX_AGAW_PFN_WIDTH ); +qi_batch_add_dev_iotlb (iommu, sid, info->pfsid, info->ats_qdep,0 , +MAX_AGAW_PFN_WIDTH , domain->qi_batch); }/*
@@ -341,6 +417,7 @@ static void cache_tag_flush_devtlb_all(struct dmar_domain *domain, struct cache_void cache_tag_flush_range (struct dmar_domain *domain,unsigned long start,unsigned long end,int ih) { +struct intel_iommu *iommu =NULL ;unsigned long pages, mask, addr;struct cache_tag *tag;unsigned long flags;
@@ -349,6 +426,10 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,spin_lock_irqsave (&domain->cache_lock, flags);list_for_each_entry (tag, &domain->cache_tags, node) { +if (iommu && iommu != tag->iommu) +qi_batch_flush_descs (iommu, domain->qi_batch); + iommu = tag->iommu; +switch (tag->type) {case CACHE_TAG_IOTLB :case CACHE_TAG_NESTING_IOTLB :
@@ -372,6 +453,7 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,trace_cache_tag_flush_range (tag, start, end, addr, pages, mask); } +qi_batch_flush_descs (iommu, domain->qi_batch);spin_unlock_irqrestore (&domain->cache_lock, flags); }
@@ -381,11 +463,16 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,*/ void cache_tag_flush_all (struct dmar_domain *domain) { +struct intel_iommu *iommu =NULL ;struct cache_tag *tag;unsigned long flags;spin_lock_irqsave (&domain->cache_lock, flags);list_for_each_entry (tag, &domain->cache_tags, node) { +if (iommu && iommu != tag->iommu) +qi_batch_flush_descs (iommu, domain->qi_batch); + iommu = tag->iommu; +switch (tag->type) {case CACHE_TAG_IOTLB :case CACHE_TAG_NESTING_IOTLB :
@@ -399,6 +486,7 @@ void cache_tag_flush_all(struct dmar_domain *domain)trace_cache_tag_flush_all (tag); } +qi_batch_flush_descs (iommu, domain->qi_batch);spin_unlock_irqrestore (&domain->cache_lock, flags); }
@@ -416,6 +504,7 @@ void cache_tag_flush_all(struct dmar_domain *domain)void cache_tag_flush_range_np (struct dmar_domain *domain,unsigned long start,unsigned long end) { +struct intel_iommu *iommu =NULL ;unsigned long pages, mask, addr;struct cache_tag *tag;unsigned long flags;
@@ -424,7 +513,9 @@ void cache_tag_flush_range_np(struct dmar_domain *domain, unsigned long start,spin_lock_irqsave (&domain->cache_lock, flags);list_for_each_entry (tag, &domain->cache_tags, node) { -struct intel_iommu *iommu = tag->iommu; +if (iommu && iommu != tag->iommu) +qi_batch_flush_descs (iommu, domain->qi_batch); + iommu = tag->iommu;if (!cap_caching_mode (iommu->cap) || domain->use_first_level) {iommu_flush_write_buffer (iommu);
@@ -437,5 +528,6 @@ void cache_tag_flush_range_np(struct dmar_domain *domain, unsigned long start,trace_cache_tag_flush_range_np (tag, start, end, addr, pages, mask); } +qi_batch_flush_descs (iommu, domain->qi_batch);spin_unlock_irqrestore (&domain->cache_lock, flags); }
Signature · 1 line
2.34.1
Re: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Quoted 13 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang <[email protected]> wrote:
(quoted diff) +2
-1@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * u64 type = DMA_TLB_PSI_FLUSH; if (domain->use_first_level) { - qi_flush_piotlb(iommu, tag->domain_id, tag->pasid, addr, pages, ih); + qi_batch_add_piotlb(iommu, tag->domain_id, tag->pasid, addr, + pages, ih, domain->qi_batch); return;(quoted diff) +0
-0}
Review static void cache_tag_flush_iotlb(struct · line 368
Quoted 9 lines
(quoted diff) +2
-1@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * } if (ecap_qis(iommu->ecap)) - qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);
If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
Baolu Lu
maintainerRe: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Quoted 18 lines · Jacob Pan
Quoted 15 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang <[email protected]> wrote:
(quoted diff) +2
-1@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * u64 type = DMA_TLB_PSI_FLUSH; if (domain->use_first_level) { - qi_flush_piotlb(iommu, tag->domain_id, tag->pasid, addr, pages, ih); + qi_batch_add_piotlb(iommu, tag->domain_id, tag->pasid, addr, + pages, ih, domain->qi_batch); return;}(quoted diff) +0
-0@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * }
Quoted 10 lines
Review static void cache_tag_flush_iotlb(struct · line 368
Quoted 7 lines
(quoted diff) +2
-1if (ecap_qis(iommu->ecap)) - qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
Thanks,
baolu
Re: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Quoted 25 lines · Baolu Lu
Quoted 16 lines · Jacob Pan
Quoted 13 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang <[email protected]> wrote:
(quoted diff) +2
-1@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * u64 type = DMA_TLB_PSI_FLUSH; if (domain->use_first_level) { - qi_flush_piotlb(iommu, tag->domain_id, tag->pasid, addr, pages, ih); + qi_batch_add_piotlb(iommu, tag->domain_id, tag->pasid, addr, + pages, ih, domain->qi_batch); return; }@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * } if (ecap_qis(iommu->ecap))Quoted 8 lines
Review static void cache_tag_flush_iotlb(struct · line 366
Quoted 5 lines
(quoted diff) +2
-1- qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
Quoted 5 lines
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
Hi Jacob,
Do you mean the configuration that iommu.strict==0? :) As the above explanation from Baolu, this patch is not for that although it uses the term "batched". Also, it would reduce the VMExits that due to the IOTLB/DevTLB invalidation a lot in the virtualization environment.
Signature · 2 lines
Regards, Yi Liu
Re: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Hi Yi,
Quoted 32 lines · On Mon, 19 Aug 2024 15:58:35 +0800 Yi Liu <[email protected]> wrote:
Quoted 25 lines · Baolu Lu
Quoted 16 lines · Jacob Pan
Quoted 13 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang <[email protected]> wrote:
(quoted diff) +0
-0@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct
dmar_domain*domain, structcache_tag* u64 type =DMA_TLB_PSI_FLUSH; if (domain->use_first_level) { -qi_flush_piotlb(iommu, tag->domain_id, tag->pasid, addr, pages, ih);
+ qi_batch_add_piotlb(iommu,tag->domain_id,tag->pasid, addr,
+ pages, ih,domain->qi_batch); return;
}(quoted diff) +0
-0@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * } if (ecap_qis(iommu->ecap))Quoted 8 lines
Review static void cache_tag_flush_iotlb(struct · line 366
Quoted 5 lines
(quoted diff) +2
-1- qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
Quoted 5 lines
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
Quoted 7 lines
Hi Jacob,
Do you mean the configuration that iommu.strict==0? :) As the above explanation from Baolu, this patch is not for that although it uses the term "batched". Also, it would reduce the VMExits that due to the IOTLB/DevTLB invalidation a lot in the virtualization environment.
No, I understand this is a different "batch", not for deferred flush. I am asking why it has to gather QI_MAX_BATCHED_DESC_COUNT before it does the flush. See my other reply.
Thanks,
Jacob
Re: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
In reply to Baolu LuQuoted 29 lines · On Sat, 17 Aug 2024 11:28:21 +0800 Baolu Lu <[email protected]> wrote:
Quoted 18 lines · Jacob Pan
Quoted 15 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang <[email protected]> wrote:
(quoted diff) +0
-0@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct
dmar_domain*domain, structcache_tag* u64 type =DMA_TLB_PSI_FLUSH;
if (domain->use_first_level) {(quoted diff) +0
-1- qi_flush_piotlb(iommu, tag->domain_id, tag->pasid,addr, pages, ih);
(quoted diff) +1
-0+ qi_batch_add_piotlb(iommu, tag->domain_id,
tag->pasid, addr,drivers/iommu/intel/cache.c +1
-0+ pages, ih, domain->qi_batch);return ; }@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * }Quoted 10 lines
Review static void cache_tag_flush_iotlb(struct · line 368
Quoted 7 lines
(quoted diff) +2
-1if (ecap_qis(iommu->ecap)) - qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
Quoted 5 lines
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
I was looking at the code below, if the index does not reach QI_MAX_BATCHED_DESC_COUNT. There will be no flush after cache_tag_flush_iotlb() returns, right?
+static void qi_batch_increment_index(structintel_iommu *iommu, struct qi_batch *batch) +{
+ if (++batch->index == QI_MAX_BATCHED_DESC_COUNT)
+ qi_batch_flush_descs(iommu, batch);
+}
+
+static void qi_batch_add_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,
+ unsigned int size_order, u64 type,
+ struct qi_batch *batch)
+{
+ qi_desc_iotlb(iommu, did, addr, size_order, type,
&(batch->descs[batch->index]));
+ qi_batch_increment_index(iommu, batch);
+}
Quoted 2 lines
Thanks,
baolu
Baolu Lu
maintainerRe: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Quoted 32 lines · Jacob Pan
Quoted 29 lines · On Sat, 17 Aug 2024 11:28:21 +0800 Baolu Lu<[email protected]> wrote:
Quoted 28 lines · Jacob Pan
Quoted 13 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang<[email protected]> wrote:
(quoted diff) +0
-0@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct
dmar_domain*domain, structcache_tag* u64 type =DMA_TLB_PSI_FLUSH; if (domain->use_first_level) {(quoted diff) +0
-1- qi_flush_piotlb(iommu, tag->domain_id, tag->pasid,addr, pages, ih);
(quoted diff) +1
-0+ qi_batch_add_piotlb(iommu, tag->domain_id,
tag->pasid, addr,drivers/iommu/intel/cache.c +1
-0+ pages, ih, domain->qi_batch);return ; }drivers/iommu/intel/cache.c static void cache_tag_flush_iotlb(struct · line 368
Quoted 9 lines
drivers/iommu/intel/cache.c +2
-1@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * }if (ecap_qis (iommu->ecap)) -qi_flush_iotlb (iommu, tag->domain_id, addr | ih, mask, type); +qi_batch_add_iotlb (iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
Quoted 8 lines
Quoted 5 lines
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
I was looking at the code below, if the index does not reach
QI_MAX_BATCHED_DESC_COUNT. There will be no flush aftercache_tag_flush_iotlb()returns, right?
No. qi_batch_flush_descs() is called explicitly before return.
@@ -341,6 +417,7 @@ static void cache_tag_flush_devtlb_all(struct dmar_domain *domain, struct cache_
void cache_tag_flush_range(struct dmar_domain *domain, unsigned long
start,
unsigned long end, int ih)
{
+ struct intel_iommu *iommu = NULL;
unsigned long pages, mask, addr;
struct cache_tag *tag;
unsigned long flags;@@ -349,6 +426,10 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,
spin_lock_irqsave(&domain->cache_lock, flags);
list_for_each_entry(tag, &domain->cache_tags, node) {+ if (iommu && iommu != tag->iommu)
+ qi_batch_flush_descs(iommu, domain->qi_batch);
+ iommu = tag->iommu;
+
switch (tag->type) {
case CACHE_TAG_IOTLB:
case CACHE_TAG_NESTING_IOTLB:@@ -372,6 +453,7 @@ void cache_tag_flush_range(struct dmar_domain *domain, unsigned long start,
trace_cache_tag_flush_range(tag, start, end, addr,
pages, mask);
}
+ qi_batch_flush_descs(iommu, domain->qi_batch);
spin_unlock_irqrestore(&domain->cache_lock, flags);}
Thanks,
baolu
Re: [PATCH v3 4/4] iommu/vt-d: Introduce batched cache invalidation
Hi Baolu,
Quoted 38 lines · On Tue, 20 Aug 2024 10:06:05 +0800 Baolu Lu <[email protected]> wrote:
Quoted 37 lines · Jacob Pan
Quoted 34 lines · On Sat, 17 Aug 2024 11:28:21 +0800 Baolu Lu<[email protected]> wrote:
Quoted 18 lines · Jacob Pan
Quoted 15 lines · On Thu, 15 Aug 2024 14:52:21 +0800 Tina Zhang<[email protected]> wrote:
(quoted diff) +0
-0@@ -270,7 +343,8 @@ static void cache_tag_flush_iotlb(struct
dmar_domain*domain, structcache_tag* u64 type =DMA_TLB_PSI_FLUSH; if (domain->use_first_level) {(quoted diff) +0
-1- qi_flush_piotlb(iommu, tag->domain_id,
tag->pasid, addr, pages, ih);(quoted diff) +1
-0+ qi_batch_add_piotlb(iommu, tag->domain_id,
tag->pasid, addr,(quoted diff) +1
-0+ pages, ih,domain->qi_batch); return; }(quoted diff) +0
-0@@ -287,7 +361,8 @@ static void cache_tag_flush_iotlb(struct dmar_domain *domain, struct cache_tag * }Quoted 10 lines
Review static void cache_tag_flush_iotlb(struct · line 368
Quoted 7 lines
(quoted diff) +2
-1if (ecap_qis(iommu->ecap)) - qi_flush_iotlb(iommu, tag->domain_id, addr | ih, mask, type); + qi_batch_add_iotlb(iommu, tag->domain_id, addr | ih, mask, type, + domain->qi_batch);If I understand this correctly, IOTLB flush maybe deferred until the batch array is full, right? If so, is there a security gap where callers think the mapping is gone after the call returns?
No. All related caches are flushed before function return. A domain can have multiple cache tags. Previously, we sent individual cache invalidation requests to hardware. This change combines all necessary invalidation requests into a single batch and raise them to hardware together to make it more efficient.
Quoted 5 lines
Quoted 3 lines
I was looking at the code below, if the index does not reach
QI_MAX_BATCHED_DESC_COUNT. There will be no flush aftercache_tag_flush_iotlb()returns, right?No.
qi_batch_flush_descs()is called explicitly before return.
I see, cache_tag_flush_iotlb() is really just adding descriptors to the batch. Not doing any flush for most cases. IMHO, the name is a little confusing.
Thanks,
Jacob
Baolu Lu
maintainerQuoted 36 lines · Tina Zhang
IOTLB and dev-IOTLB invalidation operations are performance-critical. The current implementation in the VT-d driver submits these commands individually, leading to some inefficiencies due to the IOMMU programming and invalidation command processing overhead for each operation.
This patch series enhances the efficiency of Queue Invalidation (QI) operations by adding support for batch processing. Microbenchmarks show that with a DSA device working in SVA, batching IOTLB and dev-IOTLB invalidations can decrease the time spent in
qi_submit_sync()by roughly more than 800 cycles.Changelog
v3:
* Rebased on 6.11-rc3.
* Updated commit messages which are revised by Baolu.
* Dropped the refactoringquirk_extra_dev_tlb_flush()patch.
* Added "Addqi_batchfordmar_domain" patch which is provided by Baolu.v2:
* Rebased on 6.11-rc2.
* Updated commit messages.
* Added changes of refactoring IOTLB/Dev-IOTLB invalidation logic andquirk_extra_dev_tlb_flush()logic.v1:
https://lore.kernel.org/linux-iommu/[email protected]/Lu Baolu (1):
iommu/vt-d: Addqi_batchfordmar_domainTina Zhang (3):
iommu/vt-d: Factor out invalidation descriptor composition iommu/vt-d: Refactor IOTLB and Dev-IOTLB flush for batching iommu/vt-d: Introduce batched cache invalidation
Quoted 7 lines
drivers/iommu/intel/cache.c | 239 ++++++++++++++++++++++++++--------- drivers/iommu/intel/dmar.c | 93 +------------- drivers/iommu/intel/iommu.c | 6 +- drivers/iommu/intel/iommu.h | 126 ++++++++++++++++++ drivers/iommu/intel/nested.c | 1 + drivers/iommu/intel/svm.c | 5 +- 6 files changed, 316 insertions(+), 154 deletions(-)
Queued for v6.12-rc1.
Thanks,
baolu