Skip to content

bpo-46968: Query altstack size dynamically on Linux kernel >=5.14 #31789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:mod:`faulthandler`: On Linux 5.14 and newer, dynamically determine size of
signal handler stack size CPython allocates using ``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.
26 changes: 20 additions & 6 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
# include <sys/resource.h>
#endif

/* Using an alternative stack requires sigaltstack()
and sigaction() SA_ONSTACK */
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
# define FAULTHANDLER_USE_ALT_STACK
#endif

#if defined(FAULTHANDLER_USE_ALT_STACK) && defined(HAVE_LINUX_AUXVEC_H)
# include <linux/auxvec.h>
# include <sys/auxv.h>
#endif

/* Allocate at maximum 100 MiB of the stack to raise the stack overflow */
#define STACK_OVERFLOW_MAX_SIZE (100 * 1024 * 1024)

Expand Down Expand Up @@ -137,12 +148,6 @@ static fault_handler_t faulthandler_handlers[] = {
static const size_t faulthandler_nsignals = \
Py_ARRAY_LENGTH(faulthandler_handlers);

/* Using an alternative stack requires sigaltstack()
and sigaction() SA_ONSTACK */
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
# define FAULTHANDLER_USE_ALT_STACK
#endif

#ifdef FAULTHANDLER_USE_ALT_STACK
static stack_t stack;
static stack_t old_stack;
Expand Down Expand Up @@ -1373,6 +1378,15 @@ _PyFaulthandler_Init(int enable)
signal handler uses more than SIGSTKSZ bytes of stack memory on some
platforms. */
stack.ss_size = SIGSTKSZ * 2;
#ifdef AT_MINSIGSTKSZ
/* bpo-46968: Query Linux for minimal stack size to ensure signal delivery
for the hardware running CPython. This OS feature is available in
Linux kernel version >= 5.14 */
unsigned long at_minstack_size = getauxval(AT_MINSIGSTKSZ);
if (at_minstack_size != 0) {
stack.ss_size = SIGSTKSZ + at_minstack_size;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that AT_MINSIGSTKSZ doesn't return directly the value which should be passed to sigaltstack(), but it seems like your code is the correct way to use the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AT_MINSIGSTKSZ is a preprocessor variable. The getauxval checks hardware capabilities (e.g. total size of CPU state to be saved at context switches) and may return different values on different platforms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that I expected to write (simplified code without error handling): stack.ss_size = getauxval(AT_MINSIGSTKSZ), rather than stack.ss_size = SIGSTKSZ + getauxval(AT_MINSIGSTKSZ). But your code looks correct according to the doc that I found on the Internet. Like the official Linux documentation:

ss.ss_size = getauxval(AT_MINSIGSTKSZ) + SIGSTKSZ;

ref: https://docs.kernel.org/x86/elf_auxvec.html#introduction

}
#endif
#endif

memset(&thread, 0, sizeof(thread));
Expand Down
2 changes: 1 addition & 1 deletion configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2375,7 +2375,7 @@ AC_DEFINE(STDC_HEADERS, 1, [Define to 1 if you have the ANSI C header files.])
# checks for header files
AC_CHECK_HEADERS([ \
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/auxvec.h linux/memfd.h linux/random.h linux/soundcard.h \
linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@
/* Define to 1 if you have the `linkat' function. */
#undef HAVE_LINKAT

/* Define to 1 if you have the <linux/auxvec.h> header file. */
#undef HAVE_LINUX_AUXVEC_H

/* Define to 1 if you have the <linux/can/bcm.h> header file. */
#undef HAVE_LINUX_CAN_BCM_H

Expand Down