Add Intel VT-d nested translation
Subsystems (5)
v4 cover Add Intel VT-d nested translation
This is to add Intel VT-d nested translation based on IOMMUFD nesting infrastructure. As the iommufd nesting infrastructure series[1], iommu core supports new ops to report iommu hardware information, allocate domains with user data and invalidate stage-1 IOTLB when there is mapping changed in stage-1 page table. The data required in the three paths are vendor-specific, so
1) IOMMU_HWPT_TYPE_VTD_S1 is defined for the Intel VT-d stage-1 page table, it will be used in the stage-1 domain allocation and IOTLB syncing path. struct iommu_hwpt_vtd_s1 is defined to pass user_data for the Intel VT-d stage-1 domain allocation.
struct iommu_hwpt_vtd_s1_invalidate is defined to pass the data for the Intel VT-d stage-1 IOTLB invalidation.
2) IOMMU_HW_INFO_TYPE_INTEL_VTD and struct iommu_hw_info_vtd are defined to report iommu hardware information for Intel VT-d.
With above IOMMUFD extensions, the intel iommu driver implements the three paths to support nested translation.
The first Intel platform supporting nested translation is Sapphire Rapids which, unfortunately, has a hardware errata [2] requiring special treatment. This errata happens when a stage-1 page table page (either level) is located in a stage-2 read-only region. In that case the IOMMU hardware may ignore the stage-2 RO permission and still set the A/D bit in stage-1 page table entries during page table walking.
A flag IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 is introduced to report this errata to userspace. With that restriction the user should either disable nested translation to favor RO stage-2 mappings or ensure no RO stage-2 mapping to enable nested translation.
Intel-iommu driver is armed with necessary checks to prevent such mix in patch12 of this series.
Qemu currently does add RO mappings though. The vfio agent in Qemu simply maps all valid regions in the GPA address space which certainly includes RO regions e.g. vbios.
In reality we don't know a usage relying on DMA reads from the BIOS region. Hence finding a way to skip RO regions (e.g. via a discard manager) in Qemu might be an acceptable tradeoff. The actual change needs more discussion in Qemu community. For now we just hacked Qemu to test.
Complete code can be found in [3], corresponding QEMU could can be found in [4].
[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://www.intel.com/content/www/us/en/content-details/772415/content-details.html
[3] https://github.com/yiliu1765/iommufd/tree/iommufd_nesting
[4] https://github.com/yiliu1765/qemu/tree/wip/iommufd_rfcv4_nesting
Change log:
v4:
- Remove ascii art tables (Jason)
- Drop EMT (Tina, Jason)
- Drop MTS and related definitions (Kevin)
- Rename macro IOMMU_VTD_PGTBL_ to IOMMU_VTD_S1_ (Kevin)
- Rename struct iommu_hwpt_intel_vtd_ to iommu_hwpt_vtd_ (Kevin)
- Rename struct iommu_hwpt_intel_vtd to iommu_hwpt_vtd_s1 (Kevin)
- Put the vendor specific hwpt alloc data structure before enuma iommu_hwpt_type (Kevin)
- Do not trim the higher page levels of S2 domain in nested domain attachment as the S2 domain may have been used independently. (Kevin)
- Remove the first-stage pgd check against the maximum address of s2_domain as hw can check it anyhow. It makes sense to check every pfns used in the stage-1 page table. But it cannot make it. So just leave it to hw. (Kevin)
- Split the iotlb flush part into an order of uapi, helper and callback implementation (Kevin)
- Change the policy of VT-d nesting errata, disallow RO mapping once a domain is used as parent domain of a nested domain. This removes the nested_users counting. (Kevin)
- Minor fix for "make htmldocs"
v3: https://lore.kernel.org/linux-iommu/[email protected]/
- Further split the patches into an order of adding helpers for nested domain, iotlb flush, nested domain attachment and nested domain allocation callback, then report the hw_info to userspace.
- Add batch support in cache invalidation from userspace
- Disallow nested translation usage if RO mappings exists in stage-2 domain due to errata on readonly mappings on Sapphire Rapids platform.
v2: https://lore.kernel.org/linux-iommu/[email protected]/
- The iommufd infrastructure is split to be separate series.
v1: https://lore.kernel.org/linux-iommu/[email protected]/
Regards,
Yi Liu
Lu Baolu (5):
iommu/vt-d: Extend dmar_domain to support nested domain iommu/vt-d: Add helper for nested domain allocation iommu/vt-d: Add helper to setup pasid nested translation iommu/vt-d: Add nested domain allocation
iommu/vt-d: Disallow nesting on domains with read-only mappings
Yi Liu (7):
iommufd: Add data structure for Intel VT-d stage-1 domain allocation iommu/vt-d: Make domain attach helpers to be extern iommu/vt-d: Set the nested domain to a device
iommufd: Add data structure for Intel VT-d stage-1 cache invalidation iommu/vt-d: Make iotlb flush helpers to be extern iommu/vt-d: Add iotlb flush for nested domain
iommu/vt-d: Implement hw_info for iommu capability query
drivers/iommu/intel/Makefile | 2 +- drivers/iommu/intel/iommu.c | 80 +++++++++++++--- drivers/iommu/intel/iommu.h | 55 +++++++++-- drivers/iommu/intel/nested.c | 174 +++++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.c | 127 +++++++++++++++++++++++++ drivers/iommu/intel/pasid.h | 2 + drivers/iommu/iommufd/main.c | 6 ++ include/linux/iommu.h | 1 + include/uapi/linux/iommufd.h | 124 +++++++++++++++++++++++++ 9 files changed, 549 insertions(+), 22 deletions(-) create mode 100644 drivers/iommu/intel/nested.c
Signature · 1 line
2.34.1
PATCH v4 1/12 iommufd: Add data structure for Intel VT-d stage-1 domain allocation
This adds IOMMU_HWPT_TYPE_VTD_S1 for stage-1 hw_pagetable of Intel VT-d and the corressponding data structure for userspace specified parameter for the domain allocation.
--- include/uapi/linux/iommufd.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
include/uapi/linux/iommufd.h +33 -0
@@ -351,12 +351,45 @@ struct iommu_vfio_ioas { };#define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) +/** +* enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table +* entry attributes +* @IOMMU_VTD_S1_SRE: Supervisor request +* @IOMMU_VTD_S1_EAFE: Extended access enable +* @IOMMU_VTD_S1_WPE: Write protect enable +*/ +enum iommu_hwpt_vtd_s1_flags { +IOMMU_VTD_S1_SRE =1 <<0 , +IOMMU_VTD_S1_EAFE =1 <<1 , +IOMMU_VTD_S1_WPE =1 <<2 , +}; + +/** +* struct iommu_hwpt_vtd_s1 - Intel VT-d specific user-managed stage-1 +* page table info (IOMMU_HWPT_TYPE_VTD_S1) +* @flags: Combination of enum iommu_hwpt_vtd_s1_flags +* @pgtbl_addr: The base address of the stage-1 page table. +* @addr_width: The address width of the stage-1 page table +* @__reserved: Must be 0 +* +* VT-d specific data for creating a stage-1 page table that is used +* in nested translation. +*/ +struct iommu_hwpt_vtd_s1 { +__aligned_u64 flags; +__aligned_u64 pgtbl_addr; +__u32 addr_width; +__u32 __reserved; +}; +/** * enum iommu_hwpt_type - IOMMU HWPT Type * @IOMMU_HWPT_TYPE_DEFAULT: default +* @IOMMU_HWPT_TYPE_VTD_S1: Intel VT-d stage-1 page table */ enum iommu_hwpt_type {IOMMU_HWPT_TYPE_DEFAULT , +IOMMU_HWPT_TYPE_VTD_S1 , };/**
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 01/12] iommufd: Add data structure for Intel VT-d stage-1 domain allocation
include/uapi/linux/iommufd.h struct iommu_vfio_ioas { · line 369
Quoted 7 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMinclude/uapi/linux/iommufd.h +4
-0+ +/** +* struct iommu_hwpt_vtd_s1 - Intel VT-d specific user-managed stage-1 +* page table info (IOMMU_HWPT_TYPE_VTD_S1)
remove "specific user-managed"
Quoted 7 lines
+ * @flags: Combination of enum
iommu_hwpt_vtd_s1_flags
+ * @pgtbl_addr: The base address of the stage-1 page table.
+ * @addr_width: The address width of the stage-1 page table
+ * @__reserved: Must be 0
+ *
+ * VT-d specific data for creating a stage-1 page table that is used
+ * in nested translation.
this doesn't add more info. could be removed.
PATCH v4 2/12 iommu/vt-d: Extend dmar_domain to support nested domain
In reply to Yi LiuFrom: Lu Baolu <[email protected]>
The nested domain fields are exclusive to those that used for a DMA remapping domain. Use union to avoid memory waste.
--- drivers/iommu/intel/iommu.h | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-)
drivers/iommu/intel/iommu.h +29 -6
@@ -596,15 +596,38 @@ struct dmar_domain {spinlock_t lock;/* Protect device tracking lists */ struct list_head devices;/* all devices' list */ -struct dma_pte *pgd;/* virtual address */ -int gaw;/* max guest address width */ - -/* adjusted guest address width, 0 is level 2 30-bit */ -int agaw;int iommu_superpage;/* Level of superpages supported: 0 == 4KiB (no superpages), 1 == 2MiB, 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */ -u64 max_addr;/* maximum mapped address */ +union { +/* DMA remapping domain */ +struct { +/* virtual address */ +struct dma_pte *pgd; +/* max guest address width */ +int gaw; +/* +* adjusted guest address width: +* 0: level 2 30-bit +* 1: level 3 39-bit +* 2: level 4 48-bit +* 3: level 5 57-bit +*/ +int agaw; +/* maximum mapped address */ +u64 max_addr; + }; + +/* Nested user domain */ +struct { +/* parent page table which the user domain is nested on */ +struct dmar_domain *s2_domain; +/* user page table pointer (in GPA) */ +unsigned long s1_pgtbl; +/* page table attributes */ +struct iommu_hwpt_vtd_s1 s1_cfg; + }; + };struct iommu_domain domain;/* generic domain data structure for iommu core */
Signature · 1 line
2.34.1
RE: [PATCH v4 02/12] iommu/vt-d: Extend dmar_domain to support nested domain
Quoted 10 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMFrom: Lu Baolu <[email protected]>
The nested domain fields are exclusive to those that used for a DMA remapping domain. Use union to avoid memory waste.
Signed-off-by Lu Baolu <[email protected]>Signed-off-by Yi Liu <[email protected]>
PATCH v4 3/12 iommu/vt-d: Add helper for nested domain allocation
In reply to Yi LiuFrom: Lu Baolu <[email protected]>
This adds helper for accepting user parameters and allocate a nested domain.
--- drivers/iommu/intel/Makefile | 2 +- drivers/iommu/intel/iommu.h | 2 ++ drivers/iommu/intel/nested.c | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/intel/nested.c
drivers/iommu/intel/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DMAR_TABLE) += dmar.o -obj-$(CONFIG_INTEL_IOMMU) += iommu.o pasid.o +obj-$(CONFIG_INTEL_IOMMU) += iommu.o pasid.o nested.o obj-$(CONFIG_DMAR_TABLE) += trace.o cap_audit.o obj-$(CONFIG_DMAR_PERF) += perf.o obj-$(CONFIG_INTEL_IOMMU_DEBUGFS) += debugfs.o
drivers/iommu/intel/iommu.h +2 -0
@@ -858,6 +858,8 @@ void *alloc_pgtable_page(int node, gfp_t gfp);void free_pgtable_page (void *vaddr);void iommu_flush_write_buffer (struct intel_iommu *iommu);struct intel_iommu *device_to_iommu (struct device *dev,u8 *bus,u8 *devfn); +struct iommu_domain *intel_nested_domain_alloc (struct iommu_domain *s2_domain, +const union iommu_domain_user_data *user_data);#ifdef CONFIG_INTEL_IOMMU_SVM void intel_svm_check (struct intel_iommu *iommu);
drivers/iommu/intel/nested.c new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.34.1
RE: [PATCH v4 03/12] iommu/vt-d: Add helper for nested domain allocation
Quoted 11 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMFrom: Lu Baolu <[email protected]>
This adds helper for accepting user parameters and allocate a nested domain.
Signed-off-by Jacob Pan <[email protected]>Signed-off-by Lu Baolu <[email protected]>Signed-off-by Yi Liu <[email protected]>
PATCH v4 4/12 iommu/vt-d: Add helper to setup pasid nested translation
In reply to Yi LiuFrom: Lu Baolu <[email protected]>
The configurations are passed in from the user when the user domain is allocated. This helper interprets these configurations according to the data structure defined in uapi/linux/iommufd.h. The EINVAL error will be returned if any of configurations are not compatible with the hardware capabilities. The caller can retry with another compatible user domain. The encoding of fields of each pasid entry is defined in section 9.6 of the VT-d spec.
--- drivers/iommu/intel/pasid.c | 127 ++++++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.h | 2 + 2 files changed, 129 insertions(+)
drivers/iommu/intel/pasid.c +127 -0
@@ -335,6 +335,15 @@ static inline void pasid_set_fault_enable(struct pasid_entry *pe)pasid_set_bits (&pe->val[0 ],1 <<1 ,0 ); } +/* +* Setup the SRE(Supervisor Request Enable) field (Bit 128) of a +* scalable mode PASID entry. +*/ +static inline void pasid_set_sre (struct pasid_entry *pe) +{ +pasid_set_bits (&pe->val[2 ],1 <<0 ,1 ); +} +/* * Setup the WPE(Write Protect Enable) field (Bit 132) of a * scalable mode PASID entry.
@@ -402,6 +411,15 @@ pasid_set_flpm(struct pasid_entry *pe, u64 value)pasid_set_bits (&pe->val[2 ],GENMASK_ULL (3 ,2 ), value <<2 ); } +/* +* Setup the Extended Access Flag Enable (EAFE) field (Bit 135) +* of a scalable mode PASID entry. +*/ +static inline void pasid_set_eafe (struct pasid_entry *pe) +{ +pasid_set_bits (&pe->val[2 ],1 <<7 ,1 <<7 ); +} +static void pasid_cache_invalidation_with_pasid (struct intel_iommu *iommu,u16 did,u32 pasid)
@@ -713,3 +731,112 @@ void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,if (!cap_caching_mode (iommu->cap))devtlb_invalidation_with_pasid (iommu, dev, pasid); } + +/** +* intel_pasid_setup_nested() - Set up PASID entry for nested translation. +* This could be used for nested translation based vIOMMU. e.g. guest IOVA +* and guest shared virtual address. In this case, the first level page +* tables are used for GVA/GIOVA-GPA translation in the guest, second level +* page tables are used for GPA-HPA translation. +* +* @iommu: IOMMU which the device belong to +* @dev: Device to be set up for translation +* @pasid: PASID to be programmed in the device PASID table +* @domain: User stage-1 domain nested on a s2 domain +*/ +int intel_pasid_setup_nested (struct intel_iommu *iommu,struct device *dev, +u32 pasid,struct dmar_domain *domain) +{ +struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg; +pgd_t *s1_gpgd = (pgd_t *)(uintptr_t )domain->s1_pgtbl; +struct dmar_domain *s2_domain = domain->s2_domain; +u16 did =domain_id_iommu (domain, iommu); +struct dma_pte *pgd = s2_domain->pgd; +struct pasid_entry *pte; + +if (!ecap_nest (iommu->ecap)) { +pr_err_ratelimited ("%s: No nested translation support\n" , + iommu->name); +return -ENODEV ; + } + +/* +* Address width should match in two dimensions: CPU vs. IOMMU, +* guest vs. host. +*/ +switch (s1_cfg->addr_width) { +case ADDR_WIDTH_4LEVEL : +break ; +#ifdef CONFIG_X86 +case ADDR_WIDTH_5LEVEL : +if (!cpu_feature_enabled (X86_FEATURE_LA57 ) || + !cap_fl5lp_support (iommu->cap)) { +dev_err_ratelimited (dev, +"5-level paging not supported\n" ); +return -EINVAL ; + } +break ; +#endif +default : +dev_err_ratelimited (dev,"Invalid guest address width %d\n" , + s1_cfg->addr_width); +return -EINVAL ; + } + +if ((s1_cfg->flags &IOMMU_VTD_S1_SRE ) && !ecap_srs (iommu->ecap)) { +pr_err_ratelimited ("No supervisor request support on %s\n" , + iommu->name); +return -EINVAL ; + } + +if ((s1_cfg->flags &IOMMU_VTD_S1_EAFE ) && !ecap_eafs (iommu->ecap)) { +pr_err_ratelimited ("No extended access flag support on %s\n" , + iommu->name); +return -EINVAL ; + } + +if (s2_domain->agaw > iommu->agaw) { +pr_err_ratelimited ("Incompatible agaw %s\n" , iommu->name); +return -EINVAL ; + } + +spin_lock (&iommu->lock); + pte =intel_pasid_get_entry (dev, pasid); +if (!pte) { +spin_unlock (&iommu->lock); +return -ENODEV ; + } +if (pasid_pte_is_present (pte)) { +spin_unlock (&iommu->lock); +return -EBUSY ; + } + +pasid_clear_entry (pte); + +if (s1_cfg->addr_width ==ADDR_WIDTH_5LEVEL ) +pasid_set_flpm (pte,1 ); + +pasid_set_flptr (pte, (uintptr_t )s1_gpgd); + +if (s1_cfg->flags &IOMMU_VTD_S1_SRE ) { +pasid_set_sre (pte); +if (s1_cfg->flags &IOMMU_VTD_S1_WPE ) +pasid_set_wpe (pte); + } + +if (s1_cfg->flags &IOMMU_VTD_S1_EAFE ) +pasid_set_eafe (pte); + +pasid_set_slptr (pte,virt_to_phys (pgd)); +pasid_set_fault_enable (pte); +pasid_set_domain_id (pte, did); +pasid_set_address_width (pte, s2_domain->agaw); +pasid_set_page_snoop (pte, !!ecap_smpwc (iommu->ecap)); +pasid_set_translation_type (pte,PASID_ENTRY_PGTT_NESTED ); +pasid_set_present (pte); +spin_unlock (&iommu->lock); + +pasid_flush_caches (iommu, pte, pasid, did); + +return 0 ; +}
drivers/iommu/intel/pasid.h +2 -0
@@ -111,6 +111,8 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,int intel_pasid_setup_pass_through (struct intel_iommu *iommu,struct dmar_domain *domain,struct device *dev,u32 pasid); +int intel_pasid_setup_nested (struct intel_iommu *iommu,struct device *dev, +u32 pasid,struct dmar_domain *domain);void intel_pasid_tear_down_entry (struct intel_iommu *iommu,struct device *dev,u32 pasid,bool fault_ignore);
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 737
Quoted 7 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PM
}drivers/iommu/intel/pasid.c +4
-0+ +/** +* intel_pasid_setup_nested() - Set up PASID entry for nested translation. +* This could be used for nested translation based vIOMMU. e.g. guest IOVA
s/could be/is/
Quoted 3 lines
+ * and guest shared virtual address. In this case, the first level page
+ * tables are used for GVA/GIOVA-GPA translation in the guest, second level
+ * page tables are used for GPA-HPA translation.
let's be consistent on using stage-1/stage-2
btw the convention is to have 1-line summary, then the list of parameters followed by detail explanation of the function.
Quoted 12 lines
+ *
+ * @iommu: IOMMU which the device belong to + * @dev: Device to be set up for translation + * @pasid: PASID to be programmed in the device PASID table + * @domain: User stage-1 domain nested on a s2 domain+ */
+int intel_pasid_setup_nested(structintel_iommu*iommu, struct device *dev,drivers/iommu/intel/pasid.c +4
-0+ u32 pasid, struct dmar_domain *domain) +{ + struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg; +pgd_t *s1_gpgd = (pgd_t *)(uintptr_t )domain->s1_pgtbl;
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 761
Quoted 10 lines
drivers/iommu/intel/pasid.c +10
-0+struct dmar_domain *s2_domain = domain->s2_domain; +u16 did =domain_id_iommu (domain, iommu); +struct dma_pte *pgd = s2_domain->pgd; +struct pasid_entry *pte; + +if (!ecap_nest (iommu->ecap)) { +pr_err_ratelimited ("%s: No nested translation support\n" , + iommu->name); +return -ENODEV ; + }
-EINVAL
Review
Quoted 6 lines
drivers/iommu/intel/pasid.c +3
-0+ +if (s2_domain->agaw > iommu->agaw) { + pr_err_ratelimited("Incompatible agaw %s\n", iommu-Quoted 1 line
name);
(quoted diff) +2
-0+ return -EINVAL; + }
there is a duplicated check in intel_nested_attach_dev().
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 838
Quoted 10 lines
drivers/iommu/intel/pasid.c +10
-0+ +pasid_set_slptr (pte,virt_to_phys (pgd)); +pasid_set_fault_enable (pte); +pasid_set_domain_id (pte, did); +pasid_set_address_width (pte, s2_domain->agaw); +pasid_set_page_snoop (pte, !!ecap_smpwc (iommu->ecap)); +pasid_set_translation_type (pte,PASID_ENTRY_PGTT_NESTED ); +pasid_set_present (pte); +spin_unlock (&iommu->lock); +
this lacks of handling of force_snooping
Baolu Lu
maintainerRe: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
Quoted 11 lines · Kevin
Review
Quoted 10 lines
(quoted diff) +10
-0+ + pasid_set_slptr(pte, virt_to_phys(pgd)); + pasid_set_fault_enable(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, s2_domain->agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); + spin_unlock(&iommu->lock); +this lacks of handling of
force_snooping
If stage-2 domain has force_snooping attribute set, then we should set the bit field in the pasid entry, right?
How about below additional change?
drivers/iommu/intel/pasid.c +3 -0
@@ -827,6 +827,9 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,if (s1_cfg->flags &IOMMU_VTD_S1_EAFE )pasid_set_eafe (pte); +if (s2_domain>force_snooping) +pasid_set_pgsnp (pte); +pasid_set_slptr (pte,virt_to_phys (pgd));pasid_set_fault_enable (pte);pasid_set_domain_id (pte, did);
Best regards,
baolu
Baolu Lu
maintainerRe: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
Quoted 23 lines · Baolu Lu
Quoted 11 lines · Kevin
Review
Quoted 10 lines
(quoted diff) +10
-0+ + pasid_set_slptr(pte, virt_to_phys(pgd)); + pasid_set_fault_enable(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, s2_domain->agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); + spin_unlock(&iommu->lock); +this lacks of handling of
force_snoopingIf stage-2 domain has
force_snoopingattribute set, then we should set the bit field in the pasid entry, right?How about below additional change?
drivers/iommu/intel/pasid.c +0
-0
drivers/iommu/intel/pasid.c int intel_pasid_setup_nested(struct intel_iommu · line 831
Quoted 5 lines
drivers/iommu/intel/pasid.c +1
-0@@ -827,6 +827,9 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) pasid_set_eafe(pte); + if (s2_domain>force_snooping)
+ if (s2_domain->force_snooping)
Sorry for typo.
Quoted 8 lines
drivers/iommu/intel/pasid.c +2
-0+ pasid_set_pgsnp(pte); + pasid_set_slptr(pte, virt_to_phys(pgd)); pasid_set_fault_enable(pte);
pasid_set_domain_id(pte, did);Best regards,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
Quoted 21 lines
From: Baolu Lu <[email protected]>
Sent: Wednesday, August 2, 2023 4:12 PMQuoted 17 lines · Baolu Lu
Quoted 11 lines · Kevin
Review
Quoted 10 lines
(quoted diff) +10
-0+ + pasid_set_slptr(pte, virt_to_phys(pgd)); + pasid_set_fault_enable(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, s2_domain->agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); + spin_unlock(&iommu->lock); +this lacks of handling of
force_snoopingIf stage-2 domain has
force_snoopingattribute set, then we should set the bit field in the pasid entry, right?How about below additional change?
yes
Quoted 24 lines
drivers/iommu/intel/pasid.c int intel_pasid_setup_nested(struct intel_iommu · line 831
Quoted 11 lines
drivers/iommu/intel/pasid.c +1
-0@@ -827,6 +827,9 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) pasid_set_eafe(pte); + if (s2_domain>force_snooping)+ if (
s2_domain->force_snooping)Sorry for typo.
Quoted 8 lines
drivers/iommu/intel/pasid.c +2
-0+ pasid_set_pgsnp(pte); + pasid_set_slptr(pte, virt_to_phys(pgd)); pasid_set_fault_enable(pte);
pasid_set_domain_id(pte, did);Best regards,
baolu
Baolu Lu
maintainerRe: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
In reply to Tian, KevinQuoted 8 lines · Kevin
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 737
Quoted 7 lines
From: Liu, Yi L<[email protected]>
Sent: Monday, July 24, 2023 7:13 PM
}drivers/iommu/intel/pasid.c +4
-0+ +/** +* intel_pasid_setup_nested() - Set up PASID entry for nested translation. +* This could be used for nested translation based vIOMMU. e.g. guest IOVA s/could be/is/
Ack.
Quoted 9 lines
Quoted 4 lines
+ * and guest shared virtual address. In this case, the first level page
+ * tables are used for GVA/GIOVA-GPA translation in the guest, second level
+ * page tables are used for GPA-HPA translation.let's be consistent on using stage-1/stage-2
btw the convention is to have 1-line summary, then the list of parameters followed by detail explanation of the function.
This patch just follows the existing code style in this file. Need a separated patch to cleanup this.
Quoted 15 lines
Quoted 15 lines
+ *
+ * @iommu: IOMMU which the device belong to + * @dev: Device to be set up for translation + * @pasid: PASID to be programmed in the device PASID table + * @domain: User stage-1 domain nested on a s2 domain+ */
+int intel_pasid_setup_nested(structintel_iommu*iommu, struct device *dev,drivers/iommu/intel/pasid.c +7
-0+ u32 pasid, struct dmar_domain *domain) +{ + struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg; +pgd_t *s1_gpgd = (pgd_t *)(uintptr_t )domain->s1_pgtbl; +struct dmar_domain *s2_domain = domain->s2_domain; +u16 did =domain_id_iommu (domain, iommu); +struct dma_pte *pgd = s2_domain->pgd;
Quoted 8 lines
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 761
Quoted 7 lines
drivers/iommu/intel/pasid.c +7
-0+struct pasid_entry *pte; + +if (!ecap_nest (iommu->ecap)) { +pr_err_ratelimited ("%s: No nested translation support\n" , + iommu->name); +return -ENODEV ; + }
-EINVAL
This is in the attach domain path. -EINVAL has the special meaning of "this domain is not compatible with iommu for the device".
So here, I still think we should return -ENODEV and the caller doesn't need to retry anymore.
Quoted 9 lines
Review
Quoted 7 lines
drivers/iommu/intel/pasid.c +3
-0+ +if (s2_domain->agaw > iommu->agaw) { + pr_err_ratelimited("Incompatible agaw %s\n", iommu-Quoted 1 line
name);
(quoted diff) +2
-0+ return -EINVAL; + }there is a duplicated check in
intel_nested_attach_dev().
Yeah, should be removed.
Best regards,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 04/12] iommu/vt-d: Add helper to setup pasid nested translation
Quoted 14 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:13 AMQuoted 9 lines · Kevin
drivers/iommu/intel/pasid.c void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, · line 761
Quoted 8 lines
From: Liu, Yi L<[email protected]>
Sent: Monday, July 24, 2023 7:13 PMdrivers/iommu/intel/pasid.c +6
-0+ +if (!ecap_nest (iommu->ecap)) { +pr_err_ratelimited ("%s: No nested translation support\n" , + iommu->name); +return -ENODEV ; + }
-EINVAL
Quoted 6 lines
This is in the attach domain path.
-EINVALhas the special meaning of "this domain is not compatible with iommu for the device".So here, I still think we should return
-ENODEVand the caller doesn't need to retry anymore.
You are right. I overlooked that this validation is for a device cap.
PATCH v4 5/12 iommu/vt-d: Make domain attach helpers to be extern
In reply to Yi LiuThis makes the helpers visible to nested.c.
--- drivers/iommu/intel/iommu.c | 17 +++++++---------- drivers/iommu/intel/iommu.h | 8 ++++++++ 2 files changed, 15 insertions(+), 10 deletions(-)
drivers/iommu/intel/iommu.c +7 -10
@@ -277,7 +277,6 @@ static LIST_HEAD(dmar_satc_units);#define for_each_rmrr_units (rmrr) \ list_for_each_entry(rmrr, &dmar_rmrr_units, list) -static void device_block_translation (struct device *dev);static void intel_iommu_domain_free (struct iommu_domain *domain);int dmar_disabled = !IS_ENABLED (CONFIG_INTEL_IOMMU_DEFAULT_ON );
@@ -555,7 +554,7 @@ static unsigned long domain_super_pgsize_bitmap(struct dmar_domain *domain) }/* Some capabilities may be different across iommus */ -static void domain_update_iommu_cap (struct dmar_domain *domain) +void domain_update_iommu_cap (struct dmar_domain *domain) {domain_update_iommu_coherency (domain); domain->iommu_superpage =domain_update_iommu_superpage (domain,NULL );
@@ -1732,8 +1731,7 @@ static struct dmar_domain *alloc_domain(unsigned int type)return domain; } -static int domain_attach_iommu (struct dmar_domain *domain, -struct intel_iommu *iommu) +int domain_attach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) {struct iommu_domain_info *info, *curr;unsigned long ndomains;
@@ -1782,8 +1780,7 @@ static int domain_attach_iommu(struct dmar_domain *domain,return ret; } -static void domain_detach_iommu (struct dmar_domain *domain, -struct intel_iommu *iommu) +void domain_detach_iommu (struct dmar_domain *domain,struct intel_iommu *iommu) {struct iommu_domain_info *info;
@@ -3987,7 +3984,7 @@ static void dmar_remove_one_dev_info(struct device *dev)* all DMA requests without PASID from the device are blocked. If the page * table has been set, clean up the data structures. */ -static void device_block_translation (struct device *dev) +void device_block_translation (struct device *dev) {struct device_domain_info *info =dev_iommu_priv_get (dev);struct intel_iommu *iommu = info->iommu;
@@ -4093,8 +4090,8 @@ static void intel_iommu_domain_free(struct iommu_domain *domain)domain_exit (to_dmar_domain (domain)); } -static int prepare_domain_attach_device (struct iommu_domain *domain, -struct device *dev) +int prepare_domain_attach_device (struct iommu_domain *domain, +struct device *dev) {struct dmar_domain *dmar_domain =to_dmar_domain (domain);struct intel_iommu *iommu;
@@ -4334,7 +4331,7 @@ static void domain_set_force_snooping(struct dmar_domain *domain) PASID_RID2PASID); } -static bool intel_iommu_enforce_cache_coherency (struct iommu_domain *domain) +bool intel_iommu_enforce_cache_coherency (struct iommu_domain *domain) {struct dmar_domain *dmar_domain =to_dmar_domain (domain);unsigned long flags;
drivers/iommu/intel/iommu.h +8 -0
@@ -852,6 +852,14 @@ int qi_submit_sync(struct intel_iommu *iommu, struct qi_desc *desc,*/ #define QI_OPT_WAIT_DRAIN BIT(0) +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); +bool intel_iommu_enforce_cache_coherency (struct iommu_domain *domain); +void domain_update_iommu_cap (struct dmar_domain *domain); +int dmar_ir_support (void );void *alloc_pgtable_page (int node,gfp_t gfp);
Signature · 1 line
2.34.1
RE: [PATCH v4 05/12] iommu/vt-d: Make domain attach helpers to be extern
Quoted 7 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMThis makes the helpers visible to
nested.c.Suggested-by Lu Baolu <[email protected]>Signed-off-by Yi Liu <[email protected]>
PATCH v4 6/12 iommu/vt-d: Set the nested domain to a device
In reply to Yi LiuThis adds the helper for setting the nested domain to a device hence enable nested domain usage on Intel VT-d.
--- drivers/iommu/intel/nested.c | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
drivers/iommu/intel/nested.c +52 -0
@@ -11,8 +11,58 @@#define pr_fmt (fmt) "DMAR: " fmt#include <linux/iommu.h> +#include <linux/pci.h> +#include <linux/pci-ats.h> #include "iommu.h" +#include "pasid.h" + +static int intel_nested_attach_dev (struct iommu_domain *domain, +struct device *dev) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +struct intel_iommu *iommu = info->iommu; +unsigned long flags; +int ret =0 ; + +if (info->domain) +device_block_translation (dev); + +if (iommu->agaw < dmar_domain->s2_domain->agaw) { +dev_err_ratelimited (dev,"Adjusted guest address width not compatible\n" ); +return -ENODEV ; + } + +/* Is s2_domain compatible with this IOMMU? */ + ret =prepare_domain_attach_device (&dmar_domain->s2_domain->domain, dev); +if (ret) { +dev_err_ratelimited (dev,"s2 domain is not compatible\n" ); +return ret; + } + + ret =domain_attach_iommu (dmar_domain, iommu); +if (ret) { +dev_err_ratelimited (dev,"Failed to attach domain to iommu\n" ); +return ret; + } + + ret =intel_pasid_setup_nested (iommu, dev, +PASID_RID2PASID , dmar_domain); +if (ret) { +domain_detach_iommu (dmar_domain, iommu); +dev_err_ratelimited (dev,"Failed to setup pasid entry\n" ); +return ret; + } + + info->domain = dmar_domain; +spin_lock_irqsave (&dmar_domain->lock, flags); +list_add (&info->link, &dmar_domain->devices); +spin_unlock_irqrestore (&dmar_domain->lock, flags); +domain_update_iommu_cap (dmar_domain); + +return 0 ; +}static void intel_nested_domain_free (struct iommu_domain *domain) {
@@ -20,7 +70,9 @@ static void intel_nested_domain_free(struct iommu_domain *domain) }static const struct iommu_domain_ops intel_nested_domain_ops = { + .attach_dev = intel_nested_attach_dev, .free = intel_nested_domain_free, + .enforce_cache_coherency = intel_iommu_enforce_cache_coherency, }; struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain,
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 06/12] iommu/vt-d: Set the nested domain to a device
Quoted 18 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMdrivers/iommu/intel/nested.c +15
-0+ +static int intel_nested_attach_dev (struct iommu_domain *domain, +struct device *dev) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +struct intel_iommu *iommu = info->iommu; +unsigned long flags; +int ret =0 ; + +if (info->domain) +device_block_translation (dev); + +if (iommu->agaw < dmar_domain->s2_domain->agaw) { + dev_err_ratelimited(dev, "Adjusted guest address width notcompatible\n");
Review
Quoted 2 lines
(quoted diff) +2
-0+ return -ENODEV; + }
this is the check duplicated with patch04.
Quoted 8 lines
drivers/iommu/intel/nested.c +4
-0+ + ret =domain_attach_iommu (dmar_domain, iommu); +if (ret) { + dev_err_ratelimited(dev, "Failed to attach domain toiommu\n");
drivers/iommu/intel/nested.c +3
-0+return ret; + } +
[...]
drivers/iommu/intel/nested.c line 62
Quoted 1 line
drivers/iommu/intel/nested.c +1
-0+domain_update_iommu_cap (dmar_domain);
iommu_cap is already updated in domain_attach_iommu().
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 75
Quoted 5 lines
static const struct
iommu_domain_opsintel_nested_domain_ops= {drivers/iommu/intel/nested.c +2
-0+ .attach_dev = intel_nested_attach_dev, .free = intel_nested_domain_free, + .enforce_cache_coherency = intel_iommu_enforce_cache_coherency,
this is not required. enforce_cache_coherency() will be called on parent hwpt when it's being created. patch04 should check parent's force_snooping to set pgsnp in the pasid entry.
As Jason explained it should be done only for kernel owned page table.
Baolu Lu
maintainerRe: [PATCH v4 06/12] iommu/vt-d: Set the nested domain to a device
Quoted 14 lines · Kevin
Quoted 14 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMdrivers/iommu/intel/nested.c +12
-0+ +static int intel_nested_attach_dev (struct iommu_domain *domain, +struct device *dev) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +struct intel_iommu *iommu = info->iommu; +unsigned long flags; +int ret =0 ; + +if (info->domain) +device_block_translation (dev);
Quoted 8 lines
Review
Quoted 6 lines
drivers/iommu/intel/nested.c +3
-0+ +if (iommu->agaw < dmar_domain->s2_domain->agaw) { + dev_err_ratelimited(dev, "Adjusted guest address width notcompatible\n");
(quoted diff) +2
-0+ return -ENODEV; + }this is the check duplicated with patch04.
Ack. No need for a check here.
Quoted 12 lines
Quoted 9 lines
drivers/iommu/intel/nested.c +4
-0+ + ret =domain_attach_iommu (dmar_domain, iommu); +if (ret) { + dev_err_ratelimited(dev, "Failed to attach domain toiommu\n");
drivers/iommu/intel/nested.c +3
-0+return ret; + } +[...]
Quoted 3 lines
drivers/iommu/intel/nested.c line 62
Quoted 1 line
drivers/iommu/intel/nested.c +1
-0+domain_update_iommu_cap (dmar_domain);
iommu_capis already updated indomain_attach_iommu().
Ack. We will eventually remove this helper when every domain is allocated for a specific device.
Quoted 11 lines
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 75
Quoted 5 lines
static const struct iommu_domain_ops intel_nested_domain_ops = {drivers/iommu/intel/nested.c +2
-0+ .attach_dev = intel_nested_attach_dev, .free = intel_nested_domain_free, + .enforce_cache_coherency = intel_iommu_enforce_cache_coherency,this is not required.
enforce_cache_coherency()will be called on parent hwpt when it's being created. patch04 should check parent'sforce_snoopingto set pgsnp in the pasid entry.As Jason explained it should be done only for kernel owned page table.
Ack and thanks!
Best regards,
baolu
PATCH v4 7/12 iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
In reply to Yi LiuThis adds the data structure for flushing iotlb for the nested domain allocated with IOMMU_HWPT_TYPE_VTD_S1 type.
Cache invalidation path is performance path, so it's better to avoid memory allocation in such path. To achieve it, this path reuses the ucmd_buffer to copy user data. So the new data structures are added in the ucmd_buffer union to avoid overflow.
This only supports invalidating IOTLB, but no for device-TLB as device-TLB invalidation will be covered automatically in the IOTLB invalidation if the underlying IOMMU driver has enabled ATS for the affected device.
--- drivers/iommu/iommufd/main.c | 6 ++++ include/uapi/linux/iommufd.h | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+)
drivers/iommu/iommufd/main.c +6 -0
@@ -485,6 +485,12 @@ union ucmd_buffer {#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif +/* +* hwpt_type specific structure used in the cache invalidation +* path. +*/ +struct iommu_hwpt_vtd_s1_invalidate vtd; +struct iommu_hwpt_vtd_s1_invalidate_desc req_vtd; };struct iommufd_ioctl_op {
include/uapi/linux/iommufd.h +58 -0
64 lines not on this page · raw message
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 10 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:13 PMThis adds the data structure for flushing iotlb for the nested domain allocated with
IOMMU_HWPT_TYPE_VTD_S1type.Cache invalidation path is performance path, so it's better to avoid memory allocation in such path. To achieve it, this path reuses the
ucmd_bufferto copy user data. So the new data structures are added in theucmd_bufferunion to avoid overflow.
this patch has nothing to do with ucmd_buffer
Quoted 15 lines
include/uapi/linux/iommufd.h +15
-0+ +/** +* struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation +* (IOMMU_HWPT_TYPE_VTD_S1) +* @flags: Must be 0 +* @entry_size: Size in bytes of each cache invalidation request +* @entry_nr_uptr: User pointer to the number of invalidation requests. +* Kernel reads it to get the number of requests and +* updates the buffer with the number of requests that +* have been processed successfully. This pointer must +* point to a __u32 type of memory location. +* @inv_data_uptr: Pointer to the cache invalidation requests +* +* The Intel VT-d specific invalidation data for a set of cache invalidation +* requests. Kernel loops the requests one-by-one and stops when failure
include/uapi/linux/iommufd.h struct iommu_resv_iova_ranges { · line 583
Quoted 10 lines
include/uapi/linux/iommufd.h +10
-0+* is encountered. The number of handled requests is reported to user by +* writing the buffer pointed by @entry_nr_uptr. +*/ +struct iommu_hwpt_vtd_s1_invalidate { +__u32 flags; +__u32 entry_size; +__aligned_u64 entry_nr_uptr; +__aligned_u64 inv_data_uptr; +}; +
I wonder whether this array can be defined directly in the common struct iommu_hwpt_invalidate so there is no need for underlying iommu driver to further deal with user buffers, including various minsz/backward compat. check.
smmu may not require it by using a native queue format. But that could be considered as a special case of 1-entry array. With careful coding the added cost should be negligible.
Jason, your thought?
Jason Gunthorpe
maintainerRe: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 21 lines · Kevin
Quoted 21 lines
include/uapi/linux/iommufd.h +21
-0+/** +* struct iommu_hwpt_vtd_s1_invalidate - Intel VT-d cache invalidation +* (IOMMU_HWPT_TYPE_VTD_S1) +* @flags: Must be 0 +* @entry_size: Size in bytes of each cache invalidation request +* @entry_nr_uptr: User pointer to the number of invalidation requests. +* Kernel reads it to get the number of requests and +* updates the buffer with the number of requests that +* have been processed successfully. This pointer must +* point to a __u32 type of memory location. +* @inv_data_uptr: Pointer to the cache invalidation requests +* +* The Intel VT-d specific invalidation data for a set of cache invalidation +* requests. Kernel loops the requests one-by-one and stops when failure +* is encountered. The number of handled requests is reported to user by +* writing the buffer pointed by @entry_nr_uptr. +*/ +struct iommu_hwpt_vtd_s1_invalidate { +__u32 flags; +__u32 entry_size; +__aligned_u64 entry_nr_uptr;
Quoted 8 lines
include/uapi/linux/iommufd.h struct iommu_resv_iova_ranges { · line 583
Quoted 3 lines
include/uapi/linux/iommufd.h +3
-0+__aligned_u64 inv_data_uptr; +}; +I wonder whether this array can be defined directly in the common struct
iommu_hwpt_invalidateso there is no need for underlying iommu driver to further deal with user buffers, including various minsz/backward compat. check.
You want to have an array and another chunk of data?
What is the array for? To do batching?
It means we have to allocate memory on this path, that doesn't seem like the right direction for a performance improvement..
Having the driver copy in a loop might be better
Jason
Tian, Kevin
maintainerRE: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 36 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, August 2, 2023 9:48 PMQuoted 6 lines · Kevin
Quoted 6 lines
+/**
+ * structiommu_hwpt_vtd_s1_invalidate- Intel VT-d cache invalidation
+ * (IOMMU_HWPT_TYPE_VTD_S1)
+ * @flags: Must be 0
+ * @entry_size: Size in bytes of each cache invalidation request
+ * @entry_nr_uptr: User pointer to the number of invalidation requests.Quoted 8 lines
Quoted 8 lines
+ * Kernel reads it to get the number of requests and + * updates the buffer with the number of requests that + * have been processed successfully. This pointer must + * point to a __u32 type of memory location.+ * @inv_data_uptr: Pointer to the cache invalidation requests
+ *
+ * The Intel VT-d specific invalidation data for a set of cache invalidation
+ * requests. Kernel loops the requests one-by-one and stops whenfailure
Quoted 1 line
Quoted 1 line
+ * is encountered. The number of handled requests is reported to user
by
Quoted 6 lines
Quoted 6 lines
+ * writing the buffer pointed by @entry_nr_uptr.
+ */include/uapi/linux/iommufd.h +4
-0+struct iommu_hwpt_vtd_s1_invalidate { +__u32 flags; +__u32 entry_size; +__aligned_u64 entry_nr_uptr;Quoted 8 lines
include/uapi/linux/iommufd.h struct iommu_resv_iova_ranges { · line 583
Quoted 3 lines
include/uapi/linux/iommufd.h +3
-0+__aligned_u64 inv_data_uptr; +}; +I wonder whether this array can be defined directly in the common struct
iommu_hwpt_invalidateso there is no need for underlying iommu driver to further deal with user buffers, including various minsz/backward compat. check.
Quoted 3 lines
You want to have an array and another chunk of data?
What is the array for? To do batching?
yes, it's for batching
Quoted 3 lines
It means we have to allocate memory on this path, that doesn't seem like the right direction for a performance improvement..
It reuses the ucmd_buffer to avoid memory allocation:
@@ -485,6 +485,12 @@ union ucmd_buffer {
#ifdef CONFIG_IOMMUFD_TEST
struct iommu_test_cmd test;
#endif
+ /*
+ * hwpt_type specific structure used in the cache invalidation
+ * path.
+ */
+ struct iommu_hwpt_vtd_s1_invalidate vtd;
+ struct iommu_hwpt_vtd_s1_invalidate_desc req_vtd;
};
I don't quite like this way.
Quoted 3 lines
Having the driver copy in a loop might be better
Can you elaborate?
RE: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 43 lines
From: Tian, Kevin <[email protected]>
Sent: Thursday, August 3, 2023 8:39 AMQuoted 36 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, August 2, 2023 9:48 PMQuoted 6 lines · Kevin
Quoted 6 lines
+/**
+ * structiommu_hwpt_vtd_s1_invalidate- Intel VT-d cache invalidation
+ * (IOMMU_HWPT_TYPE_VTD_S1)
+ * @flags: Must be 0
+ * @entry_size: Size in bytes of each cache invalidation request
+ * @entry_nr_uptr: User pointer to the number of invalidation requests.Quoted 8 lines
Quoted 8 lines
+ * Kernel reads it to get the number of requests and + * updates the buffer with the number of requests that + * have been processed successfully. This pointer must + * point to a __u32 type of memory location.+ * @inv_data_uptr: Pointer to the cache invalidation requests
+ *
+ * The Intel VT-d specific invalidation data for a set of cache invalidation
+ * requests. Kernel loops the requests one-by-one and stops whenfailure
Quoted 1 line
Quoted 1 line
+ * is encountered. The number of handled requests is reported to user
by
Quoted 6 lines
Quoted 6 lines
+ * writing the buffer pointed by @entry_nr_uptr.
+ */include/uapi/linux/iommufd.h +4
-0+struct iommu_hwpt_vtd_s1_invalidate { +__u32 flags; +__u32 entry_size; +__aligned_u64 entry_nr_uptr;Quoted 8 lines
include/uapi/linux/iommufd.h struct iommu_resv_iova_ranges { · line 583
Quoted 3 lines
include/uapi/linux/iommufd.h +3
-0+__aligned_u64 inv_data_uptr; +}; +I wonder whether this array can be defined directly in the common struct
iommu_hwpt_invalidateso there is no need for underlying iommu driver to further deal with user buffers, including various minsz/backward compat. check.Quoted 3 lines
You want to have an array and another chunk of data?
What is the array for? To do batching?
Quoted 7 lines
yes, it's for batching
Quoted 3 lines
It means we have to allocate memory on this path, that doesn't seem like the right direction for a performance improvement..
It reuses the
ucmd_bufferto avoid memory allocation:
I guess your point is to copy each invalidation descriptor in the common layer and pass the descriptor to iommu driver. right?
Quoted 12 lines
drivers/iommu/iommufd/main.c +6
-0@@ -485,6 +485,12 @@ union ucmd_buffer {#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif +/* +* hwpt_type specific structure used in the cache invalidation +* path. +*/ +struct iommu_hwpt_vtd_s1_invalidate vtd; +struct iommu_hwpt_vtd_s1_invalidate_desc req_vtd; };
Quoted 1 line
I don't quite like this way.
This is because each descriptor is stored in the uncmd_buffer. So Need to put the struct iommu_hwpt_vtd_s1_invalidate_desc here.
Quoted 5 lines
Quoted 3 lines
Having the driver copy in a loop might be better
Can you elaborate?
I think Jason means the way in patch 09.
Regards,
Yi Liu
Jason Gunthorpe
maintainerRe: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 6 lines · Yi L
Quoted 4 lines
Quoted 2 lines
Having the driver copy in a loop might be better
Can you elaborate?
I think Jason means the way in patch 09.
Yeah, you can't reuse the stack buffer for an array, so patch 9 copies each element uniquely.
This is more calls to copy_to_user, which has some cost
But we avoid a memory allocation
Patch 9 should not abuse the user_data, cast it to the inv_info and just put req on the stack:
struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; struct iommu_hwpt_vtd_s1_invalidate_desc req;
But I'm not sure about this entry_size logic, what happens if the entry_size is larger than the kernel supports? I think it should fail..
Jason
RE: [PATCH v4 07/12] iommufd: Add data structure for Intel VT-d stage-1 cache invalidation
Quoted 9 lines
From: Jason Gunthorpe <[email protected]>
Sent: Friday, August 4, 2023 10:04 PMQuoted 5 lines · Yi L
Quoted 4 lines
Quoted 2 lines
Having the driver copy in a loop might be better
Can you elaborate?
Quoted 8 lines
Quoted 1 line
I think Jason means the way in patch 09.
Yeah, you can't reuse the stack buffer for an array, so patch 9 copies each element uniquely.
This is more calls to
copy_to_user, which has some costBut we avoid a memory allocation
Yes.
Quoted 5 lines
Patch 9 should not abuse the
user_data, cast it to theinv_infoand just put req on the stack:struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; struct iommu_hwpt_vtd_s1_invalidate_desc req;
Sure. The way in patch 09 is a bit tricky. The above is better and clearer. 😊
Quoted 3 lines
But I'm not sure about this
entry_sizelogic, what happens if theentry_sizeis larger than the kernel supports? I think it should fail..
Yes. should fail. It should be failed in copy_struct_from_user() as I use it to copy the struct iommu_hwpt_vtd_s1_invalidate_desc.
* -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src.
Regards,
Yi Liu
PATCH v4 8/12 iommu/vt-d: Make iotlb flush helpers to be extern
In reply to Yi LiuThis makes the helpers visible to nested.c.
--- drivers/iommu/intel/iommu.c | 10 +++++----- drivers/iommu/intel/iommu.h | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-)
drivers/iommu/intel/iommu.c +5 -5
@@ -1466,10 +1466,10 @@ static void iommu_flush_dev_iotlb(struct dmar_domain *domain,spin_unlock_irqrestore (&domain->lock, flags); } -static void iommu_flush_iotlb_psi (struct intel_iommu *iommu, -struct dmar_domain *domain, -unsigned long pfn,unsigned int pages, -int ih,int map) +void iommu_flush_iotlb_psi (struct intel_iommu *iommu, +struct dmar_domain *domain, +unsigned long pfn,unsigned int pages, +int ih,int map) {unsigned int aligned_pages =__roundup_pow_of_two (pages);unsigned int mask =ilog2 (aligned_pages);
@@ -1542,7 +1542,7 @@ static inline void __mapping_notify_one(struct intel_iommu *iommu,iommu_flush_write_buffer (iommu); } -static void intel_flush_iotlb_all (struct iommu_domain *domain) +void intel_flush_iotlb_all (struct iommu_domain *domain) {struct dmar_domain *dmar_domain =to_dmar_domain (domain);struct iommu_domain_info *info;
drivers/iommu/intel/iommu.h +6 -0
@@ -859,6 +859,12 @@ int prepare_domain_attach_device(struct iommu_domain *domain, struct device *dev);bool intel_iommu_enforce_cache_coherency (struct iommu_domain *domain);void domain_update_iommu_cap (struct dmar_domain *domain); +void iommu_flush_iotlb_psi (struct intel_iommu *iommu, +struct dmar_domain *domain, +unsigned long pfn,unsigned int pages, +int ih,int map); +void intel_flush_iotlb_all (struct iommu_domain *domain); +int dmar_ir_support (void );
Signature · 1 line
2.34.1
RE: [PATCH v4 08/12] iommu/vt-d: Make iotlb flush helpers to be extern
Quoted 6 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMThis makes the helpers visible to
nested.c.Signed-off-by Yi Liu <[email protected]>
PATCH v4 9/12 iommu/vt-d: Add iotlb flush for nested domain
In reply to Yi LiuThis implements the .cache_invalidate_user() callback and sets the .cache_invalidate_user_data_len to support iotlb flush for nested domain.
--- drivers/iommu/intel/nested.c | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+)
drivers/iommu/intel/nested.c +63 -0
@@ -69,8 +69,71 @@ static void intel_nested_domain_free(struct iommu_domain *domain)kfree (to_dmar_domain (domain)); } +static void intel_nested_invalidate (struct device *dev, +struct dmar_domain *domain, +u64 addr,unsigned long npages) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct intel_iommu *iommu = info->iommu; + +if (addr ==0 && npages ==-1 ) +intel_flush_iotlb_all (&domain->domain); +else +iommu_flush_iotlb_psi (iommu, domain, + addr >>VTD_PAGE_SHIFT , + npages,1 ,0 ); +} + +static int intel_nested_cache_invalidate_user (struct iommu_domain *domain, +void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size; +u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + +if (get_user (entry_nr, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +for (index =0 ; index < entry_nr; index++) { + ret =copy_struct_from_user (req,sizeof (*req), +u64_to_user_ptr (uptr + index * entry_size), + entry_size); +if (ret) { +pr_err_ratelimited ("Failed to fetch invalidation request\n" ); +break ; + } + +if (req->__reserved || (req->flags & ~IOMMU_VTD_QI_FLAGS_LEAF ) || + !IS_ALIGNED (req->addr,VTD_PAGE_SIZE )) { + ret = -EINVAL ; +break ; + } + +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags); + } + +if (put_user (index, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +return ret; +} +static const struct iommu_domain_ops intel_nested_domain_ops = { .attach_dev = intel_nested_attach_dev, + .cache_invalidate_user = intel_nested_cache_invalidate_user, + .cache_invalidate_user_data_len = +sizeof (struct iommu_hwpt_vtd_s1_invalidate ), .free = intel_nested_domain_free, .enforce_cache_coherency = intel_iommu_enforce_cache_coherency, };
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 25 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +19
-0+ void *user_data) +{ + struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; + struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + unsigned int entry_size = inv_info->entry_size; + u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (get_user(entry_nr, (uint32_t __user *)u64_to_user_ptr(nr_uptr))) + return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 107
Quoted 1 line
drivers/iommu/intel/nested.c +1
-0+ entry_size);
If continuing this direction then the driver should also check minsz etc. for struct iommu_hwpt_vtd_s1_invalidate and iommu_hwpt_vtd_s1_invalidate_desc since they are uAPI and subject to change.
Quoted 8 lines
(quoted diff) +2
-0+ if (ret) { + pr_err_ratelimited("Failed to fetch invalidationrequest\n");
drivers/iommu/intel/nested.c +4
-0+break ; + } + + if (req->__reserved || (req->flags &~IOMMU_VTD_QI_FLAGS_LEAF) ||
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 10 lines
drivers/iommu/intel/nested.c +10
-0+ !IS_ALIGNED(req->addr, VTD_PAGE_SIZE)) { + ret = -EINVAL ; +break ; + } + +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);
Disabling interrupt while invalidating iotlb is certainly unacceptable.
Actually there is no need to walk devices. Under dmar_domain there is already a list of attached iommu's.
Baolu Lu
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 22 lines · Kevin
Quoted 22 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +17
-0+ void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size; +u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + +if (get_user (entry_nr, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +for (index =0 ; index < entry_nr; index++) {
Quoted 8 lines
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 107
Quoted 4 lines
(quoted diff) +2
-0+ ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),drivers/iommu/intel/nested.c +1
-0+ entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateandiommu_hwpt_vtd_s1_invalidate_descsince they are uAPI and subject to change.
Agreed.
Quoted 22 lines
Quoted 9 lines
(quoted diff) +2
-0+ if (ret) { + pr_err_ratelimited("Failed to fetch invalidationrequest\n");
drivers/iommu/intel/nested.c +4
-0+break ; + } + + if (req->__reserved || (req->flags &~IOMMU_VTD_QI_FLAGS_LEAF) ||
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 10 lines
drivers/iommu/intel/nested.c +10
-0+ !IS_ALIGNED(req->addr, VTD_PAGE_SIZE)) { + ret = -EINVAL ; +break ; + } + +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);Disabling interrupt while invalidating iotlb is certainly unacceptable.
Quoted 2 lines
Actually there is no need to walk devices. Under
dmar_domainthere is already a list of attached iommu's.
Walking device is only necessary when invalidating device TLB. For iotlb invalidation, it only needs to know the iommu's.
Best regards,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 16 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:25 AMQuoted 12 lines · Kevin
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 9 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/nested.c +6
-0+ +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);Disabling interrupt while invalidating iotlb is certainly unacceptable.
Quoted 6 lines
Quoted 2 lines
Actually there is no need to walk devices. Under
dmar_domainthere is already a list of attached iommu's.Walking device is only necessary when invalidating device TLB. For iotlb invalidation, it only needs to know the iommu's.
even for device tlb we may think whether there is any better way to avoid disabling interrupt. It's a slow path, especially in a guest.
Baolu Lu
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 19 lines · Kevin
Quoted 19 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:25 AMQuoted 14 lines · Kevin
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 9 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/nested.c +6
-0+ +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);Disabling interrupt while invalidating iotlb is certainly unacceptable.
Actually there is no need to walk devices. Under
dmar_domainthere is already a list of attached iommu's.
Quoted 6 lines
Quoted 3 lines
Walking device is only necessary when invalidating device TLB. For iotlb invalidation, it only needs to know the iommu's.
even for device tlb we may think whether there is any better way to avoid disabling interrupt. It's a slow path, especially in a guest.
I ever tried this. But some device drivers call iommu_unmap() in the interrupt critical path. :-( So we have a long way to go.
Best regards,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 27 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 12:06 PMQuoted 23 lines · Kevin
Quoted 22 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:25 AMQuoted 12 lines · Kevin
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 9 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/nested.c +6
-0+ +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);Disabling interrupt while invalidating iotlb is certainly unacceptable.
Quoted 2 lines
Actually there is no need to walk devices. Under
dmar_domainthere is already a list of attached iommu's.Walking device is only necessary when invalidating device TLB. For iotlb invalidation, it only needs to know the iommu's.
Quoted 6 lines
Quoted 2 lines
even for device tlb we may think whether there is any better way to avoid disabling interrupt. It's a slow path, especially in a guest.
I ever tried this. But some device drivers call
iommu_unmap()in the interrupt critical path. :-( So we have a long way to go.
emmm... this path only comes from iommufd and the domain is user-managed. There won't be kernel drivers to call iommu_unmap() on such domain.
Baolu Lu
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 30 lines · Kevin
Quoted 30 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 12:06 PMQuoted 25 lines · Kevin
Quoted 16 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:25 AMQuoted 12 lines · Kevin
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 123
Quoted 9 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/nested.c +6
-0+ +spin_lock_irqsave (&dmar_domain->lock, flags); +list_for_each_entry (info, &dmar_domain->devices, link) +intel_nested_invalidate (info->dev, dmar_domain, + req->addr, req->npages); +spin_unlock_irqrestore (&dmar_domain->lock, flags);Disabling interrupt while invalidating iotlb is certainly unacceptable.
Quoted 6 lines
Quoted 2 lines
Actually there is no need to walk devices. Under
dmar_domainthere is already a list of attached iommu's.Walking device is only necessary when invalidating device TLB. For iotlb invalidation, it only needs to know the iommu's.
even for device tlb we may think whether there is any better way to avoid disabling interrupt. It's a slow path, especially in a guest.
Quoted 7 lines
Quoted 3 lines
I ever tried this. But some device drivers call
iommu_unmap()in the interrupt critical path. :-( So we have a long way to go.emmm... this path only comes from iommufd and the domain is user-managed. There won't be kernel drivers to call
iommu_unmap()on such domain.
Probably we can use a different lock for nested domain and add a comment around the lock with above explanation.
Best regards,
baolu
RE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
In reply to Tian, KevinQuoted 27 lines
From: Tian, Kevin <[email protected]>
Sent: Wednesday, August 2, 2023 3:47 PMSubject: RE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 22 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +17
-0+ void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size; +u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + +if (get_user (entry_nr, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +for (index =0 ; index < entry_nr; index++) {
Quoted 8 lines
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain) · line 107
Quoted 4 lines
(quoted diff) +2
-0+ ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),drivers/iommu/intel/nested.c +1
-0+ entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateandiommu_hwpt_vtd_s1_invalidate_descsince they are uAPI and subject to change.
Then needs to define size in the uapi data structure, and copy size first and check minsz before going forward. How about the structures for hwpt alloc like struct iommu_hwpt_vtd_s1? Should check minsz for them as well?
Regards,
Yi Liu
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 31 lines · Yi L
Quoted 22 lines
Quoted 22 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +17
-0+ void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size; +u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + +if (get_user (entry_nr, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +for (index =0 ; index < entry_nr; index++) {Quoted 8 lines
Quoted 4 lines
(quoted diff) +2
-0+ ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateandiommu_hwpt_vtd_s1_invalidate_descsince they are uAPI and subject to change.
Quoted 3 lines
Then needs to define size in the uapi data structure, and copy size first and check minsz before going forward. How about the structures for hwpt alloc like struct
iommu_hwpt_vtd_s1? Should check minsz for them as well?
Assuming that every uAPI data structure needs a min_size, we can either add a structure holding all min_sizes like iommufd main.c or have another xx_min_len in iommu_/domain_ops.
Currently, we have the union of structures in iommu.h and also a xx_data_len in iommu_ops. So, neither of the two places seem to be optimal from my p.o.v... any suggestion?
Also, alloc allows data_len=0, so a min_size check will be only applied to data_len > 0 case.
Thanks
Nic
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 32 lines · Nicolin Chen
Quoted 31 lines · Yi L
Quoted 30 lines
Quoted 24 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +19
-0+ void *user_data) +{ + struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; + struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + unsigned int entry_size = inv_info->entry_size; + u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (get_user(entry_nr, (uint32_t __user *)u64_to_user_ptr(nr_uptr))) + return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *Quoted 2 lines
entry_size),
+entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateandiommu_hwpt_vtd_s1_invalidate_descsince they are uAPI and subject to change.
Quoted 7 lines
Quoted 3 lines
Then needs to define size in the uapi data structure, and copy size first and check minsz before going forward. How about the structures for hwpt alloc like struct
iommu_hwpt_vtd_s1? Should check minsz for them as well?Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.
If driver is doing the copy it is OK that driver does the min_size check too
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 37 lines · Jason Gunthorpe
Quoted 36 lines · Nicolin Chen
Quoted 34 lines · Yi L
Quoted 22 lines
Quoted 22 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domain*domain,drivers/iommu/intel/nested.c +17
-0+ void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size; +u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + +if (get_user (entry_nr, (uint32_t __user *)u64_to_user_ptr (nr_uptr))) +return -EFAULT ; + +for (index =0 ; index < entry_nr; index++) {Quoted 8 lines
Quoted 4 lines
(quoted diff) +2
-0+ ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateandiommu_hwpt_vtd_s1_invalidate_descsince they are uAPI and subject to change.Then needs to define size in the uapi data structure, and copy size first and check minsz before going forward. How about the structures for hwpt alloc like struct
iommu_hwpt_vtd_s1? Should check minsz for them as well?
Quoted 6 lines
Quoted 3 lines
Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.If driver is doing the copy it is OK that driver does the
min_sizecheck too
Ah, just realized my reply above was missing a context..
Yi and I are having a concern that the core iommu_hpwt_alloc() and iommu_hwpt_cache_invalidate(), in the nesting series, copy data without checking the min_sizes. So, we are trying to add the missing piece into the next version but not sure which way could be optimal.
It probably makes sense to add cache_invalidate_user_min_len next to the existing cache_invalidate_user_data_len. For the iommu_hwpt_alloc, we are missing a data_len, as the core just uses sizeof(union iommu_domain_user_data) when calling the copy_struct_from_user().
Perhaps we could add two pairs of data_len/min_len in the ops structs:
// iommu_ops
const size_t domain_alloc_user_data_len; // for sanity© const size_t domain_alloc_user_min_len; // for sanity only // iommu_domain_ops
const size_t cache_invalidate_user_data_len; // for sanity© const size_t cache_invalidate_user_min_len; // for sanity only
Thanks
Nic
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 67 lines
From: Nicolin Chen <[email protected]>
Sent: Wednesday, August 9, 2023 1:42 AMQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 4 lines · Yi L
Quoted 4 lines
Quoted 4 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domainQuoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
*domain,
drivers/iommu/intel/nested.c +6
-0+ void *user_data) +{ +struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; +struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; +struct dmar_domain *dmar_domain =to_dmar_domain (domain); +unsigned int entry_size = inv_info->entry_size;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/iommu/intel/nested.c static void intel_nested_domain_free(struct iommu_domain *domain)
Quoted 8 lines
drivers/iommu/intel/nested.c +8
-0+u64 uptr = inv_info->inv_data_uptr; +u64 nr_uptr = inv_info->entry_nr_uptr; +struct device_domain_info *info; +u32 entry_nr, index; +unsigned long flags; +int ret =0 ; + + if (get_user(entry_nr, (uint32_t __user*)u64_to_user_ptr(nr_uptr)))
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
drivers/iommu/intel/nested.c +5
-0+ return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateand
iommu_hwpt_vtd_s1_invalidate_descQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
since they are uAPI and subject to change.
Then needs to define size in the uapi data structure, and copy size first
and
Quoted 1 line
Quoted 1 line
Quoted 1 line
check minsz before going forward. How about the structures for hwpt
alloc
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
like struct
iommu_hwpt_vtd_s1? Should check minsz for them as well?Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.If driver is doing the copy it is OK that driver does the
min_sizecheck tooAh, just realized my reply above was missing a context..
Yi and I are having a concern that the core
iommu_hpwt_alloc()andiommu_hwpt_cache_invalidate(), in the nesting series, copy data without checking themin_sizes. So, we are trying to add the missing piece into the next version but not sure which way could be optimal.It probably makes sense to add
cache_invalidate_user_min_lennext to the existingcache_invalidate_user_data_len. For theiommu_hwpt_alloc, we are missing adata_len, as the core just usessizeof(union iommu_domain_user_data)when calling thecopy_struct_from_user().
Quoted 9 lines
Perhaps we could add two pairs of data_len/min_len in the ops structs:
//iommu_ops
constsize_tdomain_alloc_user_data_len; // for sanity© constsize_tdomain_alloc_user_min_len; // for sanity only //iommu_domain_ops
constsize_tcache_invalidate_user_data_len; // for sanity© constsize_tcache_invalidate_user_min_len; // for sanity only
What about creating a simple array to track type specific len in iommufd instead of adding more fields to iommu/domain_ops? anyway it's iommufd doing the copy and all the type specific structures are already defined in the uapi header.
and a similar example already exists in union ucmd_buffer which includes type specific structures to avoid memory copy...
RE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 80 lines
From: Tian, Kevin <[email protected]>
Sent: Wednesday, August 9, 2023 4:23 PMQuoted 67 lines
From: Nicolin Chen <[email protected]>
Sent: Wednesday, August 9, 2023 1:42 AMQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 4 lines · Yi L
Quoted 4 lines
Quoted 4 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domainQuoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
*domain,
(quoted diff) +6
-0+ void *user_data) +{ + struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; + struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + unsigned int entry_size = inv_info->entry_size;Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (get_user(entry_nr, (uint32_t __user*)u64_to_user_ptr(nr_uptr)))
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
(quoted diff) +5
-0+ return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);If continuing this direction then the driver should also check minsz etc. for struct
iommu_hwpt_vtd_s1_invalidateand
iommu_hwpt_vtd_s1_invalidate_descQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
since they are uAPI and subject to change.
Then needs to define size in the uapi data structure, and copy size first
and
Quoted 1 line
Quoted 1 line
Quoted 1 line
check minsz before going forward. How about the structures for hwpt
alloc
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
like struct
iommu_hwpt_vtd_s1? Should check minsz for them as well?Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.If driver is doing the copy it is OK that driver does the
min_sizecheck tooAh, just realized my reply above was missing a context..
Yi and I are having a concern that the core
iommu_hpwt_alloc()andiommu_hwpt_cache_invalidate(), in the nesting series, copy data without checking themin_sizes. So, we are trying to add the missing piece into the next version but not sure which way could be optimal.It probably makes sense to add
cache_invalidate_user_min_lennext to the existingcache_invalidate_user_data_len. For theiommu_hwpt_alloc, we are missing adata_len, as the core just usessizeof(union iommu_domain_user_data)when calling thecopy_struct_from_user().Quoted 9 lines
Perhaps we could add two pairs of data_len/min_len in the ops structs:
//iommu_ops
constsize_tdomain_alloc_user_data_len; // for sanity© constsize_tdomain_alloc_user_min_len; // for sanity only //iommu_domain_ops
constsize_tcache_invalidate_user_data_len; // for sanity© constsize_tcache_invalidate_user_min_len; // for sanity only
Quoted 4 lines
What about creating a simple array to track type specific len in iommufd instead of adding more fields to iommu/domain_ops? anyway it's iommufd doing the copy and all the type specific structures are already defined in the uapi header.
Then index the array with type value, is it? Seems like we have defined such array before for the length of hwpt_alloc and invalidate structures. but finally we dropped it the array may grow largely per new types.
Quoted 3 lines
and a similar example already exists in union
ucmd_bufferwhich includes type specific structures to avoid memory copy...
Not quite get here. ucmd_buffer is a union used to copy any user data. But here we want to check the minsz of the the user data. Seems not related.
Regards,
Yi Liu
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 88 lines
From: Liu, Yi L <[email protected]>
Sent: Wednesday, August 9, 2023 4:50 PMQuoted 20 lines
From: Tian, Kevin <[email protected]>
Sent: Wednesday, August 9, 2023 4:23 PMQuoted 17 lines
From: Nicolin Chen <[email protected]>
Sent: Wednesday, August 9, 2023 1:42 AMQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 4 lines · Yi L
Quoted 4 lines
Quoted 4 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domainQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
*domain,
(quoted diff) +5
-0+ void *user_data) +{ + struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; + struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; + struct dmar_domain *dmar_domain =
to_dmar_domain(domain);Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+ unsigned int entry_size = inv_info->entry_size; + u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (get_user(entry_nr, (uint32_t __user*)u64_to_user_ptr(nr_uptr)))
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
(quoted diff) +5
-0+ return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
If continuing this direction then the driver should also check minsz
etc.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
for struct
iommu_hwpt_vtd_s1_invalidateand
iommu_hwpt_vtd_s1_invalidate_descQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
since they are uAPI and subject to change.
Then needs to define size in the uapi data structure, and copy size
first
Quoted 2 lines
Quoted 2 lines
and
Quoted 1 line
Quoted 1 line
Quoted 1 line
check minsz before going forward. How about the structures for
hwpt
Quoted 2 lines
Quoted 2 lines
alloc
Quoted 1 line
Quoted 1 line
Quoted 1 line
like struct
iommu_hwpt_vtd_s1? Should check minsz for them aswell?
Quoted 32 lines
Quoted 31 lines
Quoted 7 lines
Quoted 4 lines
Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.If driver is doing the copy it is OK that driver does the
min_sizecheck tooAh, just realized my reply above was missing a context..
Yi and I are having a concern that the core
iommu_hpwt_alloc()andiommu_hwpt_cache_invalidate(), in the nesting series, copy data without checking themin_sizes. So, we are trying to add the missing piece into the next version but not sure which way could be optimal.It probably makes sense to add
cache_invalidate_user_min_lennext to the existingcache_invalidate_user_data_len. For theiommu_hwpt_alloc, we are missing adata_len, as the core just usessizeof(union iommu_domain_user_data)when calling thecopy_struct_from_user().Perhaps we could add two pairs of data_len/min_len in the ops structs:
//iommu_ops
constsize_tdomain_alloc_user_data_len; // for sanity© constsize_tdomain_alloc_user_min_len; // for sanity only //iommu_domain_ops
constsize_tcache_invalidate_user_data_len; // for sanity© constsize_tcache_invalidate_user_min_len; // for sanity only
Quoted 8 lines
Quoted 4 lines
What about creating a simple array to track type specific len in iommufd instead of adding more fields to iommu/domain_ops? anyway it's iommufd doing the copy and all the type specific structures are already defined in the uapi header.
Then index the array with type value, is it? Seems like we have defined such array before for the length of
hwpt_allocand invalidate structures. but finally we dropped it the array may grow largely per new types.
I'm not sure how many types iommufd will support in reality.
Just my personal feeling that having information contained in iommufd is cleaner than expanding iommu core abstraction to assist iommufd user buffer copy/verification.
Quoted 9 lines
Quoted 4 lines
and a similar example already exists in union
ucmd_bufferwhich includes type specific structures to avoid memory copy...Not quite get here.
ucmd_bufferis a union used to copy any user data. But here we want to check the minsz of the the user data. Seems not related.
that's the example for recording vendor specific structure information in iommufd and it will also grow along with new added types.
RE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 111 lines
From: Tian, Kevin <[email protected]>
Sent: Wednesday, August 9, 2023 4:58 PMQuoted 88 lines
From: Liu, Yi L <[email protected]>
Sent: Wednesday, August 9, 2023 4:50 PMQuoted 20 lines
From: Tian, Kevin <[email protected]>
Sent: Wednesday, August 9, 2023 4:23 PMQuoted 17 lines
From: Nicolin Chen <[email protected]>
Sent: Wednesday, August 9, 2023 1:42 AMQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 4 lines · Yi L
Quoted 4 lines
Quoted 4 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM+static int intel_nested_cache_invalidate_user(struct
iommu_domainQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
*domain,
(quoted diff) +5
-0+ void *user_data) +{ + struct iommu_hwpt_vtd_s1_invalidate_desc *req = user_data; + struct iommu_hwpt_vtd_s1_invalidate *inv_info = user_data; + struct dmar_domain *dmar_domain =
to_dmar_domain(domain);Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+ unsigned int entry_size = inv_info->entry_size; + u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (get_user(entry_nr, (uint32_t __user*)u64_to_user_ptr(nr_uptr)))
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
(quoted diff) +5
-0+ return -EFAULT; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index *
entry_size),
+entry_size);Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
If continuing this direction then the driver should also check minsz
etc.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
for struct
iommu_hwpt_vtd_s1_invalidateand
iommu_hwpt_vtd_s1_invalidate_descQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
since they are uAPI and subject to change.
Then needs to define size in the uapi data structure, and copy size
first
Quoted 2 lines
Quoted 2 lines
and
Quoted 1 line
Quoted 1 line
Quoted 1 line
check minsz before going forward. How about the structures for
hwpt
Quoted 2 lines
Quoted 2 lines
alloc
Quoted 1 line
Quoted 1 line
Quoted 1 line
like struct
iommu_hwpt_vtd_s1? Should check minsz for them aswell?
Quoted 32 lines
Quoted 31 lines
Quoted 7 lines
Quoted 4 lines
Assuming that every uAPI data structure needs a
min_size, we can either add a structure holding allmin_sizeslike iommufdmain.cor have anotherxx_min_lenin iommu_/domain_ops.If driver is doing the copy it is OK that driver does the
min_sizecheck tooAh, just realized my reply above was missing a context..
Yi and I are having a concern that the core
iommu_hpwt_alloc()andiommu_hwpt_cache_invalidate(), in the nesting series, copy data without checking themin_sizes. So, we are trying to add the missing piece into the next version but not sure which way could be optimal.It probably makes sense to add
cache_invalidate_user_min_lennext to the existingcache_invalidate_user_data_len. For theiommu_hwpt_alloc, we are missing adata_len, as the core just usessizeof(union iommu_domain_user_data)when calling thecopy_struct_from_user().Perhaps we could add two pairs of data_len/min_len in the ops structs:
//iommu_ops
constsize_tdomain_alloc_user_data_len; // for sanity© constsize_tdomain_alloc_user_min_len; // for sanity only //iommu_domain_ops
constsize_tcache_invalidate_user_data_len; // for sanity© constsize_tcache_invalidate_user_min_len; // for sanity onlyQuoted 8 lines
Quoted 4 lines
What about creating a simple array to track type specific len in iommufd instead of adding more fields to iommu/domain_ops? anyway it's iommufd doing the copy and all the type specific structures are already defined in the uapi header.
Then index the array with type value, is it? Seems like we have defined such array before for the length of
hwpt_allocand invalidate structures. but finally we dropped it the array may grow largely per new types.I'm not sure how many types iommufd will support in reality.
Just my personal feeling that having information contained in iommufd is cleaner than expanding iommu core abstraction to assist iommufd user buffer copy/verification.
Quoted 5 lines
Quoted 4 lines
and a similar example already exists in union
ucmd_bufferwhich includes type specific structures to avoid memory copy...
Quoted 7 lines
Quoted 4 lines
Not quite get here.
ucmd_bufferis a union used to copy any user data. But here we want to check the minsz of the the user data. Seems not related.that's the example for recording vendor specific structure information in iommufd and it will also grow along with new added types.
Yeah, adding new structures to ucmd_buffer may increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as 0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things in iommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.
Regards,
Yi Liu
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 7 lines · Yi L
Yeah, adding new structures to
ucmd_buffermay increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things iniommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.
Maybe we are trying too hard to make it "easy" on the driver.
Can't we just have the driver invoke some:
driver_iommufd_invalidate_op(??? *opaque)
{
struct driver_base_struct args;
rc = iommufd_get_args(opaque, &args, sizeof(args),
offsetof(args, last));
if (rc)
return rc;
}The whole point of this excercise was to avoid the mistake where drivers code the uapi checks incorrectly. We can achieve the same outcome by providing a mandatory helper.
Similarly for managing the array of invalidation commands.
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Yi L
Yeah, adding new structures to
ucmd_buffermay increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things iniommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.Maybe we are trying too hard to make it "easy" on the driver.
Quoted 8 lines
Can't we just have the driver invoke some:
driver_iommufd_invalidate_op(??? *opaque){ struct driver_base_struct args; rc = iommufd_get_args(opaque, &args, sizeof(args), offsetof(args, last));
OK. So, IIUIC, the opaque should be:
struct iommu_user_data {
void __user *data_uptr;
size_t data_len;
}user_data;And core does basic sanity of data_uptr != NULL and data_len !=0 before passing this to driver, and then do a full sanity during the iommufd_get_args (or iommufd_get_user_data?) call.
Quoted 3 lines
The whole point of this excercise was to avoid the mistake where drivers code the uapi checks incorrectly. We can achieve the same outcome by providing a mandatory helper.
OK. I will rework this part today.
Quoted 1 line
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
Thanks
Nicolin
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 24 lines · Nicolin Chen
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Yi L
Yeah, adding new structures to
ucmd_buffermay increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things iniommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.Maybe we are trying too hard to make it "easy" on the driver.
Quoted 8 lines
Can't we just have the driver invoke some:
driver_iommufd_invalidate_op(??? *opaque){ struct driver_base_struct args; rc = iommufd_get_args(opaque, &args, sizeof(args), offsetof(args, last));OK. So, IIUIC, the opaque should be:
Quoted 8 lines
struct iommu_user_data { void __user *data_uptr; size_t data_len; }user_data;And core does basic sanity of
data_uptr!= NULL anddata_len!=0 before passing this to driver, and then do a full sanity during theiommufd_get_args(oriommufd_get_user_data?) call.
Don't even need to check datA_uptr and data_len, the helper should check the size and null is caught by copy from user
Quoted 4 lines
Quoted 1 line
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same user_data struct and use another helper to allow the driver to iterate through it.
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 30 lines · Jason Gunthorpe
Quoted 29 lines · Nicolin Chen
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Yi L
Yeah, adding new structures to
ucmd_buffermay increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things iniommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.Maybe we are trying too hard to make it "easy" on the driver.
Quoted 8 lines
Can't we just have the driver invoke some:
driver_iommufd_invalidate_op(??? *opaque){ struct driver_base_struct args; rc = iommufd_get_args(opaque, &args, sizeof(args), offsetof(args, last));OK. So, IIUIC, the opaque should be:
struct iommu_user_data { void __user *data_uptr; size_t data_len; }user_data;
Quoted 6 lines
Quoted 3 lines
And core does basic sanity of
data_uptr!= NULL anddata_len!=0 before passing this to driver, and then do a full sanity during theiommufd_get_args(oriommufd_get_user_data?) call.Don't even need to check
datA_uptranddata_len, the helper should check the size and null is caught by copy from user
I see. I was worried about the alloc path since its data input is optional upon IOMMU_DOMAIN_UNMANAGED. But this helper should work for that also.
In that case, we might not even need to define the union with all structures, in iommu.h.
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.
I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
Then, the driver-specific data structure will be the array entry level only.
@Yi,
This seems to be a bigger rework than the top level struct. Along with Jason's request for fail_nth below, we'd need to bisect the workload between us, or can just continue each other's daily work. Let me know which one you prefer.
https://lore.kernel.org/linux-iommu/[email protected]/
Thanks!
Nic
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 16 lines
From: Nicolin Chen <[email protected]>
Sent: Thursday, August 10, 2023 4:17 AMQuoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 1 line · Jason Gunthorpe
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.
Quoted 8 lines
I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
I like this approach.
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 25 lines · Kevin
Quoted 16 lines
From: Nicolin Chen <[email protected]>
Sent: Thursday, August 10, 2023 4:17 AMQuoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 1 line · Jason Gunthorpe
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.Quoted 8 lines
I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
Quoted 1 line
I like this approach.
Do we need to worry about the ring wrap around? It is already the case that the VMM has to scan the ring and extract the invalidation commands, wouldn't it already just linearize them?
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Let's answer those questions before designing something complicated :)
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 26 lines · Jason Gunthorpe
Quoted 25 lines · Kevin
Quoted 24 lines
From: Nicolin Chen <[email protected]>
Sent: Thursday, August 10, 2023 4:17 AMQuoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 1 line · Jason Gunthorpe
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
Quoted 5 lines
Quoted 1 line
I like this approach.
Do we need to worry about the ring wrap around? It is already the case that the VMM has to scan the ring and extract the invalidation commands, wouldn't it already just linearize them?
I haven't got the chance to send the latest vSMMU series but I pass down the raw user CMDQ to the host to go through, as it'd be easier to stall the consumer index movement when a command in the middle fails.
Quoted 2 lines
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Hmm, why a SW emulated ring?
Yes for the latter question. SMMU kernel driver has something like Q_WRP and other helpers, so it wasn't difficult to process the user CMDQ in the same raw form. But it does complicates the common code if we want to do it there.
Thanks
Nic
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 29 lines · Nicolin Chen
Quoted 28 lines · Jason Gunthorpe
Quoted 26 lines · Kevin
Quoted 16 lines
From: Nicolin Chen <[email protected]>
Sent: Thursday, August 10, 2023 4:17 AMQuoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 1 line · Jason Gunthorpe
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.Quoted 8 lines
I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
I like this approach.
Quoted 8 lines
Quoted 3 lines
Do we need to worry about the ring wrap around? It is already the case that the VMM has to scan the ring and extract the invalidation commands, wouldn't it already just linearize them?
I haven't got the chance to send the latest vSMMU series but I pass down the raw user CMDQ to the host to go through, as it'd be easier to stall the consumer index movement when a command in the middle fails.
Don't some commands have to be executed by the VMM?
Even so, it seems straightforward enough for the kernel to report the number of commands it executed and the VMM can adjust the virtual consumer index.
Quoted 5 lines
Quoted 3 lines
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Hmm, why a SW emulated ring?
That is what you are building. The VMM catches the write of the producer pointer and the VMM SW bundles it up to call into the kernel.
Quoted 4 lines
Yes for the latter question. SMMU kernel driver has something like
Q_WRPand other helpers, so it wasn't difficult to process the user CMDQ in the same raw form. But it does complicates the common code if we want to do it there.
Optimizing wrap around means when the producer/consumer pointers pass the end of the queue memory we execute one, not two ioctls toward the kernel. That is possible a very minor optimization, it depends how big the queues are and how frequent multi-entry items will be present.
Jaso
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 10 lines · Jason Gunthorpe
Quoted 8 lines
Quoted 3 lines
Do we need to worry about the ring wrap around? It is already the case that the VMM has to scan the ring and extract the invalidation commands, wouldn't it already just linearize them?
I haven't got the chance to send the latest vSMMU series but I pass down the raw user CMDQ to the host to go through, as it'd be easier to stall the consumer index movement when a command in the middle fails.
Don't some commands have to be executed by the VMM?
Well, they do. VMM would go through the queue and "execute" non- invalidation commands, then defer the queue to the kernel to go through the queue once more. So, the flaw could be that some of the commands behind the failing TLB flush command got "executed", though in a real case most of other commands would be "executed" standalone with a CMD_SYNC, i.e. not mixing with any invalidation command.
Quoted 3 lines
Even so, it seems straightforward enough for the kernel to report the number of commands it executed and the VMM can adjust the virtual consumer index.
It is not that straightforward to revert an array index back to a consumer index because they might not be 1:1 mapped, since in theory there could be other commands mixing in-between, although it unlikely happens.
So, another index-mapping array would be needed for this matter. And this doesn't address the flaw that I mentioned above either. So, I took the former solution to reduce the complication.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Hmm, why a SW emulated ring?
That is what you are building. The VMM catches the write of the producer pointer and the VMM SW bundles it up to call into the kernel.
Still not fully getting it. Do you mean a ring that is prepared by the VMM? I think the only case that we need to handle a ring is what I did by forwarding the guest CMDQ (a ring) to the host directly. Not sure why VMM would need another ring for those linearized invalidation commands. Or maybe I misunderstood..
Quoted 9 lines
Quoted 4 lines
Yes for the latter question. SMMU kernel driver has something like
Q_WRPand other helpers, so it wasn't difficult to process the user CMDQ in the same raw form. But it does complicates the common code if we want to do it there.Optimizing wrap around means when the producer/consumer pointers pass the end of the queue memory we execute one, not two ioctls toward the kernel. That is possible a very minor optimization, it depends how big the queues are and how frequent multi-entry items will be present.
There could be other commands being issued by other VMs or even the host between the two ioctls. So probably we'd need to handle the wrapping case when doing a ring solution?
Thanks
Nicolin
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Friday, August 11, 2023 5:03 AMQuoted 5 lines
Quoted 4 lines
Quoted 2 lines
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Hmm, why a SW emulated ring?
Quoted 9 lines
Quoted 2 lines
That is what you are building. The VMM catches the write of the producer pointer and the VMM SW bundles it up to call into the kernel.
Still not fully getting it. Do you mean a ring that is prepared by the VMM? I think the only case that we need to handle a ring is what I did by forwarding the guest CMDQ (a ring) to the host directly. Not sure why VMM would need another ring for those linearized invalidation commands. Or maybe I misunderstood..
iiuc the point of a ring-based native format is to maximum code reuse when later in-kernel fast invalidation path (from kvm to smmu driver) is enabled. Then both slow (via vmm) and fast (in-kernel) path use the same logic to handle guest invalidation queue.
But if stepping back a bit supporting an array-based non-native format could simplify the uAPI design and allows code sharing for array among vendor drivers. You can still keep the entry as native format then the only difference with future in-kernel fast path is just on walking an array vs. walking a ring. And VMM doesn't need to expose non-invalidate cmds to the kernel and then be skipped.
Thanks
Kevin
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 18 lines · Kevin
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Friday, August 11, 2023 5:03 AMQuoted 5 lines
Quoted 4 lines
Quoted 2 lines
Is there a use case for invaliation only SW emulated rings, and do we care about optimizing for the wrap around case?
Hmm, why a SW emulated ring?
Quoted 9 lines
Quoted 2 lines
That is what you are building. The VMM catches the write of the producer pointer and the VMM SW bundles it up to call into the kernel.
Still not fully getting it. Do you mean a ring that is prepared by the VMM? I think the only case that we need to handle a ring is what I did by forwarding the guest CMDQ (a ring) to the host directly. Not sure why VMM would need another ring for those linearized invalidation commands. Or maybe I misunderstood..
Quoted 4 lines
iiuc the point of a ring-based native format is to maximum code reuse when later in-kernel fast invalidation path (from kvm to smmu driver) is enabled. Then both slow (via vmm) and fast (in-kernel) path use the same logic to handle guest invalidation queue.
I see. That's about the fast path topic. Thanks for the input.
Quoted 6 lines
But if stepping back a bit supporting an array-based non-native format could simplify the uAPI design and allows code sharing for array among vendor drivers. You can still keep the entry as native format then the only difference with future in-kernel fast path is just on walking an array vs. walking a ring. And VMM doesn't need to expose non-invalidate cmds to the kernel and then be skipped.
Ah, so we might still design the uAPI to be ring based at this moment, yet don't support a case CONS > 0 to leave that to an upgrade in the future.
I will try estimating a bit how complicated to implement the ring, to see if we could just start with that. Otherwise, will just start with an array.
Thanks
Nic
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 7 lines · Nicolin Chen
Quoted 6 lines
But if stepping back a bit supporting an array-based non-native format could simplify the uAPI design and allows code sharing for array among vendor drivers. You can still keep the entry as native format then the only difference with future in-kernel fast path is just on walking an array vs. walking a ring. And VMM doesn't need to expose non-invalidate cmds to the kernel and then be skipped.
Quoted 7 lines
Ah, so we might still design the uAPI to be ring based at this moment, yet don't support a case CONS > 0 to leave that to an upgrade in the future.
I will try estimating a bit how complicated to implement the ring, to see if we could just start with that. Otherwise, will just start with an array.
I drafted a uAPI structure for a ring-based SW queue. While I am trying an implementation, I'd like to collect some comments at the structure, to see if it overall makes sense.
One thing that I couldn't add to this common structure for SMMU is the hardware error code, which should be encoded in the higher bits of the consumer index register, following the SMMU spec:
ERR, bits [30:24] Error reason code.
- When a command execution error is detected, ERR is set to a reason code and then the SMMU_GERROR.CMDQ_ERR global error becomes active.
- The value in this field is UNKNOWN when the CMDQ_ERR global error is not active. This field resets to an UNKNOWN value.
But, I feel it odd to do the same to the generic structure. So, perhaps an optional @out_error can be added to this structure. Or some other idea?
Thanks
Nic
/**
* struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE)
* @size: sizeof(struct iommu_hwpt_invalidate)
* @hwpt_id: HWPT ID of target hardware page table for the invalidation
* @q_uptr: User pointer to an invalidation queue, which can be used as a flat
* array or a circular ring queue. The entiry(s) in the queue must be
* at a fixed width @q_entry_len, containing a user data structure for
* an invalidation request, specific to the given hardware pagetable.
* @q_cons_uptr: User pointer to the consumer index (with its wrap flag) of an
* invalidation queue. This pointer must point to a __u32 type of
* memory location. The consumer index tells kernel to read from
* the entry pointed by it (and then its next entry) until kernel
* reaches the entry pointed by the producer index @q_prod, and
* allows kernel to update the consumer index to where it stops:
* on success, it should be updated to @q_prod; otherwise, to the
* index pointing to the failed entry.
* @q_prod: Producer index (with its wrap flag) of an invalidation queue. This
* index points to the entry next to the last requested entry in the
* invalidation queue. In case of using the queue as a flat array, it
* equals to the number of entries @q_entry_num.
* @q_index_bits: Effective bits of both indexes. Defines the maximum value an
* index can be. Must not be greater than 31 bits. A wrap flag
* is defined at the next higher bit adjacent to the index bits:
* e.g. if @q_index_bits is 20, @q_prod[19:0] are the index bits
* and @q_prod[20] is the wrap flag. The wrap flag, acting like
* a sign flag, must be toggled each time an index overflow and
* wraps to the lower end of the circular queue.
* @q_entry_num: Totaly number of the entries in an invalidation queue
* @q_entry_len: Length (in bytes) of an entry of an invalidation queue
*
* Invalidate the iommu cache for user-managed page table. Modifications on a
* user-managed page table should be followed by this operation to sync cache.
*
* One request supports multiple invalidations via a multi-entry queue:
* |<----------- Length of Queue = @q_entry_num * @q_entry_len ------------>|
* --------------------------------------------------------------------------
* | 0 | 1 | 2 | 3 | ... | @q_entry_num-3 | @q_entry_num-2 | @q_entry_num-1 |
* --------------------------------------------------------------------------
* ^ ^ ^ |<-@q_entry_len->|
* | | |
* @q_uptr @q_cons_uptr @q_prod
*
* A queue index can wrap its index bits off the high end of the queue and back
* onto the low end by toggling its wrap flag: e.g. when @q_entry_num=0x10 and
* @q_index_bits=4, *@q_cons_uptr=0xf and @q_prod=0x11 inputs mean the producer
* index is wrapped to 0x1 with its wrap flag set, so kernel reads/handles the
* entry starting from by the consumer index (0xf) and wraps it back to 0x0 and
* 0x1 by toggling the wrap flag, i.e. *@q_cons_uptr has a final value of 0x11.
*/
struct iommu_hwpt_invalidate {
__u32 size;
__u32 hwpt_id;
__aligned_u64 q_uptr;
__aligned_u64 q_cons_uptr;
__u32 q_prod;
__u32 q_index_bits;
__u32 q_entry_num;
__u32 q_entry_len;
};#define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE)
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 13 lines · Nicolin Chen
Quoted 11 lines · Nicolin Chen
Quoted 6 lines
But if stepping back a bit supporting an array-based non-native format could simplify the uAPI design and allows code sharing for array among vendor drivers. You can still keep the entry as native format then the only difference with future in-kernel fast path is just on walking an array vs. walking a ring. And VMM doesn't need to expose non-invalidate cmds to the kernel and then be skipped.
Ah, so we might still design the uAPI to be ring based at this moment, yet don't support a case CONS > 0 to leave that to an upgrade in the future.
Quoted 7 lines
Quoted 3 lines
I will try estimating a bit how complicated to implement the ring, to see if we could just start with that. Otherwise, will just start with an array.
I drafted a uAPI structure for a ring-based SW queue. While I am trying an implementation, I'd like to collect some comments at the structure, to see if it overall makes sense.
I don't think a ring makes alot of sense at this point. The only thing it optimizes is a system call if the kernel needs to wrap around the tail of the ring. It would possibly be better to have a small gather list than try to describe the ring logic..
Further, the VMM already has to process it, so the vmm already knows what ops are going to kernel are not. The VMM can just organize them in a linear list in one way or another. We need to copy and read this stuff in the VMM anyhow to protect against a hostile VM.
Quoted 9 lines
One thing that I couldn't add to this common structure for SMMU is the hardware error code, which should be encoded in the higher bits of the consumer index register, following the SMMU spec:
ERR, bits [30:24] Error reason code.
- When a command execution error is detected, ERR is set to a reason code and then the SMMU_GERROR.CMDQ_ERR global error becomes active.
- The value in this field is UNKNOWN when theCMDQ_ERRglobal error is not active. This field resets to an UNKNOWN value.
The invalidate ioctl should fail in some deterministic way and report back the error code and the highest array index that maybe could have triggered it.
The highest array index sounds generic, the error code maybe is too
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 27 lines · Jason Gunthorpe
Quoted 13 lines · Nicolin Chen
Quoted 11 lines · Nicolin Chen
Quoted 6 lines
But if stepping back a bit supporting an array-based non-native format could simplify the uAPI design and allows code sharing for array among vendor drivers. You can still keep the entry as native format then the only difference with future in-kernel fast path is just on walking an array vs. walking a ring. And VMM doesn't need to expose non-invalidate cmds to the kernel and then be skipped.
Ah, so we might still design the uAPI to be ring based at this moment, yet don't support a case CONS > 0 to leave that to an upgrade in the future.
Quoted 7 lines
Quoted 3 lines
I will try estimating a bit how complicated to implement the ring, to see if we could just start with that. Otherwise, will just start with an array.
I drafted a uAPI structure for a ring-based SW queue. While I am trying an implementation, I'd like to collect some comments at the structure, to see if it overall makes sense.
I don't think a ring makes alot of sense at this point. The only thing it optimizes is a system call if the kernel needs to wrap around the tail of the ring. It would possibly be better to have a small gather list than try to describe the ring logic..
Quoted 4 lines
Further, the VMM already has to process it, so the vmm already knows what ops are going to kernel are not. The VMM can just organize them in a linear list in one way or another. We need to copy and read this stuff in the VMM anyhow to protect against a hostile VM.
OK. Then an linear array it is.
Quoted 10 lines
Quoted 9 lines
One thing that I couldn't add to this common structure for SMMU is the hardware error code, which should be encoded in the higher bits of the consumer index register, following the SMMU spec:
ERR, bits [30:24] Error reason code.
- When a command execution error is detected, ERR is set to a reason code and then the SMMU_GERROR.CMDQ_ERR global error becomes active.
- The value in this field is UNKNOWN when theCMDQ_ERRglobal error is not active. This field resets to an UNKNOWN value.
Quoted 3 lines
The invalidate ioctl should fail in some deterministic way and report back the error code and the highest array index that maybe could have triggered it.
Yea. Having an error code in the highest bits of array_index, "array_index != array_max" could be the deterministic way to indicate a failure. And a kernel errno could be returned also to the invalidate ioctl.
Quoted 1 line
The highest array index sounds generic, the error code maybe is too
We could do in its and report the error code in its raw form:
__u32 out_array_index; /* number of bits used to report error code in the returned array_index */ __u32 out_array_index_error_code_bits; Or just: __u32 out_array_index; __u32 out_error_code;
Do we have to define a list of generic error code?
Thanks!
Nic
Jason Gunthorpe
maintainerRe: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 11 lines · Nicolin Chen
Quoted 1 line
The highest array index sounds generic, the error code maybe is too
We could do in its and report the error code in its raw form:
__u32 out_array_index; /* number of bits used to report error code in the returned array_index */ __u32 out_array_index_error_code_bits; Or just: __u32 out_array_index; __u32 out_error_code;Do we have to define a list of generic error code?
out_driver_error_code may be OK
Jason
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Nicolin Chen
Quoted 1 line
The highest array index sounds generic, the error code maybe is too
We could do in its and report the error code in its raw form:
__u32 out_array_index; /* number of bits used to report error code in the returned array_index */ __u32 out_array_index_error_code_bits; Or just: __u32 out_array_index; __u32 out_error_code;
Quoted 3 lines
Quoted 1 line
Do we have to define a list of generic error code?
out_driver_error_codemay be OK
Ack. Will implement the array in that way.
Thanks!
Nic
Tian, Kevin
maintainerRE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 17 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, August 19, 2023 2:26 AMQuoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Nicolin Chen
Quoted 1 line
The highest array index sounds generic, the error code maybe is too
We could do in its and report the error code in its raw form:
__u32 out_array_index; /* number of bits used to report error code in the returned
array_index*/Quoted 5 lines
Quoted 5 lines
__u32 out_array_index_error_code_bits; Or just: __u32 out_array_index; __u32 out_error_code;
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Do we have to define a list of generic error code?
out_driver_error_codemay be OKAck. Will implement the array in that way.
Yes. Error code is driver specific. Just having a field to carry it is sufficient.
RE: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
In reply to Nicolin ChenQuoted 69 lines
From: Nicolin Chen <[email protected]>
Sent: Thursday, August 10, 2023 4:17 AMQuoted 30 lines · Jason Gunthorpe
Quoted 29 lines · Nicolin Chen
Quoted 12 lines · Jason Gunthorpe
Quoted 7 lines · Yi L
Yeah, adding new structures to
ucmd_buffermay increase the size as well if the new one is larger. While for an array, if there is new entry, it is for sure to increase the size. I remember there is one tricky thing when handling the selftest type. E.g. it is defined as0xbadbeef, if using it to index array, it would expire. So we have some special handling on it. If defining the things iniommu_ops, it is simpler. Selftest may be not so critical to determining the direction though.Maybe we are trying too hard to make it "easy" on the driver.
Quoted 8 lines
Can't we just have the driver invoke some:
driver_iommufd_invalidate_op(??? *opaque){ struct driver_base_struct args; rc = iommufd_get_args(opaque, &args, sizeof(args), offsetof(args, last));OK. So, IIUIC, the opaque should be:
struct iommu_user_data { void __user *data_uptr; size_t data_len; }user_data;Quoted 6 lines
Quoted 3 lines
And core does basic sanity of
data_uptr!= NULL anddata_len!=0 before passing this to driver, and then do a full sanity during theiommufd_get_args(oriommufd_get_user_data?) call.Don't even need to check
datA_uptranddata_len, the helper should check the size and null is caught by copy from userI see. I was worried about the alloc path since its data input is optional upon
IOMMU_DOMAIN_UNMANAGED. But this helper should work for that also.In that case, we might not even need to define the union with all structures, in
iommu.h.Quoted 9 lines
Quoted 4 lines
Quoted 1 line
Similarly for managing the array of invalidation commands.
You mean an embedded uptr inside a driver user data struct right? Sure, that should go through the new helper too.
If we are committed that all drivers have to process an array then put the array in the top level struct and pass it in the same
user_datastruct and use another helper to allow the driver to iterate through it.I see. Both VTD and SMMU pass uptr to the arrays of invalidation commands/requests. The only difference is that SMMU's array is a ring buffer other than a plain one indexing from the beginning. But the helper could take two index inputs, which should work for VTD case too. If another IOMMU driver only supports one request, rather than a array of requests, we can treat that as a single- entry array.
Then, the driver-specific data structure will be the array entry level only.
Quoted 6 lines
@Yi,
This seems to be a bigger rework than the top level struct. Along with Jason's request forfail_nthbelow, we'd need to bisect the workload between us, or can just continue each other's daily work. Let me know which one you prefer.
https://lore.kernel.org/linux-iommu/[email protected]/
Let me address the fail_nth request first. You may rework the iommufd_get_user_data(). If I can finish the fail_nth soon, then may help to lift the array to the top level. If not, you may make it as well. 😊 I guess I need some study on nth as well.
Regards,
Yi Liu
Re: [PATCH v4 09/12] iommu/vt-d: Add iotlb flush for nested domain
Quoted 7 lines · Yi L
Quoted 6 lines
@Yi,
This seems to be a bigger rework than the top level struct. Along with Jason's request forfail_nthbelow, we'd need to bisect the workload between us, or can just continue each other's daily work. Let me know which one you prefer.
https://lore.kernel.org/linux-iommu/[email protected]/
Quoted 5 lines
Let me address the
fail_nthrequest first. You may rework theiommufd_get_user_data(). If I can finish thefail_nthsoon, then may help to lift the array to the top level. If not, you may make it as well. 😊 I guess I need some study on nth as well.
Ack. Am working on the iommufd_get_user_data() already. Will continue the ring buffer thing.
Nic
PATCH v4 10/12 iommu/vt-d: Add nested domain allocation
In reply to Yi LiuFrom: Lu Baolu <[email protected]>
This adds the support for IOMMU_HWPT_TYPE_VTD_S1 type.
--- drivers/iommu/intel/iommu.c | 20 ++++++++++++++++++++ include/linux/iommu.h | 1 + 2 files changed, 21 insertions(+)
drivers/iommu/intel/iommu.c +20 -0
@@ -4084,6 +4084,25 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)return NULL ; } +static struct iommu_domain * +intel_iommu_domain_alloc_user (struct device *dev, +enum iommu_hwpt_type hwpt_type, +struct iommu_domain *parent, +const union iommu_domain_user_data *user_data) +{ +if (hwpt_type !=IOMMU_HWPT_TYPE_DEFAULT && + hwpt_type !=IOMMU_HWPT_TYPE_VTD_S1 ) +return ERR_PTR (-EINVAL ); + +if ((hwpt_type ==IOMMU_HWPT_TYPE_DEFAULT ) == !!parent) +return ERR_PTR (-EINVAL ); + +if (parent) +return intel_nested_domain_alloc (parent, user_data); +else +return iommu_domain_alloc (dev->bus); +} +static void intel_iommu_domain_free (struct iommu_domain *domain) {if (domain != &si_domain->domain && domain != &blocking_domain)
@@ -4732,6 +4751,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, .domain_alloc = intel_iommu_domain_alloc, + .domain_alloc_user = intel_iommu_domain_alloc_user, .probe_device = intel_iommu_probe_device, .probe_finalize = intel_iommu_probe_finalize, .release_device = intel_iommu_release_device,
include/linux/iommu.h +1 -0
@@ -240,6 +240,7 @@ union iommu_domain_user_data {#ifdef CONFIG_IOMMUFD_TEST __u64 test[2 ];#endif +struct iommu_hwpt_vtd_s1 vtd; };/**
Signature · 1 line
2.34.1
RE: [PATCH v4 10/12] iommu/vt-d: Add nested domain allocation
Quoted 9 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMFrom: Lu Baolu <[email protected]>
This adds the support for
IOMMU_HWPT_TYPE_VTD_S1type.Signed-off-by Lu Baolu <[email protected]>Signed-off-by Yi Liu <[email protected]>
PATCH v4 11/12 iommu/vt-d: Implement hw_info for iommu capability query
In reply to Yi LiuAdd intel_iommu_hw_info() to report cap_reg and ecap_reg information.
--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)
drivers/iommu/intel/iommu.c +19 -0
@@ -4748,8 +4748,26 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)intel_pasid_tear_down_entry (iommu, dev, pasid, false); } +static void *intel_iommu_hw_info (struct device *dev,u32 *length) +{ +struct device_domain_info *info =dev_iommu_priv_get (dev); +struct intel_iommu *iommu = info->iommu; +struct iommu_hw_info_vtd *vtd; + + vtd =kzalloc (sizeof (*vtd),GFP_KERNEL ); +if (!vtd) +return ERR_PTR (-ENOMEM ); + + vtd->cap_reg = iommu->cap; + vtd->ecap_reg = iommu->ecap; + *length =sizeof (*vtd); + +return vtd; +} +const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, + .hw_info = intel_iommu_hw_info, .domain_alloc = intel_iommu_domain_alloc, .domain_alloc_user = intel_iommu_domain_alloc_user, .probe_device = intel_iommu_probe_device,
@@ -4763,6 +4781,7 @@ const struct iommu_ops intel_iommu_ops = { .def_domain_type = device_def_domain_type, .remove_dev_pasid = intel_iommu_remove_dev_pasid, .pgsize_bitmap = SZ_4K, + .hw_info_type = IOMMU_HW_INFO_TYPE_INTEL_VTD,#ifdef CONFIG_INTEL_IOMMU_SVM .page_response = intel_svm_page_response,#endif
include/uapi/linux/iommufd.h +23 -0
@@ -435,12 +435,35 @@ struct iommu_hwpt_alloc { };#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) +/** +* struct iommu_hw_info_vtd - Intel VT-d hardware information +* +* @flags: Must be 0 +* @__reserved: Must be 0 +* +* @cap_reg: Value of Intel VT-d capability register defined in VT-d spec +* section 11.4.2 Capability Register. +* @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec +* section 11.4.3 Extended Capability Register. +* +* User needs to understand the Intel VT-d specification to decode the +* register value. +*/ +struct iommu_hw_info_vtd { +__u32 flags; +__u32 __reserved; +__aligned_u64 cap_reg; +__aligned_u64 ecap_reg; +}; +/** * enum iommu_hw_info_type - IOMMU Hardware Info Types * @IOMMU_HW_INFO_TYPE_NONE: Used by the drivers that does not report hardware info +* @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type */ enum iommu_hw_info_type {IOMMU_HW_INFO_TYPE_NONE , +IOMMU_HW_INFO_TYPE_INTEL_VTD , };/**
Signature · 1 line
2.34.1
Jason Gunthorpe
maintainerRe: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu <[email protected]>Signed-off-by Nicolin Chen <[email protected]>Signed-off-by Yi Liu <[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)
I would like to pick this patch out of this series to go with the main get_info stuff so that we have drivers implementing what is merged. I made the trivial fixup.
Lu are you OK?
Thanks,
Jason
Re: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
Quoted 10 lines · Jason Gunthorpe
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu<[email protected]>Signed-off-by Nicolin Chen<[email protected]>Signed-off-by Yi Liu<[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)
Quoted 5 lines
I would like to pick this patch out of this series to go with the main
get_infostuff so that we have drivers implementing what is merged. I made the trivial fixup.Lu are you OK?
Yes.
Best regards,
baolu
Re: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
Quoted 15 lines · Baolu Lu
Quoted 14 lines · Jason Gunthorpe
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu<[email protected]>Signed-off-by Nicolin Chen<[email protected]>Signed-off-by Yi Liu<[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)I would like to pick this patch out of this series to go with the main
get_infostuff so that we have drivers implementing what is merged. I made the trivial fixup.
Quoted 5 lines
Quoted 1 line
Lu are you OK?
Yes.
Reviewed-by Lu Baolu <[email protected]>
Hmm...
We have Yi in the author line and Baolu in the first signed-off line, and now Baolu with "Reviewed-by" again...
I guess we might need a bit of fix or re-arrange? :)
Thanks
Nic
Jason Gunthorpe
maintainerRe: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
In reply to Baolu LuQuoted 15 lines · Baolu Lu
Quoted 14 lines · Jason Gunthorpe
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu<[email protected]>Signed-off-by Nicolin Chen<[email protected]>Signed-off-by Yi Liu<[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)I would like to pick this patch out of this series to go with the main
get_infostuff so that we have drivers implementing what is merged. I made the trivial fixup.
Quoted 5 lines
Quoted 1 line
Lu are you OK?
Yes.
Reviewed-by Lu Baolu <[email protected]>
I changed this to an acked-by since you helpd write the patch :)
Jason
Baolu Lu
maintainerRe: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
Quoted 16 lines · Jason Gunthorpe
Quoted 15 lines · Baolu Lu
Quoted 14 lines · Jason Gunthorpe
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu<[email protected]>Signed-off-by Nicolin Chen<[email protected]>Signed-off-by Yi Liu<[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)I would like to pick this patch out of this series to go with the main
get_infostuff so that we have drivers implementing what is merged. I made the trivial fixup.
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Lu are you OK?
Yes.
Reviewed-by Lu Baolu<[email protected]>I changed this to an acked-by since you helpd write the patch 😄
Okay, fine to me.
Best regards,
baolu
Liu, Yi L
Acked-by Lu BaoluRE: [PATCH v4 11/12] iommu/vt-d: Implement hw_info for iommu capability query
Quoted 20 lines
From: Baolu Lu <[email protected]>
Sent: Wednesday, August 16, 2023 7:49 PMQuoted 16 lines · Jason Gunthorpe
Quoted 15 lines · Baolu Lu
Quoted 14 lines · Jason Gunthorpe
Quoted 9 lines · Yi Liu
Add
intel_iommu_hw_info()to reportcap_regandecap_reginformation.Signed-off-by Lu Baolu<[email protected]>Signed-off-by Nicolin Chen<[email protected]>Signed-off-by Yi Liu<[email protected]>--- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+)I would like to pick this patch out of this series to go with the main
get_infostuff so that we have drivers implementing what is merged. I made the trivial fixup.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
Lu are you OK?
Yes.
Reviewed-by Lu Baolu<[email protected]>I changed this to an acked-by since you helpd write the patch 😄
Okay, fine to me.
I'm going to send hw_info v8 which includes this patch, so will add below tag, thanks.
Regards,
Yi Liu
PATCH v4 12/12 iommu/vt-d: Disallow nesting on domains with read-only mappings
In reply to Yi LiuFrom: Lu Baolu <[email protected]>
When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only.
As the result, contents of pages designated by VMM as Read-Only can be modified by IOMMU via PML5E (PML4E for 4-level tables) access as part of address translation process due to DMAs issued by Guest.
Disallow the nested translation when there are read-only pages in the corresponding second-stage mappings. And, no read-only pages are allowed to be configured in the second-stage table of a nested translation.
For simplicity the 2nd restriction is not relaxed even when the nesting is turned off later due to vIOMMU config change. In concept if the user understands this errata and does expect to enable nested translation it should never install any RO mapping in stage-2 in the entire VM life cycle. Accordingly introduce a single sticky bit to mark the parent role on a domain instead of tracking the role with a counter.
Reference from Sapphire Rapids Specification Update [1], errata details, SPR17.
[1] https://www.intel.com/content/www/us/en/content-details/772415/content-details.html
--- drivers/iommu/intel/iommu.c | 14 ++++++++++++++ drivers/iommu/intel/iommu.h | 4 ++++ drivers/iommu/intel/nested.c | 14 +++++++++++++- include/uapi/linux/iommufd.h | 12 +++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +14 -0
@@ -2138,6 +2138,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,struct dma_pte *first_pte =NULL , *pte =NULL ;unsigned int largepage_lvl =0 ;unsigned long lvl_pages =0 ; +unsigned long flags;phys_addr_t pteval;u64 attr;
@@ -2147,6 +2148,18 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,if ((prot & (DMA_PTE_READ |DMA_PTE_WRITE )) ==0 )return -EINVAL ; +if (!(prot &DMA_PTE_WRITE ) && !domain->read_only_mapped) { +spin_lock_irqsave (&domain->lock, flags); +if (domain->set_nested) { +pr_err_ratelimited ("No read-only mapping permitted\n" ); +spin_unlock_irqrestore (&domain->lock, flags); +return -EINVAL ; + } + + domain->read_only_mapped = true; +spin_unlock_irqrestore (&domain->lock, flags); + } + attr = prot & (DMA_PTE_READ |DMA_PTE_WRITE |DMA_PTE_SNP ); attr |=DMA_FL_PTE_PRESENT ;if (domain->use_first_level) {
@@ -4758,6 +4771,7 @@ static void *intel_iommu_hw_info(struct device *dev, u32 *length)if (!vtd)return ERR_PTR (-ENOMEM ); + vtd->flags =IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 ; vtd->cap_reg = iommu->cap; vtd->ecap_reg = iommu->ecap; *length =sizeof (*vtd);
drivers/iommu/intel/iommu.h +4 -0
@@ -592,6 +592,10 @@ struct dmar_domain {* otherwise, goes through the second * level. */ + u8 read_only_mapped:1; /* domain has mappings with read-only +* permission. +*/ + u8 set_nested:1; /* has other domains nested on it */spinlock_t lock;/* Protect device tracking lists */ struct list_head devices;/* all devices' list */
drivers/iommu/intel/nested.c +13 -1
@@ -142,14 +142,26 @@ struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, const union iommu_domain_user_data *user_data) {const struct iommu_hwpt_vtd_s1 *vtd = (struct iommu_hwpt_vtd_s1 *)user_data; +struct dmar_domain *s2_dmar_domain =to_dmar_domain (s2_domain);struct dmar_domain *domain; +unsigned long flags; domain =kzalloc (sizeof (*domain),GFP_KERNEL_ACCOUNT );if (!domain)return NULL ; +spin_lock_irqsave (&s2_dmar_domain->lock, flags); +if (s2_dmar_domain->read_only_mapped) { +spin_unlock_irqrestore (&s2_dmar_domain->lock, flags); +pr_err_ratelimited ("S2 domain has read-only mappings\n" ); +kfree (domain); +return NULL ; + } + s2_dmar_domain->set_nested = true; +spin_unlock_irqrestore (&s2_dmar_domain->lock, flags); + domain->use_first_level = true; - domain->s2_domain =to_dmar_domain (s2_domain); + domain->s2_domain = s2_dmar_domain; domain->s1_pgtbl = vtd->pgtbl_addr; domain->s1_cfg = *vtd; domain->domain.ops = &intel_nested_domain_ops;
include/uapi/linux/iommufd.h +11 -1
@@ -435,10 +435,20 @@ struct iommu_hwpt_alloc { };#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) +/** +* enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info +* @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow nesting on domains +* with read-only mapping. +* https://www.intel.com/content/www/us/en/content-details/772415/content-details.html +*/ +enum iommu_hw_info_vtd_flags { +IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 =1 <<0 , +}; +/** * struct iommu_hw_info_vtd - Intel VT-d hardware information * -* @flags: Must be 0 +* @flags: Combination of enum iommu_hw_info_vtd_flags * @__reserved: Must be 0 * * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec
Signature · 1 line
2.34.1
Tian, Kevin
maintainerRE: [PATCH v4 12/12] iommu/vt-d: Disallow nesting on domains with read-only mappings
drivers/iommu/intel/iommu.c __domain_mapping(struct dmar_domain · line 2156
Quoted 13 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/iommu.c +4
-0@@ -2147,6 +2148,18 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) return -EINVAL; +if (!(prot &DMA_PTE_WRITE ) && !domain->read_only_mapped) { +spin_lock_irqsave (&domain->lock, flags); +if (domain->set_nested) { +pr_err_ratelimited ("No read-only mapping permitted\n");
"Read-only mapping is disallowed on the domain which serves as the parent in a nested configuration, due to HW errata (ERRATA_772415_SPR17)"
drivers/iommu/intel/iommu.h struct dmar_domain { · line 598
Quoted 5 lines
(quoted diff) +1
-0+ u8 read_only_mapped:1; /* domain has mappings withread-only
drivers/iommu/intel/iommu.h +3
-0+* permission. +*/ + u8 set_nested:1; /* has other domains nested on it */
what about "is_parent"?
drivers/iommu/intel/nested.c struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, · line 156
Quoted 5 lines
drivers/iommu/intel/nested.c +4
-0+spin_lock_irqsave (&s2_dmar_domain->lock, flags); +if (s2_dmar_domain->read_only_mapped) { +spin_unlock_irqrestore (&s2_dmar_domain->lock, flags); +pr_err_ratelimited ("S2 domain has read-only mappings\n" );
"Nested configuration is disallowed when the stage-2 domain already has read-only mappings, due to HW errata (ERRATA_772415_SPR17)"
Baolu Lu
maintainerRe: [PATCH v4 12/12] iommu/vt-d: Disallow nesting on domains with read-only mappings
Quoted 8 lines · Kevin
Quoted 8 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PMdrivers/iommu/intel/iommu.c +0
-0@@ -2147,6 +2148,18 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) return -EINVAL;
Quoted 8 lines
drivers/iommu/intel/iommu.c __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
Quoted 5 lines
drivers/iommu/intel/iommu.c +4
-0+if (!(prot &DMA_PTE_WRITE ) && !domain->read_only_mapped) { +spin_lock_irqsave (&domain->lock, flags); +if (domain->set_nested) { +pr_err_ratelimited ("No read-only mapping permitted\n");"Read-only mapping is disallowed on the domain which serves as the parent in a nested configuration, due to HW errata (
ERRATA_772415_SPR17)"
Ack.
Quoted 8 lines
drivers/iommu/intel/iommu.h struct dmar_domain { · line 598
Quoted 6 lines
(quoted diff) +1
-0+ u8 read_only_mapped:1; /* domain has mappings withread-only
drivers/iommu/intel/iommu.h +3
-0+* permission. +*/ + u8 set_nested:1; /* has other domains nested on it */what about "
is_parent"?
"is_parent" is still a bit generic. How about "is_nested_parent"?
Quoted 9 lines
drivers/iommu/intel/nested.c struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, · line 156
Quoted 6 lines
drivers/iommu/intel/nested.c +4
-0+spin_lock_irqsave (&s2_dmar_domain->lock, flags); +if (s2_dmar_domain->read_only_mapped) { +spin_unlock_irqrestore (&s2_dmar_domain->lock, flags); +pr_err_ratelimited ("S2 domain has read-only mappings\n" );"Nested configuration is disallowed when the stage-2 domain already has read-only mappings, due to HW errata (
ERRATA_772415_SPR17)"
Ack.
Best regards,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 12/12] iommu/vt-d: Disallow nesting on domains with read-only mappings
Quoted 15 lines
From: Baolu Lu <[email protected]>
Sent: Thursday, August 3, 2023 11:27 AMQuoted 10 lines · Kevin
drivers/iommu/intel/iommu.h struct dmar_domain { · line 598
Quoted 8 lines
From: Liu, Yi L <[email protected]>
Sent: Monday, July 24, 2023 7:14 PM(quoted diff) +1
-0+ u8 read_only_mapped:1; /* domain has mappings withread-only
drivers/iommu/intel/iommu.h +3
-0+* permission. +*/ + u8 set_nested:1; /* has other domains nested on it */what about "
is_parent"?
Quoted 2 lines
"
is_parent" is still a bit generic. How about "is_nested_parent"?
looks good.