]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: selftests: Add SMT control state helper
authorPratik R. Sampat <prsampat@amd.com>
Wed, 5 Mar 2025 22:59:54 +0000 (16:59 -0600)
committerSean Christopherson <seanjc@google.com>
Fri, 2 May 2025 19:32:33 +0000 (12:32 -0700)
Move the SMT control check out of the hyperv_cpuid selftest so that it
is generally accessible all KVM selftests. Split the functionality into
a helper that populates a buffer with SMT control value which other
helpers can use to ascertain if SMT state is available and active.

Signed-off-by: Pratik R. Sampat <prsampat@amd.com>
Link: https://lore.kernel.org/r/20250305230000.231025-5-prsampat@amd.com
[sean: prepend is_ to the helpers]
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/kvm_util.h
tools/testing/selftests/kvm/x86/hyperv_cpuid.c

index 373912464fb4036de21eb628fb9045060ef3edbb..a1fc52bbdf7a64d7de6e1e801bde596e48966bbc 100644 (file)
@@ -549,6 +549,41 @@ void kvm_get_stat(struct kvm_binary_stats *stats, const char *name,
 #define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat)
 #define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat)
 
+static inline bool read_smt_control(char *buf, size_t buf_size)
+{
+       FILE *f = fopen("/sys/devices/system/cpu/smt/control", "r");
+       bool ret;
+
+       if (!f)
+               return false;
+
+       ret = fread(buf, sizeof(*buf), buf_size, f) > 0;
+       fclose(f);
+
+       return ret;
+}
+
+static inline bool is_smt_possible(void)
+{
+       char buf[16];
+
+       if (read_smt_control(buf, sizeof(buf)) &&
+           (!strncmp(buf, "forceoff", 8) || !strncmp(buf, "notsupported", 12)))
+               return false;
+
+       return true;
+}
+
+static inline bool is_smt_on(void)
+{
+       char buf[16];
+
+       if (read_smt_control(buf, sizeof(buf)) && !strncmp(buf, "on", 2))
+               return true;
+
+       return false;
+}
+
 void vm_create_irqchip(struct kvm_vm *vm);
 
 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
index 4e920705681aef6b9311287ce3dfcf6b00d3a62a..c863a689aa9850545d3636a96e80811da0e12c2e 100644 (file)
@@ -22,25 +22,6 @@ static void guest_code(void)
 {
 }
 
-static bool smt_possible(void)
-{
-       char buf[16];
-       FILE *f;
-       bool res = true;
-
-       f = fopen("/sys/devices/system/cpu/smt/control", "r");
-       if (f) {
-               if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
-                       if (!strncmp(buf, "forceoff", 8) ||
-                           !strncmp(buf, "notsupported", 12))
-                               res = false;
-               }
-               fclose(f);
-       }
-
-       return res;
-}
-
 static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 {
        const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
@@ -93,7 +74,7 @@ static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
                case 0x40000004:
                        test_val = entry->eax & (1UL << 18);
 
-                       TEST_ASSERT(!!test_val == !smt_possible(),
+                       TEST_ASSERT(!!test_val == !is_smt_possible(),
                                    "NoNonArchitecturalCoreSharing bit"
                                    " doesn't reflect SMT setting");