Skip to content

Commit 3b128c0

Browse files
bpo-46968: Fix faulthandler for Sapphire Rapids Xeon (GH-31789)
In Linux kernel 5.14 one can dynamically request size of altstacksize based on hardware capabilities with getauxval(AT_MINSIGSTKSZ). This changes allows for Python extension's request to Linux kernel to use AMX_TILE instruction set on Sapphire Rapids Xeon processor to succeed, unblocking use of the ISA in frameworks. Introduced HAVE_LINUX_AUXVEC_H in configure.ac and pyconfig.h.in Used cpython_autoconf:269 docker container to generate configure.
1 parent dc374ac commit 3b128c0

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`faulthandler`: On Linux 5.14 and newer, dynamically determine size of
2+
signal handler stack size CPython allocates using ``getauxval(AT_MINSIGSTKSZ)``.
3+
This changes allows for Python extension's request to Linux kernel to use
4+
AMX_TILE instruction set on Sapphire Rapids Xeon processor to succeed,
5+
unblocking use of the ISA in frameworks.

Modules/faulthandler.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
# include <sys/resource.h>
2121
#endif
2222

23+
/* Using an alternative stack requires sigaltstack()
24+
and sigaction() SA_ONSTACK */
25+
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
26+
# define FAULTHANDLER_USE_ALT_STACK
27+
#endif
28+
29+
#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H)
30+
# include <linux/auxvec.h>
31+
# include <sys/auxv.h>
32+
#endif
33+
2334
/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
2435
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
2536

@@ -137,12 +148,6 @@ static fault_handler_t faulthandler_handlers[] = {
137148
static const size_t faulthandler_nsignals = \
138149
Py_ARRAY_LENGTH(faulthandler_handlers);
139150

140-
/* Using an alternative stack requires sigaltstack()
141-
and sigaction() SA_ONSTACK */
142-
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
143-
# define FAULTHANDLER_USE_ALT_STACK
144-
#endif
145-
146151
#ifdef FAULTHANDLER_USE_ALT_STACK
147152
static stack_t stack;
148153
static stack_t old_stack;
@@ -1373,6 +1378,15 @@ _PyFaulthandler_Init(int enable)
13731378
signal handler uses more than SIGSTKSZ bytes of stack memory on some
13741379
platforms. */
13751380
stack.ss_size = SIGSTKSZ * 2;
1381+
#ifdef AT_MINSIGSTKSZ
1382+
/* bpo-46968: Query Linux for minimal stack size to ensure signal delivery
1383+
for the hardware running CPython. This OS feature is available in
1384+
Linux kernel version >= 5.14 */
1385+
unsigned long at_minstack_size = getauxval(AT_MINSIGSTKSZ);
1386+
if (at_minstack_size != 0) {
1387+
stack.ss_size = SIGSTKSZ + at_minstack_size;
1388+
}
1389+
#endif
13761390
#endif
13771391

13781392
memset(&thread, 0, sizeof(thread));

configure

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ AC_DEFINE(STDC_HEADERS, 1, [Define to 1 if you have the ANSI C header files.])
23752375
# checks for header files
23762376
AC_CHECK_HEADERS([ \
23772377
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
2378-
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
2378+
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/auxvec.h linux/memfd.h linux/random.h linux/soundcard.h \
23792379
linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
23802380
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
23812381
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@
661661
/* Define to 1 if you have the `linkat' function. */
662662
#undef HAVE_LINKAT
663663

664+
/* Define to 1 if you have the <linux/auxvec.h> header file. */
665+
#undef HAVE_LINUX_AUXVEC_H
666+
664667
/* Define to 1 if you have the <linux/can/bcm.h> header file. */
665668
#undef HAVE_LINUX_CAN_BCM_H
666669

0 commit comments

Comments
 (0)