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 */ 622a785996SArnd Bergmann SYSCALL_DEFINE1(time, __kernel_old_time_t __user *, tloc) 635cee9645SThomas Gleixner { 642a785996SArnd Bergmann __kernel_old_time_t i = (__kernel_old_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 812a785996SArnd Bergmann SYSCALL_DEFINE1(stime, __kernel_old_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 14075d319c0SArnd Bergmann SYSCALL_DEFINE2(gettimeofday, struct __kernel_old_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) { 1821d6acc18SMukesh 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 1995e0fb1b5SArnd Bergmann SYSCALL_DEFINE2(settimeofday, struct __kernel_old_timeval __user *, tv, 2005cee9645SThomas Gleixner struct timezone __user *, tz) 2015cee9645SThomas Gleixner { 2022ac00f17SDeepa Dinamani struct timespec64 new_ts; 2035cee9645SThomas Gleixner struct timezone new_tz; 2045cee9645SThomas Gleixner 2055cee9645SThomas Gleixner if (tv) { 2065e0fb1b5SArnd Bergmann if (get_user(new_ts.tv_sec, &tv->tv_sec) || 2075e0fb1b5SArnd Bergmann get_user(new_ts.tv_nsec, &tv->tv_usec)) 2085cee9645SThomas Gleixner return -EFAULT; 2096ada1fc0SSasha Levin 2105e0fb1b5SArnd Bergmann if (new_ts.tv_nsec > USEC_PER_SEC || new_ts.tv_nsec < 0) 2116ada1fc0SSasha Levin return -EINVAL; 2126ada1fc0SSasha Levin 2135e0fb1b5SArnd Bergmann new_ts.tv_nsec *= NSEC_PER_USEC; 2145cee9645SThomas Gleixner } 2155cee9645SThomas Gleixner if (tz) { 2165cee9645SThomas Gleixner if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2175cee9645SThomas Gleixner return -EFAULT; 2185cee9645SThomas Gleixner } 2195cee9645SThomas Gleixner 2202ac00f17SDeepa Dinamani return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2215cee9645SThomas Gleixner } 2225cee9645SThomas Gleixner 2232b2d0285SAl Viro #ifdef CONFIG_COMPAT 2249afc5eeeSArnd Bergmann COMPAT_SYSCALL_DEFINE2(gettimeofday, struct old_timeval32 __user *, tv, 2252b2d0285SAl Viro struct timezone __user *, tz) 2262b2d0285SAl Viro { 2272b2d0285SAl Viro if (tv) { 22833e26418SArnd Bergmann struct timespec64 ts; 2292b2d0285SAl Viro 23033e26418SArnd Bergmann ktime_get_real_ts64(&ts); 23133e26418SArnd Bergmann if (put_user(ts.tv_sec, &tv->tv_sec) || 23233e26418SArnd Bergmann put_user(ts.tv_nsec / 1000, &tv->tv_usec)) 2332b2d0285SAl Viro return -EFAULT; 2342b2d0285SAl Viro } 2352b2d0285SAl Viro if (tz) { 2362b2d0285SAl Viro if (copy_to_user(tz, &sys_tz, sizeof(sys_tz))) 2372b2d0285SAl Viro return -EFAULT; 2382b2d0285SAl Viro } 2392b2d0285SAl Viro 2402b2d0285SAl Viro return 0; 2412b2d0285SAl Viro } 2422b2d0285SAl Viro 2439afc5eeeSArnd Bergmann COMPAT_SYSCALL_DEFINE2(settimeofday, struct old_timeval32 __user *, tv, 2442b2d0285SAl Viro struct timezone __user *, tz) 2452b2d0285SAl Viro { 2462b2d0285SAl Viro struct timespec64 new_ts; 2472b2d0285SAl Viro struct timezone new_tz; 2482b2d0285SAl Viro 2492b2d0285SAl Viro if (tv) { 2505e0fb1b5SArnd Bergmann if (get_user(new_ts.tv_sec, &tv->tv_sec) || 2515e0fb1b5SArnd Bergmann get_user(new_ts.tv_nsec, &tv->tv_usec)) 2522b2d0285SAl Viro return -EFAULT; 2539176ab1bSzhengbin 2545e0fb1b5SArnd Bergmann if (new_ts.tv_nsec > USEC_PER_SEC || new_ts.tv_nsec < 0) 2559176ab1bSzhengbin return -EINVAL; 2569176ab1bSzhengbin 2575e0fb1b5SArnd Bergmann new_ts.tv_nsec *= NSEC_PER_USEC; 2582b2d0285SAl Viro } 2592b2d0285SAl Viro if (tz) { 2602b2d0285SAl Viro if (copy_from_user(&new_tz, tz, sizeof(*tz))) 2612b2d0285SAl Viro return -EFAULT; 2622b2d0285SAl Viro } 2632b2d0285SAl Viro 2642b2d0285SAl Viro return do_sys_settimeofday64(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 2652b2d0285SAl Viro } 2662b2d0285SAl Viro #endif 2672b2d0285SAl Viro 2683ca47e95SArnd Bergmann #ifdef CONFIG_64BIT 2693876ced4SDeepa Dinamani SYSCALL_DEFINE1(adjtimex, struct __kernel_timex __user *, txc_p) 2705cee9645SThomas Gleixner { 271ead25417SDeepa Dinamani struct __kernel_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 */ 278ead25417SDeepa Dinamani if (copy_from_user(&txc, txc_p, sizeof(struct __kernel_timex))) 2795cee9645SThomas Gleixner return -EFAULT; 2805cee9645SThomas Gleixner ret = do_adjtimex(&txc); 281ead25417SDeepa Dinamani return copy_to_user(txc_p, &txc, sizeof(struct __kernel_timex)) ? -EFAULT : ret; 2825cee9645SThomas Gleixner } 2833876ced4SDeepa Dinamani #endif 2845cee9645SThomas Gleixner 2854d5f007eSArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME 286ead25417SDeepa Dinamani int get_old_timex32(struct __kernel_timex *txc, const struct old_timex32 __user *utp) 2874d5f007eSArnd Bergmann { 2884d5f007eSArnd Bergmann struct old_timex32 tx32; 2893a4d44b6SAl Viro 290ead25417SDeepa Dinamani memset(txc, 0, sizeof(struct __kernel_timex)); 2914d5f007eSArnd Bergmann if (copy_from_user(&tx32, utp, sizeof(struct old_timex32))) 2924d5f007eSArnd Bergmann return -EFAULT; 2934d5f007eSArnd Bergmann 2944d5f007eSArnd Bergmann txc->modes = tx32.modes; 2954d5f007eSArnd Bergmann txc->offset = tx32.offset; 2964d5f007eSArnd Bergmann txc->freq = tx32.freq; 2974d5f007eSArnd Bergmann txc->maxerror = tx32.maxerror; 2984d5f007eSArnd Bergmann txc->esterror = tx32.esterror; 2994d5f007eSArnd Bergmann txc->status = tx32.status; 3004d5f007eSArnd Bergmann txc->constant = tx32.constant; 3014d5f007eSArnd Bergmann txc->precision = tx32.precision; 3024d5f007eSArnd Bergmann txc->tolerance = tx32.tolerance; 3034d5f007eSArnd Bergmann txc->time.tv_sec = tx32.time.tv_sec; 3044d5f007eSArnd Bergmann txc->time.tv_usec = tx32.time.tv_usec; 3054d5f007eSArnd Bergmann txc->tick = tx32.tick; 3064d5f007eSArnd Bergmann txc->ppsfreq = tx32.ppsfreq; 3074d5f007eSArnd Bergmann txc->jitter = tx32.jitter; 3084d5f007eSArnd Bergmann txc->shift = tx32.shift; 3094d5f007eSArnd Bergmann txc->stabil = tx32.stabil; 3104d5f007eSArnd Bergmann txc->jitcnt = tx32.jitcnt; 3114d5f007eSArnd Bergmann txc->calcnt = tx32.calcnt; 3124d5f007eSArnd Bergmann txc->errcnt = tx32.errcnt; 3134d5f007eSArnd Bergmann txc->stbcnt = tx32.stbcnt; 3144d5f007eSArnd Bergmann 3154d5f007eSArnd Bergmann return 0; 3164d5f007eSArnd Bergmann } 3174d5f007eSArnd Bergmann 318ead25417SDeepa Dinamani int put_old_timex32(struct old_timex32 __user *utp, const struct __kernel_timex *txc) 3194d5f007eSArnd Bergmann { 3204d5f007eSArnd Bergmann struct old_timex32 tx32; 3214d5f007eSArnd Bergmann 3224d5f007eSArnd Bergmann memset(&tx32, 0, sizeof(struct old_timex32)); 3234d5f007eSArnd Bergmann tx32.modes = txc->modes; 3244d5f007eSArnd Bergmann tx32.offset = txc->offset; 3254d5f007eSArnd Bergmann tx32.freq = txc->freq; 3264d5f007eSArnd Bergmann tx32.maxerror = txc->maxerror; 3274d5f007eSArnd Bergmann tx32.esterror = txc->esterror; 3284d5f007eSArnd Bergmann tx32.status = txc->status; 3294d5f007eSArnd Bergmann tx32.constant = txc->constant; 3304d5f007eSArnd Bergmann tx32.precision = txc->precision; 3314d5f007eSArnd Bergmann tx32.tolerance = txc->tolerance; 3324d5f007eSArnd Bergmann tx32.time.tv_sec = txc->time.tv_sec; 3334d5f007eSArnd Bergmann tx32.time.tv_usec = txc->time.tv_usec; 3344d5f007eSArnd Bergmann tx32.tick = txc->tick; 3354d5f007eSArnd Bergmann tx32.ppsfreq = txc->ppsfreq; 3364d5f007eSArnd Bergmann tx32.jitter = txc->jitter; 3374d5f007eSArnd Bergmann tx32.shift = txc->shift; 3384d5f007eSArnd Bergmann tx32.stabil = txc->stabil; 3394d5f007eSArnd Bergmann tx32.jitcnt = txc->jitcnt; 3404d5f007eSArnd Bergmann tx32.calcnt = txc->calcnt; 3414d5f007eSArnd Bergmann tx32.errcnt = txc->errcnt; 3424d5f007eSArnd Bergmann tx32.stbcnt = txc->stbcnt; 3434d5f007eSArnd Bergmann tx32.tai = txc->tai; 3444d5f007eSArnd Bergmann if (copy_to_user(utp, &tx32, sizeof(struct old_timex32))) 3454d5f007eSArnd Bergmann return -EFAULT; 3464d5f007eSArnd Bergmann return 0; 3474d5f007eSArnd Bergmann } 3484d5f007eSArnd Bergmann 3498dabe724SArnd Bergmann SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp) 3503a4d44b6SAl Viro { 351ead25417SDeepa Dinamani struct __kernel_timex txc; 3523a4d44b6SAl Viro int err, ret; 3533a4d44b6SAl Viro 3544d5f007eSArnd Bergmann err = get_old_timex32(&txc, utp); 3553a4d44b6SAl Viro if (err) 3563a4d44b6SAl Viro return err; 3573a4d44b6SAl Viro 3583a4d44b6SAl Viro ret = do_adjtimex(&txc); 3593a4d44b6SAl Viro 3604d5f007eSArnd Bergmann err = put_old_timex32(utp, &txc); 3613a4d44b6SAl Viro if (err) 3623a4d44b6SAl Viro return err; 3633a4d44b6SAl Viro 3643a4d44b6SAl Viro return ret; 3653a4d44b6SAl Viro } 3663a4d44b6SAl Viro #endif 3673a4d44b6SAl Viro 36867b3f564SRandy Dunlap /** 36967b3f564SRandy Dunlap * jiffies_to_msecs - Convert jiffies to milliseconds 37067b3f564SRandy Dunlap * @j: jiffies value 3715cee9645SThomas Gleixner * 3725cee9645SThomas Gleixner * Avoid unnecessary multiplications/divisions in the 37367b3f564SRandy Dunlap * two most common HZ cases. 37467b3f564SRandy Dunlap * 37567b3f564SRandy Dunlap * Return: milliseconds value 3765cee9645SThomas Gleixner */ 3775cee9645SThomas Gleixner unsigned int jiffies_to_msecs(const unsigned long j) 3785cee9645SThomas Gleixner { 3795cee9645SThomas Gleixner #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 3805cee9645SThomas Gleixner return (MSEC_PER_SEC / HZ) * j; 3815cee9645SThomas Gleixner #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 3825cee9645SThomas Gleixner return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); 3835cee9645SThomas Gleixner #else 3845cee9645SThomas Gleixner # if BITS_PER_LONG == 32 385abcbcb80SGeert Uytterhoeven return (HZ_TO_MSEC_MUL32 * j + (1ULL << HZ_TO_MSEC_SHR32) - 1) >> 386abcbcb80SGeert Uytterhoeven HZ_TO_MSEC_SHR32; 3875cee9645SThomas Gleixner # else 388abcbcb80SGeert Uytterhoeven return DIV_ROUND_UP(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN); 3895cee9645SThomas Gleixner # endif 3905cee9645SThomas Gleixner #endif 3915cee9645SThomas Gleixner } 3925cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_msecs); 3935cee9645SThomas Gleixner 39467b3f564SRandy Dunlap /** 39567b3f564SRandy Dunlap * jiffies_to_usecs - Convert jiffies to microseconds 39667b3f564SRandy Dunlap * @j: jiffies value 39767b3f564SRandy Dunlap * 39867b3f564SRandy Dunlap * Return: microseconds value 39967b3f564SRandy Dunlap */ 4005cee9645SThomas Gleixner unsigned int jiffies_to_usecs(const unsigned long j) 4015cee9645SThomas Gleixner { 402e0758676SFrederic Weisbecker /* 403e0758676SFrederic Weisbecker * Hz usually doesn't go much further MSEC_PER_SEC. 404e0758676SFrederic Weisbecker * jiffies_to_usecs() and usecs_to_jiffies() depend on that. 405e0758676SFrederic Weisbecker */ 406e0758676SFrederic Weisbecker BUILD_BUG_ON(HZ > USEC_PER_SEC); 407e0758676SFrederic Weisbecker 408e0758676SFrederic Weisbecker #if !(USEC_PER_SEC % HZ) 4095cee9645SThomas Gleixner return (USEC_PER_SEC / HZ) * j; 4105cee9645SThomas Gleixner #else 4115cee9645SThomas Gleixner # if BITS_PER_LONG == 32 4125cee9645SThomas Gleixner return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; 4135cee9645SThomas Gleixner # else 4145cee9645SThomas Gleixner return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; 4155cee9645SThomas Gleixner # endif 4165cee9645SThomas Gleixner #endif 4175cee9645SThomas Gleixner } 4185cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_usecs); 4195cee9645SThomas Gleixner 42067b3f564SRandy Dunlap /** 42190b6ce9cSpang.xunlei * mktime64 - Converts date to seconds. 42267b3f564SRandy Dunlap * @year0: year to convert 42367b3f564SRandy Dunlap * @mon0: month to convert 42467b3f564SRandy Dunlap * @day: day to convert 42567b3f564SRandy Dunlap * @hour: hour to convert 42667b3f564SRandy Dunlap * @min: minute to convert 42767b3f564SRandy Dunlap * @sec: second to convert 42867b3f564SRandy Dunlap * 42990b6ce9cSpang.xunlei * Converts Gregorian date to seconds since 1970-01-01 00:00:00. 4305cee9645SThomas Gleixner * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 4315cee9645SThomas Gleixner * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. 4325cee9645SThomas Gleixner * 4335cee9645SThomas Gleixner * [For the Julian calendar (which was used in Russia before 1917, 4345cee9645SThomas Gleixner * Britain & colonies before 1752, anywhere else before 1582, 4355cee9645SThomas Gleixner * and is still in use by some communities) leave out the 4365cee9645SThomas Gleixner * -year/100+year/400 terms, and add 10.] 4375cee9645SThomas Gleixner * 4385cee9645SThomas Gleixner * This algorithm was first published by Gauss (I think). 439ede5147dSDavid Howells * 440ede5147dSDavid Howells * A leap second can be indicated by calling this function with sec as 441ede5147dSDavid Howells * 60 (allowable under ISO 8601). The leap second is treated the same 442ede5147dSDavid Howells * as the following second since they don't exist in UNIX time. 443ede5147dSDavid Howells * 444ede5147dSDavid Howells * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight 445ede5147dSDavid Howells * tomorrow - (allowable under ISO 8601) is supported. 44667b3f564SRandy Dunlap * 44767b3f564SRandy Dunlap * Return: seconds since the epoch time for the given input date 4485cee9645SThomas Gleixner */ 44990b6ce9cSpang.xunlei time64_t mktime64(const unsigned int year0, const unsigned int mon0, 4505cee9645SThomas Gleixner const unsigned int day, const unsigned int hour, 4515cee9645SThomas Gleixner const unsigned int min, const unsigned int sec) 4525cee9645SThomas Gleixner { 4535cee9645SThomas Gleixner unsigned int mon = mon0, year = year0; 4545cee9645SThomas Gleixner 4555cee9645SThomas Gleixner /* 1..12 -> 11,12,1..10 */ 4565cee9645SThomas Gleixner if (0 >= (int) (mon -= 2)) { 4575cee9645SThomas Gleixner mon += 12; /* Puts Feb last since it has leap day */ 4585cee9645SThomas Gleixner year -= 1; 4595cee9645SThomas Gleixner } 4605cee9645SThomas Gleixner 46190b6ce9cSpang.xunlei return ((((time64_t) 4625cee9645SThomas Gleixner (year/4 - year/100 + year/400 + 367*mon/12 + day) + 4635cee9645SThomas Gleixner year*365 - 719499 464ede5147dSDavid Howells )*24 + hour /* now have hours - midnight tomorrow handled here */ 4655cee9645SThomas Gleixner )*60 + min /* now have minutes */ 4665cee9645SThomas Gleixner )*60 + sec; /* finally seconds */ 4675cee9645SThomas Gleixner } 46890b6ce9cSpang.xunlei EXPORT_SYMBOL(mktime64); 4695cee9645SThomas Gleixner 47046dae32fSYoungmin Nam struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec) 471a84d1169SArnd Bergmann { 472a84d1169SArnd Bergmann struct timespec64 ts = ns_to_timespec64(nsec); 473a84d1169SArnd Bergmann struct __kernel_old_timeval tv; 474a84d1169SArnd Bergmann 475a84d1169SArnd Bergmann tv.tv_sec = ts.tv_sec; 476a84d1169SArnd Bergmann tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000; 477a84d1169SArnd Bergmann 478a84d1169SArnd Bergmann return tv; 479a84d1169SArnd Bergmann } 480a84d1169SArnd Bergmann EXPORT_SYMBOL(ns_to_kernel_old_timeval); 481a84d1169SArnd Bergmann 48249cd6f86SJohn Stultz /** 483f3cb8080SRandy Dunlap * set_normalized_timespec64 - set timespec sec and nsec parts and normalize 48449cd6f86SJohn Stultz * 48549cd6f86SJohn Stultz * @ts: pointer to timespec variable to be set 48649cd6f86SJohn Stultz * @sec: seconds to set 48749cd6f86SJohn Stultz * @nsec: nanoseconds to set 48849cd6f86SJohn Stultz * 48949cd6f86SJohn Stultz * Set seconds and nanoseconds field of a timespec variable and 49049cd6f86SJohn Stultz * normalize to the timespec storage format 49149cd6f86SJohn Stultz * 49267b3f564SRandy Dunlap * Note: The tv_nsec part is always in the range of 0 <= tv_nsec < NSEC_PER_SEC. 49349cd6f86SJohn Stultz * For negative values only the tv_sec field is negative ! 49449cd6f86SJohn Stultz */ 49549cd6f86SJohn Stultz void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) 49649cd6f86SJohn Stultz { 49749cd6f86SJohn Stultz while (nsec >= NSEC_PER_SEC) { 49849cd6f86SJohn Stultz /* 49949cd6f86SJohn Stultz * The following asm() prevents the compiler from 50049cd6f86SJohn Stultz * optimising this loop into a modulo operation. See 50149cd6f86SJohn Stultz * also __iter_div_u64_rem() in include/linux/time.h 50249cd6f86SJohn Stultz */ 50349cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 50449cd6f86SJohn Stultz nsec -= NSEC_PER_SEC; 50549cd6f86SJohn Stultz ++sec; 50649cd6f86SJohn Stultz } 50749cd6f86SJohn Stultz while (nsec < 0) { 50849cd6f86SJohn Stultz asm("" : "+rm"(nsec)); 50949cd6f86SJohn Stultz nsec += NSEC_PER_SEC; 51049cd6f86SJohn Stultz --sec; 51149cd6f86SJohn Stultz } 51249cd6f86SJohn Stultz ts->tv_sec = sec; 51349cd6f86SJohn Stultz ts->tv_nsec = nsec; 51449cd6f86SJohn Stultz } 51549cd6f86SJohn Stultz EXPORT_SYMBOL(set_normalized_timespec64); 51649cd6f86SJohn Stultz 51749cd6f86SJohn Stultz /** 51849cd6f86SJohn Stultz * ns_to_timespec64 - Convert nanoseconds to timespec64 51949cd6f86SJohn Stultz * @nsec: the nanoseconds value to be converted 52049cd6f86SJohn Stultz * 52167b3f564SRandy Dunlap * Return: the timespec64 representation of the nsec parameter. 52249cd6f86SJohn Stultz */ 52346dae32fSYoungmin Nam struct timespec64 ns_to_timespec64(s64 nsec) 52449cd6f86SJohn Stultz { 52520d08736SArnd Bergmann struct timespec64 ts = { 0, 0 }; 52649cd6f86SJohn Stultz s32 rem; 52749cd6f86SJohn Stultz 52820d08736SArnd Bergmann if (likely(nsec > 0)) { 52920d08736SArnd Bergmann ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem); 53049cd6f86SJohn Stultz ts.tv_nsec = rem; 53120d08736SArnd Bergmann } else if (nsec < 0) { 53220d08736SArnd Bergmann /* 53320d08736SArnd Bergmann * With negative times, tv_sec points to the earlier 53420d08736SArnd Bergmann * second, and tv_nsec counts the nanoseconds since 53520d08736SArnd Bergmann * then, so tv_nsec is always a positive number. 53620d08736SArnd Bergmann */ 53720d08736SArnd Bergmann ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1; 53820d08736SArnd Bergmann ts.tv_nsec = NSEC_PER_SEC - rem - 1; 53920d08736SArnd Bergmann } 54049cd6f86SJohn Stultz 54149cd6f86SJohn Stultz return ts; 54249cd6f86SJohn Stultz } 54349cd6f86SJohn Stultz EXPORT_SYMBOL(ns_to_timespec64); 544abc8f96eSArnd Bergmann 545ca42aaf0SNicholas Mc Guire /** 546f3cb8080SRandy Dunlap * __msecs_to_jiffies: - convert milliseconds to jiffies 547ca42aaf0SNicholas Mc Guire * @m: time in milliseconds 548ca42aaf0SNicholas Mc Guire * 549ca42aaf0SNicholas Mc Guire * conversion is done as follows: 5505cee9645SThomas Gleixner * 5515cee9645SThomas Gleixner * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) 5525cee9645SThomas Gleixner * 5535cee9645SThomas Gleixner * - 'too large' values [that would result in larger than 5545cee9645SThomas Gleixner * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. 5555cee9645SThomas Gleixner * 5565cee9645SThomas Gleixner * - all other values are converted to jiffies by either multiplying 557ca42aaf0SNicholas Mc Guire * the input value by a factor or dividing it with a factor and 558ca42aaf0SNicholas Mc Guire * handling any 32-bit overflows. 559ca42aaf0SNicholas Mc Guire * for the details see __msecs_to_jiffies() 5605cee9645SThomas Gleixner * 561*7723bfe6SMiguel Ojeda * msecs_to_jiffies() checks for the passed in value being a constant 562ca42aaf0SNicholas Mc Guire * via __builtin_constant_p() allowing gcc to eliminate most of the 563ca42aaf0SNicholas Mc Guire * code, __msecs_to_jiffies() is called if the value passed does not 564ca42aaf0SNicholas Mc Guire * allow constant folding and the actual conversion must be done at 565ca42aaf0SNicholas Mc Guire * runtime. 566f3cb8080SRandy Dunlap * The _msecs_to_jiffies helpers are the HZ dependent conversion 567ca42aaf0SNicholas Mc Guire * routines found in include/linux/jiffies.h 56867b3f564SRandy Dunlap * 56967b3f564SRandy Dunlap * Return: jiffies value 5705cee9645SThomas Gleixner */ 571ca42aaf0SNicholas Mc Guire unsigned long __msecs_to_jiffies(const unsigned int m) 5725cee9645SThomas Gleixner { 5735cee9645SThomas Gleixner /* 5745cee9645SThomas Gleixner * Negative value, means infinite timeout: 5755cee9645SThomas Gleixner */ 5765cee9645SThomas Gleixner if ((int)m < 0) 5775cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 578ca42aaf0SNicholas Mc Guire return _msecs_to_jiffies(m); 5795cee9645SThomas Gleixner } 580ca42aaf0SNicholas Mc Guire EXPORT_SYMBOL(__msecs_to_jiffies); 5815cee9645SThomas Gleixner 58267b3f564SRandy Dunlap /** 58367b3f564SRandy Dunlap * __usecs_to_jiffies: - convert microseconds to jiffies 58467b3f564SRandy Dunlap * @u: time in milliseconds 58567b3f564SRandy Dunlap * 58667b3f564SRandy Dunlap * Return: jiffies value 58767b3f564SRandy Dunlap */ 588ae60d6a0SNicholas Mc Guire unsigned long __usecs_to_jiffies(const unsigned int u) 5895cee9645SThomas Gleixner { 5905cee9645SThomas Gleixner if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) 5915cee9645SThomas Gleixner return MAX_JIFFY_OFFSET; 592ae60d6a0SNicholas Mc Guire return _usecs_to_jiffies(u); 5935cee9645SThomas Gleixner } 594ae60d6a0SNicholas Mc Guire EXPORT_SYMBOL(__usecs_to_jiffies); 5955cee9645SThomas Gleixner 59667b3f564SRandy Dunlap /** 59767b3f564SRandy Dunlap * timespec64_to_jiffies - convert a timespec64 value to jiffies 59867b3f564SRandy Dunlap * @value: pointer to &struct timespec64 59967b3f564SRandy Dunlap * 6005cee9645SThomas Gleixner * The TICK_NSEC - 1 rounds up the value to the next resolution. Note 6015cee9645SThomas Gleixner * that a remainder subtract here would not do the right thing as the 6024bf07f65SIngo Molnar * resolution values don't fall on second boundaries. I.e. the line: 6035cee9645SThomas Gleixner * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. 604d78c9300SAndrew Hunter * Note that due to the small error in the multiplier here, this 605d78c9300SAndrew Hunter * rounding is incorrect for sufficiently large values of tv_nsec, but 606d78c9300SAndrew Hunter * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're 607d78c9300SAndrew Hunter * OK. 6085cee9645SThomas Gleixner * 6095cee9645SThomas Gleixner * Rather, we just shift the bits off the right. 6105cee9645SThomas Gleixner * 6115cee9645SThomas Gleixner * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec 6125cee9645SThomas Gleixner * value to a scaled second value. 61367b3f564SRandy Dunlap * 61467b3f564SRandy Dunlap * Return: jiffies value 6155cee9645SThomas Gleixner */ 616751addacSArnd Bergmann unsigned long 617751addacSArnd Bergmann timespec64_to_jiffies(const struct timespec64 *value) 6185cee9645SThomas Gleixner { 619751addacSArnd Bergmann u64 sec = value->tv_sec; 620751addacSArnd Bergmann long nsec = value->tv_nsec + TICK_NSEC - 1; 6215cee9645SThomas Gleixner 6225cee9645SThomas Gleixner if (sec >= MAX_SEC_IN_JIFFIES){ 6235cee9645SThomas Gleixner sec = MAX_SEC_IN_JIFFIES; 6245cee9645SThomas Gleixner nsec = 0; 6255cee9645SThomas Gleixner } 6269ca30850SBaolin Wang return ((sec * SEC_CONVERSION) + 6275cee9645SThomas Gleixner (((u64)nsec * NSEC_CONVERSION) >> 6285cee9645SThomas Gleixner (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; 6295cee9645SThomas Gleixner 6305cee9645SThomas Gleixner } 6319ca30850SBaolin Wang EXPORT_SYMBOL(timespec64_to_jiffies); 6325cee9645SThomas Gleixner 63367b3f564SRandy Dunlap /** 63467b3f564SRandy Dunlap * jiffies_to_timespec64 - convert jiffies value to &struct timespec64 63567b3f564SRandy Dunlap * @jiffies: jiffies value 63667b3f564SRandy Dunlap * @value: pointer to &struct timespec64 63767b3f564SRandy Dunlap */ 6385cee9645SThomas Gleixner void 6399ca30850SBaolin Wang jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) 6405cee9645SThomas Gleixner { 6415cee9645SThomas Gleixner /* 6425cee9645SThomas Gleixner * Convert jiffies to nanoseconds and separate with 6435cee9645SThomas Gleixner * one divide. 6445cee9645SThomas Gleixner */ 6455cee9645SThomas Gleixner u32 rem; 6465cee9645SThomas Gleixner value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 6475cee9645SThomas Gleixner NSEC_PER_SEC, &rem); 6485cee9645SThomas Gleixner value->tv_nsec = rem; 6495cee9645SThomas Gleixner } 6509ca30850SBaolin Wang EXPORT_SYMBOL(jiffies_to_timespec64); 6515cee9645SThomas Gleixner 652d78c9300SAndrew Hunter /* 6535cee9645SThomas Gleixner * Convert jiffies/jiffies_64 to clock_t and back. 6545cee9645SThomas Gleixner */ 65567b3f564SRandy Dunlap 65667b3f564SRandy Dunlap /** 65767b3f564SRandy Dunlap * jiffies_to_clock_t - Convert jiffies to clock_t 65867b3f564SRandy Dunlap * @x: jiffies value 65967b3f564SRandy Dunlap * 66067b3f564SRandy Dunlap * Return: jiffies converted to clock_t (CLOCKS_PER_SEC) 66167b3f564SRandy Dunlap */ 6625cee9645SThomas Gleixner clock_t jiffies_to_clock_t(unsigned long x) 6635cee9645SThomas Gleixner { 6645cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 6655cee9645SThomas Gleixner # if HZ < USER_HZ 6665cee9645SThomas Gleixner return x * (USER_HZ / HZ); 6675cee9645SThomas Gleixner # else 6685cee9645SThomas Gleixner return x / (HZ / USER_HZ); 6695cee9645SThomas Gleixner # endif 6705cee9645SThomas Gleixner #else 6715cee9645SThomas Gleixner return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); 6725cee9645SThomas Gleixner #endif 6735cee9645SThomas Gleixner } 6745cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_to_clock_t); 6755cee9645SThomas Gleixner 67667b3f564SRandy Dunlap /** 67767b3f564SRandy Dunlap * clock_t_to_jiffies - Convert clock_t to jiffies 67867b3f564SRandy Dunlap * @x: clock_t value 67967b3f564SRandy Dunlap * 68067b3f564SRandy Dunlap * Return: clock_t value converted to jiffies 68167b3f564SRandy Dunlap */ 6825cee9645SThomas Gleixner unsigned long clock_t_to_jiffies(unsigned long x) 6835cee9645SThomas Gleixner { 6845cee9645SThomas Gleixner #if (HZ % USER_HZ)==0 6855cee9645SThomas Gleixner if (x >= ~0UL / (HZ / USER_HZ)) 6865cee9645SThomas Gleixner return ~0UL; 6875cee9645SThomas Gleixner return x * (HZ / USER_HZ); 6885cee9645SThomas Gleixner #else 6895cee9645SThomas Gleixner /* Don't worry about loss of precision here .. */ 6905cee9645SThomas Gleixner if (x >= ~0UL / HZ * USER_HZ) 6915cee9645SThomas Gleixner return ~0UL; 6925cee9645SThomas Gleixner 6935cee9645SThomas Gleixner /* .. but do try to contain it here */ 6945cee9645SThomas Gleixner return div_u64((u64)x * HZ, USER_HZ); 6955cee9645SThomas Gleixner #endif 6965cee9645SThomas Gleixner } 6975cee9645SThomas Gleixner EXPORT_SYMBOL(clock_t_to_jiffies); 6985cee9645SThomas Gleixner 69967b3f564SRandy Dunlap /** 70067b3f564SRandy Dunlap * jiffies_64_to_clock_t - Convert jiffies_64 to clock_t 70167b3f564SRandy Dunlap * @x: jiffies_64 value 70267b3f564SRandy Dunlap * 70367b3f564SRandy Dunlap * Return: jiffies_64 value converted to 64-bit "clock_t" (CLOCKS_PER_SEC) 70467b3f564SRandy Dunlap */ 7055cee9645SThomas Gleixner u64 jiffies_64_to_clock_t(u64 x) 7065cee9645SThomas Gleixner { 7075cee9645SThomas Gleixner #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 7085cee9645SThomas Gleixner # if HZ < USER_HZ 7095cee9645SThomas Gleixner x = div_u64(x * USER_HZ, HZ); 7105cee9645SThomas Gleixner # elif HZ > USER_HZ 7115cee9645SThomas Gleixner x = div_u64(x, HZ / USER_HZ); 7125cee9645SThomas Gleixner # else 7135cee9645SThomas Gleixner /* Nothing to do */ 7145cee9645SThomas Gleixner # endif 7155cee9645SThomas Gleixner #else 7165cee9645SThomas Gleixner /* 7175cee9645SThomas Gleixner * There are better ways that don't overflow early, 7185cee9645SThomas Gleixner * but even this doesn't overflow in hundreds of years 7195cee9645SThomas Gleixner * in 64 bits, so.. 7205cee9645SThomas Gleixner */ 7215cee9645SThomas Gleixner x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); 7225cee9645SThomas Gleixner #endif 7235cee9645SThomas Gleixner return x; 7245cee9645SThomas Gleixner } 7255cee9645SThomas Gleixner EXPORT_SYMBOL(jiffies_64_to_clock_t); 7265cee9645SThomas Gleixner 72767b3f564SRandy Dunlap /** 72867b3f564SRandy Dunlap * nsec_to_clock_t - Convert nsec value to clock_t 72967b3f564SRandy Dunlap * @x: nsec value 73067b3f564SRandy Dunlap * 73167b3f564SRandy Dunlap * Return: nsec value converted to 64-bit "clock_t" (CLOCKS_PER_SEC) 73267b3f564SRandy Dunlap */ 7335cee9645SThomas Gleixner u64 nsec_to_clock_t(u64 x) 7345cee9645SThomas Gleixner { 7355cee9645SThomas Gleixner #if (NSEC_PER_SEC % USER_HZ) == 0 7365cee9645SThomas Gleixner return div_u64(x, NSEC_PER_SEC / USER_HZ); 7375cee9645SThomas Gleixner #elif (USER_HZ % 512) == 0 7385cee9645SThomas Gleixner return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); 7395cee9645SThomas Gleixner #else 7405cee9645SThomas Gleixner /* 7415cee9645SThomas Gleixner * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, 7425cee9645SThomas Gleixner * overflow after 64.99 years. 7435cee9645SThomas Gleixner * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... 7445cee9645SThomas Gleixner */ 7455cee9645SThomas Gleixner return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); 7465cee9645SThomas Gleixner #endif 7475cee9645SThomas Gleixner } 7485cee9645SThomas Gleixner 74967b3f564SRandy Dunlap /** 75067b3f564SRandy Dunlap * jiffies64_to_nsecs - Convert jiffies64 to nanoseconds 75167b3f564SRandy Dunlap * @j: jiffies64 value 75267b3f564SRandy Dunlap * 75367b3f564SRandy Dunlap * Return: nanoseconds value 75467b3f564SRandy Dunlap */ 75507e5f5e3SFrederic Weisbecker u64 jiffies64_to_nsecs(u64 j) 75607e5f5e3SFrederic Weisbecker { 75707e5f5e3SFrederic Weisbecker #if !(NSEC_PER_SEC % HZ) 75807e5f5e3SFrederic Weisbecker return (NSEC_PER_SEC / HZ) * j; 75907e5f5e3SFrederic Weisbecker # else 76007e5f5e3SFrederic Weisbecker return div_u64(j * HZ_TO_NSEC_NUM, HZ_TO_NSEC_DEN); 76107e5f5e3SFrederic Weisbecker #endif 76207e5f5e3SFrederic Weisbecker } 76307e5f5e3SFrederic Weisbecker EXPORT_SYMBOL(jiffies64_to_nsecs); 76407e5f5e3SFrederic Weisbecker 76567b3f564SRandy Dunlap /** 76667b3f564SRandy Dunlap * jiffies64_to_msecs - Convert jiffies64 to milliseconds 76767b3f564SRandy Dunlap * @j: jiffies64 value 76867b3f564SRandy Dunlap * 76967b3f564SRandy Dunlap * Return: milliseconds value 77067b3f564SRandy Dunlap */ 7713b15d09fSLi RongQing u64 jiffies64_to_msecs(const u64 j) 7723b15d09fSLi RongQing { 7733b15d09fSLi RongQing #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 7743b15d09fSLi RongQing return (MSEC_PER_SEC / HZ) * j; 7753b15d09fSLi RongQing #else 7763b15d09fSLi RongQing return div_u64(j * HZ_TO_MSEC_NUM, HZ_TO_MSEC_DEN); 7773b15d09fSLi RongQing #endif 7783b15d09fSLi RongQing } 7793b15d09fSLi RongQing EXPORT_SYMBOL(jiffies64_to_msecs); 7803b15d09fSLi RongQing 7815cee9645SThomas Gleixner /** 7825cee9645SThomas Gleixner * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 7835cee9645SThomas Gleixner * 7845cee9645SThomas Gleixner * @n: nsecs in u64 7855cee9645SThomas Gleixner * 7865cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 7875cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 7885cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 7895cee9645SThomas Gleixner * 7905cee9645SThomas Gleixner * note: 7915cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 7925cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 79367b3f564SRandy Dunlap * 79467b3f564SRandy Dunlap * Return: nsecs converted to jiffies64 value 7955cee9645SThomas Gleixner */ 7965cee9645SThomas Gleixner u64 nsecs_to_jiffies64(u64 n) 7975cee9645SThomas Gleixner { 7985cee9645SThomas Gleixner #if (NSEC_PER_SEC % HZ) == 0 7995cee9645SThomas Gleixner /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ 8005cee9645SThomas Gleixner return div_u64(n, NSEC_PER_SEC / HZ); 8015cee9645SThomas Gleixner #elif (HZ % 512) == 0 8025cee9645SThomas Gleixner /* overflow after 292 years if HZ = 1024 */ 8035cee9645SThomas Gleixner return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); 8045cee9645SThomas Gleixner #else 8055cee9645SThomas Gleixner /* 8065cee9645SThomas Gleixner * Generic case - optimized for cases where HZ is a multiple of 3. 8075cee9645SThomas Gleixner * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. 8085cee9645SThomas Gleixner */ 8095cee9645SThomas Gleixner return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); 8105cee9645SThomas Gleixner #endif 8115cee9645SThomas Gleixner } 8127bd0e226SDaniel Vetter EXPORT_SYMBOL(nsecs_to_jiffies64); 8135cee9645SThomas Gleixner 8145cee9645SThomas Gleixner /** 8155cee9645SThomas Gleixner * nsecs_to_jiffies - Convert nsecs in u64 to jiffies 8165cee9645SThomas Gleixner * 8175cee9645SThomas Gleixner * @n: nsecs in u64 8185cee9645SThomas Gleixner * 8195cee9645SThomas Gleixner * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. 8205cee9645SThomas Gleixner * And this doesn't return MAX_JIFFY_OFFSET since this function is designed 8215cee9645SThomas Gleixner * for scheduler, not for use in device drivers to calculate timeout value. 8225cee9645SThomas Gleixner * 8235cee9645SThomas Gleixner * note: 8245cee9645SThomas Gleixner * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) 8255cee9645SThomas Gleixner * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years 82667b3f564SRandy Dunlap * 82767b3f564SRandy Dunlap * Return: nsecs converted to jiffies value 8285cee9645SThomas Gleixner */ 8295cee9645SThomas Gleixner unsigned long nsecs_to_jiffies(u64 n) 8305cee9645SThomas Gleixner { 8315cee9645SThomas Gleixner return (unsigned long)nsecs_to_jiffies64(n); 8325cee9645SThomas Gleixner } 833d560fed6SThomas Gleixner EXPORT_SYMBOL_GPL(nsecs_to_jiffies); 8345cee9645SThomas Gleixner 83567b3f564SRandy Dunlap /** 83667b3f564SRandy Dunlap * timespec64_add_safe - Add two timespec64 values and do a safety check 83767b3f564SRandy Dunlap * for overflow. 83867b3f564SRandy Dunlap * @lhs: first (left) timespec64 to add 83967b3f564SRandy Dunlap * @rhs: second (right) timespec64 to add 84067b3f564SRandy Dunlap * 841bc2c53e5SDeepa Dinamani * It's assumed that both values are valid (>= 0). 842bc2c53e5SDeepa Dinamani * And, each timespec64 is in normalized form. 84367b3f564SRandy Dunlap * 84467b3f564SRandy Dunlap * Return: sum of @lhs + @rhs 845bc2c53e5SDeepa Dinamani */ 846bc2c53e5SDeepa Dinamani struct timespec64 timespec64_add_safe(const struct timespec64 lhs, 847bc2c53e5SDeepa Dinamani const struct timespec64 rhs) 848bc2c53e5SDeepa Dinamani { 849bc2c53e5SDeepa Dinamani struct timespec64 res; 850bc2c53e5SDeepa Dinamani 851469e857fSVegard Nossum set_normalized_timespec64(&res, (timeu64_t) lhs.tv_sec + rhs.tv_sec, 852bc2c53e5SDeepa Dinamani lhs.tv_nsec + rhs.tv_nsec); 853bc2c53e5SDeepa Dinamani 854bc2c53e5SDeepa Dinamani if (unlikely(res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)) { 855bc2c53e5SDeepa Dinamani res.tv_sec = TIME64_MAX; 856bc2c53e5SDeepa Dinamani res.tv_nsec = 0; 857bc2c53e5SDeepa Dinamani } 858bc2c53e5SDeepa Dinamani 859bc2c53e5SDeepa Dinamani return res; 860bc2c53e5SDeepa Dinamani } 861f59dd9c8SDeepa Dinamani 86267b3f564SRandy Dunlap /** 86367b3f564SRandy Dunlap * get_timespec64 - get user's time value into kernel space 86467b3f564SRandy Dunlap * @ts: destination &struct timespec64 86567b3f564SRandy Dunlap * @uts: user's time value as &struct __kernel_timespec 86667b3f564SRandy Dunlap * 86767b3f564SRandy Dunlap * Handles compat or 32-bit modes. 86867b3f564SRandy Dunlap * 86967b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 87067b3f564SRandy Dunlap */ 871f59dd9c8SDeepa Dinamani int get_timespec64(struct timespec64 *ts, 872ea2ce8f3SDeepa Dinamani const struct __kernel_timespec __user *uts) 873f59dd9c8SDeepa Dinamani { 874ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts; 875f59dd9c8SDeepa Dinamani int ret; 876f59dd9c8SDeepa Dinamani 877f59dd9c8SDeepa Dinamani ret = copy_from_user(&kts, uts, sizeof(kts)); 878f59dd9c8SDeepa Dinamani if (ret) 879f59dd9c8SDeepa Dinamani return -EFAULT; 880f59dd9c8SDeepa Dinamani 881f59dd9c8SDeepa Dinamani ts->tv_sec = kts.tv_sec; 882ea2ce8f3SDeepa Dinamani 883043cf468SLinus Torvalds /* Zero out the padding in compat mode */ 8843ca47e95SArnd Bergmann if (in_compat_syscall()) 885ea2ce8f3SDeepa Dinamani kts.tv_nsec &= 0xFFFFFFFFUL; 886ea2ce8f3SDeepa Dinamani 887043cf468SLinus Torvalds /* In 32-bit mode, this drops the padding */ 888f59dd9c8SDeepa Dinamani ts->tv_nsec = kts.tv_nsec; 889f59dd9c8SDeepa Dinamani 890f59dd9c8SDeepa Dinamani return 0; 891f59dd9c8SDeepa Dinamani } 892f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(get_timespec64); 893f59dd9c8SDeepa Dinamani 89467b3f564SRandy Dunlap /** 89567b3f564SRandy Dunlap * put_timespec64 - convert timespec64 value to __kernel_timespec format and 89667b3f564SRandy Dunlap * copy the latter to userspace 89767b3f564SRandy Dunlap * @ts: input &struct timespec64 89867b3f564SRandy Dunlap * @uts: user's &struct __kernel_timespec 89967b3f564SRandy Dunlap * 90067b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 90167b3f564SRandy Dunlap */ 902f59dd9c8SDeepa Dinamani int put_timespec64(const struct timespec64 *ts, 903ea2ce8f3SDeepa Dinamani struct __kernel_timespec __user *uts) 904f59dd9c8SDeepa Dinamani { 905ea2ce8f3SDeepa Dinamani struct __kernel_timespec kts = { 906f59dd9c8SDeepa Dinamani .tv_sec = ts->tv_sec, 907f59dd9c8SDeepa Dinamani .tv_nsec = ts->tv_nsec 908f59dd9c8SDeepa Dinamani }; 909ea2ce8f3SDeepa Dinamani 910f59dd9c8SDeepa Dinamani return copy_to_user(uts, &kts, sizeof(kts)) ? -EFAULT : 0; 911f59dd9c8SDeepa Dinamani } 912f59dd9c8SDeepa Dinamani EXPORT_SYMBOL_GPL(put_timespec64); 913d5b7ffbfSDeepa Dinamani 914743f5cdbSkbuild test robot static int __get_old_timespec32(struct timespec64 *ts64, 9159afc5eeeSArnd Bergmann const struct old_timespec32 __user *cts) 9161c68adf6SDeepa Dinamani { 9179afc5eeeSArnd Bergmann struct old_timespec32 ts; 9181c68adf6SDeepa Dinamani int ret; 9191c68adf6SDeepa Dinamani 9201c68adf6SDeepa Dinamani ret = copy_from_user(&ts, cts, sizeof(ts)); 9211c68adf6SDeepa Dinamani if (ret) 9221c68adf6SDeepa Dinamani return -EFAULT; 9231c68adf6SDeepa Dinamani 9241c68adf6SDeepa Dinamani ts64->tv_sec = ts.tv_sec; 9251c68adf6SDeepa Dinamani ts64->tv_nsec = ts.tv_nsec; 9261c68adf6SDeepa Dinamani 9271c68adf6SDeepa Dinamani return 0; 9281c68adf6SDeepa Dinamani } 9291c68adf6SDeepa Dinamani 930743f5cdbSkbuild test robot static int __put_old_timespec32(const struct timespec64 *ts64, 9319afc5eeeSArnd Bergmann struct old_timespec32 __user *cts) 9321c68adf6SDeepa Dinamani { 9339afc5eeeSArnd Bergmann struct old_timespec32 ts = { 9341c68adf6SDeepa Dinamani .tv_sec = ts64->tv_sec, 9351c68adf6SDeepa Dinamani .tv_nsec = ts64->tv_nsec 9361c68adf6SDeepa Dinamani }; 9371c68adf6SDeepa Dinamani return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0; 9381c68adf6SDeepa Dinamani } 9391c68adf6SDeepa Dinamani 94067b3f564SRandy Dunlap /** 94167b3f564SRandy Dunlap * get_old_timespec32 - get user's old-format time value into kernel space 94267b3f564SRandy Dunlap * @ts: destination &struct timespec64 94367b3f564SRandy Dunlap * @uts: user's old-format time value (&struct old_timespec32) 94467b3f564SRandy Dunlap * 94567b3f564SRandy Dunlap * Handles X86_X32_ABI compatibility conversion. 94667b3f564SRandy Dunlap * 94767b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 94867b3f564SRandy Dunlap */ 9499afc5eeeSArnd Bergmann int get_old_timespec32(struct timespec64 *ts, const void __user *uts) 9501c68adf6SDeepa Dinamani { 9511c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 9521c68adf6SDeepa Dinamani return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0; 9531c68adf6SDeepa Dinamani else 9549afc5eeeSArnd Bergmann return __get_old_timespec32(ts, uts); 9551c68adf6SDeepa Dinamani } 9569afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(get_old_timespec32); 9571c68adf6SDeepa Dinamani 95867b3f564SRandy Dunlap /** 95967b3f564SRandy Dunlap * put_old_timespec32 - convert timespec64 value to &struct old_timespec32 and 96067b3f564SRandy Dunlap * copy the latter to userspace 96167b3f564SRandy Dunlap * @ts: input &struct timespec64 96267b3f564SRandy Dunlap * @uts: user's &struct old_timespec32 96367b3f564SRandy Dunlap * 96467b3f564SRandy Dunlap * Handles X86_X32_ABI compatibility conversion. 96567b3f564SRandy Dunlap * 96667b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 96767b3f564SRandy Dunlap */ 9689afc5eeeSArnd Bergmann int put_old_timespec32(const struct timespec64 *ts, void __user *uts) 9691c68adf6SDeepa Dinamani { 9701c68adf6SDeepa Dinamani if (COMPAT_USE_64BIT_TIME) 9711c68adf6SDeepa Dinamani return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0; 9721c68adf6SDeepa Dinamani else 9739afc5eeeSArnd Bergmann return __put_old_timespec32(ts, uts); 9741c68adf6SDeepa Dinamani } 9759afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(put_old_timespec32); 9761c68adf6SDeepa Dinamani 97767b3f564SRandy Dunlap /** 97867b3f564SRandy Dunlap * get_itimerspec64 - get user's &struct __kernel_itimerspec into kernel space 97967b3f564SRandy Dunlap * @it: destination &struct itimerspec64 98067b3f564SRandy Dunlap * @uit: user's &struct __kernel_itimerspec 98167b3f564SRandy Dunlap * 98267b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 98367b3f564SRandy Dunlap */ 984d5b7ffbfSDeepa Dinamani int get_itimerspec64(struct itimerspec64 *it, 985d0dd63a8SDeepa Dinamani const struct __kernel_itimerspec __user *uit) 986d5b7ffbfSDeepa Dinamani { 987d5b7ffbfSDeepa Dinamani int ret; 988d5b7ffbfSDeepa Dinamani 989d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_interval, &uit->it_interval); 990d5b7ffbfSDeepa Dinamani if (ret) 991d5b7ffbfSDeepa Dinamani return ret; 992d5b7ffbfSDeepa Dinamani 993d5b7ffbfSDeepa Dinamani ret = get_timespec64(&it->it_value, &uit->it_value); 994d5b7ffbfSDeepa Dinamani 995d5b7ffbfSDeepa Dinamani return ret; 996d5b7ffbfSDeepa Dinamani } 997d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(get_itimerspec64); 998d5b7ffbfSDeepa Dinamani 99967b3f564SRandy Dunlap /** 100067b3f564SRandy Dunlap * put_itimerspec64 - convert &struct itimerspec64 to __kernel_itimerspec format 100167b3f564SRandy Dunlap * and copy the latter to userspace 100267b3f564SRandy Dunlap * @it: input &struct itimerspec64 100367b3f564SRandy Dunlap * @uit: user's &struct __kernel_itimerspec 100467b3f564SRandy Dunlap * 100567b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 100667b3f564SRandy Dunlap */ 1007d5b7ffbfSDeepa Dinamani int put_itimerspec64(const struct itimerspec64 *it, 1008d0dd63a8SDeepa Dinamani struct __kernel_itimerspec __user *uit) 1009d5b7ffbfSDeepa Dinamani { 1010d5b7ffbfSDeepa Dinamani int ret; 1011d5b7ffbfSDeepa Dinamani 1012d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_interval, &uit->it_interval); 1013d5b7ffbfSDeepa Dinamani if (ret) 1014d5b7ffbfSDeepa Dinamani return ret; 1015d5b7ffbfSDeepa Dinamani 1016d5b7ffbfSDeepa Dinamani ret = put_timespec64(&it->it_value, &uit->it_value); 1017d5b7ffbfSDeepa Dinamani 1018d5b7ffbfSDeepa Dinamani return ret; 1019d5b7ffbfSDeepa Dinamani } 1020d5b7ffbfSDeepa Dinamani EXPORT_SYMBOL_GPL(put_itimerspec64); 1021afef05cfSDeepa Dinamani 102267b3f564SRandy Dunlap /** 102367b3f564SRandy Dunlap * get_old_itimerspec32 - get user's &struct old_itimerspec32 into kernel space 102467b3f564SRandy Dunlap * @its: destination &struct itimerspec64 102567b3f564SRandy Dunlap * @uits: user's &struct old_itimerspec32 102667b3f564SRandy Dunlap * 102767b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 102867b3f564SRandy Dunlap */ 10299afc5eeeSArnd Bergmann int get_old_itimerspec32(struct itimerspec64 *its, 10309afc5eeeSArnd Bergmann const struct old_itimerspec32 __user *uits) 1031afef05cfSDeepa Dinamani { 1032afef05cfSDeepa Dinamani 10339afc5eeeSArnd Bergmann if (__get_old_timespec32(&its->it_interval, &uits->it_interval) || 10349afc5eeeSArnd Bergmann __get_old_timespec32(&its->it_value, &uits->it_value)) 1035afef05cfSDeepa Dinamani return -EFAULT; 1036afef05cfSDeepa Dinamani return 0; 1037afef05cfSDeepa Dinamani } 10389afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(get_old_itimerspec32); 1039afef05cfSDeepa Dinamani 104067b3f564SRandy Dunlap /** 104167b3f564SRandy Dunlap * put_old_itimerspec32 - convert &struct itimerspec64 to &struct 104267b3f564SRandy Dunlap * old_itimerspec32 and copy the latter to userspace 104367b3f564SRandy Dunlap * @its: input &struct itimerspec64 104467b3f564SRandy Dunlap * @uits: user's &struct old_itimerspec32 104567b3f564SRandy Dunlap * 104667b3f564SRandy Dunlap * Return: %0 on success or negative errno on error 104767b3f564SRandy Dunlap */ 10489afc5eeeSArnd Bergmann int put_old_itimerspec32(const struct itimerspec64 *its, 10499afc5eeeSArnd Bergmann struct old_itimerspec32 __user *uits) 1050afef05cfSDeepa Dinamani { 10519afc5eeeSArnd Bergmann if (__put_old_timespec32(&its->it_interval, &uits->it_interval) || 10529afc5eeeSArnd Bergmann __put_old_timespec32(&its->it_value, &uits->it_value)) 1053afef05cfSDeepa Dinamani return -EFAULT; 1054afef05cfSDeepa Dinamani return 0; 1055afef05cfSDeepa Dinamani } 10569afc5eeeSArnd Bergmann EXPORT_SYMBOL_GPL(put_old_itimerspec32); 1057