Skip to content

Commit 2f5213b

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Use magic value to signal ucall_alloc() failure
Use a magic value to signal a ucall_alloc() failure instead of simply doing GUEST_ASSERT(). GUEST_ASSERT() relies on ucall_alloc() and so a failure puts the guest into an infinite loop. Use -1 as the magic value, as a real ucall struct should never wrap. Reported-by: Oliver Upton <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent db7b780 commit 2f5213b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tools/testing/selftests/kvm/lib/ucall_common.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "linux/bitmap.h"
55
#include "linux/atomic.h"
66

7+
#define GUEST_UCALL_FAILED -1
8+
79
struct ucall_header {
810
DECLARE_BITMAP(in_use, KVM_MAX_VCPUS);
911
struct ucall ucalls[KVM_MAX_VCPUS];
@@ -41,7 +43,8 @@ static struct ucall *ucall_alloc(void)
4143
struct ucall *uc;
4244
int i;
4345

44-
GUEST_ASSERT(ucall_pool);
46+
if (!ucall_pool)
47+
goto ucall_failed;
4548

4649
for (i = 0; i < KVM_MAX_VCPUS; ++i) {
4750
if (!test_and_set_bit(i, ucall_pool->in_use)) {
@@ -51,7 +54,13 @@ static struct ucall *ucall_alloc(void)
5154
}
5255
}
5356

54-
GUEST_ASSERT(0);
57+
ucall_failed:
58+
/*
59+
* If the vCPU cannot grab a ucall structure, make a bare ucall with a
60+
* magic value to signal to get_ucall() that things went sideways.
61+
* GUEST_ASSERT() depends on ucall_alloc() and so cannot be used here.
62+
*/
63+
ucall_arch_do_ucall(GUEST_UCALL_FAILED);
5564
return NULL;
5665
}
5766

@@ -93,6 +102,9 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
93102

94103
addr = ucall_arch_get_ucall(vcpu);
95104
if (addr) {
105+
TEST_ASSERT(addr != (void *)GUEST_UCALL_FAILED,
106+
"Guest failed to allocate ucall struct");
107+
96108
memcpy(uc, addr, sizeof(*uc));
97109
vcpu_run_complete_io(vcpu);
98110
} else {

0 commit comments

Comments
 (0)