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

Open
github-bot wants to merge 1 commit from github-bot/gh-1609 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 18:14 UTC · original
slopgobblercobbler:fix/kernelx-priority-affinity → main · +347 −8 · 2 files · 1 commits · open

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 #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

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 18:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/1609) `slopgobblercobbler:fix/kernelx-priority-affinity` → `main` · +347 −8 · 2 files · 1 commits · open ## 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 #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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:59:14 +00:00
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 18:23 UTC · original

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.

**@⁠Anon64** on GitHub · 2026-11-24 18:23 UTC · [original](https://github.com/xcl-dev/xcl/pull/1609#issuecomment-7140580585) 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.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 19:00 UTC · original

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 #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.
**@⁠futurepro62** on GitHub · 2026-11-24 19:00 UTC · [original](https://github.com/xcl-dev/xcl/pull/1609#issuecomment-7141471450) 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 #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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 20:30 UTC · original

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

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

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 20:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/1609#issuecomment-7143646885) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. Thanks!
@ -1439,0 +1475,4 @@
if (allowed >> b) & 1 != 0 {
m | (1 << b)
} else {
m | (1 << pcs[b as usize % pcs.len()])
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 18:21 UTC · original

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.

**@⁠gut0to** on GitHub · 2026-11-24 18:21 UTC · [original](https://github.com/xcl-dev/xcl/pull/1609#discussion_r4684590425) 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`.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1609/head:github-bot/gh-1609'
git switch 'github-bot/gh-1609'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1609'
git switch 'github-bot/gh-1609'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1609'
git switch 'github-bot/gh-1609'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1609'
git switch 'main'
git merge --squash 'github-bot/gh-1609'
git switch 'main'
git merge --ff-only 'github-bot/gh-1609'
git switch 'main'
git merge 'github-bot/gh-1609'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.