From: Piotr Zarycki <piotr.zarycki@gmail.com>
To: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: seanjc@google.com, vkuznets@redhat.com, shuah@kernel.org,
linux-kernel@vger.kernel.org,
Piotr Zarycki <piotr.zarycki@gmail.com>
Subject: [PATCH v3] selftests: add swap() macro to kselftest.h
Date: Thu, 25 Jun 2026 09:09:14 +0200 [thread overview]
Message-ID: <20260625070914.2175124-1-piotr.zarycki@gmail.com> (raw)
In-Reply-To: <20260528154003.3594107-1-piotr.zarycki@gmail.com>
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
next prev parent reply other threads:[~2026-06-25 7:10 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Piotr Zarycki [this message]
2026-07-09 14:55 ` [PATCH v3] selftests: add swap() macro to kselftest.h Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260625070914.2175124-1-piotr.zarycki@gmail.com \
--to=piotr.zarycki@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=vkuznets@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox