timekeeping.c 67 KB

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