time.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. * jiffies_to_msecs - Convert jiffies to milliseconds
  310. * @j: jiffies value
  311. *
  312. * Avoid unnecessary multiplications/divisions in the
  313. * two most common HZ cases.
  314. *
  315. * Return: milliseconds value
  316. */
  317. unsigned int jiffies_to_msecs(const unsigned long j)
  318. {
  319. #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
  320. return (MSEC_PER_SEC / HZ) * j;
  321. #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
  322. return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
  323. #else
  324. # if BITS_PER_LONG == 32
  325. return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >>
  326. HZ_TO_MSEC_SHR32;
  327. # else
  328. return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
  329. # endif
  330. #endif
  331. }
  332. EXPORT_SYMBOL(jiffies_to_msecs);
  333. /**
  334. * jiffies_to_usecs - Convert jiffies to microseconds
  335. * @j: jiffies value
  336. *
  337. * Return: microseconds value
  338. */
  339. unsigned int jiffies_to_usecs(const unsigned long j)
  340. {
  341. /*
  342. * Hz usually doesn't go much further MSEC_PER_SEC.
  343. * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
  344. */
  345. BUILD_BUG_ON(HZ > USEC_PER_SEC);
  346. #if !(USEC_PER_SEC % HZ)
  347. return (USEC_PER_SEC / HZ) * j;
  348. #else
  349. # if BITS_PER_LONG == 32
  350. return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
  351. # else
  352. return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
  353. # endif
  354. #endif
  355. }
  356. EXPORT_SYMBOL(jiffies_to_usecs);
  357. /**
  358. * mktime64 - Converts date to seconds.
  359. * @year0: year to convert
  360. * @mon0: month to convert
  361. * @day: day to convert
  362. * @hour: hour to convert
  363. * @min: minute to convert
  364. * @sec: second to convert
  365. *
  366. * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  367. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  368. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  369. *
  370. * [For the Julian calendar (which was used in Russia before 1917,
  371. * Britain & colonies before 1752, anywhere else before 1582,
  372. * and is still in use by some communities) leave out the
  373. * -year/100+year/400 terms, and add 10.]
  374. *
  375. * This algorithm was first published by Gauss (I think).
  376. *
  377. * A leap second can be indicated by calling this function with sec as
  378. * 60 (allowable under ISO 8601). The leap second is treated the same
  379. * as the following second since they don't exist in UNIX time.
  380. *
  381. * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
  382. * tomorrow - (allowable under ISO 8601) is supported.
  383. *
  384. * Return: seconds since the epoch time for the given input date
  385. */
  386. time64_t mktime64(const unsigned int year0, const unsigned int mon0,
  387. const unsigned int day, const unsigned int hour,
  388. const unsigned int min, const unsigned int sec)
  389. {
  390. unsigned int mon = mon0, year = year0;
  391. /* 1..12 -> 11,12,1..10 */
  392. if (0 >= (int) (mon -= 2)) {
  393. mon += 12; /* Puts Feb last since it has leap day */
  394. year -= 1;
  395. }
  396. return ((((time64_t)
  397. (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  398. year*365 - 719499
  399. )*24 + hour /* now have hours - midnight tomorrow handled here */
  400. )*60 + min /* now have minutes */
  401. )*60 + sec; /* finally seconds */
  402. }
  403. EXPORT_SYMBOL(mktime64);
  404. struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec)
  405. {
  406. struct timespec64 ts = ns_to_timespec64(nsec);
  407. struct __kernel_old_timeval tv;
  408. tv.tv_sec = ts.tv_sec;
  409. tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;
  410. return tv;
  411. }
  412. EXPORT_SYMBOL(ns_to_kernel_old_timeval);
  413. /**
  414. * set_normalized_timespec64 - set timespec sec and nsec parts and normalize
  415. *
  416. * @ts: pointer to timespec variable to be set
  417. * @sec: seconds to set
  418. * @nsec: nanoseconds to set
  419. *
  420. * Set seconds and nanoseconds field of a timespec variable and
  421. * normalize to the timespec storage format
  422. *
  423. * Note: The tv_nsec part is always in the range of 0 <= tv_nsec < NSEC_PER_SEC.
  424. * For negative values only the tv_sec field is negative !
  425. */
  426. void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
  427. {
  428. while (nsec >= NSEC_PER_SEC) {
  429. /*
  430. * The following asm() prevents the compiler from
  431. * optimising this loop into a modulo operation. See
  432. * also __iter_div_u64_rem() in include/linux/time.h
  433. */
  434. asm("" : "+rm"(nsec));
  435. nsec -= NSEC_PER_SEC;
  436. ++sec;
  437. }
  438. while (nsec < 0) {
  439. asm("" : "+rm"(nsec));
  440. nsec += NSEC_PER_SEC;
  441. --sec;
  442. }
  443. ts->tv_sec = sec;
  444. ts->tv_nsec = nsec;
  445. }
  446. EXPORT_SYMBOL(set_normalized_timespec64);
  447. /**
  448. * ns_to_timespec64 - Convert nanoseconds to timespec64
  449. * @nsec: the nanoseconds value to be converted
  450. *
  451. * Return: the timespec64 representation of the nsec parameter.
  452. */
  453. struct timespec64 ns_to_timespec64(s64 nsec)
  454. {
  455. struct timespec64 ts = { 0, 0 };
  456. s32 rem;
  457. if (likely(nsec > 0)) {
  458. ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
  459. ts.tv_nsec = rem;
  460. } else if (nsec < 0) {
  461. /*
  462. * With negative times, tv_sec points to the earlier
  463. * second, and tv_nsec counts the nanoseconds since
  464. * then, so tv_nsec is always a positive number.
  465. */
  466. ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1;
  467. ts.tv_nsec = NSEC_PER_SEC - rem - 1;
  468. }
  469. return ts;
  470. }
  471. EXPORT_SYMBOL(ns_to_timespec64);
  472. /**
  473. * __msecs_to_jiffies: - convert milliseconds to jiffies
  474. * @m: time in milliseconds
  475. *
  476. * conversion is done as follows:
  477. *
  478. * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
  479. *
  480. * - 'too large' values [that would result in larger than
  481. * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
  482. *
  483. * - all other values are converted to jiffies by either multiplying
  484. * the input value by a factor or dividing it with a factor and
  485. * handling any 32-bit overflows.
  486. * for the details see _msecs_to_jiffies()
  487. *
  488. * msecs_to_jiffies() checks for the passed in value being a constant
  489. * via __builtin_constant_p() allowing gcc to eliminate most of the
  490. * code, __msecs_to_jiffies() is called if the value passed does not
  491. * allow constant folding and the actual conversion must be done at
  492. * runtime.
  493. * The _msecs_to_jiffies helpers are the HZ dependent conversion
  494. * routines found in include/linux/jiffies.h
  495. *
  496. * Return: jiffies value
  497. */
  498. unsigned long __msecs_to_jiffies(const unsigned int m)
  499. {
  500. /*
  501. * Negative value, means infinite timeout:
  502. */
  503. if ((int)m < 0)
  504. return MAX_JIFFY_OFFSET;
  505. return _msecs_to_jiffies(m);
  506. }
  507. EXPORT_SYMBOL(__msecs_to_jiffies);
  508. /**
  509. * __usecs_to_jiffies: - convert microseconds to jiffies
  510. * @u: time in milliseconds
  511. *
  512. * Return: jiffies value
  513. */
  514. unsigned long __usecs_to_jiffies(const unsigned int u)
  515. {
  516. if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
  517. return MAX_JIFFY_OFFSET;
  518. return _usecs_to_jiffies(u);
  519. }
  520. EXPORT_SYMBOL(__usecs_to_jiffies);
  521. /**
  522. * timespec64_to_jiffies - convert a timespec64 value to jiffies
  523. * @value: pointer to &struct timespec64
  524. *
  525. * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
  526. * that a remainder subtract here would not do the right thing as the
  527. * resolution values don't fall on second boundaries. I.e. the line:
  528. * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
  529. * Note that due to the small error in the multiplier here, this
  530. * rounding is incorrect for sufficiently large values of tv_nsec, but
  531. * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
  532. * OK.
  533. *
  534. * Rather, we just shift the bits off the right.
  535. *
  536. * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
  537. * value to a scaled second value.
  538. *
  539. * Return: jiffies value
  540. */
  541. unsigned long
  542. timespec64_to_jiffies(const struct timespec64 *value)
  543. {
  544. u64 sec = value->tv_sec;
  545. long nsec = value->tv_nsec + TICK_NSEC - 1;
  546. if (sec >= MAX_SEC_IN_JIFFIES){
  547. sec = MAX_SEC_IN_JIFFIES;
  548. nsec = 0;
  549. }
  550. return ((sec * SEC_CONVERSION) +
  551. (((u64)nsec * NSEC_CONVERSION) >>
  552. (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
  553. }
  554. EXPORT_SYMBOL(timespec64_to_jiffies);
  555. /**
  556. * jiffies_to_timespec64 - convert jiffies value to &struct timespec64
  557. * @jiffies: jiffies value
  558. * @value: pointer to &struct timespec64
  559. */
  560. void
  561. jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
  562. {
  563. /*
  564. * Convert jiffies to nanoseconds and separate with
  565. * one divide.
  566. */
  567. u32 rem;
  568. value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
  569. NSEC_PER_SEC, &rem);
  570. value->tv_nsec = rem;
  571. }
  572. EXPORT_SYMBOL(jiffies_to_timespec64);
  573. /*
  574. * Convert jiffies/jiffies_64 to clock_t and back.
  575. */
  576. /**
  577. * jiffies_to_clock_t - Convert jiffies to clock_t
  578. * @x: jiffies value
  579. *
  580. * Return: jiffies converted to clock_t (CLOCKS_PER_SEC)
  581. */
  582. clock_t jiffies_to_clock_t(unsigned long x)
  583. {
  584. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  585. # if HZ < USER_HZ
  586. return x * (USER_HZ / HZ);
  587. # else
  588. return x / (HZ / USER_HZ);
  589. # endif
  590. #else
  591. return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
  592. #endif
  593. }
  594. EXPORT_SYMBOL(jiffies_to_clock_t);
  595. /**
  596. * clock_t_to_jiffies - Convert clock_t to jiffies
  597. * @x: clock_t value
  598. *
  599. * Return: clock_t value converted to jiffies
  600. */
  601. unsigned long clock_t_to_jiffies(unsigned long x)
  602. {
  603. #if (HZ % USER_HZ)==0
  604. if (x >= ~0UL / (HZ / USER_HZ))
  605. return ~0UL;
  606. return x * (HZ / USER_HZ);
  607. #else
  608. /* Don't worry about loss of precision here .. */
  609. if (x >= ~0UL / HZ * USER_HZ)
  610. return ~0UL;
  611. /* .. but do try to contain it here */
  612. return div_u64((u64)x * HZ, USER_HZ);
  613. #endif
  614. }
  615. EXPORT_SYMBOL(clock_t_to_jiffies);
  616. /**
  617. * jiffies_64_to_clock_t - Convert jiffies_64 to clock_t
  618. * @x: jiffies_64 value
  619. *
  620. * Return: jiffies_64 value converted to 64-bit "clock_t" (CLOCKS_PER_SEC)
  621. */
  622. u64 jiffies_64_to_clock_t(u64 x)
  623. {
  624. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  625. # if HZ < USER_HZ
  626. x = div_u64(x * USER_HZ, HZ);
  627. # elif HZ > USER_HZ
  628. x = div_u64(x, HZ / USER_HZ);
  629. # else
  630. /* Nothing to do */
  631. # endif
  632. #else
  633. /*
  634. * There are better ways that don't overflow early,
  635. * but even this doesn't overflow in hundreds of years
  636. * in 64 bits, so..
  637. */
  638. x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
  639. #endif
  640. return x;
  641. }
  642. EXPORT_SYMBOL(jiffies_64_to_clock_t);
  643. /**
  644. * nsec_to_clock_t - Convert nsec value to clock_t
  645. * @x: nsec value
  646. *
  647. * Return: nsec value converted to 64-bit "clock_t" (CLOCKS_PER_SEC)
  648. */
  649. u64 nsec_to_clock_t(u64 x)
  650. {
  651. #if (NSEC_PER_SEC % USER_HZ) == 0
  652. return div_u64(x, NSEC_PER_SEC / USER_HZ);
  653. #elif (USER_HZ % 512) == 0
  654. return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
  655. #else
  656. /*
  657. * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
  658. * overflow after 64.99 years.
  659. * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
  660. */
  661. return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
  662. #endif
  663. }
  664. /**
  665. * jiffies64_to_nsecs - Convert jiffies64 to nanoseconds
  666. * @j: jiffies64 value
  667. *
  668. * Return: nanoseconds value
  669. */
  670. u64 jiffies64_to_nsecs(u64 j)
  671. {
  672. #if !(NSEC_PER_SEC % HZ)
  673. return (NSEC_PER_SEC / HZ) * j;
  674. # else
  675. return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN);
  676. #endif
  677. }
  678. EXPORT_SYMBOL(jiffies64_to_nsecs);
  679. /**
  680. * jiffies64_to_msecs - Convert jiffies64 to milliseconds
  681. * @j: jiffies64 value
  682. *
  683. * Return: milliseconds value
  684. */
  685. u64 jiffies64_to_msecs(const u64 j)
  686. {
  687. #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
  688. return (MSEC_PER_SEC / HZ) * j;
  689. #else
  690. return div_u64(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
  691. #endif
  692. }
  693. EXPORT_SYMBOL(jiffies64_to_msecs);
  694. /**
  695. * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
  696. *
  697. * @n: nsecs in u64
  698. *
  699. * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
  700. * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
  701. * for scheduler, not for use in device drivers to calculate timeout value.
  702. *
  703. * note:
  704. * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
  705. * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
  706. *
  707. * Return: nsecs converted to jiffies64 value
  708. */
  709. u64 nsecs_to_jiffies64(u64 n)
  710. {
  711. #if (NSEC_PER_SEC % HZ) == 0
  712. /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
  713. return div_u64(n, NSEC_PER_SEC / HZ);
  714. #elif (HZ % 512) == 0
  715. /* overflow after 292 years if HZ = 1024 */
  716. return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
  717. #else
  718. /*
  719. * Generic case - optimized for cases where HZ is a multiple of 3.
  720. * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
  721. */
  722. return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
  723. #endif
  724. }
  725. EXPORT_SYMBOL(nsecs_to_jiffies64);
  726. /**
  727. * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
  728. *
  729. * @n: nsecs in u64
  730. *
  731. * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
  732. * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
  733. * for scheduler, not for use in device drivers to calculate timeout value.
  734. *
  735. * note:
  736. * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
  737. * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
  738. *
  739. * Return: nsecs converted to jiffies value
  740. */
  741. unsigned long nsecs_to_jiffies(u64 n)
  742. {
  743. return (unsigned long)nsecs_to_jiffies64(n);
  744. }
  745. EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
  746. /**
  747. * timespec64_add_safe - Add two timespec64 values and do a safety check
  748. * for overflow.
  749. * @lhs: first (left) timespec64 to add
  750. * @rhs: second (right) timespec64 to add
  751. *
  752. * It's assumed that both values are valid (>= 0).
  753. * And, each timespec64 is in normalized form.
  754. *
  755. * Return: sum of @lhs + @rhs
  756. */
  757. struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
  758. const struct timespec64 rhs)
  759. {
  760. struct timespec64 res;
  761. set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec,
  762. lhs.tv_nsec + rhs.tv_nsec);
  763. if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) {
  764. res.tv_sec = TIME64_MAX;
  765. res.tv_nsec = 0;
  766. }
  767. return res;
  768. }
  769. /**
  770. * get_timespec64 - get user's time value into kernel space
  771. * @ts: destination &struct timespec64
  772. * @uts: user's time value as &struct __kernel_timespec
  773. *
  774. * Handles compat or 32-bit modes.
  775. *
  776. * Return: %0 on success or negative errno on error
  777. */
  778. int get_timespec64(struct timespec64 *ts,
  779. const struct __kernel_timespec __user *uts)
  780. {
  781. struct __kernel_timespec kts;
  782. int ret;
  783. ret = copy_from_user(&kts, uts, sizeof(kts));
  784. if (ret)
  785. return -EFAULT;
  786. ts->tv_sec = kts.tv_sec;
  787. /* Zero out the padding in compat mode */
  788. if (in_compat_syscall())
  789. kts.tv_nsec &= 0xFFFFFFFFUL;
  790. /* In 32-bit mode, this drops the padding */
  791. ts->tv_nsec = kts.tv_nsec;
  792. return 0;
  793. }
  794. EXPORT_SYMBOL_GPL(get_timespec64);
  795. /**
  796. * put_timespec64 - convert timespec64 value to __kernel_timespec format and
  797. * copy the latter to userspace
  798. * @ts: input &struct timespec64
  799. * @uts: user's &struct __kernel_timespec
  800. *
  801. * Return: %0 on success or negative errno on error
  802. */
  803. int put_timespec64(const struct timespec64 *ts,
  804. struct __kernel_timespec __user *uts)
  805. {
  806. struct __kernel_timespec kts = {
  807. .tv_sec = ts->tv_sec,
  808. .tv_nsec = ts->tv_nsec
  809. };
  810. return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0;
  811. }
  812. EXPORT_SYMBOL_GPL(put_timespec64);
  813. static int __get_old_timespec32(struct timespec64 *ts64,
  814. const struct old_timespec32 __user *cts)
  815. {
  816. struct old_timespec32 ts;
  817. int ret;
  818. ret = copy_from_user(&ts, cts, sizeof(ts));
  819. if (ret)
  820. return -EFAULT;
  821. ts64->tv_sec = ts.tv_sec;
  822. ts64->tv_nsec = ts.tv_nsec;
  823. return 0;
  824. }
  825. static int __put_old_timespec32(const struct timespec64 *ts64,
  826. struct old_timespec32 __user *cts)
  827. {
  828. struct old_timespec32 ts = {
  829. .tv_sec = ts64->tv_sec,
  830. .tv_nsec = ts64->tv_nsec
  831. };
  832. return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0;
  833. }
  834. /**
  835. * get_old_timespec32 - get user's old-format time value into kernel space
  836. * @ts: destination &struct timespec64
  837. * @uts: user's old-format time value (&struct old_timespec32)
  838. *
  839. * Handles X86_X32_ABI compatibility conversion.
  840. *
  841. * Return: %0 on success or negative errno on error
  842. */
  843. int get_old_timespec32(struct timespec64 *ts, const void __user *uts)
  844. {
  845. if (COMPAT_USE_64BIT_TIME)
  846. return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
  847. else
  848. return __get_old_timespec32(ts, uts);
  849. }
  850. EXPORT_SYMBOL_GPL(get_old_timespec32);
  851. /**
  852. * put_old_timespec32 - convert timespec64 value to &struct old_timespec32 and
  853. * copy the latter to userspace
  854. * @ts: input &struct timespec64
  855. * @uts: user's &struct old_timespec32
  856. *
  857. * Handles X86_X32_ABI compatibility conversion.
  858. *
  859. * Return: %0 on success or negative errno on error
  860. */
  861. int put_old_timespec32(const struct timespec64 *ts, void __user *uts)
  862. {
  863. if (COMPAT_USE_64BIT_TIME)
  864. return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
  865. else
  866. return __put_old_timespec32(ts, uts);
  867. }
  868. EXPORT_SYMBOL_GPL(put_old_timespec32);
  869. /**
  870. * get_itimerspec64 - get user's &struct __kernel_itimerspec into kernel space
  871. * @it: destination &struct itimerspec64
  872. * @uit: user's &struct __kernel_itimerspec
  873. *
  874. * Return: %0 on success or negative errno on error
  875. */
  876. int get_itimerspec64(struct itimerspec64 *it,
  877. const struct __kernel_itimerspec __user *uit)
  878. {
  879. int ret;
  880. ret = get_timespec64(&it->it_interval, &uit->it_interval);
  881. if (ret)
  882. return ret;
  883. ret = get_timespec64(&it->it_value, &uit->it_value);
  884. return ret;
  885. }
  886. EXPORT_SYMBOL_GPL(get_itimerspec64);
  887. /**
  888. * put_itimerspec64 - convert &struct itimerspec64 to __kernel_itimerspec format
  889. * and copy the latter to userspace
  890. * @it: input &struct itimerspec64
  891. * @uit: user's &struct __kernel_itimerspec
  892. *
  893. * Return: %0 on success or negative errno on error
  894. */
  895. int put_itimerspec64(const struct itimerspec64 *it,
  896. struct __kernel_itimerspec __user *uit)
  897. {
  898. int ret;
  899. ret = put_timespec64(&it->it_interval, &uit->it_interval);
  900. if (ret)
  901. return ret;
  902. ret = put_timespec64(&it->it_value, &uit->it_value);
  903. return ret;
  904. }
  905. EXPORT_SYMBOL_GPL(put_itimerspec64);
  906. /**
  907. * get_old_itimerspec32 - get user's &struct old_itimerspec32 into kernel space
  908. * @its: destination &struct itimerspec64
  909. * @uits: user's &struct old_itimerspec32
  910. *
  911. * Return: %0 on success or negative errno on error
  912. */
  913. int get_old_itimerspec32(struct itimerspec64 *its,
  914. const struct old_itimerspec32 __user *uits)
  915. {
  916. if (__get_old_timespec32(&its->it_interval, &uits->it_interval) ||
  917. __get_old_timespec32(&its->it_value, &uits->it_value))
  918. return -EFAULT;
  919. return 0;
  920. }
  921. EXPORT_SYMBOL_GPL(get_old_itimerspec32);
  922. /**
  923. * put_old_itimerspec32 - convert &struct itimerspec64 to &struct
  924. * old_itimerspec32 and copy the latter to userspace
  925. * @its: input &struct itimerspec64
  926. * @uits: user's &struct old_itimerspec32
  927. *
  928. * Return: %0 on success or negative errno on error
  929. */
  930. int put_old_itimerspec32(const struct itimerspec64 *its,
  931. struct old_itimerspec32 __user *uits)
  932. {
  933. if (__put_old_timespec32(&its->it_interval, &uits->it_interval) ||
  934. __put_old_timespec32(&its->it_value, &uits->it_value))
  935. return -EFAULT;
  936. return 0;
  937. }
  938. EXPORT_SYMBOL_GPL(put_old_itimerspec32);