* [PATCH] compat: Avoid double rounding in times()
@ 2026-10-20 21:01 Hungyu Lin
2026-10-20 21:35 ` Andrew Morton
2026-10-20 21:36 ` Arnd Bergmann
0 siblings, 2 replies; 3+ messages in thread
From: Hungyu Lin @ 2026-10-20 21:01 UTC (permalink / raw)
To: arnd; +Cc: akpm, tglx, linux-kernel, stable, Hungyu Lin
do_sys_times() already converts CPU time to USER_HZ ticks. The compat
path converts each value back to jiffies before converting it to
COMPAT_USER_HZ. This round trip can lose precision even when the two
userspace rates match.
With HZ=250 and USER_HZ=COMPAT_USER_HZ=100, one user tick becomes two
jiffies and then zero compat ticks. Every odd tick count loses one
tick, so any of the four tms fields can underreport CPU time by an
additional 10 ms.
Return the value directly when the native and compat units match. Keep
the existing conversion for differing rates.
Fixes: f06febc96ba8 ("timers: fix itimer/many thread hang")
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
kernel/sys.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/kernel/sys.c b/kernel/sys.c
index 35b538ba843c..755bfa5ccd92 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1077,6 +1077,9 @@ SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
#ifdef CONFIG_COMPAT
static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
{
+ if (USER_HZ == COMPAT_USER_HZ)
+ return x;
+
return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
}
base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] compat: Avoid double rounding in times()
2026-10-20 21:01 [PATCH] compat: Avoid double rounding in times() Hungyu Lin
@ 2026-10-20 21:35 ` Andrew Morton
2026-10-20 21:36 ` Arnd Bergmann
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-10-20 21:35 UTC (permalink / raw)
To: Hungyu Lin; +Cc: arnd, tglx, linux-kernel, stable
On Tue, 20 Oct 2026 21:01:57 +0000 Hungyu Lin <dennylin0707@gmail.com> wrote:
> do_sys_times() already converts CPU time to USER_HZ ticks. The compat
> path converts each value back to jiffies before converting it to
> COMPAT_USER_HZ. This round trip can lose precision even when the two
> userspace rates match.
>
> With HZ=250 and USER_HZ=COMPAT_USER_HZ=100, one user tick becomes two
> jiffies and then zero compat ticks. Every odd tick count loses one
> tick, so any of the four tms fields can underreport CPU time by an
> additional 10 ms.
>
> Return the value directly when the native and compat units match. Keep
> the existing conversion for differing rates.
The fix looks correct. The compat times() path unnecessarily converts
CPU time from USER_HZ ticks to jiffies and back, losing precision.
I had $LLM model the userspace-visible results for tick counts 0 through
10,000. With HZ=250 and USER_HZ=COMPAT_USER_HZ=100, 5,000 values
underreport CPU time by 10 ms. The patch eliminates these errors
without changing the differing-rate case.
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] compat: Avoid double rounding in times()
2026-10-20 21:01 [PATCH] compat: Avoid double rounding in times() Hungyu Lin
2026-10-20 21:35 ` Andrew Morton
@ 2026-10-20 21:36 ` Arnd Bergmann
1 sibling, 0 replies; 3+ messages in thread
From: Arnd Bergmann @ 2026-10-20 21:36 UTC (permalink / raw)
To: Hungyu Lin; +Cc: Andrew Morton, Thomas Gleixner, linux-kernel, stable
On Tue, Oct 20, 2026, at 23:01, Hungyu Lin wrote:
> do_sys_times() already converts CPU time to USER_HZ ticks. The compat
> path converts each value back to jiffies before converting it to
> COMPAT_USER_HZ. This round trip can lose precision even when the two
> userspace rates match.
>
> With HZ=250 and USER_HZ=COMPAT_USER_HZ=100, one user tick becomes two
> jiffies and then zero compat ticks. Every odd tick count loses one
> tick, so any of the four tms fields can underreport CPU time by an
> additional 10 ms.
>
> Return the value directly when the native and compat units match. Keep
> the existing conversion for differing rates.
Good catch!
> Fixes: f06febc96ba8 ("timers: fix itimer/many thread hang")
That one apparently just moved the code into a separate function, but
the bug was already there before 2.6.12:
+ tmp.tms_utime = compat_jiffies_to_clock_t(cputime_to_jiffies(utime));
+ tmp.tms_stime = compat_jiffies_to_clock_t(cputime_to_jiffies(stime));
+ tmp.tms_cutime = compat_jiffies_to_clock_t(cputime_to_jiffies(cutime));
+ tmp.tms_cstime = compat_jiffies_to_clock_t(cputime_to_jiffies(cstime));
> #ifdef CONFIG_COMPAT
> static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
> {
> + if (USER_HZ == COMPAT_USER_HZ)
> + return x;
> +
> return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
I don't think there are any architectures left that define USER_HZ
and COMPAT_USER_HZ differently. This was true on ia64 with x86 compat,
but that was dropped in v2.6.33 back in 2010.
Arnd
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-20 21:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 21:01 [PATCH] compat: Avoid double rounding in times() Hungyu Lin
2026-10-20 21:35 ` Andrew Morton
2026-10-20 21:36 ` Arnd Bergmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®