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