Skip to contentSkip to conversation

Add compatibility fixes for KVM selftests with non-glibc C libraries

PATCH Superseded Aqib Faruqui 3 participants 18 messages Aug 29, 2025 – Sep 8, 2025 1 message not in the archive

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add compatibility fixes for KVM selftests with non-glibc C libraries

This patch series adds compatibility fixes for building and running KVM selftests with musl and other non-glibc implementations. The changes address missing function definitions, syscall numbers, macros, type definitions, and runtime compatibility issues that are otherwise handled by glibc.

The series includes both build-time compatibility fixes (missing definitions, type mismatches) and runtime fixes (stdbuf compatibility in mixed libc environments) to ensure KVM selftests work correctly across different C library implementations.

Aqib Faruqui (9):
KVM: selftests: Add pidfd_open syscall number fallback
KVM: selftests: Add __packed attribute fallback
KVM: selftests: Add pthread_attr_setaffinity_np fallback
selftests: kselftest: Add memfd_create syscall compatibility
KVM: selftests: Prevent PAGE_SIZE redefinition on x86
KVM: selftests: Add backtrace fallback
rseq: selftests: Add non-glibc compatibility fixes
selftests: Fix stdbuf compatibility in mixed libc environments
selftests: kselftest: Add ulong typedef for non-glibc compatibility

 tools/testing/selftests/kselftest.h                    | 24 ++++++++++++++++++++++++
 tools/testing/selftests/kselftest/runner.sh            |  2 +-
 tools/testing/selftests/kselftest_harness.h            |  1 +
 tools/testing/selftests/kvm/include/kvm_util.h         |  8 ++++++++
 tools/testing/selftests/kvm/include/x86/processor.h    |  2 ++
 tools/testing/selftests/kvm/lib/assert.c               | 10 +++++++++-
 tools/testing/selftests/kvm/lib/kvm_util.c             | 12 ++++++++++++
 tools/testing/selftests/pidfd/pidfd.h                  |  4 ++++
 tools/testing/selftests/rseq/rseq-x86-thread-pointer.h | 14 ++++++++++++++
 tools/testing/selftests/rseq/rseq.c                    |  8 ++++++++
 10 files changed, 83 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/9 KVM: selftests: Add pidfd_open syscall number fallback

The kselftest-harness uses pidfd_open() for test timeout handling, but non-glibc C library headers may not define this syscall number.

Add architecture-specific fallback definitions to pidfd.h, including support for Alpha (544) and other architectures (434). Update kselftest_harness.h to include pidfd.h for the syscall definitions.

---
 tools/testing/selftests/kselftest_harness.h | 1 +
 tools/testing/selftests/pidfd/pidfd.h       | 4 ++++
 2 files changed, 5 insertions(+)
tools/testing/selftests/kselftest_harness.h +1 -0
index 2925e47db..1dd3e5a1b 100644
@@ -69,6 +69,7 @@
 #include <unistd.h>
 
 #include "kselftest.h"
+#include "pidfd/pidfd.h"
 
 #define TEST_TIMEOUT_DEFAULT 30
 
tools/testing/selftests/pidfd/pidfd.h +4 -0
index f87993def..c373ff18e 100644
@@ -45,8 +45,12 @@
 #endif
 
 #ifndef __NR_pidfd_open
+#ifdef __alpha__
+#define __NR_pidfd_open 544
+#else
 #define __NR_pidfd_open 434
 #endif
+#endif
 
 #ifndef __NR_pidfd_send_signal
 #define __NR_pidfd_send_signal 424
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback

Subject says KVM, but I don't see anything KVM related in here. I don't think any of the KVM selftests even use pidfd_open() directly.

Quoted 6 lines · Aqib Faruqui

The kselftest-harness uses pidfd_open() for test timeout handling, but non-glibc C library headers may not define this syscall number.

Add architecture-specific fallback definitions to pidfd.h, including support for Alpha (544) and other architectures (434). Update kselftest_harness.h to include pidfd.h for the syscall definitions.

This should probably be split into two patches:

selftests: harness: Include pidfd.h to get syscall definitions from tools/

and

selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open

as there are two distinct changes here. And kselftest_harness.h also tends to be a finicky little bugger, isolating changes to that file is almost always a good idea, no matter how trivial they might seem.

Quoted 38 lines
---
 tools/testing/selftests/kselftest_harness.h | 1 +
 tools/testing/selftests/pidfd/pidfd.h       | 4 ++++
 2 files changed, 5 insertions(+)
tools/testing/selftests/kselftest_harness.h +1 -0
index 2925e47db..1dd3e5a1b 100644
@@ -69,6 +69,7 @@
 #include <unistd.h>
 
 #include "kselftest.h"
+#include "pidfd/pidfd.h"
 
 #define TEST_TIMEOUT_DEFAULT 30
 
tools/testing/selftests/pidfd/pidfd.h +4 -0
index f87993def..c373ff18e 100644
@@ -45,8 +45,12 @@
 #endif
 
 #ifndef __NR_pidfd_open
+#ifdef __alpha__
+#define __NR_pidfd_open 544
+#else
 #define __NR_pidfd_open 434
 #endif
+#endif
 
 #ifndef __NR_pidfd_send_signal
 #define __NR_pidfd_send_signal 424
Signature · 1 line
2.47.3
#3 lore raw

PATCH 2/9 KVM: selftests: Add __packed attribute fallback

In reply to Aqib Faruqui

Kernel UAPI headers use __packed but don't provide the definition in userspace builds.

Add a fallback definition matching the kernel's implementation. This follows the same pattern used by BPF and SGX selftests.

---
 tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
index 23a506d7e..7fae7f5e7 100644
@@ -5,6 +5,10 @@
 #ifndef SELFTEST_KVM_UTIL_H
 #define SELFTEST_KVM_UTIL_H
 
+#ifndef __packed
+#define __packed __attribute__((__packed__))
+#endif
+
 #include "test_util.h"
 
 #include <linux/compiler.h>
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH 2/9] KVM: selftests: Add __packed attribute fallback

Quoted 5 lines · Aqib Faruqui

Kernel UAPI headers use __packed but don't provide the definition in userspace builds.

Add a fallback definition matching the kernel's implementation. This follows the same pattern used by BPF and SGX selftests.

Ugh. No, this needs to be fixed in a central location, not splattered all over random subsystem selftests. My first choice would be to copy (and keep synchronize) all of the include/linux/compiler*.h headers to tools/include/linux/.

If for some reason that's not a viable option, we should yank the __packed and similar #defines out of tools/include/linux/compiler-gcc.h and place them in tools/include/linux/compiler.h. AFAICT, none of them are actually GCC-only.

Quoted 23 lines
---
 tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
index 23a506d7e..7fae7f5e7 100644
@@ -5,6 +5,10 @@
 #ifndef SELFTEST_KVM_UTIL_H
 #define SELFTEST_KVM_UTIL_H
 
+#ifndef __packed
+#define __packed __attribute__((__packed__))
+#endif
+
 #include "test_util.h"
 
 #include <linux/compiler.h>
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH 2/9] KVM: selftests: Add __packed attribute fallback

After investigating a little, looks like tools/include/linux/compiler.h already defines __packed correctly. While UAPI headers (e.g. linux/kvm.h) use __packed but don't include it themselves, the include ordering looks fine for this to be handled by the build system.

I'll drop this patch and investigate further why the tools infrastructure isn't working correctly in my setup.

Thanks for the feedback!

Signature · 3 lines
Aqib Faruqui 
Software Dev Intern (Embedded) | EC2 Accelerated Nitro | AWS 
+44 7763104413 

On 29/08/2025, 23:48, "Sean Christopherson" <[email protected] <mailto:[email protected]>> wrote:

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

Quoted 5 lines · Aqib Faruqui

Kernel UAPI headers use __packed but don't provide the definition in userspace builds.

Add a fallback definition matching the kernel's implementation. This follows the same pattern used by BPF and SGX selftests.

Ugh. No, this needs to be fixed in a central location, not splattered all over random subsystem selftests. My first choice would be to copy (and keep synchronize) all of the include/linux/compiler*.h headers to tools/include/linux/.

If for some reason that's not a viable option, we should yank the __packed and similar #defines out of tools/include/linux/compiler-gcc.h and place them in tools/include/linux/compiler.h. AFAICT, none of them are actually GCC-only.

Quoted 23 lines
Signed-off-by Aqib Faruqui <[email protected] <mailto:[email protected]>>

---
tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++

1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +4 -1
index 23a506d7e..7fae7f5e7 100644
@@ -5,6 +5,10 @@
 #ifndef SELFTEST_KVM_UTIL_H
 #define SELFTEST_KVM_UTIL_H
 
+#ifndef __packed
+#define __packed __attribute__((__packed__))
+#endif
+
 #include "test_util.h"
 
 #include <linux/compiler.h>
--

2.47.3

#6 lore raw

PATCH 3/9 KVM: selftests: Add pthread_attr_setaffinity_np fallback

In reply to Aqib Faruqui

The pthread_attr_setaffinity_np function is a GNU extension that may not be available in non-glibc C libraries. Some KVM selftests use this function for CPU affinity control.

Add a function declaration and weak stub implementation for non-glibc builds. This allows tests to build, with the affinity setting being a no-op and errno set for the caller when the actual function is not available.

---
 tools/testing/selftests/kvm/include/kvm_util.h |  4 ++++
 tools/testing/selftests/kvm/lib/kvm_util.c     | 11 +++++++++++
 2 files changed, 15 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
index 7fae7f5e7..8177178b5 100644
@@ -31,6 +31,10 @@
 #include "kvm_util_types.h"
 #include "sparsebit.h"
 
+#ifndef __GLIBC__
+int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
+#endif /* __GLIBC__ */
+
 #define KVM_DEV_PATH "/dev/kvm"
 #define KVM_MAX_VCPUS 512
 
tools/testing/selftests/kvm/lib/kvm_util.c +11 -0
index c3f5142b0..5ce80303d 100644
@@ -20,6 +20,17 @@
 
 #define KVM_UTIL_MIN_PFN	2
 
+#ifndef __GLIBC__
+int __attribute__((weak))
+pthread_attr_setaffinity_np(pthread_attr_t *__attr,
+				size_t __cpusetsize,
+				const cpu_set_t *__cpuset)
+{
+	errno = ENOSYS;
+	return -1;
+}
+#endif
+
 uint32_t guest_random_seed;
 struct guest_random_state guest_rng;
 static uint32_t last_guest_seed;
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback

Quoted 7 lines · Aqib Faruqui

The pthread_attr_setaffinity_np function is a GNU extension that may not be available in non-glibc C libraries. Some KVM selftests use this function for CPU affinity control.

Add a function declaration and weak stub implementation for non-glibc builds. This allows tests to build, with the affinity setting being a no-op and errno set for the caller when the actual function is not available.

Except this isn't a fallback, for all intents and purposes it silently breaks the test. A "fallback" is generally something that provides roughly equivalent functionality. This particular test will still pass because forced preemption isn't strictly necessary, but this is still gross.

Luckily, KVM selftests already provides APIs to pin tasks, just use those and the problem naturally goes away.

--
From: Sean Christopherson <[email protected]>
Date: Fri, 29 Aug 2025 15:31:44 -0700
Subject: [PATCH] KVM: selftests: Use KVM's task pinning APIs in steal_time
time

Use pin_self_to_cpu() and pin_task_to_cpu() to pin the vCPU thread and the stealer thread to pCPU0 in the steal_time. Eliminating the usage of pthread_attr_setaffinity_np() in particular allows building the test again non-glibc C libraries.

---
 tools/testing/selftests/kvm/steal_time.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/steal_time.c +4 -8
index cce2520af720..663c99c81703 100644
@@ -341,9 +341,7 @@ int main(int ac, char **av)
 {
 	struct kvm_vcpu *vcpus[NR_VCPUS];
 	struct kvm_vm *vm;
-	pthread_attr_t attr;
 	pthread_t thread;
-	cpu_set_t cpuset;
 	unsigned int gpages;
 	long stolen_time;
 	long run_delay;
@@ -353,11 +351,7 @@ int main(int ac, char **av)
 	verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
 
 	/* Set CPU affinity so we can force preemption of the VCPU */
-	CPU_ZERO(&cpuset);
-	CPU_SET(0, &cpuset);
-	pthread_attr_init(&attr);
-	pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
-	pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
+	pin_self_to_cpu(0);
 
 	/* Create a VM and an identity mapped memslot for the steal time structure */
 	vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
@@ -389,7 +383,9 @@ int main(int ac, char **av)
 
 		/* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
 		run_delay = get_run_delay();
-		pthread_create(&thread, &attr, do_steal_time, NULL);
+		pthread_create(&thread, NULL, do_steal_time, NULL);
+		pin_task_to_cpu(thread, 0);
+
 		do
 			sched_yield();
 		while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);

base-commit: ecbcc2461839e848970468b44db32282e5059925 --

#8 lore raw

PATCH 4/9 selftests: kselftest: Add memfd_create syscall compatibility

In reply to Aqib Faruqui

The memfd_create function and related MFD_* flags may not be available in non-glibc C libraries. Some selftests use memfd_create for memory backing operations.

Add fallback definitions for MFD_CLOEXEC and MFD_HUGETLB flags, and provide a memfd_create wrapper.

---
 tools/testing/selftests/kselftest.h        | 19 +++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c |  1 +
 2 files changed, 20 insertions(+)
tools/testing/selftests/kselftest.h +19 -0
index c3b6d2604..f362c6766 100644
@@ -57,6 +57,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <sys/utsname.h>
+#include <sys/syscall.h>
 #endif
 
 #ifndef ARRAY_SIZE
@@ -80,6 +81,24 @@
 #endif
 #endif /* end arch */
 
+#ifndef MFD_CLOEXEC
+#define MFD_CLOEXEC 0x0001U
+#endif
+
+#ifndef MFD_HUGETLB
+#define MFD_HUGETLB 0x0004U
+#endif
+
+static inline int memfd_create(const char *name, unsigned int flags)
+{
+#ifdef __NR_memfd_create
+	return syscall(__NR_memfd_create, name, flags);
+#else
+	errno = ENOSYS;
+	return -1;
+#endif
+}
+
 /* define kselftest exit codes */
 #define KSFT_PASS  0
 #define KSFT_FAIL  1
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
index 5ce80303d..cb5209f6a 100644
@@ -15,6 +15,7 @@
 #include <sys/resource.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <unistd.h>
 #include <linux/kernel.h>
 
Signature · 1 line
2.47.3
#9 lore raw

PATCH 5/9 KVM: selftests: Prevent PAGE_SIZE redefinition on x86

In reply to Aqib Faruqui

Prevent PAGE_SIZE redefinition warnings that can occur due to namespace pollution from included headers.

Add an #ifndef directive before defining PAGE_SIZE to avoid redefinition conflicts.

---
 tools/testing/selftests/kvm/include/x86/processor.h | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
index 2efb05c2f..3f93d1b4f 100644
@@ -368,7 +368,9 @@ static inline unsigned int x86_model(unsigned int eax)
 #define PHYSICAL_PAGE_MASK      GENMASK_ULL(51, 12)
 
 #define PAGE_SHIFT		12
+#ifndef PAGE_SIZE
 #define PAGE_SIZE		(1ULL << PAGE_SHIFT)
+#endif
 #define PAGE_MASK		(~(PAGE_SIZE-1) & PHYSICAL_PAGE_MASK)
 
 #define HUGEPAGE_SHIFT(x)	(PAGE_SHIFT + (((x) - 1) * 9))
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86

Quoted 5 lines · Aqib Faruqui

Prevent PAGE_SIZE redefinition warnings that can occur due to namespace pollution from included headers.

Add an #ifndef directive before defining PAGE_SIZE to avoid redefinition conflicts.

Please provide more details on what is causing the conflicts. Blindly using a PAGE_SIZE without _knowing_ it's aligned with PAGE_SHIFT and PHYSICAL_PAGE_MASK is far from ideal.

Quoted 22 lines
---
 tools/testing/selftests/kvm/include/x86/processor.h | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
index 2efb05c2f..3f93d1b4f 100644
@@ -368,7 +368,9 @@ static inline unsigned int x86_model(unsigned int eax)
 #define PHYSICAL_PAGE_MASK      GENMASK_ULL(51, 12)
 
 #define PAGE_SHIFT		12
+#ifndef PAGE_SIZE
 #define PAGE_SIZE		(1ULL << PAGE_SHIFT)
+#endif
 #define PAGE_MASK		(~(PAGE_SIZE-1) & PHYSICAL_PAGE_MASK)
 
 #define HUGEPAGE_SHIFT(x)	(PAGE_SHIFT + (((x) - 1) * 9))
Signature · 1 line
2.47.3

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86

+lists

Please keep discussions on-list unless there's something that can't/shouldn't be posted publicly, e.g. for confidentiality or security reasons.

Quoted 10 lines · Aqib

I suppose a fix for blindly using PAGE_SIZE in subsequent macros:

#ifdef PAGE_SIZE
#undef PAGE_SIZE
#endif
#define PAGE_SIZE (1ULL << PAGE_SHIFT)

Is no better and is instead blindly suppressing the compiler's redefinition warning.

I'm having trouble finding what causes the conflict, any advice here?

Maybe try a newer compiler? E.g. gcc-14.2 will spit out the exact location of the previous definition.

In file included from include/x86/svm_util.h:13,

                 from include/x86/sev.h:15,
                 from lib/x86/sev.c:5:
include/x86/processor.h:373:9: error: "PAGE_SIZE" redefined [-Werror]
  373 | #define PAGE_SIZE               (1ULL << PAGE_SHIFT)
      |         ^~~~~~~~~
include/x86/processor.h:370:9: note: this is the location of the previous definition
  370 | #define PAGE_SIZE               BIT(12)
      |         ^~~~~~~~~
#13 lore raw

Re: [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86

Thanks for the suggestion. I don't see your previous redefinition of PAGE_SIZE upstream, just 3 lines above the warning redefinition:

Quoted 9 lines

In file included from include/x86/svm_util.h:13,

                 from include/x86/sev.h:15,
                 from lib/x86/sev.c:5:
include/x86/processor.h:373:9: error: "PAGE_SIZE" redefined [-Werror]
  373 | #define PAGE_SIZE               (1ULL << PAGE_SHIFT)
      |         ^~~~~~~~~
include/x86/processor.h:370:9: note: this is the location of the previous definition
  370 | #define PAGE_SIZE               BIT(12)
      |         ^~~~~~~~~

But I investigated further and found that both glibc and musl define PAGE_SIZE in sys/user.h:

glibc (sys/user.h):
#define PAGE_SHIFT 12
#define PAGE_SIZE (1UL << PAGE_SHIFT)

musl (sys/user.h):
#define PAGE_SIZE 4096

KVM selftests (include/x86/processor.h):
#define PAGE_SHIFT 12
#define PAGE_SIZE (1ULL << PAGE_SHIFT)

This creates redefinition warnings with both C libraries on my system. I've already sent a v2 patch series with the PAGE_SIZE patch dropped but I'm not sure what the next course of action would be for this?

Quoted 2 lines

Please keep discussions on-list unless there's something that can't/shouldn't be posted publicly, e.g. for confidentiality or security reasons.

Apologies, doing this for the first time! Hopefully this one works as it should.

#14 lore raw

Re: [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86

Quoted 2 lines · Aqib Faruqui

Thanks for the suggestion. I don't see your previous redefinition of PAGE_SIZE upstream, just 3 lines above the warning redefinition:

Oh, I manually added a conflicting definition to demonstrate the warning gcc will provide.

Quoted 13 lines
Quoted 9 lines

In file included from include/x86/svm_util.h:13,

                 from include/x86/sev.h:15,
                 from lib/x86/sev.c:5:
include/x86/processor.h:373:9: error: "PAGE_SIZE" redefined [-Werror]
  373 | #define PAGE_SIZE               (1ULL << PAGE_SHIFT)
      |         ^~~~~~~~~
include/x86/processor.h:370:9: note: this is the location of the previous definition
  370 | #define PAGE_SIZE               BIT(12)
      |         ^~~~~~~~~

But I investigated further and found that both glibc and musl define PAGE_SIZE in sys/user.h:

Quoted 6 lines

glibc (sys/user.h):
#define PAGE_SHIFT 12
#define PAGE_SIZE (1UL << PAGE_SHIFT)

musl (sys/user.h):
#define PAGE_SIZE 4096

But that still doesn't fully explain how the previous definition comes into play. E.g. if I modify x86/processor.h to explicitly #include <sys/user.h>, then I see redefinition warnings:

In file included from x86/tsc_scaling_sync.c:8:
include/x86/processor.h:373:9: warning: "PAGE_SIZE" redefined
373 | #define PAGE_SIZE (1ULL << PAGE_SHIFT)
| ^~~~~~~~~
In file included from include/x86/processor.h:13:
/usr/include/x86_64-linux-gnu/sys/user.h:173:9: note: this is the location of the previous definition
173 | #define PAGE_SIZE (1UL << PAGE_SHIFT)
| ^~~~~~~~~

Quoted 7 lines

KVM selftests (include/x86/processor.h):
#define PAGE_SHIFT 12
#define PAGE_SIZE (1ULL << PAGE_SHIFT)

This creates redefinition warnings with both C libraries on my system. I've already sent a v2 patch series with the PAGE_SIZE patch dropped but I'm not sure what the next course of action would be for this?

I don't see a clean way to resolve this other than to eliminate the #include of whatever is leading to musl defining PAGE_SIZE. I don't want to #undef and re-define PAGE_SIZE because then different compilation units (or even code chunks) could see different definitions. And as below, relying on libc for the #define isn't viable. So I think before we change any code, we need to first figure out exactly how musl's conflicting definition comes into existence, and then go from there.

Ideally, we would skip selftests' definition and assert that the existing definition is compatible, but that won't work because musl's definition isn't actually compatible with KVM selftests' definition, and more importantly isn't compatible with glibc's definition. KVM selftests and glibc both effectively #define PAGE_SIZE to be a u64 (KVM selftests only support 64-bit builds, so glibc's 1UL is an unsigned 64-bit value). But musl's definition is a signed int. I.e. we can't solve the 64-bit unsigned vs. 32-bit signed by relying on libc.

64-bit unsigned vs. 32-bit signed is unlikely to cause problems in practice, and in fact there are no problems in the current code base. But I don't love creating a potential pitfall for musl, especially since it's quite obviously not a common libc for KVM selftests, i.e. could lead to very latent bugs.

E.g. building with this

tools/testing/selftests/kvm/include/x86/processor.h +4 -0
index 488d516c4f6f..1b5e92debd20 100644
@@ -368,7 +368,11 @@ static inline unsigned int x86_model(unsigned int eax)
 #define PHYSICAL_PAGE_MASK      GENMASK_ULL(51, 12)
 
 #define PAGE_SHIFT             12
+#define PAGE_SIZE              4096
+#ifndef PAGE_SIZE
 #define PAGE_SIZE              (1ULL << PAGE_SHIFT)
+#endif
+kvm_static_assert(PAGE_SIZE == (1ULL << PAGE_SHIFT));
 #define PAGE_MASK              (~(PAGE_SIZE-1) & PHYSICAL_PAGE_MASK)
 
 #define HUGEPAGE_SHIFT(x)      (PAGE_SHIFT + (((x) - 1) * 9))
tools/testing/selftests/kvm/set_memory_region_test.c +3 -0
index ce3ac0fd6dfb..822119e8853a 100644
@@ -608,9 +608,12 @@ static void test_mmio_during_vectoring(void)
 int main(int argc, char *argv[])
 {
 #ifdef __x86_64__
+       u64 total_size = PAGE_SIZE * 1048576;
        int i, loops;
        int j, disable_slot_zap_quirk = 0;
 
+       printf("Total size = %lx\n", total_size);
+
        if (kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_SLOT_ZAP_ALL)
                disable_slot_zap_quirk = 1;
        /*

Generates a warning:

set_memory_region_test.c: In function ‘main’:
set_memory_region_test.c:611:36: warning: integer overflow in expression of type ‘int’ results in ‘0’ [-Woverflow]

    611 |         u64 total_size = PAGE_SIZE * 1048576;
        |     

And yields:

  $ ./set_memory_region_test 
  Total size = 0
Quoted 4 lines
Quoted 2 lines

Please keep discussions on-list unless there's something that can't/shouldn't be posted publicly, e.g. for confidentiality or security reasons.

Apologies, doing this for the first time!

No worries, we've all been there :-)

#15 lore raw

PATCH 6/9 KVM: selftests: Add backtrace fallback

In reply to Aqib Faruqui

The backtrace() function is a GNU extension available in glibc but may not be present in non-glibc libraries. KVM selftests use backtrace() for error reporting and debugging.

Add conditional inclusion of execinfo.h only for glibc builds and provide a weak stub implementation of backtrace() that returns 0 (stack trace empty) for non-glibc systems.

---
 tools/testing/selftests/kvm/lib/assert.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/assert.c +9 -1
index b49690658..c9778dc6c 100644
@@ -6,11 +6,19 @@
  */
 #include "test_util.h"
 
-#include <execinfo.h>
 #include <sys/syscall.h>
 
+#ifdef __GLIBC__
+#include <execinfo.h> /* backtrace */
+#endif
+
 #include "kselftest.h"
 
+int __attribute__((weak)) backtrace(void **buffer, int size)
+{
+	return 0;
+}
+
 /* Dumps the current stack trace to stderr. */
 static void __attribute__((noinline)) test_dump_stack(void);
 static void test_dump_stack(void)
Signature · 1 line
2.47.3
#16 lore raw

PATCH 7/9 rseq: selftests: Add non-glibc compatibility fixes

In reply to Aqib Faruqui

The rseq selftests rely on features provided by glibc that may not be available in non-glibc C libraries:

1. The __GNU_PREREQ macro and glibc's thread pointer implementation are not available in non-glibc libraries
2. The __NR_rseq syscall number may not be defined in non-glibc headers

Add a fallback thread pointer implementation for non-glibc systems using the pre-existing inline assembly to access thread-local storage directly via %fs/%gs registers. Also provide a fallback definition for __NR_rseq when not already defined by the C library headers: 527 for alpha and 293 for other architectures.

---
 .../selftests/rseq/rseq-x86-thread-pointer.h       | 14 ++++++++++++++
 tools/testing/selftests/rseq/rseq.c                |  8 ++++++++
 2 files changed, 22 insertions(+)
tools/testing/selftests/rseq/rseq-x86-thread-pointer.h +14 -0
index d3133587d..a7c402926 100644
@@ -14,6 +14,7 @@
 extern "C" {
 #endif
 
+#ifdef __GLIBC__
 #if __GNUC_PREREQ (11, 1)
 static inline void *rseq_thread_pointer(void)
 {
@@ -32,6 +33,19 @@ static inline void *rseq_thread_pointer(void)
 	return __result;
 }
 #endif /* !GCC 11 */
+#else
+static inline void *rseq_thread_pointer(void)
+{
+	void *__result;
+
+# ifdef __x86_64__
+	__asm__ ("mov %%fs:0, %0" : "=r" (__result));
+# else
+	__asm__ ("mov %%gs:0, %0" : "=r" (__result));
+# endif
+	return __result;
+}
+#endif /* !__GLIBC__ */
 
 #ifdef __cplusplus
 }
tools/testing/selftests/rseq/rseq.c +8 -0
index 663a9cef1..1a6f73c98 100644
@@ -36,6 +36,14 @@
 #include "../kselftest.h"
 #include "rseq.h"
 
+#ifndef __NR_rseq
+#ifdef __alpha__
+#define __NR_rseq 527
+#else
+#define __NR_rseq 293
+#endif
+#endif
+
 /*
  * Define weak versions to play nice with binaries that are statically linked
  * against a libc that doesn't support registering its own rseq.
Signature · 1 line
2.47.3
#17 lore raw

PATCH 8/9 selftests: Fix stdbuf compatibility in mixed libc environments

In reply to Aqib Faruqui

The original stdbuf use only checked if /usr/bin/stdbuf exists in the host's system but failed to verify compatibility between stdbuf and the target test binary.

The issue occurs when:
- Host system has glibc-based stdbuf from coreutils
- Selftest binaries are compiled with a non-glibc toolchain (cross compilation)

The fix adds a runtime compatibility test against the target test binary before enabling stdbuf, enabling cross-compiled selftests to run successfully.

---
 tools/testing/selftests/kselftest/runner.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kselftest/runner.sh +1 -1
index 2c3c58e65..8d4e33bd5 100644
@@ -107,7 +107,7 @@ run_one()
 		echo "# Warning: file $TEST is missing!"
 		echo "not ok $test_num $TEST_HDR_MSG"
 	else
-		if [ -x /usr/bin/stdbuf ]; then
+		if [ -x /usr/bin/stdbuf ] && [ -x "$TEST" ] && /usr/bin/stdbuf --output=L ldd "$TEST" >/dev/null 2>&1; then
 			stdbuf="/usr/bin/stdbuf --output=L "
 		fi
 		eval kselftest_cmd_args="\$${kselftest_cmd_args_ref:-}"
Signature · 1 line
2.47.3
#18 lore raw

PATCH 9/9 selftests: kselftest: Add ulong typedef for non-glibc compatibility

In reply to Aqib Faruqui

Some C libraries may not define the ulong typedef that is commonly available as a BSD/GNU extension. Add a fallback typedef to ensure ulong is available across all selftest environments.

---
 tools/testing/selftests/kselftest.h | 5 +++++
 1 file changed, 5 insertions(+)
tools/testing/selftests/kselftest.h +5 -0
index f362c6766..a1088a2af 100644
@@ -58,6 +58,11 @@
 #include <stdio.h>
 #include <sys/utsname.h>
 #include <sys/syscall.h>
+#include <sys/types.h>
+#endif
+
+#ifndef ulong
+typedef unsigned long ulong;
 #endif
 
 #ifndef ARRAY_SIZE
Signature · 1 line
2.47.3