OSDN Git Service

KVM: selftests: move common startup logic to kvm_util.c
authorVishal Annapurve <vannapurve@google.com>
Tue, 15 Nov 2022 21:38:43 +0000 (21:38 +0000)
committerSean Christopherson <seanjc@google.com>
Thu, 17 Nov 2022 00:58:56 +0000 (16:58 -0800)
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.com
Suggested-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Reviewed-by: Peter Gonda <pgonda@google.com>
Signed-off-by: Vishal Annapurve <vannapurve@google.com>
Link: https://lore.kernel.org/r/20221115213845.3348210-2-vannapurve@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
20 files changed:
tools/testing/selftests/kvm/aarch64/arch_timer.c
tools/testing/selftests/kvm/aarch64/hypercalls.c
tools/testing/selftests/kvm/aarch64/vgic_irq.c
tools/testing/selftests/kvm/lib/kvm_util.c
tools/testing/selftests/kvm/memslot_perf_test.c
tools/testing/selftests/kvm/rseq_test.c
tools/testing/selftests/kvm/s390x/memop.c
tools/testing/selftests/kvm/s390x/resets.c
tools/testing/selftests/kvm/s390x/sync_regs_test.c
tools/testing/selftests/kvm/set_memory_region_test.c
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c
tools/testing/selftests/kvm/x86_64/emulator_error_test.c
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c
tools/testing/selftests/kvm/x86_64/platform_info_test.c
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
tools/testing/selftests/kvm/x86_64/set_sregs_test.c
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c
tools/testing/selftests/kvm/x86_64/sync_regs_test.c
tools/testing/selftests/kvm/x86_64/userspace_io_test.c
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c

index 54016cd..f2a9677 100644 (file)
@@ -446,9 +446,6 @@ int main(int argc, char *argv[])
 {
        struct kvm_vm *vm;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        if (!parse_args(argc, argv))
                exit(KSFT_SKIP);
 
index 3dceecf..bef1499 100644 (file)
@@ -305,8 +305,6 @@ static void test_run(void)
 
 int main(void)
 {
-       setbuf(stdout, NULL);
-
        test_run();
        return 0;
 }
index e0310eb..90d854e 100644 (file)
@@ -817,9 +817,6 @@ int main(int argc, char **argv)
        int opt;
        bool eoi_split = false;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
                switch (opt) {
                case 'n':
index 07c8edd..575a0c3 100644 (file)
@@ -2086,3 +2086,9 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
                break;
        }
 }
+
+void __attribute((constructor)) kvm_selftest_init(void)
+{
+       /* Tell stdout not to buffer its content. */
+       setbuf(stdout, NULL);
+}
index d771262..36b20ab 100644 (file)
@@ -994,9 +994,6 @@ int main(int argc, char *argv[])
        struct test_result rbestslottime;
        int tctr;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        if (!parse_args(argc, argv, &targs))
                return -1;
 
index 0e9e2b4..3045fdf 100644 (file)
@@ -205,9 +205,6 @@ int main(int argc, char *argv[])
        struct kvm_vcpu *vcpu;
        u32 cpu, rseq_cpu;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask);
        TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)", errno,
                    strerror(errno));
index 9113696..3fd81e5 100644 (file)
@@ -760,8 +760,6 @@ int main(int argc, char *argv[])
 
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_MEM_OP));
 
-       setbuf(stdout, NULL);   /* Tell stdout not to buffer its content */
-
        ksft_print_header();
 
        ksft_set_plan(ARRAY_SIZE(testlist));
index 1948608..e41e2cb 100644 (file)
@@ -296,8 +296,6 @@ int main(int argc, char *argv[])
        bool has_s390_vcpu_resets = kvm_check_cap(KVM_CAP_S390_VCPU_RESETS);
        int idx;
 
-       setbuf(stdout, NULL);   /* Tell stdout not to buffer its content */
-
        ksft_print_header();
        ksft_set_plan(ARRAY_SIZE(testlist));
 
index 3fdb6e2..2ddde41 100644 (file)
@@ -231,9 +231,6 @@ int main(int argc, char *argv[])
 
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        ksft_print_header();
 
        ksft_set_plan(ARRAY_SIZE(testlist));
index 85c16f0..2ef1d1b 100644 (file)
@@ -392,9 +392,6 @@ int main(int argc, char *argv[])
        int i, loops;
 #endif
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
 #ifdef __x86_64__
        /*
         * FIXME: the zero-memslot test fails on aarch64 and s390x because
index 4208487..1027a67 100644 (file)
@@ -57,9 +57,6 @@ int main(int argc, char *argv[])
 
        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);
        run = vcpu->run;
 
index 1abb347..d945e57 100644 (file)
@@ -157,9 +157,6 @@ int main(int argc, char *argv[])
        uint64_t gpa;
        int rc;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR));
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
index e804eb0..5c27efb 100644 (file)
@@ -134,9 +134,6 @@ int main(int argc, char *argv[])
        const struct kvm_cpuid2 *hv_cpuid_entries;
        struct kvm_vcpu *vcpu;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
index 76417c7..310a104 100644 (file)
@@ -72,9 +72,6 @@ int main(int argc, char *argv[])
        struct kvm_vm *vm;
        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));
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
index ea4e259..a6ffa24 100644 (file)
@@ -447,9 +447,6 @@ int main(int argc, char *argv[])
        struct kvm_vcpu *vcpu;
        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(use_intel_pmu() || use_amd_pmu());
index 2bb08bf..a284fce 100644 (file)
@@ -82,9 +82,6 @@ int main(int argc, char *argv[])
        uint64_t cr4;
        int rc;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        /*
         * 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
index e637d77..e497ace 100644 (file)
@@ -194,9 +194,6 @@ done:
 
 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_ASSERT(kvm_cpu_has(X86_FEATURE_NRIPS),
index 9b6db0b..d2f9b5b 100644 (file)
@@ -90,9 +90,6 @@ int main(int argc, char *argv[])
        struct kvm_vcpu_events events;
        int rv, cap;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
        TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS);
        TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD));
index 7316521..91076c9 100644 (file)
@@ -56,9 +56,6 @@ int main(int argc, char *argv[])
        struct kvm_vm *vm;
        struct ucall uc;
 
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        vm = vm_create_with_one_vcpu(&vcpu, guest_code);
        run = vcpu->run;
 
index fae9508..25fa553 100644 (file)
@@ -818,9 +818,6 @@ static void test_user_exit_msr_flags(void)
 
 int main(int argc, char *argv[])
 {
-       /* Tell stdout not to buffer its content */
-       setbuf(stdout, NULL);
-
        test_msr_filter_allow();
 
        test_msr_filter_deny();