Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
@ 2026-05-28  9:45 Piotr Zarycki
  2026-05-28 13:13 ` Sean Christopherson
  0 siblings, 1 reply; 9+ messages in thread
From: Piotr Zarycki @ 2026-05-28  9:45 UTC (permalink / raw)
  To: seanjc, pbonzini
  Cc: shuah, dmatlack, vkuznets, kvm, linux-kselftest, linux-kernel,
	Piotr Zarycki

Replace the open-coded swap with the swap() macro.

Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
---
 tools/testing/selftests/kvm/include/test_util.h    | 2 ++
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index d9b433b834f1..a66bc9ccba65 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -26,6 +26,8 @@
 
 #define msecs_to_usecs(msec)    ((msec) * 1000ULL)
 
+#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+
 static inline __printf(1, 2) int _no_printf(const char *format, ...) { return 0; }
 
 #ifdef DEBUG
diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
index 15ee8b7bfc11..514d41f00714 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
@@ -131,14 +131,10 @@ static void set_expected_val(void *addr, u64 val, int vcpu_id)
 
 /*
  * Update PTEs swapping two test pages.
- * TODO: use swap()/xchg() when these are provided.
  */
 static void swap_two_test_pages(gpa_t pte_gva1, gpa_t pte_gva2)
 {
-	u64 tmp = *(u64 *)pte_gva1;
-
-	*(u64 *)pte_gva1 = *(u64 *)pte_gva2;
-	*(u64 *)pte_gva2 = tmp;
+	swap(*(u64 *)pte_gva1, *(u64 *)pte_gva2);
 }
 
 /*
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28  9:45 [PATCH] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs Piotr Zarycki
@ 2026-05-28 13:13 ` Sean Christopherson
  2026-05-28 14:07   ` Piotr Zarycki
                     ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Sean Christopherson @ 2026-05-28 13:13 UTC (permalink / raw)
  To: Piotr Zarycki
  Cc: pbonzini, shuah, dmatlack, vkuznets, kvm, linux-kselftest,
	linux-kernel

On Thu, May 28, 2026, Piotr Zarycki wrote:
> Replace the open-coded swap with the swap() macro.
> 
> Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
> ---
>  tools/testing/selftests/kvm/include/test_util.h    | 2 ++
>  tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
>  2 files changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> index d9b433b834f1..a66bc9ccba65 100644
> --- a/tools/testing/selftests/kvm/include/test_util.h
> +++ b/tools/testing/selftests/kvm/include/test_util.h
> @@ -26,6 +26,8 @@
>  
>  #define msecs_to_usecs(msec)    ((msec) * 1000ULL)
>  
> +#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)

This belongs in a common tools/ header, not in KVM's test_util.h.  I don't see
an obvious place, maybe tools/include/linux/kernel.h?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28 13:13 ` Sean Christopherson
@ 2026-05-28 14:07   ` Piotr Zarycki
  2026-05-28 14:10   ` [PATCH v2] " Piotr Zarycki
  2026-05-28 15:35   ` Piotr Zarycki
  2 siblings, 0 replies; 9+ messages in thread
From: Piotr Zarycki @ 2026-05-28 14:07 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: pbonzini, shuah, dmatlack, vkuznets, kvm, linux-kselftest,
	linux-kernel

On Thu, May 28, 2026 at 06:13:16AM -0700, Sean Christopherson wrote:
> On Thu, May 28, 2026, Piotr Zarycki wrote:
> > Replace the open-coded swap with the swap() macro.
> > 
> > Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
> > ---
> >  tools/testing/selftests/kvm/include/test_util.h    | 2 ++
> >  tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
> >  2 files changed, 3 insertions(+), 5 deletions(-)
> > 
> > diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
> > index d9b433b834f1..a66bc9ccba65 100644
> > --- a/tools/testing/selftests/kvm/include/test_util.h
> > +++ b/tools/testing/selftests/kvm/include/test_util.h
> > @@ -26,6 +26,8 @@
> >  
> >  #define msecs_to_usecs(msec)    ((msec) * 1000ULL)
> >  
> > +#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> 
> This belongs in a common tools/ header, not in KVM's test_util.h.  I don't see
> an obvious place, maybe tools/include/linux/kernel.h?
Good point, v2 below.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28 13:13 ` Sean Christopherson
  2026-05-28 14:07   ` Piotr Zarycki
@ 2026-05-28 14:10   ` Piotr Zarycki
  2026-05-28 14:35     ` sashiko-bot
  2026-05-28 15:35   ` Piotr Zarycki
  2 siblings, 1 reply; 9+ messages in thread
From: Piotr Zarycki @ 2026-05-28 14:10 UTC (permalink / raw)
  To: seanjc, pbonzini
  Cc: shuah, dmatlack, vkuznets, kvm, linux-kselftest, linux-kernel,
	Piotr Zarycki

Replace the open-coded swap with the swap() macro.

Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
---
 tools/include/linux/kernel.h                       | 4 ++++
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h
index c8c18d3908a9..381e457960dc 100644
--- a/tools/include/linux/kernel.h
+++ b/tools/include/linux/kernel.h
@@ -46,6 +46,10 @@
 #define min_t(type, x, y)	min((type)x, (type)y)
 #define clamp(val, lo, hi)	min((typeof(val))max(val, lo), hi)
 
+#ifndef swap
+#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#endif
+
 #ifndef BUG_ON
 #ifdef NDEBUG
 #define BUG_ON(cond) do { if (cond) {} } while (0)
diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
index 15ee8b7bfc11..514d41f00714 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
@@ -131,14 +131,10 @@ static void set_expected_val(void *addr, u64 val, int vcpu_id)
 
 /*
  * Update PTEs swapping two test pages.
- * TODO: use swap()/xchg() when these are provided.
  */
 static void swap_two_test_pages(gpa_t pte_gva1, gpa_t pte_gva2)
 {
-	u64 tmp = *(u64 *)pte_gva1;
-
-	*(u64 *)pte_gva1 = *(u64 *)pte_gva2;
-	*(u64 *)pte_gva2 = tmp;
+	swap(*(u64 *)pte_gva1, *(u64 *)pte_gva2);
 }
 
 /*
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28 14:10   ` [PATCH v2] " Piotr Zarycki
@ 2026-05-28 14:35     ` sashiko-bot
  0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-05-28 14:35 UTC (permalink / raw)
  To: Piotr Zarycki; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Adding a generic `swap` macro to `tools/include/linux/kernel.h` causes macro redefinition conflicts and namespace pollution that break the compilation of other tools (like perf) and selftests.
--

commit 484fb42146bff71557e97872274f2ed19e30162f
Author: Piotr Zarycki <piotr.zarycki@gmail.com>

KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
    
This commit replaces the open-coded swap in the hyperv_tlb_flush test
with the swap() macro, adding the macro definition to
tools/include/linux/kernel.h.

> diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h
> index c8c18d3908a90..381e457960dc6 100644
> --- a/tools/include/linux/kernel.h
> +++ b/tools/include/linux/kernel.h
> @@ -46,6 +46,10 @@
>  #define min_t(type, x, y)	min((type)x, (type)y)
>  #define clamp(val, lo, hi)	min((typeof(val))max(val, lo), hi)
>  
> +#ifndef swap
> +#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +#endif
> +

[Severity: Low]
Does adding this generic macro cause namespace pollution and build failures
in other tools that include <linux/kernel.h>?

For example, in tools/perf/util/session.c, swap is used as a local variable
for a function pointer:

	perf_event__swap_op swap;

The new macro will incorrectly expand function calls like
swap(event, sample_id_all), causing a type mismatch compilation error.

Also, tools/testing/selftests/bpf/xdp_hw_metadata.c defines its own 3-argument
swap macro. This new definition triggers a -Werror macro redefinition failure.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260528141059.3337431-1-piotr.zarycki@gmail.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28 13:13 ` Sean Christopherson
  2026-05-28 14:07   ` Piotr Zarycki
  2026-05-28 14:10   ` [PATCH v2] " Piotr Zarycki
@ 2026-05-28 15:35   ` Piotr Zarycki
  2026-05-28 16:04     ` sashiko-bot
  2026-06-25  7:09     ` [PATCH v3] selftests: add swap() macro to kselftest.h Piotr Zarycki
  2 siblings, 2 replies; 9+ messages in thread
From: Piotr Zarycki @ 2026-05-28 15:35 UTC (permalink / raw)
  To: seanjc, pbonzini
  Cc: shuah, dmatlack, vkuznets, kvm, linux-kselftest, linux-kernel,
	Piotr Zarycki

Replace the open-coded swap with the swap() macro.

Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
---
tools/include/linux/kernel.h breaks perf — swap is used there as a
function pointer call. Using tools/testing/selftests/kselftest.h instead.

 tools/testing/selftests/kselftest.h                | 4 ++++
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index 60838b61a2da..7f53751523d8 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -64,6 +64,10 @@
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #endif
 
+#ifndef swap
+#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#endif
+
 #if defined(__i386__) || defined(__x86_64__) /* arch */
 /*
  * gcc cpuid.h provides __cpuid_count() since v4.4.
diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
index 15ee8b7bfc11..514d41f00714 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
@@ -131,14 +131,10 @@ static void set_expected_val(void *addr, u64 val, int vcpu_id)
 
 /*
  * Update PTEs swapping two test pages.
- * TODO: use swap()/xchg() when these are provided.
  */
 static void swap_two_test_pages(gpa_t pte_gva1, gpa_t pte_gva2)
 {
-	u64 tmp = *(u64 *)pte_gva1;
-
-	*(u64 *)pte_gva1 = *(u64 *)pte_gva2;
-	*(u64 *)pte_gva2 = tmp;
+	swap(*(u64 *)pte_gva1, *(u64 *)pte_gva2);
 }
 
 /*
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs
  2026-05-28 15:35   ` Piotr Zarycki
@ 2026-05-28 16:04     ` sashiko-bot
  2026-06-25  7:09     ` [PATCH v3] selftests: add swap() macro to kselftest.h Piotr Zarycki
  1 sibling, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-05-28 16:04 UTC (permalink / raw)
  To: Piotr Zarycki; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Adding the `swap()` macro to `kselftest.h` introduces a macro redefinition warning in the mm selftests.
--

commit 677f4e84e8344f4b974940d4980c2813e71714b6
Author: Piotr Zarycki <piotr.zarycki@gmail.com>

KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs

This commit replaces the open-coded variable swapping in the hyperv_tlb_flush
selftest by introducing a swap() macro in the shared kselftest.h header.

> diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
> index 60838b61a2da5..7f53751523d89 100644
> --- a/tools/testing/selftests/kselftest.h
> +++ b/tools/testing/selftests/kselftest.h
> @@ -64,6 +64,10 @@
>  #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
>  #endif
>  
> +#ifndef swap
> +#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +#endif

[Severity: Low]
Does adding this swap() macro to the global kselftest.h header cause a macro
redefinition warning elsewhere in the selftests?

The tools/testing/selftests/mm/uffd-stress.c test includes kselftest.h (via
uffd-common.h) and unconditionally defines its own swap(a, b) macro using
__auto_type. Because this patch adds swap() to the shared kselftest.h header,
compiling uffd-stress.c will now trigger a compiler warning for macro
redefinition.

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260528154003.3594107-1-piotr.zarycki@gmail.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v3] selftests: add swap() macro to kselftest.h
  2026-05-28 15:35   ` Piotr Zarycki
  2026-05-28 16:04     ` sashiko-bot
@ 2026-06-25  7:09     ` Piotr Zarycki
  2026-07-09 14:55       ` Sean Christopherson
  1 sibling, 1 reply; 9+ messages in thread
From: Piotr Zarycki @ 2026-06-25  7:09 UTC (permalink / raw)
  To: kvm, linux-kselftest; +Cc: seanjc, vkuznets, shuah, linux-kernel, Piotr Zarycki

Add swap() to tools/testing/selftests/kselftest.h with an #ifndef guard.

Guard the local swap() definition in mm/uffd-stress.c with #ifndef to
prevent a redefinition warning.

Use swap() in hyperv_tlb_flush.c to replace the open-coded PTE swap and
remove the TODO comment.

Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
---
Changes in v3:
- Add #ifndef guard to mm/uffd-stress.c to fix a redefinition warning;
  uffd-stress.c defines its own swap() without a guard, which conflicts
  when kselftest.h is included first via uffd-common.h.

Changes in v2:
- Move swap() from tools/include/linux/kernel.h to kselftest.h; kernel.h
  breaks perf (swap is used there as a function pointer call).

 tools/testing/selftests/kselftest.h                | 4 ++++
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
 tools/testing/selftests/mm/uffd-stress.c           | 2 ++
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index 60838b61a2da..7f53751523d8 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -64,6 +64,10 @@
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #endif
 
+#ifndef swap
+#define swap(a, b)  do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#endif
+
 #if defined(__i386__) || defined(__x86_64__) /* arch */
 /*
  * gcc cpuid.h provides __cpuid_count() since v4.4.
diff --git a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
index 15ee8b7bfc11..514d41f00714 100644
--- a/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
+++ b/tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c
@@ -131,14 +131,10 @@ static void set_expected_val(void *addr, u64 val, int vcpu_id)
 
 /*
  * Update PTEs swapping two test pages.
- * TODO: use swap()/xchg() when these are provided.
  */
 static void swap_two_test_pages(gpa_t pte_gva1, gpa_t pte_gva2)
 {
-	u64 tmp = *(u64 *)pte_gva1;
-
-	*(u64 *)pte_gva1 = *(u64 *)pte_gva2;
-	*(u64 *)pte_gva2 = tmp;
+	swap(*(u64 *)pte_gva1, *(u64 *)pte_gva2);
 }
 
 /*
diff --git a/tools/testing/selftests/mm/uffd-stress.c b/tools/testing/selftests/mm/uffd-stress.c
index 700fbaa18d44..802046e905dd 100644
--- a/tools/testing/selftests/mm/uffd-stress.c
+++ b/tools/testing/selftests/mm/uffd-stress.c
@@ -56,8 +56,10 @@ static uffd_global_test_opts_t *gopts;
 static char *zeropage;
 pthread_attr_t attr;
 
+#ifndef swap
 #define swap(a, b) \
 	do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#endif
 
 const char *examples =
 	"# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v3] selftests: add swap() macro to kselftest.h
  2026-06-25  7:09     ` [PATCH v3] selftests: add swap() macro to kselftest.h Piotr Zarycki
@ 2026-07-09 14:55       ` Sean Christopherson
  0 siblings, 0 replies; 9+ messages in thread
From: Sean Christopherson @ 2026-07-09 14:55 UTC (permalink / raw)
  To: Piotr Zarycki; +Cc: kvm, linux-kselftest, vkuznets, shuah, linux-kernel

On Thu, Jun 25, 2026, Piotr Zarycki wrote:
> Add swap() to tools/testing/selftests/kselftest.h with an #ifndef guard.
> 
> Guard the local swap() definition in mm/uffd-stress.c with #ifndef to
> prevent a redefinition warning.
> 
> Use swap() in hyperv_tlb_flush.c to replace the open-coded PTE swap and
> remove the TODO comment.
> 
> Signed-off-by: Piotr Zarycki <piotr.zarycki@gmail.com>
> ---
> Changes in v3:
> - Add #ifndef guard to mm/uffd-stress.c to fix a redefinition warning;
>   uffd-stress.c defines its own swap() without a guard, which conflicts
>   when kselftest.h is included first via uffd-common.h.

Why not explicitly include kselftest.h in mm/uffd-stress.c and drop uffd-stress.c's
version of swap()?

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-07-09 14:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-28  9:45 [PATCH] KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs Piotr Zarycki
2026-05-28 13:13 ` Sean Christopherson
2026-05-28 14:07   ` Piotr Zarycki
2026-05-28 14:10   ` [PATCH v2] " Piotr Zarycki
2026-05-28 14:35     ` sashiko-bot
2026-05-28 15:35   ` Piotr Zarycki
2026-05-28 16:04     ` sashiko-bot
2026-06-25  7:09     ` [PATCH v3] selftests: add swap() macro to kselftest.h Piotr Zarycki
2026-07-09 14:55       ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox