Skip to contentSkip to conversation

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

PATCH New Aqib Faruqui 1 participant 8 messages Sep 2, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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

Changes in v2:
- Split pidfd patch into two separate commits
- Dropped PAGE_SIZE redefinition patch - will root cause warning separately
- Dropped __packed fallback patch - tools infrastructure already handles this correctly
- Dropped pthread_attr_setaffinity_np patch - maintainer provided solution

Aqib Faruqui (7):
selftests/pidfd: Add architecture-specific fallback definitions for
pidfd_open
selftests: harness: Include pidfd.h to get syscall definitions from
tools/
selftests: kselftest: Add memfd_create syscall compatibility
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/lib/assert.c               | 10 +++++++++-
 tools/testing/selftests/kvm/lib/kvm_util.c             |  1 +
 tools/testing/selftests/pidfd/pidfd.h                  |  4 ++++
 tools/testing/selftests/rseq/rseq-x86-thread-pointer.h | 14 ++++++++++++++
 tools/testing/selftests/rseq/rseq.c                    |  8 ++++++++
 8 files changed, 62 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/7 selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open

The pidfd_open syscall number varies by architecture. Add fallback definitions for Alpha (544) and other architectures (434) to ensure compatibility with non-glibc C libraries that may not define these syscall numbers.

---
 tools/testing/selftests/pidfd/pidfd.h | 4 ++++
 1 file changed, 4 insertions(+)
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

PATCH v2 2/7 selftests: harness: Include pidfd.h to get syscall definitions from tools/

In reply to Aqib Faruqui

The kselftest harness uses pidfd_open() for test timeout handling but may not have access to the syscall definitions in non-glibc environments. Include pidfd.h to ensure the syscall numbers are available.

---
 tools/testing/selftests/kselftest_harness.h | 1 +
 1 file changed, 1 insertion(+)
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
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/7 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 c3f5142b0..a78b64117 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
#4 lore raw

PATCH v2 4/7 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
#5 lore raw

PATCH v2 5/7 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
#6 lore raw

PATCH v2 6/7 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
#7 lore raw

PATCH v2 7/7 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