Skip to content

Commit d495f94

Browse files
committed
KVM: fix bad user ABI for KVM_EXIT_SYSTEM_EVENT
When KVM_EXIT_SYSTEM_EVENT was introduced, it included a flags member that at the time was unused. Unfortunately this extensibility mechanism has several issues: - x86 is not writing the member, so it would not be possible to use it on x86 except for new events - the member is not aligned to 64 bits, so the definition of the uAPI struct is incorrect for 32- on 64-bit userspace. This is a problem for RISC-V, which supports CONFIG_KVM_COMPAT, but fortunately usage of flags was only introduced in 5.18. Since padding has to be introduced, place a new field in there that tells if the flags field is valid. To allow further extensibility, in fact, change flags to an array of 16 values, and store how many of the values are valid. The availability of the new ndata field is tied to a system capability; all architectures are changed to fill in the field. To avoid breaking compilation of userspace that was using the flags field, provide a userspace-only union to overlap flags with data[0]. The new field is placed at the same offset for both 32- and 64-bit userspace. Cc: Will Deacon <will@kernel.org> Cc: Marc Zyngier <maz@kernel.org> Cc: Peter Gonda <pgonda@google.com> Cc: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reported-by: kernel test robot <lkp@intel.com> Message-Id: <20220422103013.34832-1-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 86931ff commit d495f94

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5986,16 +5986,16 @@ should put the acknowledged interrupt vector into the 'epr' field.
59865986
#define KVM_SYSTEM_EVENT_RESET 2
59875987
#define KVM_SYSTEM_EVENT_CRASH 3
59885988
__u32 type;
5989-
__u64 flags;
5989+
__u32 ndata;
5990+
__u64 data[16];
59905991
} system_event;
59915992

59925993
If exit_reason is KVM_EXIT_SYSTEM_EVENT then the vcpu has triggered
59935994
a system-level event using some architecture specific mechanism (hypercall
59945995
or some special instruction). In case of ARM64, this is triggered using
5995-
HVC instruction based PSCI call from the vcpu. The 'type' field describes
5996-
the system-level event type. The 'flags' field describes architecture
5997-
specific flags for the system-level event.
5996+
HVC instruction based PSCI call from the vcpu.
59985997

5998+
The 'type' field describes the system-level event type.
59995999
Valid values for 'type' are:
60006000

60016001
- KVM_SYSTEM_EVENT_SHUTDOWN -- the guest has requested a shutdown of the
@@ -6010,10 +6010,20 @@ Valid values for 'type' are:
60106010
to ignore the request, or to gather VM memory core dump and/or
60116011
reset/shutdown of the VM.
60126012

6013-
Valid flags are:
6013+
If KVM_CAP_SYSTEM_EVENT_DATA is present, the 'data' field can contain
6014+
architecture specific information for the system-level event. Only
6015+
the first `ndata` items (possibly zero) of the data array are valid.
60146016

6015-
- KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2 (arm64 only) -- the guest issued
6016-
a SYSTEM_RESET2 call according to v1.1 of the PSCI specification.
6017+
- for arm64, data[0] is set to KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2 if
6018+
the guest issued a SYSTEM_RESET2 call according to v1.1 of the PSCI
6019+
specification.
6020+
6021+
- for RISC-V, data[0] is set to the value of the second argument of the
6022+
``sbi_system_reset`` call.
6023+
6024+
Previous versions of Linux defined a `flags` member in this struct. The
6025+
field is now aliased to `data[0]`. Userspace can assume that it is only
6026+
written if ndata is greater than 0.
60176027

60186028
::
60196029

arch/arm64/kvm/psci.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
181181

182182
memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
183183
vcpu->run->system_event.type = type;
184-
vcpu->run->system_event.flags = flags;
184+
vcpu->run->system_event.ndata = 1;
185+
vcpu->run->system_event.data[0] = flags;
185186
vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
186187
}
187188

arch/riscv/kvm/vcpu_sbi.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run)
8383

8484
void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu,
8585
struct kvm_run *run,
86-
u32 type, u64 flags)
86+
u32 type, u64 reason)
8787
{
8888
unsigned long i;
8989
struct kvm_vcpu *tmp;
@@ -94,7 +94,8 @@ void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu,
9494

9595
memset(&run->system_event, 0, sizeof(run->system_event));
9696
run->system_event.type = type;
97-
run->system_event.flags = flags;
97+
run->system_event.ndata = 1;
98+
run->system_event.data[0] = reason;
9899
run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
99100
}
100101

arch/x86/kvm/x86.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10015,12 +10015,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1001510015
if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
1001610016
vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
1001710017
vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
10018+
vcpu->run->system_event.ndata = 0;
1001810019
r = 0;
1001910020
goto out;
1002010021
}
1002110022
if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
1002210023
vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
1002310024
vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
10025+
vcpu->run->system_event.ndata = 0;
1002410026
r = 0;
1002510027
goto out;
1002610028
}

include/uapi/linux/kvm.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,13 @@ struct kvm_run {
445445
#define KVM_SYSTEM_EVENT_RESET 2
446446
#define KVM_SYSTEM_EVENT_CRASH 3
447447
__u32 type;
448-
__u64 flags;
448+
__u32 ndata;
449+
union {
450+
#ifndef __KERNEL__
451+
__u64 flags;
452+
#endif
453+
__u64 data[16];
454+
};
449455
} system_event;
450456
/* KVM_EXIT_S390_STSI */
451457
struct {
@@ -1144,6 +1150,8 @@ struct kvm_ppc_resize_hpt {
11441150
#define KVM_CAP_S390_MEM_OP_EXTENSION 211
11451151
#define KVM_CAP_PMU_CAPABILITY 212
11461152
#define KVM_CAP_DISABLE_QUIRKS2 213
1153+
/* #define KVM_CAP_VM_TSC_CONTROL 214 */
1154+
#define KVM_CAP_SYSTEM_EVENT_DATA 215
11471155

11481156
#ifdef KVM_CAP_IRQ_ROUTING
11491157

virt/kvm/kvm_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4333,6 +4333,7 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
43334333
return 0;
43344334
#endif
43354335
case KVM_CAP_BINARY_STATS_FD:
4336+
case KVM_CAP_SYSTEM_EVENT_DATA:
43364337
return 1;
43374338
default:
43384339
break;

0 commit comments

Comments
 (0)