Skip to content

Commit 7dd32a0

Browse files
Dave MartinMarc Zyngier
authored andcommitted
KVM: arm/arm64: Add KVM_ARM_VCPU_FINALIZE ioctl
Some aspects of vcpu configuration may be too complex to be completed inside KVM_ARM_VCPU_INIT. Thus, there may be a requirement for userspace to do some additional configuration before various other ioctls will work in a consistent way. In particular this will be the case for SVE, where userspace will need to negotiate the set of vector lengths to be made available to the guest before the vcpu becomes fully usable. In order to provide an explicit way for userspace to confirm that it has finished setting up a particular vcpu feature, this patch adds a new ioctl KVM_ARM_VCPU_FINALIZE. When userspace has opted into a feature that requires finalization, typically by means of a feature flag passed to KVM_ARM_VCPU_INIT, a matching call to KVM_ARM_VCPU_FINALIZE is now required before KVM_RUN or KVM_GET_REG_LIST is allowed. Individual features may impose additional restrictions where appropriate. No existing vcpu features are affected by this, so current userspace implementations will continue to work exactly as before, with no need to issue KVM_ARM_VCPU_FINALIZE. As implemented in this patch, KVM_ARM_VCPU_FINALIZE is currently a placeholder: no finalizable features exist yet, so ioctl is not required and will always yield EINVAL. Subsequent patches will add the finalization logic to make use of this ioctl for SVE. No functional change for existing userspace. Signed-off-by: Dave Martin <Dave.Martin@arm.com> Reviewed-by: Julien Thierry <julien.thierry@arm.com> Tested-by: zhang.lei <zhang.lei@jp.fujitsu.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 0f062bf commit 7dd32a0

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎arch/arm/include/asm/kvm_host.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef __ARM_KVM_HOST_H__
2020
#define __ARM_KVM_HOST_H__
2121

22+
#include <linux/errno.h>
2223
#include <linux/types.h>
2324
#include <linux/kvm_types.h>
2425
#include <asm/cputype.h>
@@ -411,4 +412,7 @@ static inline int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)
411412
return 0;
412413
}
413414

415+
#define kvm_arm_vcpu_finalize(vcpu, what) (-EINVAL)
416+
#define kvm_arm_vcpu_is_finalized(vcpu) true
417+
414418
#endif /* __ARM_KVM_HOST_H__ */

‎arch/arm64/include/asm/kvm_host.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define __ARM64_KVM_HOST_H__
2424

2525
#include <linux/bitmap.h>
26+
#include <linux/errno.h>
2627
#include <linux/types.h>
2728
#include <linux/jump_label.h>
2829
#include <linux/kvm_types.h>
@@ -625,4 +626,7 @@ void kvm_arch_free_vm(struct kvm *kvm);
625626

626627
int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type);
627628

629+
#define kvm_arm_vcpu_finalize(vcpu, what) (-EINVAL)
630+
#define kvm_arm_vcpu_is_finalized(vcpu) true
631+
628632
#endif /* __ARM64_KVM_HOST_H__ */

‎include/uapi/linux/kvm.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,9 @@ struct kvm_enc_region {
14411441
/* Available with KVM_CAP_HYPERV_CPUID */
14421442
#define KVM_GET_SUPPORTED_HV_CPUID _IOWR(KVMIO, 0xc1, struct kvm_cpuid2)
14431443

1444+
/* Available with KVM_CAP_ARM_SVE */
1445+
#define KVM_ARM_VCPU_FINALIZE _IOW(KVMIO, 0xc2, int)
1446+
14441447
/* Secure Encrypted Virtualization command */
14451448
enum sev_cmd_id {
14461449
/* Guest initialization commands */

‎virt/kvm/arm/arm.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
545545
if (likely(vcpu->arch.has_run_once))
546546
return 0;
547547

548+
if (!kvm_arm_vcpu_is_finalized(vcpu))
549+
return -EPERM;
550+
548551
vcpu->arch.has_run_once = true;
549552

550553
if (likely(irqchip_in_kernel(kvm))) {
@@ -1116,6 +1119,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
11161119
if (unlikely(!kvm_vcpu_initialized(vcpu)))
11171120
break;
11181121

1122+
r = -EPERM;
1123+
if (!kvm_arm_vcpu_is_finalized(vcpu))
1124+
break;
1125+
11191126
r = -EFAULT;
11201127
if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
11211128
break;
@@ -1169,6 +1176,17 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
11691176

11701177
return kvm_arm_vcpu_set_events(vcpu, &events);
11711178
}
1179+
case KVM_ARM_VCPU_FINALIZE: {
1180+
int what;
1181+
1182+
if (!kvm_vcpu_initialized(vcpu))
1183+
return -ENOEXEC;
1184+
1185+
if (get_user(what, (const int __user *)argp))
1186+
return -EFAULT;
1187+
1188+
return kvm_arm_vcpu_finalize(vcpu, what);
1189+
}
11721190
default:
11731191
r = -EINVAL;
11741192
}

0 commit comments

Comments
 (0)