Skip to contentSkip to conversation

KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode

PATCH Superseded Amit Machhiwal 3 participants 4 messages Jun 8 – Jun 9

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode

On IBM POWER systems, newer processor generations can operate in compatibility modes corresponding to earlier generations. This becomes relevant for nested virtualization, where nested KVM guests may need to run with a specific processor compatibility level.

Currently, when running a nested KVM guest (L2) inside a Power11 pSeries logical partition (L1) booted in Power10 compatibility mode, the guest fails to boot while setting 'arch_compat'. This happens because the CPU class is derived from the hardware PVR (via mfspr()), which reflects the physical processor generation (Power11), rather than the effective compatibility mode (Power10).

As a result, userspace may request a Power11 arch_compat for the L2 guest. However, the L1 partition, running in Power10 compatibility, has only negotiated support up to Power10 with the Power Hypervisor (L0). When H_GUEST_SET_STATE is invoked with a Power11 Logical PVR, the hypervisor rejects the request, leading to a late guest boot failure:

KVM-NESTEDv2: couldn't set guest wide elements
[..KVM reg dump..]

This situation should be detected earlier and rejected by KVM. Without proper validation, if userspace ignores the error, the guest may continue to boot in Power11 raw mode on a Power10 compatibility host, which should not be allowed.

Introduce a validation mechanism that detects unsupported arch_compat values early in the guest initialization path. When an unsupported arch_compat is requested (e.g., Power11 on a Power10 compatibility mode host), kvmppc_set_arch_compat() uses cpu_has_feature(CPU_FTR_P11_PVR) to detect the mismatch and sets arch_compat to PVR_ARCH_INVALID. This triggers kvmppc_sanity_check() to mark the vCPU as invalid by setting vcpu->arch.sane to false. On the next vCPU run, kvmppc_vcpu_run_hv() checks this flag and returns -EINVAL, preventing the guest from running with an invalid processor compatibility configuration.

With this, when a Power11 arch_compat is requested on a Power10 compatibility mode host, the guest fails early during boot with:

error: kvm run failed Invalid argument

This provides a much clearer failure mode compared to the previous behavior where the guest could boot in Power11 raw mode (if userspace ignored the error) or fail late during H_GUEST_SET_STATE.

---
Changes in v2:
* Fixed issue where v1 allowed guest to boot in Power11 raw mode when userspace ignored the error, by adding validation in kvmppc_sanity_check() to ensure early failure during vCPU run [Found the issue after posting v1, also reported by Gautam.]
* Introduced PVR_ARCH_INVALID constant for marking invalid arch_compat
* Dropped all Reviewed-by and Tested-by tags due to code changes; requesting fresh reviews
* v1: https://lore.kernel.org/all/[email protected]/

Changes in v1:
* Moved this patch out of the v3 series [1] as discussed here [2]
* Addressed below review comments from Ritesh:
- Based the PVR validation on cpu features
- Fixed hcall name typo
- Stable backport

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/include/asm/reg.h |  1 +
 arch/powerpc/kvm/book3s_hv.c   | 15 ++++++++++++++-
 arch/powerpc/kvm/powerpc.c     |  3 +++
 3 files changed, 18 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/reg.h +1 -0
index 3449dd2b577d..7472b9522f71 100644
@@ -1356,6 +1356,7 @@
 #define PVR_ARCH_300	0x0f000005
 #define PVR_ARCH_31	0x0f000006
 #define PVR_ARCH_31_P11	0x0f000007
+#define PVR_ARCH_INVALID	0xffffffff
 
 /* Macros for setting and retrieving special purpose registers */
 #ifndef __ASSEMBLER__
arch/powerpc/kvm/book3s_hv.c +14 -1
index 61dbeea317f3..f9380ef65750 100644
@@ -446,7 +446,19 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
 			guest_pcr_bit = PCR_ARCH_300;
 			break;
 		case PVR_ARCH_31:
+			guest_pcr_bit = PCR_ARCH_31;
+			break;
 		case PVR_ARCH_31_P11:
+			/*
+			 * Need to check this for ISA 3.1, as Power10 and
+			 * Power11 share the same PCR. For any subsequent ISA
+			 * versions, this will be taken care of by the guest vs
+			 * host PCR comparison below.
+			 */
+			if (!cpu_has_feature(CPU_FTR_P11_PVR)) {
+				arch_compat = PVR_ARCH_INVALID;
+				goto out;
+			}
 			guest_pcr_bit = PCR_ARCH_31;
 			break;
 		default:
@@ -469,6 +481,7 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
 			return -EINVAL;
 	}
 
+out:
 	spin_lock(&vc->lock);
 	vc->arch_compat = arch_compat;
 	kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LOGICAL_PVR);
@@ -479,7 +492,7 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
 	vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
 	spin_unlock(&vc->lock);
 
-	return 0;
+	return kvmppc_sanity_check(vcpu);
 }
 
 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
arch/powerpc/kvm/powerpc.c +3 -0
index 00302399fc37..7e2d489af642 100644
@@ -258,6 +258,9 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
 	if (!vcpu->arch.pvr)
 		goto out;
 
+	if (vcpu->arch.vcore->arch_compat == PVR_ARCH_INVALID)
+		goto out;
+
 	/* PAPR only works with book3s_64 */
 	if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
 		goto out;

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Unconditional dereference of `vcpu->arch.vcore` in `kvmppc_sanity_check()` causes a deterministic…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unconditional dereference of vcpu->arch.vcore in kvmppc_sanity_check() causes a deterministic kernel panic for Book3S PR and BookE KVM guests.
- [Medium] Bypassing PCR capability checks corrupts shared VCORE state on older architectures and inconsistently blocks sibling VCPUs. --

commit 8aae14bd53c26cd9bec6b3a9d75dc94d32e6e296
Author: Amit Machhiwal <[email protected]>

KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode

This commit detects unsupported arch_compat values early in the guest initialization path. It sets arch_compat to PVR_ARCH_INVALID and marks the vCPU as invalid, preventing the guest from running with an invalid processor compatibility configuration.

Cc [email protected] # v6.13+
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +0 -0
index 61dbeea317f3b..f9380ef657500 100644

[ ... ]

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +2 -0
@@ -446,7 +446,19 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
 			guest_pcr_bit = PCR_ARCH_300;
 			break;
 		case PVR_ARCH_31:
+			guest_pcr_bit = PCR_ARCH_31;
+			break;

arch/powerpc/kvm/book3s_hv.c static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) · line 460

Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +9 -0
 		case PVR_ARCH_31_P11:
+			/*
+			 * Need to check this for ISA 3.1, as Power10 and
+			 * Power11 share the same PCR. For any subsequent ISA
+			 * versions, this will be taken care of by the guest vs
+			 * host PCR comparison below.
+			 */
+			if (!cpu_has_feature(CPU_FTR_P11_PVR)) {
+				arch_compat = PVR_ARCH_INVALID;
+				goto out;

[Severity: Medium]
When jumping to the out label, the code applies the invalid arch_compat and a restricted pcr to the shared vcore structure before returning an error. Since vcore can be shared among multiple vCPUs on older Power architectures, does this state affect sibling vCPUs?

While kvmppc_sanity_check() marks the current vCPU as insane, sibling vCPUs might retain sane == true. Could they then execute the guest using the updated pcr and invalid arch_compat state?

Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +1 -0
+			}
 			guest_pcr_bit = PCR_ARCH_31;
 			break;
 		default:

[ ... ]

arch/powerpc/kvm/powerpc.c int kvmppc_sanity_check(struct kvm_vcpu *vcpu) · line 263

Quoted 11 lines
arch/powerpc/kvm/powerpc.c +3 -0
index 00302399fc37b..7e2d489af6423 100644
@@ -258,6 +258,9 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
 	if (!vcpu->arch.pvr)
 		goto out;
 
+	if (vcpu->arch.vcore->arch_compat == PVR_ARCH_INVALID)
+		goto out;
+

[Severity: High]
Does this code introduce a null pointer dereference for Book3S PR and BookE guests?

kvmppc_sanity_check() is a generic helper used across Book3S HV, PR, and BookE KVM implementations. While vcpu->arch.vcore is populated for Book3S HV, it remains null for Book3S PR and BookE guests.

During vCPU creation for PR or BookE, the initialization sequence sets vcpu->arch.pvr and calls kvmppc_sanity_check(). Because pvr is non-zero, execution will reach this check and dereference the null vcore pointer, which can cause a host panic.

Quoted 3 lines
arch/powerpc/kvm/powerpc.c +0 -0
 	/* PAPR only works with book3s_64 */
 	if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
 		goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#3 lore raw
In reply to Amit Machhiwal

Amit Machhiwal <[email protected]> writes:

<snip>

Quoted 15 lines

Introduce a validation mechanism that detects unsupported arch_compat values early in the guest initialization path. When an unsupported arch_compat is requested (e.g., Power11 on a Power10 compatibility mode host), kvmppc_set_arch_compat() uses cpu_has_feature(CPU_FTR_P11_PVR) to detect the mismatch and sets arch_compat to PVR_ARCH_INVALID. This triggers kvmppc_sanity_check() to mark the vCPU as invalid by setting vcpu->arch.sane to false. On the next vCPU run, kvmppc_vcpu_run_hv() checks this flag and returns -EINVAL, preventing the guest from running with an invalid processor compatibility configuration.

With this, when a Power11 arch_compat is requested on a Power10 compatibility mode host, the guest fails early during boot with:

error: kvm run failed Invalid argument

<snip>

Quoted 4 lines

Thanks Amit for addressing the issue reported on v1. The v2 patch changes look aligned to what we discussed offline. Hence,

Signature · 2 lines
Cheers
~ Vaibhav
#2 lore raw
In reply to [email protected]
Quoted 36 lines · On 2026/06/08 08:27 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unconditional dereference of vcpu->arch.vcore in kvmppc_sanity_check() causes a deterministic kernel panic for Book3S PR and BookE KVM guests.
- [Medium] Bypassing PCR capability checks corrupts shared VCORE state on older architectures and inconsistently blocks sibling VCPUs. --

commit 8aae14bd53c26cd9bec6b3a9d75dc94d32e6e296
Author: Amit Machhiwal <[email protected]>

KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode

This commit detects unsupported arch_compat values early in the guest initialization path. It sets arch_compat to PVR_ARCH_INVALID and marks the vCPU as invalid, preventing the guest from running with an invalid processor compatibility configuration.

Cc [email protected] # v6.13+
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +0 -0
index 61dbeea317f3b..f9380ef657500 100644

[ ... ]

Quoted 14 lines
arch/powerpc/kvm/book3s_hv.c +9 -0
@@ -446,7 +446,19 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
 			guest_pcr_bit = PCR_ARCH_300;
 			break;
 		case PVR_ARCH_31:
+			guest_pcr_bit = PCR_ARCH_31;
+			break;
 		case PVR_ARCH_31_P11:
+			/*
+			 * Need to check this for ISA 3.1, as Power10 and
+			 * Power11 share the same PCR. For any subsequent ISA
+			 * versions, this will be taken care of by the guest vs
+			 * host PCR comparison below.
+			 */
+			if (!cpu_has_feature(CPU_FTR_P11_PVR)) {
Quoted 8 lines

arch/powerpc/kvm/book3s_hv.c static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) · line 460

Quoted 2 lines
arch/powerpc/kvm/book3s_hv.c +2 -0
+				arch_compat = PVR_ARCH_INVALID;
+				goto out;

[Severity: Medium]
When jumping to the out label, the code applies the invalid arch_compat and a restricted pcr to the shared vcore structure before returning an error. Since vcore can be shared among multiple vCPUs on older Power architectures, does this state affect sibling vCPUs?

Setting arch_compat to PVR_ARCH_INVALID is intentional. The value is stored in the shared vcore so that all vCPUs associated with the VM observe the invalid compatibility state. Subsequent calls to kvmppc_sanity_check() fail and prevent any vCPU from entering the guest.

Quoted 26 lines

While kvmppc_sanity_check() marks the current vCPU as insane, sibling vCPUs might retain sane == true. Could they then execute the guest using the updated pcr and invalid arch_compat state?

Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +1 -0
+			}
 			guest_pcr_bit = PCR_ARCH_31;
 			break;
 		default:

[ ... ]

arch/powerpc/kvm/powerpc.c int kvmppc_sanity_check(struct kvm_vcpu *vcpu) · line 263

Quoted 11 lines
arch/powerpc/kvm/powerpc.c +3 -0
index 00302399fc37b..7e2d489af6423 100644
@@ -258,6 +258,9 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
 	if (!vcpu->arch.pvr)
 		goto out;
 
+	if (vcpu->arch.vcore->arch_compat == PVR_ARCH_INVALID)
+		goto out;
+

[Severity: High]
Does this code introduce a null pointer dereference for Book3S PR and BookE guests?

Quoted 3 lines

kvmppc_sanity_check() is a generic helper used across Book3S HV, PR, and BookE KVM implementations. While vcpu->arch.vcore is populated for Book3S HV, it remains null for Book3S PR and BookE guests.

Good catch! I see vcore is only set for KVM-HV and may cause a null deref on PR and BookE. I'll introduce a NULL check in the next version.

# git grep "arch.vcore.*="
[...]
arch/powerpc/kvm/book3s_hv.c:   vcpu->arch.vcore = vcore;

Thanks,
Amit

Quoted 12 lines

During vCPU creation for PR or BookE, the initialization sequence sets vcpu->arch.pvr and calls kvmppc_sanity_check(). Because pvr is non-zero, execution will reach this check and dereference the null vcore pointer, which can cause a host panic.

Quoted 3 lines
arch/powerpc/kvm/powerpc.c +0 -0
 	/* PAPR only works with book3s_64 */
 	if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
 		goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1