Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v6.17-rc2 199 lines 6.9 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * You SHOULD NOT be including this unless you're vsyscall 4 * handling code or timekeeping internal code! 5 */ 6 7#ifndef _LINUX_TIMEKEEPER_INTERNAL_H 8#define _LINUX_TIMEKEEPER_INTERNAL_H 9 10#include <linux/clocksource.h> 11#include <linux/jiffies.h> 12#include <linux/time.h> 13 14/** 15 * timekeeper_ids - IDs for various time keepers in the kernel 16 * @TIMEKEEPER_CORE: The central core timekeeper managing system time 17 * @TIMEKEEPER_AUX_FIRST: The first AUX timekeeper 18 * @TIMEKEEPER_AUX_LAST: The last AUX timekeeper 19 * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed 20 */ 21enum timekeeper_ids { 22 TIMEKEEPER_CORE, 23#ifdef CONFIG_POSIX_AUX_CLOCKS 24 TIMEKEEPER_AUX_FIRST, 25 TIMEKEEPER_AUX_LAST = TIMEKEEPER_AUX_FIRST + MAX_AUX_CLOCKS - 1, 26#endif 27 TIMEKEEPERS_MAX, 28}; 29 30/** 31 * struct tk_read_base - base structure for timekeeping readout 32 * @clock: Current clocksource used for timekeeping. 33 * @mask: Bitmask for two's complement subtraction of non 64bit clocks 34 * @cycle_last: @clock cycle value at last update 35 * @mult: (NTP adjusted) multiplier for scaled math conversion 36 * @shift: Shift value for scaled math conversion 37 * @xtime_nsec: Shifted (fractional) nano seconds offset for readout 38 * @base: ktime_t (nanoseconds) base time for readout 39 * @base_real: Nanoseconds base value for clock REALTIME readout 40 * 41 * This struct has size 56 byte on 64 bit. Together with a seqcount it 42 * occupies a single 64byte cache line. 43 * 44 * The struct is separate from struct timekeeper as it is also used 45 * for the fast NMI safe accessors. 46 * 47 * @base_real is for the fast NMI safe accessor to allow reading clock 48 * realtime from any context. 49 */ 50struct tk_read_base { 51 struct clocksource *clock; 52 u64 mask; 53 u64 cycle_last; 54 u32 mult; 55 u32 shift; 56 u64 xtime_nsec; 57 ktime_t base; 58 u64 base_real; 59}; 60 61/** 62 * struct timekeeper - Structure holding internal timekeeping values. 63 * @tkr_mono: The readout base structure for CLOCK_MONOTONIC 64 * @xtime_sec: Current CLOCK_REALTIME time in seconds 65 * @ktime_sec: Current CLOCK_MONOTONIC time in seconds 66 * @wall_to_monotonic: CLOCK_REALTIME to CLOCK_MONOTONIC offset 67 * @offs_real: Offset clock monotonic -> clock realtime 68 * @offs_boot: Offset clock monotonic -> clock boottime 69 * @offs_tai: Offset clock monotonic -> clock tai 70 * @offs_aux: Offset clock monotonic -> clock AUX 71 * @coarse_nsec: The nanoseconds part for coarse time getters 72 * @id: The timekeeper ID 73 * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW 74 * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds 75 * @clock_was_set_seq: The sequence number of clock was set events 76 * @cs_was_changed_seq: The sequence number of clocksource change events 77 * @clock_valid: Indicator for valid clock 78 * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset 79 * @cycle_interval: Number of clock cycles in one NTP interval 80 * @xtime_interval: Number of clock shifted nano seconds in one NTP 81 * interval. 82 * @xtime_remainder: Shifted nano seconds left over when rounding 83 * @cycle_interval 84 * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. 85 * @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second 86 * @ntp_tick: The ntp_tick_length() value currently being 87 * used. This cached copy ensures we consistently 88 * apply the tick length for an entire tick, as 89 * ntp_tick_length may change mid-tick, and we don't 90 * want to apply that new value to the tick in 91 * progress. 92 * @ntp_error: Difference between accumulated time and NTP time in ntp 93 * shifted nano seconds. 94 * @ntp_error_shift: Shift conversion between clock shifted nano seconds and 95 * ntp shifted nano seconds. 96 * @ntp_err_mult: Multiplication factor for scaled math conversion 97 * @skip_second_overflow: Flag used to avoid updating NTP twice with same second 98 * @tai_offset: The current UTC to TAI offset in seconds 99 * 100 * Note: For timespec(64) based interfaces wall_to_monotonic is what 101 * we need to add to xtime (or xtime corrected for sub jiffy times) 102 * to get to monotonic time. Monotonic is pegged at zero at system 103 * boot time, so wall_to_monotonic will be negative, however, we will 104 * ALWAYS keep the tv_nsec part positive so we can use the usual 105 * normalization. 106 * 107 * wall_to_monotonic is moved after resume from suspend for the 108 * monotonic time not to jump. We need to add total_sleep_time to 109 * wall_to_monotonic to get the real boot based time offset. 110 * 111 * wall_to_monotonic is no longer the boot time, getboottime must be 112 * used instead. 113 * 114 * @monotonic_to_boottime is a timespec64 representation of @offs_boot to 115 * accelerate the VDSO update for CLOCK_BOOTTIME. 116 * 117 * @offs_aux is used by the auxiliary timekeepers which do not utilize any 118 * of the regular timekeeper offset fields. 119 * 120 * The cacheline ordering of the structure is optimized for in kernel usage of 121 * the ktime_get() and ktime_get_ts64() family of time accessors. Struct 122 * timekeeper is prepended in the core timekeeping code with a sequence count, 123 * which results in the following cacheline layout: 124 * 125 * 0: seqcount, tkr_mono 126 * 1: xtime_sec ... id 127 * 2: tkr_raw, raw_sec 128 * 3,4: Internal variables 129 * 130 * Cacheline 0,1 contain the data which is used for accessing 131 * CLOCK_MONOTONIC/REALTIME/BOOTTIME/TAI, while cacheline 2 contains the 132 * data for accessing CLOCK_MONOTONIC_RAW. Cacheline 3,4 are internal 133 * variables which are only accessed during timekeeper updates once per 134 * tick. 135 */ 136struct timekeeper { 137 /* Cacheline 0 (together with prepended seqcount of timekeeper core): */ 138 struct tk_read_base tkr_mono; 139 140 /* Cacheline 1: */ 141 u64 xtime_sec; 142 unsigned long ktime_sec; 143 struct timespec64 wall_to_monotonic; 144 ktime_t offs_real; 145 ktime_t offs_boot; 146 union { 147 ktime_t offs_tai; 148 ktime_t offs_aux; 149 }; 150 u32 coarse_nsec; 151 enum timekeeper_ids id; 152 153 /* Cacheline 2: */ 154 struct tk_read_base tkr_raw; 155 u64 raw_sec; 156 157 /* Cachline 3 and 4 (timekeeping internal variables): */ 158 unsigned int clock_was_set_seq; 159 u8 cs_was_changed_seq; 160 u8 clock_valid; 161 162 struct timespec64 monotonic_to_boot; 163 164 u64 cycle_interval; 165 u64 xtime_interval; 166 s64 xtime_remainder; 167 u64 raw_interval; 168 169 ktime_t next_leap_ktime; 170 u64 ntp_tick; 171 s64 ntp_error; 172 u32 ntp_error_shift; 173 u32 ntp_err_mult; 174 u32 skip_second_overflow; 175 s32 tai_offset; 176}; 177 178#ifdef CONFIG_GENERIC_TIME_VSYSCALL 179 180extern void update_vsyscall(struct timekeeper *tk); 181extern void update_vsyscall_tz(void); 182 183#else 184 185static inline void update_vsyscall(struct timekeeper *tk) 186{ 187} 188static inline void update_vsyscall_tz(void) 189{ 190} 191#endif 192 193#if defined(CONFIG_GENERIC_GETTIMEOFDAY) && defined(CONFIG_POSIX_AUX_CLOCKS) 194extern void vdso_time_update_aux(struct timekeeper *tk); 195#else 196static inline void vdso_time_update_aux(struct timekeeper *tk) { } 197#endif 198 199#endif /* _LINUX_TIMEKEEPER_INTERNAL_H */