Skip to contentSkip to conversation

iommu/arm-smmu-v3: Document SVA interaction with new pagetable features

PATCH Applied Robin Murphy 6 participants 12 messages Dec 5, 2024 – Dec 10, 2024

Subsystems (4)
lore
Share & tools
lore

Robin Murphy

author reviewer
#0 lore raw

PATCH 1/2 iommu/arm-smmu-v3: Document SVA interaction with new pagetable features

Process pagetables may now be using new permission-indirection-based features which an SMMU may not understand when given such a table for SVA. Although SMMUv3.4 does add its own S1PIE feature, realistically we're still going to have to cope with feature mismatches between CPUs and SMMUs, so let's start simple and essentially just document the expectations for what falls out as-is. Although it seems unlikely for SVA applications to also depend on memory-hardening features, or vice-versa, the relative lifecycles make it tricky to enforce mutual exclusivity. Thankfully our PIE index allocation makes it relatively benign for an SMMU to keep interpreting them as direct permissions, the only real implication is that an SVA application cannot harden itself against its own devices with these features. Thus, inform the user about that just in case they have other expectations.

Also we don't (yet) support LPA2, so deny SVA entirely if we're going to misunderstand the pagetable format altogether.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +14 -1
index 1d3e71569775..9ba596430e7c 100644
@@ -112,6 +112,15 @@ void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
 	 * from the current CPU register
 	 */
 	target->data[3] = cpu_to_le64(read_sysreg(mair_el1));
+
+	/*
+	 * Note that we don't bother with S1PIE on the SMMU, we just rely on
+	 * our default encoding scheme matching direct permissions anyway.
+	 * SMMU has no notion of S1POE nor GCS, so make sure that is clear if
+	 * either is enabled for CPUs, just in case anyone imagines otherwise.
+	 */
+	if (system_supports_poe() || system_supports_gcs())
+		dev_warn_once(master->smmu->dev, "SVA devices ignore permission overlays and GCS\n");
 }
 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_sva_cd);
 
@@ -206,8 +215,12 @@ bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
 	unsigned long asid_bits;
 	u32 feat_mask = ARM_SMMU_FEAT_COHERENCY;
 
-	if (vabits_actual == 52)
+	if (vabits_actual == 52) {
+		/* We don't support LPA2 */
+		if (PAGE_SIZE != SZ_64K)
+			return false;
 		feat_mask |= ARM_SMMU_FEAT_VAX;
+	}
 
 	if ((smmu->features & feat_mask) != feat_mask)
 		return false;
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author reviewer
#1 lore raw

PATCH 2/2 arm64: cpufeature: Add GCS to cpucap_is_possible()

Since system_supports_gcs() ends up referring to cpucap_is_possible(), teach the latter about GCS for consistency with similar features.

---
 arch/arm64/include/asm/cpucaps.h    | 2 ++
 arch/arm64/include/asm/cpufeature.h | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/arm64/include/asm/cpucaps.h +2 -0
index 201a46efd918..cbbf70e0f204 100644
@@ -44,6 +44,8 @@ cpucap_is_possible(const unsigned int cap)
 		return IS_ENABLED(CONFIG_ARM64_TLB_RANGE);
 	case ARM64_HAS_S1POE:
 		return IS_ENABLED(CONFIG_ARM64_POE);
+	case ARM64_HAS_GCS:
+		return IS_ENABLED(CONFIG_ARM64_GCS);
 	case ARM64_UNMAP_KERNEL_AT_EL0:
 		return IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0);
 	case ARM64_WORKAROUND_843419:
arch/arm64/include/asm/cpufeature.h +1 -2
index b64e49bd9d10..8b4e5a3cd24c 100644
@@ -847,8 +847,7 @@ static inline bool system_supports_poe(void)
 
 static inline bool system_supports_gcs(void)
 {
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
 }
 
 static inline bool system_supports_haft(void)
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
reviewer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

Quoted 4 lines · Robin Murphy

Since system_supports_gcs() ends up referring to cpucap_is_possible(), teach the latter about GCS for consistency with similar features.

Looking around, we should probably do the same for HAFT.

Mark.

Quoted 35 lines
---
 arch/arm64/include/asm/cpucaps.h    | 2 ++
 arch/arm64/include/asm/cpufeature.h | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/arm64/include/asm/cpucaps.h +2 -0
index 201a46efd918..cbbf70e0f204 100644
@@ -44,6 +44,8 @@ cpucap_is_possible(const unsigned int cap)
 		return IS_ENABLED(CONFIG_ARM64_TLB_RANGE);
 	case ARM64_HAS_S1POE:
 		return IS_ENABLED(CONFIG_ARM64_POE);
+	case ARM64_HAS_GCS:
+		return IS_ENABLED(CONFIG_ARM64_GCS);
 	case ARM64_UNMAP_KERNEL_AT_EL0:
 		return IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0);
 	case ARM64_WORKAROUND_843419:
arch/arm64/include/asm/cpufeature.h +1 -2
index b64e49bd9d10..8b4e5a3cd24c 100644
@@ -847,8 +847,7 @@ static inline bool system_supports_poe(void)
 
 static inline bool system_supports_gcs(void)
 {
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
 }
 
 static inline bool system_supports_haft(void)
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

In reply to Robin Murphy
Quoted 2 lines · Robin Murphy

Since system_supports_gcs() ends up referring to cpucap_is_possible(), teach the latter about GCS for consistency with similar features.

Not clear why this is part of a series, there's no obvious relationship with patch 1?

arch/arm64/include/asm/cpufeature.h static inline bool system_supports_poe(void) · line 851

Quoted 6 lines

static inline bool system_supports_gcs(void)
{

arch/arm64/include/asm/cpufeature.h +1 -2
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
 }

Ah, this is bitrot since the series was on the list for so long. As well as HAFT which Rutland mentioned it looks like _bti_kernel(), _irq_prio_masking() and possibly others have the same thing. Ideally we'd have no uses of IS_ENABLED() in these functions so it's a bit more obvious. In any case

maintainer
#4 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

Quoted 6 lines · Mark Brown
Quoted 2 lines · Robin Murphy

Since system_supports_gcs() ends up referring to cpucap_is_possible(), teach the latter about GCS for consistency with similar features.

Not clear why this is part of a series, there's no obvious relationship with patch 1?

No, probably Robing forgot to pass --no-thread to git.

Quoted 13 lines

arch/arm64/include/asm/cpufeature.h static inline bool system_supports_poe(void) · line 851

Quoted 6 lines

static inline bool system_supports_gcs(void)
{

arch/arm64/include/asm/cpufeature.h +1 -2
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
 }

Ah, this is bitrot since the series was on the list for so long. As well as HAFT which Rutland mentioned it looks like _bti_kernel(), _irq_prio_masking() and possibly others have the same thing. Ideally we'd have no uses of IS_ENABLED() in these functions so it's a bit more obvious. In any case

Quoted 1 line

Thanks. This patch will go in via the arm64 tree. I'll leave the other for the smmu tree.

Signature · 1 line
Catalin
author reviewer
#5 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

Quoted 9 lines · Catalin Marinas
Quoted 6 lines · Mark Brown
Quoted 2 lines · Robin Murphy

Since system_supports_gcs() ends up referring to cpucap_is_possible(), teach the latter about GCS for consistency with similar features.

Not clear why this is part of a series, there's no obvious relationship with patch 1?

No, probably Robing forgot to pass --no-thread to git.

Yeah, this was just a tangent from poking around figuring out how to detect GCS being enabled for the main patch.

Quoted 13 lines
Quoted 13 lines

arch/arm64/include/asm/cpufeature.h static inline bool system_supports_poe(void) · line 851

Quoted 6 lines
  static inline bool system_supports_gcs(void)
  {
arch/arm64/include/asm/cpufeature.h +1 -2
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
  }

Ah, this is bitrot since the series was on the list for so long. As well as HAFT which Rutland mentioned it looks like _bti_kernel(), _irq_prio_masking() and possibly others have the same thing. Ideally we'd have no uses of IS_ENABLED() in these functions so it's a bit more obvious. In any case

Quoted 4 lines
Quoted 1 line

Thanks. This patch will go in via the arm64 tree. I'll leave the other for the smmu tree.

Thanks!

Robin.

reviewer
#6 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

In reply to Mark Brown
Quoted 11 lines · Mark Brown

arch/arm64/include/asm/cpufeature.h static inline bool system_supports_poe(void) · line 851

Quoted 6 lines · Robin Murphy

static inline bool system_supports_gcs(void)
{

arch/arm64/include/asm/cpufeature.h +1 -2
-	return IS_ENABLED(CONFIG_ARM64_GCS) &&
-		alternative_has_cap_unlikely(ARM64_HAS_GCS);
+	return alternative_has_cap_unlikely(ARM64_HAS_GCS);
 }

Ah, this is bitrot since the series was on the list for so long. As well as HAFT which Rutland mentioned it looks like _bti_kernel(), _irq_prio_masking() and possibly others have the same thing.

In <asm/cpufeature.h>, only system_supports_gcs() and system_supports_haft() don't use cpucap_is_possible(); all the other IS_ENABLED() checks are for additional dependent properties, and rely on the base case being handled in cpucap_is_possible().

In <asm/cpucaps.h> we have:

	static __always_inline bool
	cpucap_is_possible(const unsigned int cap)
	{
		...
		switch (cap) {
		...
		case ARM64_HAS_PAN:
			return IS_ENABLED(CONFIG_ARM64_PAN);
		...
		case ARM64_BTI:
			return IS_ENABLED(CONFIG_ARM64_BTI);
		...
		case ARM64_HAS_GIC_PRIO_MASKING:
			return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI);
		...
		}
	}

In <asm/cpufeature.h> we have:

	// base case
	static inline bool system_uses_hw_pan(void)
	{
		return alternative_has_cap_unlikely(ARM64_HAS_PAN);
	}

	// additional dependent property
	static inline bool system_uses_ttbr0_pan(void)
	{
		return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&
		       !system_uses_hw_pan();
	}

	...

	// base case
	static __always_inline bool system_uses_irq_prio_masking(void)
	{
		return alternative_has_cap_unlikely(ARM64_HAS_GIC_PRIO_MASKING);
	}

	// additional dependent property
	static inline bool system_has_prio_mask_debugging(void)
	{
		return IS_ENABLED(CONFIG_ARM64_DEBUG_PRIORITY_MASKING) &&
		       system_uses_irq_prio_masking();
	}

	...

	// base case
	static inline bool system_supports_bti(void)
	{
		return cpus_have_final_cap(ARM64_BTI);
	}

	// additional dependent property
	static inline bool system_supports_bti_kernel(void)
	{
		return IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) &&
			cpus_have_final_boot_cap(ARM64_BTI);
	}
Quoted 2 lines

Ideally we'd have no uses of IS_ENABLED() in these functions so it's a bit more obvious.

For the three cases above we can't do that without additional cpucaps, and I think that'd be worse. I don't see anything we can practically do other than add comments, and I suspect that's not going to help either.

Mark.

#7 lore raw

Re: [PATCH 2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

Quoted 4 lines · Mark Rutland

In <asm/cpufeature.h>, only system_supports_gcs() and system_supports_haft() don't use cpucap_is_possible(); all the other IS_ENABLED() checks are for additional dependent properties, and rely on the base case being handled in cpucap_is_possible().

Ah, yes - I got lost in indirections while checking.

#8 lore raw
In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

Process pagetables may now be using new permission-indirection-based features which an SMMU may not understand when given such a table for SVA. Although SMMUv3.4 does add its own S1PIE feature, realistically we're still going to have to cope with feature mismatches between CPUs and SMMUs, so let's start simple and essentially just document the expectations for what falls out as-is. Although it seems unlikely for SVA applications to also depend on memory-hardening features, or vice-versa,

Hard to predict, but SVA could be indirectly enabled by some common
library: for example a libcrypto plugin that shares the address space with an accelerator when one is available (see uadk_engine). I'm not familiar with the POE use-cases, but I guess a common library could also enable those memory-hardening features, such that unsuspecting applications have everything enabled.

Quoted 22 lines

the relative lifecycles make it tricky to enforce mutual exclusivity. Thankfully our PIE index allocation makes it relatively benign for an SMMU to keep interpreting them as direct permissions, the only real implication is that an SVA application cannot harden itself against its own devices with these features. Thus, inform the user about that just in case they have other expectations.

Also we don't (yet) support LPA2, so deny SVA entirely if we're going to misunderstand the pagetable format altogether.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +0 -0
index 1d3e71569775..9ba596430e7c 100644
@@ -112,6 +112,15 @@ void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
 	 * from the current CPU register
 	 */

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, · line 123

Quoted 10 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +9 -0
 	target->data[3] = cpu_to_le64(read_sysreg(mair_el1));
+
+	/*
+	 * Note that we don't bother with S1PIE on the SMMU, we just rely on
+	 * our default encoding scheme matching direct permissions anyway.
+	 * SMMU has no notion of S1POE nor GCS, so make sure that is clear if
+	 * either is enabled for CPUs, just in case anyone imagines otherwise.
+	 */
+	if (system_supports_poe() || system_supports_gcs())
+		dev_warn_once(master->smmu->dev, "SVA devices ignore permission overlays and GCS\n");

If POE tightens the page permissions, enabling SVA on top lets the application easily bypass the POE protection. Could we actually return false in arm_smmu_sva_supported() instead of displaying a warning that likely no one will notice?

I guess enforcing !(SVA & POE) on an address space would be too much work?

Thanks,
Jean

Quoted 20 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +5 -1
 }
 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_sva_cd);
 
@@ -206,8 +215,12 @@ bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
 	unsigned long asid_bits;
 	u32 feat_mask = ARM_SMMU_FEAT_COHERENCY;
 
-	if (vabits_actual == 52)
+	if (vabits_actual == 52) {
+		/* We don't support LPA2 */
+		if (PAGE_SIZE != SZ_64K)
+			return false;
 		feat_mask |= ARM_SMMU_FEAT_VAX;
+	}
 
 	if ((smmu->features & feat_mask) != feat_mask)
 		return false;
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author reviewer
#9 lore raw
Quoted 46 lines · Jean-Philippe Brucker
Quoted 8 lines · Robin Murphy

Process pagetables may now be using new permission-indirection-based features which an SMMU may not understand when given such a table for SVA. Although SMMUv3.4 does add its own S1PIE feature, realistically we're still going to have to cope with feature mismatches between CPUs and SMMUs, so let's start simple and essentially just document the expectations for what falls out as-is. Although it seems unlikely for SVA applications to also depend on memory-hardening features, or vice-versa,

Hard to predict, but SVA could be indirectly enabled by some common
library: for example a libcrypto plugin that shares the address space with an accelerator when one is available (see uadk_engine). I'm not familiar with the POE use-cases, but I guess a common library could also enable those memory-hardening features, such that unsuspecting applications have everything enabled.

Quoted 29 lines

the relative lifecycles make it tricky to enforce mutual exclusivity. Thankfully our PIE index allocation makes it relatively benign for an SMMU to keep interpreting them as direct permissions, the only real implication is that an SVA application cannot harden itself against its own devices with these features. Thus, inform the user about that just in case they have other expectations.

Also we don't (yet) support LPA2, so deny SVA entirely if we're going to misunderstand the pagetable format altogether.

---
  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +6 -0
index 1d3e71569775..9ba596430e7c 100644
@@ -112,6 +112,15 @@ void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
  	 * from the current CPU register
  	 */
  	target->data[3] = cpu_to_le64(read_sysreg(mair_el1));
+
+	/*
+	 * Note that we don't bother with S1PIE on the SMMU, we just rely on
+	 * our default encoding scheme matching direct permissions anyway.
+	 * SMMU has no notion of S1POE nor GCS, so make sure that is clear if
+	 * either is enabled for CPUs, just in case anyone imagines otherwise.
Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, · line 123

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -0
+	 */
+	if (system_supports_poe() || system_supports_gcs())
+		dev_warn_once(master->smmu->dev, "SVA devices ignore permission overlays and GCS\n");

If POE tightens the page permissions, enabling SVA on top lets the application easily bypass the POE protection. Could we actually return false in arm_smmu_sva_supported() instead of displaying a warning that likely no one will notice?

The difficulty with both features is that they're always unconditionally *enabled* if present, but the SVA concern only arises if and when they are *actively used*, and that can be a transient thing. It doesn't seem reasonable to effectively make SVA unusable on newer CPUs (or at least force users to boot with MMFR overrides to lose GCS/POE if they want to use SVA), especially when it's far from clear how bothered anyone's really going to be about this anyway. I just think we should do at least *something* to remember and acknowledge that certain CPU features impact SVA too.

Quoted 1 line

I guess enforcing !(SVA & POE) on an address space would be too much work?

Yes, to attempt to do this "properly" I believe we'd need to keep track of how many SVA attachments, overlays, and GCS pages (if the SMMU can't support S1PIE) a process has at any given point in time, then check those in all 3 places to deny individual usage calls if the "opposing" feature is currently in use.

Thanks,
Robin.

Quoted 24 lines

Thanks,
Jean

Quoted 20 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +5 -1
  }
  EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_sva_cd);
 
@@ -206,8 +215,12 @@ bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
  	unsigned long asid_bits;
  	u32 feat_mask = ARM_SMMU_FEAT_COHERENCY;
 
-	if (vabits_actual == 52)
+	if (vabits_actual == 52) {
+		/* We don't support LPA2 */
+		if (PAGE_SIZE != SZ_64K)
+			return false;
  		feat_mask |= ARM_SMMU_FEAT_VAX;
+	}
 
  	if ((smmu->features & feat_mask) != feat_mask)
  		return false;
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
maintainer Applied
#10 lore raw

Re: (subset) [PATCH 1/2] iommu/arm-smmu-v3: Document SVA interaction with new pagetable features

In reply to Robin Murphy
Quoted 14 lines · Robin Murphy

Process pagetables may now be using new permission-indirection-based features which an SMMU may not understand when given such a table for SVA. Although SMMUv3.4 does add its own S1PIE feature, realistically we're still going to have to cope with feature mismatches between CPUs and SMMUs, so let's start simple and essentially just document the expectations for what falls out as-is. Although it seems unlikely for SVA applications to also depend on memory-hardening features, or vice-versa, the relative lifecycles make it tricky to enforce mutual exclusivity. Thankfully our PIE index allocation makes it relatively benign for an SMMU to keep interpreting them as direct permissions, the only real implication is that an SVA application cannot harden itself against its own devices with these features. Thus, inform the user about that just in case they have other expectations.

Quoted 1 line

[...]

Applied to arm64 (for-next/fixes), thanks!

[2/2] arm64: cpufeature: Add GCS to cpucap_is_possible()

      https://git.kernel.org/arm64/c/f00b53f1614f
Signature · 1 line
Catalin
maintainer
#11 lore raw
In reply to Robin Murphy
Quoted 14 lines · Robin Murphy

Process pagetables may now be using new permission-indirection-based features which an SMMU may not understand when given such a table for SVA. Although SMMUv3.4 does add its own S1PIE feature, realistically we're still going to have to cope with feature mismatches between CPUs and SMMUs, so let's start simple and essentially just document the expectations for what falls out as-is. Although it seems unlikely for SVA applications to also depend on memory-hardening features, or vice-versa, the relative lifecycles make it tricky to enforce mutual exclusivity. Thankfully our PIE index allocation makes it relatively benign for an SMMU to keep interpreting them as direct permissions, the only real implication is that an SVA application cannot harden itself against its own devices with these features. Thus, inform the user about that just in case they have other expectations.

Quoted 1 line

[...]

Applied SMMU change to will (for-joerg/arm-smmu/updates), thanks!

[1/2] iommu/arm-smmu-v3: Document SVA interaction with new pagetable features

      https://git.kernel.org/will/c/6e192214c6c8

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev