feat(libkernel): accept EVFILT_PROC with NOTE_EXIT on the calling process #14854

Merged
kallmek3nny merged 3 commits from upstream-pr/14854 into main 2026-11-06 10:03:14 +00:00
kallmek3nny commented 2026-11-01 20:23:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kallmek3nny on 2026-11-01T20:23:41Z.


What

kevent threw NotImplemented_nid_no_patch for every EVFILT_PROC change. It now accepts EVFILT_PROC on the calling process with NOTE_EXIT or no notes, as FreeBSD 11.4 does. In kern_event.c, filt_procattach finds the process with pfind(kn->kn_id), checks p_cansee, adds EV_CLEAR to the knote and activates it at attach only for a process that is already exiting or for a NOTE_TRACK child knote. filt_proc reports a knote only for a hint carrying a subscribed note (NOTE_EXIT when the process exits, NOTE_FORK and NOTE_EXEC when it forks or execs), so a NOTE_EXIT knote on the calling process is never reported while that process runs:

  • EV_ADD stores the knote with EV_CLEAR added and fflags and data 0, as kqueue_register moves kev->fflags and kev->data into kn_sfflags and kn_sdata and clears them in the stored kevent. An add with EV_RECEIPT returns EV_ERROR with data 0 and fflags 0.
  • EV_ADD on the existing knote, EV_DISABLE and EV_ENABLE report nothing: proc_filtops has no f_touch, and filt_proc with hint 0 returns kn_fflags != 0, which is false.
  • EV_FORCEONESHOT reports the knote once, with its stored flags plus EV_ONESHOT, fflags 0, data 0 and the udata of that change, and then it is gone: kqueue_register sets EV_ONESHOT and activates the knote, and kqueue_scan copies a one-shot knote's kevent without calling f_event.
  • EV_DELETE removes the knote; EV_DELETE or EV_ENABLE with no knote fails with ENOENT.
  • Another ident, and any note other than NOTE_EXIT (NOTE_FORK, NOTE_EXEC, NOTE_TRACK and the rest), still throw: this PR does not look up other processes or report forks and execs.

The kevent measurements entry in docs/dev/debt/libkernel.md (payload processes on a PS5, firmware 12.70) recorded that EVFILT_PROC on the process itself with NOTE_EXIT was accepted there and still threw here. This PR accepts it and rewrites that clause (line 172) to say so and that another process and notes other than NOTE_EXIT still throw. The kqueue and kevent entry (line 167) now lists EVFILT_PROC on the process itself with NOTE_EXIT or no notes among the filters that do not throw. Only the acceptance was measured on a PS5; the behaviour after it follows FreeBSD 11.4.

Code: Register in Equeue/src/Kqueue.cpp checks every EVFILT_PROC change with the new DefinedProcessChange and throws when it returns false. A change to an existing knote is defined when it deletes the knote or carries no note other than NOTE_EXIT; a change without EV_ADD and with no knote goes on to the existing ENOENT path; an add is defined when ident is getpid() and the notes are NOTE_EXIT or none. The new AddProcessKnote stores the knote with EV_CLEAR added and fflags and data cleared in both the stored event and the change.

Tests: ProcessExitOnSelf in core/libs/tests/GuestKqueue.cpp: EV_DELETE with no knote (ENOENT); an EV_ADD | EV_RECEIPT add with NOTE_EXIT (receipt with EV_ERROR, data 0 and fflags 0, then no event); re-adds with NOTE_EXIT and with no notes, then EV_DISABLE and EV_ENABLE (no event); EV_FORCEONESHOT (one event with EV_RECEIPT | EV_CLEAR | EV_ONESHOT, fflags 0, data 0 and the new udata, then none, and EV_DELETE then fails with ENOENT); an EV_ADD | EV_ONESHOT add with NOTE_EXIT and its EV_DELETE (both 0), then EV_ENABLE (ENOENT).

Overlap: no open PR implements EVFILT_PROC.

  • gh pr list --repo boykopovar/AnyPS5 --state open --search for EVFILT_PROC, NOTE_EXIT, EVFILT_SIGNAL, EVFILT_FS, filt_proc, filt_signal and kevent proc returned no PRs. On this branch's base, the searches for EVFILT_PROC, NOTE_EXIT and filt_procattach again returned none, and kevent process exit returned #14832, #10645 and #7532: #14832 is my NOTE_LOWAT PR (below), and by title #10645 (pipe write events and socket low watermarks in sceKernelAdd*Event) and #7532 (guest entry stack on Linux) are unrelated.
  • tools/pr_overlap.py reports three PRs. #7492 (wake every waiter on an equeue or kqueue) adds UserEventWakesEveryWaiter(); after UserEvents(); in main in GuestKqueue.cpp (line 1636 on main), where this PR adds ProcessExitOnSelf();; it adds and removes no process-filter code or text. #12741 (macOS edge watches) rewrites debt line 167 and adds or removes no EVFILT_PROC text. My #14832 (NOTE_LOWAT below 1) rewrites debt lines 167 and 172, the same two lines, and keeps the EVFILT_PROC clause as it is on main. git merge-tree on main at b6e3f68173 merges this PR and each of the three cleanly on its own, but this PR then has one content conflict with each: in GuestKqueue.cpp after #7492, and in the debt file after #12741 or #14832. All three were opened first, so the proposed order is all three before this PR (Depends on: #7492, #12741, #14832, proposed to the authors of #7492 and #12741 in a comment here). After each merges, this branch is rebased onto main and keeps both edits to those lines.
  • pr_overlap could not compare 40 PRs that conflict with main. I searched each of their diffs for changed code lines that this PR also changes and for added or removed lines naming EVFILT_PROC, NOTE_EXIT, NOTE_FORK, NOTE_EXEC, NOTE_TRACK, filt_proc or the process itself; for #5021, where gh pr diff failed, I searched its per-file patches from the GitHub API. None match. #7088, #7121 and #9843 share Kqueue.cpp, GuestKqueue.cpp and the debt file; #7088 and #9843 also rewrite the kqueue and kevent entry (line 167) without naming EVFILT_PROC, so the second to merge will need a rebase there.

Tested

Windows 11 Home 10.0.26300.9711, Git Bash, WinLibs gcc/g++, Ninja, Release. No title or homebrew; only the guest kqueue test was run.

  • Full build of this worktree at e450145140, before the branch was cut: git submodule update --init --recursive --jobs 8, cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_TESTING=ON -DPython3_EXECUTABLE=C:/Python314/python.exe, cmake --build build --parallel 4: exit 0.
  • On this branch (base e0caf13600), after the last edit and before the commit: cmake --build build --target guest_kqueue_tests --parallel 4 rebuilt 10 libkernel objects, among them Kqueue.cpp, and GuestKqueue.cpp, and relinked libkernel.prx and guest_kqueue_tests.exe: exit 0. ctest --test-dir build -R '^guest_kqueue$' --no-tests=error --output-on-failure --timeout 120: 1/1 passed (5.48 s). The commit f5e1921254 has the same 3 files, 64 insertions and 3 deletions as the tested working tree.
  • On f5e1921254: python tools/check_conventions.py --base origin/main: exit 0, 0 errors and 2 debt-length warnings for docs/dev/debt/libkernel.md. Line 167 is 3603 characters (3537 on main) and line 172 is 4635 characters (4336 on main); both were already over 1000 characters on main.
  • After 575c14f9b4, which drops the two Throws assertions for NOTE_FORK adds from ProcessExitOnSelf and the unused NoteFork constant (NOTE_FORK still throws in Kqueue.cpp, and no test pins it), in a new worktree of this branch with the submodule and configure commands above: cmake --build build --target guest_kqueue_tests --parallel 4: exit 0. ctest --test-dir build -R '^guest_kqueue' --output-on-failure: 2/2 passed (guest_kqueue, guest_kqueue_cleared_race). The commit has the same 1 file and 3 deletions as the tested working tree. On 575c14f9b4, python tools/check_conventions.py --base origin/main: exit 0, 0 errors and the same 2 debt-length warnings.
  • python tools/pr_overlap.py --branch feat/libkernel-kevent-proc-exit-self: exit 2. It probed 1851 open PRs and fails closed when it cannot compare every one. The manual comparison is under What.

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
  • Generic behaviour, not specific to one title
  • Depends on: #7492, #12741, #14832
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5111212373 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14854). Originally posted by [kallmek3nny](https://github.com/kallmek3nny) on 2026-11-01T20:23:41Z. --- ### What `kevent` threw `NotImplemented_nid_no_patch` for every `EVFILT_PROC` change. It now accepts `EVFILT_PROC` on the calling process with `NOTE_EXIT` or no notes, as FreeBSD 11.4 does. In [`kern_event.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_event.c), `filt_procattach` finds the process with `pfind(kn->kn_id)`, checks `p_cansee`, adds `EV_CLEAR` to the knote and activates it at attach only for a process that is already exiting or for a `NOTE_TRACK` child knote. `filt_proc` reports a knote only for a hint carrying a subscribed note (`NOTE_EXIT` when the process exits, `NOTE_FORK` and `NOTE_EXEC` when it forks or execs), so a `NOTE_EXIT` knote on the calling process is never reported while that process runs: - `EV_ADD` stores the knote with `EV_CLEAR` added and `fflags` and `data` 0, as `kqueue_register` moves `kev->fflags` and `kev->data` into `kn_sfflags` and `kn_sdata` and clears them in the stored kevent. An add with `EV_RECEIPT` returns `EV_ERROR` with `data` 0 and `fflags` 0. - `EV_ADD` on the existing knote, `EV_DISABLE` and `EV_ENABLE` report nothing: `proc_filtops` has no `f_touch`, and `filt_proc` with hint 0 returns `kn_fflags != 0`, which is false. - `EV_FORCEONESHOT` reports the knote once, with its stored flags plus `EV_ONESHOT`, `fflags` 0, `data` 0 and the `udata` of that change, and then it is gone: `kqueue_register` sets `EV_ONESHOT` and activates the knote, and `kqueue_scan` copies a one-shot knote's kevent without calling `f_event`. - `EV_DELETE` removes the knote; `EV_DELETE` or `EV_ENABLE` with no knote fails with `ENOENT`. - Another ident, and any note other than `NOTE_EXIT` (`NOTE_FORK`, `NOTE_EXEC`, `NOTE_TRACK` and the rest), still throw: this PR does not look up other processes or report forks and execs. The kevent measurements entry in `docs/dev/debt/libkernel.md` (payload processes on a PS5, firmware 12.70) recorded that `EVFILT_PROC` on the process itself with `NOTE_EXIT` was accepted there and still threw here. This PR accepts it and rewrites that clause (line 172) to say so and that another process and notes other than `NOTE_EXIT` still throw. The kqueue and kevent entry (line 167) now lists `EVFILT_PROC` on the process itself with `NOTE_EXIT` or no notes among the filters that do not throw. Only the acceptance was measured on a PS5; the behaviour after it follows FreeBSD 11.4. Code: `Register` in `Equeue/src/Kqueue.cpp` checks every `EVFILT_PROC` change with the new `DefinedProcessChange` and throws when it returns false. A change to an existing knote is defined when it deletes the knote or carries no note other than `NOTE_EXIT`; a change without `EV_ADD` and with no knote goes on to the existing `ENOENT` path; an add is defined when `ident` is `getpid()` and the notes are `NOTE_EXIT` or none. The new `AddProcessKnote` stores the knote with `EV_CLEAR` added and `fflags` and `data` cleared in both the stored event and the change. Tests: `ProcessExitOnSelf` in `core/libs/tests/GuestKqueue.cpp`: `EV_DELETE` with no knote (`ENOENT`); an `EV_ADD | EV_RECEIPT` add with `NOTE_EXIT` (receipt with `EV_ERROR`, `data` 0 and `fflags` 0, then no event); re-adds with `NOTE_EXIT` and with no notes, then `EV_DISABLE` and `EV_ENABLE` (no event); `EV_FORCEONESHOT` (one event with `EV_RECEIPT | EV_CLEAR | EV_ONESHOT`, `fflags` 0, `data` 0 and the new `udata`, then none, and `EV_DELETE` then fails with `ENOENT`); an `EV_ADD | EV_ONESHOT` add with `NOTE_EXIT` and its `EV_DELETE` (both 0), then `EV_ENABLE` (`ENOENT`). Overlap: no open PR implements `EVFILT_PROC`. - `gh pr list --repo boykopovar/AnyPS5 --state open --search` for `EVFILT_PROC`, `NOTE_EXIT`, `EVFILT_SIGNAL`, `EVFILT_FS`, `filt_proc`, `filt_signal` and `kevent proc` returned no PRs. On this branch's base, the searches for `EVFILT_PROC`, `NOTE_EXIT` and `filt_procattach` again returned none, and `kevent process exit` returned #14832, #10645 and #7532: #14832 is my NOTE_LOWAT PR (below), and by title #10645 (pipe write events and socket low watermarks in `sceKernelAdd*Event`) and #7532 (guest entry stack on Linux) are unrelated. - `tools/pr_overlap.py` reports three PRs. #7492 (wake every waiter on an equeue or kqueue) adds `UserEventWakesEveryWaiter();` after `UserEvents();` in `main` in `GuestKqueue.cpp` (line 1636 on `main`), where this PR adds `ProcessExitOnSelf();`; it adds and removes no process-filter code or text. #12741 (macOS edge watches) rewrites debt line 167 and adds or removes no `EVFILT_PROC` text. My #14832 (NOTE_LOWAT below 1) rewrites debt lines 167 and 172, the same two lines, and keeps the `EVFILT_PROC` clause as it is on `main`. `git merge-tree` on `main` at `b6e3f68173` merges this PR and each of the three cleanly on its own, but this PR then has one content conflict with each: in `GuestKqueue.cpp` after #7492, and in the debt file after #12741 or #14832. All three were opened first, so the proposed order is all three before this PR (`Depends on: #7492, #12741, #14832`, proposed to the authors of #7492 and #12741 in a comment here). After each merges, this branch is rebased onto `main` and keeps both edits to those lines. - pr_overlap could not compare 40 PRs that conflict with `main`. I searched each of their diffs for changed code lines that this PR also changes and for added or removed lines naming `EVFILT_PROC`, `NOTE_EXIT`, `NOTE_FORK`, `NOTE_EXEC`, `NOTE_TRACK`, `filt_proc` or the process itself; for #5021, where `gh pr diff` failed, I searched its per-file patches from the GitHub API. None match. #7088, #7121 and #9843 share `Kqueue.cpp`, `GuestKqueue.cpp` and the debt file; #7088 and #9843 also rewrite the kqueue and kevent entry (line 167) without naming `EVFILT_PROC`, so the second to merge will need a rebase there. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Home 10.0.26300.9711, Git Bash, WinLibs gcc/g++, Ninja, Release. No title or homebrew; only the guest kqueue test was run. - Full build of this worktree at `e450145140`, before the branch was cut: `git submodule update --init --recursive --jobs 8`, `cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_TESTING=ON -DPython3_EXECUTABLE=C:/Python314/python.exe`, `cmake --build build --parallel 4`: exit 0. - On this branch (base `e0caf13600`), after the last edit and before the commit: `cmake --build build --target guest_kqueue_tests --parallel 4` rebuilt 10 libkernel objects, among them `Kqueue.cpp`, and `GuestKqueue.cpp`, and relinked `libkernel.prx` and `guest_kqueue_tests.exe`: exit 0. `ctest --test-dir build -R '^guest_kqueue$' --no-tests=error --output-on-failure --timeout 120`: 1/1 passed (5.48 s). The commit `f5e1921254` has the same 3 files, 64 insertions and 3 deletions as the tested working tree. - On `f5e1921254`: `python tools/check_conventions.py --base origin/main`: exit 0, 0 errors and 2 `debt-length` warnings for `docs/dev/debt/libkernel.md`. Line 167 is 3603 characters (3537 on `main`) and line 172 is 4635 characters (4336 on `main`); both were already over 1000 characters on `main`. - After `575c14f9b4`, which drops the two `Throws` assertions for `NOTE_FORK` adds from `ProcessExitOnSelf` and the unused `NoteFork` constant (`NOTE_FORK` still throws in `Kqueue.cpp`, and no test pins it), in a new worktree of this branch with the submodule and configure commands above: `cmake --build build --target guest_kqueue_tests --parallel 4`: exit 0. `ctest --test-dir build -R '^guest_kqueue' --output-on-failure`: 2/2 passed (`guest_kqueue`, `guest_kqueue_cleared_race`). The commit has the same 1 file and 3 deletions as the tested working tree. On `575c14f9b4`, `python tools/check_conventions.py --base origin/main`: exit 0, 0 errors and the same 2 `debt-length` warnings. - `python tools/pr_overlap.py --branch feat/libkernel-kevent-proc-exit-self`: exit 2. It probed 1851 open PRs and fails closed when it cannot compare every one. The manual comparison is under What. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> #7492, #12741, #14832 - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!14854
No description provided.