fix(libkernel): take the console's 8- to 16-byte sets in pthread_setaffinity_np #15219

Merged
EagleBearerYS merged 2 commits from upstream-pr/15219 into main 2026-11-07 01:37:54 +00:00
EagleBearerYS commented 2026-11-02 21:08:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-11-02T21:08:07Z.


What

pthread_setaffinity_np follows the set sizes measured on a PS5:

  • a set of fewer than 8 bytes, or of 24 bytes and more, returns ERANGE;
  • a set of 8 to 16 bytes sets the thread's affinity from its first 8 bytes.

Before, only FreeBSD's 32-byte cpuset_t was accepted, so the console's 8- and 16-byte sets returned ERANGE, and a 32-byte set succeeded where the console returns ERANGE.

Sources:

  • @rooklz's measurement of the setter on a PS5 (comment on #7135): 8 and 16 bytes succeed, and 0, 1, 4, 32, 64 and 128 return ERANGE.
  • Their fuller sweep of cpuset_setaffinity (comment on #9156): any size from 8 to 16 bytes is accepted, and ERANGE is returned below 8 and from 24 (0, 1, 4, 7, 8, 9, 12, 16, 24, 32, 64 and 128 tried). FreeBSD 11.4's libthr passes the setter's size straight to that call.
  • pthread_getaffinity_np got the same rule in #14039, where @B-Howell pointed out that the setter had been measured too (comment).

Still throws: 17 to 23 bytes, which weren't measured, as in the getter.

Not measured on a PS5: a set longer than 8 bytes with a bit set past the first 8 bytes returns EINVAL, as kern_cpuset_setaffinity does, and as cpuset_setaffinity already does here. The test doesn't assert it.

Debt: this builds on #15179, which gives these sizes their own entry in docs/dev/debt/libkernel.md. It takes pthread_setaffinity_np out of that entry, which now covers cpuset_getaffinity and cpuset_setaffinity only, and rewrites the setter's own entry: the gap first, the sources, and the unmeasured EINVAL.

Overlap: #7135 (AlexOfTheForest) and #14885 (@oneandonlydean) change cpuset_setaffinity's levels and ids in the same file. Neither touches the setter's size check.

  • Thread.cpp merges cleanly with #14885.
  • In docs/dev/debt/libkernel.md, #14885 rewrites the cpuset_setaffinity and cpuset_getaffinity entries just above the setter's entry, which this PR rewrites, so the two touch adjacent lines. Whichever merges second keeps #14885's two entries and this PR's setter entry.
  • #7135 conflicts with main itself in cpuset_getaffinity, not in the setter.

Claim check (open PRs, and PRs closed or merged since 10-19): no other PR changes pthread_setaffinity_np's set sizes. #14039 (merged) did the getter, and #15179 (open, this PR's base) only corrects the debt entry.

Tested

guest_available_cpumask's setter cases now use the measured sizes:

  • 8, 12 and 16 bytes set the affinity of the calling thread and of another thread, and bits outside the available mask are dropped;
  • 0, 1, 4, 7, 24, 32, 64 and 128 bytes return ERANGE and change nothing;
  • the EDEADLK and EFAULT cases now use 8 or 16 bytes instead of 32;
  • the EINVAL and ESRCH thread cases are unchanged, since a thread error comes before the size check.

GitHub's runners (run), guest_available_cpumask:

runner #15179's Thread.cpp with the new test, 3 runs this PR, 10 runs libc and libkernel suites with this PR
Linux 0 / 3 10 / 10 235 and 320 run, 0 failed
Windows 0 / 3: each aborts at the first 8-byte set 10 / 10 262 and 341 run, 0 failed

bf4f7b7239, a merge of main d1ae628bc2. The only conflict was in docs/dev/debt/libkernel.md: #14885 rewrote the cpuset_setaffinity and cpuset_getaffinity entries next to this PR's pthread_setaffinity_np entry, and the merge keeps main's two entries and this PR's. Thread.cpp and the test merge without a conflict, since #14885 changes the cpuset_* functions and this PR the setter, and this PR's diff over main is unchanged. On GitHub's runners (run), guest_available_cpumask passes 10 of 10 on Linux and on Windows, and fails on both with main's implementation. The libc and libkernel suites pass: 297 and 321 run on Linux, 324 and 342 on Windows, 0 failed. The suites ran with APS5_SKIP_TIMING_TESTS=1, which relaxes precise_wait's median bounds on GitHub's hosted runners.

Checks:

  • tools/check_conventions.py: all passed.
  • No comments added.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source: n/a, none
  • Generic behaviour, not specific to one title
  • Depends on: #15179
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5127447137 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15219). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-11-02T21:08:07Z. --- ### What `pthread_setaffinity_np` follows the set sizes measured on a PS5: - a set of fewer than 8 bytes, or of 24 bytes and more, returns `ERANGE`; - a set of 8 to 16 bytes sets the thread's affinity from its first 8 bytes. Before, only FreeBSD's 32-byte `cpuset_t` was accepted, so the console's 8- and 16-byte sets returned `ERANGE`, and a 32-byte set succeeded where the console returns `ERANGE`. **Sources:** - @rooklz's measurement of the setter on a PS5 ([comment on #7135](https://github.com/boykopovar/AnyPS5/pull/7135#issuecomment-6390891591)): 8 and 16 bytes succeed, and 0, 1, 4, 32, 64 and 128 return `ERANGE`. - Their fuller sweep of `cpuset_setaffinity` ([comment on #9156](https://github.com/boykopovar/AnyPS5/pull/9156#issuecomment-6391607043)): any size from 8 to 16 bytes is accepted, and `ERANGE` is returned below 8 and from 24 (0, 1, 4, 7, 8, 9, 12, 16, 24, 32, 64 and 128 tried). [FreeBSD 11.4's libthr](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_affinity.c#L41-L62) passes the setter's size straight to that call. - `pthread_getaffinity_np` got the same rule in #14039, where @B-Howell pointed out that the setter had been measured too ([comment](https://github.com/boykopovar/AnyPS5/pull/14039#issuecomment-6586782053)). **Still throws:** 17 to 23 bytes, which weren't measured, as in the getter. **Not measured on a PS5:** a set longer than 8 bytes with a bit set past the first 8 bytes returns `EINVAL`, as [`kern_cpuset_setaffinity`](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/sys/kern/kern_cpuset.c#L1235-L1246) does, and as `cpuset_setaffinity` already does here. The test doesn't assert it. **Debt:** this builds on #15179, which gives these sizes their own entry in `docs/dev/debt/libkernel.md`. It takes `pthread_setaffinity_np` out of that entry, which now covers `cpuset_getaffinity` and `cpuset_setaffinity` only, and rewrites the setter's own entry: the gap first, the sources, and the unmeasured `EINVAL`. **Overlap:** #7135 (AlexOfTheForest) and #14885 (@oneandonlydean) change `cpuset_setaffinity`'s levels and ids in the same file. Neither touches the setter's size check. - `Thread.cpp` merges cleanly with #14885. - In `docs/dev/debt/libkernel.md`, #14885 rewrites the `cpuset_setaffinity` and `cpuset_getaffinity` entries just above the setter's entry, which this PR rewrites, so the two touch adjacent lines. Whichever merges second keeps #14885's two entries and this PR's setter entry. - #7135 conflicts with `main` itself in `cpuset_getaffinity`, not in the setter. **Claim check** (open PRs, and PRs closed or merged since 10-19): no other PR changes `pthread_setaffinity_np`'s set sizes. #14039 (merged) did the getter, and #15179 (open, this PR's base) only corrects the debt entry. ### Tested `guest_available_cpumask`'s setter cases now use the measured sizes: - 8, 12 and 16 bytes set the affinity of the calling thread and of another thread, and bits outside the available mask are dropped; - 0, 1, 4, 7, 24, 32, 64 and 128 bytes return `ERANGE` and change nothing; - the `EDEADLK` and `EFAULT` cases now use 8 or 16 bytes instead of 32; - the `EINVAL` and `ESRCH` thread cases are unchanged, since a thread error comes before the size check. **GitHub's runners** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/41518462477)), `guest_available_cpumask`: | runner | #15179's `Thread.cpp` with the new test, 3 runs | this PR, 10 runs | libc and libkernel suites with this PR | |---|---|---|---| | Linux | 0 / 3 | **10 / 10** | 235 and 320 run, 0 failed | | Windows | 0 / 3: each aborts at the first 8-byte set | **10 / 10** | 262 and 341 run, 0 failed | **bf4f7b7239, a merge of `main` d1ae628bc2.** The only conflict was in `docs/dev/debt/libkernel.md`: #14885 rewrote the `cpuset_setaffinity` and `cpuset_getaffinity` entries next to this PR's `pthread_setaffinity_np` entry, and the merge keeps `main`'s two entries and this PR's. `Thread.cpp` and the test merge without a conflict, since #14885 changes the `cpuset_*` functions and this PR the setter, and this PR's diff over `main` is unchanged. On GitHub's runners ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/41584291972)), `guest_available_cpumask` passes 10 of 10 on Linux and on Windows, and fails on both with `main`'s implementation. The libc and libkernel suites pass: 297 and 321 run on Linux, 324 and 342 on Windows, 0 failed. The suites ran with `APS5_SKIP_TIMING_TESTS=1`, which relaxes `precise_wait`'s median bounds on GitHub's hosted runners. **Checks:** - `tools/check_conventions.py`: all passed. - No comments added. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source: n/a, none - [x] Generic behaviour, not specific to one title - [x] Depends on: #15179 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-03 23:08:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-03T23:08:12Z.

Review: APPROVED


@EagleBearerYS approved at bf4f7b7239.

What I ran: this head merged onto main (merge 0504cfbc3b; only docs/dev/debt/libkernel.md needed git's auto-merge) on Windows 11 with WinLibs GCC, Release.

  • guest_available_cpumask passes (1 of 1).
  • With main's core/libs/prx/libkernel/Pthread/Posix/Thread.cpp put back and this branch's test kept, the test aborts (1 of 1 failed). So the new size cases check the change.

Read in the diff:

  • The thread checks (EINVAL for a null thread, ESRCH) still come before the size check, as the PR says.
  • The size check now matches rooklz's sweep:
    • fewer than 8 bytes, or 24 and more, returns ERANGE;
    • 17 to 23 bytes still throws (Thread.cpp:362); those sizes weren't in the sweep;
    • 8 to 16 bytes is copied into a 16-byte buffer, and the affinity comes from the first 8 bytes.
  • pthread_getaffinity_np and the setter now share GUEST_AFFINITY_ERANGE_BYTES. GUEST_CPUSET_BYTES is still used by the getter's buffer at Thread.cpp:349.
  • The EDEADLK and EFAULT cases moved to 8- and 16-byte sets. outside's second word is now 0 in GuestAvailableCpumask.cpp; with a nonzero second word its 16-byte call would hit the new EINVAL before EDEADLK.
  • The EINVAL for a bit past the first 8 bytes isn't asserted, and the debt entry says it's unmeasured. That matches the description. If a console run ever settles it, a row with a nonzero byte at offset 8 to 15 and a 16-byte size would cover it.

The debt change reads right: pthread_setaffinity_np leaves the shared size entry, and its own entry now states the gap first (17 to 23 bytes throw, the unmeasured EINVAL).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5760774706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15219#pullrequestreview-5760774706). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-03T23:08:12Z. **Review:** APPROVED --- @EagleBearerYS approved at `bf4f7b7239`. **What I ran:** this head merged onto `main` (merge `0504cfbc3b`; only `docs/dev/debt/libkernel.md` needed git's auto-merge) on Windows 11 with WinLibs GCC, Release. - `guest_available_cpumask` passes (1 of 1). - With `main`'s `core/libs/prx/libkernel/Pthread/Posix/Thread.cpp` put back and this branch's test kept, the test aborts (1 of 1 failed). So the new size cases check the change. **Read in the diff:** - The thread checks (`EINVAL` for a null thread, `ESRCH`) still come before the size check, as the PR says. - The size check now matches rooklz's sweep: - fewer than 8 bytes, or 24 and more, returns `ERANGE`; - 17 to 23 bytes still throws (`Thread.cpp:362`); those sizes weren't in the sweep; - 8 to 16 bytes is copied into a 16-byte buffer, and the affinity comes from the first 8 bytes. - `pthread_getaffinity_np` and the setter now share `GUEST_AFFINITY_ERANGE_BYTES`. `GUEST_CPUSET_BYTES` is still used by the getter's buffer at `Thread.cpp:349`. - The `EDEADLK` and `EFAULT` cases moved to 8- and 16-byte sets. `outside`'s second word is now 0 in `GuestAvailableCpumask.cpp`; with a nonzero second word its 16-byte call would hit the new `EINVAL` before `EDEADLK`. - The `EINVAL` for a bit past the first 8 bytes isn't asserted, and the debt entry says it's unmeasured. That matches the description. If a console run ever settles it, a row with a nonzero byte at offset 8 to 15 and a 16-byte size would cover it. The debt change reads right: `pthread_setaffinity_np` leaves the shared size entry, and its own entry now states the gap first (17 to 23 bytes throw, the unmeasured `EINVAL`).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!15219
No description provided.