Skip to content

proc: use CLOCK_BOOTTIME for uptime and task start times - #17657

Open
asimawdah wants to merge 1 commit into
google:masterfrom
asimawdah:fix/proc-boottime-clock-17066
Open

asimawdah wants to merge 1 commit into
google:masterfrom
asimawdah:fix/proc-boottime-clock-17066

Conversation

@asimawdah

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

The uptime values exposed by /proc/uptime and /proc/stat are derived by subtracting the boot-time realtime timestamp from the current realtime clock.

The starttime field in /proc/[pid]/stat is derived from each task's realtime start timestamp.

When the host realtime clock moves backwards, these values can move backwards too. A process created after the clock adjustment can appear to have started before an older process.

What is the new behavior?

  • Derive /proc/uptime and /proc/stat uptime from CLOCK_BOOTTIME.
  • Record each task's start time on CLOCK_BOOTTIME separately from its realtime start timestamp.
  • Use the boot-relative task start time for /proc/[pid]/stat.
  • Preserve both start-time values when an exec replaces the thread-group leader.

The existing realtime Task.StartTime() value remains unchanged for control, tracing, and other consumers.

Fixes #17066

How was this tested?

Tests were not run locally.

The existing pkg/sentry/kernel/timekeeper_test.go includes coverage for realtime moving backwards while boot time remains continuous.

oldStartTimeSinceBoot := oldLeader.StartTimeSinceBoot()
t.mu.Lock()
t.startTime = oldStartTime
t.startTimeSinceBoot = oldStartTimeSinceBoot

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the new timestamp's instant/duration representation consistent. StartTimeSinceBoot() returns time.Duration, but startTimeSinceBoot is ktime.Time, so this assignment fails to compile. The focused //pkg/sentry/kernel:kernel_test build fails here with “cannot use oldStartTimeSinceBoot ... as ktime.Time value in assignment”. There is another mismatch in statData.Generate: uptime is now ktime.Time, while linux.ClockTFromDuration(uptime) requires time.Duration. Preserve the stored clock instant when copying it and explicitly convert to elapsed duration for the proc calculations (or consistently store a duration).

Codex posting on behalf of Ayush

Comment thread pkg/sentry/fsimpl/proc/tasks_files.go Outdated

// Pretend that we've spent zero time sleeping (second number).
fmt.Fprintf(buf, "%.2f 0.00\n", now.Sub(k.Timekeeper().BootTime()).Seconds())
fmt.Fprintf(buf, "%.2f 0.00\n", uptime.Seconds())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert the boot-clock instant to a duration before formatting its seconds. ktime.Time.Seconds() returns int64, whereas the previous time.Duration.Seconds() returned float64. With the unchanged %.2f, this now writes a token such as %!f(int64=123) 0.00 instead of numeric uptime, breaking readers of /proc/uptime even after the compilation errors are fixed. Using an elapsed duration's Seconds() preserves the fractional seconds and the existing numeric format. The existing ProcUptime.IsPresent test also depends on this parseable, advancing value.

Codex posting on behalf of Ayush

@ayushr2

ayushr2 commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Please squash the three follow-up fix commits (dedb0e74a, 6a166785a, and 18889c154) into the original change before landing. They repair type/conversion errors in the same logical change, and the intermediate revisions do not build independently. gVisor applies the PR commits directly without squashing, so folding these corrections in will keep the permanent history buildable. The current head passes the focused kernel and procfs unit tests.

Codex posting on behalf of Ayush

@asimawdah
asimawdah force-pushed the fix/proc-boottime-clock-17066 branch from 18889c1 to 3d5d7e7 Compare November 15, 2026 01:56
@asimawdah

Copy link
Copy Markdown
Author

Hi Ayush,

I've fixed the issues you pointed out and squashed the follow-up commits into one.

Please let me know if anything else needs to be fixed.

Thanks for your feedback ❤️

@ayushr2

ayushr2 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@asimawdah I don't think the above comments are addressed. We still call fmt.Fprintf(buf, "%.2f 0.00\n", uptime.Seconds())

@asimawdah
asimawdah force-pushed the fix/proc-boottime-clock-17066 branch from afbe7a1 to 89857e9 Compare November 16, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/proc/[pid]/stat starttime and /proc/uptime follow the realtime clock, so a host clock step reorders processes

2 participants