Skip to content

Commit e440c5f

Browse files
vittyvksean-jc
authored andcommitted
KVM: selftests: Generalize check_clocksource() from kvm_clock_test
Several existing x86 selftests need to check that the underlying system clocksource is TSC or based on TSC but every test implements its own check. As a first step towards unification, extract check_clocksource() from kvm_clock_test and split it into two functions: arch-neutral 'sys_get_cur_clocksource()' and x86-specific 'sys_clocksource_is_tsc()'. Fix a couple of pre-existing issues in kvm_clock_test: memory leakage in check_clocksource() and using TEST_ASSERT() instead of TEST_REQUIRE(). The change also makes the test fail when system clocksource can't be read from sysfs. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20240109141121.1619463-2-vkuznets@redhat.com [sean: eliminate if-elif pattern just to set a bool true] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c2a449a commit e440c5f

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,6 @@ __printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
195195

196196
char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
197197

198+
char *sys_get_cur_clocksource(void);
199+
198200
#endif /* SELFTEST_KVM_TEST_UTIL_H */

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,4 +1271,6 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
12711271
#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT)
12721272
#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT)
12731273

1274+
bool sys_clocksource_is_tsc(void);
1275+
12741276
#endif /* SELFTEST_KVM_PROCESSOR_H */

tools/testing/selftests/kvm/lib/test_util.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,28 @@ char *strdup_printf(const char *fmt, ...)
392392

393393
return str;
394394
}
395+
396+
#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource"
397+
398+
char *sys_get_cur_clocksource(void)
399+
{
400+
char *clk_name;
401+
struct stat st;
402+
FILE *fp;
403+
404+
fp = fopen(CLOCKSOURCE_PATH, "r");
405+
TEST_ASSERT(fp, "failed to open clocksource file, errno: %d", errno);
406+
407+
TEST_ASSERT(!fstat(fileno(fp), &st), "failed to stat clocksource file, errno: %d",
408+
errno);
409+
410+
clk_name = malloc(st.st_size);
411+
TEST_ASSERT(clk_name, "failed to allocate buffer to read file");
412+
413+
TEST_ASSERT(fgets(clk_name, st.st_size, fp), "failed to read clocksource file: %d",
414+
ferror(fp));
415+
416+
fclose(fp);
417+
418+
return clk_name;
419+
}

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,3 +1299,13 @@ void kvm_selftest_arch_init(void)
12991299
host_cpu_is_intel = this_cpu_is_intel();
13001300
host_cpu_is_amd = this_cpu_is_amd();
13011301
}
1302+
1303+
bool sys_clocksource_is_tsc(void)
1304+
{
1305+
char *clk_name = sys_get_cur_clocksource();
1306+
bool ret = !strcmp(clk_name, "tsc\n");
1307+
1308+
free(clk_name);
1309+
1310+
return ret;
1311+
}

tools/testing/selftests/kvm/x86_64/kvm_clock_test.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -132,42 +132,6 @@ static void enter_guest(struct kvm_vcpu *vcpu)
132132
}
133133
}
134134

135-
#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource"
136-
137-
static void check_clocksource(void)
138-
{
139-
char *clk_name;
140-
struct stat st;
141-
FILE *fp;
142-
143-
fp = fopen(CLOCKSOURCE_PATH, "r");
144-
if (!fp) {
145-
pr_info("failed to open clocksource file: %d; assuming TSC.\n",
146-
errno);
147-
return;
148-
}
149-
150-
if (fstat(fileno(fp), &st)) {
151-
pr_info("failed to stat clocksource file: %d; assuming TSC.\n",
152-
errno);
153-
goto out;
154-
}
155-
156-
clk_name = malloc(st.st_size);
157-
TEST_ASSERT(clk_name, "failed to allocate buffer to read file");
158-
159-
if (!fgets(clk_name, st.st_size, fp)) {
160-
pr_info("failed to read clocksource file: %d; assuming TSC.\n",
161-
ferror(fp));
162-
goto out;
163-
}
164-
165-
TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size),
166-
"clocksource not supported: %s", clk_name);
167-
out:
168-
fclose(fp);
169-
}
170-
171135
int main(void)
172136
{
173137
struct kvm_vcpu *vcpu;
@@ -179,7 +143,7 @@ int main(void)
179143
flags = kvm_check_cap(KVM_CAP_ADJUST_CLOCK);
180144
TEST_REQUIRE(flags & KVM_CLOCK_REALTIME);
181145

182-
check_clocksource();
146+
TEST_REQUIRE(sys_clocksource_is_tsc());
183147

184148
vm = vm_create_with_one_vcpu(&vcpu, guest_main);
185149

0 commit comments

Comments
 (0)