projects/linux·blob uaccess-cleanups · refs

1069 lines · 27.2 KiB

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
27#include <linux/export.h>
28#include <linux/kernel.h>
29#include <linux/timex.h>
30#include <linux/capability.h>
31#include <linux/timekeeper_internal.h>
32#include <linux/errno.h>
33#include <linux/syscalls.h>
34#include <linux/security.h>
35#include <linux/fs.h>
36#include <linux/math64.h>
37#include <linux/ptrace.h>
38
39#include <linux/uaccess.h>
40#include <linux/compat.h>
41#include <asm/unistd.h>
42
43#include <generated/timeconst.h>
44#include "timekeeping.h"
45#include "timekeeping_internal.h"
46
47#if defined(CONFIG_64BIT) || defined(CONFIG_COMPAT_32BIT_TIME)
48#define __WANT_OLD_TIME_TYPE_SYSCALL 1
49#endif
50
51static_assert(sizeof(__kernel_old_time_t) == 8 ? IS_ENABLED(__WANT_OLD_TIME_TYPE_SYSCALL) : true);
52
53/*
54 * The timezone where the local system is located. Used as a default by some
55 * programs who obtain this value by using gettimeofday.
56 */
57struct timezone sys_tz;
58
59EXPORT_SYMBOL(sys_tz);
60
61#if defined(__ARCH_WANT_SYS_TIME) && defined(__WANT_OLD_TIME_TYPE_SYSCALL)
62
63/*
64 * sys_time() can be implemented in user-level using
65 * sys_gettimeofday(). Is this for backwards compatibility? If so,
66 * why not move it into the appropriate arch directory (for those
67 * architectures that need it).
68 */
69SYSCALL_DEFINE1(time, __kernel_old_time_t __user *, tloc)
70{
71 __kernel_old_time_t i = (__kernel_old_time_t)ktime_get_real_seconds();
72
73 if (tloc) {
74 if (put_user(i,tloc))
75 return -EFAULT;
76 }
77 force_successful_syscall_return();
78 return i;
79}
80
81/*
82 * sys_stime() can be implemented in user-level using
83 * sys_settimeofday(). Is this for backwards compatibility? If so,
84 * why not move it into the appropriate arch directory (for those
85 * architectures that need it).
86 */
87
88SYSCALL_DEFINE1(stime, __kernel_old_time_t __user *, tptr)
89{
90 struct timespec64 tv;
91 int err;
92
93 if (get_user(tv.tv_sec, tptr))
94 return -EFAULT;
95
96 tv.tv_nsec = 0;
97
98 err = security_settime64(&tv, NULL);
99 if (err)
100 return err;
101
102 do_settimeofday64(&tv);
103 return 0;
104}
105
106#endif /* __ARCH_WANT_SYS_TIME && __WANT_OLD_TIME_TYPE_SYSCALL */
107
108#ifdef CONFIG_COMPAT_32BIT_TIME
109#ifdef __ARCH_WANT_SYS_TIME32
110
111/* old_time32_t is a 32 bit "long" and needs to get converted. */
112SYSCALL_DEFINE1(time32, old_time32_t __user *, tloc)
113{
114 old_time32_t i;
115
116 i = (old_time32_t)ktime_get_real_seconds();
117
118 if (tloc) {
119 if (put_user(i,tloc))
120 return -EFAULT;
121 }
122 force_successful_syscall_return();
123 return i;
124}
125
126SYSCALL_DEFINE1(stime32, old_time32_t __user *, tptr)
127{
128 struct timespec64 tv;
129 int err;
130
131 if (get_user(tv.tv_sec, tptr))
132 return -EFAULT;
133
134 tv.tv_nsec = 0;
135
136 err = security_settime64(&tv, NULL);
137 if (err)
138 return err;
139
140 do_settimeofday64(&tv);
141 return 0;
142}
143
144#endif /* __ARCH_WANT_SYS_TIME32 */
145#endif
146
147#ifdef __WANT_OLD_TIME_TYPE_SYSCALL
148SYSCALL_DEFINE2(gettimeofday, struct __kernel_old_timeval __user *, tv,
149 struct timezone __user *, tz)
150{
151 if (likely(tv != NULL)) {
152 struct timespec64 ts;
153
154 ktime_get_real_ts64(&ts);
155 if (put_user(ts.tv_sec, &tv->tv_sec) ||
156 put_user(ts.tv_nsec / 1000, &tv->tv_usec))
157 return -EFAULT;
158 }
159 if (unlikely(tz != NULL)) {
160 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
161 return -EFAULT;
162 }
163 return 0;
164}
165#endif /* __WANT_OLD_TIME_TYPE_SYSCALL */
166
167/*
168 * In case for some reason the CMOS clock has not already been running
169 * in UTC, but in some local time: The first time we set the timezone,
170 * we will warp the clock so that it is ticking UTC time instead of
171 * local time. Presumably, if someone is setting the timezone then we
172 * are running in an environment where the programs understand about
173 * timezones. This should be done at boot time in the /etc/rc script,
174 * as soon as possible, so that the clock can be set right. Otherwise,
175 * various programs will get confused when the clock gets warped.
176 */
177
178int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz)
179{
180 static int firsttime = 1;
181 int error = 0;
182
183 if (tv && !timespec64_valid_settod(tv))
184 return -EINVAL;
185
186 error = security_settime64(tv, tz);
187 if (error)
188 return error;
189
190 if (tz) {
191 /* Verify we're within the +-15 hrs range */
192 if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60)
193 return -EINVAL;
194
195 sys_tz = *tz;
196 update_vsyscall_tz();
197 if (firsttime) {
198 firsttime = 0;
199 if (!tv)
200 timekeeping_warp_clock();
201 }
202 }
203 if (tv)
204 return do_settimeofday64(tv);
205 return 0;
206}
207
208SYSCALL_DEFINE2(settimeofday, struct __kernel_old_timeval __user *, tv,
209 struct timezone __user *, tz)
210{
211 struct timespec64 new_ts;
212 struct timezone new_tz;
213
214 if (tv) {
215 if (!IS_ENABLED(__WANT_OLD_TIME_TYPE_SYSCALL))
216 return -EINVAL;
217
218 if (get_user(new_ts.tv_sec, &tv->tv_sec) ||
219 get_user(new_ts.tv_nsec, &tv->tv_usec))
220 return -EFAULT;
221
222 if (new_ts.tv_nsec >= USEC_PER_SEC || new_ts.tv_nsec < 0)
223 return -EINVAL;
224
225 new_ts.tv_nsec *= NSEC_PER_USEC;
226 }
227 if (tz) {
228 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
229 return -EFAULT;
230 }
231
232 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
233}
234
235#ifdef CONFIG_COMPAT_32BIT_TIME
236COMPAT_SYSCALL_DEFINE2(gettimeofday, struct old_timeval32 __user *, tv,
237 struct timezone __user *, tz)
238{
239 if (tv) {
240 struct timespec64 ts;
241
242 ktime_get_real_ts64(&ts);
243 if (put_user(ts.tv_sec, &tv->tv_sec) ||
244 put_user(ts.tv_nsec / 1000, &tv->tv_usec))
245 return -EFAULT;
246 }
247 if (tz) {
248 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
249 return -EFAULT;
250 }
251
252 return 0;
253}
254#endif /* CONFIG_COMPAT_32BIT_TIME */
255
256#ifdef CONFIG_COMPAT
257COMPAT_SYSCALL_DEFINE2(settimeofday, struct old_timeval32 __user *, tv,
258 struct timezone __user *, tz)
259{
260 struct timespec64 new_ts;
261 struct timezone new_tz;
262
263 if (tv) {
264 if (!IS_ENABLED(CONFIG_COMPAT_32BIT_TIME))
265 return -EINVAL;
266
267 if (get_user(new_ts.tv_sec, &tv->tv_sec) ||
268 get_user(new_ts.tv_nsec, &tv->tv_usec))
269 return -EFAULT;
270
271 if (new_ts.tv_nsec >= USEC_PER_SEC || new_ts.tv_nsec < 0)
272 return -EINVAL;
273
274 new_ts.tv_nsec *= NSEC_PER_USEC;
275 }
276 if (tz) {
277 if (copy_from_user(&new_tz, tz, sizeof(*tz)))
278 return -EFAULT;
279 }
280
281 return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
282}
283#endif /* CONFIG_COMPAT */
284
285#ifdef CONFIG_64BIT
286SYSCALL_DEFINE1(adjtimex, struct __kernel_timex __user *, txc_p)
287{
288 struct __kernel_timex txc; /* Local copy of parameter */
289 int ret;
290
291 /* Copy the user data space into the kernel copy
292 * structure. But bear in mind that the structures
293 * may change
294 */
295 if (copy_from_user(&txc, txc_p, sizeof(struct __kernel_timex)))
296 return -EFAULT;
297 ret = do_adjtimex(&txc);
298 return copy_to_user(txc_p, &txc, sizeof(struct __kernel_timex)) ? -EFAULT : ret;
299}
300#endif
301
302#ifdef CONFIG_COMPAT_32BIT_TIME
303int get_old_timex32(struct __kernel_timex *txc, const struct old_timex32 __user *utp)
304{
305 struct old_timex32 tx32;
306
307 memset(txc, 0, sizeof(struct __kernel_timex));
308 if (copy_from_user(&tx32, utp, sizeof(struct old_timex32)))
309 return -EFAULT;
310
311 txc->modes = tx32.modes;
312 txc->offset = tx32.offset;
313 txc->freq = tx32.freq;
314 txc->maxerror = tx32.maxerror;
315 txc->esterror = tx32.esterror;
316 txc->status = tx32.status;
317 txc->constant = tx32.constant;
318 txc->precision = tx32.precision;
319 txc->tolerance = tx32.tolerance;
320 txc->time.tv_sec = tx32.time.tv_sec;
321 txc->time.tv_usec = tx32.time.tv_usec;
322 txc->tick = tx32.tick;
323 txc->ppsfreq = tx32.ppsfreq;
324 txc->jitter = tx32.jitter;
325 txc->shift = tx32.shift;
326 txc->stabil = tx32.stabil;
327 txc->jitcnt = tx32.jitcnt;
328 txc->calcnt = tx32.calcnt;
329 txc->errcnt = tx32.errcnt;
330 txc->stbcnt = tx32.stbcnt;
331
332 return 0;
333}
334
335int put_old_timex32(struct old_timex32 __user *utp, const struct __kernel_timex *txc)
336{
337 struct old_timex32 tx32;
338
339 memset(&tx32, 0, sizeof(struct old_timex32));
340 tx32.modes = txc->modes;
341 tx32.offset = txc->offset;
342 tx32.freq = txc->freq;
343 tx32.maxerror = txc->maxerror;
344 tx32.esterror = txc->esterror;
345 tx32.status = txc->status;
346 tx32.constant = txc->constant;
347 tx32.precision = txc->precision;
348 tx32.tolerance = txc->tolerance;
349 tx32.time.tv_sec = txc->time.tv_sec;
350 tx32.time.tv_usec = txc->time.tv_usec;
351 tx32.tick = txc->tick;
352 tx32.ppsfreq = txc->ppsfreq;
353 tx32.jitter = txc->jitter;
354 tx32.shift = txc->shift;
355 tx32.stabil = txc->stabil;
356 tx32.jitcnt = txc->jitcnt;
357 tx32.calcnt = txc->calcnt;
358 tx32.errcnt = txc->errcnt;
359 tx32.stbcnt = txc->stbcnt;
360 tx32.tai = txc->tai;
361 if (copy_to_user(utp, &tx32, sizeof(struct old_timex32)))
362 return -EFAULT;
363 return 0;
364}
365
366SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp)
367{
368 struct __kernel_timex txc;
369 int err, ret;
370
371 err = get_old_timex32(&txc, utp);
372 if (err)
373 return err;
374
375 ret = do_adjtimex(&txc);
376
377 err = put_old_timex32(utp, &txc);
378 if (err)
379 return err;
380
381 return ret;
382}
383#endif
384
385#if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ)
386/**
387 * jiffies_to_msecs - Convert jiffies to milliseconds
388 * @j: jiffies value
389 *
390 * Return: milliseconds value
391 */
392unsigned int jiffies_to_msecs(const unsigned long j)
393{
394#if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
395 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
396#else
397# if BITS_PER_LONG == 32
398 return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >>
399 HZ_TO_MSEC_SHR32;
400# else
401 return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
402# endif
403#endif
404}
405EXPORT_SYMBOL(jiffies_to_msecs);
406#endif
407
408#if (USEC_PER_SEC % HZ)
409/**
410 * jiffies_to_usecs - Convert jiffies to microseconds
411 * @j: jiffies value
412 *
413 * Return: microseconds value
414 */
415unsigned int jiffies_to_usecs(const unsigned long j)
416{
417 /*
418 * Hz usually doesn't go much further MSEC_PER_SEC.
419 * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
420 */
421 BUILD_BUG_ON(HZ > USEC_PER_SEC);
422
423#if BITS_PER_LONG == 32
424 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
425#else
426 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
427#endif
428}
429EXPORT_SYMBOL(jiffies_to_usecs);
430#endif
431
432/**
433 * mktime64 - Converts date to seconds.
434 * @year0: year to convert
435 * @mon0: month to convert
436 * @day: day to convert
437 * @hour: hour to convert
438 * @min: minute to convert
439 * @sec: second to convert
440 *
441 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
442 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
443 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
444 *
445 * [For the Julian calendar (which was used in Russia before 1917,
446 * Britain & colonies before 1752, anywhere else before 1582,
447 * and is still in use by some communities) leave out the
448 * -year/100+year/400 terms, and add 10.]
449 *
450 * This algorithm was first published by Gauss (I think).
451 *
452 * A leap second can be indicated by calling this function with sec as
453 * 60 (allowable under ISO 8601). The leap second is treated the same
454 * as the following second since they don't exist in UNIX time.
455 *
456 * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
457 * tomorrow - (allowable under ISO 8601) is supported.
458 *
459 * Return: seconds since the epoch time for the given input date
460 */
461time64_t mktime64(const unsigned int year0, const unsigned int mon0,
462 const unsigned int day, const unsigned int hour,
463 const unsigned int min, const unsigned int sec)
464{
465 unsigned int mon = mon0, year = year0;
466
467 /* 1..12 -> 11,12,1..10 */
468 if (0 >= (int) (mon -= 2)) {
469 mon += 12; /* Puts Feb last since it has leap day */
470 year -= 1;
471 }
472
473 return ((((time64_t)
474 (year/4 - year/100 + year/400 + 367*mon/12 + day) +
475 year*365 - 719499
476 )*24 + hour /* now have hours - midnight tomorrow handled here */
477 )*60 + min /* now have minutes */
478 )*60 + sec; /* finally seconds */
479}
480EXPORT_SYMBOL(mktime64);
481
482struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec)
483{
484 struct timespec64 ts = ns_to_timespec64(nsec);
485 struct __kernel_old_timeval tv;
486
487 tv.tv_sec = ts.tv_sec;
488 tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;
489
490 return tv;
491}
492EXPORT_SYMBOL(ns_to_kernel_old_timeval);
493
494/**
495 * set_normalized_timespec64 - set timespec sec and nsec parts and normalize
496 *
497 * @ts: pointer to timespec variable to be set
498 * @sec: seconds to set
499 * @nsec: nanoseconds to set
500 *
501 * Set seconds and nanoseconds field of a timespec variable and
502 * normalize to the timespec storage format
503 *
504 * Note: The tv_nsec part is always in the range of 0 <= tv_nsec < NSEC_PER_SEC.
505 * For negative values only the tv_sec field is negative !
506 */
507void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
508{
509 while (nsec >= NSEC_PER_SEC) {
510 /*
511 * The following asm() prevents the compiler from
512 * optimising this loop into a modulo operation. See
513 * also __iter_div_u64_rem() in include/linux/time.h
514 */
515 asm("" : "+rm"(nsec));
516 nsec -= NSEC_PER_SEC;
517 ++sec;
518 }
519 while (nsec < 0) {
520 asm("" : "+rm"(nsec));
521 nsec += NSEC_PER_SEC;
522 --sec;
523 }
524 ts->tv_sec = sec;
525 ts->tv_nsec = nsec;
526}
527EXPORT_SYMBOL(set_normalized_timespec64);
528
529/**
530 * ns_to_timespec64 - Convert nanoseconds to timespec64
531 * @nsec: the nanoseconds value to be converted
532 *
533 * Return: the timespec64 representation of the nsec parameter.
534 */
535struct timespec64 ns_to_timespec64(s64 nsec)
536{
537 struct timespec64 ts = { 0, 0 };
538 s32 rem;
539
540 if (likely(nsec > 0)) {
541 ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
542 ts.tv_nsec = rem;
543 } else if (nsec < 0) {
544 /*
545 * With negative times, tv_sec points to the earlier
546 * second, and tv_nsec counts the nanoseconds since
547 * then, so tv_nsec is always a positive number.
548 */
549 ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1;
550 ts.tv_nsec = NSEC_PER_SEC - rem - 1;
551 }
552
553 return ts;
554}
555EXPORT_SYMBOL(ns_to_timespec64);
556
557/**
558 * __msecs_to_jiffies: - convert milliseconds to jiffies
559 * @m: time in milliseconds
560 *
561 * conversion is done as follows:
562 *
563 * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
564 *
565 * - 'too large' values [that would result in larger than
566 * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
567 *
568 * - all other values are converted to jiffies by either multiplying
569 * the input value by a factor or dividing it with a factor and
570 * handling any 32-bit overflows.
571 * for the details see _msecs_to_jiffies()
572 *
573 * msecs_to_jiffies() checks for the passed in value being a constant
574 * via __builtin_constant_p() allowing gcc to eliminate most of the
575 * code, __msecs_to_jiffies() is called if the value passed does not
576 * allow constant folding and the actual conversion must be done at
577 * runtime.
578 * The _msecs_to_jiffies helpers are the HZ dependent conversion
579 * routines found in include/linux/jiffies.h
580 *
581 * Return: jiffies value
582 */
583unsigned long __msecs_to_jiffies(const unsigned int m)
584{
585 /*
586 * Negative value, means infinite timeout:
587 */
588 if ((int)m < 0)
589 return MAX_JIFFY_OFFSET;
590 return _msecs_to_jiffies(m);
591}
592EXPORT_SYMBOL(__msecs_to_jiffies);
593
594/**
595 * __usecs_to_jiffies: - convert microseconds to jiffies
596 * @u: time in milliseconds
597 *
598 * Return: jiffies value
599 */
600unsigned long __usecs_to_jiffies(const unsigned int u)
601{
602 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
603 return MAX_JIFFY_OFFSET;
604 return _usecs_to_jiffies(u);
605}
606EXPORT_SYMBOL(__usecs_to_jiffies);
607
608/**
609 * timespec64_to_jiffies - convert a timespec64 value to jiffies
610 * @value: pointer to &struct timespec64
611 *
612 * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
613 * that a remainder subtract here would not do the right thing as the
614 * resolution values don't fall on second boundaries. I.e. the line:
615 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
616 * Note that due to the small error in the multiplier here, this
617 * rounding is incorrect for sufficiently large values of tv_nsec, but
618 * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
619 * OK.
620 *
621 * Rather, we just shift the bits off the right.
622 *
623 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
624 * value to a scaled second value.
625 *
626 * Return: jiffies value
627 */
628unsigned long
629timespec64_to_jiffies(const struct timespec64 *value)
630{
631 u64 sec = value->tv_sec;
632 long nsec = value->tv_nsec + TICK_NSEC - 1;
633
634 if (sec >= MAX_SEC_IN_JIFFIES){
635 sec = MAX_SEC_IN_JIFFIES;
636 nsec = 0;
637 }
638 return ((sec * SEC_CONVERSION) +
639 (((u64)nsec * NSEC_CONVERSION) >>
640 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
641
642}
643EXPORT_SYMBOL(timespec64_to_jiffies);
644
645/**
646 * jiffies_to_timespec64 - convert jiffies value to &struct timespec64
647 * @jiffies: jiffies value
648 * @value: pointer to &struct timespec64
649 */
650void
651jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
652{
653 /*
654 * Convert jiffies to nanoseconds and separate with
655 * one divide.
656 */
657 u32 rem;
658 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
659 NSEC_PER_SEC, &rem);
660 value->tv_nsec = rem;
661}
662EXPORT_SYMBOL(jiffies_to_timespec64);
663
664/*
665 * Convert jiffies/jiffies_64 to clock_t and back.
666 */
667
668/**
669 * jiffies_to_clock_t - Convert jiffies to clock_t
670 * @x: jiffies value
671 *
672 * Return: jiffies converted to clock_t (CLOCKS_PER_SEC)
673 */
674clock_t jiffies_to_clock_t(unsigned long x)
675{
676#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
677# if HZ < USER_HZ
678 return x * (USER_HZ / HZ);
679# else
680 return x / (HZ / USER_HZ);
681# endif
682#else
683 return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
684#endif
685}
686EXPORT_SYMBOL(jiffies_to_clock_t);
687
688/**
689 * clock_t_to_jiffies - Convert clock_t to jiffies
690 * @x: clock_t value
691 *
692 * Return: clock_t value converted to jiffies
693 */
694unsigned long clock_t_to_jiffies(unsigned long x)
695{
696#if (HZ % USER_HZ)==0
697 if (x >= ~0UL / (HZ / USER_HZ))
698 return ~0UL;
699 return x * (HZ / USER_HZ);
700#else
701 /* Don't worry about loss of precision here .. */
702 if (x >= ~0UL / HZ * USER_HZ)
703 return ~0UL;
704
705 /* .. but do try to contain it here */
706 return div_u64((u64)x * HZ, USER_HZ);
707#endif
708}
709EXPORT_SYMBOL(clock_t_to_jiffies);
710
711/**
712 * jiffies_64_to_clock_t - Convert jiffies_64 to clock_t
713 * @x: jiffies_64 value
714 *
715 * Return: jiffies_64 value converted to 64-bit "clock_t" (CLOCKS_PER_SEC)
716 */
717notrace u64 jiffies_64_to_clock_t(u64 x)
718{
719#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
720# if HZ < USER_HZ
721 x = div_u64(x * USER_HZ, HZ);
722# elif HZ > USER_HZ
723 x = div_u64(x, HZ / USER_HZ);
724# else
725 /* Nothing to do */
726# endif
727#else
728 /*
729 * There are better ways that don't overflow early,
730 * but even this doesn't overflow in hundreds of years
731 * in 64 bits, so..
732 */
733 x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
734#endif
735 return x;
736}
737EXPORT_SYMBOL(jiffies_64_to_clock_t);
738
739/**
740 * nsec_to_clock_t - Convert nsec value to clock_t
741 * @x: nsec value
742 *
743 * Return: nsec value converted to 64-bit "clock_t" (CLOCKS_PER_SEC)
744 */
745u64 nsec_to_clock_t(u64 x)
746{
747#if (NSEC_PER_SEC % USER_HZ) == 0
748 return div_u64(x, NSEC_PER_SEC / USER_HZ);
749#elif (USER_HZ % 512) == 0
750 return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
751#else
752 /*
753 * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
754 * overflow after 64.99 years.
755 * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
756 */
757 return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
758#endif
759}
760
761/**
762 * jiffies64_to_nsecs - Convert jiffies64 to nanoseconds
763 * @j: jiffies64 value
764 *
765 * Return: nanoseconds value
766 */
767u64 jiffies64_to_nsecs(u64 j)
768{
769#if !(NSEC_PER_SEC % HZ)
770 return (NSEC_PER_SEC / HZ) * j;
771# else
772 return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN);
773#endif
774}
775EXPORT_SYMBOL(jiffies64_to_nsecs);
776
777/**
778 * jiffies64_to_msecs - Convert jiffies64 to milliseconds
779 * @j: jiffies64 value
780 *
781 * Return: milliseconds value
782 */
783u64 jiffies64_to_msecs(const u64 j)
784{
785#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
786 return (MSEC_PER_SEC / HZ) * j;
787#else
788 return div_u64(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN);
789#endif
790}
791EXPORT_SYMBOL(jiffies64_to_msecs);
792
793/**
794 * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
795 *
796 * @n: nsecs in u64
797 *
798 * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
799 * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
800 * for scheduler, not for use in device drivers to calculate timeout value.
801 *
802 * note:
803 * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
804 * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
805 *
806 * Return: nsecs converted to jiffies64 value
807 */
808u64 nsecs_to_jiffies64(u64 n)
809{
810#if (NSEC_PER_SEC % HZ) == 0
811 /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
812 return div_u64(n, NSEC_PER_SEC / HZ);
813#elif (HZ % 512) == 0
814 /* overflow after 292 years if HZ = 1024 */
815 return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
816#else
817 /*
818 * Generic case - optimized for cases where HZ is a multiple of 3.
819 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
820 */
821 return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
822#endif
823}
824EXPORT_SYMBOL(nsecs_to_jiffies64);
825
826/**
827 * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
828 *
829 * @n: nsecs in u64
830 *
831 * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
832 * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
833 * for scheduler, not for use in device drivers to calculate timeout value.
834 *
835 * note:
836 * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
837 * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
838 *
839 * Return: nsecs converted to jiffies value
840 */
841unsigned long nsecs_to_jiffies(u64 n)
842{
843 return (unsigned long)nsecs_to_jiffies64(n);
844}
845EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
846
847/**
848 * timespec64_add_safe - Add two timespec64 values and do a safety check
849 * for overflow.
850 * @lhs: first (left) timespec64 to add
851 * @rhs: second (right) timespec64 to add
852 *
853 * It's assumed that both values are valid (>= 0).
854 * And, each timespec64 is in normalized form.
855 *
856 * Return: sum of @lhs + @rhs
857 */
858struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
859 const struct timespec64 rhs)
860{
861 struct timespec64 res;
862
863 set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec,
864 lhs.tv_nsec + rhs.tv_nsec);
865
866 if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) {
867 res.tv_sec = TIME64_MAX;
868 res.tv_nsec = 0;
869 }
870
871 return res;
872}
873EXPORT_SYMBOL_GPL(timespec64_add_safe);
874
875/**
876 * get_timespec64 - get user's time value into kernel space
877 * @ts: destination &struct timespec64
878 * @uts: user's time value as &struct __kernel_timespec
879 *
880 * Handles compat or 32-bit modes.
881 *
882 * Return: 0 on success or negative errno on error
883 */
884int get_timespec64(struct timespec64 *ts,
885 const struct __kernel_timespec __user *uts)
886{
887 struct __kernel_timespec kts;
888 int ret;
889
890 ret = copy_from_user(&kts, uts, sizeof(kts));
891 if (ret)
892 return -EFAULT;
893
894 ts->tv_sec = kts.tv_sec;
895
896 /* Zero out the padding in compat mode */
897 if (in_compat_syscall())
898 kts.tv_nsec &= 0xFFFFFFFFUL;
899
900 /* In 32-bit mode, this drops the padding */
901 ts->tv_nsec = kts.tv_nsec;
902
903 return 0;
904}
905EXPORT_SYMBOL_GPL(get_timespec64);
906
907/**
908 * put_timespec64 - convert timespec64 value to __kernel_timespec format and
909 * copy the latter to userspace
910 * @ts: input &struct timespec64
911 * @uts: user's &struct __kernel_timespec
912 *
913 * Return: 0 on success or negative errno on error
914 */
915int put_timespec64(const struct timespec64 *ts,
916 struct __kernel_timespec __user *uts)
917{
918 struct __kernel_timespec kts = {
919 .tv_sec = ts->tv_sec,
920 .tv_nsec = ts->tv_nsec
921 };
922
923 return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0;
924}
925EXPORT_SYMBOL_GPL(put_timespec64);
926
927static int __get_old_timespec32(struct timespec64 *ts64,
928 const struct old_timespec32 __user *cts)
929{
930 struct old_timespec32 ts;
931 int ret;
932
933 ret = copy_from_user(&ts, cts, sizeof(ts));
934 if (ret)
935 return -EFAULT;
936
937 ts64->tv_sec = ts.tv_sec;
938 ts64->tv_nsec = ts.tv_nsec;
939
940 return 0;
941}
942
943static int __put_old_timespec32(const struct timespec64 *ts64,
944 struct old_timespec32 __user *cts)
945{
946 struct old_timespec32 ts = {
947 .tv_sec = ts64->tv_sec,
948 .tv_nsec = ts64->tv_nsec
949 };
950 return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0;
951}
952
953/**
954 * get_old_timespec32 - get user's old-format time value into kernel space
955 * @ts: destination &struct timespec64
956 * @uts: user's old-format time value (&struct old_timespec32)
957 *
958 * Handles X86_X32_ABI compatibility conversion.
959 *
960 * Return: 0 on success or negative errno on error
961 */
962int get_old_timespec32(struct timespec64 *ts, const void __user *uts)
963{
964 if (COMPAT_USE_64BIT_TIME)
965 return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
966 else
967 return __get_old_timespec32(ts, uts);
968}
969EXPORT_SYMBOL_GPL(get_old_timespec32);
970
971/**
972 * put_old_timespec32 - convert timespec64 value to &struct old_timespec32 and
973 * copy the latter to userspace
974 * @ts: input &struct timespec64
975 * @uts: user's &struct old_timespec32
976 *
977 * Handles X86_X32_ABI compatibility conversion.
978 *
979 * Return: 0 on success or negative errno on error
980 */
981int put_old_timespec32(const struct timespec64 *ts, void __user *uts)
982{
983 if (COMPAT_USE_64BIT_TIME)
984 return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
985 else
986 return __put_old_timespec32(ts, uts);
987}
988EXPORT_SYMBOL_GPL(put_old_timespec32);
989
990/**
991 * get_itimerspec64 - get user's &struct __kernel_itimerspec into kernel space
992 * @it: destination &struct itimerspec64
993 * @uit: user's &struct __kernel_itimerspec
994 *
995 * Return: 0 on success or negative errno on error
996 */
997int get_itimerspec64(struct itimerspec64 *it,
998 const struct __kernel_itimerspec __user *uit)
999{
1000 int ret;
1001
1002 ret = get_timespec64(&it->it_interval, &uit->it_interval);
1003 if (ret)
1004 return ret;
1005
1006 ret = get_timespec64(&it->it_value, &uit->it_value);
1007
1008 return ret;
1009}
1010EXPORT_SYMBOL_GPL(get_itimerspec64);
1011
1012/**
1013 * put_itimerspec64 - convert &struct itimerspec64 to __kernel_itimerspec format
1014 * and copy the latter to userspace
1015 * @it: input &struct itimerspec64
1016 * @uit: user's &struct __kernel_itimerspec
1017 *
1018 * Return: 0 on success or negative errno on error
1019 */
1020int put_itimerspec64(const struct itimerspec64 *it,
1021 struct __kernel_itimerspec __user *uit)
1022{
1023 int ret;
1024
1025 ret = put_timespec64(&it->it_interval, &uit->it_interval);
1026 if (ret)
1027 return ret;
1028
1029 ret = put_timespec64(&it->it_value, &uit->it_value);
1030
1031 return ret;
1032}
1033EXPORT_SYMBOL_GPL(put_itimerspec64);
1034
1035/**
1036 * get_old_itimerspec32 - get user's &struct old_itimerspec32 into kernel space
1037 * @its: destination &struct itimerspec64
1038 * @uits: user's &struct old_itimerspec32
1039 *
1040 * Return: 0 on success or negative errno on error
1041 */
1042int get_old_itimerspec32(struct itimerspec64 *its,
1043 const struct old_itimerspec32 __user *uits)
1044{
1045
1046 if (__get_old_timespec32(&its->it_interval, &uits->it_interval) ||
1047 __get_old_timespec32(&its->it_value, &uits->it_value))
1048 return -EFAULT;
1049 return 0;
1050}
1051EXPORT_SYMBOL_GPL(get_old_itimerspec32);
1052
1053/**
1054 * put_old_itimerspec32 - convert &struct itimerspec64 to &struct
1055 * old_itimerspec32 and copy the latter to userspace
1056 * @its: input &struct itimerspec64
1057 * @uits: user's &struct old_itimerspec32
1058 *
1059 * Return: 0 on success or negative errno on error
1060 */
1061int put_old_itimerspec32(const struct itimerspec64 *its,
1062 struct old_itimerspec32 __user *uits)
1063{
1064 if (__put_old_timespec32(&its->it_interval, &uits->it_interval) ||
1065 __put_old_timespec32(&its->it_value, &uits->it_value))
1066 return -EFAULT;
1067 return 0;
1068}
1069EXPORT_SYMBOL_GPL(put_old_itimerspec32);