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> 31abcbcb80SGeert Uytterhoeven #include <linux/kernel.h> 325cee9645SThomas Gleixner #include <linux/timex.h> 335cee9645SThomas Gleixner #include <linux/capability.h> 345cee9645SThomas Gleixner #include <linux/timekeeper_internal.h> 355cee9645SThomas Gleixner #include <linux/errno.h> 365cee9645SThomas Gleixner #include <linux/syscalls.h> 375cee9645SThomas Gleixner #include <linux/security.h> 385cee9645SThomas Gleixner #include <linux/fs.h> 395cee9645SThomas Gleixner #include <linux/math64.h> 405cee9645SThomas Gleixner #include <linux/ptrace.h> 415cee9645SThomas Gleixner 427c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 433a4d44b6SAl Viro #include <linux/compat.h> 445cee9645SThomas Gleixner #include <asm/unistd.h> 455cee9645SThomas Gleixner 460a227985SNicholas Mc Guire #include <generated/timeconst.h> 478b094cd0SThomas Gleixner #include "timekeeping.h" 485cee9645SThomas Gleixner 495cee9645SThomas Gleixner /* 505cee9645SThomas Gleixner * The timezone where the local system is located. Used as a default by some 515cee9645SThomas Gleixner * programs who obtain this value by using gettimeofday. 525cee9645SThomas Gleixner */ 535cee9645SThomas Gleixner struct timezone sys_tz; 545cee9645SThomas Gleixner 555cee9645SThomas Gleixner EXPORT_SYMBOL(sys_tz); 565cee9645SThomas Gleixner 575cee9645SThomas Gleixner #ifdef __ARCH_WANT_SYS_TIME 585cee9645SThomas Gleixner 595cee9645SThomas Gleixner /* 605cee9645SThomas Gleixner * sys_time() can be implemented in user-level using 615cee9645SThomas Gleixner * sys_gettimeofday(). Is this for backwards compatibility? If so, 625cee9645SThomas Gleixner * why not move it into the appropriate arch directory (for those 635cee9645SThomas Gleixner * architectures that need it). 645cee9645SThomas Gleixner */ 655cee9645SThomas Gleixner SYSCALL_DEFINE1(time, time_t __user *, tloc) 665cee9645SThomas Gleixner { 67f5a89295SArnd Bergmann time_t i = (time_t)ktime_get_real_seconds(); 685cee9645SThomas Gleixner 695cee9645SThomas Gleixner if (tloc) { 705cee9645SThomas Gleixner if (put_user(i,tloc)) 715cee9645SThomas Gleixner return -EFAULT; 725cee9645SThomas Gleixner } 735cee9645SThomas Gleixner force_successful_syscall_return(); 745cee9645SThomas Gleixner return i; 755cee9645SThomas Gleixner } 765cee9645SThomas Gleixner 775cee9645SThomas Gleixner /* 785cee9645SThomas Gleixner * sys_stime() can be implemented in user-level using 795cee9645SThomas Gleixner * sys_settimeofday(). Is this for backwards compatibility? If so, 805cee9645SThomas Gleixner * why not move it into the appropriate arch directory (for those 815cee9645SThomas Gleixner * architectures that need it). 825cee9645SThomas Gleixner */ 835cee9645SThomas Gleixner 845cee9645SThomas Gleixner SYSCALL_DEFINE1(stime, time_t __user *, tptr) 855cee9645SThomas Gleixner { 864eb1bca1SArnd Bergmann struct timespec64 tv; 875cee9645SThomas Gleixner int err; 885cee9645SThomas Gleixner 895cee9645SThomas Gleixner if (get_user(tv.tv_sec, tptr)) 905cee9645SThomas Gleixner return -EFAULT; 915cee9645SThomas Gleixner 925cee9645SThomas Gleixner tv.tv_nsec = 0; 935cee9645SThomas Gleixner 944eb1bca1SArnd Bergmann err = security_settime64(&tv, NULL); 955cee9645SThomas Gleixner if (err) 965cee9645SThomas Gleixner return err; 975cee9645SThomas Gleixner 984eb1bca1SArnd Bergmann do_settimeofday64(&tv); 995cee9645SThomas Gleixner return 0; 1005cee9645SThomas Gleixner } 1015cee9645SThomas Gleixner 1025cee9645SThomas Gleixner #endif /* __ARCH_WANT_SYS_TIME */ 1035cee9645SThomas Gleixner 104b180db2cSAl Viro #ifdef CONFIG_COMPAT 105b180db2cSAl Viro #ifdef __ARCH_WANT_COMPAT_SYS_TIME 106b180db2cSAl Viro 107b180db2cSAl Viro /* compat_time_t is a 32 bit "long" and needs to get converted. */ 108b180db2cSAl Viro COMPAT_SYSCALL_DEFINE1(time, compat_time_t __user *, tloc) 109b180db2cSAl Viro { 110b180db2cSAl Viro compat_time_t i; 111b180db2cSAl Viro 112f5a89295SArnd Bergmann i = (compat_time_t)ktime_get_real_seconds(); 113b180db2cSAl Viro 114b180db2cSAl Viro if (tloc) { 115b180db2cSAl Viro if (put_user(i,tloc)) 116b180db2cSAl Viro return -EFAULT; 117b180db2cSAl Viro } 118b180db2cSAl Viro force_successful_syscall_return(); 119b180db2cSAl Viro return i; 120b180db2cSAl Viro } 121b180db2cSAl Viro 122b180db2cSAl Viro COMPAT_SYSCALL_DEFINE1(stime, compat_time_t __user *, tptr) 123b180db2cSAl Viro { 1244eb1bca1SArnd Bergmann struct timespec64 tv; 125b180db2cSAl Viro int err; 126b180db2cSAl Viro 127b180db2cSAl Viro if (get_user(tv.tv_sec, tptr)) 128b180db2cSAl Viro return -EFAULT; 129b180db2cSAl Viro 130b180db2cSAl Viro tv.tv_nsec = 0; 131b180db2cSAl Viro 1324eb1bca1SArnd Bergmann err = security_settime64(&tv, NULL); 133b180db2cSAl Viro if (err) 134b180db2cSAl Viro return err; 135b180db2cSAl Viro 1364eb1bca1SArnd Bergmann do_settimeofday64(&tv); 137b180db2cSAl Viro return 0; 138b180db2cSAl Viro } 139b180db2cSAl Viro 140b180db2cSAl Viro #endif /* __ARCH_WANT_COMPAT_SYS_TIME */ 141b180db2cSAl Viro #endif 142b180db2cSAl Viro 1435cee9645SThomas Gleixner SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, 1445cee9645SThomas Gleixner struct timezone __user *, tz) 1455cee9645SThomas Gleixner { 1465cee9645SThomas Gleixner if (likely(tv != NULL)) { 147*33e26418SArnd Bergmann struct timespec64 ts; 148*33e26418SArnd Bergmann 149*33e26418SArnd Bergmann ktime_get_real_ts64(&ts); 150*33e26418SArnd Bergmann if (put_user(ts.tv_sec, &tv->tv_sec) || 151*33e26418SArnd Bergmann put_user(ts.tv_nsec / 1000, &tv->tv_usec)) 1525cee9645SThomas Gleixner return -EFAULT; 1535cee9645SThomas Gleixner } 1545cee9645SThomas Gleixner if (unlikely(tz != NULL)) { 1555cee9645SThomas Gleixner if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 1565cee9645SThomas Gleixner return -EFAULT; 1575cee9645SThomas Gleixner } 1585cee9645SThomas Gleixner return 0; 1595cee9645SThomas Gleixner } 1605cee9645SThomas Gleixner 1615cee9645SThomas Gleixner /* 1625cee9645SThomas Gleixner * In case for some reason the CMOS clock has not already been running 1635cee9645SThomas Gleixner * in UTC, but in some local time: The first time we set the timezone, 1645cee9645SThomas Gleixner * we will warp the clock so that it is ticking UTC time instead of 1655cee9645SThomas Gleixner * local time. Presumably, if someone is setting the timezone then we 1665cee9645SThomas Gleixner * are running in an environment where the programs understand about 1675cee9645SThomas Gleixner * timezones. This should be done at boot time in the /etc/rc script, 1685cee9645SThomas Gleixner * as soon as possible, so that the clock can be set right. Otherwise, 1695cee9645SThomas Gleixner * various programs will get confused when the clock gets warped. 1705cee9645SThomas Gleixner */ 1715cee9645SThomas Gleixner 17286d34732SBaolin Wang int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz) 1735cee9645SThomas Gleixner { 1745cee9645SThomas Gleixner static int firsttime = 1; 1755cee9645SThomas Gleixner int error = 0; 1765cee9645SThomas Gleixner 17786d34732SBaolin Wang if (tv && !timespec64_valid(tv)) 1785cee9645SThomas Gleixner return -EINVAL; 1795cee9645SThomas Gleixner 18086d34732SBaolin Wang error = security_settime64(tv, tz); 1815cee9645SThomas Gleixner if (error) 1825cee9645SThomas Gleixner return error; 1835cee9645SThomas Gleixner 1845cee9645SThomas Gleixner if (tz) { 1856f7d7984SSasha Levin /* Verify we're witin the +-15 hrs range */ 1866f7d7984SSasha Levin if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60) 1876f7d7984SSasha Levin return -EINVAL; 1886f7d7984SSasha Levin 1895cee9645SThomas Gleixner sys_tz = *tz; 1905cee9645SThomas Gleixner update_vsyscall_tz(); 1915cee9645SThomas Gleixner if (firsttime) { 1925cee9645SThomas Gleixner firsttime = 0; 1935cee9645SThomas Gleixner if (!tv) 194e0956dccSArnd Bergmann timekeeping_warp_clock(); 1955cee9645SThomas Gleixner } 1965cee9645SThomas Gleixner } 1975cee9645SThomas Gleixner if (tv) 19886d34732SBaolin Wang return do_settimeofday64(tv); 1995cee9645SThomas Gleixner return 0; 2005cee9645SThomas Gleixner } 2015cee9645SThomas Gleixner 2025cee9645SThomas Gleixner SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv, 2035cee9645SThomas Gleixner struct timezone __user *, tz) 2045cee9645SThomas Gleixner { 2052ac00f17SDeepa Dinamani struct timespec64 new_ts; 2065cee9645SThomas Gleixner struct timeval user_tv; 2075cee9645SThomas Gleixner struct timezone new_tz; 2085cee9645SThomas Gleixner 2095cee9645SThomas Gleixner if (tv) { 2105cee9645SThomas Gleixner if (copy_from_user(&user_tv, tv, sizeof(*tv))) 2115cee9645SThomas Gleixner return -EFAULT; 2126ada1fc0SSasha Levin 2136ada1fc0SSasha Levin if (!timeval_valid(&user_tv)) 2146ada1fc0SSasha Levin return -EINVAL; 2156ada1fc0SSasha Levin 2165cee9645SThomas Gleixner new_ts.tv_sec = user_tv.tv_sec; 2175cee9645SThomas Gleixner new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; 2185cee9645SThomas Gleixner } 2195cee9645SThomas Gleixner if (tz) { 2205cee9645SThomas Gleixner if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2215cee9645SThomas Gleixner return -EFAULT; 2225cee9645SThomas Gleixner } 2235cee9645SThomas Gleixner 2242ac00f17SDeepa Dinamani return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2255cee9645SThomas Gleixner } 2265cee9645SThomas Gleixner 2272b2d0285SAl Viro #ifdef CONFIG_COMPAT 2282b2d0285SAl Viro COMPAT_SYSCALL_DEFINE2(gettimeofday, struct compat_timeval __user *, tv, 2292b2d0285SAl Viro struct timezone __user *, tz) 2302b2d0285SAl Viro { 2312b2d0285SAl Viro if (tv) { 232*33e26418SArnd Bergmann struct timespec64 ts; 2332b2d0285SAl Viro 234*33e26418SArnd Bergmann ktime_get_real_ts64(&ts); 235*33e26418SArnd Bergmann if (put_user(ts.tv_sec, &tv->tv_sec) || 236*33e26418SArnd Bergmann put_user(ts.tv_nsec / 1000, &tv->tv_usec)) 2372b2d0285SAl Viro return -EFAULT; 2382b2d0285SAl Viro } 2392b2d0285SAl Viro if (tz) { 2402b2d0285SAl Viro if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 2412b2d0285SAl Viro return -EFAULT; 2422b2d0285SAl Viro } 2432b2d0285SAl Viro 2442b2d0285SAl Viro return 0; 2452b2d0285SAl Viro } 2462b2d0285SAl Viro 2472b2d0285SAl Viro COMPAT_SYSCALL_DEFINE2(settimeofday, struct compat_timeval __user *, tv, 2482b2d0285SAl Viro struct timezone __user *, tz) 2492b2d0285SAl Viro { 2502b2d0285SAl Viro struct timespec64 new_ts; 2512b2d0285SAl Viro struct timeval user_tv; 2522b2d0285SAl Viro struct timezone new_tz; 2532b2d0285SAl Viro 2542b2d0285SAl Viro if (tv) { 2552b2d0285SAl Viro if (compat_get_timeval(&user_tv, tv)) 2562b2d0285SAl Viro return -EFAULT; 2572b2d0285SAl Viro new_ts.tv_sec = user_tv.tv_sec; 2582b2d0285SAl Viro new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; 2592b2d0285SAl Viro } 2602b2d0285SAl Viro if (tz) { 2612b2d0285SAl Viro if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2622b2d0285SAl Viro return -EFAULT; 2632b2d0285SAl Viro } 2642b2d0285SAl Viro 2652b2d0285SAl Viro return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2662b2d0285SAl Viro } 2672b2d0285SAl Viro #endif 2682b2d0285SAl Viro 2695cee9645SThomas Gleixner SYSCALL_DEFINE1(adjtimex, struct timex __user *, txc_p) 2705cee9645SThomas Gleixner { 2715cee9645SThomas Gleixner struct timex txc; /* Local copy of parameter */ 2725cee9645SThomas Gleixner int ret; 2735cee9645SThomas Gleixner 2745cee9645SThomas Gleixner /* Copy the user data space into the kernel copy 2755cee9645SThomas Gleixner * structure. But bear in mind that the structures 2765cee9645SThomas Gleixner * may change 2775cee9645SThomas Gleixner */ 2785cee9645SThomas Gleixner if (copy_from_user(&txc, txc_p, sizeof(struct timex))) 2795cee9645SThomas Gleixner return -EFAULT; 2805cee9645SThomas Gleixner ret = do_adjtimex(&txc); 2815cee9645SThomas Gleixner return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret; 2825cee9645SThomas Gleixner } 2835cee9645SThomas Gleixner 2843a4d44b6SAl Viro #ifdef CONFIG_COMPAT 2853a4d44b6SAl Viro 2863a4d44b6SAl Viro COMPAT_SYSCALL_DEFINE1(adjtimex, struct compat_timex __user *, utp) 2873a4d44b6SAl Viro { 2883a4d44b6SAl Viro struct timex txc; 2893a4d44b6SAl Viro int err, ret; 2903a4d44b6SAl Viro 2913a4d44b6SAl Viro err = compat_get_timex(&txc, utp); 2923a4d44b6SAl Viro if (err) 2933a4d44b6SAl Viro return err; 2943a4d44b6SAl Viro 2953a4d44b6SAl Viro ret = do_adjtimex(&txc); 2963a4d44b6SAl Viro 2973a4d44b6SAl Viro err = compat_put_timex(utp, &txc); 2983a4d44b6SAl Viro if (err) 2993a4d44b6SAl Viro return err; 3003a4d44b6SAl Viro 3013a4d44b6SAl Viro return ret; 3023a4d44b6SAl Viro } 3033a4d44b6SAl Viro #endif 3043a4d44b6SAl Viro 3055cee9645SThomas Gleixner /* 3065cee9645SThomas Gleixner * Convert jiffies to milliseconds and back. 3075cee9645SThomas Gleixner * 3085cee9645SThomas Gleixner * Avoid unnecessary multiplications/divisions in the 3095cee9645SThomas Gleixner * two most common HZ cases: 3105cee9645SThomas Gleixner */ 3115cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j) 3125cee9645SThomas Gleixner { 3135cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 3145cee9645SThomas Gleixner return (MSEC_PER_SEC / HZ) * j; 3155cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 3165cee9645SThomas Gleixner return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); 3175cee9645SThomas Gleixner #else 3185cee9645SThomas Gleixner # if BITS_PER_LONG == 32 319abcbcb80SGeert Uytterhoeven return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >> 320abcbcb80SGeert Uytterhoeven HZ_TO_MSEC_SHR32; 3215cee9645SThomas Gleixner # else 322abcbcb80SGeert Uytterhoeven return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN); 3235cee9645SThomas Gleixner # endif 3245cee9645SThomas Gleixner #endif 3255cee9645SThomas Gleixner } 3265cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs); 3275cee9645SThomas Gleixner 3285cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j) 3295cee9645SThomas Gleixner { 330e0758676SFrederic Weisbecker /* 331e0758676SFrederic Weisbecker * Hz usually doesn't go much further MSEC_PER_SEC. 332e0758676SFrederic Weisbecker * jiffies_to_usecs() and usecs_to_jiffies() depend on that. 333e0758676SFrederic Weisbecker */ 334e0758676SFrederic Weisbecker BUILD_BUG_ON(HZ > USEC_PER_SEC); 335e0758676SFrederic Weisbecker 336e0758676SFrederic Weisbecker #if !(USEC_PER_SEC % HZ) 3375cee9645SThomas Gleixner return (USEC_PER_SEC / HZ) * j; 3385cee9645SThomas Gleixner #else 3395cee9645SThomas Gleixner # if BITS_PER_LONG == 32 3405cee9645SThomas Gleixner return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; 3415cee9645SThomas Gleixner # else 3425cee9645SThomas Gleixner return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; 3435cee9645SThomas Gleixner # endif 3445cee9645SThomas Gleixner #endif 3455cee9645SThomas Gleixner } 3465cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs); 3475cee9645SThomas Gleixner 34890b6ce9cSpang.xunlei /* 34990b6ce9cSpang.xunlei * mktime64 - Converts date to seconds. 35090b6ce9cSpang.xunlei * Converts Gregorian date to seconds since 1970-01-01 00:00:00. 3515cee9645SThomas Gleixner * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 3525cee9645SThomas Gleixner * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. 3535cee9645SThomas Gleixner * 3545cee9645SThomas Gleixner * [For the Julian calendar (which was used in Russia before 1917, 3555cee9645SThomas Gleixner * Britain & colonies before 1752, anywhere else before 1582, 3565cee9645SThomas Gleixner * and is still in use by some communities) leave out the 3575cee9645SThomas Gleixner * -year/100+year/400 terms, and add 10.] 3585cee9645SThomas Gleixner * 3595cee9645SThomas Gleixner * This algorithm was first published by Gauss (I think). 360ede5147dSDavid Howells * 361ede5147dSDavid Howells * A leap second can be indicated by calling this function with sec as 362ede5147dSDavid Howells * 60 (allowable under ISO 8601). The leap second is treated the same 363ede5147dSDavid Howells * as the following second since they don't exist in UNIX time. 364ede5147dSDavid Howells * 365ede5147dSDavid Howells * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight 366ede5147dSDavid Howells * tomorrow - (allowable under ISO 8601) is supported. 3675cee9645SThomas Gleixner */ 36890b6ce9cSpang.xunlei time64_t mktime64(const unsigned int year0, const unsigned int mon0, 3695cee9645SThomas Gleixner const unsigned int day, const unsigned int hour, 3705cee9645SThomas Gleixner const unsigned int min, const unsigned int sec) 3715cee9645SThomas Gleixner { 3725cee9645SThomas Gleixner unsigned int mon = mon0, year = year0; 3735cee9645SThomas Gleixner 3745cee9645SThomas Gleixner /* 1..12 -> 11,12,1..10 */ 3755cee9645SThomas Gleixner if (0 >= (int) (mon -= 2)) { 3765cee9645SThomas Gleixner mon += 12; /* Puts Feb last since it has leap day */ 3775cee9645SThomas Gleixner year -= 1; 3785cee9645SThomas Gleixner } 3795cee9645SThomas Gleixner 38090b6ce9cSpang.xunlei return ((((time64_t) 3815cee9645SThomas Gleixner (year/4 - year/100 + year/400 + 367*mon/12 + day) + 3825cee9645SThomas Gleixner year*365 - 719499 383ede5147dSDavid Howells )*24 + hour /* now have hours - midnight tomorrow handled here */ 3845cee9645SThomas Gleixner )*60 + min /* now have minutes */ 3855cee9645SThomas Gleixner )*60 + sec; /* finally seconds */ 3865cee9645SThomas Gleixner } 38790b6ce9cSpang.xunlei EXPORT_SYMBOL(mktime64); 3885cee9645SThomas Gleixner 3895cee9645SThomas Gleixner /** 3905cee9645SThomas Gleixner * set_normalized_timespec - set timespec sec and nsec parts and normalize 3915cee9645SThomas Gleixner * 3925cee9645SThomas Gleixner * @ts: pointer to timespec variable to be set 3935cee9645SThomas Gleixner * @sec: seconds to set 3945cee9645SThomas Gleixner * @nsec: nanoseconds to set 3955cee9645SThomas Gleixner * 3965cee9645SThomas Gleixner * Set seconds and nanoseconds field of a timespec variable and 3975cee9645SThomas Gleixner * normalize to the timespec storage format 3985cee9645SThomas Gleixner * 3995cee9645SThomas Gleixner * Note: The tv_nsec part is always in the range of 4005cee9645SThomas Gleixner * 0 <= tv_nsec < NSEC_PER_SEC 4015cee9645SThomas Gleixner * For negative values only the tv_sec field is negative ! 4025cee9645SThomas Gleixner */ 4035cee9645SThomas Gleixner void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec) 4045cee9645SThomas Gleixner { 4055cee9645SThomas Gleixner while (nsec >= NSEC_PER_SEC) { 4065cee9645SThomas Gleixner /* 4075cee9645SThomas Gleixner * The following asm() prevents the compiler from 4085cee9645SThomas Gleixner * optimising this loop into a modulo operation. See 4095cee9645SThomas Gleixner * also __iter_div_u64_rem() in include/linux/time.h 4105cee9645SThomas Gleixner */ 4115cee9645SThomas Gleixner asm("" : "+rm"(nsec)); 4125cee9645SThomas Gleixner nsec -= NSEC_PER_SEC; 4135cee9645SThomas Gleixner ++sec; 4145cee9645SThomas Gleixner } 4155cee9645SThomas Gleixner while (nsec < 0) { 4165cee9645SThomas Gleixner asm("" : "+rm"(nsec)); 4175cee9645SThomas Gleixner nsec += NSEC_PER_SEC; 4185cee9645SThomas Gleixner --sec; 4195cee9645SThomas Gleixner } 4205cee9645SThomas Gleixner ts->tv_sec = sec; 4215cee9645SThomas Gleixner ts->tv_nsec = nsec; 4225cee9645SThomas Gleixner } 4235cee9645SThomas Gleixner EXPORT_SYMBOL(set_normalized_timespec); 4245cee9645SThomas Gleixner 4255cee9645SThomas Gleixner /** 4265cee9645SThomas Gleixner * ns_to_timespec - Convert nanoseconds to timespec 4275cee9645SThomas Gleixner * @nsec: the nanoseconds value to be converted 4285cee9645SThomas Gleixner * 4295cee9645SThomas Gleixner * Returns the timespec representation of the nsec parameter. 4305cee9645SThomas Gleixner */ 4315cee9645SThomas Gleixner struct timespec ns_to_timespec(const s64 nsec) 4325cee9645SThomas Gleixner { 4335cee9645SThomas Gleixner struct timespec ts; 4345cee9645SThomas Gleixner s32 rem; 4355cee9645SThomas Gleixner 4365cee9645SThomas Gleixner if (!nsec) 4375cee9645SThomas Gleixner return (struct timespec) {0, 0}; 4385cee9645SThomas Gleixner 4395cee9645SThomas Gleixner ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); 4405cee9645SThomas Gleixner if (unlikely(rem < 0)) { 4415cee9645SThomas Gleixner ts.tv_sec--; 4425cee9645SThomas Gleixner rem += NSEC_PER_SEC; 4435cee9645SThomas Gleixner } 4445cee9645SThomas Gleixner ts.tv_nsec = rem; 4455cee9645SThomas Gleixner 4465cee9645SThomas Gleixner return ts; 4475cee9645SThomas Gleixner } 4485cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timespec); 4495cee9645SThomas Gleixner 4505cee9645SThomas Gleixner /** 4515cee9645SThomas Gleixner * ns_to_timeval - Convert nanoseconds to timeval 4525cee9645SThomas Gleixner * @nsec: the nanoseconds value to be converted 4535cee9645SThomas Gleixner * 4545cee9645SThomas Gleixner * Returns the timeval representation of the nsec parameter. 4555cee9645SThomas Gleixner */ 4565cee9645SThomas Gleixner struct timeval ns_to_timeval(const s64 nsec) 4575cee9645SThomas Gleixner { 4585cee9645SThomas Gleixner struct timespec ts = ns_to_timespec(nsec); 4595cee9645SThomas Gleixner struct timeval tv; 4605cee9645SThomas Gleixner 4615cee9645SThomas Gleixner tv.tv_sec = ts.tv_sec; 4625cee9645SThomas Gleixner tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; 4635cee9645SThomas Gleixner 4645cee9645SThomas Gleixner return tv; 4655cee9645SThomas Gleixner } 4665cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timeval); 4675cee9645SThomas Gleixner 468a84d1169SArnd Bergmann struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec) 469a84d1169SArnd Bergmann { 470a84d1169SArnd Bergmann struct timespec64 ts = ns_to_timespec64(nsec); 471a84d1169SArnd Bergmann struct __kernel_old_timeval tv; 472a84d1169SArnd Bergmann 473a84d1169SArnd Bergmann tv.tv_sec = ts.tv_sec; 474a84d1169SArnd Bergmann tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000; 475a84d1169SArnd Bergmann 476a84d1169SArnd Bergmann return tv; 477a84d1169SArnd Bergmann } 478a84d1169SArnd Bergmann EXPORT_SYMBOL(ns_to_kernel_old_timeval); 479a84d1169SArnd Bergmann 48049cd6f86SJohn Stultz /** 48149cd6f86SJohn Stultz * set_normalized_timespec - set timespec sec and nsec parts and normalize 48249cd6f86SJohn Stultz * 48349cd6f86SJohn Stultz * @ts: pointer to timespec variable to be set 48449cd6f86SJohn Stultz * @sec: seconds to set 48549cd6f86SJohn Stultz * @nsec: nanoseconds to set 48649cd6f86SJohn Stultz * 48749cd6f86SJohn Stultz * Set seconds and nanoseconds field of a timespec variable and 48849cd6f86SJohn Stultz * normalize to the timespec storage format 48949cd6f86SJohn Stultz * 49049cd6f86SJohn Stultz * Note: The tv_nsec part is always in the range of 49149cd6f86SJohn Stultz * 0 <= tv_nsec < NSEC_PER_SEC 49249cd6f86SJohn Stultz * For negative values only the tv_sec field is negative ! 49349cd6f86SJohn Stultz */ 49449cd6f86SJohn Stultz void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) 49549cd6f86SJohn Stultz { 49649cd6f86SJohn Stultz while (nsec >= NSEC_PER_SEC) { 49749cd6f86SJohn Stultz /* 49849cd6f86SJohn Stultz * The following asm() prevents the compiler from 49949cd6f86SJohn Stultz * optimising this loop into a modulo operation. See 50049cd6f86SJohn Stultz * also __iter_div_u64_rem() in include/linux/time.h 50149cd6f86SJohn Stultz */ 50249cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 50349cd6f86SJohn Stultz nsec -= NSEC_PER_SEC; 50449cd6f86SJohn Stultz ++sec; 50549cd6f86SJohn Stultz } 50649cd6f86SJohn Stultz while (nsec < 0) { 50749cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 50849cd6f86SJohn Stultz nsec += NSEC_PER_SEC; 50949cd6f86SJohn Stultz --sec; 51049cd6f86SJohn Stultz } 51149cd6f86SJohn Stultz ts->tv_sec = sec; 51249cd6f86SJohn Stultz ts->tv_nsec = nsec; 51349cd6f86SJohn Stultz } 51449cd6f86SJohn Stultz EXPORT_SYMBOL(set_normalized_timespec64); 51549cd6f86SJohn Stultz 51649cd6f86SJohn Stultz /** 51749cd6f86SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 51849cd6f86SJohn Stultz * @nsec: the nanoseconds value to be converted 51949cd6f86SJohn Stultz * 52049cd6f86SJohn Stultz * Returns the timespec64 representation of the nsec parameter. 52149cd6f86SJohn Stultz */ 52249cd6f86SJohn Stultz struct timespec64 ns_to_timespec64(const s64 nsec) 52349cd6f86SJohn Stultz { 52449cd6f86SJohn Stultz struct timespec64 ts; 52549cd6f86SJohn Stultz s32 rem; 52649cd6f86SJohn Stultz 52749cd6f86SJohn Stultz if (!nsec) 52849cd6f86SJohn Stultz return (struct timespec64) {0, 0}; 52949cd6f86SJohn Stultz 53049cd6f86SJohn Stultz ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); 53149cd6f86SJohn Stultz if (unlikely(rem < 0)) { 53249cd6f86SJohn Stultz ts.tv_sec--; 53349cd6f86SJohn Stultz rem += NSEC_PER_SEC; 53449cd6f86SJohn Stultz } 53549cd6f86SJohn Stultz ts.tv_nsec = rem; 53649cd6f86SJohn Stultz 53749cd6f86SJohn Stultz return ts; 53849cd6f86SJohn Stultz } 53949cd6f86SJohn Stultz EXPORT_SYMBOL(ns_to_timespec64); 540abc8f96eSArnd Bergmann 541ca42aaf0SNicholas Mc Guire /** 542ca42aaf0SNicholas Mc Guire * msecs_to_jiffies: - convert milliseconds to jiffies 543ca42aaf0SNicholas Mc Guire * @m: time in milliseconds 544ca42aaf0SNicholas Mc Guire * 545ca42aaf0SNicholas Mc Guire * conversion is done as follows: 5465cee9645SThomas Gleixner * 5475cee9645SThomas Gleixner * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) 5485cee9645SThomas Gleixner * 5495cee9645SThomas Gleixner * - 'too large' values [that would result in larger than 5505cee9645SThomas Gleixner * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. 5515cee9645SThomas Gleixner * 5525cee9645SThomas Gleixner * - all other values are converted to jiffies by either multiplying 553ca42aaf0SNicholas Mc Guire * the input value by a factor or dividing it with a factor and 554ca42aaf0SNicholas Mc Guire * handling any 32-bit overflows. 555ca42aaf0SNicholas Mc Guire * for the details see __msecs_to_jiffies() 5565cee9645SThomas Gleixner * 557ca42aaf0SNicholas Mc Guire * msecs_to_jiffies() checks for the passed in value being a constant 558ca42aaf0SNicholas Mc Guire * via __builtin_constant_p() allowing gcc to eliminate most of the 559ca42aaf0SNicholas Mc Guire * code, __msecs_to_jiffies() is called if the value passed does not 560ca42aaf0SNicholas Mc Guire * allow constant folding and the actual conversion must be done at 561ca42aaf0SNicholas Mc Guire * runtime. 562ca42aaf0SNicholas Mc Guire * the _msecs_to_jiffies helpers are the HZ dependent conversion 563ca42aaf0SNicholas Mc Guire * routines found in include/linux/jiffies.h 5645cee9645SThomas Gleixner */ 565ca42aaf0SNicholas Mc Guire unsigned long __msecs_to_jiffies(const unsigned int m) 5665cee9645SThomas Gleixner { 5675cee9645SThomas Gleixner /* 5685cee9645SThomas Gleixner * Negative value, means infinite timeout: 5695cee9645SThomas Gleixner */ 5705cee9645SThomas Gleixner if ((int)m < 0) 5715cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 572ca42aaf0SNicholas Mc Guire return _msecs_to_jiffies(m); 5735cee9645SThomas Gleixner } 574ca42aaf0SNicholas Mc Guire EXPORT_SYMBOL(__msecs_to_jiffies); 5755cee9645SThomas Gleixner 576ae60d6a0SNicholas Mc Guire unsigned long __usecs_to_jiffies(const unsigned int u) 5775cee9645SThomas Gleixner { 5785cee9645SThomas Gleixner if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) 5795cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 580ae60d6a0SNicholas Mc Guire return _usecs_to_jiffies(u); 5815cee9645SThomas Gleixner } 582ae60d6a0SNicholas Mc Guire EXPORT_SYMBOL(__usecs_to_jiffies); 5835cee9645SThomas Gleixner 5845cee9645SThomas Gleixner /* 5855cee9645SThomas Gleixner * The TICK_NSEC - 1 rounds up the value to the next resolution. Note 5865cee9645SThomas Gleixner * that a remainder subtract here would not do the right thing as the 5875cee9645SThomas Gleixner * resolution values don't fall on second boundries. I.e. the line: 5885cee9645SThomas Gleixner * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. 589d78c9300SAndrew Hunter * Note that due to the small error in the multiplier here, this 590d78c9300SAndrew Hunter * rounding is incorrect for sufficiently large values of tv_nsec, but 591d78c9300SAndrew Hunter * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're 592d78c9300SAndrew Hunter * OK. 5935cee9645SThomas Gleixner * 5945cee9645SThomas Gleixner * Rather, we just shift the bits off the right. 5955cee9645SThomas Gleixner * 5965cee9645SThomas Gleixner * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec 5975cee9645SThomas Gleixner * value to a scaled second value. 5985cee9645SThomas Gleixner */ 599d78c9300SAndrew Hunter static unsigned long 6009ca30850SBaolin Wang __timespec64_to_jiffies(u64 sec, long nsec) 6015cee9645SThomas Gleixner { 602d78c9300SAndrew Hunter nsec = nsec + TICK_NSEC - 1; 6035cee9645SThomas Gleixner 6045cee9645SThomas Gleixner if (sec >= MAX_SEC_IN_JIFFIES){ 6055cee9645SThomas Gleixner sec = MAX_SEC_IN_JIFFIES; 6065cee9645SThomas Gleixner nsec = 0; 6075cee9645SThomas Gleixner } 6089ca30850SBaolin Wang return ((sec * SEC_CONVERSION) + 6095cee9645SThomas Gleixner (((u64)nsec * NSEC_CONVERSION) >> 6105cee9645SThomas Gleixner (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; 6115cee9645SThomas Gleixner 6125cee9645SThomas Gleixner } 613d78c9300SAndrew Hunter 6149ca30850SBaolin Wang static unsigned long 6159ca30850SBaolin Wang __timespec_to_jiffies(unsigned long sec, long nsec) 616d78c9300SAndrew Hunter { 6179ca30850SBaolin Wang return __timespec64_to_jiffies((u64)sec, nsec); 618d78c9300SAndrew Hunter } 619d78c9300SAndrew Hunter 6209ca30850SBaolin Wang unsigned long 6219ca30850SBaolin Wang timespec64_to_jiffies(const struct timespec64 *value) 6229ca30850SBaolin Wang { 6239ca30850SBaolin Wang return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); 6249ca30850SBaolin Wang } 6259ca30850SBaolin Wang EXPORT_SYMBOL(timespec64_to_jiffies); 6265cee9645SThomas Gleixner 6275cee9645SThomas Gleixner void 6289ca30850SBaolin Wang jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) 6295cee9645SThomas Gleixner { 6305cee9645SThomas Gleixner /* 6315cee9645SThomas Gleixner * Convert jiffies to nanoseconds and separate with 6325cee9645SThomas Gleixner * one divide. 6335cee9645SThomas Gleixner */ 6345cee9645SThomas Gleixner u32 rem; 6355cee9645SThomas Gleixner value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 6365cee9645SThomas Gleixner NSEC_PER_SEC, &rem); 6375cee9645SThomas Gleixner value->tv_nsec = rem; 6385cee9645SThomas Gleixner } 6399ca30850SBaolin Wang EXPORT_SYMBOL(jiffies_to_timespec64); 6405cee9645SThomas Gleixner 641d78c9300SAndrew Hunter /* 642d78c9300SAndrew Hunter * We could use a similar algorithm to timespec_to_jiffies (with a 643d78c9300SAndrew Hunter * different multiplier for usec instead of nsec). But this has a 644d78c9300SAndrew Hunter * problem with rounding: we can't exactly add TICK_NSEC - 1 to the 645d78c9300SAndrew Hunter * usec value, since it's not necessarily integral. 6465cee9645SThomas Gleixner * 647d78c9300SAndrew Hunter * We could instead round in the intermediate scaled representation 648d78c9300SAndrew Hunter * (i.e. in units of 1/2^(large scale) jiffies) but that's also 649d78c9300SAndrew Hunter * perilous: the scaling introduces a small positive error, which 650d78c9300SAndrew Hunter * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 651d78c9300SAndrew Hunter * units to the intermediate before shifting) leads to accidental 652d78c9300SAndrew Hunter * overflow and overestimates. 653d78c9300SAndrew Hunter * 654d78c9300SAndrew Hunter * At the cost of one additional multiplication by a constant, just 655d78c9300SAndrew Hunter * use the timespec implementation. 6565cee9645SThomas Gleixner */ 6575cee9645SThomas Gleixner unsigned long 6585cee9645SThomas Gleixner timeval_to_jiffies(const struct timeval *value) 6595cee9645SThomas Gleixner { 660d78c9300SAndrew Hunter return __timespec_to_jiffies(value->tv_sec, 661d78c9300SAndrew Hunter value->tv_usec * NSEC_PER_USEC); 6625cee9645SThomas Gleixner } 6635cee9645SThomas Gleixner EXPORT_SYMBOL(timeval_to_jiffies); 6645cee9645SThomas Gleixner 6655cee9645SThomas Gleixner void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) 6665cee9645SThomas Gleixner { 6675cee9645SThomas Gleixner /* 6685cee9645SThomas Gleixner * Convert jiffies to nanoseconds and separate with 6695cee9645SThomas Gleixner * one divide. 6705cee9645SThomas Gleixner */ 6715cee9645SThomas Gleixner u32 rem; 6725cee9645SThomas Gleixner 6735cee9645SThomas Gleixner value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 6745cee9645SThomas Gleixner NSEC_PER_SEC, &rem); 6755cee9645SThomas Gleixner value->tv_usec = rem / NSEC_PER_USEC; 6765cee9645SThomas Gleixner } 6775cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timeval); 6785cee9645SThomas Gleixner 6795cee9645SThomas Gleixner /* 6805cee9645SThomas Gleixner * Convert jiffies/jiffies_64 to clock_t and back. 6815cee9645SThomas Gleixner */ 6825cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x) 6835cee9645SThomas Gleixner { 6845cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 6855cee9645SThomas Gleixner # if HZ < USER_HZ 6865cee9645SThomas Gleixner return x * (USER_HZ / HZ); 6875cee9645SThomas Gleixner # else 6885cee9645SThomas Gleixner return x / (HZ / USER_HZ); 6895cee9645SThomas Gleixner # endif 6905cee9645SThomas Gleixner #else 6915cee9645SThomas Gleixner return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); 6925cee9645SThomas Gleixner #endif 6935cee9645SThomas Gleixner } 6945cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t); 6955cee9645SThomas Gleixner 6965cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x) 6975cee9645SThomas Gleixner { 6985cee9645SThomas Gleixner #if (HZ % USER_HZ)==0 6995cee9645SThomas Gleixner if (x >= ~0UL / (HZ / USER_HZ)) 7005cee9645SThomas Gleixner return ~0UL; 7015cee9645SThomas Gleixner return x * (HZ / USER_HZ); 7025cee9645SThomas Gleixner #else 7035cee9645SThomas Gleixner /* Don't worry about loss of precision here .. */ 7045cee9645SThomas Gleixner if (x >= ~0UL / HZ * USER_HZ) 7055cee9645SThomas Gleixner return ~0UL; 7065cee9645SThomas Gleixner 7075cee9645SThomas Gleixner /* .. but do try to contain it here */ 7085cee9645SThomas Gleixner return div_u64((u64)x * HZ, USER_HZ); 7095cee9645SThomas Gleixner #endif 7105cee9645SThomas Gleixner } 7115cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies); 7125cee9645SThomas Gleixner 7135cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x) 7145cee9645SThomas Gleixner { 7155cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 7165cee9645SThomas Gleixner # if HZ < USER_HZ 7175cee9645SThomas Gleixner x = div_u64(x * USER_HZ, HZ); 7185cee9645SThomas Gleixner # elif HZ > USER_HZ 7195cee9645SThomas Gleixner x = div_u64(x, HZ / USER_HZ); 7205cee9645SThomas Gleixner # else 7215cee9645SThomas Gleixner /* Nothing to do */ 7225cee9645SThomas Gleixner # endif 7235cee9645SThomas Gleixner #else 7245cee9645SThomas Gleixner /* 7255cee9645SThomas Gleixner * There are better ways that don't overflow early, 7265cee9645SThomas Gleixner * but even this doesn't overflow in hundreds of years 7275cee9645SThomas Gleixner * in 64 bits, so.. 7285cee9645SThomas Gleixner */ 7295cee9645SThomas Gleixner x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); 7305cee9645SThomas Gleixner #endif 7315cee9645SThomas Gleixner return x; 7325cee9645SThomas Gleixner } 7335cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t); 7345cee9645SThomas Gleixner 7355cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x) 7365cee9645SThomas Gleixner { 7375cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0 7385cee9645SThomas Gleixner return div_u64(x, NSEC_PER_SEC / USER_HZ); 7395cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0 7405cee9645SThomas Gleixner return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); 7415cee9645SThomas Gleixner #else 7425cee9645SThomas Gleixner /* 7435cee9645SThomas Gleixner * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, 7445cee9645SThomas Gleixner * overflow after 64.99 years. 7455cee9645SThomas Gleixner * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... 7465cee9645SThomas Gleixner */ 7475cee9645SThomas Gleixner return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); 7485cee9645SThomas Gleixner #endif 7495cee9645SThomas Gleixner } 7505cee9645SThomas Gleixner 75107e5f5e3SFrederic Weisbecker u64 jiffies64_to_nsecs(u64 j) 75207e5f5e3SFrederic Weisbecker { 75307e5f5e3SFrederic Weisbecker #if !(NSEC_PER_SEC % HZ) 75407e5f5e3SFrederic Weisbecker return (NSEC_PER_SEC / HZ) * j; 75507e5f5e3SFrederic Weisbecker # else 75607e5f5e3SFrederic Weisbecker return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN); 75707e5f5e3SFrederic Weisbecker #endif 75807e5f5e3SFrederic Weisbecker } 75907e5f5e3SFrederic Weisbecker EXPORT_SYMBOL(jiffies64_to_nsecs); 76007e5f5e3SFrederic Weisbecker 7615cee9645SThomas Gleixner /** 7625cee9645SThomas Gleixner * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 7635cee9645SThomas Gleixner * 7645cee9645SThomas Gleixner * @n: nsecs in u64 7655cee9645SThomas Gleixner * 7665cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 7675cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 7685cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 7695cee9645SThomas Gleixner * 7705cee9645SThomas Gleixner * note: 7715cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 7725cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 7735cee9645SThomas Gleixner */ 7745cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n) 7755cee9645SThomas Gleixner { 7765cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0 7775cee9645SThomas Gleixner /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ 7785cee9645SThomas Gleixner return div_u64(n, NSEC_PER_SEC / HZ); 7795cee9645SThomas Gleixner #elif (HZ % 512) == 0 7805cee9645SThomas Gleixner /* overflow after 292 years if HZ = 1024 */ 7815cee9645SThomas Gleixner return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); 7825cee9645SThomas Gleixner #else 7835cee9645SThomas Gleixner /* 7845cee9645SThomas Gleixner * Generic case - optimized for cases where HZ is a multiple of 3. 7855cee9645SThomas Gleixner * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. 7865cee9645SThomas Gleixner */ 7875cee9645SThomas Gleixner return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); 7885cee9645SThomas Gleixner #endif 7895cee9645SThomas Gleixner } 7907bd0e226SDaniel Vetter EXPORT_SYMBOL(nsecs_to_jiffies64); 7915cee9645SThomas Gleixner 7925cee9645SThomas Gleixner /** 7935cee9645SThomas Gleixner * nsecs_to_jiffies - Convert nsecs in u64 to jiffies 7945cee9645SThomas Gleixner * 7955cee9645SThomas Gleixner * @n: nsecs in u64 7965cee9645SThomas Gleixner * 7975cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 7985cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 7995cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 8005cee9645SThomas Gleixner * 8015cee9645SThomas Gleixner * note: 8025cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 8035cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 8045cee9645SThomas Gleixner */ 8055cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n) 8065cee9645SThomas Gleixner { 8075cee9645SThomas Gleixner return (unsigned long)nsecs_to_jiffies64(n); 8085cee9645SThomas Gleixner } 809d560fed6SThomas Gleixner EXPORT_SYMBOL_GPL(nsecs_to_jiffies); 8105cee9645SThomas Gleixner 8115cee9645SThomas Gleixner /* 812bc2c53e5SDeepa Dinamani * Add two timespec64 values and do a safety check for overflow. 813bc2c53e5SDeepa Dinamani * It's assumed that both values are valid (>= 0). 814bc2c53e5SDeepa Dinamani * And, each timespec64 is in normalized form. 815bc2c53e5SDeepa Dinamani */ 816bc2c53e5SDeepa Dinamani struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 817bc2c53e5SDeepa Dinamani const struct timespec64 rhs) 818bc2c53e5SDeepa Dinamani { 819bc2c53e5SDeepa Dinamani struct timespec64 res; 820bc2c53e5SDeepa Dinamani 821469e857fSVegard Nossum set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec, 822bc2c53e5SDeepa Dinamani lhs.tv_nsec + rhs.tv_nsec); 823bc2c53e5SDeepa Dinamani 824bc2c53e5SDeepa Dinamani if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) { 825bc2c53e5SDeepa Dinamani res.tv_sec = TIME64_MAX; 826bc2c53e5SDeepa Dinamani res.tv_nsec = 0; 827bc2c53e5SDeepa Dinamani } 828bc2c53e5SDeepa Dinamani 829bc2c53e5SDeepa Dinamani return res; 830bc2c53e5SDeepa Dinamani } 831f59dd9c8SDeepa Dinamani 832f59dd9c8SDeepa Dinamani int get_timespec64(struct timespec64 *ts, 833ea2ce8f3SDeepa Dinamani const struct __kernel_timespec __user *uts) 834f59dd9c8SDeepa Dinamani { 835ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts; 836f59dd9c8SDeepa Dinamani int ret; 837f59dd9c8SDeepa Dinamani 838f59dd9c8SDeepa Dinamani ret = copy_from_user(&kts, uts, sizeof(kts)); 839f59dd9c8SDeepa Dinamani if (ret) 840f59dd9c8SDeepa Dinamani return -EFAULT; 841f59dd9c8SDeepa Dinamani 842f59dd9c8SDeepa Dinamani ts->tv_sec = kts.tv_sec; 843ea2ce8f3SDeepa Dinamani 844ea2ce8f3SDeepa Dinamani /* Zero out the padding for 32 bit systems or in compat mode */ 845ea2ce8f3SDeepa Dinamani if (IS_ENABLED(CONFIG_64BIT_TIME) && (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall())) 846ea2ce8f3SDeepa Dinamani kts.tv_nsec &= 0xFFFFFFFFUL; 847ea2ce8f3SDeepa Dinamani 848f59dd9c8SDeepa Dinamani ts->tv_nsec = kts.tv_nsec; 849f59dd9c8SDeepa Dinamani 850f59dd9c8SDeepa Dinamani return 0; 851f59dd9c8SDeepa Dinamani } 852f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(get_timespec64); 853f59dd9c8SDeepa Dinamani 854f59dd9c8SDeepa Dinamani int put_timespec64(const struct timespec64 *ts, 855ea2ce8f3SDeepa Dinamani struct __kernel_timespec __user *uts) 856f59dd9c8SDeepa Dinamani { 857ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts = { 858f59dd9c8SDeepa Dinamani .tv_sec = ts->tv_sec, 859f59dd9c8SDeepa Dinamani .tv_nsec = ts->tv_nsec 860f59dd9c8SDeepa Dinamani }; 861ea2ce8f3SDeepa Dinamani 862f59dd9c8SDeepa Dinamani return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0; 863f59dd9c8SDeepa Dinamani } 864f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(put_timespec64); 865d5b7ffbfSDeepa Dinamani 8661c68adf6SDeepa Dinamani int __compat_get_timespec64(struct timespec64 *ts64, 8671c68adf6SDeepa Dinamani const struct compat_timespec __user *cts) 8681c68adf6SDeepa Dinamani { 8691c68adf6SDeepa Dinamani struct compat_timespec ts; 8701c68adf6SDeepa Dinamani int ret; 8711c68adf6SDeepa Dinamani 8721c68adf6SDeepa Dinamani ret = copy_from_user(&ts, cts, sizeof(ts)); 8731c68adf6SDeepa Dinamani if (ret) 8741c68adf6SDeepa Dinamani return -EFAULT; 8751c68adf6SDeepa Dinamani 8761c68adf6SDeepa Dinamani ts64->tv_sec = ts.tv_sec; 8771c68adf6SDeepa Dinamani ts64->tv_nsec = ts.tv_nsec; 8781c68adf6SDeepa Dinamani 8791c68adf6SDeepa Dinamani return 0; 8801c68adf6SDeepa Dinamani } 8811c68adf6SDeepa Dinamani 8821c68adf6SDeepa Dinamani int __compat_put_timespec64(const struct timespec64 *ts64, 8831c68adf6SDeepa Dinamani struct compat_timespec __user *cts) 8841c68adf6SDeepa Dinamani { 8851c68adf6SDeepa Dinamani struct compat_timespec ts = { 8861c68adf6SDeepa Dinamani .tv_sec = ts64->tv_sec, 8871c68adf6SDeepa Dinamani .tv_nsec = ts64->tv_nsec 8881c68adf6SDeepa Dinamani }; 8891c68adf6SDeepa Dinamani return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0; 8901c68adf6SDeepa Dinamani } 8911c68adf6SDeepa Dinamani 8921c68adf6SDeepa Dinamani int compat_get_timespec64(struct timespec64 *ts, const void __user *uts) 8931c68adf6SDeepa Dinamani { 8941c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 8951c68adf6SDeepa Dinamani return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; 8961c68adf6SDeepa Dinamani else 8971c68adf6SDeepa Dinamani return __compat_get_timespec64(ts, uts); 8981c68adf6SDeepa Dinamani } 8991c68adf6SDeepa Dinamani EXPORT_SYMBOL_GPL(compat_get_timespec64); 9001c68adf6SDeepa Dinamani 9011c68adf6SDeepa Dinamani int compat_put_timespec64(const struct timespec64 *ts, void __user *uts) 9021c68adf6SDeepa Dinamani { 9031c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 9041c68adf6SDeepa Dinamani return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; 9051c68adf6SDeepa Dinamani else 9061c68adf6SDeepa Dinamani return __compat_put_timespec64(ts, uts); 9071c68adf6SDeepa Dinamani } 9081c68adf6SDeepa Dinamani EXPORT_SYMBOL_GPL(compat_put_timespec64); 9091c68adf6SDeepa Dinamani 910d5b7ffbfSDeepa Dinamani int get_itimerspec64(struct itimerspec64 *it, 911d0dd63a8SDeepa Dinamani const struct __kernel_itimerspec __user *uit) 912d5b7ffbfSDeepa Dinamani { 913d5b7ffbfSDeepa Dinamani int ret; 914d5b7ffbfSDeepa Dinamani 915d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_interval, &uit->it_interval); 916d5b7ffbfSDeepa Dinamani if (ret) 917d5b7ffbfSDeepa Dinamani return ret; 918d5b7ffbfSDeepa Dinamani 919d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_value, &uit->it_value); 920d5b7ffbfSDeepa Dinamani 921d5b7ffbfSDeepa Dinamani return ret; 922d5b7ffbfSDeepa Dinamani } 923d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(get_itimerspec64); 924d5b7ffbfSDeepa Dinamani 925d5b7ffbfSDeepa Dinamani int put_itimerspec64(const struct itimerspec64 *it, 926d0dd63a8SDeepa Dinamani struct __kernel_itimerspec __user *uit) 927d5b7ffbfSDeepa Dinamani { 928d5b7ffbfSDeepa Dinamani int ret; 929d5b7ffbfSDeepa Dinamani 930d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_interval, &uit->it_interval); 931d5b7ffbfSDeepa Dinamani if (ret) 932d5b7ffbfSDeepa Dinamani return ret; 933d5b7ffbfSDeepa Dinamani 934d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_value, &uit->it_value); 935d5b7ffbfSDeepa Dinamani 936d5b7ffbfSDeepa Dinamani return ret; 937d5b7ffbfSDeepa Dinamani } 938d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(put_itimerspec64); 939afef05cfSDeepa Dinamani 940afef05cfSDeepa Dinamani int get_compat_itimerspec64(struct itimerspec64 *its, 941afef05cfSDeepa Dinamani const struct compat_itimerspec __user *uits) 942afef05cfSDeepa Dinamani { 943afef05cfSDeepa Dinamani 944afef05cfSDeepa Dinamani if (__compat_get_timespec64(&its->it_interval, &uits->it_interval) || 945afef05cfSDeepa Dinamani __compat_get_timespec64(&its->it_value, &uits->it_value)) 946afef05cfSDeepa Dinamani return -EFAULT; 947afef05cfSDeepa Dinamani return 0; 948afef05cfSDeepa Dinamani } 949afef05cfSDeepa Dinamani EXPORT_SYMBOL_GPL(get_compat_itimerspec64); 950afef05cfSDeepa Dinamani 951afef05cfSDeepa Dinamani int put_compat_itimerspec64(const struct itimerspec64 *its, 952afef05cfSDeepa Dinamani struct compat_itimerspec __user *uits) 953afef05cfSDeepa Dinamani { 954afef05cfSDeepa Dinamani if (__compat_put_timespec64(&its->it_interval, &uits->it_interval) || 955afef05cfSDeepa Dinamani __compat_put_timespec64(&its->it_value, &uits->it_value)) 956afef05cfSDeepa Dinamani return -EFAULT; 957afef05cfSDeepa Dinamani return 0; 958afef05cfSDeepa Dinamani } 959afef05cfSDeepa Dinamani EXPORT_SYMBOL_GPL(put_compat_itimerspec64); 960