Repository navigation
fix(libkernel): answer sched_rr_get_interval with 4 ms and accept pid 1 as the console does - #22219
Conversation
… pid and pid 1 as measured on the console Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… 1 as the console does Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source (built with ps5-payload-sdk v0.45; probe.c/* sched_rr_get_interval and adjtime (read only), payload process, firmware 11.60 (debt libkernel_sys.md
sched_rr_get_interval entry: AnyPS5 accepts pid 0 or the guest pid and reports the FreeBSD 11 ULE default time slice
(94 ms), other pids return ESRCH and a null interval EFAULT; the console's values are not measured; adjtime entry: under
the model agreed on #11216 every readable non-null delta returns EPERM without writing the old delta, bad input returns
EFAULT first and a null delta reports zero; console privileges and clock slewing are unverified). The probe looks both up
with dlsym in libkernel.sprx and libc.sprx. sched_rr_get_interval(0, &ts) and (getpid(), &ts) on a
0xaa-filled timespec (rc, seconds, nanoseconds), then pid 1, 0x7fffffff and -1 (rc, errno, whether the timespec changed),
and a NULL timespec once at the end of that group. adjtime is called ONLY with a NULL delta and an old-delta buffer
filled with 0xaa (rc, errno, the old delta); it is never called with a delta that is not NULL, because that would set a
clock adjustment, and then once with both pointers NULL as the last call of the probe. Nothing is written, nothing is
sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h> /* only for kernel_get_fw_version() */
#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif
static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}
static void probe_alarm(int sig) {
static const char msg[] = "TIMEOUT\tprobe self limit\n";
(void)sig;
(void)!write(1, msg, sizeof msg - 1);
reset_rate();
_exit(124);
}
/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
as a second safeguard. */
static void* probe_watchdog(void* arg) {
static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
unsigned left = PROBE_MAX_S;
(void)arg;
while (left) left = sleep(left);
(void)!write(1, msg, sizeof msg - 1);
reset_rate();
_exit(124);
return 0;
}
static void probe_watchdog_start(void) {
pthread_t t;
if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}
static int cases = 0;
static void probe_header(void) {
char utc[32];
time_t now = time(0);
probe_watchdog_start();
struct tm tmv;
gmtime_r(&now, &tmv);
strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
getpid(), utc);
fflush(stdout);
}
static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
fflush(stdout);
cases++;
}
static void probe_end(void) {
printf("END\t%d\n", cases);
fflush(stdout);
}
#include <time.h>
#include <sys/time.h>
typedef int (*fn_rr)(int, struct timespec*);
typedef int (*fn_adj)(const struct timeval*, struct timeval*);
static void fault(int sig) {
static const char msg[] = "FAULT\tsignal in the call\n";
(void)sig;
(void)!write(1, msg, sizeof msg - 1);
_exit(125);
}
static void rr_case(const char* id, const char* label, fn_rr f, int pid) {
struct timespec ts;
memset(&ts, 0xaa, sizeof ts);
char extra[120];
errno = 0;
int r = f(pid, &ts);
int e = errno;
unsigned char* p = (unsigned char*)&ts;
int changed = 0;
for (size_t i = 0; i < sizeof ts; ++i) if (p[i] != 0xaa) changed++;
snprintf(extra, sizeof extra, "rc=%d errno=%d changed=%d sec=%lld nsec=%lld", r, e, changed, (long long)ts.tv_sec, (long long)ts.tv_nsec);
probe_case(id, label, r, e, extra);
}
int main(void) {
signal(SIGALRM, probe_alarm);
signal(SIGSEGV, fault);
signal(SIGBUS, fault);
alarm(PROBE_MAX_S);
probe_header();
char extra[160];
errno = 0;
void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
errno = 0;
void* ls = dlopen("libc.sprx", RTLD_LAZY);
probe_case("0a", "load libc.sprx", ls ? 0 : -1, errno, ls ? "loaded" : dlerror());
fn_rr rr = 0;
fn_adj adj = 0;
const char* wr = "none";
const char* wa = "none";
if (lk) { rr = (fn_rr)dlsym(lk, "sched_rr_get_interval"); if (rr) wr = "libkernel"; adj = (fn_adj)dlsym(lk, "adjtime"); if (adj) wa = "libkernel"; }
if (!rr && ls) { rr = (fn_rr)dlsym(ls, "sched_rr_get_interval"); if (rr) wr = "libc"; }
if (!adj && ls) { adj = (fn_adj)dlsym(ls, "adjtime"); if (adj) wa = "libc"; }
snprintf(extra, sizeof extra, "sched_rr_get_interval_in=%s adjtime_in=%s", wr, wa);
probe_case("0b", "dlsym of the functions", 0, 0, extra);
if (rr) {
rr_case("1.1", "sched_rr_get_interval(0, &ts)", rr, 0);
rr_case("1.2", "sched_rr_get_interval(getpid(), &ts)", rr, (int)getpid());
rr_case("1.3", "sched_rr_get_interval(1, &ts)", rr, 1);
rr_case("1.4", "sched_rr_get_interval(0x7fffffff, &ts)", rr, 0x7fffffff);
rr_case("1.5", "sched_rr_get_interval(-1, &ts)", rr, -1);
errno = 0;
int r = rr(0, 0);
snprintf(extra, sizeof extra, "rc=%d errno=%d", r, errno);
probe_case("1.6", "sched_rr_get_interval(0, NULL)", r, errno, extra);
}
if (adj) {
struct timeval old;
memset(&old, 0xaa, sizeof old);
errno = 0;
int r = adj(0, &old);
snprintf(extra, sizeof extra, "rc=%d errno=%d old=%lld.%lld", r, errno, (long long)old.tv_sec, (long long)old.tv_usec);
probe_case("2", "adjtime(NULL, &old), delta NULL only", r, errno, extra);
errno = 0;
r = adj(0, 0);
snprintf(extra, sizeof extra, "rc=%d errno=%d", r, errno);
probe_case("3", "adjtime(NULL, NULL)", r, errno, extra);
}
probe_end();
return 0;
}stdoutAI-assisted (Claude Code). |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 53e3c00988:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_sched_priority_testsbuilt cleanly. - CTest (
guest_sched_priority):1/1 Test #5432: guest_sched_priority ............. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel_sys/Export.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #5432: guest_sched_priority .............Subprocess aborted***Exception: 0.12 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified sched_rr_get_interval_nid_postfix accepts pid 0, pid 1, and the current guest pid (self = getpid_nid_postfix()), returning quantum {0, 4000000} (4 ms) matching PS5 firmware 11.60 console probe measurements.
- Verified error paths: invalid pids return -1 with errno ESRCH (3), and null or unwritable interval pointers return -1 with errno EFAULT (14) via GuestCopyOut_nid_no_patch.
- Verified struct KernelTimespec layout and 4 ms values (tv_sec = 0, tv_nsec = 4000000) are cross-platform safe across LP64 and LLP64 targets.
- Verified AGENTS.md technical debt documentation conventions: fragment added at docs/dev/debt/changes/fix-kernel-sys-sched-rr-interval-4ms.md with proper markdown header and explanation.
- Verified GuestSchedPriority.cpp unit test updates check pid 0, self, pid 1, invalid pids (-1), and preserve errno verification.
Looks Good To Me
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the reported console measurement is reflected in the implementation, tests, and debt entry.
What I ran
Reviewed head 53e3c009889c40a7f5963389eea6b6148c5a2eb2. I read description.md, commits.md, manifest.json, diff.patch, and the full text of all three changed files via repo_read.py. I did not run commands or tests.
Results
| Check | Result | Note |
|---|---|---|
| Changed code review | passed | Implementation, tests, and debt entry agree |
| Tests | not run | Test and build results were supplied by the author |
Findings
Blocking
None.
Non-blocking
None.
Not verified
I did not independently verify the console measurement or run builds and tests. Firmware, title process behavior, and other existing process IDs were not independently checked. files.json was unavailable in the review bundle; manifest.json lists three changed files, and I read all three.
To get this to a good state
Nothing is required.
What
sched_rr_get_intervalinlibkernel_sys/Export.cppreturned the FreeBSD 11 ULE default time slice of 94 ms and failed withESRCHfor every pid except 0 and the process's own. A PS5 payload process on firmware 11.60 called the console'ssched_rr_get_intervalthrough thelibkernelexport and got 4 ms (sec0,nsec4000000, 16 bytes written) for pid 0, for its own pid and for pid 1 (cases 1.1 to 1.3, lines 5 to 7 of the raw output). The pids0x7fffffffand -1 returned -1 witherrno3 (ESRCH) and left the interval untouched (cases 1.4 and 1.5, lines 8 and 9), and a null interval returned -1 witherrno14 (EFAULT; case 1.6, line 10). The function now returns{0, 4000000}for pid 0, pid 1 and the own pid and keepsESRCHfor the others; the null interval still givesEFAULT. The entry indocs/dev/debt/libkernel_sys.mdsaid that the console-specific clock rates and quantum configuration were unverified.The 4 ms is the value the console reported to this process; this library has no guest scheduler and no per-process quantum, so the number is the console's reported value and not runtime scheduling, as the entry says. The test commit changes the three expectations of 94 ms in
GuestSchedPriority.cppto 4 ms and adds a check for pid 1; theESRCHchecks (for -1 and for the own pid plus 1) and theEFAULTchecks are unchanged. The debt fragment states the console result for pid 0, the own pid and pid 1 and says that pids which exist on the console are not measured.Not measured on the console: other existing pids (this library returns
ESRCHfor them, and also for the own pid plus 1), the value after a scheduler setting, a title process, and what pid 1 is there. No title is shown to call the function.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loadslibkernel.sprxand takessched_rr_get_intervalwithdlsym, on 2026-11-17 at 06:35 UTC. A title process may differ. The cases are 0 to 1.6, lines 2 to 10 of the raw output in the first comment (the probe also readadjtime, which is another pull request's subject).mainbefore (test commit only)guest_sched_priorityfails (94 ms,ESRCHfor pid 1)0x7fffffff, -1ESRCH, interval untouched (lines 8, 9)0x7fffffffEFAULT(line 10)On
main, with the test commit alone, one test,guest_sched_priority, fails; it contains all the checks above. Windows 10 Pro 10.0.19045 and Linux (WSL), one build per commit, on baseupstream/mainbdc13ebcde2, on 2026-11-17 between 07:24 and 07:34 UTC. Windows: the test commit alone built withninjareturning 0 andctestreturned 8; the head of this pull request built withninjareturning 0 andctestreturned 0. Linux: on commits whose trees equal the three commits of this pull request,ninjareturned 0 for all three, the test commit alone abortedguest_sched_priority(1 of 1 tests failed), and the fix commit and the documentation commit passed it in three runs each;tools/check_conventions.pypassed for each commit.tools/check_conventions.py --base upstream/mainpasses with no error and no warning (the changed entry is 647 characters, up from 519, counted without the leading '- '). The change is 3 files, +12/-5, with no new comments in code. Not tested: macOS, the CI runner, a title process, other firmware, and the cases listed above as not measured.Overlap: the other open pull requests that change one of the files of this branch (
core/libs/prx/libkernel_sys/Export.cpp,core/libs/tests/GuestSchedPriority.cpp) or the debt fragment, found by reading the changed files of all 1644 open pull requests at about 08:07 UTC, by reading what they change indocs/dev/debt/libkernel_sys.mdand in the fragments underdocs/dev/debt/changes/forsched_rr_get_interval, and by searching their titles and branch names for the same name (one hit, #17518 below), were test-merged on 2026-11-17 at about 09:02 UTC againstmaincf5e16b6c9etogether with this head; the branch is based onbdc13ebcde2, an ancestor of it, merges intomainwithout a conflict (also againstmain0e925c3970dat about 09:05 UTC), andtools/debt_fold.py --checkapplies its fragment without an error (checked without the fragmentlibressl3-20171-inventory.md, which already conflicts withfeat-libressl3-evp-pkey-new-mac-key-v2.mdonmainand is not part of this change). Two open pull requests matter. #17518 (test(libkernel): audit sched_rr_get_interval timespec and pid boundary validation) addsGuestKernelSchedRrInterval.cpp, which requires 94 ms for pid 0 and the own pid; it does not conflict textually, but its test contradicts the console value measured here and would fail after this change; its reviewer noted on 2026-11-11 thatGuestSchedPriority.cpponmainalready asserts the same cases, which this change updates to 4 ms. #18190 (feat(libs): mirror libkernel exports through libkernel_sys) merges cleanly withmainand conflicts only with this change, inExport.cpparoundsched_rr_get_interval; whichever lands second needs a rebase. Of the other nine pull requests that list one of the files, #12315 and #9389 already conflict withmainand conflict in the same files with this head merged first, and #21984, #17908, #17897, #17885, #15037 and #6918 merge cleanly withmainand with this head. No open pull request changes thesched_rr_get_intervaldebt entry. The merged result was not built or run.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)