Skip to content

Commit ba58f87

Browse files
committed
KVM: selftests: Fix a semaphore imbalance in the dirty ring logging test
When finishing the final iteration of dirty_log_test testcase, set host_quit _before_ the final "continue" so that the vCPU worker doesn't run an extra iteration, and delete the hack-a-fix of an extra "continue" from the dirty ring testcase. This fixes a bug where the extra post to sem_vcpu_cont may not be consumed, which results in failures in subsequent runs of the testcases. The bug likely was missed during development as x86 supports only a single "guest mode", i.e. there aren't any subsequent testcases after the dirty ring test, because for_each_guest_mode() only runs a single iteration. For the regular dirty log testcases, letting the vCPU run one extra iteration is a non-issue as the vCPU worker waits on sem_vcpu_cont if and only if the worker is explicitly told to stop (vcpu_sync_stop_requested). But for the dirty ring test, which needs to periodically stop the vCPU to reap the dirty ring, letting the vCPU resume the guest _after_ the last iteration means the vCPU will get stuck without an extra "continue". However, blindly firing off an post to sem_vcpu_cont isn't guaranteed to be consumed, e.g. if the vCPU worker sees host_quit==true before resuming the guest. This results in a dangling sem_vcpu_cont, which leads to subsequent iterations getting out of sync, as the vCPU worker will continue on before the main task is ready for it to resume the guest, leading to a variety of asserts, e.g. ==== Test Assertion Failure ==== dirty_log_test.c:384: dirty_ring_vcpu_ring_full pid=14854 tid=14854 errno=22 - Invalid argument 1 0x00000000004033eb: dirty_ring_collect_dirty_pages at dirty_log_test.c:384 2 0x0000000000402d27: log_mode_collect_dirty_pages at dirty_log_test.c:505 3 (inlined by) run_test at dirty_log_test.c:802 4 0x0000000000403dc7: for_each_guest_mode at guest_modes.c:100 5 0x0000000000401dff: main at dirty_log_test.c:941 (discriminator 3) 6 0x0000ffff9be173c7: ?? ??:0 7 0x0000ffff9be1749f: ?? ??:0 8 0x000000000040206f: _start at ??:? Didn't continue vcpu even without ring full Alternatively, the test could simply reset the semaphores before each testcase, but papering over hacks with more hacks usually ends in tears. Reported-by: Shaoqin Huang <shahuang@redhat.com> Fixes: 84292e5 ("KVM: selftests: Add dirty ring buffer test") Reviewed-by: Peter Xu <peterx@redhat.com> Reviewed-by: Shaoqin Huang <shahuang@redhat.com> Link: https://lore.kernel.org/r/20240202231831.354848-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9e62797 commit ba58f87

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

tools/testing/selftests/kvm/dirty_log_test.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ static void dirty_ring_collect_dirty_pages(struct kvm_vcpu *vcpu, int slot,
376376

377377
cleared = kvm_vm_reset_dirty_ring(vcpu->vm);
378378

379-
/* Cleared pages should be the same as collected */
379+
/*
380+
* Cleared pages should be the same as collected, as KVM is supposed to
381+
* clear only the entries that have been harvested.
382+
*/
380383
TEST_ASSERT(cleared == count, "Reset dirty pages (%u) mismatch "
381384
"with collected (%u)", cleared, count);
382385

@@ -415,12 +418,6 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
415418
}
416419
}
417420

418-
static void dirty_ring_before_vcpu_join(void)
419-
{
420-
/* Kick another round of vcpu just to make sure it will quit */
421-
sem_post(&sem_vcpu_cont);
422-
}
423-
424421
struct log_mode {
425422
const char *name;
426423
/* Return true if this mode is supported, otherwise false */
@@ -433,7 +430,6 @@ struct log_mode {
433430
uint32_t *ring_buf_idx);
434431
/* Hook to call when after each vcpu run */
435432
void (*after_vcpu_run)(struct kvm_vcpu *vcpu, int ret, int err);
436-
void (*before_vcpu_join) (void);
437433
} log_modes[LOG_MODE_NUM] = {
438434
{
439435
.name = "dirty-log",
@@ -452,7 +448,6 @@ struct log_mode {
452448
.supported = dirty_ring_supported,
453449
.create_vm_done = dirty_ring_create_vm_done,
454450
.collect_dirty_pages = dirty_ring_collect_dirty_pages,
455-
.before_vcpu_join = dirty_ring_before_vcpu_join,
456451
.after_vcpu_run = dirty_ring_after_vcpu_run,
457452
},
458453
};
@@ -513,14 +508,6 @@ static void log_mode_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err)
513508
mode->after_vcpu_run(vcpu, ret, err);
514509
}
515510

516-
static void log_mode_before_vcpu_join(void)
517-
{
518-
struct log_mode *mode = &log_modes[host_log_mode];
519-
520-
if (mode->before_vcpu_join)
521-
mode->before_vcpu_join();
522-
}
523-
524511
static void generate_random_array(uint64_t *guest_array, uint64_t size)
525512
{
526513
uint64_t i;
@@ -719,6 +706,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
719706
struct kvm_vm *vm;
720707
unsigned long *bmap;
721708
uint32_t ring_buf_idx = 0;
709+
int sem_val;
722710

723711
if (!log_mode_supported()) {
724712
print_skip("Log mode '%s' not supported",
@@ -788,12 +776,22 @@ static void run_test(enum vm_guest_mode mode, void *arg)
788776
/* Start the iterations */
789777
iteration = 1;
790778
sync_global_to_guest(vm, iteration);
791-
host_quit = false;
779+
WRITE_ONCE(host_quit, false);
792780
host_dirty_count = 0;
793781
host_clear_count = 0;
794782
host_track_next_count = 0;
795783
WRITE_ONCE(dirty_ring_vcpu_ring_full, false);
796784

785+
/*
786+
* Ensure the previous iteration didn't leave a dangling semaphore, i.e.
787+
* that the main task and vCPU worker were synchronized and completed
788+
* verification of all iterations.
789+
*/
790+
sem_getvalue(&sem_vcpu_stop, &sem_val);
791+
TEST_ASSERT_EQ(sem_val, 0);
792+
sem_getvalue(&sem_vcpu_cont, &sem_val);
793+
TEST_ASSERT_EQ(sem_val, 0);
794+
797795
pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu);
798796

799797
while (iteration < p->iterations) {
@@ -819,15 +817,21 @@ static void run_test(enum vm_guest_mode mode, void *arg)
819817
assert(host_log_mode == LOG_MODE_DIRTY_RING ||
820818
atomic_read(&vcpu_sync_stop_requested) == false);
821819
vm_dirty_log_verify(mode, bmap);
822-
sem_post(&sem_vcpu_cont);
823820

824-
iteration++;
821+
/*
822+
* Set host_quit before sem_vcpu_cont in the final iteration to
823+
* ensure that the vCPU worker doesn't resume the guest. As
824+
* above, the dirty ring test may stop and wait even when not
825+
* explicitly request to do so, i.e. would hang waiting for a
826+
* "continue" if it's allowed to resume the guest.
827+
*/
828+
if (++iteration == p->iterations)
829+
WRITE_ONCE(host_quit, true);
830+
831+
sem_post(&sem_vcpu_cont);
825832
sync_global_to_guest(vm, iteration);
826833
}
827834

828-
/* Tell the vcpu thread to quit */
829-
host_quit = true;
830-
log_mode_before_vcpu_join();
831835
pthread_join(vcpu_thread, NULL);
832836

833837
pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "

0 commit comments

Comments
 (0)