Skip to content

Commit 0297cdc

Browse files
sean-jcbonzini
authored andcommitted
KVM: selftests: Add option to rseq test to override /dev/cpu_dma_latency
Add a "-l <latency>" param to the rseq test so that the user can override /dev/cpu_dma_latency, as described by the test's suggested workaround for not being able to complete enough migrations. cpu_dma_latency is not a normal file, even as far as procfs files go. Writes to cpu_dma_latency only persist so long as the file is open, e.g. so that the kernel automatically reverts back to a power-optimized state once the sensitive workload completes. Provide the necessary functionality instead of effectively forcing the user to write a non-obvious wrapper. Cc: Dongsheng Zhang <dongsheng.x.zhang@intel.com> Cc: Zide Chen <zide.chen@intel.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Message-ID: <20250401142238.819487-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent ef01cac commit 0297cdc

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

tools/testing/selftests/kvm/rseq_test.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,25 +196,27 @@ static void calc_min_max_cpu(void)
196196
static void help(const char *name)
197197
{
198198
puts("");
199-
printf("usage: %s [-h] [-u]\n", name);
199+
printf("usage: %s [-h] [-u] [-l latency]\n", name);
200200
printf(" -u: Don't sanity check the number of successful KVM_RUNs\n");
201+
printf(" -l: Set /dev/cpu_dma_latency to suppress deep sleep states\n");
201202
puts("");
202203
exit(0);
203204
}
204205

205206
int main(int argc, char *argv[])
206207
{
208+
int r, i, snapshot, opt, fd = -1, latency = -1;
207209
bool skip_sanity_check = false;
208-
int r, i, snapshot;
209210
struct kvm_vm *vm;
210211
struct kvm_vcpu *vcpu;
211212
u32 cpu, rseq_cpu;
212-
int opt;
213213

214-
while ((opt = getopt(argc, argv, "hu")) != -1) {
214+
while ((opt = getopt(argc, argv, "hl:u")) != -1) {
215215
switch (opt) {
216216
case 'u':
217217
skip_sanity_check = true;
218+
case 'l':
219+
latency = atoi_paranoid(optarg);
218220
break;
219221
case 'h':
220222
default:
@@ -243,6 +245,20 @@ int main(int argc, char *argv[])
243245
pthread_create(&migration_thread, NULL, migration_worker,
244246
(void *)(unsigned long)syscall(SYS_gettid));
245247

248+
if (latency >= 0) {
249+
/*
250+
* Writes to cpu_dma_latency persist only while the file is
251+
* open, i.e. it allows userspace to provide guaranteed latency
252+
* while running a workload. Keep the file open until the test
253+
* completes, otherwise writing cpu_dma_latency is meaningless.
254+
*/
255+
fd = open("/dev/cpu_dma_latency", O_RDWR);
256+
TEST_ASSERT(fd >= 0, __KVM_SYSCALL_ERROR("open() /dev/cpu_dma_latency", fd));
257+
258+
r = write(fd, &latency, 4);
259+
TEST_ASSERT(r >= 1, "Error setting /dev/cpu_dma_latency");
260+
}
261+
246262
for (i = 0; !done; i++) {
247263
vcpu_run(vcpu);
248264
TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC,
@@ -278,6 +294,9 @@ int main(int argc, char *argv[])
278294
"rseq CPU = %d, sched CPU = %d", rseq_cpu, cpu);
279295
}
280296

297+
if (fd > 0)
298+
close(fd);
299+
281300
/*
282301
* Sanity check that the test was able to enter the guest a reasonable
283302
* number of times, e.g. didn't get stalled too often/long waiting for
@@ -293,8 +312,8 @@ int main(int argc, char *argv[])
293312
TEST_ASSERT(skip_sanity_check || i > (NR_TASK_MIGRATIONS / 2),
294313
"Only performed %d KVM_RUNs, task stalled too much?\n\n"
295314
" Try disabling deep sleep states to reduce CPU wakeup latency,\n"
296-
" e.g. via cpuidle.off=1 or setting /dev/cpu_dma_latency to '0',\n"
297-
" or run with -u to disable this sanity check.", i);
315+
" e.g. via cpuidle.off=1 or via -l <latency>, or run with -u to\n"
316+
" disable this sanity check.", i);
298317

299318
pthread_join(migration_thread, NULL);
300319

0 commit comments

Comments
 (0)