Skip to content

KernelX: audit thread priority mappings and CPU affinity mask bounds (#106) - #1609

Open
slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/kernelx-priority-affinity
Open

slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/kernelx-priority-affinity

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

Summary

Audits thread priority mappings and CPU affinity mask bounds for console 7-core environments in crates/xcl/src/hle/kernelx.rs and resolver.rs:

  1. Thread priority level mappings & bounds (Issue Runtime / Loader / Platform: static TLS alignment & attach races, eh::dump_threads deadlock & VEH bounds, C-unwind FFI boundaries, AVX2 check, and hybrid CPU affinity / PLS quota #106):
    • Mapped non-standard and console integer priority tiers via map_thread_priority to valid Win32 constants (3..=6 -> HIGHEST, >=7 -> TIME_CRITICAL, -14..=-3 -> LOWEST, <= -15 -> IDLE), preventing Windows NT ERROR_INVALID_PARAMETER (87) failures.
    • Implemented SetThreadPriority, GetThreadPriority, SetThreadPriorityBoost, and GetThreadPriorityBoost.
    • Added null thread handle validation returning ERROR_INVALID_HANDLE (6) and null pointer validation on p_disable_priority_boost returning ERROR_INVALID_PARAMETER (87).
    • Exported thread priority APIs and wired them in resolver for console/legacy imports.
  2. CPU affinity mask bounds & console core fitting:
    • Fixed fit_affinity_mask: previously, mixed affinity masks (bits partially inside and partially outside process affinity) discarded out-of-process console cores completely. The updated mapping retains in-process bits and maps out-of-process console cores modulo host available processors.
    • Defined console constants CONSOLE_TITLE_CORES (7), CONSOLE_TITLE_AFFINITY_MASK (0x7F), CONSOLE_TITLE_SMT_AFFINITY_MASK (0x3FFF).
    • Implemented fit_ideal_processor, SetThreadIdealProcessor, and SetThreadIdealProcessorEx, mapping console core IDs onto available host processors and validating null out-pointers and handles.
    • Hardened SetThreadpoolAffinityMask: validates null threadpool pointers and zero affinity masks, returning ERROR_INVALID_PARAMETER.
    • Hardened SetThreadAffinityMask and SetThreadGroupAffinity: validates null thread handles (ERROR_INVALID_HANDLE) and zero masks (ERROR_INVALID_PARAMETER).
  3. Unit tests:
    • Added unit tests:
      • affinity_masks_outside_the_process_are_fitted
      • set_thread_affinity_mask_takes_console_masks
      • threadpool_affinity_mask_validation_and_bounds
      • ideal_processor_bounds_and_fitting
      • thread_priority_mappings_and_error_handling

Changes

  • crates/xcl/src/hle/kernelx.rs: thread priority tier mappings, affinity bounds fitting, and validation.
  • crates/xcl/src/resolver.rs: exported thread priority and ideal processor APIs.

Tested

  • Windows 11 x64, Rust MSVC stable.
  • cargo test -p xcl -- kernelx: 35 passed, 0 failed.
  • Clean diff: zero trailing whitespace, clean rebase on latest upstream/main, GPL-2.0-only compliant, zero retail game data or keys.

Not Tested

  • AMD 3D V-Cache preferred core scheduling hints.

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI-assisted review; I checked the sparse-mask case against the added implementation and tests.

if (allowed >> b) & 1 != 0 {
m | (1 << b)
} else {
m | (1 << pcs[b as usize % pcs.len()])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A mixed mask can collapse onto one allowed CPU. For mask = 0b0111 and allowed = 0b1010, CPUs 0 and 2 map to pcs[0] (CPU 1), while requested CPU 1 is retained, so this returns only 0b0010 and never uses allowed CPU 3. This leaves a title that requested three processors pinned to one even though two are available. Could the remapping distribute out-of-range bits without duplicating a retained CPU where possible? AI-assisted review; I verified this case against affinity_masks_outside_the_process_are_fitted.

@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression run for this PR: head a7e3240, based on main 9e3831e. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, back to back with the main run.

main 9e3831e:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1587 (PresentX 1588)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 99220 MB

this PR:

xcl-smoke v1
commit: a7e324046d4a3562ada2e7eed0b5bfb3ae9f4ce8
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1513 (PresentX 1513)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 97315 MB

No device removals or crashes; 95 % of main's frames, 98 % of its peak commit.

Peak commit on this machine is mostly the pipeline preload's DXC compiles (#1035) and varies a lot from run to run, so it says little about this PR.

Not tested: the whole suite; whether the title reaches the changed code in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CONTRIBUTING.md also requires the full workspace GPU suite and a real-title regression comparison for runtime changes. This description reports only cargo test -p xcl -- kernelx and no scripts/smoke.ps1 blocks for main and this head. Please add both results before merge, including the skipped GPU test names. AI-assisted review; I checked the Tested and Regression run sections against the guide.

@futurepro62

Copy link
Copy Markdown

Full-suite GPU run (NVIDIA and AMD), which the review above asks for. Tested exact head a7e3240 (on its base 9e3831e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form: 778 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: threadpool_affinity_mask_validation_and_bounds, ideal_processor_bounds_and_fitting, thread_priority_mappings_and_error_handling.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 779 passed, 0 failed, 41 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • heap_window_out_of_memory_falls_back_to_the_switch_form was skipped with --skip. It fails on main 9e3831e too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (XCL_HEAP_WINDOWS unset). Reported as Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored.

Thanks!

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
… upstream xcl-dev#1609 priority half)

Windows hle/kernelx.rs + resolver.rs hunks verbatim reference-only
(cfg(windows)-only): THREAD_PRIORITY_ERROR_RETURN, map_thread_priority,
Set/GetThreadPriority, Set/GetThreadPriorityBoost, 4 export rows,
SetThreadPriority resolver row, priority test. Linux twin
hle/kernelx_linux.rs: map + map-before-store, Get null/unknown error
return, new Boost fns, export rows; hle_linux sync count 69->73.
Affinity/ideal half stays for step-80 (old fit intact, 0 new hits).
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…rt upstream xcl-dev#1609 affinity/ideal half)

Windows hle/kernelx.rs hunks verbatim reference-only (cfg(windows)-only):
PROCESSOR_NUMBER + sys SetThreadIdealProcessor/Ex imports, 2 export rows,
CONSOLE_TITLE consts, SetThreadpoolAffinityMask validation, partial-overlap
fit_affinity_mask rewrite, affinity null/zero guards, fit_ideal_processor +
SetThreadIdealProcessor/Ex, affinity/ideal tests. Linux twin
hle/kernelx_linux.rs: CONSOLE_TITLE consts, partial-overlap fit rewrite,
null/zero guards on all three affinity fns, PROCESSOR_NUMBER +
fit_ideal_processor + IdealProcessor fns (best-effort record-only, no
affinity change: an ideal processor is a hint, not a pin), export rows;
hle_linux sync count 73->75; resolver SetThreadIdealProcessor row.
Rebases onto step-79 (shared exports table, landed a6b1181).
Failing-first: extended fit + new guard tests FAILED pre-fix, green after.
@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 28, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head a7e3240

No open or merged PR makes the same change: main does not hook SetThreadPriority / SetThreadIdealProcessor, and its fit_affinity_mask still cuts a partly-overlapping mask to the allowed bits. The other #106 PRs (#196, #240, #291, #1367, #1376, #1431, #1436) touch TLS, the PE loader, reports and RegisterClassExA, not affinity or priority.
Notes on the diff (kernelx.rs):

  • It bundles three changes: priority mapping, ideal-processor fitting, and affinity validation plus a new partial-overlap mapping. They would be easier to review as separate PRs.
  • map_thread_priority maps every value from 7 to 31 to TIME_CRITICAL with no source for these console tiers.
  • It declares its own PROCESSOR_NUMBER and raw-dylib externs instead of using windows_sys.
  • The PR description check fails.
    Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants