iommu/vt-d: Add domain_alloc_paging support
Subsystems (2)
Lu Baolu
maintainerv2 cover iommu/vt-d: Add domain_alloc_paging support
The Intel iommu driver will now use the domain_alloc_paging callback and remove the legacy domain_alloc callback. This ensures that a valid device pointer is provided whenever a paging domain is allocated, and all dmar_domain attributes can be set up at the time of allocation.
Both first-stage and second-stage page tables can be used for a paging domain. Unless IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING is specified during paging domain allocation, this driver will try to use first-stage page tables if the hardware is capable. This is assuming that the first-stage page table is compatible with both the host and guest kernels.
The whole series is also available on GitHub:
https://github.com/LuBaolu/intel-iommu/commits/vtd-domain_alloc_paging-v2
Please help review and comment.
Change log:
v2:
- Make prepare_domain_attach_device() a specific helper to check whether a paging domain is compatible with the iommu hardware capability.
- Rename prepare_domain_attach_device() to paging_domain_compatible() to make it more meaningful.
v1: https://lore.kernel.org/linux-iommu/[email protected]/
Lu Baolu (7):
iommu/vt-d: Add domain_alloc_paging support iommu/vt-d: Remove unused domain_alloc callback iommu/vt-d: Enhance compatibility check for paging domain attach iommu/vt-d: Remove domain_update_iommu_cap() iommu/vt-d: Remove domain_update_iommu_superpage() iommu/vt-d: Refactor first_level_by_default() iommu/vt-d: Refine intel_iommu_domain_alloc_user()
drivers/iommu/intel/iommu.h | 4 +- drivers/iommu/intel/iommu.c | 328 +++++++---------------------------- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/intel/pasid.c | 28 +-- 4 files changed, 64 insertions(+), 298 deletions(-)
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 1/7 iommu/vt-d: Add domain_alloc_paging support
Add the domain_alloc_paging callback for domain allocation using the iommu_paging_domain_alloc() interface.
--- drivers/iommu/intel/iommu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/iommu/intel/iommu.c +14 -0
@@ -4590,6 +4590,19 @@ static struct iommu_domain identity_domain = { }, }; +static struct iommu_domain *intel_iommu_domain_alloc_paging (struct device *dev) +{ +struct dmar_domain *dmar_domain; +bool first_stage; + + first_stage =first_level_by_default (0 ); + dmar_domain =paging_domain_alloc (dev, first_stage); +if (IS_ERR (dmar_domain)) +return ERR_CAST (dmar_domain); + +return &dmar_domain->domain; +} +const struct iommu_ops intel_iommu_ops = { .blocked_domain = &blocking_domain, .release_domain = &blocking_domain,
@@ -4599,6 +4612,7 @@ const struct iommu_ops intel_iommu_ops = { .domain_alloc = intel_iommu_domain_alloc, .domain_alloc_user = intel_iommu_domain_alloc_user, .domain_alloc_sva = intel_svm_domain_alloc, + .domain_alloc_paging = intel_iommu_domain_alloc_paging, .probe_device = intel_iommu_probe_device, .release_device = intel_iommu_release_device, .get_resv_regions = intel_iommu_get_resv_regions,
Signature · 1 line
2.43.0
Quoted 7 lines · Lu Baolu
Add the
domain_alloc_pagingcallback for domain allocation using theiommu_paging_domain_alloc()interface.Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/intel/iommu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Jason
Lu Baolu
maintainerPATCH v2 2/7 iommu/vt-d: Remove unused domain_alloc callback
In reply to Lu BaoluWith domain_alloc_paging callback supported, the legacy domain_alloc callback will never be used anymore. Remove it to avoid dead code.
--- drivers/iommu/intel/iommu.c | 90 ------------------------------------- 1 file changed, 90 deletions(-)
drivers/iommu/intel/iommu.c +0 -90
@@ -1454,27 +1454,6 @@ static bool first_level_by_default(unsigned int type)return type !=IOMMU_DOMAIN_UNMANAGED ; } -static struct dmar_domain *alloc_domain (unsigned int type) -{ -struct dmar_domain *domain; - - domain =kzalloc (sizeof (*domain),GFP_KERNEL ); -if (!domain) -return NULL ; - - domain->nid =NUMA_NO_NODE ; -if (first_level_by_default (type)) - domain->use_first_level = true; -INIT_LIST_HEAD (&domain->devices); -INIT_LIST_HEAD (&domain->dev_pasids); -INIT_LIST_HEAD (&domain->cache_tags); -spin_lock_init (&domain->lock); -spin_lock_init (&domain->cache_lock); -xa_init (&domain->iommu_array); - -return domain; -} -int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) {struct iommu_domain_info *info, *curr;
@@ -1546,20 +1525,6 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)spin_unlock (&iommu->lock); } -static int guestwidth_to_adjustwidth (int gaw) -{ -int agaw; -int r = (gaw -12 ) %9 ; - -if (r ==0 ) - agaw = gaw; -else - agaw = gaw +9 - r; -if (agaw >64 ) - agaw =64 ; -return agaw; -} -static void domain_exit (struct dmar_domain *domain) {if (domain->pgd) {
@@ -3379,27 +3344,6 @@ void device_block_translation(struct device *dev) info->domain =NULL ; } -static int md_domain_init (struct dmar_domain *domain,int guest_width) -{ -int adjust_width; - -/* calculate AGAW */ - domain->gaw = guest_width; - adjust_width =guestwidth_to_adjustwidth (guest_width); - domain->agaw =width_to_agaw (adjust_width); - - domain->iommu_coherency = false; - domain->iommu_superpage =0 ; - domain->max_addr =0 ; - -/* always allocate the top pgd */ - domain->pgd =iommu_alloc_page_node (domain->nid,GFP_ATOMIC ); -if (!domain->pgd) -return -ENOMEM ; -domain_flush_cache (domain, domain->pgd,PAGE_SIZE ); -return 0 ; -} -static int blocking_domain_attach_dev (struct iommu_domain *domain,struct device *dev) {
@@ -3486,39 +3430,6 @@ static struct dmar_domain *paging_domain_alloc(struct device *dev, bool first_streturn domain; } -static struct iommu_domain *intel_iommu_domain_alloc (unsigned type) -{ -struct dmar_domain *dmar_domain; -struct iommu_domain *domain; - -switch (type) { -case IOMMU_DOMAIN_DMA : -case IOMMU_DOMAIN_UNMANAGED : - dmar_domain =alloc_domain (type); -if (!dmar_domain) { -pr_err ("Can't allocate dmar_domain\n" ); -return NULL ; - } -if (md_domain_init (dmar_domain,DEFAULT_DOMAIN_ADDRESS_WIDTH )) { -pr_err ("Domain initialization failed\n" ); -domain_exit (dmar_domain); -return NULL ; - } - - domain = &dmar_domain->domain; - domain->geometry.aperture_start =0 ; - domain->geometry.aperture_end = -__DOMAIN_MAX_ADDR (dmar_domain->gaw); - domain->geometry.force_aperture = true; - -return domain; -default : -return NULL ; - } - -return NULL ; -} -static struct iommu_domain * intel_iommu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent,
@@ -4609,7 +4520,6 @@ const struct iommu_ops intel_iommu_ops = { .identity_domain = &identity_domain, .capable = intel_iommu_capable, .hw_info = intel_iommu_hw_info, - .domain_alloc = intel_iommu_domain_alloc, .domain_alloc_user = intel_iommu_domain_alloc_user, .domain_alloc_sva = intel_svm_domain_alloc, .domain_alloc_paging = intel_iommu_domain_alloc_paging,
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 3/7 iommu/vt-d: Enhance compatibility check for paging domain attach
In reply to Lu BaoluThe driver now supports domain_alloc_paging, ensuring that a valid device pointer is provided whenever a paging domain is allocated. Additionally, the dmar_domain attributes are set up at the time of allocation.
Consistent with the established semantics in the IOMMU core, if a domain is attached to a device and found to be incompatible with the IOMMU hardware capabilities, the operation will return an -EINVAL error. This implicitly advises the caller to allocate a new domain for the device and attempt the domain attachment again.
Rename prepare_domain_attach_device() to a more meaningful name.
--- drivers/iommu/intel/iommu.h | 3 +- drivers/iommu/intel/iommu.c | 70 ++++++++++++------------------------ drivers/iommu/intel/nested.c | 2 +- drivers/iommu/intel/pasid.c | 28 +-------------- 4 files changed, 26 insertions(+), 77 deletions(-)
drivers/iommu/intel/iommu.h +1 -2
@@ -1230,8 +1230,7 @@ void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr,int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void device_block_translation (struct device *dev); -int prepare_domain_attach_device (struct iommu_domain *domain, -struct device *dev); +int paging_domain_compatible (struct iommu_domain *domain,struct device *dev);void domain_update_iommu_cap (struct dmar_domain *domain);int dmar_ir_support (void );
drivers/iommu/intel/iommu.c +23 -47
133 lines not on this page · raw message
drivers/iommu/intel/nested.c +1 -1
@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,* The s2_domain will be used in nested translation, hence needs * to ensure the s2_domain is compatible with this IOMMU. */ - ret =prepare_domain_attach_device (&dmar_domain->s2_domain->domain, dev); + ret =paging_domain_compatible (&dmar_domain->s2_domain->domain, dev);if (ret) {dev_err_ratelimited (dev,"s2 domain is not compatible\n" );return ret;
drivers/iommu/intel/pasid.c +1 -27
@@ -345,25 +345,6 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,return 0 ; } -/* -* Skip top levels of page tables for iommu which has less agaw -* than default. Unnecessary for PT mode. -*/ -static int iommu_skip_agaw (struct dmar_domain *domain, -struct intel_iommu *iommu, -struct dma_pte **pgd) -{ -int agaw; - -for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) { - *pgd =phys_to_virt (dma_pte_addr (*pgd)); -if (!dma_pte_present (*pgd)) -return -EINVAL ; - } - -return agaw; -} -/* * Set up the scalable mode pasid entry for second only translation type. */
@@ -374,7 +355,6 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,struct pasid_entry *pte;struct dma_pte *pgd;u64 pgd_val; -int agaw;u16 did;/*
@@ -388,12 +368,6 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu, } pgd = domain->pgd; - agaw =iommu_skip_agaw (domain, iommu, &pgd); -if (agaw <0 ) { -dev_err (dev,"Invalid domain page table\n" ); -return -EINVAL ; - } - pgd_val =virt_to_phys (pgd); did =domain_id_iommu (domain, iommu);
@@ -412,7 +386,7 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,pasid_clear_entry (pte);pasid_set_domain_id (pte, did);pasid_set_slptr (pte, pgd_val); -pasid_set_address_width (pte, agaw); +pasid_set_address_width (pte, domain->agaw);pasid_set_translation_type (pte,PASID_ENTRY_PGTT_SL_ONLY );pasid_set_fault_enable (pte);pasid_set_page_snoop (pte, !!ecap_smpwc (iommu->ecap));
Signature · 1 line
2.43.0
Re: [PATCH v2 3/7] iommu/vt-d: Enhance compatibility check for paging domain attach
drivers/iommu/intel/nested.c static int intel_nested_attach_dev(struct iommu_domain *domain, · line 46
Quoted 13 lines · Lu Baolu
drivers/iommu/intel/nested.c +1
-1@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,* The s2_domain will be used in nested translation, hence needs * to ensure the s2_domain is compatible with this IOMMU. */ - ret =prepare_domain_attach_device (&dmar_domain->s2_domain->domain, dev); + ret =paging_domain_compatible (&dmar_domain->s2_domain->domain, dev);if (ret) {dev_err_ratelimited (dev,"s2 domain is not compatible\n" );return ret;
This dev_err shouldn't be here, this path would be userspace triggerable.
Otherwise
Jason
Baolu Lu
maintainerRe: [PATCH v2 3/7] iommu/vt-d: Enhance compatibility check for paging domain attach
Quoted 16 lines · Jason Gunthorpe
drivers/iommu/intel/nested.c static int intel_nested_attach_dev(struct iommu_domain *domain, · line 46
Quoted 13 lines · Lu Baolu
drivers/iommu/intel/nested.c +1
-1@@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain,* The s2_domain will be used in nested translation, hence needs * to ensure the s2_domain is compatible with this IOMMU. */ - ret =prepare_domain_attach_device (&dmar_domain->s2_domain->domain, dev); + ret =paging_domain_compatible (&dmar_domain->s2_domain->domain, dev);if (ret) {dev_err_ratelimited (dev,"s2 domain is not compatible\n" );return ret;This
dev_errshouldn't be here, this path would be userspace triggerable.
Yes, agreed. I will remove this dev_err in a separated patch.
Thanks,
baolu
Lu Baolu
maintainerPATCH v2 4/7 iommu/vt-d: Remove domain_update_iommu_cap()
In reply to Lu BaoluThe attributes of a paging domain are initialized during the allocation process, and any attempt to attach a domain that is not compatible will result in a failure. Therefore, there is no need to update the domain attributes at the time of domain attachment.
--- drivers/iommu/intel/iommu.h | 1 - drivers/iommu/intel/iommu.c | 83 ------------------------------------- 2 files changed, 84 deletions(-)
drivers/iommu/intel/iommu.h +0 -1
@@ -1231,7 +1231,6 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu);void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu);void device_block_translation (struct device *dev);int paging_domain_compatible (struct iommu_domain *domain,struct device *dev); -void domain_update_iommu_cap (struct dmar_domain *domain);int dmar_ir_support (void );
drivers/iommu/intel/iommu.c +0 -83
113 lines not on this page · raw message
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 5/7 iommu/vt-d: Remove domain_update_iommu_superpage()
In reply to Lu BaoluThe requirement for consistent super page support across all the IOMMU hardware in the system has been removed. In the past, if a new IOMMU was hot-added and lacked consistent super page capability, the hot-add process would be aborted. However, with the updated attachment semantics, it is now permissible for the super page capability to vary among different IOMMU hardware units.
--- drivers/iommu/intel/iommu.c | 39 +------------------------------------ 1 file changed, 1 insertion(+), 38 deletions(-)
drivers/iommu/intel/iommu.c +1 -38
@@ -352,36 +352,6 @@ static bool iommu_paging_structure_coherency(struct intel_iommu *iommu) ecap_smpwc(iommu->ecap) : ecap_coherent(iommu->ecap); } -static int domain_update_iommu_superpage (struct dmar_domain *domain, -struct intel_iommu *skip) -{ -struct dmar_drhd_unit *drhd; -struct intel_iommu *iommu; -int mask =0x3 ; - -if (!intel_iommu_superpage) -return 0 ; - -/* set iommu_superpage to the smallest common denominator */ -rcu_read_lock (); -for_each_active_iommu (iommu, drhd) { -if (iommu != skip) { -if (domain && domain->use_first_level) { -if (!cap_fl1gp_support (iommu->cap)) - mask =0x1 ; - }else { - mask &=cap_super_page_val (iommu->cap); - } - -if (!mask) -break ; - } - } -rcu_read_unlock (); - -return fls (mask); -} -/* Return the super pagesize bitmap if supported. */ static unsigned long domain_super_pgsize_bitmap (struct dmar_domain *domain) {
@@ -2605,20 +2575,13 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hdr, void *arg)static int intel_iommu_add (struct dmar_drhd_unit *dmaru) { -int sp, ret;struct intel_iommu *iommu = dmaru->iommu; +int ret; ret =intel_cap_audit (CAP_AUDIT_HOTPLUG_DMAR , iommu);if (ret) goto out; - sp =domain_update_iommu_superpage (NULL , iommu) -1 ; -if (sp >=0 && !(cap_super_page_val (iommu->cap) & (1 << sp))) { -pr_warn ("%s: Doesn't support large page.\n" , - iommu->name); -return -ENXIO ; - } -/* * Disable translation if already enabled prior to OS handover. */
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 6/7 iommu/vt-d: Refactor first_level_by_default()
In reply to Lu BaoluThe first stage page table is compatible across host and guest kernels. Therefore, this driver uses the first stage page table as the default for paging domains.
The helper first_level_by_default() determines the feasibility of using the first stage page table based on a global policy. This policy requires consistency in scalable mode and first stage translation capability among all iommu units. However, this is unnecessary as domain allocation, attachment, and removal operations are performed on a per-device basis.
The domain type (IOMMU_DOMAIN_DMA vs. IOMMU_DOMAIN_UNMANAGED) should not be a factor in determining the first stage page table usage. Both types are for paging domains, and there's no fundamental difference between them. The driver should not be aware of this distinction unless the core specifies allocation flags that require special handling.
Convert first_level_by_default() from global to per-iommu and remove the 'type' input.
--- drivers/iommu/intel/iommu.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/iommu/intel/iommu.c +9 -8
@@ -1329,18 +1329,17 @@ static void free_dmar_iommu(struct intel_iommu *iommu)* Check and return whether first level is used by default for * DMA translation. */ -static bool first_level_by_default (unsigned int type) +static bool first_level_by_default (struct intel_iommu *iommu) {/* Only SL is available in legacy mode */ -if (!scalable_mode_support ()) +if (!sm_supported (iommu))return false;/* Only level (either FL or SL) is available, just use it */ -if (intel_cap_flts_sanity () ^intel_cap_slts_sanity ()) -return intel_cap_flts_sanity (); +if (ecap_flts (iommu->ecap) ^ecap_slts (iommu->ecap)) +return ecap_flts (iommu->ecap); -/* Both levels are available, decide it based on domain type */ -return type !=IOMMU_DOMAIN_UNMANAGED ; +return true; }int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu)
@@ -3110,7 +3109,7 @@ int __init intel_iommu_init(void)* the virtual and physical IOMMU page-tables. */ if (cap_caching_mode (iommu->cap) && - !first_level_by_default (IOMMU_DOMAIN_DMA )) { + !first_level_by_default (iommu)) {pr_info_once ("IOMMU batching disallowed due to virtualization\n" );iommu_set_dma_strict (); }
@@ -4359,10 +4358,12 @@ static struct iommu_domain identity_domain = {static struct iommu_domain *intel_iommu_domain_alloc_paging (struct device *dev) { +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct intel_iommu *iommu = info->iommu;struct dmar_domain *dmar_domain;bool first_stage; - first_stage =first_level_by_default (0 ); + first_stage =first_level_by_default (iommu); dmar_domain =paging_domain_alloc (dev, first_stage);if (IS_ERR (dmar_domain))return ERR_CAST (dmar_domain);
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH v2 7/7 iommu/vt-d: Refine intel_iommu_domain_alloc_user()
In reply to Lu BaoluThe domain_alloc_user ops should always allocate a guest-compatible page table unless specific allocation flags are specified.
Currently, IOMMU_HWPT_ALLOC_NEST_PARENT and IOMMU_HWPT_ALLOC_DIRTY_TRACKING require special handling, as both require hardware support for scalable mode and second-stage translation. In such cases, the driver should select a second-stage page table for the paging domain.
--- drivers/iommu/intel/iommu.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +15 -2
@@ -3297,6 +3297,7 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,struct intel_iommu *iommu = info->iommu;struct dmar_domain *dmar_domain;struct iommu_domain *domain; +bool first_stage;/* Must be NESTING domain */ if (parent) {
@@ -3313,8 +3314,20 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,if (user_data || (dirty_tracking && !ssads_supported (iommu)))return ERR_PTR (-EOPNOTSUPP ); -/* Do not use first stage for user domain translation. */ - dmar_domain =paging_domain_alloc (dev, false); +/* +* Always allocate the guest compatible page table unless +* IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING +* is specified. +*/ +if (nested_parent || dirty_tracking) { +if (!sm_supported (iommu) || !ecap_slts (iommu->ecap)) +return ERR_PTR (-EOPNOTSUPP ); + first_stage = false; + }else { + first_stage =first_level_by_default (iommu); + } + + dmar_domain =paging_domain_alloc (dev, first_stage);if (IS_ERR (dmar_domain))return ERR_CAST (dmar_domain); domain = &dmar_domain->domain;
Signature · 1 line
2.43.0
Baolu Lu
maintainerQuoted 37 lines · Lu Baolu
The Intel iommu driver will now use the
domain_alloc_pagingcallback and remove the legacydomain_alloccallback. This ensures that a valid device pointer is provided whenever a paging domain is allocated, and alldmar_domainattributes can be set up at the time of allocation.Both first-stage and second-stage page tables can be used for a paging domain. Unless
IOMMU_HWPT_ALLOC_NEST_PARENTorIOMMU_HWPT_ALLOC_DIRTY_TRACKINGis specified during paging domain allocation, this driver will try to use first-stage page tables if the hardware is capable. This is assuming that the first-stage page table is compatible with both the host and guest kernels.The whole series is also available on GitHub:
https://github.com/LuBaolu/intel-iommu/commits/vtd-domain_alloc_paging-v2Please help review and comment.
Change log:
v2:
- Makeprepare_domain_attach_device()a specific helper to check whether a paging domain is compatible with the iommu hardware capability.
- Renameprepare_domain_attach_device()topaging_domain_compatible()to make it more meaningful.v1:https://lore.kernel.org/linux-iommu/20241011042722.73930-1- [email protected]/
Lu Baolu (7):
iommu/vt-d: Adddomain_alloc_pagingsupport iommu/vt-d: Remove unuseddomain_alloccallback iommu/vt-d: Enhance compatibility check for paging domain attach iommu/vt-d: Removedomain_update_iommu_cap()iommu/vt-d: Removedomain_update_iommu_superpage()iommu/vt-d: Refactorfirst_level_by_default()iommu/vt-d: Refineintel_iommu_domain_alloc_user()
Quoted 5 lines
drivers/iommu/intel/iommu.h | 4 +- drivers/iommu/intel/iommu.c | 328 +++++++---------------------------- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/intel/pasid.c | 28 +-- 4 files changed, 64 insertions(+), 298 deletions(-)
Queued for v6.13.
--
baolu