* [PATCH 1/7] iommu/vt-d: Support enforce_cache_coherency only for empty domains
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 2/7] iommu/vt-d: Omit devTLB invalidation requests when TES=0 Lu Baolu
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
The enforce_cache_coherency callback ensures DMA cache coherency for
devices attached to the domain.
Intel IOMMU supports enforced DMA cache coherency when the Snoop
Control bit in the IOMMU's extended capability register is set.
Supporting it differs between legacy and scalable modes.
In legacy mode, it's supported page-level by setting the SNP field
in second-stage page-table entries. In scalable mode, it's supported
in PASID-table granularity by setting the PGSNP field in PASID-table
entries.
In legacy mode, mappings before attaching to a device have SNP
fields cleared, while mappings after the callback have them set.
This means partial DMAs are cache coherent while others are not.
One possible fix is replaying mappings and flipping SNP bits when
attaching a domain to a device. But this seems to be over-engineered,
given that all real use cases just attach an empty domain to a device.
To meet practical needs while reducing mode differences, only support
enforce_cache_coherency on a domain without mappings if SNP field is
used.
Fixes: fc0051cb9590 ("iommu/vt-d: Check domain force_snooping against attached devices")
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20231114011036.70142-1-baolu.lu@linux.intel.com
---
drivers/iommu/intel/iommu.h | 3 +++
drivers/iommu/intel/iommu.c | 5 ++++-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
index 65d37a138c75..ce030c5b5772 100644
--- a/drivers/iommu/intel/iommu.h
+++ b/drivers/iommu/intel/iommu.h
@@ -602,6 +602,9 @@ struct dmar_domain {
*/
u8 dirty_tracking:1; /* Dirty tracking is enabled */
u8 nested_parent:1; /* Has other domains nested on it */
+ u8 has_mappings:1; /* Has mappings configured through
+ * iommu_map() interface.
+ */
spinlock_t lock; /* Protect device tracking lists */
struct list_head devices; /* all devices' list */
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 3531b956556c..11670cd812a3 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2207,6 +2207,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
attr |= DMA_FL_PTE_DIRTY;
}
+ domain->has_mappings = true;
+
pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | attr;
while (nr_pages > 0) {
@@ -4360,7 +4362,8 @@ static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)
return true;
spin_lock_irqsave(&dmar_domain->lock, flags);
- if (!domain_support_force_snooping(dmar_domain)) {
+ if (!domain_support_force_snooping(dmar_domain) ||
+ (!dmar_domain->use_first_level && dmar_domain->has_mappings)) {
spin_unlock_irqrestore(&dmar_domain->lock, flags);
return false;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 2/7] iommu/vt-d: Omit devTLB invalidation requests when TES=0
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
2023-11-22 3:26 ` [PATCH 1/7] iommu/vt-d: Support enforce_cache_coherency only for empty domains Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 3/7] iommu/vt-d: Disable PCI ATS in legacy passthrough mode Lu Baolu
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
The latest VT-d spec indicates that when remapping hardware is disabled
(TES=0 in Global Status Register), upstream ATS Invalidation Completion
requests are treated as UR (Unsupported Request).
Consequently, the spec recommends in section 4.3 Handling of Device-TLB
Invalidations that software refrain from submitting any Device-TLB
invalidation requests when address remapping hardware is disabled.
Verify address remapping hardware is enabled prior to submitting Device-
TLB invalidation requests.
Fixes: 792fb43ce2c9 ("iommu/vt-d: Enable Intel IOMMU scalable mode by default")
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20231114011036.70142-2-baolu.lu@linux.intel.com
---
drivers/iommu/intel/dmar.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
index a3414afe11b0..23cb80d62a9a 100644
--- a/drivers/iommu/intel/dmar.c
+++ b/drivers/iommu/intel/dmar.c
@@ -1522,6 +1522,15 @@ void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,
{
struct qi_desc desc;
+ /*
+ * VT-d spec, section 4.3:
+ *
+ * Software is recommended to not submit any Device-TLB invalidation
+ * requests while address remapping hardware is disabled.
+ */
+ if (!(iommu->gcmd & DMA_GCMD_TE))
+ return;
+
if (mask) {
addr |= (1ULL << (VTD_PAGE_SHIFT + mask - 1)) - 1;
desc.qw1 = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
@@ -1587,6 +1596,15 @@ void qi_flush_dev_iotlb_pasid(struct intel_iommu *iommu, u16 sid, u16 pfsid,
unsigned long mask = 1UL << (VTD_PAGE_SHIFT + size_order - 1);
struct qi_desc desc = {.qw1 = 0, .qw2 = 0, .qw3 = 0};
+ /*
+ * VT-d spec, section 4.3:
+ *
+ * Software is recommended to not submit any Device-TLB invalidation
+ * requests while address remapping hardware is disabled.
+ */
+ if (!(iommu->gcmd & DMA_GCMD_TE))
+ return;
+
desc.qw0 = QI_DEV_EIOTLB_PASID(pasid) | QI_DEV_EIOTLB_SID(sid) |
QI_DEV_EIOTLB_QDEP(qdep) | QI_DEIOTLB_TYPE |
QI_DEV_IOTLB_PFSID(pfsid);
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 3/7] iommu/vt-d: Disable PCI ATS in legacy passthrough mode
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
2023-11-22 3:26 ` [PATCH 1/7] iommu/vt-d: Support enforce_cache_coherency only for empty domains Lu Baolu
2023-11-22 3:26 ` [PATCH 2/7] iommu/vt-d: Omit devTLB invalidation requests when TES=0 Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 4/7] iommu/vt-d: Make context clearing consistent with context mapping Lu Baolu
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
When IOMMU hardware operates in legacy mode, the TT field of the context
entry determines the translation type, with three supported types (Section
9.3 Context Entry):
- DMA translation without device TLB support
- DMA translation with device TLB support
- Passthrough mode with translated and translation requests blocked
Device TLB support is absent when hardware is configured in passthrough
mode.
Disable the PCI ATS feature when IOMMU is configured for passthrough
translation type in legacy (non-scalable) mode.
Fixes: 0faa19a1515f ("iommu/vt-d: Decouple PASID & PRI enabling from SVA")
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20231114011036.70142-3-baolu.lu@linux.intel.com
---
drivers/iommu/intel/iommu.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 11670cd812a3..9bddd4fbbdf8 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -2492,7 +2492,8 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,
return ret;
}
- iommu_enable_pci_caps(info);
+ if (sm_supported(info->iommu) || !domain_type_is_si(info->domain))
+ iommu_enable_pci_caps(info);
return 0;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/7] iommu/vt-d: Make context clearing consistent with context mapping
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
` (2 preceding siblings ...)
2023-11-22 3:26 ` [PATCH 3/7] iommu/vt-d: Disable PCI ATS in legacy passthrough mode Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 5/7] iommu/vt-d: Add MTL to quirk list to skip TE disabling Lu Baolu
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
In the iommu probe_device path, domain_context_mapping() allows setting
up the context entry for a non-PCI device. However, in the iommu
release_device path, domain_context_clear() only clears context entries
for PCI devices.
Make domain_context_clear() behave consistently with
domain_context_mapping() by clearing context entries for both PCI and
non-PCI devices.
Fixes: 579305f75d34 ("iommu/vt-d: Update to use PCI DMA aliases")
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20231114011036.70142-4-baolu.lu@linux.intel.com
---
drivers/iommu/intel/iommu.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 9bddd4fbbdf8..4c257ccf9dc3 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -3928,8 +3928,8 @@ static int domain_context_clear_one_cb(struct pci_dev *pdev, u16 alias, void *op
*/
static void domain_context_clear(struct device_domain_info *info)
{
- if (!info->iommu || !info->dev || !dev_is_pci(info->dev))
- return;
+ if (!dev_is_pci(info->dev))
+ domain_context_clear_one(info, info->bus, info->devfn);
pci_for_each_dma_alias(to_pci_dev(info->dev),
&domain_context_clear_one_cb, info);
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5/7] iommu/vt-d: Add MTL to quirk list to skip TE disabling
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
` (3 preceding siblings ...)
2023-11-22 3:26 ` [PATCH 4/7] iommu/vt-d: Make context clearing consistent with context mapping Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 6/7] iommu/vt-d: Fix incorrect cache invalidation for mm notification Lu Baolu
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
From: "Abdul Halim, Mohd Syazwan" <mohd.syazwan.abdul.halim@intel.com>
The VT-d spec requires (10.4.4 Global Command Register, TE field) that:
Hardware implementations supporting DMA draining must drain any in-flight
DMA read/write requests queued within the Root-Complex before switching
address translation on or off and reflecting the status of the command
through the TES field in the Global Status register.
Unfortunately, some integrated graphic devices fail to do so after some
kind of power state transition. As the result, the system might stuck in
iommu_disable_translation(), waiting for the completion of TE transition.
Add MTL to the quirk list for those devices and skips TE disabling if the
qurik hits.
Fixes: b1012ca8dc4f ("iommu/vt-d: Skip TE disabling on quirky gfx dedicated iommu")
Cc: stable@vger.kernel.org
Signed-off-by: Abdul Halim, Mohd Syazwan <mohd.syazwan.abdul.halim@intel.com>
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Link: https://lore.kernel.org/r/20231116022324.30120-1-baolu.lu@linux.intel.com
---
drivers/iommu/intel/iommu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 4c257ccf9dc3..68f121c28fbf 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -5077,7 +5077,7 @@ static void quirk_igfx_skip_te_disable(struct pci_dev *dev)
ver = (dev->device >> 8) & 0xff;
if (ver != 0x45 && ver != 0x46 && ver != 0x4c &&
ver != 0x4e && ver != 0x8a && ver != 0x98 &&
- ver != 0x9a && ver != 0xa7)
+ ver != 0x9a && ver != 0xa7 && ver != 0x7d)
return;
if (risky_device(dev))
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 6/7] iommu/vt-d: Fix incorrect cache invalidation for mm notification
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
` (4 preceding siblings ...)
2023-11-22 3:26 ` [PATCH 5/7] iommu/vt-d: Add MTL to quirk list to skip TE disabling Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-22 3:26 ` [PATCH 7/7] iommu/vt-d: Set variable intel_dirty_ops to static Lu Baolu
2023-11-27 10:08 ` [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Joerg Roedel
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
Commit 6bbd42e2df8f ("mmu_notifiers: call invalidate_range() when
invalidating TLBs") moved the secondary TLB invalidations into the TLB
invalidation functions to ensure that all secondary TLB invalidations
happen at the same time as the CPU invalidation and added a flush-all
type of secondary TLB invalidation for the batched mode, where a range
of [0, -1UL) is used to indicates that the range extends to the end of
the address space.
However, using an end address of -1UL caused an overflow in the Intel
IOMMU driver, where the end address was rounded up to the next page.
As a result, both the IOTLB and device ATC were not invalidated correctly.
Add a flush all helper function and call it when the invalidation range
is from 0 to -1UL, ensuring that the entire caches are invalidated
correctly.
Fixes: 6bbd42e2df8f ("mmu_notifiers: call invalidate_range() when invalidating TLBs")
Cc: stable@vger.kernel.org
Cc: Huang Ying <ying.huang@intel.com>
Cc: Alistair Popple <apopple@nvidia.com>
Tested-by: Luo Yuzhang <yuzhang.luo@intel.com> # QAT
Tested-by: Tony Zhu <tony.zhu@intel.com> # DSA
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Link: https://lore.kernel.org/r/20231117090933.75267-1-baolu.lu@linux.intel.com
---
drivers/iommu/intel/svm.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c
index 50a481c895b8..ac12f76c1212 100644
--- a/drivers/iommu/intel/svm.c
+++ b/drivers/iommu/intel/svm.c
@@ -216,6 +216,27 @@ static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
rcu_read_unlock();
}
+static void intel_flush_svm_all(struct intel_svm *svm)
+{
+ struct device_domain_info *info;
+ struct intel_svm_dev *sdev;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(sdev, &svm->devs, list) {
+ info = dev_iommu_priv_get(sdev->dev);
+
+ qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, 0, -1UL, 0);
+ if (info->ats_enabled) {
+ qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid,
+ svm->pasid, sdev->qdep,
+ 0, 64 - VTD_PAGE_SHIFT);
+ quirk_extra_dev_tlb_flush(info, 0, 64 - VTD_PAGE_SHIFT,
+ svm->pasid, sdev->qdep);
+ }
+ }
+ rcu_read_unlock();
+}
+
/* Pages have been freed at this point */
static void intel_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,
struct mm_struct *mm,
@@ -223,6 +244,11 @@ static void intel_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,
{
struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
+ if (start == 0 && end == -1UL) {
+ intel_flush_svm_all(svm);
+ return;
+ }
+
intel_flush_svm_range(svm, start,
(end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 7/7] iommu/vt-d: Set variable intel_dirty_ops to static
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
` (5 preceding siblings ...)
2023-11-22 3:26 ` [PATCH 6/7] iommu/vt-d: Fix incorrect cache invalidation for mm notification Lu Baolu
@ 2023-11-22 3:26 ` Lu Baolu
2023-11-27 10:08 ` [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Joerg Roedel
7 siblings, 0 replies; 9+ messages in thread
From: Lu Baolu @ 2023-11-22 3:26 UTC (permalink / raw)
To: Joerg Roedel; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
From: Kunwu Chan <chentao@kylinos.cn>
Fix the following warning:
drivers/iommu/intel/iommu.c:302:30: warning: symbol
'intel_dirty_ops' was not declared. Should it be static?
This variable is only used in its defining file, so it should be static.
Fixes: f35f22cc760e ("iommu/vt-d: Access/Dirty bit support for SS domains")
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Link: https://lore.kernel.org/r/20231120101025.1103404-1-chentao@kylinos.cn
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
drivers/iommu/intel/iommu.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 68f121c28fbf..897159dba47d 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -299,7 +299,7 @@ static int iommu_skip_te_disable;
#define IDENTMAP_AZALIA 4
const struct iommu_ops intel_iommu_ops;
-const struct iommu_dirty_ops intel_dirty_ops;
+static const struct iommu_dirty_ops intel_dirty_ops;
static bool translation_pre_enabled(struct intel_iommu *iommu)
{
@@ -4929,7 +4929,7 @@ static int intel_iommu_read_and_clear_dirty(struct iommu_domain *domain,
return 0;
}
-const struct iommu_dirty_ops intel_dirty_ops = {
+static const struct iommu_dirty_ops intel_dirty_ops = {
.set_dirty_tracking = intel_iommu_set_dirty_tracking,
.read_and_clear_dirty = intel_iommu_read_and_clear_dirty,
};
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3
2023-11-22 3:26 [PATCH 0/7] [PULL REQUEST] iommu/vt-d: Fixes for v6.7-rc3 Lu Baolu
` (6 preceding siblings ...)
2023-11-22 3:26 ` [PATCH 7/7] iommu/vt-d: Set variable intel_dirty_ops to static Lu Baolu
@ 2023-11-27 10:08 ` Joerg Roedel
7 siblings, 0 replies; 9+ messages in thread
From: Joerg Roedel @ 2023-11-27 10:08 UTC (permalink / raw)
To: Lu Baolu; +Cc: mohd.syazwan.abdul.halim, Kunwu Chan, iommu, linux-kernel
On Wed, Nov 22, 2023 at 11:26:01AM +0800, Lu Baolu wrote:
> Abdul Halim, Mohd Syazwan (1):
> iommu/vt-d: Add MTL to quirk list to skip TE disabling
>
> Kunwu Chan (1):
> iommu/vt-d: Set variable intel_dirty_ops to static
>
> Lu Baolu (5):
> iommu/vt-d: Support enforce_cache_coherency only for empty domains
> iommu/vt-d: Omit devTLB invalidation requests when TES=0
> iommu/vt-d: Disable PCI ATS in legacy passthrough mode
> iommu/vt-d: Make context clearing consistent with context mapping
> iommu/vt-d: Fix incorrect cache invalidation for mm notification
Applied for v6.7, thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread