On Mon, Mar 27, 2023, Colton Lewis wrote: > diff --git a/tools/testing/selftests/kvm/include/aarch64/processor.h b/tools/testing/selftests/kvm/include/aarch64/processor.h > index f65e491763e0..d441f485e9c6 100644 > --- a/tools/testing/selftests/kvm/include/aarch64/processor.h > +++ b/tools/testing/selftests/kvm/include/aarch64/processor.h > @@ -219,4 +219,14 @@ uint32_t guest_get_vcpuid(void); > uint64_t cycles_read(void); > uint64_t cycles_to_ns(struct kvm_vcpu *vcpu, uint64_t cycles); > > +#define MEASURE_CYCLES(x) \ > + ({ \ > + uint64_t start; \ > + start = cycles_read(); \ > + isb(); \ Would it make sense to put the necessary barriers inside the cycles_read() (or whatever we end up calling it)? Or does that not make sense on ARM? > + x; \ > + dsb(nsh); \ > + cycles_read() - start; \ > + }) > + > #endif /* SELFTEST_KVM_PROCESSOR_H */ ... > diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h > index 5d977f95d5f5..7352e02db4ee 100644 > --- a/tools/testing/selftests/kvm/include/x86_64/processor.h > +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h > @@ -1137,4 +1137,14 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, > uint64_t cycles_read(void); > uint64_t cycles_to_ns(struct kvm_vcpu *vcpu, uint64_t cycles); > > +#define MEASURE_CYCLES(x) \ > + ({ \ > + uint64_t start; \ > + start = cycles_read(); \ > + asm volatile("mfence"); \ This is incorrect as placing the barrier after the RDTSC allows the RDTSC to be executed before earlier loads, e.g. could measure memory accesses from whatever was before MEASURE_CYCLES(). And per the kernel's rdtsc_ordered(), it sounds like RDTSC can only be hoisted before prior loads, i.e. will be ordered with respect to future loads and stores. > + x; \ > + asm volatile("mfence"); \ > + cycles_read() - start; \ > + })