ktime accessors

Device drivers can read the current time usingktime_get() and the manyrelated functions declared in linux/timekeeping.h. As a rule of thumb,using an accessor with a shorter name is preferred over one with a longername if both are equally fit for a particular use case.

Basic ktime_t based interfaces

The recommended simplest form returns an opaque ktime_t, with variantsthat return time for different clock references:

ktime_tktime_get(void)

CLOCK_MONOTONIC

Useful for reliable timestamps and measuring short time intervalsaccurately. Starts at system boot time but stops during suspend.

ktime_tktime_get_boottime(void)

CLOCK_BOOTTIME

Likektime_get(), but does not stop when suspended. This can beused e.g. for key expiration times that need to be synchronizedwith other machines across a suspend operation.

ktime_tktime_get_real(void)

CLOCK_REALTIME

Returns the time in relative to the UNIX epoch starting in 1970using the Coordinated Universal Time (UTC), same asgettimeofday()user space. This is used for all timestamps that need topersist across a reboot, like inode times, but should be avoidedfor internal uses, since it can jump backwards due to a leapsecond update, NTP adjustmentsettimeofday() operation from userspace.

ktime_tktime_get_clocktai(void)

CLOCK_TAI

Likektime_get_real(), but uses the International Atomic Time (TAI)reference instead of UTC to avoid jumping on leap second updates.This is rarely useful in the kernel.

ktime_tktime_get_raw(void)

CLOCK_MONOTONIC_RAW

Likektime_get(), but runs at the same rate as the hardwareclocksource without (NTP) adjustments for clock drift. This isalso rarely needed in the kernel.

nanosecond, timespec64, and second output

For all of the above, there are variants that return the time in adifferent format depending on what is required by the user:

u64ktime_get_ns(void)
u64ktime_get_boottime_ns(void)
u64ktime_get_real_ns(void)
u64ktime_get_clocktai_ns(void)
u64ktime_get_raw_ns(void)

Same as the plain ktime_get functions, but returning a u64 numberof nanoseconds in the respective time reference, which may bemore convenient for some callers.

voidktime_get_ts64(structtimespec64*)
voidktime_get_boottime_ts64(structtimespec64*)
voidktime_get_real_ts64(structtimespec64*)
voidktime_get_clocktai_ts64(structtimespec64*)
voidktime_get_raw_ts64(structtimespec64*)

Same above, but returns the time in a ‘structtimespec64’, splitinto seconds and nanoseconds. This can avoid an extra divisionwhen printing the time, or when passing it into an externalinterface that expects a ‘timespec’ or ‘timeval’ structure.

time64_tktime_get_seconds(void)
time64_tktime_get_boottime_seconds(void)
time64_tktime_get_real_seconds(void)
time64_tktime_get_clocktai_seconds(void)
time64_tktime_get_raw_seconds(void)

Return a coarse-grained version of the time as a scalartime64_t. This avoids accessing the clock hardware and roundsdown the seconds to the full seconds of the last timer tickusing the respective reference.

Coarse and fast_ns access

Some additional variants exist for more specialized cases:

ktime_tktime_get_coarse(void)
ktime_tktime_get_coarse_boottime(void)
ktime_tktime_get_coarse_real(void)
ktime_tktime_get_coarse_clocktai(void)
u64ktime_get_coarse_ns(void)
u64ktime_get_coarse_boottime_ns(void)
u64ktime_get_coarse_real_ns(void)
u64ktime_get_coarse_clocktai_ns(void)
voidktime_get_coarse_ts64(structtimespec64*)
voidktime_get_coarse_boottime_ts64(structtimespec64*)
voidktime_get_coarse_real_ts64(structtimespec64*)
voidktime_get_coarse_clocktai_ts64(structtimespec64*)

These are quicker than the non-coarse versions, but less accurate,corresponding to CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSEin user space, along with the equivalent boottime/tai/rawtimebase not available in user space.

The time returned here corresponds to the last timer tick, whichmay be as much as 10ms in the past (for CONFIG_HZ=100), same asreading the ‘jiffies’ variable. These are only useful when calledin a fast path and one still expects better than second accuracy,but can’t easily use ‘jiffies’, e.g. for inode timestamps.Skipping the hardware clock access saves around 100 CPU cycleson most modern machines with a reliable cycle counter, butup to several microseconds on older hardware with an externalclocksource.

u64ktime_get_mono_fast_ns(void)
u64ktime_get_raw_fast_ns(void)
u64ktime_get_boot_fast_ns(void)
u64ktime_get_tai_fast_ns(void)
u64ktime_get_real_fast_ns(void)

These variants are safe to call from any context, including froma non-maskable interrupt (NMI) during a timekeeper update, andwhile we are entering suspend with the clocksource powered down.This is useful in some tracing or debugging code as well asmachine check reporting, but most drivers should never call them,since the time is allowed to jump under certain conditions.

Deprecated time interfaces

Older kernels used some other interfaces that are now being phased outbut may appear in third-party drivers being ported here. In particular,all interfaces returning a ‘structtimeval’ or ‘structtimespec’ havebeen replaced because the tv_sec member overflows in year 2038 on 32-bitarchitectures. These are the recommended replacements:

voidktime_get_ts(structtimespec*)

Usektime_get() orktime_get_ts64() instead.

voiddo_gettimeofday(structtimeval*)
voidgetnstimeofday(structtimespec*)
voidgetnstimeofday64(structtimespec64*)
voidktime_get_real_ts(structtimespec*)

ktime_get_real_ts64() is a direct replacement, but consider usingmonotonic time (ktime_get_ts64()) and/or a ktime_t based interface(ktime_get()/ktime_get_real()).

structtimespeccurrent_kernel_time(void)
structtimespec64current_kernel_time64(void)
structtimespecget_monotonic_coarse(void)
structtimespec64get_monotonic_coarse64(void)

These are replaced byktime_get_coarse_real_ts64() andktime_get_coarse_ts64(). However, A lot of code that wantscoarse-grained times can use the simple ‘jiffies’ instead, whilesome drivers may actually want the higher resolution accessorsthese days.

structtimespecgetrawmonotonic(void)
structtimespec64getrawmonotonic64(void)
structtimespectimekeeping_clocktai(void)
structtimespec64timekeeping_clocktai64(void)
structtimespecget_monotonic_boottime(void)
structtimespec64get_monotonic_boottime64(void)

These are replaced byktime_get_raw()/ktime_get_raw_ts64(),ktime_get_clocktai()/ktime_get_clocktai_ts64() as wellasktime_get_boottime()/ktime_get_boottime_ts64().However, if the particular choice of clock source is notimportant for the user, consider converting toktime_get()/ktime_get_ts64() instead for consistency.