KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode
Subsystems (4)
PATCH v4 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 (0xffffffff). This sentinel value is architecturally safe: PAPR specifies that valid logical PVR values must have 0x0f as the first byte, ensuring 0xffffffff lies permanently outside the specification-defined range. Setting this value 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.
---
Testing: Both Anushree and I have tested the below scenarios:
1. P11 guest on P11 host - Works
2. P10 compat guest on P11 host - Works
3. P11 guest on compat-P10 host - Correctly fails with "Invalid argument"
4. P10 guest on compat-P10 host - Works
Changes in v4:
* Added documentation for PVR_ARCH_INVALID explaining why 0xffffffff is architecturally safe to use as a sentinel value (PAPR constraint on first byte being 0x0f) [Ritesh]
* Updated commit message
* v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
* Fixed null pointer dereference in kvmppc_sanity_check(): added check for vcpu->arch.vcore before accessing arch_compat, as vcore is NULL for Book3S PR and BookE guests (only Book3S HV uses vcore) [Reported by Sashiko AI]
* Added Reviewed-by tag from Vaibhav
* v2: https://lore.kernel.org/all/[email protected]/
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 | 12 ++++++++++++ arch/powerpc/kvm/book3s_hv.c | 15 ++++++++++++++- arch/powerpc/kvm/powerpc.c | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/reg.h +12 -0
@@ -1357,6 +1357,18 @@#define PVR_ARCH_31 0x0f000006#define PVR_ARCH_31_P11 0x0f000007 +/* +* Kernel-internal sentinel for invalid processor compatibility modes. +* PAPR specifies that the first byte of a valid logical PVR value is +* 0x0f. So 0xffffffff lies permanently outside the PAPR-defined range +* and is safe to repurpose. KVM stores it in vcpu->arch.arch_compat +* when userspace requests an unsupported compatibility mode (e.g., +* Power11 PVR on a Power11 host booted in Power10 compat). +* kvmppc_sanity_check() detects this and prevents the vCPU from +* running with an unsupported arch_compat. +*/ +#define PVR_ARCH_INVALID 0xffffffff +/* Macros for setting and retrieving special purpose registers */ #ifndef __ASSEMBLER__
arch/powerpc/kvm/book3s_hv.c +14 -1
@@ -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 +4 -0
@@ -258,6 +258,10 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)if (!vcpu->arch.pvr) goto out; +if (vcpu->arch.vcore && + 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: 6b5a2b7d9bc156e505f09e698d85d6a1547c1206
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Setting an invalid arch_compat bypasses capability checks and poisons the shared vcore state, but…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Setting an invalid arch_compat bypasses capability checks and poisons the shared vcore state, but only marks the calling vCPU as insane, allowing sibling vCPUs to exploit the invalid state.
- [High] The patch breaks the KVM kernel build for BookE and non-Book3S PowerPC configurations by dereferencing an incomplete type struct kvmppc_vcore.
--
commit c3e328e2b42077592c9f8abacefc77f063b6a36c
Author: Amit Machhiwal <[email protected]>
KVM: PPC: Book3S HV: Validate arch_compat against host compatibility mode
This commit introduces a validation mechanism to detect and reject unsupported arch_compat values during guest initialization on PowerPC Book3S HV. It uses a sentinel value to mark mismatched compatibility configurations and updates kvmppc_sanity_check() to prevent the vCPU from running.
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +0
-0
[ ... ]
Quoted 1 line
arch/powerpc/kvm/book3s_hv.c +0
-0@@ -446,7 +446,19 @@ static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
[ ... ]
Quoted 23 lines
arch/powerpc/kvm/book3s_hv.c +11
-0case 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 );
arch/powerpc/kvm/book3s_hv.c static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) · line 495
Quoted 5 lines
arch/powerpc/kvm/book3s_hv.c +1
-1@@ -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);
[Severity: High]
If we set arch_compat to PVR_ARCH_INVALID and take the goto out path, does this bypass the guest_pcr_bit > host_pcr_bit check and persist the sentinel value into the shared vc->arch_compat?
Since kvmppc_sanity_check() is only called for the current vcpu, could sibling vCPUs sharing the same vcore bypass the sanity check and run with this corrupted vcore state?
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c +0
-0}static void kvmppc_dump_regs (struct kvm_vcpu *vcpu)arch/powerpc/kvm/powerpc.c +0
-0
arch/powerpc/kvm/powerpc.c int kvmppc_sanity_check(struct kvm_vcpu *vcpu) · line 264
Quoted 7 lines
arch/powerpc/kvm/powerpc.c +4
-0@@ -258,6 +258,10 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)if (!vcpu->arch.pvr) goto out; +if (vcpu->arch.vcore && + vcpu->arch.vcore->arch_compat ==PVR_ARCH_INVALID ) + goto out; +
[Severity: High]
Does this break the build for BookE and non-Book3S configurations?
kvmppc_sanity_check() is common code located in arch/powerpc/kvm/powerpc.c, but struct kvmppc_vcore is only defined in asm/kvm_book3s.h.
If a BookE environment compiles this, won't dereferencing vcpu->arch.vcore->arch_compat result in an incomplete type error?
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
Quoted 54 lines · Amit Machhiwal
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 (viamfspr()), which reflects the physical processor generation (Power11), rather than the effective compatibility mode (Power10).As a result, userspace may request a Power11
arch_compatfor the L2 guest. However, the L1 partition, running in Power10 compatibility, has only negotiated support up to Power10 with the Power Hypervisor (L0). WhenH_GUEST_SET_STATEis 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_compatvalues early in the guest initialization path. When an unsupportedarch_compatis requested (e.g., Power11 on a Power10 compatibility mode host),kvmppc_set_arch_compat()usescpu_has_feature(CPU_FTR_P11_PVR)to detect the mismatch and setsarch_compattoPVR_ARCH_INVALID(0xffffffff). This sentinel value is architecturally safe: PAPR specifies that valid logical PVR values must have0x0fas the first byte, ensuring0xfffffffflies permanently outside the specification-defined range. Setting this value triggerskvmppc_sanity_check()to mark the vCPU as invalid by settingvcpu->arch.saneto 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_compatis 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.Suggested-by Vaibhav Jain <[email protected]>Reviewed-by Vaibhav Jain <[email protected]>Tested-by Anushree Mathur <[email protected]>Acked-by Gautam Menghani <[email protected]>Cc [email protected] # v6.13+Signed-off-by Amit Machhiwal <[email protected]>
Quoted 7 lines
---
Testing: Both Anushree and I have tested the below scenarios:
1. P11 guest on P11 host - Works
2. P10 compat guest on P11 host - Works
3. P11 guest on compat-P10 host - Correctly fails with "Invalid argument"
4. P10 guest on compat-P10 host - Works
Thanks for incorporating all the changes and adding the test result matrix in the changelog.
The changes looks good, feel free to add:
Quoted 110 lines · Amit Machhiwal
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 (viamfspr()), which reflects the physical processor generation (Power11), rather than the effective compatibility mode (Power10).As a result, userspace may request a Power11
arch_compatfor the L2 guest. However, the L1 partition, running in Power10 compatibility, has only negotiated support up to Power10 with the Power Hypervisor (L0). WhenH_GUEST_SET_STATEis 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_compatvalues early in the guest initialization path. When an unsupportedarch_compatis requested (e.g., Power11 on a Power10 compatibility mode host),kvmppc_set_arch_compat()usescpu_has_feature(CPU_FTR_P11_PVR)to detect the mismatch and setsarch_compattoPVR_ARCH_INVALID(0xffffffff). This sentinel value is architecturally safe: PAPR specifies that valid logical PVR values must have0x0fas the first byte, ensuring0xfffffffflies permanently outside the specification-defined range. Setting this value triggerskvmppc_sanity_check()to mark the vCPU as invalid by settingvcpu->arch.saneto 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_compatis 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.Suggested-by Vaibhav Jain <[email protected]>Reviewed-by Vaibhav Jain <[email protected]>Tested-by Anushree Mathur <[email protected]>Acked-by Gautam Menghani <[email protected]>Cc [email protected] # v6.13+Signed-off-by Amit Machhiwal <[email protected]>---
Testing: Both Anushree and I have tested the below scenarios:
1. P11 guest on P11 host - Works
2. P10 compat guest on P11 host - Works
3. P11 guest on compat-P10 host - Correctly fails with "Invalid argument"
4. P10 guest on compat-P10 host - WorksChanges in v4:
* Added documentation forPVR_ARCH_INVALIDexplaining why0xffffffffis architecturally safe to use as a sentinel value (PAPR constraint on first byte being0x0f) [Ritesh]
* Updated commit message
* v3: https://lore.kernel.org/all/[email protected]/Changes in v3:
* Fixed null pointer dereference inkvmppc_sanity_check(): added check forvcpu->arch.vcorebefore accessingarch_compat, as vcore is NULL for Book3S PR and BookE guests (only Book3S HV uses vcore) [Reported by Sashiko AI]
* Added Reviewed-by tag from Vaibhav
* v2: https://lore.kernel.org/all/[email protected]/Changes in v2:
* Fixed issue where v1 allowed guest to boot in Power11 raw mode when userspace ignored the error, by adding validation inkvmppc_sanity_check()to ensure early failure during vCPU run [Found the issue after posting v1, also reported by Gautam.]
* IntroducedPVR_ARCH_INVALIDconstant for marking invalidarch_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 | 12 ++++++++++++ arch/powerpc/kvm/book3s_hv.c | 15 ++++++++++++++- arch/powerpc/kvm/powerpc.c | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-)arch/powerpc/include/asm/reg.h +2
-0@@ -1357,6 +1357,18 @@#define PVR_ARCH_31 0x0f000006#define PVR_ARCH_31_P11 0x0f000007 +/* +* Kernel-internal sentinel for invalid processor compatibility modes.
arch/powerpc/include/asm/reg.h line 1371
Quoted 10 lines
arch/powerpc/include/asm/reg.h +10
-0+* PAPR specifies that the first byte of a valid logical PVR value is +* 0x0f. So 0xffffffff lies permanently outside the PAPR-defined range +* and is safe to repurpose. KVM stores it in vcpu->arch.arch_compat +* when userspace requests an unsupported compatibility mode (e.g., +* Power11 PVR on a Power11 host booted in Power10 compat). +* kvmppc_sanity_check() detects this and prevents the vCPU from +* running with an unsupported arch_compat. +*/ +#define PVR_ARCH_INVALID 0xffffffff +
nit:
I think the description of the invalid value should go into the commit message rather then here. It creates an unnecessary clutter here. If anyone wants to know why it's added they can always get the blame and get the description.
Apart from this.
Quoted 64 lines
arch/powerpc/include/asm/reg.h +0
-0/* Macros for setting and retrieving special purpose registers */ #ifndef __ASSEMBLER__arch/powerpc/kvm/book3s_hv.c +14
-1@@ -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 +4
-0@@ -258,6 +258,10 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)if (!vcpu->arch.pvr) goto out; +if (vcpu->arch.vcore && + 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:
6b5a2b7d9bc156e505f09e698d85d6a1547c1206Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 127 lines · Mukesh Kumar Chaurasiya
Quoted 110 lines · Amit Machhiwal
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 (viamfspr()), which reflects the physical processor generation (Power11), rather than the effective compatibility mode (Power10).As a result, userspace may request a Power11
arch_compatfor the L2 guest. However, the L1 partition, running in Power10 compatibility, has only negotiated support up to Power10 with the Power Hypervisor (L0). WhenH_GUEST_SET_STATEis 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_compatvalues early in the guest initialization path. When an unsupportedarch_compatis requested (e.g., Power11 on a Power10 compatibility mode host),kvmppc_set_arch_compat()usescpu_has_feature(CPU_FTR_P11_PVR)to detect the mismatch and setsarch_compattoPVR_ARCH_INVALID(0xffffffff). This sentinel value is architecturally safe: PAPR specifies that valid logical PVR values must have0x0fas the first byte, ensuring0xfffffffflies permanently outside the specification-defined range. Setting this value triggerskvmppc_sanity_check()to mark the vCPU as invalid by settingvcpu->arch.saneto 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_compatis 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.Suggested-by Vaibhav Jain <[email protected]>Reviewed-by Vaibhav Jain <[email protected]>Tested-by Anushree Mathur <[email protected]>Acked-by Gautam Menghani <[email protected]>Cc [email protected] # v6.13+Signed-off-by Amit Machhiwal <[email protected]>---
Testing: Both Anushree and I have tested the below scenarios:
1. P11 guest on P11 host - Works
2. P10 compat guest on P11 host - Works
3. P11 guest on compat-P10 host - Correctly fails with "Invalid argument"
4. P10 guest on compat-P10 host - WorksChanges in v4:
* Added documentation forPVR_ARCH_INVALIDexplaining why0xffffffffis architecturally safe to use as a sentinel value (PAPR constraint on first byte being0x0f) [Ritesh]
* Updated commit message
* v3: https://lore.kernel.org/all/[email protected]/Changes in v3:
* Fixed null pointer dereference inkvmppc_sanity_check(): added check forvcpu->arch.vcorebefore accessingarch_compat, as vcore is NULL for Book3S PR and BookE guests (only Book3S HV uses vcore) [Reported by Sashiko AI]
* Added Reviewed-by tag from Vaibhav
* v2: https://lore.kernel.org/all/[email protected]/Changes in v2:
* Fixed issue where v1 allowed guest to boot in Power11 raw mode when userspace ignored the error, by adding validation inkvmppc_sanity_check()to ensure early failure during vCPU run [Found the issue after posting v1, also reported by Gautam.]
* IntroducedPVR_ARCH_INVALIDconstant for marking invalidarch_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 | 12 ++++++++++++ arch/powerpc/kvm/book3s_hv.c | 15 ++++++++++++++- arch/powerpc/kvm/powerpc.c | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-)arch/powerpc/include/asm/reg.h +2
-0@@ -1357,6 +1357,18 @@#define PVR_ARCH_31 0x0f000006#define PVR_ARCH_31_P11 0x0f000007 +/* +* Kernel-internal sentinel for invalid processor compatibility modes. arch/powerpc/include/asm/reg.h line 1371
Quoted 10 lines
arch/powerpc/include/asm/reg.h +10
-0+* PAPR specifies that the first byte of a valid logical PVR value is +* 0x0f. So 0xffffffff lies permanently outside the PAPR-defined range +* and is safe to repurpose. KVM stores it in vcpu->arch.arch_compat +* when userspace requests an unsupported compatibility mode (e.g., +* Power11 PVR on a Power11 host booted in Power10 compat). +* kvmppc_sanity_check() detects this and prevents the vCPU from +* running with an unsupported arch_compat. +*/ +#define PVR_ARCH_INVALID 0xffffffff +nit:
I think the description of the invalid value should go into the commit message rather then here. It creates an unnecessary clutter here. If anyone wants to know why it's added they can always get the blame and get the description.
Quoted 3 lines
Apart from this.
I just saw the conversation on previous version. After that it makes sense to have it here.
Regards,
Mukesh
Quoted 64 lines
Quoted 64 lines
arch/powerpc/include/asm/reg.h +0
-0/* Macros for setting and retrieving special purpose registers */ #ifndef __ASSEMBLER__arch/powerpc/kvm/book3s_hv.c +14
-1@@ -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 +4
-0@@ -258,6 +258,10 @@ int kvmppc_sanity_check(struct kvm_vcpu *vcpu)if (!vcpu->arch.pvr) goto out; +if (vcpu->arch.vcore && + 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:
6b5a2b7d9bc156e505f09e698d85d6a1547c1206Signature · 1 line
2.50.1 (Apple Git-155)