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 <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>
This commit is contained in:
Pratik R. Sampat 2025-03-05 16:59:54 -06:00 committed by Sean Christopherson
parent c4e1a848d7
commit acf0643450
2 changed files with 36 additions and 20 deletions

View 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,

View 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");