Intel IOMMU updates for v6.18
Subsystems (2)
Lu Baolu
maintainerHi Joerg,
The following changes have been queued for v6.18-rc1. They are about new features and code refactoring, including:
- IOMMU driver updated to the latest VT-d specification
- Don't enable PRS if PDS isn't supported
- Replace snprintf with scnprintf
- Fix legacy mode page table dump through debugfs
- Miscellaneous cleanups
These patches are based on v6.17-rc6. Please consider them for the iommu/vt-d branch.
Best regards,
baolu
Lu Baolu (4):
iommu/vt-d: Remove LPIG from page group response descriptor iommu/vt-d: PRS isn't usable if PDS isn't supported iommu/vt-d: Removal of Advanced Fault Logging iommu/vt-d: debugfs: Avoid dumping context command register
Seyediman Seyedarab (1):
iommu/vt-d: Replace snprintf with scnprintf in dmar_latency_snapshot()
Vineeth Pillai (Google) (1):
iommu/vt-d: debugfs: Fix legacy mode page table dump logic
Yury Norov (NVIDIA) (1):
iommu/vt-d: Drop unused cap_super_offset()
drivers/iommu/intel/debugfs.c | 29 +++++++++++++++++------------ drivers/iommu/intel/iommu.c | 2 +- drivers/iommu/intel/iommu.h | 4 ---- drivers/iommu/intel/perf.c | 10 ++++------ drivers/iommu/intel/perf.h | 5 ++--- drivers/iommu/intel/prq.c | 7 ++----- 6 files changed, 26 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 1/7 iommu/vt-d: Replace snprintf with scnprintf in dmar_latency_snapshot()
From: Seyediman Seyedarab <[email protected]>
snprintf() returns the number of bytes that would have been written, not the number actually written. Using this for offset tracking can cause buffer overruns if truncation occurs.
Replace snprintf() with scnprintf() to ensure the offset stays within bounds.
Since scnprintf() never returns a negative value, and zero is not possible in this context because 'bytes' starts at 0 and 'size - bytes' is DEBUG_BUFFER_SIZE in the first call, which is large enough to hold the string literals used, the return value is always positive. An integer overflow is also completely out of reach here due to the small and fixed buffer size. The error check in latency_show_one() is therefore unnecessary. Remove it and make dmar_latency_snapshot() return void.
--- drivers/iommu/intel/debugfs.c | 10 ++-------- drivers/iommu/intel/perf.c | 10 ++++------ drivers/iommu/intel/perf.h | 5 ++--- 3 files changed, 8 insertions(+), 17 deletions(-)
drivers/iommu/intel/debugfs.c +2 -8
@@ -648,17 +648,11 @@ DEFINE_SHOW_ATTRIBUTE(ir_translation_struct);static void latency_show_one (struct seq_file *m,struct intel_iommu *iommu,struct dmar_drhd_unit *drhd) { -int ret; -seq_printf (m,"IOMMU: %s Register Base Address: %llx\n" , iommu->name, drhd->reg_base_addr); - ret =dmar_latency_snapshot (iommu, debug_buf,DEBUG_BUFFER_SIZE ); -if (ret <0 ) -seq_puts (m,"Failed to get latency snapshot" ); -else -seq_puts (m, debug_buf); -seq_puts (m,"\n" ); +dmar_latency_snapshot (iommu, debug_buf,DEBUG_BUFFER_SIZE ); +seq_printf (m,"%s\n" , debug_buf); }static int latency_show (struct seq_file *m,void *v)
drivers/iommu/intel/perf.c +4 -6
@@ -113,7 +113,7 @@ static char *latency_type_names[] = { " svm_prq" }; -int dmar_latency_snapshot (struct intel_iommu *iommu,char *str,size_t size) +void dmar_latency_snapshot (struct intel_iommu *iommu,char *str,size_t size) {struct latency_statistic *lstat = iommu->perf_statistic;unsigned long flags;
@@ -122,7 +122,7 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)memset (str,0 , size);for (i =0 ; i <COUNTS_NUM ; i++) - bytes +=snprintf (str + bytes, size - bytes, + bytes +=scnprintf (str + bytes, size - bytes,"%s" , latency_counter_names[i]);spin_lock_irqsave (&latency_lock, flags);
@@ -130,7 +130,7 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)if (!dmar_latency_enabled (iommu, i))continue ; - bytes +=snprintf (str + bytes, size - bytes, + bytes +=scnprintf (str + bytes, size - bytes,"\n%s" , latency_type_names[i]);for (j =0 ; j <COUNTS_NUM ; j++) {
@@ -156,11 +156,9 @@ int dmar_latency_snapshot(struct intel_iommu *iommu, char *str, size_t size)break ; } - bytes +=snprintf (str + bytes, size - bytes, + bytes +=scnprintf (str + bytes, size - bytes,"%12lld" , val); } }spin_unlock_irqrestore (&latency_lock, flags); - -return bytes; }
drivers/iommu/intel/perf.h +2 -3
@@ -40,7 +40,7 @@ void dmar_latency_disable(struct intel_iommu *iommu, enum latency_type type);bool dmar_latency_enabled (struct intel_iommu *iommu,enum latency_type type);void dmar_latency_update (struct intel_iommu *iommu,enum latency_type type,u64 latency); -int dmar_latency_snapshot (struct intel_iommu *iommu,char *str,size_t size); +void dmar_latency_snapshot (struct intel_iommu *iommu,char *str,size_t size);#else static inline int dmar_latency_enable (struct intel_iommu *iommu,enum latency_type type)
@@ -64,9 +64,8 @@ dmar_latency_update(struct intel_iommu *iommu, enum latency_type type, u64 laten { } -static inline int +static inline void dmar_latency_snapshot (struct intel_iommu *iommu,char *str,size_t size) { -return 0 ; }#endif /* CONFIG_DMAR_PERF */
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 2/7 iommu/vt-d: debugfs: Fix legacy mode page table dump logic
In reply to Lu BaoluFrom: "Vineeth Pillai (Google)" <[email protected]>
In legacy mode, SSPTPTR is ignored if TT is not 00b or 01b. SSPTPTR maybe uninitialized or zero in that case and may cause oops like:
Oops: general protection fault, probably for non-canonical address
0xf00087d3f000f000: 0000 [#1] SMP NOPTI
CPU: 2 UID: 0 PID: 786 Comm: cat Not tainted 6.16.0 #191 PREEMPT(voluntary)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.17.0-5.fc42 04/01/2014
RIP: 0010:pgtable_walk_level+0x98/0x150
RSP: 0018:ffffc90000f279c0 EFLAGS: 00010206
RAX: 0000000040000000 RBX: ffffc90000f27ab0 RCX: 000000000000001e
RDX: 0000000000000003 RSI: f00087d3f000f000 RDI: f00087d3f0010000
RBP: ffffc90000f27a00 R08: ffffc90000f27a98 R09: 0000000000000002
R10: 0000000000000000 R11: 0000000000000000 R12: f00087d3f000f000
R13: 0000000000000000 R14: 0000000040000000 R15: ffffc90000f27a98
FS: 0000764566dcb740(0000) GS:ffff8881f812c000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000764566d44000 CR3: 0000000109d81003 CR4: 0000000000772ef0
PKRU: 55555554
Call Trace:
<TASK>
pgtable_walk_level+0x88/0x150
domain_translation_struct_show.isra.0+0x2d9/0x300
dev_domain_translation_struct_show+0x20/0x40
seq_read_iter+0x12d/0x490...
Avoid walking the page table if TT is not 00b or 01b.
--- drivers/iommu/intel/debugfs.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iommu/intel/debugfs.c +15 -2
@@ -435,8 +435,21 @@ static int domain_translation_struct_show(struct seq_file *m, } pgd &=VTD_PAGE_MASK ; }else {/* legacy mode */ - pgd = context->lo &VTD_PAGE_MASK ; - agaw = context->hi &7 ; +u8 tt = (u8)(context->lo &GENMASK_ULL (3 ,2 )) >>2 ; + +/* +* According to Translation Type(TT), +* get the page table pointer(SSPTPTR). +*/ +switch (tt) { +case CONTEXT_TT_MULTI_LEVEL : +case CONTEXT_TT_DEV_IOTLB : + pgd = context->lo &VTD_PAGE_MASK ; + agaw = context->hi &7 ; +break ; +default : + goto iommu_unlock; + } } seq_printf(m, "Device %04x:%02x:%02x.%x ",
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 3/7 iommu/vt-d: Drop unused cap_super_offset()
In reply to Lu BaoluFrom: "Yury Norov (NVIDIA)" <[email protected]>
The macro is unused. Drop the dead code.
--- drivers/iommu/intel/iommu.h | 2 -- 1 file changed, 2 deletions(-)
drivers/iommu/intel/iommu.h +0 -2
@@ -173,8 +173,6 @@#define cap_pgsel_inv (c) (((c) >> 39) & 1)#define cap_super_page_val (c) (((c) >> 34) & 0xf) -#define cap_super_offset (c) (((find_first_bit(&cap_super_page_val(c), 4)) \ - * OFFSET_STRIDE) + 21)#define cap_fault_reg_offset (c) ((((c) >> 24) & 0x3ff) * 16)#define cap_max_fault_reg_offset (c) \
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 4/7 iommu/vt-d: Remove LPIG from page group response descriptor
In reply to Lu BaoluBit 66 in the page group response descriptor used to be the LPIG (Last Page in Group), but it was marked as Reserved since Specification 4.0. Remove programming on this bit to make it consistent with the latest specification.
Existing hardware all treats bit 66 of the page group response descriptor as "ignored", therefore this change doesn't break any existing hardware.
--- drivers/iommu/intel/iommu.h | 1 - drivers/iommu/intel/prq.c | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-)
drivers/iommu/intel/iommu.h +0 -1
@@ -460,7 +460,6 @@ enum {#define QI_PGRP_PASID (pasid) (((u64)(pasid)) << 32)/* Page group response descriptor QW1 */ -#define QI_PGRP_LPIG (x) (((u64)(x)) << 2)#define QI_PGRP_IDX (idx) (((u64)(idx)) << 3)
drivers/iommu/intel/prq.c +2 -5
@@ -151,8 +151,7 @@ static void handle_bad_prq_event(struct intel_iommu *iommu,QI_PGRP_PASID_P (req->pasid_present) |QI_PGRP_RESP_CODE (result) |QI_PGRP_RESP_TYPE ; - desc.qw1 =QI_PGRP_IDX (req->prg_index) | -QI_PGRP_LPIG (req->lpig); + desc.qw1 =QI_PGRP_IDX (req->prg_index);qi_submit_sync (iommu, &desc,1 ,0 ); }
@@ -379,19 +378,17 @@ void intel_iommu_page_response(struct device *dev, struct iopf_fault *evt,struct iommu_fault_page_request *prm;struct qi_desc desc;bool pasid_present; -bool last_page;u16 sid; prm = &evt->fault.prm; sid =PCI_DEVID (bus, devfn); pasid_present = prm->flags &IOMMU_FAULT_PAGE_REQUEST_PASID_VALID ; - last_page = prm->flags &IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE ; desc.qw0 =QI_PGRP_PASID (prm->pasid) |QI_PGRP_DID (sid) |QI_PGRP_PASID_P (pasid_present) |QI_PGRP_RESP_CODE (msg->code) |QI_PGRP_RESP_TYPE ; - desc.qw1 =QI_PGRP_IDX (prm->grpid) |QI_PGRP_LPIG (last_page); + desc.qw1 =QI_PGRP_IDX (prm->grpid); desc.qw2 =0 ; desc.qw3 =0 ;
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 5/7 iommu/vt-d: PRS isn't usable if PDS isn't supported
In reply to Lu BaoluThe specification, Section 7.10, "Software Steps to Drain Page Requests & Responses," requires software to submit an Invalidation Wait Descriptor (inv_wait_dsc) with the Page-request Drain (PD=1) flag set, along with the Invalidation Wait Completion Status Write flag (SW=1). It then waits for the Invalidation Wait Descriptor's completion.
However, the PD field in the Invalidation Wait Descriptor is optional, as stated in Section 6.5.2.9, "Invalidation Wait Descriptor":
"Page-request Drain (PD): Remapping hardware implementations reporting
Page-request draining as not supported (PDS = 0 in ECAP_REG) treat this field as reserved."
This implies that if the IOMMU doesn't support the PDS capability, software can't drain page requests and group responses as expected.
Do not enable PCI/PRI if the IOMMU doesn't support PDS.
--- drivers/iommu/intel/iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
@@ -3812,7 +3812,7 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) }if (info->ats_supported &&ecap_prs (iommu->ecap) && -pci_pri_supported (pdev)) +ecap_pds (iommu->ecap) &&pci_pri_supported (pdev)) info->pri_supported =1 ; } }
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 6/7 iommu/vt-d: Removal of Advanced Fault Logging
In reply to Lu BaoluThe advanced fault logging has been removed from the specification since v4.0. Linux doesn't implement advanced fault logging functionality, but it currently dumps the advanced logging registers through debugfs. Remove the dumping of these advanced fault logging registers through debugfs to avoid potential access to non-present registers.
--- drivers/iommu/intel/debugfs.c | 1 - drivers/iommu/intel/iommu.h | 1 - 2 files changed, 2 deletions(-)
drivers/iommu/intel/debugfs.c +0 -1
@@ -63,7 +63,6 @@ static const struct iommu_regset iommu_regs_64[] = {IOMMU_REGSET_ENTRY (ECAP ),IOMMU_REGSET_ENTRY (RTADDR ),IOMMU_REGSET_ENTRY (CCMD ), -IOMMU_REGSET_ENTRY (AFLOG ),IOMMU_REGSET_ENTRY (PHMBASE ),IOMMU_REGSET_ENTRY (PHMLIMIT ), IOMMU_REGSET_ENTRY(IQH),
drivers/iommu/intel/iommu.h +0 -1
@@ -77,7 +77,6 @@#define DMAR_FEDATA_REG 0x3c/* Fault event interrupt data register */ #define DMAR_FEADDR_REG 0x40/* Fault event interrupt addr register */ #define DMAR_FEUADDR_REG 0x44/* Upper address register */ -#define DMAR_AFLOG_REG 0x58/* Advanced Fault control */ #define DMAR_PMEN_REG 0x64/* Enable Protected Memory Region */ #define DMAR_PLMBASE_REG 0x68/* PMRR Low addr */ #define DMAR_PLMLIMIT_REG 0x6c/* PMRR low limit */
Signature · 1 line
2.43.0
Lu Baolu
maintainerPATCH 7/7 iommu/vt-d: debugfs: Avoid dumping context command register
In reply to Lu BaoluThe register-based cache invalidation interface is in the process of being replaced by the queued invalidation interface. The VT-d architecture allows hardware implementations with a queued invalidation interface to not implement the registers used for cache invalidation. Currently, the debugfs interface dumps the Context Command Register unconditionally, which is not reasonable.
Remove it to avoid potential access to non-present registers.
--- drivers/iommu/intel/debugfs.c | 1 - 1 file changed, 1 deletion(-)
drivers/iommu/intel/debugfs.c +0 -1
@@ -62,7 +62,6 @@ static const struct iommu_regset iommu_regs_64[] = {IOMMU_REGSET_ENTRY (CAP ),IOMMU_REGSET_ENTRY (ECAP ),IOMMU_REGSET_ENTRY (RTADDR ), -IOMMU_REGSET_ENTRY (CCMD ),IOMMU_REGSET_ENTRY (PHMBASE ),IOMMU_REGSET_ENTRY (PHMLIMIT ), IOMMU_REGSET_ENTRY(IQH),
Signature · 1 line
2.43.0
Quoted 15 lines · Lu Baolu
Lu Baolu (4):
iommu/vt-d: Remove LPIG from page group response descriptor iommu/vt-d: PRS isn't usable if PDS isn't supported iommu/vt-d: Removal of Advanced Fault Logging iommu/vt-d: debugfs: Avoid dumping context command registerSeyediman Seyedarab (1):
iommu/vt-d: Replace snprintf with scnprintf indmar_latency_snapshot()Vineeth Pillai (Google) (1):
iommu/vt-d: debugfs: Fix legacy mode page table dump logicYury Norov (NVIDIA) (1):
iommu/vt-d: Drop unusedcap_super_offset()
Quoted 7 lines
drivers/iommu/intel/debugfs.c | 29 +++++++++++++++++------------ drivers/iommu/intel/iommu.c | 2 +- drivers/iommu/intel/iommu.h | 4 ---- drivers/iommu/intel/perf.c | 10 ++++------ drivers/iommu/intel/perf.h | 5 ++--- drivers/iommu/intel/prq.c | 7 ++----- 6 files changed, 26 insertions(+), 31 deletions(-)
Applied, thanks Baolu.