Skip to contentSkip to conversation

KVM: selftests: Refactor stable TSC check to use TEST_REQUIRE()

PATCH In review Sean Christopherson 1 participant 2 messages Apr 6, 2023 – Jun 2, 2023

Subsystems (2)
lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 KVM: selftests: Refactor stable TSC check to use TEST_REQUIRE()

Refactor the nested TSC scaling test's check on a stable system TSC to use TEST_REQUIRE() to do the heavy lifting when the system doesn't have a stable TSC. Using a helper+TEST_REQUIRE() eliminates the need for gotos and a custom message.

---
 .../kvm/x86_64/vmx_nested_tsc_scaling_test.c  | 22 ++++++-------------
 1 file changed, 7 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/x86_64/vmx_nested_tsc_scaling_test.c +7 -15
index fa03c8d1ce4e..e710b6e7fb38 100644
@@ -116,29 +116,21 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
 	GUEST_DONE();
 }
 
-static void stable_tsc_check_supported(void)
+static bool system_has_stable_tsc(void)
 {
+	bool tsc_is_stable;
 	FILE *fp;
 	char buf[4];
 
 	fp = fopen("/sys/devices/system/clocksource/clocksource0/current_clocksource", "r");
 	if (fp == NULL)
-		goto skip_test;
+		return false;
 
-	if (fgets(buf, sizeof(buf), fp) == NULL)
-		goto close_fp;
+	tsc_is_stable = fgets(buf, sizeof(buf), fp) &&
+			!strncmp(buf, "tsc", sizeof(buf));
 
-	if (strncmp(buf, "tsc", sizeof(buf)))
-		goto close_fp;
-
-	fclose(fp);
-	return;
-
-close_fp:
 	fclose(fp);
-skip_test:
-	print_skip("Kernel does not use TSC clocksource - assuming that host TSC is not stable");
-	exit(KSFT_SKIP);
+	return tsc_is_stable;
 }
 
 int main(int argc, char *argv[])
@@ -156,7 +148,7 @@ int main(int argc, char *argv[])
 
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_TSC_CONTROL));
-	stable_tsc_check_supported();
+	TEST_REQUIRE(system_has_stable_tsc());
 
 	/*
 	 * We set L1's scale factor to be a random number from 2 to 10.

base-commit: 7712145073876092e9aa81f0b836fef8b5694b14

Signature · 1 line
2.40.0.348.gf938b09366-goog
author reviewer
#1 lore raw
Quoted 6 lines · Sean Christopherson

Refactor the nested TSC scaling test's check on a stable system TSC to use TEST_REQUIRE() to do the heavy lifting when the system doesn't have a stable TSC. Using a helper+TEST_REQUIRE() eliminates the need for gotos and a custom message.

Applied to kvm-x86 selftests, thanks!

[1/1] KVM: selftests: Refactor stable TSC check to use TEST_REQUIRE()

      https://github.com/kvm-x86/linux/commit/5efde6d73d58

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes