Skip to content

Commit 393e2bf

Browse files
bpo-46968: Fix faulthandler for Sapphire Rapids Xeon (GH-31789) (GH-31830)
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 b35b36e commit 393e2bf

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

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

@@ -139,12 +150,6 @@ static fault_handler_t faulthandler_handlers[] = {
139150
static const size_t faulthandler_nsignals = \
140151
Py_ARRAY_LENGTH(faulthandler_handlers);
141152

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

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

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8101,7 +8101,7 @@ sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
81018101
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
81028102
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
81038103
linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
8104-
sys/endian.h sys/sysmacros.h linux/memfd.h linux/wait.h sys/memfd.h \
8104+
sys/endian.h sys/sysmacros.h linux/auxvec.h linux/memfd.h linux/wait.h sys/memfd.h \
81058105
sys/mman.h sys/eventfd.h
81068106
do :
81078107
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`

configure.ac

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

pyconfig.h.in

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

640+
/* Define to 1 if you have the <linux/auxvec.h> header file. */
641+
#undef HAVE_LINUX_AUXVEC_H
642+
640643
/* Define to 1 if you have the <linux/can/bcm.h> header file. */
641644
#undef HAVE_LINUX_CAN_BCM_H
642645

0 commit comments

Comments
 (0)