fix(vcores): keep a thread that never had a mask on its ideal core #2368
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!2368
Loading…
Reference in a new issue
No description provided.
Delete branch "DuckNintendo/Longshot:fix/vcore-unmasked-ideal"
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
Follow-up to #327. That change made a raw mask 0 mean “no affinity”, but a thread that never called
SetThreadCoreMaskalso has mask 0, so every such thread — includingMainThread— started floating instead of staying on the core it was created for. This PR records whether a mask was ever set: only an explicit 0 floats, and a thread that was never masked stays on its ideal core, as the kernel and main did.On the mobile Radeon 780M here the new-game movie runs at about 37 fps and Hyrule Field at about 10 fps; main and this PR are the same within the run spread. To see the bug on main, run
python -I -B tests/test_vcore_mask0.py --source <main's host/hos_thread.c>: the never-masked checks fail becausevc_allowed()returns every core andvc_free_for()floats such a thread. With this PR the fixture printsok.SetThreadCoreMasknow publishes its ideal core, mask and mask_set under the scheduler lock, and the svc call returns the kernel's InvalidCombination for an empty mask instead of success. Readhost/hos_thread.c’svc_set_affinity(),mask_set,vc_allowed(),vc_free_for()andvc_pref_core()first;host/hos_svc.c’sSetThreadCoreMaskcase and the new cases intests/test_vcore_mask0.pyfollow.Testing
Tested: Linux, host-only fixture and the full game-free suite; and the game on the Radeon 780M, headless, with interleaved main/PR runs.
python -I -B tests/test_vcore_mask0.pypasses, including the never-masked and first-SetThreadCoreMaskcases; the fixture fails on main at the new never-masked checks.python -I -B tests/run_all.py --jobs 8gives 855 passed, 11 failed and 108 skipped; untouched59f9632bgives 854 passed with the same 11 failures:tests/test_amd_subgroup_profiles.py,tests/test_dev_packs.py,tests/test_flat_map.py,tests/test_guest_holes.py,tests/test_pipeline_state_computes.py,tests/test_rdna2_mesh_only.py,tests/test_readback_timeout_source.py,tests/test_shader_profile_caps.py,tests/test_target_image_reuse.py,tests/test_vram_compact.pyandtests/test_wb_spans.py. The touched host files compile with the build's flags. The newtests/test_svc_core_mask.pychecks the empty-mask result and that the thread's affinity is unchanged. On the 780M, two interleaved runs of the title plus new-game movie and two of Hyrule Field showed no crash and no regression: movie 37.0 against 36.95 fps, Hyrule Field 9.83 against 9.97 fps; the details are below.Still to test:
Measurements
Verdict: the new fixture fails on main and passes here; the full game-free suite is unchanged; on the Radeon 780M the new-game movie and Hyrule Field runs are within run spread (movie 37.0 against 36.95 fps, field 9.83 against 9.97 fps).
Fixture and suite
Setup: Linux host-only; the fixture compiles the virtual-core block from
host/hos_thread.cwith a host harness, and the suite runs eachtests/test_*.pyin its own process. No game or GPU path is involved.59f9632be1aff25dMain's new fixture output:
This PR:
Game runs on the Radeon 780M
Setup: Linux, AMD Zen 4 8-core/16-thread APU with Radeon 780M (RADV Phoenix), Mesa 26.0.2, 60 GB; headless offscreen 1920x1080, uncapped. A
dev.py packsbase at59f9632b, with main and this PR as runtimes; two interleaved runs each, main/PR/main/PR. The movie run holds the title for 20 s, then measures 130 s of the new-game movie; Hyrule Field is the fixed save, 10 s warm-up and 60 s of scripted movement. Frame times fromRX_VK_FRAME_LOG; scheduling from the 5 svcores:lines.59f9632b65f2456fThe title screen runs at about 35 fps and the new-game movie at about 37 fps; Hyrule Field is GPU-bound at about 10 fps on the 780M. The scheduling counters in the final 5 s were: movie 100 us mean wait and 0 escapes on both; Hyrule Field 676/741 us mean wait on main and 635/769 us here, 0 over the long-wait threshold and 0 escapes on both. No crash in either side. Those two-run medians are at
65f2456f; on the final heade1aff25da single pair measured movie 29.7 (main) / 30.4 (this PR) fps and Hyrule Field 9.8 / 9.8 fps, so the lock change did not move the game result.The final 5 s lines (run 1 of each):
Risks
A thread that never called
SetThreadCoreMaskis pinned again, as on main; if the game relied on such a thread floating, that behaviour is lost. The explicit mask-0 path from #327 is unchanged. ThesvcSetThreadCoreMask(0)call is ignored as the real kernel does.Turning it off: revert the commit; there is no switch. Saves, options, caches and the vk ABI are unchanged.
Checklist
Ready for review
main; the title is a Conventional Commit.python -I -B tests/run_all.py --jobs 8), with the failures named above.mise run testpasses (the same failures named above also fail onmain).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.Assisted-by:/Co-Authored-By:trailers.Ready to merge
Generated by opencode with deepseek-v4.1-flash-1017
fix(vcores): keep a thread that never had a mask on its ideal coreto WIP: fix(vcores): keep a thread that never had a mask on its ideal core31858e077656f3ab706b56f3ab706b65f2456f56@review
Recommendation: Request changes. The new affinity state can race with scheduling, and a rejected service call incorrectly reports success. The scheduler change also needs in-game validation on Windows and Linux.
The change keeps never-masked threads on their initial virtual core while preserving explicit mask-zero floating. The fixture demonstrates that distinction, but the new shared state needs synchronization and the service-call rejection needs a correct result.
Blocking (2): must change before merging.
host/hos_thread.c:130: The new mask_set field is written without the lock used by scheduler readers, so the affinity state has a data race and can be observed inconsistently. (more on the line) Do: Publish the ideal core, mask and mask-set state together under vc_lock, and synchronize other readers and writers of that state; arrange the notification so it does not reacquire the lock recursively.host/hos_svc.c:427: An empty mask is ignored but the service call still returns success for a valid handle. (more on the line) Do: Return the kernel's invalid-affinity result for an empty mask and add a service-call test covering the result and unchanged affinity.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
host/hos_thread.c:1869: The changed scheduling behavior has not been exercised in the game on Linux. (more on the line) Do: Run the game on Linux with masked and never-masked threads active, compare against the base, and report behavior and frame pacing.host/hos_thread.c:1869: The changed scheduling behavior has not been exercised in the game on native Windows. (more on the line) Do: Run the game on native Windows with masked and never-masked threads active, compare against the base, and report behavior and frame pacing.@fixcan do L1 and L2.Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven in the game. The author reports that the new host fixture fails on base
59f9632band passes on65f2456f; the reported game-free suite has the same 854 passed, 11 failed and 108 skipped on both revisions.Other systems: The virtual-core scheduler is on by default and affects guest thread scheduling on both Windows and Linux, independently of GPU vendor. Pinning can change contention and frame pacing; no game runs were reported. HOS_VCORES=0 bypasses this scheduler.
Switches: None added, changed or removed.
Reviewed:
65f2456f56against59f9632bef: Read the repository rules, PR template, standards, commit and all three diffs; read the changed test and the scheduler, thread creation, affinity callers and service-call sections. Other portions of the large host files were skimmed, not read in full. The referenced Tooling checkout and docs/review-checklist.md were absent.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no commands from the project, builds or tests were run. CI was still running in the supplied status.
Overlaps and merge order: Not checked; network access is closed. The PR identifies #327 as its predecessor.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -425,2 +425,3 @@HosThread *t = thread_handle((uint32_t)c->x[0]);if (t) hos_thread_affinity(t, (int)c->x[1], c->x[2]);/* the real kernel refuses an empty affinity; only nn::os::SetThreadCoreMask's 0 is the game's no-affinity */if (t && c->x[2]) hos_thread_affinity(t, (int)c->x[1], c->x[2]);L2 (blocking): An empty mask is ignored but the service call still returns success for a valid handle.
When: A guest calls svcSetThreadCoreMask with a valid thread handle and mask 0. Its affinity remains unchanged, yet x0 is R_OK, so the caller is told that the rejected operation succeeded.
Do: Return the kernel's invalid-affinity result for an empty mask and add a service-call test covering the result and unchanged affinity.
@ -125,3 +127,4 @@/* (the real kernel refuses an empty affinity; 0 is this game's own no-affinity convention, and itstrampoline caches the entry core for it) */t->core_mask = mask;t->mask_set = 1;L1 (blocking): The new mask_set field is written without the lock used by scheduler readers, so the affinity state has a data race and can be observed inconsistently.
When: One guest thread calls SetThreadCoreMask on a running or queued thread while vc_allowed or vc_free_for examines that target under vc_lock. The writer changes core_mask and mask_set before taking that lock, so the scheduler can observe the intermediate state or race on mask_set and choose the wrong allowed cores.
Do: Publish the ideal core, mask and mask-set state together under vc_lock, and synchronize other readers and writers of that state; arrange the notification so it does not reacquire the lock recursively.
@ -1858,1 +1867,3 @@if (t->core_mask == 0) return all; /* (the guest's no-affinity value) *//* (0 is the guest's no-affinity value only after SetThreadCoreMask; before that it is "never set", and thethread stays on its ideal core as the kernel and main pinned it) */if (t->core_mask == 0) return t->mask_set ? all : (1u << vc_pin_core(t));L3 (should fix): The changed scheduling behavior has not been exercised in the game on Linux.
When: Never-masked threads now contend for their ideal core instead of using another free core; the host fixture does not show whether gameplay remains responsive or frame pacing changes.
Do: Run the game on Linux with masked and never-masked threads active, compare against the base, and report behavior and frame pacing.
L4 (should fix): The changed scheduling behavior has not been exercised in the game on native Windows.
When: The same core restriction changes contention on Windows, where the host threading implementation differs from Linux; the Linux-only fixture cannot establish game behavior there.
Do: Run the game on native Windows with masked and never-masked threads active, compare against the base, and report behavior and frame pacing.
WIP: fix(vcores): keep a thread that never had a mask on its ideal coreto fix(vcores): keep a thread that never had a mask on its ideal coreAddressed in
e1aff25d.SetThreadCoreMasknow writesideal_core,core_maskandmask_settogether undervc_lock;vc_set_affinity()calls a locked remask helper, so it does not reacquire the lock. The fixture exercises the first-set path through that function.svc::ResultInvalidCombination(0xE801) for an empty mask and does not call the affinity function. The newtests/test_svc_core_mask.pychecks the result and that the thread's affinity is unchanged; a valid mask still returns success and passes the ideal core and mask through.65f2456f; movie 37.0 against 36.95 fps, Hyrule Field 9.83 against 9.97 fps, no crash. A final-head pair ate1aff25dmeasured 29.7/30.4 fps in the movie and 9.8/9.8 in the field, so the lock change did not move the result.The game-free suite at
e1aff25dis 855 passed, 11 failed, 108 skipped; untouched59f9632bis 854 passed with the same 11 failures.Generated by opencode with deepseek-v4.1-flash-1017
@review
Recommendation: Approve. The code is ready: only testing is left. The affinity and empty-mask fixes address the reported defects, and the Linux game runs show no clear regression. Native Windows game testing remains outstanding. Recommend approval; not approved on the forge.
Threads that never set a mask stay on their ideal virtual core, while an explicitly set zero mask retains the game's floating behavior. Affinity updates are now published under the scheduler lock, and the service rejects an empty mask.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
host/hos_thread.c:1869: The changed scheduling behavior has not been exercised in the game on native Windows. When: The same core restriction changes contention on Windows, where the host threading implementation differs from Linux; the Linux-only fixture cannot establish game behavior there. Do: Run the game on native Windows with masked and never-masked threads active, compare against the base, and report behavior and frame pacing.Since the last review (on
65f2456f56)Resolved: L1 (The new mask_set field is written without the lock used by scheduler readers, so the affin…); L2 (An empty mask is ignored but the service call still returns success for a valid handle.); L3 (The changed scheduling behavior has not been exercised in the game on Linux.)
Still open: L4 (The changed scheduling behavior has not been exercised in the game on native Windows.)
Checks: Still running: 9 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes: the reported fixture fails on main and passes with the fix. On Linux, two interleaved Radeon 780M runs showed movie averages of 37.01 versus 36.95 fps and field averages of 9.83 versus 9.97 fps; these differences are within run spread.
Other systems: The scheduler affects native Linux and Windows and CPUs with different core counts. Linux game and host-fixture results are reported; native Windows and a low-core CPU or Steam Deck remain untested.
Switches: None added, changed or removed.
Reviewed:
e1aff25d5eagainst59f9632bef: Read the repository rules, pull request template, diffs, changed scheduler and service paths, and fixture changes. The full host files and test files were skimmed; the combined file output was truncated.; nothing run (a code review).Checked and fine:
Testing: No commands from the project were run. The author reports a passing focused fixture and the same 11 game-free suite failures on base and head; CI is still running.
Overlaps and merge order: Not checked; the network is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.