xref: /openbmc/linux/kernel/time/time.c (revision 49cd6f86)
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 
415cee9645SThomas Gleixner #include <asm/uaccess.h>
425cee9645SThomas Gleixner #include <asm/unistd.h>
435cee9645SThomas Gleixner 
445cee9645SThomas Gleixner #include "timeconst.h"
455cee9645SThomas Gleixner 
465cee9645SThomas Gleixner /*
475cee9645SThomas Gleixner  * The timezone where the local system is located.  Used as a default by some
485cee9645SThomas Gleixner  * programs who obtain this value by using gettimeofday.
495cee9645SThomas Gleixner  */
505cee9645SThomas Gleixner struct timezone sys_tz;
515cee9645SThomas Gleixner 
525cee9645SThomas Gleixner EXPORT_SYMBOL(sys_tz);
535cee9645SThomas Gleixner 
545cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_TIME
555cee9645SThomas Gleixner 
565cee9645SThomas Gleixner /*
575cee9645SThomas Gleixner  * sys_time() can be implemented in user-level using
585cee9645SThomas Gleixner  * sys_gettimeofday().  Is this for backwards compatibility?  If so,
595cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
605cee9645SThomas Gleixner  * architectures that need it).
615cee9645SThomas Gleixner  */
625cee9645SThomas Gleixner SYSCALL_DEFINE1(time, time_t __user *, tloc)
635cee9645SThomas Gleixner {
645cee9645SThomas Gleixner 	time_t i = get_seconds();
655cee9645SThomas Gleixner 
665cee9645SThomas Gleixner 	if (tloc) {
675cee9645SThomas Gleixner 		if (put_user(i,tloc))
685cee9645SThomas Gleixner 			return -EFAULT;
695cee9645SThomas Gleixner 	}
705cee9645SThomas Gleixner 	force_successful_syscall_return();
715cee9645SThomas Gleixner 	return i;
725cee9645SThomas Gleixner }
735cee9645SThomas Gleixner 
745cee9645SThomas Gleixner /*
755cee9645SThomas Gleixner  * sys_stime() can be implemented in user-level using
765cee9645SThomas Gleixner  * sys_settimeofday().  Is this for backwards compatibility?  If so,
775cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
785cee9645SThomas Gleixner  * architectures that need it).
795cee9645SThomas Gleixner  */
805cee9645SThomas Gleixner 
815cee9645SThomas Gleixner SYSCALL_DEFINE1(stime, time_t __user *, tptr)
825cee9645SThomas Gleixner {
835cee9645SThomas Gleixner 	struct timespec tv;
845cee9645SThomas Gleixner 	int err;
855cee9645SThomas Gleixner 
865cee9645SThomas Gleixner 	if (get_user(tv.tv_sec, tptr))
875cee9645SThomas Gleixner 		return -EFAULT;
885cee9645SThomas Gleixner 
895cee9645SThomas Gleixner 	tv.tv_nsec = 0;
905cee9645SThomas Gleixner 
915cee9645SThomas Gleixner 	err = security_settime(&tv, NULL);
925cee9645SThomas Gleixner 	if (err)
935cee9645SThomas Gleixner 		return err;
945cee9645SThomas Gleixner 
955cee9645SThomas Gleixner 	do_settimeofday(&tv);
965cee9645SThomas Gleixner 	return 0;
975cee9645SThomas Gleixner }
985cee9645SThomas Gleixner 
995cee9645SThomas Gleixner #endif /* __ARCH_WANT_SYS_TIME */
1005cee9645SThomas Gleixner 
1015cee9645SThomas Gleixner SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
1025cee9645SThomas Gleixner 		struct timezone __user *, tz)
1035cee9645SThomas Gleixner {
1045cee9645SThomas Gleixner 	if (likely(tv != NULL)) {
1055cee9645SThomas Gleixner 		struct timeval ktv;
1065cee9645SThomas Gleixner 		do_gettimeofday(&ktv);
1075cee9645SThomas Gleixner 		if (copy_to_user(tv, &ktv, sizeof(ktv)))
1085cee9645SThomas Gleixner 			return -EFAULT;
1095cee9645SThomas Gleixner 	}
1105cee9645SThomas Gleixner 	if (unlikely(tz != NULL)) {
1115cee9645SThomas Gleixner 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1125cee9645SThomas Gleixner 			return -EFAULT;
1135cee9645SThomas Gleixner 	}
1145cee9645SThomas Gleixner 	return 0;
1155cee9645SThomas Gleixner }
1165cee9645SThomas Gleixner 
1175cee9645SThomas Gleixner /*
1185cee9645SThomas Gleixner  * Indicates if there is an offset between the system clock and the hardware
1195cee9645SThomas Gleixner  * clock/persistent clock/rtc.
1205cee9645SThomas Gleixner  */
1215cee9645SThomas Gleixner int persistent_clock_is_local;
1225cee9645SThomas Gleixner 
1235cee9645SThomas Gleixner /*
1245cee9645SThomas Gleixner  * Adjust the time obtained from the CMOS to be UTC time instead of
1255cee9645SThomas Gleixner  * local time.
1265cee9645SThomas Gleixner  *
1275cee9645SThomas Gleixner  * This is ugly, but preferable to the alternatives.  Otherwise we
1285cee9645SThomas Gleixner  * would either need to write a program to do it in /etc/rc (and risk
1295cee9645SThomas Gleixner  * confusion if the program gets run more than once; it would also be
1305cee9645SThomas Gleixner  * hard to make the program warp the clock precisely n hours)  or
1315cee9645SThomas Gleixner  * compile in the timezone information into the kernel.  Bad, bad....
1325cee9645SThomas Gleixner  *
1335cee9645SThomas Gleixner  *						- TYT, 1992-01-01
1345cee9645SThomas Gleixner  *
1355cee9645SThomas Gleixner  * The best thing to do is to keep the CMOS clock in universal time (UTC)
1365cee9645SThomas Gleixner  * as real UNIX machines always do it. This avoids all headaches about
1375cee9645SThomas Gleixner  * daylight saving times and warping kernel clocks.
1385cee9645SThomas Gleixner  */
1395cee9645SThomas Gleixner static inline void warp_clock(void)
1405cee9645SThomas Gleixner {
1415cee9645SThomas Gleixner 	if (sys_tz.tz_minuteswest != 0) {
1425cee9645SThomas Gleixner 		struct timespec adjust;
1435cee9645SThomas Gleixner 
1445cee9645SThomas Gleixner 		persistent_clock_is_local = 1;
1455cee9645SThomas Gleixner 		adjust.tv_sec = sys_tz.tz_minuteswest * 60;
1465cee9645SThomas Gleixner 		adjust.tv_nsec = 0;
1475cee9645SThomas Gleixner 		timekeeping_inject_offset(&adjust);
1485cee9645SThomas Gleixner 	}
1495cee9645SThomas Gleixner }
1505cee9645SThomas Gleixner 
1515cee9645SThomas Gleixner /*
1525cee9645SThomas Gleixner  * In case for some reason the CMOS clock has not already been running
1535cee9645SThomas Gleixner  * in UTC, but in some local time: The first time we set the timezone,
1545cee9645SThomas Gleixner  * we will warp the clock so that it is ticking UTC time instead of
1555cee9645SThomas Gleixner  * local time. Presumably, if someone is setting the timezone then we
1565cee9645SThomas Gleixner  * are running in an environment where the programs understand about
1575cee9645SThomas Gleixner  * timezones. This should be done at boot time in the /etc/rc script,
1585cee9645SThomas Gleixner  * as soon as possible, so that the clock can be set right. Otherwise,
1595cee9645SThomas Gleixner  * various programs will get confused when the clock gets warped.
1605cee9645SThomas Gleixner  */
1615cee9645SThomas Gleixner 
1625cee9645SThomas Gleixner int do_sys_settimeofday(const struct timespec *tv, const struct timezone *tz)
1635cee9645SThomas Gleixner {
1645cee9645SThomas Gleixner 	static int firsttime = 1;
1655cee9645SThomas Gleixner 	int error = 0;
1665cee9645SThomas Gleixner 
1675cee9645SThomas Gleixner 	if (tv && !timespec_valid(tv))
1685cee9645SThomas Gleixner 		return -EINVAL;
1695cee9645SThomas Gleixner 
1705cee9645SThomas Gleixner 	error = security_settime(tv, tz);
1715cee9645SThomas Gleixner 	if (error)
1725cee9645SThomas Gleixner 		return error;
1735cee9645SThomas Gleixner 
1745cee9645SThomas Gleixner 	if (tz) {
1755cee9645SThomas Gleixner 		sys_tz = *tz;
1765cee9645SThomas Gleixner 		update_vsyscall_tz();
1775cee9645SThomas Gleixner 		if (firsttime) {
1785cee9645SThomas Gleixner 			firsttime = 0;
1795cee9645SThomas Gleixner 			if (!tv)
1805cee9645SThomas Gleixner 				warp_clock();
1815cee9645SThomas Gleixner 		}
1825cee9645SThomas Gleixner 	}
1835cee9645SThomas Gleixner 	if (tv)
1845cee9645SThomas Gleixner 		return do_settimeofday(tv);
1855cee9645SThomas Gleixner 	return 0;
1865cee9645SThomas Gleixner }
1875cee9645SThomas Gleixner 
1885cee9645SThomas Gleixner SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
1895cee9645SThomas Gleixner 		struct timezone __user *, tz)
1905cee9645SThomas Gleixner {
1915cee9645SThomas Gleixner 	struct timeval user_tv;
1925cee9645SThomas Gleixner 	struct timespec	new_ts;
1935cee9645SThomas Gleixner 	struct timezone new_tz;
1945cee9645SThomas Gleixner 
1955cee9645SThomas Gleixner 	if (tv) {
1965cee9645SThomas Gleixner 		if (copy_from_user(&user_tv, tv, sizeof(*tv)))
1975cee9645SThomas Gleixner 			return -EFAULT;
1985cee9645SThomas Gleixner 		new_ts.tv_sec = user_tv.tv_sec;
1995cee9645SThomas Gleixner 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
2005cee9645SThomas Gleixner 	}
2015cee9645SThomas Gleixner 	if (tz) {
2025cee9645SThomas Gleixner 		if (copy_from_user(&new_tz, tz, sizeof(*tz)))
2035cee9645SThomas Gleixner 			return -EFAULT;
2045cee9645SThomas Gleixner 	}
2055cee9645SThomas Gleixner 
2065cee9645SThomas Gleixner 	return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
2075cee9645SThomas Gleixner }
2085cee9645SThomas Gleixner 
2095cee9645SThomas Gleixner SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
2105cee9645SThomas Gleixner {
2115cee9645SThomas Gleixner 	struct timex txc;		/* Local copy of parameter */
2125cee9645SThomas Gleixner 	int ret;
2135cee9645SThomas Gleixner 
2145cee9645SThomas Gleixner 	/* Copy the user data space into the kernel copy
2155cee9645SThomas Gleixner 	 * structure. But bear in mind that the structures
2165cee9645SThomas Gleixner 	 * may change
2175cee9645SThomas Gleixner 	 */
2185cee9645SThomas Gleixner 	if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
2195cee9645SThomas Gleixner 		return -EFAULT;
2205cee9645SThomas Gleixner 	ret = do_adjtimex(&txc);
2215cee9645SThomas Gleixner 	return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
2225cee9645SThomas Gleixner }
2235cee9645SThomas Gleixner 
2245cee9645SThomas Gleixner /**
2255cee9645SThomas Gleixner  * current_fs_time - Return FS time
2265cee9645SThomas Gleixner  * @sb: Superblock.
2275cee9645SThomas Gleixner  *
2285cee9645SThomas Gleixner  * Return the current time truncated to the time granularity supported by
2295cee9645SThomas Gleixner  * the fs.
2305cee9645SThomas Gleixner  */
2315cee9645SThomas Gleixner struct timespec current_fs_time(struct super_block *sb)
2325cee9645SThomas Gleixner {
2335cee9645SThomas Gleixner 	struct timespec now = current_kernel_time();
2345cee9645SThomas Gleixner 	return timespec_trunc(now, sb->s_time_gran);
2355cee9645SThomas Gleixner }
2365cee9645SThomas Gleixner EXPORT_SYMBOL(current_fs_time);
2375cee9645SThomas Gleixner 
2385cee9645SThomas Gleixner /*
2395cee9645SThomas Gleixner  * Convert jiffies to milliseconds and back.
2405cee9645SThomas Gleixner  *
2415cee9645SThomas Gleixner  * Avoid unnecessary multiplications/divisions in the
2425cee9645SThomas Gleixner  * two most common HZ cases:
2435cee9645SThomas Gleixner  */
2445cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j)
2455cee9645SThomas Gleixner {
2465cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
2475cee9645SThomas Gleixner 	return (MSEC_PER_SEC / HZ) * j;
2485cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
2495cee9645SThomas Gleixner 	return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
2505cee9645SThomas Gleixner #else
2515cee9645SThomas Gleixner # if BITS_PER_LONG == 32
2525cee9645SThomas Gleixner 	return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
2535cee9645SThomas Gleixner # else
2545cee9645SThomas Gleixner 	return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
2555cee9645SThomas Gleixner # endif
2565cee9645SThomas Gleixner #endif
2575cee9645SThomas Gleixner }
2585cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs);
2595cee9645SThomas Gleixner 
2605cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j)
2615cee9645SThomas Gleixner {
2625cee9645SThomas Gleixner #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
2635cee9645SThomas Gleixner 	return (USEC_PER_SEC / HZ) * j;
2645cee9645SThomas Gleixner #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
2655cee9645SThomas Gleixner 	return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
2665cee9645SThomas Gleixner #else
2675cee9645SThomas Gleixner # if BITS_PER_LONG == 32
2685cee9645SThomas Gleixner 	return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
2695cee9645SThomas Gleixner # else
2705cee9645SThomas Gleixner 	return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
2715cee9645SThomas Gleixner # endif
2725cee9645SThomas Gleixner #endif
2735cee9645SThomas Gleixner }
2745cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs);
2755cee9645SThomas Gleixner 
2765cee9645SThomas Gleixner /**
2775cee9645SThomas Gleixner  * timespec_trunc - Truncate timespec to a granularity
2785cee9645SThomas Gleixner  * @t: Timespec
2795cee9645SThomas Gleixner  * @gran: Granularity in ns.
2805cee9645SThomas Gleixner  *
2815cee9645SThomas Gleixner  * Truncate a timespec to a granularity. gran must be smaller than a second.
2825cee9645SThomas Gleixner  * Always rounds down.
2835cee9645SThomas Gleixner  *
2845cee9645SThomas Gleixner  * This function should be only used for timestamps returned by
2855cee9645SThomas Gleixner  * current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because
2865cee9645SThomas Gleixner  * it doesn't handle the better resolution of the latter.
2875cee9645SThomas Gleixner  */
2885cee9645SThomas Gleixner struct timespec timespec_trunc(struct timespec t, unsigned gran)
2895cee9645SThomas Gleixner {
2905cee9645SThomas Gleixner 	/*
2915cee9645SThomas Gleixner 	 * Division is pretty slow so avoid it for common cases.
2925cee9645SThomas Gleixner 	 * Currently current_kernel_time() never returns better than
2935cee9645SThomas Gleixner 	 * jiffies resolution. Exploit that.
2945cee9645SThomas Gleixner 	 */
2955cee9645SThomas Gleixner 	if (gran <= jiffies_to_usecs(1) * 1000) {
2965cee9645SThomas Gleixner 		/* nothing */
2975cee9645SThomas Gleixner 	} else if (gran == 1000000000) {
2985cee9645SThomas Gleixner 		t.tv_nsec = 0;
2995cee9645SThomas Gleixner 	} else {
3005cee9645SThomas Gleixner 		t.tv_nsec -= t.tv_nsec % gran;
3015cee9645SThomas Gleixner 	}
3025cee9645SThomas Gleixner 	return t;
3035cee9645SThomas Gleixner }
3045cee9645SThomas Gleixner EXPORT_SYMBOL(timespec_trunc);
3055cee9645SThomas Gleixner 
3065cee9645SThomas Gleixner /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
3075cee9645SThomas Gleixner  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
3085cee9645SThomas Gleixner  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
3095cee9645SThomas Gleixner  *
3105cee9645SThomas Gleixner  * [For the Julian calendar (which was used in Russia before 1917,
3115cee9645SThomas Gleixner  * Britain & colonies before 1752, anywhere else before 1582,
3125cee9645SThomas Gleixner  * and is still in use by some communities) leave out the
3135cee9645SThomas Gleixner  * -year/100+year/400 terms, and add 10.]
3145cee9645SThomas Gleixner  *
3155cee9645SThomas Gleixner  * This algorithm was first published by Gauss (I think).
3165cee9645SThomas Gleixner  *
3175cee9645SThomas Gleixner  * WARNING: this function will overflow on 2106-02-07 06:28:16 on
3185cee9645SThomas Gleixner  * machines where long is 32-bit! (However, as time_t is signed, we
3195cee9645SThomas Gleixner  * will already get problems at other places on 2038-01-19 03:14:08)
3205cee9645SThomas Gleixner  */
3215cee9645SThomas Gleixner unsigned long
3225cee9645SThomas Gleixner mktime(const unsigned int year0, const unsigned int mon0,
3235cee9645SThomas Gleixner        const unsigned int day, const unsigned int hour,
3245cee9645SThomas Gleixner        const unsigned int min, const unsigned int sec)
3255cee9645SThomas Gleixner {
3265cee9645SThomas Gleixner 	unsigned int mon = mon0, year = year0;
3275cee9645SThomas Gleixner 
3285cee9645SThomas Gleixner 	/* 1..12 -> 11,12,1..10 */
3295cee9645SThomas Gleixner 	if (0 >= (int) (mon -= 2)) {
3305cee9645SThomas Gleixner 		mon += 12;	/* Puts Feb last since it has leap day */
3315cee9645SThomas Gleixner 		year -= 1;
3325cee9645SThomas Gleixner 	}
3335cee9645SThomas Gleixner 
3345cee9645SThomas Gleixner 	return ((((unsigned long)
3355cee9645SThomas Gleixner 		  (year/4 - year/100 + year/400 + 367*mon/12 + day) +
3365cee9645SThomas Gleixner 		  year*365 - 719499
3375cee9645SThomas Gleixner 	    )*24 + hour /* now have hours */
3385cee9645SThomas Gleixner 	  )*60 + min /* now have minutes */
3395cee9645SThomas Gleixner 	)*60 + sec; /* finally seconds */
3405cee9645SThomas Gleixner }
3415cee9645SThomas Gleixner 
3425cee9645SThomas Gleixner EXPORT_SYMBOL(mktime);
3435cee9645SThomas Gleixner 
3445cee9645SThomas Gleixner /**
3455cee9645SThomas Gleixner  * set_normalized_timespec - set timespec sec and nsec parts and normalize
3465cee9645SThomas Gleixner  *
3475cee9645SThomas Gleixner  * @ts:		pointer to timespec variable to be set
3485cee9645SThomas Gleixner  * @sec:	seconds to set
3495cee9645SThomas Gleixner  * @nsec:	nanoseconds to set
3505cee9645SThomas Gleixner  *
3515cee9645SThomas Gleixner  * Set seconds and nanoseconds field of a timespec variable and
3525cee9645SThomas Gleixner  * normalize to the timespec storage format
3535cee9645SThomas Gleixner  *
3545cee9645SThomas Gleixner  * Note: The tv_nsec part is always in the range of
3555cee9645SThomas Gleixner  *	0 <= tv_nsec < NSEC_PER_SEC
3565cee9645SThomas Gleixner  * For negative values only the tv_sec field is negative !
3575cee9645SThomas Gleixner  */
3585cee9645SThomas Gleixner void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
3595cee9645SThomas Gleixner {
3605cee9645SThomas Gleixner 	while (nsec >= NSEC_PER_SEC) {
3615cee9645SThomas Gleixner 		/*
3625cee9645SThomas Gleixner 		 * The following asm() prevents the compiler from
3635cee9645SThomas Gleixner 		 * optimising this loop into a modulo operation. See
3645cee9645SThomas Gleixner 		 * also __iter_div_u64_rem() in include/linux/time.h
3655cee9645SThomas Gleixner 		 */
3665cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
3675cee9645SThomas Gleixner 		nsec -= NSEC_PER_SEC;
3685cee9645SThomas Gleixner 		++sec;
3695cee9645SThomas Gleixner 	}
3705cee9645SThomas Gleixner 	while (nsec < 0) {
3715cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
3725cee9645SThomas Gleixner 		nsec += NSEC_PER_SEC;
3735cee9645SThomas Gleixner 		--sec;
3745cee9645SThomas Gleixner 	}
3755cee9645SThomas Gleixner 	ts->tv_sec = sec;
3765cee9645SThomas Gleixner 	ts->tv_nsec = nsec;
3775cee9645SThomas Gleixner }
3785cee9645SThomas Gleixner EXPORT_SYMBOL(set_normalized_timespec);
3795cee9645SThomas Gleixner 
3805cee9645SThomas Gleixner /**
3815cee9645SThomas Gleixner  * ns_to_timespec - Convert nanoseconds to timespec
3825cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
3835cee9645SThomas Gleixner  *
3845cee9645SThomas Gleixner  * Returns the timespec representation of the nsec parameter.
3855cee9645SThomas Gleixner  */
3865cee9645SThomas Gleixner struct timespec ns_to_timespec(const s64 nsec)
3875cee9645SThomas Gleixner {
3885cee9645SThomas Gleixner 	struct timespec ts;
3895cee9645SThomas Gleixner 	s32 rem;
3905cee9645SThomas Gleixner 
3915cee9645SThomas Gleixner 	if (!nsec)
3925cee9645SThomas Gleixner 		return (struct timespec) {0, 0};
3935cee9645SThomas Gleixner 
3945cee9645SThomas Gleixner 	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
3955cee9645SThomas Gleixner 	if (unlikely(rem < 0)) {
3965cee9645SThomas Gleixner 		ts.tv_sec--;
3975cee9645SThomas Gleixner 		rem += NSEC_PER_SEC;
3985cee9645SThomas Gleixner 	}
3995cee9645SThomas Gleixner 	ts.tv_nsec = rem;
4005cee9645SThomas Gleixner 
4015cee9645SThomas Gleixner 	return ts;
4025cee9645SThomas Gleixner }
4035cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timespec);
4045cee9645SThomas Gleixner 
4055cee9645SThomas Gleixner /**
4065cee9645SThomas Gleixner  * ns_to_timeval - Convert nanoseconds to timeval
4075cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
4085cee9645SThomas Gleixner  *
4095cee9645SThomas Gleixner  * Returns the timeval representation of the nsec parameter.
4105cee9645SThomas Gleixner  */
4115cee9645SThomas Gleixner struct timeval ns_to_timeval(const s64 nsec)
4125cee9645SThomas Gleixner {
4135cee9645SThomas Gleixner 	struct timespec ts = ns_to_timespec(nsec);
4145cee9645SThomas Gleixner 	struct timeval tv;
4155cee9645SThomas Gleixner 
4165cee9645SThomas Gleixner 	tv.tv_sec = ts.tv_sec;
4175cee9645SThomas Gleixner 	tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
4185cee9645SThomas Gleixner 
4195cee9645SThomas Gleixner 	return tv;
4205cee9645SThomas Gleixner }
4215cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timeval);
4225cee9645SThomas Gleixner 
42349cd6f86SJohn Stultz #if BITS_PER_LONG == 32
42449cd6f86SJohn Stultz /**
42549cd6f86SJohn Stultz  * set_normalized_timespec - set timespec sec and nsec parts and normalize
42649cd6f86SJohn Stultz  *
42749cd6f86SJohn Stultz  * @ts:		pointer to timespec variable to be set
42849cd6f86SJohn Stultz  * @sec:	seconds to set
42949cd6f86SJohn Stultz  * @nsec:	nanoseconds to set
43049cd6f86SJohn Stultz  *
43149cd6f86SJohn Stultz  * Set seconds and nanoseconds field of a timespec variable and
43249cd6f86SJohn Stultz  * normalize to the timespec storage format
43349cd6f86SJohn Stultz  *
43449cd6f86SJohn Stultz  * Note: The tv_nsec part is always in the range of
43549cd6f86SJohn Stultz  *	0 <= tv_nsec < NSEC_PER_SEC
43649cd6f86SJohn Stultz  * For negative values only the tv_sec field is negative !
43749cd6f86SJohn Stultz  */
43849cd6f86SJohn Stultz void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
43949cd6f86SJohn Stultz {
44049cd6f86SJohn Stultz 	while (nsec >= NSEC_PER_SEC) {
44149cd6f86SJohn Stultz 		/*
44249cd6f86SJohn Stultz 		 * The following asm() prevents the compiler from
44349cd6f86SJohn Stultz 		 * optimising this loop into a modulo operation. See
44449cd6f86SJohn Stultz 		 * also __iter_div_u64_rem() in include/linux/time.h
44549cd6f86SJohn Stultz 		 */
44649cd6f86SJohn Stultz 		asm("" : "+rm"(nsec));
44749cd6f86SJohn Stultz 		nsec -= NSEC_PER_SEC;
44849cd6f86SJohn Stultz 		++sec;
44949cd6f86SJohn Stultz 	}
45049cd6f86SJohn Stultz 	while (nsec < 0) {
45149cd6f86SJohn Stultz 		asm("" : "+rm"(nsec));
45249cd6f86SJohn Stultz 		nsec += NSEC_PER_SEC;
45349cd6f86SJohn Stultz 		--sec;
45449cd6f86SJohn Stultz 	}
45549cd6f86SJohn Stultz 	ts->tv_sec = sec;
45649cd6f86SJohn Stultz 	ts->tv_nsec = nsec;
45749cd6f86SJohn Stultz }
45849cd6f86SJohn Stultz EXPORT_SYMBOL(set_normalized_timespec64);
45949cd6f86SJohn Stultz 
46049cd6f86SJohn Stultz /**
46149cd6f86SJohn Stultz  * ns_to_timespec64 - Convert nanoseconds to timespec64
46249cd6f86SJohn Stultz  * @nsec:       the nanoseconds value to be converted
46349cd6f86SJohn Stultz  *
46449cd6f86SJohn Stultz  * Returns the timespec64 representation of the nsec parameter.
46549cd6f86SJohn Stultz  */
46649cd6f86SJohn Stultz struct timespec64 ns_to_timespec64(const s64 nsec)
46749cd6f86SJohn Stultz {
46849cd6f86SJohn Stultz 	struct timespec64 ts;
46949cd6f86SJohn Stultz 	s32 rem;
47049cd6f86SJohn Stultz 
47149cd6f86SJohn Stultz 	if (!nsec)
47249cd6f86SJohn Stultz 		return (struct timespec64) {0, 0};
47349cd6f86SJohn Stultz 
47449cd6f86SJohn Stultz 	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
47549cd6f86SJohn Stultz 	if (unlikely(rem < 0)) {
47649cd6f86SJohn Stultz 		ts.tv_sec--;
47749cd6f86SJohn Stultz 		rem += NSEC_PER_SEC;
47849cd6f86SJohn Stultz 	}
47949cd6f86SJohn Stultz 	ts.tv_nsec = rem;
48049cd6f86SJohn Stultz 
48149cd6f86SJohn Stultz 	return ts;
48249cd6f86SJohn Stultz }
48349cd6f86SJohn Stultz EXPORT_SYMBOL(ns_to_timespec64);
48449cd6f86SJohn Stultz #endif
4855cee9645SThomas Gleixner /*
4865cee9645SThomas Gleixner  * When we convert to jiffies then we interpret incoming values
4875cee9645SThomas Gleixner  * the following way:
4885cee9645SThomas Gleixner  *
4895cee9645SThomas Gleixner  * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
4905cee9645SThomas Gleixner  *
4915cee9645SThomas Gleixner  * - 'too large' values [that would result in larger than
4925cee9645SThomas Gleixner  *   MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
4935cee9645SThomas Gleixner  *
4945cee9645SThomas Gleixner  * - all other values are converted to jiffies by either multiplying
4955cee9645SThomas Gleixner  *   the input value by a factor or dividing it with a factor
4965cee9645SThomas Gleixner  *
4975cee9645SThomas Gleixner  * We must also be careful about 32-bit overflows.
4985cee9645SThomas Gleixner  */
4995cee9645SThomas Gleixner unsigned long msecs_to_jiffies(const unsigned int m)
5005cee9645SThomas Gleixner {
5015cee9645SThomas Gleixner 	/*
5025cee9645SThomas Gleixner 	 * Negative value, means infinite timeout:
5035cee9645SThomas Gleixner 	 */
5045cee9645SThomas Gleixner 	if ((int)m < 0)
5055cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
5065cee9645SThomas Gleixner 
5075cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
5085cee9645SThomas Gleixner 	/*
5095cee9645SThomas Gleixner 	 * HZ is equal to or smaller than 1000, and 1000 is a nice
5105cee9645SThomas Gleixner 	 * round multiple of HZ, divide with the factor between them,
5115cee9645SThomas Gleixner 	 * but round upwards:
5125cee9645SThomas Gleixner 	 */
5135cee9645SThomas Gleixner 	return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
5145cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
5155cee9645SThomas Gleixner 	/*
5165cee9645SThomas Gleixner 	 * HZ is larger than 1000, and HZ is a nice round multiple of
5175cee9645SThomas Gleixner 	 * 1000 - simply multiply with the factor between them.
5185cee9645SThomas Gleixner 	 *
5195cee9645SThomas Gleixner 	 * But first make sure the multiplication result cannot
5205cee9645SThomas Gleixner 	 * overflow:
5215cee9645SThomas Gleixner 	 */
5225cee9645SThomas Gleixner 	if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
5235cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
5245cee9645SThomas Gleixner 
5255cee9645SThomas Gleixner 	return m * (HZ / MSEC_PER_SEC);
5265cee9645SThomas Gleixner #else
5275cee9645SThomas Gleixner 	/*
5285cee9645SThomas Gleixner 	 * Generic case - multiply, round and divide. But first
5295cee9645SThomas Gleixner 	 * check that if we are doing a net multiplication, that
5305cee9645SThomas Gleixner 	 * we wouldn't overflow:
5315cee9645SThomas Gleixner 	 */
5325cee9645SThomas Gleixner 	if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
5335cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
5345cee9645SThomas Gleixner 
5355cee9645SThomas Gleixner 	return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
5365cee9645SThomas Gleixner 		>> MSEC_TO_HZ_SHR32;
5375cee9645SThomas Gleixner #endif
5385cee9645SThomas Gleixner }
5395cee9645SThomas Gleixner EXPORT_SYMBOL(msecs_to_jiffies);
5405cee9645SThomas Gleixner 
5415cee9645SThomas Gleixner unsigned long usecs_to_jiffies(const unsigned int u)
5425cee9645SThomas Gleixner {
5435cee9645SThomas Gleixner 	if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
5445cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
5455cee9645SThomas Gleixner #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
5465cee9645SThomas Gleixner 	return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
5475cee9645SThomas Gleixner #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
5485cee9645SThomas Gleixner 	return u * (HZ / USEC_PER_SEC);
5495cee9645SThomas Gleixner #else
5505cee9645SThomas Gleixner 	return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32)
5515cee9645SThomas Gleixner 		>> USEC_TO_HZ_SHR32;
5525cee9645SThomas Gleixner #endif
5535cee9645SThomas Gleixner }
5545cee9645SThomas Gleixner EXPORT_SYMBOL(usecs_to_jiffies);
5555cee9645SThomas Gleixner 
5565cee9645SThomas Gleixner /*
5575cee9645SThomas Gleixner  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
5585cee9645SThomas Gleixner  * that a remainder subtract here would not do the right thing as the
5595cee9645SThomas Gleixner  * resolution values don't fall on second boundries.  I.e. the line:
5605cee9645SThomas Gleixner  * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
5615cee9645SThomas Gleixner  *
5625cee9645SThomas Gleixner  * Rather, we just shift the bits off the right.
5635cee9645SThomas Gleixner  *
5645cee9645SThomas Gleixner  * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
5655cee9645SThomas Gleixner  * value to a scaled second value.
5665cee9645SThomas Gleixner  */
5675cee9645SThomas Gleixner unsigned long
5685cee9645SThomas Gleixner timespec_to_jiffies(const struct timespec *value)
5695cee9645SThomas Gleixner {
5705cee9645SThomas Gleixner 	unsigned long sec = value->tv_sec;
5715cee9645SThomas Gleixner 	long nsec = value->tv_nsec + TICK_NSEC - 1;
5725cee9645SThomas Gleixner 
5735cee9645SThomas Gleixner 	if (sec >= MAX_SEC_IN_JIFFIES){
5745cee9645SThomas Gleixner 		sec = MAX_SEC_IN_JIFFIES;
5755cee9645SThomas Gleixner 		nsec = 0;
5765cee9645SThomas Gleixner 	}
5775cee9645SThomas Gleixner 	return (((u64)sec * SEC_CONVERSION) +
5785cee9645SThomas Gleixner 		(((u64)nsec * NSEC_CONVERSION) >>
5795cee9645SThomas Gleixner 		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
5805cee9645SThomas Gleixner 
5815cee9645SThomas Gleixner }
5825cee9645SThomas Gleixner EXPORT_SYMBOL(timespec_to_jiffies);
5835cee9645SThomas Gleixner 
5845cee9645SThomas Gleixner void
5855cee9645SThomas Gleixner jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
5865cee9645SThomas Gleixner {
5875cee9645SThomas Gleixner 	/*
5885cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
5895cee9645SThomas Gleixner 	 * one divide.
5905cee9645SThomas Gleixner 	 */
5915cee9645SThomas Gleixner 	u32 rem;
5925cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
5935cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
5945cee9645SThomas Gleixner 	value->tv_nsec = rem;
5955cee9645SThomas Gleixner }
5965cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timespec);
5975cee9645SThomas Gleixner 
5985cee9645SThomas Gleixner /* Same for "timeval"
5995cee9645SThomas Gleixner  *
6005cee9645SThomas Gleixner  * Well, almost.  The problem here is that the real system resolution is
6015cee9645SThomas Gleixner  * in nanoseconds and the value being converted is in micro seconds.
6025cee9645SThomas Gleixner  * Also for some machines (those that use HZ = 1024, in-particular),
6035cee9645SThomas Gleixner  * there is a LARGE error in the tick size in microseconds.
6045cee9645SThomas Gleixner 
6055cee9645SThomas Gleixner  * The solution we use is to do the rounding AFTER we convert the
6065cee9645SThomas Gleixner  * microsecond part.  Thus the USEC_ROUND, the bits to be shifted off.
6075cee9645SThomas Gleixner  * Instruction wise, this should cost only an additional add with carry
6085cee9645SThomas Gleixner  * instruction above the way it was done above.
6095cee9645SThomas Gleixner  */
6105cee9645SThomas Gleixner unsigned long
6115cee9645SThomas Gleixner timeval_to_jiffies(const struct timeval *value)
6125cee9645SThomas Gleixner {
6135cee9645SThomas Gleixner 	unsigned long sec = value->tv_sec;
6145cee9645SThomas Gleixner 	long usec = value->tv_usec;
6155cee9645SThomas Gleixner 
6165cee9645SThomas Gleixner 	if (sec >= MAX_SEC_IN_JIFFIES){
6175cee9645SThomas Gleixner 		sec = MAX_SEC_IN_JIFFIES;
6185cee9645SThomas Gleixner 		usec = 0;
6195cee9645SThomas Gleixner 	}
6205cee9645SThomas Gleixner 	return (((u64)sec * SEC_CONVERSION) +
6215cee9645SThomas Gleixner 		(((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
6225cee9645SThomas Gleixner 		 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
6235cee9645SThomas Gleixner }
6245cee9645SThomas Gleixner EXPORT_SYMBOL(timeval_to_jiffies);
6255cee9645SThomas Gleixner 
6265cee9645SThomas Gleixner void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
6275cee9645SThomas Gleixner {
6285cee9645SThomas Gleixner 	/*
6295cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
6305cee9645SThomas Gleixner 	 * one divide.
6315cee9645SThomas Gleixner 	 */
6325cee9645SThomas Gleixner 	u32 rem;
6335cee9645SThomas Gleixner 
6345cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
6355cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
6365cee9645SThomas Gleixner 	value->tv_usec = rem / NSEC_PER_USEC;
6375cee9645SThomas Gleixner }
6385cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timeval);
6395cee9645SThomas Gleixner 
6405cee9645SThomas Gleixner /*
6415cee9645SThomas Gleixner  * Convert jiffies/jiffies_64 to clock_t and back.
6425cee9645SThomas Gleixner  */
6435cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x)
6445cee9645SThomas Gleixner {
6455cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
6465cee9645SThomas Gleixner # if HZ < USER_HZ
6475cee9645SThomas Gleixner 	return x * (USER_HZ / HZ);
6485cee9645SThomas Gleixner # else
6495cee9645SThomas Gleixner 	return x / (HZ / USER_HZ);
6505cee9645SThomas Gleixner # endif
6515cee9645SThomas Gleixner #else
6525cee9645SThomas Gleixner 	return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
6535cee9645SThomas Gleixner #endif
6545cee9645SThomas Gleixner }
6555cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t);
6565cee9645SThomas Gleixner 
6575cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x)
6585cee9645SThomas Gleixner {
6595cee9645SThomas Gleixner #if (HZ % USER_HZ)==0
6605cee9645SThomas Gleixner 	if (x >= ~0UL / (HZ / USER_HZ))
6615cee9645SThomas Gleixner 		return ~0UL;
6625cee9645SThomas Gleixner 	return x * (HZ / USER_HZ);
6635cee9645SThomas Gleixner #else
6645cee9645SThomas Gleixner 	/* Don't worry about loss of precision here .. */
6655cee9645SThomas Gleixner 	if (x >= ~0UL / HZ * USER_HZ)
6665cee9645SThomas Gleixner 		return ~0UL;
6675cee9645SThomas Gleixner 
6685cee9645SThomas Gleixner 	/* .. but do try to contain it here */
6695cee9645SThomas Gleixner 	return div_u64((u64)x * HZ, USER_HZ);
6705cee9645SThomas Gleixner #endif
6715cee9645SThomas Gleixner }
6725cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies);
6735cee9645SThomas Gleixner 
6745cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x)
6755cee9645SThomas Gleixner {
6765cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
6775cee9645SThomas Gleixner # if HZ < USER_HZ
6785cee9645SThomas Gleixner 	x = div_u64(x * USER_HZ, HZ);
6795cee9645SThomas Gleixner # elif HZ > USER_HZ
6805cee9645SThomas Gleixner 	x = div_u64(x, HZ / USER_HZ);
6815cee9645SThomas Gleixner # else
6825cee9645SThomas Gleixner 	/* Nothing to do */
6835cee9645SThomas Gleixner # endif
6845cee9645SThomas Gleixner #else
6855cee9645SThomas Gleixner 	/*
6865cee9645SThomas Gleixner 	 * There are better ways that don't overflow early,
6875cee9645SThomas Gleixner 	 * but even this doesn't overflow in hundreds of years
6885cee9645SThomas Gleixner 	 * in 64 bits, so..
6895cee9645SThomas Gleixner 	 */
6905cee9645SThomas Gleixner 	x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
6915cee9645SThomas Gleixner #endif
6925cee9645SThomas Gleixner 	return x;
6935cee9645SThomas Gleixner }
6945cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t);
6955cee9645SThomas Gleixner 
6965cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x)
6975cee9645SThomas Gleixner {
6985cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0
6995cee9645SThomas Gleixner 	return div_u64(x, NSEC_PER_SEC / USER_HZ);
7005cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0
7015cee9645SThomas Gleixner 	return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
7025cee9645SThomas Gleixner #else
7035cee9645SThomas Gleixner 	/*
7045cee9645SThomas Gleixner          * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
7055cee9645SThomas Gleixner          * overflow after 64.99 years.
7065cee9645SThomas Gleixner          * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
7075cee9645SThomas Gleixner          */
7085cee9645SThomas Gleixner 	return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
7095cee9645SThomas Gleixner #endif
7105cee9645SThomas Gleixner }
7115cee9645SThomas Gleixner 
7125cee9645SThomas Gleixner /**
7135cee9645SThomas Gleixner  * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
7145cee9645SThomas Gleixner  *
7155cee9645SThomas Gleixner  * @n:	nsecs in u64
7165cee9645SThomas Gleixner  *
7175cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
7185cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
7195cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
7205cee9645SThomas Gleixner  *
7215cee9645SThomas Gleixner  * note:
7225cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
7235cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
7245cee9645SThomas Gleixner  */
7255cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n)
7265cee9645SThomas Gleixner {
7275cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0
7285cee9645SThomas Gleixner 	/* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
7295cee9645SThomas Gleixner 	return div_u64(n, NSEC_PER_SEC / HZ);
7305cee9645SThomas Gleixner #elif (HZ % 512) == 0
7315cee9645SThomas Gleixner 	/* overflow after 292 years if HZ = 1024 */
7325cee9645SThomas Gleixner 	return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
7335cee9645SThomas Gleixner #else
7345cee9645SThomas Gleixner 	/*
7355cee9645SThomas Gleixner 	 * Generic case - optimized for cases where HZ is a multiple of 3.
7365cee9645SThomas Gleixner 	 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
7375cee9645SThomas Gleixner 	 */
7385cee9645SThomas Gleixner 	return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
7395cee9645SThomas Gleixner #endif
7405cee9645SThomas Gleixner }
7415cee9645SThomas Gleixner 
7425cee9645SThomas Gleixner /**
7435cee9645SThomas Gleixner  * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
7445cee9645SThomas Gleixner  *
7455cee9645SThomas Gleixner  * @n:	nsecs in u64
7465cee9645SThomas Gleixner  *
7475cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
7485cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
7495cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
7505cee9645SThomas Gleixner  *
7515cee9645SThomas Gleixner  * note:
7525cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
7535cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
7545cee9645SThomas Gleixner  */
7555cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n)
7565cee9645SThomas Gleixner {
7575cee9645SThomas Gleixner 	return (unsigned long)nsecs_to_jiffies64(n);
7585cee9645SThomas Gleixner }
7595cee9645SThomas Gleixner 
7605cee9645SThomas Gleixner /*
7615cee9645SThomas Gleixner  * Add two timespec values and do a safety check for overflow.
7625cee9645SThomas Gleixner  * It's assumed that both values are valid (>= 0)
7635cee9645SThomas Gleixner  */
7645cee9645SThomas Gleixner struct timespec timespec_add_safe(const struct timespec lhs,
7655cee9645SThomas Gleixner 				  const struct timespec rhs)
7665cee9645SThomas Gleixner {
7675cee9645SThomas Gleixner 	struct timespec res;
7685cee9645SThomas Gleixner 
7695cee9645SThomas Gleixner 	set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
7705cee9645SThomas Gleixner 				lhs.tv_nsec + rhs.tv_nsec);
7715cee9645SThomas Gleixner 
7725cee9645SThomas Gleixner 	if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
7735cee9645SThomas Gleixner 		res.tv_sec = TIME_T_MAX;
7745cee9645SThomas Gleixner 
7755cee9645SThomas Gleixner 	return res;
7765cee9645SThomas Gleixner }
777