15cee9645SThomas Gleixner /* 25cee9645SThomas Gleixner * linux/kernel/posix-timers.c 35cee9645SThomas Gleixner * 45cee9645SThomas Gleixner * 55cee9645SThomas Gleixner * 2002-10-15 Posix Clocks & timers 65cee9645SThomas Gleixner * by George Anzinger george@mvista.com 75cee9645SThomas Gleixner * 85cee9645SThomas Gleixner * Copyright (C) 2002 2003 by MontaVista Software. 95cee9645SThomas Gleixner * 105cee9645SThomas Gleixner * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug. 115cee9645SThomas Gleixner * Copyright (C) 2004 Boris Hu 125cee9645SThomas Gleixner * 135cee9645SThomas Gleixner * This program is free software; you can redistribute it and/or modify 145cee9645SThomas Gleixner * it under the terms of the GNU General Public License as published by 155cee9645SThomas Gleixner * the Free Software Foundation; either version 2 of the License, or (at 165cee9645SThomas Gleixner * your option) any later version. 175cee9645SThomas Gleixner * 185cee9645SThomas Gleixner * This program is distributed in the hope that it will be useful, but 195cee9645SThomas Gleixner * WITHOUT ANY WARRANTY; without even the implied warranty of 205cee9645SThomas Gleixner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 215cee9645SThomas Gleixner * General Public License for more details. 225cee9645SThomas Gleixner 235cee9645SThomas Gleixner * You should have received a copy of the GNU General Public License 245cee9645SThomas Gleixner * along with this program; if not, write to the Free Software 255cee9645SThomas Gleixner * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 265cee9645SThomas Gleixner * 275cee9645SThomas Gleixner * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA 285cee9645SThomas Gleixner */ 295cee9645SThomas Gleixner 305cee9645SThomas Gleixner /* These are all the functions necessary to implement 315cee9645SThomas Gleixner * POSIX clocks & timers 325cee9645SThomas Gleixner */ 335cee9645SThomas Gleixner #include <linux/mm.h> 345cee9645SThomas Gleixner #include <linux/interrupt.h> 355cee9645SThomas Gleixner #include <linux/slab.h> 365cee9645SThomas Gleixner #include <linux/time.h> 375cee9645SThomas Gleixner #include <linux/mutex.h> 3861855b6bSIngo Molnar #include <linux/sched/task.h> 395cee9645SThomas Gleixner 407c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 415cee9645SThomas Gleixner #include <linux/list.h> 425cee9645SThomas Gleixner #include <linux/init.h> 435cee9645SThomas Gleixner #include <linux/compiler.h> 445cee9645SThomas Gleixner #include <linux/hash.h> 455cee9645SThomas Gleixner #include <linux/posix-clock.h> 465cee9645SThomas Gleixner #include <linux/posix-timers.h> 475cee9645SThomas Gleixner #include <linux/syscalls.h> 485cee9645SThomas Gleixner #include <linux/wait.h> 495cee9645SThomas Gleixner #include <linux/workqueue.h> 505cee9645SThomas Gleixner #include <linux/export.h> 515cee9645SThomas Gleixner #include <linux/hashtable.h> 52edbeda46SAl Viro #include <linux/compat.h> 5319b558dbSThomas Gleixner #include <linux/nospec.h> 545cee9645SThomas Gleixner 558b094cd0SThomas Gleixner #include "timekeeping.h" 56bab0aae9SThomas Gleixner #include "posix-timers.h" 578b094cd0SThomas Gleixner 585cee9645SThomas Gleixner /* 595cee9645SThomas Gleixner * Management arrays for POSIX timers. Timers are now kept in static hash table 605cee9645SThomas Gleixner * with 512 entries. 615cee9645SThomas Gleixner * Timer ids are allocated by local routine, which selects proper hash head by 625cee9645SThomas Gleixner * key, constructed from current->signal address and per signal struct counter. 635cee9645SThomas Gleixner * This keeps timer ids unique per process, but now they can intersect between 645cee9645SThomas Gleixner * processes. 655cee9645SThomas Gleixner */ 665cee9645SThomas Gleixner 675cee9645SThomas Gleixner /* 685cee9645SThomas Gleixner * Lets keep our timers in a slab cache :-) 695cee9645SThomas Gleixner */ 705cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache; 715cee9645SThomas Gleixner 725cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9); 735cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock); 745cee9645SThomas Gleixner 756631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[]; 766631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id); 7767edab48SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic; 786631fa12SThomas Gleixner 795cee9645SThomas Gleixner /* 805cee9645SThomas Gleixner * we assume that the new SIGEV_THREAD_ID shares no bits with the other 815cee9645SThomas Gleixner * SIGEV values. Here we put out an error if this assumption fails. 825cee9645SThomas Gleixner */ 835cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ 845cee9645SThomas Gleixner ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD)) 855cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!" 865cee9645SThomas Gleixner #endif 875cee9645SThomas Gleixner 885cee9645SThomas Gleixner /* 895cee9645SThomas Gleixner * parisc wants ENOTSUP instead of EOPNOTSUPP 905cee9645SThomas Gleixner */ 915cee9645SThomas Gleixner #ifndef ENOTSUP 925cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP EOPNOTSUPP 935cee9645SThomas Gleixner #else 945cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP ENOTSUP 955cee9645SThomas Gleixner #endif 965cee9645SThomas Gleixner 975cee9645SThomas Gleixner /* 985cee9645SThomas Gleixner * The timer ID is turned into a timer address by idr_find(). 995cee9645SThomas Gleixner * Verifying a valid ID consists of: 1005cee9645SThomas Gleixner * 1015cee9645SThomas Gleixner * a) checking that idr_find() returns other than -1. 1025cee9645SThomas Gleixner * b) checking that the timer id matches the one in the timer itself. 1035cee9645SThomas Gleixner * c) that the timer owner is in the callers thread group. 1045cee9645SThomas Gleixner */ 1055cee9645SThomas Gleixner 1065cee9645SThomas Gleixner /* 1075cee9645SThomas Gleixner * CLOCKs: The POSIX standard calls for a couple of clocks and allows us 1085cee9645SThomas Gleixner * to implement others. This structure defines the various 1095cee9645SThomas Gleixner * clocks. 1105cee9645SThomas Gleixner * 1115cee9645SThomas Gleixner * RESOLUTION: Clock resolution is used to round up timer and interval 1125cee9645SThomas Gleixner * times, NOT to report clock times, which are reported with as 1135cee9645SThomas Gleixner * much resolution as the system can muster. In some cases this 1145cee9645SThomas Gleixner * resolution may depend on the underlying clock hardware and 1155cee9645SThomas Gleixner * may not be quantifiable until run time, and only then is the 1165cee9645SThomas Gleixner * necessary code is written. The standard says we should say 1175cee9645SThomas Gleixner * something about this issue in the documentation... 1185cee9645SThomas Gleixner * 1195cee9645SThomas Gleixner * FUNCTIONS: The CLOCKs structure defines possible functions to 1205cee9645SThomas Gleixner * handle various clock functions. 1215cee9645SThomas Gleixner * 1225cee9645SThomas Gleixner * The standard POSIX timer management code assumes the 1235cee9645SThomas Gleixner * following: 1.) The k_itimer struct (sched.h) is used for 1245cee9645SThomas Gleixner * the timer. 2.) The list, it_lock, it_clock, it_id and 1255cee9645SThomas Gleixner * it_pid fields are not modified by timer code. 1265cee9645SThomas Gleixner * 1275cee9645SThomas Gleixner * Permissions: It is assumed that the clock_settime() function defined 1285cee9645SThomas Gleixner * for each clock will take care of permission checks. Some 1295cee9645SThomas Gleixner * clocks may be set able by any user (i.e. local process 1305cee9645SThomas Gleixner * clocks) others not. Currently the only set able clock we 1315cee9645SThomas Gleixner * have is CLOCK_REALTIME and its high res counter part, both of 1325cee9645SThomas Gleixner * which we beg off on and pass to do_sys_settimeofday(). 1335cee9645SThomas Gleixner */ 1345cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); 1355cee9645SThomas Gleixner 1365cee9645SThomas Gleixner #define lock_timer(tid, flags) \ 1375cee9645SThomas Gleixner ({ struct k_itimer *__timr; \ 1385cee9645SThomas Gleixner __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \ 1395cee9645SThomas Gleixner __timr; \ 1405cee9645SThomas Gleixner }) 1415cee9645SThomas Gleixner 1425cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr) 1435cee9645SThomas Gleixner { 1445cee9645SThomas Gleixner return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable)); 1455cee9645SThomas Gleixner } 1465cee9645SThomas Gleixner 1475cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head, 1485cee9645SThomas Gleixner struct signal_struct *sig, 1495cee9645SThomas Gleixner timer_t id) 1505cee9645SThomas Gleixner { 1515cee9645SThomas Gleixner struct k_itimer *timer; 1525cee9645SThomas Gleixner 1535cee9645SThomas Gleixner hlist_for_each_entry_rcu(timer, head, t_hash) { 1545cee9645SThomas Gleixner if ((timer->it_signal == sig) && (timer->it_id == id)) 1555cee9645SThomas Gleixner return timer; 1565cee9645SThomas Gleixner } 1575cee9645SThomas Gleixner return NULL; 1585cee9645SThomas Gleixner } 1595cee9645SThomas Gleixner 1605cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id) 1615cee9645SThomas Gleixner { 1625cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1635cee9645SThomas Gleixner struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)]; 1645cee9645SThomas Gleixner 1655cee9645SThomas Gleixner return __posix_timers_find(head, sig, id); 1665cee9645SThomas Gleixner } 1675cee9645SThomas Gleixner 1685cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer) 1695cee9645SThomas Gleixner { 1705cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1715cee9645SThomas Gleixner int first_free_id = sig->posix_timer_id; 1725cee9645SThomas Gleixner struct hlist_head *head; 1735cee9645SThomas Gleixner int ret = -ENOENT; 1745cee9645SThomas Gleixner 1755cee9645SThomas Gleixner do { 1765cee9645SThomas Gleixner spin_lock(&hash_lock); 1775cee9645SThomas Gleixner head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; 1785cee9645SThomas Gleixner if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { 1795cee9645SThomas Gleixner hlist_add_head_rcu(&timer->t_hash, head); 1805cee9645SThomas Gleixner ret = sig->posix_timer_id; 1815cee9645SThomas Gleixner } 1825cee9645SThomas Gleixner if (++sig->posix_timer_id < 0) 1835cee9645SThomas Gleixner sig->posix_timer_id = 0; 1845cee9645SThomas Gleixner if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT)) 1855cee9645SThomas Gleixner /* Loop over all possible ids completed */ 1865cee9645SThomas Gleixner ret = -EAGAIN; 1875cee9645SThomas Gleixner spin_unlock(&hash_lock); 1885cee9645SThomas Gleixner } while (ret == -ENOENT); 1895cee9645SThomas Gleixner return ret; 1905cee9645SThomas Gleixner } 1915cee9645SThomas Gleixner 1925cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) 1935cee9645SThomas Gleixner { 1945cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, flags); 1955cee9645SThomas Gleixner } 1965cee9645SThomas Gleixner 1975cee9645SThomas Gleixner /* Get clock_realtime */ 1983c9c12f4SDeepa Dinamani static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp) 1995cee9645SThomas Gleixner { 2003c9c12f4SDeepa Dinamani ktime_get_real_ts64(tp); 2015cee9645SThomas Gleixner return 0; 2025cee9645SThomas Gleixner } 2035cee9645SThomas Gleixner 2045cee9645SThomas Gleixner /* Set clock_realtime */ 2055cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock, 2060fe6afe3SDeepa Dinamani const struct timespec64 *tp) 2075cee9645SThomas Gleixner { 2080fe6afe3SDeepa Dinamani return do_sys_settimeofday64(tp, NULL); 2095cee9645SThomas Gleixner } 2105cee9645SThomas Gleixner 2115cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock, 2125cee9645SThomas Gleixner struct timex *t) 2135cee9645SThomas Gleixner { 2145cee9645SThomas Gleixner return do_adjtimex(t); 2155cee9645SThomas Gleixner } 2165cee9645SThomas Gleixner 2175cee9645SThomas Gleixner /* 2185cee9645SThomas Gleixner * Get monotonic time for posix timers 2195cee9645SThomas Gleixner */ 2203c9c12f4SDeepa Dinamani static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) 2215cee9645SThomas Gleixner { 2223c9c12f4SDeepa Dinamani ktime_get_ts64(tp); 2235cee9645SThomas Gleixner return 0; 2245cee9645SThomas Gleixner } 2255cee9645SThomas Gleixner 2265cee9645SThomas Gleixner /* 2275cee9645SThomas Gleixner * Get monotonic-raw time for posix timers 2285cee9645SThomas Gleixner */ 2293c9c12f4SDeepa Dinamani static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) 2305cee9645SThomas Gleixner { 2313c9c12f4SDeepa Dinamani getrawmonotonic64(tp); 2325cee9645SThomas Gleixner return 0; 2335cee9645SThomas Gleixner } 2345cee9645SThomas Gleixner 2355cee9645SThomas Gleixner 2363c9c12f4SDeepa Dinamani static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp) 2375cee9645SThomas Gleixner { 2383c9c12f4SDeepa Dinamani *tp = current_kernel_time64(); 2395cee9645SThomas Gleixner return 0; 2405cee9645SThomas Gleixner } 2415cee9645SThomas Gleixner 2425cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock, 2433c9c12f4SDeepa Dinamani struct timespec64 *tp) 2445cee9645SThomas Gleixner { 2453c9c12f4SDeepa Dinamani *tp = get_monotonic_coarse64(); 2465cee9645SThomas Gleixner return 0; 2475cee9645SThomas Gleixner } 2485cee9645SThomas Gleixner 249d2e3e0caSDeepa Dinamani static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp) 2505cee9645SThomas Gleixner { 251d2e3e0caSDeepa Dinamani *tp = ktime_to_timespec64(KTIME_LOW_RES); 2525cee9645SThomas Gleixner return 0; 2535cee9645SThomas Gleixner } 2545cee9645SThomas Gleixner 2553c9c12f4SDeepa Dinamani static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp) 2565cee9645SThomas Gleixner { 2573c9c12f4SDeepa Dinamani timekeeping_clocktai64(tp); 2585cee9645SThomas Gleixner return 0; 2595cee9645SThomas Gleixner } 2605cee9645SThomas Gleixner 26172199320SThomas Gleixner static int posix_get_monotonic_active(clockid_t which_clock, 26272199320SThomas Gleixner struct timespec64 *tp) 26372199320SThomas Gleixner { 26472199320SThomas Gleixner ktime_get_active_ts64(tp); 26572199320SThomas Gleixner return 0; 26672199320SThomas Gleixner } 26772199320SThomas Gleixner 268d2e3e0caSDeepa Dinamani static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp) 269056a3cacSThomas Gleixner { 270056a3cacSThomas Gleixner tp->tv_sec = 0; 271056a3cacSThomas Gleixner tp->tv_nsec = hrtimer_resolution; 272056a3cacSThomas Gleixner return 0; 273056a3cacSThomas Gleixner } 274056a3cacSThomas Gleixner 275d3ba5a9aSChristoph Hellwig /* 276d3ba5a9aSChristoph Hellwig * Initialize everything, well, just everything in Posix clocks/timers ;) 277d3ba5a9aSChristoph Hellwig */ 278d3ba5a9aSChristoph Hellwig static __init int init_posix_timers(void) 279d3ba5a9aSChristoph Hellwig { 2805cee9645SThomas Gleixner posix_timers_cache = kmem_cache_create("posix_timers_cache", 2815cee9645SThomas Gleixner sizeof (struct k_itimer), 0, SLAB_PANIC, 2825cee9645SThomas Gleixner NULL); 2835cee9645SThomas Gleixner return 0; 2845cee9645SThomas Gleixner } 2855cee9645SThomas Gleixner __initcall(init_posix_timers); 2865cee9645SThomas Gleixner 287f37fb0aaSThomas Gleixner static void common_hrtimer_rearm(struct k_itimer *timr) 2885cee9645SThomas Gleixner { 2895cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 2905cee9645SThomas Gleixner 29180105cd0SThomas Gleixner if (!timr->it_interval) 2925cee9645SThomas Gleixner return; 2935cee9645SThomas Gleixner 2945cee9645SThomas Gleixner timr->it_overrun += (unsigned int) hrtimer_forward(timer, 2955cee9645SThomas Gleixner timer->base->get_time(), 29680105cd0SThomas Gleixner timr->it_interval); 2975cee9645SThomas Gleixner hrtimer_restart(timer); 2985cee9645SThomas Gleixner } 2995cee9645SThomas Gleixner 3005cee9645SThomas Gleixner /* 3015cee9645SThomas Gleixner * This function is exported for use by the signal deliver code. It is 3025cee9645SThomas Gleixner * called just prior to the info block being released and passes that 3035cee9645SThomas Gleixner * block to us. It's function is to update the overrun entry AND to 3045cee9645SThomas Gleixner * restart the timer. It should only be called if the timer is to be 3055cee9645SThomas Gleixner * restarted (i.e. we have flagged this in the sys_private entry of the 3065cee9645SThomas Gleixner * info block). 3075cee9645SThomas Gleixner * 3085cee9645SThomas Gleixner * To protect against the timer going away while the interrupt is queued, 3095cee9645SThomas Gleixner * we require that the it_requeue_pending flag be set. 3105cee9645SThomas Gleixner */ 31196fe3b07SThomas Gleixner void posixtimer_rearm(struct siginfo *info) 3125cee9645SThomas Gleixner { 3135cee9645SThomas Gleixner struct k_itimer *timr; 3145cee9645SThomas Gleixner unsigned long flags; 3155cee9645SThomas Gleixner 3165cee9645SThomas Gleixner timr = lock_timer(info->si_tid, &flags); 317af888d67SThomas Gleixner if (!timr) 318af888d67SThomas Gleixner return; 3195cee9645SThomas Gleixner 320af888d67SThomas Gleixner if (timr->it_requeue_pending == info->si_sys_private) { 321f37fb0aaSThomas Gleixner timr->kclock->timer_rearm(timr); 3225cee9645SThomas Gleixner 32321e55c1fSThomas Gleixner timr->it_active = 1; 324af888d67SThomas Gleixner timr->it_overrun_last = timr->it_overrun; 325af888d67SThomas Gleixner timr->it_overrun = -1; 326af888d67SThomas Gleixner ++timr->it_requeue_pending; 327af888d67SThomas Gleixner 3285cee9645SThomas Gleixner info->si_overrun += timr->it_overrun_last; 3295cee9645SThomas Gleixner } 3305cee9645SThomas Gleixner 3315cee9645SThomas Gleixner unlock_timer(timr, flags); 3325cee9645SThomas Gleixner } 3335cee9645SThomas Gleixner 3345cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private) 3355cee9645SThomas Gleixner { 3365cee9645SThomas Gleixner struct task_struct *task; 3375cee9645SThomas Gleixner int shared, ret = -1; 3385cee9645SThomas Gleixner /* 3395cee9645SThomas Gleixner * FIXME: if ->sigq is queued we can race with 34096fe3b07SThomas Gleixner * dequeue_signal()->posixtimer_rearm(). 3415cee9645SThomas Gleixner * 3425cee9645SThomas Gleixner * If dequeue_signal() sees the "right" value of 34396fe3b07SThomas Gleixner * si_sys_private it calls posixtimer_rearm(). 3445cee9645SThomas Gleixner * We re-queue ->sigq and drop ->it_lock(). 34596fe3b07SThomas Gleixner * posixtimer_rearm() locks the timer 3465cee9645SThomas Gleixner * and re-schedules it while ->sigq is pending. 3475cee9645SThomas Gleixner * Not really bad, but not that we want. 3485cee9645SThomas Gleixner */ 3495cee9645SThomas Gleixner timr->sigq->info.si_sys_private = si_private; 3505cee9645SThomas Gleixner 3515cee9645SThomas Gleixner rcu_read_lock(); 3525cee9645SThomas Gleixner task = pid_task(timr->it_pid, PIDTYPE_PID); 3535cee9645SThomas Gleixner if (task) { 3545cee9645SThomas Gleixner shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID); 3555cee9645SThomas Gleixner ret = send_sigqueue(timr->sigq, task, shared); 3565cee9645SThomas Gleixner } 3575cee9645SThomas Gleixner rcu_read_unlock(); 3585cee9645SThomas Gleixner /* If we failed to send the signal the timer stops. */ 3595cee9645SThomas Gleixner return ret > 0; 3605cee9645SThomas Gleixner } 3615cee9645SThomas Gleixner 3625cee9645SThomas Gleixner /* 3635cee9645SThomas Gleixner * This function gets called when a POSIX.1b interval timer expires. It 3645cee9645SThomas Gleixner * is used as a callback from the kernel internal timer. The 3655cee9645SThomas Gleixner * run_timer_list code ALWAYS calls with interrupts on. 3665cee9645SThomas Gleixner 3675cee9645SThomas Gleixner * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. 3685cee9645SThomas Gleixner */ 3695cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) 3705cee9645SThomas Gleixner { 3715cee9645SThomas Gleixner struct k_itimer *timr; 3725cee9645SThomas Gleixner unsigned long flags; 3735cee9645SThomas Gleixner int si_private = 0; 3745cee9645SThomas Gleixner enum hrtimer_restart ret = HRTIMER_NORESTART; 3755cee9645SThomas Gleixner 3765cee9645SThomas Gleixner timr = container_of(timer, struct k_itimer, it.real.timer); 3775cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, flags); 3785cee9645SThomas Gleixner 37921e55c1fSThomas Gleixner timr->it_active = 0; 38080105cd0SThomas Gleixner if (timr->it_interval != 0) 3815cee9645SThomas Gleixner si_private = ++timr->it_requeue_pending; 3825cee9645SThomas Gleixner 3835cee9645SThomas Gleixner if (posix_timer_event(timr, si_private)) { 3845cee9645SThomas Gleixner /* 3855cee9645SThomas Gleixner * signal was not sent because of sig_ignor 3865cee9645SThomas Gleixner * we will not get a call back to restart it AND 3875cee9645SThomas Gleixner * it should be restarted. 3885cee9645SThomas Gleixner */ 38980105cd0SThomas Gleixner if (timr->it_interval != 0) { 3905cee9645SThomas Gleixner ktime_t now = hrtimer_cb_get_time(timer); 3915cee9645SThomas Gleixner 3925cee9645SThomas Gleixner /* 3935cee9645SThomas Gleixner * FIXME: What we really want, is to stop this 3945cee9645SThomas Gleixner * timer completely and restart it in case the 3955cee9645SThomas Gleixner * SIG_IGN is removed. This is a non trivial 3965cee9645SThomas Gleixner * change which involves sighand locking 3975cee9645SThomas Gleixner * (sigh !), which we don't want to do late in 3985cee9645SThomas Gleixner * the release cycle. 3995cee9645SThomas Gleixner * 4005cee9645SThomas Gleixner * For now we just let timers with an interval 4015cee9645SThomas Gleixner * less than a jiffie expire every jiffie to 4025cee9645SThomas Gleixner * avoid softirq starvation in case of SIG_IGN 4035cee9645SThomas Gleixner * and a very small interval, which would put 4045cee9645SThomas Gleixner * the timer right back on the softirq pending 4055cee9645SThomas Gleixner * list. By moving now ahead of time we trick 4065cee9645SThomas Gleixner * hrtimer_forward() to expire the timer 4075cee9645SThomas Gleixner * later, while we still maintain the overrun 4085cee9645SThomas Gleixner * accuracy, but have some inconsistency in 4095cee9645SThomas Gleixner * the timer_gettime() case. This is at least 4105cee9645SThomas Gleixner * better than a starved softirq. A more 4115cee9645SThomas Gleixner * complex fix which solves also another related 4125cee9645SThomas Gleixner * inconsistency is already in the pipeline. 4135cee9645SThomas Gleixner */ 4145cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 4155cee9645SThomas Gleixner { 4168b0e1953SThomas Gleixner ktime_t kj = NSEC_PER_SEC / HZ; 4175cee9645SThomas Gleixner 41880105cd0SThomas Gleixner if (timr->it_interval < kj) 4195cee9645SThomas Gleixner now = ktime_add(now, kj); 4205cee9645SThomas Gleixner } 4215cee9645SThomas Gleixner #endif 4225cee9645SThomas Gleixner timr->it_overrun += (unsigned int) 4235cee9645SThomas Gleixner hrtimer_forward(timer, now, 42480105cd0SThomas Gleixner timr->it_interval); 4255cee9645SThomas Gleixner ret = HRTIMER_RESTART; 4265cee9645SThomas Gleixner ++timr->it_requeue_pending; 42721e55c1fSThomas Gleixner timr->it_active = 1; 4285cee9645SThomas Gleixner } 4295cee9645SThomas Gleixner } 4305cee9645SThomas Gleixner 4315cee9645SThomas Gleixner unlock_timer(timr, flags); 4325cee9645SThomas Gleixner return ret; 4335cee9645SThomas Gleixner } 4345cee9645SThomas Gleixner 4355cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event) 4365cee9645SThomas Gleixner { 4375cee9645SThomas Gleixner struct task_struct *rtn = current->group_leader; 4385cee9645SThomas Gleixner 439cef31d9aSThomas Gleixner switch (event->sigev_notify) { 440cef31d9aSThomas Gleixner case SIGEV_SIGNAL | SIGEV_THREAD_ID: 441cef31d9aSThomas Gleixner rtn = find_task_by_vpid(event->sigev_notify_thread_id); 442cef31d9aSThomas Gleixner if (!rtn || !same_thread_group(rtn, current)) 4435cee9645SThomas Gleixner return NULL; 444cef31d9aSThomas Gleixner /* FALLTHRU */ 445cef31d9aSThomas Gleixner case SIGEV_SIGNAL: 446cef31d9aSThomas Gleixner case SIGEV_THREAD: 447cef31d9aSThomas Gleixner if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX) 4485cee9645SThomas Gleixner return NULL; 449cef31d9aSThomas Gleixner /* FALLTHRU */ 450cef31d9aSThomas Gleixner case SIGEV_NONE: 4515cee9645SThomas Gleixner return task_pid(rtn); 452cef31d9aSThomas Gleixner default: 453cef31d9aSThomas Gleixner return NULL; 454cef31d9aSThomas Gleixner } 4555cee9645SThomas Gleixner } 4565cee9645SThomas Gleixner 4575cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void) 4585cee9645SThomas Gleixner { 4595cee9645SThomas Gleixner struct k_itimer *tmr; 4605cee9645SThomas Gleixner tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); 4615cee9645SThomas Gleixner if (!tmr) 4625cee9645SThomas Gleixner return tmr; 4635cee9645SThomas Gleixner if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { 4645cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4655cee9645SThomas Gleixner return NULL; 4665cee9645SThomas Gleixner } 4673b10db2bSEric W. Biederman clear_siginfo(&tmr->sigq->info); 4685cee9645SThomas Gleixner return tmr; 4695cee9645SThomas Gleixner } 4705cee9645SThomas Gleixner 4715cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head) 4725cee9645SThomas Gleixner { 4735cee9645SThomas Gleixner struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu); 4745cee9645SThomas Gleixner 4755cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4765cee9645SThomas Gleixner } 4775cee9645SThomas Gleixner 4785cee9645SThomas Gleixner #define IT_ID_SET 1 4795cee9645SThomas Gleixner #define IT_ID_NOT_SET 0 4805cee9645SThomas Gleixner static void release_posix_timer(struct k_itimer *tmr, int it_id_set) 4815cee9645SThomas Gleixner { 4825cee9645SThomas Gleixner if (it_id_set) { 4835cee9645SThomas Gleixner unsigned long flags; 4845cee9645SThomas Gleixner spin_lock_irqsave(&hash_lock, flags); 4855cee9645SThomas Gleixner hlist_del_rcu(&tmr->t_hash); 4865cee9645SThomas Gleixner spin_unlock_irqrestore(&hash_lock, flags); 4875cee9645SThomas Gleixner } 4885cee9645SThomas Gleixner put_pid(tmr->it_pid); 4895cee9645SThomas Gleixner sigqueue_free(tmr->sigq); 4905cee9645SThomas Gleixner call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 4915cee9645SThomas Gleixner } 4925cee9645SThomas Gleixner 4935cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer) 4945cee9645SThomas Gleixner { 4955cee9645SThomas Gleixner hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); 4965cee9645SThomas Gleixner return 0; 4975cee9645SThomas Gleixner } 4985cee9645SThomas Gleixner 4995cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */ 5002482097cSAl Viro static int do_timer_create(clockid_t which_clock, struct sigevent *event, 5012482097cSAl Viro timer_t __user *created_timer_id) 5025cee9645SThomas Gleixner { 503d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 5045cee9645SThomas Gleixner struct k_itimer *new_timer; 5055cee9645SThomas Gleixner int error, new_timer_id; 5065cee9645SThomas Gleixner int it_id_set = IT_ID_NOT_SET; 5075cee9645SThomas Gleixner 5085cee9645SThomas Gleixner if (!kc) 5095cee9645SThomas Gleixner return -EINVAL; 5105cee9645SThomas Gleixner if (!kc->timer_create) 5115cee9645SThomas Gleixner return -EOPNOTSUPP; 5125cee9645SThomas Gleixner 5135cee9645SThomas Gleixner new_timer = alloc_posix_timer(); 5145cee9645SThomas Gleixner if (unlikely(!new_timer)) 5155cee9645SThomas Gleixner return -EAGAIN; 5165cee9645SThomas Gleixner 5175cee9645SThomas Gleixner spin_lock_init(&new_timer->it_lock); 5185cee9645SThomas Gleixner new_timer_id = posix_timer_add(new_timer); 5195cee9645SThomas Gleixner if (new_timer_id < 0) { 5205cee9645SThomas Gleixner error = new_timer_id; 5215cee9645SThomas Gleixner goto out; 5225cee9645SThomas Gleixner } 5235cee9645SThomas Gleixner 5245cee9645SThomas Gleixner it_id_set = IT_ID_SET; 5255cee9645SThomas Gleixner new_timer->it_id = (timer_t) new_timer_id; 5265cee9645SThomas Gleixner new_timer->it_clock = which_clock; 527d97bb75dSThomas Gleixner new_timer->kclock = kc; 5285cee9645SThomas Gleixner new_timer->it_overrun = -1; 5295cee9645SThomas Gleixner 5302482097cSAl Viro if (event) { 5315cee9645SThomas Gleixner rcu_read_lock(); 5322482097cSAl Viro new_timer->it_pid = get_pid(good_sigevent(event)); 5335cee9645SThomas Gleixner rcu_read_unlock(); 5345cee9645SThomas Gleixner if (!new_timer->it_pid) { 5355cee9645SThomas Gleixner error = -EINVAL; 5365cee9645SThomas Gleixner goto out; 5375cee9645SThomas Gleixner } 5382482097cSAl Viro new_timer->it_sigev_notify = event->sigev_notify; 5392482097cSAl Viro new_timer->sigq->info.si_signo = event->sigev_signo; 5402482097cSAl Viro new_timer->sigq->info.si_value = event->sigev_value; 5415cee9645SThomas Gleixner } else { 5422482097cSAl Viro new_timer->it_sigev_notify = SIGEV_SIGNAL; 5432482097cSAl Viro new_timer->sigq->info.si_signo = SIGALRM; 5442482097cSAl Viro memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); 5452482097cSAl Viro new_timer->sigq->info.si_value.sival_int = new_timer->it_id; 5465cee9645SThomas Gleixner new_timer->it_pid = get_pid(task_tgid(current)); 5475cee9645SThomas Gleixner } 5485cee9645SThomas Gleixner 5495cee9645SThomas Gleixner new_timer->sigq->info.si_tid = new_timer->it_id; 5505cee9645SThomas Gleixner new_timer->sigq->info.si_code = SI_TIMER; 5515cee9645SThomas Gleixner 5525cee9645SThomas Gleixner if (copy_to_user(created_timer_id, 5535cee9645SThomas Gleixner &new_timer_id, sizeof (new_timer_id))) { 5545cee9645SThomas Gleixner error = -EFAULT; 5555cee9645SThomas Gleixner goto out; 5565cee9645SThomas Gleixner } 5575cee9645SThomas Gleixner 5585cee9645SThomas Gleixner error = kc->timer_create(new_timer); 5595cee9645SThomas Gleixner if (error) 5605cee9645SThomas Gleixner goto out; 5615cee9645SThomas Gleixner 5625cee9645SThomas Gleixner spin_lock_irq(¤t->sighand->siglock); 5635cee9645SThomas Gleixner new_timer->it_signal = current->signal; 5645cee9645SThomas Gleixner list_add(&new_timer->list, ¤t->signal->posix_timers); 5655cee9645SThomas Gleixner spin_unlock_irq(¤t->sighand->siglock); 5665cee9645SThomas Gleixner 5675cee9645SThomas Gleixner return 0; 5685cee9645SThomas Gleixner /* 5695cee9645SThomas Gleixner * In the case of the timer belonging to another task, after 5705cee9645SThomas Gleixner * the task is unlocked, the timer is owned by the other task 5715cee9645SThomas Gleixner * and may cease to exist at any time. Don't use or modify 5725cee9645SThomas Gleixner * new_timer after the unlock call. 5735cee9645SThomas Gleixner */ 5745cee9645SThomas Gleixner out: 5755cee9645SThomas Gleixner release_posix_timer(new_timer, it_id_set); 5765cee9645SThomas Gleixner return error; 5775cee9645SThomas Gleixner } 5785cee9645SThomas Gleixner 5792482097cSAl Viro SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, 5802482097cSAl Viro struct sigevent __user *, timer_event_spec, 5812482097cSAl Viro timer_t __user *, created_timer_id) 5822482097cSAl Viro { 5832482097cSAl Viro if (timer_event_spec) { 5842482097cSAl Viro sigevent_t event; 5852482097cSAl Viro 5862482097cSAl Viro if (copy_from_user(&event, timer_event_spec, sizeof (event))) 5872482097cSAl Viro return -EFAULT; 5882482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id); 5892482097cSAl Viro } 5902482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id); 5912482097cSAl Viro } 5922482097cSAl Viro 5932482097cSAl Viro #ifdef CONFIG_COMPAT 5942482097cSAl Viro COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, 5952482097cSAl Viro struct compat_sigevent __user *, timer_event_spec, 5962482097cSAl Viro timer_t __user *, created_timer_id) 5972482097cSAl Viro { 5982482097cSAl Viro if (timer_event_spec) { 5992482097cSAl Viro sigevent_t event; 6002482097cSAl Viro 6012482097cSAl Viro if (get_compat_sigevent(&event, timer_event_spec)) 6022482097cSAl Viro return -EFAULT; 6032482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id); 6042482097cSAl Viro } 6052482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id); 6062482097cSAl Viro } 6072482097cSAl Viro #endif 6082482097cSAl Viro 6095cee9645SThomas Gleixner /* 6105cee9645SThomas Gleixner * Locking issues: We need to protect the result of the id look up until 6115cee9645SThomas Gleixner * we get the timer locked down so it is not deleted under us. The 6125cee9645SThomas Gleixner * removal is done under the idr spinlock so we use that here to bridge 6135cee9645SThomas Gleixner * the find to the timer lock. To avoid a dead lock, the timer id MUST 6145cee9645SThomas Gleixner * be release with out holding the timer lock. 6155cee9645SThomas Gleixner */ 6165cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) 6175cee9645SThomas Gleixner { 6185cee9645SThomas Gleixner struct k_itimer *timr; 6195cee9645SThomas Gleixner 6205cee9645SThomas Gleixner /* 6215cee9645SThomas Gleixner * timer_t could be any type >= int and we want to make sure any 6225cee9645SThomas Gleixner * @timer_id outside positive int range fails lookup. 6235cee9645SThomas Gleixner */ 6245cee9645SThomas Gleixner if ((unsigned long long)timer_id > INT_MAX) 6255cee9645SThomas Gleixner return NULL; 6265cee9645SThomas Gleixner 6275cee9645SThomas Gleixner rcu_read_lock(); 6285cee9645SThomas Gleixner timr = posix_timer_by_id(timer_id); 6295cee9645SThomas Gleixner if (timr) { 6305cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, *flags); 6315cee9645SThomas Gleixner if (timr->it_signal == current->signal) { 6325cee9645SThomas Gleixner rcu_read_unlock(); 6335cee9645SThomas Gleixner return timr; 6345cee9645SThomas Gleixner } 6355cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, *flags); 6365cee9645SThomas Gleixner } 6375cee9645SThomas Gleixner rcu_read_unlock(); 6385cee9645SThomas Gleixner 6395cee9645SThomas Gleixner return NULL; 6405cee9645SThomas Gleixner } 6415cee9645SThomas Gleixner 64291d57baeSThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now) 64391d57baeSThomas Gleixner { 64491d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 64591d57baeSThomas Gleixner 64691d57baeSThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, now); 64791d57baeSThomas Gleixner } 64891d57baeSThomas Gleixner 64991d57baeSThomas Gleixner static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now) 65091d57baeSThomas Gleixner { 65191d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 65291d57baeSThomas Gleixner 65391d57baeSThomas Gleixner return (int)hrtimer_forward(timer, now, timr->it_interval); 65491d57baeSThomas Gleixner } 65591d57baeSThomas Gleixner 6565cee9645SThomas Gleixner /* 6575cee9645SThomas Gleixner * Get the time remaining on a POSIX.1b interval timer. This function 6585cee9645SThomas Gleixner * is ALWAYS called with spin_lock_irq on the timer, thus it must not 6595cee9645SThomas Gleixner * mess with irq. 6605cee9645SThomas Gleixner * 6615cee9645SThomas Gleixner * We have a couple of messes to clean up here. First there is the case 6625cee9645SThomas Gleixner * of a timer that has a requeue pending. These timers should appear to 6635cee9645SThomas Gleixner * be in the timer list with an expiry as if we were to requeue them 6645cee9645SThomas Gleixner * now. 6655cee9645SThomas Gleixner * 6665cee9645SThomas Gleixner * The second issue is the SIGEV_NONE timer which may be active but is 6675cee9645SThomas Gleixner * not really ever put in the timer list (to save system resources). 6685cee9645SThomas Gleixner * This timer may be expired, and if so, we will do it here. Otherwise 6695cee9645SThomas Gleixner * it is the same as a requeue pending timer WRT to what we should 6705cee9645SThomas Gleixner * report. 6715cee9645SThomas Gleixner */ 672f2c45807SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) 6735cee9645SThomas Gleixner { 67491d57baeSThomas Gleixner const struct k_clock *kc = timr->kclock; 6755cee9645SThomas Gleixner ktime_t now, remaining, iv; 67691d57baeSThomas Gleixner struct timespec64 ts64; 67791d57baeSThomas Gleixner bool sig_none; 6785cee9645SThomas Gleixner 679cef31d9aSThomas Gleixner sig_none = timr->it_sigev_notify == SIGEV_NONE; 68080105cd0SThomas Gleixner iv = timr->it_interval; 6815cee9645SThomas Gleixner 6825cee9645SThomas Gleixner /* interval timer ? */ 68391d57baeSThomas Gleixner if (iv) { 6845f252b32SDeepa Dinamani cur_setting->it_interval = ktime_to_timespec64(iv); 68591d57baeSThomas Gleixner } else if (!timr->it_active) { 68691d57baeSThomas Gleixner /* 68791d57baeSThomas Gleixner * SIGEV_NONE oneshot timers are never queued. Check them 68891d57baeSThomas Gleixner * below. 68991d57baeSThomas Gleixner */ 69091d57baeSThomas Gleixner if (!sig_none) 6915cee9645SThomas Gleixner return; 69291d57baeSThomas Gleixner } 6935cee9645SThomas Gleixner 6945cee9645SThomas Gleixner /* 69591d57baeSThomas Gleixner * The timespec64 based conversion is suboptimal, but it's not 69691d57baeSThomas Gleixner * worth to implement yet another callback. 6975cee9645SThomas Gleixner */ 69891d57baeSThomas Gleixner kc->clock_get(timr->it_clock, &ts64); 69991d57baeSThomas Gleixner now = timespec64_to_ktime(ts64); 7005cee9645SThomas Gleixner 70191d57baeSThomas Gleixner /* 70291d57baeSThomas Gleixner * When a requeue is pending or this is a SIGEV_NONE timer move the 70391d57baeSThomas Gleixner * expiry time forward by intervals, so expiry is > now. 70491d57baeSThomas Gleixner */ 70591d57baeSThomas Gleixner if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) 70691d57baeSThomas Gleixner timr->it_overrun += kc->timer_forward(timr, now); 70791d57baeSThomas Gleixner 70891d57baeSThomas Gleixner remaining = kc->timer_remaining(timr, now); 7095cee9645SThomas Gleixner /* Return 0 only, when the timer is expired and not pending */ 7102456e855SThomas Gleixner if (remaining <= 0) { 7115cee9645SThomas Gleixner /* 7125cee9645SThomas Gleixner * A single shot SIGEV_NONE timer must return 0, when 7135cee9645SThomas Gleixner * it is expired ! 7145cee9645SThomas Gleixner */ 71591d57baeSThomas Gleixner if (!sig_none) 7165cee9645SThomas Gleixner cur_setting->it_value.tv_nsec = 1; 71791d57baeSThomas Gleixner } else { 7185f252b32SDeepa Dinamani cur_setting->it_value = ktime_to_timespec64(remaining); 7195cee9645SThomas Gleixner } 72091d57baeSThomas Gleixner } 7215cee9645SThomas Gleixner 7225cee9645SThomas Gleixner /* Get the time remaining on a POSIX.1b interval timer. */ 723b0dc1242SAl Viro static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting) 7245cee9645SThomas Gleixner { 7255cee9645SThomas Gleixner struct k_itimer *timr; 726d3ba5a9aSChristoph Hellwig const struct k_clock *kc; 7275cee9645SThomas Gleixner unsigned long flags; 7285cee9645SThomas Gleixner int ret = 0; 7295cee9645SThomas Gleixner 7305cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7315cee9645SThomas Gleixner if (!timr) 7325cee9645SThomas Gleixner return -EINVAL; 7335cee9645SThomas Gleixner 734b0dc1242SAl Viro memset(setting, 0, sizeof(*setting)); 735d97bb75dSThomas Gleixner kc = timr->kclock; 7365cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_get)) 7375cee9645SThomas Gleixner ret = -EINVAL; 7385cee9645SThomas Gleixner else 739b0dc1242SAl Viro kc->timer_get(timr, setting); 7405cee9645SThomas Gleixner 7415cee9645SThomas Gleixner unlock_timer(timr, flags); 7425cee9645SThomas Gleixner return ret; 7435cee9645SThomas Gleixner } 7445cee9645SThomas Gleixner 745b0dc1242SAl Viro /* Get the time remaining on a POSIX.1b interval timer. */ 746b0dc1242SAl Viro SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 747b0dc1242SAl Viro struct itimerspec __user *, setting) 748b0dc1242SAl Viro { 749725816e8SDeepa Dinamani struct itimerspec64 cur_setting; 750b0dc1242SAl Viro 751725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting); 752b0dc1242SAl Viro if (!ret) { 753725816e8SDeepa Dinamani if (put_itimerspec64(&cur_setting, setting)) 754b0dc1242SAl Viro ret = -EFAULT; 755b0dc1242SAl Viro } 756b0dc1242SAl Viro return ret; 757b0dc1242SAl Viro } 758b0dc1242SAl Viro 759b0dc1242SAl Viro #ifdef CONFIG_COMPAT 760b0dc1242SAl Viro COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 761b0dc1242SAl Viro struct compat_itimerspec __user *, setting) 762b0dc1242SAl Viro { 763725816e8SDeepa Dinamani struct itimerspec64 cur_setting; 764b0dc1242SAl Viro 765725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting); 766b0dc1242SAl Viro if (!ret) { 767725816e8SDeepa Dinamani if (put_compat_itimerspec64(&cur_setting, setting)) 768b0dc1242SAl Viro ret = -EFAULT; 769b0dc1242SAl Viro } 770b0dc1242SAl Viro return ret; 771b0dc1242SAl Viro } 772b0dc1242SAl Viro #endif 773b0dc1242SAl Viro 7745cee9645SThomas Gleixner /* 7755cee9645SThomas Gleixner * Get the number of overruns of a POSIX.1b interval timer. This is to 7765cee9645SThomas Gleixner * be the overrun of the timer last delivered. At the same time we are 7775cee9645SThomas Gleixner * accumulating overruns on the next timer. The overrun is frozen when 7785cee9645SThomas Gleixner * the signal is delivered, either at the notify time (if the info block 7795cee9645SThomas Gleixner * is not queued) or at the actual delivery time (as we are informed by 78096fe3b07SThomas Gleixner * the call back to posixtimer_rearm(). So all we need to do is 7815cee9645SThomas Gleixner * to pick up the frozen overrun. 7825cee9645SThomas Gleixner */ 7835cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) 7845cee9645SThomas Gleixner { 7855cee9645SThomas Gleixner struct k_itimer *timr; 7865cee9645SThomas Gleixner int overrun; 7875cee9645SThomas Gleixner unsigned long flags; 7885cee9645SThomas Gleixner 7895cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7905cee9645SThomas Gleixner if (!timr) 7915cee9645SThomas Gleixner return -EINVAL; 7925cee9645SThomas Gleixner 7935cee9645SThomas Gleixner overrun = timr->it_overrun_last; 7945cee9645SThomas Gleixner unlock_timer(timr, flags); 7955cee9645SThomas Gleixner 7965cee9645SThomas Gleixner return overrun; 7975cee9645SThomas Gleixner } 7985cee9645SThomas Gleixner 799eae1c4aeSThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires, 800eae1c4aeSThomas Gleixner bool absolute, bool sigev_none) 8015cee9645SThomas Gleixner { 8025cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 8035cee9645SThomas Gleixner enum hrtimer_mode mode; 8045cee9645SThomas Gleixner 805eae1c4aeSThomas Gleixner mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; 80667edab48SThomas Gleixner /* 80767edab48SThomas Gleixner * Posix magic: Relative CLOCK_REALTIME timers are not affected by 80867edab48SThomas Gleixner * clock modifications, so they become CLOCK_MONOTONIC based under the 80967edab48SThomas Gleixner * hood. See hrtimer_init(). Update timr->kclock, so the generic 81067edab48SThomas Gleixner * functions which use timr->kclock->clock_get() work. 81167edab48SThomas Gleixner * 81267edab48SThomas Gleixner * Note: it_clock stays unmodified, because the next timer_set() might 81367edab48SThomas Gleixner * use ABSTIME, so it needs to switch back. 81467edab48SThomas Gleixner */ 81567edab48SThomas Gleixner if (timr->it_clock == CLOCK_REALTIME) 81667edab48SThomas Gleixner timr->kclock = absolute ? &clock_realtime : &clock_monotonic; 81767edab48SThomas Gleixner 818eae1c4aeSThomas Gleixner hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); 819eae1c4aeSThomas Gleixner timr->it.real.timer.function = posix_timer_fn; 820eae1c4aeSThomas Gleixner 821eae1c4aeSThomas Gleixner if (!absolute) 822eae1c4aeSThomas Gleixner expires = ktime_add_safe(expires, timer->base->get_time()); 823eae1c4aeSThomas Gleixner hrtimer_set_expires(timer, expires); 824eae1c4aeSThomas Gleixner 825eae1c4aeSThomas Gleixner if (!sigev_none) 826eae1c4aeSThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 827eae1c4aeSThomas Gleixner } 828eae1c4aeSThomas Gleixner 829eae1c4aeSThomas Gleixner static int common_hrtimer_try_to_cancel(struct k_itimer *timr) 830eae1c4aeSThomas Gleixner { 831eae1c4aeSThomas Gleixner return hrtimer_try_to_cancel(&timr->it.real.timer); 832eae1c4aeSThomas Gleixner } 833eae1c4aeSThomas Gleixner 834eae1c4aeSThomas Gleixner /* Set a POSIX.1b interval timer. */ 835f2c45807SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags, 836eae1c4aeSThomas Gleixner struct itimerspec64 *new_setting, 837eae1c4aeSThomas Gleixner struct itimerspec64 *old_setting) 838eae1c4aeSThomas Gleixner { 839eae1c4aeSThomas Gleixner const struct k_clock *kc = timr->kclock; 840eae1c4aeSThomas Gleixner bool sigev_none; 841eae1c4aeSThomas Gleixner ktime_t expires; 842eae1c4aeSThomas Gleixner 8435cee9645SThomas Gleixner if (old_setting) 8445cee9645SThomas Gleixner common_timer_get(timr, old_setting); 8455cee9645SThomas Gleixner 846eae1c4aeSThomas Gleixner /* Prevent rearming by clearing the interval */ 84780105cd0SThomas Gleixner timr->it_interval = 0; 8485cee9645SThomas Gleixner /* 849eae1c4aeSThomas Gleixner * Careful here. On SMP systems the timer expiry function could be 850eae1c4aeSThomas Gleixner * active and spinning on timr->it_lock. 8515cee9645SThomas Gleixner */ 852eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timr) < 0) 8535cee9645SThomas Gleixner return TIMER_RETRY; 8545cee9645SThomas Gleixner 85521e55c1fSThomas Gleixner timr->it_active = 0; 8565cee9645SThomas Gleixner timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 8575cee9645SThomas Gleixner ~REQUEUE_PENDING; 8585cee9645SThomas Gleixner timr->it_overrun_last = 0; 8595cee9645SThomas Gleixner 860eae1c4aeSThomas Gleixner /* Switch off the timer when it_value is zero */ 8615cee9645SThomas Gleixner if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 8625cee9645SThomas Gleixner return 0; 8635cee9645SThomas Gleixner 86480105cd0SThomas Gleixner timr->it_interval = timespec64_to_ktime(new_setting->it_interval); 865eae1c4aeSThomas Gleixner expires = timespec64_to_ktime(new_setting->it_value); 866cef31d9aSThomas Gleixner sigev_none = timr->it_sigev_notify == SIGEV_NONE; 8675cee9645SThomas Gleixner 868eae1c4aeSThomas Gleixner kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); 869eae1c4aeSThomas Gleixner timr->it_active = !sigev_none; 8705cee9645SThomas Gleixner return 0; 8715cee9645SThomas Gleixner } 8725cee9645SThomas Gleixner 8731acbe770SAl Viro static int do_timer_settime(timer_t timer_id, int flags, 8741acbe770SAl Viro struct itimerspec64 *new_spec64, 8751acbe770SAl Viro struct itimerspec64 *old_spec64) 8765cee9645SThomas Gleixner { 8771acbe770SAl Viro const struct k_clock *kc; 8785f252b32SDeepa Dinamani struct k_itimer *timr; 8795cee9645SThomas Gleixner unsigned long flag; 8805f252b32SDeepa Dinamani int error = 0; 8815cee9645SThomas Gleixner 8821acbe770SAl Viro if (!timespec64_valid(&new_spec64->it_interval) || 8831acbe770SAl Viro !timespec64_valid(&new_spec64->it_value)) 8845cee9645SThomas Gleixner return -EINVAL; 8855cee9645SThomas Gleixner 8861acbe770SAl Viro if (old_spec64) 8871acbe770SAl Viro memset(old_spec64, 0, sizeof(*old_spec64)); 8885cee9645SThomas Gleixner retry: 8895cee9645SThomas Gleixner timr = lock_timer(timer_id, &flag); 8905cee9645SThomas Gleixner if (!timr) 8915cee9645SThomas Gleixner return -EINVAL; 8925cee9645SThomas Gleixner 893d97bb75dSThomas Gleixner kc = timr->kclock; 8945cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_set)) 8955cee9645SThomas Gleixner error = -EINVAL; 8965cee9645SThomas Gleixner else 8971acbe770SAl Viro error = kc->timer_set(timr, flags, new_spec64, old_spec64); 8985cee9645SThomas Gleixner 8995cee9645SThomas Gleixner unlock_timer(timr, flag); 9005cee9645SThomas Gleixner if (error == TIMER_RETRY) { 9011acbe770SAl Viro old_spec64 = NULL; // We already got the old time... 9025cee9645SThomas Gleixner goto retry; 9035cee9645SThomas Gleixner } 9045cee9645SThomas Gleixner 9055cee9645SThomas Gleixner return error; 9065cee9645SThomas Gleixner } 9075cee9645SThomas Gleixner 9081acbe770SAl Viro /* Set a POSIX.1b interval timer */ 9091acbe770SAl Viro SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 9101acbe770SAl Viro const struct itimerspec __user *, new_setting, 9111acbe770SAl Viro struct itimerspec __user *, old_setting) 9121acbe770SAl Viro { 913725816e8SDeepa Dinamani struct itimerspec64 new_spec, old_spec; 914725816e8SDeepa Dinamani struct itimerspec64 *rtn = old_setting ? &old_spec : NULL; 9151acbe770SAl Viro int error = 0; 9161acbe770SAl Viro 9171acbe770SAl Viro if (!new_setting) 9181acbe770SAl Viro return -EINVAL; 9191acbe770SAl Viro 920725816e8SDeepa Dinamani if (get_itimerspec64(&new_spec, new_setting)) 9211acbe770SAl Viro return -EFAULT; 9221acbe770SAl Viro 923725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn); 9241acbe770SAl Viro if (!error && old_setting) { 925725816e8SDeepa Dinamani if (put_itimerspec64(&old_spec, old_setting)) 9261acbe770SAl Viro error = -EFAULT; 9271acbe770SAl Viro } 9281acbe770SAl Viro return error; 9291acbe770SAl Viro } 9301acbe770SAl Viro 9311acbe770SAl Viro #ifdef CONFIG_COMPAT 9321acbe770SAl Viro COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 9331acbe770SAl Viro struct compat_itimerspec __user *, new, 9341acbe770SAl Viro struct compat_itimerspec __user *, old) 9351acbe770SAl Viro { 936725816e8SDeepa Dinamani struct itimerspec64 new_spec, old_spec; 937725816e8SDeepa Dinamani struct itimerspec64 *rtn = old ? &old_spec : NULL; 9381acbe770SAl Viro int error = 0; 9391acbe770SAl Viro 9401acbe770SAl Viro if (!new) 9411acbe770SAl Viro return -EINVAL; 942725816e8SDeepa Dinamani if (get_compat_itimerspec64(&new_spec, new)) 9431acbe770SAl Viro return -EFAULT; 9441acbe770SAl Viro 945725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn); 9461acbe770SAl Viro if (!error && old) { 947725816e8SDeepa Dinamani if (put_compat_itimerspec64(&old_spec, old)) 9481acbe770SAl Viro error = -EFAULT; 9491acbe770SAl Viro } 9501acbe770SAl Viro return error; 9511acbe770SAl Viro } 9521acbe770SAl Viro #endif 9531acbe770SAl Viro 954f2c45807SThomas Gleixner int common_timer_del(struct k_itimer *timer) 9555cee9645SThomas Gleixner { 956eae1c4aeSThomas Gleixner const struct k_clock *kc = timer->kclock; 9575cee9645SThomas Gleixner 958eae1c4aeSThomas Gleixner timer->it_interval = 0; 959eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timer) < 0) 9605cee9645SThomas Gleixner return TIMER_RETRY; 96121e55c1fSThomas Gleixner timer->it_active = 0; 9625cee9645SThomas Gleixner return 0; 9635cee9645SThomas Gleixner } 9645cee9645SThomas Gleixner 9655cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer) 9665cee9645SThomas Gleixner { 967d97bb75dSThomas Gleixner const struct k_clock *kc = timer->kclock; 9685cee9645SThomas Gleixner 9695cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_del)) 9705cee9645SThomas Gleixner return -EINVAL; 9715cee9645SThomas Gleixner return kc->timer_del(timer); 9725cee9645SThomas Gleixner } 9735cee9645SThomas Gleixner 9745cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */ 9755cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) 9765cee9645SThomas Gleixner { 9775cee9645SThomas Gleixner struct k_itimer *timer; 9785cee9645SThomas Gleixner unsigned long flags; 9795cee9645SThomas Gleixner 9805cee9645SThomas Gleixner retry_delete: 9815cee9645SThomas Gleixner timer = lock_timer(timer_id, &flags); 9825cee9645SThomas Gleixner if (!timer) 9835cee9645SThomas Gleixner return -EINVAL; 9845cee9645SThomas Gleixner 9855cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) { 9865cee9645SThomas Gleixner unlock_timer(timer, flags); 9875cee9645SThomas Gleixner goto retry_delete; 9885cee9645SThomas Gleixner } 9895cee9645SThomas Gleixner 9905cee9645SThomas Gleixner spin_lock(¤t->sighand->siglock); 9915cee9645SThomas Gleixner list_del(&timer->list); 9925cee9645SThomas Gleixner spin_unlock(¤t->sighand->siglock); 9935cee9645SThomas Gleixner /* 9945cee9645SThomas Gleixner * This keeps any tasks waiting on the spin lock from thinking 9955cee9645SThomas Gleixner * they got something (see the lock code above). 9965cee9645SThomas Gleixner */ 9975cee9645SThomas Gleixner timer->it_signal = NULL; 9985cee9645SThomas Gleixner 9995cee9645SThomas Gleixner unlock_timer(timer, flags); 10005cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 10015cee9645SThomas Gleixner return 0; 10025cee9645SThomas Gleixner } 10035cee9645SThomas Gleixner 10045cee9645SThomas Gleixner /* 10055cee9645SThomas Gleixner * return timer owned by the process, used by exit_itimers 10065cee9645SThomas Gleixner */ 10075cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer) 10085cee9645SThomas Gleixner { 10095cee9645SThomas Gleixner unsigned long flags; 10105cee9645SThomas Gleixner 10115cee9645SThomas Gleixner retry_delete: 10125cee9645SThomas Gleixner spin_lock_irqsave(&timer->it_lock, flags); 10135cee9645SThomas Gleixner 10145cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) { 10155cee9645SThomas Gleixner unlock_timer(timer, flags); 10165cee9645SThomas Gleixner goto retry_delete; 10175cee9645SThomas Gleixner } 10185cee9645SThomas Gleixner list_del(&timer->list); 10195cee9645SThomas Gleixner /* 10205cee9645SThomas Gleixner * This keeps any tasks waiting on the spin lock from thinking 10215cee9645SThomas Gleixner * they got something (see the lock code above). 10225cee9645SThomas Gleixner */ 10235cee9645SThomas Gleixner timer->it_signal = NULL; 10245cee9645SThomas Gleixner 10255cee9645SThomas Gleixner unlock_timer(timer, flags); 10265cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 10275cee9645SThomas Gleixner } 10285cee9645SThomas Gleixner 10295cee9645SThomas Gleixner /* 10305cee9645SThomas Gleixner * This is called by do_exit or de_thread, only when there are no more 10315cee9645SThomas Gleixner * references to the shared signal_struct. 10325cee9645SThomas Gleixner */ 10335cee9645SThomas Gleixner void exit_itimers(struct signal_struct *sig) 10345cee9645SThomas Gleixner { 10355cee9645SThomas Gleixner struct k_itimer *tmr; 10365cee9645SThomas Gleixner 10375cee9645SThomas Gleixner while (!list_empty(&sig->posix_timers)) { 10385cee9645SThomas Gleixner tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); 10395cee9645SThomas Gleixner itimer_delete(tmr); 10405cee9645SThomas Gleixner } 10415cee9645SThomas Gleixner } 10425cee9645SThomas Gleixner 10435cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, 10446d5b8413SDeepa Dinamani const struct __kernel_timespec __user *, tp) 10455cee9645SThomas Gleixner { 1046d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10475c499410SDeepa Dinamani struct timespec64 new_tp; 10485cee9645SThomas Gleixner 10495cee9645SThomas Gleixner if (!kc || !kc->clock_set) 10505cee9645SThomas Gleixner return -EINVAL; 10515cee9645SThomas Gleixner 10525c499410SDeepa Dinamani if (get_timespec64(&new_tp, tp)) 10535cee9645SThomas Gleixner return -EFAULT; 10545cee9645SThomas Gleixner 10555c499410SDeepa Dinamani return kc->clock_set(which_clock, &new_tp); 10565cee9645SThomas Gleixner } 10575cee9645SThomas Gleixner 10585cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, 10596d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp) 10605cee9645SThomas Gleixner { 1061d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10625c499410SDeepa Dinamani struct timespec64 kernel_tp; 10635cee9645SThomas Gleixner int error; 10645cee9645SThomas Gleixner 10655cee9645SThomas Gleixner if (!kc) 10665cee9645SThomas Gleixner return -EINVAL; 10675cee9645SThomas Gleixner 10685c499410SDeepa Dinamani error = kc->clock_get(which_clock, &kernel_tp); 10695cee9645SThomas Gleixner 10705c499410SDeepa Dinamani if (!error && put_timespec64(&kernel_tp, tp)) 10715cee9645SThomas Gleixner error = -EFAULT; 10725cee9645SThomas Gleixner 10735cee9645SThomas Gleixner return error; 10745cee9645SThomas Gleixner } 10755cee9645SThomas Gleixner 10765cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, 10775cee9645SThomas Gleixner struct timex __user *, utx) 10785cee9645SThomas Gleixner { 1079d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10805cee9645SThomas Gleixner struct timex ktx; 10815cee9645SThomas Gleixner int err; 10825cee9645SThomas Gleixner 10835cee9645SThomas Gleixner if (!kc) 10845cee9645SThomas Gleixner return -EINVAL; 10855cee9645SThomas Gleixner if (!kc->clock_adj) 10865cee9645SThomas Gleixner return -EOPNOTSUPP; 10875cee9645SThomas Gleixner 10885cee9645SThomas Gleixner if (copy_from_user(&ktx, utx, sizeof(ktx))) 10895cee9645SThomas Gleixner return -EFAULT; 10905cee9645SThomas Gleixner 10915cee9645SThomas Gleixner err = kc->clock_adj(which_clock, &ktx); 10925cee9645SThomas Gleixner 10935cee9645SThomas Gleixner if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) 10945cee9645SThomas Gleixner return -EFAULT; 10955cee9645SThomas Gleixner 10965cee9645SThomas Gleixner return err; 10975cee9645SThomas Gleixner } 10985cee9645SThomas Gleixner 1099d822cdccSAl Viro SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, 11006d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp) 1101d822cdccSAl Viro { 1102d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11035c499410SDeepa Dinamani struct timespec64 rtn_tp; 1104d822cdccSAl Viro int error; 1105d822cdccSAl Viro 1106d822cdccSAl Viro if (!kc) 1107d822cdccSAl Viro return -EINVAL; 1108d822cdccSAl Viro 11095c499410SDeepa Dinamani error = kc->clock_getres(which_clock, &rtn_tp); 1110d822cdccSAl Viro 11115c499410SDeepa Dinamani if (!error && tp && put_timespec64(&rtn_tp, tp)) 1112d822cdccSAl Viro error = -EFAULT; 1113d822cdccSAl Viro 1114d822cdccSAl Viro return error; 1115d822cdccSAl Viro } 1116d822cdccSAl Viro 1117b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 11183a4d44b6SAl Viro 1119d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock, 1120d822cdccSAl Viro struct compat_timespec __user *, tp) 1121d822cdccSAl Viro { 1122d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11235c499410SDeepa Dinamani struct timespec64 ts; 1124d822cdccSAl Viro 1125d822cdccSAl Viro if (!kc || !kc->clock_set) 1126d822cdccSAl Viro return -EINVAL; 1127d822cdccSAl Viro 11285c499410SDeepa Dinamani if (compat_get_timespec64(&ts, tp)) 1129d822cdccSAl Viro return -EFAULT; 1130d822cdccSAl Viro 11315c499410SDeepa Dinamani return kc->clock_set(which_clock, &ts); 1132d822cdccSAl Viro } 1133d822cdccSAl Viro 1134d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, 1135d822cdccSAl Viro struct compat_timespec __user *, tp) 1136d822cdccSAl Viro { 1137d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11385c499410SDeepa Dinamani struct timespec64 ts; 11395c499410SDeepa Dinamani int err; 1140d822cdccSAl Viro 1141d822cdccSAl Viro if (!kc) 1142d822cdccSAl Viro return -EINVAL; 1143d822cdccSAl Viro 11445c499410SDeepa Dinamani err = kc->clock_get(which_clock, &ts); 1145d822cdccSAl Viro 11465c499410SDeepa Dinamani if (!err && compat_put_timespec64(&ts, tp)) 11475c499410SDeepa Dinamani err = -EFAULT; 1148d822cdccSAl Viro 11495c499410SDeepa Dinamani return err; 1150d822cdccSAl Viro } 1151d822cdccSAl Viro 1152b5793b0dSDeepa Dinamani #endif 1153b5793b0dSDeepa Dinamani 1154b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT 1155b5793b0dSDeepa Dinamani 11563a4d44b6SAl Viro COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, 11573a4d44b6SAl Viro struct compat_timex __user *, utp) 11583a4d44b6SAl Viro { 11593a4d44b6SAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11603a4d44b6SAl Viro struct timex ktx; 11613a4d44b6SAl Viro int err; 11623a4d44b6SAl Viro 11633a4d44b6SAl Viro if (!kc) 11643a4d44b6SAl Viro return -EINVAL; 11653a4d44b6SAl Viro if (!kc->clock_adj) 11663a4d44b6SAl Viro return -EOPNOTSUPP; 11673a4d44b6SAl Viro 11683a4d44b6SAl Viro err = compat_get_timex(&ktx, utp); 11693a4d44b6SAl Viro if (err) 11703a4d44b6SAl Viro return err; 11713a4d44b6SAl Viro 11723a4d44b6SAl Viro err = kc->clock_adj(which_clock, &ktx); 11733a4d44b6SAl Viro 11743a4d44b6SAl Viro if (err >= 0) 11753a4d44b6SAl Viro err = compat_put_timex(utp, &ktx); 11763a4d44b6SAl Viro 11773a4d44b6SAl Viro return err; 11783a4d44b6SAl Viro } 11793a4d44b6SAl Viro 1180b5793b0dSDeepa Dinamani #endif 1181b5793b0dSDeepa Dinamani 1182b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 1183b5793b0dSDeepa Dinamani 1184d822cdccSAl Viro COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock, 1185d822cdccSAl Viro struct compat_timespec __user *, tp) 11865cee9645SThomas Gleixner { 1187d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 11885c499410SDeepa Dinamani struct timespec64 ts; 11895c499410SDeepa Dinamani int err; 11905cee9645SThomas Gleixner 11915cee9645SThomas Gleixner if (!kc) 11925cee9645SThomas Gleixner return -EINVAL; 11935cee9645SThomas Gleixner 11945c499410SDeepa Dinamani err = kc->clock_getres(which_clock, &ts); 11955c499410SDeepa Dinamani if (!err && tp && compat_put_timespec64(&ts, tp)) 11965c499410SDeepa Dinamani return -EFAULT; 11975cee9645SThomas Gleixner 11985c499410SDeepa Dinamani return err; 11995cee9645SThomas Gleixner } 12005c499410SDeepa Dinamani 1201d822cdccSAl Viro #endif 12025cee9645SThomas Gleixner 12035cee9645SThomas Gleixner /* 12045cee9645SThomas Gleixner * nanosleep for monotonic and realtime clocks 12055cee9645SThomas Gleixner */ 12065cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags, 1207938e7cf2SThomas Gleixner const struct timespec64 *rqtp) 12085cee9645SThomas Gleixner { 1209938e7cf2SThomas Gleixner return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ? 12105cee9645SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL, 12115cee9645SThomas Gleixner which_clock); 12125cee9645SThomas Gleixner } 12135cee9645SThomas Gleixner 12145cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, 121501909974SDeepa Dinamani const struct __kernel_timespec __user *, rqtp, 121601909974SDeepa Dinamani struct __kernel_timespec __user *, rmtp) 12175cee9645SThomas Gleixner { 1218d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1219c0edd7c9SDeepa Dinamani struct timespec64 t; 12205cee9645SThomas Gleixner 12215cee9645SThomas Gleixner if (!kc) 12225cee9645SThomas Gleixner return -EINVAL; 12235cee9645SThomas Gleixner if (!kc->nsleep) 12245cee9645SThomas Gleixner return -ENANOSLEEP_NOTSUP; 12255cee9645SThomas Gleixner 1226c0edd7c9SDeepa Dinamani if (get_timespec64(&t, rqtp)) 12275cee9645SThomas Gleixner return -EFAULT; 12285cee9645SThomas Gleixner 1229c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t)) 12305cee9645SThomas Gleixner return -EINVAL; 123199e6c0e6SAl Viro if (flags & TIMER_ABSTIME) 123299e6c0e6SAl Viro rmtp = NULL; 1233edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; 123499e6c0e6SAl Viro current->restart_block.nanosleep.rmtp = rmtp; 12355cee9645SThomas Gleixner 1236c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t); 12375cee9645SThomas Gleixner } 12385cee9645SThomas Gleixner 1239b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 1240b5793b0dSDeepa Dinamani 1241edbeda46SAl Viro COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags, 1242edbeda46SAl Viro struct compat_timespec __user *, rqtp, 1243edbeda46SAl Viro struct compat_timespec __user *, rmtp) 12445cee9645SThomas Gleixner { 1245d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1246c0edd7c9SDeepa Dinamani struct timespec64 t; 12475cee9645SThomas Gleixner 1248edbeda46SAl Viro if (!kc) 12495cee9645SThomas Gleixner return -EINVAL; 1250edbeda46SAl Viro if (!kc->nsleep) 1251edbeda46SAl Viro return -ENANOSLEEP_NOTSUP; 12525cee9645SThomas Gleixner 1253c0edd7c9SDeepa Dinamani if (compat_get_timespec64(&t, rqtp)) 1254edbeda46SAl Viro return -EFAULT; 1255edbeda46SAl Viro 1256c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t)) 1257edbeda46SAl Viro return -EINVAL; 1258edbeda46SAl Viro if (flags & TIMER_ABSTIME) 1259edbeda46SAl Viro rmtp = NULL; 1260edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; 1261edbeda46SAl Viro current->restart_block.nanosleep.compat_rmtp = rmtp; 1262edbeda46SAl Viro 1263c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t); 12645cee9645SThomas Gleixner } 1265b5793b0dSDeepa Dinamani 1266edbeda46SAl Viro #endif 12676631fa12SThomas Gleixner 12686631fa12SThomas Gleixner static const struct k_clock clock_realtime = { 12696631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12706631fa12SThomas Gleixner .clock_get = posix_clock_realtime_get, 12716631fa12SThomas Gleixner .clock_set = posix_clock_realtime_set, 12726631fa12SThomas Gleixner .clock_adj = posix_clock_realtime_adj, 12736631fa12SThomas Gleixner .nsleep = common_nsleep, 12746631fa12SThomas Gleixner .timer_create = common_timer_create, 12756631fa12SThomas Gleixner .timer_set = common_timer_set, 12766631fa12SThomas Gleixner .timer_get = common_timer_get, 12776631fa12SThomas Gleixner .timer_del = common_timer_del, 1278f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 127991d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 128091d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1281eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1282eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 12836631fa12SThomas Gleixner }; 12846631fa12SThomas Gleixner 12856631fa12SThomas Gleixner static const struct k_clock clock_monotonic = { 12866631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12876631fa12SThomas Gleixner .clock_get = posix_ktime_get_ts, 12886631fa12SThomas Gleixner .nsleep = common_nsleep, 12896631fa12SThomas Gleixner .timer_create = common_timer_create, 12906631fa12SThomas Gleixner .timer_set = common_timer_set, 12916631fa12SThomas Gleixner .timer_get = common_timer_get, 12926631fa12SThomas Gleixner .timer_del = common_timer_del, 1293f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 129491d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 129591d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1296eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1297eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 12986631fa12SThomas Gleixner }; 12996631fa12SThomas Gleixner 13006631fa12SThomas Gleixner static const struct k_clock clock_monotonic_raw = { 13016631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 13026631fa12SThomas Gleixner .clock_get = posix_get_monotonic_raw, 13036631fa12SThomas Gleixner }; 13046631fa12SThomas Gleixner 13056631fa12SThomas Gleixner static const struct k_clock clock_realtime_coarse = { 13066631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 13076631fa12SThomas Gleixner .clock_get = posix_get_realtime_coarse, 13086631fa12SThomas Gleixner }; 13096631fa12SThomas Gleixner 13106631fa12SThomas Gleixner static const struct k_clock clock_monotonic_coarse = { 13116631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 13126631fa12SThomas Gleixner .clock_get = posix_get_monotonic_coarse, 13136631fa12SThomas Gleixner }; 13146631fa12SThomas Gleixner 13156631fa12SThomas Gleixner static const struct k_clock clock_tai = { 13166631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 13176631fa12SThomas Gleixner .clock_get = posix_get_tai, 13186631fa12SThomas Gleixner .nsleep = common_nsleep, 13196631fa12SThomas Gleixner .timer_create = common_timer_create, 13206631fa12SThomas Gleixner .timer_set = common_timer_set, 13216631fa12SThomas Gleixner .timer_get = common_timer_get, 13226631fa12SThomas Gleixner .timer_del = common_timer_del, 1323f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 132491d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 132591d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1326eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1327eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 13286631fa12SThomas Gleixner }; 13296631fa12SThomas Gleixner 133072199320SThomas Gleixner static const struct k_clock clock_monotonic_active = { 13316631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 133272199320SThomas Gleixner .clock_get = posix_get_monotonic_active, 13336631fa12SThomas Gleixner }; 13346631fa12SThomas Gleixner 13356631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[] = { 13366631fa12SThomas Gleixner [CLOCK_REALTIME] = &clock_realtime, 13376631fa12SThomas Gleixner [CLOCK_MONOTONIC] = &clock_monotonic, 13386631fa12SThomas Gleixner [CLOCK_PROCESS_CPUTIME_ID] = &clock_process, 13396631fa12SThomas Gleixner [CLOCK_THREAD_CPUTIME_ID] = &clock_thread, 13406631fa12SThomas Gleixner [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw, 13416631fa12SThomas Gleixner [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse, 13426631fa12SThomas Gleixner [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse, 13437250a404SThomas Gleixner [CLOCK_BOOTTIME] = &clock_monotonic, 13446631fa12SThomas Gleixner [CLOCK_REALTIME_ALARM] = &alarm_clock, 13456631fa12SThomas Gleixner [CLOCK_BOOTTIME_ALARM] = &alarm_clock, 13466631fa12SThomas Gleixner [CLOCK_TAI] = &clock_tai, 134772199320SThomas Gleixner [CLOCK_MONOTONIC_ACTIVE] = &clock_monotonic_active, 13486631fa12SThomas Gleixner }; 13496631fa12SThomas Gleixner 13506631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id) 13516631fa12SThomas Gleixner { 135219b558dbSThomas Gleixner clockid_t idx = id; 135319b558dbSThomas Gleixner 135419b558dbSThomas Gleixner if (id < 0) { 13556631fa12SThomas Gleixner return (id & CLOCKFD_MASK) == CLOCKFD ? 13566631fa12SThomas Gleixner &clock_posix_dynamic : &clock_posix_cpu; 135719b558dbSThomas Gleixner } 13586631fa12SThomas Gleixner 135919b558dbSThomas Gleixner if (id >= ARRAY_SIZE(posix_clocks)) 13606631fa12SThomas Gleixner return NULL; 136119b558dbSThomas Gleixner 136219b558dbSThomas Gleixner return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))]; 13636631fa12SThomas Gleixner } 1364