Skip to content

Commit ba2b795

Browse files
bpo-46968: Fix faulthandler for Sapphire Rapids Xeon (GH-31789) (GH-31831)
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. (cherry picked from commit 3b128c0) Co-authored-by: Oleksandr Pavlyk <[email protected]>
1 parent 30d8021 commit ba2b795

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
@@ -15,6 +15,17 @@
1515
# include <sys/resource.h>
1616
#endif
1717

18+
/* Using an alternative stack requires sigaltstack()
19+
and sigaction() SA_ONSTACK */
20+
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
21+
# define FAULTHANDLER_USE_ALT_STACK
22+
#endif
23+
24+
#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H)
25+
# include <linux/auxvec.h>
26+
# include <sys/auxv.h>
27+
#endif
28+
1829
/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
1930
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)
2031

@@ -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;
@@ -1395,6 +1400,15 @@ _PyFaulthandler_Init(int enable)
13951400
signal handler uses more than SIGSTKSZ bytes of stack memory on some
13961401
platforms. */
13971402
stack.ss_size = SIGSTKSZ * 2;
1403+
#ifdef AT_MINSIGSTKSZ
1404+
/* bpo-46968: Query Linux for minimal stack size to ensure signal delivery
1405+
for the hardware running CPython. This OS feature is available in
1406+
Linux kernel version >= 5.14 */
1407+
unsigned long at_minstack_size = getauxval(AT_MINSIGSTKSZ);
1408+
if (at_minstack_size != 0) {
1409+
stack.ss_size = SIGSTKSZ + at_minstack_size;
1410+
}
1411+
#endif
13981412
#endif
13991413

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

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8042,7 +8042,7 @@ sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
80428042
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
80438043
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
80448044
linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
8045-
sys/endian.h sys/sysmacros.h linux/memfd.h linux/wait.h sys/memfd.h sys/mman.h
8045+
sys/endian.h sys/sysmacros.h linux/auxvec.h linux/memfd.h linux/wait.h sys/memfd.h sys/mman.h
80468046
do :
80478047
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
80488048
ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
22282228
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
22292229
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
22302230
linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
2231-
sys/endian.h sys/sysmacros.h linux/memfd.h linux/wait.h sys/memfd.h sys/mman.h)
2231+
sys/endian.h sys/sysmacros.h linux/auxvec.h linux/memfd.h linux/wait.h sys/memfd.h sys/mman.h)
22322232
AC_HEADER_DIRENT
22332233
AC_HEADER_MAJOR
22342234

pyconfig.h.in

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

625+
/* Define to 1 if you have the <linux/auxvec.h> header file. */
626+
#undef HAVE_LINUX_AUXVEC_H
627+
625628
/* Define to 1 if you have the <linux/can/bcm.h> header file. */
626629
#undef HAVE_LINUX_CAN_BCM_H
627630

0 commit comments

Comments
 (0)