xref: /openbmc/linux/kernel/time/time.c (revision 5cee964597260237dd2cabb3ec22bba0da24b25d)
1*5cee9645SThomas Gleixner /*
2*5cee9645SThomas Gleixner  *  linux/kernel/time.c
3*5cee9645SThomas Gleixner  *
4*5cee9645SThomas Gleixner  *  Copyright (C) 1991, 1992  Linus Torvalds
5*5cee9645SThomas Gleixner  *
6*5cee9645SThomas Gleixner  *  This file contains the interface functions for the various
7*5cee9645SThomas Gleixner  *  time related system calls: time, stime, gettimeofday, settimeofday,
8*5cee9645SThomas Gleixner  *			       adjtime
9*5cee9645SThomas Gleixner  */
10*5cee9645SThomas Gleixner /*
11*5cee9645SThomas Gleixner  * Modification history kernel/time.c
12*5cee9645SThomas Gleixner  *
13*5cee9645SThomas Gleixner  * 1993-09-02    Philip Gladstone
14*5cee9645SThomas Gleixner  *      Created file with time related functions from sched/core.c and adjtimex()
15*5cee9645SThomas Gleixner  * 1993-10-08    Torsten Duwe
16*5cee9645SThomas Gleixner  *      adjtime interface update and CMOS clock write code
17*5cee9645SThomas Gleixner  * 1995-08-13    Torsten Duwe
18*5cee9645SThomas Gleixner  *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
19*5cee9645SThomas Gleixner  * 1999-01-16    Ulrich Windl
20*5cee9645SThomas Gleixner  *	Introduced error checking for many cases in adjtimex().
21*5cee9645SThomas Gleixner  *	Updated NTP code according to technical memorandum Jan '96
22*5cee9645SThomas Gleixner  *	"A Kernel Model for Precision Timekeeping" by Dave Mills
23*5cee9645SThomas Gleixner  *	Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
24*5cee9645SThomas Gleixner  *	(Even though the technical memorandum forbids it)
25*5cee9645SThomas Gleixner  * 2004-07-14	 Christoph Lameter
26*5cee9645SThomas Gleixner  *	Added getnstimeofday to allow the posix timer functions to return
27*5cee9645SThomas Gleixner  *	with nanosecond accuracy
28*5cee9645SThomas Gleixner  */
29*5cee9645SThomas Gleixner 
30*5cee9645SThomas Gleixner #include <linux/export.h>
31*5cee9645SThomas Gleixner #include <linux/timex.h>
32*5cee9645SThomas Gleixner #include <linux/capability.h>
33*5cee9645SThomas Gleixner #include <linux/timekeeper_internal.h>
34*5cee9645SThomas Gleixner #include <linux/errno.h>
35*5cee9645SThomas Gleixner #include <linux/syscalls.h>
36*5cee9645SThomas Gleixner #include <linux/security.h>
37*5cee9645SThomas Gleixner #include <linux/fs.h>
38*5cee9645SThomas Gleixner #include <linux/math64.h>
39*5cee9645SThomas Gleixner #include <linux/ptrace.h>
40*5cee9645SThomas Gleixner 
41*5cee9645SThomas Gleixner #include <asm/uaccess.h>
42*5cee9645SThomas Gleixner #include <asm/unistd.h>
43*5cee9645SThomas Gleixner 
44*5cee9645SThomas Gleixner #include "timeconst.h"
45*5cee9645SThomas Gleixner 
46*5cee9645SThomas Gleixner /*
47*5cee9645SThomas Gleixner  * The timezone where the local system is located.  Used as a default by some
48*5cee9645SThomas Gleixner  * programs who obtain this value by using gettimeofday.
49*5cee9645SThomas Gleixner  */
50*5cee9645SThomas Gleixner struct timezone sys_tz;
51*5cee9645SThomas Gleixner 
52*5cee9645SThomas Gleixner EXPORT_SYMBOL(sys_tz);
53*5cee9645SThomas Gleixner 
54*5cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_TIME
55*5cee9645SThomas Gleixner 
56*5cee9645SThomas Gleixner /*
57*5cee9645SThomas Gleixner  * sys_time() can be implemented in user-level using
58*5cee9645SThomas Gleixner  * sys_gettimeofday().  Is this for backwards compatibility?  If so,
59*5cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
60*5cee9645SThomas Gleixner  * architectures that need it).
61*5cee9645SThomas Gleixner  */
62*5cee9645SThomas Gleixner SYSCALL_DEFINE1(time, time_t __user *, tloc)
63*5cee9645SThomas Gleixner {
64*5cee9645SThomas Gleixner 	time_t i = get_seconds();
65*5cee9645SThomas Gleixner 
66*5cee9645SThomas Gleixner 	if (tloc) {
67*5cee9645SThomas Gleixner 		if (put_user(i,tloc))
68*5cee9645SThomas Gleixner 			return -EFAULT;
69*5cee9645SThomas Gleixner 	}
70*5cee9645SThomas Gleixner 	force_successful_syscall_return();
71*5cee9645SThomas Gleixner 	return i;
72*5cee9645SThomas Gleixner }
73*5cee9645SThomas Gleixner 
74*5cee9645SThomas Gleixner /*
75*5cee9645SThomas Gleixner  * sys_stime() can be implemented in user-level using
76*5cee9645SThomas Gleixner  * sys_settimeofday().  Is this for backwards compatibility?  If so,
77*5cee9645SThomas Gleixner  * why not move it into the appropriate arch directory (for those
78*5cee9645SThomas Gleixner  * architectures that need it).
79*5cee9645SThomas Gleixner  */
80*5cee9645SThomas Gleixner 
81*5cee9645SThomas Gleixner SYSCALL_DEFINE1(stime, time_t __user *, tptr)
82*5cee9645SThomas Gleixner {
83*5cee9645SThomas Gleixner 	struct timespec tv;
84*5cee9645SThomas Gleixner 	int err;
85*5cee9645SThomas Gleixner 
86*5cee9645SThomas Gleixner 	if (get_user(tv.tv_sec, tptr))
87*5cee9645SThomas Gleixner 		return -EFAULT;
88*5cee9645SThomas Gleixner 
89*5cee9645SThomas Gleixner 	tv.tv_nsec = 0;
90*5cee9645SThomas Gleixner 
91*5cee9645SThomas Gleixner 	err = security_settime(&tv, NULL);
92*5cee9645SThomas Gleixner 	if (err)
93*5cee9645SThomas Gleixner 		return err;
94*5cee9645SThomas Gleixner 
95*5cee9645SThomas Gleixner 	do_settimeofday(&tv);
96*5cee9645SThomas Gleixner 	return 0;
97*5cee9645SThomas Gleixner }
98*5cee9645SThomas Gleixner 
99*5cee9645SThomas Gleixner #endif /* __ARCH_WANT_SYS_TIME */
100*5cee9645SThomas Gleixner 
101*5cee9645SThomas Gleixner SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv,
102*5cee9645SThomas Gleixner 		struct timezone __user *, tz)
103*5cee9645SThomas Gleixner {
104*5cee9645SThomas Gleixner 	if (likely(tv != NULL)) {
105*5cee9645SThomas Gleixner 		struct timeval ktv;
106*5cee9645SThomas Gleixner 		do_gettimeofday(&ktv);
107*5cee9645SThomas Gleixner 		if (copy_to_user(tv, &ktv, sizeof(ktv)))
108*5cee9645SThomas Gleixner 			return -EFAULT;
109*5cee9645SThomas Gleixner 	}
110*5cee9645SThomas Gleixner 	if (unlikely(tz != NULL)) {
111*5cee9645SThomas Gleixner 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
112*5cee9645SThomas Gleixner 			return -EFAULT;
113*5cee9645SThomas Gleixner 	}
114*5cee9645SThomas Gleixner 	return 0;
115*5cee9645SThomas Gleixner }
116*5cee9645SThomas Gleixner 
117*5cee9645SThomas Gleixner /*
118*5cee9645SThomas Gleixner  * Indicates if there is an offset between the system clock and the hardware
119*5cee9645SThomas Gleixner  * clock/persistent clock/rtc.
120*5cee9645SThomas Gleixner  */
121*5cee9645SThomas Gleixner int persistent_clock_is_local;
122*5cee9645SThomas Gleixner 
123*5cee9645SThomas Gleixner /*
124*5cee9645SThomas Gleixner  * Adjust the time obtained from the CMOS to be UTC time instead of
125*5cee9645SThomas Gleixner  * local time.
126*5cee9645SThomas Gleixner  *
127*5cee9645SThomas Gleixner  * This is ugly, but preferable to the alternatives.  Otherwise we
128*5cee9645SThomas Gleixner  * would either need to write a program to do it in /etc/rc (and risk
129*5cee9645SThomas Gleixner  * confusion if the program gets run more than once; it would also be
130*5cee9645SThomas Gleixner  * hard to make the program warp the clock precisely n hours)  or
131*5cee9645SThomas Gleixner  * compile in the timezone information into the kernel.  Bad, bad....
132*5cee9645SThomas Gleixner  *
133*5cee9645SThomas Gleixner  *						- TYT, 1992-01-01
134*5cee9645SThomas Gleixner  *
135*5cee9645SThomas Gleixner  * The best thing to do is to keep the CMOS clock in universal time (UTC)
136*5cee9645SThomas Gleixner  * as real UNIX machines always do it. This avoids all headaches about
137*5cee9645SThomas Gleixner  * daylight saving times and warping kernel clocks.
138*5cee9645SThomas Gleixner  */
139*5cee9645SThomas Gleixner static inline void warp_clock(void)
140*5cee9645SThomas Gleixner {
141*5cee9645SThomas Gleixner 	if (sys_tz.tz_minuteswest != 0) {
142*5cee9645SThomas Gleixner 		struct timespec adjust;
143*5cee9645SThomas Gleixner 
144*5cee9645SThomas Gleixner 		persistent_clock_is_local = 1;
145*5cee9645SThomas Gleixner 		adjust.tv_sec = sys_tz.tz_minuteswest * 60;
146*5cee9645SThomas Gleixner 		adjust.tv_nsec = 0;
147*5cee9645SThomas Gleixner 		timekeeping_inject_offset(&adjust);
148*5cee9645SThomas Gleixner 	}
149*5cee9645SThomas Gleixner }
150*5cee9645SThomas Gleixner 
151*5cee9645SThomas Gleixner /*
152*5cee9645SThomas Gleixner  * In case for some reason the CMOS clock has not already been running
153*5cee9645SThomas Gleixner  * in UTC, but in some local time: The first time we set the timezone,
154*5cee9645SThomas Gleixner  * we will warp the clock so that it is ticking UTC time instead of
155*5cee9645SThomas Gleixner  * local time. Presumably, if someone is setting the timezone then we
156*5cee9645SThomas Gleixner  * are running in an environment where the programs understand about
157*5cee9645SThomas Gleixner  * timezones. This should be done at boot time in the /etc/rc script,
158*5cee9645SThomas Gleixner  * as soon as possible, so that the clock can be set right. Otherwise,
159*5cee9645SThomas Gleixner  * various programs will get confused when the clock gets warped.
160*5cee9645SThomas Gleixner  */
161*5cee9645SThomas Gleixner 
162*5cee9645SThomas Gleixner int do_sys_settimeofday(const struct timespec *tv, const struct timezone *tz)
163*5cee9645SThomas Gleixner {
164*5cee9645SThomas Gleixner 	static int firsttime = 1;
165*5cee9645SThomas Gleixner 	int error = 0;
166*5cee9645SThomas Gleixner 
167*5cee9645SThomas Gleixner 	if (tv && !timespec_valid(tv))
168*5cee9645SThomas Gleixner 		return -EINVAL;
169*5cee9645SThomas Gleixner 
170*5cee9645SThomas Gleixner 	error = security_settime(tv, tz);
171*5cee9645SThomas Gleixner 	if (error)
172*5cee9645SThomas Gleixner 		return error;
173*5cee9645SThomas Gleixner 
174*5cee9645SThomas Gleixner 	if (tz) {
175*5cee9645SThomas Gleixner 		sys_tz = *tz;
176*5cee9645SThomas Gleixner 		update_vsyscall_tz();
177*5cee9645SThomas Gleixner 		if (firsttime) {
178*5cee9645SThomas Gleixner 			firsttime = 0;
179*5cee9645SThomas Gleixner 			if (!tv)
180*5cee9645SThomas Gleixner 				warp_clock();
181*5cee9645SThomas Gleixner 		}
182*5cee9645SThomas Gleixner 	}
183*5cee9645SThomas Gleixner 	if (tv)
184*5cee9645SThomas Gleixner 		return do_settimeofday(tv);
185*5cee9645SThomas Gleixner 	return 0;
186*5cee9645SThomas Gleixner }
187*5cee9645SThomas Gleixner 
188*5cee9645SThomas Gleixner SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv,
189*5cee9645SThomas Gleixner 		struct timezone __user *, tz)
190*5cee9645SThomas Gleixner {
191*5cee9645SThomas Gleixner 	struct timeval user_tv;
192*5cee9645SThomas Gleixner 	struct timespec	new_ts;
193*5cee9645SThomas Gleixner 	struct timezone new_tz;
194*5cee9645SThomas Gleixner 
195*5cee9645SThomas Gleixner 	if (tv) {
196*5cee9645SThomas Gleixner 		if (copy_from_user(&user_tv, tv, sizeof(*tv)))
197*5cee9645SThomas Gleixner 			return -EFAULT;
198*5cee9645SThomas Gleixner 		new_ts.tv_sec = user_tv.tv_sec;
199*5cee9645SThomas Gleixner 		new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC;
200*5cee9645SThomas Gleixner 	}
201*5cee9645SThomas Gleixner 	if (tz) {
202*5cee9645SThomas Gleixner 		if (copy_from_user(&new_tz, tz, sizeof(*tz)))
203*5cee9645SThomas Gleixner 			return -EFAULT;
204*5cee9645SThomas Gleixner 	}
205*5cee9645SThomas Gleixner 
206*5cee9645SThomas Gleixner 	return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
207*5cee9645SThomas Gleixner }
208*5cee9645SThomas Gleixner 
209*5cee9645SThomas Gleixner SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p)
210*5cee9645SThomas Gleixner {
211*5cee9645SThomas Gleixner 	struct timex txc;		/* Local copy of parameter */
212*5cee9645SThomas Gleixner 	int ret;
213*5cee9645SThomas Gleixner 
214*5cee9645SThomas Gleixner 	/* Copy the user data space into the kernel copy
215*5cee9645SThomas Gleixner 	 * structure. But bear in mind that the structures
216*5cee9645SThomas Gleixner 	 * may change
217*5cee9645SThomas Gleixner 	 */
218*5cee9645SThomas Gleixner 	if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
219*5cee9645SThomas Gleixner 		return -EFAULT;
220*5cee9645SThomas Gleixner 	ret = do_adjtimex(&txc);
221*5cee9645SThomas Gleixner 	return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
222*5cee9645SThomas Gleixner }
223*5cee9645SThomas Gleixner 
224*5cee9645SThomas Gleixner /**
225*5cee9645SThomas Gleixner  * current_fs_time - Return FS time
226*5cee9645SThomas Gleixner  * @sb: Superblock.
227*5cee9645SThomas Gleixner  *
228*5cee9645SThomas Gleixner  * Return the current time truncated to the time granularity supported by
229*5cee9645SThomas Gleixner  * the fs.
230*5cee9645SThomas Gleixner  */
231*5cee9645SThomas Gleixner struct timespec current_fs_time(struct super_block *sb)
232*5cee9645SThomas Gleixner {
233*5cee9645SThomas Gleixner 	struct timespec now = current_kernel_time();
234*5cee9645SThomas Gleixner 	return timespec_trunc(now, sb->s_time_gran);
235*5cee9645SThomas Gleixner }
236*5cee9645SThomas Gleixner EXPORT_SYMBOL(current_fs_time);
237*5cee9645SThomas Gleixner 
238*5cee9645SThomas Gleixner /*
239*5cee9645SThomas Gleixner  * Convert jiffies to milliseconds and back.
240*5cee9645SThomas Gleixner  *
241*5cee9645SThomas Gleixner  * Avoid unnecessary multiplications/divisions in the
242*5cee9645SThomas Gleixner  * two most common HZ cases:
243*5cee9645SThomas Gleixner  */
244*5cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j)
245*5cee9645SThomas Gleixner {
246*5cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
247*5cee9645SThomas Gleixner 	return (MSEC_PER_SEC / HZ) * j;
248*5cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
249*5cee9645SThomas Gleixner 	return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
250*5cee9645SThomas Gleixner #else
251*5cee9645SThomas Gleixner # if BITS_PER_LONG == 32
252*5cee9645SThomas Gleixner 	return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
253*5cee9645SThomas Gleixner # else
254*5cee9645SThomas Gleixner 	return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
255*5cee9645SThomas Gleixner # endif
256*5cee9645SThomas Gleixner #endif
257*5cee9645SThomas Gleixner }
258*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs);
259*5cee9645SThomas Gleixner 
260*5cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j)
261*5cee9645SThomas Gleixner {
262*5cee9645SThomas Gleixner #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
263*5cee9645SThomas Gleixner 	return (USEC_PER_SEC / HZ) * j;
264*5cee9645SThomas Gleixner #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
265*5cee9645SThomas Gleixner 	return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
266*5cee9645SThomas Gleixner #else
267*5cee9645SThomas Gleixner # if BITS_PER_LONG == 32
268*5cee9645SThomas Gleixner 	return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
269*5cee9645SThomas Gleixner # else
270*5cee9645SThomas Gleixner 	return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
271*5cee9645SThomas Gleixner # endif
272*5cee9645SThomas Gleixner #endif
273*5cee9645SThomas Gleixner }
274*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs);
275*5cee9645SThomas Gleixner 
276*5cee9645SThomas Gleixner /**
277*5cee9645SThomas Gleixner  * timespec_trunc - Truncate timespec to a granularity
278*5cee9645SThomas Gleixner  * @t: Timespec
279*5cee9645SThomas Gleixner  * @gran: Granularity in ns.
280*5cee9645SThomas Gleixner  *
281*5cee9645SThomas Gleixner  * Truncate a timespec to a granularity. gran must be smaller than a second.
282*5cee9645SThomas Gleixner  * Always rounds down.
283*5cee9645SThomas Gleixner  *
284*5cee9645SThomas Gleixner  * This function should be only used for timestamps returned by
285*5cee9645SThomas Gleixner  * current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because
286*5cee9645SThomas Gleixner  * it doesn't handle the better resolution of the latter.
287*5cee9645SThomas Gleixner  */
288*5cee9645SThomas Gleixner struct timespec timespec_trunc(struct timespec t, unsigned gran)
289*5cee9645SThomas Gleixner {
290*5cee9645SThomas Gleixner 	/*
291*5cee9645SThomas Gleixner 	 * Division is pretty slow so avoid it for common cases.
292*5cee9645SThomas Gleixner 	 * Currently current_kernel_time() never returns better than
293*5cee9645SThomas Gleixner 	 * jiffies resolution. Exploit that.
294*5cee9645SThomas Gleixner 	 */
295*5cee9645SThomas Gleixner 	if (gran <= jiffies_to_usecs(1) * 1000) {
296*5cee9645SThomas Gleixner 		/* nothing */
297*5cee9645SThomas Gleixner 	} else if (gran == 1000000000) {
298*5cee9645SThomas Gleixner 		t.tv_nsec = 0;
299*5cee9645SThomas Gleixner 	} else {
300*5cee9645SThomas Gleixner 		t.tv_nsec -= t.tv_nsec % gran;
301*5cee9645SThomas Gleixner 	}
302*5cee9645SThomas Gleixner 	return t;
303*5cee9645SThomas Gleixner }
304*5cee9645SThomas Gleixner EXPORT_SYMBOL(timespec_trunc);
305*5cee9645SThomas Gleixner 
306*5cee9645SThomas Gleixner /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
307*5cee9645SThomas Gleixner  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
308*5cee9645SThomas Gleixner  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
309*5cee9645SThomas Gleixner  *
310*5cee9645SThomas Gleixner  * [For the Julian calendar (which was used in Russia before 1917,
311*5cee9645SThomas Gleixner  * Britain & colonies before 1752, anywhere else before 1582,
312*5cee9645SThomas Gleixner  * and is still in use by some communities) leave out the
313*5cee9645SThomas Gleixner  * -year/100+year/400 terms, and add 10.]
314*5cee9645SThomas Gleixner  *
315*5cee9645SThomas Gleixner  * This algorithm was first published by Gauss (I think).
316*5cee9645SThomas Gleixner  *
317*5cee9645SThomas Gleixner  * WARNING: this function will overflow on 2106-02-07 06:28:16 on
318*5cee9645SThomas Gleixner  * machines where long is 32-bit! (However, as time_t is signed, we
319*5cee9645SThomas Gleixner  * will already get problems at other places on 2038-01-19 03:14:08)
320*5cee9645SThomas Gleixner  */
321*5cee9645SThomas Gleixner unsigned long
322*5cee9645SThomas Gleixner mktime(const unsigned int year0, const unsigned int mon0,
323*5cee9645SThomas Gleixner        const unsigned int day, const unsigned int hour,
324*5cee9645SThomas Gleixner        const unsigned int min, const unsigned int sec)
325*5cee9645SThomas Gleixner {
326*5cee9645SThomas Gleixner 	unsigned int mon = mon0, year = year0;
327*5cee9645SThomas Gleixner 
328*5cee9645SThomas Gleixner 	/* 1..12 -> 11,12,1..10 */
329*5cee9645SThomas Gleixner 	if (0 >= (int) (mon -= 2)) {
330*5cee9645SThomas Gleixner 		mon += 12;	/* Puts Feb last since it has leap day */
331*5cee9645SThomas Gleixner 		year -= 1;
332*5cee9645SThomas Gleixner 	}
333*5cee9645SThomas Gleixner 
334*5cee9645SThomas Gleixner 	return ((((unsigned long)
335*5cee9645SThomas Gleixner 		  (year/4 - year/100 + year/400 + 367*mon/12 + day) +
336*5cee9645SThomas Gleixner 		  year*365 - 719499
337*5cee9645SThomas Gleixner 	    )*24 + hour /* now have hours */
338*5cee9645SThomas Gleixner 	  )*60 + min /* now have minutes */
339*5cee9645SThomas Gleixner 	)*60 + sec; /* finally seconds */
340*5cee9645SThomas Gleixner }
341*5cee9645SThomas Gleixner 
342*5cee9645SThomas Gleixner EXPORT_SYMBOL(mktime);
343*5cee9645SThomas Gleixner 
344*5cee9645SThomas Gleixner /**
345*5cee9645SThomas Gleixner  * set_normalized_timespec - set timespec sec and nsec parts and normalize
346*5cee9645SThomas Gleixner  *
347*5cee9645SThomas Gleixner  * @ts:		pointer to timespec variable to be set
348*5cee9645SThomas Gleixner  * @sec:	seconds to set
349*5cee9645SThomas Gleixner  * @nsec:	nanoseconds to set
350*5cee9645SThomas Gleixner  *
351*5cee9645SThomas Gleixner  * Set seconds and nanoseconds field of a timespec variable and
352*5cee9645SThomas Gleixner  * normalize to the timespec storage format
353*5cee9645SThomas Gleixner  *
354*5cee9645SThomas Gleixner  * Note: The tv_nsec part is always in the range of
355*5cee9645SThomas Gleixner  *	0 <= tv_nsec < NSEC_PER_SEC
356*5cee9645SThomas Gleixner  * For negative values only the tv_sec field is negative !
357*5cee9645SThomas Gleixner  */
358*5cee9645SThomas Gleixner void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
359*5cee9645SThomas Gleixner {
360*5cee9645SThomas Gleixner 	while (nsec >= NSEC_PER_SEC) {
361*5cee9645SThomas Gleixner 		/*
362*5cee9645SThomas Gleixner 		 * The following asm() prevents the compiler from
363*5cee9645SThomas Gleixner 		 * optimising this loop into a modulo operation. See
364*5cee9645SThomas Gleixner 		 * also __iter_div_u64_rem() in include/linux/time.h
365*5cee9645SThomas Gleixner 		 */
366*5cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
367*5cee9645SThomas Gleixner 		nsec -= NSEC_PER_SEC;
368*5cee9645SThomas Gleixner 		++sec;
369*5cee9645SThomas Gleixner 	}
370*5cee9645SThomas Gleixner 	while (nsec < 0) {
371*5cee9645SThomas Gleixner 		asm("" : "+rm"(nsec));
372*5cee9645SThomas Gleixner 		nsec += NSEC_PER_SEC;
373*5cee9645SThomas Gleixner 		--sec;
374*5cee9645SThomas Gleixner 	}
375*5cee9645SThomas Gleixner 	ts->tv_sec = sec;
376*5cee9645SThomas Gleixner 	ts->tv_nsec = nsec;
377*5cee9645SThomas Gleixner }
378*5cee9645SThomas Gleixner EXPORT_SYMBOL(set_normalized_timespec);
379*5cee9645SThomas Gleixner 
380*5cee9645SThomas Gleixner /**
381*5cee9645SThomas Gleixner  * ns_to_timespec - Convert nanoseconds to timespec
382*5cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
383*5cee9645SThomas Gleixner  *
384*5cee9645SThomas Gleixner  * Returns the timespec representation of the nsec parameter.
385*5cee9645SThomas Gleixner  */
386*5cee9645SThomas Gleixner struct timespec ns_to_timespec(const s64 nsec)
387*5cee9645SThomas Gleixner {
388*5cee9645SThomas Gleixner 	struct timespec ts;
389*5cee9645SThomas Gleixner 	s32 rem;
390*5cee9645SThomas Gleixner 
391*5cee9645SThomas Gleixner 	if (!nsec)
392*5cee9645SThomas Gleixner 		return (struct timespec) {0, 0};
393*5cee9645SThomas Gleixner 
394*5cee9645SThomas Gleixner 	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
395*5cee9645SThomas Gleixner 	if (unlikely(rem < 0)) {
396*5cee9645SThomas Gleixner 		ts.tv_sec--;
397*5cee9645SThomas Gleixner 		rem += NSEC_PER_SEC;
398*5cee9645SThomas Gleixner 	}
399*5cee9645SThomas Gleixner 	ts.tv_nsec = rem;
400*5cee9645SThomas Gleixner 
401*5cee9645SThomas Gleixner 	return ts;
402*5cee9645SThomas Gleixner }
403*5cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timespec);
404*5cee9645SThomas Gleixner 
405*5cee9645SThomas Gleixner /**
406*5cee9645SThomas Gleixner  * ns_to_timeval - Convert nanoseconds to timeval
407*5cee9645SThomas Gleixner  * @nsec:       the nanoseconds value to be converted
408*5cee9645SThomas Gleixner  *
409*5cee9645SThomas Gleixner  * Returns the timeval representation of the nsec parameter.
410*5cee9645SThomas Gleixner  */
411*5cee9645SThomas Gleixner struct timeval ns_to_timeval(const s64 nsec)
412*5cee9645SThomas Gleixner {
413*5cee9645SThomas Gleixner 	struct timespec ts = ns_to_timespec(nsec);
414*5cee9645SThomas Gleixner 	struct timeval tv;
415*5cee9645SThomas Gleixner 
416*5cee9645SThomas Gleixner 	tv.tv_sec = ts.tv_sec;
417*5cee9645SThomas Gleixner 	tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
418*5cee9645SThomas Gleixner 
419*5cee9645SThomas Gleixner 	return tv;
420*5cee9645SThomas Gleixner }
421*5cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timeval);
422*5cee9645SThomas Gleixner 
423*5cee9645SThomas Gleixner /*
424*5cee9645SThomas Gleixner  * When we convert to jiffies then we interpret incoming values
425*5cee9645SThomas Gleixner  * the following way:
426*5cee9645SThomas Gleixner  *
427*5cee9645SThomas Gleixner  * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
428*5cee9645SThomas Gleixner  *
429*5cee9645SThomas Gleixner  * - 'too large' values [that would result in larger than
430*5cee9645SThomas Gleixner  *   MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
431*5cee9645SThomas Gleixner  *
432*5cee9645SThomas Gleixner  * - all other values are converted to jiffies by either multiplying
433*5cee9645SThomas Gleixner  *   the input value by a factor or dividing it with a factor
434*5cee9645SThomas Gleixner  *
435*5cee9645SThomas Gleixner  * We must also be careful about 32-bit overflows.
436*5cee9645SThomas Gleixner  */
437*5cee9645SThomas Gleixner unsigned long msecs_to_jiffies(const unsigned int m)
438*5cee9645SThomas Gleixner {
439*5cee9645SThomas Gleixner 	/*
440*5cee9645SThomas Gleixner 	 * Negative value, means infinite timeout:
441*5cee9645SThomas Gleixner 	 */
442*5cee9645SThomas Gleixner 	if ((int)m < 0)
443*5cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
444*5cee9645SThomas Gleixner 
445*5cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
446*5cee9645SThomas Gleixner 	/*
447*5cee9645SThomas Gleixner 	 * HZ is equal to or smaller than 1000, and 1000 is a nice
448*5cee9645SThomas Gleixner 	 * round multiple of HZ, divide with the factor between them,
449*5cee9645SThomas Gleixner 	 * but round upwards:
450*5cee9645SThomas Gleixner 	 */
451*5cee9645SThomas Gleixner 	return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
452*5cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
453*5cee9645SThomas Gleixner 	/*
454*5cee9645SThomas Gleixner 	 * HZ is larger than 1000, and HZ is a nice round multiple of
455*5cee9645SThomas Gleixner 	 * 1000 - simply multiply with the factor between them.
456*5cee9645SThomas Gleixner 	 *
457*5cee9645SThomas Gleixner 	 * But first make sure the multiplication result cannot
458*5cee9645SThomas Gleixner 	 * overflow:
459*5cee9645SThomas Gleixner 	 */
460*5cee9645SThomas Gleixner 	if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
461*5cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
462*5cee9645SThomas Gleixner 
463*5cee9645SThomas Gleixner 	return m * (HZ / MSEC_PER_SEC);
464*5cee9645SThomas Gleixner #else
465*5cee9645SThomas Gleixner 	/*
466*5cee9645SThomas Gleixner 	 * Generic case - multiply, round and divide. But first
467*5cee9645SThomas Gleixner 	 * check that if we are doing a net multiplication, that
468*5cee9645SThomas Gleixner 	 * we wouldn't overflow:
469*5cee9645SThomas Gleixner 	 */
470*5cee9645SThomas Gleixner 	if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
471*5cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
472*5cee9645SThomas Gleixner 
473*5cee9645SThomas Gleixner 	return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
474*5cee9645SThomas Gleixner 		>> MSEC_TO_HZ_SHR32;
475*5cee9645SThomas Gleixner #endif
476*5cee9645SThomas Gleixner }
477*5cee9645SThomas Gleixner EXPORT_SYMBOL(msecs_to_jiffies);
478*5cee9645SThomas Gleixner 
479*5cee9645SThomas Gleixner unsigned long usecs_to_jiffies(const unsigned int u)
480*5cee9645SThomas Gleixner {
481*5cee9645SThomas Gleixner 	if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
482*5cee9645SThomas Gleixner 		return MAX_JIFFY_OFFSET;
483*5cee9645SThomas Gleixner #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
484*5cee9645SThomas Gleixner 	return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
485*5cee9645SThomas Gleixner #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
486*5cee9645SThomas Gleixner 	return u * (HZ / USEC_PER_SEC);
487*5cee9645SThomas Gleixner #else
488*5cee9645SThomas Gleixner 	return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32)
489*5cee9645SThomas Gleixner 		>> USEC_TO_HZ_SHR32;
490*5cee9645SThomas Gleixner #endif
491*5cee9645SThomas Gleixner }
492*5cee9645SThomas Gleixner EXPORT_SYMBOL(usecs_to_jiffies);
493*5cee9645SThomas Gleixner 
494*5cee9645SThomas Gleixner /*
495*5cee9645SThomas Gleixner  * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
496*5cee9645SThomas Gleixner  * that a remainder subtract here would not do the right thing as the
497*5cee9645SThomas Gleixner  * resolution values don't fall on second boundries.  I.e. the line:
498*5cee9645SThomas Gleixner  * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
499*5cee9645SThomas Gleixner  *
500*5cee9645SThomas Gleixner  * Rather, we just shift the bits off the right.
501*5cee9645SThomas Gleixner  *
502*5cee9645SThomas Gleixner  * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
503*5cee9645SThomas Gleixner  * value to a scaled second value.
504*5cee9645SThomas Gleixner  */
505*5cee9645SThomas Gleixner unsigned long
506*5cee9645SThomas Gleixner timespec_to_jiffies(const struct timespec *value)
507*5cee9645SThomas Gleixner {
508*5cee9645SThomas Gleixner 	unsigned long sec = value->tv_sec;
509*5cee9645SThomas Gleixner 	long nsec = value->tv_nsec + TICK_NSEC - 1;
510*5cee9645SThomas Gleixner 
511*5cee9645SThomas Gleixner 	if (sec >= MAX_SEC_IN_JIFFIES){
512*5cee9645SThomas Gleixner 		sec = MAX_SEC_IN_JIFFIES;
513*5cee9645SThomas Gleixner 		nsec = 0;
514*5cee9645SThomas Gleixner 	}
515*5cee9645SThomas Gleixner 	return (((u64)sec * SEC_CONVERSION) +
516*5cee9645SThomas Gleixner 		(((u64)nsec * NSEC_CONVERSION) >>
517*5cee9645SThomas Gleixner 		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
518*5cee9645SThomas Gleixner 
519*5cee9645SThomas Gleixner }
520*5cee9645SThomas Gleixner EXPORT_SYMBOL(timespec_to_jiffies);
521*5cee9645SThomas Gleixner 
522*5cee9645SThomas Gleixner void
523*5cee9645SThomas Gleixner jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
524*5cee9645SThomas Gleixner {
525*5cee9645SThomas Gleixner 	/*
526*5cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
527*5cee9645SThomas Gleixner 	 * one divide.
528*5cee9645SThomas Gleixner 	 */
529*5cee9645SThomas Gleixner 	u32 rem;
530*5cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
531*5cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
532*5cee9645SThomas Gleixner 	value->tv_nsec = rem;
533*5cee9645SThomas Gleixner }
534*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timespec);
535*5cee9645SThomas Gleixner 
536*5cee9645SThomas Gleixner /* Same for "timeval"
537*5cee9645SThomas Gleixner  *
538*5cee9645SThomas Gleixner  * Well, almost.  The problem here is that the real system resolution is
539*5cee9645SThomas Gleixner  * in nanoseconds and the value being converted is in micro seconds.
540*5cee9645SThomas Gleixner  * Also for some machines (those that use HZ = 1024, in-particular),
541*5cee9645SThomas Gleixner  * there is a LARGE error in the tick size in microseconds.
542*5cee9645SThomas Gleixner 
543*5cee9645SThomas Gleixner  * The solution we use is to do the rounding AFTER we convert the
544*5cee9645SThomas Gleixner  * microsecond part.  Thus the USEC_ROUND, the bits to be shifted off.
545*5cee9645SThomas Gleixner  * Instruction wise, this should cost only an additional add with carry
546*5cee9645SThomas Gleixner  * instruction above the way it was done above.
547*5cee9645SThomas Gleixner  */
548*5cee9645SThomas Gleixner unsigned long
549*5cee9645SThomas Gleixner timeval_to_jiffies(const struct timeval *value)
550*5cee9645SThomas Gleixner {
551*5cee9645SThomas Gleixner 	unsigned long sec = value->tv_sec;
552*5cee9645SThomas Gleixner 	long usec = value->tv_usec;
553*5cee9645SThomas Gleixner 
554*5cee9645SThomas Gleixner 	if (sec >= MAX_SEC_IN_JIFFIES){
555*5cee9645SThomas Gleixner 		sec = MAX_SEC_IN_JIFFIES;
556*5cee9645SThomas Gleixner 		usec = 0;
557*5cee9645SThomas Gleixner 	}
558*5cee9645SThomas Gleixner 	return (((u64)sec * SEC_CONVERSION) +
559*5cee9645SThomas Gleixner 		(((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
560*5cee9645SThomas Gleixner 		 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
561*5cee9645SThomas Gleixner }
562*5cee9645SThomas Gleixner EXPORT_SYMBOL(timeval_to_jiffies);
563*5cee9645SThomas Gleixner 
564*5cee9645SThomas Gleixner void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
565*5cee9645SThomas Gleixner {
566*5cee9645SThomas Gleixner 	/*
567*5cee9645SThomas Gleixner 	 * Convert jiffies to nanoseconds and separate with
568*5cee9645SThomas Gleixner 	 * one divide.
569*5cee9645SThomas Gleixner 	 */
570*5cee9645SThomas Gleixner 	u32 rem;
571*5cee9645SThomas Gleixner 
572*5cee9645SThomas Gleixner 	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
573*5cee9645SThomas Gleixner 				    NSEC_PER_SEC, &rem);
574*5cee9645SThomas Gleixner 	value->tv_usec = rem / NSEC_PER_USEC;
575*5cee9645SThomas Gleixner }
576*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timeval);
577*5cee9645SThomas Gleixner 
578*5cee9645SThomas Gleixner /*
579*5cee9645SThomas Gleixner  * Convert jiffies/jiffies_64 to clock_t and back.
580*5cee9645SThomas Gleixner  */
581*5cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x)
582*5cee9645SThomas Gleixner {
583*5cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
584*5cee9645SThomas Gleixner # if HZ < USER_HZ
585*5cee9645SThomas Gleixner 	return x * (USER_HZ / HZ);
586*5cee9645SThomas Gleixner # else
587*5cee9645SThomas Gleixner 	return x / (HZ / USER_HZ);
588*5cee9645SThomas Gleixner # endif
589*5cee9645SThomas Gleixner #else
590*5cee9645SThomas Gleixner 	return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
591*5cee9645SThomas Gleixner #endif
592*5cee9645SThomas Gleixner }
593*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t);
594*5cee9645SThomas Gleixner 
595*5cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x)
596*5cee9645SThomas Gleixner {
597*5cee9645SThomas Gleixner #if (HZ % USER_HZ)==0
598*5cee9645SThomas Gleixner 	if (x >= ~0UL / (HZ / USER_HZ))
599*5cee9645SThomas Gleixner 		return ~0UL;
600*5cee9645SThomas Gleixner 	return x * (HZ / USER_HZ);
601*5cee9645SThomas Gleixner #else
602*5cee9645SThomas Gleixner 	/* Don't worry about loss of precision here .. */
603*5cee9645SThomas Gleixner 	if (x >= ~0UL / HZ * USER_HZ)
604*5cee9645SThomas Gleixner 		return ~0UL;
605*5cee9645SThomas Gleixner 
606*5cee9645SThomas Gleixner 	/* .. but do try to contain it here */
607*5cee9645SThomas Gleixner 	return div_u64((u64)x * HZ, USER_HZ);
608*5cee9645SThomas Gleixner #endif
609*5cee9645SThomas Gleixner }
610*5cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies);
611*5cee9645SThomas Gleixner 
612*5cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x)
613*5cee9645SThomas Gleixner {
614*5cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
615*5cee9645SThomas Gleixner # if HZ < USER_HZ
616*5cee9645SThomas Gleixner 	x = div_u64(x * USER_HZ, HZ);
617*5cee9645SThomas Gleixner # elif HZ > USER_HZ
618*5cee9645SThomas Gleixner 	x = div_u64(x, HZ / USER_HZ);
619*5cee9645SThomas Gleixner # else
620*5cee9645SThomas Gleixner 	/* Nothing to do */
621*5cee9645SThomas Gleixner # endif
622*5cee9645SThomas Gleixner #else
623*5cee9645SThomas Gleixner 	/*
624*5cee9645SThomas Gleixner 	 * There are better ways that don't overflow early,
625*5cee9645SThomas Gleixner 	 * but even this doesn't overflow in hundreds of years
626*5cee9645SThomas Gleixner 	 * in 64 bits, so..
627*5cee9645SThomas Gleixner 	 */
628*5cee9645SThomas Gleixner 	x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
629*5cee9645SThomas Gleixner #endif
630*5cee9645SThomas Gleixner 	return x;
631*5cee9645SThomas Gleixner }
632*5cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t);
633*5cee9645SThomas Gleixner 
634*5cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x)
635*5cee9645SThomas Gleixner {
636*5cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0
637*5cee9645SThomas Gleixner 	return div_u64(x, NSEC_PER_SEC / USER_HZ);
638*5cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0
639*5cee9645SThomas Gleixner 	return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
640*5cee9645SThomas Gleixner #else
641*5cee9645SThomas Gleixner 	/*
642*5cee9645SThomas Gleixner          * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
643*5cee9645SThomas Gleixner          * overflow after 64.99 years.
644*5cee9645SThomas Gleixner          * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
645*5cee9645SThomas Gleixner          */
646*5cee9645SThomas Gleixner 	return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
647*5cee9645SThomas Gleixner #endif
648*5cee9645SThomas Gleixner }
649*5cee9645SThomas Gleixner 
650*5cee9645SThomas Gleixner /**
651*5cee9645SThomas Gleixner  * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
652*5cee9645SThomas Gleixner  *
653*5cee9645SThomas Gleixner  * @n:	nsecs in u64
654*5cee9645SThomas Gleixner  *
655*5cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
656*5cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
657*5cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
658*5cee9645SThomas Gleixner  *
659*5cee9645SThomas Gleixner  * note:
660*5cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
661*5cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
662*5cee9645SThomas Gleixner  */
663*5cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n)
664*5cee9645SThomas Gleixner {
665*5cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0
666*5cee9645SThomas Gleixner 	/* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
667*5cee9645SThomas Gleixner 	return div_u64(n, NSEC_PER_SEC / HZ);
668*5cee9645SThomas Gleixner #elif (HZ % 512) == 0
669*5cee9645SThomas Gleixner 	/* overflow after 292 years if HZ = 1024 */
670*5cee9645SThomas Gleixner 	return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
671*5cee9645SThomas Gleixner #else
672*5cee9645SThomas Gleixner 	/*
673*5cee9645SThomas Gleixner 	 * Generic case - optimized for cases where HZ is a multiple of 3.
674*5cee9645SThomas Gleixner 	 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
675*5cee9645SThomas Gleixner 	 */
676*5cee9645SThomas Gleixner 	return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
677*5cee9645SThomas Gleixner #endif
678*5cee9645SThomas Gleixner }
679*5cee9645SThomas Gleixner 
680*5cee9645SThomas Gleixner /**
681*5cee9645SThomas Gleixner  * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
682*5cee9645SThomas Gleixner  *
683*5cee9645SThomas Gleixner  * @n:	nsecs in u64
684*5cee9645SThomas Gleixner  *
685*5cee9645SThomas Gleixner  * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
686*5cee9645SThomas Gleixner  * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
687*5cee9645SThomas Gleixner  * for scheduler, not for use in device drivers to calculate timeout value.
688*5cee9645SThomas Gleixner  *
689*5cee9645SThomas Gleixner  * note:
690*5cee9645SThomas Gleixner  *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
691*5cee9645SThomas Gleixner  *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
692*5cee9645SThomas Gleixner  */
693*5cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n)
694*5cee9645SThomas Gleixner {
695*5cee9645SThomas Gleixner 	return (unsigned long)nsecs_to_jiffies64(n);
696*5cee9645SThomas Gleixner }
697*5cee9645SThomas Gleixner 
698*5cee9645SThomas Gleixner /*
699*5cee9645SThomas Gleixner  * Add two timespec values and do a safety check for overflow.
700*5cee9645SThomas Gleixner  * It's assumed that both values are valid (>= 0)
701*5cee9645SThomas Gleixner  */
702*5cee9645SThomas Gleixner struct timespec timespec_add_safe(const struct timespec lhs,
703*5cee9645SThomas Gleixner 				  const struct timespec rhs)
704*5cee9645SThomas Gleixner {
705*5cee9645SThomas Gleixner 	struct timespec res;
706*5cee9645SThomas Gleixner 
707*5cee9645SThomas Gleixner 	set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
708*5cee9645SThomas Gleixner 				lhs.tv_nsec + rhs.tv_nsec);
709*5cee9645SThomas Gleixner 
710*5cee9645SThomas Gleixner 	if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
711*5cee9645SThomas Gleixner 		res.tv_sec = TIME_T_MAX;
712*5cee9645SThomas Gleixner 
713*5cee9645SThomas Gleixner 	return res;
714*5cee9645SThomas Gleixner }
715