135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0 25cee9645SThomas Gleixner /* 35cee9645SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds 45cee9645SThomas Gleixner * 558c5fc2bSThomas Gleixner * This file contains the interface functions for the various time related 658c5fc2bSThomas Gleixner * system calls: time, stime, gettimeofday, settimeofday, adjtime 758c5fc2bSThomas Gleixner * 858c5fc2bSThomas Gleixner * Modification history: 95cee9645SThomas Gleixner * 105cee9645SThomas Gleixner * 1993-09-02 Philip Gladstone 115cee9645SThomas Gleixner * Created file with time related functions from sched/core.c and adjtimex() 125cee9645SThomas Gleixner * 1993-10-08 Torsten Duwe 135cee9645SThomas Gleixner * adjtime interface update and CMOS clock write code 145cee9645SThomas Gleixner * 1995-08-13 Torsten Duwe 155cee9645SThomas Gleixner * kernel PLL updated to 1994-12-13 specs (rfc-1589) 165cee9645SThomas Gleixner * 1999-01-16 Ulrich Windl 175cee9645SThomas Gleixner * Introduced error checking for many cases in adjtimex(). 185cee9645SThomas Gleixner * Updated NTP code according to technical memorandum Jan '96 195cee9645SThomas Gleixner * "A Kernel Model for Precision Timekeeping" by Dave Mills 205cee9645SThomas Gleixner * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10) 215cee9645SThomas Gleixner * (Even though the technical memorandum forbids it) 225cee9645SThomas Gleixner * 2004-07-14 Christoph Lameter 235cee9645SThomas Gleixner * Added getnstimeofday to allow the posix timer functions to return 245cee9645SThomas Gleixner * with nanosecond accuracy 255cee9645SThomas Gleixner */ 265cee9645SThomas Gleixner 275cee9645SThomas Gleixner #include <linux/export.h> 28abcbcb80SGeert Uytterhoeven #include <linux/kernel.h> 295cee9645SThomas Gleixner #include <linux/timex.h> 305cee9645SThomas Gleixner #include <linux/capability.h> 315cee9645SThomas Gleixner #include <linux/timekeeper_internal.h> 325cee9645SThomas Gleixner #include <linux/errno.h> 335cee9645SThomas Gleixner #include <linux/syscalls.h> 345cee9645SThomas Gleixner #include <linux/security.h> 355cee9645SThomas Gleixner #include <linux/fs.h> 365cee9645SThomas Gleixner #include <linux/math64.h> 375cee9645SThomas Gleixner #include <linux/ptrace.h> 385cee9645SThomas Gleixner 397c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 403a4d44b6SAl Viro #include <linux/compat.h> 415cee9645SThomas Gleixner #include <asm/unistd.h> 425cee9645SThomas Gleixner 430a227985SNicholas Mc Guire #include <generated/timeconst.h> 448b094cd0SThomas Gleixner #include "timekeeping.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 { 64f5a89295SArnd Bergmann time_t i = (time_t)ktime_get_real_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 { 834eb1bca1SArnd Bergmann struct timespec64 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 914eb1bca1SArnd Bergmann err = security_settime64(&tv, NULL); 925cee9645SThomas Gleixner if (err) 935cee9645SThomas Gleixner return err; 945cee9645SThomas Gleixner 954eb1bca1SArnd Bergmann do_settimeofday64(&tv); 965cee9645SThomas Gleixner return 0; 975cee9645SThomas Gleixner } 985cee9645SThomas Gleixner 995cee9645SThomas Gleixner #endif /* __ARCH_WANT_SYS_TIME */ 1005cee9645SThomas Gleixner 1018dabe724SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME 102d33c577cSArnd Bergmann #ifdef __ARCH_WANT_SYS_TIME32 103b180db2cSAl Viro 1049afc5eeeSArnd Bergmann /* old_time32_t is a 32 bit "long" and needs to get converted. */ 1058dabe724SArnd Bergmann SYSCALL_DEFINE1(time32, old_time32_t __user *, tloc) 106b180db2cSAl Viro { 1079afc5eeeSArnd Bergmann old_time32_t i; 108b180db2cSAl Viro 1099afc5eeeSArnd Bergmann i = (old_time32_t)ktime_get_real_seconds(); 110b180db2cSAl Viro 111b180db2cSAl Viro if (tloc) { 112b180db2cSAl Viro if (put_user(i,tloc)) 113b180db2cSAl Viro return -EFAULT; 114b180db2cSAl Viro } 115b180db2cSAl Viro force_successful_syscall_return(); 116b180db2cSAl Viro return i; 117b180db2cSAl Viro } 118b180db2cSAl Viro 1198dabe724SArnd Bergmann SYSCALL_DEFINE1(stime32, old_time32_t __user *, tptr) 120b180db2cSAl Viro { 1214eb1bca1SArnd Bergmann struct timespec64 tv; 122b180db2cSAl Viro int err; 123b180db2cSAl Viro 124b180db2cSAl Viro if (get_user(tv.tv_sec, tptr)) 125b180db2cSAl Viro return -EFAULT; 126b180db2cSAl Viro 127b180db2cSAl Viro tv.tv_nsec = 0; 128b180db2cSAl Viro 1294eb1bca1SArnd Bergmann err = security_settime64(&tv, NULL); 130b180db2cSAl Viro if (err) 131b180db2cSAl Viro return err; 132b180db2cSAl Viro 1334eb1bca1SArnd Bergmann do_settimeofday64(&tv); 134b180db2cSAl Viro return 0; 135b180db2cSAl Viro } 136b180db2cSAl Viro 137d33c577cSArnd Bergmann #endif /* __ARCH_WANT_SYS_TIME32 */ 138b180db2cSAl Viro #endif 139b180db2cSAl Viro 1405cee9645SThomas Gleixner SYSCALL_DEFINE2(gettimeofday, struct timeval __user *, tv, 1415cee9645SThomas Gleixner struct timezone __user *, tz) 1425cee9645SThomas Gleixner { 1435cee9645SThomas Gleixner if (likely(tv != NULL)) { 14433e26418SArnd Bergmann struct timespec64 ts; 14533e26418SArnd Bergmann 14633e26418SArnd Bergmann ktime_get_real_ts64(&ts); 14733e26418SArnd Bergmann if (put_user(ts.tv_sec, &tv->tv_sec) || 14833e26418SArnd Bergmann put_user(ts.tv_nsec / 1000, &tv->tv_usec)) 1495cee9645SThomas Gleixner return -EFAULT; 1505cee9645SThomas Gleixner } 1515cee9645SThomas Gleixner if (unlikely(tz != NULL)) { 1525cee9645SThomas Gleixner if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 1535cee9645SThomas Gleixner return -EFAULT; 1545cee9645SThomas Gleixner } 1555cee9645SThomas Gleixner return 0; 1565cee9645SThomas Gleixner } 1575cee9645SThomas Gleixner 1585cee9645SThomas Gleixner /* 1595cee9645SThomas Gleixner * In case for some reason the CMOS clock has not already been running 1605cee9645SThomas Gleixner * in UTC, but in some local time: The first time we set the timezone, 1615cee9645SThomas Gleixner * we will warp the clock so that it is ticking UTC time instead of 1625cee9645SThomas Gleixner * local time. Presumably, if someone is setting the timezone then we 1635cee9645SThomas Gleixner * are running in an environment where the programs understand about 1645cee9645SThomas Gleixner * timezones. This should be done at boot time in the /etc/rc script, 1655cee9645SThomas Gleixner * as soon as possible, so that the clock can be set right. Otherwise, 1665cee9645SThomas Gleixner * various programs will get confused when the clock gets warped. 1675cee9645SThomas Gleixner */ 1685cee9645SThomas Gleixner 16986d34732SBaolin Wang int do_sys_settimeofday64(const struct timespec64 *tv, const struct timezone *tz) 1705cee9645SThomas Gleixner { 1715cee9645SThomas Gleixner static int firsttime = 1; 1725cee9645SThomas Gleixner int error = 0; 1735cee9645SThomas Gleixner 1747a8e61f8SThomas Gleixner if (tv && !timespec64_valid_settod(tv)) 1755cee9645SThomas Gleixner return -EINVAL; 1765cee9645SThomas Gleixner 17786d34732SBaolin Wang error = security_settime64(tv, tz); 1785cee9645SThomas Gleixner if (error) 1795cee9645SThomas Gleixner return error; 1805cee9645SThomas Gleixner 1815cee9645SThomas Gleixner if (tz) { 182*1d6acc18SMukesh Ojha /* Verify we're within the +-15 hrs range */ 1836f7d7984SSasha Levin if (tz->tz_minuteswest > 15*60 || tz->tz_minuteswest < -15*60) 1846f7d7984SSasha Levin return -EINVAL; 1856f7d7984SSasha Levin 1865cee9645SThomas Gleixner sys_tz = *tz; 1875cee9645SThomas Gleixner update_vsyscall_tz(); 1885cee9645SThomas Gleixner if (firsttime) { 1895cee9645SThomas Gleixner firsttime = 0; 1905cee9645SThomas Gleixner if (!tv) 191e0956dccSArnd Bergmann timekeeping_warp_clock(); 1925cee9645SThomas Gleixner } 1935cee9645SThomas Gleixner } 1945cee9645SThomas Gleixner if (tv) 19586d34732SBaolin Wang return do_settimeofday64(tv); 1965cee9645SThomas Gleixner return 0; 1975cee9645SThomas Gleixner } 1985cee9645SThomas Gleixner 1995cee9645SThomas Gleixner SYSCALL_DEFINE2(settimeofday, struct timeval __user *, tv, 2005cee9645SThomas Gleixner struct timezone __user *, tz) 2015cee9645SThomas Gleixner { 2022ac00f17SDeepa Dinamani struct timespec64 new_ts; 2035cee9645SThomas Gleixner struct timeval user_tv; 2045cee9645SThomas Gleixner struct timezone new_tz; 2055cee9645SThomas Gleixner 2065cee9645SThomas Gleixner if (tv) { 2075cee9645SThomas Gleixner if (copy_from_user(&user_tv, tv, sizeof(*tv))) 2085cee9645SThomas Gleixner return -EFAULT; 2096ada1fc0SSasha Levin 2106ada1fc0SSasha Levin if (!timeval_valid(&user_tv)) 2116ada1fc0SSasha Levin return -EINVAL; 2126ada1fc0SSasha Levin 2135cee9645SThomas Gleixner new_ts.tv_sec = user_tv.tv_sec; 2145cee9645SThomas Gleixner new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; 2155cee9645SThomas Gleixner } 2165cee9645SThomas Gleixner if (tz) { 2175cee9645SThomas Gleixner if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2185cee9645SThomas Gleixner return -EFAULT; 2195cee9645SThomas Gleixner } 2205cee9645SThomas Gleixner 2212ac00f17SDeepa Dinamani return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2225cee9645SThomas Gleixner } 2235cee9645SThomas Gleixner 2242b2d0285SAl Viro #ifdef CONFIG_COMPAT 2259afc5eeeSArnd Bergmann COMPAT_SYSCALL_DEFINE2(gettimeofday, struct old_timeval32 __user *, tv, 2262b2d0285SAl Viro struct timezone __user *, tz) 2272b2d0285SAl Viro { 2282b2d0285SAl Viro if (tv) { 22933e26418SArnd Bergmann struct timespec64 ts; 2302b2d0285SAl Viro 23133e26418SArnd Bergmann ktime_get_real_ts64(&ts); 23233e26418SArnd Bergmann if (put_user(ts.tv_sec, &tv->tv_sec) || 23333e26418SArnd Bergmann put_user(ts.tv_nsec / 1000, &tv->tv_usec)) 2342b2d0285SAl Viro return -EFAULT; 2352b2d0285SAl Viro } 2362b2d0285SAl Viro if (tz) { 2372b2d0285SAl Viro if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 2382b2d0285SAl Viro return -EFAULT; 2392b2d0285SAl Viro } 2402b2d0285SAl Viro 2412b2d0285SAl Viro return 0; 2422b2d0285SAl Viro } 2432b2d0285SAl Viro 2449afc5eeeSArnd Bergmann COMPAT_SYSCALL_DEFINE2(settimeofday, struct old_timeval32 __user *, tv, 2452b2d0285SAl Viro struct timezone __user *, tz) 2462b2d0285SAl Viro { 2472b2d0285SAl Viro struct timespec64 new_ts; 2482b2d0285SAl Viro struct timeval user_tv; 2492b2d0285SAl Viro struct timezone new_tz; 2502b2d0285SAl Viro 2512b2d0285SAl Viro if (tv) { 2522b2d0285SAl Viro if (compat_get_timeval(&user_tv, tv)) 2532b2d0285SAl Viro return -EFAULT; 2549176ab1bSzhengbin 2559176ab1bSzhengbin if (!timeval_valid(&user_tv)) 2569176ab1bSzhengbin return -EINVAL; 2579176ab1bSzhengbin 2582b2d0285SAl Viro new_ts.tv_sec = user_tv.tv_sec; 2592b2d0285SAl Viro new_ts.tv_nsec = user_tv.tv_usec * NSEC_PER_USEC; 2602b2d0285SAl Viro } 2612b2d0285SAl Viro if (tz) { 2622b2d0285SAl Viro if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2632b2d0285SAl Viro return -EFAULT; 2642b2d0285SAl Viro } 2652b2d0285SAl Viro 2662b2d0285SAl Viro return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2672b2d0285SAl Viro } 2682b2d0285SAl Viro #endif 2692b2d0285SAl Viro 2703876ced4SDeepa Dinamani #if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT) 2713876ced4SDeepa Dinamani SYSCALL_DEFINE1(adjtimex, struct __kernel_timex __user *, txc_p) 2725cee9645SThomas Gleixner { 273ead25417SDeepa Dinamani struct __kernel_timex txc; /* Local copy of parameter */ 2745cee9645SThomas Gleixner int ret; 2755cee9645SThomas Gleixner 2765cee9645SThomas Gleixner /* Copy the user data space into the kernel copy 2775cee9645SThomas Gleixner * structure. But bear in mind that the structures 2785cee9645SThomas Gleixner * may change 2795cee9645SThomas Gleixner */ 280ead25417SDeepa Dinamani if (copy_from_user(&txc, txc_p, sizeof(struct __kernel_timex))) 2815cee9645SThomas Gleixner return -EFAULT; 2825cee9645SThomas Gleixner ret = do_adjtimex(&txc); 283ead25417SDeepa Dinamani return copy_to_user(txc_p, &txc, sizeof(struct __kernel_timex)) ? -EFAULT : ret; 2845cee9645SThomas Gleixner } 2853876ced4SDeepa Dinamani #endif 2865cee9645SThomas Gleixner 2874d5f007eSArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME 288ead25417SDeepa Dinamani int get_old_timex32(struct __kernel_timex *txc, const struct old_timex32 __user *utp) 2894d5f007eSArnd Bergmann { 2904d5f007eSArnd Bergmann struct old_timex32 tx32; 2913a4d44b6SAl Viro 292ead25417SDeepa Dinamani memset(txc, 0, sizeof(struct __kernel_timex)); 2934d5f007eSArnd Bergmann if (copy_from_user(&tx32, utp, sizeof(struct old_timex32))) 2944d5f007eSArnd Bergmann return -EFAULT; 2954d5f007eSArnd Bergmann 2964d5f007eSArnd Bergmann txc->modes = tx32.modes; 2974d5f007eSArnd Bergmann txc->offset = tx32.offset; 2984d5f007eSArnd Bergmann txc->freq = tx32.freq; 2994d5f007eSArnd Bergmann txc->maxerror = tx32.maxerror; 3004d5f007eSArnd Bergmann txc->esterror = tx32.esterror; 3014d5f007eSArnd Bergmann txc->status = tx32.status; 3024d5f007eSArnd Bergmann txc->constant = tx32.constant; 3034d5f007eSArnd Bergmann txc->precision = tx32.precision; 3044d5f007eSArnd Bergmann txc->tolerance = tx32.tolerance; 3054d5f007eSArnd Bergmann txc->time.tv_sec = tx32.time.tv_sec; 3064d5f007eSArnd Bergmann txc->time.tv_usec = tx32.time.tv_usec; 3074d5f007eSArnd Bergmann txc->tick = tx32.tick; 3084d5f007eSArnd Bergmann txc->ppsfreq = tx32.ppsfreq; 3094d5f007eSArnd Bergmann txc->jitter = tx32.jitter; 3104d5f007eSArnd Bergmann txc->shift = tx32.shift; 3114d5f007eSArnd Bergmann txc->stabil = tx32.stabil; 3124d5f007eSArnd Bergmann txc->jitcnt = tx32.jitcnt; 3134d5f007eSArnd Bergmann txc->calcnt = tx32.calcnt; 3144d5f007eSArnd Bergmann txc->errcnt = tx32.errcnt; 3154d5f007eSArnd Bergmann txc->stbcnt = tx32.stbcnt; 3164d5f007eSArnd Bergmann 3174d5f007eSArnd Bergmann return 0; 3184d5f007eSArnd Bergmann } 3194d5f007eSArnd Bergmann 320ead25417SDeepa Dinamani int put_old_timex32(struct old_timex32 __user *utp, const struct __kernel_timex *txc) 3214d5f007eSArnd Bergmann { 3224d5f007eSArnd Bergmann struct old_timex32 tx32; 3234d5f007eSArnd Bergmann 3244d5f007eSArnd Bergmann memset(&tx32, 0, sizeof(struct old_timex32)); 3254d5f007eSArnd Bergmann tx32.modes = txc->modes; 3264d5f007eSArnd Bergmann tx32.offset = txc->offset; 3274d5f007eSArnd Bergmann tx32.freq = txc->freq; 3284d5f007eSArnd Bergmann tx32.maxerror = txc->maxerror; 3294d5f007eSArnd Bergmann tx32.esterror = txc->esterror; 3304d5f007eSArnd Bergmann tx32.status = txc->status; 3314d5f007eSArnd Bergmann tx32.constant = txc->constant; 3324d5f007eSArnd Bergmann tx32.precision = txc->precision; 3334d5f007eSArnd Bergmann tx32.tolerance = txc->tolerance; 3344d5f007eSArnd Bergmann tx32.time.tv_sec = txc->time.tv_sec; 3354d5f007eSArnd Bergmann tx32.time.tv_usec = txc->time.tv_usec; 3364d5f007eSArnd Bergmann tx32.tick = txc->tick; 3374d5f007eSArnd Bergmann tx32.ppsfreq = txc->ppsfreq; 3384d5f007eSArnd Bergmann tx32.jitter = txc->jitter; 3394d5f007eSArnd Bergmann tx32.shift = txc->shift; 3404d5f007eSArnd Bergmann tx32.stabil = txc->stabil; 3414d5f007eSArnd Bergmann tx32.jitcnt = txc->jitcnt; 3424d5f007eSArnd Bergmann tx32.calcnt = txc->calcnt; 3434d5f007eSArnd Bergmann tx32.errcnt = txc->errcnt; 3444d5f007eSArnd Bergmann tx32.stbcnt = txc->stbcnt; 3454d5f007eSArnd Bergmann tx32.tai = txc->tai; 3464d5f007eSArnd Bergmann if (copy_to_user(utp, &tx32, sizeof(struct old_timex32))) 3474d5f007eSArnd Bergmann return -EFAULT; 3484d5f007eSArnd Bergmann return 0; 3494d5f007eSArnd Bergmann } 3504d5f007eSArnd Bergmann 3518dabe724SArnd Bergmann SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp) 3523a4d44b6SAl Viro { 353ead25417SDeepa Dinamani struct __kernel_timex txc; 3543a4d44b6SAl Viro int err, ret; 3553a4d44b6SAl Viro 3564d5f007eSArnd Bergmann err = get_old_timex32(&txc, utp); 3573a4d44b6SAl Viro if (err) 3583a4d44b6SAl Viro return err; 3593a4d44b6SAl Viro 3603a4d44b6SAl Viro ret = do_adjtimex(&txc); 3613a4d44b6SAl Viro 3624d5f007eSArnd Bergmann err = put_old_timex32(utp, &txc); 3633a4d44b6SAl Viro if (err) 3643a4d44b6SAl Viro return err; 3653a4d44b6SAl Viro 3663a4d44b6SAl Viro return ret; 3673a4d44b6SAl Viro } 3683a4d44b6SAl Viro #endif 3693a4d44b6SAl Viro 3705cee9645SThomas Gleixner /* 3715cee9645SThomas Gleixner * Convert jiffies to milliseconds and back. 3725cee9645SThomas Gleixner * 3735cee9645SThomas Gleixner * Avoid unnecessary multiplications/divisions in the 3745cee9645SThomas Gleixner * two most common HZ cases: 3755cee9645SThomas Gleixner */ 3765cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j) 3775cee9645SThomas Gleixner { 3785cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 3795cee9645SThomas Gleixner return (MSEC_PER_SEC / HZ) * j; 3805cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 3815cee9645SThomas Gleixner return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); 3825cee9645SThomas Gleixner #else 3835cee9645SThomas Gleixner # if BITS_PER_LONG == 32 384abcbcb80SGeert Uytterhoeven return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >> 385abcbcb80SGeert Uytterhoeven HZ_TO_MSEC_SHR32; 3865cee9645SThomas Gleixner # else 387abcbcb80SGeert Uytterhoeven return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN); 3885cee9645SThomas Gleixner # endif 3895cee9645SThomas Gleixner #endif 3905cee9645SThomas Gleixner } 3915cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs); 3925cee9645SThomas Gleixner 3935cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j) 3945cee9645SThomas Gleixner { 395e0758676SFrederic Weisbecker /* 396e0758676SFrederic Weisbecker * Hz usually doesn't go much further MSEC_PER_SEC. 397e0758676SFrederic Weisbecker * jiffies_to_usecs() and usecs_to_jiffies() depend on that. 398e0758676SFrederic Weisbecker */ 399e0758676SFrederic Weisbecker BUILD_BUG_ON(HZ > USEC_PER_SEC); 400e0758676SFrederic Weisbecker 401e0758676SFrederic Weisbecker #if !(USEC_PER_SEC % HZ) 4025cee9645SThomas Gleixner return (USEC_PER_SEC / HZ) * j; 4035cee9645SThomas Gleixner #else 4045cee9645SThomas Gleixner # if BITS_PER_LONG == 32 4055cee9645SThomas Gleixner return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; 4065cee9645SThomas Gleixner # else 4075cee9645SThomas Gleixner return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; 4085cee9645SThomas Gleixner # endif 4095cee9645SThomas Gleixner #endif 4105cee9645SThomas Gleixner } 4115cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs); 4125cee9645SThomas Gleixner 41390b6ce9cSpang.xunlei /* 41490b6ce9cSpang.xunlei * mktime64 - Converts date to seconds. 41590b6ce9cSpang.xunlei * Converts Gregorian date to seconds since 1970-01-01 00:00:00. 4165cee9645SThomas Gleixner * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 4175cee9645SThomas Gleixner * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. 4185cee9645SThomas Gleixner * 4195cee9645SThomas Gleixner * [For the Julian calendar (which was used in Russia before 1917, 4205cee9645SThomas Gleixner * Britain & colonies before 1752, anywhere else before 1582, 4215cee9645SThomas Gleixner * and is still in use by some communities) leave out the 4225cee9645SThomas Gleixner * -year/100+year/400 terms, and add 10.] 4235cee9645SThomas Gleixner * 4245cee9645SThomas Gleixner * This algorithm was first published by Gauss (I think). 425ede5147dSDavid Howells * 426ede5147dSDavid Howells * A leap second can be indicated by calling this function with sec as 427ede5147dSDavid Howells * 60 (allowable under ISO 8601). The leap second is treated the same 428ede5147dSDavid Howells * as the following second since they don't exist in UNIX time. 429ede5147dSDavid Howells * 430ede5147dSDavid Howells * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight 431ede5147dSDavid Howells * tomorrow - (allowable under ISO 8601) is supported. 4325cee9645SThomas Gleixner */ 43390b6ce9cSpang.xunlei time64_t mktime64(const unsigned int year0, const unsigned int mon0, 4345cee9645SThomas Gleixner const unsigned int day, const unsigned int hour, 4355cee9645SThomas Gleixner const unsigned int min, const unsigned int sec) 4365cee9645SThomas Gleixner { 4375cee9645SThomas Gleixner unsigned int mon = mon0, year = year0; 4385cee9645SThomas Gleixner 4395cee9645SThomas Gleixner /* 1..12 -> 11,12,1..10 */ 4405cee9645SThomas Gleixner if (0 >= (int) (mon -= 2)) { 4415cee9645SThomas Gleixner mon += 12; /* Puts Feb last since it has leap day */ 4425cee9645SThomas Gleixner year -= 1; 4435cee9645SThomas Gleixner } 4445cee9645SThomas Gleixner 44590b6ce9cSpang.xunlei return ((((time64_t) 4465cee9645SThomas Gleixner (year/4 - year/100 + year/400 + 367*mon/12 + day) + 4475cee9645SThomas Gleixner year*365 - 719499 448ede5147dSDavid Howells )*24 + hour /* now have hours - midnight tomorrow handled here */ 4495cee9645SThomas Gleixner )*60 + min /* now have minutes */ 4505cee9645SThomas Gleixner )*60 + sec; /* finally seconds */ 4515cee9645SThomas Gleixner } 45290b6ce9cSpang.xunlei EXPORT_SYMBOL(mktime64); 4535cee9645SThomas Gleixner 4545cee9645SThomas Gleixner /** 4555cee9645SThomas Gleixner * ns_to_timespec - Convert nanoseconds to timespec 4565cee9645SThomas Gleixner * @nsec: the nanoseconds value to be converted 4575cee9645SThomas Gleixner * 4585cee9645SThomas Gleixner * Returns the timespec representation of the nsec parameter. 4595cee9645SThomas Gleixner */ 4605cee9645SThomas Gleixner struct timespec ns_to_timespec(const s64 nsec) 4615cee9645SThomas Gleixner { 4625cee9645SThomas Gleixner struct timespec ts; 4635cee9645SThomas Gleixner s32 rem; 4645cee9645SThomas Gleixner 4655cee9645SThomas Gleixner if (!nsec) 4665cee9645SThomas Gleixner return (struct timespec) {0, 0}; 4675cee9645SThomas Gleixner 4685cee9645SThomas Gleixner ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); 4695cee9645SThomas Gleixner if (unlikely(rem < 0)) { 4705cee9645SThomas Gleixner ts.tv_sec--; 4715cee9645SThomas Gleixner rem += NSEC_PER_SEC; 4725cee9645SThomas Gleixner } 4735cee9645SThomas Gleixner ts.tv_nsec = rem; 4745cee9645SThomas Gleixner 4755cee9645SThomas Gleixner return ts; 4765cee9645SThomas Gleixner } 4775cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timespec); 4785cee9645SThomas Gleixner 4795cee9645SThomas Gleixner /** 4805cee9645SThomas Gleixner * ns_to_timeval - Convert nanoseconds to timeval 4815cee9645SThomas Gleixner * @nsec: the nanoseconds value to be converted 4825cee9645SThomas Gleixner * 4835cee9645SThomas Gleixner * Returns the timeval representation of the nsec parameter. 4845cee9645SThomas Gleixner */ 4855cee9645SThomas Gleixner struct timeval ns_to_timeval(const s64 nsec) 4865cee9645SThomas Gleixner { 4875cee9645SThomas Gleixner struct timespec ts = ns_to_timespec(nsec); 4885cee9645SThomas Gleixner struct timeval tv; 4895cee9645SThomas Gleixner 4905cee9645SThomas Gleixner tv.tv_sec = ts.tv_sec; 4915cee9645SThomas Gleixner tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; 4925cee9645SThomas Gleixner 4935cee9645SThomas Gleixner return tv; 4945cee9645SThomas Gleixner } 4955cee9645SThomas Gleixner EXPORT_SYMBOL(ns_to_timeval); 4965cee9645SThomas Gleixner 497a84d1169SArnd Bergmann struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec) 498a84d1169SArnd Bergmann { 499a84d1169SArnd Bergmann struct timespec64 ts = ns_to_timespec64(nsec); 500a84d1169SArnd Bergmann struct __kernel_old_timeval tv; 501a84d1169SArnd Bergmann 502a84d1169SArnd Bergmann tv.tv_sec = ts.tv_sec; 503a84d1169SArnd Bergmann tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000; 504a84d1169SArnd Bergmann 505a84d1169SArnd Bergmann return tv; 506a84d1169SArnd Bergmann } 507a84d1169SArnd Bergmann EXPORT_SYMBOL(ns_to_kernel_old_timeval); 508a84d1169SArnd Bergmann 50949cd6f86SJohn Stultz /** 51049cd6f86SJohn Stultz * set_normalized_timespec - set timespec sec and nsec parts and normalize 51149cd6f86SJohn Stultz * 51249cd6f86SJohn Stultz * @ts: pointer to timespec variable to be set 51349cd6f86SJohn Stultz * @sec: seconds to set 51449cd6f86SJohn Stultz * @nsec: nanoseconds to set 51549cd6f86SJohn Stultz * 51649cd6f86SJohn Stultz * Set seconds and nanoseconds field of a timespec variable and 51749cd6f86SJohn Stultz * normalize to the timespec storage format 51849cd6f86SJohn Stultz * 51949cd6f86SJohn Stultz * Note: The tv_nsec part is always in the range of 52049cd6f86SJohn Stultz * 0 <= tv_nsec < NSEC_PER_SEC 52149cd6f86SJohn Stultz * For negative values only the tv_sec field is negative ! 52249cd6f86SJohn Stultz */ 52349cd6f86SJohn Stultz void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) 52449cd6f86SJohn Stultz { 52549cd6f86SJohn Stultz while (nsec >= NSEC_PER_SEC) { 52649cd6f86SJohn Stultz /* 52749cd6f86SJohn Stultz * The following asm() prevents the compiler from 52849cd6f86SJohn Stultz * optimising this loop into a modulo operation. See 52949cd6f86SJohn Stultz * also __iter_div_u64_rem() in include/linux/time.h 53049cd6f86SJohn Stultz */ 53149cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 53249cd6f86SJohn Stultz nsec -= NSEC_PER_SEC; 53349cd6f86SJohn Stultz ++sec; 53449cd6f86SJohn Stultz } 53549cd6f86SJohn Stultz while (nsec < 0) { 53649cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 53749cd6f86SJohn Stultz nsec += NSEC_PER_SEC; 53849cd6f86SJohn Stultz --sec; 53949cd6f86SJohn Stultz } 54049cd6f86SJohn Stultz ts->tv_sec = sec; 54149cd6f86SJohn Stultz ts->tv_nsec = nsec; 54249cd6f86SJohn Stultz } 54349cd6f86SJohn Stultz EXPORT_SYMBOL(set_normalized_timespec64); 54449cd6f86SJohn Stultz 54549cd6f86SJohn Stultz /** 54649cd6f86SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 54749cd6f86SJohn Stultz * @nsec: the nanoseconds value to be converted 54849cd6f86SJohn Stultz * 54949cd6f86SJohn Stultz * Returns the timespec64 representation of the nsec parameter. 55049cd6f86SJohn Stultz */ 55149cd6f86SJohn Stultz struct timespec64 ns_to_timespec64(const s64 nsec) 55249cd6f86SJohn Stultz { 55320d08736SArnd Bergmann struct timespec64 ts = { 0, 0 }; 55449cd6f86SJohn Stultz s32 rem; 55549cd6f86SJohn Stultz 55620d08736SArnd Bergmann if (likely(nsec > 0)) { 55720d08736SArnd Bergmann ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem); 55849cd6f86SJohn Stultz ts.tv_nsec = rem; 55920d08736SArnd Bergmann } else if (nsec < 0) { 56020d08736SArnd Bergmann /* 56120d08736SArnd Bergmann * With negative times, tv_sec points to the earlier 56220d08736SArnd Bergmann * second, and tv_nsec counts the nanoseconds since 56320d08736SArnd Bergmann * then, so tv_nsec is always a positive number. 56420d08736SArnd Bergmann */ 56520d08736SArnd Bergmann ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1; 56620d08736SArnd Bergmann ts.tv_nsec = NSEC_PER_SEC - rem - 1; 56720d08736SArnd Bergmann } 56849cd6f86SJohn Stultz 56949cd6f86SJohn Stultz return ts; 57049cd6f86SJohn Stultz } 57149cd6f86SJohn Stultz EXPORT_SYMBOL(ns_to_timespec64); 572abc8f96eSArnd Bergmann 573ca42aaf0SNicholas Mc Guire /** 574ca42aaf0SNicholas Mc Guire * msecs_to_jiffies: - convert milliseconds to jiffies 575ca42aaf0SNicholas Mc Guire * @m: time in milliseconds 576ca42aaf0SNicholas Mc Guire * 577ca42aaf0SNicholas Mc Guire * conversion is done as follows: 5785cee9645SThomas Gleixner * 5795cee9645SThomas Gleixner * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) 5805cee9645SThomas Gleixner * 5815cee9645SThomas Gleixner * - 'too large' values [that would result in larger than 5825cee9645SThomas Gleixner * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. 5835cee9645SThomas Gleixner * 5845cee9645SThomas Gleixner * - all other values are converted to jiffies by either multiplying 585ca42aaf0SNicholas Mc Guire * the input value by a factor or dividing it with a factor and 586ca42aaf0SNicholas Mc Guire * handling any 32-bit overflows. 587ca42aaf0SNicholas Mc Guire * for the details see __msecs_to_jiffies() 5885cee9645SThomas Gleixner * 589ca42aaf0SNicholas Mc Guire * msecs_to_jiffies() checks for the passed in value being a constant 590ca42aaf0SNicholas Mc Guire * via __builtin_constant_p() allowing gcc to eliminate most of the 591ca42aaf0SNicholas Mc Guire * code, __msecs_to_jiffies() is called if the value passed does not 592ca42aaf0SNicholas Mc Guire * allow constant folding and the actual conversion must be done at 593ca42aaf0SNicholas Mc Guire * runtime. 594ca42aaf0SNicholas Mc Guire * the _msecs_to_jiffies helpers are the HZ dependent conversion 595ca42aaf0SNicholas Mc Guire * routines found in include/linux/jiffies.h 5965cee9645SThomas Gleixner */ 597ca42aaf0SNicholas Mc Guire unsigned long __msecs_to_jiffies(const unsigned int m) 5985cee9645SThomas Gleixner { 5995cee9645SThomas Gleixner /* 6005cee9645SThomas Gleixner * Negative value, means infinite timeout: 6015cee9645SThomas Gleixner */ 6025cee9645SThomas Gleixner if ((int)m < 0) 6035cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 604ca42aaf0SNicholas Mc Guire return _msecs_to_jiffies(m); 6055cee9645SThomas Gleixner } 606ca42aaf0SNicholas Mc Guire EXPORT_SYMBOL(__msecs_to_jiffies); 6075cee9645SThomas Gleixner 608ae60d6a0SNicholas Mc Guire unsigned long __usecs_to_jiffies(const unsigned int u) 6095cee9645SThomas Gleixner { 6105cee9645SThomas Gleixner if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) 6115cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 612ae60d6a0SNicholas Mc Guire return _usecs_to_jiffies(u); 6135cee9645SThomas Gleixner } 614ae60d6a0SNicholas Mc Guire EXPORT_SYMBOL(__usecs_to_jiffies); 6155cee9645SThomas Gleixner 6165cee9645SThomas Gleixner /* 6175cee9645SThomas Gleixner * The TICK_NSEC - 1 rounds up the value to the next resolution. Note 6185cee9645SThomas Gleixner * that a remainder subtract here would not do the right thing as the 6195cee9645SThomas Gleixner * resolution values don't fall on second boundries. I.e. the line: 6205cee9645SThomas Gleixner * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. 621d78c9300SAndrew Hunter * Note that due to the small error in the multiplier here, this 622d78c9300SAndrew Hunter * rounding is incorrect for sufficiently large values of tv_nsec, but 623d78c9300SAndrew Hunter * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're 624d78c9300SAndrew Hunter * OK. 6255cee9645SThomas Gleixner * 6265cee9645SThomas Gleixner * Rather, we just shift the bits off the right. 6275cee9645SThomas Gleixner * 6285cee9645SThomas Gleixner * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec 6295cee9645SThomas Gleixner * value to a scaled second value. 6305cee9645SThomas Gleixner */ 631d78c9300SAndrew Hunter static unsigned long 6329ca30850SBaolin Wang __timespec64_to_jiffies(u64 sec, long nsec) 6335cee9645SThomas Gleixner { 634d78c9300SAndrew Hunter nsec = nsec + TICK_NSEC - 1; 6355cee9645SThomas Gleixner 6365cee9645SThomas Gleixner if (sec >= MAX_SEC_IN_JIFFIES){ 6375cee9645SThomas Gleixner sec = MAX_SEC_IN_JIFFIES; 6385cee9645SThomas Gleixner nsec = 0; 6395cee9645SThomas Gleixner } 6409ca30850SBaolin Wang return ((sec * SEC_CONVERSION) + 6415cee9645SThomas Gleixner (((u64)nsec * NSEC_CONVERSION) >> 6425cee9645SThomas Gleixner (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; 6435cee9645SThomas Gleixner 6445cee9645SThomas Gleixner } 645d78c9300SAndrew Hunter 6469ca30850SBaolin Wang static unsigned long 6479ca30850SBaolin Wang __timespec_to_jiffies(unsigned long sec, long nsec) 648d78c9300SAndrew Hunter { 6499ca30850SBaolin Wang return __timespec64_to_jiffies((u64)sec, nsec); 650d78c9300SAndrew Hunter } 651d78c9300SAndrew Hunter 6529ca30850SBaolin Wang unsigned long 6539ca30850SBaolin Wang timespec64_to_jiffies(const struct timespec64 *value) 6549ca30850SBaolin Wang { 6559ca30850SBaolin Wang return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); 6569ca30850SBaolin Wang } 6579ca30850SBaolin Wang EXPORT_SYMBOL(timespec64_to_jiffies); 6585cee9645SThomas Gleixner 6595cee9645SThomas Gleixner void 6609ca30850SBaolin Wang jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) 6615cee9645SThomas Gleixner { 6625cee9645SThomas Gleixner /* 6635cee9645SThomas Gleixner * Convert jiffies to nanoseconds and separate with 6645cee9645SThomas Gleixner * one divide. 6655cee9645SThomas Gleixner */ 6665cee9645SThomas Gleixner u32 rem; 6675cee9645SThomas Gleixner value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 6685cee9645SThomas Gleixner NSEC_PER_SEC, &rem); 6695cee9645SThomas Gleixner value->tv_nsec = rem; 6705cee9645SThomas Gleixner } 6719ca30850SBaolin Wang EXPORT_SYMBOL(jiffies_to_timespec64); 6725cee9645SThomas Gleixner 673d78c9300SAndrew Hunter /* 674d78c9300SAndrew Hunter * We could use a similar algorithm to timespec_to_jiffies (with a 675d78c9300SAndrew Hunter * different multiplier for usec instead of nsec). But this has a 676d78c9300SAndrew Hunter * problem with rounding: we can't exactly add TICK_NSEC - 1 to the 677d78c9300SAndrew Hunter * usec value, since it's not necessarily integral. 6785cee9645SThomas Gleixner * 679d78c9300SAndrew Hunter * We could instead round in the intermediate scaled representation 680d78c9300SAndrew Hunter * (i.e. in units of 1/2^(large scale) jiffies) but that's also 681d78c9300SAndrew Hunter * perilous: the scaling introduces a small positive error, which 682d78c9300SAndrew Hunter * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 683d78c9300SAndrew Hunter * units to the intermediate before shifting) leads to accidental 684d78c9300SAndrew Hunter * overflow and overestimates. 685d78c9300SAndrew Hunter * 686d78c9300SAndrew Hunter * At the cost of one additional multiplication by a constant, just 687d78c9300SAndrew Hunter * use the timespec implementation. 6885cee9645SThomas Gleixner */ 6895cee9645SThomas Gleixner unsigned long 6905cee9645SThomas Gleixner timeval_to_jiffies(const struct timeval *value) 6915cee9645SThomas Gleixner { 692d78c9300SAndrew Hunter return __timespec_to_jiffies(value->tv_sec, 693d78c9300SAndrew Hunter value->tv_usec * NSEC_PER_USEC); 6945cee9645SThomas Gleixner } 6955cee9645SThomas Gleixner EXPORT_SYMBOL(timeval_to_jiffies); 6965cee9645SThomas Gleixner 6975cee9645SThomas Gleixner void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) 6985cee9645SThomas Gleixner { 6995cee9645SThomas Gleixner /* 7005cee9645SThomas Gleixner * Convert jiffies to nanoseconds and separate with 7015cee9645SThomas Gleixner * one divide. 7025cee9645SThomas Gleixner */ 7035cee9645SThomas Gleixner u32 rem; 7045cee9645SThomas Gleixner 7055cee9645SThomas Gleixner value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 7065cee9645SThomas Gleixner NSEC_PER_SEC, &rem); 7075cee9645SThomas Gleixner value->tv_usec = rem / NSEC_PER_USEC; 7085cee9645SThomas Gleixner } 7095cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_timeval); 7105cee9645SThomas Gleixner 7115cee9645SThomas Gleixner /* 7125cee9645SThomas Gleixner * Convert jiffies/jiffies_64 to clock_t and back. 7135cee9645SThomas Gleixner */ 7145cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x) 7155cee9645SThomas Gleixner { 7165cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 7175cee9645SThomas Gleixner # if HZ < USER_HZ 7185cee9645SThomas Gleixner return x * (USER_HZ / HZ); 7195cee9645SThomas Gleixner # else 7205cee9645SThomas Gleixner return x / (HZ / USER_HZ); 7215cee9645SThomas Gleixner # endif 7225cee9645SThomas Gleixner #else 7235cee9645SThomas Gleixner return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); 7245cee9645SThomas Gleixner #endif 7255cee9645SThomas Gleixner } 7265cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t); 7275cee9645SThomas Gleixner 7285cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x) 7295cee9645SThomas Gleixner { 7305cee9645SThomas Gleixner #if (HZ % USER_HZ)==0 7315cee9645SThomas Gleixner if (x >= ~0UL / (HZ / USER_HZ)) 7325cee9645SThomas Gleixner return ~0UL; 7335cee9645SThomas Gleixner return x * (HZ / USER_HZ); 7345cee9645SThomas Gleixner #else 7355cee9645SThomas Gleixner /* Don't worry about loss of precision here .. */ 7365cee9645SThomas Gleixner if (x >= ~0UL / HZ * USER_HZ) 7375cee9645SThomas Gleixner return ~0UL; 7385cee9645SThomas Gleixner 7395cee9645SThomas Gleixner /* .. but do try to contain it here */ 7405cee9645SThomas Gleixner return div_u64((u64)x * HZ, USER_HZ); 7415cee9645SThomas Gleixner #endif 7425cee9645SThomas Gleixner } 7435cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies); 7445cee9645SThomas Gleixner 7455cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x) 7465cee9645SThomas Gleixner { 7475cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 7485cee9645SThomas Gleixner # if HZ < USER_HZ 7495cee9645SThomas Gleixner x = div_u64(x * USER_HZ, HZ); 7505cee9645SThomas Gleixner # elif HZ > USER_HZ 7515cee9645SThomas Gleixner x = div_u64(x, HZ / USER_HZ); 7525cee9645SThomas Gleixner # else 7535cee9645SThomas Gleixner /* Nothing to do */ 7545cee9645SThomas Gleixner # endif 7555cee9645SThomas Gleixner #else 7565cee9645SThomas Gleixner /* 7575cee9645SThomas Gleixner * There are better ways that don't overflow early, 7585cee9645SThomas Gleixner * but even this doesn't overflow in hundreds of years 7595cee9645SThomas Gleixner * in 64 bits, so.. 7605cee9645SThomas Gleixner */ 7615cee9645SThomas Gleixner x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); 7625cee9645SThomas Gleixner #endif 7635cee9645SThomas Gleixner return x; 7645cee9645SThomas Gleixner } 7655cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t); 7665cee9645SThomas Gleixner 7675cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x) 7685cee9645SThomas Gleixner { 7695cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0 7705cee9645SThomas Gleixner return div_u64(x, NSEC_PER_SEC / USER_HZ); 7715cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0 7725cee9645SThomas Gleixner return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); 7735cee9645SThomas Gleixner #else 7745cee9645SThomas Gleixner /* 7755cee9645SThomas Gleixner * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, 7765cee9645SThomas Gleixner * overflow after 64.99 years. 7775cee9645SThomas Gleixner * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... 7785cee9645SThomas Gleixner */ 7795cee9645SThomas Gleixner return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); 7805cee9645SThomas Gleixner #endif 7815cee9645SThomas Gleixner } 7825cee9645SThomas Gleixner 78307e5f5e3SFrederic Weisbecker u64 jiffies64_to_nsecs(u64 j) 78407e5f5e3SFrederic Weisbecker { 78507e5f5e3SFrederic Weisbecker #if !(NSEC_PER_SEC % HZ) 78607e5f5e3SFrederic Weisbecker return (NSEC_PER_SEC / HZ) * j; 78707e5f5e3SFrederic Weisbecker # else 78807e5f5e3SFrederic Weisbecker return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN); 78907e5f5e3SFrederic Weisbecker #endif 79007e5f5e3SFrederic Weisbecker } 79107e5f5e3SFrederic Weisbecker EXPORT_SYMBOL(jiffies64_to_nsecs); 79207e5f5e3SFrederic Weisbecker 7933b15d09fSLi RongQing u64 jiffies64_to_msecs(const u64 j) 7943b15d09fSLi RongQing { 7953b15d09fSLi RongQing #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 7963b15d09fSLi RongQing return (MSEC_PER_SEC / HZ) * j; 7973b15d09fSLi RongQing #else 7983b15d09fSLi RongQing return div_u64(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN); 7993b15d09fSLi RongQing #endif 8003b15d09fSLi RongQing } 8013b15d09fSLi RongQing EXPORT_SYMBOL(jiffies64_to_msecs); 8023b15d09fSLi RongQing 8035cee9645SThomas Gleixner /** 8045cee9645SThomas Gleixner * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 8055cee9645SThomas Gleixner * 8065cee9645SThomas Gleixner * @n: nsecs in u64 8075cee9645SThomas Gleixner * 8085cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 8095cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 8105cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 8115cee9645SThomas Gleixner * 8125cee9645SThomas Gleixner * note: 8135cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 8145cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 8155cee9645SThomas Gleixner */ 8165cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n) 8175cee9645SThomas Gleixner { 8185cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0 8195cee9645SThomas Gleixner /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ 8205cee9645SThomas Gleixner return div_u64(n, NSEC_PER_SEC / HZ); 8215cee9645SThomas Gleixner #elif (HZ % 512) == 0 8225cee9645SThomas Gleixner /* overflow after 292 years if HZ = 1024 */ 8235cee9645SThomas Gleixner return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); 8245cee9645SThomas Gleixner #else 8255cee9645SThomas Gleixner /* 8265cee9645SThomas Gleixner * Generic case - optimized for cases where HZ is a multiple of 3. 8275cee9645SThomas Gleixner * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. 8285cee9645SThomas Gleixner */ 8295cee9645SThomas Gleixner return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); 8305cee9645SThomas Gleixner #endif 8315cee9645SThomas Gleixner } 8327bd0e226SDaniel Vetter EXPORT_SYMBOL(nsecs_to_jiffies64); 8335cee9645SThomas Gleixner 8345cee9645SThomas Gleixner /** 8355cee9645SThomas Gleixner * nsecs_to_jiffies - Convert nsecs in u64 to jiffies 8365cee9645SThomas Gleixner * 8375cee9645SThomas Gleixner * @n: nsecs in u64 8385cee9645SThomas Gleixner * 8395cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 8405cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 8415cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 8425cee9645SThomas Gleixner * 8435cee9645SThomas Gleixner * note: 8445cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 8455cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 8465cee9645SThomas Gleixner */ 8475cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n) 8485cee9645SThomas Gleixner { 8495cee9645SThomas Gleixner return (unsigned long)nsecs_to_jiffies64(n); 8505cee9645SThomas Gleixner } 851d560fed6SThomas Gleixner EXPORT_SYMBOL_GPL(nsecs_to_jiffies); 8525cee9645SThomas Gleixner 8535cee9645SThomas Gleixner /* 854bc2c53e5SDeepa Dinamani * Add two timespec64 values and do a safety check for overflow. 855bc2c53e5SDeepa Dinamani * It's assumed that both values are valid (>= 0). 856bc2c53e5SDeepa Dinamani * And, each timespec64 is in normalized form. 857bc2c53e5SDeepa Dinamani */ 858bc2c53e5SDeepa Dinamani struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 859bc2c53e5SDeepa Dinamani const struct timespec64 rhs) 860bc2c53e5SDeepa Dinamani { 861bc2c53e5SDeepa Dinamani struct timespec64 res; 862bc2c53e5SDeepa Dinamani 863469e857fSVegard Nossum set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec, 864bc2c53e5SDeepa Dinamani lhs.tv_nsec + rhs.tv_nsec); 865bc2c53e5SDeepa Dinamani 866bc2c53e5SDeepa Dinamani if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) { 867bc2c53e5SDeepa Dinamani res.tv_sec = TIME64_MAX; 868bc2c53e5SDeepa Dinamani res.tv_nsec = 0; 869bc2c53e5SDeepa Dinamani } 870bc2c53e5SDeepa Dinamani 871bc2c53e5SDeepa Dinamani return res; 872bc2c53e5SDeepa Dinamani } 873f59dd9c8SDeepa Dinamani 874f59dd9c8SDeepa Dinamani int get_timespec64(struct timespec64 *ts, 875ea2ce8f3SDeepa Dinamani const struct __kernel_timespec __user *uts) 876f59dd9c8SDeepa Dinamani { 877ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts; 878f59dd9c8SDeepa Dinamani int ret; 879f59dd9c8SDeepa Dinamani 880f59dd9c8SDeepa Dinamani ret = copy_from_user(&kts, uts, sizeof(kts)); 881f59dd9c8SDeepa Dinamani if (ret) 882f59dd9c8SDeepa Dinamani return -EFAULT; 883f59dd9c8SDeepa Dinamani 884f59dd9c8SDeepa Dinamani ts->tv_sec = kts.tv_sec; 885ea2ce8f3SDeepa Dinamani 886ea2ce8f3SDeepa Dinamani /* Zero out the padding for 32 bit systems or in compat mode */ 88798f76206SDmitry Safonov if (IS_ENABLED(CONFIG_64BIT_TIME) && in_compat_syscall()) 888ea2ce8f3SDeepa Dinamani kts.tv_nsec &= 0xFFFFFFFFUL; 889ea2ce8f3SDeepa Dinamani 890f59dd9c8SDeepa Dinamani ts->tv_nsec = kts.tv_nsec; 891f59dd9c8SDeepa Dinamani 892f59dd9c8SDeepa Dinamani return 0; 893f59dd9c8SDeepa Dinamani } 894f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(get_timespec64); 895f59dd9c8SDeepa Dinamani 896f59dd9c8SDeepa Dinamani int put_timespec64(const struct timespec64 *ts, 897ea2ce8f3SDeepa Dinamani struct __kernel_timespec __user *uts) 898f59dd9c8SDeepa Dinamani { 899ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts = { 900f59dd9c8SDeepa Dinamani .tv_sec = ts->tv_sec, 901f59dd9c8SDeepa Dinamani .tv_nsec = ts->tv_nsec 902f59dd9c8SDeepa Dinamani }; 903ea2ce8f3SDeepa Dinamani 904f59dd9c8SDeepa Dinamani return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0; 905f59dd9c8SDeepa Dinamani } 906f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(put_timespec64); 907d5b7ffbfSDeepa Dinamani 908743f5cdbSkbuild test robot static int __get_old_timespec32(struct timespec64 *ts64, 9099afc5eeeSArnd Bergmann const struct old_timespec32 __user *cts) 9101c68adf6SDeepa Dinamani { 9119afc5eeeSArnd Bergmann struct old_timespec32 ts; 9121c68adf6SDeepa Dinamani int ret; 9131c68adf6SDeepa Dinamani 9141c68adf6SDeepa Dinamani ret = copy_from_user(&ts, cts, sizeof(ts)); 9151c68adf6SDeepa Dinamani if (ret) 9161c68adf6SDeepa Dinamani return -EFAULT; 9171c68adf6SDeepa Dinamani 9181c68adf6SDeepa Dinamani ts64->tv_sec = ts.tv_sec; 9191c68adf6SDeepa Dinamani ts64->tv_nsec = ts.tv_nsec; 9201c68adf6SDeepa Dinamani 9211c68adf6SDeepa Dinamani return 0; 9221c68adf6SDeepa Dinamani } 9231c68adf6SDeepa Dinamani 924743f5cdbSkbuild test robot static int __put_old_timespec32(const struct timespec64 *ts64, 9259afc5eeeSArnd Bergmann struct old_timespec32 __user *cts) 9261c68adf6SDeepa Dinamani { 9279afc5eeeSArnd Bergmann struct old_timespec32 ts = { 9281c68adf6SDeepa Dinamani .tv_sec = ts64->tv_sec, 9291c68adf6SDeepa Dinamani .tv_nsec = ts64->tv_nsec 9301c68adf6SDeepa Dinamani }; 9311c68adf6SDeepa Dinamani return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0; 9321c68adf6SDeepa Dinamani } 9331c68adf6SDeepa Dinamani 9349afc5eeeSArnd Bergmann int get_old_timespec32(struct timespec64 *ts, const void __user *uts) 9351c68adf6SDeepa Dinamani { 9361c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 9371c68adf6SDeepa Dinamani return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; 9381c68adf6SDeepa Dinamani else 9399afc5eeeSArnd Bergmann return __get_old_timespec32(ts, uts); 9401c68adf6SDeepa Dinamani } 9419afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(get_old_timespec32); 9421c68adf6SDeepa Dinamani 9439afc5eeeSArnd Bergmann int put_old_timespec32(const struct timespec64 *ts, void __user *uts) 9441c68adf6SDeepa Dinamani { 9451c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 9461c68adf6SDeepa Dinamani return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; 9471c68adf6SDeepa Dinamani else 9489afc5eeeSArnd Bergmann return __put_old_timespec32(ts, uts); 9491c68adf6SDeepa Dinamani } 9509afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(put_old_timespec32); 9511c68adf6SDeepa Dinamani 952d5b7ffbfSDeepa Dinamani int get_itimerspec64(struct itimerspec64 *it, 953d0dd63a8SDeepa Dinamani const struct __kernel_itimerspec __user *uit) 954d5b7ffbfSDeepa Dinamani { 955d5b7ffbfSDeepa Dinamani int ret; 956d5b7ffbfSDeepa Dinamani 957d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_interval, &uit->it_interval); 958d5b7ffbfSDeepa Dinamani if (ret) 959d5b7ffbfSDeepa Dinamani return ret; 960d5b7ffbfSDeepa Dinamani 961d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_value, &uit->it_value); 962d5b7ffbfSDeepa Dinamani 963d5b7ffbfSDeepa Dinamani return ret; 964d5b7ffbfSDeepa Dinamani } 965d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(get_itimerspec64); 966d5b7ffbfSDeepa Dinamani 967d5b7ffbfSDeepa Dinamani int put_itimerspec64(const struct itimerspec64 *it, 968d0dd63a8SDeepa Dinamani struct __kernel_itimerspec __user *uit) 969d5b7ffbfSDeepa Dinamani { 970d5b7ffbfSDeepa Dinamani int ret; 971d5b7ffbfSDeepa Dinamani 972d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_interval, &uit->it_interval); 973d5b7ffbfSDeepa Dinamani if (ret) 974d5b7ffbfSDeepa Dinamani return ret; 975d5b7ffbfSDeepa Dinamani 976d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_value, &uit->it_value); 977d5b7ffbfSDeepa Dinamani 978d5b7ffbfSDeepa Dinamani return ret; 979d5b7ffbfSDeepa Dinamani } 980d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(put_itimerspec64); 981afef05cfSDeepa Dinamani 9829afc5eeeSArnd Bergmann int get_old_itimerspec32(struct itimerspec64 *its, 9839afc5eeeSArnd Bergmann const struct old_itimerspec32 __user *uits) 984afef05cfSDeepa Dinamani { 985afef05cfSDeepa Dinamani 9869afc5eeeSArnd Bergmann if (__get_old_timespec32(&its->it_interval, &uits->it_interval) || 9879afc5eeeSArnd Bergmann __get_old_timespec32(&its->it_value, &uits->it_value)) 988afef05cfSDeepa Dinamani return -EFAULT; 989afef05cfSDeepa Dinamani return 0; 990afef05cfSDeepa Dinamani } 9919afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(get_old_itimerspec32); 992afef05cfSDeepa Dinamani 9939afc5eeeSArnd Bergmann int put_old_itimerspec32(const struct itimerspec64 *its, 9949afc5eeeSArnd Bergmann struct old_itimerspec32 __user *uits) 995afef05cfSDeepa Dinamani { 9969afc5eeeSArnd Bergmann if (__put_old_timespec32(&its->it_interval, &uits->it_interval) || 9979afc5eeeSArnd Bergmann __put_old_timespec32(&its->it_value, &uits->it_value)) 998afef05cfSDeepa Dinamani return -EFAULT; 999afef05cfSDeepa Dinamani return 0; 1000afef05cfSDeepa Dinamani } 10019afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(put_old_itimerspec32); 1002