NetBSD Problem Report #61040
From www@netbsd.org Sat Nov 21 22:51:53 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
client-signature RSA-PSS (2048 bits) client-digest SHA256)
(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id 0590E1A923B
for <gnats-bugs@gnats.NetBSD.org>; Sat, 21 Nov 2026 22:51:53 +0000 (UTC)
Message-Id: <20261121225150.B279E1A923D@mollari.NetBSD.org>
Date: Sat, 21 Nov 2026 22:51:50 +0000 (UTC)
From: osbre@protonmail.com
Reply-To: osbre@protonmail.com
To: gnats-bugs@NetBSD.org
Subject: itimer(9): one-shot CPU-time timers keep firing after they expire
X-Send-Pr-Version: www-1.0
X-From4GNATS: "osbre@protonmail.com via gnats" <gnats-admin@NetBSD.org>
>Number: 61040
>Category: kern
>Synopsis: itimer(9): one-shot CPU-time timers keep firing after they expire
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: kern-bug-people
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sat Nov 21 22:55:00 +0000 2026
>Originator: Ostap Brehin
>Release: NetBSD 11.1 and -current
>Organization:
>Environment:
$ uname -a
NetBSD netbsd-vm 11.1 NetBSD 11.1 (GENERIC64) #0: Mon Nov 2 16:34:32 UTC 2026 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/GENERIC64 evbarm
>Description:
If a one-shot ITIMER_PROF expires, the process gets SIGPROF on every
clock tick after that, but getitimer() says the timer is off.
ITIMER_VIRTUAL does the same. ITIMER_REAL is fine.
I hit this with FrankenPHP (a PHP app server). PHP's set_time_limit()
arms a one-shot ITIMER_PROF with an SA_RESETHAND handler. The second
SIGPROF, one tick later, kills the whole server.
The CPU-time timers (ITIMER_PROF, ITIMER_VIRTUAL, and timer_create()
with CLOCK_PROF or CLOCK_VIRTUAL) are kept on a list sorted by expiry.
Each timer stores only its time after the one in front of it. In
sys/kern/kern_time.c this goes wrong in three places:
1. itimer_decr() leaves an expired one-shot timer at the head of the
list, where it expires again on every tick.
https://nxr.netbsd.org/xref/src/sys/kern/kern_time.c?r=1.233#815
2. itimer_settime() takes the timer off the list using its value, but
dotimer_settime() and dosetitimer() have already overwritten that
value with the new one. The next timer gets the new value added
instead of the old one.
https://nxr.netbsd.org/xref/src/sys/kern/kern_time.c?r=1.233#1425
https://nxr.netbsd.org/xref/src/sys/kern/kern_time.c?r=1.233#1719
3. When a timer is taken off the list or put on it, every timer behind
it is adjusted, not just the next one.
https://nxr.netbsd.org/xref/src/sys/kern/kern_time.c?r=1.233#947
https://nxr.netbsd.org/xref/src/sys/kern/kern_time.c?r=1.233#967
With more than one such timer, the times go wrong. A timer behind
an expired or periodic one never fires, and changing one timer changes
the others: with timers set to 900, 600 and 300 ms, timer_gettime()
says the 900 ms one has 600 ms left.
This code hasn't changed since netbsd-2.
>How-To-Repeat:
This should print 1, but prints about 150:
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
static volatile sig_atomic_t n;
static void h(int s) { n++; }
int
main(void)
{
struct itimerval it = { .it_value = {0, 500000} };
struct timespec t0, t;
signal(SIGPROF, h);
setitimer(ITIMER_PROF, &it, NULL);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
do
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
while (t.tv_sec - t0.tv_sec < 2);
printf("%d\n", (int)n);
return 0;
}
The new ATF test cases in the patch show the other problems.
>Fix:
1. Add itimer_vlist_remove(), which takes a timer off its list and
gives its time to the next timer.
2. Call it in itimer_decr(), dosetitimer(), dotimer_settime() and
sys_timer_delete() before they change or free the timer.
3. Make itimer_settime() only put timers on the list, and adjust only
the next one.
Tested on 11.1 evbarm-aarch64, with and without options DIAGNOSTIC.
The program above prints 1. The seven new test cases fail without the
patch and pass with it, and the rest of t_getitimer and t_timer_create
still pass. A 20 s stress test (4 threads burning CPU while timers are
set, re-armed and deleted at random) found no wrong times.
The patch also applies to netbsd-11, but not to netbsd-10 as is.
--- sys/kern/kern_time.c 1.233
+++ sys/kern/kern_time.c working copy
@@ -774,6 +774,23 @@
/* Destroy the callout, if needed. */
if (!CLOCK_VIRTUAL_P(it->it_clockid))
callout_destroy(&it->it_ch);
+}
+
+static void
+itimer_vlist_remove(struct itimer *it)
+{
+ struct itimer *itn;
+
+ KASSERT(itimer_lock_held());
+
+ if (!CLOCK_VIRTUAL_P(it->it_clockid) || !it->it_active)
+ return;
+ itn = LIST_NEXT(it, it_list);
+ LIST_REMOVE(it, it_list);
+ if (itn != NULL)
+ timespecadd(&it->it_time.it_value, &itn->it_time.it_value,
+ &itn->it_time.it_value);
+ it->it_active = false;
}
/*
@@ -813,6 +830,8 @@
return false;
/* expired, exactly at end of interval */
expire:
+ timespecclear(&itp->it_value);
+ itimer_vlist_remove(it);
if (timespecisset(&itp->it_interval)) {
itp->it_value = itp->it_interval;
itp->it_value.tv_nsec -= nsec;
@@ -822,8 +841,7 @@
}
error = itimer_settime(it);
KASSERT(error == 0); /* virtual, never fails */
- } else
- itp->it_value.tv_nsec = 0; /* sec is already 0 */
+ }
return true;
}
@@ -941,14 +959,6 @@
if (timespecisset(&it->it_time.it_value))
itimer_arm_real(it);
} else {
- if (it->it_active) {
- itn = LIST_NEXT(it, it_list);
- LIST_REMOVE(it, it_list);
- for ( ; itn; itn = LIST_NEXT(itn, it_list))
- timespecadd(&it->it_time.it_value,
- &itn->it_time.it_value,
- &itn->it_time.it_value);
- }
if (timespecisset(&it->it_time.it_value)) {
itl = it->it_vlist;
for (itn = LIST_FIRST(itl), pitn = NULL;
@@ -964,7 +974,7 @@
else
LIST_INSERT_HEAD(itl, it, it_list);
- for ( ; itn ; itn = LIST_NEXT(itn, it_list))
+ if (itn != NULL)
timespecsub(&itn->it_time.it_value,
&it->it_time.it_value,
&itn->it_time.it_value);
@@ -1326,7 +1336,7 @@
struct proc *p = l->l_proc;
timer_t timerid;
struct ptimers *pts;
- struct itimer *it, *itn;
+ struct itimer *it;
timerid = SCARG(uap, timerid);
pts = p->p_timers;
@@ -1340,17 +1350,7 @@
return SET_ERROR(EINVAL);
}
- if (CLOCK_VIRTUAL_P(it->it_clockid)) {
- if (it->it_active) {
- itn = LIST_NEXT(it, it_list);
- LIST_REMOVE(it, it_list);
- for ( ; itn; itn = LIST_NEXT(itn, it_list))
- timespecadd(&it->it_time.it_value,
- &itn->it_time.it_value,
- &itn->it_time.it_value);
- it->it_active = false;
- }
- }
+ itimer_vlist_remove(it);
/* Free the timer and release the lock. */
ptimer_free(pts, timerid);
@@ -1422,6 +1422,7 @@
if (ovalue)
itimer_gettime(it, ovalue);
+ itimer_vlist_remove(it);
it->it_time = val;
/*
@@ -1716,6 +1717,7 @@
pts->pts_timers[which] = it;
}
+ itimer_vlist_remove(it);
TIMEVAL_TO_TIMESPEC(&itvp->it_value, &it->it_time.it_value);
TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &it->it_time.it_interval);
--- tests/lib/libc/sys/t_getitimer.c 1.6
+++ tests/lib/libc/sys/t_getitimer.c working copy
@@ -38,6 +38,7 @@
#include <limits.h>
#include <signal.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
#include "h_macros.h"
@@ -239,8 +240,150 @@
ATF_CHECK_ERRNO(EAGAIN,
sigtimedwait(&sigs, NULL, &(const struct timespec){2, 0}) == -1);
RL(sigprocmask(SIG_SETMASK, &mask, NULL));
+}
+
+static volatile sig_atomic_t nprof, nvtalrm, nusr1;
+
+static void
+counthandler(int signo)
+{
+
+ if (signo == SIGPROF)
+ nprof++;
+ if (signo == SIGVTALRM)
+ nvtalrm++;
+ if (signo == SIGUSR1)
+ nusr1++;
+}
+
+static void
+burn(time_t secs)
+{
+ struct timespec start, now;
+ volatile unsigned long i;
+
+ RL(clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start));
+ for (;;) {
+ for (i = 0; i < 1000000; i++)
+ continue;
+ RL(clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now));
+ if (now.tv_sec - start.tv_sec >= secs)
+ return;
+ }
}
+static void
+setitimer_oneshot(int which, int signo, volatile sig_atomic_t *count)
+{
+ const struct itimerval it = { .it_value = {0, 500000} };
+ struct itimerval ot;
+
+ nprof = nvtalrm = 0;
+ ATF_REQUIRE(signal(signo, counthandler) != SIG_ERR);
+ RL(setitimer(which, &it, NULL));
+ burn(2);
+ RL(getitimer(which, &ot));
+
+ ATF_CHECK_MSG(*count == 1, "timer fired %d times, expected once",
+ (int)*count);
+ ATF_CHECK(!timerisset(&ot.it_value));
+}
+
+ATF_TC(setitimer_prof_oneshot);
+ATF_TC_HEAD(setitimer_prof_oneshot, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "A one-shot ITIMER_PROF timer fires only once");
+}
+
+ATF_TC_BODY(setitimer_prof_oneshot, tc)
+{
+
+ setitimer_oneshot(ITIMER_PROF, SIGPROF, &nprof);
+}
+
+ATF_TC(setitimer_virtual_oneshot);
+ATF_TC_HEAD(setitimer_virtual_oneshot, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "A one-shot ITIMER_VIRTUAL timer fires only once");
+}
+
+ATF_TC_BODY(setitimer_virtual_oneshot, tc)
+{
+
+ setitimer_oneshot(ITIMER_VIRTUAL, SIGVTALRM, &nvtalrm);
+}
+
+ATF_TC(setitimer_prof_second);
+ATF_TC_HEAD(setitimer_prof_second, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "A CLOCK_PROF timer fires after a shorter ITIMER_PROF one expires");
+}
+
+ATF_TC_BODY(setitimer_prof_second, tc)
+{
+ const struct itimerval it[] = {
+ [0] = { .it_value = {0, 200000} },
+ [1] = { .it_value = {0, 100000}, .it_interval = {0, 100000} },
+ };
+ struct sigevent ev = {
+ .sigev_notify = SIGEV_SIGNAL,
+ .sigev_signo = SIGUSR1,
+ };
+ const struct itimerspec its = { .it_value = {1, 0} };
+ timer_t t;
+ unsigned i;
+
+ ATF_REQUIRE(signal(SIGPROF, counthandler) != SIG_ERR);
+ ATF_REQUIRE(signal(SIGUSR1, counthandler) != SIG_ERR);
+
+ for (i = 0; i < __arraycount(it); i++) {
+ fprintf(stderr, "case %u\n", i);
+ nprof = nusr1 = 0;
+ RL(timer_create(CLOCK_PROF, &ev, &t));
+ RL(timer_settime(t, 0, &its, NULL));
+ RL(setitimer(ITIMER_PROF, &it[i], NULL));
+ burn(2);
+ RL(setitimer(ITIMER_PROF,
+ &(const struct itimerval){{0, 0}, {0, 0}}, NULL));
+ RL(timer_delete(t));
+ ATF_CHECK_MSG(nusr1 == 1,
+ "CLOCK_PROF timer fired %d times, expected once",
+ (int)nusr1);
+ }
+}
+
+ATF_TC(setitimer_prof_disarm);
+ATF_TC_HEAD(setitimer_prof_disarm, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Disarming ITIMER_PROF doesn't change a later CLOCK_PROF timer");
+}
+
+ATF_TC_BODY(setitimer_prof_disarm, tc)
+{
+ struct sigevent ev = { .sigev_notify = SIGEV_NONE };
+ const struct itimerspec its = { .it_value = {0, 900000000} };
+ const struct itimerval it = { .it_value = {0, 300000} };
+ struct itimerspec ots;
+ timer_t t;
+ long ms;
+
+ RL(timer_create(CLOCK_PROF, &ev, &t));
+ RL(timer_settime(t, 0, &its, NULL));
+ RL(setitimer(ITIMER_PROF, &it, NULL));
+ RL(setitimer(ITIMER_PROF,
+ &(const struct itimerval){{0, 0}, {0, 0}}, NULL));
+ RL(timer_gettime(t, &ots));
+ RL(timer_delete(t));
+
+ ms = ots.it_value.tv_sec * 1000 + ots.it_value.tv_nsec / 1000000;
+ ATF_CHECK_MSG(ms > 800 && ms <= 900,
+ "CLOCK_PROF timer has %ld ms left, expected about 900", ms);
+}
+
ATF_TP_ADD_TCS(tp)
{
@@ -250,6 +393,10 @@
ATF_TP_ADD_TC(tp, setitimer_err);
ATF_TP_ADD_TC(tp, setitimer_old);
ATF_TP_ADD_TC(tp, setitimer_invalidtime);
+ ATF_TP_ADD_TC(tp, setitimer_prof_oneshot);
+ ATF_TP_ADD_TC(tp, setitimer_virtual_oneshot);
+ ATF_TP_ADD_TC(tp, setitimer_prof_second);
+ ATF_TP_ADD_TC(tp, setitimer_prof_disarm);
return atf_no_error();
}
--- tests/lib/libc/sys/t_timer_create.c 1.11
+++ tests/lib/libc/sys/t_timer_create.c working copy
@@ -472,8 +472,117 @@
RL(sigprocmask(SIG_SETMASK, &mask, NULL));
RL(timer_delete(t));
+}
+
+static void
+prof_set(timer_t tid, long ms)
+{
+ const struct itimerspec its = {
+ .it_value = { ms / 1000, (ms % 1000) * 1000000 },
+ };
+
+ RL(timer_settime(tid, 0, &its, NULL));
+}
+
+static void
+prof_check(timer_t tid, long ms)
+{
+ struct itimerspec its;
+ long left;
+
+ RL(timer_gettime(tid, &its));
+ left = its.it_value.tv_sec * 1000 + its.it_value.tv_nsec / 1000000;
+ ATF_CHECK_MSG(left > ms - 100 && left <= ms,
+ "timer set to %ld ms has %ld ms left", ms, left);
+}
+
+ATF_TC(timer_create_prof_order);
+ATF_TC_HEAD(timer_create_prof_order, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that CLOCK_PROF timers set in front of other ones "
+ "keep their times");
+}
+
+ATF_TC_BODY(timer_create_prof_order, tc)
+{
+ struct sigevent ev = { .sigev_notify = SIGEV_NONE };
+ const long ms[] = { 900, 600, 300 };
+ timer_t tid[__arraycount(ms)];
+ unsigned i;
+
+ for (i = 0; i < __arraycount(ms); i++) {
+ RL(timer_create(CLOCK_PROF, &ev, &tid[i]));
+ prof_set(tid[i], ms[i]);
+ }
+ for (i = 0; i < __arraycount(ms); i++)
+ prof_check(tid[i], ms[i]);
+ for (i = 0; i < __arraycount(ms); i++)
+ RL(timer_delete(tid[i]));
+}
+
+ATF_TC(timer_create_prof_delete);
+ATF_TC_HEAD(timer_create_prof_delete, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that deleting a CLOCK_PROF timer doesn't change "
+ "the later ones");
}
+ATF_TC_BODY(timer_create_prof_delete, tc)
+{
+ struct sigevent ev = { .sigev_notify = SIGEV_NONE };
+ const long ms[] = { 300, 600, 900, 1200 };
+ timer_t tid[__arraycount(ms)];
+ unsigned i;
+
+ for (i = 0; i < __arraycount(ms); i++) {
+ RL(timer_create(CLOCK_PROF, &ev, &tid[i]));
+ prof_set(tid[i], ms[i]);
+ }
+ RL(timer_delete(tid[1]));
+ for (i = 0; i < __arraycount(ms); i++) {
+ if (i == 1)
+ continue;
+ prof_check(tid[i], ms[i]);
+ RL(timer_delete(tid[i]));
+ }
+}
+
+ATF_TC(timer_create_prof_rearm);
+ATF_TC_HEAD(timer_create_prof_rearm, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "Checks that re-arming or disarming a CLOCK_PROF timer doesn't "
+ "change a later one");
+}
+
+ATF_TC_BODY(timer_create_prof_rearm, tc)
+{
+ struct sigevent ev = { .sigev_notify = SIGEV_NONE };
+ timer_t a, b;
+
+ RL(timer_create(CLOCK_PROF, &ev, &a));
+ RL(timer_create(CLOCK_PROF, &ev, &b));
+ prof_set(b, 900);
+ prof_set(a, 300);
+
+ fprintf(stderr, "re-arm\n");
+ prof_set(a, 500);
+ prof_check(a, 500);
+ prof_check(b, 900);
+
+ fprintf(stderr, "disarm\n");
+ prof_set(a, 0);
+ prof_check(b, 900);
+
+ RL(timer_delete(a));
+ RL(timer_delete(b));
+}
+
ATF_TP_ADD_TCS(tp)
{
@@ -489,6 +598,9 @@
ATF_TP_ADD_TC(tp, timer_create_real_past_abs);
ATF_TP_ADD_TC(tp, timer_create_mono_past_abs);
ATF_TP_ADD_TC(tp, timer_invalidtime);
+ ATF_TP_ADD_TC(tp, timer_create_prof_order);
+ ATF_TP_ADD_TC(tp, timer_create_prof_delete);
+ ATF_TP_ADD_TC(tp, timer_create_prof_rearm);
return atf_no_error();
}
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.