Skip to contentSkip to conversation

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

PATCH Superseded Piotr Zarycki 3 participants 11 messages May 28 – Jul 16

Subsystems (5)
KVM Kselftest KVM x86 MM misc +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs

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

---
 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(-)
tools/testing/selftests/kvm/include/test_util.h +2 -0
index d9b433b834f1..a66bc9ccba65 100644
@@ -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
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -5
index 15ee8b7bfc11..514d41f00714 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.54.0
#1 lore raw
Quoted 13 lines · Piotr Zarycki

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

---
 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(-)
tools/testing/selftests/kvm/include/test_util.h +0 -0
index d9b433b834f1..a66bc9ccba65 100644

tools/testing/selftests/kvm/include/test_util.h line 29

Quoted 4 lines
tools/testing/selftests/kvm/include/test_util.h +1 -0
@@ -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?

#2 lore raw
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Piotr Zarycki

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

---
 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(-)
tools/testing/selftests/kvm/include/test_util.h +0 -0
index d9b433b834f1..a66bc9ccba65 100644
@@ -26,6 +26,8 @@
 
Quoted 6 lines

tools/testing/selftests/kvm/include/test_util.h line 29

Quoted 3 lines
tools/testing/selftests/kvm/include/test_util.h +1 -0
 #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.

#3 lore raw

PATCH v2 1/1 KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs

In reply to Sean Christopherson

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

---
 tools/include/linux/kernel.h                       | 4 ++++
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 6 +-----
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/include/linux/kernel.h +4 -0
index c8c18d3908a9..381e457960dc 100644
@@ -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)
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -5
index 15ee8b7bfc11..514d41f00714 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.54.0
#4 lore raw
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…

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 <[email protected]>

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.

tools/include/linux/kernel.h line 52

Quoted 12 lines
tools/include/linux/kernel.h +4 -0
index c8c18d3908a90..381e457960dc6 100644
@@ -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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

PATCH v2 1/1 KVM: selftests: hyperv_tlb_flush: use swap() to swap PTEs

In reply to Sean Christopherson

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

---
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(-)
tools/testing/selftests/kselftest.h +4 -0
index 60838b61a2da..7f53751523d8 100644
@@ -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.
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -5
index 15ee8b7bfc11..514d41f00714 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.54.0
#6 lore raw
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…

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 <[email protected]>

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.

tools/testing/selftests/kselftest.h line 69

Quoted 11 lines
tools/testing/selftests/kselftest.h +3 -0
index 60838b61a2da5..7f53751523d89 100644
@@ -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.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

PATCH v3 1/1 selftests: add swap() macro to kselftest.h

In reply to 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.

---
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(-)
tools/testing/selftests/kselftest.h +4 -0
index 60838b61a2da..7f53751523d8 100644
@@ -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.
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -5
index 15ee8b7bfc11..514d41f00714 100644
@@ -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);
 }
 
 /*
tools/testing/selftests/mm/uffd-stress.c +2 -0
index 700fbaa18d44..802046e905dd 100644
@@ -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"
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH v3] selftests: add swap() macro to kselftest.h

Quoted 8 lines · 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.

Quoted 6 lines

---
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()?

#9 lore raw

Re: [PATCH v3] selftests: add swap() macro to kselftest.h

Quoted 16 lines · Sean Christopherson
Quoted 8 lines · 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.

Quoted 6 lines

---
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.

Quoted 2 lines

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

Dropped the local definition entirely. Sending v4.

#10 lore raw

PATCH v4 1/1 selftests: add swap() macro to kselftest.h

In reply to Piotr Zarycki

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

Remove the local swap() definition from mm/uffd-stress.c; kselftest.h is already included via uffd-common.h, so the local definition is redundant.

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

---
Changes in v4:
- Drop uffd-stress.c's local swap() entirely instead of guarding it; kselftest.h is already included via uffd-common.h.

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, 5 insertions(+), 7 deletions(-)
tools/testing/selftests/kselftest.h +4 -0
index 60838b61a2da..7f53751523d8 100644
@@ -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.
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -5
index 15ee8b7bfc11..514d41f00714 100644
@@ -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);
 }
 
 /*
tools/testing/selftests/mm/uffd-stress.c +0 -2
index 700fbaa18d44..3a85a29203f4 100644
@@ -56,8 +56,6 @@ static uffd_global_test_opts_t *gopts;
 static char *zeropage;
 pthread_attr_t attr;
 
-#define swap(a, b) \
-	do { __auto_type __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
 const char *examples =
 	"# Run anonymous memory test on 100MiB region with 99999 bounces:\n"
Signature · 1 line
2.55.0