timekeeping.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  1. /*
  2. * linux/kernel/time/timekeeping.c
  3. *
  4. * Kernel timekeeping code and accessor functions
  5. *
  6. * This code was moved from linux/kernel/timer.c.
  7. * Please see that file for copyright and history logs.
  8. *
  9. */
  10. #include <linux/timekeeper_internal.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/percpu.h>
  14. #include <linux/init.h>
  15. #include <linux/mm.h>
  16. #include <linux/nmi.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/loadavg.h>
  19. #include <linux/sched/clock.h>
  20. #include <linux/syscore_ops.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/time.h>
  24. #include <linux/tick.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/pvclock_gtod.h>
  27. #include <linux/compiler.h>
  28. #include "tick-internal.h"
  29. #include "ntp_internal.h"
  30. #include "timekeeping_internal.h"
  31. #define TK_CLEAR_NTP (1 << 0)
  32. #define TK_MIRROR (1 << 1)
  33. #define TK_CLOCK_WAS_SET (1 << 2)
  34. enum timekeeping_adv_mode {
  35. /* Update timekeeper when a tick has passed */
  36. TK_ADV_TICK,
  37. /* Update timekeeper on a direct frequency change */
  38. TK_ADV_FREQ
  39. };
  40. /*
  41. * The most important data for readout fits into a single 64 byte
  42. * cache line.
  43. */
  44. static struct {
  45. seqcount_t seq;
  46. struct timekeeper timekeeper;
  47. } tk_core ____cacheline_aligned = {
  48. .seq = SEQCNT_ZERO(tk_core.seq),
  49. };
  50. static DEFINE_RAW_SPINLOCK(timekeeper_lock);
  51. static struct timekeeper shadow_timekeeper;
  52. /**
  53. * struct tk_fast - NMI safe timekeeper
  54. * @seq: Sequence counter for protecting updates. The lowest bit
  55. * is the index for the tk_read_base array
  56. * @base: tk_read_base array. Access is indexed by the lowest bit of
  57. * @seq.
  58. *
  59. * See @update_fast_timekeeper() below.
  60. */
  61. struct tk_fast {
  62. seqcount_t seq;
  63. struct tk_read_base base[2];
  64. };
  65. /* Suspend-time cycles value for halted fast timekeeper. */
  66. static u64 cycles_at_suspend;
  67. static u64 dummy_clock_read(struct clocksource *cs)
  68. {
  69. return cycles_at_suspend;
  70. }
  71. static struct clocksource dummy_clock = {
  72. .read = dummy_clock_read,
  73. };
  74. static struct tk_fast tk_fast_mono ____cacheline_aligned = {
  75. .base[0] = { .clock = &dummy_clock, },
  76. .base[1] = { .clock = &dummy_clock, },
  77. };
  78. static struct tk_fast tk_fast_raw ____cacheline_aligned = {
  79. .base[0] = { .clock = &dummy_clock, },
  80. .base[1] = { .clock = &dummy_clock, },
  81. };
  82. /* flag for if timekeeping is suspended */
  83. int __read_mostly timekeeping_suspended;
  84. static inline void tk_normalize_xtime(struct timekeeper *tk)
  85. {
  86. while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
  87. tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
  88. tk->xtime_sec++;
  89. }
  90. while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
  91. tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
  92. tk->raw_sec++;
  93. }
  94. }
  95. static inline struct timespec64 tk_xtime(const struct timekeeper *tk)
  96. {
  97. struct timespec64 ts;
  98. ts.tv_sec = tk->xtime_sec;
  99. ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
  100. return ts;
  101. }
  102. static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
  103. {
  104. tk->xtime_sec = ts->tv_sec;
  105. tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
  106. }
  107. static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
  108. {
  109. tk->xtime_sec += ts->tv_sec;
  110. tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
  111. tk_normalize_xtime(tk);
  112. }
  113. static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
  114. {
  115. struct timespec64 tmp;
  116. /*
  117. * Verify consistency of: offset_real = -wall_to_monotonic
  118. * before modifying anything
  119. */
  120. set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
  121. -tk->wall_to_monotonic.tv_nsec);
  122. WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
  123. tk->wall_to_monotonic = wtm;
  124. set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
  125. tk->offs_real = timespec64_to_ktime(tmp);
  126. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
  127. }
  128. static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
  129. {
  130. tk->offs_boot = ktime_add(tk->offs_boot, delta);
  131. }
  132. /*
  133. * tk_clock_read - atomic clocksource read() helper
  134. *
  135. * This helper is necessary to use in the read paths because, while the
  136. * seqlock ensures we don't return a bad value while structures are updated,
  137. * it doesn't protect from potential crashes. There is the possibility that
  138. * the tkr's clocksource may change between the read reference, and the
  139. * clock reference passed to the read function. This can cause crashes if
  140. * the wrong clocksource is passed to the wrong read function.
  141. * This isn't necessary to use when holding the timekeeper_lock or doing
  142. * a read of the fast-timekeeper tkrs (which is protected by its own locking
  143. * and update logic).
  144. */
  145. static inline u64 tk_clock_read(const struct tk_read_base *tkr)
  146. {
  147. struct clocksource *clock = READ_ONCE(tkr->clock);
  148. return clock->read(clock);
  149. }
  150. #ifdef CONFIG_DEBUG_TIMEKEEPING
  151. #define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
  152. static void timekeeping_check_update(struct timekeeper *tk, u64 offset)
  153. {
  154. u64 max_cycles = tk->tkr_mono.clock->max_cycles;
  155. const char *name = tk->tkr_mono.clock->name;
  156. if (offset > max_cycles) {
  157. printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n",
  158. offset, name, max_cycles);
  159. printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
  160. } else {
  161. if (offset > (max_cycles >> 1)) {
  162. printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the '%s' clock's 50%% safety margin (%lld)\n",
  163. offset, name, max_cycles >> 1);
  164. printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
  165. }
  166. }
  167. if (tk->underflow_seen) {
  168. if (jiffies - tk->last_warning > WARNING_FREQ) {
  169. printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
  170. printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
  171. printk_deferred(" Your kernel is probably still fine.\n");
  172. tk->last_warning = jiffies;
  173. }
  174. tk->underflow_seen = 0;
  175. }
  176. if (tk->overflow_seen) {
  177. if (jiffies - tk->last_warning > WARNING_FREQ) {
  178. printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
  179. printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
  180. printk_deferred(" Your kernel is probably still fine.\n");
  181. tk->last_warning = jiffies;
  182. }
  183. tk->overflow_seen = 0;
  184. }
  185. }
  186. static inline u64 timekeeping_get_delta(const struct tk_read_base *tkr)
  187. {
  188. struct timekeeper *tk = &tk_core.timekeeper;
  189. u64 now, last, mask, max, delta;
  190. unsigned int seq;
  191. /*
  192. * Since we're called holding a seqlock, the data may shift
  193. * under us while we're doing the calculation. This can cause
  194. * false positives, since we'd note a problem but throw the
  195. * results away. So nest another seqlock here to atomically
  196. * grab the points we are checking with.
  197. */
  198. do {
  199. seq = read_seqcount_begin(&tk_core.seq);
  200. now = tk_clock_read(tkr);
  201. last = tkr->cycle_last;
  202. mask = tkr->mask;
  203. max = tkr->clock->max_cycles;
  204. } while (read_seqcount_retry(&tk_core.seq, seq));
  205. delta = clocksource_delta(now, last, mask);
  206. /*
  207. * Try to catch underflows by checking if we are seeing small
  208. * mask-relative negative values.
  209. */
  210. if (unlikely((~delta & mask) < (mask >> 3))) {
  211. tk->underflow_seen = 1;
  212. delta = 0;
  213. }
  214. /* Cap delta value to the max_cycles values to avoid mult overflows */
  215. if (unlikely(delta > max)) {
  216. tk->overflow_seen = 1;
  217. delta = tkr->clock->max_cycles;
  218. }
  219. return delta;
  220. }
  221. #else
  222. static inline void timekeeping_check_update(struct timekeeper *tk, u64 offset)
  223. {
  224. }
  225. static inline u64 timekeeping_get_delta(const struct tk_read_base *tkr)
  226. {
  227. u64 cycle_now, delta;
  228. /* read clocksource */
  229. cycle_now = tk_clock_read(tkr);
  230. /* calculate the delta since the last update_wall_time */
  231. delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
  232. return delta;
  233. }
  234. #endif
  235. /**
  236. * tk_setup_internals - Set up internals to use clocksource clock.
  237. *
  238. * @tk: The target timekeeper to setup.
  239. * @clock: Pointer to clocksource.
  240. *
  241. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  242. * pair and interval request.
  243. *
  244. * Unless you're the timekeeping code, you should not be using this!
  245. */
  246. static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
  247. {
  248. u64 interval;
  249. u64 tmp, ntpinterval;
  250. struct clocksource *old_clock;
  251. ++tk->cs_was_changed_seq;
  252. old_clock = tk->tkr_mono.clock;
  253. tk->tkr_mono.clock = clock;
  254. tk->tkr_mono.mask = clock->mask;
  255. tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
  256. tk->tkr_raw.clock = clock;
  257. tk->tkr_raw.mask = clock->mask;
  258. tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
  259. /* Do the ns -> cycle conversion first, using original mult */
  260. tmp = NTP_INTERVAL_LENGTH;
  261. tmp <<= clock->shift;
  262. ntpinterval = tmp;
  263. tmp += clock->mult/2;
  264. do_div(tmp, clock->mult);
  265. if (tmp == 0)
  266. tmp = 1;
  267. interval = (u64) tmp;
  268. tk->cycle_interval = interval;
  269. /* Go back from cycles -> shifted ns */
  270. tk->xtime_interval = interval * clock->mult;
  271. tk->xtime_remainder = ntpinterval - tk->xtime_interval;
  272. tk->raw_interval = interval * clock->mult;
  273. /* if changing clocks, convert xtime_nsec shift units */
  274. if (old_clock) {
  275. int shift_change = clock->shift - old_clock->shift;
  276. if (shift_change < 0) {
  277. tk->tkr_mono.xtime_nsec >>= -shift_change;
  278. tk->tkr_raw.xtime_nsec >>= -shift_change;
  279. } else {
  280. tk->tkr_mono.xtime_nsec <<= shift_change;
  281. tk->tkr_raw.xtime_nsec <<= shift_change;
  282. }
  283. }
  284. tk->tkr_mono.shift = clock->shift;
  285. tk->tkr_raw.shift = clock->shift;
  286. tk->ntp_error = 0;
  287. tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  288. tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
  289. /*
  290. * The timekeeper keeps its own mult values for the currently
  291. * active clocksource. These value will be adjusted via NTP
  292. * to counteract clock drifting.
  293. */
  294. tk->tkr_mono.mult = clock->mult;
  295. tk->tkr_raw.mult = clock->mult;
  296. tk->ntp_err_mult = 0;
  297. tk->skip_second_overflow = 0;
  298. }
  299. /* Timekeeper helper functions. */
  300. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  301. static u32 default_arch_gettimeoffset(void) { return 0; }
  302. u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
  303. #else
  304. static inline u32 arch_gettimeoffset(void) { return 0; }
  305. #endif
  306. static inline u64 timekeeping_delta_to_ns(const struct tk_read_base *tkr, u64 delta)
  307. {
  308. u64 nsec;
  309. nsec = delta * tkr->mult + tkr->xtime_nsec;
  310. nsec >>= tkr->shift;
  311. /* If arch requires, add in get_arch_timeoffset() */
  312. return nsec + arch_gettimeoffset();
  313. }
  314. static inline u64 timekeeping_get_ns(const struct tk_read_base *tkr)
  315. {
  316. u64 delta;
  317. delta = timekeeping_get_delta(tkr);
  318. return timekeeping_delta_to_ns(tkr, delta);
  319. }
  320. static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles)
  321. {
  322. u64 delta;
  323. /* calculate the delta since the last update_wall_time */
  324. delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
  325. return timekeeping_delta_to_ns(tkr, delta);
  326. }
  327. /**
  328. * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
  329. * @tkr: Timekeeping readout base from which we take the update
  330. *
  331. * We want to use this from any context including NMI and tracing /
  332. * instrumenting the timekeeping code itself.
  333. *
  334. * Employ the latch technique; see @raw_write_seqcount_latch.
  335. *
  336. * So if a NMI hits the update of base[0] then it will use base[1]
  337. * which is still consistent. In the worst case this can result is a
  338. * slightly wrong timestamp (a few nanoseconds). See
  339. * @ktime_get_mono_fast_ns.
  340. */
  341. static void update_fast_timekeeper(const struct tk_read_base *tkr,
  342. struct tk_fast *tkf)
  343. {
  344. struct tk_read_base *base = tkf->base;
  345. /* Force readers off to base[1] */
  346. raw_write_seqcount_latch(&tkf->seq);
  347. /* Update base[0] */
  348. memcpy(base, tkr, sizeof(*base));
  349. /* Force readers back to base[0] */
  350. raw_write_seqcount_latch(&tkf->seq);
  351. /* Update base[1] */
  352. memcpy(base + 1, base, sizeof(*base));
  353. }
  354. /**
  355. * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
  356. *
  357. * This timestamp is not guaranteed to be monotonic across an update.
  358. * The timestamp is calculated by:
  359. *
  360. * now = base_mono + clock_delta * slope
  361. *
  362. * So if the update lowers the slope, readers who are forced to the
  363. * not yet updated second array are still using the old steeper slope.
  364. *
  365. * tmono
  366. * ^
  367. * | o n
  368. * | o n
  369. * | u
  370. * | o
  371. * |o
  372. * |12345678---> reader order
  373. *
  374. * o = old slope
  375. * u = update
  376. * n = new slope
  377. *
  378. * So reader 6 will observe time going backwards versus reader 5.
  379. *
  380. * While other CPUs are likely to be able observe that, the only way
  381. * for a CPU local observation is when an NMI hits in the middle of
  382. * the update. Timestamps taken from that NMI context might be ahead
  383. * of the following timestamps. Callers need to be aware of that and
  384. * deal with it.
  385. */
  386. static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
  387. {
  388. struct tk_read_base *tkr;
  389. unsigned int seq;
  390. u64 now;
  391. do {
  392. seq = raw_read_seqcount_latch(&tkf->seq);
  393. tkr = tkf->base + (seq & 0x01);
  394. now = ktime_to_ns(tkr->base);
  395. now += timekeeping_delta_to_ns(tkr,
  396. clocksource_delta(
  397. tk_clock_read(tkr),
  398. tkr->cycle_last,
  399. tkr->mask));
  400. } while (read_seqcount_retry(&tkf->seq, seq));
  401. return now;
  402. }
  403. u64 ktime_get_mono_fast_ns(void)
  404. {
  405. return __ktime_get_fast_ns(&tk_fast_mono);
  406. }
  407. EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
  408. u64 ktime_get_raw_fast_ns(void)
  409. {
  410. return __ktime_get_fast_ns(&tk_fast_raw);
  411. }
  412. EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
  413. /**
  414. * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
  415. *
  416. * To keep it NMI safe since we're accessing from tracing, we're not using a
  417. * separate timekeeper with updates to monotonic clock and boot offset
  418. * protected with seqlocks. This has the following minor side effects:
  419. *
  420. * (1) Its possible that a timestamp be taken after the boot offset is updated
  421. * but before the timekeeper is updated. If this happens, the new boot offset
  422. * is added to the old timekeeping making the clock appear to update slightly
  423. * earlier:
  424. * CPU 0 CPU 1
  425. * timekeeping_inject_sleeptime64()
  426. * __timekeeping_inject_sleeptime(tk, delta);
  427. * timestamp();
  428. * timekeeping_update(tk, TK_CLEAR_NTP...);
  429. *
  430. * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
  431. * partially updated. Since the tk->offs_boot update is a rare event, this
  432. * should be a rare occurrence which postprocessing should be able to handle.
  433. */
  434. u64 notrace ktime_get_boot_fast_ns(void)
  435. {
  436. struct timekeeper *tk = &tk_core.timekeeper;
  437. return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
  438. }
  439. EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
  440. /*
  441. * See comment for __ktime_get_fast_ns() vs. timestamp ordering
  442. */
  443. static __always_inline u64 __ktime_get_real_fast_ns(struct tk_fast *tkf)
  444. {
  445. struct tk_read_base *tkr;
  446. unsigned int seq;
  447. u64 now;
  448. do {
  449. seq = raw_read_seqcount_latch(&tkf->seq);
  450. tkr = tkf->base + (seq & 0x01);
  451. now = ktime_to_ns(tkr->base_real);
  452. now += timekeeping_delta_to_ns(tkr,
  453. clocksource_delta(
  454. tk_clock_read(tkr),
  455. tkr->cycle_last,
  456. tkr->mask));
  457. } while (read_seqcount_retry(&tkf->seq, seq));
  458. return now;
  459. }
  460. /**
  461. * ktime_get_real_fast_ns: - NMI safe and fast access to clock realtime.
  462. */
  463. u64 ktime_get_real_fast_ns(void)
  464. {
  465. return __ktime_get_real_fast_ns(&tk_fast_mono);
  466. }
  467. EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns);
  468. /**
  469. * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
  470. * @tk: Timekeeper to snapshot.
  471. *
  472. * It generally is unsafe to access the clocksource after timekeeping has been
  473. * suspended, so take a snapshot of the readout base of @tk and use it as the
  474. * fast timekeeper's readout base while suspended. It will return the same
  475. * number of cycles every time until timekeeping is resumed at which time the
  476. * proper readout base for the fast timekeeper will be restored automatically.
  477. */
  478. static void halt_fast_timekeeper(const struct timekeeper *tk)
  479. {
  480. static struct tk_read_base tkr_dummy;
  481. const struct tk_read_base *tkr = &tk->tkr_mono;
  482. memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
  483. cycles_at_suspend = tk_clock_read(tkr);
  484. tkr_dummy.clock = &dummy_clock;
  485. tkr_dummy.base_real = tkr->base + tk->offs_real;
  486. update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
  487. tkr = &tk->tkr_raw;
  488. memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
  489. tkr_dummy.clock = &dummy_clock;
  490. update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
  491. }
  492. static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
  493. static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
  494. {
  495. raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
  496. }
  497. /**
  498. * pvclock_gtod_register_notifier - register a pvclock timedata update listener
  499. */
  500. int pvclock_gtod_register_notifier(struct notifier_block *nb)
  501. {
  502. struct timekeeper *tk = &tk_core.timekeeper;
  503. unsigned long flags;
  504. int ret;
  505. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  506. ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
  507. update_pvclock_gtod(tk, true);
  508. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  509. return ret;
  510. }
  511. EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
  512. /**
  513. * pvclock_gtod_unregister_notifier - unregister a pvclock
  514. * timedata update listener
  515. */
  516. int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
  517. {
  518. unsigned long flags;
  519. int ret;
  520. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  521. ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
  522. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  523. return ret;
  524. }
  525. EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
  526. /*
  527. * tk_update_leap_state - helper to update the next_leap_ktime
  528. */
  529. static inline void tk_update_leap_state(struct timekeeper *tk)
  530. {
  531. tk->next_leap_ktime = ntp_get_next_leap();
  532. if (tk->next_leap_ktime != KTIME_MAX)
  533. /* Convert to monotonic time */
  534. tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
  535. }
  536. /*
  537. * Update the ktime_t based scalar nsec members of the timekeeper
  538. */
  539. static inline void tk_update_ktime_data(struct timekeeper *tk)
  540. {
  541. u64 seconds;
  542. u32 nsec;
  543. /*
  544. * The xtime based monotonic readout is:
  545. * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
  546. * The ktime based monotonic readout is:
  547. * nsec = base_mono + now();
  548. * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
  549. */
  550. seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
  551. nsec = (u32) tk->wall_to_monotonic.tv_nsec;
  552. tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
  553. /*
  554. * The sum of the nanoseconds portions of xtime and
  555. * wall_to_monotonic can be greater/equal one second. Take
  556. * this into account before updating tk->ktime_sec.
  557. */
  558. nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
  559. if (nsec >= NSEC_PER_SEC)
  560. seconds++;
  561. tk->ktime_sec = seconds;
  562. /* Update the monotonic raw base */
  563. tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
  564. }
  565. /* must hold timekeeper_lock */
  566. static void timekeeping_update(struct timekeeper *tk, unsigned int action)
  567. {
  568. if (action & TK_CLEAR_NTP) {
  569. tk->ntp_error = 0;
  570. ntp_clear();
  571. }
  572. tk_update_leap_state(tk);
  573. tk_update_ktime_data(tk);
  574. update_vsyscall(tk);
  575. update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
  576. tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real;
  577. update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
  578. update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
  579. if (action & TK_CLOCK_WAS_SET)
  580. tk->clock_was_set_seq++;
  581. /*
  582. * The mirroring of the data to the shadow-timekeeper needs
  583. * to happen last here to ensure we don't over-write the
  584. * timekeeper structure on the next update with stale data
  585. */
  586. if (action & TK_MIRROR)
  587. memcpy(&shadow_timekeeper, &tk_core.timekeeper,
  588. sizeof(tk_core.timekeeper));
  589. }
  590. /**
  591. * timekeeping_forward_now - update clock to the current time
  592. *
  593. * Forward the current clock to update its state since the last call to
  594. * update_wall_time(). This is useful before significant clock changes,
  595. * as it avoids having to deal with this time offset explicitly.
  596. */
  597. static void timekeeping_forward_now(struct timekeeper *tk)
  598. {
  599. u64 cycle_now, delta;
  600. cycle_now = tk_clock_read(&tk->tkr_mono);
  601. delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
  602. tk->tkr_mono.cycle_last = cycle_now;
  603. tk->tkr_raw.cycle_last = cycle_now;
  604. tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
  605. /* If arch requires, add in get_arch_timeoffset() */
  606. tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
  607. tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
  608. /* If arch requires, add in get_arch_timeoffset() */
  609. tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
  610. tk_normalize_xtime(tk);
  611. }
  612. /**
  613. * ktime_get_real_ts64 - Returns the time of day in a timespec64.
  614. * @ts: pointer to the timespec to be set
  615. *
  616. * Returns the time of day in a timespec64 (WARN if suspended).
  617. */
  618. void ktime_get_real_ts64(struct timespec64 *ts)
  619. {
  620. struct timekeeper *tk = &tk_core.timekeeper;
  621. unsigned long seq;
  622. u64 nsecs;
  623. WARN_ON(timekeeping_suspended);
  624. do {
  625. seq = read_seqcount_begin(&tk_core.seq);
  626. ts->tv_sec = tk->xtime_sec;
  627. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  628. } while (read_seqcount_retry(&tk_core.seq, seq));
  629. ts->tv_nsec = 0;
  630. timespec64_add_ns(ts, nsecs);
  631. }
  632. EXPORT_SYMBOL(ktime_get_real_ts64);
  633. ktime_t ktime_get(void)
  634. {
  635. struct timekeeper *tk = &tk_core.timekeeper;
  636. unsigned int seq;
  637. ktime_t base;
  638. u64 nsecs;
  639. WARN_ON(timekeeping_suspended);
  640. do {
  641. seq = read_seqcount_begin(&tk_core.seq);
  642. base = tk->tkr_mono.base;
  643. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  644. } while (read_seqcount_retry(&tk_core.seq, seq));
  645. return ktime_add_ns(base, nsecs);
  646. }
  647. EXPORT_SYMBOL_GPL(ktime_get);
  648. u32 ktime_get_resolution_ns(void)
  649. {
  650. struct timekeeper *tk = &tk_core.timekeeper;
  651. unsigned int seq;
  652. u32 nsecs;
  653. WARN_ON(timekeeping_suspended);
  654. do {
  655. seq = read_seqcount_begin(&tk_core.seq);
  656. nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
  657. } while (read_seqcount_retry(&tk_core.seq, seq));
  658. return nsecs;
  659. }
  660. EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
  661. static ktime_t *offsets[TK_OFFS_MAX] = {
  662. [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
  663. [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
  664. [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
  665. };
  666. ktime_t ktime_get_with_offset(enum tk_offsets offs)
  667. {
  668. struct timekeeper *tk = &tk_core.timekeeper;
  669. unsigned int seq;
  670. ktime_t base, *offset = offsets[offs];
  671. u64 nsecs;
  672. WARN_ON(timekeeping_suspended);
  673. do {
  674. seq = read_seqcount_begin(&tk_core.seq);
  675. base = ktime_add(tk->tkr_mono.base, *offset);
  676. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  677. } while (read_seqcount_retry(&tk_core.seq, seq));
  678. return ktime_add_ns(base, nsecs);
  679. }
  680. EXPORT_SYMBOL_GPL(ktime_get_with_offset);
  681. ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
  682. {
  683. struct timekeeper *tk = &tk_core.timekeeper;
  684. unsigned int seq;
  685. ktime_t base, *offset = offsets[offs];
  686. u64 nsecs;
  687. WARN_ON(timekeeping_suspended);
  688. do {
  689. seq = read_seqcount_begin(&tk_core.seq);
  690. base = ktime_add(tk->tkr_mono.base, *offset);
  691. nsecs = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
  692. } while (read_seqcount_retry(&tk_core.seq, seq));
  693. return ktime_add_ns(base, nsecs);
  694. }
  695. EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset);
  696. /**
  697. * ktime_mono_to_any() - convert mononotic time to any other time
  698. * @tmono: time to convert.
  699. * @offs: which offset to use
  700. */
  701. ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
  702. {
  703. ktime_t *offset = offsets[offs];
  704. unsigned long seq;
  705. ktime_t tconv;
  706. do {
  707. seq = read_seqcount_begin(&tk_core.seq);
  708. tconv = ktime_add(tmono, *offset);
  709. } while (read_seqcount_retry(&tk_core.seq, seq));
  710. return tconv;
  711. }
  712. EXPORT_SYMBOL_GPL(ktime_mono_to_any);
  713. /**
  714. * ktime_get_raw - Returns the raw monotonic time in ktime_t format
  715. */
  716. ktime_t ktime_get_raw(void)
  717. {
  718. struct timekeeper *tk = &tk_core.timekeeper;
  719. unsigned int seq;
  720. ktime_t base;
  721. u64 nsecs;
  722. do {
  723. seq = read_seqcount_begin(&tk_core.seq);
  724. base = tk->tkr_raw.base;
  725. nsecs = timekeeping_get_ns(&tk->tkr_raw);
  726. } while (read_seqcount_retry(&tk_core.seq, seq));
  727. return ktime_add_ns(base, nsecs);
  728. }
  729. EXPORT_SYMBOL_GPL(ktime_get_raw);
  730. /**
  731. * ktime_get_ts64 - get the monotonic clock in timespec64 format
  732. * @ts: pointer to timespec variable
  733. *
  734. * The function calculates the monotonic clock from the realtime
  735. * clock and the wall_to_monotonic offset and stores the result
  736. * in normalized timespec64 format in the variable pointed to by @ts.
  737. */
  738. void ktime_get_ts64(struct timespec64 *ts)
  739. {
  740. struct timekeeper *tk = &tk_core.timekeeper;
  741. struct timespec64 tomono;
  742. unsigned int seq;
  743. u64 nsec;
  744. WARN_ON(timekeeping_suspended);
  745. do {
  746. seq = read_seqcount_begin(&tk_core.seq);
  747. ts->tv_sec = tk->xtime_sec;
  748. nsec = timekeeping_get_ns(&tk->tkr_mono);
  749. tomono = tk->wall_to_monotonic;
  750. } while (read_seqcount_retry(&tk_core.seq, seq));
  751. ts->tv_sec += tomono.tv_sec;
  752. ts->tv_nsec = 0;
  753. timespec64_add_ns(ts, nsec + tomono.tv_nsec);
  754. }
  755. EXPORT_SYMBOL_GPL(ktime_get_ts64);
  756. /**
  757. * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
  758. *
  759. * Returns the seconds portion of CLOCK_MONOTONIC with a single non
  760. * serialized read. tk->ktime_sec is of type 'unsigned long' so this
  761. * works on both 32 and 64 bit systems. On 32 bit systems the readout
  762. * covers ~136 years of uptime which should be enough to prevent
  763. * premature wrap arounds.
  764. */
  765. time64_t ktime_get_seconds(void)
  766. {
  767. struct timekeeper *tk = &tk_core.timekeeper;
  768. WARN_ON(timekeeping_suspended);
  769. return tk->ktime_sec;
  770. }
  771. EXPORT_SYMBOL_GPL(ktime_get_seconds);
  772. /**
  773. * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
  774. *
  775. * Returns the wall clock seconds since 1970. This replaces the
  776. * get_seconds() interface which is not y2038 safe on 32bit systems.
  777. *
  778. * For 64bit systems the fast access to tk->xtime_sec is preserved. On
  779. * 32bit systems the access must be protected with the sequence
  780. * counter to provide "atomic" access to the 64bit tk->xtime_sec
  781. * value.
  782. */
  783. time64_t ktime_get_real_seconds(void)
  784. {
  785. struct timekeeper *tk = &tk_core.timekeeper;
  786. time64_t seconds;
  787. unsigned int seq;
  788. if (IS_ENABLED(CONFIG_64BIT))
  789. return tk->xtime_sec;
  790. do {
  791. seq = read_seqcount_begin(&tk_core.seq);
  792. seconds = tk->xtime_sec;
  793. } while (read_seqcount_retry(&tk_core.seq, seq));
  794. return seconds;
  795. }
  796. EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
  797. /**
  798. * __ktime_get_real_seconds - The same as ktime_get_real_seconds
  799. * but without the sequence counter protect. This internal function
  800. * is called just when timekeeping lock is already held.
  801. */
  802. time64_t __ktime_get_real_seconds(void)
  803. {
  804. struct timekeeper *tk = &tk_core.timekeeper;
  805. return tk->xtime_sec;
  806. }
  807. /**
  808. * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
  809. * @systime_snapshot: pointer to struct receiving the system time snapshot
  810. */
  811. void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
  812. {
  813. struct timekeeper *tk = &tk_core.timekeeper;
  814. unsigned long seq;
  815. ktime_t base_raw;
  816. ktime_t base_real;
  817. u64 nsec_raw;
  818. u64 nsec_real;
  819. u64 now;
  820. WARN_ON_ONCE(timekeeping_suspended);
  821. do {
  822. seq = read_seqcount_begin(&tk_core.seq);
  823. now = tk_clock_read(&tk->tkr_mono);
  824. systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
  825. systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
  826. base_real = ktime_add(tk->tkr_mono.base,
  827. tk_core.timekeeper.offs_real);
  828. base_raw = tk->tkr_raw.base;
  829. nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
  830. nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
  831. } while (read_seqcount_retry(&tk_core.seq, seq));
  832. systime_snapshot->cycles = now;
  833. systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
  834. systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
  835. }
  836. EXPORT_SYMBOL_GPL(ktime_get_snapshot);
  837. /* Scale base by mult/div checking for overflow */
  838. static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
  839. {
  840. u64 tmp, rem;
  841. tmp = div64_u64_rem(*base, div, &rem);
  842. if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
  843. ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
  844. return -EOVERFLOW;
  845. tmp *= mult;
  846. rem = div64_u64(rem * mult, div);
  847. *base = tmp + rem;
  848. return 0;
  849. }
  850. /**
  851. * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
  852. * @history: Snapshot representing start of history
  853. * @partial_history_cycles: Cycle offset into history (fractional part)
  854. * @total_history_cycles: Total history length in cycles
  855. * @discontinuity: True indicates clock was set on history period
  856. * @ts: Cross timestamp that should be adjusted using
  857. * partial/total ratio
  858. *
  859. * Helper function used by get_device_system_crosststamp() to correct the
  860. * crosstimestamp corresponding to the start of the current interval to the
  861. * system counter value (timestamp point) provided by the driver. The
  862. * total_history_* quantities are the total history starting at the provided
  863. * reference point and ending at the start of the current interval. The cycle
  864. * count between the driver timestamp point and the start of the current
  865. * interval is partial_history_cycles.
  866. */
  867. static int adjust_historical_crosststamp(struct system_time_snapshot *history,
  868. u64 partial_history_cycles,
  869. u64 total_history_cycles,
  870. bool discontinuity,
  871. struct system_device_crosststamp *ts)
  872. {
  873. struct timekeeper *tk = &tk_core.timekeeper;
  874. u64 corr_raw, corr_real;
  875. bool interp_forward;
  876. int ret;
  877. if (total_history_cycles == 0 || partial_history_cycles == 0)
  878. return 0;
  879. /* Interpolate shortest distance from beginning or end of history */
  880. interp_forward = partial_history_cycles > total_history_cycles / 2;
  881. partial_history_cycles = interp_forward ?
  882. total_history_cycles - partial_history_cycles :
  883. partial_history_cycles;
  884. /*
  885. * Scale the monotonic raw time delta by:
  886. * partial_history_cycles / total_history_cycles
  887. */
  888. corr_raw = (u64)ktime_to_ns(
  889. ktime_sub(ts->sys_monoraw, history->raw));
  890. ret = scale64_check_overflow(partial_history_cycles,
  891. total_history_cycles, &corr_raw);
  892. if (ret)
  893. return ret;
  894. /*
  895. * If there is a discontinuity in the history, scale monotonic raw
  896. * correction by:
  897. * mult(real)/mult(raw) yielding the realtime correction
  898. * Otherwise, calculate the realtime correction similar to monotonic
  899. * raw calculation
  900. */
  901. if (discontinuity) {
  902. corr_real = mul_u64_u32_div
  903. (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
  904. } else {
  905. corr_real = (u64)ktime_to_ns(
  906. ktime_sub(ts->sys_realtime, history->real));
  907. ret = scale64_check_overflow(partial_history_cycles,
  908. total_history_cycles, &corr_real);
  909. if (ret)
  910. return ret;
  911. }
  912. /* Fixup monotonic raw and real time time values */
  913. if (interp_forward) {
  914. ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
  915. ts->sys_realtime = ktime_add_ns(history->real, corr_real);
  916. } else {
  917. ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
  918. ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
  919. }
  920. return 0;
  921. }
  922. /*
  923. * cycle_between - true if test occurs chronologically between before and after
  924. */
  925. static bool cycle_between(u64 before, u64 test, u64 after)
  926. {
  927. if (test > before && test < after)
  928. return true;
  929. if (test < before && before > after)
  930. return true;
  931. return false;
  932. }
  933. /**
  934. * get_device_system_crosststamp - Synchronously capture system/device timestamp
  935. * @get_time_fn: Callback to get simultaneous device time and
  936. * system counter from the device driver
  937. * @ctx: Context passed to get_time_fn()
  938. * @history_begin: Historical reference point used to interpolate system
  939. * time when counter provided by the driver is before the current interval
  940. * @xtstamp: Receives simultaneously captured system and device time
  941. *
  942. * Reads a timestamp from a device and correlates it to system time
  943. */
  944. int get_device_system_crosststamp(int (*get_time_fn)
  945. (ktime_t *device_time,
  946. struct system_counterval_t *sys_counterval,
  947. void *ctx),
  948. void *ctx,
  949. struct system_time_snapshot *history_begin,
  950. struct system_device_crosststamp *xtstamp)
  951. {
  952. struct system_counterval_t system_counterval;
  953. struct timekeeper *tk = &tk_core.timekeeper;
  954. u64 cycles, now, interval_start;
  955. unsigned int clock_was_set_seq = 0;
  956. ktime_t base_real, base_raw;
  957. u64 nsec_real, nsec_raw;
  958. u8 cs_was_changed_seq;
  959. unsigned long seq;
  960. bool do_interp;
  961. int ret;
  962. do {
  963. seq = read_seqcount_begin(&tk_core.seq);
  964. /*
  965. * Try to synchronously capture device time and a system
  966. * counter value calling back into the device driver
  967. */
  968. ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
  969. if (ret)
  970. return ret;
  971. /*
  972. * Verify that the clocksource associated with the captured
  973. * system counter value is the same as the currently installed
  974. * timekeeper clocksource
  975. */
  976. if (tk->tkr_mono.clock != system_counterval.cs)
  977. return -ENODEV;
  978. cycles = system_counterval.cycles;
  979. /*
  980. * Check whether the system counter value provided by the
  981. * device driver is on the current timekeeping interval.
  982. */
  983. now = tk_clock_read(&tk->tkr_mono);
  984. interval_start = tk->tkr_mono.cycle_last;
  985. if (!cycle_between(interval_start, cycles, now)) {
  986. clock_was_set_seq = tk->clock_was_set_seq;
  987. cs_was_changed_seq = tk->cs_was_changed_seq;
  988. cycles = interval_start;
  989. do_interp = true;
  990. } else {
  991. do_interp = false;
  992. }
  993. base_real = ktime_add(tk->tkr_mono.base,
  994. tk_core.timekeeper.offs_real);
  995. base_raw = tk->tkr_raw.base;
  996. nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
  997. system_counterval.cycles);
  998. nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
  999. system_counterval.cycles);
  1000. } while (read_seqcount_retry(&tk_core.seq, seq));
  1001. xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
  1002. xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
  1003. /*
  1004. * Interpolate if necessary, adjusting back from the start of the
  1005. * current interval
  1006. */
  1007. if (do_interp) {
  1008. u64 partial_history_cycles, total_history_cycles;
  1009. bool discontinuity;
  1010. /*
  1011. * Check that the counter value occurs after the provided
  1012. * history reference and that the history doesn't cross a
  1013. * clocksource change
  1014. */
  1015. if (!history_begin ||
  1016. !cycle_between(history_begin->cycles,
  1017. system_counterval.cycles, cycles) ||
  1018. history_begin->cs_was_changed_seq != cs_was_changed_seq)
  1019. return -EINVAL;
  1020. partial_history_cycles = cycles - system_counterval.cycles;
  1021. total_history_cycles = cycles - history_begin->cycles;
  1022. discontinuity =
  1023. history_begin->clock_was_set_seq != clock_was_set_seq;
  1024. ret = adjust_historical_crosststamp(history_begin,
  1025. partial_history_cycles,
  1026. total_history_cycles,
  1027. discontinuity, xtstamp);
  1028. if (ret)
  1029. return ret;
  1030. }
  1031. return 0;
  1032. }
  1033. EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
  1034. /**
  1035. * do_settimeofday64 - Sets the time of day.
  1036. * @ts: pointer to the timespec64 variable containing the new time
  1037. *
  1038. * Sets the time of day to the new time and update NTP and notify hrtimers
  1039. */
  1040. int do_settimeofday64(const struct timespec64 *ts)
  1041. {
  1042. struct timekeeper *tk = &tk_core.timekeeper;
  1043. struct timespec64 ts_delta, xt;
  1044. unsigned long flags;
  1045. int ret = 0;
  1046. if (!timespec64_valid_settod(ts))
  1047. return -EINVAL;
  1048. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1049. write_seqcount_begin(&tk_core.seq);
  1050. timekeeping_forward_now(tk);
  1051. xt = tk_xtime(tk);
  1052. ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
  1053. ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
  1054. if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
  1055. ret = -EINVAL;
  1056. goto out;
  1057. }
  1058. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
  1059. tk_set_xtime(tk, ts);
  1060. out:
  1061. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1062. write_seqcount_end(&tk_core.seq);
  1063. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1064. /* signal hrtimers about time change */
  1065. clock_was_set();
  1066. return ret;
  1067. }
  1068. EXPORT_SYMBOL(do_settimeofday64);
  1069. /**
  1070. * timekeeping_inject_offset - Adds or subtracts from the current time.
  1071. * @tv: pointer to the timespec variable containing the offset
  1072. *
  1073. * Adds or subtracts an offset value from the current time.
  1074. */
  1075. static int timekeeping_inject_offset(const struct timespec64 *ts)
  1076. {
  1077. struct timekeeper *tk = &tk_core.timekeeper;
  1078. unsigned long flags;
  1079. struct timespec64 tmp;
  1080. int ret = 0;
  1081. if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
  1082. return -EINVAL;
  1083. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1084. write_seqcount_begin(&tk_core.seq);
  1085. timekeeping_forward_now(tk);
  1086. /* Make sure the proposed value is valid */
  1087. tmp = timespec64_add(tk_xtime(tk), *ts);
  1088. if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 ||
  1089. !timespec64_valid_settod(&tmp)) {
  1090. ret = -EINVAL;
  1091. goto error;
  1092. }
  1093. tk_xtime_add(tk, ts);
  1094. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts));
  1095. error: /* even if we error out, we forwarded the time, so call update */
  1096. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1097. write_seqcount_end(&tk_core.seq);
  1098. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1099. /* signal hrtimers about time change */
  1100. clock_was_set();
  1101. return ret;
  1102. }
  1103. /*
  1104. * Indicates if there is an offset between the system clock and the hardware
  1105. * clock/persistent clock/rtc.
  1106. */
  1107. int persistent_clock_is_local;
  1108. /*
  1109. * Adjust the time obtained from the CMOS to be UTC time instead of
  1110. * local time.
  1111. *
  1112. * This is ugly, but preferable to the alternatives. Otherwise we
  1113. * would either need to write a program to do it in /etc/rc (and risk
  1114. * confusion if the program gets run more than once; it would also be
  1115. * hard to make the program warp the clock precisely n hours) or
  1116. * compile in the timezone information into the kernel. Bad, bad....
  1117. *
  1118. * - TYT, 1992-01-01
  1119. *
  1120. * The best thing to do is to keep the CMOS clock in universal time (UTC)
  1121. * as real UNIX machines always do it. This avoids all headaches about
  1122. * daylight saving times and warping kernel clocks.
  1123. */
  1124. void timekeeping_warp_clock(void)
  1125. {
  1126. if (sys_tz.tz_minuteswest != 0) {
  1127. struct timespec64 adjust;
  1128. persistent_clock_is_local = 1;
  1129. adjust.tv_sec = sys_tz.tz_minuteswest * 60;
  1130. adjust.tv_nsec = 0;
  1131. timekeeping_inject_offset(&adjust);
  1132. }
  1133. }
  1134. /**
  1135. * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic
  1136. *
  1137. */
  1138. static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
  1139. {
  1140. tk->tai_offset = tai_offset;
  1141. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
  1142. }
  1143. /**
  1144. * change_clocksource - Swaps clocksources if a new one is available
  1145. *
  1146. * Accumulates current time interval and initializes new clocksource
  1147. */
  1148. static int change_clocksource(void *data)
  1149. {
  1150. struct timekeeper *tk = &tk_core.timekeeper;
  1151. struct clocksource *new, *old;
  1152. unsigned long flags;
  1153. new = (struct clocksource *) data;
  1154. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1155. write_seqcount_begin(&tk_core.seq);
  1156. timekeeping_forward_now(tk);
  1157. /*
  1158. * If the cs is in module, get a module reference. Succeeds
  1159. * for built-in code (owner == NULL) as well.
  1160. */
  1161. if (try_module_get(new->owner)) {
  1162. if (!new->enable || new->enable(new) == 0) {
  1163. old = tk->tkr_mono.clock;
  1164. tk_setup_internals(tk, new);
  1165. if (old->disable)
  1166. old->disable(old);
  1167. module_put(old->owner);
  1168. } else {
  1169. module_put(new->owner);
  1170. }
  1171. }
  1172. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1173. write_seqcount_end(&tk_core.seq);
  1174. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1175. return 0;
  1176. }
  1177. /**
  1178. * timekeeping_notify - Install a new clock source
  1179. * @clock: pointer to the clock source
  1180. *
  1181. * This function is called from clocksource.c after a new, better clock
  1182. * source has been registered. The caller holds the clocksource_mutex.
  1183. */
  1184. int timekeeping_notify(struct clocksource *clock)
  1185. {
  1186. struct timekeeper *tk = &tk_core.timekeeper;
  1187. if (tk->tkr_mono.clock == clock)
  1188. return 0;
  1189. stop_machine(change_clocksource, clock, NULL);
  1190. tick_clock_notify();
  1191. return tk->tkr_mono.clock == clock ? 0 : -1;
  1192. }
  1193. /**
  1194. * ktime_get_raw_ts64 - Returns the raw monotonic time in a timespec
  1195. * @ts: pointer to the timespec64 to be set
  1196. *
  1197. * Returns the raw monotonic time (completely un-modified by ntp)
  1198. */
  1199. void ktime_get_raw_ts64(struct timespec64 *ts)
  1200. {
  1201. struct timekeeper *tk = &tk_core.timekeeper;
  1202. unsigned long seq;
  1203. u64 nsecs;
  1204. do {
  1205. seq = read_seqcount_begin(&tk_core.seq);
  1206. ts->tv_sec = tk->raw_sec;
  1207. nsecs = timekeeping_get_ns(&tk->tkr_raw);
  1208. } while (read_seqcount_retry(&tk_core.seq, seq));
  1209. ts->tv_nsec = 0;
  1210. timespec64_add_ns(ts, nsecs);
  1211. }
  1212. EXPORT_SYMBOL(ktime_get_raw_ts64);
  1213. /**
  1214. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  1215. */
  1216. int timekeeping_valid_for_hres(void)
  1217. {
  1218. struct timekeeper *tk = &tk_core.timekeeper;
  1219. unsigned long seq;
  1220. int ret;
  1221. do {
  1222. seq = read_seqcount_begin(&tk_core.seq);
  1223. ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  1224. } while (read_seqcount_retry(&tk_core.seq, seq));
  1225. return ret;
  1226. }
  1227. /**
  1228. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  1229. */
  1230. u64 timekeeping_max_deferment(void)
  1231. {
  1232. struct timekeeper *tk = &tk_core.timekeeper;
  1233. unsigned long seq;
  1234. u64 ret;
  1235. do {
  1236. seq = read_seqcount_begin(&tk_core.seq);
  1237. ret = tk->tkr_mono.clock->max_idle_ns;
  1238. } while (read_seqcount_retry(&tk_core.seq, seq));
  1239. return ret;
  1240. }
  1241. /**
  1242. * read_persistent_clock - Return time from the persistent clock.
  1243. *
  1244. * Weak dummy function for arches that do not yet support it.
  1245. * Reads the time from the battery backed persistent clock.
  1246. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  1247. *
  1248. * XXX - Do be sure to remove it once all arches implement it.
  1249. */
  1250. void __weak read_persistent_clock(struct timespec *ts)
  1251. {
  1252. ts->tv_sec = 0;
  1253. ts->tv_nsec = 0;
  1254. }
  1255. void __weak read_persistent_clock64(struct timespec64 *ts64)
  1256. {
  1257. struct timespec ts;
  1258. read_persistent_clock(&ts);
  1259. *ts64 = timespec_to_timespec64(ts);
  1260. }
  1261. /**
  1262. * read_persistent_wall_and_boot_offset - Read persistent clock, and also offset
  1263. * from the boot.
  1264. *
  1265. * Weak dummy function for arches that do not yet support it.
  1266. * wall_time - current time as returned by persistent clock
  1267. * boot_offset - offset that is defined as wall_time - boot_time
  1268. * The default function calculates offset based on the current value of
  1269. * local_clock(). This way architectures that support sched_clock() but don't
  1270. * support dedicated boot time clock will provide the best estimate of the
  1271. * boot time.
  1272. */
  1273. void __weak __init
  1274. read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
  1275. struct timespec64 *boot_offset)
  1276. {
  1277. read_persistent_clock64(wall_time);
  1278. *boot_offset = ns_to_timespec64(local_clock());
  1279. }
  1280. /*
  1281. * Flag reflecting whether timekeeping_resume() has injected sleeptime.
  1282. *
  1283. * The flag starts of false and is only set when a suspend reaches
  1284. * timekeeping_suspend(), timekeeping_resume() sets it to false when the
  1285. * timekeeper clocksource is not stopping across suspend and has been
  1286. * used to update sleep time. If the timekeeper clocksource has stopped
  1287. * then the flag stays true and is used by the RTC resume code to decide
  1288. * whether sleeptime must be injected and if so the flag gets false then.
  1289. *
  1290. * If a suspend fails before reaching timekeeping_resume() then the flag
  1291. * stays false and prevents erroneous sleeptime injection.
  1292. */
  1293. static bool suspend_timing_needed;
  1294. /* Flag for if there is a persistent clock on this platform */
  1295. static bool persistent_clock_exists;
  1296. /*
  1297. * timekeeping_init - Initializes the clocksource and common timekeeping values
  1298. */
  1299. void __init timekeeping_init(void)
  1300. {
  1301. struct timespec64 wall_time, boot_offset, wall_to_mono;
  1302. struct timekeeper *tk = &tk_core.timekeeper;
  1303. struct clocksource *clock;
  1304. unsigned long flags;
  1305. read_persistent_wall_and_boot_offset(&wall_time, &boot_offset);
  1306. if (timespec64_valid_settod(&wall_time) &&
  1307. timespec64_to_ns(&wall_time) > 0) {
  1308. persistent_clock_exists = true;
  1309. } else if (timespec64_to_ns(&wall_time) != 0) {
  1310. pr_warn("Persistent clock returned invalid value");
  1311. wall_time = (struct timespec64){0};
  1312. }
  1313. if (timespec64_compare(&wall_time, &boot_offset) < 0)
  1314. boot_offset = (struct timespec64){0};
  1315. /*
  1316. * We want set wall_to_mono, so the following is true:
  1317. * wall time + wall_to_mono = boot time
  1318. */
  1319. wall_to_mono = timespec64_sub(boot_offset, wall_time);
  1320. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1321. write_seqcount_begin(&tk_core.seq);
  1322. ntp_init();
  1323. clock = clocksource_default_clock();
  1324. if (clock->enable)
  1325. clock->enable(clock);
  1326. tk_setup_internals(tk, clock);
  1327. tk_set_xtime(tk, &wall_time);
  1328. tk->raw_sec = 0;
  1329. tk_set_wall_to_mono(tk, wall_to_mono);
  1330. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1331. write_seqcount_end(&tk_core.seq);
  1332. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1333. }
  1334. /* time in seconds when suspend began for persistent clock */
  1335. static struct timespec64 timekeeping_suspend_time;
  1336. /**
  1337. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  1338. * @delta: pointer to a timespec delta value
  1339. *
  1340. * Takes a timespec offset measuring a suspend interval and properly
  1341. * adds the sleep offset to the timekeeping variables.
  1342. */
  1343. static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  1344. const struct timespec64 *delta)
  1345. {
  1346. if (!timespec64_valid_strict(delta)) {
  1347. printk_deferred(KERN_WARNING
  1348. "__timekeeping_inject_sleeptime: Invalid "
  1349. "sleep delta value!\n");
  1350. return;
  1351. }
  1352. tk_xtime_add(tk, delta);
  1353. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
  1354. tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
  1355. tk_debug_account_sleep_time(delta);
  1356. }
  1357. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  1358. /**
  1359. * We have three kinds of time sources to use for sleep time
  1360. * injection, the preference order is:
  1361. * 1) non-stop clocksource
  1362. * 2) persistent clock (ie: RTC accessible when irqs are off)
  1363. * 3) RTC
  1364. *
  1365. * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
  1366. * If system has neither 1) nor 2), 3) will be used finally.
  1367. *
  1368. *
  1369. * If timekeeping has injected sleeptime via either 1) or 2),
  1370. * 3) becomes needless, so in this case we don't need to call
  1371. * rtc_resume(), and this is what timekeeping_rtc_skipresume()
  1372. * means.
  1373. */
  1374. bool timekeeping_rtc_skipresume(void)
  1375. {
  1376. return !suspend_timing_needed;
  1377. }
  1378. /**
  1379. * 1) can be determined whether to use or not only when doing
  1380. * timekeeping_resume() which is invoked after rtc_suspend(),
  1381. * so we can't skip rtc_suspend() surely if system has 1).
  1382. *
  1383. * But if system has 2), 2) will definitely be used, so in this
  1384. * case we don't need to call rtc_suspend(), and this is what
  1385. * timekeeping_rtc_skipsuspend() means.
  1386. */
  1387. bool timekeeping_rtc_skipsuspend(void)
  1388. {
  1389. return persistent_clock_exists;
  1390. }
  1391. /**
  1392. * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
  1393. * @delta: pointer to a timespec64 delta value
  1394. *
  1395. * This hook is for architectures that cannot support read_persistent_clock64
  1396. * because their RTC/persistent clock is only accessible when irqs are enabled.
  1397. * and also don't have an effective nonstop clocksource.
  1398. *
  1399. * This function should only be called by rtc_resume(), and allows
  1400. * a suspend offset to be injected into the timekeeping values.
  1401. */
  1402. void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
  1403. {
  1404. struct timekeeper *tk = &tk_core.timekeeper;
  1405. unsigned long flags;
  1406. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1407. write_seqcount_begin(&tk_core.seq);
  1408. suspend_timing_needed = false;
  1409. timekeeping_forward_now(tk);
  1410. __timekeeping_inject_sleeptime(tk, delta);
  1411. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1412. write_seqcount_end(&tk_core.seq);
  1413. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1414. /* signal hrtimers about time change */
  1415. clock_was_set();
  1416. }
  1417. #endif
  1418. /**
  1419. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  1420. */
  1421. void timekeeping_resume(void)
  1422. {
  1423. struct timekeeper *tk = &tk_core.timekeeper;
  1424. struct clocksource *clock = tk->tkr_mono.clock;
  1425. unsigned long flags;
  1426. struct timespec64 ts_new, ts_delta;
  1427. u64 cycle_now, nsec;
  1428. bool inject_sleeptime = false;
  1429. read_persistent_clock64(&ts_new);
  1430. clockevents_resume();
  1431. clocksource_resume();
  1432. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1433. write_seqcount_begin(&tk_core.seq);
  1434. /*
  1435. * After system resumes, we need to calculate the suspended time and
  1436. * compensate it for the OS time. There are 3 sources that could be
  1437. * used: Nonstop clocksource during suspend, persistent clock and rtc
  1438. * device.
  1439. *
  1440. * One specific platform may have 1 or 2 or all of them, and the
  1441. * preference will be:
  1442. * suspend-nonstop clocksource -> persistent clock -> rtc
  1443. * The less preferred source will only be tried if there is no better
  1444. * usable source. The rtc part is handled separately in rtc core code.
  1445. */
  1446. cycle_now = tk_clock_read(&tk->tkr_mono);
  1447. nsec = clocksource_stop_suspend_timing(clock, cycle_now);
  1448. if (nsec > 0) {
  1449. ts_delta = ns_to_timespec64(nsec);
  1450. inject_sleeptime = true;
  1451. } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  1452. ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
  1453. inject_sleeptime = true;
  1454. }
  1455. if (inject_sleeptime) {
  1456. suspend_timing_needed = false;
  1457. __timekeeping_inject_sleeptime(tk, &ts_delta);
  1458. }
  1459. /* Re-base the last cycle value */
  1460. tk->tkr_mono.cycle_last = cycle_now;
  1461. tk->tkr_raw.cycle_last = cycle_now;
  1462. tk->ntp_error = 0;
  1463. timekeeping_suspended = 0;
  1464. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1465. write_seqcount_end(&tk_core.seq);
  1466. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1467. touch_softlockup_watchdog();
  1468. tick_resume();
  1469. hrtimers_resume();
  1470. }
  1471. int timekeeping_suspend(void)
  1472. {
  1473. struct timekeeper *tk = &tk_core.timekeeper;
  1474. unsigned long flags;
  1475. struct timespec64 delta, delta_delta;
  1476. static struct timespec64 old_delta;
  1477. struct clocksource *curr_clock;
  1478. u64 cycle_now;
  1479. read_persistent_clock64(&timekeeping_suspend_time);
  1480. /*
  1481. * On some systems the persistent_clock can not be detected at
  1482. * timekeeping_init by its return value, so if we see a valid
  1483. * value returned, update the persistent_clock_exists flag.
  1484. */
  1485. if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
  1486. persistent_clock_exists = true;
  1487. suspend_timing_needed = true;
  1488. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1489. write_seqcount_begin(&tk_core.seq);
  1490. timekeeping_forward_now(tk);
  1491. timekeeping_suspended = 1;
  1492. /*
  1493. * Since we've called forward_now, cycle_last stores the value
  1494. * just read from the current clocksource. Save this to potentially
  1495. * use in suspend timing.
  1496. */
  1497. curr_clock = tk->tkr_mono.clock;
  1498. cycle_now = tk->tkr_mono.cycle_last;
  1499. clocksource_start_suspend_timing(curr_clock, cycle_now);
  1500. if (persistent_clock_exists) {
  1501. /*
  1502. * To avoid drift caused by repeated suspend/resumes,
  1503. * which each can add ~1 second drift error,
  1504. * try to compensate so the difference in system time
  1505. * and persistent_clock time stays close to constant.
  1506. */
  1507. delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
  1508. delta_delta = timespec64_sub(delta, old_delta);
  1509. if (abs(delta_delta.tv_sec) >= 2) {
  1510. /*
  1511. * if delta_delta is too large, assume time correction
  1512. * has occurred and set old_delta to the current delta.
  1513. */
  1514. old_delta = delta;
  1515. } else {
  1516. /* Otherwise try to adjust old_system to compensate */
  1517. timekeeping_suspend_time =
  1518. timespec64_add(timekeeping_suspend_time, delta_delta);
  1519. }
  1520. }
  1521. timekeeping_update(tk, TK_MIRROR);
  1522. halt_fast_timekeeper(tk);
  1523. write_seqcount_end(&tk_core.seq);
  1524. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1525. tick_suspend();
  1526. clocksource_suspend();
  1527. clockevents_suspend();
  1528. return 0;
  1529. }
  1530. /* sysfs resume/suspend bits for timekeeping */
  1531. static struct syscore_ops timekeeping_syscore_ops = {
  1532. .resume = timekeeping_resume,
  1533. .suspend = timekeeping_suspend,
  1534. };
  1535. static int __init timekeeping_init_ops(void)
  1536. {
  1537. register_syscore_ops(&timekeeping_syscore_ops);
  1538. return 0;
  1539. }
  1540. device_initcall(timekeeping_init_ops);
  1541. /*
  1542. * Apply a multiplier adjustment to the timekeeper
  1543. */
  1544. static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  1545. s64 offset,
  1546. s32 mult_adj)
  1547. {
  1548. s64 interval = tk->cycle_interval;
  1549. if (mult_adj == 0) {
  1550. return;
  1551. } else if (mult_adj == -1) {
  1552. interval = -interval;
  1553. offset = -offset;
  1554. } else if (mult_adj != 1) {
  1555. interval *= mult_adj;
  1556. offset *= mult_adj;
  1557. }
  1558. /*
  1559. * So the following can be confusing.
  1560. *
  1561. * To keep things simple, lets assume mult_adj == 1 for now.
  1562. *
  1563. * When mult_adj != 1, remember that the interval and offset values
  1564. * have been appropriately scaled so the math is the same.
  1565. *
  1566. * The basic idea here is that we're increasing the multiplier
  1567. * by one, this causes the xtime_interval to be incremented by
  1568. * one cycle_interval. This is because:
  1569. * xtime_interval = cycle_interval * mult
  1570. * So if mult is being incremented by one:
  1571. * xtime_interval = cycle_interval * (mult + 1)
  1572. * Its the same as:
  1573. * xtime_interval = (cycle_interval * mult) + cycle_interval
  1574. * Which can be shortened to:
  1575. * xtime_interval += cycle_interval
  1576. *
  1577. * So offset stores the non-accumulated cycles. Thus the current
  1578. * time (in shifted nanoseconds) is:
  1579. * now = (offset * adj) + xtime_nsec
  1580. * Now, even though we're adjusting the clock frequency, we have
  1581. * to keep time consistent. In other words, we can't jump back
  1582. * in time, and we also want to avoid jumping forward in time.
  1583. *
  1584. * So given the same offset value, we need the time to be the same
  1585. * both before and after the freq adjustment.
  1586. * now = (offset * adj_1) + xtime_nsec_1
  1587. * now = (offset * adj_2) + xtime_nsec_2
  1588. * So:
  1589. * (offset * adj_1) + xtime_nsec_1 =
  1590. * (offset * adj_2) + xtime_nsec_2
  1591. * And we know:
  1592. * adj_2 = adj_1 + 1
  1593. * So:
  1594. * (offset * adj_1) + xtime_nsec_1 =
  1595. * (offset * (adj_1+1)) + xtime_nsec_2
  1596. * (offset * adj_1) + xtime_nsec_1 =
  1597. * (offset * adj_1) + offset + xtime_nsec_2
  1598. * Canceling the sides:
  1599. * xtime_nsec_1 = offset + xtime_nsec_2
  1600. * Which gives us:
  1601. * xtime_nsec_2 = xtime_nsec_1 - offset
  1602. * Which simplfies to:
  1603. * xtime_nsec -= offset
  1604. */
  1605. if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
  1606. /* NTP adjustment caused clocksource mult overflow */
  1607. WARN_ON_ONCE(1);
  1608. return;
  1609. }
  1610. tk->tkr_mono.mult += mult_adj;
  1611. tk->xtime_interval += interval;
  1612. tk->tkr_mono.xtime_nsec -= offset;
  1613. }
  1614. /*
  1615. * Adjust the timekeeper's multiplier to the correct frequency
  1616. * and also to reduce the accumulated error value.
  1617. */
  1618. static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
  1619. {
  1620. u32 mult;
  1621. /*
  1622. * Determine the multiplier from the current NTP tick length.
  1623. * Avoid expensive division when the tick length doesn't change.
  1624. */
  1625. if (likely(tk->ntp_tick == ntp_tick_length())) {
  1626. mult = tk->tkr_mono.mult - tk->ntp_err_mult;
  1627. } else {
  1628. tk->ntp_tick = ntp_tick_length();
  1629. mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) -
  1630. tk->xtime_remainder, tk->cycle_interval);
  1631. }
  1632. /*
  1633. * If the clock is behind the NTP time, increase the multiplier by 1
  1634. * to catch up with it. If it's ahead and there was a remainder in the
  1635. * tick division, the clock will slow down. Otherwise it will stay
  1636. * ahead until the tick length changes to a non-divisible value.
  1637. */
  1638. tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0;
  1639. mult += tk->ntp_err_mult;
  1640. timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult);
  1641. if (unlikely(tk->tkr_mono.clock->maxadj &&
  1642. (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
  1643. > tk->tkr_mono.clock->maxadj))) {
  1644. printk_once(KERN_WARNING
  1645. "Adjusting %s more than 11%% (%ld vs %ld)\n",
  1646. tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
  1647. (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
  1648. }
  1649. /*
  1650. * It may be possible that when we entered this function, xtime_nsec
  1651. * was very small. Further, if we're slightly speeding the clocksource
  1652. * in the code above, its possible the required corrective factor to
  1653. * xtime_nsec could cause it to underflow.
  1654. *
  1655. * Now, since we have already accumulated the second and the NTP
  1656. * subsystem has been notified via second_overflow(), we need to skip
  1657. * the next update.
  1658. */
  1659. if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
  1660. tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC <<
  1661. tk->tkr_mono.shift;
  1662. tk->xtime_sec--;
  1663. tk->skip_second_overflow = 1;
  1664. }
  1665. }
  1666. /**
  1667. * accumulate_nsecs_to_secs - Accumulates nsecs into secs
  1668. *
  1669. * Helper function that accumulates the nsecs greater than a second
  1670. * from the xtime_nsec field to the xtime_secs field.
  1671. * It also calls into the NTP code to handle leapsecond processing.
  1672. *
  1673. */
  1674. static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
  1675. {
  1676. u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
  1677. unsigned int clock_set = 0;
  1678. while (tk->tkr_mono.xtime_nsec >= nsecps) {
  1679. int leap;
  1680. tk->tkr_mono.xtime_nsec -= nsecps;
  1681. tk->xtime_sec++;
  1682. /*
  1683. * Skip NTP update if this second was accumulated before,
  1684. * i.e. xtime_nsec underflowed in timekeeping_adjust()
  1685. */
  1686. if (unlikely(tk->skip_second_overflow)) {
  1687. tk->skip_second_overflow = 0;
  1688. continue;
  1689. }
  1690. /* Figure out if its a leap sec and apply if needed */
  1691. leap = second_overflow(tk->xtime_sec);
  1692. if (unlikely(leap)) {
  1693. struct timespec64 ts;
  1694. tk->xtime_sec += leap;
  1695. ts.tv_sec = leap;
  1696. ts.tv_nsec = 0;
  1697. tk_set_wall_to_mono(tk,
  1698. timespec64_sub(tk->wall_to_monotonic, ts));
  1699. __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
  1700. clock_set = TK_CLOCK_WAS_SET;
  1701. }
  1702. }
  1703. return clock_set;
  1704. }
  1705. /**
  1706. * logarithmic_accumulation - shifted accumulation of cycles
  1707. *
  1708. * This functions accumulates a shifted interval of cycles into
  1709. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  1710. * loop.
  1711. *
  1712. * Returns the unconsumed cycles.
  1713. */
  1714. static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
  1715. u32 shift, unsigned int *clock_set)
  1716. {
  1717. u64 interval = tk->cycle_interval << shift;
  1718. u64 snsec_per_sec;
  1719. /* If the offset is smaller than a shifted interval, do nothing */
  1720. if (offset < interval)
  1721. return offset;
  1722. /* Accumulate one shifted interval */
  1723. offset -= interval;
  1724. tk->tkr_mono.cycle_last += interval;
  1725. tk->tkr_raw.cycle_last += interval;
  1726. tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
  1727. *clock_set |= accumulate_nsecs_to_secs(tk);
  1728. /* Accumulate raw time */
  1729. tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
  1730. snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
  1731. while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
  1732. tk->tkr_raw.xtime_nsec -= snsec_per_sec;
  1733. tk->raw_sec++;
  1734. }
  1735. /* Accumulate error between NTP and clock interval */
  1736. tk->ntp_error += tk->ntp_tick << shift;
  1737. tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
  1738. (tk->ntp_error_shift + shift);
  1739. return offset;
  1740. }
  1741. /*
  1742. * timekeeping_advance - Updates the timekeeper to the current time and
  1743. * current NTP tick length
  1744. */
  1745. static void timekeeping_advance(enum timekeeping_adv_mode mode)
  1746. {
  1747. struct timekeeper *real_tk = &tk_core.timekeeper;
  1748. struct timekeeper *tk = &shadow_timekeeper;
  1749. u64 offset;
  1750. int shift = 0, maxshift;
  1751. unsigned int clock_set = 0;
  1752. unsigned long flags;
  1753. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1754. /* Make sure we're fully resumed: */
  1755. if (unlikely(timekeeping_suspended))
  1756. goto out;
  1757. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  1758. offset = real_tk->cycle_interval;
  1759. if (mode != TK_ADV_TICK)
  1760. goto out;
  1761. #else
  1762. offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
  1763. tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
  1764. /* Check if there's really nothing to do */
  1765. if (offset < real_tk->cycle_interval && mode == TK_ADV_TICK)
  1766. goto out;
  1767. #endif
  1768. /* Do some additional sanity checking */
  1769. timekeeping_check_update(tk, offset);
  1770. /*
  1771. * With NO_HZ we may have to accumulate many cycle_intervals
  1772. * (think "ticks") worth of time at once. To do this efficiently,
  1773. * we calculate the largest doubling multiple of cycle_intervals
  1774. * that is smaller than the offset. We then accumulate that
  1775. * chunk in one go, and then try to consume the next smaller
  1776. * doubled multiple.
  1777. */
  1778. shift = ilog2(offset) - ilog2(tk->cycle_interval);
  1779. shift = max(0, shift);
  1780. /* Bound shift to one less than what overflows tick_length */
  1781. maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
  1782. shift = min(shift, maxshift);
  1783. while (offset >= tk->cycle_interval) {
  1784. offset = logarithmic_accumulation(tk, offset, shift,
  1785. &clock_set);
  1786. if (offset < tk->cycle_interval<<shift)
  1787. shift--;
  1788. }
  1789. /* Adjust the multiplier to correct NTP error */
  1790. timekeeping_adjust(tk, offset);
  1791. /*
  1792. * Finally, make sure that after the rounding
  1793. * xtime_nsec isn't larger than NSEC_PER_SEC
  1794. */
  1795. clock_set |= accumulate_nsecs_to_secs(tk);
  1796. write_seqcount_begin(&tk_core.seq);
  1797. /*
  1798. * Update the real timekeeper.
  1799. *
  1800. * We could avoid this memcpy by switching pointers, but that
  1801. * requires changes to all other timekeeper usage sites as
  1802. * well, i.e. move the timekeeper pointer getter into the
  1803. * spinlocked/seqcount protected sections. And we trade this
  1804. * memcpy under the tk_core.seq against one before we start
  1805. * updating.
  1806. */
  1807. timekeeping_update(tk, clock_set);
  1808. memcpy(real_tk, tk, sizeof(*tk));
  1809. /* The memcpy must come last. Do not put anything here! */
  1810. write_seqcount_end(&tk_core.seq);
  1811. out:
  1812. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1813. if (clock_set)
  1814. /* Have to call _delayed version, since in irq context*/
  1815. clock_was_set_delayed();
  1816. }
  1817. /**
  1818. * update_wall_time - Uses the current clocksource to increment the wall time
  1819. *
  1820. */
  1821. void update_wall_time(void)
  1822. {
  1823. timekeeping_advance(TK_ADV_TICK);
  1824. }
  1825. /**
  1826. * getboottime64 - Return the real time of system boot.
  1827. * @ts: pointer to the timespec64 to be set
  1828. *
  1829. * Returns the wall-time of boot in a timespec64.
  1830. *
  1831. * This is based on the wall_to_monotonic offset and the total suspend
  1832. * time. Calls to settimeofday will affect the value returned (which
  1833. * basically means that however wrong your real time clock is at boot time,
  1834. * you get the right time here).
  1835. */
  1836. void getboottime64(struct timespec64 *ts)
  1837. {
  1838. struct timekeeper *tk = &tk_core.timekeeper;
  1839. ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
  1840. *ts = ktime_to_timespec64(t);
  1841. }
  1842. EXPORT_SYMBOL_GPL(getboottime64);
  1843. void ktime_get_coarse_real_ts64(struct timespec64 *ts)
  1844. {
  1845. struct timekeeper *tk = &tk_core.timekeeper;
  1846. unsigned long seq;
  1847. do {
  1848. seq = read_seqcount_begin(&tk_core.seq);
  1849. *ts = tk_xtime(tk);
  1850. } while (read_seqcount_retry(&tk_core.seq, seq));
  1851. }
  1852. EXPORT_SYMBOL(ktime_get_coarse_real_ts64);
  1853. void ktime_get_coarse_ts64(struct timespec64 *ts)
  1854. {
  1855. struct timekeeper *tk = &tk_core.timekeeper;
  1856. struct timespec64 now, mono;
  1857. unsigned long seq;
  1858. do {
  1859. seq = read_seqcount_begin(&tk_core.seq);
  1860. now = tk_xtime(tk);
  1861. mono = tk->wall_to_monotonic;
  1862. } while (read_seqcount_retry(&tk_core.seq, seq));
  1863. set_normalized_timespec64(ts, now.tv_sec + mono.tv_sec,
  1864. now.tv_nsec + mono.tv_nsec);
  1865. }
  1866. EXPORT_SYMBOL(ktime_get_coarse_ts64);
  1867. /*
  1868. * Must hold jiffies_lock
  1869. */
  1870. void do_timer(unsigned long ticks)
  1871. {
  1872. jiffies_64 += ticks;
  1873. calc_global_load(ticks);
  1874. }
  1875. /**
  1876. * ktime_get_update_offsets_now - hrtimer helper
  1877. * @cwsseq: pointer to check and store the clock was set sequence number
  1878. * @offs_real: pointer to storage for monotonic -> realtime offset
  1879. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1880. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1881. *
  1882. * Returns current monotonic time and updates the offsets if the
  1883. * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
  1884. * different.
  1885. *
  1886. * Called from hrtimer_interrupt() or retrigger_next_event()
  1887. */
  1888. ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
  1889. ktime_t *offs_boot, ktime_t *offs_tai)
  1890. {
  1891. struct timekeeper *tk = &tk_core.timekeeper;
  1892. unsigned int seq;
  1893. ktime_t base;
  1894. u64 nsecs;
  1895. do {
  1896. seq = read_seqcount_begin(&tk_core.seq);
  1897. base = tk->tkr_mono.base;
  1898. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  1899. base = ktime_add_ns(base, nsecs);
  1900. if (*cwsseq != tk->clock_was_set_seq) {
  1901. *cwsseq = tk->clock_was_set_seq;
  1902. *offs_real = tk->offs_real;
  1903. *offs_boot = tk->offs_boot;
  1904. *offs_tai = tk->offs_tai;
  1905. }
  1906. /* Handle leapsecond insertion adjustments */
  1907. if (unlikely(base >= tk->next_leap_ktime))
  1908. *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
  1909. } while (read_seqcount_retry(&tk_core.seq, seq));
  1910. return base;
  1911. }
  1912. /**
  1913. * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex
  1914. */
  1915. static int timekeeping_validate_timex(const struct timex *txc)
  1916. {
  1917. if (txc->modes & ADJ_ADJTIME) {
  1918. /* singleshot must not be used with any other mode bits */
  1919. if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
  1920. return -EINVAL;
  1921. if (!(txc->modes & ADJ_OFFSET_READONLY) &&
  1922. !capable(CAP_SYS_TIME))
  1923. return -EPERM;
  1924. } else {
  1925. /* In order to modify anything, you gotta be super-user! */
  1926. if (txc->modes && !capable(CAP_SYS_TIME))
  1927. return -EPERM;
  1928. /*
  1929. * if the quartz is off by more than 10% then
  1930. * something is VERY wrong!
  1931. */
  1932. if (txc->modes & ADJ_TICK &&
  1933. (txc->tick < 900000/USER_HZ ||
  1934. txc->tick > 1100000/USER_HZ))
  1935. return -EINVAL;
  1936. }
  1937. if (txc->modes & ADJ_SETOFFSET) {
  1938. /* In order to inject time, you gotta be super-user! */
  1939. if (!capable(CAP_SYS_TIME))
  1940. return -EPERM;
  1941. /*
  1942. * Validate if a timespec/timeval used to inject a time
  1943. * offset is valid. Offsets can be postive or negative, so
  1944. * we don't check tv_sec. The value of the timeval/timespec
  1945. * is the sum of its fields,but *NOTE*:
  1946. * The field tv_usec/tv_nsec must always be non-negative and
  1947. * we can't have more nanoseconds/microseconds than a second.
  1948. */
  1949. if (txc->time.tv_usec < 0)
  1950. return -EINVAL;
  1951. if (txc->modes & ADJ_NANO) {
  1952. if (txc->time.tv_usec >= NSEC_PER_SEC)
  1953. return -EINVAL;
  1954. } else {
  1955. if (txc->time.tv_usec >= USEC_PER_SEC)
  1956. return -EINVAL;
  1957. }
  1958. }
  1959. /*
  1960. * Check for potential multiplication overflows that can
  1961. * only happen on 64-bit systems:
  1962. */
  1963. if ((txc->modes & ADJ_FREQUENCY) && (BITS_PER_LONG == 64)) {
  1964. if (LLONG_MIN / PPM_SCALE > txc->freq)
  1965. return -EINVAL;
  1966. if (LLONG_MAX / PPM_SCALE < txc->freq)
  1967. return -EINVAL;
  1968. }
  1969. return 0;
  1970. }
  1971. /**
  1972. * do_adjtimex() - Accessor function to NTP __do_adjtimex function
  1973. */
  1974. int do_adjtimex(struct timex *txc)
  1975. {
  1976. struct timekeeper *tk = &tk_core.timekeeper;
  1977. unsigned long flags;
  1978. struct timespec64 ts;
  1979. s32 orig_tai, tai;
  1980. int ret;
  1981. /* Validate the data before disabling interrupts */
  1982. ret = timekeeping_validate_timex(txc);
  1983. if (ret)
  1984. return ret;
  1985. if (txc->modes & ADJ_SETOFFSET) {
  1986. struct timespec64 delta;
  1987. delta.tv_sec = txc->time.tv_sec;
  1988. delta.tv_nsec = txc->time.tv_usec;
  1989. if (!(txc->modes & ADJ_NANO))
  1990. delta.tv_nsec *= 1000;
  1991. ret = timekeeping_inject_offset(&delta);
  1992. if (ret)
  1993. return ret;
  1994. }
  1995. ktime_get_real_ts64(&ts);
  1996. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1997. write_seqcount_begin(&tk_core.seq);
  1998. orig_tai = tai = tk->tai_offset;
  1999. ret = __do_adjtimex(txc, &ts, &tai);
  2000. if (tai != orig_tai) {
  2001. __timekeeping_set_tai_offset(tk, tai);
  2002. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  2003. }
  2004. tk_update_leap_state(tk);
  2005. write_seqcount_end(&tk_core.seq);
  2006. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  2007. /* Update the multiplier immediately if frequency was set directly */
  2008. if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
  2009. timekeeping_advance(TK_ADV_FREQ);
  2010. if (tai != orig_tai)
  2011. clock_was_set();
  2012. ntp_notify_cmos_timer();
  2013. return ret;
  2014. }
  2015. #ifdef CONFIG_NTP_PPS
  2016. /**
  2017. * hardpps() - Accessor function to NTP __hardpps function
  2018. */
  2019. void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
  2020. {
  2021. unsigned long flags;
  2022. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  2023. write_seqcount_begin(&tk_core.seq);
  2024. __hardpps(phase_ts, raw_ts);
  2025. write_seqcount_end(&tk_core.seq);
  2026. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  2027. }
  2028. EXPORT_SYMBOL(hardpps);
  2029. #endif /* CONFIG_NTP_PPS */
  2030. /**
  2031. * xtime_update() - advances the timekeeping infrastructure
  2032. * @ticks: number of ticks, that have elapsed since the last call.
  2033. *
  2034. * Must be called with interrupts disabled.
  2035. */
  2036. void xtime_update(unsigned long ticks)
  2037. {
  2038. write_seqlock(&jiffies_lock);
  2039. do_timer(ticks);
  2040. write_sequnlock(&jiffies_lock);
  2041. update_wall_time();
  2042. }