[PATCH v2] KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()

Piotr Zarycki posted 1 patch 1 month, 3 weeks ago
.../testing/selftests/kvm/x86/hyperv_tlb_flush.c  | 15 ++-------------
1 file changed, 2 insertions(+), 13 deletions(-)
[PATCH v2] KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()
Posted by Piotr Zarycki 1 month, 3 weeks ago
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.

Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
---
 .../testing/selftests/kvm/x86/hyperv_tlb_flush.c  | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
index c542cc4762b1..4b7f089542c1 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
@@ -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
-- 
2.53.0
Re: [PATCH v2] KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()
Posted by Sean Christopherson 4 weeks, 1 day ago
On Wed, 22 Apr 2026 15:03:07 +0200, Piotr Zarycki wrote:
> 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
Re: [PATCH v2] KVM: selftests: hyperv_tlb_flush: replace NOP loop with udelay()
Posted by Vitaly Kuznetsov 1 month, 3 weeks ago
Piotr Zarycki <piotr.zarycki@gmail.com> writes:

> 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.
>
> Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>

Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>

Thanks!

> ---
>  .../testing/selftests/kvm/x86/hyperv_tlb_flush.c  | 15 ++-------------
>  1 file changed, 2 insertions(+), 13 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
> index c542cc4762b1..4b7f089542c1 100644
> --- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
> +++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
> @@ -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

-- 
Vitaly