Skip to content
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

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

Merged
merged 3 commits into from
Mar 11, 2022

Conversation

oleksandr-pavlyk
Copy link
Contributor

@oleksandr-pavlyk oleksandr-pavlyk commented Mar 9, 2022

In Linux kernel 5.14 one can dynamically request size of altstacksize
needed to handle signals based on hardware capabilities.
This patch sets stack size to be the maximum of current value of SIGSTKSZ * 2 and
SIGSTKSZ + 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.

This change proposes a solution to the problem described
in https://bugs.python.org/msg414809

https://bugs.python.org/issue46968

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@oleksandr-pavlyk

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@oleksandr-pavlyk
Copy link
Contributor Author

oleksandr-pavlyk commented Mar 9, 2022

The CLA has been signed. I am the first time contributor to CPython proper. Please guide me through the process of getting this PR to the required state to make the merge possible. Thank you.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 9, 2022

The CLA has been signed. I am the first time contributor to CPython proper. Please guide through the process of getting this PR to the required state to make the merge possible. Thank you.

Thanks for signing the CLA! It may take a few days for the bot to update your PR with the "CLA signed" label, as the form needs to be manually reviewed by a human at the PSF :)

The first step is to change the title of your PR to "bpo-46968: Query altstack size dynamically on Linux kernel >=5.14". This will cause the bots to automatically link this PR to that BPO issue, which will make the relevant CI check go green.

The second thing you'll need to do is to add a NEWS entry to your PR, which you can do using https://blurb-it.herokuapp.com.

@oleksandr-pavlyk oleksandr-pavlyk changed the title Query altstack size dynamically on Linux kernel >=5.14 bpo-46968: Query altstack size dynamically on Linux kernel >=5.14 Mar 9, 2022
@AlexWaygood
Copy link
Member

You'll probably also need to add a test to your PR to make it mergeable.

For a fuller guide to the process, the devguide is here :)

@oleksandr-pavlyk
Copy link
Contributor Author

You'll probably also need to add a test to your PR to make it mergeable.

Since size of size of sigaltstack used by faulthandler extensions is not user accessible to the best of my knowledge, I am not entirely sure how once can test the change.

Suggestions are welcome.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 9, 2022

Suggestions are welcome.

I'm afraid this is an area I know next to nothing about, so I can't really help you any further I'm afraid :/

Don't worry too much about it — if it seems like a good idea, a core developer with more expertise in this area should be able to help you with writing tests 🙂 (though there's quite a large PR backlog, so it might take a while).

Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

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

The core of the changes look good, the annoying part is in the generated configure and aclocal.m4 files - those should be generated with the same version as the existing ones.

See the 2022 comment on https://bugs.python.org/issue44035 for a pointer to a docker image that can be used for the autoconf regeneration stuff. (that image is not yet documented AFAICT)

aclocal.m4 Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@gpshead gpshead self-assigned this Mar 10, 2022
@gpshead gpshead requested a review from tiran March 10, 2022 08:46
@gpshead gpshead added type-bug An unexpected behavior, bug, or error needs backport to 3.10 only security fixes labels Mar 10, 2022
configure.ac Outdated Show resolved Hide resolved
@oleksandr-pavlyk
Copy link
Contributor Author

I have made the requested changes; please review again

I modified configure.ac as requested by @tiran

I regenerated configure and aclocal.m4 by rerunning autoreconf using

bash-4.4$ autoreconf --version
autoreconf (GNU Autoconf) 2.69
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.

Assuming this change eventually gets accepted, what is the process of requesting backports to all earlier versions of CPython?

@bedevere-bot
Copy link

Thanks for making the requested changes!

@gpshead: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from gpshead March 10, 2022 12:39
Copy link
Member

@tiran tiran left a comment

Choose a reason for hiding this comment

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

You need a verify specific combination of autoconf 2.69 + runstatedir patch, autoarchive version, and pkg-config m4 macro version. Your version of autoconf aclocal macros is too old. If you cannot use the container image to regenerate the files, then apply the changes manually.

  • revert aclocal.m4 and configure changes.
  • manually add linux/auxvec.h to configure

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Introduced HAVE_LINUX_AUXVEC_H in configure.ac and pyconfig.h.in
Used cpython_autoconf:269 docker container to generate configure
@oleksandr-pavlyk
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@tiran, @gpshead: please review the changes made to this pull request.

Modules/faulthandler.c Outdated Show resolved Hide resolved
Modules/faulthandler.c Outdated Show resolved Hide resolved
oleksandr-pavlyk and others added 2 commits March 11, 2022 04:11
In Linux kernel 5.14 one can dynamically request size of altstacksize
based on hardware capabilities. This patch sets stack size to be
the maximum of current value of SIGSTKSZ * 2 and
SIGSTKSZ + 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.
@oleksandr-pavlyk
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@tiran, @gpshead: please review the changes made to this pull request.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

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

@vstinner
Copy link
Member

I wrote PR #31824 to add os.sysconf_names['SC_MINSIGSTKSZ'].

@miss-islington
Copy link
Contributor

Thanks @oleksandr-pavlyk for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @oleksandr-pavlyk and @vstinner, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 3b128c054885fe881c3b57a5978de3ea89c81a9c 3.10

@miss-islington miss-islington assigned vstinner and unassigned gpshead Mar 11, 2022
@vstinner
Copy link
Member

Merged, thank you!

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Mar 11, 2022
@bedevere-bot
Copy link

GH-31830 is a backport of this pull request to the 3.10 branch.

@vstinner
Copy link
Member

I fixed the conflict on configure.ac and backported the change to 3.10: #31830 Then I will backport the fix to 3.9.

vstinner added a commit that referenced this pull request Mar 11, 2022
…1830)

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]>
vstinner added a commit that referenced this pull request Mar 11, 2022
…1831)

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]>
@oleksandr-pavlyk oleksandr-pavlyk deleted the altstack-control branch March 12, 2022 00:59
hello-adam pushed a commit to hello-adam/cpython that referenced this pull request Jun 2, 2022
…pythonGH-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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants