Skip to content

Commit acf0643

Browse files
pratiksampatsean-jc
authored andcommitted
KVM: selftests: Add SMT control state helper
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: prepend is_ to the helpers] Signed-off-by: Sean Christopherson <[email protected]>
1 parent c4e1a84 commit acf0643

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

tools/testing/selftests/kvm/include/kvm_util.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,41 @@ void kvm_get_stat(struct kvm_binary_stats *stats, const char *name,
549549
#define vm_get_stat(vm, stat) __get_stat(&(vm)->stats, stat)
550550
#define vcpu_get_stat(vcpu, stat) __get_stat(&(vcpu)->stats, stat)
551551

552+
static inline bool read_smt_control(char *buf, size_t buf_size)
553+
{
554+
FILE *f = fopen("/sys/devices/system/cpu/smt/control", "r");
555+
bool ret;
556+
557+
if (!f)
558+
return false;
559+
560+
ret = fread(buf, sizeof(*buf), buf_size, f) > 0;
561+
fclose(f);
562+
563+
return ret;
564+
}
565+
566+
static inline bool is_smt_possible(void)
567+
{
568+
char buf[16];
569+
570+
if (read_smt_control(buf, sizeof(buf)) &&
571+
(!strncmp(buf, "forceoff", 8) || !strncmp(buf, "notsupported", 12)))
572+
return false;
573+
574+
return true;
575+
}
576+
577+
static inline bool is_smt_on(void)
578+
{
579+
char buf[16];
580+
581+
if (read_smt_control(buf, sizeof(buf)) && !strncmp(buf, "on", 2))
582+
return true;
583+
584+
return false;
585+
}
586+
552587
void vm_create_irqchip(struct kvm_vm *vm);
553588

554589
static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,

tools/testing/selftests/kvm/x86/hyperv_cpuid.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@ static void guest_code(void)
2222
{
2323
}
2424

25-
static bool smt_possible(void)
26-
{
27-
char buf[16];
28-
FILE *f;
29-
bool res = true;
30-
31-
f = fopen("/sys/devices/system/cpu/smt/control", "r");
32-
if (f) {
33-
if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
34-
if (!strncmp(buf, "forceoff", 8) ||
35-
!strncmp(buf, "notsupported", 12))
36-
res = false;
37-
}
38-
fclose(f);
39-
}
40-
41-
return res;
42-
}
43-
4425
static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
4526
{
4627
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)
9374
case 0x40000004:
9475
test_val = entry->eax & (1UL << 18);
9576

96-
TEST_ASSERT(!!test_val == !smt_possible(),
77+
TEST_ASSERT(!!test_val == !is_smt_possible(),
9778
"NoNonArchitecturalCoreSharing bit"
9879
" doesn't reflect SMT setting");
9980

0 commit comments

Comments
 (0)