tsacct: keep task begin times stable across suspend
Subsystems (2)
v2 cover tsacct: keep task begin times stable across suspend
Keep taskstats begin times stable across suspend without changing the MONOTONIC elapsed-time fields.
Patch 1 makes getboottime64() sample its offsets under tk_core.seq. Patch 2 uses that helper and the task's BOOTTIME start timestamp, and clamps negative begin times to zero. Keep the helper prerequisite when backporting the taskstats change.
Changes in v2, addressing Andrew Morton's review:
- Add the getboottime64() sampling prerequisite as patch 1/2.
- Lower-clamp ac_btime64 without imposing a 32-bit upper limit.
- Add Cc: stable to both patches.
- Rebase onto mainline d89e1d186ec9.
Validation used GCC 13.3.0 with W=1. ARM64 and native 32-bit SMP x86 QEMU guests pass all 36 checks, including the original 24. The 32-bit helper stress reproduced inconsistent timestamps before the fix and none after. Suspend time was injected through the real timekeeping hook.
v1: https://lore.kernel.org/r/[email protected]
Hungyu Lin (2):
timekeeping: Read getboottime64() offsets under the sequence counter
tsacct: keep task begin times stable across suspend
kernel/time/timekeeping.c | 8 +++++++- kernel/tsacct.c | 10 ++++++---- 2 files changed, 13 insertions(+), 5 deletions(-)
base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3
Signature · 1 line
2.43.0
PATCH v2 1/2 timekeeping: Read getboottime64() offsets under the sequence counter
getboottime64() computes the wall-clock boot timestamp from offs_real minus offs_boot without checking tk_core.seq. A concurrent timekeeping update can leave the reader with an inconsistent pair of offsets or, on 32-bit systems, a torn 64-bit offset, producing an incorrect boot timestamp.
Read both offsets under tk_core.seq and retry if an update overlaps the sample. Convert the resulting local value to a timespec64 afterward.
---
New prerequisite in v2, addressing Andrew Morton's review.
kernel/time/timekeeping.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/time/timekeeping.c +7 -1
@@ -2700,7 +2700,13 @@ void update_wall_time(void)void getboottime64 (struct timespec64 *ts) {struct timekeeper *tk = &tk_core.timekeeper; -ktime_t t =ktime_sub (tk->offs_real, tk->offs_boot); +unsigned int seq; +ktime_t t; + +do { + seq =read_seqcount_begin (&tk_core.seq); + t =ktime_sub (tk->offs_real, tk->offs_boot); + }while (read_seqcount_retry (&tk_core.seq, seq)); *ts =ktime_to_timespec64 (t); }
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] timekeeping: Read getboottime64() offsets under the sequence counter
Quoted 28 lines · Hungyu Lin
getboottime64()computes the wall-clock boot timestamp fromoffs_realminusoffs_bootwithout checking tk_core.seq. A concurrent timekeeping update can leave the reader with an inconsistent pair of offsets or, on 32-bit systems, a torn 64-bit offset, producing an incorrect boot timestamp.Read both offsets under tk_core.seq and retry if an update overlaps the sample. Convert the resulting local value to a timespec64 afterward.
Fixes 7c3f1a573237 ("Introduce boot based time")Signed-off-by Hungyu Lin <[email protected]>---
New prerequisite in v2, addressing Andrew Morton's review.kernel/time/timekeeping.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/time/timekeeping.c +0
-1@@ -2700,7 +2700,13 @@ void update_wall_time(void)void getboottime64 (struct timespec64 *ts) {struct timekeeper *tk = &tk_core.timekeeper; -ktime_t t =ktime_sub (tk->offs_real, tk->offs_boot);
kernel/time/timekeeping.c void update_wall_time(void) · line 2712
Quoted 10 lines
kernel/time/timekeeping.c +7
-0+unsigned int seq; +ktime_t t; + +do { + seq =read_seqcount_begin (&tk_core.seq); + t =ktime_sub (tk->offs_real, tk->offs_boot); + }while (read_seqcount_retry (&tk_core.seq, seq)); *ts =ktime_to_timespec64 (t); }
Seems reasonable to me. Thanks for sending this out!
[tip: timers/urgent] timekeeping: Read getboottime64() offsets under the sequence counter
In reply to Hungyu LinThe following commit has been merged into the timers/urgent branch of tip:
The following commit has been merged into the timers/urgent branch of tip:
Commit-ID: 385b5ec1f31dd39297578eed42be74b07ae74017 Gitweb: https://git.kernel.org/tip/385b5ec1f31dd39297578eed42be74b07ae74017 Author: Hungyu Lin <[email protected]> AuthorDate: Fri, 30 Oct 2026 05:42:26 Committer: Thomas Gleixner <[email protected]> CommitterDate: Mon, 09 Nov 2026 20:15:58 +01:00
timekeeping: Read getboottime64() offsets under the sequence counter
getboottime64() computes the wall-clock boot timestamp from offs_real minus offs_boot without checking tk_core.seq. A concurrent timekeeping update can leave the reader with an inconsistent pair of offsets or, on 32-bit systems, a torn 64-bit offset, producing an incorrect boot timestamp.
Read both offsets under tk_core.seq and retry if an update overlaps the sample. Convert the resulting local value to a timespec64 afterward.
--- kernel/time/timekeeping.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/time/timekeeping.c +7 -1
@@ -2700,7 +2700,13 @@ void update_wall_time(void)void getboottime64 (struct timespec64 *ts) {struct timekeeper *tk = &tk_core.timekeeper; -ktime_t t =ktime_sub (tk->offs_real, tk->offs_boot); +unsigned int seq; +ktime_t t; + +do { + seq =read_seqcount_begin (&tk_core.seq); + t =ktime_sub (tk->offs_real, tk->offs_boot); + }while (read_seqcount_retry (&tk_core.seq, seq)); *ts =ktime_to_timespec64 (t); }
PATCH v2 2/2 tsacct: keep task begin times stable across suspend
In reply to Hungyu Linbacct_add_tsk() derives ac_btime and ac_btime64 by subtracting a CLOCK_MONOTONIC elapsed duration from CLOCK_REALTIME. MONOTONIC stops during suspend while REALTIME continues, so querying an existing task after resume moves its reported begin time forward by the time spent suspended.
Calculate the begin time from the wall-clock boot timestamp and the task's CLOCK_BOOTTIME start timestamp instead. This accounts for suspend and adds the nanoseconds before truncating to seconds. Keep ac_etime and ac_tgetime on CLOCK_MONOTONIC and retain the existing rebasing when the wall clock is adjusted.
The new begin time can be negative: setting CLOCK_REALTIME is bounded by CLOCK_MONOTONIC, which excludes suspend, rather than CLOCK_BOOTTIME. A permitted backward clock adjustment after suspend can therefore put the task's begin time before the epoch. Clamp ac_btime64 at zero too, instead of wrapping in the unsigned field, and retain its positive range.
---
Changes in v2:
- Clamp negative ac_btime64 values to zero without a 32-bit upper limit.
- Precede this change with the getboottime64() sampling fix.
- Add Cc: stable and rebase onto mainline d89e1d186ec9.
kernel/tsacct.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/tsacct.c +6 -4
@@ -24,7 +24,7 @@ void bacct_add_tsk(struct user_namespace *user_ns,const struct cred *tcred;u64 utime, stime, utimescaled, stimescaled;u64 now_ns, delta; -time64_t btime; +struct timespec64 btime;BUILD_BUG_ON (TS_COMM_LEN <TASK_COMM_LEN );
@@ -39,9 +39,11 @@ void bacct_add_tsk(struct user_namespace *user_ns,do_div (delta,NSEC_PER_USEC ); stats->ac_etime = delta;/* Convert to seconds for btime (note y2106 limit) */ - btime =ktime_get_real_seconds () -div_u64 (delta,USEC_PER_SEC ); - stats->ac_btime =clamp_t (time64_t, btime,0 ,U32_MAX ); - stats->ac_btime64 = btime; +getboottime64 (&btime); + btime =timespec64_add (btime,ns_to_timespec64 (tsk->start_boottime)); + stats->ac_btime =clamp_t (time64_t, btime.tv_sec,0 ,U32_MAX ); +/* Setting the clock back after suspend can make btime negative. */ + stats->ac_btime64 =max_t (time64_t, btime.tv_sec,0 );if (tsk->flags &PF_EXITING ) stats->ac_exitcode = tsk->exit_code;
Signature · 1 line
2.43.0
[tip: timers/urgent] tsacct: Keep task begin times stable across suspend
The following commit has been merged into the timers/urgent branch of tip:
The following commit has been merged into the timers/urgent branch of tip:
Commit-ID: c49b3c20a2d0b22700e2100a527e68e829ece7f0 Gitweb: https://git.kernel.org/tip/c49b3c20a2d0b22700e2100a527e68e829ece7f0 Author: Hungyu Lin <[email protected]> AuthorDate: Fri, 30 Oct 2026 05:42:27 Committer: Thomas Gleixner <[email protected]> CommitterDate: Mon, 09 Nov 2026 20:15:58 +01:00
tsacct: Keep task begin times stable across suspend
bacct_add_tsk() derives ac_btime and ac_btime64 by subtracting a CLOCK_MONOTONIC elapsed duration from CLOCK_REALTIME. MONOTONIC stops during suspend while REALTIME continues, so querying an existing task after resume moves its reported begin time forward by the time spent suspended.
Calculate the begin time from the wall-clock boot timestamp and the task's CLOCK_BOOTTIME start timestamp instead. This accounts for suspend and adds the nanoseconds before truncating to seconds. Keep ac_etime and ac_tgetime on CLOCK_MONOTONIC and retain the existing rebasing when the wall clock is adjusted.
The new begin time can be negative: setting CLOCK_REALTIME is bounded by CLOCK_MONOTONIC, which excludes suspend, rather than CLOCK_BOOTTIME. A permitted backward clock adjustment after suspend can therefore put the task's begin time before the epoch. Clamp ac_btime64 at zero too, instead of wrapping in the unsigned field, and retain its positive range.
--- kernel/tsacct.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/tsacct.c +6 -4
@@ -24,7 +24,7 @@ void bacct_add_tsk(struct user_namespace *user_ns,const struct cred *tcred;u64 utime, stime, utimescaled, stimescaled;u64 now_ns, delta; -time64_t btime; +struct timespec64 btime;BUILD_BUG_ON (TS_COMM_LEN <TASK_COMM_LEN );
@@ -39,9 +39,11 @@ void bacct_add_tsk(struct user_namespace *user_ns,do_div (delta,NSEC_PER_USEC ); stats->ac_etime = delta;/* Convert to seconds for btime (note y2106 limit) */ - btime =ktime_get_real_seconds () -div_u64 (delta,USEC_PER_SEC ); - stats->ac_btime =clamp_t (time64_t, btime,0 ,U32_MAX ); - stats->ac_btime64 = btime; +getboottime64 (&btime); + btime =timespec64_add (btime,ns_to_timespec64 (tsk->start_boottime)); + stats->ac_btime =clamp_t (time64_t, btime.tv_sec,0 ,U32_MAX ); +/* Setting the clock back after suspend can make btime negative. */ + stats->ac_btime64 =max_t (time64_t, btime.tv_sec,0 );if (tsk->flags &PF_EXITING ) stats->ac_exitcode = tsk->exit_code;