timekeeping.c 65 KB

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