Skip to content

Commit 2680dcf

Browse files
committed
KVM: selftests: Set per-iteration variables at the start of each iteration
Set the per-iteration variables at the start of each iteration instead of setting them before the loop, and at the end of each iteration. To ensure the vCPU doesn't race ahead before the first iteration, simply have the vCPU worker want for sem_vcpu_cont, which conveniently avoids the need to special case posting sem_vcpu_cont from the loop. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 2020d3b commit 2680dcf

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

tools/testing/selftests/kvm/dirty_log_test.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ static void *vcpu_worker(void *data)
481481
{
482482
struct kvm_vcpu *vcpu = data;
483483

484+
sem_wait(&sem_vcpu_cont);
485+
484486
while (!READ_ONCE(host_quit)) {
485487
/* Let the guest dirty the random pages */
486488
vcpu_run(vcpu);
@@ -675,15 +677,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)
675677
sync_global_to_guest(vm, guest_test_virt_mem);
676678
sync_global_to_guest(vm, guest_num_pages);
677679

678-
/* Start the iterations */
679-
iteration = 1;
680-
sync_global_to_guest(vm, iteration);
681-
WRITE_ONCE(host_quit, false);
682680
host_dirty_count = 0;
683681
host_clear_count = 0;
684-
WRITE_ONCE(dirty_ring_vcpu_ring_full, false);
685-
WRITE_ONCE(nr_writes, 0);
686-
sync_global_to_guest(vm, nr_writes);
682+
WRITE_ONCE(host_quit, false);
687683

688684
/*
689685
* Ensure the previous iteration didn't leave a dangling semaphore, i.e.
@@ -695,12 +691,22 @@ static void run_test(enum vm_guest_mode mode, void *arg)
695691
sem_getvalue(&sem_vcpu_cont, &sem_val);
696692
TEST_ASSERT_EQ(sem_val, 0);
697693

694+
TEST_ASSERT_EQ(vcpu_stop, false);
695+
698696
pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu);
699697

700-
while (iteration < p->iterations) {
698+
for (iteration = 1; iteration < p->iterations; iteration++) {
701699
unsigned long i;
702700

701+
sync_global_to_guest(vm, iteration);
702+
703+
WRITE_ONCE(nr_writes, 0);
704+
sync_global_to_guest(vm, nr_writes);
705+
703706
dirty_ring_prev_iteration_last_page = dirty_ring_last_page;
707+
WRITE_ONCE(dirty_ring_vcpu_ring_full, false);
708+
709+
sem_post(&sem_vcpu_cont);
704710

705711
/*
706712
* Let the vCPU run beyond the configured interval until it has
@@ -785,26 +791,11 @@ static void run_test(enum vm_guest_mode mode, void *arg)
785791
bmap[1], host_num_pages,
786792
&ring_buf_idx);
787793
vm_dirty_log_verify(mode, bmap);
788-
789-
/*
790-
* Set host_quit before sem_vcpu_cont in the final iteration to
791-
* ensure that the vCPU worker doesn't resume the guest. As
792-
* above, the dirty ring test may stop and wait even when not
793-
* explicitly request to do so, i.e. would hang waiting for a
794-
* "continue" if it's allowed to resume the guest.
795-
*/
796-
if (++iteration == p->iterations)
797-
WRITE_ONCE(host_quit, true);
798-
sync_global_to_guest(vm, iteration);
799-
800-
WRITE_ONCE(nr_writes, 0);
801-
sync_global_to_guest(vm, nr_writes);
802-
803-
WRITE_ONCE(dirty_ring_vcpu_ring_full, false);
804-
805-
sem_post(&sem_vcpu_cont);
806794
}
807795

796+
WRITE_ONCE(host_quit, true);
797+
sem_post(&sem_vcpu_cont);
798+
808799
pthread_join(vcpu_thread, NULL);
809800

810801
pr_info("Total bits checked: dirty (%lu), clear (%lu)\n",

0 commit comments

Comments
 (0)