Skip to contentSkip to conversation

signal handling support for nolibc

PATCH In review Benjamin Berg 3 participants 9 messages Jul 31, 2025 – Aug 2, 2025

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

v3 cover signal handling support for nolibc

From: Benjamin Berg <[email protected]>

Hi,

This patchset adds signal handling to nolibc. Initially, I would like to use this for tests. But in the long run, the goal is to use nolibc for the UML kernel itself. In both cases, signal handling will be needed.

With v3 everything is now included in nolibc instead of trying to use the messy kernel headers.

Benjamin

Benjamin Berg (4):
selftests/nolibc: fix EXPECT_NZ macro selftests/nolibc: remove outdated comment about construct order tools/nolibc: add more generic bitmask macros for FD_* tools/nolibc: add signal support

 tools/include/nolibc/Makefile                |   1 +
 tools/include/nolibc/arch-s390.h             |   4 +-
 tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++
 tools/include/nolibc/signal.h                | 179 ++++++++++++++
 tools/include/nolibc/sys.h                   |   2 +-
 tools/include/nolibc/sys/wait.h              |   1 +
 tools/include/nolibc/time.h                  |   2 +-
 tools/include/nolibc/types.h                 |  81 ++++---
 tools/testing/selftests/nolibc/nolibc-test.c | 139 ++++++++++-
 9 files changed, 608 insertions(+), 38 deletions(-)
 create mode 100644 tools/include/nolibc/asm-signal.h
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 1/4 selftests/nolibc: fix EXPECT_NZ macro

From: Benjamin Berg <[email protected]>

The expect non-zero macro was incorrect and never used. Fix its definition.

Fixes 362aecb2d8cfa ("selftests/nolibc: add basic infrastructure to ease creation of nolibc tests")
---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +2 -2
index a297ee0d6d07..97efc98b6a3d 100644
@@ -196,8 +196,8 @@ int expect_zr(int expr, int llen)
 }
 
 
-#define EXPECT_NZ(cond, expr, val)			\
-	do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen; } while (0)
+#define EXPECT_NZ(cond, expr)				\
+	do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen); } while (0)
 
 static __attribute__((unused))
 int expect_nz(int expr, int llen)
Signature · 1 line
2.50.1
#2 lore raw

PATCH v3 2/4 selftests/nolibc: remove outdated comment about construct order

In reply to Benjamin Berg

From: Benjamin Berg <[email protected]>

The constructor order is not (and should not) be tested. Remove the comment.

Fixes a782d45c867c ("selftests/nolibc: stop testing constructor order")
---
 tools/testing/selftests/nolibc/nolibc-test.c | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +0 -1
index 97efc98b6a3d..180f0436127a 100644
@@ -686,7 +686,6 @@ int expect_strtox(int llen, void *func, const char *input, int base, intmax_t ex
 #define CASE_TEST(name) \
 	case __LINE__: llen += printf("%d %s", test, #name);
 
-/* constructors validate that they are executed in definition order */
 __attribute__((constructor))
 static void constructor1(void)
 {
Signature · 1 line
2.50.1
#3 lore raw

PATCH v3 3/4 tools/nolibc: add more generic bitmask macros for FD_*

In reply to Benjamin Berg

From: Benjamin Berg <[email protected]>

The FD_* macros are assuming a specific type for the bitmask. Add new macros that introspect the type of the passed variable in order to know the size of the bitmask. This way the same macros can be used for other purposes.

---

v2:
- Rename the macros for consistency and to mark them internal
- Fix shift to use the correct element type

---
 tools/include/nolibc/types.h | 72 ++++++++++++++++++++----------------
 1 file changed, 40 insertions(+), 32 deletions(-)
tools/include/nolibc/types.h +40 -32
index 16c6e9ec9451..f7f2ddf41e89 100644
@@ -115,48 +115,56 @@
 #define EXIT_SUCCESS 0
 #define EXIT_FAILURE 1
 
-#define FD_SETIDXMASK (8 * sizeof(unsigned long))
-#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
-
-/* for select() */
-typedef struct {
-	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
-} fd_set;
-
-#define FD_CLR(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] &=		\
-				~(1U << (__fd & FD_SETBITMASK));	\
+#define __NOLIBC_BITMASK_CLEAR(num, set) do {				     \
+		__typeof__(set) *__set = &(set);			     \
+		int __num = (num);					     \
+		__typeof__(**__set) __bit = 1;				     \
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	     \
+			(*__set)[__num / (8 * sizeof(**__set))] &=	     \
+				~(__bit << (__num % (8 * sizeof(**__set)))); \
 	} while (0)
 
-#define FD_SET(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] |=		\
-				1 << (__fd & FD_SETBITMASK);		\
+#define __NOLIBC_BITMASK_SET(num, set) do {				  \
+		__typeof__(set) *__set = &(set);			  \
+		int __num = (num);					  \
+		__typeof__(**__set) __bit = 1;				  \
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	  \
+			(*__set)[__num / (8 * sizeof(**__set))] |=	  \
+				__bit << (__num % (8 * sizeof(**__set))); \
 	} while (0)
 
-#define FD_ISSET(fd, set) ({						\
-			fd_set *__set = (set);				\
-			int __fd = (fd);				\
-		int __r = 0;						\
-		if (__fd >= 0)						\
-			__r = !!(__set->fds[__fd / FD_SETIDXMASK] &	\
-1U << (__fd & FD_SETBITMASK));						\
-		__r;							\
+#define __NOLIBC_BITMASK_TEST(num, set) ({				  \
+		__typeof__(set) *__set = &(set);			  \
+		int __num = (num);					  \
+		__typeof__(**__set) __r = 0;				  \
+		__typeof__(**__set) __bit = 1;				  \
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	  \
+			__r = (*__set)[__num / (8 * sizeof(**__set))] &	  \
+			      (__bit << (__num % (8 * sizeof(**__set)))); \
+		!!__r;							  \
 	})
 
-#define FD_ZERO(set) do {						\
-		fd_set *__set = (set);					\
+#define __NOLIBC_BITMASK_ZERO(set) do {					\
+		__typeof__(set) *__set = &(set);			\
 		int __idx;						\
-		int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
+		int __size = sizeof(*__set) / sizeof(**__set);		\
 		for (__idx = 0; __idx < __size; __idx++)		\
-			__set->fds[__idx] = 0;				\
+			(*__set)[__idx] = 0;				\
 	} while (0)
 
+#define FD_SETIDXMASK (8 * sizeof(unsigned long))
+#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
+
+/* for select() */
+typedef struct {
+	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
+} fd_set;
+
+#define FD_CLR(fd, set) __NOLIBC_BITMASK_CLEAR(fd, (set)->fds)
+#define FD_SET(fd, set) __NOLIBC_BITMASK_SET(fd, (set)->fds)
+#define FD_ISSET(fd, set) __NOLIBC_BITMASK_TEST(fd, (set)->fds)
+#define FD_ZERO(set) __NOLIBC_BITMASK_ZERO((set)->fds)
+
 /* for getdents64() */
 struct linux_dirent64 {
 	uint64_t       d_ino;
Signature · 1 line
2.50.1
#4 lore raw

PATCH v3 4/4 tools/nolibc: add signal support

In reply to Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() using the rt_sigaction syscall and implement the normal sa_mask helpers.

For the uapi definitions, everything is copied into nolibc. This avoids issues with kernel architecture headers that are not usable with the rt_sigaction syscall.

---

v3:
- put everything into signal.h and the new asm-signal.h
- split out sigset_t tests
- actually mark signal_check static
- remove unused string.h include
- fix SIGUSR2 reset
- Use integer for signal_check as the signals are emitted from the syscall context.

v2:
- Use newly added macros to check signal emission order
- Add tests for sigset handling
- Restore the default handler after signal test
- make signal_check variable static

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes

---
 tools/include/nolibc/Makefile                |   1 +
 tools/include/nolibc/arch-s390.h             |   4 +-
 tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++
 tools/include/nolibc/signal.h                | 179 ++++++++++++++
 tools/include/nolibc/sys.h                   |   2 +-
 tools/include/nolibc/sys/wait.h              |   1 +
 tools/include/nolibc/time.h                  |   2 +-
 tools/include/nolibc/types.h                 |   9 +
 tools/testing/selftests/nolibc/nolibc-test.c | 134 +++++++++++
 9 files changed, 566 insertions(+), 3 deletions(-)
 create mode 100644 tools/include/nolibc/asm-signal.h
tools/include/nolibc/Makefile +1 -0
index 143c2d2c2ba6..55db0677cfc3 100644
@@ -25,6 +25,7 @@ endif
 
 arch_file := arch-$(ARCH).h
 all_files := \
+		asm-signal.h \
 		compiler.h \
 		crt.h \
 		ctype.h \
tools/include/nolibc/arch-s390.h +3 -1
index df4c3cc713ac..f22f9a6d9887 100644
@@ -5,13 +5,15 @@
 
 #ifndef _NOLIBC_ARCH_S390_H
 #define _NOLIBC_ARCH_S390_H
-#include <linux/signal.h>
 #include <linux/unistd.h>
 
 #include "compiler.h"
 #include "crt.h"
 #include "std.h"
 
+/* For SIGCHLD */
+#include "asm-signal.h"
+
 /* Syscalls for s390:
  *   - registers are 64-bit
  *   - syscall number is passed in r1
tools/include/nolibc/asm-signal.h new file +237 -0

238 lines not on this page · raw message

tools/include/nolibc/signal.h +179 -0

189 lines not on this page · raw message

tools/include/nolibc/sys.h +1 -1
index 295e71d34aba..a790e816565b 100644
@@ -14,7 +14,6 @@
 
 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>
 #include <linux/fs.h>
@@ -28,6 +27,7 @@
 #include "errno.h"
 #include "stdarg.h"
 #include "types.h"
+#include "asm-signal.h" /* for SIGCHLD */
 
 
 /* Syscall return helper: takes the syscall value in argument and checks for an
tools/include/nolibc/sys/wait.h +1 -0
index 56ddb806da7f..e2aa90cc3cf3 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_SYS_WAIT_H
 #define _NOLIBC_SYS_WAIT_H
 
+#include <asm/siginfo.h>
 #include "../arch.h"
 #include "../std.h"
 #include "../types.h"
tools/include/nolibc/time.h +1 -1
index d02bc44d2643..6734a3a702a6 100644
@@ -15,7 +15,7 @@
 #include "types.h"
 #include "sys.h"
 
-#include <linux/signal.h>
+#include <asm/siginfo.h>
 #include <linux/time.h>
 
 static __inline__
tools/include/nolibc/types.h +9 -0
index f7f2ddf41e89..7e205386b72c 100644
@@ -152,6 +152,15 @@
 			(*__set)[__idx] = 0;				\
 	} while (0)
 
+#define __NOLIBC_BITMASK_FILL(set) do {					\
+		__typeof__(set) *__set = &(set);			\
+		int __idx;						\
+		int __size = sizeof(*__set) / sizeof(**__set);		\
+		__typeof__(**__set) __zero = 0;				\
+		for (__idx = 0; __idx < __size; __idx++)		\
+			(*__set)[__idx] = ~__zero;			\
+	} while (0)
+
 #define FD_SETIDXMASK (8 * sizeof(unsigned long))
 #define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
 
tools/testing/selftests/nolibc/nolibc-test.c +134 -0

146 lines not on this page · raw message

Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH v3 4/4] tools/nolibc: add signal support

Hi Benjamin,

Quoted 9 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() using the rt_sigaction syscall and implement the normal sa_mask helpers.

For the uapi definitions, everything is copied into nolibc. This avoids issues with kernel architecture headers that are not usable with the rt_sigaction syscall.

Quoted 6 lines

---

v3:
- put everything into signal.h and the new asm-signal.h

Hm, did we decide on that? We don't want the per-architecture include dance, but static overrides should still be fine I think. Keeping the architecture ifdeffery inside the respective arch header. And all the generic stuff in a shared header.

Quoted 6 lines

- split out sigset_t tests
- actually mark signal_check static
- remove unused string.h include
- fix SIGUSR2 reset
- Use integer for signal_check as the signals are emitted from the syscall context.

I don't understand this point, isn't it a signal handler?

Quoted 25 lines

v2:
- Use newly added macros to check signal emission order
- Add tests for sigset handling
- Restore the default handler after signal test
- make signal_check variable static

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes

---
 tools/include/nolibc/Makefile                |   1 +
 tools/include/nolibc/arch-s390.h             |   4 +-
 tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++
 tools/include/nolibc/signal.h                | 179 ++++++++++++++
 tools/include/nolibc/sys.h                   |   2 +-
 tools/include/nolibc/sys/wait.h              |   1 +
 tools/include/nolibc/time.h                  |   2 +-
 tools/include/nolibc/types.h                 |   9 +
 tools/testing/selftests/nolibc/nolibc-test.c | 134 +++++++++++
 9 files changed, 566 insertions(+), 3 deletions(-)
 create mode 100644 tools/include/nolibc/asm-signal.h

(...)

Quoted 13 lines
tools/include/nolibc/sys.h +0 -1
index 295e71d34aba..a790e816565b 100644
@@ -14,7 +14,6 @@
 
 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>
 #include <linux/fs.h>

tools/include/nolibc/sys.h line 30

Quoted 4 lines
tools/include/nolibc/sys.h +1 -0
@@ -28,6 +27,7 @@
 #include "errno.h"
 #include "stdarg.h"
 #include "types.h"
+#include "asm-signal.h" /* for SIGCHLD */

#include "signal.h"

tools/include/nolibc/sys/wait.h line 13

Quoted 10 lines
tools/include/nolibc/sys.h +0 -0
 /* Syscall return helper: takes the syscall value in argument and checks for an
tools/include/nolibc/sys/wait.h +1 -0
index 56ddb806da7f..e2aa90cc3cf3 100644
@@ -10,6 +10,7 @@
 #ifndef _NOLIBC_SYS_WAIT_H
 #define _NOLIBC_SYS_WAIT_H
 
+#include <asm/siginfo.h>

#include "signal.h"

The asm/ usage should be hidden.

Quoted 3 lines
tools/include/nolibc/sys/wait.h +0 -0
 #include "../arch.h"
 #include "../std.h"
 #include "../types.h"

(...)

Quoted 5 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index 180f0436127a..75b96eaa4c65 100644

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1278

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +7 -0
@@ -1269,6 +1269,138 @@ int test_namespace(void)
 	return ret;
 }
 
+int test_sigset_t(int test_idx)
+{
+	int llen;
+	int ret = 0;
+
+#ifdef NOLIBC
+	if (is_nolibc) {

This looks unnecessary. The #ifdef should be sufficient.

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1280

Quoted 2 lines
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
+		sigset_t sigset;
+

(...)

Looks nice, thanks!

#6 lore raw

Re: [PATCH v3 4/4] tools/nolibc: add signal support

Hi Thomas,

Quoted 14 lines · Thomas Weißschuh
Quoted 13 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() using the rt_sigaction syscall and implement the normal sa_mask helpers.

For the uapi definitions, everything is copied into nolibc. This avoids issues with kernel architecture headers that are not usable with the rt_sigaction syscall.

---

Quoted 7 lines
Quoted 2 lines

v3:
- put everything into signal.h and the new asm-signal.h

Hm, did we decide on that? We don't want the per-architecture include dance, but static overrides should still be fine I think. Keeping the architecture ifdeffery inside the respective arch header. And all the generic stuff in a shared header.

I probably just didn't really understand what you meant :-)

You are right, we can have the common definitions in signal.h and just skip them if the architecture header did already define them.

I think I'll also drop asm-signal.h again, see below.

Quoted 8 lines
Quoted 6 lines

- split out sigset_t tests
- actually mark signal_check static
- remove unused string.h include
- fix SIGUSR2 reset
- Use integer for signal_check as the signals are emitted from the syscall context.

I don't understand this point, isn't it a signal handler?

My reasoning is, that the signal emission by the kernel happens from the kill syscall or function return. Both cases implicitly act as a memory barrier. So in this specific case we do not actually need an atomic variable.

Quoted 39 lines
Quoted 25 lines

v2:
- Use newly added macros to check signal emission order
- Add tests for sigset handling
- Restore the default handler after signal test
- make signal_check variable static

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes
---
 tools/include/nolibc/Makefile                |   1 + tools/include/nolibc/arch-s390.h             |   4 +- tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++ tools/include/nolibc/signal.h                | 179 ++++++++++++++ tools/include/nolibc/sys.h                   |   2 +- tools/include/nolibc/sys/wait.h              |   1 + tools/include/nolibc/time.h                  |   2 +- tools/include/nolibc/types.h                 |   9 + tools/testing/selftests/nolibc/nolibc-test.c | 134 +++++++++++

 9 files changed, 566 insertions(+), 3 deletions(-)

 create mode 100644 tools/include/nolibc/asm-signal.h

(...)

Quoted 11 lines
tools/include/nolibc/sys.h +0 -0
index 295e71d34aba..a790e816565b 100644
@@ -14,7 +14,6 @@


 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>

Quoted 8 lines

tools/include/nolibc/sys.h line 30

Quoted 6 lines

 #include <linux/fs.h>

tools/include/nolibc/sys.h +1 -0
@@ -28,6 +27,7 @@
  #include "errno.h"
  #include "stdarg.h"
  #include "types.h"
+#include "asm-signal.h" /* for SIGCHLD */

#include "signal.h"

Right, this and asm-signal.h happened because signal.h uses sys_kill() for raise(), resulting in a circular dependency.

The simplest solution is probably to avoid the circular include by implementing raise() as:

int raise(int signal); __attribute__((weak,unused,section(".text.nolibc_raise"))) int raise(int signal)

{
	return my_syscall2(__NR_kill, my_syscall0(__NR_getpid), signal);
}
Quoted 39 lines
Quoted 10 lines
tools/include/nolibc/sys.h +0 -0
  /* Syscall return helper: takes the syscall value in argument and checks for an
tools/include/nolibc/sys/wait.h +0 -0
index 56ddb806da7f..e2aa90cc3cf3 100644
@@ -10,6 +10,7 @@
  #ifndef _NOLIBC_SYS_WAIT_H
  #define _NOLIBC_SYS_WAIT_H


+#include <asm/siginfo.h>

#include "signal.h"

The asm/ usage should be hidden.

Quoted 3 lines

 #include "../arch.h"
 #include "../std.h"
 #include "../types.h"

(...)

Quoted 8 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
index 180f0436127a..75b96eaa4c65 100644
@@ -1269,6 +1269,138 @@ int test_namespace(void)
  	return ret;
  }

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1278

Quoted 7 lines
tools/testing/selftests/nolibc/nolibc-test.c +7 -0
+int test_sigset_t(int test_idx)
+{
+	int llen;
+	int ret = 0;
+
+#ifdef NOLIBC
+	if (is_nolibc) {

This looks unnecessary. The #ifdef should be sufficient.

Quoted 7 lines

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1280

Quoted 2 lines
tools/testing/selftests/nolibc/nolibc-test.c +2 -0
+		sigset_t sigset;
+

(...)

Looks nice, thanks!

Great, thanks for the reviews!

Benjamin
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

#7 lore raw

Re: [PATCH v3 4/4] tools/nolibc: add signal support

Note: Please also Cc the patches to LKML in the future.

Quoted 23 lines · Benjamin
Quoted 14 lines · Thomas Weißschuh
Quoted 13 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() using the rt_sigaction syscall and implement the normal sa_mask helpers.

For the uapi definitions, everything is copied into nolibc. This avoids issues with kernel architecture headers that are not usable with the rt_sigaction syscall.

---

Quoted 7 lines
Quoted 2 lines

v3:
- put everything into signal.h and the new asm-signal.h

Hm, did we decide on that? We don't want the per-architecture include dance, but static overrides should still be fine I think. Keeping the architecture ifdeffery inside the respective arch header. And all the generic stuff in a shared header.

Quoted 4 lines

I probably just didn't really understand what you meant :-)

You are right, we can have the common definitions in signal.h and just skip them if the architecture header did already define them.

Sounds good to me.

Quoted 9 lines

I think I'll also drop asm-signal.h again, see below.

Quoted 7 lines
Quoted 6 lines

- split out sigset_t tests
- actually mark signal_check static
- remove unused string.h include
- fix SIGUSR2 reset
- Use integer for signal_check as the signals are emitted from the syscall context.

Quoted 6 lines
Quoted 1 line

I don't understand this point, isn't it a signal handler?

My reasoning is, that the signal emission by the kernel happens from the kill syscall or function return. Both cases implicitly act as a memory barrier. So in this specific case we do not actually need an atomic variable.

Ok. Given that the code is doing a read-modify-write, the guarantees of sig_atomic_t are probably not enough anyways.
What about volatile?

Quoted 54 lines
Quoted 39 lines
Quoted 25 lines

v2:
- Use newly added macros to check signal emission order
- Add tests for sigset handling
- Restore the default handler after signal test
- make signal_check variable static

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes
---
 tools/include/nolibc/Makefile                |   1 + tools/include/nolibc/arch-s390.h             |   4 +- tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++ tools/include/nolibc/signal.h                | 179 ++++++++++++++ tools/include/nolibc/sys.h                   |   2 +- tools/include/nolibc/sys/wait.h              |   1 + tools/include/nolibc/time.h                  |   2 +- tools/include/nolibc/types.h                 |   9 + tools/testing/selftests/nolibc/nolibc-test.c | 134 +++++++++++

 9 files changed, 566 insertions(+), 3 deletions(-)

 create mode 100644 tools/include/nolibc/asm-signal.h

(...)

Quoted 11 lines
tools/include/nolibc/sys.h +0 -0
index 295e71d34aba..a790e816565b 100644
@@ -14,7 +14,6 @@


 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>

Quoted 8 lines

tools/include/nolibc/sys.h line 30

Quoted 6 lines

 #include <linux/fs.h>

tools/include/nolibc/sys.h +1 -0
@@ -28,6 +27,7 @@
  #include "errno.h"
  #include "stdarg.h"
  #include "types.h"
+#include "asm-signal.h" /* for SIGCHLD */

#include "signal.h"

Right, this and asm-signal.h happened because signal.h uses sys_kill() for raise(), resulting in a circular dependency.

The simplest solution is probably to avoid the circular include by implementing raise() as:

Quoted 6 lines

int raise(int signal); __attribute__((weak,unused,section(".text.nolibc_raise"))) int raise(int signal)

{
	return my_syscall2(__NR_kill, my_syscall0(__NR_getpid), signal);
}

Also sounds good. Or add local declarations sys_kill() and sys_getpid().

(...)

#8 lore raw

Re: [PATCH v3 4/4] tools/nolibc: add signal support

Hi,

Quoted 44 lines · Thomas Weißschuh

Note: Please also Cc the patches to LKML in the future.

Quoted 23 lines · Benjamin
Quoted 14 lines · Thomas Weißschuh
Quoted 13 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() using the rt_sigaction syscall and implement the normal sa_mask helpers.

For the uapi definitions, everything is copied into nolibc. This avoids issues with kernel architecture headers that are not usable with the rt_sigaction syscall.

---

Quoted 7 lines
Quoted 2 lines

v3:
- put everything into signal.h and the new asm-signal.h

Hm, did we decide on that? We don't want the per-architecture include dance, but static overrides should still be fine I think. Keeping the architecture ifdeffery inside the respective arch header. And all the generic stuff in a shared header.

Quoted 4 lines

I probably just didn't really understand what you meant :-)

You are right, we can have the common definitions in signal.h and just skip them if the architecture header did already define them.

Sounds good to me.

Quoted 11 lines

I think I'll also drop asm-signal.h again, see below.

Quoted 8 lines
Quoted 6 lines

- split out sigset_t tests
- actually mark signal_check static
- remove unused string.h include
- fix SIGUSR2 reset
- Use integer for signal_check as the signals are emitted from the syscall context.

I don't understand this point, isn't it a signal handler?

Quoted 8 lines
Quoted 4 lines

My reasoning is, that the signal emission by the kernel happens from the kill syscall or function return. Both cases implicitly act as a memory barrier. So in this specific case we do not actually need an atomic variable.

Ok. Given that the code is doing a read-modify-write, the guarantees of sig_atomic_t are probably not enough anyways.
What about volatile?

As I said, I don't think we need any additional guarantees. The compiler is not permitted/able to keep the variable in a register at the relevant times.

This is because syscalls act as memory barrier (the kill here). I believe the same is true for the function return of the signal handler. So the fact that the signal emission can only happen in these two specific locations results in sufficient guarantees.

Now, all this would not be true if the kill was done by a separate thread or process. But, it should be entirely safe here.

I'll add a comment about why no signal safety measures are needed.

Benjamin

Quoted 61 lines
Quoted 54 lines
Quoted 39 lines
Quoted 25 lines

v2:
- Use newly added macros to check signal emission order
- Add tests for sigset handling
- Restore the default handler after signal test
- make signal_check variable static

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes
---
 tools/include/nolibc/Makefile                |   1 + tools/include/nolibc/arch-s390.h             |   4 +- tools/include/nolibc/asm-signal.h            | 237 +++++++++++++++++++ tools/include/nolibc/signal.h                | 179 ++++++++++++++ tools/include/nolibc/sys.h                   |   2 +- tools/include/nolibc/sys/wait.h              |   1 + tools/include/nolibc/time.h                  |   2 +- tools/include/nolibc/types.h                 |   9 + tools/testing/selftests/nolibc/nolibc-test.c | 134 +++++++++++

 9 files changed, 566 insertions(+), 3 deletions(-)

 create mode 100644 tools/include/nolibc/asm-signal.h

(...)

Quoted 11 lines
tools/include/nolibc/sys.h +0 -0
index 295e71d34aba..a790e816565b 100644
@@ -14,7 +14,6 @@


 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>

Quoted 8 lines

tools/include/nolibc/sys.h line 30

Quoted 6 lines

 #include <linux/fs.h>

tools/include/nolibc/sys.h +1 -0
@@ -28,6 +27,7 @@
  #include "errno.h"
  #include "stdarg.h"
  #include "types.h"
+#include "asm-signal.h" /* for SIGCHLD */

#include "signal.h"

Right, this and asm-signal.h happened because signal.h uses sys_kill() for raise(), resulting in a circular dependency.

The simplest solution is probably to avoid the circular include by implementing raise() as:

Quoted 6 lines

int raise(int signal); __attribute__((weak,unused,section(".text.nolibc_raise"))) int raise(int signal)

{
	return my_syscall2(__NR_kill, my_syscall0(__NR_getpid), signal);
}
Quoted 3 lines

Also sounds good. Or add local declarations sys_kill() and sys_getpid().

(...)

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928