Commit 197ebb71 authored by Vishal Annapurve's avatar Vishal Annapurve Committed by Sean Christopherson

KVM: selftests: move common startup logic to kvm_util.c

Consolidate common startup logic in one place by implementing a single
setup function with __attribute((constructor)) for all selftests within
kvm_util.c.

This allows moving logic like:
        /* Tell stdout not to buffer its content */
        setbuf(stdout, NULL);
to a single file for all selftests.

This will also allow any required setup at entry in future to be done in
common main function.

Link: https://lore.kernel.org/lkml/Ywa9T+jKUpaHLu%2Fl@google.comSuggested-by: default avatarSean Christopherson <seanjc@google.com>
Reviewed-by: default avatarAndrew Jones <andrew.jones@linux.dev>
Reviewed-by: default avatarPeter Gonda <pgonda@google.com>
Signed-off-by: default avatarVishal Annapurve <vannapurve@google.com>
Link: https://lore.kernel.org/r/20221115213845.3348210-2-vannapurve@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 96b69958
...@@ -446,9 +446,6 @@ int main(int argc, char *argv[]) ...@@ -446,9 +446,6 @@ int main(int argc, char *argv[])
{ {
struct kvm_vm *vm; struct kvm_vm *vm;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
if (!parse_args(argc, argv)) if (!parse_args(argc, argv))
exit(KSFT_SKIP); exit(KSFT_SKIP);
......
...@@ -305,8 +305,6 @@ static void test_run(void) ...@@ -305,8 +305,6 @@ static void test_run(void)
int main(void) int main(void)
{ {
setbuf(stdout, NULL);
test_run(); test_run();
return 0; return 0;
} }
...@@ -817,9 +817,6 @@ int main(int argc, char **argv) ...@@ -817,9 +817,6 @@ int main(int argc, char **argv)
int opt; int opt;
bool eoi_split = false; bool eoi_split = false;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) { while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
switch (opt) { switch (opt) {
case 'n': case 'n':
......
...@@ -2086,3 +2086,9 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, ...@@ -2086,3 +2086,9 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
break; break;
} }
} }
void __attribute((constructor)) kvm_selftest_init(void)
{
/* Tell stdout not to buffer its content. */
setbuf(stdout, NULL);
}
...@@ -994,9 +994,6 @@ int main(int argc, char *argv[]) ...@@ -994,9 +994,6 @@ int main(int argc, char *argv[])
struct test_result rbestslottime; struct test_result rbestslottime;
int tctr; int tctr;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
if (!parse_args(argc, argv, &targs)) if (!parse_args(argc, argv, &targs))
return -1; return -1;
......
...@@ -205,9 +205,6 @@ int main(int argc, char *argv[]) ...@@ -205,9 +205,6 @@ int main(int argc, char *argv[])
struct kvm_vcpu *vcpu; struct kvm_vcpu *vcpu;
u32 cpu, rseq_cpu; u32 cpu, rseq_cpu;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask); r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask);
TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)", errno, TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)", errno,
strerror(errno)); strerror(errno));
......
...@@ -760,8 +760,6 @@ int main(int argc, char *argv[]) ...@@ -760,8 +760,6 @@ int main(int argc, char *argv[])
TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_MEM_OP)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_MEM_OP));
setbuf(stdout, NULL); /* Tell stdout not to buffer its content */
ksft_print_header(); ksft_print_header();
ksft_set_plan(ARRAY_SIZE(testlist)); ksft_set_plan(ARRAY_SIZE(testlist));
......
...@@ -296,8 +296,6 @@ int main(int argc, char *argv[]) ...@@ -296,8 +296,6 @@ int main(int argc, char *argv[])
bool has_s390_vcpu_resets = kvm_check_cap(KVM_CAP_S390_VCPU_RESETS); bool has_s390_vcpu_resets = kvm_check_cap(KVM_CAP_S390_VCPU_RESETS);
int idx; int idx;
setbuf(stdout, NULL); /* Tell stdout not to buffer its content */
ksft_print_header(); ksft_print_header();
ksft_set_plan(ARRAY_SIZE(testlist)); ksft_set_plan(ARRAY_SIZE(testlist));
......
...@@ -231,9 +231,6 @@ int main(int argc, char *argv[]) ...@@ -231,9 +231,6 @@ int main(int argc, char *argv[])
TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
ksft_print_header(); ksft_print_header();
ksft_set_plan(ARRAY_SIZE(testlist)); ksft_set_plan(ARRAY_SIZE(testlist));
......
...@@ -392,9 +392,6 @@ int main(int argc, char *argv[]) ...@@ -392,9 +392,6 @@ int main(int argc, char *argv[])
int i, loops; int i, loops;
#endif #endif
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
#ifdef __x86_64__ #ifdef __x86_64__
/* /*
* FIXME: the zero-memslot test fails on aarch64 and s390x because * FIXME: the zero-memslot test fails on aarch64 and s390x because
......
...@@ -57,9 +57,6 @@ int main(int argc, char *argv[]) ...@@ -57,9 +57,6 @@ int main(int argc, char *argv[])
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE)); TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE));
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
run = vcpu->run; run = vcpu->run;
......
...@@ -157,9 +157,6 @@ int main(int argc, char *argv[]) ...@@ -157,9 +157,6 @@ int main(int argc, char *argv[])
uint64_t gpa; uint64_t gpa;
int rc; int rc;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR));
vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
......
...@@ -134,9 +134,6 @@ int main(int argc, char *argv[]) ...@@ -134,9 +134,6 @@ int main(int argc, char *argv[])
const struct kvm_cpuid2 *hv_cpuid_entries; const struct kvm_cpuid2 *hv_cpuid_entries;
struct kvm_vcpu *vcpu; struct kvm_vcpu *vcpu;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
......
...@@ -72,9 +72,6 @@ int main(int argc, char *argv[]) ...@@ -72,9 +72,6 @@ int main(int argc, char *argv[])
struct kvm_vm *vm; struct kvm_vm *vm;
uint64_t msr_platform_info; uint64_t msr_platform_info;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO));
vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
......
...@@ -447,9 +447,6 @@ int main(int argc, char *argv[]) ...@@ -447,9 +447,6 @@ int main(int argc, char *argv[])
struct kvm_vcpu *vcpu; struct kvm_vcpu *vcpu;
struct kvm_vm *vm; struct kvm_vm *vm;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
TEST_REQUIRE(kvm_has_cap(KVM_CAP_PMU_EVENT_FILTER)); TEST_REQUIRE(kvm_has_cap(KVM_CAP_PMU_EVENT_FILTER));
TEST_REQUIRE(use_intel_pmu() || use_amd_pmu()); TEST_REQUIRE(use_intel_pmu() || use_amd_pmu());
......
...@@ -82,9 +82,6 @@ int main(int argc, char *argv[]) ...@@ -82,9 +82,6 @@ int main(int argc, char *argv[])
uint64_t cr4; uint64_t cr4;
int rc; int rc;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
/* /*
* Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and
* use it to verify all supported CR4 bits can be set prior to defining * use it to verify all supported CR4 bits can be set prior to defining
......
...@@ -194,9 +194,6 @@ static void run_test(bool is_nmi) ...@@ -194,9 +194,6 @@ static void run_test(bool is_nmi)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM)); TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
TEST_ASSERT(kvm_cpu_has(X86_FEATURE_NRIPS), TEST_ASSERT(kvm_cpu_has(X86_FEATURE_NRIPS),
......
...@@ -90,9 +90,6 @@ int main(int argc, char *argv[]) ...@@ -90,9 +90,6 @@ int main(int argc, char *argv[])
struct kvm_vcpu_events events; struct kvm_vcpu_events events;
int rv, cap; int rv, cap;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
cap = kvm_check_cap(KVM_CAP_SYNC_REGS); cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS); TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS);
TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD)); TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD));
......
...@@ -56,9 +56,6 @@ int main(int argc, char *argv[]) ...@@ -56,9 +56,6 @@ int main(int argc, char *argv[])
struct kvm_vm *vm; struct kvm_vm *vm;
struct ucall uc; struct ucall uc;
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
run = vcpu->run; run = vcpu->run;
......
...@@ -818,9 +818,6 @@ static void test_user_exit_msr_flags(void) ...@@ -818,9 +818,6 @@ static void test_user_exit_msr_flags(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
test_msr_filter_allow(); test_msr_filter_allow();
test_msr_filter_deny(); test_msr_filter_deny();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment