* [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries
@ 2025-08-29 14:25 Aqib Faruqui
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
` (8 more replies)
0 siblings, 9 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Christian Brauner, linux-kernel; +Cc: nh-open-source, aqibaf
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(-)
--
2.47.3
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 22:52 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 2/9] KVM: selftests: Add __packed attribute fallback Aqib Faruqui
` (7 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Kees Cook, Andy Lutomirski, Will Drewry, Shuah Khan,
Christian Brauner, linux-kselftest, linux-kernel
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kselftest_harness.h | 1 +
tools/testing/selftests/pidfd/pidfd.h | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 2925e47db..1dd3e5a1b 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -69,6 +69,7 @@
#include <unistd.h>
#include "kselftest.h"
+#include "pidfd/pidfd.h"
#define TEST_TIMEOUT_DEFAULT 30
diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h
index f87993def..c373ff18e 100644
--- a/tools/testing/selftests/pidfd/pidfd.h
+++ b/tools/testing/selftests/pidfd/pidfd.h
@@ -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
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 2/9] KVM: selftests: Add __packed attribute fallback
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 22:46 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback Aqib Faruqui
` (6 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 23a506d7e..7fae7f5e7 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -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
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
2025-08-29 14:25 ` [PATCH 2/9] KVM: selftests: Add __packed attribute fallback Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 22:37 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 4/9] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
` (5 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
tools/testing/selftests/kvm/lib/kvm_util.c | 11 +++++++++++
2 files changed, 15 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 7fae7f5e7..8177178b5 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -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
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index c3f5142b0..5ce80303d 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -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;
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 4/9] selftests: kselftest: Add memfd_create syscall compatibility
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (2 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 14:25 ` [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86 Aqib Faruqui
` (4 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Shuah Khan, Paolo Bonzini, linux-kselftest, linux-kernel, kvm
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kselftest.h | 19 +++++++++++++++++++
tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
2 files changed, 20 insertions(+)
diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index c3b6d2604..f362c6766 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -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
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 5ce80303d..cb5209f6a 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -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>
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (3 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 4/9] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 20:38 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 6/9] KVM: selftests: Add backtrace fallback Aqib Faruqui
` (3 subsequent siblings)
8 siblings, 1 reply; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Shuah Khan, kvm,
linux-kselftest, linux-kernel
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kvm/include/x86/processor.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
index 2efb05c2f..3f93d1b4f 100644
--- a/tools/testing/selftests/kvm/include/x86/processor.h
+++ b/tools/testing/selftests/kvm/include/x86/processor.h
@@ -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))
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 6/9] KVM: selftests: Add backtrace fallback
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (4 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86 Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 14:25 ` [PATCH 7/9] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
` (2 subsequent siblings)
8 siblings, 0 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kvm/lib/assert.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kvm/lib/assert.c b/tools/testing/selftests/kvm/lib/assert.c
index b49690658..c9778dc6c 100644
--- a/tools/testing/selftests/kvm/lib/assert.c
+++ b/tools/testing/selftests/kvm/lib/assert.c
@@ -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)
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 7/9] rseq: selftests: Add non-glibc compatibility fixes
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (5 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 6/9] KVM: selftests: Add backtrace fallback Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 14:25 ` [PATCH 8/9] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
2025-08-29 14:25 ` [PATCH 9/9] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
8 siblings, 0 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Mathieu Desnoyers, Peter Zijlstra, Paul E. McKenney, Boqun Feng,
Shuah Khan, linux-kernel, linux-kselftest
Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
.../selftests/rseq/rseq-x86-thread-pointer.h | 14 ++++++++++++++
tools/testing/selftests/rseq/rseq.c | 8 ++++++++
2 files changed, 22 insertions(+)
diff --git a/tools/testing/selftests/rseq/rseq-x86-thread-pointer.h b/tools/testing/selftests/rseq/rseq-x86-thread-pointer.h
index d3133587d..a7c402926 100644
--- a/tools/testing/selftests/rseq/rseq-x86-thread-pointer.h
+++ b/tools/testing/selftests/rseq/rseq-x86-thread-pointer.h
@@ -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
}
diff --git a/tools/testing/selftests/rseq/rseq.c b/tools/testing/selftests/rseq/rseq.c
index 663a9cef1..1a6f73c98 100644
--- a/tools/testing/selftests/rseq/rseq.c
+++ b/tools/testing/selftests/rseq/rseq.c
@@ -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.
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 8/9] selftests: Fix stdbuf compatibility in mixed libc environments
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (6 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 7/9] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
2025-08-29 14:25 ` [PATCH 9/9] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
8 siblings, 0 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Shuah Khan, linux-kselftest, linux-kernel; +Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kselftest/runner.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/kselftest/runner.sh b/tools/testing/selftests/kselftest/runner.sh
index 2c3c58e65..8d4e33bd5 100644
--- a/tools/testing/selftests/kselftest/runner.sh
+++ b/tools/testing/selftests/kselftest/runner.sh
@@ -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:-}"
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 9/9] selftests: kselftest: Add ulong typedef for non-glibc compatibility
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
` (7 preceding siblings ...)
2025-08-29 14:25 ` [PATCH 8/9] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
@ 2025-08-29 14:25 ` Aqib Faruqui
8 siblings, 0 replies; 15+ messages in thread
From: Aqib Faruqui @ 2025-08-29 14:25 UTC (permalink / raw)
To: Shuah Khan, linux-kselftest, linux-kernel; +Cc: nh-open-source, aqibaf
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.
Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
tools/testing/selftests/kselftest.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index f362c6766..a1088a2af 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -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
--
2.47.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86
2025-08-29 14:25 ` [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86 Aqib Faruqui
@ 2025-08-29 20:38 ` Sean Christopherson
0 siblings, 0 replies; 15+ messages in thread
From: Sean Christopherson @ 2025-08-29 20:38 UTC (permalink / raw)
To: Aqib Faruqui
Cc: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel,
nh-open-source
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> 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.
> Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
> ---
> tools/testing/selftests/kvm/include/x86/processor.h | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
> index 2efb05c2f..3f93d1b4f 100644
> --- a/tools/testing/selftests/kvm/include/x86/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86/processor.h
> @@ -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))
> --
> 2.47.3
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback
2025-08-29 14:25 ` [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback Aqib Faruqui
@ 2025-08-29 22:37 ` Sean Christopherson
0 siblings, 0 replies; 15+ messages in thread
From: Sean Christopherson @ 2025-08-29 22:37 UTC (permalink / raw)
To: Aqib Faruqui
Cc: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel,
nh-open-source
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> 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 <seanjc@google.com>
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.
Reported-by: Aqib Faruqui <aqibaf@amazon.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/steal_time.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/kvm/steal_time.c b/tools/testing/selftests/kvm/steal_time.c
index cce2520af720..663c99c81703 100644
--- a/tools/testing/selftests/kvm/steal_time.c
+++ b/tools/testing/selftests/kvm/steal_time.c
@@ -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
--
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH 2/9] KVM: selftests: Add __packed attribute fallback
2025-08-29 14:25 ` [PATCH 2/9] KVM: selftests: Add __packed attribute fallback Aqib Faruqui
@ 2025-08-29 22:46 ` Sean Christopherson
2025-09-01 15:08 ` Faruqui, Aqib
0 siblings, 1 reply; 15+ messages in thread
From: Sean Christopherson @ 2025-08-29 22:46 UTC (permalink / raw)
To: Aqib Faruqui
Cc: Paolo Bonzini, Shuah Khan, kvm, linux-kselftest, linux-kernel,
nh-open-source
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> 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.
> Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
> ---
> tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index 23a506d7e..7fae7f5e7 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -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
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
@ 2025-08-29 22:52 ` Sean Christopherson
0 siblings, 0 replies; 15+ messages in thread
From: Sean Christopherson @ 2025-08-29 22:52 UTC (permalink / raw)
To: Aqib Faruqui
Cc: Kees Cook, Andy Lutomirski, Will Drewry, Shuah Khan,
Christian Brauner, linux-kselftest, linux-kernel, nh-open-source
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.
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> 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.
> Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
> ---
> tools/testing/selftests/kselftest_harness.h | 1 +
> tools/testing/selftests/pidfd/pidfd.h | 4 ++++
> 2 files changed, 5 insertions(+)
>
> diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
> index 2925e47db..1dd3e5a1b 100644
> --- a/tools/testing/selftests/kselftest_harness.h
> +++ b/tools/testing/selftests/kselftest_harness.h
> @@ -69,6 +69,7 @@
> #include <unistd.h>
>
> #include "kselftest.h"
> +#include "pidfd/pidfd.h"
>
> #define TEST_TIMEOUT_DEFAULT 30
>
> diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h
> index f87993def..c373ff18e 100644
> --- a/tools/testing/selftests/pidfd/pidfd.h
> +++ b/tools/testing/selftests/pidfd/pidfd.h
> @@ -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
> --
> 2.47.3
>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 2/9] KVM: selftests: Add __packed attribute fallback
2025-08-29 22:46 ` Sean Christopherson
@ 2025-09-01 15:08 ` Faruqui, Aqib
0 siblings, 0 replies; 15+ messages in thread
From: Faruqui, Aqib @ 2025-09-01 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Shuah Khan, kvm@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
nh-open-source@amazon.com
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!
--
Aqib Faruqui
Software Dev Intern (Embedded) | EC2 Accelerated Nitro | AWS
+44 7763104413
On 29/08/2025, 23:48, "Sean Christopherson" <seanjc@google.com <mailto:seanjc@google.com>> 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.
On Fri, Aug 29, 2025, Aqib Faruqui wrote:
> 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.
> Signed-off-by: Aqib Faruqui <aqibaf@amazon.com <mailto:aqibaf@amazon.com>>
> ---
> tools/testing/selftests/kvm/include/kvm_util.h | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index 23a506d7e..7fae7f5e7 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -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
>
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2025-09-01 15:08 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-29 14:25 [PATCH 0/9] Add compatibility fixes for KVM selftests with non-glibc C libraries Aqib Faruqui
2025-08-29 14:25 ` [PATCH 1/9] KVM: selftests: Add pidfd_open syscall number fallback Aqib Faruqui
2025-08-29 22:52 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 2/9] KVM: selftests: Add __packed attribute fallback Aqib Faruqui
2025-08-29 22:46 ` Sean Christopherson
2025-09-01 15:08 ` Faruqui, Aqib
2025-08-29 14:25 ` [PATCH 3/9] KVM: selftests: Add pthread_attr_setaffinity_np fallback Aqib Faruqui
2025-08-29 22:37 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 4/9] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
2025-08-29 14:25 ` [PATCH 5/9] KVM: selftests: Prevent PAGE_SIZE redefinition on x86 Aqib Faruqui
2025-08-29 20:38 ` Sean Christopherson
2025-08-29 14:25 ` [PATCH 6/9] KVM: selftests: Add backtrace fallback Aqib Faruqui
2025-08-29 14:25 ` [PATCH 7/9] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
2025-08-29 14:25 ` [PATCH 8/9] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
2025-08-29 14:25 ` [PATCH 9/9] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).