KVM: PPC: Expose CPU compatibility modes for nested guests
Subsystems (4)
v4 cover KVM: PPC: Expose CPU compatibility modes for nested guests
On POWER systems, newer processor generations can operate in compatibility modes corresponding to earlier generations (e.g., a Power11 system running in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
This creates a problem for nested virtualization. When booting a nested KVM guest (L2) inside a host KVM guest (L1) running in a compatibility mode, userspace (e.g., QEMU) may derive the CPU model from the raw hardware PVR and attempt to configure the nested guest accordingly. However, the L1 partition is constrained by the compatibility level negotiated with the hypervisor (L0), and requests exceeding that level are rejected, leading to guest boot failures such as:
KVM-NESTEDv2: couldn't set guest wide elements
This series provides a mechanism for userspace to query the effective CPU compatibility modes supported by the host, so it can select an appropriate CPU model for nested guests.
To achieve this, the series introduces a new KVM capability and ioctl (KVM_CAP_PPC_COMPAT_CAPS / KVM_PPC_GET_COMPAT_CAPS) that expose the compatibility modes supported by the host.
Why a new UAPI?
---------------
While cpu-version is available in /proc/device-tree/cpus/<cpu#>/cpu-version on both L1 booted on PowerNV and PowerVM LPARs, the UAPI approach is preferable for several reasons:
1. pHYP (L0) capabilities: On PowerVM, we need to rely on capabilities negotiated with pHYP in KVM, not just device tree properties. The cpu-version property depicts the current compat mode but doesn't point to what all compat modes are supported for the nested guest.
2. procfs dependency: Not all systems run with procfs enabled (CONFIG_PROC_FS is optional). Minimal configurations like buildroot might disable it, but KVM ioctl works regardless since it accesses kernel data structures directly.
3. Kernel validation: The kernel validates and normalizes the compatibility information, ensuring userspace gets validated, consistent data.
4. Abstraction & stability: /proc/device-tree is an implementation detail. The UAPI provides a stable interface that won't break if the underlying mechanism changes.
5. Semantic clarity: KVM_PPC_GET_COMPAT_CAPS clearly expresses what compatibility modes can be used for KVM guests, vs. parsing device tree which requires understanding the semantic meaning of cpu-version.
The implementation supports both:
- PowerVM (nested API v2), where compatibility information is obtained via the H_GUEST_GET_CAPABILITIES hypercall.
- PowerNV (nested API v1), where compatibility is derived from the device tree ("cpu-version") representing the effective processor compatibility level.
This allows userspace (e.g., QEMU) to select a CPU model consistent with the host compatibility mode, avoiding mismatches and enabling successful nested guest boot.
Changes in v4:
- Added 'size' field to struct kvm_ppc_compat_caps for forward compatibility and ABI extensibility
- Implemented size validation in ioctl handler to ensure correct structure size from userspace
- Introduced KVM-specific capability constants (KVM_PPC_COMPAT_CAP_POWER9/ 10/11) instead of exposing hypervisor-internal H_GUEST_CAP_* constants
- Added capability masking using KVM_PPC_COMPAT_BITMASK to ensure only supported processor modes are exposed
- Enhanced error handling with comprehensive error codes (EINVAL, EFAULT, ENOTTY) and detailed documentation
- Removed Tested-by tags pending re-testing with v4 changes
- Separated validation patch (patch 1 from v3) and sent independently [1]
Note: This series is built on top of patches [1] and [2] which must be applied first. Patch [1] ensures arch_compat is validated against the host compatibility mode before this series adds the capability query mechanism. Patch [2] sets CPU_FTR_P11_PVR for Power11 and later processors, which is needed for proper CPU feature detection in dt-cpu-ftrs environments.
Changes in v3:
- Added "Why a new UAPI?" section to cover letter addressing questions about the need for a new UAPI vs. using existing mechanisms like /proc/device-tree
- Fixed initialization of 'r' in KVM_PPC_GET_COMPAT_CAPS ioctl handler from 0 to -ENOTTY for proper error handling when the operation is not supported
- Added Vaibhav's "Suggested-by" tags
- Have retained Anushree's "Tested-by" tags as no major code changes
- Fixed documentation build warning reported by kernel test robot and added "Reported-by" and "Closes" tags to patch 5
Changes in v2:
- Squashed patches 2 and 3 from v1 (capability introduction and ioctl wiring) into a single patch for better logical grouping
- Changed kvm_ppc_compat_caps.flags from __u32 to __u64 for consistency and future extensibility
- Addressed other review comments
- Improved commit messages with clearer explanations of the changes
Patch summary:
[1/4] Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl [2/4] Implement capability retrieval for PowerVM (API v2) [3/4] Add PowerNV support (API v1)
[4/4] Document the new ioctl
Testing (with QEMU v3 patches and on top of patches [1] and [2]):
KVM APIv1 Testing
=================
On P10 PowerNV machine (L0)
---------------------------
- P10 L1 KVM guest -> works
- P10 nested L2 KVM guest -> works
- P9 compat nested L2 KVM guest -> works
- P9 compat L1 KVM guest -> works
- P9 nested L2 KVM guest -> works
On Powernv11 TCG Guest (L0)
---------------------------
- P11 L1 KVM guest -> works
- P11 L2 KVM guest -> works
- P10 compat L1 KVM guest -> works
- P10 L2 KVM guest -> works
- P9 compat L1 KVM guest -> works
- P9 L2 KVM guest -> works
KVM APIv2 Testing
=================
On P11 PowerVM LPAR (L1)
------------------------
- P11 L2 KVM guest -> works
- P10 compat L2 KVM guest -> works
On P11 LPAR in P10 compat (L1)
------------------------------
- P10 (host compat) L2 KVM guest -> works
On P10 PowerVM LPAR (L1)
------------------------
- P10 L2 KVM guest -> works
With this series, nested guests boot successfully in configurations where they previously failed due to compatibility mismatches.
Related QEMU series:
--------------------
A corresponding QEMU v3 series adds support for querying and using these compatibility capabilities when configuring nested KVM guests:
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Previous versions:
------------------
v3: https://lore.kernel.org/linuxppc-dev/[email protected]/
v2: https://lore.kernel.org/linuxppc-dev/[email protected]/
v1: https://lore.kernel.org/linuxppc-dev/[email protected]/
References:
-----------
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Amit Machhiwal (4):
KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM
on PowerVM
KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM
on PowerNV
KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
Documentation/virt/kvm/api.rst | 47 ++++++++++++++++++++++++++ arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/include/uapi/asm/kvm.h | 16 +++++++++ arch/powerpc/kvm/book3s_hv.c | 52 +++++++++++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 35 +++++++++++++++++++ include/uapi/linux/kvm.h | 4 +++ 6 files changed, 155 insertions(+)
base-commit: 8b308f96484e37d92d2fc6b72b091f60496c000e
prerequisite-patch-id: ce9521668d549f2bd731d321a38f720b789e0b4e
prerequisite-patch-id: 4662f01d2101cfae8502f04290658deed60eec26
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v4 1/4 KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
Introduce a new capability and ioctl to expose CPU compatibility modes supported by the host processor for nested guests.
On IBM POWER systems, newer processor generations (N) can operate in compatibility modes corresponding to earlier generations, like (N-1) and (N-2). This is particularly relevant for nested virtualization, where nested KVM guests may need to run with a specific processor compatibility level.
Introduce KVM_CAP_PPC_COMPAT_CAPS capability and the corresponding KVM_PPC_GET_COMPAT_CAPS vm ioctl. The ioctl returns a bitmap describing the compatibility modes supported by the host in respective bit numbers, allowing userspace (e.g., QEMU) to select an appropriate compatibility level when configuring nested KVM guests.
The ioctl handling is added in kvm_arch_vm_ioctl() and retrieves host CPU compatibility capabilities via a PowerPC-specific backend implementation when available. The implementation validates the structure size from userspace to ensure forward compatibility and returns appropriate error codes (EINVAL for invalid size, EFAULT for copy failures, ENOTTY if backend is not implemented). The struct kvm_ppc_compat_caps includes a size field to support future ABI extensions.
--- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/include/uapi/asm/kvm.h | 7 ++++++ arch/powerpc/kvm/powerpc.c | 35 +++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 4 ++++ 4 files changed, 47 insertions(+)
arch/powerpc/include/asm/kvm_ppc.h +1 -0
@@ -319,6 +319,7 @@ struct kvmppc_ops {bool (*hash_v3_possible)(void );int (*create_vm_debugfs)(struct kvm *kvm);int (*create_vcpu_debugfs)(struct kvm_vcpu *vcpu,struct dentry *debugfs_dentry); +int (*get_compat_caps)(struct kvm_ppc_compat_caps *host_caps); };extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/include/uapi/asm/kvm.h +7 -0
@@ -437,6 +437,13 @@ struct kvm_ppc_cpu_char {__u64 behaviour_mask;/* valid bits in behaviour */ }; +/* For KVM_PPC_GET_COMPAT_CAPS */ +struct kvm_ppc_compat_caps { +__u64 flags;/* Reserved for future use */ +__u64 size;/* Size of this structure */ +__u64 compat_capabilities;/* Capabilities supported by the host */ +}; +/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS.
arch/powerpc/kvm/powerpc.c +35 -0
@@ -701,6 +701,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) } }break ; +#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE ) +case KVM_CAP_PPC_COMPAT_CAPS : + r =0 ; +if (kvmhv_on_pseries ()) + r =1 ; +break ; +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ default : r =0 ;break ;
@@ -2467,6 +2474,34 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm->arch.kvm_ops->svm_off (kvm);break ; } +case KVM_PPC_GET_COMPAT_CAPS : { +struct kvm_ppc_compat_caps host_caps; +u64 user_size; + + r = -EFAULT ; +/* First, get the size field from userspace to validate */ +if (copy_from_user (&user_size, &((struct kvm_ppc_compat_caps +__user *)argp)->size,sizeof (user_size))) { + goto out; + } + +/* Validate size - must be at least the current structure size */ + r = -EINVAL ; +if (user_size <sizeof (host_caps)) + goto out; + + r = -ENOTTY ; +memset (&host_caps,0 ,sizeof (host_caps)); +if (!kvm->arch.kvm_ops->get_compat_caps) + goto out; + + r = kvm->arch.kvm_ops->get_compat_caps (&host_caps); +/* Set the actual size of the structure we're returning */ + host_caps.size =sizeof (host_caps); +if (!r &©_to_user (argp, &host_caps,sizeof (host_caps))) + r = -EFAULT ; +break ; + }default : {struct kvm *kvm = filp->private_data; r = kvm->arch.kvm_ops->arch_vm_ioctl (filp, ioctl, arg);
include/uapi/linux/kvm.h +4 -0
@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_PPC_COMPAT_CAPS 249struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps) +/* * ioctls for vcpu fds */
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [PATCH v4 1/4] KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Backward compatibility failure due to strict size check and unconditional copy size. - [High]…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Backward compatibility failure due to strict size check and unconditional copy size.
- [High] Incorrect _IOR ioctl direction flag breaks UAPI extensibility and tooling.
- [High] Unvalidated reserved flags field prevents future use as an input.
- [Medium] Capability KVM_CAP_PPC_COMPAT_CAPS is advertised even when the backend lacks support. --
commit c1e1544ff6eb8d147eda5713932bdc47b539c526
Author: Amit Machhiwal <[email protected]>
Subject: KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
This commit introduces a new KVM capability and ioctl to expose host CPU compatibility modes to nested guests. It adds the KVM_CAP_PPC_COMPAT_CAPS capability and the KVM_PPC_GET_COMPAT_CAPS ioctl, which returns a bitmap of supported compatibility modes via the kvm_ppc_compat_caps structure.
Quoted 4 lines
arch/powerpc/kvm/powerpc.c +0
-0
[ ... ]
arch/powerpc/kvm/powerpc.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 706
Quoted 11 lines
arch/powerpc/kvm/powerpc.c +7
-0@@ -697,6 +697,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) } }break ; +#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE ) +case KVM_CAP_PPC_COMPAT_CAPS : + r =0 ; +if (kvmhv_on_pseries ()) + r =1 ; +break ; +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
[Severity: Medium]
Will this falsely advertise support if the backend lacks the get_compat_caps callback?
It appears this unconditionally returns 1 when kvmhv_on_pseries() is true, even if running on a backend like KVM-PR that might not populate kvm->arch.kvm_ops->get_compat_caps.
Should this verify the backend capabilities before returning 1?
[ ... ]
Quoted 9 lines
arch/powerpc/kvm/powerpc.c +5
-0@@ -2463,6 +2470,34 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm->arch.kvm_ops->svm_off (kvm);break ; } +case KVM_PPC_GET_COMPAT_CAPS : { +struct kvm_ppc_compat_caps host_caps; +u64 user_size; + + r = -EFAULT ;
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2487
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +10
-0+/* First, get the size field from userspace to validate */ +if (copy_from_user (&user_size, &((struct kvm_ppc_compat_caps +__user *)argp)->size,sizeof (user_size))) { + goto out; + } + +/* Validate size - must be at least the current structure size */ + r = -EINVAL ; +if (user_size <sizeof (host_caps)) + goto out;
[Severity: High]
Does this strict size check prevent backward compatibility?
If the structure is extended in a future kernel, sizeof(host_caps) will increase. Older userspace binaries passing their original, smaller user_size will be rejected with -EINVAL.
Could this be resolved by accepting smaller sizes and limiting the copy_to_user() length to min(user_size, sizeof(host_caps))?
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2494
Quoted 3 lines
arch/powerpc/kvm/powerpc.c +3
-0+ + r = -ENOTTY ; +memset (&host_caps,0 ,sizeof (host_caps));
[Severity: High]
Does zeroing the structure locally without validating the user-provided flags prevent those flags from being used in the future?
Since the userspace flags aren't read and verified to be zero, applications might pass uninitialized garbage memory. If a future kernel assigns meaning to these flags, wouldn't it interpret that legacy garbage data as valid input?
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +10
-0+if (!kvm->arch.kvm_ops->get_compat_caps) + goto out; + + r = kvm->arch.kvm_ops->get_compat_caps (&host_caps); +/* Set the actual size of the structure we're returning */ + host_caps.size =sizeof (host_caps); +if (!r &©_to_user (argp, &host_caps,sizeof (host_caps))) + r = -EFAULT ; +break ; + }
[ ... ]
Quoted 4 lines
include/uapi/linux/kvm.h +0
-0
[ ... ]
include/uapi/linux/kvm.h struct kvm_s390_keyop { · line 1354
Quoted 6 lines
include/uapi/linux/kvm.h +2
-0@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps)
[Severity: High]
Does using _IOR here cause issues for tracing tools and user-mode translation layers?
Since the implementation reads the size field from userspace via copy_from_user(), it seems this should be defined with _IOWR.
Also, since _IOR embeds the structure size into the ioctl number, won't expanding the structure in the future change the ioctl number and break older binaries?
Quoted 3 lines
include/uapi/linux/kvm.h +0
-0/* * ioctls for vcpu fds */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v4 1/4] KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
In reply to Amit MachhiwalHi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 50 lines · Amit Machhiwal
Introduce a new capability and ioctl to expose CPU compatibility modes supported by the host processor for nested guests.
On IBM POWER systems, newer processor generations (N) can operate in compatibility modes corresponding to earlier generations, like (N-1) and (N-2). This is particularly relevant for nested virtualization, where nested KVM guests may need to run with a specific processor compatibility level.
Introduce
KVM_CAP_PPC_COMPAT_CAPScapability and the correspondingKVM_PPC_GET_COMPAT_CAPSvm ioctl. The ioctl returns a bitmap describing the compatibility modes supported by the host in respective bit numbers, allowing userspace (e.g., QEMU) to select an appropriate compatibility level when configuring nested KVM guests.The ioctl handling is added in
kvm_arch_vm_ioctl()and retrieves host CPU compatibility capabilities via a PowerPC-specific backend implementation when available. The implementation validates the structure size from userspace to ensure forward compatibility and returns appropriate error codes (EINVAL for invalid size, EFAULT for copy failures, ENOTTY if backend is not implemented). The structkvm_ppc_compat_capsincludes a size field to support future ABI extensions.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/include/uapi/asm/kvm.h | 7 ++++++ arch/powerpc/kvm/powerpc.c | 35 +++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 4 ++++ 4 files changed, 47 insertions(+)arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -319,6 +319,7 @@ struct kvmppc_ops {bool (*hash_v3_possible)(void );int (*create_vm_debugfs)(struct kvm *kvm);int (*create_vcpu_debugfs)(struct kvm_vcpu *vcpu,struct dentry *debugfs_dentry); +int (*get_compat_caps)(struct kvm_ppc_compat_caps *host_caps); };extern struct kvmppc_ops *kvmppc_hv_ops;arch/powerpc/include/uapi/asm/kvm.h +0
-0
arch/powerpc/include/uapi/asm/kvm.h struct kvm_ppc_cpu_char { · line 443
Quoted 7 lines
arch/powerpc/include/uapi/asm/kvm.h +4
-0@@ -437,6 +437,13 @@ struct kvm_ppc_cpu_char {__u64 behaviour_mask;/* valid bits in behaviour */ }; +/* For KVM_PPC_GET_COMPAT_CAPS */ +struct kvm_ppc_compat_caps { +__u64 flags;/* Reserved for future use */ +__u64 size;/* Size of this structure */
Suggesting moving the 'size' as the first member of the struct. That way copying the struct from userspace becomes bit easier.
Quoted 26 lines
arch/powerpc/include/uapi/asm/kvm.h +3
-0+__u64 compat_capabilities;/* Capabilities supported by the host */ +}; +/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS. arch/powerpc/kvm/powerpc.c +7
-0@@ -701,6 +701,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) } }break ; +#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE ) +case KVM_CAP_PPC_COMPAT_CAPS : + r =0 ; +if (kvmhv_on_pseries ()) + r =1 ; +break ; +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ default : r =0 ;break ;@@ -2467,6 +2474,34 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm->arch.kvm_ops->svm_off (kvm);
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2484
Quoted 10 lines
arch/powerpc/kvm/powerpc.c +8
-0break ; } +case KVM_PPC_GET_COMPAT_CAPS : { +struct kvm_ppc_compat_caps host_caps; +u64 user_size; + + r = -EFAULT ; +/* First, get the size field from userspace to validate */ +if (copy_from_user (&user_size, &((struct kvm_ppc_compat_caps +__user *)argp)->size,sizeof (user_size))) {
move the struct size member to the first field. That way from_from_user() call is simplified and you wont have to do some wired pointer arithmetic.
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2491
Quoted 7 lines
arch/powerpc/kvm/powerpc.c +7
-0+ goto out; + } + +/* Validate size - must be at least the current structure size */ + r = -EINVAL ; +if (user_size <sizeof (host_caps)) + goto out;
Check should be strengthed to
if (user_size != sizeof(host_caps))
So that in case used space sends a struct larger than what kernel knows abt it will be rejected. This will prevent surprises in future in case VMM sends a larger struct expecting kernel to know abt it but an older kernel only knows abt older smaller sized struct. Also look at the review comment below.
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2502
Quoted 11 lines
arch/powerpc/kvm/powerpc.c +11
-0+ + r = -ENOTTY ; +memset (&host_caps,0 ,sizeof (host_caps)); +if (!kvm->arch.kvm_ops->get_compat_caps) + goto out; + + r = kvm->arch.kvm_ops->get_compat_caps (&host_caps); +/* Set the actual size of the structure we're returning */ + host_caps.size =sizeof (host_caps); +if (!r &©_to_user (argp, &host_caps,sizeof (host_caps))) + r = -EFAULT ;
You are allowing a future userspace VMM to potentially send a larger 'struct kvm_ppc_compat_caps' that what kernel knows about. This makes error handling in userspace bit involved since there might be some fields in the 'struct kvm_ppc_compat_caps' given from userspace may remain un-initialized when userspace sees it. So please mention this subtle behaviour should be mentioned in patch description and also update it the doc in the later patch.
Quoted 31 lines
arch/powerpc/kvm/powerpc.c +2
-0+break ; + }default : {struct kvm *kvm = filp->private_data; r = kvm->arch.kvm_ops->arch_vm_ioctl (filp, ioctl, arg);include/uapi/linux/kvm.h +4
-0@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_PPC_COMPAT_CAPS 249struct kvm_irq_routing_irqchip {__u32 irqchip;@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps) +/* * ioctls for vcpu fds */ Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v4 1/4] KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl
Hi Vaibhav,
Thanks for the detailed review. My responses are inline below.
Quoted 60 lines · Vaibhav Jain
Hi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 53 lines · Amit Machhiwal
Introduce a new capability and ioctl to expose CPU compatibility modes supported by the host processor for nested guests.
On IBM POWER systems, newer processor generations (N) can operate in compatibility modes corresponding to earlier generations, like (N-1) and (N-2). This is particularly relevant for nested virtualization, where nested KVM guests may need to run with a specific processor compatibility level.
Introduce
KVM_CAP_PPC_COMPAT_CAPScapability and the correspondingKVM_PPC_GET_COMPAT_CAPSvm ioctl. The ioctl returns a bitmap describing the compatibility modes supported by the host in respective bit numbers, allowing userspace (e.g., QEMU) to select an appropriate compatibility level when configuring nested KVM guests.The ioctl handling is added in
kvm_arch_vm_ioctl()and retrieves host CPU compatibility capabilities via a PowerPC-specific backend implementation when available. The implementation validates the structure size from userspace to ensure forward compatibility and returns appropriate error codes (EINVAL for invalid size, EFAULT for copy failures, ENOTTY if backend is not implemented). The structkvm_ppc_compat_capsincludes a size field to support future ABI extensions.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/include/asm/kvm_ppc.h | 1 + arch/powerpc/include/uapi/asm/kvm.h | 7 ++++++ arch/powerpc/kvm/powerpc.c | 35 +++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 4 ++++ 4 files changed, 47 insertions(+)arch/powerpc/include/asm/kvm_ppc.h +1
-0@@ -319,6 +319,7 @@ struct kvmppc_ops {bool (*hash_v3_possible)(void );int (*create_vm_debugfs)(struct kvm *kvm);int (*create_vcpu_debugfs)(struct kvm_vcpu *vcpu,struct dentry *debugfs_dentry); +int (*get_compat_caps)(struct kvm_ppc_compat_caps *host_caps); };extern struct kvmppc_ops *kvmppc_hv_ops;arch/powerpc/include/uapi/asm/kvm.h +0
-0@@ -437,6 +437,13 @@ struct kvm_ppc_cpu_char {__u64 behaviour_mask;/* valid bits in behaviour */ };
Quoted 6 lines
arch/powerpc/include/uapi/asm/kvm.h struct kvm_ppc_cpu_char { · line 443
Quoted 4 lines
arch/powerpc/include/uapi/asm/kvm.h +4
-0+/* For KVM_PPC_GET_COMPAT_CAPS */ +struct kvm_ppc_compat_caps { +__u64 flags;/* Reserved for future use */ +__u64 size;/* Size of this structure */ Suggesting moving the 'size' as the first member of the struct. That way copying the struct from userspace becomes bit easier.
Yeah, I think it would make more sense and will simplify the copy_from_user() call. I will make the change in v5. I will change to:
struct kvm_ppc_compat_caps {
__u64 size;
__u64 flags;
__u64 compat_capabilities;
};Quoted 32 lines
Quoted 32 lines
arch/powerpc/include/uapi/asm/kvm.h +3
-0+__u64 compat_capabilities;/* Capabilities supported by the host */ +}; +/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS. arch/powerpc/kvm/powerpc.c +10
-0@@ -701,6 +701,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) } }break ; +#if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE ) +case KVM_CAP_PPC_COMPAT_CAPS : + r =0 ; +if (kvmhv_on_pseries ()) + r =1 ; +break ; +#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ default : r =0 ;break ;@@ -2467,6 +2474,34 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = kvm->arch.kvm_ops->svm_off (kvm);break ; } +case KVM_PPC_GET_COMPAT_CAPS : { +struct kvm_ppc_compat_caps host_caps; +u64 user_size;
Quoted 8 lines
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2484
Quoted 5 lines
arch/powerpc/kvm/powerpc.c +5
-0+ + r = -EFAULT ; +/* First, get the size field from userspace to validate */ +if (copy_from_user (&user_size, &((struct kvm_ppc_compat_caps +__user *)argp)->size,sizeof (user_size))) {move the struct size member to the first field. That way
from_from_user()call is simplified and you wont have to do some wired pointer arithmetic.
Will do as mentioned above.
Quoted 16 lines
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2491
Quoted 9 lines
arch/powerpc/kvm/powerpc.c +7
-0+ goto out; + } + +/* Validate size - must be at least the current structure size */ + r = -EINVAL ; +if (user_size <sizeof (host_caps)) + goto out;Check should be strengthed to
if (user_size!=sizeof(host_caps))
So that in case used space sends a struct larger than what kernel knows abt it will be rejected. This will prevent surprises in future in case VMM sends a larger struct expecting kernel to know abt it but an older kernel only knows abt older smaller sized struct. Also look at the review comment below.
Agreed. I'll change the validation to use strict equality. This is simpler and clearer - userspace must provide exactly the size the kernel expects.
Quoted 11 lines
Quoted 11 lines
arch/powerpc/kvm/powerpc.c +10
-0+ + r = -ENOTTY ; +memset (&host_caps,0 ,sizeof (host_caps)); +if (!kvm->arch.kvm_ops->get_compat_caps) + goto out; + + r = kvm->arch.kvm_ops->get_compat_caps (&host_caps); +/* Set the actual size of the structure we're returning */ + host_caps.size =sizeof (host_caps); +if (!r &©_to_user (argp, &host_caps,sizeof (host_caps)))
Quoted 8 lines
arch/powerpc/kvm/powerpc.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2502
Quoted 1 line
arch/powerpc/kvm/powerpc.c +1
-0+ r = -EFAULT ;You are allowing a future userspace VMM to potentially send a larger 'struct
kvm_ppc_compat_caps' that what kernel knows about. This makes error handling in userspace bit involved since there might be some fields in the 'structkvm_ppc_compat_caps' given from userspace may remain un-initialized when userspace sees it. So please mention this subtle behaviour should be mentioned in patch description and also update it the doc in the later patch.
With the strict equality check (user_size != sizeof(host_caps)), this concern should be addressed - we won't accept larger structs from userspace. However, I'll still improve the documentation to:
1. In the commit message:
- Explain the size field validation
- Document that exact size match is required
- Clarify error handling behavior
2. In Documentation/virt/kvm/api.rst:
- Add improved documentation for KVM_PPC_GET_COMPAT_CAPS
- Document the size field requirement and validation
Thanks,
Amit
Quoted 36 lines
Quoted 32 lines
arch/powerpc/kvm/powerpc.c +2
-0+break ; + }default : {struct kvm *kvm = filp->private_data; r = kvm->arch.kvm_ops->arch_vm_ioctl (filp, ioctl, arg);include/uapi/linux/kvm.h +4
-0@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_PPC_COMPAT_CAPS 249struct kvm_irq_routing_irqchip {__u32 irqchip;@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps) +/* * ioctls for vcpu fds */ Signature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav
PATCH v4 2/4 KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM
In reply to Amit MachhiwalOn POWER systems, the host CPU may run in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
When running nested KVM guests, QEMU derives the host CPU type using mfpvr(), which reflects the physical processor version. This can result in a mismatch between the CPU model selected by QEMU and the compatibility mode enforced by the host, leading to guest boot failures.
For example, booting a nested guest on a Power11 LPAR configured in Power10 compatibility mode fails with:
KVM-NESTEDv2: couldn't set guest wide elements
[..KVM reg dump..]
This occurs because QEMU selects a CPU model corresponding to the physical processor (via mfpvr()), while the host operates in a lower compatibility mode. As a result, KVM rejects the requested compatibility level during guest initialization.
Add support for retrieving host CPU compatibility capabilities for nested guests on PowerVM (PAPR nested API v2). The hypervisor provides the effective compatibility levels via the H_GUEST_GET_CAPABILITIES hcall, which reflects the processor modes negotiated between the Power hypervisor (L0) and the host partition (L1).
On pseries systems, obtain the capability bitmap using plpar_guest_get_capabilities() and return it via struct kvm_ppc_compat_caps. The implementation defines KVM-specific capability constants (KVM_PPC_COMPAT_CAP_POWER9/10/11) and applies masking to ensure only supported processor modes are exposed to userspace. This information is then exposed through the KVM_PPC_GET_COMPAT_CAPS ioctl.
Hook the implementation into the Book3S HV kvmppc_ops so that it can be invoked by the generic KVM ioctl handling code.
--- arch/powerpc/include/uapi/asm/kvm.h | 11 ++++++++++- arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
arch/powerpc/include/uapi/asm/kvm.h +10 -1
@@ -443,7 +443,16 @@ struct kvm_ppc_compat_caps {__u64 size;/* Size of this structure */ __u64 compat_capabilities;/* Capabilities supported by the host */ }; - +/* +* Capability bits for compat_capabilities field in kvm_ppc_compat_caps. +* These bits indicate which processor compatibility modes are supported. +*/ +#define KVM_PPC_COMPAT_CAP_POWER9 (1ULL << 62) +#define KVM_PPC_COMPAT_CAP_POWER10 (1ULL << 61) +#define KVM_PPC_COMPAT_CAP_POWER11 (1ULL << 60) +#define KVM_PPC_COMPAT_BITMASK (KVM_PPC_COMPAT_CAP_POWER9 | \ + KVM_PPC_COMPAT_CAP_POWER10 | \ + KVM_PPC_COMPAT_CAP_POWER11)/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS.
arch/powerpc/kvm/book3s_hv.c +17 -0
@@ -6523,6 +6523,22 @@ static bool kvmppc_hash_v3_possible(void)return true; } + +static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) +{ +unsigned long capabilities =0 ; +long rc = -EINVAL ; + +if (kvmhv_on_pseries ()) { +if (kvmhv_is_nestedv2 ()) + rc =plpar_guest_get_capabilities (0 , &capabilities); + host_caps->compat_capabilities = capabilities & +KVM_PPC_COMPAT_BITMASK ; + } + +return rc; +} +static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
@@ -6565,6 +6581,7 @@ static struct kvmppc_ops kvm_ops_hv = { .hash_v3_possible = kvmppc_hash_v3_possible, .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv, .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv, + .get_compat_caps = kvmppc_get_compat_caps, };static int kvm_init_subcore_bitmap (void )
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [PATCH v4 2/4] KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Strict size validation of the extensible `struct kvm_ppc_compat_caps` prevents future extensions…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Strict size validation of the extensible struct kvm_ppc_compat_caps prevents future extensions without breaking backward compatibility.
- [High] Raw hypervisor H_* error codes are directly leaked to userspace as POSIX error numbers, causing incorrect system call mapping and potential infinite loops.
- [High] The ioctl incorrectly queries raw L0 hypervisor capabilities instead of the capabilities actually negotiated by the L1 KVM host.
- [Medium] Device tree node reference leak in for_each_node_by_type loop.
--
commit c079a1c0671cc47d6e2767d0d012a074b48a3edf
Author: Amit Machhiwal <[email protected]>
KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM
On POWER systems, the host CPU may run in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
[ ... ]
arch/powerpc/include/uapi/asm/kvm.h struct kvm_ppc_compat_caps { · line 445
Quoted 8 lines
arch/powerpc/include/uapi/asm/kvm.h +0
-0@@ -443,7 +443,16 @@ struct kvm_ppc_compat_caps {__u64 size;/* Size of this structure */ __u64 compat_capabilities;/* Capabilities supported by the host */ };
[Severity: High]
The KVM_PPC_GET_COMPAT_CAPS ioctl is defined using _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps). Since _IOR embeds the structure size in the ioctl number, if fields are added to this structure in the future, the ioctl number will change.
Additionally, the explicit size validation in kvm_arch_vm_ioctl() rejects requests where user_size < sizeof(host_caps), returning -EINVAL:
arch/powerpc/kvm/powerpc.c:kvm_arch_vm_ioctl() {
...
/* Validate size - must be at least the current structure size */
r = -EINVAL;
if (user_size < sizeof(host_caps))
goto out;
...
}Does this prevent extending the structure in the future without breaking backward UAPI compatibility for older userspace binaries?
Quoted 5 lines
arch/powerpc/include/uapi/asm/kvm.h +5
-0+/* +* Capability bits for compat_capabilities field in kvm_ppc_compat_caps. +* These bits indicate which processor compatibility modes are supported. +*/ +#define KVM_PPC_COMPAT_CAP_POWER9 (1ULL << 62)
[ ... ]
Quoted 5 lines
arch/powerpc/kvm/book3s_hv.c +0
-0@@ -6510,6 +6510,22 @@ static bool kvmppc_hash_v3_possible(void)
[ ... ]
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6534
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c +8
-0+static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) +{ +unsigned long capabilities =0 ; +long rc = -EINVAL ; + +if (kvmhv_on_pseries ()) { +if (kvmhv_is_nestedv2 ()) + rc =plpar_guest_get_capabilities (0 , &capabilities);
[Severity: High]
Are we returning a raw hypervisor error code directly to userspace here?
Negative codes like H_PARAMETER (-4) could be incorrectly interpreted as standard POSIX errors (e.g., -EINTR). Standard userspace libraries might handle EINTR by automatically retrying the ioctl, which could lead to an infinite loop.
Positive codes like H_NOT_AVAILABLE (3) will be returned directly to the userspace caller, skipping the copy_to_user logic in kvm_arch_vm_ioctl() while still appearing as a success to userspace.
Should we map these raw hypervisor status codes to standard POSIX errors before returning?
[Severity: High]
Does this query the raw L0 hypervisor capabilities instead of the capabilities actively negotiated by the L1 KVM host?
According to the PAPR nested virtualization specification, this hypercall retrieves unmodified L0 capabilities. However, KVM limits its nested guest capabilities based on nested_capabilities negotiated during initialization:
arch/powerpc/kvm/book3s_hv_nested.c:kvmhv_nestedv2_init() {
...
nested_capabilities = capabilities & host_capabilities;
rc = plpar_guest_set_capabilities(0, nested_capabilities);
...
}If QEMU receives raw L0 capabilities, could it attempt to use CPU modes that KVM will subsequently reject during vCPU initialization, leading to guest boot failures?
Should we return the negotiated nested_capabilities instead?
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +6
-0+ host_caps->compat_capabilities = capabilities & +KVM_PPC_COMPAT_BITMASK ; + } + +return rc; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v4 2/4] KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM
In reply to Amit MachhiwalHi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 74 lines · Amit Machhiwal
On POWER systems, the host CPU may run in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
When running nested KVM guests, QEMU derives the host CPU type using
mfpvr(), which reflects the physical processor version. This can result in a mismatch between the CPU model selected by QEMU and the compatibility mode enforced by the host, leading to guest boot failures.For example, booting a nested guest on a Power11 LPAR configured in Power10 compatibility mode fails with:
KVM-NESTEDv2: couldn't set guest wide elements
[..KVM reg dump..]This occurs because QEMU selects a CPU model corresponding to the physical processor (via
mfpvr()), while the host operates in a lower compatibility mode. As a result, KVM rejects the requested compatibility level during guest initialization.Add support for retrieving host CPU compatibility capabilities for nested guests on PowerVM (PAPR nested API v2). The hypervisor provides the effective compatibility levels via the
H_GUEST_GET_CAPABILITIEShcall, which reflects the processor modes negotiated between the Power hypervisor (L0) and the host partition (L1).On pseries systems, obtain the capability bitmap using
plpar_guest_get_capabilities()and return it via structkvm_ppc_compat_caps. The implementation defines KVM-specific capability constants (KVM_PPC_COMPAT_CAP_POWER9/10/11) and applies masking to ensure only supported processor modes are exposed to userspace. This information is then exposed through theKVM_PPC_GET_COMPAT_CAPSioctl.Hook the implementation into the Book3S HV
kvmppc_opsso that it can be invoked by the generic KVM ioctl handling code.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/include/uapi/asm/kvm.h | 11 ++++++++++- arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)arch/powerpc/include/uapi/asm/kvm.h +10
-1@@ -443,7 +443,16 @@ struct kvm_ppc_compat_caps {__u64 size;/* Size of this structure */ __u64 compat_capabilities;/* Capabilities supported by the host */ }; - +/* +* Capability bits for compat_capabilities field in kvm_ppc_compat_caps. +* These bits indicate which processor compatibility modes are supported. +*/ +#define KVM_PPC_COMPAT_CAP_POWER9 (1ULL << 62) +#define KVM_PPC_COMPAT_CAP_POWER10 (1ULL << 61) +#define KVM_PPC_COMPAT_CAP_POWER11 (1ULL << 60) +#define KVM_PPC_COMPAT_BITMASK (KVM_PPC_COMPAT_CAP_POWER9 | \ + KVM_PPC_COMPAT_CAP_POWER10 | \ + KVM_PPC_COMPAT_CAP_POWER11)/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS. arch/powerpc/kvm/book3s_hv.c +0
-0@@ -6523,6 +6523,22 @@ static bool kvmppc_hash_v3_possible(void)return true; }
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6535
Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +9
-0+ +static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) +{ +unsigned long capabilities =0 ; +long rc = -EINVAL ; + +if (kvmhv_on_pseries ()) { +if (kvmhv_is_nestedv2 ()) + rc =plpar_guest_get_capabilities (0 , &capabilities);
I think instead of making the hcall you should use the 'nested_capabilities' extern symbol as it would already the same value. This symbol is already accessible in 'book3s_hv.c'
Quoted 22 lines
arch/powerpc/kvm/book3s_hv.c +7
-0+ host_caps->compat_capabilities = capabilities & +KVM_PPC_COMPAT_BITMASK ; + } + +return rc; +} +static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,arch/powerpc/kvm/book3s_hv.c +1
-0.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,@@ -6565,6 +6581,7 @@ static struct kvmppc_ops kvm_ops_hv = { .hash_v3_possible = kvmppc_hash_v3_possible, .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv, .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv, + .get_compat_caps = kvmppc_get_compat_caps, };static int kvm_init_subcore_bitmap (void )Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v4 2/4] KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM
Hi Vaibhav,
Thanks for revewing this patch. My response is inline.
Quoted 81 lines · Vaibhav Jain
Hi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 74 lines · Amit Machhiwal
On POWER systems, the host CPU may run in a compatibility mode (e.g., a Power11 processor operating in Power10 compatibility mode). In such cases, the effective CPU level exposed to guests differs from the physical processor generation.
When running nested KVM guests, QEMU derives the host CPU type using
mfpvr(), which reflects the physical processor version. This can result in a mismatch between the CPU model selected by QEMU and the compatibility mode enforced by the host, leading to guest boot failures.For example, booting a nested guest on a Power11 LPAR configured in Power10 compatibility mode fails with:
KVM-NESTEDv2: couldn't set guest wide elements
[..KVM reg dump..]This occurs because QEMU selects a CPU model corresponding to the physical processor (via
mfpvr()), while the host operates in a lower compatibility mode. As a result, KVM rejects the requested compatibility level during guest initialization.Add support for retrieving host CPU compatibility capabilities for nested guests on PowerVM (PAPR nested API v2). The hypervisor provides the effective compatibility levels via the
H_GUEST_GET_CAPABILITIEShcall, which reflects the processor modes negotiated between the Power hypervisor (L0) and the host partition (L1).On pseries systems, obtain the capability bitmap using
plpar_guest_get_capabilities()and return it via structkvm_ppc_compat_caps. The implementation defines KVM-specific capability constants (KVM_PPC_COMPAT_CAP_POWER9/10/11) and applies masking to ensure only supported processor modes are exposed to userspace. This information is then exposed through theKVM_PPC_GET_COMPAT_CAPSioctl.Hook the implementation into the Book3S HV
kvmppc_opsso that it can be invoked by the generic KVM ioctl handling code.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/include/uapi/asm/kvm.h | 11 ++++++++++- arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)arch/powerpc/include/uapi/asm/kvm.h +10
-1@@ -443,7 +443,16 @@ struct kvm_ppc_compat_caps {__u64 size;/* Size of this structure */ __u64 compat_capabilities;/* Capabilities supported by the host */ }; - +/* +* Capability bits for compat_capabilities field in kvm_ppc_compat_caps. +* These bits indicate which processor compatibility modes are supported. +*/ +#define KVM_PPC_COMPAT_CAP_POWER9 (1ULL << 62) +#define KVM_PPC_COMPAT_CAP_POWER10 (1ULL << 61) +#define KVM_PPC_COMPAT_CAP_POWER11 (1ULL << 60) +#define KVM_PPC_COMPAT_BITMASK (KVM_PPC_COMPAT_CAP_POWER9 | \ + KVM_PPC_COMPAT_CAP_POWER10 | \ + KVM_PPC_COMPAT_CAP_POWER11)/* * Values for character and character_mask. * These are identical to the values used by H_GET_CPU_CHARACTERISTICS. arch/powerpc/kvm/book3s_hv.c +0
-0@@ -6523,6 +6523,22 @@ static bool kvmppc_hash_v3_possible(void)return true; }
Quoted 13 lines
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void)
Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +9
-0+ +static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) +{ +unsigned long capabilities =0 ; +long rc = -EINVAL ; + +if (kvmhv_on_pseries ()) { +if (kvmhv_is_nestedv2 ()) + rc =plpar_guest_get_capabilities (0 , &capabilities);I think instead of making the hcall you should use the '
nested_capabilities' extern symbol as it would already the same value. This symbol is already accessible in 'book3s_hv.c'
Agreed! Will change to use nested_capabilities directly instead of making the hcall. This is more efficient as this will help reduce an hcall overhead while the value is already cached during module initialization (in kvmhv_nested_init()).
Thanks,
Amit
Quoted 27 lines
Quoted 23 lines
arch/powerpc/kvm/book3s_hv.c +7
-0+ host_caps->compat_capabilities = capabilities & +KVM_PPC_COMPAT_BITMASK ; + } + +return rc; +} +static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,arch/powerpc/kvm/book3s_hv.c +1
-0.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,@@ -6565,6 +6581,7 @@ static struct kvmppc_ops kvm_ops_hv = { .hash_v3_possible = kvmppc_hash_v3_possible, .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv, .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv, + .get_compat_caps = kvmppc_get_compat_caps, };static int kvm_init_subcore_bitmap (void )Signature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav
PATCH v4 3/4 KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV
In reply to Amit MachhiwalCurrently, when booting a compatibility-mode KVM guest (L1) on a PowerNV hypervisor (L0), the guest runs with the expected processor compatibility level. However, when booting a nested KVM guest (L2) inside the L1, QEMU derives the CPU model from the raw host PVR and attempts to run the nested guest at that level, instead of honoring the compatibility mode of the L1.
Extend host CPU compatibility capability reporting to support nested virtualization on PowerNV systems (PAPR nested API v1).
For nested API v2 (PowerVM), compatibility capabilities are obtained from the hypervisor via the H_GUEST_GET_CAPABILITIES hcall. This information is not available on PowerNV systems.
For nested API v1, derive the compatibility capabilities from the L1 guest by reading the "cpu-version" property from the device tree, which reflects the effective (logical) processor compatibility level. Map this value to the corresponding compatibility capability bitmap using KVM-specific constants.
Introduce a helper to translate CPU version values into KVM_PPC_COMPAT_CAP bits and integrate it into kvmppc_get_compat_caps(). The implementation applies masking to ensure only supported processor modes are exposed.
This allows userspace to query host CPU compatibility modes on both PowerVM and PowerNV platforms via the KVM_PPC_GET_COMPAT_CAPS ioctl.
--- arch/powerpc/kvm/book3s_hv.c | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +36 -1
@@ -6523,15 +6523,50 @@ static bool kvmppc_hash_v3_possible(void)return true; } +static int kvmppc_map_compat_capabilities (const __be32 cpu_version, +unsigned long *capabilities) +{ +switch (cpu_version) { +case PVR_ARCH_31_P11 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER11 ; +break ; +case PVR_ARCH_31 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER10 ; +break ; +case PVR_ARCH_300 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER9 ; +break ; +default : +return -EINVAL ; + } + +return 0 ; +}static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) { +struct device_node *np;unsigned long capabilities =0 ; +const __be32 *prop =NULL ;long rc = -EINVAL ; +u32 cpu_version;if (kvmhv_on_pseries ()) { -if (kvmhv_is_nestedv2 ()) +if (kvmhv_is_nestedv2 ()) { rc =plpar_guest_get_capabilities (0 , &capabilities); + }else { +for_each_node_by_type (np,"cpu" ) { + prop =of_get_property (np,"cpu-version" ,NULL ); +if (prop) { + cpu_version =be32_to_cpup (prop); +break ; + } + } +if (!prop) +return -EINVAL ; + rc =kvmppc_map_compat_capabilities (cpu_version, + &capabilities); + } host_caps->compat_capabilities = capabilities &KVM_PPC_COMPAT_BITMASK ; }
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [PATCH v4 3/4] KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Missing `of_node_put()` causes an OF node reference count leak when breaking out of…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Missing of_node_put() causes an OF node reference count leak when breaking out of for_each_node_by_type().
- [High] Incomplete capability reporting due to missing fallthrough in kvmppc_map_compat_capabilities.
- [High] Hypercall H_ error codes are leaked directly to userspace as standard Linux errnos.
- [Medium] KVM_PPC_GET_COMPAT_CAPS is defined as _IOR but expects input from userspace.
- [Low] Endianness type mismatch for cpu_version between caller and callee violates sparse typing. --
commit 2d0f9bcccc58cf2776556c592aad6db1adf0c8aa
Author: Amit Machhiwal <[email protected]>
KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV
Extend host CPU compatibility capability reporting to support nested virtualization on PowerNV systems. For nested API v1, derive the capabilities from the L1 guest via the "cpu-version" device tree property and map them to KVM_PPC_COMPAT_CAP bits.
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +0
-0
[ ... ]
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6514
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +2
-0@@ -6510,6 +6510,57 @@ static bool kvmppc_hash_v3_possible(void)return true; } +static int kvmppc_map_compat_capabilities (const __be32 cpu_version, +unsigned long *capabilities)
[Severity: Low]
Does this parameter type violate sparse typing rules?
The caller kvmppc_get_compat_caps() passes a host-endian u32 value that was converted via be32_to_cpup(). If cpu_version is annotated as __be32, might this trigger a static analyzer warning?
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6538
Quoted 11 lines
arch/powerpc/kvm/book3s_hv.c +11
-0+{ +switch (cpu_version) { +case PVR_ARCH_31_P11 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER11 ; +break ; +case PVR_ARCH_31 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER10 ; +break ; +case PVR_ARCH_300 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER9 ; +break ;
[Severity: High]
Could this switch statement be missing fallthroughs?
Host CPU compatibility modes are typically cumulative. For example, a POWER10 CPU would support both POWER10 and POWER9 compatibility modes. With these breaks, only a single highest capability bit is set. Is there a risk QEMU might incorrectly conclude that older nested guests are unsupported?
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c +8
-0+default : +return -EINVAL ; + } + +return 0 ; +} + +static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps)
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void)
Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +10
-0+{ +struct device_node *np; +unsigned long capabilities =0 ; +const __be32 *prop =NULL ; +long rc = -EINVAL ; +u32 cpu_version; + +if (kvmhv_on_pseries ()) { +if (kvmhv_is_nestedv2 ()) { + rc =plpar_guest_get_capabilities (0 , &capabilities);
[Severity: High]
Are hypercall error codes being leaked directly to userspace here?
If plpar_guest_get_capabilities() fails, it returns a hypervisor error code like H_PARAMETER (-4). This value is directly assigned to rc and returned to the ioctl handler. Since -4 corresponds to -EINTR in Linux, would userspace misinterpret the error as an interrupted system call and infinitely retry the ioctl?
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6562
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +6
-0+ }else { +for_each_node_by_type (np,"cpu" ) { + prop =of_get_property (np,"cpu-version" ,NULL ); +if (prop) { + cpu_version =be32_to_cpup (prop); +break ;
[Severity: High]
Does this early break leak the OF node reference?
The for_each_node_by_type() macro drops the reference to the previous node and acquires a reference to the current node. By breaking out of the loop without calling of_node_put(np), the reference count appears to be leaked.
Quoted 13 lines
arch/powerpc/kvm/book3s_hv.c +13
-0+ } + } +if (!prop) +return -EINVAL ; + rc =kvmppc_map_compat_capabilities (cpu_version, + &capabilities); + } + host_caps->compat_capabilities = capabilities & +KVM_PPC_COMPAT_BITMASK ; + } + +return rc; +}
[ ... ]
Quoted 4 lines
include/uapi/linux/kvm.h +0
-0
[ ... ]
include/uapi/linux/kvm.h struct kvm_s390_keyop { · line 1354
Quoted 6 lines
include/uapi/linux/kvm.h +2
-0@@ -1349,6 +1350,9 @@ struct kvm_s390_keyop {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +/* Available with KVM_CAP_PPC_COMPAT_CAPS */ +#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps)
[Severity: Medium]
Could this ioctl definition cause issues with wrapper libraries?
The KVM_PPC_GET_COMPAT_CAPS ioctl handler in kvm_arch_vm_ioctl() uses copy_from_user() to read the size field from the userspace structure. Since it reads input data from userspace, should this be defined as _IOWR? Using _IOR might cause unexpected behavior with tools that assume the parameter is purely an output buffer.
Quoted 4 lines
include/uapi/linux/kvm.h +1
-0+/* * ioctls for vcpu fds */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v4 3/4] KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV
In reply to Amit MachhiwalHi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 38 lines · Amit Machhiwal
Currently, when booting a compatibility-mode KVM guest (L1) on a PowerNV hypervisor (L0), the guest runs with the expected processor compatibility level. However, when booting a nested KVM guest (L2) inside the L1, QEMU derives the CPU model from the raw host PVR and attempts to run the nested guest at that level, instead of honoring the compatibility mode of the L1.
Extend host CPU compatibility capability reporting to support nested virtualization on PowerNV systems (PAPR nested API v1).
For nested API v2 (PowerVM), compatibility capabilities are obtained from the hypervisor via the
H_GUEST_GET_CAPABILITIEShcall. This information is not available on PowerNV systems.For nested API v1, derive the compatibility capabilities from the L1 guest by reading the "cpu-version" property from the device tree, which reflects the effective (logical) processor compatibility level. Map this value to the corresponding compatibility capability bitmap using KVM-specific constants.
Introduce a helper to translate CPU version values into
KVM_PPC_COMPAT_CAPbits and integrate it intokvmppc_get_compat_caps(). The implementation applies masking to ensure only supported processor modes are exposed.This allows userspace to query host CPU compatibility modes on both PowerVM and PowerNV platforms via the
KVM_PPC_GET_COMPAT_CAPSioctl.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)arch/powerpc/kvm/book3s_hv.c +0
-0
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6531
Quoted 9 lines
arch/powerpc/kvm/book3s_hv.c +6
-0@@ -6523,15 +6523,50 @@ static bool kvmppc_hash_v3_possible(void)return true; } +static int kvmppc_map_compat_capabilities (const __be32 cpu_version, +unsigned long *capabilities) +{ +switch (cpu_version) { +case PVR_ARCH_31_P11 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER11 ;
Do you need to do 'break' here instead of falling through. Since P11 host can support P10 and P9 compat modes
Quoted 29 lines
arch/powerpc/kvm/book3s_hv.c +20
-1+break ; +case PVR_ARCH_31 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER10 ; +break ; +case PVR_ARCH_300 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER9 ; +break ; +default : +return -EINVAL ; + } + +return 0 ; +}static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) { +struct device_node *np;unsigned long capabilities =0 ; +const __be32 *prop =NULL ;long rc = -EINVAL ; +u32 cpu_version;if (kvmhv_on_pseries ()) { -if (kvmhv_is_nestedv2 ()) +if (kvmhv_is_nestedv2 ()) { rc =plpar_guest_get_capabilities (0 , &capabilities); + }else { +for_each_node_by_type (np,"cpu" ) { + prop =of_get_property (np,"cpu-version" ,NULL );
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6569
Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +10
-0+if (prop) { + cpu_version =be32_to_cpup (prop); +break ; + } + } +if (!prop) +return -EINVAL ; + rc =kvmppc_map_compat_capabilities (cpu_version, + &capabilities); + }
should you check for 'rc' error here before assigning 'capabilities' to 'host_caps->compat_capabilities' . I understand it will be set to '0' due to its initialization at the top of the function. But would be better to make it more explicit
Quoted 7 lines
arch/powerpc/kvm/book3s_hv.c +0
-0host_caps->compat_capabilities = capabilities &KVM_PPC_COMPAT_BITMASK ; }Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v4 3/4] KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV
Hi Vaibhav,
Thanks for reviewing this patch. Please find my response inline.
Quoted 48 lines · Vaibhav Jain
Hi Amit.
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 41 lines · Amit Machhiwal
Currently, when booting a compatibility-mode KVM guest (L1) on a PowerNV hypervisor (L0), the guest runs with the expected processor compatibility level. However, when booting a nested KVM guest (L2) inside the L1, QEMU derives the CPU model from the raw host PVR and attempts to run the nested guest at that level, instead of honoring the compatibility mode of the L1.
Extend host CPU compatibility capability reporting to support nested virtualization on PowerNV systems (PAPR nested API v1).
For nested API v2 (PowerVM), compatibility capabilities are obtained from the hypervisor via the
H_GUEST_GET_CAPABILITIEShcall. This information is not available on PowerNV systems.For nested API v1, derive the compatibility capabilities from the L1 guest by reading the "cpu-version" property from the device tree, which reflects the effective (logical) processor compatibility level. Map this value to the corresponding compatibility capability bitmap using KVM-specific constants.
Introduce a helper to translate CPU version values into
KVM_PPC_COMPAT_CAPbits and integrate it intokvmppc_get_compat_caps(). The implementation applies masking to ensure only supported processor modes are exposed.This allows userspace to query host CPU compatibility modes on both PowerVM and PowerNV platforms via the
KVM_PPC_GET_COMPAT_CAPSioctl.Suggested-by Vaibhav Jain <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- arch/powerpc/kvm/book3s_hv.c | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)arch/powerpc/kvm/book3s_hv.c +0
-0@@ -6523,15 +6523,50 @@ static bool kvmppc_hash_v3_possible(void)return true; }
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6531
Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +6
-0+static int kvmppc_map_compat_capabilities (const __be32 cpu_version, +unsigned long *capabilities) +{ +switch (cpu_version) { +case PVR_ARCH_31_P11 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER11 ;Do you need to do 'break' here instead of falling through. Since P11 host can support P10 and P9 compat modes
I had addressed a similar comment from Harsh in v1 of the series here:
https://lore.kernel.org/all/[email protected]/
The current implementation with break statements is intentional. This function (kvmppc_map_compat_capabilities()) is called only when booting a nested KVM guest (L2) on **KVM on PowerNV**.
When the L1 KVM guest is booted in a compat mode, L2 is supposed to boot with the **same PVR version** as that of the L1, which is already taken care of with the current changes. If L2 needs to boot with a different *lower* compat mode, it would use max-cpu-compat, which takes a different code path for setting the compat.
Even if I included all lower compat modes in the compat caps **APIv1**, I don't think we'll be using those lower compat bits unless we wanted to block a specific older compat for a given pvr level - which neither we are doing in this series nor we may want to put such a restriction for APIv1.
Please let me know if you think otherwise.
Quoted 36 lines
Quoted 36 lines
arch/powerpc/kvm/book3s_hv.c +26
-1+break ; +case PVR_ARCH_31 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER10 ; +break ; +case PVR_ARCH_300 : + *capabilities |=KVM_PPC_COMPAT_CAP_POWER9 ; +break ; +default : +return -EINVAL ; + } + +return 0 ; +}static int kvmppc_get_compat_caps (struct kvm_ppc_compat_caps *host_caps) { +struct device_node *np;unsigned long capabilities =0 ; +const __be32 *prop =NULL ;long rc = -EINVAL ; +u32 cpu_version;if (kvmhv_on_pseries ()) { -if (kvmhv_is_nestedv2 ()) +if (kvmhv_is_nestedv2 ()) { rc =plpar_guest_get_capabilities (0 , &capabilities); + }else { +for_each_node_by_type (np,"cpu" ) { + prop =of_get_property (np,"cpu-version" ,NULL ); +if (prop) { + cpu_version =be32_to_cpup (prop); +break ; + } + } +if (!prop)
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c static bool kvmppc_hash_v3_possible(void) · line 6569
Quoted 4 lines
arch/powerpc/kvm/book3s_hv.c +4
-0+return -EINVAL ; + rc =kvmppc_map_compat_capabilities (cpu_version, + &capabilities); + }should you check for 'rc' error here before assigning 'capabilities' to '
host_caps->compat_capabilities' . I understand it will be set to '0' due to its initialization at the top of the function. But would be better to make it more explicit
Sure. The return value rc is checked by the caller but more error checking is always good I guess. :)
I'll add a check for rc something like this (or something similar):
if (rc) {
return -EINVAL;
} host_caps->compat_capabilities = capabilities &
KVM_PPC_COMPAT_BITMASK;
Thanks,
Amit
Quoted 12 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_hv.c +0
-0host_caps->compat_capabilities = capabilities &KVM_PPC_COMPAT_BITMASK ; }Signature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav
PATCH v4 4/4 KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
In reply to Amit MachhiwalAdd documentation for the KVM_PPC_GET_COMPAT_CAPS ioctl to the KVM API documentation.
The ioctl exposes host processor compatibility modes supported for nested KVM guests on PowerPC systems. The documentation includes comprehensive error code descriptions, structure field definitions including the size field for forward compatibility, and KVM-specific capability bit constants.
--- Documentation/virt/kvm/api.rst | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
Documentation/virt/kvm/api.rst +47 -0
@@ -6553,6 +6553,53 @@ KVM_S390_KEYOP_SSKE Sets the storage key for the guest address ``guest_addr`` to the key specified in ``key``, returning the previous value in ``key``. +4.145 KVM_PPC_GET_COMPAT_CAPS +----------------------------- +:Capability: KVM_CAP_PPC_COMPAT_CAPS +:Architectures: powerpc +:Type: vm ioctl +:Parameters: struct kvm_ppc_compat_caps (out) +:Returns: 0 on success, negative value on failure + +Errors include: + + ======== ============================================================ + EFAULT if ``struct kvm_ppc_compat_caps`` cannot be read from or + written to userspace + EINVAL if the ``size`` field is smaller than the current structure + size, or if the backend implementation fails to retrieve or + map CPU compatibility capabilities + ENOTTY if the backend does not implement the ``get_compat_caps`` + operation (e.g., on non-pseries platforms or when the + required KVM operations are not available) + ======== ============================================================ + +IBM POWER system server-based processors provide a compatibility mode feature +where an Nth generation processor can operate in modes consistent with earlier +generations such as (N-1) and (N-2). + +This ioctl provides userspace with information about the CPU compatibility modes +supported by the current host processor for booting the nested KVM guests on +PowerNV (KVM nested APIv1) and PowerVM (KVM nested APIv2) platforms. + +:: + + struct kvm_ppc_compat_caps { + __u64 flags; /* Reserved for future use */ + __u64 size; /* Size of this structure */ + __u64 compat_capabilities; /* Capabilities supported by the host */ + }; + +The ``compat_capabilities`` bit field describes the processor compatibility +modes supported by the host. For example, the following bits indicate support +for specific processor modes. + +:: + + KVM_PPC_COMPAT_CAP_POWER9 (bit 1): KVM guests can run in Power9 processor mode + KVM_PPC_COMPAT_CAP_POWER10 (bit 2): KVM guests can run in Power10 processor mode + KVM_PPC_COMPAT_CAP_POWER11 (bit 3): KVM guests can run in Power11 processor mode + .. _kvm_run: 5. The kvm_run structure
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [PATCH v4 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] UAPI backwards compatibility breakage due to invalid struct size validation. - [High] Reserved…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] UAPI backwards compatibility breakage due to invalid struct size validation.
- [High] Reserved flags field is not checked for zero, rendering it permanently unusable for future input.
- [High] Capability mapping logic lacks fallthrough, artificially restricting nested guests.
- [High] Resource leak of device_node refcount in kvmppc_get_compat_caps().
- [Medium] Misleading documentation causing userspace to pass uninitialized memory to the kernel.
- [Medium] UAPI misconfiguration: IOCTL direction is _IOR but kernel reads from userspace.
- [Medium] Inconsistent error return code on non-pseries platforms contradicts documentation. --
commit 8e3e8114a54d0e9097662db3db8e0e1e2ee2506b
Author: Amit Machhiwal <[email protected]>
KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
This commit adds documentation for the KVM_PPC_GET_COMPAT_CAPS ioctl in the KVM API. It details the capabilities, structure fields, and expected error codes.
Quoted 5 lines
Documentation/virt/kvm/api.rst +0
-0
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6561
Quoted 9 lines
Documentation/virt/kvm/api.rst +6
-0@@ -6553,6 +6553,53 @@ KVM_S390_KEYOP_SSKE Sets the storage key for the guest address ``guest_addr`` to the key specified in ``key``, returning the previous value in ``key``. +4.145 KVM_PPC_GET_COMPAT_CAPS +----------------------------- +:Capability: KVM_CAP_PPC_COMPAT_CAPS +:Architectures: powerpc +:Type: vm ioctl +:Parameters: struct kvm_ppc_compat_caps (out)
[Severity: Medium]
Is this parameter accurately described as strictly output-only?
Looking at the existing implementation, the kernel reads the size field from userspace:
arch/powerpc/kvm/powerpc.c:kvm_arch_vm_ioctl() {
...
if (copy_from_user(&user_size, &((struct kvm_ppc_compat_caps
__user *)argp)->size, sizeof(user_size))) {
...
}
If userspace leaves the struct uninitialized based on this documentation, the kernel will read random memory for size, leading to -EINVAL failures. Should the documentation indicate this is an input/output parameter?
[Severity: Medium]
This isn't a bug introduced by this patch, but should the ioctl direction be updated?
The IOCTL is currently defined as read-only:
include/uapi/linux/kvm.h:
#define KVM_PPC_GET_COMPAT_CAPS _IOR(KVMIO, 0xe4, struct kvm_ppc_compat_caps)
Since the kernel reads the size field from userspace, this violates conventions and can cause issues with tracing tools. Should it be defined with _IOWR?
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6571
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Returns: 0 on success, negative value on failure + +Errors include: + + ======== ============================================================ + EFAULT if ``struct kvm_ppc_compat_caps`` cannot be read from or + written to userspace + EINVAL if the ``size`` field is smaller than the current structure + size, or if the backend implementation fails to retrieve or + map CPU compatibility capabilities
[Severity: High]
This isn't a bug introduced by this patch, but could the current size validation break backwards compatibility?
The existing implementation in arch/powerpc/kvm/powerpc.c rejects requests if the size is smaller than the host structure:
arch/powerpc/kvm/powerpc.c:kvm_arch_vm_ioctl() {
...
if (user_size < sizeof(host_caps))
goto out;
...
}
If struct kvm_ppc_compat_caps is ever extended, older userspace binaries passing the old, smaller structure size will be unconditionally rejected.
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6575
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+ ENOTTY if the backend does not implement the ``get_compat_caps`` + operation (e.g., on non-pseries platforms or when the + required KVM operations are not available) + ======== ============================================================
[Severity: Medium]
Will the existing implementation return -ENOTTY as documented here?
Looking at kvmppc_get_compat_caps(), it returns -EINVAL on non-pseries platforms:
arch/powerpc/kvm/book3s_hv.c:kvmppc_get_compat_caps() {
int rc = -EINVAL;
...
if (kvmhv_on_pseries()) { ... }
return rc;
}This could confuse userspace into thinking it passed an invalid argument size. Should the documentation or the code be adjusted?
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6588
Quoted 13 lines
Documentation/virt/kvm/api.rst +13
-0+ +IBM POWER system server-based processors provide a compatibility mode feature +where an Nth generation processor can operate in modes consistent with earlier +generations such as (N-1) and (N-2). + +This ioctl provides userspace with information about the CPU compatibility modes +supported by the current host processor for booting the nested KVM guests on +PowerNV (KVM nested APIv1) and PowerVM (KVM nested APIv2) platforms. + +:: + + struct kvm_ppc_compat_caps { + __u64 flags; /* Reserved for future use */
[Severity: High]
This isn't a bug introduced by this patch, but is the reserved flags field permanently unusable for future input?
The existing implementation skips copying and validating the flags field:
arch/powerpc/kvm/powerpc.c:kvm_arch_vm_ioctl() {
...
if (copy_from_user(&user_size, &((struct kvm_ppc_compat_caps
__user *)argp)->size, sizeof(user_size))) {
...
}
Because the parameter is documented as (out) and the kernel ignores any uninitialized data in flags, a future kernel will not be able to safely repurpose flags for input. Should this validate that flags is zero?
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6601
Quoted 13 lines
Documentation/virt/kvm/api.rst +13
-0+ __u64 size; /* Size of this structure */ + __u64 compat_capabilities; /* Capabilities supported by the host */ + }; + +The ``compat_capabilities`` bit field describes the processor compatibility +modes supported by the host. For example, the following bits indicate support +for specific processor modes. + +:: + + KVM_PPC_COMPAT_CAP_POWER9 (bit 1): KVM guests can run in Power9 processor mode + KVM_PPC_COMPAT_CAP_POWER10 (bit 2): KVM guests can run in Power10 processor mode + KVM_PPC_COMPAT_CAP_POWER11 (bit 3): KVM guests can run in Power11 processor mode
[Severity: High]
This isn't a bug introduced by this patch, but can the capability mapping logic falsely restrict nested guests?
The switch statement lacks fallthrough annotations:
arch/powerpc/kvm/book3s_hv.c:kvmppc_map_compat_capabilities() {
...
case PVR_ARCH_31_P11:
*capabilities |= KVM_PPC_COMPAT_CAP_POWER11;
break;
...
}This causes the host to report a capability mask with only a single bit, indicating it cannot run nested guests in older compatibility modes.
[Severity: High]
This is a pre-existing issue, but are we missing a reference count cleanup here?
In the compatibility capabilities retrieval:
arch/powerpc/kvm/book3s_hv.c:kvmppc_get_compat_caps() {
...
for_each_node_by_type(np, "cpu") {
prop = of_get_property(np, "cpu-version", NULL);
if (prop) {
cpu_version = be32_to_cpup(prop);
break;
}
}
...
}The for_each_node_by_type macro takes a reference count on the device node. Exiting the loop early via break requires manually dropping the reference via of_node_put(np). Does this leak a reference on every ioctl call?
Quoted 3 lines
Documentation/virt/kvm/api.rst +0
-0.. _kvm_run: 5. The kvm_run structure
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v4 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
In reply to Amit MachhiwalHi Amit,
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 41 lines · Amit Machhiwal
Add documentation for the
KVM_PPC_GET_COMPAT_CAPSioctl to the KVM API documentation.The ioctl exposes host processor compatibility modes supported for nested KVM guests on PowerPC systems. The documentation includes comprehensive error code descriptions, structure field definitions including the size field for forward compatibility, and KVM-specific capability bit constants.
Signed-off-by Amit Machhiwal <[email protected]>--- Documentation/virt/kvm/api.rst | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)Documentation/virt/kvm/api.rst +19
-0@@ -6553,6 +6553,53 @@ KVM_S390_KEYOP_SSKE Sets the storage key for the guest address ``guest_addr`` to the key specified in ``key``, returning the previous value in ``key``. +4.145 KVM_PPC_GET_COMPAT_CAPS +----------------------------- +:Capability: KVM_CAP_PPC_COMPAT_CAPS +:Architectures: powerpc +:Type: vm ioctl +:Parameters: struct kvm_ppc_compat_caps (out) +:Returns: 0 on success, negative value on failure + +Errors include: + + ======== ============================================================ + EFAULT if ``struct kvm_ppc_compat_caps`` cannot be read from or + written to userspace + EINVAL if the ``size`` field is smaller than the current structure + size, or if the backend implementation fails to retrieve or + map CPU compatibility capabilities + ENOTTY if the backend does not implement the ``get_compat_caps`` + operation (e.g., on non-pseries platforms or when the + required KVM operations are not available)
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6584
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ ======== ============================================================ + +IBM POWER system server-based processors provide a compatibility mode feature +where an Nth generation processor can operate in modes consistent with earlier +generations such as (N-1) and (N-2). + +This ioctl provides userspace with information about the CPU compatibility modes +supported by the current host processor for booting the nested KVM guests on +PowerNV (KVM nested APIv1) and PowerVM (KVM nested APIv2) platforms. +
Please add a detail on how returned 'size' field can be less than what the userspace has sent and how it should be handled.
Quoted 24 lines
Documentation/virt/kvm/api.rst +18
-0+:: + + struct kvm_ppc_compat_caps { + __u64 flags; /* Reserved for future use */ + __u64 size; /* Size of this structure */ + __u64 compat_capabilities; /* Capabilities supported by the host */ + }; + +The ``compat_capabilities`` bit field describes the processor compatibility +modes supported by the host. For example, the following bits indicate support +for specific processor modes. + +:: + + KVM_PPC_COMPAT_CAP_POWER9 (bit 1): KVM guests can run in Power9 processor mode + KVM_PPC_COMPAT_CAP_POWER10 (bit 2): KVM guests can run in Power10 processor mode + KVM_PPC_COMPAT_CAP_POWER11 (bit 3): KVM guests can run in Power11 processor mode + .. _kvm_run: 5. The kvm_run structureSignature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Cheers ~ Vaibhav
Re: [PATCH v4 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
Hi Vaibhav,
Thanks for reviewing this patch. Please find my response below.
Quoted 53 lines · Vaibhav Jain
Hi Amit,
Thanks for the patch and incorporating V3 review comments. Further review comments inline below:
Quoted 46 lines · Amit Machhiwal
Add documentation for the
KVM_PPC_GET_COMPAT_CAPSioctl to the KVM API documentation.The ioctl exposes host processor compatibility modes supported for nested KVM guests on PowerPC systems. The documentation includes comprehensive error code descriptions, structure field definitions including the size field for forward compatibility, and KVM-specific capability bit constants.
Signed-off-by Amit Machhiwal <[email protected]>--- Documentation/virt/kvm/api.rst | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)Documentation/virt/kvm/api.rst +24
-0@@ -6553,6 +6553,53 @@ KVM_S390_KEYOP_SSKE Sets the storage key for the guest address ``guest_addr`` to the key specified in ``key``, returning the previous value in ``key``. +4.145 KVM_PPC_GET_COMPAT_CAPS +----------------------------- +:Capability: KVM_CAP_PPC_COMPAT_CAPS +:Architectures: powerpc +:Type: vm ioctl +:Parameters: struct kvm_ppc_compat_caps (out) +:Returns: 0 on success, negative value on failure + +Errors include: + + ======== ============================================================ + EFAULT if ``struct kvm_ppc_compat_caps`` cannot be read from or + written to userspace + EINVAL if the ``size`` field is smaller than the current structure + size, or if the backend implementation fails to retrieve or + map CPU compatibility capabilities + ENOTTY if the backend does not implement the ``get_compat_caps`` + operation (e.g., on non-pseries platforms or when the + required KVM operations are not available) + ======== ============================================================ + +IBM POWER system server-based processors provide a compatibility mode feature +where an Nth generation processor can operate in modes consistent with earlier +generations such as (N-1) and (N-2).
Quoted 8 lines
Documentation/virt/kvm/api.rst KVM_S390_KEYOP_SSKE · line 6584
Quoted 5 lines
Documentation/virt/kvm/api.rst +5
-0+ +This ioctl provides userspace with information about the CPU compatibility modes +supported by the current host processor for booting the nested KVM guests on +PowerNV (KVM nested APIv1) and PowerVM (KVM nested APIv2) platforms. +Please add a detail on how returned 'size' field can be less than what the userspace has sent and how it should be handled.
Good point! However, with the suggested change from patch-1 (using strict equality check user_size != sizeof(host_caps), the returned size will always equal what userspace sent, because we reject any size mismatch with -EINVAL.
I'll update the documentation to clarify the strict size matching behavior:
- Userspace must set size to sizeof(struct kvm_ppc_compat_caps) before the ioctl
- The kernel validates this with strict equality (!= check)
- Returns -EINVAL if size doesn't match
- On success, the returned size will always equal what was sent
This should eliminates the forward-compatibility scenario where returned size could differ from sent size.
Thank,
Amit
Quoted 29 lines
Quoted 25 lines
Documentation/virt/kvm/api.rst +18
-0+:: + + struct kvm_ppc_compat_caps { + __u64 flags; /* Reserved for future use */ + __u64 size; /* Size of this structure */ + __u64 compat_capabilities; /* Capabilities supported by the host */ + }; + +The ``compat_capabilities`` bit field describes the processor compatibility +modes supported by the host. For example, the following bits indicate support +for specific processor modes. + +:: + + KVM_PPC_COMPAT_CAP_POWER9 (bit 1): KVM guests can run in Power9 processor mode + KVM_PPC_COMPAT_CAP_POWER10 (bit 2): KVM guests can run in Power10 processor mode + KVM_PPC_COMPAT_CAP_POWER11 (bit 3): KVM guests can run in Power11 processor mode + .. _kvm_run: 5. The kvm_run structureSignature · 1 line
2.50.1 (Apple Git-155)Signature · 2 lines
Cheers ~ Vaibhav