Skip to contentSkip to conversation

KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()

PATCH Superseded Piotr Zarycki 4 participants 7 messages Apr 21 – May 19

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

---
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -7
index c542cc4762b1..68ebd790ff41 100644
@@ -141,15 +141,9 @@ static void swap_two_test_pages(vm_paddr_t pte_gva1, vm_paddr_t pte_gva2)
 	*(uint64_t *)pte_gva2 = tmp;
 }
 
-/*
- * TODO: replace the silly NOP loop with a proper udelay() implementation.
- */
 static inline void do_delay(void)
 {
-	int i;
-
-	for (i = 0; i < 1000000; i++)
-		asm volatile("nop");
+	udelay(100);
 }
 
 /*
Signature · 1 line
2.53.0
#1 lore raw
Quoted 7 lines · Piotr Zarycki

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

---

How did you test this?

regards,
dan carpenter

#2 lore raw
Quoted 3 lines · Dan Carpenter

How did you test this?

I built the test using:

make -C tools/testing/selftests/kvm ARCH=x86_64 x86/hyperv_tlb_flush

And ran it 100 times to verify it passes consistently

results: 100/100 passed, 0 failed
average time: 466ms

For comparison, the original NOP loop averaged ~537ms on the same machine.

Reviewed-by on v1 1/1
#3 lore raw
In reply to Piotr Zarycki
Quoted 21 lines · Piotr Zarycki

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

---
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +0 -3
index c542cc4762b1..68ebd790ff41 100644
@@ -141,15 +141,9 @@ static void swap_two_test_pages(vm_paddr_t pte_gva1, vm_paddr_t pte_gva2)
 	*(uint64_t *)pte_gva2 = tmp;
 }
 
-/*
- * TODO: replace the silly NOP loop with a proper udelay() implementation.
- */

tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c static void swap_two_test_pages(vm_paddr_t pte_gva1, vm_paddr_t pte_gva2) · line 149

Quoted 10 lines
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -4
 static inline void do_delay(void)
 {
-	int i;
-
-	for (i = 0; i < 1000000; i++)
-		asm volatile("nop");
+	udelay(100);
 }
 
 /*

Oh, I completely forgot I added the TODO :-) Thanks for the patch! I also gave it a spin and it looks like rdtsc() from udelay() doesn't hurt.

Personally, I belive we should now just drop the whole do_delay() function and use udelay(100) instead. We have only two call sites and there's no strict requirement that the delay time should match.

In any case,

Signature · 1 line
Vitaly
#4 lore raw

PATCH v2 1/1 KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

---
 .../testing/selftests/kvm/x86/hyperv_tlb_flush.c  | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +2 -13
index c542cc4762b1..4b7f089542c1 100644
@@ -141,17 +141,6 @@ static void swap_two_test_pages(vm_paddr_t pte_gva1, vm_paddr_t pte_gva2)
 	*(uint64_t *)pte_gva2 = tmp;
 }
 
-/*
- * TODO: replace the silly NOP loop with a proper udelay() implementation.
- */
-static inline void do_delay(void)
-{
-	int i;
-
-	for (i = 0; i < 1000000; i++)
-		asm volatile("nop");
-}
-
 /*
  * Prepare to test: 'disable' workers by setting the expectation to '0',
  * clear hypercall input page and then swap two test pages.
@@ -169,7 +158,7 @@ static inline void prepare_to_test(struct test_data *data)
 	wmb();
 
 	/* Make sure workers have enough time to notice */
-	do_delay();
+	udelay(100);
 
 	/* Swap test page mappings */
 	swap_two_test_pages(data->test_pages_pte[0], data->test_pages_pte[1]);
@@ -189,7 +178,7 @@ static inline void post_test(struct test_data *data, u64 exp1, u64 exp2)
 	set_expected_val((void *)data->test_pages, exp2, WORKER_VCPU_ID_2);
 
 	/* Make sure workers have enough time to test */
-	do_delay();
+	udelay(100);
 }
 
 #define TESTVAL1 0x0101010101010101
Signature · 1 line
2.53.0
Reviewed-by on v2 1/1
#5 lore raw
Quoted 6 lines · Piotr Zarycki

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

Thanks!

Quoted 44 lines
---
 .../testing/selftests/kvm/x86/hyperv_tlb_flush.c  | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +2 -13
index c542cc4762b1..4b7f089542c1 100644
@@ -141,17 +141,6 @@ static void swap_two_test_pages(vm_paddr_t pte_gva1, vm_paddr_t pte_gva2)
 	*(uint64_t *)pte_gva2 = tmp;
 }
 
-/*
- * TODO: replace the silly NOP loop with a proper udelay() implementation.
- */
-static inline void do_delay(void)
-{
-	int i;
-
-	for (i = 0; i < 1000000; i++)
-		asm volatile("nop");
-}
-
 /*
  * Prepare to test: 'disable' workers by setting the expectation to '0',
  * clear hypercall input page and then swap two test pages.
@@ -169,7 +158,7 @@ static inline void prepare_to_test(struct test_data *data)
 	wmb();
 
 	/* Make sure workers have enough time to notice */
-	do_delay();
+	udelay(100);
 
 	/* Swap test page mappings */
 	swap_two_test_pages(data->test_pages_pte[0], data->test_pages_pte[1]);
@@ -189,7 +178,7 @@ static inline void post_test(struct test_data *data, u64 exp1, u64 exp2)
 	set_expected_val((void *)data->test_pages, exp2, WORKER_VCPU_ID_2);
 
 	/* Make sure workers have enough time to test */
-	do_delay();
+	udelay(100);
 }
 
 #define TESTVAL1 0x0101010101010101
Signature · 1 line
Vitaly
#6 lore raw
In reply to Piotr Zarycki
Quoted 4 lines · Piotr Zarycki

Replace the open-coded NOP loop with udelay() which was added to KVM selftests in commit 6b878cbb87bf ("KVM: selftests: Add guest udelay() utility for x86"). The NOP loop is CPU speed dependent while udelay() provides a deterministic delay regardless of host CPU frequency.

Applied to kvm-x86 selftests, thanks!

[1/1] KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()

      https://github.com/kvm-x86/linux/commit/749fbf2bdcd7

--
https://github.com/kvm-x86/linux/tree/next