timekeeping.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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/sched.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/clocksource.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/time.h>
  21. #include <linux/tick.h>
  22. #include <linux/stop_machine.h>
  23. #include <linux/pvclock_gtod.h>
  24. #include <linux/compiler.h>
  25. #include "tick-internal.h"
  26. #include "ntp_internal.h"
  27. #include "timekeeping_internal.h"
  28. #define TK_CLEAR_NTP (1 << 0)
  29. #define TK_MIRROR (1 << 1)
  30. #define TK_CLOCK_WAS_SET (1 << 2)
  31. /*
  32. * The most important data for readout fits into a single 64 byte
  33. * cache line.
  34. */
  35. static struct {
  36. seqcount_t seq;
  37. struct timekeeper timekeeper;
  38. } tk_core ____cacheline_aligned;
  39. static DEFINE_RAW_SPINLOCK(timekeeper_lock);
  40. static struct timekeeper shadow_timekeeper;
  41. /**
  42. * struct tk_fast - NMI safe timekeeper
  43. * @seq: Sequence counter for protecting updates. The lowest bit
  44. * is the index for the tk_read_base array
  45. * @base: tk_read_base array. Access is indexed by the lowest bit of
  46. * @seq.
  47. *
  48. * See @update_fast_timekeeper() below.
  49. */
  50. struct tk_fast {
  51. seqcount_t seq;
  52. struct tk_read_base base[2];
  53. };
  54. static struct tk_fast tk_fast_mono ____cacheline_aligned;
  55. /* flag for if timekeeping is suspended */
  56. int __read_mostly timekeeping_suspended;
  57. /* Flag for if there is a persistent clock on this platform */
  58. bool __read_mostly persistent_clock_exist = false;
  59. static inline void tk_normalize_xtime(struct timekeeper *tk)
  60. {
  61. while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) {
  62. tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift;
  63. tk->xtime_sec++;
  64. }
  65. }
  66. static inline struct timespec64 tk_xtime(struct timekeeper *tk)
  67. {
  68. struct timespec64 ts;
  69. ts.tv_sec = tk->xtime_sec;
  70. ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
  71. return ts;
  72. }
  73. static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
  74. {
  75. tk->xtime_sec = ts->tv_sec;
  76. tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift;
  77. }
  78. static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
  79. {
  80. tk->xtime_sec += ts->tv_sec;
  81. tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift;
  82. tk_normalize_xtime(tk);
  83. }
  84. static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
  85. {
  86. struct timespec64 tmp;
  87. /*
  88. * Verify consistency of: offset_real = -wall_to_monotonic
  89. * before modifying anything
  90. */
  91. set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
  92. -tk->wall_to_monotonic.tv_nsec);
  93. WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
  94. tk->wall_to_monotonic = wtm;
  95. set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
  96. tk->offs_real = timespec64_to_ktime(tmp);
  97. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
  98. }
  99. static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
  100. {
  101. tk->offs_boot = ktime_add(tk->offs_boot, delta);
  102. }
  103. /**
  104. * tk_setup_internals - Set up internals to use clocksource clock.
  105. *
  106. * @tk: The target timekeeper to setup.
  107. * @clock: Pointer to clocksource.
  108. *
  109. * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
  110. * pair and interval request.
  111. *
  112. * Unless you're the timekeeping code, you should not be using this!
  113. */
  114. static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
  115. {
  116. cycle_t interval;
  117. u64 tmp, ntpinterval;
  118. struct clocksource *old_clock;
  119. old_clock = tk->tkr.clock;
  120. tk->tkr.clock = clock;
  121. tk->tkr.read = clock->read;
  122. tk->tkr.mask = clock->mask;
  123. tk->tkr.cycle_last = tk->tkr.read(clock);
  124. /* Do the ns -> cycle conversion first, using original mult */
  125. tmp = NTP_INTERVAL_LENGTH;
  126. tmp <<= clock->shift;
  127. ntpinterval = tmp;
  128. tmp += clock->mult/2;
  129. do_div(tmp, clock->mult);
  130. if (tmp == 0)
  131. tmp = 1;
  132. interval = (cycle_t) tmp;
  133. tk->cycle_interval = interval;
  134. /* Go back from cycles -> shifted ns */
  135. tk->xtime_interval = (u64) interval * clock->mult;
  136. tk->xtime_remainder = ntpinterval - tk->xtime_interval;
  137. tk->raw_interval =
  138. ((u64) interval * clock->mult) >> clock->shift;
  139. /* if changing clocks, convert xtime_nsec shift units */
  140. if (old_clock) {
  141. int shift_change = clock->shift - old_clock->shift;
  142. if (shift_change < 0)
  143. tk->tkr.xtime_nsec >>= -shift_change;
  144. else
  145. tk->tkr.xtime_nsec <<= shift_change;
  146. }
  147. tk->tkr.shift = clock->shift;
  148. tk->ntp_error = 0;
  149. tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
  150. tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
  151. /*
  152. * The timekeeper keeps its own mult values for the currently
  153. * active clocksource. These value will be adjusted via NTP
  154. * to counteract clock drifting.
  155. */
  156. tk->tkr.mult = clock->mult;
  157. tk->ntp_err_mult = 0;
  158. }
  159. /* Timekeeper helper functions. */
  160. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  161. static u32 default_arch_gettimeoffset(void) { return 0; }
  162. u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
  163. #else
  164. static inline u32 arch_gettimeoffset(void) { return 0; }
  165. #endif
  166. static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
  167. {
  168. cycle_t cycle_now, delta;
  169. s64 nsec;
  170. /* read clocksource: */
  171. cycle_now = tkr->read(tkr->clock);
  172. /* calculate the delta since the last update_wall_time: */
  173. delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
  174. nsec = delta * tkr->mult + tkr->xtime_nsec;
  175. nsec >>= tkr->shift;
  176. /* If arch requires, add in get_arch_timeoffset() */
  177. return nsec + arch_gettimeoffset();
  178. }
  179. static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
  180. {
  181. struct clocksource *clock = tk->tkr.clock;
  182. cycle_t cycle_now, delta;
  183. s64 nsec;
  184. /* read clocksource: */
  185. cycle_now = tk->tkr.read(clock);
  186. /* calculate the delta since the last update_wall_time: */
  187. delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
  188. /* convert delta to nanoseconds. */
  189. nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
  190. /* If arch requires, add in get_arch_timeoffset() */
  191. return nsec + arch_gettimeoffset();
  192. }
  193. /**
  194. * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
  195. * @tk: The timekeeper from which we take the update
  196. * @tkf: The fast timekeeper to update
  197. * @tbase: The time base for the fast timekeeper (mono/raw)
  198. *
  199. * We want to use this from any context including NMI and tracing /
  200. * instrumenting the timekeeping code itself.
  201. *
  202. * So we handle this differently than the other timekeeping accessor
  203. * functions which retry when the sequence count has changed. The
  204. * update side does:
  205. *
  206. * smp_wmb(); <- Ensure that the last base[1] update is visible
  207. * tkf->seq++;
  208. * smp_wmb(); <- Ensure that the seqcount update is visible
  209. * update(tkf->base[0], tk);
  210. * smp_wmb(); <- Ensure that the base[0] update is visible
  211. * tkf->seq++;
  212. * smp_wmb(); <- Ensure that the seqcount update is visible
  213. * update(tkf->base[1], tk);
  214. *
  215. * The reader side does:
  216. *
  217. * do {
  218. * seq = tkf->seq;
  219. * smp_rmb();
  220. * idx = seq & 0x01;
  221. * now = now(tkf->base[idx]);
  222. * smp_rmb();
  223. * } while (seq != tkf->seq)
  224. *
  225. * As long as we update base[0] readers are forced off to
  226. * base[1]. Once base[0] is updated readers are redirected to base[0]
  227. * and the base[1] update takes place.
  228. *
  229. * So if a NMI hits the update of base[0] then it will use base[1]
  230. * which is still consistent. In the worst case this can result is a
  231. * slightly wrong timestamp (a few nanoseconds). See
  232. * @ktime_get_mono_fast_ns.
  233. */
  234. static void update_fast_timekeeper(struct timekeeper *tk)
  235. {
  236. struct tk_read_base *base = tk_fast_mono.base;
  237. /* Force readers off to base[1] */
  238. raw_write_seqcount_latch(&tk_fast_mono.seq);
  239. /* Update base[0] */
  240. memcpy(base, &tk->tkr, sizeof(*base));
  241. /* Force readers back to base[0] */
  242. raw_write_seqcount_latch(&tk_fast_mono.seq);
  243. /* Update base[1] */
  244. memcpy(base + 1, base, sizeof(*base));
  245. }
  246. /**
  247. * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
  248. *
  249. * This timestamp is not guaranteed to be monotonic across an update.
  250. * The timestamp is calculated by:
  251. *
  252. * now = base_mono + clock_delta * slope
  253. *
  254. * So if the update lowers the slope, readers who are forced to the
  255. * not yet updated second array are still using the old steeper slope.
  256. *
  257. * tmono
  258. * ^
  259. * | o n
  260. * | o n
  261. * | u
  262. * | o
  263. * |o
  264. * |12345678---> reader order
  265. *
  266. * o = old slope
  267. * u = update
  268. * n = new slope
  269. *
  270. * So reader 6 will observe time going backwards versus reader 5.
  271. *
  272. * While other CPUs are likely to be able observe that, the only way
  273. * for a CPU local observation is when an NMI hits in the middle of
  274. * the update. Timestamps taken from that NMI context might be ahead
  275. * of the following timestamps. Callers need to be aware of that and
  276. * deal with it.
  277. */
  278. u64 notrace ktime_get_mono_fast_ns(void)
  279. {
  280. struct tk_read_base *tkr;
  281. unsigned int seq;
  282. u64 now;
  283. do {
  284. seq = raw_read_seqcount(&tk_fast_mono.seq);
  285. tkr = tk_fast_mono.base + (seq & 0x01);
  286. now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr);
  287. } while (read_seqcount_retry(&tk_fast_mono.seq, seq));
  288. return now;
  289. }
  290. EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
  291. #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
  292. static inline void update_vsyscall(struct timekeeper *tk)
  293. {
  294. struct timespec xt, wm;
  295. xt = timespec64_to_timespec(tk_xtime(tk));
  296. wm = timespec64_to_timespec(tk->wall_to_monotonic);
  297. update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult,
  298. tk->tkr.cycle_last);
  299. }
  300. static inline void old_vsyscall_fixup(struct timekeeper *tk)
  301. {
  302. s64 remainder;
  303. /*
  304. * Store only full nanoseconds into xtime_nsec after rounding
  305. * it up and add the remainder to the error difference.
  306. * XXX - This is necessary to avoid small 1ns inconsistnecies caused
  307. * by truncating the remainder in vsyscalls. However, it causes
  308. * additional work to be done in timekeeping_adjust(). Once
  309. * the vsyscall implementations are converted to use xtime_nsec
  310. * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
  311. * users are removed, this can be killed.
  312. */
  313. remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1);
  314. tk->tkr.xtime_nsec -= remainder;
  315. tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift;
  316. tk->ntp_error += remainder << tk->ntp_error_shift;
  317. tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift;
  318. }
  319. #else
  320. #define old_vsyscall_fixup(tk)
  321. #endif
  322. static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
  323. static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
  324. {
  325. raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
  326. }
  327. /**
  328. * pvclock_gtod_register_notifier - register a pvclock timedata update listener
  329. */
  330. int pvclock_gtod_register_notifier(struct notifier_block *nb)
  331. {
  332. struct timekeeper *tk = &tk_core.timekeeper;
  333. unsigned long flags;
  334. int ret;
  335. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  336. ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
  337. update_pvclock_gtod(tk, true);
  338. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  339. return ret;
  340. }
  341. EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
  342. /**
  343. * pvclock_gtod_unregister_notifier - unregister a pvclock
  344. * timedata update listener
  345. */
  346. int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
  347. {
  348. unsigned long flags;
  349. int ret;
  350. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  351. ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
  352. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  353. return ret;
  354. }
  355. EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
  356. /*
  357. * Update the ktime_t based scalar nsec members of the timekeeper
  358. */
  359. static inline void tk_update_ktime_data(struct timekeeper *tk)
  360. {
  361. u64 seconds;
  362. u32 nsec;
  363. /*
  364. * The xtime based monotonic readout is:
  365. * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
  366. * The ktime based monotonic readout is:
  367. * nsec = base_mono + now();
  368. * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
  369. */
  370. seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
  371. nsec = (u32) tk->wall_to_monotonic.tv_nsec;
  372. tk->tkr.base_mono = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
  373. /* Update the monotonic raw base */
  374. tk->base_raw = timespec64_to_ktime(tk->raw_time);
  375. /*
  376. * The sum of the nanoseconds portions of xtime and
  377. * wall_to_monotonic can be greater/equal one second. Take
  378. * this into account before updating tk->ktime_sec.
  379. */
  380. nsec += (u32)(tk->tkr.xtime_nsec >> tk->tkr.shift);
  381. if (nsec >= NSEC_PER_SEC)
  382. seconds++;
  383. tk->ktime_sec = seconds;
  384. }
  385. /* must hold timekeeper_lock */
  386. static void timekeeping_update(struct timekeeper *tk, unsigned int action)
  387. {
  388. if (action & TK_CLEAR_NTP) {
  389. tk->ntp_error = 0;
  390. ntp_clear();
  391. }
  392. tk_update_ktime_data(tk);
  393. update_vsyscall(tk);
  394. update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
  395. if (action & TK_MIRROR)
  396. memcpy(&shadow_timekeeper, &tk_core.timekeeper,
  397. sizeof(tk_core.timekeeper));
  398. update_fast_timekeeper(tk);
  399. }
  400. /**
  401. * timekeeping_forward_now - update clock to the current time
  402. *
  403. * Forward the current clock to update its state since the last call to
  404. * update_wall_time(). This is useful before significant clock changes,
  405. * as it avoids having to deal with this time offset explicitly.
  406. */
  407. static void timekeeping_forward_now(struct timekeeper *tk)
  408. {
  409. struct clocksource *clock = tk->tkr.clock;
  410. cycle_t cycle_now, delta;
  411. s64 nsec;
  412. cycle_now = tk->tkr.read(clock);
  413. delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
  414. tk->tkr.cycle_last = cycle_now;
  415. tk->tkr.xtime_nsec += delta * tk->tkr.mult;
  416. /* If arch requires, add in get_arch_timeoffset() */
  417. tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift;
  418. tk_normalize_xtime(tk);
  419. nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
  420. timespec64_add_ns(&tk->raw_time, nsec);
  421. }
  422. /**
  423. * __getnstimeofday64 - Returns the time of day in a timespec64.
  424. * @ts: pointer to the timespec to be set
  425. *
  426. * Updates the time of day in the timespec.
  427. * Returns 0 on success, or -ve when suspended (timespec will be undefined).
  428. */
  429. int __getnstimeofday64(struct timespec64 *ts)
  430. {
  431. struct timekeeper *tk = &tk_core.timekeeper;
  432. unsigned long seq;
  433. s64 nsecs = 0;
  434. do {
  435. seq = read_seqcount_begin(&tk_core.seq);
  436. ts->tv_sec = tk->xtime_sec;
  437. nsecs = timekeeping_get_ns(&tk->tkr);
  438. } while (read_seqcount_retry(&tk_core.seq, seq));
  439. ts->tv_nsec = 0;
  440. timespec64_add_ns(ts, nsecs);
  441. /*
  442. * Do not bail out early, in case there were callers still using
  443. * the value, even in the face of the WARN_ON.
  444. */
  445. if (unlikely(timekeeping_suspended))
  446. return -EAGAIN;
  447. return 0;
  448. }
  449. EXPORT_SYMBOL(__getnstimeofday64);
  450. /**
  451. * getnstimeofday64 - Returns the time of day in a timespec64.
  452. * @ts: pointer to the timespec64 to be set
  453. *
  454. * Returns the time of day in a timespec64 (WARN if suspended).
  455. */
  456. void getnstimeofday64(struct timespec64 *ts)
  457. {
  458. WARN_ON(__getnstimeofday64(ts));
  459. }
  460. EXPORT_SYMBOL(getnstimeofday64);
  461. ktime_t ktime_get(void)
  462. {
  463. struct timekeeper *tk = &tk_core.timekeeper;
  464. unsigned int seq;
  465. ktime_t base;
  466. s64 nsecs;
  467. WARN_ON(timekeeping_suspended);
  468. do {
  469. seq = read_seqcount_begin(&tk_core.seq);
  470. base = tk->tkr.base_mono;
  471. nsecs = timekeeping_get_ns(&tk->tkr);
  472. } while (read_seqcount_retry(&tk_core.seq, seq));
  473. return ktime_add_ns(base, nsecs);
  474. }
  475. EXPORT_SYMBOL_GPL(ktime_get);
  476. static ktime_t *offsets[TK_OFFS_MAX] = {
  477. [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
  478. [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
  479. [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
  480. };
  481. ktime_t ktime_get_with_offset(enum tk_offsets offs)
  482. {
  483. struct timekeeper *tk = &tk_core.timekeeper;
  484. unsigned int seq;
  485. ktime_t base, *offset = offsets[offs];
  486. s64 nsecs;
  487. WARN_ON(timekeeping_suspended);
  488. do {
  489. seq = read_seqcount_begin(&tk_core.seq);
  490. base = ktime_add(tk->tkr.base_mono, *offset);
  491. nsecs = timekeeping_get_ns(&tk->tkr);
  492. } while (read_seqcount_retry(&tk_core.seq, seq));
  493. return ktime_add_ns(base, nsecs);
  494. }
  495. EXPORT_SYMBOL_GPL(ktime_get_with_offset);
  496. /**
  497. * ktime_mono_to_any() - convert mononotic time to any other time
  498. * @tmono: time to convert.
  499. * @offs: which offset to use
  500. */
  501. ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
  502. {
  503. ktime_t *offset = offsets[offs];
  504. unsigned long seq;
  505. ktime_t tconv;
  506. do {
  507. seq = read_seqcount_begin(&tk_core.seq);
  508. tconv = ktime_add(tmono, *offset);
  509. } while (read_seqcount_retry(&tk_core.seq, seq));
  510. return tconv;
  511. }
  512. EXPORT_SYMBOL_GPL(ktime_mono_to_any);
  513. /**
  514. * ktime_get_raw - Returns the raw monotonic time in ktime_t format
  515. */
  516. ktime_t ktime_get_raw(void)
  517. {
  518. struct timekeeper *tk = &tk_core.timekeeper;
  519. unsigned int seq;
  520. ktime_t base;
  521. s64 nsecs;
  522. do {
  523. seq = read_seqcount_begin(&tk_core.seq);
  524. base = tk->base_raw;
  525. nsecs = timekeeping_get_ns_raw(tk);
  526. } while (read_seqcount_retry(&tk_core.seq, seq));
  527. return ktime_add_ns(base, nsecs);
  528. }
  529. EXPORT_SYMBOL_GPL(ktime_get_raw);
  530. /**
  531. * ktime_get_ts64 - get the monotonic clock in timespec64 format
  532. * @ts: pointer to timespec variable
  533. *
  534. * The function calculates the monotonic clock from the realtime
  535. * clock and the wall_to_monotonic offset and stores the result
  536. * in normalized timespec64 format in the variable pointed to by @ts.
  537. */
  538. void ktime_get_ts64(struct timespec64 *ts)
  539. {
  540. struct timekeeper *tk = &tk_core.timekeeper;
  541. struct timespec64 tomono;
  542. s64 nsec;
  543. unsigned int seq;
  544. WARN_ON(timekeeping_suspended);
  545. do {
  546. seq = read_seqcount_begin(&tk_core.seq);
  547. ts->tv_sec = tk->xtime_sec;
  548. nsec = timekeeping_get_ns(&tk->tkr);
  549. tomono = tk->wall_to_monotonic;
  550. } while (read_seqcount_retry(&tk_core.seq, seq));
  551. ts->tv_sec += tomono.tv_sec;
  552. ts->tv_nsec = 0;
  553. timespec64_add_ns(ts, nsec + tomono.tv_nsec);
  554. }
  555. EXPORT_SYMBOL_GPL(ktime_get_ts64);
  556. /**
  557. * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
  558. *
  559. * Returns the seconds portion of CLOCK_MONOTONIC with a single non
  560. * serialized read. tk->ktime_sec is of type 'unsigned long' so this
  561. * works on both 32 and 64 bit systems. On 32 bit systems the readout
  562. * covers ~136 years of uptime which should be enough to prevent
  563. * premature wrap arounds.
  564. */
  565. time64_t ktime_get_seconds(void)
  566. {
  567. struct timekeeper *tk = &tk_core.timekeeper;
  568. WARN_ON(timekeeping_suspended);
  569. return tk->ktime_sec;
  570. }
  571. EXPORT_SYMBOL_GPL(ktime_get_seconds);
  572. /**
  573. * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
  574. *
  575. * Returns the wall clock seconds since 1970. This replaces the
  576. * get_seconds() interface which is not y2038 safe on 32bit systems.
  577. *
  578. * For 64bit systems the fast access to tk->xtime_sec is preserved. On
  579. * 32bit systems the access must be protected with the sequence
  580. * counter to provide "atomic" access to the 64bit tk->xtime_sec
  581. * value.
  582. */
  583. time64_t ktime_get_real_seconds(void)
  584. {
  585. struct timekeeper *tk = &tk_core.timekeeper;
  586. time64_t seconds;
  587. unsigned int seq;
  588. if (IS_ENABLED(CONFIG_64BIT))
  589. return tk->xtime_sec;
  590. do {
  591. seq = read_seqcount_begin(&tk_core.seq);
  592. seconds = tk->xtime_sec;
  593. } while (read_seqcount_retry(&tk_core.seq, seq));
  594. return seconds;
  595. }
  596. EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
  597. #ifdef CONFIG_NTP_PPS
  598. /**
  599. * getnstime_raw_and_real - get day and raw monotonic time in timespec format
  600. * @ts_raw: pointer to the timespec to be set to raw monotonic time
  601. * @ts_real: pointer to the timespec to be set to the time of day
  602. *
  603. * This function reads both the time of day and raw monotonic time at the
  604. * same time atomically and stores the resulting timestamps in timespec
  605. * format.
  606. */
  607. void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  608. {
  609. struct timekeeper *tk = &tk_core.timekeeper;
  610. unsigned long seq;
  611. s64 nsecs_raw, nsecs_real;
  612. WARN_ON_ONCE(timekeeping_suspended);
  613. do {
  614. seq = read_seqcount_begin(&tk_core.seq);
  615. *ts_raw = timespec64_to_timespec(tk->raw_time);
  616. ts_real->tv_sec = tk->xtime_sec;
  617. ts_real->tv_nsec = 0;
  618. nsecs_raw = timekeeping_get_ns_raw(tk);
  619. nsecs_real = timekeeping_get_ns(&tk->tkr);
  620. } while (read_seqcount_retry(&tk_core.seq, seq));
  621. timespec_add_ns(ts_raw, nsecs_raw);
  622. timespec_add_ns(ts_real, nsecs_real);
  623. }
  624. EXPORT_SYMBOL(getnstime_raw_and_real);
  625. #endif /* CONFIG_NTP_PPS */
  626. /**
  627. * do_gettimeofday - Returns the time of day in a timeval
  628. * @tv: pointer to the timeval to be set
  629. *
  630. * NOTE: Users should be converted to using getnstimeofday()
  631. */
  632. void do_gettimeofday(struct timeval *tv)
  633. {
  634. struct timespec64 now;
  635. getnstimeofday64(&now);
  636. tv->tv_sec = now.tv_sec;
  637. tv->tv_usec = now.tv_nsec/1000;
  638. }
  639. EXPORT_SYMBOL(do_gettimeofday);
  640. /**
  641. * do_settimeofday64 - Sets the time of day.
  642. * @ts: pointer to the timespec64 variable containing the new time
  643. *
  644. * Sets the time of day to the new time and update NTP and notify hrtimers
  645. */
  646. int do_settimeofday64(const struct timespec64 *ts)
  647. {
  648. struct timekeeper *tk = &tk_core.timekeeper;
  649. struct timespec64 ts_delta, xt;
  650. unsigned long flags;
  651. if (!timespec64_valid_strict(ts))
  652. return -EINVAL;
  653. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  654. write_seqcount_begin(&tk_core.seq);
  655. timekeeping_forward_now(tk);
  656. xt = tk_xtime(tk);
  657. ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
  658. ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
  659. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
  660. tk_set_xtime(tk, ts);
  661. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  662. write_seqcount_end(&tk_core.seq);
  663. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  664. /* signal hrtimers about time change */
  665. clock_was_set();
  666. return 0;
  667. }
  668. EXPORT_SYMBOL(do_settimeofday64);
  669. /**
  670. * timekeeping_inject_offset - Adds or subtracts from the current time.
  671. * @tv: pointer to the timespec variable containing the offset
  672. *
  673. * Adds or subtracts an offset value from the current time.
  674. */
  675. int timekeeping_inject_offset(struct timespec *ts)
  676. {
  677. struct timekeeper *tk = &tk_core.timekeeper;
  678. unsigned long flags;
  679. struct timespec64 ts64, tmp;
  680. int ret = 0;
  681. if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  682. return -EINVAL;
  683. ts64 = timespec_to_timespec64(*ts);
  684. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  685. write_seqcount_begin(&tk_core.seq);
  686. timekeeping_forward_now(tk);
  687. /* Make sure the proposed value is valid */
  688. tmp = timespec64_add(tk_xtime(tk), ts64);
  689. if (!timespec64_valid_strict(&tmp)) {
  690. ret = -EINVAL;
  691. goto error;
  692. }
  693. tk_xtime_add(tk, &ts64);
  694. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
  695. error: /* even if we error out, we forwarded the time, so call update */
  696. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  697. write_seqcount_end(&tk_core.seq);
  698. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  699. /* signal hrtimers about time change */
  700. clock_was_set();
  701. return ret;
  702. }
  703. EXPORT_SYMBOL(timekeeping_inject_offset);
  704. /**
  705. * timekeeping_get_tai_offset - Returns current TAI offset from UTC
  706. *
  707. */
  708. s32 timekeeping_get_tai_offset(void)
  709. {
  710. struct timekeeper *tk = &tk_core.timekeeper;
  711. unsigned int seq;
  712. s32 ret;
  713. do {
  714. seq = read_seqcount_begin(&tk_core.seq);
  715. ret = tk->tai_offset;
  716. } while (read_seqcount_retry(&tk_core.seq, seq));
  717. return ret;
  718. }
  719. /**
  720. * __timekeeping_set_tai_offset - Lock free worker function
  721. *
  722. */
  723. static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
  724. {
  725. tk->tai_offset = tai_offset;
  726. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
  727. }
  728. /**
  729. * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
  730. *
  731. */
  732. void timekeeping_set_tai_offset(s32 tai_offset)
  733. {
  734. struct timekeeper *tk = &tk_core.timekeeper;
  735. unsigned long flags;
  736. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  737. write_seqcount_begin(&tk_core.seq);
  738. __timekeeping_set_tai_offset(tk, tai_offset);
  739. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  740. write_seqcount_end(&tk_core.seq);
  741. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  742. clock_was_set();
  743. }
  744. /**
  745. * change_clocksource - Swaps clocksources if a new one is available
  746. *
  747. * Accumulates current time interval and initializes new clocksource
  748. */
  749. static int change_clocksource(void *data)
  750. {
  751. struct timekeeper *tk = &tk_core.timekeeper;
  752. struct clocksource *new, *old;
  753. unsigned long flags;
  754. new = (struct clocksource *) data;
  755. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  756. write_seqcount_begin(&tk_core.seq);
  757. timekeeping_forward_now(tk);
  758. /*
  759. * If the cs is in module, get a module reference. Succeeds
  760. * for built-in code (owner == NULL) as well.
  761. */
  762. if (try_module_get(new->owner)) {
  763. if (!new->enable || new->enable(new) == 0) {
  764. old = tk->tkr.clock;
  765. tk_setup_internals(tk, new);
  766. if (old->disable)
  767. old->disable(old);
  768. module_put(old->owner);
  769. } else {
  770. module_put(new->owner);
  771. }
  772. }
  773. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  774. write_seqcount_end(&tk_core.seq);
  775. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  776. return 0;
  777. }
  778. /**
  779. * timekeeping_notify - Install a new clock source
  780. * @clock: pointer to the clock source
  781. *
  782. * This function is called from clocksource.c after a new, better clock
  783. * source has been registered. The caller holds the clocksource_mutex.
  784. */
  785. int timekeeping_notify(struct clocksource *clock)
  786. {
  787. struct timekeeper *tk = &tk_core.timekeeper;
  788. if (tk->tkr.clock == clock)
  789. return 0;
  790. stop_machine(change_clocksource, clock, NULL);
  791. tick_clock_notify();
  792. return tk->tkr.clock == clock ? 0 : -1;
  793. }
  794. /**
  795. * getrawmonotonic64 - Returns the raw monotonic time in a timespec
  796. * @ts: pointer to the timespec64 to be set
  797. *
  798. * Returns the raw monotonic time (completely un-modified by ntp)
  799. */
  800. void getrawmonotonic64(struct timespec64 *ts)
  801. {
  802. struct timekeeper *tk = &tk_core.timekeeper;
  803. struct timespec64 ts64;
  804. unsigned long seq;
  805. s64 nsecs;
  806. do {
  807. seq = read_seqcount_begin(&tk_core.seq);
  808. nsecs = timekeeping_get_ns_raw(tk);
  809. ts64 = tk->raw_time;
  810. } while (read_seqcount_retry(&tk_core.seq, seq));
  811. timespec64_add_ns(&ts64, nsecs);
  812. *ts = ts64;
  813. }
  814. EXPORT_SYMBOL(getrawmonotonic64);
  815. /**
  816. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  817. */
  818. int timekeeping_valid_for_hres(void)
  819. {
  820. struct timekeeper *tk = &tk_core.timekeeper;
  821. unsigned long seq;
  822. int ret;
  823. do {
  824. seq = read_seqcount_begin(&tk_core.seq);
  825. ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  826. } while (read_seqcount_retry(&tk_core.seq, seq));
  827. return ret;
  828. }
  829. /**
  830. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  831. */
  832. u64 timekeeping_max_deferment(void)
  833. {
  834. struct timekeeper *tk = &tk_core.timekeeper;
  835. unsigned long seq;
  836. u64 ret;
  837. do {
  838. seq = read_seqcount_begin(&tk_core.seq);
  839. ret = tk->tkr.clock->max_idle_ns;
  840. } while (read_seqcount_retry(&tk_core.seq, seq));
  841. return ret;
  842. }
  843. /**
  844. * read_persistent_clock - Return time from the persistent clock.
  845. *
  846. * Weak dummy function for arches that do not yet support it.
  847. * Reads the time from the battery backed persistent clock.
  848. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  849. *
  850. * XXX - Do be sure to remove it once all arches implement it.
  851. */
  852. void __weak read_persistent_clock(struct timespec *ts)
  853. {
  854. ts->tv_sec = 0;
  855. ts->tv_nsec = 0;
  856. }
  857. /**
  858. * read_boot_clock - Return time of the system start.
  859. *
  860. * Weak dummy function for arches that do not yet support it.
  861. * Function to read the exact time the system has been started.
  862. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  863. *
  864. * XXX - Do be sure to remove it once all arches implement it.
  865. */
  866. void __weak read_boot_clock(struct timespec *ts)
  867. {
  868. ts->tv_sec = 0;
  869. ts->tv_nsec = 0;
  870. }
  871. /*
  872. * timekeeping_init - Initializes the clocksource and common timekeeping values
  873. */
  874. void __init timekeeping_init(void)
  875. {
  876. struct timekeeper *tk = &tk_core.timekeeper;
  877. struct clocksource *clock;
  878. unsigned long flags;
  879. struct timespec64 now, boot, tmp;
  880. struct timespec ts;
  881. read_persistent_clock(&ts);
  882. now = timespec_to_timespec64(ts);
  883. if (!timespec64_valid_strict(&now)) {
  884. pr_warn("WARNING: Persistent clock returned invalid value!\n"
  885. " Check your CMOS/BIOS settings.\n");
  886. now.tv_sec = 0;
  887. now.tv_nsec = 0;
  888. } else if (now.tv_sec || now.tv_nsec)
  889. persistent_clock_exist = true;
  890. read_boot_clock(&ts);
  891. boot = timespec_to_timespec64(ts);
  892. if (!timespec64_valid_strict(&boot)) {
  893. pr_warn("WARNING: Boot clock returned invalid value!\n"
  894. " Check your CMOS/BIOS settings.\n");
  895. boot.tv_sec = 0;
  896. boot.tv_nsec = 0;
  897. }
  898. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  899. write_seqcount_begin(&tk_core.seq);
  900. ntp_init();
  901. clock = clocksource_default_clock();
  902. if (clock->enable)
  903. clock->enable(clock);
  904. tk_setup_internals(tk, clock);
  905. tk_set_xtime(tk, &now);
  906. tk->raw_time.tv_sec = 0;
  907. tk->raw_time.tv_nsec = 0;
  908. tk->base_raw.tv64 = 0;
  909. if (boot.tv_sec == 0 && boot.tv_nsec == 0)
  910. boot = tk_xtime(tk);
  911. set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
  912. tk_set_wall_to_mono(tk, tmp);
  913. timekeeping_update(tk, TK_MIRROR);
  914. write_seqcount_end(&tk_core.seq);
  915. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  916. }
  917. /* time in seconds when suspend began */
  918. static struct timespec64 timekeeping_suspend_time;
  919. /**
  920. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  921. * @delta: pointer to a timespec delta value
  922. *
  923. * Takes a timespec offset measuring a suspend interval and properly
  924. * adds the sleep offset to the timekeeping variables.
  925. */
  926. static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  927. struct timespec64 *delta)
  928. {
  929. if (!timespec64_valid_strict(delta)) {
  930. printk_deferred(KERN_WARNING
  931. "__timekeeping_inject_sleeptime: Invalid "
  932. "sleep delta value!\n");
  933. return;
  934. }
  935. tk_xtime_add(tk, delta);
  936. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
  937. tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
  938. tk_debug_account_sleep_time(delta);
  939. }
  940. /**
  941. * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
  942. * @delta: pointer to a timespec64 delta value
  943. *
  944. * This hook is for architectures that cannot support read_persistent_clock
  945. * because their RTC/persistent clock is only accessible when irqs are enabled.
  946. *
  947. * This function should only be called by rtc_resume(), and allows
  948. * a suspend offset to be injected into the timekeeping values.
  949. */
  950. void timekeeping_inject_sleeptime64(struct timespec64 *delta)
  951. {
  952. struct timekeeper *tk = &tk_core.timekeeper;
  953. unsigned long flags;
  954. /*
  955. * Make sure we don't set the clock twice, as timekeeping_resume()
  956. * already did it
  957. */
  958. if (has_persistent_clock())
  959. return;
  960. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  961. write_seqcount_begin(&tk_core.seq);
  962. timekeeping_forward_now(tk);
  963. __timekeeping_inject_sleeptime(tk, delta);
  964. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  965. write_seqcount_end(&tk_core.seq);
  966. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  967. /* signal hrtimers about time change */
  968. clock_was_set();
  969. }
  970. /**
  971. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  972. *
  973. * This is for the generic clocksource timekeeping.
  974. * xtime/wall_to_monotonic/jiffies/etc are
  975. * still managed by arch specific suspend/resume code.
  976. */
  977. static void timekeeping_resume(void)
  978. {
  979. struct timekeeper *tk = &tk_core.timekeeper;
  980. struct clocksource *clock = tk->tkr.clock;
  981. unsigned long flags;
  982. struct timespec64 ts_new, ts_delta;
  983. struct timespec tmp;
  984. cycle_t cycle_now, cycle_delta;
  985. bool suspendtime_found = false;
  986. read_persistent_clock(&tmp);
  987. ts_new = timespec_to_timespec64(tmp);
  988. clockevents_resume();
  989. clocksource_resume();
  990. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  991. write_seqcount_begin(&tk_core.seq);
  992. /*
  993. * After system resumes, we need to calculate the suspended time and
  994. * compensate it for the OS time. There are 3 sources that could be
  995. * used: Nonstop clocksource during suspend, persistent clock and rtc
  996. * device.
  997. *
  998. * One specific platform may have 1 or 2 or all of them, and the
  999. * preference will be:
  1000. * suspend-nonstop clocksource -> persistent clock -> rtc
  1001. * The less preferred source will only be tried if there is no better
  1002. * usable source. The rtc part is handled separately in rtc core code.
  1003. */
  1004. cycle_now = tk->tkr.read(clock);
  1005. if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
  1006. cycle_now > tk->tkr.cycle_last) {
  1007. u64 num, max = ULLONG_MAX;
  1008. u32 mult = clock->mult;
  1009. u32 shift = clock->shift;
  1010. s64 nsec = 0;
  1011. cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last,
  1012. tk->tkr.mask);
  1013. /*
  1014. * "cycle_delta * mutl" may cause 64 bits overflow, if the
  1015. * suspended time is too long. In that case we need do the
  1016. * 64 bits math carefully
  1017. */
  1018. do_div(max, mult);
  1019. if (cycle_delta > max) {
  1020. num = div64_u64(cycle_delta, max);
  1021. nsec = (((u64) max * mult) >> shift) * num;
  1022. cycle_delta -= num * max;
  1023. }
  1024. nsec += ((u64) cycle_delta * mult) >> shift;
  1025. ts_delta = ns_to_timespec64(nsec);
  1026. suspendtime_found = true;
  1027. } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  1028. ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
  1029. suspendtime_found = true;
  1030. }
  1031. if (suspendtime_found)
  1032. __timekeeping_inject_sleeptime(tk, &ts_delta);
  1033. /* Re-base the last cycle value */
  1034. tk->tkr.cycle_last = cycle_now;
  1035. tk->ntp_error = 0;
  1036. timekeeping_suspended = 0;
  1037. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1038. write_seqcount_end(&tk_core.seq);
  1039. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1040. touch_softlockup_watchdog();
  1041. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  1042. /* Resume hrtimers */
  1043. hrtimers_resume();
  1044. }
  1045. static int timekeeping_suspend(void)
  1046. {
  1047. struct timekeeper *tk = &tk_core.timekeeper;
  1048. unsigned long flags;
  1049. struct timespec64 delta, delta_delta;
  1050. static struct timespec64 old_delta;
  1051. struct timespec tmp;
  1052. read_persistent_clock(&tmp);
  1053. timekeeping_suspend_time = timespec_to_timespec64(tmp);
  1054. /*
  1055. * On some systems the persistent_clock can not be detected at
  1056. * timekeeping_init by its return value, so if we see a valid
  1057. * value returned, update the persistent_clock_exists flag.
  1058. */
  1059. if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
  1060. persistent_clock_exist = true;
  1061. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1062. write_seqcount_begin(&tk_core.seq);
  1063. timekeeping_forward_now(tk);
  1064. timekeeping_suspended = 1;
  1065. /*
  1066. * To avoid drift caused by repeated suspend/resumes,
  1067. * which each can add ~1 second drift error,
  1068. * try to compensate so the difference in system time
  1069. * and persistent_clock time stays close to constant.
  1070. */
  1071. delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
  1072. delta_delta = timespec64_sub(delta, old_delta);
  1073. if (abs(delta_delta.tv_sec) >= 2) {
  1074. /*
  1075. * if delta_delta is too large, assume time correction
  1076. * has occured and set old_delta to the current delta.
  1077. */
  1078. old_delta = delta;
  1079. } else {
  1080. /* Otherwise try to adjust old_system to compensate */
  1081. timekeeping_suspend_time =
  1082. timespec64_add(timekeeping_suspend_time, delta_delta);
  1083. }
  1084. timekeeping_update(tk, TK_MIRROR);
  1085. write_seqcount_end(&tk_core.seq);
  1086. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1087. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  1088. clocksource_suspend();
  1089. clockevents_suspend();
  1090. return 0;
  1091. }
  1092. /* sysfs resume/suspend bits for timekeeping */
  1093. static struct syscore_ops timekeeping_syscore_ops = {
  1094. .resume = timekeeping_resume,
  1095. .suspend = timekeeping_suspend,
  1096. };
  1097. static int __init timekeeping_init_ops(void)
  1098. {
  1099. register_syscore_ops(&timekeeping_syscore_ops);
  1100. return 0;
  1101. }
  1102. device_initcall(timekeeping_init_ops);
  1103. /*
  1104. * Apply a multiplier adjustment to the timekeeper
  1105. */
  1106. static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  1107. s64 offset,
  1108. bool negative,
  1109. int adj_scale)
  1110. {
  1111. s64 interval = tk->cycle_interval;
  1112. s32 mult_adj = 1;
  1113. if (negative) {
  1114. mult_adj = -mult_adj;
  1115. interval = -interval;
  1116. offset = -offset;
  1117. }
  1118. mult_adj <<= adj_scale;
  1119. interval <<= adj_scale;
  1120. offset <<= adj_scale;
  1121. /*
  1122. * So the following can be confusing.
  1123. *
  1124. * To keep things simple, lets assume mult_adj == 1 for now.
  1125. *
  1126. * When mult_adj != 1, remember that the interval and offset values
  1127. * have been appropriately scaled so the math is the same.
  1128. *
  1129. * The basic idea here is that we're increasing the multiplier
  1130. * by one, this causes the xtime_interval to be incremented by
  1131. * one cycle_interval. This is because:
  1132. * xtime_interval = cycle_interval * mult
  1133. * So if mult is being incremented by one:
  1134. * xtime_interval = cycle_interval * (mult + 1)
  1135. * Its the same as:
  1136. * xtime_interval = (cycle_interval * mult) + cycle_interval
  1137. * Which can be shortened to:
  1138. * xtime_interval += cycle_interval
  1139. *
  1140. * So offset stores the non-accumulated cycles. Thus the current
  1141. * time (in shifted nanoseconds) is:
  1142. * now = (offset * adj) + xtime_nsec
  1143. * Now, even though we're adjusting the clock frequency, we have
  1144. * to keep time consistent. In other words, we can't jump back
  1145. * in time, and we also want to avoid jumping forward in time.
  1146. *
  1147. * So given the same offset value, we need the time to be the same
  1148. * both before and after the freq adjustment.
  1149. * now = (offset * adj_1) + xtime_nsec_1
  1150. * now = (offset * adj_2) + xtime_nsec_2
  1151. * So:
  1152. * (offset * adj_1) + xtime_nsec_1 =
  1153. * (offset * adj_2) + xtime_nsec_2
  1154. * And we know:
  1155. * adj_2 = adj_1 + 1
  1156. * So:
  1157. * (offset * adj_1) + xtime_nsec_1 =
  1158. * (offset * (adj_1+1)) + xtime_nsec_2
  1159. * (offset * adj_1) + xtime_nsec_1 =
  1160. * (offset * adj_1) + offset + xtime_nsec_2
  1161. * Canceling the sides:
  1162. * xtime_nsec_1 = offset + xtime_nsec_2
  1163. * Which gives us:
  1164. * xtime_nsec_2 = xtime_nsec_1 - offset
  1165. * Which simplfies to:
  1166. * xtime_nsec -= offset
  1167. *
  1168. * XXX - TODO: Doc ntp_error calculation.
  1169. */
  1170. if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
  1171. /* NTP adjustment caused clocksource mult overflow */
  1172. WARN_ON_ONCE(1);
  1173. return;
  1174. }
  1175. tk->tkr.mult += mult_adj;
  1176. tk->xtime_interval += interval;
  1177. tk->tkr.xtime_nsec -= offset;
  1178. tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
  1179. }
  1180. /*
  1181. * Calculate the multiplier adjustment needed to match the frequency
  1182. * specified by NTP
  1183. */
  1184. static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
  1185. s64 offset)
  1186. {
  1187. s64 interval = tk->cycle_interval;
  1188. s64 xinterval = tk->xtime_interval;
  1189. s64 tick_error;
  1190. bool negative;
  1191. u32 adj;
  1192. /* Remove any current error adj from freq calculation */
  1193. if (tk->ntp_err_mult)
  1194. xinterval -= tk->cycle_interval;
  1195. tk->ntp_tick = ntp_tick_length();
  1196. /* Calculate current error per tick */
  1197. tick_error = ntp_tick_length() >> tk->ntp_error_shift;
  1198. tick_error -= (xinterval + tk->xtime_remainder);
  1199. /* Don't worry about correcting it if its small */
  1200. if (likely((tick_error >= 0) && (tick_error <= interval)))
  1201. return;
  1202. /* preserve the direction of correction */
  1203. negative = (tick_error < 0);
  1204. /* Sort out the magnitude of the correction */
  1205. tick_error = abs(tick_error);
  1206. for (adj = 0; tick_error > interval; adj++)
  1207. tick_error >>= 1;
  1208. /* scale the corrections */
  1209. timekeeping_apply_adjustment(tk, offset, negative, adj);
  1210. }
  1211. /*
  1212. * Adjust the timekeeper's multiplier to the correct frequency
  1213. * and also to reduce the accumulated error value.
  1214. */
  1215. static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
  1216. {
  1217. /* Correct for the current frequency error */
  1218. timekeeping_freqadjust(tk, offset);
  1219. /* Next make a small adjustment to fix any cumulative error */
  1220. if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
  1221. tk->ntp_err_mult = 1;
  1222. timekeeping_apply_adjustment(tk, offset, 0, 0);
  1223. } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
  1224. /* Undo any existing error adjustment */
  1225. timekeeping_apply_adjustment(tk, offset, 1, 0);
  1226. tk->ntp_err_mult = 0;
  1227. }
  1228. if (unlikely(tk->tkr.clock->maxadj &&
  1229. (abs(tk->tkr.mult - tk->tkr.clock->mult)
  1230. > tk->tkr.clock->maxadj))) {
  1231. printk_once(KERN_WARNING
  1232. "Adjusting %s more than 11%% (%ld vs %ld)\n",
  1233. tk->tkr.clock->name, (long)tk->tkr.mult,
  1234. (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj);
  1235. }
  1236. /*
  1237. * It may be possible that when we entered this function, xtime_nsec
  1238. * was very small. Further, if we're slightly speeding the clocksource
  1239. * in the code above, its possible the required corrective factor to
  1240. * xtime_nsec could cause it to underflow.
  1241. *
  1242. * Now, since we already accumulated the second, cannot simply roll
  1243. * the accumulated second back, since the NTP subsystem has been
  1244. * notified via second_overflow. So instead we push xtime_nsec forward
  1245. * by the amount we underflowed, and add that amount into the error.
  1246. *
  1247. * We'll correct this error next time through this function, when
  1248. * xtime_nsec is not as small.
  1249. */
  1250. if (unlikely((s64)tk->tkr.xtime_nsec < 0)) {
  1251. s64 neg = -(s64)tk->tkr.xtime_nsec;
  1252. tk->tkr.xtime_nsec = 0;
  1253. tk->ntp_error += neg << tk->ntp_error_shift;
  1254. }
  1255. }
  1256. /**
  1257. * accumulate_nsecs_to_secs - Accumulates nsecs into secs
  1258. *
  1259. * Helper function that accumulates a the nsecs greater then a second
  1260. * from the xtime_nsec field to the xtime_secs field.
  1261. * It also calls into the NTP code to handle leapsecond processing.
  1262. *
  1263. */
  1264. static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
  1265. {
  1266. u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift;
  1267. unsigned int clock_set = 0;
  1268. while (tk->tkr.xtime_nsec >= nsecps) {
  1269. int leap;
  1270. tk->tkr.xtime_nsec -= nsecps;
  1271. tk->xtime_sec++;
  1272. /* Figure out if its a leap sec and apply if needed */
  1273. leap = second_overflow(tk->xtime_sec);
  1274. if (unlikely(leap)) {
  1275. struct timespec64 ts;
  1276. tk->xtime_sec += leap;
  1277. ts.tv_sec = leap;
  1278. ts.tv_nsec = 0;
  1279. tk_set_wall_to_mono(tk,
  1280. timespec64_sub(tk->wall_to_monotonic, ts));
  1281. __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
  1282. clock_set = TK_CLOCK_WAS_SET;
  1283. }
  1284. }
  1285. return clock_set;
  1286. }
  1287. /**
  1288. * logarithmic_accumulation - shifted accumulation of cycles
  1289. *
  1290. * This functions accumulates a shifted interval of cycles into
  1291. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  1292. * loop.
  1293. *
  1294. * Returns the unconsumed cycles.
  1295. */
  1296. static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
  1297. u32 shift,
  1298. unsigned int *clock_set)
  1299. {
  1300. cycle_t interval = tk->cycle_interval << shift;
  1301. u64 raw_nsecs;
  1302. /* If the offset is smaller then a shifted interval, do nothing */
  1303. if (offset < interval)
  1304. return offset;
  1305. /* Accumulate one shifted interval */
  1306. offset -= interval;
  1307. tk->tkr.cycle_last += interval;
  1308. tk->tkr.xtime_nsec += tk->xtime_interval << shift;
  1309. *clock_set |= accumulate_nsecs_to_secs(tk);
  1310. /* Accumulate raw time */
  1311. raw_nsecs = (u64)tk->raw_interval << shift;
  1312. raw_nsecs += tk->raw_time.tv_nsec;
  1313. if (raw_nsecs >= NSEC_PER_SEC) {
  1314. u64 raw_secs = raw_nsecs;
  1315. raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
  1316. tk->raw_time.tv_sec += raw_secs;
  1317. }
  1318. tk->raw_time.tv_nsec = raw_nsecs;
  1319. /* Accumulate error between NTP and clock interval */
  1320. tk->ntp_error += tk->ntp_tick << shift;
  1321. tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
  1322. (tk->ntp_error_shift + shift);
  1323. return offset;
  1324. }
  1325. /**
  1326. * update_wall_time - Uses the current clocksource to increment the wall time
  1327. *
  1328. */
  1329. void update_wall_time(void)
  1330. {
  1331. struct timekeeper *real_tk = &tk_core.timekeeper;
  1332. struct timekeeper *tk = &shadow_timekeeper;
  1333. cycle_t offset;
  1334. int shift = 0, maxshift;
  1335. unsigned int clock_set = 0;
  1336. unsigned long flags;
  1337. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1338. /* Make sure we're fully resumed: */
  1339. if (unlikely(timekeeping_suspended))
  1340. goto out;
  1341. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  1342. offset = real_tk->cycle_interval;
  1343. #else
  1344. offset = clocksource_delta(tk->tkr.read(tk->tkr.clock),
  1345. tk->tkr.cycle_last, tk->tkr.mask);
  1346. #endif
  1347. /* Check if there's really nothing to do */
  1348. if (offset < real_tk->cycle_interval)
  1349. goto out;
  1350. /*
  1351. * With NO_HZ we may have to accumulate many cycle_intervals
  1352. * (think "ticks") worth of time at once. To do this efficiently,
  1353. * we calculate the largest doubling multiple of cycle_intervals
  1354. * that is smaller than the offset. We then accumulate that
  1355. * chunk in one go, and then try to consume the next smaller
  1356. * doubled multiple.
  1357. */
  1358. shift = ilog2(offset) - ilog2(tk->cycle_interval);
  1359. shift = max(0, shift);
  1360. /* Bound shift to one less than what overflows tick_length */
  1361. maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
  1362. shift = min(shift, maxshift);
  1363. while (offset >= tk->cycle_interval) {
  1364. offset = logarithmic_accumulation(tk, offset, shift,
  1365. &clock_set);
  1366. if (offset < tk->cycle_interval<<shift)
  1367. shift--;
  1368. }
  1369. /* correct the clock when NTP error is too big */
  1370. timekeeping_adjust(tk, offset);
  1371. /*
  1372. * XXX This can be killed once everyone converts
  1373. * to the new update_vsyscall.
  1374. */
  1375. old_vsyscall_fixup(tk);
  1376. /*
  1377. * Finally, make sure that after the rounding
  1378. * xtime_nsec isn't larger than NSEC_PER_SEC
  1379. */
  1380. clock_set |= accumulate_nsecs_to_secs(tk);
  1381. write_seqcount_begin(&tk_core.seq);
  1382. /*
  1383. * Update the real timekeeper.
  1384. *
  1385. * We could avoid this memcpy by switching pointers, but that
  1386. * requires changes to all other timekeeper usage sites as
  1387. * well, i.e. move the timekeeper pointer getter into the
  1388. * spinlocked/seqcount protected sections. And we trade this
  1389. * memcpy under the tk_core.seq against one before we start
  1390. * updating.
  1391. */
  1392. memcpy(real_tk, tk, sizeof(*tk));
  1393. timekeeping_update(real_tk, clock_set);
  1394. write_seqcount_end(&tk_core.seq);
  1395. out:
  1396. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1397. if (clock_set)
  1398. /* Have to call _delayed version, since in irq context*/
  1399. clock_was_set_delayed();
  1400. }
  1401. /**
  1402. * getboottime64 - Return the real time of system boot.
  1403. * @ts: pointer to the timespec64 to be set
  1404. *
  1405. * Returns the wall-time of boot in a timespec64.
  1406. *
  1407. * This is based on the wall_to_monotonic offset and the total suspend
  1408. * time. Calls to settimeofday will affect the value returned (which
  1409. * basically means that however wrong your real time clock is at boot time,
  1410. * you get the right time here).
  1411. */
  1412. void getboottime64(struct timespec64 *ts)
  1413. {
  1414. struct timekeeper *tk = &tk_core.timekeeper;
  1415. ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
  1416. *ts = ktime_to_timespec64(t);
  1417. }
  1418. EXPORT_SYMBOL_GPL(getboottime64);
  1419. unsigned long get_seconds(void)
  1420. {
  1421. struct timekeeper *tk = &tk_core.timekeeper;
  1422. return tk->xtime_sec;
  1423. }
  1424. EXPORT_SYMBOL(get_seconds);
  1425. struct timespec __current_kernel_time(void)
  1426. {
  1427. struct timekeeper *tk = &tk_core.timekeeper;
  1428. return timespec64_to_timespec(tk_xtime(tk));
  1429. }
  1430. struct timespec current_kernel_time(void)
  1431. {
  1432. struct timekeeper *tk = &tk_core.timekeeper;
  1433. struct timespec64 now;
  1434. unsigned long seq;
  1435. do {
  1436. seq = read_seqcount_begin(&tk_core.seq);
  1437. now = tk_xtime(tk);
  1438. } while (read_seqcount_retry(&tk_core.seq, seq));
  1439. return timespec64_to_timespec(now);
  1440. }
  1441. EXPORT_SYMBOL(current_kernel_time);
  1442. struct timespec64 get_monotonic_coarse64(void)
  1443. {
  1444. struct timekeeper *tk = &tk_core.timekeeper;
  1445. struct timespec64 now, mono;
  1446. unsigned long seq;
  1447. do {
  1448. seq = read_seqcount_begin(&tk_core.seq);
  1449. now = tk_xtime(tk);
  1450. mono = tk->wall_to_monotonic;
  1451. } while (read_seqcount_retry(&tk_core.seq, seq));
  1452. set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
  1453. now.tv_nsec + mono.tv_nsec);
  1454. return now;
  1455. }
  1456. /*
  1457. * Must hold jiffies_lock
  1458. */
  1459. void do_timer(unsigned long ticks)
  1460. {
  1461. jiffies_64 += ticks;
  1462. calc_global_load(ticks);
  1463. }
  1464. /**
  1465. * ktime_get_update_offsets_tick - hrtimer helper
  1466. * @offs_real: pointer to storage for monotonic -> realtime offset
  1467. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1468. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1469. *
  1470. * Returns monotonic time at last tick and various offsets
  1471. */
  1472. ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
  1473. ktime_t *offs_tai)
  1474. {
  1475. struct timekeeper *tk = &tk_core.timekeeper;
  1476. unsigned int seq;
  1477. ktime_t base;
  1478. u64 nsecs;
  1479. do {
  1480. seq = read_seqcount_begin(&tk_core.seq);
  1481. base = tk->tkr.base_mono;
  1482. nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift;
  1483. *offs_real = tk->offs_real;
  1484. *offs_boot = tk->offs_boot;
  1485. *offs_tai = tk->offs_tai;
  1486. } while (read_seqcount_retry(&tk_core.seq, seq));
  1487. return ktime_add_ns(base, nsecs);
  1488. }
  1489. #ifdef CONFIG_HIGH_RES_TIMERS
  1490. /**
  1491. * ktime_get_update_offsets_now - hrtimer helper
  1492. * @offs_real: pointer to storage for monotonic -> realtime offset
  1493. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1494. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1495. *
  1496. * Returns current monotonic time and updates the offsets
  1497. * Called from hrtimer_interrupt() or retrigger_next_event()
  1498. */
  1499. ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
  1500. ktime_t *offs_tai)
  1501. {
  1502. struct timekeeper *tk = &tk_core.timekeeper;
  1503. unsigned int seq;
  1504. ktime_t base;
  1505. u64 nsecs;
  1506. do {
  1507. seq = read_seqcount_begin(&tk_core.seq);
  1508. base = tk->tkr.base_mono;
  1509. nsecs = timekeeping_get_ns(&tk->tkr);
  1510. *offs_real = tk->offs_real;
  1511. *offs_boot = tk->offs_boot;
  1512. *offs_tai = tk->offs_tai;
  1513. } while (read_seqcount_retry(&tk_core.seq, seq));
  1514. return ktime_add_ns(base, nsecs);
  1515. }
  1516. #endif
  1517. /**
  1518. * do_adjtimex() - Accessor function to NTP __do_adjtimex function
  1519. */
  1520. int do_adjtimex(struct timex *txc)
  1521. {
  1522. struct timekeeper *tk = &tk_core.timekeeper;
  1523. unsigned long flags;
  1524. struct timespec64 ts;
  1525. s32 orig_tai, tai;
  1526. int ret;
  1527. /* Validate the data before disabling interrupts */
  1528. ret = ntp_validate_timex(txc);
  1529. if (ret)
  1530. return ret;
  1531. if (txc->modes & ADJ_SETOFFSET) {
  1532. struct timespec delta;
  1533. delta.tv_sec = txc->time.tv_sec;
  1534. delta.tv_nsec = txc->time.tv_usec;
  1535. if (!(txc->modes & ADJ_NANO))
  1536. delta.tv_nsec *= 1000;
  1537. ret = timekeeping_inject_offset(&delta);
  1538. if (ret)
  1539. return ret;
  1540. }
  1541. getnstimeofday64(&ts);
  1542. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1543. write_seqcount_begin(&tk_core.seq);
  1544. orig_tai = tai = tk->tai_offset;
  1545. ret = __do_adjtimex(txc, &ts, &tai);
  1546. if (tai != orig_tai) {
  1547. __timekeeping_set_tai_offset(tk, tai);
  1548. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1549. }
  1550. write_seqcount_end(&tk_core.seq);
  1551. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1552. if (tai != orig_tai)
  1553. clock_was_set();
  1554. ntp_notify_cmos_timer();
  1555. return ret;
  1556. }
  1557. #ifdef CONFIG_NTP_PPS
  1558. /**
  1559. * hardpps() - Accessor function to NTP __hardpps function
  1560. */
  1561. void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
  1562. {
  1563. unsigned long flags;
  1564. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1565. write_seqcount_begin(&tk_core.seq);
  1566. __hardpps(phase_ts, raw_ts);
  1567. write_seqcount_end(&tk_core.seq);
  1568. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1569. }
  1570. EXPORT_SYMBOL(hardpps);
  1571. #endif
  1572. /**
  1573. * xtime_update() - advances the timekeeping infrastructure
  1574. * @ticks: number of ticks, that have elapsed since the last call.
  1575. *
  1576. * Must be called with interrupts disabled.
  1577. */
  1578. void xtime_update(unsigned long ticks)
  1579. {
  1580. write_seqlock(&jiffies_lock);
  1581. do_timer(ticks);
  1582. write_sequnlock(&jiffies_lock);
  1583. update_wall_time();
  1584. }