[PATCH v2 1/2] KVM: selftests: Fail explicitly when schedstat cannot be read
From: Bingyu Xian
Date: Mon Nov 23 2026 - 22:33:51 EST
get_run_delay() currently passes a failed fopen() result directly to
fscanf(), and substitutes MIN_RUN_DELAY_NS when parsing fails. Steal-time
checks need real scheduling delay measurements, so neither dereferencing a
null stream nor returning a fabricated constant is suitable; repeated parse
failures also cause polling loops to spin indefinitely.
Assert that opening /proc/<tid>/schedstat and parsing both fields succeed.
Co-developed-by: Quan Zhou <zhouquan@xxxxxxxxxxx>
Signed-off-by: Quan Zhou <zhouquan@xxxxxxxxxxx>
Signed-off-by: Bingyu Xian <shanbeeyoo@xxxxxxxxx>
---
tools/testing/selftests/kvm/lib/test_util.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index f5f7189f9..eccb03153 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -417,13 +417,14 @@ long get_run_delay(void)
char path[64];
long val[2];
FILE *fp;
+ int ret;
sprintf(path, "/proc/%ld/schedstat", (long)kvm_gettid());
fp = fopen(path, "r");
- /* Return MIN_RUN_DELAY_NS upon failure just to be safe */
- if (fscanf(fp, "%ld %ld ", &val[0], &val[1]) < 2)
- val[1] = MIN_RUN_DELAY_NS;
+ TEST_ASSERT(fp, "Failed to open %s: %s", path, strerror(errno));
+ ret = fscanf(fp, "%ld %ld ", &val[0], &val[1]);
fclose(fp);
+ TEST_ASSERT(ret == 2, "Failed to parse run delay from %s", path);
return val[1];
}
--
2.34.1