Intel IOMMU updates for v6.16
Subsystems (2)
Lu Baolu
maintainerHi Joerg,
The following changes have been queued for v6.16-rc1. They are about new features and code refactoring, including:
- Restore WO permissions on second-level paging entries
- Use ida to manage domain id
- Miscellaneous cleanups
These patches are based on v6.15-rc6. Please consider them for the iommu/vt-d branch.
Best regards,
baolu
Jason Gunthorpe (1):
iommu/vt-d: Restore WO permissions on second-level paging entries
Lu Baolu (2):
iommu/vt-d: Use ida to manage domain id
iommu/vt-d: Replace spin_lock with mutex to protect domain ida
Wei Wang (2):
iommu/vt-d: Eliminate pci_physfn() in dmar_find_matched_satc_unit() iommu/vt-d: Change dmar_ats_supported() to return boolean
drivers/iommu/intel/dmar.c | 4 ++ drivers/iommu/intel/iommu.c | 113 ++++++++++-------------------------- drivers/iommu/intel/iommu.h | 21 +++++-- 3 files changed, 51 insertions(+), 87 deletions(-)
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 1/5 iommu/vt-d: Restore WO permissions on second-level paging entries
From: Jason Gunthorpe <[email protected]>
VT-D HW can do WO permissions on the second-stage but not the first-stage page table formats. The commit eea53c581688 ("iommu/vt-d: Remove WO permissions on second-level paging entries") wanted to make this uniform for VT-D by disabling the support for WO permissions in the second-stage.
This isn't consistent with how other drivers are working. Instead if the underlying HW can support WO, it should. For instance AMD already supports WO on its second stage (v1) format and not its first (v2).
If WO support needs to be discoverable it should be done through an iommu_domain capability flag.
--- drivers/iommu/intel/iommu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +1 -2
@@ -1681,9 +1681,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, } attr = prot & (DMA_PTE_READ |DMA_PTE_WRITE |DMA_PTE_SNP ); - attr |=DMA_FL_PTE_PRESENT ;if (domain->use_first_level) { - attr |=DMA_FL_PTE_US |DMA_FL_PTE_ACCESS ; + attr |=DMA_FL_PTE_PRESENT |DMA_FL_PTE_US |DMA_FL_PTE_ACCESS ;if (prot &DMA_PTE_WRITE ) attr |=DMA_FL_PTE_DIRTY ; }
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 2/5 iommu/vt-d: Use ida to manage domain id
In reply to Lu BaoluSwitch the intel iommu driver to use the ida mechanism for managing domain IDs, replacing the previous fixed-size bitmap.
The previous approach allocated a bitmap large enough to cover the maximum number of domain IDs supported by the hardware, regardless of the actual number of domains in use. This led to unnecessary memory consumption, especially on systems supporting a large number of iommu units but only utilizing a small number of domain IDs.
The ida allocator dynamically manages the allocation and freeing of integer IDs, only consuming memory for the IDs that are currently in use. This significantly optimizes memory usage compared to the fixed-size bitmap.
--- drivers/iommu/intel/dmar.c | 3 ++ drivers/iommu/intel/iommu.c | 80 ++++++++----------------------------- drivers/iommu/intel/iommu.h | 19 +++++++-- 3 files changed, 34 insertions(+), 68 deletions(-)
drivers/iommu/intel/dmar.c +3 -0
@@ -1099,6 +1099,8 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)spin_lock_init (&iommu->device_rbtree_lock);mutex_init (&iommu->iopf_lock); iommu->node =NUMA_NO_NODE ; +spin_lock_init (&iommu->lock); +ida_init (&iommu->domain_ida); ver =readl (iommu->reg +DMAR_VER_REG ); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
@@ -1195,6 +1197,7 @@ static void free_iommu(struct intel_iommu *iommu)if (iommu->reg)unmap_iommu (iommu); +ida_destroy (&iommu->domain_ida);ida_free (&dmar_seq_ids, iommu->seq_id);kfree (iommu); }
drivers/iommu/intel/iommu.c +16 -64
150 lines not on this page · raw message
drivers/iommu/intel/iommu.h +15 -4
@@ -722,7 +722,7 @@ struct intel_iommu {unsigned char name[16 ];/* Device Name */ #ifdef CONFIG_INTEL_IOMMU -unsigned long *domain_ids;/* bitmap of domains */ +struct ida domain_ida;/* domain id allocator */ unsigned long *copied_tables;/* bitmap of copied tables */ spinlock_t lock;/* protect context, domain ids */ struct root_entry *root_entry;/* virtual address */
@@ -809,11 +809,22 @@ static inline struct dmar_domain *to_dmar_domain(struct iommu_domain *dom) }/* -* Domain ID reserved for pasid entries programmed for first-level -* only and pass-through transfer modes. +* Domain ID 0 and 1 are reserved: +* +* If Caching mode is set, then invalid translations are tagged +* with domain-id 0, hence we need to pre-allocate it. We also +* use domain-id 0 as a marker for non-allocated domain-id, so +* make sure it is not used for a real domain. +* +* Vt-d spec rev3.0 (section 6.2.3.1) requires that each pasid +* entry for first-level or pass-through translation modes should +* be programmed with a domain id different from those used for +* second-level or nested translation. We reserve a domain id for +* this purpose. This domain id is also used for identity domain +* in legacy mode. */ #define FLPT_DEFAULT_DID 1 -#define NUM_RESERVED_DID 2 +#define IDA_START_DID 2/* Retrieve the domain ID which has allocated to the domain */ static inline u16
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 3/5 iommu/vt-d: Replace spin_lock with mutex to protect domain ida
In reply to Lu BaoluThe domain ID allocator is currently protected by a spin_lock. However, ida_alloc_range can potentially block if it needs to allocate memory to grow its internal structures.
Replace the spin_lock with a mutex which allows sleep on block. Thus, the memory allocation flags can be updated from GFP_ATOMIC to GFP_KERNEL to allow blocking memory allocations if necessary.
Introduce a new mutex, did_lock, specifically for protecting the domain ida. The existing spinlock will remain for protecting other intel_iommu fields.
--- drivers/iommu/intel/dmar.c | 1 + drivers/iommu/intel/iommu.c | 12 ++++-------- drivers/iommu/intel/iommu.h | 2 ++ 3 files changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/intel/dmar.c +1 -0
@@ -1101,6 +1101,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) iommu->node =NUMA_NO_NODE ;spin_lock_init (&iommu->lock);ida_init (&iommu->domain_ida); +mutex_init (&iommu->did_lock); ver =readl (iommu->reg +DMAR_VER_REG ); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n",
drivers/iommu/intel/iommu.c +4 -8
@@ -1345,17 +1345,16 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)if (!info)return -ENOMEM ; -spin_lock (&iommu->lock); +guard (mutex)(&iommu->did_lock); curr =xa_load (&domain->iommu_array, iommu->seq_id);if (curr) { curr->refcnt++; -spin_unlock (&iommu->lock);kfree (info);return 0 ; } num =ida_alloc_range (&iommu->domain_ida,IDA_START_DID , -cap_ndoms (iommu->cap) -1 ,GFP_ATOMIC ); +cap_ndoms (iommu->cap) -1 ,GFP_KERNEL );if (num <0 ) {pr_err ("%s: No free domain ids\n" , iommu->name); goto err_unlock;
@@ -1365,19 +1364,17 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) info->did = num; info->iommu = iommu; curr =xa_cmpxchg (&domain->iommu_array, iommu->seq_id, -NULL , info,GFP_ATOMIC ); +NULL , info,GFP_KERNEL );if (curr) { ret =xa_err (curr) ? : -EBUSY ; goto err_clear; } -spin_unlock (&iommu->lock);return 0 ; err_clear:ida_free (&iommu->domain_ida, info->did); err_unlock: -spin_unlock (&iommu->lock);kfree (info);return ret; }
@@ -1389,7 +1386,7 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)if (domain->domain.type ==IOMMU_DOMAIN_SVA )return ; -spin_lock (&iommu->lock); +guard (mutex)(&iommu->did_lock); info =xa_load (&domain->iommu_array, iommu->seq_id);if (--info->refcnt ==0 ) {ida_free (&iommu->domain_ida, info->did);
@@ -1397,7 +1394,6 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) domain->nid =NUMA_NO_NODE ;kfree (info); } -spin_unlock (&iommu->lock); }static void domain_exit (struct dmar_domain *domain)
drivers/iommu/intel/iommu.h +2 -0
@@ -722,6 +722,8 @@ struct intel_iommu {unsigned char name[16 ];/* Device Name */ #ifdef CONFIG_INTEL_IOMMU +/* mutex to protect domain_ida */ +struct mutex did_lock;struct ida domain_ida;/* domain id allocator */ unsigned long *copied_tables;/* bitmap of copied tables */ spinlock_t lock;/* protect context, domain ids */
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 4/5 iommu/vt-d: Eliminate pci_physfn() in dmar_find_matched_satc_unit()
In reply to Lu BaoluFrom: Wei Wang <[email protected]>
The function dmar_find_matched_satc_unit() contains a duplicate call to pci_physfn(). This call is unnecessary as pci_physfn() has already been invoked by the caller. Removing the redundant call simplifies the code and improves efficiency a bit.
--- drivers/iommu/intel/iommu.c | 1 - 1 file changed, 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -1
@@ -2686,7 +2686,6 @@ static struct dmar_satc_unit *dmar_find_matched_satc_unit(struct pci_dev *dev)struct device *tmp;int i; - dev =pci_physfn (dev);rcu_read_lock ();list_for_each_entry_rcu (satcu, &dmar_satc_units, list) {
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 5/5 iommu/vt-d: Change dmar_ats_supported() to return boolean
In reply to Lu BaoluFrom: Wei Wang <[email protected]>
According to "Function return values and names" in coding-style.rst, the dmar_ats_supported() function should return a boolean instead of an integer. Also, rename "ret" to "supported" to be more straightforward.
--- drivers/iommu/intel/iommu.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/iommu/intel/iommu.c +10 -9
@@ -2702,15 +2702,16 @@ static struct dmar_satc_unit *dmar_find_matched_satc_unit(struct pci_dev *dev)return satcu; } -static int dmar_ats_supported (struct pci_dev *dev,struct intel_iommu *iommu) +static bool dmar_ats_supported (struct pci_dev *dev,struct intel_iommu *iommu) { -int i, ret =1 ; -struct pci_bus *bus;struct pci_dev *bridge =NULL ; -struct device *tmp; -struct acpi_dmar_atsr *atsr;struct dmar_atsr_unit *atsru;struct dmar_satc_unit *satcu; +struct acpi_dmar_atsr *atsr; +bool supported = true; +struct pci_bus *bus; +struct device *tmp; +int i; dev =pci_physfn (dev); satcu =dmar_find_matched_satc_unit (dev);
@@ -2728,11 +2729,11 @@ static int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu) bridge = bus->self;/* If it's an integrated device, allow ATS */ if (!bridge) -return 1 ; +return true;/* Connected via non-PCIe: no ATS */ if (!pci_is_pcie (bridge) ||pci_pcie_type (bridge) ==PCI_EXP_TYPE_PCI_BRIDGE ) -return 0 ; +return false;/* If we found the root port, look it up in the ATSR */ if (pci_pcie_type (bridge) ==PCI_EXP_TYPE_ROOT_PORT )break ;
@@ -2751,11 +2752,11 @@ static int dmar_ats_supported(struct pci_dev *dev, struct intel_iommu *iommu)if (atsru->include_all) goto out; } - ret =0 ; + supported = false; out:rcu_read_unlock (); -return ret; +return supported; }int dmar_iommu_notify_scope_dev (struct dmar_pci_notify_info *info)
Signature · 1 line
2.43.0
Quoted 7 lines · Lu Baolu
Jason Gunthorpe (1):
iommu/vt-d: Restore WO permissions on second-level paging entriesLu Baolu (2):
iommu/vt-d: Use ida to manage domain id
iommu/vt-d: Replacespin_lockwith mutex to protect domain ida
Quoted 8 lines
Wei Wang (2):
iommu/vt-d: Eliminatepci_physfn()indmar_find_matched_satc_unit()iommu/vt-d: Changedmar_ats_supported()to return booleandrivers/iommu/intel/dmar.c | 4 ++ drivers/iommu/intel/iommu.c | 113 ++++++++++-------------------------- drivers/iommu/intel/iommu.h | 21 +++++-- 3 files changed, 51 insertions(+), 87 deletions(-)
Applied all, thanks Baolu.