KVM: selftests: Use non-atomic clear/set bit helpers in KVM tests
authorSean Christopherson <seanjc@google.com>
Sat, 19 Nov 2022 01:34:47 +0000 (01:34 +0000)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 2 Dec 2022 18:22:33 +0000 (13:22 -0500)
Use the dedicated non-atomic helpers for {clear,set}_bit() and their
test variants, i.e. the double-underscore versions.  Depsite being
defined in atomic.h, and despite the kernel versions being atomic in the
kernel, tools' {clear,set}_bit() helpers aren't actually atomic.  Move
to the double-underscore versions so that the versions that are expected
to be atomic (for kernel developers) can be made atomic without affecting
users that don't want atomic operations.

Leave the usage in ucall_free() as-is, it's the one place in tools/ that
actually wants/needs atomic behavior.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20221119013450.2643007-7-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/aarch64/arch_timer.c
tools/testing/selftests/kvm/dirty_log_test.c
tools/testing/selftests/kvm/x86_64/hyperv_evmcs.c
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c

index f2a9677..26556a2 100644 (file)
@@ -222,7 +222,7 @@ static void *test_vcpu_run(void *arg)
 
        /* Currently, any exit from guest is an indication of completion */
        pthread_mutex_lock(&vcpu_done_map_lock);
-       set_bit(vcpu_idx, vcpu_done_map);
+       __set_bit(vcpu_idx, vcpu_done_map);
        pthread_mutex_unlock(&vcpu_done_map_lock);
 
        switch (get_ucall(vcpu, &uc)) {
index a38c436..a755488 100644 (file)
 # define BITOP_LE_SWIZZLE      ((BITS_PER_LONG-1) & ~0x7)
 # define test_bit_le(nr, addr) \
        test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
-# define set_bit_le(nr, addr) \
-       set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
-# define clear_bit_le(nr, addr) \
-       clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
-# define test_and_set_bit_le(nr, addr) \
-       test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
-# define test_and_clear_bit_le(nr, addr) \
-       test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define __set_bit_le(nr, addr) \
+       __set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define __clear_bit_le(nr, addr) \
+       __clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define __test_and_set_bit_le(nr, addr) \
+       __test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define __test_and_clear_bit_le(nr, addr) \
+       __test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
 #else
-# define test_bit_le           test_bit
-# define set_bit_le            set_bit
-# define clear_bit_le          clear_bit
-# define test_and_set_bit_le   test_and_set_bit
-# define test_and_clear_bit_le test_and_clear_bit
+# define test_bit_le                   test_bit
+# define __set_bit_le                  __set_bit
+# define __clear_bit_le                        __clear_bit
+# define __test_and_set_bit_le         __test_and_set_bit
+# define __test_and_clear_bit_le       __test_and_clear_bit
 #endif
 
 #define TEST_DIRTY_RING_COUNT          65536
@@ -305,7 +305,7 @@ static uint32_t dirty_ring_collect_one(struct kvm_dirty_gfn *dirty_gfns,
                TEST_ASSERT(cur->offset < num_pages, "Offset overflow: "
                            "0x%llx >= 0x%x", cur->offset, num_pages);
                //pr_info("fetch 0x%x page %llu\n", *fetch_index, cur->offset);
-               set_bit_le(cur->offset, bitmap);
+               __set_bit_le(cur->offset, bitmap);
                dirty_ring_last_page = cur->offset;
                dirty_gfn_set_collected(cur);
                (*fetch_index)++;
@@ -560,7 +560,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
                value_ptr = host_test_mem + page * host_page_size;
 
                /* If this is a special page that we were tracking... */
-               if (test_and_clear_bit_le(page, host_bmap_track)) {
+               if (__test_and_clear_bit_le(page, host_bmap_track)) {
                        host_track_next_count++;
                        TEST_ASSERT(test_bit_le(page, bmap),
                                    "Page %"PRIu64" should have its dirty bit "
@@ -568,7 +568,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
                                    page);
                }
 
-               if (test_and_clear_bit_le(page, bmap)) {
+               if (__test_and_clear_bit_le(page, bmap)) {
                        bool matched;
 
                        host_dirty_count++;
@@ -661,7 +661,7 @@ static void vm_dirty_log_verify(enum vm_guest_mode mode, unsigned long *bmap)
                                 * should report its dirtyness in the
                                 * next run
                                 */
-                               set_bit_le(page, host_bmap_track);
+                               __set_bit_le(page, host_bmap_track);
                        }
                }
        }
index ba09d30..af29e57 100644 (file)
@@ -142,7 +142,7 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,
        /* Intercept RDMSR 0xc0000100 */
        vmwrite(CPU_BASED_VM_EXEC_CONTROL, vmreadz(CPU_BASED_VM_EXEC_CONTROL) |
                CPU_BASED_USE_MSR_BITMAPS);
-       set_bit(MSR_FS_BASE & 0x1fff, vmx_pages->msr + 0x400);
+       __set_bit(MSR_FS_BASE & 0x1fff, vmx_pages->msr + 0x400);
        GUEST_ASSERT(!vmresume());
        GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_MSR_READ);
        current_evmcs->guest_rip += 2; /* rdmsr */
@@ -154,7 +154,7 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,
        current_evmcs->guest_rip += 2; /* rdmsr */
 
        /* Intercept RDMSR 0xc0000101 without telling KVM about it */
-       set_bit(MSR_GS_BASE & 0x1fff, vmx_pages->msr + 0x400);
+       __set_bit(MSR_GS_BASE & 0x1fff, vmx_pages->msr + 0x400);
        /* Make sure HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP is set */
        current_evmcs->hv_clean_fields |= HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
        GUEST_ASSERT(!vmresume());
index 3b3cc94..68a7d35 100644 (file)
@@ -103,7 +103,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm,
 
        /* Intercept RDMSR 0xc0000100 */
        vmcb->control.intercept |= 1ULL << INTERCEPT_MSR_PROT;
-       set_bit(2 * (MSR_FS_BASE & 0x1fff), svm->msr + 0x800);
+       __set_bit(2 * (MSR_FS_BASE & 0x1fff), svm->msr + 0x800);
        run_guest(vmcb, svm->vmcb_gpa);
        GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_MSR);
        vmcb->save.rip += 2; /* rdmsr */
@@ -115,7 +115,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm,
        vmcb->save.rip += 2; /* rdmsr */
 
        /* Intercept RDMSR 0xc0000101 without telling KVM about it */
-       set_bit(2 * (MSR_GS_BASE & 0x1fff), svm->msr + 0x800);
+       __set_bit(2 * (MSR_GS_BASE & 0x1fff), svm->msr + 0x800);
        /* Make sure HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP is set */
        vmcb->control.clean |= HV_VMCB_NESTED_ENLIGHTENMENTS;
        run_guest(vmcb, svm->vmcb_gpa);