xref: /openbmc/linux/kernel/time/time.c (revision d5b7ffbf)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  *  linux/kernel/time.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  *  Copyright (C) 1991, 1992  Linus Torvalds
55cee9645SThomas Gleixner  *
65cee9645SThomas Gleixner  *  This file contains the interface functions for the various
75cee9645SThomas Gleixner  *  time related system calls: time, stime, gettimeofday, settimeofday,
85cee9645SThomas Gleixner  *			       adjtime
95cee9645SThomas Gleixner  */
105cee9645SThomas Gleixner /*
115cee9645SThomas Gleixner  * Modification history kernel/time.c
125cee9645SThomas Gleixner  *
135cee9645SThomas Gleixner  * 1993-09-02    Philip Gladstone
145cee9645SThomas Gleixner  *      Created file with time related functions from sched/core.c and adjtimex()
155cee9645SThomas Gleixner  * 1993-10-08    Torsten Duwe
165cee9645SThomas Gleixner  *      adjtime interface update and CMOS clock write code
175cee9645SThomas Gleixner  * 1995-08-13    Torsten Duwe
185cee9645SThomas Gleixner  *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
195cee9645SThomas Gleixner  * 1999-01-16    Ulrich Windl
205cee9645SThomas Gleixner  *	Introduced error checking for many cases in adjtimex().
215cee9645SThomas Gleixner  *	Updated NTP code according to technical memorandum Jan '96
225cee9645SThomas Gleixner  *	"A Kernel Model for Precision Timekeeping" by Dave Mills
235cee9645SThomas Gleixner  *	Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
245cee9645SThomas Gleixner  *	(Even though the technical memorandum forbids it)
255cee9645SThomas Gleixner  * 2004-07-14	 Christoph Lameter
265cee9645SThomas Gleixner  *	Added getnstimeofday to allow the posix timer functions to return
275cee9645SThomas Gleixner  *	with nanosecond accuracy
285cee9645SThomas Gleixner  */
295cee9645SThomas Gleixner 
305cee9645SThomas Gleixner #include <linux/export.h>
315cee9645SThomas Gleixner #include <linux/timex.h>
325cee9645SThomas Gleixner #include <linux/capability.h>
335cee9645SThomas Gleixner #include <linux/timekeeper_internal.h>
345cee9645SThomas Gleixner #include <linux/errno.h>
355cee9645SThomas Gleixner #include <linux/syscalls.h>
365cee9645SThomas Gleixner #include <linux/security.h>
375cee9645SThomas Gleixner #include <linux/fs.h>
385cee9645SThomas Gleixner #include <linux/math64.h>
395cee9645SThomas Gleixner #include <linux/ptrace.h>
405cee9645SThomas Gleixner 
417c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
423a4d44b6SAl Viro #include <linux/compat.h>
435cee9645SThomas Gleixner #include <asm/unistd.h>
445cee9645SThomas Gleixner 
450a227985SNicholas Mc Guire #include <generated/timeconst.h>
468b094cd0SThomas Gleixner #include "timekeeping.h"
475cee9645SThomas Gleixner 
485cee9645SThomas Gleixner /*
495cee9645SThomas Gleixner  * The timezone where the local system is located.  Used as a default by some
505cee9645SThomas Gleixner  * programs who obtain this value by using gettimeofday.
515cee9645SThomas Gleixner  */
525cee9645SThomas Gleixner struct timezone sys_tz;
535cee9645SThomas Gleixner 
545cee9645SThomas Gleixner EXPORT_SYMBOL(sys_tz);
555cee9645SThomas Gleixner 
565cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_TIME
575cee9645SThomas Gleixner 
585cee9645SThomas Gleixner /*
595cee9645SThomas Gleixner  * sys_time() can be implemented in user-level using
605cee9645SThomas Gleixner  * sys_gettimeofday().  Is this for backwards compatibility?  If so,
615cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
625cee9645SThomas Gleixner  * architectures that need it).
635cee9645SThomas Gleixner  */
645cee9645SThomas Gleixner SYSCALL_DEFINE1(time, time_t __user *, tloc)
655cee9645SThomas Gleixner {
665cee9645SThomas Gleixner 	time_t i = get_seconds();
675cee9645SThomas Gleixner 
685cee9645SThomas Gleixner 	if (tloc) {
695cee9645SThomas Gleixner 		if (put_user(i,tloc))
705cee9645SThomas Gleixner 			return -EFAULT;
715cee9645SThomas Gleixner 	}
725cee9645SThomas Gleixner 	force_successful_syscall_return();
735cee9645SThomas Gleixner 	return i;
745cee9645SThomas Gleixner }
755cee9645SThomas Gleixner 
765cee9645SThomas Gleixner /*
775cee9645SThomas Gleixner  * sys_stime() can be implemented in user-level using
785cee9645SThomas Gleixner  * sys_settimeofday().  Is this for backwards compatibility?  If so,
795cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
805cee9645SThomas Gleixner  * architectures that need it).
815cee9645SThomas Gleixner  */
825cee9645SThomas Gleixner 
835cee9645SThomas Gleixner SYSCALL_DEFINE1(stime, time_t __user *, tptr)
845cee9645SThomas Gleixner {
855cee9645SThomas Gleixner 	struct timespec tv;
865cee9645SThomas Gleixner 	int err;
875cee9645SThomas Gleixner 
885cee9645SThomas Gleixner 	if (get_user(tv.tv_sec, tptr))
895cee9645SThomas Gleixner 		return -EFAULT;
905cee9645SThomas Gleixner 
915cee9645SThomas Gleixner 	tv.tv_nsec = 0;
925cee9645SThomas Gleixner 
935cee9645SThomas Gleixner 	err = security_settime(&tv, NULL);
945cee9645SThomas Gleixner 	if (err)
955cee9645SThomas Gleixner 		return err;
965cee9645SThomas Gleixner 
975cee9645SThomas Gleixner 	do_settimeofday(&tv);
985cee9645SThomas Gleixner 	return 0;
995cee9645SThomas Gleixner }
1005cee9645SThomas Gleixner 
1015cee9645SThomas Gleixner #endif /* __ARCH_WANT_SYS_TIME */
1025cee9645SThomas Gleixner 
103b180db2cSAl Viro #ifdef CONFIG_COMPAT
104b180db2cSAl Viro #ifdef __ARCH_WANT_COMPAT_SYS_TIME
105b180db2cSAl Viro 
106b180db2cSAl Viro /* compat_time_t is a 32 bit "long" and needs to get converted. */
107b180db2cSAl Viro COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc)
108b180db2cSAl Viro {
109b180db2cSAl Viro 	struct timeval tv;
110b180db2cSAl Viro 	compat_time_t i;
111b180db2cSAl Viro 
112b180db2cSAl Viro 	do_gettimeofday(&tv);
113b180db2cSAl Viro 	i = tv.tv_sec;
114b180db2cSAl Viro 
115b180db2cSAl Viro 	if (tloc) {
116b180db2cSAl Viro 		if (put_user(i,tloc))
117b180db2cSAl Viro 			return -EFAULT;
118b180db2cSAl Viro 	}
119b180db2cSAl Viro 	force_successful_syscall_return();
120b180db2cSAl Viro 	return i;
121b180db2cSAl Viro }
122b180db2cSAl Viro 
123b180db2cSAl Viro COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr)
124b180db2cSAl Viro {
125b180db2cSAl Viro 	struct timespec tv;
126b180db2cSAl Viro 	int err;
127b180db2cSAl Viro 
128b180db2cSAl Viro 	if (get_user(tv.tv_sec, tptr))
129b180db2cSAl Viro 		return -EFAULT;
130b180db2cSAl Viro 
131b180db2cSAl Viro 	tv.tv_nsec = 0;
132b180db2cSAl Viro 
133b180db2cSAl Viro 	err = security_settime(&tv, NULL);
134b180db2cSAl Viro 	if (err)
135b180db2cSAl Viro 		return err;
136b180db2cSAl Viro 
137b180db2cSAl Viro 	do_settimeofday(&tv);
138b180db2cSAl Viro 	return 0;
139b180db2cSAl Viro }
140b180db2cSAl Viro 
141b180db2cSAl Viro #endif /* __ARCH_WANT_COMPAT_SYS_TIME */
142b180db2cSAl Viro #endif
143b180db2cSAl Viro 
1445cee9645SThomas Gleixner SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
1455cee9645SThomas Gleixner 		struct timezone __user *, tz)
1465cee9645SThomas Gleixner {
1475cee9645SThomas Gleixner 	if (likely(tv != NULL)) {
1485cee9645SThomas Gleixner 		struct timeval ktv;
1495cee9645SThomas Gleixner 		do_gettimeofday(&ktv);
1505cee9645SThomas Gleixner 		if (copy_to_user(tv, &ktv, sizeof(ktv)))
1515cee9645SThomas Gleixner 			return -EFAULT;
1525cee9645SThomas Gleixner 	}
1535cee9645SThomas Gleixner 	if (unlikely(tz != NULL)) {
1545cee9645SThomas Gleixner 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1555cee9645SThomas Gleixner 			return -EFAULT;
1565cee9645SThomas Gleixner 	}
1575cee9645SThomas Gleixner 	return 0;
1585cee9645SThomas Gleixner }
1595cee9645SThomas Gleixner 
1605cee9645SThomas Gleixner /*
1615cee9645SThomas Gleixner  * Indicates if there is an offset between the system clock and the hardware
1625cee9645SThomas Gleixner  * clock/persistent clock/rtc.
1635cee9645SThomas Gleixner  */
1645cee9645SThomas Gleixner int persistent_clock_is_local;
1655cee9645SThomas Gleixner 
1665cee9645SThomas Gleixner /*
1675cee9645SThomas Gleixner  * Adjust the time obtained from the CMOS to be UTC time instead of
1685cee9645SThomas Gleixner  * local time.
1695cee9645SThomas Gleixner  *
1705cee9645SThomas Gleixner  * This is ugly, but preferable to the alternatives.  Otherwise we
1715cee9645SThomas Gleixner  * would either need to write a program to do it in /etc/rc (and risk
1725cee9645SThomas Gleixner  * confusion if the program gets run more than once; it would also be
1735cee9645SThomas Gleixner  * hard to make the program warp the clock precisely n hours)  or
1745cee9645SThomas Gleixner  * compile in the timezone information into the kernel.  Bad, bad....
1755cee9645SThomas Gleixner  *
1765cee9645SThomas Gleixner  *						- TYT, 1992-01-01
1775cee9645SThomas Gleixner  *
1785cee9645SThomas Gleixner  * The best thing to do is to keep the CMOS clock in universal time (UTC)
1795cee9645SThomas Gleixner  * as real UNIX machines always do it. This avoids all headaches about
1805cee9645SThomas Gleixner  * daylight saving times and warping kernel clocks.
1815cee9645SThomas Gleixner  */
1825cee9645SThomas Gleixner static inline void warp_clock(void)
1835cee9645SThomas Gleixner {
1845cee9645SThomas Gleixner 	if (sys_tz.tz_minuteswest != 0) {
1855cee9645SThomas Gleixner 		struct timespec adjust;
1865cee9645SThomas Gleixner 
1875cee9645SThomas Gleixner 		persistent_clock_is_local = 1;
1885cee9645SThomas Gleixner 		adjust.tv_sec = sys_tz.tz_minuteswest * 60;
1895cee9645SThomas Gleixner 		adjust.tv_nsec = 0;
1905cee9645SThomas Gleixner 		timekeeping_inject_offset(&adjust);
1915cee9645SThomas Gleixner 	}
1925cee9645SThomas Gleixner }
1935cee9645SThomas Gleixner 
1945cee9645SThomas Gleixner /*
1955cee9645SThomas Gleixner  * In case for some reason the CMOS clock has not already been running
1965cee9645SThomas Gleixner  * in UTC, but in some local time: The first time we set the timezone,
1975cee9645SThomas Gleixner  * we will warp the clock so that it is ticking UTC time instead of
1985cee9645SThomas Gleixner  * local time. Presumably, if someone is setting the timezone then we
1995cee9645SThomas Gleixner  * are running in an environment where the programs understand about
2005cee9645SThomas Gleixner  * timezones. This should be done at boot time in the /etc/rc script,
2015cee9645SThomas Gleixner  * as soon as possible, so that the clock can be set right. Otherwise,
2025cee9645SThomas Gleixner  * various programs will get confused when the clock gets warped.
2035cee9645SThomas Gleixner  */
2045cee9645SThomas Gleixner 
20586d34732SBaolin Wang int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz)
2065cee9645SThomas Gleixner {
2075cee9645SThomas Gleixner 	static int firsttime = 1;
2085cee9645SThomas Gleixner 	int error = 0;
2095cee9645SThomas Gleixner 
21086d34732SBaolin Wang 	if (tv && !timespec64_valid(tv))
2115cee9645SThomas Gleixner 		return -EINVAL;
2125cee9645SThomas Gleixner 
21386d34732SBaolin Wang 	error = security_settime64(tv, tz);
2145cee9645SThomas Gleixner 	if (error)
2155cee9645SThomas Gleixner 		return error;
2165cee9645SThomas Gleixner 
2175cee9645SThomas Gleixner 	if (tz) {
2186f7d7984SSasha Levin 		/* Verify we're witin the +-15 hrs range */
2196f7d7984SSasha Levin 		if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60)
2206f7d7984SSasha Levin 			return -EINVAL;
2216f7d7984SSasha Levin 
2225cee9645SThomas Gleixner 		sys_tz = *tz;
2235cee9645SThomas Gleixner 		update_vsyscall_tz();
2245cee9645SThomas Gleixner 		if (firsttime) {
2255cee9645SThomas Gleixner 			firsttime = 0;
2265cee9645SThomas Gleixner 			if (!tv)
2275cee9645SThomas Gleixner 				warp_clock();
2285cee9645SThomas Gleixner 		}
2295cee9645SThomas Gleixner 	}
2305cee9645SThomas Gleixner 	if (tv)
23186d34732SBaolin Wang 		return do_settimeofday64(tv);
2325cee9645SThomas Gleixner 	return 0;
2335cee9645SThomas Gleixner }
2345cee9645SThomas Gleixner 
2355cee9645SThomas Gleixner SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
2365cee9645SThomas Gleixner 		struct timezone __user *, tz)
2375cee9645SThomas Gleixner {
2382ac00f17SDeepa Dinamani 	struct timespec64 new_ts;
2395cee9645SThomas Gleixner 	struct timeval user_tv;
2405cee9645SThomas Gleixner 	struct timezone new_tz;
2415cee9645SThomas Gleixner 
2425cee9645SThomas Gleixner 	if (tv) {
2435cee9645SThomas Gleixner 		if (copy_from_user(&user_tv, tv, sizeof(*tv)))
2445cee9645SThomas Gleixner 			return -EFAULT;
2456ada1fc0SSasha Levin 
2466ada1fc0SSasha Levin 		if (!timeval_valid(&user_tv))
2476ada1fc0SSasha Levin 			return -EINVAL;
2486ada1fc0SSasha Levin 
2495cee9645SThomas Gleixner 		new_ts.tv_sec = user_tv.tv_sec;
2505cee9645SThomas Gleixner 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
2515cee9645SThomas Gleixner 	}
2525cee9645SThomas Gleixner 	if (tz) {
2535cee9645SThomas Gleixner 		if (copy_from_user(&new_tz, tz, sizeof(*tz)))
2545cee9645SThomas Gleixner 			return -EFAULT;
2555cee9645SThomas Gleixner 	}
2565cee9645SThomas Gleixner 
2572ac00f17SDeepa Dinamani 	return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
2585cee9645SThomas Gleixner }
2595cee9645SThomas Gleixner 
2602b2d0285SAl Viro #ifdef CONFIG_COMPAT
2612b2d0285SAl Viro COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv,
2622b2d0285SAl Viro 		       struct timezone __user *, tz)
2632b2d0285SAl Viro {
2642b2d0285SAl Viro 	if (tv) {
2652b2d0285SAl Viro 		struct timeval ktv;
2662b2d0285SAl Viro 
2672b2d0285SAl Viro 		do_gettimeofday(&ktv);
2682b2d0285SAl Viro 		if (compat_put_timeval(&ktv, tv))
2692b2d0285SAl Viro 			return -EFAULT;
2702b2d0285SAl Viro 	}
2712b2d0285SAl Viro 	if (tz) {
2722b2d0285SAl Viro 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
2732b2d0285SAl Viro 			return -EFAULT;
2742b2d0285SAl Viro 	}
2752b2d0285SAl Viro 
2762b2d0285SAl Viro 	return 0;
2772b2d0285SAl Viro }
2782b2d0285SAl Viro 
2792b2d0285SAl Viro COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv,
2802b2d0285SAl Viro 		       struct timezone __user *, tz)
2812b2d0285SAl Viro {
2822b2d0285SAl Viro 	struct timespec64 new_ts;
2832b2d0285SAl Viro 	struct timeval user_tv;
2842b2d0285SAl Viro 	struct timezone new_tz;
2852b2d0285SAl Viro 
2862b2d0285SAl Viro 	if (tv) {
2872b2d0285SAl Viro 		if (compat_get_timeval(&user_tv, tv))
2882b2d0285SAl Viro 			return -EFAULT;
2892b2d0285SAl Viro 		new_ts.tv_sec = user_tv.tv_sec;
2902b2d0285SAl Viro 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
2912b2d0285SAl Viro 	}
2922b2d0285SAl Viro 	if (tz) {
2932b2d0285SAl Viro 		if (copy_from_user(&new_tz, tz, sizeof(*tz)))
2942b2d0285SAl Viro 			return -EFAULT;
2952b2d0285SAl Viro 	}
2962b2d0285SAl Viro 
2972b2d0285SAl Viro 	return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
2982b2d0285SAl Viro }
2992b2d0285SAl Viro #endif
3002b2d0285SAl Viro 
3015cee9645SThomas Gleixner SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
3025cee9645SThomas Gleixner {
3035cee9645SThomas Gleixner 	struct timex txc;		/* Local copy of parameter */
3045cee9645SThomas Gleixner 	int ret;
3055cee9645SThomas Gleixner 
3065cee9645SThomas Gleixner 	/* Copy the user data space into the kernel copy
3075cee9645SThomas Gleixner 	 * structure. But bear in mind that the structures
3085cee9645SThomas Gleixner 	 * may change
3095cee9645SThomas Gleixner 	 */
3105cee9645SThomas Gleixner 	if (copy_from_user(&txc, txc_p, sizeof(struct timex)))
3115cee9645SThomas Gleixner 		return -EFAULT;
3125cee9645SThomas Gleixner 	ret = do_adjtimex(&txc);
3135cee9645SThomas Gleixner 	return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
3145cee9645SThomas Gleixner }
3155cee9645SThomas Gleixner 
3163a4d44b6SAl Viro #ifdef CONFIG_COMPAT
3173a4d44b6SAl Viro 
3183a4d44b6SAl Viro COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp)
3193a4d44b6SAl Viro {
3203a4d44b6SAl Viro 	struct timex txc;
3213a4d44b6SAl Viro 	int err, ret;
3223a4d44b6SAl Viro 
3233a4d44b6SAl Viro 	err = compat_get_timex(&txc, utp);
3243a4d44b6SAl Viro 	if (err)
3253a4d44b6SAl Viro 		return err;
3263a4d44b6SAl Viro 
3273a4d44b6SAl Viro 	ret = do_adjtimex(&txc);
3283a4d44b6SAl Viro 
3293a4d44b6SAl Viro 	err = compat_put_timex(utp, &txc);
3303a4d44b6SAl Viro 	if (err)
3313a4d44b6SAl Viro 		return err;
3323a4d44b6SAl Viro 
3333a4d44b6SAl Viro 	return ret;
3343a4d44b6SAl Viro }
3353a4d44b6SAl Viro #endif
3363a4d44b6SAl Viro 
3375cee9645SThomas Gleixner /*
3385cee9645SThomas Gleixner  * Convert jiffies to milliseconds and back.
3395cee9645SThomas Gleixner  *
3405cee9645SThomas Gleixner  * Avoid unnecessary multiplications/divisions in the
3415cee9645SThomas Gleixner  * two most common HZ cases:
3425cee9645SThomas Gleixner  */
3435cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j)
3445cee9645SThomas Gleixner {
3455cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
3465cee9645SThomas Gleixner 	return (MSEC_PER_SEC / HZ) * j;
3475cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
3485cee9645SThomas Gleixner 	return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
3495cee9645SThomas Gleixner #else
3505cee9645SThomas Gleixner # if BITS_PER_LONG == 32
3515cee9645SThomas Gleixner 	return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
3525cee9645SThomas Gleixner # else
3535cee9645SThomas Gleixner 	return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
3545cee9645SThomas Gleixner # endif
3555cee9645SThomas Gleixner #endif
3565cee9645SThomas Gleixner }
3575cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs);
3585cee9645SThomas Gleixner 
3595cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j)
3605cee9645SThomas Gleixner {
361e0758676SFrederic Weisbecker 	/*
362e0758676SFrederic Weisbecker 	 * Hz usually doesn't go much further MSEC_PER_SEC.
363e0758676SFrederic Weisbecker 	 * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
364e0758676SFrederic Weisbecker 	 */
365e0758676SFrederic Weisbecker 	BUILD_BUG_ON(HZ > USEC_PER_SEC);
366e0758676SFrederic Weisbecker 
367e0758676SFrederic Weisbecker #if !(USEC_PER_SEC % HZ)
3685cee9645SThomas Gleixner 	return (USEC_PER_SEC / HZ) * j;
3695cee9645SThomas Gleixner #else
3705cee9645SThomas Gleixner # if BITS_PER_LONG == 32
3715cee9645SThomas Gleixner 	return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
3725cee9645SThomas Gleixner # else
3735cee9645SThomas Gleixner 	return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
3745cee9645SThomas Gleixner # endif
3755cee9645SThomas Gleixner #endif
3765cee9645SThomas Gleixner }
3775cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs);
3785cee9645SThomas Gleixner 
3795cee9645SThomas Gleixner /**
3805cee9645SThomas Gleixner  * timespec_trunc - Truncate timespec to a granularity
3815cee9645SThomas Gleixner  * @t: Timespec
3825cee9645SThomas Gleixner  * @gran: Granularity in ns.
3835cee9645SThomas Gleixner  *
384de4a95faSKarsten Blees  * Truncate a timespec to a granularity. Always rounds down. gran must
385de4a95faSKarsten Blees  * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
3865cee9645SThomas Gleixner  */
3875cee9645SThomas Gleixner struct timespec timespec_trunc(struct timespec t, unsigned gran)
3885cee9645SThomas Gleixner {
389de4a95faSKarsten Blees 	/* Avoid division in the common cases 1 ns and 1 s. */
390de4a95faSKarsten Blees 	if (gran == 1) {
3915cee9645SThomas Gleixner 		/* nothing */
392de4a95faSKarsten Blees 	} else if (gran == NSEC_PER_SEC) {
3935cee9645SThomas Gleixner 		t.tv_nsec = 0;
394de4a95faSKarsten Blees 	} else if (gran > 1 && gran < NSEC_PER_SEC) {
3955cee9645SThomas Gleixner 		t.tv_nsec -= t.tv_nsec % gran;
396de4a95faSKarsten Blees 	} else {
397de4a95faSKarsten Blees 		WARN(1, "illegal file time granularity: %u", gran);
3985cee9645SThomas Gleixner 	}
3995cee9645SThomas Gleixner 	return t;
4005cee9645SThomas Gleixner }
4015cee9645SThomas Gleixner EXPORT_SYMBOL(timespec_trunc);
4025cee9645SThomas Gleixner 
40390b6ce9cSpang.xunlei /*
40490b6ce9cSpang.xunlei  * mktime64 - Converts date to seconds.
40590b6ce9cSpang.xunlei  * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
4065cee9645SThomas Gleixner  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
4075cee9645SThomas Gleixner  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
4085cee9645SThomas Gleixner  *
4095cee9645SThomas Gleixner  * [For the Julian calendar (which was used in Russia before 1917,
4105cee9645SThomas Gleixner  * Britain & colonies before 1752, anywhere else before 1582,
4115cee9645SThomas Gleixner  * and is still in use by some communities) leave out the
4125cee9645SThomas Gleixner  * -year/100+year/400 terms, and add 10.]
4135cee9645SThomas Gleixner  *
4145cee9645SThomas Gleixner  * This algorithm was first published by Gauss (I think).
415ede5147dSDavid Howells  *
416ede5147dSDavid Howells  * A leap second can be indicated by calling this function with sec as
417ede5147dSDavid Howells  * 60 (allowable under ISO 8601).  The leap second is treated the same
418ede5147dSDavid Howells  * as the following second since they don't exist in UNIX time.
419ede5147dSDavid Howells  *
420ede5147dSDavid Howells  * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
421ede5147dSDavid Howells  * tomorrow - (allowable under ISO 8601) is supported.
4225cee9645SThomas Gleixner  */
42390b6ce9cSpang.xunlei time64_t mktime64(const unsigned int year0, const unsigned int mon0,
4245cee9645SThomas Gleixner 		const unsigned int day, const unsigned int hour,
4255cee9645SThomas Gleixner 		const unsigned int min, const unsigned int sec)
4265cee9645SThomas Gleixner {
4275cee9645SThomas Gleixner 	unsigned int mon = mon0, year = year0;
4285cee9645SThomas Gleixner 
4295cee9645SThomas Gleixner 	/* 1..12 -> 11,12,1..10 */
4305cee9645SThomas Gleixner 	if (0 >= (int) (mon -= 2)) {
4315cee9645SThomas Gleixner 		mon += 12;	/* Puts Feb last since it has leap day */
4325cee9645SThomas Gleixner 		year -= 1;
4335cee9645SThomas Gleixner 	}
4345cee9645SThomas Gleixner 
43590b6ce9cSpang.xunlei 	return ((((time64_t)
4365cee9645SThomas Gleixner 		  (year/4 - year/100 + year/400 + 367*mon/12 + day) +
4375cee9645SThomas Gleixner 		  year*365 - 719499
438ede5147dSDavid Howells 	    )*24 + hour /* now have hours - midnight tomorrow handled here */
4395cee9645SThomas Gleixner 	  )*60 + min /* now have minutes */
4405cee9645SThomas Gleixner 	)*60 + sec; /* finally seconds */
4415cee9645SThomas Gleixner }
44290b6ce9cSpang.xunlei EXPORT_SYMBOL(mktime64);
4435cee9645SThomas Gleixner 
4445cee9645SThomas Gleixner /**
4455cee9645SThomas Gleixner  * set_normalized_timespec - set timespec sec and nsec parts and normalize
4465cee9645SThomas Gleixner  *
4475cee9645SThomas Gleixner  * @ts:		pointer to timespec variable to be set
4485cee9645SThomas Gleixner  * @sec:	seconds to set
4495cee9645SThomas Gleixner  * @nsec:	nanoseconds to set
4505cee9645SThomas Gleixner  *
4515cee9645SThomas Gleixner  * Set seconds and nanoseconds field of a timespec variable and
4525cee9645SThomas Gleixner  * normalize to the timespec storage format
4535cee9645SThomas Gleixner  *
4545cee9645SThomas Gleixner  * Note: The tv_nsec part is always in the range of
4555cee9645SThomas Gleixner  *	0 <= tv_nsec < NSEC_PER_SEC
4565cee9645SThomas Gleixner  * For negative values only the tv_sec field is negative !
4575cee9645SThomas Gleixner  */
4585cee9645SThomas Gleixner void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
4595cee9645SThomas Gleixner {
4605cee9645SThomas Gleixner 	while (nsec >= NSEC_PER_SEC) {
4615cee9645SThomas Gleixner 		/*
4625cee9645SThomas Gleixner 		 * The following asm() prevents the compiler from
4635cee9645SThomas Gleixner 		 * optimising this loop into a modulo operation. See
4645cee9645SThomas Gleixner 		 * also __iter_div_u64_rem() in include/linux/time.h
4655cee9645SThomas Gleixner 		 */
4665cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
4675cee9645SThomas Gleixner 		nsec -= NSEC_PER_SEC;
4685cee9645SThomas Gleixner 		++sec;
4695cee9645SThomas Gleixner 	}
4705cee9645SThomas Gleixner 	while (nsec < 0) {
4715cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
4725cee9645SThomas Gleixner 		nsec += NSEC_PER_SEC;
4735cee9645SThomas Gleixner 		--sec;
4745cee9645SThomas Gleixner 	}
4755cee9645SThomas Gleixner 	ts->tv_sec = sec;
4765cee9645SThomas Gleixner 	ts->tv_nsec = nsec;
4775cee9645SThomas Gleixner }
4785cee9645SThomas Gleixner EXPORT_SYMBOL(set_normalized_timespec);
4795cee9645SThomas Gleixner 
4805cee9645SThomas Gleixner /**
4815cee9645SThomas Gleixner  * ns_to_timespec - Convert nanoseconds to timespec
4825cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
4835cee9645SThomas Gleixner  *
4845cee9645SThomas Gleixner  * Returns the timespec representation of the nsec parameter.
4855cee9645SThomas Gleixner  */
4865cee9645SThomas Gleixner struct timespec ns_to_timespec(const s64 nsec)
4875cee9645SThomas Gleixner {
4885cee9645SThomas Gleixner 	struct timespec ts;
4895cee9645SThomas Gleixner 	s32 rem;
4905cee9645SThomas Gleixner 
4915cee9645SThomas Gleixner 	if (!nsec)
4925cee9645SThomas Gleixner 		return (struct timespec) {0, 0};
4935cee9645SThomas Gleixner 
4945cee9645SThomas Gleixner 	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
4955cee9645SThomas Gleixner 	if (unlikely(rem < 0)) {
4965cee9645SThomas Gleixner 		ts.tv_sec--;
4975cee9645SThomas Gleixner 		rem += NSEC_PER_SEC;
4985cee9645SThomas Gleixner 	}
4995cee9645SThomas Gleixner 	ts.tv_nsec = rem;
5005cee9645SThomas Gleixner 
5015cee9645SThomas Gleixner 	return ts;
5025cee9645SThomas Gleixner }
5035cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timespec);
5045cee9645SThomas Gleixner 
5055cee9645SThomas Gleixner /**
5065cee9645SThomas Gleixner  * ns_to_timeval - Convert nanoseconds to timeval
5075cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
5085cee9645SThomas Gleixner  *
5095cee9645SThomas Gleixner  * Returns the timeval representation of the nsec parameter.
5105cee9645SThomas Gleixner  */
5115cee9645SThomas Gleixner struct timeval ns_to_timeval(const s64 nsec)
5125cee9645SThomas Gleixner {
5135cee9645SThomas Gleixner 	struct timespec ts = ns_to_timespec(nsec);
5145cee9645SThomas Gleixner 	struct timeval tv;
5155cee9645SThomas Gleixner 
5165cee9645SThomas Gleixner 	tv.tv_sec = ts.tv_sec;
5175cee9645SThomas Gleixner 	tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
5185cee9645SThomas Gleixner 
5195cee9645SThomas Gleixner 	return tv;
5205cee9645SThomas Gleixner }
5215cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timeval);
5225cee9645SThomas Gleixner 
52349cd6f86SJohn Stultz #if BITS_PER_LONG == 32
52449cd6f86SJohn Stultz /**
52549cd6f86SJohn Stultz  * set_normalized_timespec - set timespec sec and nsec parts and normalize
52649cd6f86SJohn Stultz  *
52749cd6f86SJohn Stultz  * @ts:		pointer to timespec variable to be set
52849cd6f86SJohn Stultz  * @sec:	seconds to set
52949cd6f86SJohn Stultz  * @nsec:	nanoseconds to set
53049cd6f86SJohn Stultz  *
53149cd6f86SJohn Stultz  * Set seconds and nanoseconds field of a timespec variable and
53249cd6f86SJohn Stultz  * normalize to the timespec storage format
53349cd6f86SJohn Stultz  *
53449cd6f86SJohn Stultz  * Note: The tv_nsec part is always in the range of
53549cd6f86SJohn Stultz  *	0 <= tv_nsec < NSEC_PER_SEC
53649cd6f86SJohn Stultz  * For negative values only the tv_sec field is negative !
53749cd6f86SJohn Stultz  */
53849cd6f86SJohn Stultz void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
53949cd6f86SJohn Stultz {
54049cd6f86SJohn Stultz 	while (nsec >= NSEC_PER_SEC) {
54149cd6f86SJohn Stultz 		/*
54249cd6f86SJohn Stultz 		 * The following asm() prevents the compiler from
54349cd6f86SJohn Stultz 		 * optimising this loop into a modulo operation. See
54449cd6f86SJohn Stultz 		 * also __iter_div_u64_rem() in include/linux/time.h
54549cd6f86SJohn Stultz 		 */
54649cd6f86SJohn Stultz 		asm("" : "+rm"(nsec));
54749cd6f86SJohn Stultz 		nsec -= NSEC_PER_SEC;
54849cd6f86SJohn Stultz 		++sec;
54949cd6f86SJohn Stultz 	}
55049cd6f86SJohn Stultz 	while (nsec < 0) {
55149cd6f86SJohn Stultz 		asm("" : "+rm"(nsec));
55249cd6f86SJohn Stultz 		nsec += NSEC_PER_SEC;
55349cd6f86SJohn Stultz 		--sec;
55449cd6f86SJohn Stultz 	}
55549cd6f86SJohn Stultz 	ts->tv_sec = sec;
55649cd6f86SJohn Stultz 	ts->tv_nsec = nsec;
55749cd6f86SJohn Stultz }
55849cd6f86SJohn Stultz EXPORT_SYMBOL(set_normalized_timespec64);
55949cd6f86SJohn Stultz 
56049cd6f86SJohn Stultz /**
56149cd6f86SJohn Stultz  * ns_to_timespec64 - Convert nanoseconds to timespec64
56249cd6f86SJohn Stultz  * @nsec:       the nanoseconds value to be converted
56349cd6f86SJohn Stultz  *
56449cd6f86SJohn Stultz  * Returns the timespec64 representation of the nsec parameter.
56549cd6f86SJohn Stultz  */
56649cd6f86SJohn Stultz struct timespec64 ns_to_timespec64(const s64 nsec)
56749cd6f86SJohn Stultz {
56849cd6f86SJohn Stultz 	struct timespec64 ts;
56949cd6f86SJohn Stultz 	s32 rem;
57049cd6f86SJohn Stultz 
57149cd6f86SJohn Stultz 	if (!nsec)
57249cd6f86SJohn Stultz 		return (struct timespec64) {0, 0};
57349cd6f86SJohn Stultz 
57449cd6f86SJohn Stultz 	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
57549cd6f86SJohn Stultz 	if (unlikely(rem < 0)) {
57649cd6f86SJohn Stultz 		ts.tv_sec--;
57749cd6f86SJohn Stultz 		rem += NSEC_PER_SEC;
57849cd6f86SJohn Stultz 	}
57949cd6f86SJohn Stultz 	ts.tv_nsec = rem;
58049cd6f86SJohn Stultz 
58149cd6f86SJohn Stultz 	return ts;
58249cd6f86SJohn Stultz }
58349cd6f86SJohn Stultz EXPORT_SYMBOL(ns_to_timespec64);
58449cd6f86SJohn Stultz #endif
585ca42aaf0SNicholas Mc Guire /**
586ca42aaf0SNicholas Mc Guire  * msecs_to_jiffies: - convert milliseconds to jiffies
587ca42aaf0SNicholas Mc Guire  * @m:	time in milliseconds
588ca42aaf0SNicholas Mc Guire  *
589ca42aaf0SNicholas Mc Guire  * conversion is done as follows:
5905cee9645SThomas Gleixner  *
5915cee9645SThomas Gleixner  * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
5925cee9645SThomas Gleixner  *
5935cee9645SThomas Gleixner  * - 'too large' values [that would result in larger than
5945cee9645SThomas Gleixner  *   MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
5955cee9645SThomas Gleixner  *
5965cee9645SThomas Gleixner  * - all other values are converted to jiffies by either multiplying
597ca42aaf0SNicholas Mc Guire  *   the input value by a factor or dividing it with a factor and
598ca42aaf0SNicholas Mc Guire  *   handling any 32-bit overflows.
599ca42aaf0SNicholas Mc Guire  *   for the details see __msecs_to_jiffies()
6005cee9645SThomas Gleixner  *
601ca42aaf0SNicholas Mc Guire  * msecs_to_jiffies() checks for the passed in value being a constant
602ca42aaf0SNicholas Mc Guire  * via __builtin_constant_p() allowing gcc to eliminate most of the
603ca42aaf0SNicholas Mc Guire  * code, __msecs_to_jiffies() is called if the value passed does not
604ca42aaf0SNicholas Mc Guire  * allow constant folding and the actual conversion must be done at
605ca42aaf0SNicholas Mc Guire  * runtime.
606ca42aaf0SNicholas Mc Guire  * the _msecs_to_jiffies helpers are the HZ dependent conversion
607ca42aaf0SNicholas Mc Guire  * routines found in include/linux/jiffies.h
6085cee9645SThomas Gleixner  */
609ca42aaf0SNicholas Mc Guire unsigned long __msecs_to_jiffies(const unsigned int m)
6105cee9645SThomas Gleixner {
6115cee9645SThomas Gleixner 	/*
6125cee9645SThomas Gleixner 	 * Negative value, means infinite timeout:
6135cee9645SThomas Gleixner 	 */
6145cee9645SThomas Gleixner 	if ((int)m < 0)
6155cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
616ca42aaf0SNicholas Mc Guire 	return _msecs_to_jiffies(m);
6175cee9645SThomas Gleixner }
618ca42aaf0SNicholas Mc Guire EXPORT_SYMBOL(__msecs_to_jiffies);
6195cee9645SThomas Gleixner 
620ae60d6a0SNicholas Mc Guire unsigned long __usecs_to_jiffies(const unsigned int u)
6215cee9645SThomas Gleixner {
6225cee9645SThomas Gleixner 	if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
6235cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
624ae60d6a0SNicholas Mc Guire 	return _usecs_to_jiffies(u);
6255cee9645SThomas Gleixner }
626ae60d6a0SNicholas Mc Guire EXPORT_SYMBOL(__usecs_to_jiffies);
6275cee9645SThomas Gleixner 
6285cee9645SThomas Gleixner /*
6295cee9645SThomas Gleixner  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
6305cee9645SThomas Gleixner  * that a remainder subtract here would not do the right thing as the
6315cee9645SThomas Gleixner  * resolution values don't fall on second boundries.  I.e. the line:
6325cee9645SThomas Gleixner  * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
633d78c9300SAndrew Hunter  * Note that due to the small error in the multiplier here, this
634d78c9300SAndrew Hunter  * rounding is incorrect for sufficiently large values of tv_nsec, but
635d78c9300SAndrew Hunter  * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
636d78c9300SAndrew Hunter  * OK.
6375cee9645SThomas Gleixner  *
6385cee9645SThomas Gleixner  * Rather, we just shift the bits off the right.
6395cee9645SThomas Gleixner  *
6405cee9645SThomas Gleixner  * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
6415cee9645SThomas Gleixner  * value to a scaled second value.
6425cee9645SThomas Gleixner  */
643d78c9300SAndrew Hunter static unsigned long
6449ca30850SBaolin Wang __timespec64_to_jiffies(u64 sec, long nsec)
6455cee9645SThomas Gleixner {
646d78c9300SAndrew Hunter 	nsec = nsec + TICK_NSEC - 1;
6475cee9645SThomas Gleixner 
6485cee9645SThomas Gleixner 	if (sec >= MAX_SEC_IN_JIFFIES){
6495cee9645SThomas Gleixner 		sec = MAX_SEC_IN_JIFFIES;
6505cee9645SThomas Gleixner 		nsec = 0;
6515cee9645SThomas Gleixner 	}
6529ca30850SBaolin Wang 	return ((sec * SEC_CONVERSION) +
6535cee9645SThomas Gleixner 		(((u64)nsec * NSEC_CONVERSION) >>
6545cee9645SThomas Gleixner 		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
6555cee9645SThomas Gleixner 
6565cee9645SThomas Gleixner }
657d78c9300SAndrew Hunter 
6589ca30850SBaolin Wang static unsigned long
6599ca30850SBaolin Wang __timespec_to_jiffies(unsigned long sec, long nsec)
660d78c9300SAndrew Hunter {
6619ca30850SBaolin Wang 	return __timespec64_to_jiffies((u64)sec, nsec);
662d78c9300SAndrew Hunter }
663d78c9300SAndrew Hunter 
6649ca30850SBaolin Wang unsigned long
6659ca30850SBaolin Wang timespec64_to_jiffies(const struct timespec64 *value)
6669ca30850SBaolin Wang {
6679ca30850SBaolin Wang 	return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
6689ca30850SBaolin Wang }
6699ca30850SBaolin Wang EXPORT_SYMBOL(timespec64_to_jiffies);
6705cee9645SThomas Gleixner 
6715cee9645SThomas Gleixner void
6729ca30850SBaolin Wang jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
6735cee9645SThomas Gleixner {
6745cee9645SThomas Gleixner 	/*
6755cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
6765cee9645SThomas Gleixner 	 * one divide.
6775cee9645SThomas Gleixner 	 */
6785cee9645SThomas Gleixner 	u32 rem;
6795cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
6805cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
6815cee9645SThomas Gleixner 	value->tv_nsec = rem;
6825cee9645SThomas Gleixner }
6839ca30850SBaolin Wang EXPORT_SYMBOL(jiffies_to_timespec64);
6845cee9645SThomas Gleixner 
685d78c9300SAndrew Hunter /*
686d78c9300SAndrew Hunter  * We could use a similar algorithm to timespec_to_jiffies (with a
687d78c9300SAndrew Hunter  * different multiplier for usec instead of nsec). But this has a
688d78c9300SAndrew Hunter  * problem with rounding: we can't exactly add TICK_NSEC - 1 to the
689d78c9300SAndrew Hunter  * usec value, since it's not necessarily integral.
6905cee9645SThomas Gleixner  *
691d78c9300SAndrew Hunter  * We could instead round in the intermediate scaled representation
692d78c9300SAndrew Hunter  * (i.e. in units of 1/2^(large scale) jiffies) but that's also
693d78c9300SAndrew Hunter  * perilous: the scaling introduces a small positive error, which
694d78c9300SAndrew Hunter  * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1
695d78c9300SAndrew Hunter  * units to the intermediate before shifting) leads to accidental
696d78c9300SAndrew Hunter  * overflow and overestimates.
697d78c9300SAndrew Hunter  *
698d78c9300SAndrew Hunter  * At the cost of one additional multiplication by a constant, just
699d78c9300SAndrew Hunter  * use the timespec implementation.
7005cee9645SThomas Gleixner  */
7015cee9645SThomas Gleixner unsigned long
7025cee9645SThomas Gleixner timeval_to_jiffies(const struct timeval *value)
7035cee9645SThomas Gleixner {
704d78c9300SAndrew Hunter 	return __timespec_to_jiffies(value->tv_sec,
705d78c9300SAndrew Hunter 				     value->tv_usec * NSEC_PER_USEC);
7065cee9645SThomas Gleixner }
7075cee9645SThomas Gleixner EXPORT_SYMBOL(timeval_to_jiffies);
7085cee9645SThomas Gleixner 
7095cee9645SThomas Gleixner void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
7105cee9645SThomas Gleixner {
7115cee9645SThomas Gleixner 	/*
7125cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
7135cee9645SThomas Gleixner 	 * one divide.
7145cee9645SThomas Gleixner 	 */
7155cee9645SThomas Gleixner 	u32 rem;
7165cee9645SThomas Gleixner 
7175cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
7185cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
7195cee9645SThomas Gleixner 	value->tv_usec = rem / NSEC_PER_USEC;
7205cee9645SThomas Gleixner }
7215cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timeval);
7225cee9645SThomas Gleixner 
7235cee9645SThomas Gleixner /*
7245cee9645SThomas Gleixner  * Convert jiffies/jiffies_64 to clock_t and back.
7255cee9645SThomas Gleixner  */
7265cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x)
7275cee9645SThomas Gleixner {
7285cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
7295cee9645SThomas Gleixner # if HZ < USER_HZ
7305cee9645SThomas Gleixner 	return x * (USER_HZ / HZ);
7315cee9645SThomas Gleixner # else
7325cee9645SThomas Gleixner 	return x / (HZ / USER_HZ);
7335cee9645SThomas Gleixner # endif
7345cee9645SThomas Gleixner #else
7355cee9645SThomas Gleixner 	return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
7365cee9645SThomas Gleixner #endif
7375cee9645SThomas Gleixner }
7385cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t);
7395cee9645SThomas Gleixner 
7405cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x)
7415cee9645SThomas Gleixner {
7425cee9645SThomas Gleixner #if (HZ % USER_HZ)==0
7435cee9645SThomas Gleixner 	if (x >= ~0UL / (HZ / USER_HZ))
7445cee9645SThomas Gleixner 		return ~0UL;
7455cee9645SThomas Gleixner 	return x * (HZ / USER_HZ);
7465cee9645SThomas Gleixner #else
7475cee9645SThomas Gleixner 	/* Don't worry about loss of precision here .. */
7485cee9645SThomas Gleixner 	if (x >= ~0UL / HZ * USER_HZ)
7495cee9645SThomas Gleixner 		return ~0UL;
7505cee9645SThomas Gleixner 
7515cee9645SThomas Gleixner 	/* .. but do try to contain it here */
7525cee9645SThomas Gleixner 	return div_u64((u64)x * HZ, USER_HZ);
7535cee9645SThomas Gleixner #endif
7545cee9645SThomas Gleixner }
7555cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies);
7565cee9645SThomas Gleixner 
7575cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x)
7585cee9645SThomas Gleixner {
7595cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
7605cee9645SThomas Gleixner # if HZ < USER_HZ
7615cee9645SThomas Gleixner 	x = div_u64(x * USER_HZ, HZ);
7625cee9645SThomas Gleixner # elif HZ > USER_HZ
7635cee9645SThomas Gleixner 	x = div_u64(x, HZ / USER_HZ);
7645cee9645SThomas Gleixner # else
7655cee9645SThomas Gleixner 	/* Nothing to do */
7665cee9645SThomas Gleixner # endif
7675cee9645SThomas Gleixner #else
7685cee9645SThomas Gleixner 	/*
7695cee9645SThomas Gleixner 	 * There are better ways that don't overflow early,
7705cee9645SThomas Gleixner 	 * but even this doesn't overflow in hundreds of years
7715cee9645SThomas Gleixner 	 * in 64 bits, so..
7725cee9645SThomas Gleixner 	 */
7735cee9645SThomas Gleixner 	x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
7745cee9645SThomas Gleixner #endif
7755cee9645SThomas Gleixner 	return x;
7765cee9645SThomas Gleixner }
7775cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t);
7785cee9645SThomas Gleixner 
7795cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x)
7805cee9645SThomas Gleixner {
7815cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0
7825cee9645SThomas Gleixner 	return div_u64(x, NSEC_PER_SEC / USER_HZ);
7835cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0
7845cee9645SThomas Gleixner 	return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
7855cee9645SThomas Gleixner #else
7865cee9645SThomas Gleixner 	/*
7875cee9645SThomas Gleixner          * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
7885cee9645SThomas Gleixner          * overflow after 64.99 years.
7895cee9645SThomas Gleixner          * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
7905cee9645SThomas Gleixner          */
7915cee9645SThomas Gleixner 	return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
7925cee9645SThomas Gleixner #endif
7935cee9645SThomas Gleixner }
7945cee9645SThomas Gleixner 
79507e5f5e3SFrederic Weisbecker u64 jiffies64_to_nsecs(u64 j)
79607e5f5e3SFrederic Weisbecker {
79707e5f5e3SFrederic Weisbecker #if !(NSEC_PER_SEC % HZ)
79807e5f5e3SFrederic Weisbecker 	return (NSEC_PER_SEC / HZ) * j;
79907e5f5e3SFrederic Weisbecker # else
80007e5f5e3SFrederic Weisbecker 	return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN);
80107e5f5e3SFrederic Weisbecker #endif
80207e5f5e3SFrederic Weisbecker }
80307e5f5e3SFrederic Weisbecker EXPORT_SYMBOL(jiffies64_to_nsecs);
80407e5f5e3SFrederic Weisbecker 
8055cee9645SThomas Gleixner /**
8065cee9645SThomas Gleixner  * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
8075cee9645SThomas Gleixner  *
8085cee9645SThomas Gleixner  * @n:	nsecs in u64
8095cee9645SThomas Gleixner  *
8105cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
8115cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
8125cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
8135cee9645SThomas Gleixner  *
8145cee9645SThomas Gleixner  * note:
8155cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
8165cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
8175cee9645SThomas Gleixner  */
8185cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n)
8195cee9645SThomas Gleixner {
8205cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0
8215cee9645SThomas Gleixner 	/* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
8225cee9645SThomas Gleixner 	return div_u64(n, NSEC_PER_SEC / HZ);
8235cee9645SThomas Gleixner #elif (HZ % 512) == 0
8245cee9645SThomas Gleixner 	/* overflow after 292 years if HZ = 1024 */
8255cee9645SThomas Gleixner 	return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
8265cee9645SThomas Gleixner #else
8275cee9645SThomas Gleixner 	/*
8285cee9645SThomas Gleixner 	 * Generic case - optimized for cases where HZ is a multiple of 3.
8295cee9645SThomas Gleixner 	 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
8305cee9645SThomas Gleixner 	 */
8315cee9645SThomas Gleixner 	return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
8325cee9645SThomas Gleixner #endif
8335cee9645SThomas Gleixner }
8347bd0e226SDaniel Vetter EXPORT_SYMBOL(nsecs_to_jiffies64);
8355cee9645SThomas Gleixner 
8365cee9645SThomas Gleixner /**
8375cee9645SThomas Gleixner  * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
8385cee9645SThomas Gleixner  *
8395cee9645SThomas Gleixner  * @n:	nsecs in u64
8405cee9645SThomas Gleixner  *
8415cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
8425cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
8435cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
8445cee9645SThomas Gleixner  *
8455cee9645SThomas Gleixner  * note:
8465cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
8475cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
8485cee9645SThomas Gleixner  */
8495cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n)
8505cee9645SThomas Gleixner {
8515cee9645SThomas Gleixner 	return (unsigned long)nsecs_to_jiffies64(n);
8525cee9645SThomas Gleixner }
853d560fed6SThomas Gleixner EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
8545cee9645SThomas Gleixner 
8555cee9645SThomas Gleixner /*
8565cee9645SThomas Gleixner  * Add two timespec values and do a safety check for overflow.
8575cee9645SThomas Gleixner  * It's assumed that both values are valid (>= 0)
8585cee9645SThomas Gleixner  */
8595cee9645SThomas Gleixner struct timespec timespec_add_safe(const struct timespec lhs,
8605cee9645SThomas Gleixner 				  const struct timespec rhs)
8615cee9645SThomas Gleixner {
8625cee9645SThomas Gleixner 	struct timespec res;
8635cee9645SThomas Gleixner 
8645cee9645SThomas Gleixner 	set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
8655cee9645SThomas Gleixner 				lhs.tv_nsec + rhs.tv_nsec);
8665cee9645SThomas Gleixner 
8675cee9645SThomas Gleixner 	if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
8685cee9645SThomas Gleixner 		res.tv_sec = TIME_T_MAX;
8695cee9645SThomas Gleixner 
8705cee9645SThomas Gleixner 	return res;
8715cee9645SThomas Gleixner }
872bc2c53e5SDeepa Dinamani 
873bc2c53e5SDeepa Dinamani /*
874bc2c53e5SDeepa Dinamani  * Add two timespec64 values and do a safety check for overflow.
875bc2c53e5SDeepa Dinamani  * It's assumed that both values are valid (>= 0).
876bc2c53e5SDeepa Dinamani  * And, each timespec64 is in normalized form.
877bc2c53e5SDeepa Dinamani  */
878bc2c53e5SDeepa Dinamani struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
879bc2c53e5SDeepa Dinamani 				const struct timespec64 rhs)
880bc2c53e5SDeepa Dinamani {
881bc2c53e5SDeepa Dinamani 	struct timespec64 res;
882bc2c53e5SDeepa Dinamani 
883469e857fSVegard Nossum 	set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec,
884bc2c53e5SDeepa Dinamani 			lhs.tv_nsec + rhs.tv_nsec);
885bc2c53e5SDeepa Dinamani 
886bc2c53e5SDeepa Dinamani 	if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) {
887bc2c53e5SDeepa Dinamani 		res.tv_sec = TIME64_MAX;
888bc2c53e5SDeepa Dinamani 		res.tv_nsec = 0;
889bc2c53e5SDeepa Dinamani 	}
890bc2c53e5SDeepa Dinamani 
891bc2c53e5SDeepa Dinamani 	return res;
892bc2c53e5SDeepa Dinamani }
893f59dd9c8SDeepa Dinamani 
894f59dd9c8SDeepa Dinamani int get_timespec64(struct timespec64 *ts,
895f59dd9c8SDeepa Dinamani 		   const struct timespec __user *uts)
896f59dd9c8SDeepa Dinamani {
897f59dd9c8SDeepa Dinamani 	struct timespec kts;
898f59dd9c8SDeepa Dinamani 	int ret;
899f59dd9c8SDeepa Dinamani 
900f59dd9c8SDeepa Dinamani 	ret = copy_from_user(&kts, uts, sizeof(kts));
901f59dd9c8SDeepa Dinamani 	if (ret)
902f59dd9c8SDeepa Dinamani 		return -EFAULT;
903f59dd9c8SDeepa Dinamani 
904f59dd9c8SDeepa Dinamani 	ts->tv_sec = kts.tv_sec;
905f59dd9c8SDeepa Dinamani 	ts->tv_nsec = kts.tv_nsec;
906f59dd9c8SDeepa Dinamani 
907f59dd9c8SDeepa Dinamani 	return 0;
908f59dd9c8SDeepa Dinamani }
909f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(get_timespec64);
910f59dd9c8SDeepa Dinamani 
911f59dd9c8SDeepa Dinamani int put_timespec64(const struct timespec64 *ts,
912f59dd9c8SDeepa Dinamani 		   struct timespec __user *uts)
913f59dd9c8SDeepa Dinamani {
914f59dd9c8SDeepa Dinamani 	struct timespec kts = {
915f59dd9c8SDeepa Dinamani 		.tv_sec = ts->tv_sec,
916f59dd9c8SDeepa Dinamani 		.tv_nsec = ts->tv_nsec
917f59dd9c8SDeepa Dinamani 	};
918f59dd9c8SDeepa Dinamani 	return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0;
919f59dd9c8SDeepa Dinamani }
920f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(put_timespec64);
921d5b7ffbfSDeepa Dinamani 
922d5b7ffbfSDeepa Dinamani int get_itimerspec64(struct itimerspec64 *it,
923d5b7ffbfSDeepa Dinamani 			const struct itimerspec __user *uit)
924d5b7ffbfSDeepa Dinamani {
925d5b7ffbfSDeepa Dinamani 	int ret;
926d5b7ffbfSDeepa Dinamani 
927d5b7ffbfSDeepa Dinamani 	ret = get_timespec64(&it->it_interval, &uit->it_interval);
928d5b7ffbfSDeepa Dinamani 	if (ret)
929d5b7ffbfSDeepa Dinamani 		return ret;
930d5b7ffbfSDeepa Dinamani 
931d5b7ffbfSDeepa Dinamani 	ret = get_timespec64(&it->it_value, &uit->it_value);
932d5b7ffbfSDeepa Dinamani 
933d5b7ffbfSDeepa Dinamani 	return ret;
934d5b7ffbfSDeepa Dinamani }
935d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(get_itimerspec64);
936d5b7ffbfSDeepa Dinamani 
937d5b7ffbfSDeepa Dinamani int put_itimerspec64(const struct itimerspec64 *it,
938d5b7ffbfSDeepa Dinamani 			struct itimerspec __user *uit)
939d5b7ffbfSDeepa Dinamani {
940d5b7ffbfSDeepa Dinamani 	int ret;
941d5b7ffbfSDeepa Dinamani 
942d5b7ffbfSDeepa Dinamani 	ret = put_timespec64(&it->it_interval, &uit->it_interval);
943d5b7ffbfSDeepa Dinamani 	if (ret)
944d5b7ffbfSDeepa Dinamani 		return ret;
945d5b7ffbfSDeepa Dinamani 
946d5b7ffbfSDeepa Dinamani 	ret = put_timespec64(&it->it_value, &uit->it_value);
947d5b7ffbfSDeepa Dinamani 
948d5b7ffbfSDeepa Dinamani 	return ret;
949d5b7ffbfSDeepa Dinamani }
950d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(put_itimerspec64);
951