*How to Reproduce* The bug was discovered while attempting to use PVS-Studio (linux compat). For testing purposes, I created a primitive script: https://gist.github.com/darkvision77/2ebebced40d7333626f4a9cade1b2178. (PVS-Studio is also available in ports: devel/linux-pvs-studio, but it is currently broken due to this bug.) Two analyzer diagnostics are expected to be output, but an error occurs on the affected kernels. *Affected kernel versions* Anything above 15. To be more precise, after commit 108de784513d: review D39270. *Description* During execution, linux_timer_create (sys/compat/linux/linux_timer.c) is called. LINUX_CLOCK_MONOTONIC is used as clock_id. Further, clock_id is translated into native using linux_to_native_clockid. Before commit 108de784513d, it returned CLOCK_MONOTONIC and everything worked fine. After the commit, linux_to_native_clockid started returning CLOCK_UPTIME. Next, CLOCK_UPTIME is passed to kern_ktimer_create (sys/kern/kern_time.c), which returns EINVAL when checking if (clock_id < 0 || clock_id >= MAX_CLOCKS), since MAX_CLOCKS = 5 and CLOCK_UPTIME is also equal to 5.
> After the commit, linux_to_native_clockid started returning CLOCK_UPTIME. Next, CLOCK_UPTIME is passed to kern_ktimer_create (sys/kern/kern_time.c), which returns EINVAL when checking if (clock_id < 0 || clock_id >= MAX_CLOCKS), since MAX_CLOCKS = 5 and CLOCK_UPTIME is also equal to 5. But #define MAX_CLOCKS (CLOCK_TAI+1) and #define CLOCK_TAI 16 In both current and stable. How can MAX_CLOCKS be 5? Examining the code suggests it should be fine. Can you dig in a little deeper in your environment to see what's going on?
(In reply to Warner Losh from comment #1) > #define MAX_CLOCKS (CLOCK_TAI+1) > #define CLOCK_TAI 16 Indeed. I checked it on 16-CURRENT, but didn't look at its source code. Well, the error remains in CURRENT, now it occurs on the next check in kern_ktimer_create: if (posix_clocks[clock_id].timer_create == NULL). I don't know what register_posix_clock is, but I added register_posix_clock(CLOCK_UPTIME, &rt_clock) to itimer_start() and it worked.
(In reply to Andrey Antufyev from comment #2) So I think that's a good chance. It's certainly necessary, and may well be sufficient as well. I see we're currently handling CLOCK_MONOTONIC and CLOCK_UPTIME the same in many details. While it would be a posix extension, given it's Linux emulation duties, I think it's a fair one to make.
https://reviews.freebsd.org/D54746 should fix the problem. Thanks for deep diving into this... once the problem was identified, the solution was easy to confirm.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=689663d4654fd3a98a78c66ed99f7ed51164c771 commit 689663d4654fd3a98a78c66ed99f7ed51164c771 Author: Warner Losh <imp@FreeBSD.org> AuthorDate: 2026-01-16 21:16:42 +0000 Commit: Warner Losh <imp@FreeBSD.org> CommitDate: 2026-01-16 21:20:20 +0000 Initialize CLOCK_UPTIME for itmer events Since we move to implement Linux's CLOCK_MONOTONIC with CLOCK_UPTIME, we broke the some timer support for Linux. Fix this by initializing CLOCK_UPTIME as a posix clock so we can use in that context. PR: 292496 MFC After: 5 days Fixes: 108de784513d Sponsored by: Netflix Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D54746 sys/kern/kern_time.c | 1 + 1 file changed, 1 insertion(+)
(In reply to Andrey Antufyev from comment #2) Can you make sure there's nothing ELSE causing problems after my commit? I'll get it MFC'd
(In reply to Warner Losh from comment #6) I don't see any problems with linux_timer_create and LINUX_CLOCK_MONOTONIC with this patch. There were no other problems with PVS-Studio emulation, so everything works fine. Thanks.