fix(host): start a thread whose stack is under the static TLS block #2971
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2971
Loading…
Reference in a new issue
No description provided.
Delete branch "omgitsgela/Longshot:fix/ab-thread-stack"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
On Linux the host could not start a thread that asks for a small stack, on a build whose executable carries a large static
TLS block.
host/hos_ab.casks for 64 KiB for its A/B thread, and on a Steam Deck it never started:The cause is in
host/posix/win32_posix.c'sCreateThread: it passed the requested size straight topthread_attr_setstacksize. glibc keeps the executable's static TLS block at the top of every thread's stack and refuses astack too small to hold that block plus a guard page and a rest stack, with EINVAL. The Deck's host binary's
PT_TLSMemSiz is0x00f1f0= 61,936 bytes, so a 64 KiB stack is refused there while the same request starts in a small program (which is whyit reproduced in the game and not in a probe). Windows keeps TLS outside the thread stack and honors any explicit size, so
this never showed there.
This change reads the executable's
PT_TLSonce (dl_iterate_phdr, cached withpthread_once) and treats an explicit stackrequest as a floor: below the block plus 64 KiB, the request is grown to fit. The executable's block is not the whole of
glibc's reservation (a startup library brings its own), so a creation glibc still refuses is retried larger, up to 64 MB. Sizes
are checked for overflow, and a request that cannot be a stack is refused rather than rounded into a small one. Asking for no
size still means the executable's 8 MB, unchanged, and guest threads' 64 MB is untouched.
To see the bug on
main: on a Linux build whose binary carries a static TLS block, run withHOS_AB=<a wired switch>=<value>,<period>,<warm-up>. The log says "the A/B's thread did not start: no A/B in this run" and the frame trace'sab_sidecolumn stays empty. With this change the run logs the alternation andscripts/perf.py abreports its periods.Read first:
host/posix/win32_posix.c(static_tls_size,CreateThread),tests/test_create_thread_small_stack.py.Testing
Tested: Steam Deck LCD (RADV VANGOGH, Mesa 26.2.0-devel, Linux 6.16), runtime built from this branch (base
87045899),HOS_AB=HOS_COLLECT_WIDE=0,4,20, Hyrule Field route, 1280x800 at 50 % render. Before: "the A/B's thread did not start".After: "ab: the warm-up is over: HOS_COLLECT_WIDE alternates now", 16 side 0 and 16 side 1 flips over the run, and
scripts/perf.py abon the trace reports 31 periods (15 side 0, 16 side 1) with each side's means. The A/B is usable again onthis hardware.
Game-free test:
tests/test_create_thread_small_stack.pyfails onmain(the 64 KiBCreateThreadreturns NULL when theprogram carries a 61,936-byte
__threadblock) and passes here. It asserts the chosen size directly (stack_want: the floorfor a small request, the request unchanged above it, 8 MB for none, 0 for an impossible size), then reads back what each
thread actually got with
pthread_getattr_np, so a glibc that refuses the small stack cannot hide a missing floor behind theretry.
tests/test_posix_cv_wake.pyandtests/test_thread_arg_self.py, which cut the same file, still pass.tests/test_host_alloc_wait.pycutsCreateThreadtoo: it now stubs the new
static_tls_size(that fixture starts no thread and asks for no size), after which it passes.tests/run_all.py --strict --changed: 22 passed, 6 failed on this machine (Linux, no Vulkan device). The six(
test_crash_record,test_image_reaper_threads,test_windows_on_linux,test_linux_audio_output,test_mods_list,test_mods_sizes) fail the same way on a cleanmaincheckout here; the full suite on cleanmainon this machine is 683passed, 229 failed, so this environment cannot serve as the gate. CI should be read for that.
Still to test:
host/posix/win32_posix.cis not compiled there (host/win32/is), so the Windows host isunaffected (by hand: nothing to run, no Test Farm run needed).
expecting it to pass (by hand; the test asserts the growth and the pass-through).
Measurements
Nothing to measure: this starts a thread that was not starting, changes no renderer path and no switch's default, and the A/B it restores is itself a measuring tool.
Risks
64 KiB. That costs address space, not committed memory, and only affects explicit small requests: the default (no size)
and guest threads' 64 MB are unchanged, and a request already above the floor is passed through untouched.
requests; a default-size thread is never retried.
dl_iterate_phdrreports noPT_TLSfor the executable, the size is left exactly as before: the change can only growa request, never shrink one.
HOS_AB: no switch, no cache, no saved option.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named):--changedis 22 passed and 6 failed here,the six named in Testing all failing the same way on a clean
maincheckout on this machine.main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened:test_host_alloc_waitcutsCreateThreadand now stubs the new helper, and
test_create_thread_small_stackis added.compiles exactly what it compiled before.
Assisted-by:/Co-Authored-By:trailers.glibc keeps the executable's static TLS block at the top of every thread's stack and refuses, with EINVAL, a stack too small to hold that block plus a guard page and a rest stack. host/posix/win32_posix.c passed the requested size straight through, so a host with a large block could not start a small thread: HOS_AB asks for 64 KiB and the Deck's host binary's block is 61,936 bytes, so the A/B thread never started there ('ab: the A/B's thread did not start'), while the same request starts in a small program. Windows keeps TLS outside the stack and honors any explicit size, so an explicit request is now a floor: below the block plus 64 KiB it is grown to fit, read from the executable's PT_TLS once. Assisted-by: Hermes Agent (DeepSeek)Tested in game: Steam Deck LCD, RADV VANGOGH, Mesa 26.2.0-devel, Linux 6.16. With HOS_AB=HOS_COLLECT_WIDE=0,4,20 in Hyrule Field the A/B thread now starts: the log says 'the warm-up is over ... alternates now', 16 side 0 and 16 side 1 flips in one run, and scripts/perf.py ab reports 31 periods (15/16) from the trace. On main the same launch logs 'the A/B's thread did not start: no A/B in this run'.
Description filled in: the still-to-test line says who runs it, Measurements states why there is nothing to measure, and the checklist names the six tests that also fail on a clean main on this machine. The test that cuts CreateThread now stubs the new helper.
One CI job is red: 'Third-party parts and guide screenshots' (fails after 10 s, before the compile jobs). Its first step is
python3 -I scripts/check_third_party.py, which passes on this branch and on a clean main checkout here ('third_party: 17 parts as their notes say'), and this PR touches nothing underthird_party/. Reporting it as an environment failure rather than a change failure; if it stays red on other PRs it needs its own look.WIP: fix(host): start a thread whose stack is under the static TLS blockto fix(host): start a thread whose stack is under the static TLS blockRecommendation: Approve. The reported Steam Deck run shows that the A/B thread starts again. The new test has synchronization and compiler-selection problems, and CI and the stated different-compiler check are still pending. Nothing blocking is left. Recommend approval; not approved on the forge.
Linux grows an explicitly requested thread stack to accommodate the executable’s static TLS, restoring the A/B thread on the tested Deck. The calculation does not include static TLS from other startup modules, and the new test needs more reliable synchronization.
Should fix (3): not blocking, but worth doing before merging.
host/posix/win32_posix.c:300: The floor counts only the executable’s TLS, although glibc also places startup libraries’ static TLS in each thread’s stack. (more on the line) Do: Account conservatively for the process’s complete static TLS allocation, or use a sufficiently safe stack size for small requests; add a fixture with a startup library carrying substantial TLS.tests/test_create_thread_small_stack.py:67: The fixture uses volatile variables for communication between threads, which does not synchronize C memory accesses. (more on the line) Do: Publish completion with a release atomic store and wait with an acquire load, or synchronize and join the worker before inspecting its result.tests/test_create_thread_small_stack.py:17: The test hard-codes cc, bypassing the runner’s CC selection. (more on the line) Do: Use the runner’s {cc} placeholder and support its compiler command, including any configured arguments, for direct invocation too.@fixcan do L1, L2 and L3.Checks: Failing on this head, which is behind its base, where they pass:
CI / Third-party parts and guide screenshots (pull_request),CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request).@rebase(or@fix) brings it up to date and runs them again; not held against it. Still running: 7 checks.More: does it help, other systems, switches, what was read
Does it help: Yes: the author reports that the A/B thread failed to start on main and produced 16 flips on each side with this change on a Steam Deck. This is a functional fix, not a performance claim.
Other systems: Windows uses a different host implementation. Linux CreateThread calls with explicit sizes use the new floor; default-size threads retain their existing size. Startup libraries with large static TLS can still exhaust the calculated floor.
Switches: None.
Reviewed:
dd62e0eff5against4908cc6dad: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the PR template, all three changed files and their diff, commit messages, the A/B caller, related source-cut tests, and relevant test-runner and loader code. Skimmed the remainder of win32_posix.c and the test runner. docs/review-checklist.md and the adjacent Tooling checkout were absent.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no code or tests were run. The author reports the new test passing and the Deck A/B run succeeding. Several CI jobs remain running, and the PR lists a different-compiler check as still to test.
Overlaps and merge order: Not checked; network access was unavailable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, as omgitsgela took it out of WIP. Nothing was run; a maintainer decides.
@ -293,0 +297,4 @@glibc that takes it, leaves a few KiB of usable stack. Read from the phdrs rather than guessed, once. */static int tls_of_main(struct dl_phdr_info *info, size_t size, void *data) {(void)size;if (info->dlpi_name && info->dlpi_name[0]) return 0; /* (a library's, not the executable's) */L1 (should fix): The floor counts only the executable’s TLS, although glibc also places startup libraries’ static TLS in each thread’s stack.
When: If a Linux process starts with a preloaded library containing a sufficiently large static TLS block, the 64 KiB A/B request is grown using only the executable’s block. The combined TLS can still exceed that stack, so pthread_create fails and A/B remains disabled.
Do: Account conservatively for the process’s complete static TLS allocation, or use a sufficiently safe stack size for small requests; add a fixture with a startup library carrying substantial TLS.
@ -0,0 +14,4 @@import sysimport tempfileRUN_ALL_ARGS = ["--cc", "cc"]L3 (should fix): The test hard-codes cc, bypassing the runner’s CC selection.
When: With CC set to clang, the suite still invokes cc for this test; on a system with the selected compiler but no cc alias, the check is skipped or fails even though a compiler is available.
Do: Use the runner’s {cc} placeholder and support its compiler command, including any configured arguments, for direct invocation too.
@ -0,0 +64,4 @@}"""cases = r"""static volatile int body_ran;L2 (should fix): The fixture uses volatile variables for communication between threads, which does not synchronize C memory accesses.
When: The worker writes body_ran and seen_stack while the main thread reads them without a happens-before relationship. The test has undefined behavior and can miss completion or read a stale stack size under optimization.
Do: Publish completion with a release atomic store and wait with an acquire load, or synchronize and join the worker before inspecting its result.
Review answered, head
4de0ebbd. The executable's PT_TLS is not glibc's whole reservation, so the chosen size is now stack_want (0 for an impossible size, 8 MB for none, the request above the block plus 64 KiB, that floor otherwise) and a refused creation is retried larger, up to 64 MB. Rounding and the floor addition are overflow-checked, pthread_attr_setstacksize's answer is no longer ignored, and an unrepresentable request is refused instead of rounded into a small thread. The test asserts stack_want's arithmetic directly (a glibc that refuses the small stack would otherwise hide a missing floor behind the retry), reads back what each thread got, uses C11 atomics instead of volatile, escapes the TLS block as well as writing it, and no longer spins on a fixed yield count. The fixture that cuts CreateThread stubs the two helpers; the changed subset is 22 passed, 6 failed, the six being the ones that also fail on clean main here.Second red job on head
4de0ebbd: 'Website build and checks', failing after 33 s, also in its setup steps (main's tree restored from the cache, then checkout) rather than in the site build. Both red jobs fail before they reach anything this PR touches, and the third-party script passes locally on this branch and on clean main.@review
Recommendation: Approve. The reported A/B thread now starts on the Steam Deck, and the remaining ledger items are minor. Startup-library TLS can still reduce the usable stack, and the test runner still bypasses its selected C compiler. Recommend approval; not approved on the forge.
The POSIX thread shim grows small explicit stack requests and retries creation when glibc rejects the size. This restores the A/B tool on the reported system at the cost of larger stack reservations.
Should fix (2): not blocking, but worth doing before merging.
host/posix/win32_posix.c:300: The retry accounts for library TLS only when thread creation fails; a successful creation can still leave less usable stack than the floor intends. When: If startup libraries consume much of the space above the executable's TLS but glibc still accepts the chosen size, the thread starts without retrying and has less than 64 KiB of remaining stack. Do: Account for the complete static TLS reservation when choosing the floor, including startup libraries, and cover the accepted-but-small case.tests/test_create_thread_small_stack.py:17: The test hard-codes cc, bypassing the runner’s CC selection. When: With CC set to clang, the suite still invokes cc for this test; on a system with the selected compiler but no cc alias, the check is skipped or fails even though a compiler is available. Do: Use the runner’s {cc} placeholder and support its compiler command, including any configured arguments, for direct invocation too.@fixcan do L1 and L3.Since the last review (on
dd62e0eff5)Resolved: L2 (The fixture uses volatile variables for communication between threads, which does not sync…)
Still open: L1 (The retry accounts for library TLS only when thread creation fails; a successful creation…); L3 (The test hard-codes cc, bypassing the runner’s CC selection.)
Checks: Failing on this head, which is behind its base, where they pass:
CI / Third-party parts and guide screenshots (pull_request),CI / Website build and checks (pull_request),CI / Game-free checks (run_all.py, part 1/2) (pull_request),CI / Game-free checks (run_all.py, part 2/2) (pull_request).@rebase(or@fix) brings it up to date and runs them again; not held against it.More: does it help, other systems, switches, what was read
Does it help: Yes. The author reports that the A/B thread failed to start on main but alternated 16 times per side on the Steam Deck with this change; the resulting trace contained 31 periods. This is a functional fix, not a performance claim.
Other systems: The POSIX shim is also built into the Linux Vulkan backend, so its explicit thread requests can be affected. Windows uses a separate implementation; default-size requests and the tested 64 MiB request retain their sizes. No separate Linux compiler run is reported.
Switches: None added, changed or removed.
Reviewed:
4de0ebbdf0against4908cc6dad: Read the PR diff, changed test and fixture, thread creation code, A/B caller, runner compiler selection, repository rules, PR template, comments and CI summary. The full POSIX source output was truncated; unrelated portions were skimmed. The referenced review checklist was absent.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run code were executed. The author reports the new game-free test passing, a successful Steam Deck game run, and six changed-suite failures also seen on main. The listed CI failures are attributed to the branch being behind its base.
Overlaps and merge order: Not established from the supplied information.
Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.