[PATCH] timers/itimer: Preserve nonzero remaining time for CPU timers

From: Hang Zhengyang

Date: Thu Dec 03 2026 - 10:43:05 EST


getitimer() and setitimer() return the remaining time of ITIMER_VIRTUAL
and ITIMER_PROF in a struct timeval, which only stores whole
microseconds. get_cpu_itimer() and set_cpu_itimer() compute the
remaining time in nanoseconds. When less than one microsecond is left,
the conversion truncates it to zero, and an armed timer reads as
disarmed.

The remaining time is not always a whole number of microseconds,
because the CPU time samples are not always whole ticks. Steal time in
a guest, IRQ time accounting and VIRT_CPU_ACCOUNTING_GEN all account
parts of a tick.

Commit 043c11572276 ("timers/itimer: Preserve nonzero remaining time for
active timers") fixed the same truncation for ITIMER_REAL. Do the same
for the CPU timers: return less than one microsecond as one
microsecond.

A periodic timer is always armed, so its it_value should never read as
zero. Test: a KVM guest booted with nohz_full=1-7, one task per CPU
with a 2ms periodic timer, calling getitimer() in a loop for 60 seconds.

ITIMER_PROF, CPU 1 zero reads: 99422 before, 0 after
ITIMER_PROF, CPU 3 zero reads: 46114 before, 0 after
ITIMER_VIRTUAL, CPU 5 zero reads: 111991 before, 0 after

setitimer() returns the same zero as the old value. Same setup, 300
seconds, 7 CPUs, calling setitimer() whenever getitimer() reads 1us or
less:

zero old values: 19 of 198 calls before, 0 of 189 calls after

Fixes: 858cf3a8c599 ("timers/itimer: Convert internal cputime_t units to nsec")
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@xxxxxxxxx>
---

Notes:
About the Fixes tag: before 858cf3a8c599 the conversion was done by
cputime_to_timeval(), which also truncated on configurations with fine
grained cputime (s390, powerpc native accounting,
VIRT_CPU_ACCOUNTING_GEN). With jiffies based cputime the remaining time
was always whole ticks. 858cf3a8c599 moved all configurations to
nanoseconds, so steal and IRQ time made the problem reachable
everywhere. I picked that commit, but an older one may fit better.

Tested on next-20261202 with a debug config. The posix_timers selftest
passes before and after.

I used Claude Code to find the issue, write the patch and run the
tests. I reviewed the patch.

kernel/time/itimer.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
index 38ffbc514bc0..0207f83dceea 100644
--- a/kernel/time/itimer.c
+++ b/kernel/time/itimer.c
@@ -60,11 +60,13 @@ static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
thread_group_sample_cputime(tsk, samples);
t = samples[clock_id];

- if (val <= t)
+ if (val <= t) {
/* about to fire */
val = TICK_NSEC;
- else
- val -= t;
+ } else {
+ /* Less than 1us would read as 0 (disarmed) in a timeval */
+ val = max_t(u64, val - t, NSEC_PER_USEC);
+ }
}

spin_unlock_irq(&tsk->sighand->siglock);
@@ -202,6 +204,9 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
if (nval > 0)
nval += TICK_NSEC;
set_process_cpu_timer(tsk, clock_id, &nval, &oval);
+ /* Less than 1us would read as 0 (disarmed) in a timeval */
+ if (oval)
+ oval = max_t(u64, oval, NSEC_PER_USEC);
}
it->expires = nval;
it->incr = ninterval;

base-commit: b4c794ff2b232e081d8ad3f0575eba2c49442949
--
2.53.0