timekeeping.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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. * __getnstimeofday64 - Returns the time of day in a timespec64.
  604. * @ts: pointer to the timespec to be set
  605. *
  606. * Updates the time of day in the timespec.
  607. * Returns 0 on success, or -ve when suspended (timespec will be undefined).
  608. */
  609. int __getnstimeofday64(struct timespec64 *ts)
  610. {
  611. struct timekeeper *tk = &tk_core.timekeeper;
  612. unsigned long seq;
  613. u64 nsecs;
  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. * Do not bail out early, in case there were callers still using
  623. * the value, even in the face of the WARN_ON.
  624. */
  625. if (unlikely(timekeeping_suspended))
  626. return -EAGAIN;
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(__getnstimeofday64);
  630. /**
  631. * getnstimeofday64 - Returns the time of day in a timespec64.
  632. * @ts: pointer to the timespec64 to be set
  633. *
  634. * Returns the time of day in a timespec64 (WARN if suspended).
  635. */
  636. void getnstimeofday64(struct timespec64 *ts)
  637. {
  638. WARN_ON(__getnstimeofday64(ts));
  639. }
  640. EXPORT_SYMBOL(getnstimeofday64);
  641. ktime_t ktime_get(void)
  642. {
  643. struct timekeeper *tk = &tk_core.timekeeper;
  644. unsigned int seq;
  645. ktime_t base;
  646. u64 nsecs;
  647. WARN_ON(timekeeping_suspended);
  648. do {
  649. seq = read_seqcount_begin(&tk_core.seq);
  650. base = tk->tkr_mono.base;
  651. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  652. } while (read_seqcount_retry(&tk_core.seq, seq));
  653. return ktime_add_ns(base, nsecs);
  654. }
  655. EXPORT_SYMBOL_GPL(ktime_get);
  656. u32 ktime_get_resolution_ns(void)
  657. {
  658. struct timekeeper *tk = &tk_core.timekeeper;
  659. unsigned int seq;
  660. u32 nsecs;
  661. WARN_ON(timekeeping_suspended);
  662. do {
  663. seq = read_seqcount_begin(&tk_core.seq);
  664. nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
  665. } while (read_seqcount_retry(&tk_core.seq, seq));
  666. return nsecs;
  667. }
  668. EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
  669. static ktime_t *offsets[TK_OFFS_MAX] = {
  670. [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
  671. [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
  672. [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
  673. };
  674. ktime_t ktime_get_with_offset(enum tk_offsets offs)
  675. {
  676. struct timekeeper *tk = &tk_core.timekeeper;
  677. unsigned int seq;
  678. ktime_t base, *offset = offsets[offs];
  679. u64 nsecs;
  680. WARN_ON(timekeeping_suspended);
  681. do {
  682. seq = read_seqcount_begin(&tk_core.seq);
  683. base = ktime_add(tk->tkr_mono.base, *offset);
  684. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  685. } while (read_seqcount_retry(&tk_core.seq, seq));
  686. return ktime_add_ns(base, nsecs);
  687. }
  688. EXPORT_SYMBOL_GPL(ktime_get_with_offset);
  689. /**
  690. * ktime_mono_to_any() - convert mononotic time to any other time
  691. * @tmono: time to convert.
  692. * @offs: which offset to use
  693. */
  694. ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
  695. {
  696. ktime_t *offset = offsets[offs];
  697. unsigned long seq;
  698. ktime_t tconv;
  699. do {
  700. seq = read_seqcount_begin(&tk_core.seq);
  701. tconv = ktime_add(tmono, *offset);
  702. } while (read_seqcount_retry(&tk_core.seq, seq));
  703. return tconv;
  704. }
  705. EXPORT_SYMBOL_GPL(ktime_mono_to_any);
  706. /**
  707. * ktime_get_raw - Returns the raw monotonic time in ktime_t format
  708. */
  709. ktime_t ktime_get_raw(void)
  710. {
  711. struct timekeeper *tk = &tk_core.timekeeper;
  712. unsigned int seq;
  713. ktime_t base;
  714. u64 nsecs;
  715. do {
  716. seq = read_seqcount_begin(&tk_core.seq);
  717. base = tk->tkr_raw.base;
  718. nsecs = timekeeping_get_ns(&tk->tkr_raw);
  719. } while (read_seqcount_retry(&tk_core.seq, seq));
  720. return ktime_add_ns(base, nsecs);
  721. }
  722. EXPORT_SYMBOL_GPL(ktime_get_raw);
  723. /**
  724. * ktime_get_ts64 - get the monotonic clock in timespec64 format
  725. * @ts: pointer to timespec variable
  726. *
  727. * The function calculates the monotonic clock from the realtime
  728. * clock and the wall_to_monotonic offset and stores the result
  729. * in normalized timespec64 format in the variable pointed to by @ts.
  730. */
  731. void ktime_get_ts64(struct timespec64 *ts)
  732. {
  733. struct timekeeper *tk = &tk_core.timekeeper;
  734. struct timespec64 tomono;
  735. unsigned int seq;
  736. u64 nsec;
  737. WARN_ON(timekeeping_suspended);
  738. do {
  739. seq = read_seqcount_begin(&tk_core.seq);
  740. ts->tv_sec = tk->xtime_sec;
  741. nsec = timekeeping_get_ns(&tk->tkr_mono);
  742. tomono = tk->wall_to_monotonic;
  743. } while (read_seqcount_retry(&tk_core.seq, seq));
  744. ts->tv_sec += tomono.tv_sec;
  745. ts->tv_nsec = 0;
  746. timespec64_add_ns(ts, nsec + tomono.tv_nsec);
  747. }
  748. EXPORT_SYMBOL_GPL(ktime_get_ts64);
  749. /**
  750. * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
  751. *
  752. * Returns the seconds portion of CLOCK_MONOTONIC with a single non
  753. * serialized read. tk->ktime_sec is of type 'unsigned long' so this
  754. * works on both 32 and 64 bit systems. On 32 bit systems the readout
  755. * covers ~136 years of uptime which should be enough to prevent
  756. * premature wrap arounds.
  757. */
  758. time64_t ktime_get_seconds(void)
  759. {
  760. struct timekeeper *tk = &tk_core.timekeeper;
  761. WARN_ON(timekeeping_suspended);
  762. return tk->ktime_sec;
  763. }
  764. EXPORT_SYMBOL_GPL(ktime_get_seconds);
  765. /**
  766. * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
  767. *
  768. * Returns the wall clock seconds since 1970. This replaces the
  769. * get_seconds() interface which is not y2038 safe on 32bit systems.
  770. *
  771. * For 64bit systems the fast access to tk->xtime_sec is preserved. On
  772. * 32bit systems the access must be protected with the sequence
  773. * counter to provide "atomic" access to the 64bit tk->xtime_sec
  774. * value.
  775. */
  776. time64_t ktime_get_real_seconds(void)
  777. {
  778. struct timekeeper *tk = &tk_core.timekeeper;
  779. time64_t seconds;
  780. unsigned int seq;
  781. if (IS_ENABLED(CONFIG_64BIT))
  782. return tk->xtime_sec;
  783. do {
  784. seq = read_seqcount_begin(&tk_core.seq);
  785. seconds = tk->xtime_sec;
  786. } while (read_seqcount_retry(&tk_core.seq, seq));
  787. return seconds;
  788. }
  789. EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
  790. /**
  791. * __ktime_get_real_seconds - The same as ktime_get_real_seconds
  792. * but without the sequence counter protect. This internal function
  793. * is called just when timekeeping lock is already held.
  794. */
  795. time64_t __ktime_get_real_seconds(void)
  796. {
  797. struct timekeeper *tk = &tk_core.timekeeper;
  798. return tk->xtime_sec;
  799. }
  800. /**
  801. * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
  802. * @systime_snapshot: pointer to struct receiving the system time snapshot
  803. */
  804. void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
  805. {
  806. struct timekeeper *tk = &tk_core.timekeeper;
  807. unsigned long seq;
  808. ktime_t base_raw;
  809. ktime_t base_real;
  810. u64 nsec_raw;
  811. u64 nsec_real;
  812. u64 now;
  813. WARN_ON_ONCE(timekeeping_suspended);
  814. do {
  815. seq = read_seqcount_begin(&tk_core.seq);
  816. now = tk_clock_read(&tk->tkr_mono);
  817. systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
  818. systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
  819. base_real = ktime_add(tk->tkr_mono.base,
  820. tk_core.timekeeper.offs_real);
  821. base_raw = tk->tkr_raw.base;
  822. nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
  823. nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
  824. } while (read_seqcount_retry(&tk_core.seq, seq));
  825. systime_snapshot->cycles = now;
  826. systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
  827. systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
  828. }
  829. EXPORT_SYMBOL_GPL(ktime_get_snapshot);
  830. /* Scale base by mult/div checking for overflow */
  831. static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
  832. {
  833. u64 tmp, rem;
  834. tmp = div64_u64_rem(*base, div, &rem);
  835. if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
  836. ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
  837. return -EOVERFLOW;
  838. tmp *= mult;
  839. rem *= mult;
  840. do_div(rem, div);
  841. *base = tmp + rem;
  842. return 0;
  843. }
  844. /**
  845. * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
  846. * @history: Snapshot representing start of history
  847. * @partial_history_cycles: Cycle offset into history (fractional part)
  848. * @total_history_cycles: Total history length in cycles
  849. * @discontinuity: True indicates clock was set on history period
  850. * @ts: Cross timestamp that should be adjusted using
  851. * partial/total ratio
  852. *
  853. * Helper function used by get_device_system_crosststamp() to correct the
  854. * crosstimestamp corresponding to the start of the current interval to the
  855. * system counter value (timestamp point) provided by the driver. The
  856. * total_history_* quantities are the total history starting at the provided
  857. * reference point and ending at the start of the current interval. The cycle
  858. * count between the driver timestamp point and the start of the current
  859. * interval is partial_history_cycles.
  860. */
  861. static int adjust_historical_crosststamp(struct system_time_snapshot *history,
  862. u64 partial_history_cycles,
  863. u64 total_history_cycles,
  864. bool discontinuity,
  865. struct system_device_crosststamp *ts)
  866. {
  867. struct timekeeper *tk = &tk_core.timekeeper;
  868. u64 corr_raw, corr_real;
  869. bool interp_forward;
  870. int ret;
  871. if (total_history_cycles == 0 || partial_history_cycles == 0)
  872. return 0;
  873. /* Interpolate shortest distance from beginning or end of history */
  874. interp_forward = partial_history_cycles > total_history_cycles / 2;
  875. partial_history_cycles = interp_forward ?
  876. total_history_cycles - partial_history_cycles :
  877. partial_history_cycles;
  878. /*
  879. * Scale the monotonic raw time delta by:
  880. * partial_history_cycles / total_history_cycles
  881. */
  882. corr_raw = (u64)ktime_to_ns(
  883. ktime_sub(ts->sys_monoraw, history->raw));
  884. ret = scale64_check_overflow(partial_history_cycles,
  885. total_history_cycles, &corr_raw);
  886. if (ret)
  887. return ret;
  888. /*
  889. * If there is a discontinuity in the history, scale monotonic raw
  890. * correction by:
  891. * mult(real)/mult(raw) yielding the realtime correction
  892. * Otherwise, calculate the realtime correction similar to monotonic
  893. * raw calculation
  894. */
  895. if (discontinuity) {
  896. corr_real = mul_u64_u32_div
  897. (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
  898. } else {
  899. corr_real = (u64)ktime_to_ns(
  900. ktime_sub(ts->sys_realtime, history->real));
  901. ret = scale64_check_overflow(partial_history_cycles,
  902. total_history_cycles, &corr_real);
  903. if (ret)
  904. return ret;
  905. }
  906. /* Fixup monotonic raw and real time time values */
  907. if (interp_forward) {
  908. ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
  909. ts->sys_realtime = ktime_add_ns(history->real, corr_real);
  910. } else {
  911. ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
  912. ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
  913. }
  914. return 0;
  915. }
  916. /*
  917. * cycle_between - true if test occurs chronologically between before and after
  918. */
  919. static bool cycle_between(u64 before, u64 test, u64 after)
  920. {
  921. if (test > before && test < after)
  922. return true;
  923. if (test < before && before > after)
  924. return true;
  925. return false;
  926. }
  927. /**
  928. * get_device_system_crosststamp - Synchronously capture system/device timestamp
  929. * @get_time_fn: Callback to get simultaneous device time and
  930. * system counter from the device driver
  931. * @ctx: Context passed to get_time_fn()
  932. * @history_begin: Historical reference point used to interpolate system
  933. * time when counter provided by the driver is before the current interval
  934. * @xtstamp: Receives simultaneously captured system and device time
  935. *
  936. * Reads a timestamp from a device and correlates it to system time
  937. */
  938. int get_device_system_crosststamp(int (*get_time_fn)
  939. (ktime_t *device_time,
  940. struct system_counterval_t *sys_counterval,
  941. void *ctx),
  942. void *ctx,
  943. struct system_time_snapshot *history_begin,
  944. struct system_device_crosststamp *xtstamp)
  945. {
  946. struct system_counterval_t system_counterval;
  947. struct timekeeper *tk = &tk_core.timekeeper;
  948. u64 cycles, now, interval_start;
  949. unsigned int clock_was_set_seq = 0;
  950. ktime_t base_real, base_raw;
  951. u64 nsec_real, nsec_raw;
  952. u8 cs_was_changed_seq;
  953. unsigned long seq;
  954. bool do_interp;
  955. int ret;
  956. do {
  957. seq = read_seqcount_begin(&tk_core.seq);
  958. /*
  959. * Try to synchronously capture device time and a system
  960. * counter value calling back into the device driver
  961. */
  962. ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
  963. if (ret)
  964. return ret;
  965. /*
  966. * Verify that the clocksource associated with the captured
  967. * system counter value is the same as the currently installed
  968. * timekeeper clocksource
  969. */
  970. if (tk->tkr_mono.clock != system_counterval.cs)
  971. return -ENODEV;
  972. cycles = system_counterval.cycles;
  973. /*
  974. * Check whether the system counter value provided by the
  975. * device driver is on the current timekeeping interval.
  976. */
  977. now = tk_clock_read(&tk->tkr_mono);
  978. interval_start = tk->tkr_mono.cycle_last;
  979. if (!cycle_between(interval_start, cycles, now)) {
  980. clock_was_set_seq = tk->clock_was_set_seq;
  981. cs_was_changed_seq = tk->cs_was_changed_seq;
  982. cycles = interval_start;
  983. do_interp = true;
  984. } else {
  985. do_interp = false;
  986. }
  987. base_real = ktime_add(tk->tkr_mono.base,
  988. tk_core.timekeeper.offs_real);
  989. base_raw = tk->tkr_raw.base;
  990. nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
  991. system_counterval.cycles);
  992. nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
  993. system_counterval.cycles);
  994. } while (read_seqcount_retry(&tk_core.seq, seq));
  995. xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
  996. xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
  997. /*
  998. * Interpolate if necessary, adjusting back from the start of the
  999. * current interval
  1000. */
  1001. if (do_interp) {
  1002. u64 partial_history_cycles, total_history_cycles;
  1003. bool discontinuity;
  1004. /*
  1005. * Check that the counter value occurs after the provided
  1006. * history reference and that the history doesn't cross a
  1007. * clocksource change
  1008. */
  1009. if (!history_begin ||
  1010. !cycle_between(history_begin->cycles,
  1011. system_counterval.cycles, cycles) ||
  1012. history_begin->cs_was_changed_seq != cs_was_changed_seq)
  1013. return -EINVAL;
  1014. partial_history_cycles = cycles - system_counterval.cycles;
  1015. total_history_cycles = cycles - history_begin->cycles;
  1016. discontinuity =
  1017. history_begin->clock_was_set_seq != clock_was_set_seq;
  1018. ret = adjust_historical_crosststamp(history_begin,
  1019. partial_history_cycles,
  1020. total_history_cycles,
  1021. discontinuity, xtstamp);
  1022. if (ret)
  1023. return ret;
  1024. }
  1025. return 0;
  1026. }
  1027. EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
  1028. /**
  1029. * do_gettimeofday - Returns the time of day in a timeval
  1030. * @tv: pointer to the timeval to be set
  1031. *
  1032. * NOTE: Users should be converted to using getnstimeofday()
  1033. */
  1034. void do_gettimeofday(struct timeval *tv)
  1035. {
  1036. struct timespec64 now;
  1037. getnstimeofday64(&now);
  1038. tv->tv_sec = now.tv_sec;
  1039. tv->tv_usec = now.tv_nsec/1000;
  1040. }
  1041. EXPORT_SYMBOL(do_gettimeofday);
  1042. /**
  1043. * do_settimeofday64 - Sets the time of day.
  1044. * @ts: pointer to the timespec64 variable containing the new time
  1045. *
  1046. * Sets the time of day to the new time and update NTP and notify hrtimers
  1047. */
  1048. int do_settimeofday64(const struct timespec64 *ts)
  1049. {
  1050. struct timekeeper *tk = &tk_core.timekeeper;
  1051. struct timespec64 ts_delta, xt;
  1052. unsigned long flags;
  1053. int ret = 0;
  1054. if (!timespec64_valid_strict(ts))
  1055. return -EINVAL;
  1056. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1057. write_seqcount_begin(&tk_core.seq);
  1058. timekeeping_forward_now(tk);
  1059. xt = tk_xtime(tk);
  1060. ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
  1061. ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
  1062. if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
  1063. ret = -EINVAL;
  1064. goto out;
  1065. }
  1066. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
  1067. tk_set_xtime(tk, ts);
  1068. out:
  1069. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1070. write_seqcount_end(&tk_core.seq);
  1071. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1072. /* signal hrtimers about time change */
  1073. clock_was_set();
  1074. return ret;
  1075. }
  1076. EXPORT_SYMBOL(do_settimeofday64);
  1077. /**
  1078. * timekeeping_inject_offset - Adds or subtracts from the current time.
  1079. * @tv: pointer to the timespec variable containing the offset
  1080. *
  1081. * Adds or subtracts an offset value from the current time.
  1082. */
  1083. static int timekeeping_inject_offset(struct timespec64 *ts)
  1084. {
  1085. struct timekeeper *tk = &tk_core.timekeeper;
  1086. unsigned long flags;
  1087. struct timespec64 tmp;
  1088. int ret = 0;
  1089. if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
  1090. return -EINVAL;
  1091. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1092. write_seqcount_begin(&tk_core.seq);
  1093. timekeeping_forward_now(tk);
  1094. /* Make sure the proposed value is valid */
  1095. tmp = timespec64_add(tk_xtime(tk), *ts);
  1096. if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 ||
  1097. !timespec64_valid_strict(&tmp)) {
  1098. ret = -EINVAL;
  1099. goto error;
  1100. }
  1101. tk_xtime_add(tk, ts);
  1102. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts));
  1103. error: /* even if we error out, we forwarded the time, so call update */
  1104. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1105. write_seqcount_end(&tk_core.seq);
  1106. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1107. /* signal hrtimers about time change */
  1108. clock_was_set();
  1109. return ret;
  1110. }
  1111. /*
  1112. * Indicates if there is an offset between the system clock and the hardware
  1113. * clock/persistent clock/rtc.
  1114. */
  1115. int persistent_clock_is_local;
  1116. /*
  1117. * Adjust the time obtained from the CMOS to be UTC time instead of
  1118. * local time.
  1119. *
  1120. * This is ugly, but preferable to the alternatives. Otherwise we
  1121. * would either need to write a program to do it in /etc/rc (and risk
  1122. * confusion if the program gets run more than once; it would also be
  1123. * hard to make the program warp the clock precisely n hours) or
  1124. * compile in the timezone information into the kernel. Bad, bad....
  1125. *
  1126. * - TYT, 1992-01-01
  1127. *
  1128. * The best thing to do is to keep the CMOS clock in universal time (UTC)
  1129. * as real UNIX machines always do it. This avoids all headaches about
  1130. * daylight saving times and warping kernel clocks.
  1131. */
  1132. void timekeeping_warp_clock(void)
  1133. {
  1134. if (sys_tz.tz_minuteswest != 0) {
  1135. struct timespec64 adjust;
  1136. persistent_clock_is_local = 1;
  1137. adjust.tv_sec = sys_tz.tz_minuteswest * 60;
  1138. adjust.tv_nsec = 0;
  1139. timekeeping_inject_offset(&adjust);
  1140. }
  1141. }
  1142. /**
  1143. * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic
  1144. *
  1145. */
  1146. static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
  1147. {
  1148. tk->tai_offset = tai_offset;
  1149. tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
  1150. }
  1151. /**
  1152. * change_clocksource - Swaps clocksources if a new one is available
  1153. *
  1154. * Accumulates current time interval and initializes new clocksource
  1155. */
  1156. static int change_clocksource(void *data)
  1157. {
  1158. struct timekeeper *tk = &tk_core.timekeeper;
  1159. struct clocksource *new, *old;
  1160. unsigned long flags;
  1161. new = (struct clocksource *) data;
  1162. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1163. write_seqcount_begin(&tk_core.seq);
  1164. timekeeping_forward_now(tk);
  1165. /*
  1166. * If the cs is in module, get a module reference. Succeeds
  1167. * for built-in code (owner == NULL) as well.
  1168. */
  1169. if (try_module_get(new->owner)) {
  1170. if (!new->enable || new->enable(new) == 0) {
  1171. old = tk->tkr_mono.clock;
  1172. tk_setup_internals(tk, new);
  1173. if (old->disable)
  1174. old->disable(old);
  1175. module_put(old->owner);
  1176. } else {
  1177. module_put(new->owner);
  1178. }
  1179. }
  1180. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1181. write_seqcount_end(&tk_core.seq);
  1182. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1183. return 0;
  1184. }
  1185. /**
  1186. * timekeeping_notify - Install a new clock source
  1187. * @clock: pointer to the clock source
  1188. *
  1189. * This function is called from clocksource.c after a new, better clock
  1190. * source has been registered. The caller holds the clocksource_mutex.
  1191. */
  1192. int timekeeping_notify(struct clocksource *clock)
  1193. {
  1194. struct timekeeper *tk = &tk_core.timekeeper;
  1195. if (tk->tkr_mono.clock == clock)
  1196. return 0;
  1197. stop_machine(change_clocksource, clock, NULL);
  1198. tick_clock_notify();
  1199. return tk->tkr_mono.clock == clock ? 0 : -1;
  1200. }
  1201. /**
  1202. * getrawmonotonic64 - Returns the raw monotonic time in a timespec
  1203. * @ts: pointer to the timespec64 to be set
  1204. *
  1205. * Returns the raw monotonic time (completely un-modified by ntp)
  1206. */
  1207. void getrawmonotonic64(struct timespec64 *ts)
  1208. {
  1209. struct timekeeper *tk = &tk_core.timekeeper;
  1210. unsigned long seq;
  1211. u64 nsecs;
  1212. do {
  1213. seq = read_seqcount_begin(&tk_core.seq);
  1214. ts->tv_sec = tk->raw_sec;
  1215. nsecs = timekeeping_get_ns(&tk->tkr_raw);
  1216. } while (read_seqcount_retry(&tk_core.seq, seq));
  1217. ts->tv_nsec = 0;
  1218. timespec64_add_ns(ts, nsecs);
  1219. }
  1220. EXPORT_SYMBOL(getrawmonotonic64);
  1221. /**
  1222. * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  1223. */
  1224. int timekeeping_valid_for_hres(void)
  1225. {
  1226. struct timekeeper *tk = &tk_core.timekeeper;
  1227. unsigned long seq;
  1228. int ret;
  1229. do {
  1230. seq = read_seqcount_begin(&tk_core.seq);
  1231. ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
  1232. } while (read_seqcount_retry(&tk_core.seq, seq));
  1233. return ret;
  1234. }
  1235. /**
  1236. * timekeeping_max_deferment - Returns max time the clocksource can be deferred
  1237. */
  1238. u64 timekeeping_max_deferment(void)
  1239. {
  1240. struct timekeeper *tk = &tk_core.timekeeper;
  1241. unsigned long seq;
  1242. u64 ret;
  1243. do {
  1244. seq = read_seqcount_begin(&tk_core.seq);
  1245. ret = tk->tkr_mono.clock->max_idle_ns;
  1246. } while (read_seqcount_retry(&tk_core.seq, seq));
  1247. return ret;
  1248. }
  1249. /**
  1250. * read_persistent_clock - Return time from the persistent clock.
  1251. *
  1252. * Weak dummy function for arches that do not yet support it.
  1253. * Reads the time from the battery backed persistent clock.
  1254. * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  1255. *
  1256. * XXX - Do be sure to remove it once all arches implement it.
  1257. */
  1258. void __weak read_persistent_clock(struct timespec *ts)
  1259. {
  1260. ts->tv_sec = 0;
  1261. ts->tv_nsec = 0;
  1262. }
  1263. void __weak read_persistent_clock64(struct timespec64 *ts64)
  1264. {
  1265. struct timespec ts;
  1266. read_persistent_clock(&ts);
  1267. *ts64 = timespec_to_timespec64(ts);
  1268. }
  1269. /**
  1270. * read_boot_clock64 - Return time of the system start.
  1271. *
  1272. * Weak dummy function for arches that do not yet support it.
  1273. * Function to read the exact time the system has been started.
  1274. * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
  1275. *
  1276. * XXX - Do be sure to remove it once all arches implement it.
  1277. */
  1278. void __weak read_boot_clock64(struct timespec64 *ts)
  1279. {
  1280. ts->tv_sec = 0;
  1281. ts->tv_nsec = 0;
  1282. }
  1283. /* Flag for if timekeeping_resume() has injected sleeptime */
  1284. static bool sleeptime_injected;
  1285. /* Flag for if there is a persistent clock on this platform */
  1286. static bool persistent_clock_exists;
  1287. /*
  1288. * timekeeping_init - Initializes the clocksource and common timekeeping values
  1289. */
  1290. void __init timekeeping_init(void)
  1291. {
  1292. struct timekeeper *tk = &tk_core.timekeeper;
  1293. struct clocksource *clock;
  1294. unsigned long flags;
  1295. struct timespec64 now, boot, tmp;
  1296. read_persistent_clock64(&now);
  1297. if (!timespec64_valid_strict(&now)) {
  1298. pr_warn("WARNING: Persistent clock returned invalid value!\n"
  1299. " Check your CMOS/BIOS settings.\n");
  1300. now.tv_sec = 0;
  1301. now.tv_nsec = 0;
  1302. } else if (now.tv_sec || now.tv_nsec)
  1303. persistent_clock_exists = true;
  1304. read_boot_clock64(&boot);
  1305. if (!timespec64_valid_strict(&boot)) {
  1306. pr_warn("WARNING: Boot clock returned invalid value!\n"
  1307. " Check your CMOS/BIOS settings.\n");
  1308. boot.tv_sec = 0;
  1309. boot.tv_nsec = 0;
  1310. }
  1311. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1312. write_seqcount_begin(&tk_core.seq);
  1313. ntp_init();
  1314. clock = clocksource_default_clock();
  1315. if (clock->enable)
  1316. clock->enable(clock);
  1317. tk_setup_internals(tk, clock);
  1318. tk_set_xtime(tk, &now);
  1319. tk->raw_sec = 0;
  1320. if (boot.tv_sec == 0 && boot.tv_nsec == 0)
  1321. boot = tk_xtime(tk);
  1322. set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
  1323. tk_set_wall_to_mono(tk, tmp);
  1324. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1325. write_seqcount_end(&tk_core.seq);
  1326. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1327. }
  1328. /* time in seconds when suspend began for persistent clock */
  1329. static struct timespec64 timekeeping_suspend_time;
  1330. /**
  1331. * __timekeeping_inject_sleeptime - Internal function to add sleep interval
  1332. * @delta: pointer to a timespec delta value
  1333. *
  1334. * Takes a timespec offset measuring a suspend interval and properly
  1335. * adds the sleep offset to the timekeeping variables.
  1336. */
  1337. static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
  1338. struct timespec64 *delta)
  1339. {
  1340. if (!timespec64_valid_strict(delta)) {
  1341. printk_deferred(KERN_WARNING
  1342. "__timekeeping_inject_sleeptime: Invalid "
  1343. "sleep delta value!\n");
  1344. return;
  1345. }
  1346. tk_xtime_add(tk, delta);
  1347. tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
  1348. tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
  1349. tk_debug_account_sleep_time(delta);
  1350. }
  1351. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
  1352. /**
  1353. * We have three kinds of time sources to use for sleep time
  1354. * injection, the preference order is:
  1355. * 1) non-stop clocksource
  1356. * 2) persistent clock (ie: RTC accessible when irqs are off)
  1357. * 3) RTC
  1358. *
  1359. * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
  1360. * If system has neither 1) nor 2), 3) will be used finally.
  1361. *
  1362. *
  1363. * If timekeeping has injected sleeptime via either 1) or 2),
  1364. * 3) becomes needless, so in this case we don't need to call
  1365. * rtc_resume(), and this is what timekeeping_rtc_skipresume()
  1366. * means.
  1367. */
  1368. bool timekeeping_rtc_skipresume(void)
  1369. {
  1370. return sleeptime_injected;
  1371. }
  1372. /**
  1373. * 1) can be determined whether to use or not only when doing
  1374. * timekeeping_resume() which is invoked after rtc_suspend(),
  1375. * so we can't skip rtc_suspend() surely if system has 1).
  1376. *
  1377. * But if system has 2), 2) will definitely be used, so in this
  1378. * case we don't need to call rtc_suspend(), and this is what
  1379. * timekeeping_rtc_skipsuspend() means.
  1380. */
  1381. bool timekeeping_rtc_skipsuspend(void)
  1382. {
  1383. return persistent_clock_exists;
  1384. }
  1385. /**
  1386. * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
  1387. * @delta: pointer to a timespec64 delta value
  1388. *
  1389. * This hook is for architectures that cannot support read_persistent_clock64
  1390. * because their RTC/persistent clock is only accessible when irqs are enabled.
  1391. * and also don't have an effective nonstop clocksource.
  1392. *
  1393. * This function should only be called by rtc_resume(), and allows
  1394. * a suspend offset to be injected into the timekeeping values.
  1395. */
  1396. void timekeeping_inject_sleeptime64(struct timespec64 *delta)
  1397. {
  1398. struct timekeeper *tk = &tk_core.timekeeper;
  1399. unsigned long flags;
  1400. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1401. write_seqcount_begin(&tk_core.seq);
  1402. timekeeping_forward_now(tk);
  1403. __timekeeping_inject_sleeptime(tk, delta);
  1404. timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
  1405. write_seqcount_end(&tk_core.seq);
  1406. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1407. /* signal hrtimers about time change */
  1408. clock_was_set();
  1409. }
  1410. #endif
  1411. /**
  1412. * timekeeping_resume - Resumes the generic timekeeping subsystem.
  1413. */
  1414. void timekeeping_resume(void)
  1415. {
  1416. struct timekeeper *tk = &tk_core.timekeeper;
  1417. struct clocksource *clock = tk->tkr_mono.clock;
  1418. unsigned long flags;
  1419. struct timespec64 ts_new, ts_delta;
  1420. u64 cycle_now;
  1421. sleeptime_injected = false;
  1422. read_persistent_clock64(&ts_new);
  1423. clockevents_resume();
  1424. clocksource_resume();
  1425. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1426. write_seqcount_begin(&tk_core.seq);
  1427. /*
  1428. * After system resumes, we need to calculate the suspended time and
  1429. * compensate it for the OS time. There are 3 sources that could be
  1430. * used: Nonstop clocksource during suspend, persistent clock and rtc
  1431. * device.
  1432. *
  1433. * One specific platform may have 1 or 2 or all of them, and the
  1434. * preference will be:
  1435. * suspend-nonstop clocksource -> persistent clock -> rtc
  1436. * The less preferred source will only be tried if there is no better
  1437. * usable source. The rtc part is handled separately in rtc core code.
  1438. */
  1439. cycle_now = tk_clock_read(&tk->tkr_mono);
  1440. if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
  1441. cycle_now > tk->tkr_mono.cycle_last) {
  1442. u64 nsec, cyc_delta;
  1443. cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
  1444. tk->tkr_mono.mask);
  1445. nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
  1446. ts_delta = ns_to_timespec64(nsec);
  1447. sleeptime_injected = true;
  1448. } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  1449. ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
  1450. sleeptime_injected = true;
  1451. }
  1452. if (sleeptime_injected)
  1453. __timekeeping_inject_sleeptime(tk, &ts_delta);
  1454. /* Re-base the last cycle value */
  1455. tk->tkr_mono.cycle_last = cycle_now;
  1456. tk->tkr_raw.cycle_last = cycle_now;
  1457. tk->ntp_error = 0;
  1458. timekeeping_suspended = 0;
  1459. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1460. write_seqcount_end(&tk_core.seq);
  1461. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1462. touch_softlockup_watchdog();
  1463. tick_resume();
  1464. hrtimers_resume();
  1465. }
  1466. int timekeeping_suspend(void)
  1467. {
  1468. struct timekeeper *tk = &tk_core.timekeeper;
  1469. unsigned long flags;
  1470. struct timespec64 delta, delta_delta;
  1471. static struct timespec64 old_delta;
  1472. read_persistent_clock64(&timekeeping_suspend_time);
  1473. /*
  1474. * On some systems the persistent_clock can not be detected at
  1475. * timekeeping_init by its return value, so if we see a valid
  1476. * value returned, update the persistent_clock_exists flag.
  1477. */
  1478. if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
  1479. persistent_clock_exists = true;
  1480. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1481. write_seqcount_begin(&tk_core.seq);
  1482. timekeeping_forward_now(tk);
  1483. timekeeping_suspended = 1;
  1484. if (persistent_clock_exists) {
  1485. /*
  1486. * To avoid drift caused by repeated suspend/resumes,
  1487. * which each can add ~1 second drift error,
  1488. * try to compensate so the difference in system time
  1489. * and persistent_clock time stays close to constant.
  1490. */
  1491. delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
  1492. delta_delta = timespec64_sub(delta, old_delta);
  1493. if (abs(delta_delta.tv_sec) >= 2) {
  1494. /*
  1495. * if delta_delta is too large, assume time correction
  1496. * has occurred and set old_delta to the current delta.
  1497. */
  1498. old_delta = delta;
  1499. } else {
  1500. /* Otherwise try to adjust old_system to compensate */
  1501. timekeeping_suspend_time =
  1502. timespec64_add(timekeeping_suspend_time, delta_delta);
  1503. }
  1504. }
  1505. timekeeping_update(tk, TK_MIRROR);
  1506. halt_fast_timekeeper(tk);
  1507. write_seqcount_end(&tk_core.seq);
  1508. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1509. tick_suspend();
  1510. clocksource_suspend();
  1511. clockevents_suspend();
  1512. return 0;
  1513. }
  1514. /* sysfs resume/suspend bits for timekeeping */
  1515. static struct syscore_ops timekeeping_syscore_ops = {
  1516. .resume = timekeeping_resume,
  1517. .suspend = timekeeping_suspend,
  1518. };
  1519. static int __init timekeeping_init_ops(void)
  1520. {
  1521. register_syscore_ops(&timekeeping_syscore_ops);
  1522. return 0;
  1523. }
  1524. device_initcall(timekeeping_init_ops);
  1525. /*
  1526. * Apply a multiplier adjustment to the timekeeper
  1527. */
  1528. static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  1529. s64 offset,
  1530. s32 mult_adj)
  1531. {
  1532. s64 interval = tk->cycle_interval;
  1533. if (mult_adj == 0) {
  1534. return;
  1535. } else if (mult_adj == -1) {
  1536. interval = -interval;
  1537. offset = -offset;
  1538. } else if (mult_adj != 1) {
  1539. interval *= mult_adj;
  1540. offset *= mult_adj;
  1541. }
  1542. /*
  1543. * So the following can be confusing.
  1544. *
  1545. * To keep things simple, lets assume mult_adj == 1 for now.
  1546. *
  1547. * When mult_adj != 1, remember that the interval and offset values
  1548. * have been appropriately scaled so the math is the same.
  1549. *
  1550. * The basic idea here is that we're increasing the multiplier
  1551. * by one, this causes the xtime_interval to be incremented by
  1552. * one cycle_interval. This is because:
  1553. * xtime_interval = cycle_interval * mult
  1554. * So if mult is being incremented by one:
  1555. * xtime_interval = cycle_interval * (mult + 1)
  1556. * Its the same as:
  1557. * xtime_interval = (cycle_interval * mult) + cycle_interval
  1558. * Which can be shortened to:
  1559. * xtime_interval += cycle_interval
  1560. *
  1561. * So offset stores the non-accumulated cycles. Thus the current
  1562. * time (in shifted nanoseconds) is:
  1563. * now = (offset * adj) + xtime_nsec
  1564. * Now, even though we're adjusting the clock frequency, we have
  1565. * to keep time consistent. In other words, we can't jump back
  1566. * in time, and we also want to avoid jumping forward in time.
  1567. *
  1568. * So given the same offset value, we need the time to be the same
  1569. * both before and after the freq adjustment.
  1570. * now = (offset * adj_1) + xtime_nsec_1
  1571. * now = (offset * adj_2) + xtime_nsec_2
  1572. * So:
  1573. * (offset * adj_1) + xtime_nsec_1 =
  1574. * (offset * adj_2) + xtime_nsec_2
  1575. * And we know:
  1576. * adj_2 = adj_1 + 1
  1577. * So:
  1578. * (offset * adj_1) + xtime_nsec_1 =
  1579. * (offset * (adj_1+1)) + xtime_nsec_2
  1580. * (offset * adj_1) + xtime_nsec_1 =
  1581. * (offset * adj_1) + offset + xtime_nsec_2
  1582. * Canceling the sides:
  1583. * xtime_nsec_1 = offset + xtime_nsec_2
  1584. * Which gives us:
  1585. * xtime_nsec_2 = xtime_nsec_1 - offset
  1586. * Which simplfies to:
  1587. * xtime_nsec -= offset
  1588. */
  1589. if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
  1590. /* NTP adjustment caused clocksource mult overflow */
  1591. WARN_ON_ONCE(1);
  1592. return;
  1593. }
  1594. tk->tkr_mono.mult += mult_adj;
  1595. tk->xtime_interval += interval;
  1596. tk->tkr_mono.xtime_nsec -= offset;
  1597. }
  1598. /*
  1599. * Adjust the timekeeper's multiplier to the correct frequency
  1600. * and also to reduce the accumulated error value.
  1601. */
  1602. static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
  1603. {
  1604. u32 mult;
  1605. /*
  1606. * Determine the multiplier from the current NTP tick length.
  1607. * Avoid expensive division when the tick length doesn't change.
  1608. */
  1609. if (likely(tk->ntp_tick == ntp_tick_length())) {
  1610. mult = tk->tkr_mono.mult - tk->ntp_err_mult;
  1611. } else {
  1612. tk->ntp_tick = ntp_tick_length();
  1613. mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) -
  1614. tk->xtime_remainder, tk->cycle_interval);
  1615. }
  1616. /*
  1617. * If the clock is behind the NTP time, increase the multiplier by 1
  1618. * to catch up with it. If it's ahead and there was a remainder in the
  1619. * tick division, the clock will slow down. Otherwise it will stay
  1620. * ahead until the tick length changes to a non-divisible value.
  1621. */
  1622. tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0;
  1623. mult += tk->ntp_err_mult;
  1624. timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult);
  1625. if (unlikely(tk->tkr_mono.clock->maxadj &&
  1626. (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
  1627. > tk->tkr_mono.clock->maxadj))) {
  1628. printk_once(KERN_WARNING
  1629. "Adjusting %s more than 11%% (%ld vs %ld)\n",
  1630. tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
  1631. (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
  1632. }
  1633. /*
  1634. * It may be possible that when we entered this function, xtime_nsec
  1635. * was very small. Further, if we're slightly speeding the clocksource
  1636. * in the code above, its possible the required corrective factor to
  1637. * xtime_nsec could cause it to underflow.
  1638. *
  1639. * Now, since we have already accumulated the second and the NTP
  1640. * subsystem has been notified via second_overflow(), we need to skip
  1641. * the next update.
  1642. */
  1643. if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
  1644. tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC <<
  1645. tk->tkr_mono.shift;
  1646. tk->xtime_sec--;
  1647. tk->skip_second_overflow = 1;
  1648. }
  1649. }
  1650. /**
  1651. * accumulate_nsecs_to_secs - Accumulates nsecs into secs
  1652. *
  1653. * Helper function that accumulates the nsecs greater than a second
  1654. * from the xtime_nsec field to the xtime_secs field.
  1655. * It also calls into the NTP code to handle leapsecond processing.
  1656. *
  1657. */
  1658. static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
  1659. {
  1660. u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
  1661. unsigned int clock_set = 0;
  1662. while (tk->tkr_mono.xtime_nsec >= nsecps) {
  1663. int leap;
  1664. tk->tkr_mono.xtime_nsec -= nsecps;
  1665. tk->xtime_sec++;
  1666. /*
  1667. * Skip NTP update if this second was accumulated before,
  1668. * i.e. xtime_nsec underflowed in timekeeping_adjust()
  1669. */
  1670. if (unlikely(tk->skip_second_overflow)) {
  1671. tk->skip_second_overflow = 0;
  1672. continue;
  1673. }
  1674. /* Figure out if its a leap sec and apply if needed */
  1675. leap = second_overflow(tk->xtime_sec);
  1676. if (unlikely(leap)) {
  1677. struct timespec64 ts;
  1678. tk->xtime_sec += leap;
  1679. ts.tv_sec = leap;
  1680. ts.tv_nsec = 0;
  1681. tk_set_wall_to_mono(tk,
  1682. timespec64_sub(tk->wall_to_monotonic, ts));
  1683. __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
  1684. clock_set = TK_CLOCK_WAS_SET;
  1685. }
  1686. }
  1687. return clock_set;
  1688. }
  1689. /**
  1690. * logarithmic_accumulation - shifted accumulation of cycles
  1691. *
  1692. * This functions accumulates a shifted interval of cycles into
  1693. * into a shifted interval nanoseconds. Allows for O(log) accumulation
  1694. * loop.
  1695. *
  1696. * Returns the unconsumed cycles.
  1697. */
  1698. static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
  1699. u32 shift, unsigned int *clock_set)
  1700. {
  1701. u64 interval = tk->cycle_interval << shift;
  1702. u64 snsec_per_sec;
  1703. /* If the offset is smaller than a shifted interval, do nothing */
  1704. if (offset < interval)
  1705. return offset;
  1706. /* Accumulate one shifted interval */
  1707. offset -= interval;
  1708. tk->tkr_mono.cycle_last += interval;
  1709. tk->tkr_raw.cycle_last += interval;
  1710. tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
  1711. *clock_set |= accumulate_nsecs_to_secs(tk);
  1712. /* Accumulate raw time */
  1713. tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
  1714. snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
  1715. while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
  1716. tk->tkr_raw.xtime_nsec -= snsec_per_sec;
  1717. tk->raw_sec++;
  1718. }
  1719. /* Accumulate error between NTP and clock interval */
  1720. tk->ntp_error += tk->ntp_tick << shift;
  1721. tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
  1722. (tk->ntp_error_shift + shift);
  1723. return offset;
  1724. }
  1725. /**
  1726. * update_wall_time - Uses the current clocksource to increment the wall time
  1727. *
  1728. */
  1729. void update_wall_time(void)
  1730. {
  1731. struct timekeeper *real_tk = &tk_core.timekeeper;
  1732. struct timekeeper *tk = &shadow_timekeeper;
  1733. u64 offset;
  1734. int shift = 0, maxshift;
  1735. unsigned int clock_set = 0;
  1736. unsigned long flags;
  1737. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1738. /* Make sure we're fully resumed: */
  1739. if (unlikely(timekeeping_suspended))
  1740. goto out;
  1741. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  1742. offset = real_tk->cycle_interval;
  1743. #else
  1744. offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
  1745. tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
  1746. #endif
  1747. /* Check if there's really nothing to do */
  1748. if (offset < real_tk->cycle_interval)
  1749. goto out;
  1750. /* Do some additional sanity checking */
  1751. timekeeping_check_update(tk, offset);
  1752. /*
  1753. * With NO_HZ we may have to accumulate many cycle_intervals
  1754. * (think "ticks") worth of time at once. To do this efficiently,
  1755. * we calculate the largest doubling multiple of cycle_intervals
  1756. * that is smaller than the offset. We then accumulate that
  1757. * chunk in one go, and then try to consume the next smaller
  1758. * doubled multiple.
  1759. */
  1760. shift = ilog2(offset) - ilog2(tk->cycle_interval);
  1761. shift = max(0, shift);
  1762. /* Bound shift to one less than what overflows tick_length */
  1763. maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
  1764. shift = min(shift, maxshift);
  1765. while (offset >= tk->cycle_interval) {
  1766. offset = logarithmic_accumulation(tk, offset, shift,
  1767. &clock_set);
  1768. if (offset < tk->cycle_interval<<shift)
  1769. shift--;
  1770. }
  1771. /* Adjust the multiplier to correct NTP error */
  1772. timekeeping_adjust(tk, offset);
  1773. /*
  1774. * Finally, make sure that after the rounding
  1775. * xtime_nsec isn't larger than NSEC_PER_SEC
  1776. */
  1777. clock_set |= accumulate_nsecs_to_secs(tk);
  1778. write_seqcount_begin(&tk_core.seq);
  1779. /*
  1780. * Update the real timekeeper.
  1781. *
  1782. * We could avoid this memcpy by switching pointers, but that
  1783. * requires changes to all other timekeeper usage sites as
  1784. * well, i.e. move the timekeeper pointer getter into the
  1785. * spinlocked/seqcount protected sections. And we trade this
  1786. * memcpy under the tk_core.seq against one before we start
  1787. * updating.
  1788. */
  1789. timekeeping_update(tk, clock_set);
  1790. memcpy(real_tk, tk, sizeof(*tk));
  1791. /* The memcpy must come last. Do not put anything here! */
  1792. write_seqcount_end(&tk_core.seq);
  1793. out:
  1794. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1795. if (clock_set)
  1796. /* Have to call _delayed version, since in irq context*/
  1797. clock_was_set_delayed();
  1798. }
  1799. /**
  1800. * getboottime64 - Return the real time of system boot.
  1801. * @ts: pointer to the timespec64 to be set
  1802. *
  1803. * Returns the wall-time of boot in a timespec64.
  1804. *
  1805. * This is based on the wall_to_monotonic offset and the total suspend
  1806. * time. Calls to settimeofday will affect the value returned (which
  1807. * basically means that however wrong your real time clock is at boot time,
  1808. * you get the right time here).
  1809. */
  1810. void getboottime64(struct timespec64 *ts)
  1811. {
  1812. struct timekeeper *tk = &tk_core.timekeeper;
  1813. ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
  1814. *ts = ktime_to_timespec64(t);
  1815. }
  1816. EXPORT_SYMBOL_GPL(getboottime64);
  1817. unsigned long get_seconds(void)
  1818. {
  1819. struct timekeeper *tk = &tk_core.timekeeper;
  1820. return tk->xtime_sec;
  1821. }
  1822. EXPORT_SYMBOL(get_seconds);
  1823. struct timespec64 current_kernel_time64(void)
  1824. {
  1825. struct timekeeper *tk = &tk_core.timekeeper;
  1826. struct timespec64 now;
  1827. unsigned long seq;
  1828. do {
  1829. seq = read_seqcount_begin(&tk_core.seq);
  1830. now = tk_xtime(tk);
  1831. } while (read_seqcount_retry(&tk_core.seq, seq));
  1832. return now;
  1833. }
  1834. EXPORT_SYMBOL(current_kernel_time64);
  1835. struct timespec64 get_monotonic_coarse64(void)
  1836. {
  1837. struct timekeeper *tk = &tk_core.timekeeper;
  1838. struct timespec64 now, mono;
  1839. unsigned long seq;
  1840. do {
  1841. seq = read_seqcount_begin(&tk_core.seq);
  1842. now = tk_xtime(tk);
  1843. mono = tk->wall_to_monotonic;
  1844. } while (read_seqcount_retry(&tk_core.seq, seq));
  1845. set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
  1846. now.tv_nsec + mono.tv_nsec);
  1847. return now;
  1848. }
  1849. EXPORT_SYMBOL(get_monotonic_coarse64);
  1850. /*
  1851. * Must hold jiffies_lock
  1852. */
  1853. void do_timer(unsigned long ticks)
  1854. {
  1855. jiffies_64 += ticks;
  1856. calc_global_load(ticks);
  1857. }
  1858. /**
  1859. * ktime_get_update_offsets_now - hrtimer helper
  1860. * @cwsseq: pointer to check and store the clock was set sequence number
  1861. * @offs_real: pointer to storage for monotonic -> realtime offset
  1862. * @offs_boot: pointer to storage for monotonic -> boottime offset
  1863. * @offs_tai: pointer to storage for monotonic -> clock tai offset
  1864. *
  1865. * Returns current monotonic time and updates the offsets if the
  1866. * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
  1867. * different.
  1868. *
  1869. * Called from hrtimer_interrupt() or retrigger_next_event()
  1870. */
  1871. ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
  1872. ktime_t *offs_boot, ktime_t *offs_tai)
  1873. {
  1874. struct timekeeper *tk = &tk_core.timekeeper;
  1875. unsigned int seq;
  1876. ktime_t base;
  1877. u64 nsecs;
  1878. do {
  1879. seq = read_seqcount_begin(&tk_core.seq);
  1880. base = tk->tkr_mono.base;
  1881. nsecs = timekeeping_get_ns(&tk->tkr_mono);
  1882. base = ktime_add_ns(base, nsecs);
  1883. if (*cwsseq != tk->clock_was_set_seq) {
  1884. *cwsseq = tk->clock_was_set_seq;
  1885. *offs_real = tk->offs_real;
  1886. *offs_boot = tk->offs_boot;
  1887. *offs_tai = tk->offs_tai;
  1888. }
  1889. /* Handle leapsecond insertion adjustments */
  1890. if (unlikely(base >= tk->next_leap_ktime))
  1891. *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
  1892. } while (read_seqcount_retry(&tk_core.seq, seq));
  1893. return base;
  1894. }
  1895. /**
  1896. * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex
  1897. */
  1898. static int timekeeping_validate_timex(struct timex *txc)
  1899. {
  1900. if (txc->modes & ADJ_ADJTIME) {
  1901. /* singleshot must not be used with any other mode bits */
  1902. if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
  1903. return -EINVAL;
  1904. if (!(txc->modes & ADJ_OFFSET_READONLY) &&
  1905. !capable(CAP_SYS_TIME))
  1906. return -EPERM;
  1907. } else {
  1908. /* In order to modify anything, you gotta be super-user! */
  1909. if (txc->modes && !capable(CAP_SYS_TIME))
  1910. return -EPERM;
  1911. /*
  1912. * if the quartz is off by more than 10% then
  1913. * something is VERY wrong!
  1914. */
  1915. if (txc->modes & ADJ_TICK &&
  1916. (txc->tick < 900000/USER_HZ ||
  1917. txc->tick > 1100000/USER_HZ))
  1918. return -EINVAL;
  1919. }
  1920. if (txc->modes & ADJ_SETOFFSET) {
  1921. /* In order to inject time, you gotta be super-user! */
  1922. if (!capable(CAP_SYS_TIME))
  1923. return -EPERM;
  1924. /*
  1925. * Validate if a timespec/timeval used to inject a time
  1926. * offset is valid. Offsets can be postive or negative, so
  1927. * we don't check tv_sec. The value of the timeval/timespec
  1928. * is the sum of its fields,but *NOTE*:
  1929. * The field tv_usec/tv_nsec must always be non-negative and
  1930. * we can't have more nanoseconds/microseconds than a second.
  1931. */
  1932. if (txc->time.tv_usec < 0)
  1933. return -EINVAL;
  1934. if (txc->modes & ADJ_NANO) {
  1935. if (txc->time.tv_usec >= NSEC_PER_SEC)
  1936. return -EINVAL;
  1937. } else {
  1938. if (txc->time.tv_usec >= USEC_PER_SEC)
  1939. return -EINVAL;
  1940. }
  1941. }
  1942. /*
  1943. * Check for potential multiplication overflows that can
  1944. * only happen on 64-bit systems:
  1945. */
  1946. if ((txc->modes & ADJ_FREQUENCY) && (BITS_PER_LONG == 64)) {
  1947. if (LLONG_MIN / PPM_SCALE > txc->freq)
  1948. return -EINVAL;
  1949. if (LLONG_MAX / PPM_SCALE < txc->freq)
  1950. return -EINVAL;
  1951. }
  1952. return 0;
  1953. }
  1954. /**
  1955. * do_adjtimex() - Accessor function to NTP __do_adjtimex function
  1956. */
  1957. int do_adjtimex(struct timex *txc)
  1958. {
  1959. struct timekeeper *tk = &tk_core.timekeeper;
  1960. unsigned long flags;
  1961. struct timespec64 ts;
  1962. s32 orig_tai, tai;
  1963. int ret;
  1964. /* Validate the data before disabling interrupts */
  1965. ret = timekeeping_validate_timex(txc);
  1966. if (ret)
  1967. return ret;
  1968. if (txc->modes & ADJ_SETOFFSET) {
  1969. struct timespec64 delta;
  1970. delta.tv_sec = txc->time.tv_sec;
  1971. delta.tv_nsec = txc->time.tv_usec;
  1972. if (!(txc->modes & ADJ_NANO))
  1973. delta.tv_nsec *= 1000;
  1974. ret = timekeeping_inject_offset(&delta);
  1975. if (ret)
  1976. return ret;
  1977. }
  1978. getnstimeofday64(&ts);
  1979. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  1980. write_seqcount_begin(&tk_core.seq);
  1981. orig_tai = tai = tk->tai_offset;
  1982. ret = __do_adjtimex(txc, &ts, &tai);
  1983. if (tai != orig_tai) {
  1984. __timekeeping_set_tai_offset(tk, tai);
  1985. timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
  1986. }
  1987. tk_update_leap_state(tk);
  1988. write_seqcount_end(&tk_core.seq);
  1989. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  1990. if (tai != orig_tai)
  1991. clock_was_set();
  1992. ntp_notify_cmos_timer();
  1993. return ret;
  1994. }
  1995. #ifdef CONFIG_NTP_PPS
  1996. /**
  1997. * hardpps() - Accessor function to NTP __hardpps function
  1998. */
  1999. void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
  2000. {
  2001. unsigned long flags;
  2002. raw_spin_lock_irqsave(&timekeeper_lock, flags);
  2003. write_seqcount_begin(&tk_core.seq);
  2004. __hardpps(phase_ts, raw_ts);
  2005. write_seqcount_end(&tk_core.seq);
  2006. raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
  2007. }
  2008. EXPORT_SYMBOL(hardpps);
  2009. #endif /* CONFIG_NTP_PPS */
  2010. /**
  2011. * xtime_update() - advances the timekeeping infrastructure
  2012. * @ticks: number of ticks, that have elapsed since the last call.
  2013. *
  2014. * Must be called with interrupts disabled.
  2015. */
  2016. void xtime_update(unsigned long ticks)
  2017. {
  2018. write_seqlock(&jiffies_lock);
  2019. do_timer(ticks);
  2020. write_sequnlock(&jiffies_lock);
  2021. update_wall_time();
  2022. }