timekeeping.c 65 KB

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