fix(libkernel): follow kern_cpuset for the base cpuset, level checks and interrupt targets #7135
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7135"
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?
Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-17T13:07:54Z.
What
cpuset_getaffinityandcpuset_setaffinityhandled only the calling thread and process, threw for any other live thread (althoughpthread_setaffinity_npalready handles one), and differed from FreeBSD 11.4'skern_cpuset_getaffinity/kern_cpuset_setaffinityin several error paths. Four commits:fix(libkernel): reject an unknown level in cpuset_setaffinity before resolving the target: the setter switches on the level first and only resolves the id inside each level case, so an unknown level isEINVALwhatever the id names. It used to answerESRCHfor an unknown id, and the existing test asserted that; the test is corrected. The getter really does resolve first and is unchanged.feat(libkernel): read and set another guest thread's affinity through cpuset:LiveGuestThreadAffinityandSetLiveGuestThreadAffinitylook a live thread up by id under the registry lock.CPU_WHICH_TIDfor another thread now reports or records that thread's affinity, withEDEADLKfor a mask outside the available CPUs andESRCHfor an unknown id or a thread that finishes in between.feat(libkernel): answer cpuset_getaffinity for the base cpuset:CPU_LEVEL_CPUSETreturns the thread's numbered base set (cpuset_refbase), and so doesCPU_WHICH_CPUSETwith id -1. Neither moves when a per-thread mask is set. Both reportsceKernelGetAvailableCpumask, on the assumption that a title's base set is the CPUs the console makes available to it; that assumption is unverified and recorded in the debt entry. Interrupt and memory-domain targets at the ROOT and CPUSET levels now fail withEINVALas in the source.fix(libkernel): follow kern_cpuset_setaffinity for interrupt and domain targets: the setter gets the source's level-then-scope structure.EINVALfor an interrupt or domain at ROOT/CPUSET and for a domain atCPU_LEVEL_WHICH(that switch has no domain case). The interrupt-handler and ithread scopes atCPU_LEVEL_WHICHgo tointr_setaffinityin the source, so they now throw instead of answeringEINVAL.Still throwing, with a message naming what was asked:
CPU_LEVEL_ROOT(the root set on a console is unknown); changes to the base or root set (cpuset_modifyneedsPRIV_SCHED_CPUSET, and whether a title holds it is unknown); numbered cpusets; jails; interrupt and domain masks atCPU_LEVEL_WHICH. Both debt entries are updated.Tested
Linux x86_64:
guest_available_cpumaskandguest_sched_prioritypassed 5 of 5 runs. The extendedguest_available_cpumaskcovers: another thread's affinity read and set (clipped bits,EDEADLK, high bits, the caller left unchanged); the base set at both scopes while the caller's own mask is pinned to one CPU;ESRCHfor unknown ids; the newEINVALcases; and every remaining throw.guest_tcpandguest_named_shm_concurrencyfail identically on unmodifiedmainin this container.tools/check_conventions.py --base upstream/mainpasses; the progress report is unchanged.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by UmillD on 2026-10-17T14:26:25Z.
Independent Windows validation of this PR.
BUILD_TESTING=ON, bundled dependencies. CPU/library tests; no GPU or title run.92812410c8c0ff21cecdc80a2d535bf8822eaeacmerged cleanly into pinnedmain47a266cf975f00f7fba94013467d03bfa5e355e9. Integrated tree940eb657eb2fcbdf82c468446c46f3d8b45b4cc6; no other open PRs or local game patches added.GuestAvailableCpumask.cppfixture and uses exactly the pinned main's production sources. The candidate restores only the three integrated Pthread production files. Both selected-target builds succeeded. Thelibc.prxSHA-256 is identical across phases;libkernel.prxdiffers as expected.guest_available_cpumask0xc0000409guest_sched_priorityThe same extended affinity fixture therefore detects a production difference on this Windows host. Its candidate run covers reading/setting another live guest thread's affinity, clipping unavailable bits, keeping the caller unchanged,
EDEADLK/high-bit error cases, base-set reads while the caller is pinned, unknown/finished thread IDs, and the changed invalid-level/scope cases. The control abort alone does not localize which assertion failed.Neighboring
guest_thread_self,guest_pthread_attr,guest_pthread_attr_errorsandguest_pthread_startpass once on each phase (4/4 control, 4/4 candidate). The unchanged start fixture includes its 10,000 thread starts and 100 detach checks. Candidate total: 14/14 test executions, zero skipped. No timeouts or memory-guard terminations. CTest was serial; focused cases used--repeat until-fail:5, with existing individual timeout properties preserved.Scope: focused Windows API regression testing, not the full suite, a host-scheduler/performance measurement or console validation. In particular, these passes do not establish the unmeasured console assumption about the base cpuset recorded in this PR's debt entry. No local production or fixture changes were needed.
AI-assisted testing and report: yes (Codex).
Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-18T10:13:54Z.
Heads-up before rebase effort goes in:
mainalready implements this PR's core feature.main'scpuset_getaffinity/cpuset_setaffinityresolve any thread id throughGetLiveGuestThreadAffinity_nid_no_patch(tid, ...)/SetLiveGuestThreadAffinity_nid_no_patch(tid, ...)(thetid = id == -1 ? self->tid : idpath covers other threads directly), andmain's test suite exercises remote threads with its ownRemoteThreadfixture. Your branch predates that landing, so its API surface (LiveGuestThreadAffinitywith a pointer out-param, theotherself/other split) is a parallel shape of the same feature. What genuinely differs is behaviour your tests pin thatmain's do not: the EINVAL-vs-ESRCH split for bad ids on somewhichvalues, the pinned-self process-affinity coverage, and thewhich4-8 error paths — those could be worth carrying as deltas ontomain's implementation, or this PR may close as superseded ifmain's behaviour is what the console does. Your call with the maintainers; happy to carry the reduction if wanted.Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-18T12:03:13Z.
@blawesom thanks, you were right: #6540 already landed the guest-TID lookup. I rebuilt this PR on top of main's GetLiveGuestThreadAffinity_nid_no_patch / SetLiveGuestThreadAffinity_nid_no_patch and dropped my overlapping commit. What remains is the delta:
guest_available_cpumask and guest_sched_priority pass 5/5 on Linux.
Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-22T09:03:20Z.
@blawesom thanks, you were right: #6540 already landed the guest-TID lookup. I rebuilt this PR on top of main's GetLiveGuestThreadAffinity_nid_no_patch / SetLiveGuestThreadAffinity_nid_no_patch and dropped my overlapping commit. What remains is the delta:
Rebased on current main; guest_available_cpumask and guest_sched_priority pass on Linux.
Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T09:55:13Z.
Review: APPROVED
Reviewed head
eb4e07e8a9merged onto main16025845e5(clean merge). Linux x86_64, GCC 13.3, Release.What I ran:
guest_available_cpumaskandguest_sched_prioritywith the PR: pass 5/5.Thread.cppwith this PR's test:guest_available_cpumaskaborts,guest_sched_prioritypasses. So the test catches the change on Linux too.tools/check_conventions.py --base 16025845e5: pass.Source walk against releng/11.4
kern_cpuset.c(d106002967):cpuset_whichruns before the level switch (L1113 vs L1116), so a bad id still wins over a bad level, same as here. 7 and 8 areEINVALat every level becausecpuset_whichhas no case for them (L468). IRQ and domain at ROOT/CPUSET areEINVAL(L1129-1133).cpuset_whichonly runs inside the ROOT/CPUSET case (L1249-1252), so(99, TID, 999999)moving fromESRCHtoEINVALis right. IRQ/7/8/domain at ROOT/CPUSET areEINVAL(L1266-1270). AtCPU_LEVEL_WHICH, IRQ/7/8 go tointr_setaffinity(L1297-1300, throws here) and domain falls todefault(L1302,EINVAL).Not checked: whether the base set really is
sceKernelGetAvailableCpumaskon a console. I have no console; the debt entry already says it's unverified.Two small optional things:
cpuset_setaffinitydebt line puts "bits outsidesceKernelGetAvailableCpumaskare dropped, withEDEADLKwhen none remain" under "Otherwise it followskern_cpuset_setaffinity". 11.4 doesn't drop bits:cpuset_shadowreturnsEDEADLKas soon as the mask isn't a subset of the parent set (CPU_SUBSET, L497-498; same check incpuset_setproc, L568-569). Dropping is main's existing behaviour and fine to keep, it just reads as if FreeBSD does it. Maybe its own sentence, e.g. "Unlike FreeBSD, which returnsEDEADLKfor any bit outside the parent set, bits outsidesceKernelGetAvailableCpumaskare dropped, withEDEADLKwhen none remain".Thread.cppL155: thereturn fail(GuestEinval);afterNotImplemented_nid_no_patch(...)can't run, since that function is[[noreturn]](core/libs/prx/libc/include/General.hpp:15). Could just go.Pull request closed