How the Linux Timekeeping Subsystem Works: Clocksources, Mult/Shift Scaling, and Why the vDSO Answers clock_gettime Without a Trap
A process that calls clock_gettime(CLOCK_MONOTONIC) ten million times a second does not expect each call to trap into the kernel. On a modern Linux system most of those calls never leave user space. The kernel still owns the authoritative time; user space merely reads a carefully published snapshot.
This article explains that contract: how a hardware counter becomes nanoseconds, how the timekeeper maintains bases for different clocks, and how the vDSO lets user space perform the same calculation without a system call. It is not a second tour of the syscall ABI. Related reading: How System Calls Work.
The contract
The kernel selects one clocksource (commonly the TSC on x86). It records a base time and the corresponding cycle count. On every read it computes the cycle delta, scales it to nanoseconds with fixed-point mult and shift, and adds it to the base. Different clocks (REALTIME, MONOTONIC, BOOTTIME, TAI, RAW) differ only in which base and offsets they use. NTP can adjust the mult over time.
Clocksources and mult/shift
A clocksource is a continuously readable counter with a known frequency. Conversion avoids division: ns = (cycles * mult) >> shift. clocks_calc_mult_shift chooses the pair so that the multiplication stays accurate for a guaranteed number of seconds without overflowing 64 bits. The formula is approximate only in the sense of fixed-point precision; for practical frequencies the error is far below a nanosecond.
The timekeeper
struct timekeeper holds xtime, offsets, and two tk_read_base structures (mono and raw). Each tk_read_base contains the clock pointer, cycle_last, mult, shift, xtime_nsec, and the base ktime. Reads use a seqcount so that an update never produces a torn or backwards value. The periodic update (or tick) advances the bases; readers always see a consistent snapshot.
vDSO fast path
The kernel maps a shared page containing the current timekeeper parameters and a sequence counter. The vDSO clock_gettime implementation reads the sequence, copies the bases and mult/shift, reads the hardware counter (if the clocksource allows it from user space), performs the same scaling arithmetic, and retries if the sequence changed. No privilege transition occurs. Coarse clocks simply return the last published value. If the clocksource is not vDSO-capable the call falls back to a real syscall.
Misconceptions
The vDSO invents time. It only reads and scales data the kernel published. clock_gettime always traps. On modern kernels for the common clocks it does not. Mult/shift is approximate in a harmful way. The precision is chosen so that the conversion error is negligible for the guaranteed range.
Takeaways
Linux timekeeping keeps a base plus a cycle counter, converts with mult and shift, and publishes the parameters so the vDSO can answer the common clock_gettime calls entirely in user space. The seqcount guarantees consistency. The hardware counter remains the source of truth; user space merely performs the final arithmetic.