linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/7] selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 2/7] selftests: harness: Include pidfd.h to get syscall definitions from tools/ Aqib Faruqui
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 UTC (permalink / raw)
  To: Christian Brauner, Shuah Khan, linux-kernel, linux-kselftest
  Cc: nh-open-source, aqibaf

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.

Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
 tools/testing/selftests/pidfd/pidfd.h | 4 ++++
 1 file changed, 4 insertions(+)

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] 7+ messages in thread

* [PATCH v2 2/7] selftests: harness: Include pidfd.h to get syscall definitions from tools/
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
  2025-09-02 17:01 ` [PATCH v2 1/7] selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 3/7] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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
may not have access to the syscall definitions in non-glibc
environments. Include pidfd.h to ensure the syscall numbers are
available.

Signed-off-by: Aqib Faruqui <aqibaf@amazon.com>
---
 tools/testing/selftests/kselftest_harness.h | 1 +
 1 file changed, 1 insertion(+)

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/7] selftests: kselftest: Add memfd_create syscall compatibility
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
  2025-09-02 17:01 ` [PATCH v2 1/7] selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 2/7] selftests: harness: Include pidfd.h to get syscall definitions from tools/ Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 4/7] KVM: selftests: Add backtrace fallback Aqib Faruqui
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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 c3f5142b0..a78b64117 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] 7+ messages in thread

* [PATCH v2 4/7] KVM: selftests: Add backtrace fallback
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
                   ` (2 preceding siblings ...)
  2025-09-02 17:01 ` [PATCH v2 3/7] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 5/7] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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] 7+ messages in thread

* [PATCH v2 5/7] rseq: selftests: Add non-glibc compatibility fixes
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
                   ` (3 preceding siblings ...)
  2025-09-02 17:01 ` [PATCH v2 4/7] KVM: selftests: Add backtrace fallback Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 6/7] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 7/7] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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] 7+ messages in thread

* [PATCH v2 6/7] selftests: Fix stdbuf compatibility in mixed libc environments
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
                   ` (4 preceding siblings ...)
  2025-09-02 17:01 ` [PATCH v2 5/7] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  2025-09-02 17:01 ` [PATCH v2 7/7] selftests: kselftest: Add ulong typedef for non-glibc compatibility Aqib Faruqui
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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] 7+ messages in thread

* [PATCH v2 7/7] selftests: kselftest: Add ulong typedef for non-glibc compatibility
       [not found] <20250902170147.55583-1-aqibaf@amazon.com>
                   ` (5 preceding siblings ...)
  2025-09-02 17:01 ` [PATCH v2 6/7] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
@ 2025-09-02 17:01 ` Aqib Faruqui
  6 siblings, 0 replies; 7+ messages in thread
From: Aqib Faruqui @ 2025-09-02 17:01 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] 7+ messages in thread

end of thread, other threads:[~2025-09-02 17:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250902170147.55583-1-aqibaf@amazon.com>
2025-09-02 17:01 ` [PATCH v2 1/7] selftests/pidfd: Add architecture-specific fallback definitions for pidfd_open Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 2/7] selftests: harness: Include pidfd.h to get syscall definitions from tools/ Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 3/7] selftests: kselftest: Add memfd_create syscall compatibility Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 4/7] KVM: selftests: Add backtrace fallback Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 5/7] rseq: selftests: Add non-glibc compatibility fixes Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 6/7] selftests: Fix stdbuf compatibility in mixed libc environments Aqib Faruqui
2025-09-02 17:01 ` [PATCH v2 7/7] 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).