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

gh-121368: Fix seq lock memory ordering in _PyType_Lookup #121388

Merged
merged 4 commits into from
Jul 8, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jul 4, 2024

The _PySeqLock_EndRead function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.

Additionally, we need a release fence in _PySeqLock_LockWrite to
ensure that the sequence update is visible before any modifications to
the cache entry.

Also fix up the return types of a few seqlock functions.

@ambv
Copy link
Contributor

ambv commented Jul 5, 2024

After running all repros I had for several hours I confirm this change solves the issue.

The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section.

The acquire on the final sequence load isn't sufficent (or necessary).
@colesbury colesbury marked this pull request as ready for review July 8, 2024 15:40
@colesbury colesbury requested a review from DinoV July 8, 2024 15:40
@colesbury
Copy link
Contributor Author

Here's the relevant portion of a Rust seqlock implementation for reference:

https://github.com/Amanieu/seqlock/blob/b72653fd9c38141da7e588a97a1b654239f5d4df/src/lib.rs#L138-L141

@colesbury
Copy link
Contributor Author

I've added a model for the C11 model check here: https://github.com/colesbury/c11-model-checker/blob/cpython-models/test/seqlock.c

(Turns out we need the release fence too)

Copy link
Contributor

@DinoV DinoV left a comment

Choose a reason for hiding this comment

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

LGTM!

Python/lock.c Outdated
if (_Py_atomic_load_uint32_acquire(&seqlock->sequence) == previous) {
// gh-121368: We need an explicit acquire fence here to ensure that
// this load of the sequence number is not reordered before any loads
// withing the read lock.
Copy link
Contributor

Choose a reason for hiding this comment

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

withing -> within?

@colesbury colesbury merged commit 1d3cf79 into python:main Jul 8, 2024
36 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@colesbury colesbury deleted the gh-121368-seq-lock branch July 8, 2024 18:52
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 8, 2024
…onGH-121388)

The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.

Additionally, we need a release fence in `_PySeqLock_LockWrite` to
ensure that the sequence update is visible before any modifications to
the cache entry.
(cherry picked from commit 1d3cf79)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jul 8, 2024

GH-121505 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 8, 2024
colesbury added a commit that referenced this pull request Jul 8, 2024
…121388) (#121505)

The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.

Additionally, we need a release fence in `_PySeqLock_LockWrite` to
ensure that the sequence update is visible before any modifications to
the cache entry.
(cherry picked from commit 1d3cf79)

Co-authored-by: Sam Gross <[email protected]>
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
…on#121388)

The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.

Additionally, we need a release fence in `_PySeqLock_LockWrite` to
ensure that the sequence update is visible before any modifications to
the cache entry.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
…on#121388)

The `_PySeqLock_EndRead` function needs an acquire fence to ensure that
the load of the sequence happens after any loads within the read side
critical section. The missing fence can trigger bugs on macOS arm64.

Additionally, we need a release fence in `_PySeqLock_LockWrite` to
ensure that the sequence update is visible before any modifications to
the cache entry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants