fix(libkernel): support CPU interval timers #25999
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/25999"
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 malekaifa on 2026-11-23T03:50:03Z.
What
Implement FreeBSD-style CPU interval timers in libkernel:
ITIMER_VIRTUALsums user CPU time of registered guest threads (including retired threads) and expires as guestSIGVTALRM(26).ITIMER_PROFsums user and system CPU time of registered guest threads (including retired threads) and expires as guestSIGPROF(27).getitimerand the null-valuesetitimerread path report the active interval and remaining CPU time.sigwait/sigtimedwait/sigwaitinfocan consume them; ignored signals are dropped and the default disposition terminates.The signal numbers and CPU-timer semantics were checked against FreeBSD 11.4 (
SIGVTALRM=26,SIGPROF=27,ITIMER_VIRTUAL=1,ITIMER_PROF=2).Tested
Exact published head
d6de9829202811c1aaa939f034b7cb1cdd8bbc00:guest_itimer_tests;guest_itimer,guest_itimer_pending_default, andguest_itimer_short_expirypassed. CTest selected 5,629 tests: 4,023 passed, 1,606 skipped, zero failed (skip count verified against the job summary).guest_itimerto fail; restoring the change passes. Exact-head approval: PrivacyOnion in the PR review timeline.main: earlier local Windows UCRT64 syntax checks and the subsequent Linux adopted-thread syntax check are recorded in the discussion. I do not claim an independent full local runtime run on the final published head.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositorySummary by CodeRabbit
ITIMER_VIRTUALandITIMER_PROFsupport CPU-time-based one-shot and periodic timers. Virtual timers measure guest user CPU time; profiling timers measure guest user and system CPU time.getitimerreports the configured interval and remaining time, andsetitimerreturns the previous timer state. CPU timer durations below 1 ms are rounded up.Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-23T03:50:11Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T03:51:05Z.
Review: CHANGES_REQUESTED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
05330a07d9:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):Requesting changes for the points above.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T04:10:09Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: prevent CPU timer expiry from dereferencing a thread signal state after that thread exits.
What I ran
Reviewed head
05330a07d93ffadaf0f5e69553f1b83ca257c932using the pinnedrepo_read.pyreader. No build or runtime checks were run. The description reports syntax-only checks on Windows 11 with MSYS2 UCRT64 GCC 16.2.0; those results were not independently verified.Results
Findings
Blocking
core/libs/prx/libkernel/System/src/Signals.cpp:804: The timer stores a pointer to the arming thread's thread-localThreadSignals; later, the detached poller dereferences it at:684and uses it during expiry. If that guest thread exits while its CPU timer remains armed, itsThreadSignalsobject can be destroyed, leavingtimer.armerdangling and causing use-after-free in the poller. Avoid retaining a thread-local pointer beyond the thread lifetime, or clear/transfer the timer when the arming thread exits.Non-blocking
None.
Not verified
review/description.md,review/commits.md,review/manifest.json,review/diff.patch,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/tests/GuestItimer.cpp, and targeted clock implementation ranges incore/libs/prx/libkernel/Time/Time.cpp.To get this to a good state
Ensure the timer cannot access a thread-local signal state after its owner exits, then rerun the relevant build and timer tests.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T04:48:51Z.
Review: CHANGES_REQUESTED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
f3c78de454:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/System/src/Signals.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Requesting changes for the points above.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T04:54:55Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
f3c78de454:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/System/src/Signals.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T04:58:39Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: prevent the timer pollers’ own CPU use from advancing the CPU timers.
What I ran
Reviewed head
f3c78de4542481004102768763ce46272592b0a0. I read the complete changed filescore/libs/prx/libkernel/System/src/Signals.cpp,core/libs/tests/GuestItimer.cpp, anddocs/dev/debt/changes/fix-libkernel-setitimer-cpu-timers.md, plus the process CPU clock implementation and signal pending/wait paths. No commands or tests were run;repo_read.pywas used to inspect the prepared diff and pinned source.Results
Findings
Blocking
core/libs/prx/libkernel/System/src/Signals.cpp:178starts a host thread that repeatedly polls the process CPU clock (core/libs/prx/libkernel/System/src/Signals.cpp:678). That clock is backed by host process CPU accounting (core/libs/prx/libkernel/Time/Time.cpp:596-605). The poller’s own CPU time therefore advances the timer it is checking, so an armed CPU timer can expire while the guest is otherwise idle. Exclude timer-thread CPU use from the accounting or use a mechanism that does not add the poller’s work to the measured process CPU time.Non-blocking
None.
Not verified
Build and runtime behavior were not run. The changed files listed in the manifest were all read; no changed files were excluded.
To get this to a good state
Exclude the timer pollers’ CPU usage from the measured time, then validate that an armed timer does not expire during guest idle time and still expires as guest CPU time advances.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T07:34:44Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
140c4e7a9d:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cpp,core/libs/prx/libkernel/Time/include/Time.hppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T07:51:32Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: make the CPU timer clock and poller accounting use the same CPU scope, then rerun the timer checks.
What I ran
Reviewed head
140c4e7a9d4a39ada8a62c04277fc964662de598; no merged-preview check or programmatic tests were run. Read the prepared diff, manifest, changed-file contents, and targeted clock and signal call sites. No repository files were excluded by the manifest.Results
Findings
Blocking
Signals.cpp:656subtracts the timer pollers’ host thread CPU usage from a guest process CPU clock. On non-Windows hosts,ProcessCpuNanosreads process-wide user/system ticks (Time.cpp:588-595), which include every process thread, while the new helper accounts only the current poller thread (Time.cpp:500-508). Work on other host threads therefore advances the guest CPU clock without being excluded, so the pollers can consume their own CPU timers and expire them while guest work is idle. Use accounting that matches the process-wide clock, or otherwise ensure the guest clock excludes poller CPU consistently.Non-blocking
None.
Sources
Not verified
Signals.cpp,Time.cpp,Time.hpp,GuestItimer.cpp, andfix-libkernel-setitimer-cpu-timers.md. The manifest marks all five as included; no changed files were excluded.To get this to a good state
Match poller CPU exclusion to the CPU scope used by the guest timer clock, then rerun the CPU timer tests and relevant build checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T09:11:51Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this review; the implementation has no verified blocking defect, though runtime behavior and a platform-specific accounting path remain unverified.
What I ran
Reviewed head
b049dd1775063dcb123fa0e4ff9700cd3fb30cc8. I read the complete prepared diff and all six changed files listed as reviewed; I did not run build or runtime checks. The reader tool was used to inspect targeted source ranges and callers.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No build, test, or runtime checks were run. The author reports syntax-only checks for
GuestItimer.cppandSignals.cpp, but those results were not independently verified. Console behavior and Windows runtime behavior were not verified. Files read:core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cpp,core/libs/tests/GuestItimer.cpp, anddocs/dev/debt/changes/fix-libkernel-setitimer-cpu-timers.md. Files excluded: none; all six changed files in the manifest were included and read.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T09:30:02Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: prevent CPU-timer pollers from dereferencing an armer thread after it exits.
What I ran
Reviewed head
d6de9829202811c1aaa939f034b7cb1cdd8bbc00. I read all six changed files listed as reviewed in the manifest, plus the prepared description, commits, and diff.repo_read.pyprovided the pinned source. I did not run a build or tests.Results
Findings
Blocking
core/libs/prx/libkernel/System/src/Signals.cpp:812stores a raw pointer to the arming thread’sThreadSignalsin timer state. When the timer expires,RunCpuTimerdereferences it at line 349 and passes it toExpireCpuTimer, which checks the blocked mask at line 317.ThreadSignalsis thread-local and its lifetime ends with that host thread, while the CPU timer may outlive the arming thread. A guest thread that arms a timer and exits before expiry can therefore leave the poller with a dangling pointer and trigger a use-after-free. Keep the signal state alive for the timer’s lifetime, or safely resolve a live target before expiry.Non-blocking
None.
Not verified
No build or test was run. The description reports Windows syntax checks by the author; these were not run during this review. Files read:
core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cpp,core/libs/tests/GuestItimer.cpp, anddocs/dev/debt/changes/fix-libkernel-setitimer-cpu-timers.md. The manifest lists all six as included and reviewed; no changed files were excluded.To get this to a good state
Make the timer’s signal-state reference safe across arming-thread exit, then rerun the relevant build and CPU-timer tests on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T09:44:00Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
d6de982920:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-27T17:07:18Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
1fada1ac75:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T17:11:26Z.
Review: COMMENTED
Actionable comments posted: 12
600595d0-a9c9-4b10-aede-6b4bf886c994Reviewing files that changed from the base of the PR and between
1bc92752d2and1fada1ac75.core/libs/prx/libkernel/Pthread/include/Pthread.hppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/prx/libkernel/System/src/Signals.cppcore/libs/prx/libkernel/Time/Time.cppcore/libs/tests/GuestItimer.cppdocs/dev/debt/changes/fix-libkernel-setitimer-cpu-timers.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T17:12:43Z.
Review: COMMENTED
Reviewed — no blocking findings
The change at head
1fada1ac75c9bd3004612366aa2719cb8fbe10bfimplements the described CPU interval timer behavior; I found no verified blocking defects, but did not run checks on this head.What I ran
Read the prepared description, commit list, diff, manifest, and all added lines in the six changed files. Used the repository reader to inspect the timer implementation, CPU time accounting, signal handling, and relevant tests. No commands or tests were run.
Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Runtime behavior on head
1fada1ac75c9bd3004612366aa2719cb8fbe10bfwas not tested. The description reports CI and independent test runs atd6de9829202811c1aaa939f034b7cb1cdd8bbc00, an earlier head. No console test was run. Files read:core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cpp,core/libs/tests/GuestItimer.cpp, anddocs/dev/debt/changes/fix-libkernel-setitimer-cpu-timers.md. No changed files were excluded.To get this to a good state
Nothing is required. Optional: run the timer tests on this exact head before relying on runtime behavior.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-30T06:37:05Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
e3a2b11ab1:tools/check_conventions.pypassed.guest_itimer_testsbuilt cleanly.guest_itimer,guest_itimer_pending_default,guest_itimer_short_expiry):core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/System/src/Signals.cpp,core/libs/prx/libkernel/Time/Time.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T06:47:50Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: correct CPU timer arming so a timer started by an adopted guest thread cannot retain a pointer to thread-local signal state after that thread exits.
What I ran
Reviewed head
e3a2b11ab18ca0a330e94c5fe90276c0d856d6f7. Read the prepared description, commits, diff, manifest, review README, and all changed lines in the six changed files; inspected related CPU-clock and timer functions. No build or tests were run.Results
Findings
Blocking
core/libs/prx/libkernel/System/src/Signals.cpp:888: The timer stores&CurrentSignals()asarmer, then the detached poller dereferences it on expiration at lines 349 and 317. On Linux, adopted guest threads are backed by athread_local std::unique_ptr<PthreadPrivate>; when such a thread exits, its thread-local state is destroyed. A CPU timer armed by that thread can remain active, leavingarmerdangling and causing use-after-free while checking the signal mask or marking the timer signal pending. Preserve the signal state for the timer lifetime or otherwise make expiration safe after the arming thread exits.Non-blocking
None.
Not verified
No build, automated tests, physical-console tests, or GPU tests were run. The author-reported Windows and Ubuntu tests refer to a different head (
d6de9829202811c1aaa939f034b7cb1cdd8bbc00) and were not independently verified. All six changed files listed in the manifest were read; no files were excluded by the manifest.To get this to a good state
Fix the armer lifetime issue and add coverage for a timer armed by an adopted guest thread that exits before expiration. Re-run the relevant timer tests on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T10:42:35Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
366ac432778e3397a850a2245473179f8221ee68. I read the prepared description, commit list, manifest, and diff, then inspected the timer implementation and CPU accounting code at the pinned head. I did not run tests or the reader command; no repository write or external review action was performed.Results
Findings
Blocking
None.
Non-blocking
setitimerstores a raw pointer to the arming thread'sThreadSignalsinCpuTimerState::armer(core/libs/prx/libkernel/System/src/Signals.cpp:888). If that thread exits while the timer remains armed, expiry can dereference a stale pointer inExpireCpuTimer(core/libs/prx/libkernel/System/src/Signals.cpp:317). Consider associating pending delivery with process-lifetime state or clearing/reassigning the armer when its thread retires.Not verified
Pthread.hpp,Thread.cpp,Signals.cpp,Time.cpp,GuestItimer.cpp,fix-libkernel-setitimer-cpu-timers.md, plus the prepared review description, commits, manifest, and diff. The manifest marked all six changed files as included; no files were listed as excluded in the available bundle.To get this to a good state
Nothing is required. The armer lifetime point above is optional feedback.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T10:49:43Z.
Review: COMMENTED
Actionable comments posted: 1
Architecture findings (2)
Advisory architecture findings (2)
f4af7558-6640-404e-9de5-05e4e3d83e2fReviewing files that changed from the base of the PR and between
e3a2b11ab1and366ac43277.core/libs/prx/libkernel/Pthread/include/Pthread.hppcore/libs/prx/libkernel/Pthread/src/Thread.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T10:38:27Z.
Review: COMMENTED
The GuestItimer fixture coverage passed on Linux with the exact implementation from this head. The fixture exercises CPU interval timers alongside the existing timer cases.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.