Skip to contentSkip to conversation

Allow ATS to be always on for certain ATS-capable devices

PATCH Superseded Nicolin Chen 4 participants 13 messages May 20 – May 21

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Allow ATS to be always on for certain ATS-capable devices

PCI ATS function is controlled by the IOMMU driver calling pci_enable_ats() and pci_disable_ats() helpers. In general, IOMMU driver only enables ATS when a translation channel is enabled on a PASID, typically for an SVA use case. When a device's RID is IOMMU bypassed and its PASIDs are not running SVA use case, ATS is always disabled.

However, certain PCIe devices require non-PASID ATS on the RID, even if the RID is IOMMU bypassed. E.g. CXL.cache capability requires ATS to access the physical memory; some pre-CXL NVIDIA GPUs also require the ATS to be always on even when their RIDs are IOMMU bypassed.

Provide a helper function to detect CXL.cache capability and scan through a pre-CXL device ID list.

As the initial use case, call the helper in ARM SMMUv3 driver and adapt the driver accordingly with a per-device ats_always_on flag.

This is on Github:
https://github.com/nicolinc/iommufd/commits/pci_ats_always_on-v5

Changelog
v5
* Add Reviewed-by from Dave
* Update comments in pci helpers
* s/pci_ats_always_on/pci_ats_required
* s/pci_cxl_ats_always_on/pci_cxl_ats_required
* s/pci_dev_specific_ats_always_on/pci_dev_specific_ats_required
v4
https://lore.kernel.org/all/[email protected]/
* Rebase on v7.1-rc1
* Added Reviewed/Tested/Acked-by lines
* Update commit messages and inline comments
* [pci-quirks] Add range-based scan for NVIDIA GPUs
* [smmu] Add missing arm_smmu_remove_master() in error path
* [pci-ats] Don't init "cap=0"; check pci_read_config_word error
v3
https://lore.kernel.org/all/[email protected]/
* Add Reviewed-by from Jonathan
* Update function kdocs of PCI APIs
* Simplify boolean return/variable computations
v2
https://lore.kernel.org/all/[email protected]/
* s/non-CXL/pre-CXL
* Rebase on v7.0-rc1
* Update inline comments and commit message
* Add WARN_ON back at !ptr in arm_smmu_clear_cd()
* Add NVIDIA CX10 Family NVlink-C2C to the pre-CXL list
* Do not add boolean parameter to arm_smmu_attach_dev_ste()
v1
https://lore.kernel.org/all/[email protected]/

Nicolin Chen (3):
PCI: Add pci_ats_required() for CXL.cache capable devices
PCI: Allow ATS to be always on for pre-CXL devices iommu/arm-smmu-v3: Allow ATS to be always on

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  1 +
 drivers/pci/pci.h                           |  9 +++
 include/linux/pci-ats.h                     |  3 +
 include/uapi/linux/pci_regs.h               |  1 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 75 ++++++++++++++++++---
 drivers/pci/ats.c                           | 47 +++++++++++++
 drivers/pci/quirks.c                        | 42 ++++++++++++
 7 files changed, 170 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/3 PCI: Add pci_ats_required() for CXL.cache capable devices

Controlled by the IOMMU driver, ATS is usually enabled "on demand" when a given PASID on a device is attached to an I/O page table. This is working even when a device has no translation on its RID (i.e., the RID is IOMMU bypassed).

However, certain PCIe devices require non-PASID ATS on their RID even when the RID is IOMMU bypassed. Call this "ATS always on" in IOMMU term.

For example, CXL spec r4.0 notes in sec 3.2.5.13 Memory Type on CXL.cache:
"To source requests on CXL.cache, devices need to get the Host Physical
Address (HPA) from the Host by means of an ATS request on CXL.io."

In other words, the CXL.cache capability requires ATS; otherwise, it can't access host physical memory.

Introduce a new pci_ats_required() helper for the IOMMU driver to scan a PCI device and shift ATS policies between "on demand" and "always on".

Add the support for CXL.cache devices first. Pre-CXL devices will be added in quirks.c file.

Note that pci_ats_required() validates against pci_ats_supported(), so we ensure that untrusted devices (e.g. external ports) will not be always on. This maintains the existing ATS security policy regarding potential side- channel attacks via ATS.

---
 include/linux/pci-ats.h       |  3 +++
 include/uapi/linux/pci_regs.h |  1 +
 drivers/pci/ats.c             | 46 +++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)
include/linux/pci-ats.h +3 -0
index 75c6c86cf09dc..f3723b6861294 100644
@@ -12,6 +12,7 @@ int pci_prepare_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
 int pci_ats_page_aligned(struct pci_dev *dev);
+bool pci_ats_required(struct pci_dev *dev);
 #else /* CONFIG_PCI_ATS */
 static inline bool pci_ats_supported(struct pci_dev *d)
 { return false; }
@@ -24,6 +25,8 @@ static inline int pci_ats_queue_depth(struct pci_dev *d)
 { return -ENODEV; }
 static inline int pci_ats_page_aligned(struct pci_dev *dev)
 { return 0; }
+static inline bool pci_ats_required(struct pci_dev *dev)
+{ return false; }
 #endif /* CONFIG_PCI_ATS */
 
 #ifdef CONFIG_PCI_PRI
include/uapi/linux/pci_regs.h +1 -0
index 14f634ab9350d..6ac45be1008b8 100644
@@ -1349,6 +1349,7 @@
 /* CXL r4.0, 8.1.3: PCIe DVSEC for CXL Device */
 #define PCI_DVSEC_CXL_DEVICE				0
 #define  PCI_DVSEC_CXL_CAP				0xA
+#define   PCI_DVSEC_CXL_CACHE_CAPABLE			_BITUL(0)
 #define   PCI_DVSEC_CXL_MEM_CAPABLE			_BITUL(2)
 #define   PCI_DVSEC_CXL_HDM_COUNT			__GENMASK(5, 4)
 #define  PCI_DVSEC_CXL_CTRL				0xC
drivers/pci/ats.c +46 -0

52 lines not on this page · raw message

Signature · 1 line
2.43.0
Acked-by on v5 1/3
#2 lore raw

Re: [PATCH v5 1/3] PCI: Add pci_ats_required() for CXL.cache capable devices

Quoted 26 lines · Nicolin Chen

Controlled by the IOMMU driver, ATS is usually enabled "on demand" when a given PASID on a device is attached to an I/O page table. This is working even when a device has no translation on its RID (i.e., the RID is IOMMU bypassed).

However, certain PCIe devices require non-PASID ATS on their RID even when the RID is IOMMU bypassed. Call this "ATS always on" in IOMMU term.

For example, CXL spec r4.0 notes in sec 3.2.5.13 Memory Type on CXL.cache:
"To source requests on CXL.cache, devices need to get the Host Physical
Address (HPA) from the Host by means of an ATS request on CXL.io."

In other words, the CXL.cache capability requires ATS; otherwise, it can't access host physical memory.

Introduce a new pci_ats_required() helper for the IOMMU driver to scan a PCI device and shift ATS policies between "on demand" and "always on".

Add the support for CXL.cache devices first. Pre-CXL devices will be added in quirks.c file.

Note that pci_ats_required() validates against pci_ats_supported(), so we ensure that untrusted devices (e.g. external ports) will not be always on. This maintains the existing ATS security policy regarding potential side- channel attacks via ATS.

Quoted 10 lines

One lingering question below that I asked before but I don't think anybody answered.

Quoted 74 lines
---
 include/linux/pci-ats.h       |  3 +++
 include/uapi/linux/pci_regs.h |  1 +
 drivers/pci/ats.c             | 46 +++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)
include/linux/pci-ats.h +3 -0
index 75c6c86cf09dc..f3723b6861294 100644
@@ -12,6 +12,7 @@ int pci_prepare_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
 int pci_ats_page_aligned(struct pci_dev *dev);
+bool pci_ats_required(struct pci_dev *dev);
 #else /* CONFIG_PCI_ATS */
 static inline bool pci_ats_supported(struct pci_dev *d)
 { return false; }
@@ -24,6 +25,8 @@ static inline int pci_ats_queue_depth(struct pci_dev *d)
 { return -ENODEV; }
 static inline int pci_ats_page_aligned(struct pci_dev *dev)
 { return 0; }
+static inline bool pci_ats_required(struct pci_dev *dev)
+{ return false; }
 #endif /* CONFIG_PCI_ATS */
 
 #ifdef CONFIG_PCI_PRI
include/uapi/linux/pci_regs.h +1 -0
index 14f634ab9350d..6ac45be1008b8 100644
@@ -1349,6 +1349,7 @@
 /* CXL r4.0, 8.1.3: PCIe DVSEC for CXL Device */
 #define PCI_DVSEC_CXL_DEVICE				0
 #define  PCI_DVSEC_CXL_CAP				0xA
+#define   PCI_DVSEC_CXL_CACHE_CAPABLE			_BITUL(0)
 #define   PCI_DVSEC_CXL_MEM_CAPABLE			_BITUL(2)
 #define   PCI_DVSEC_CXL_HDM_COUNT			__GENMASK(5, 4)
 #define  PCI_DVSEC_CXL_CTRL				0xC
drivers/pci/ats.c +27 -0
index ec6c8dbdc5e9c..ebdf761843867 100644
@@ -205,6 +205,52 @@ int pci_ats_page_aligned(struct pci_dev *pdev)
 	return 0;
 }
 
+/*
+ * CXL r4.0, sec 3.2.5.13 Memory Type on CXL.cache notes: to source requests on
+ * CXL.cache, devices need to get the Host Physical Address (HPA) from the Host
+ * by means of an ATS request on CXL.io.
+ *
+ * In other words, CXL.cache devices cannot access host physical memory without
+ * ATS.
+ *
+ * Check Cache_Capable instead of Cache_Enable because CXL.cache may be enabled
+ * after the caller uses this to make its ATS decision.
+ */
+static bool pci_cxl_ats_required(struct pci_dev *pdev)
+{
+	int offset;
+	u16 cap;
+
+	offset = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
+					   PCI_DVSEC_CXL_DEVICE);
+	if (!offset)
+		return false;
+
+	if (pci_read_config_word(pdev, offset + PCI_DVSEC_CXL_CAP, &cap))
+		return false;
+
+	return cap & PCI_DVSEC_CXL_CACHE_CAPABLE;
+}
+

drivers/pci/ats.c int pci_ats_page_aligned(struct pci_dev *pdev) · line 244

Quoted 10 lines
drivers/pci/ats.c +10 -0
+/**
+ * pci_ats_required - Whether the PCI device requires ATS
+ * @pdev: the PCI device
+ *
+ * Returns true, if the PCI device requires ATS for basic functional operation.
+ */
+bool pci_ats_required(struct pci_dev *pdev)
+{
+	if (pci_ats_disabled() || !pci_ats_supported(pdev))
+		return false;

I still have the question about whether it's necessary to test pci_ats_disabled() here. I think pci_ats_supported() should return false if pci_ats_disabled() returns true.

Quoted 14 lines
drivers/pci/ats.c +8 -0
+	/* A VF inherits its PF's requirement for ATS function */
+	if (pdev->is_virtfn)
+		pdev = pci_physfn(pdev);
+
+	return pci_cxl_ats_required(pdev);
+}
+EXPORT_SYMBOL_GPL(pci_ats_required);
+
 #ifdef CONFIG_PCI_PRI
 void pci_pri_init(struct pci_dev *pdev)
 {
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v5 1/3] PCI: Add pci_ats_required() for CXL.cache capable devices

Quoted 9 lines · Bjorn Helgaas

drivers/pci/ats.c int pci_ats_page_aligned(struct pci_dev *pdev) · line 244

Quoted 4 lines · Nicolin Chen
drivers/pci/ats.c +4 -0
+bool pci_ats_required(struct pci_dev *pdev)
+{
+	if (pci_ats_disabled() || !pci_ats_supported(pdev))
+		return false;

I still have the question about whether it's necessary to test pci_ats_disabled() here. I think pci_ats_supported() should return false if pci_ats_disabled() returns true.

Sorry, it fell through the crack.

And you are right that. pci_ats_disabled() seems redundant.

I can respin a v6.

Thanks
Nicolin

#4 lore raw

Re: [PATCH v5 1/3] PCI: Add pci_ats_required() for CXL.cache capable devices

In reply to Nicolin Chen
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The logic intended to allow a Virtual Function (VF) to inherit its Physical Function's (PF) ATS…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The logic intended to allow a Virtual Function (VF) to inherit its Physical Function's (PF) ATS requirements is dead code. As a result, pci_ats_required() will always incorrectly return false for VFs. --

commit 0af55683d00886be6357cf5cf0cf3df17b4c4b9b
Author: Nicolin Chen <[email protected]>

PCI: Add pci_ats_required() for CXL.cache capable devices

This commit introduces pci_ats_required() to allow IOMMU drivers to keep ATS always on for specific devices, like CXL.cache devices. It includes logic for Virtual Functions (VFs) to inherit the ATS requirements of their Physical Functions (PFs).

Quoted 3 lines
drivers/pci/ats.c +0 -0

[ ... ]

drivers/pci/ats.c int pci_ats_page_aligned(struct pci_dev *pdev) · line 248

Quoted 8 lines
drivers/pci/ats.c +8 -0
+bool pci_ats_required(struct pci_dev *pdev)
+{
+	if (pci_ats_disabled() || !pci_ats_supported(pdev))
+		return false;
+
+	/* A VF inherits its PF's requirement for ATS function */
+	if (pdev->is_virtfn)
+		pdev = pci_physfn(pdev);

[Severity: High]
Will this VF inheritance logic ever be reached?

According to the PCIe SR-IOV specification (section 9.3.3.1), VFs do not implement the ATS Extended Capability, which means pdev->ats_cap is always 0 for VFs.

Because of this, pci_ats_supported(pdev) will unconditionally return false for any VF. This causes the function to return false before it can ever reach the pdev->is_virtfn check.

Could this prevent VFs from correctly enabling the ATS always on feature and leave them unable to access host memory without triggering IOMMU faults?

Quoted 3 lines
drivers/pci/ats.c +3 -0
+
+	return pci_cxl_ats_required(pdev);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

PATCH v5 2/3 PCI: Allow ATS to be always on for pre-CXL devices

In reply to Nicolin Chen

Some NVIDIA GPU/NIC devices, though they don't implement CXL config space, have many CXL-like properties. Call this kind "pre-CXL".

Similar to CXL.cache capability, these pre-CXL devices also require the ATS function even when their RIDs are IOMMU bypassed, i.e. keep ATS "always on" v.s. "on demand" when a non-zero PASID line gets enabled in SVA use cases.

Introduce pci_dev_specific_ats_required() quirk function to scan a list of IDs for these devices. Then, include it in pci_ats_required().

---
 drivers/pci/pci.h    |  9 +++++++++
 drivers/pci/ats.c    |  3 ++-
 drivers/pci/quirks.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
drivers/pci/pci.h +9 -0
index 4a14f88e543a2..e8ad27abb1cfe 100644
@@ -1155,6 +1155,15 @@ static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
 }
 #endif
 
+#if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_PCI_ATS)
+bool pci_dev_specific_ats_required(struct pci_dev *dev);
+#else
+static inline bool pci_dev_specific_ats_required(struct pci_dev *dev)
+{
+	return false;
+}
+#endif
+
 #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
 int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
 			  struct resource *res);
drivers/pci/ats.c +2 -1
index ebdf761843867..3a04d5b04c883 100644
@@ -247,7 +247,8 @@ bool pci_ats_required(struct pci_dev *pdev)
 	if (pdev->is_virtfn)
 		pdev = pci_physfn(pdev);
 
-	return pci_cxl_ats_required(pdev);
+	return pci_cxl_ats_required(pdev) ||
+	       pci_dev_specific_ats_required(pdev);
 }
 EXPORT_SYMBOL_GPL(pci_ats_required);
 
drivers/pci/quirks.c +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.43.0
Acked-by on v5 2/3
#6 lore raw

Re: [PATCH v5 2/3] PCI: Allow ATS to be always on for pre-CXL devices

Quoted 10 lines · Nicolin Chen

Some NVIDIA GPU/NIC devices, though they don't implement CXL config space, have many CXL-like properties. Call this kind "pre-CXL".

Similar to CXL.cache capability, these pre-CXL devices also require the ATS function even when their RIDs are IOMMU bypassed, i.e. keep ATS "always on" v.s. "on demand" when a non-zero PASID line gets enabled in SVA use cases.

Introduce pci_dev_specific_ats_required() quirk function to scan a list of IDs for these devices. Then, include it in pci_ats_required().

Quoted 8 lines
Quoted 96 lines
---
 drivers/pci/pci.h    |  9 +++++++++
 drivers/pci/ats.c    |  3 ++-
 drivers/pci/quirks.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
drivers/pci/pci.h +9 -0
index 4a14f88e543a2..e8ad27abb1cfe 100644
@@ -1155,6 +1155,15 @@ static inline int pci_dev_specific_reset(struct pci_dev *dev, bool probe)
 }
 #endif
 
+#if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_PCI_ATS)
+bool pci_dev_specific_ats_required(struct pci_dev *dev);
+#else
+static inline bool pci_dev_specific_ats_required(struct pci_dev *dev)
+{
+	return false;
+}
+#endif
+
 #if defined(CONFIG_PCI_QUIRKS) && defined(CONFIG_ARM64)
 int acpi_get_rc_resources(struct device *dev, const char *hid, u16 segment,
 			  struct resource *res);
drivers/pci/ats.c +2 -1
index ebdf761843867..3a04d5b04c883 100644
@@ -247,7 +247,8 @@ bool pci_ats_required(struct pci_dev *pdev)
 	if (pdev->is_virtfn)
 		pdev = pci_physfn(pdev);
 
-	return pci_cxl_ats_required(pdev);
+	return pci_cxl_ats_required(pdev) ||
+	       pci_dev_specific_ats_required(pdev);
 }
 EXPORT_SYMBOL_GPL(pci_ats_required);
 
drivers/pci/quirks.c +42 -0
index caaed1a01dc02..c0242f3e9f063 100644
@@ -5715,6 +5715,48 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1457, quirk_intel_e2000_no_ats);
 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1459, quirk_intel_e2000_no_ats);
 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x145a, quirk_intel_e2000_no_ats);
 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x145c, quirk_intel_e2000_no_ats);
+
+static bool quirk_nvidia_gpu_ats_required(struct pci_dev *pdev)
+{
+	switch (pdev->device) {
+	case 0x2e00 ... 0x2e3f: /* GB20B */
+		return true;
+	}
+	return false;
+}
+
+static const struct pci_dev_ats_required {
+	u16 vendor;
+	u16 device;
+	bool (*ats_required)(struct pci_dev *dev);
+} pci_dev_ats_required[] = {
+	/* NVIDIA GPUs */
+	{ PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, quirk_nvidia_gpu_ats_required },
+	/* NVIDIA CX10 Family NVlink-C2C */
+	{ PCI_VENDOR_ID_MELLANOX, 0x2101, NULL },
+	{ 0 }
+};
+
+/*
+ * Some NVIDIA devices do not implement CXL config space, but present as PCIe
+ * devices that can issue CXL-like cache operations like CXL.cache. Thus, they
+ * require ATS to obtain host physical addresses, like pci_cxl_ats_required().
+ */
+bool pci_dev_specific_ats_required(struct pci_dev *pdev)
+{
+	const struct pci_dev_ats_required *i;
+
+	for (i = pci_dev_ats_required; i->vendor; i++) {
+		if (i->vendor != pdev->vendor)
+			continue;
+		if (i->ats_required && i->ats_required(pdev))
+			return true;
+		if (!i->ats_required && i->device == pdev->device)
+			return true;
+	}
+
+	return false;
+}
 #endif /* CONFIG_PCI_ATS */
 
 /* Freescale PCIe doesn't support MSI in RC mode */
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v5 2/3] PCI: Allow ATS to be always on for pre-CXL devices

In reply to Nicolin Chen
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The matching logic in `pci_dev_specific_ats_required()` is flawed: it ignores the `device` field…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The matching logic in pci_dev_specific_ats_required() is flawed: it ignores the device field when a callback is provided and fails to handle PCI_ANY_ID wildcards when a callback is absent. --

commit fb36e847666c196ca64ced94fef31e14f46cf42d
Author: Nicolin Chen <[email protected]>

PCI: Allow ATS to be always on for pre-CXL devices

This commit introduces a pci_dev_specific_ats_required() quirk function to allow certain pre-CXL devices to keep ATS always on. It adds a list of IDs for these devices and integrates the quirk into pci_ats_required().

Quoted 4 lines
drivers/pci/quirks.c +0 -0
index caaed1a01dc02..c0242f3e9f063 100644

[ ... ]

Quoted 5 lines
drivers/pci/quirks.c +5 -0
+bool pci_dev_specific_ats_required(struct pci_dev *pdev)
+{
+	const struct pci_dev_ats_required *i;
+
+	for (i = pci_dev_ats_required; i->vendor; i++) {

drivers/pci/quirks.c DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x1457, quirk_intel_e2000_no_ats); · line 5759

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+		if (i->vendor != pdev->vendor)
+			continue;
+		if (i->ats_required && i->ats_required(pdev))
+			return true;
+		if (!i->ats_required && i->device == pdev->device)
+			return true;
+	}
+
+	return false;
+}

[Severity: Medium]
Does this matching logic inadvertently ignore the device field when a callback is provided, and miss handling PCI_ANY_ID when a callback is absent?

If a future entry specifies a precise device ID along with a callback (e.g., { VENDOR, 0x1234, my_quirk }), the specific device ID will be ignored, and the callback will execute for all devices from that vendor.

Alternatively, if a callback is absent, a catch-all definition like { VENDOR, PCI_ANY_ID, NULL } would literally compare pdev->device against 0xFFFF and silently fail to match because it lacks a PCI_ANY_ID check.

While this functionally works for the two currently defined table entries in the patch, could this be updated to mirror standard PCI quirk parsing behavior (like pci_dev_specific_reset()) to prevent issues for future additions?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

PATCH v5 3/3 iommu/arm-smmu-v3: Allow ATS to be always on

In reply to Nicolin Chen

When a device's default substream attaches to an identity domain, the SMMU driver currently sets the device's STE between two modes:

Mode 1: Cfg=Translate, S1DSS=Bypass, EATS=1
Mode 2: Cfg=bypass (EATS is ignored by HW)

When there is an active PASID (non-default substream), mode 1 is used. And when there is no PASID support or no active PASID, mode 2 is used.

The driver will also downgrade an STE from mode 1 to mode 2, when the last active substream becomes inactive.

However, there are PCIe devices that demand ATS to be always on. For these devices, their STEs have to use the mode 1 as HW ignores EATS with mode 2.

Change the driver accordingly:
- always use the mode 1
- never downgrade to mode 2
- allocate and retain a CD table (see note below)

Note that these devices might not support PASID, i.e. doing non-PASID ATS. In such a case, the ssid_bits is set to 0. However, s1cdmax must be set to a !0 value in order to keep the S1DSS field effective. Thus, when a master requires ats_always_on, set its s1cdmax to at least 1, meaning that the CD table will have a dummy entry (SSID=1) that will never be used.

Now for these devices, arm_smmu_cdtab_allocated() will always return true, v.s. false prior to this change. When its default substream is attached to an IDENTITY domain, its first CD is NULL in the table, which is a totally valid case. Thus, add "!master->ats_always_on" to the condition.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  1 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 75 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 8 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -0
index ef42df4753ec4..8c3600f4364c5 100644
@@ -943,6 +943,7 @@ struct arm_smmu_master {
 	bool				ats_enabled : 1;
 	bool				ste_ats_enabled : 1;
 	bool				stall_enabled;
+	bool				ats_always_on;
 	unsigned int			ssid_bits;
 	unsigned int			iopf_refcount;
 };
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +67 -8

121 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH v5 3/3] iommu/arm-smmu-v3: Allow ATS to be always on

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Silent failure to enforce ATS requirement leads to device malfunction. - [High] Inconsistent STE…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Silent failure to enforce ATS requirement leads to device malfunction.
- [High] Inconsistent STE configuration (Mode 1 vs Mode 2) for BLOCKED domains when ats_always_on is true. --

commit 592bdfd18801a5a10cbb239b47e71f0772eea39e
Author: Nicolin Chen <[email protected]>

iommu/arm-smmu-v3: Allow ATS to be always on

This patch modifies the ARM SMMUv3 driver to allow ATS to remain active even when a device's default substream is attached to an identity domain. It does this by retaining a CD table and ensuring the STE stays in Mode 1 for devices that require ATS to be always on. It also ensures the STE is not downgraded to Mode 2 when the last active substream becomes inactive.

Quoted 4 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index e8d7dbe495f03..5c9d4bb542249 100644

[ ... ]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, · line 3899

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
@@ -3875,6 +3895,8 @@ static void arm_smmu_attach_dev_ste(struct iommu_domain *domain,
 		.old_domain = old_domain,
 		.ssid = IOMMU_NO_PASID,
 	};
+	bool ats_always_on = master->ats_always_on &&
+			     s1dss != STRTAB_STE_1_S1DSS_TERMINATE;

[Severity: High]
Does this explicitly force ats_always_on to false for BLOCKED domains?

If s1dss is STRTAB_STE_1_S1DSS_TERMINATE, the device will initially use Mode 2 (CFG=ABORT), which disables EATS and appears to contradict the goal of always keeping ATS on for these devices.

Additionally, if a PASID is attached (upgrading the STE to Mode 1) and then subsequently removed:

arm_smmu_blocking_set_dev_pasid() {
    ...
    if (!master->ats_always_on &&
        !arm_smmu_ssids_in_use(&master->cd_table)) {
            ...
    }
}

The revert operation is skipped here because master->ats_always_on evaluates to true, leaving the STE permanently in Mode 1.

Can this create an inconsistent state machine where the initial Mode 2 state could trigger fatal URs for ATS requests, but detaching a PASID silently leaves the device in Mode 1?

[ ... ]

Quoted 1 line
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
@@ -4215,6 +4237,42 @@ static void arm_smmu_remove_master(struct arm_smmu_master *master)

[ ... ]

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
+static int arm_smmu_master_prepare_ats(struct arm_smmu_master *master)
+{

[ ... ]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_remove_master(struct arm_smmu_master *master) · line 4262

Quoted 11 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +11 -0
+	if (!pci_ats_required(pdev))
+		goto out_prepare;
+
+	/*
+	 * S1DSS is required for ATS to be always on for identity domain cases.
+	 * However, the S1DSS field is ignored if !IDR0_S1P or !IDR1_SSIDSIZE.
+	 */
+	if (!s1p || !master->smmu->ssid_bits) {
+		dev_info_once(master->dev,
+			      "SMMU doesn't support ATS to be always on\n");
+		goto out_prepare;

[Severity: High]
If the SMMU lacks the necessary features to support ats_always_on when it is strictly required by the device, should this code return an error like -EOPNOTSUPP instead of returning success?

Returning success here allows the IOMMU probe to succeed without configuring ats_always_on. When the device is later attached to an IDENTITY or BLOCKED domain, it will be configured in a mode where EATS is ignored.

Could this result in the SMMU aborting ATS Translation Requests as Unsupported Requests (UR) on the PCIe bus and triggering AER errors, rather than cleanly failing the probe when the hardware requirement cannot be met?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw

Re: [PATCH v5 3/3] iommu/arm-smmu-v3: Allow ATS to be always on

In reply to Nicolin Chen

Hi All,

Sashiko pointed a couple of valid points.

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain, · line 3876

Quoted 9 lines · Nicolin Chen
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -1
@@ -3851,7 +3870,8 @@ static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain,
 	 * When the last user of the CD table goes away downgrade the STE back
 	 * to a non-cd_table one, by re-attaching its sid_domain.
 	 */
-	if (!arm_smmu_ssids_in_use(&master->cd_table)) {
+	if (!master->ats_always_on &&
+	    !arm_smmu_ssids_in_use(&master->cd_table)) {
 		struct iommu_domain *sid_domain =
 			iommu_driver_get_domain_for_dev(master->dev);

Here the detach path doesn't check sid_domain's type, mismatching..

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
@@ -3875,6 +3895,8 @@ static void arm_smmu_attach_dev_ste(struct iommu_domain *domain,
 		.old_domain = old_domain,
 		.ssid = IOMMU_NO_PASID,
 	};
+	bool ats_always_on = master->ats_always_on &&
+			     s1dss != STRTAB_STE_1_S1DSS_TERMINATE;

[...]

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, · line 3911

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
-	if (arm_smmu_ssids_in_use(&master->cd_table)) {
+	if (ats_always_on || arm_smmu_ssids_in_use(&master->cd_table)) {

.. the attach path where it only applies to IOMMU_DOMAIN_IDENTITY.

I am addressing this with:

@ -3870,13 +3870,15 @@ static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain,
* When the last user of the CD table goes away downgrade the STE back
* to a non-cd_table one, by re-attaching its sid_domain.
*/
- if (!master->ats_always_on &&
- !arm_smmu_ssids_in_use(&master->cd_table)) {
+ if (!arm_smmu_ssids_in_use(&master->cd_table)) {
struct iommu_domain *sid_domain =
iommu_driver_get_domain_for_dev(master->dev);
+ bool ats_always_on = master->ats_always_on &&
+ sid_domain->type != IOMMU_DOMAIN_BLOCKED;
+ bool downgrade = sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
+ sid_domain->type == IOMMU_DOMAIN_BLOCKED;

- if (sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
- sid_domain->type == IOMMU_DOMAIN_BLOCKED)
+ if (!ats_always_on && downgrade)

                        sid_domain->ops->attach_dev(sid_domain, dev,
                                                    sid_domain);
        }
Quoted 25 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +25 -0
+static int arm_smmu_master_prepare_ats(struct arm_smmu_master *master)
+{
+	bool s1p = master->smmu->features & ARM_SMMU_FEAT_TRANS_S1;
+	unsigned int stu = __ffs(master->smmu->pgsize_bitmap);
+	struct pci_dev *pdev;
+	int ret;
+
+	if (!arm_smmu_ats_supported(master))
+		return 0;
+
+	pdev = to_pci_dev(master->dev);
+
+	if (!pci_ats_required(pdev))
+		goto out_prepare;
+
+	/*
+	 * S1DSS is required for ATS to be always on for identity domain cases.
+	 * However, the S1DSS field is ignored if !IDR0_S1P or !IDR1_SSIDSIZE.
+	 */
+	if (!s1p || !master->smmu->ssid_bits) {
+		dev_info_once(master->dev,
+			      "SMMU doesn't support ATS to be always on\n");
+		goto out_prepare;
+	}
+

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c static void arm_smmu_remove_master(struct arm_smmu_master *master) · line 4274

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +10 -0
+	master->ats_always_on = true;
+
+	ret = arm_smmu_alloc_cd_tables(master);
+	if (ret)
+		return ret;
+
+out_prepare:
+	pci_prepare_ats(pdev, stu);
+	return 0;
+}

Another issue is: arm_smmu_master_prepare_ats() here doesn't guard against cases when !arm_smmu_ats_supported() && pci_ats_required().

"!s1p || !master->smmu->ssid_bits" also needs to fail the function.

Actually, this function does not return error at all. This reminds me of the !pci_prepare_ats() issue Pranj is fixing. For this series, at least we should propagate the pci_prepare_ats() return value to the caller.

I see this single patch is getting large. Maybe it's a good idea to split a bit. I will keep the reviewed parts into patches that retain the given reviewed-by tags.

Thanks
Nicolin

#11 lore raw

Re: [PATCH v5 3/3] iommu/arm-smmu-v3: Allow ATS to be always on

Quoted 9 lines · Nicolin Chen

@ -3870,13 +3870,15 @@ static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain,
* When the last user of the CD table goes away downgrade the STE back
* to a non-cd_table one, by re-attaching its sid_domain.
*/

(quoted diff) +1 -2
-       if (!master->ats_always_on &&
-           !arm_smmu_ssids_in_use(&master->cd_table)) {
+       if (!arm_smmu_ssids_in_use(&master->cd_table)) {
                struct iommu_domain *sid_domain =
                        iommu_driver_get_domain_for_dev(master->dev);

Review

Quoted 10 lines
(quoted diff) +5 -2
+               bool ats_always_on = master->ats_always_on &&
+                                    sid_domain->type != IOMMU_DOMAIN_BLOCKED;
+               bool downgrade = sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
+                                sid_domain->type == IOMMU_DOMAIN_BLOCKED;
 
-               if (sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
-                   sid_domain->type == IOMMU_DOMAIN_BLOCKED)
+               if (!ats_always_on && downgrade)
                        sid_domain->ops->attach_dev(sid_domain, dev,
                                                    sid_domain);

Only identity should remain with the CD S1DSS STE, BLOCKED should attach the normal blocking domain still

Jason

#12 lore raw

Re: [PATCH v5 3/3] iommu/arm-smmu-v3: Allow ATS to be always on

Quoted 15 lines · Jason Gunthorpe
Quoted 14 lines · Nicolin Chen

@ -3870,13 +3870,15 @@ static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain,
* When the last user of the CD table goes away downgrade the STE back
* to a non-cd_table one, by re-attaching its sid_domain.
*/

(quoted diff) +5 -2
-       if (!master->ats_always_on &&
-           !arm_smmu_ssids_in_use(&master->cd_table)) {
+       if (!arm_smmu_ssids_in_use(&master->cd_table)) {
                struct iommu_domain *sid_domain =
                        iommu_driver_get_domain_for_dev(master->dev);
+               bool ats_always_on = master->ats_always_on &&
+                                    sid_domain->type != IOMMU_DOMAIN_BLOCKED;
+               bool downgrade = sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
+                                sid_domain->type == IOMMU_DOMAIN_BLOCKED;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +1 -2
-               if (sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
-                   sid_domain->type == IOMMU_DOMAIN_BLOCKED)
+               if (!ats_always_on && downgrade)
                        sid_domain->ops->attach_dev(sid_domain, dev,
                                                    sid_domain);

Only identity should remain with the CD S1DSS STE, BLOCKED should attach the normal blocking domain still

Yea. I have !ats_always_on guarding here.

Thanks
Nicolin