time.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. *
  5. * This file contains the interface functions for the various time related
  6. * system calls: time, stime, gettimeofday, settimeofday, adjtime
  7. *
  8. * Modification history:
  9. *
  10. * 1993-09-02 Philip Gladstone
  11. * Created file with time related functions from sched/core.c and adjtimex()
  12. * 1993-10-08 Torsten Duwe
  13. * adjtime interface update and CMOS clock write code
  14. * 1995-08-13 Torsten Duwe
  15. * kernel PLL updated to 1994-12-13 specs (rfc-1589)
  16. * 1999-01-16 Ulrich Windl
  17. * Introduced error checking for many cases in adjtimex().
  18. * Updated NTP code according to technical memorandum Jan '96
  19. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  20. * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
  21. * (Even though the technical memorandum forbids it)
  22. * 2004-07-14 Christoph Lameter
  23. * Added getnstimeofday to allow the posix timer functions to return
  24. * with nanosecond accuracy
  25. */
  26. #include <linux/export.h>
  27. #include <linux/kernel.h>
  28. #include <linux/timex.h>
  29. #include <linux/capability.h>
  30. #include <linux/timekeeper_internal.h>
  31. #include <linux/errno.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/security.h>
  34. #include <linux/fs.h>
  35. #include <linux/math64.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/compat.h>
  39. #include <asm/unistd.h>
  40. #include <generated/timeconst.h>
  41. #include "timekeeping.h"
  42. /*
  43. * The timezone where the local system is located. Used as a default by some
  44. * programs who obtain this value by using gettimeofday.
  45. */
  46. struct timezone sys_tz;
  47. EXPORT_SYMBOL(sys_tz);
  48. #ifdef __ARCH_WANT_SYS_TIME
  49. /*
  50. * sys_time() can be implemented in user-level using
  51. * sys_gettimeofday(). Is this for backwards compatibility? If so,
  52. * why not move it into the appropriate arch directory (for those
  53. * architectures that need it).
  54. */
  55. SYSCALL_DEFINE1(time, __kernel_old_time_t __user *, tloc)
  56. {
  57. __kernel_old_time_t i = (__kernel_old_time_t)ktime_get_real_seconds();
  58. if (tloc) {
  59. if (put_user(i,tloc))
  60. return -EFAULT;
  61. }
  62. force_successful_syscall_return();
  63. return i;
  64. }
  65. /*
  66. * sys_stime() can be implemented in user-level using
  67. * sys_settimeofday(). Is this for backwards compatibility? If so,
  68. * why not move it into the appropriate arch directory (for those
  69. * architectures that need it).
  70. */
  71. SYSCALL_DEFINE1(stime, __kernel_old_time_t __user *, tptr)
  72. {
  73. struct timespec64 tv;
  74. int err;
  75. if (get_user(tv.tv_sec, tptr))
  76. return -EFAULT;
  77. tv.tv_nsec = 0;
  78. err = security_settime64(&tv, NULL);
  79. if (err)
  80. return err;
  81. do_settimeofday64(&tv);
  82. return 0;
  83. }
  84. #endif /* __ARCH_WANT_SYS_TIME */
  85. #ifdef CONFIG_COMPAT_32BIT_TIME
  86. #ifdef __ARCH_WANT_SYS_TIME32
  87. /* old_time32_t is a 32 bit "long" and needs to get converted. */
  88. SYSCALL_DEFINE1(time32, old_time32_t __user *, tloc)
  89. {
  90. old_time32_t i;
  91. i = (old_time32_t)ktime_get_real_seconds();
  92. if (tloc) {
  93. if (put_user(i,tloc))
  94. return -EFAULT;
  95. }
  96. force_successful_syscall_return();
  97. return i;
  98. }
  99. SYSCALL_DEFINE1(stime32, old_time32_t __user *, tptr)
  100. {
  101. struct timespec64 tv;
  102. int err;
  103. if (get_user(tv.tv_sec, tptr))
  104. return -EFAULT;
  105. tv.tv_nsec = 0;
  106. err = security_settime64(&tv, NULL);
  107. if (err)
  108. return err;
  109. do_settimeofday64(&tv);
  110. return 0;
  111. }
  112. #endif /* __ARCH_WANT_SYS_TIME32 */
  113. #endif
  114. SYSCALL_DEFINE2(gettimeofday, struct __kernel_old_timeval __user *, tv,
  115. struct timezone __user *, tz)
  116. {
  117. if (likely(tv != NULL)) {
  118. struct timespec64 ts;
  119. ktime_get_real_ts64(&ts);
  120. if (put_user(ts.tv_sec, &tv->tv_sec) ||
  121. put_user(ts.tv_nsec / 1000, &tv->tv_usec))
  122. return -EFAULT;
  123. }
  124. if (unlikely(tz != NULL)) {
  125. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  126. return -EFAULT;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * In case for some reason the CMOS clock has not already been running
  132. * in UTC, but in some local time: The first time we set the timezone,
  133. * we will warp the clock so that it is ticking UTC time instead of
  134. * local time. Presumably, if someone is setting the timezone then we
  135. * are running in an environment where the programs understand about
  136. * timezones. This should be done at boot time in the /etc/rc script,
  137. * as soon as possible, so that the clock can be set right. Otherwise,
  138. * various programs will get confused when the clock gets warped.
  139. */
  140. int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz)
  141. {
  142. static int firsttime = 1;
  143. int error = 0;
  144. if (tv && !timespec64_valid_settod(tv))
  145. return -EINVAL;
  146. error = security_settime64(tv, tz);
  147. if (error)
  148. return error;
  149. if (tz) {
  150. /* Verify we're within the +-15 hrs range */
  151. if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60)
  152. return -EINVAL;
  153. sys_tz = *tz;
  154. update_vsyscall_tz();
  155. if (firsttime) {
  156. firsttime = 0;
  157. if (!tv)
  158. timekeeping_warp_clock();
  159. }
  160. }
  161. if (tv)
  162. return do_settimeofday64(tv);
  163. return 0;
  164. }
  165. SYSCALL_DEFINE2(settimeofday, struct __kernel_old_timeval __user *, tv,
  166. struct timezone __user *, tz)
  167. {
  168. struct timespec64 new_ts;
  169. struct timezone new_tz;
  170. if (tv) {
  171. if (get_user(new_ts.tv_sec, &tv->tv_sec) ||
  172. get_user(new_ts.tv_nsec, &tv->tv_usec))
  173. return -EFAULT;
  174. if (new_ts.tv_nsec > USEC_PER_SEC || new_ts.tv_nsec < 0)
  175. return -EINVAL;
  176. new_ts.tv_nsec *= NSEC_PER_USEC;
  177. }
  178. if (tz) {
  179. if (copy_from_user(&new_tz, tz, sizeof(*tz)))
  180. return -EFAULT;
  181. }
  182. return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
  183. }
  184. #ifdef CONFIG_COMPAT
  185. COMPAT_SYSCALL_DEFINE2(gettimeofday, struct old_timeval32 __user *, tv,
  186. struct timezone __user *, tz)
  187. {
  188. if (tv) {
  189. struct timespec64 ts;
  190. ktime_get_real_ts64(&ts);
  191. if (put_user(ts.tv_sec, &tv->tv_sec) ||
  192. put_user(ts.tv_nsec / 1000, &tv->tv_usec))
  193. return -EFAULT;
  194. }
  195. if (tz) {
  196. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  197. return -EFAULT;
  198. }
  199. return 0;
  200. }
  201. COMPAT_SYSCALL_DEFINE2(settimeofday, struct old_timeval32 __user *, tv,
  202. struct timezone __user *, tz)
  203. {
  204. struct timespec64 new_ts;
  205. struct timezone new_tz;
  206. if (tv) {
  207. if (get_user(new_ts.tv_sec, &tv->tv_sec) ||
  208. get_user(new_ts.tv_nsec, &tv->tv_usec))
  209. return -EFAULT;
  210. if (new_ts.tv_nsec > USEC_PER_SEC || new_ts.tv_nsec < 0)
  211. return -EINVAL;
  212. new_ts.tv_nsec *= NSEC_PER_USEC;
  213. }
  214. if (tz) {
  215. if (copy_from_user(&new_tz, tz, sizeof(*tz)))
  216. return -EFAULT;
  217. }
  218. return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
  219. }
  220. #endif
  221. #ifdef CONFIG_64BIT
  222. SYSCALL_DEFINE1(adjtimex, struct __kernel_timex __user *, txc_p)
  223. {
  224. struct __kernel_timex txc; /* Local copy of parameter */
  225. int ret;
  226. /* Copy the user data space into the kernel copy
  227. * structure. But bear in mind that the structures
  228. * may change
  229. */
  230. if (copy_from_user(&txc, txc_p, sizeof(struct __kernel_timex)))
  231. return -EFAULT;
  232. ret = do_adjtimex(&txc);
  233. return copy_to_user(txc_p, &txc, sizeof(struct __kernel_timex)) ? -EFAULT : ret;
  234. }
  235. #endif
  236. #ifdef CONFIG_COMPAT_32BIT_TIME
  237. int get_old_timex32(struct __kernel_timex *txc, const struct old_timex32 __user *utp)
  238. {
  239. struct old_timex32 tx32;
  240. memset(txc, 0, sizeof(struct __kernel_timex));
  241. if (copy_from_user(&tx32, utp, sizeof(struct old_timex32)))
  242. return -EFAULT;
  243. txc->modes = tx32.modes;
  244. txc->offset = tx32.offset;
  245. txc->freq = tx32.freq;
  246. txc->maxerror = tx32.maxerror;
  247. txc->esterror = tx32.esterror;
  248. txc->status = tx32.status;
  249. txc->constant = tx32.constant;
  250. txc->precision = tx32.precision;
  251. txc->tolerance = tx32.tolerance;
  252. txc->time.tv_sec = tx32.time.tv_sec;
  253. txc->time.tv_usec = tx32.time.tv_usec;
  254. txc->tick = tx32.tick;
  255. txc->ppsfreq = tx32.ppsfreq;
  256. txc->jitter = tx32.jitter;
  257. txc->shift = tx32.shift;
  258. txc->stabil = tx32.stabil;
  259. txc->jitcnt = tx32.jitcnt;
  260. txc->calcnt = tx32.calcnt;
  261. txc->errcnt = tx32.errcnt;
  262. txc->stbcnt = tx32.stbcnt;
  263. return 0;
  264. }
  265. int put_old_timex32(struct old_timex32 __user *utp, const struct __kernel_timex *txc)
  266. {
  267. struct old_timex32 tx32;
  268. memset(&tx32, 0, sizeof(struct old_timex32));
  269. tx32.modes = txc->modes;
  270. tx32.offset = txc->offset;
  271. tx32.freq = txc->freq;
  272. tx32.maxerror = txc->maxerror;
  273. tx32.esterror = txc->esterror;
  274. tx32.status = txc->status;
  275. tx32.constant = txc->constant;
  276. tx32.precision = txc->precision;
  277. tx32.tolerance = txc->tolerance;
  278. tx32.time.tv_sec = txc->time.tv_sec;
  279. tx32.time.tv_usec = txc->time.tv_usec;
  280. tx32.tick = txc->tick;
  281. tx32.ppsfreq = txc->ppsfreq;
  282. tx32.jitter = txc->jitter;
  283. tx32.shift = txc->shift;
  284. tx32.stabil = txc->stabil;
  285. tx32.jitcnt = txc->jitcnt;
  286. tx32.calcnt = txc->calcnt;
  287. tx32.errcnt = txc->errcnt;
  288. tx32.stbcnt = txc->stbcnt;
  289. tx32.tai = txc->tai;
  290. if (copy_to_user(utp, &tx32, sizeof(struct old_timex32)))
  291. return -EFAULT;
  292. return 0;
  293. }
  294. SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp)
  295. {
  296. struct __kernel_timex txc;
  297. int err, ret;
  298. err = get_old_timex32(&txc, utp);
  299. if (err)
  300. return err;
  301. ret = do_adjtimex(&txc);
  302. err = put_old_timex32(utp, &txc);
  303. if (err)
  304. return err;
  305. return ret;
  306. }
  307. #endif
  308. /*
  309. * Convert jiffies to milliseconds and back.
  310. *
  311. * Avoid unnecessary multiplications/divisions in the
  312. * two most common HZ cases:
  313. */
  314. unsigned int jiffies_to_msecs(const unsigned long j)
  315. {
  316. #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
  317. return (MSEC_PER_SEC / HZ) * j;
  318. #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
  319. return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
  320. #else
  321. # if BITS_PER_LONG == 32
  322. return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >>
  323. HZ_TO_MSEC_SHR32;
  324. # else
  325. return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
  326. # endif
  327. #endif
  328. }
  329. EXPORT_SYMBOL(jiffies_to_msecs);
  330. unsigned int jiffies_to_usecs(const unsigned long j)
  331. {
  332. /*
  333. * Hz usually doesn't go much further MSEC_PER_SEC.
  334. * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
  335. */
  336. BUILD_BUG_ON(HZ > USEC_PER_SEC);
  337. #if !(USEC_PER_SEC % HZ)
  338. return (USEC_PER_SEC / HZ) * j;
  339. #else
  340. # if BITS_PER_LONG == 32
  341. return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
  342. # else
  343. return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
  344. # endif
  345. #endif
  346. }
  347. EXPORT_SYMBOL(jiffies_to_usecs);
  348. /*
  349. * mktime64 - Converts date to seconds.
  350. * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  351. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  352. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  353. *
  354. * [For the Julian calendar (which was used in Russia before 1917,
  355. * Britain & colonies before 1752, anywhere else before 1582,
  356. * and is still in use by some communities) leave out the
  357. * -year/100+year/400 terms, and add 10.]
  358. *
  359. * This algorithm was first published by Gauss (I think).
  360. *
  361. * A leap second can be indicated by calling this function with sec as
  362. * 60 (allowable under ISO 8601). The leap second is treated the same
  363. * as the following second since they don't exist in UNIX time.
  364. *
  365. * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
  366. * tomorrow - (allowable under ISO 8601) is supported.
  367. */
  368. time64_t mktime64(const unsigned int year0, const unsigned int mon0,
  369. const unsigned int day, const unsigned int hour,
  370. const unsigned int min, const unsigned int sec)
  371. {
  372. unsigned int mon = mon0, year = year0;
  373. /* 1..12 -> 11,12,1..10 */
  374. if (0 >= (int) (mon -= 2)) {
  375. mon += 12; /* Puts Feb last since it has leap day */
  376. year -= 1;
  377. }
  378. return ((((time64_t)
  379. (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  380. year*365 - 719499
  381. )*24 + hour /* now have hours - midnight tomorrow handled here */
  382. )*60 + min /* now have minutes */
  383. )*60 + sec; /* finally seconds */
  384. }
  385. EXPORT_SYMBOL(mktime64);
  386. struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec)
  387. {
  388. struct timespec64 ts = ns_to_timespec64(nsec);
  389. struct __kernel_old_timeval tv;
  390. tv.tv_sec = ts.tv_sec;
  391. tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;
  392. return tv;
  393. }
  394. EXPORT_SYMBOL(ns_to_kernel_old_timeval);
  395. /**
  396. * set_normalized_timespec - set timespec sec and nsec parts and normalize
  397. *
  398. * @ts: pointer to timespec variable to be set
  399. * @sec: seconds to set
  400. * @nsec: nanoseconds to set
  401. *
  402. * Set seconds and nanoseconds field of a timespec variable and
  403. * normalize to the timespec storage format
  404. *
  405. * Note: The tv_nsec part is always in the range of
  406. * 0 <= tv_nsec < NSEC_PER_SEC
  407. * For negative values only the tv_sec field is negative !
  408. */
  409. void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
  410. {
  411. while (nsec >= NSEC_PER_SEC) {
  412. /*
  413. * The following asm() prevents the compiler from
  414. * optimising this loop into a modulo operation. See
  415. * also __iter_div_u64_rem() in include/linux/time.h
  416. */
  417. asm("" : "+rm"(nsec));
  418. nsec -= NSEC_PER_SEC;
  419. ++sec;
  420. }
  421. while (nsec < 0) {
  422. asm("" : "+rm"(nsec));
  423. nsec += NSEC_PER_SEC;
  424. --sec;
  425. }
  426. ts->tv_sec = sec;
  427. ts->tv_nsec = nsec;
  428. }
  429. EXPORT_SYMBOL(set_normalized_timespec64);
  430. /**
  431. * ns_to_timespec64 - Convert nanoseconds to timespec64
  432. * @nsec: the nanoseconds value to be converted
  433. *
  434. * Returns the timespec64 representation of the nsec parameter.
  435. */
  436. struct timespec64 ns_to_timespec64(const s64 nsec)
  437. {
  438. struct timespec64 ts = { 0, 0 };
  439. s32 rem;
  440. if (likely(nsec > 0)) {
  441. ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
  442. ts.tv_nsec = rem;
  443. } else if (nsec < 0) {
  444. /*
  445. * With negative times, tv_sec points to the earlier
  446. * second, and tv_nsec counts the nanoseconds since
  447. * then, so tv_nsec is always a positive number.
  448. */
  449. ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1;
  450. ts.tv_nsec = NSEC_PER_SEC - rem - 1;
  451. }
  452. return ts;
  453. }
  454. EXPORT_SYMBOL(ns_to_timespec64);
  455. /**
  456. * msecs_to_jiffies: - convert milliseconds to jiffies
  457. * @m: time in milliseconds
  458. *
  459. * conversion is done as follows:
  460. *
  461. * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
  462. *
  463. * - 'too large' values [that would result in larger than
  464. * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
  465. *
  466. * - all other values are converted to jiffies by either multiplying
  467. * the input value by a factor or dividing it with a factor and
  468. * handling any 32-bit overflows.
  469. * for the details see __msecs_to_jiffies()
  470. *
  471. * msecs_to_jiffies() checks for the passed in value being a constant
  472. * via __builtin_constant_p() allowing gcc to eliminate most of the
  473. * code, __msecs_to_jiffies() is called if the value passed does not
  474. * allow constant folding and the actual conversion must be done at
  475. * runtime.
  476. * the _msecs_to_jiffies helpers are the HZ dependent conversion
  477. * routines found in include/linux/jiffies.h
  478. */
  479. unsigned long __msecs_to_jiffies(const unsigned int m)
  480. {
  481. /*
  482. * Negative value, means infinite timeout:
  483. */
  484. if ((int)m < 0)
  485. return MAX_JIFFY_OFFSET;
  486. return _msecs_to_jiffies(m);
  487. }
  488. EXPORT_SYMBOL(__msecs_to_jiffies);
  489. unsigned long __usecs_to_jiffies(const unsigned int u)
  490. {
  491. if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
  492. return MAX_JIFFY_OFFSET;
  493. return _usecs_to_jiffies(u);
  494. }
  495. EXPORT_SYMBOL(__usecs_to_jiffies);
  496. /*
  497. * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
  498. * that a remainder subtract here would not do the right thing as the
  499. * resolution values don't fall on second boundries. I.e. the line:
  500. * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
  501. * Note that due to the small error in the multiplier here, this
  502. * rounding is incorrect for sufficiently large values of tv_nsec, but
  503. * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
  504. * OK.
  505. *
  506. * Rather, we just shift the bits off the right.
  507. *
  508. * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
  509. * value to a scaled second value.
  510. */
  511. unsigned long
  512. timespec64_to_jiffies(const struct timespec64 *value)
  513. {
  514. u64 sec = value->tv_sec;
  515. long nsec = value->tv_nsec + TICK_NSEC - 1;
  516. if (sec >= MAX_SEC_IN_JIFFIES){
  517. sec = MAX_SEC_IN_JIFFIES;
  518. nsec = 0;
  519. }
  520. return ((sec * SEC_CONVERSION) +
  521. (((u64)nsec * NSEC_CONVERSION) >>
  522. (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
  523. }
  524. EXPORT_SYMBOL(timespec64_to_jiffies);
  525. void
  526. jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
  527. {
  528. /*
  529. * Convert jiffies to nanoseconds and separate with
  530. * one divide.
  531. */
  532. u32 rem;
  533. value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
  534. NSEC_PER_SEC, &rem);
  535. value->tv_nsec = rem;
  536. }
  537. EXPORT_SYMBOL(jiffies_to_timespec64);
  538. /*
  539. * Convert jiffies/jiffies_64 to clock_t and back.
  540. */
  541. clock_t jiffies_to_clock_t(unsigned long x)
  542. {
  543. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  544. # if HZ < USER_HZ
  545. return x * (USER_HZ / HZ);
  546. # else
  547. return x / (HZ / USER_HZ);
  548. # endif
  549. #else
  550. return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
  551. #endif
  552. }
  553. EXPORT_SYMBOL(jiffies_to_clock_t);
  554. unsigned long clock_t_to_jiffies(unsigned long x)
  555. {
  556. #if (HZ % USER_HZ)==0
  557. if (x >= ~0UL / (HZ / USER_HZ))
  558. return ~0UL;
  559. return x * (HZ / USER_HZ);
  560. #else
  561. /* Don't worry about loss of precision here .. */
  562. if (x >= ~0UL / HZ * USER_HZ)
  563. return ~0UL;
  564. /* .. but do try to contain it here */
  565. return div_u64((u64)x * HZ, USER_HZ);
  566. #endif
  567. }
  568. EXPORT_SYMBOL(clock_t_to_jiffies);
  569. u64 jiffies_64_to_clock_t(u64 x)
  570. {
  571. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  572. # if HZ < USER_HZ
  573. x = div_u64(x * USER_HZ, HZ);
  574. # elif HZ > USER_HZ
  575. x = div_u64(x, HZ / USER_HZ);
  576. # else
  577. /* Nothing to do */
  578. # endif
  579. #else
  580. /*
  581. * There are better ways that don't overflow early,
  582. * but even this doesn't overflow in hundreds of years
  583. * in 64 bits, so..
  584. */
  585. x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
  586. #endif
  587. return x;
  588. }
  589. EXPORT_SYMBOL(jiffies_64_to_clock_t);
  590. u64 nsec_to_clock_t(u64 x)
  591. {
  592. #if (NSEC_PER_SEC % USER_HZ) == 0
  593. return div_u64(x, NSEC_PER_SEC / USER_HZ);
  594. #elif (USER_HZ % 512) == 0
  595. return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
  596. #else
  597. /*
  598. * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
  599. * overflow after 64.99 years.
  600. * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
  601. */
  602. return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
  603. #endif
  604. }
  605. EXPORT_SYMBOL_GPL(nsec_to_clock_t);
  606. u64 jiffies64_to_nsecs(u64 j)
  607. {
  608. #if !(NSEC_PER_SEC % HZ)
  609. return (NSEC_PER_SEC / HZ) * j;
  610. # else
  611. return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN);
  612. #endif
  613. }
  614. EXPORT_SYMBOL(jiffies64_to_nsecs);
  615. u64 jiffies64_to_msecs(const u64 j)
  616. {
  617. #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
  618. return (MSEC_PER_SEC / HZ) * j;
  619. #else
  620. return div_u64(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
  621. #endif
  622. }
  623. EXPORT_SYMBOL(jiffies64_to_msecs);
  624. /**
  625. * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
  626. *
  627. * @n: nsecs in u64
  628. *
  629. * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
  630. * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
  631. * for scheduler, not for use in device drivers to calculate timeout value.
  632. *
  633. * note:
  634. * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
  635. * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
  636. */
  637. u64 nsecs_to_jiffies64(u64 n)
  638. {
  639. #if (NSEC_PER_SEC % HZ) == 0
  640. /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
  641. return div_u64(n, NSEC_PER_SEC / HZ);
  642. #elif (HZ % 512) == 0
  643. /* overflow after 292 years if HZ = 1024 */
  644. return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
  645. #else
  646. /*
  647. * Generic case - optimized for cases where HZ is a multiple of 3.
  648. * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
  649. */
  650. return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
  651. #endif
  652. }
  653. EXPORT_SYMBOL(nsecs_to_jiffies64);
  654. /**
  655. * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
  656. *
  657. * @n: nsecs in u64
  658. *
  659. * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
  660. * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
  661. * for scheduler, not for use in device drivers to calculate timeout value.
  662. *
  663. * note:
  664. * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
  665. * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
  666. */
  667. unsigned long nsecs_to_jiffies(u64 n)
  668. {
  669. return (unsigned long)nsecs_to_jiffies64(n);
  670. }
  671. EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
  672. /*
  673. * Add two timespec64 values and do a safety check for overflow.
  674. * It's assumed that both values are valid (>= 0).
  675. * And, each timespec64 is in normalized form.
  676. */
  677. struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
  678. const struct timespec64 rhs)
  679. {
  680. struct timespec64 res;
  681. set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec,
  682. lhs.tv_nsec + rhs.tv_nsec);
  683. if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) {
  684. res.tv_sec = TIME64_MAX;
  685. res.tv_nsec = 0;
  686. }
  687. return res;
  688. }
  689. int get_timespec64(struct timespec64 *ts,
  690. const struct __kernel_timespec __user *uts)
  691. {
  692. struct __kernel_timespec kts;
  693. int ret;
  694. ret = copy_from_user(&kts, uts, sizeof(kts));
  695. if (ret)
  696. return -EFAULT;
  697. ts->tv_sec = kts.tv_sec;
  698. /* Zero out the padding in compat mode */
  699. if (in_compat_syscall())
  700. kts.tv_nsec &= 0xFFFFFFFFUL;
  701. /* In 32-bit mode, this drops the padding */
  702. ts->tv_nsec = kts.tv_nsec;
  703. return 0;
  704. }
  705. EXPORT_SYMBOL_GPL(get_timespec64);
  706. int put_timespec64(const struct timespec64 *ts,
  707. struct __kernel_timespec __user *uts)
  708. {
  709. struct __kernel_timespec kts = {
  710. .tv_sec = ts->tv_sec,
  711. .tv_nsec = ts->tv_nsec
  712. };
  713. return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0;
  714. }
  715. EXPORT_SYMBOL_GPL(put_timespec64);
  716. static int __get_old_timespec32(struct timespec64 *ts64,
  717. const struct old_timespec32 __user *cts)
  718. {
  719. struct old_timespec32 ts;
  720. int ret;
  721. ret = copy_from_user(&ts, cts, sizeof(ts));
  722. if (ret)
  723. return -EFAULT;
  724. ts64->tv_sec = ts.tv_sec;
  725. ts64->tv_nsec = ts.tv_nsec;
  726. return 0;
  727. }
  728. static int __put_old_timespec32(const struct timespec64 *ts64,
  729. struct old_timespec32 __user *cts)
  730. {
  731. struct old_timespec32 ts = {
  732. .tv_sec = ts64->tv_sec,
  733. .tv_nsec = ts64->tv_nsec
  734. };
  735. return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0;
  736. }
  737. int get_old_timespec32(struct timespec64 *ts, const void __user *uts)
  738. {
  739. if (COMPAT_USE_64BIT_TIME)
  740. return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
  741. else
  742. return __get_old_timespec32(ts, uts);
  743. }
  744. EXPORT_SYMBOL_GPL(get_old_timespec32);
  745. int put_old_timespec32(const struct timespec64 *ts, void __user *uts)
  746. {
  747. if (COMPAT_USE_64BIT_TIME)
  748. return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
  749. else
  750. return __put_old_timespec32(ts, uts);
  751. }
  752. EXPORT_SYMBOL_GPL(put_old_timespec32);
  753. int get_itimerspec64(struct itimerspec64 *it,
  754. const struct __kernel_itimerspec __user *uit)
  755. {
  756. int ret;
  757. ret = get_timespec64(&it->it_interval, &uit->it_interval);
  758. if (ret)
  759. return ret;
  760. ret = get_timespec64(&it->it_value, &uit->it_value);
  761. return ret;
  762. }
  763. EXPORT_SYMBOL_GPL(get_itimerspec64);
  764. int put_itimerspec64(const struct itimerspec64 *it,
  765. struct __kernel_itimerspec __user *uit)
  766. {
  767. int ret;
  768. ret = put_timespec64(&it->it_interval, &uit->it_interval);
  769. if (ret)
  770. return ret;
  771. ret = put_timespec64(&it->it_value, &uit->it_value);
  772. return ret;
  773. }
  774. EXPORT_SYMBOL_GPL(put_itimerspec64);
  775. int get_old_itimerspec32(struct itimerspec64 *its,
  776. const struct old_itimerspec32 __user *uits)
  777. {
  778. if (__get_old_timespec32(&its->it_interval, &uits->it_interval) ||
  779. __get_old_timespec32(&its->it_value, &uits->it_value))
  780. return -EFAULT;
  781. return 0;
  782. }
  783. EXPORT_SYMBOL_GPL(get_old_itimerspec32);
  784. int put_old_itimerspec32(const struct itimerspec64 *its,
  785. struct old_itimerspec32 __user *uits)
  786. {
  787. if (__put_old_timespec32(&its->it_interval, &uits->it_interval) ||
  788. __put_old_timespec32(&its->it_value, &uits->it_value))
  789. return -EFAULT;
  790. return 0;
  791. }
  792. EXPORT_SYMBOL_GPL(put_old_itimerspec32);