RFC 2: MT safe fiber context switch on AArch64#15581
Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom Mar 24, 2025
Merged
Conversation
We need a store-store barrier between pushing the registers to the current stack and setting the resumable flag of the current fiber, otherwise the CPU is allowed to reorder the instructions at runtime and to store the resumable flag before or while storing the registers. This can happen for example: thread 1: enqueues current fiber A therad 1: swapcontext -> store resumable thread 1: is preempted thread 2: steals fiber A thread 2: resumes fiber A thread 2: loads registers => reads garbage => segfaults thread 1: stores registers (too late)
straight-shoota
approved these changes
Mar 21, 2025
Qard
approved these changes
Mar 22, 2025
straight-shoota
pushed a commit
that referenced
this pull request
Mar 31, 2025
We need a store-store barrier between pushing the registers to the current stack and setting the resumable flag of the current fiber, otherwise the CPU is allowed to reorder the instructions at runtime and to store the resumable flag before or while storing the registers thread 1: enqueues current fiber A therad 1: swapcontext -> store resumable thread 1: is preempted thread 2: steals fiber A thread 2: resumes fiber A thread 2: loads registers => reads garbage => segfaults thread 1: stores registers (too late) The chosen assembly is compatible with armv6, but might not work on older architectures, and it doesn't take advantage of armv7 supporting the `dmb ish` instruction. We might want to consider integrating #14524 (comment) into the compiler, so we could add flags based on features, for example `armv7` when any feature matches `/\+v7(ve|r|m|em|s|k|)/`. This is the ARM32 equivalent of #15581.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We need a store-store barrier between pushing the registers to the current stack and setting the resumable flag of the current fiber, otherwise the CPU is allowed to reorder the instructions at runtime and to store the resumable flag before or while storing the registers.
This can happen for example:
The current implementation didn't need the barrier because fibers are stuck to a single thread (it must have finished swapcontext to be able to start another swapcontext), but RFC 2 adds work stealing to the MT environment.
Note: the same issue can happen in ARM32.