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> 535cee9645SThomas Gleixner 548b094cd0SThomas Gleixner #include "timekeeping.h" 55bab0aae9SThomas Gleixner #include "posix-timers.h" 568b094cd0SThomas Gleixner 575cee9645SThomas Gleixner /* 585cee9645SThomas Gleixner * Management arrays for POSIX timers. Timers are now kept in static hash table 595cee9645SThomas Gleixner * with 512 entries. 605cee9645SThomas Gleixner * Timer ids are allocated by local routine, which selects proper hash head by 615cee9645SThomas Gleixner * key, constructed from current->signal address and per signal struct counter. 625cee9645SThomas Gleixner * This keeps timer ids unique per process, but now they can intersect between 635cee9645SThomas Gleixner * processes. 645cee9645SThomas Gleixner */ 655cee9645SThomas Gleixner 665cee9645SThomas Gleixner /* 675cee9645SThomas Gleixner * Lets keep our timers in a slab cache :-) 685cee9645SThomas Gleixner */ 695cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache; 705cee9645SThomas Gleixner 715cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9); 725cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock); 735cee9645SThomas Gleixner 746631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[]; 756631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id); 7667edab48SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic; 776631fa12SThomas Gleixner 785cee9645SThomas Gleixner /* 795cee9645SThomas Gleixner * we assume that the new SIGEV_THREAD_ID shares no bits with the other 805cee9645SThomas Gleixner * SIGEV values. Here we put out an error if this assumption fails. 815cee9645SThomas Gleixner */ 825cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ 835cee9645SThomas Gleixner ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD)) 845cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!" 855cee9645SThomas Gleixner #endif 865cee9645SThomas Gleixner 875cee9645SThomas Gleixner /* 885cee9645SThomas Gleixner * parisc wants ENOTSUP instead of EOPNOTSUPP 895cee9645SThomas Gleixner */ 905cee9645SThomas Gleixner #ifndef ENOTSUP 915cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP EOPNOTSUPP 925cee9645SThomas Gleixner #else 935cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP ENOTSUP 945cee9645SThomas Gleixner #endif 955cee9645SThomas Gleixner 965cee9645SThomas Gleixner /* 975cee9645SThomas Gleixner * The timer ID is turned into a timer address by idr_find(). 985cee9645SThomas Gleixner * Verifying a valid ID consists of: 995cee9645SThomas Gleixner * 1005cee9645SThomas Gleixner * a) checking that idr_find() returns other than -1. 1015cee9645SThomas Gleixner * b) checking that the timer id matches the one in the timer itself. 1025cee9645SThomas Gleixner * c) that the timer owner is in the callers thread group. 1035cee9645SThomas Gleixner */ 1045cee9645SThomas Gleixner 1055cee9645SThomas Gleixner /* 1065cee9645SThomas Gleixner * CLOCKs: The POSIX standard calls for a couple of clocks and allows us 1075cee9645SThomas Gleixner * to implement others. This structure defines the various 1085cee9645SThomas Gleixner * clocks. 1095cee9645SThomas Gleixner * 1105cee9645SThomas Gleixner * RESOLUTION: Clock resolution is used to round up timer and interval 1115cee9645SThomas Gleixner * times, NOT to report clock times, which are reported with as 1125cee9645SThomas Gleixner * much resolution as the system can muster. In some cases this 1135cee9645SThomas Gleixner * resolution may depend on the underlying clock hardware and 1145cee9645SThomas Gleixner * may not be quantifiable until run time, and only then is the 1155cee9645SThomas Gleixner * necessary code is written. The standard says we should say 1165cee9645SThomas Gleixner * something about this issue in the documentation... 1175cee9645SThomas Gleixner * 1185cee9645SThomas Gleixner * FUNCTIONS: The CLOCKs structure defines possible functions to 1195cee9645SThomas Gleixner * handle various clock functions. 1205cee9645SThomas Gleixner * 1215cee9645SThomas Gleixner * The standard POSIX timer management code assumes the 1225cee9645SThomas Gleixner * following: 1.) The k_itimer struct (sched.h) is used for 1235cee9645SThomas Gleixner * the timer. 2.) The list, it_lock, it_clock, it_id and 1245cee9645SThomas Gleixner * it_pid fields are not modified by timer code. 1255cee9645SThomas Gleixner * 1265cee9645SThomas Gleixner * Permissions: It is assumed that the clock_settime() function defined 1275cee9645SThomas Gleixner * for each clock will take care of permission checks. Some 1285cee9645SThomas Gleixner * clocks may be set able by any user (i.e. local process 1295cee9645SThomas Gleixner * clocks) others not. Currently the only set able clock we 1305cee9645SThomas Gleixner * have is CLOCK_REALTIME and its high res counter part, both of 1315cee9645SThomas Gleixner * which we beg off on and pass to do_sys_settimeofday(). 1325cee9645SThomas Gleixner */ 1335cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); 1345cee9645SThomas Gleixner 1355cee9645SThomas Gleixner #define lock_timer(tid, flags) \ 1365cee9645SThomas Gleixner ({ struct k_itimer *__timr; \ 1375cee9645SThomas Gleixner __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \ 1385cee9645SThomas Gleixner __timr; \ 1395cee9645SThomas Gleixner }) 1405cee9645SThomas Gleixner 1415cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr) 1425cee9645SThomas Gleixner { 1435cee9645SThomas Gleixner return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable)); 1445cee9645SThomas Gleixner } 1455cee9645SThomas Gleixner 1465cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head, 1475cee9645SThomas Gleixner struct signal_struct *sig, 1485cee9645SThomas Gleixner timer_t id) 1495cee9645SThomas Gleixner { 1505cee9645SThomas Gleixner struct k_itimer *timer; 1515cee9645SThomas Gleixner 1525cee9645SThomas Gleixner hlist_for_each_entry_rcu(timer, head, t_hash) { 1535cee9645SThomas Gleixner if ((timer->it_signal == sig) && (timer->it_id == id)) 1545cee9645SThomas Gleixner return timer; 1555cee9645SThomas Gleixner } 1565cee9645SThomas Gleixner return NULL; 1575cee9645SThomas Gleixner } 1585cee9645SThomas Gleixner 1595cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id) 1605cee9645SThomas Gleixner { 1615cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1625cee9645SThomas Gleixner struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)]; 1635cee9645SThomas Gleixner 1645cee9645SThomas Gleixner return __posix_timers_find(head, sig, id); 1655cee9645SThomas Gleixner } 1665cee9645SThomas Gleixner 1675cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer) 1685cee9645SThomas Gleixner { 1695cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1705cee9645SThomas Gleixner int first_free_id = sig->posix_timer_id; 1715cee9645SThomas Gleixner struct hlist_head *head; 1725cee9645SThomas Gleixner int ret = -ENOENT; 1735cee9645SThomas Gleixner 1745cee9645SThomas Gleixner do { 1755cee9645SThomas Gleixner spin_lock(&hash_lock); 1765cee9645SThomas Gleixner head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; 1775cee9645SThomas Gleixner if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { 1785cee9645SThomas Gleixner hlist_add_head_rcu(&timer->t_hash, head); 1795cee9645SThomas Gleixner ret = sig->posix_timer_id; 1805cee9645SThomas Gleixner } 1815cee9645SThomas Gleixner if (++sig->posix_timer_id < 0) 1825cee9645SThomas Gleixner sig->posix_timer_id = 0; 1835cee9645SThomas Gleixner if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT)) 1845cee9645SThomas Gleixner /* Loop over all possible ids completed */ 1855cee9645SThomas Gleixner ret = -EAGAIN; 1865cee9645SThomas Gleixner spin_unlock(&hash_lock); 1875cee9645SThomas Gleixner } while (ret == -ENOENT); 1885cee9645SThomas Gleixner return ret; 1895cee9645SThomas Gleixner } 1905cee9645SThomas Gleixner 1915cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) 1925cee9645SThomas Gleixner { 1935cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, flags); 1945cee9645SThomas Gleixner } 1955cee9645SThomas Gleixner 1965cee9645SThomas Gleixner /* Get clock_realtime */ 1973c9c12f4SDeepa Dinamani static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp) 1985cee9645SThomas Gleixner { 1993c9c12f4SDeepa Dinamani ktime_get_real_ts64(tp); 2005cee9645SThomas Gleixner return 0; 2015cee9645SThomas Gleixner } 2025cee9645SThomas Gleixner 2035cee9645SThomas Gleixner /* Set clock_realtime */ 2045cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock, 2050fe6afe3SDeepa Dinamani const struct timespec64 *tp) 2065cee9645SThomas Gleixner { 2070fe6afe3SDeepa Dinamani return do_sys_settimeofday64(tp, NULL); 2085cee9645SThomas Gleixner } 2095cee9645SThomas Gleixner 2105cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock, 2115cee9645SThomas Gleixner struct timex *t) 2125cee9645SThomas Gleixner { 2135cee9645SThomas Gleixner return do_adjtimex(t); 2145cee9645SThomas Gleixner } 2155cee9645SThomas Gleixner 2165cee9645SThomas Gleixner /* 2175cee9645SThomas Gleixner * Get monotonic time for posix timers 2185cee9645SThomas Gleixner */ 2193c9c12f4SDeepa Dinamani static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) 2205cee9645SThomas Gleixner { 2213c9c12f4SDeepa Dinamani ktime_get_ts64(tp); 2225cee9645SThomas Gleixner return 0; 2235cee9645SThomas Gleixner } 2245cee9645SThomas Gleixner 2255cee9645SThomas Gleixner /* 2265cee9645SThomas Gleixner * Get monotonic-raw time for posix timers 2275cee9645SThomas Gleixner */ 2283c9c12f4SDeepa Dinamani static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) 2295cee9645SThomas Gleixner { 2303c9c12f4SDeepa Dinamani getrawmonotonic64(tp); 2315cee9645SThomas Gleixner return 0; 2325cee9645SThomas Gleixner } 2335cee9645SThomas Gleixner 2345cee9645SThomas Gleixner 2353c9c12f4SDeepa Dinamani static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp) 2365cee9645SThomas Gleixner { 2373c9c12f4SDeepa Dinamani *tp = current_kernel_time64(); 2385cee9645SThomas Gleixner return 0; 2395cee9645SThomas Gleixner } 2405cee9645SThomas Gleixner 2415cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock, 2423c9c12f4SDeepa Dinamani struct timespec64 *tp) 2435cee9645SThomas Gleixner { 2443c9c12f4SDeepa Dinamani *tp = get_monotonic_coarse64(); 2455cee9645SThomas Gleixner return 0; 2465cee9645SThomas Gleixner } 2475cee9645SThomas Gleixner 248d2e3e0caSDeepa Dinamani static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp) 2495cee9645SThomas Gleixner { 250d2e3e0caSDeepa Dinamani *tp = ktime_to_timespec64(KTIME_LOW_RES); 2515cee9645SThomas Gleixner return 0; 2525cee9645SThomas Gleixner } 2535cee9645SThomas Gleixner 2543c9c12f4SDeepa Dinamani static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp) 2555cee9645SThomas Gleixner { 2563c9c12f4SDeepa Dinamani get_monotonic_boottime64(tp); 2575cee9645SThomas Gleixner return 0; 2585cee9645SThomas Gleixner } 2595cee9645SThomas Gleixner 2603c9c12f4SDeepa Dinamani static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp) 2615cee9645SThomas Gleixner { 2623c9c12f4SDeepa Dinamani timekeeping_clocktai64(tp); 2635cee9645SThomas Gleixner return 0; 2645cee9645SThomas Gleixner } 2655cee9645SThomas Gleixner 266d2e3e0caSDeepa Dinamani static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp) 267056a3cacSThomas Gleixner { 268056a3cacSThomas Gleixner tp->tv_sec = 0; 269056a3cacSThomas Gleixner tp->tv_nsec = hrtimer_resolution; 270056a3cacSThomas Gleixner return 0; 271056a3cacSThomas Gleixner } 272056a3cacSThomas Gleixner 273d3ba5a9aSChristoph Hellwig /* 274d3ba5a9aSChristoph Hellwig * Initialize everything, well, just everything in Posix clocks/timers ;) 275d3ba5a9aSChristoph Hellwig */ 276d3ba5a9aSChristoph Hellwig static __init int init_posix_timers(void) 277d3ba5a9aSChristoph Hellwig { 2785cee9645SThomas Gleixner posix_timers_cache = kmem_cache_create("posix_timers_cache", 2795cee9645SThomas Gleixner sizeof (struct k_itimer), 0, SLAB_PANIC, 2805cee9645SThomas Gleixner NULL); 2815cee9645SThomas Gleixner return 0; 2825cee9645SThomas Gleixner } 2835cee9645SThomas Gleixner __initcall(init_posix_timers); 2845cee9645SThomas Gleixner 285f37fb0aaSThomas Gleixner static void common_hrtimer_rearm(struct k_itimer *timr) 2865cee9645SThomas Gleixner { 2875cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 2885cee9645SThomas Gleixner 28980105cd0SThomas Gleixner if (!timr->it_interval) 2905cee9645SThomas Gleixner return; 2915cee9645SThomas Gleixner 2925cee9645SThomas Gleixner timr->it_overrun += (unsigned int) hrtimer_forward(timer, 2935cee9645SThomas Gleixner timer->base->get_time(), 29480105cd0SThomas Gleixner timr->it_interval); 2955cee9645SThomas Gleixner hrtimer_restart(timer); 2965cee9645SThomas Gleixner } 2975cee9645SThomas Gleixner 2985cee9645SThomas Gleixner /* 2995cee9645SThomas Gleixner * This function is exported for use by the signal deliver code. It is 3005cee9645SThomas Gleixner * called just prior to the info block being released and passes that 3015cee9645SThomas Gleixner * block to us. It's function is to update the overrun entry AND to 3025cee9645SThomas Gleixner * restart the timer. It should only be called if the timer is to be 3035cee9645SThomas Gleixner * restarted (i.e. we have flagged this in the sys_private entry of the 3045cee9645SThomas Gleixner * info block). 3055cee9645SThomas Gleixner * 3065cee9645SThomas Gleixner * To protect against the timer going away while the interrupt is queued, 3075cee9645SThomas Gleixner * we require that the it_requeue_pending flag be set. 3085cee9645SThomas Gleixner */ 30996fe3b07SThomas Gleixner void posixtimer_rearm(struct siginfo *info) 3105cee9645SThomas Gleixner { 3115cee9645SThomas Gleixner struct k_itimer *timr; 3125cee9645SThomas Gleixner unsigned long flags; 3135cee9645SThomas Gleixner 3145cee9645SThomas Gleixner timr = lock_timer(info->si_tid, &flags); 315af888d67SThomas Gleixner if (!timr) 316af888d67SThomas Gleixner return; 3175cee9645SThomas Gleixner 318af888d67SThomas Gleixner if (timr->it_requeue_pending == info->si_sys_private) { 319f37fb0aaSThomas Gleixner timr->kclock->timer_rearm(timr); 3205cee9645SThomas Gleixner 32121e55c1fSThomas Gleixner timr->it_active = 1; 322af888d67SThomas Gleixner timr->it_overrun_last = timr->it_overrun; 323af888d67SThomas Gleixner timr->it_overrun = -1; 324af888d67SThomas Gleixner ++timr->it_requeue_pending; 325af888d67SThomas Gleixner 3265cee9645SThomas Gleixner info->si_overrun += timr->it_overrun_last; 3275cee9645SThomas Gleixner } 3285cee9645SThomas Gleixner 3295cee9645SThomas Gleixner unlock_timer(timr, flags); 3305cee9645SThomas Gleixner } 3315cee9645SThomas Gleixner 3325cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private) 3335cee9645SThomas Gleixner { 3345cee9645SThomas Gleixner struct task_struct *task; 3355cee9645SThomas Gleixner int shared, ret = -1; 3365cee9645SThomas Gleixner /* 3375cee9645SThomas Gleixner * FIXME: if ->sigq is queued we can race with 33896fe3b07SThomas Gleixner * dequeue_signal()->posixtimer_rearm(). 3395cee9645SThomas Gleixner * 3405cee9645SThomas Gleixner * If dequeue_signal() sees the "right" value of 34196fe3b07SThomas Gleixner * si_sys_private it calls posixtimer_rearm(). 3425cee9645SThomas Gleixner * We re-queue ->sigq and drop ->it_lock(). 34396fe3b07SThomas Gleixner * posixtimer_rearm() locks the timer 3445cee9645SThomas Gleixner * and re-schedules it while ->sigq is pending. 3455cee9645SThomas Gleixner * Not really bad, but not that we want. 3465cee9645SThomas Gleixner */ 3475cee9645SThomas Gleixner timr->sigq->info.si_sys_private = si_private; 3485cee9645SThomas Gleixner 3495cee9645SThomas Gleixner rcu_read_lock(); 3505cee9645SThomas Gleixner task = pid_task(timr->it_pid, PIDTYPE_PID); 3515cee9645SThomas Gleixner if (task) { 3525cee9645SThomas Gleixner shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID); 3535cee9645SThomas Gleixner ret = send_sigqueue(timr->sigq, task, shared); 3545cee9645SThomas Gleixner } 3555cee9645SThomas Gleixner rcu_read_unlock(); 3565cee9645SThomas Gleixner /* If we failed to send the signal the timer stops. */ 3575cee9645SThomas Gleixner return ret > 0; 3585cee9645SThomas Gleixner } 3595cee9645SThomas Gleixner 3605cee9645SThomas Gleixner /* 3615cee9645SThomas Gleixner * This function gets called when a POSIX.1b interval timer expires. It 3625cee9645SThomas Gleixner * is used as a callback from the kernel internal timer. The 3635cee9645SThomas Gleixner * run_timer_list code ALWAYS calls with interrupts on. 3645cee9645SThomas Gleixner 3655cee9645SThomas Gleixner * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. 3665cee9645SThomas Gleixner */ 3675cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) 3685cee9645SThomas Gleixner { 3695cee9645SThomas Gleixner struct k_itimer *timr; 3705cee9645SThomas Gleixner unsigned long flags; 3715cee9645SThomas Gleixner int si_private = 0; 3725cee9645SThomas Gleixner enum hrtimer_restart ret = HRTIMER_NORESTART; 3735cee9645SThomas Gleixner 3745cee9645SThomas Gleixner timr = container_of(timer, struct k_itimer, it.real.timer); 3755cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, flags); 3765cee9645SThomas Gleixner 37721e55c1fSThomas Gleixner timr->it_active = 0; 37880105cd0SThomas Gleixner if (timr->it_interval != 0) 3795cee9645SThomas Gleixner si_private = ++timr->it_requeue_pending; 3805cee9645SThomas Gleixner 3815cee9645SThomas Gleixner if (posix_timer_event(timr, si_private)) { 3825cee9645SThomas Gleixner /* 3835cee9645SThomas Gleixner * signal was not sent because of sig_ignor 3845cee9645SThomas Gleixner * we will not get a call back to restart it AND 3855cee9645SThomas Gleixner * it should be restarted. 3865cee9645SThomas Gleixner */ 38780105cd0SThomas Gleixner if (timr->it_interval != 0) { 3885cee9645SThomas Gleixner ktime_t now = hrtimer_cb_get_time(timer); 3895cee9645SThomas Gleixner 3905cee9645SThomas Gleixner /* 3915cee9645SThomas Gleixner * FIXME: What we really want, is to stop this 3925cee9645SThomas Gleixner * timer completely and restart it in case the 3935cee9645SThomas Gleixner * SIG_IGN is removed. This is a non trivial 3945cee9645SThomas Gleixner * change which involves sighand locking 3955cee9645SThomas Gleixner * (sigh !), which we don't want to do late in 3965cee9645SThomas Gleixner * the release cycle. 3975cee9645SThomas Gleixner * 3985cee9645SThomas Gleixner * For now we just let timers with an interval 3995cee9645SThomas Gleixner * less than a jiffie expire every jiffie to 4005cee9645SThomas Gleixner * avoid softirq starvation in case of SIG_IGN 4015cee9645SThomas Gleixner * and a very small interval, which would put 4025cee9645SThomas Gleixner * the timer right back on the softirq pending 4035cee9645SThomas Gleixner * list. By moving now ahead of time we trick 4045cee9645SThomas Gleixner * hrtimer_forward() to expire the timer 4055cee9645SThomas Gleixner * later, while we still maintain the overrun 4065cee9645SThomas Gleixner * accuracy, but have some inconsistency in 4075cee9645SThomas Gleixner * the timer_gettime() case. This is at least 4085cee9645SThomas Gleixner * better than a starved softirq. A more 4095cee9645SThomas Gleixner * complex fix which solves also another related 4105cee9645SThomas Gleixner * inconsistency is already in the pipeline. 4115cee9645SThomas Gleixner */ 4125cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 4135cee9645SThomas Gleixner { 4148b0e1953SThomas Gleixner ktime_t kj = NSEC_PER_SEC / HZ; 4155cee9645SThomas Gleixner 41680105cd0SThomas Gleixner if (timr->it_interval < kj) 4175cee9645SThomas Gleixner now = ktime_add(now, kj); 4185cee9645SThomas Gleixner } 4195cee9645SThomas Gleixner #endif 4205cee9645SThomas Gleixner timr->it_overrun += (unsigned int) 4215cee9645SThomas Gleixner hrtimer_forward(timer, now, 42280105cd0SThomas Gleixner timr->it_interval); 4235cee9645SThomas Gleixner ret = HRTIMER_RESTART; 4245cee9645SThomas Gleixner ++timr->it_requeue_pending; 42521e55c1fSThomas Gleixner timr->it_active = 1; 4265cee9645SThomas Gleixner } 4275cee9645SThomas Gleixner } 4285cee9645SThomas Gleixner 4295cee9645SThomas Gleixner unlock_timer(timr, flags); 4305cee9645SThomas Gleixner return ret; 4315cee9645SThomas Gleixner } 4325cee9645SThomas Gleixner 4335cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event) 4345cee9645SThomas Gleixner { 4355cee9645SThomas Gleixner struct task_struct *rtn = current->group_leader; 4365cee9645SThomas Gleixner 4375cee9645SThomas Gleixner if ((event->sigev_notify & SIGEV_THREAD_ID ) && 4385cee9645SThomas Gleixner (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) || 4395cee9645SThomas Gleixner !same_thread_group(rtn, current) || 4405cee9645SThomas Gleixner (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL)) 4415cee9645SThomas Gleixner return NULL; 4425cee9645SThomas Gleixner 4435cee9645SThomas Gleixner if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) && 4445cee9645SThomas Gleixner ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX))) 4455cee9645SThomas Gleixner return NULL; 4465cee9645SThomas Gleixner 4475cee9645SThomas Gleixner return task_pid(rtn); 4485cee9645SThomas Gleixner } 4495cee9645SThomas Gleixner 4505cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void) 4515cee9645SThomas Gleixner { 4525cee9645SThomas Gleixner struct k_itimer *tmr; 4535cee9645SThomas Gleixner tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); 4545cee9645SThomas Gleixner if (!tmr) 4555cee9645SThomas Gleixner return tmr; 4565cee9645SThomas Gleixner if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { 4575cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4585cee9645SThomas Gleixner return NULL; 4595cee9645SThomas Gleixner } 4605cee9645SThomas Gleixner memset(&tmr->sigq->info, 0, sizeof(siginfo_t)); 4615cee9645SThomas Gleixner return tmr; 4625cee9645SThomas Gleixner } 4635cee9645SThomas Gleixner 4645cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head) 4655cee9645SThomas Gleixner { 4665cee9645SThomas Gleixner struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu); 4675cee9645SThomas Gleixner 4685cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4695cee9645SThomas Gleixner } 4705cee9645SThomas Gleixner 4715cee9645SThomas Gleixner #define IT_ID_SET 1 4725cee9645SThomas Gleixner #define IT_ID_NOT_SET 0 4735cee9645SThomas Gleixner static void release_posix_timer(struct k_itimer *tmr, int it_id_set) 4745cee9645SThomas Gleixner { 4755cee9645SThomas Gleixner if (it_id_set) { 4765cee9645SThomas Gleixner unsigned long flags; 4775cee9645SThomas Gleixner spin_lock_irqsave(&hash_lock, flags); 4785cee9645SThomas Gleixner hlist_del_rcu(&tmr->t_hash); 4795cee9645SThomas Gleixner spin_unlock_irqrestore(&hash_lock, flags); 4805cee9645SThomas Gleixner } 4815cee9645SThomas Gleixner put_pid(tmr->it_pid); 4825cee9645SThomas Gleixner sigqueue_free(tmr->sigq); 4835cee9645SThomas Gleixner call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 4845cee9645SThomas Gleixner } 4855cee9645SThomas Gleixner 4865cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer) 4875cee9645SThomas Gleixner { 4885cee9645SThomas Gleixner hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); 4895cee9645SThomas Gleixner return 0; 4905cee9645SThomas Gleixner } 4915cee9645SThomas Gleixner 4925cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */ 4935cee9645SThomas Gleixner 4945cee9645SThomas Gleixner SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, 4955cee9645SThomas Gleixner struct sigevent __user *, timer_event_spec, 4965cee9645SThomas Gleixner timer_t __user *, created_timer_id) 4975cee9645SThomas Gleixner { 498d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 4995cee9645SThomas Gleixner struct k_itimer *new_timer; 5005cee9645SThomas Gleixner int error, new_timer_id; 5015cee9645SThomas Gleixner sigevent_t event; 5025cee9645SThomas Gleixner int it_id_set = IT_ID_NOT_SET; 5035cee9645SThomas Gleixner 5045cee9645SThomas Gleixner if (!kc) 5055cee9645SThomas Gleixner return -EINVAL; 5065cee9645SThomas Gleixner if (!kc->timer_create) 5075cee9645SThomas Gleixner return -EOPNOTSUPP; 5085cee9645SThomas Gleixner 5095cee9645SThomas Gleixner new_timer = alloc_posix_timer(); 5105cee9645SThomas Gleixner if (unlikely(!new_timer)) 5115cee9645SThomas Gleixner return -EAGAIN; 5125cee9645SThomas Gleixner 5135cee9645SThomas Gleixner spin_lock_init(&new_timer->it_lock); 5145cee9645SThomas Gleixner new_timer_id = posix_timer_add(new_timer); 5155cee9645SThomas Gleixner if (new_timer_id < 0) { 5165cee9645SThomas Gleixner error = new_timer_id; 5175cee9645SThomas Gleixner goto out; 5185cee9645SThomas Gleixner } 5195cee9645SThomas Gleixner 5205cee9645SThomas Gleixner it_id_set = IT_ID_SET; 5215cee9645SThomas Gleixner new_timer->it_id = (timer_t) new_timer_id; 5225cee9645SThomas Gleixner new_timer->it_clock = which_clock; 523d97bb75dSThomas Gleixner new_timer->kclock = kc; 5245cee9645SThomas Gleixner new_timer->it_overrun = -1; 5255cee9645SThomas Gleixner 5265cee9645SThomas Gleixner if (timer_event_spec) { 5275cee9645SThomas Gleixner if (copy_from_user(&event, timer_event_spec, sizeof (event))) { 5285cee9645SThomas Gleixner error = -EFAULT; 5295cee9645SThomas Gleixner goto out; 5305cee9645SThomas Gleixner } 5315cee9645SThomas Gleixner rcu_read_lock(); 5325cee9645SThomas Gleixner 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 } 5385cee9645SThomas Gleixner } else { 5396891c450SMathias Krause memset(&event.sigev_value, 0, sizeof(event.sigev_value)); 5405cee9645SThomas Gleixner event.sigev_notify = SIGEV_SIGNAL; 5415cee9645SThomas Gleixner event.sigev_signo = SIGALRM; 5425cee9645SThomas Gleixner event.sigev_value.sival_int = new_timer->it_id; 5435cee9645SThomas Gleixner new_timer->it_pid = get_pid(task_tgid(current)); 5445cee9645SThomas Gleixner } 5455cee9645SThomas Gleixner 5465cee9645SThomas Gleixner new_timer->it_sigev_notify = event.sigev_notify; 5475cee9645SThomas Gleixner new_timer->sigq->info.si_signo = event.sigev_signo; 5485cee9645SThomas Gleixner new_timer->sigq->info.si_value = event.sigev_value; 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 5795cee9645SThomas Gleixner /* 5805cee9645SThomas Gleixner * Locking issues: We need to protect the result of the id look up until 5815cee9645SThomas Gleixner * we get the timer locked down so it is not deleted under us. The 5825cee9645SThomas Gleixner * removal is done under the idr spinlock so we use that here to bridge 5835cee9645SThomas Gleixner * the find to the timer lock. To avoid a dead lock, the timer id MUST 5845cee9645SThomas Gleixner * be release with out holding the timer lock. 5855cee9645SThomas Gleixner */ 5865cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) 5875cee9645SThomas Gleixner { 5885cee9645SThomas Gleixner struct k_itimer *timr; 5895cee9645SThomas Gleixner 5905cee9645SThomas Gleixner /* 5915cee9645SThomas Gleixner * timer_t could be any type >= int and we want to make sure any 5925cee9645SThomas Gleixner * @timer_id outside positive int range fails lookup. 5935cee9645SThomas Gleixner */ 5945cee9645SThomas Gleixner if ((unsigned long long)timer_id > INT_MAX) 5955cee9645SThomas Gleixner return NULL; 5965cee9645SThomas Gleixner 5975cee9645SThomas Gleixner rcu_read_lock(); 5985cee9645SThomas Gleixner timr = posix_timer_by_id(timer_id); 5995cee9645SThomas Gleixner if (timr) { 6005cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, *flags); 6015cee9645SThomas Gleixner if (timr->it_signal == current->signal) { 6025cee9645SThomas Gleixner rcu_read_unlock(); 6035cee9645SThomas Gleixner return timr; 6045cee9645SThomas Gleixner } 6055cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, *flags); 6065cee9645SThomas Gleixner } 6075cee9645SThomas Gleixner rcu_read_unlock(); 6085cee9645SThomas Gleixner 6095cee9645SThomas Gleixner return NULL; 6105cee9645SThomas Gleixner } 6115cee9645SThomas Gleixner 61291d57baeSThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now) 61391d57baeSThomas Gleixner { 61491d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 61591d57baeSThomas Gleixner 61691d57baeSThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, now); 61791d57baeSThomas Gleixner } 61891d57baeSThomas Gleixner 61991d57baeSThomas Gleixner static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now) 62091d57baeSThomas Gleixner { 62191d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 62291d57baeSThomas Gleixner 62391d57baeSThomas Gleixner return (int)hrtimer_forward(timer, now, timr->it_interval); 62491d57baeSThomas Gleixner } 62591d57baeSThomas Gleixner 6265cee9645SThomas Gleixner /* 6275cee9645SThomas Gleixner * Get the time remaining on a POSIX.1b interval timer. This function 6285cee9645SThomas Gleixner * is ALWAYS called with spin_lock_irq on the timer, thus it must not 6295cee9645SThomas Gleixner * mess with irq. 6305cee9645SThomas Gleixner * 6315cee9645SThomas Gleixner * We have a couple of messes to clean up here. First there is the case 6325cee9645SThomas Gleixner * of a timer that has a requeue pending. These timers should appear to 6335cee9645SThomas Gleixner * be in the timer list with an expiry as if we were to requeue them 6345cee9645SThomas Gleixner * now. 6355cee9645SThomas Gleixner * 6365cee9645SThomas Gleixner * The second issue is the SIGEV_NONE timer which may be active but is 6375cee9645SThomas Gleixner * not really ever put in the timer list (to save system resources). 6385cee9645SThomas Gleixner * This timer may be expired, and if so, we will do it here. Otherwise 6395cee9645SThomas Gleixner * it is the same as a requeue pending timer WRT to what we should 6405cee9645SThomas Gleixner * report. 6415cee9645SThomas Gleixner */ 642f2c45807SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) 6435cee9645SThomas Gleixner { 64491d57baeSThomas Gleixner const struct k_clock *kc = timr->kclock; 6455cee9645SThomas Gleixner ktime_t now, remaining, iv; 64691d57baeSThomas Gleixner struct timespec64 ts64; 64791d57baeSThomas Gleixner bool sig_none; 6485cee9645SThomas Gleixner 649c6503be5SThomas Gleixner sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE; 65080105cd0SThomas Gleixner iv = timr->it_interval; 6515cee9645SThomas Gleixner 6525cee9645SThomas Gleixner /* interval timer ? */ 65391d57baeSThomas Gleixner if (iv) { 6545f252b32SDeepa Dinamani cur_setting->it_interval = ktime_to_timespec64(iv); 65591d57baeSThomas Gleixner } else if (!timr->it_active) { 65691d57baeSThomas Gleixner /* 65791d57baeSThomas Gleixner * SIGEV_NONE oneshot timers are never queued. Check them 65891d57baeSThomas Gleixner * below. 65991d57baeSThomas Gleixner */ 66091d57baeSThomas Gleixner if (!sig_none) 6615cee9645SThomas Gleixner return; 66291d57baeSThomas Gleixner } 6635cee9645SThomas Gleixner 6645cee9645SThomas Gleixner /* 66591d57baeSThomas Gleixner * The timespec64 based conversion is suboptimal, but it's not 66691d57baeSThomas Gleixner * worth to implement yet another callback. 6675cee9645SThomas Gleixner */ 66891d57baeSThomas Gleixner kc->clock_get(timr->it_clock, &ts64); 66991d57baeSThomas Gleixner now = timespec64_to_ktime(ts64); 6705cee9645SThomas Gleixner 67191d57baeSThomas Gleixner /* 67291d57baeSThomas Gleixner * When a requeue is pending or this is a SIGEV_NONE timer move the 67391d57baeSThomas Gleixner * expiry time forward by intervals, so expiry is > now. 67491d57baeSThomas Gleixner */ 67591d57baeSThomas Gleixner if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) 67691d57baeSThomas Gleixner timr->it_overrun += kc->timer_forward(timr, now); 67791d57baeSThomas Gleixner 67891d57baeSThomas Gleixner remaining = kc->timer_remaining(timr, now); 6795cee9645SThomas Gleixner /* Return 0 only, when the timer is expired and not pending */ 6802456e855SThomas Gleixner if (remaining <= 0) { 6815cee9645SThomas Gleixner /* 6825cee9645SThomas Gleixner * A single shot SIGEV_NONE timer must return 0, when 6835cee9645SThomas Gleixner * it is expired ! 6845cee9645SThomas Gleixner */ 68591d57baeSThomas Gleixner if (!sig_none) 6865cee9645SThomas Gleixner cur_setting->it_value.tv_nsec = 1; 68791d57baeSThomas Gleixner } else { 6885f252b32SDeepa Dinamani cur_setting->it_value = ktime_to_timespec64(remaining); 6895cee9645SThomas Gleixner } 69091d57baeSThomas Gleixner } 6915cee9645SThomas Gleixner 6925cee9645SThomas Gleixner /* Get the time remaining on a POSIX.1b interval timer. */ 6935cee9645SThomas Gleixner SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 6945cee9645SThomas Gleixner struct itimerspec __user *, setting) 6955cee9645SThomas Gleixner { 6965f252b32SDeepa Dinamani struct itimerspec64 cur_setting64; 6975cee9645SThomas Gleixner struct itimerspec cur_setting; 6985cee9645SThomas Gleixner struct k_itimer *timr; 699d3ba5a9aSChristoph Hellwig const struct k_clock *kc; 7005cee9645SThomas Gleixner unsigned long flags; 7015cee9645SThomas Gleixner int ret = 0; 7025cee9645SThomas Gleixner 7035cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7045cee9645SThomas Gleixner if (!timr) 7055cee9645SThomas Gleixner return -EINVAL; 7065cee9645SThomas Gleixner 707eabdec04SThomas Gleixner memset(&cur_setting64, 0, sizeof(cur_setting64)); 708d97bb75dSThomas Gleixner kc = timr->kclock; 7095cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_get)) 7105cee9645SThomas Gleixner ret = -EINVAL; 7115cee9645SThomas Gleixner else 7125f252b32SDeepa Dinamani kc->timer_get(timr, &cur_setting64); 7135cee9645SThomas Gleixner 7145cee9645SThomas Gleixner unlock_timer(timr, flags); 7155cee9645SThomas Gleixner 7165f252b32SDeepa Dinamani cur_setting = itimerspec64_to_itimerspec(&cur_setting64); 7175cee9645SThomas Gleixner if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) 7185cee9645SThomas Gleixner return -EFAULT; 7195cee9645SThomas Gleixner 7205cee9645SThomas Gleixner return ret; 7215cee9645SThomas Gleixner } 7225cee9645SThomas Gleixner 7235cee9645SThomas Gleixner /* 7245cee9645SThomas Gleixner * Get the number of overruns of a POSIX.1b interval timer. This is to 7255cee9645SThomas Gleixner * be the overrun of the timer last delivered. At the same time we are 7265cee9645SThomas Gleixner * accumulating overruns on the next timer. The overrun is frozen when 7275cee9645SThomas Gleixner * the signal is delivered, either at the notify time (if the info block 7285cee9645SThomas Gleixner * is not queued) or at the actual delivery time (as we are informed by 72996fe3b07SThomas Gleixner * the call back to posixtimer_rearm(). So all we need to do is 7305cee9645SThomas Gleixner * to pick up the frozen overrun. 7315cee9645SThomas Gleixner */ 7325cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) 7335cee9645SThomas Gleixner { 7345cee9645SThomas Gleixner struct k_itimer *timr; 7355cee9645SThomas Gleixner int overrun; 7365cee9645SThomas Gleixner unsigned long flags; 7375cee9645SThomas Gleixner 7385cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7395cee9645SThomas Gleixner if (!timr) 7405cee9645SThomas Gleixner return -EINVAL; 7415cee9645SThomas Gleixner 7425cee9645SThomas Gleixner overrun = timr->it_overrun_last; 7435cee9645SThomas Gleixner unlock_timer(timr, flags); 7445cee9645SThomas Gleixner 7455cee9645SThomas Gleixner return overrun; 7465cee9645SThomas Gleixner } 7475cee9645SThomas Gleixner 748eae1c4aeSThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires, 749eae1c4aeSThomas Gleixner bool absolute, bool sigev_none) 7505cee9645SThomas Gleixner { 7515cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 7525cee9645SThomas Gleixner enum hrtimer_mode mode; 7535cee9645SThomas Gleixner 754eae1c4aeSThomas Gleixner mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; 75567edab48SThomas Gleixner /* 75667edab48SThomas Gleixner * Posix magic: Relative CLOCK_REALTIME timers are not affected by 75767edab48SThomas Gleixner * clock modifications, so they become CLOCK_MONOTONIC based under the 75867edab48SThomas Gleixner * hood. See hrtimer_init(). Update timr->kclock, so the generic 75967edab48SThomas Gleixner * functions which use timr->kclock->clock_get() work. 76067edab48SThomas Gleixner * 76167edab48SThomas Gleixner * Note: it_clock stays unmodified, because the next timer_set() might 76267edab48SThomas Gleixner * use ABSTIME, so it needs to switch back. 76367edab48SThomas Gleixner */ 76467edab48SThomas Gleixner if (timr->it_clock == CLOCK_REALTIME) 76567edab48SThomas Gleixner timr->kclock = absolute ? &clock_realtime : &clock_monotonic; 76667edab48SThomas Gleixner 767eae1c4aeSThomas Gleixner hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); 768eae1c4aeSThomas Gleixner timr->it.real.timer.function = posix_timer_fn; 769eae1c4aeSThomas Gleixner 770eae1c4aeSThomas Gleixner if (!absolute) 771eae1c4aeSThomas Gleixner expires = ktime_add_safe(expires, timer->base->get_time()); 772eae1c4aeSThomas Gleixner hrtimer_set_expires(timer, expires); 773eae1c4aeSThomas Gleixner 774eae1c4aeSThomas Gleixner if (!sigev_none) 775eae1c4aeSThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 776eae1c4aeSThomas Gleixner } 777eae1c4aeSThomas Gleixner 778eae1c4aeSThomas Gleixner static int common_hrtimer_try_to_cancel(struct k_itimer *timr) 779eae1c4aeSThomas Gleixner { 780eae1c4aeSThomas Gleixner return hrtimer_try_to_cancel(&timr->it.real.timer); 781eae1c4aeSThomas Gleixner } 782eae1c4aeSThomas Gleixner 783eae1c4aeSThomas Gleixner /* Set a POSIX.1b interval timer. */ 784f2c45807SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags, 785eae1c4aeSThomas Gleixner struct itimerspec64 *new_setting, 786eae1c4aeSThomas Gleixner struct itimerspec64 *old_setting) 787eae1c4aeSThomas Gleixner { 788eae1c4aeSThomas Gleixner const struct k_clock *kc = timr->kclock; 789eae1c4aeSThomas Gleixner bool sigev_none; 790eae1c4aeSThomas Gleixner ktime_t expires; 791eae1c4aeSThomas Gleixner 7925cee9645SThomas Gleixner if (old_setting) 7935cee9645SThomas Gleixner common_timer_get(timr, old_setting); 7945cee9645SThomas Gleixner 795eae1c4aeSThomas Gleixner /* Prevent rearming by clearing the interval */ 79680105cd0SThomas Gleixner timr->it_interval = 0; 7975cee9645SThomas Gleixner /* 798eae1c4aeSThomas Gleixner * Careful here. On SMP systems the timer expiry function could be 799eae1c4aeSThomas Gleixner * active and spinning on timr->it_lock. 8005cee9645SThomas Gleixner */ 801eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timr) < 0) 8025cee9645SThomas Gleixner return TIMER_RETRY; 8035cee9645SThomas Gleixner 80421e55c1fSThomas Gleixner timr->it_active = 0; 8055cee9645SThomas Gleixner timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 8065cee9645SThomas Gleixner ~REQUEUE_PENDING; 8075cee9645SThomas Gleixner timr->it_overrun_last = 0; 8085cee9645SThomas Gleixner 809eae1c4aeSThomas Gleixner /* Switch off the timer when it_value is zero */ 8105cee9645SThomas Gleixner if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 8115cee9645SThomas Gleixner return 0; 8125cee9645SThomas Gleixner 81380105cd0SThomas Gleixner timr->it_interval = timespec64_to_ktime(new_setting->it_interval); 814eae1c4aeSThomas Gleixner expires = timespec64_to_ktime(new_setting->it_value); 815eae1c4aeSThomas Gleixner sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE; 8165cee9645SThomas Gleixner 817eae1c4aeSThomas Gleixner kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); 818eae1c4aeSThomas Gleixner timr->it_active = !sigev_none; 8195cee9645SThomas Gleixner return 0; 8205cee9645SThomas Gleixner } 8215cee9645SThomas Gleixner 8225cee9645SThomas Gleixner /* Set a POSIX.1b interval timer */ 8235cee9645SThomas Gleixner SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 8245cee9645SThomas Gleixner const struct itimerspec __user *, new_setting, 8255cee9645SThomas Gleixner struct itimerspec __user *, old_setting) 8265cee9645SThomas Gleixner { 8275f252b32SDeepa Dinamani struct itimerspec64 new_spec64, old_spec64; 8285f252b32SDeepa Dinamani struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL; 8295cee9645SThomas Gleixner struct itimerspec new_spec, old_spec; 8305f252b32SDeepa Dinamani struct k_itimer *timr; 8315cee9645SThomas Gleixner unsigned long flag; 832d3ba5a9aSChristoph Hellwig const struct k_clock *kc; 8335f252b32SDeepa Dinamani int error = 0; 8345cee9645SThomas Gleixner 8355cee9645SThomas Gleixner if (!new_setting) 8365cee9645SThomas Gleixner return -EINVAL; 8375cee9645SThomas Gleixner 8385cee9645SThomas Gleixner if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) 8395cee9645SThomas Gleixner return -EFAULT; 8405f252b32SDeepa Dinamani new_spec64 = itimerspec_to_itimerspec64(&new_spec); 8415cee9645SThomas Gleixner 8425f252b32SDeepa Dinamani if (!timespec64_valid(&new_spec64.it_interval) || 8435f252b32SDeepa Dinamani !timespec64_valid(&new_spec64.it_value)) 8445cee9645SThomas Gleixner return -EINVAL; 8455c7a3a3dSThomas Gleixner if (rtn) 8465c7a3a3dSThomas Gleixner memset(rtn, 0, sizeof(*rtn)); 8475cee9645SThomas Gleixner retry: 8485cee9645SThomas Gleixner timr = lock_timer(timer_id, &flag); 8495cee9645SThomas Gleixner if (!timr) 8505cee9645SThomas Gleixner return -EINVAL; 8515cee9645SThomas Gleixner 852d97bb75dSThomas Gleixner kc = timr->kclock; 8535cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_set)) 8545cee9645SThomas Gleixner error = -EINVAL; 8555cee9645SThomas Gleixner else 8565f252b32SDeepa Dinamani error = kc->timer_set(timr, flags, &new_spec64, rtn); 8575cee9645SThomas Gleixner 8585cee9645SThomas Gleixner unlock_timer(timr, flag); 8595cee9645SThomas Gleixner if (error == TIMER_RETRY) { 8605cee9645SThomas Gleixner rtn = NULL; // We already got the old time... 8615cee9645SThomas Gleixner goto retry; 8625cee9645SThomas Gleixner } 8635cee9645SThomas Gleixner 8645f252b32SDeepa Dinamani old_spec = itimerspec64_to_itimerspec(&old_spec64); 8655cee9645SThomas Gleixner if (old_setting && !error && 8665cee9645SThomas Gleixner copy_to_user(old_setting, &old_spec, sizeof (old_spec))) 8675cee9645SThomas Gleixner error = -EFAULT; 8685cee9645SThomas Gleixner 8695cee9645SThomas Gleixner return error; 8705cee9645SThomas Gleixner } 8715cee9645SThomas Gleixner 872f2c45807SThomas Gleixner int common_timer_del(struct k_itimer *timer) 8735cee9645SThomas Gleixner { 874eae1c4aeSThomas Gleixner const struct k_clock *kc = timer->kclock; 8755cee9645SThomas Gleixner 876eae1c4aeSThomas Gleixner timer->it_interval = 0; 877eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timer) < 0) 8785cee9645SThomas Gleixner return TIMER_RETRY; 87921e55c1fSThomas Gleixner timer->it_active = 0; 8805cee9645SThomas Gleixner return 0; 8815cee9645SThomas Gleixner } 8825cee9645SThomas Gleixner 8835cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer) 8845cee9645SThomas Gleixner { 885d97bb75dSThomas Gleixner const struct k_clock *kc = timer->kclock; 8865cee9645SThomas Gleixner 8875cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_del)) 8885cee9645SThomas Gleixner return -EINVAL; 8895cee9645SThomas Gleixner return kc->timer_del(timer); 8905cee9645SThomas Gleixner } 8915cee9645SThomas Gleixner 8925cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */ 8935cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) 8945cee9645SThomas Gleixner { 8955cee9645SThomas Gleixner struct k_itimer *timer; 8965cee9645SThomas Gleixner unsigned long flags; 8975cee9645SThomas Gleixner 8985cee9645SThomas Gleixner retry_delete: 8995cee9645SThomas Gleixner timer = lock_timer(timer_id, &flags); 9005cee9645SThomas Gleixner if (!timer) 9015cee9645SThomas Gleixner return -EINVAL; 9025cee9645SThomas Gleixner 9035cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) { 9045cee9645SThomas Gleixner unlock_timer(timer, flags); 9055cee9645SThomas Gleixner goto retry_delete; 9065cee9645SThomas Gleixner } 9075cee9645SThomas Gleixner 9085cee9645SThomas Gleixner spin_lock(¤t->sighand->siglock); 9095cee9645SThomas Gleixner list_del(&timer->list); 9105cee9645SThomas Gleixner spin_unlock(¤t->sighand->siglock); 9115cee9645SThomas Gleixner /* 9125cee9645SThomas Gleixner * This keeps any tasks waiting on the spin lock from thinking 9135cee9645SThomas Gleixner * they got something (see the lock code above). 9145cee9645SThomas Gleixner */ 9155cee9645SThomas Gleixner timer->it_signal = NULL; 9165cee9645SThomas Gleixner 9175cee9645SThomas Gleixner unlock_timer(timer, flags); 9185cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 9195cee9645SThomas Gleixner return 0; 9205cee9645SThomas Gleixner } 9215cee9645SThomas Gleixner 9225cee9645SThomas Gleixner /* 9235cee9645SThomas Gleixner * return timer owned by the process, used by exit_itimers 9245cee9645SThomas Gleixner */ 9255cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer) 9265cee9645SThomas Gleixner { 9275cee9645SThomas Gleixner unsigned long flags; 9285cee9645SThomas Gleixner 9295cee9645SThomas Gleixner retry_delete: 9305cee9645SThomas Gleixner spin_lock_irqsave(&timer->it_lock, flags); 9315cee9645SThomas Gleixner 9325cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) { 9335cee9645SThomas Gleixner unlock_timer(timer, flags); 9345cee9645SThomas Gleixner goto retry_delete; 9355cee9645SThomas Gleixner } 9365cee9645SThomas Gleixner list_del(&timer->list); 9375cee9645SThomas Gleixner /* 9385cee9645SThomas Gleixner * This keeps any tasks waiting on the spin lock from thinking 9395cee9645SThomas Gleixner * they got something (see the lock code above). 9405cee9645SThomas Gleixner */ 9415cee9645SThomas Gleixner timer->it_signal = NULL; 9425cee9645SThomas Gleixner 9435cee9645SThomas Gleixner unlock_timer(timer, flags); 9445cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 9455cee9645SThomas Gleixner } 9465cee9645SThomas Gleixner 9475cee9645SThomas Gleixner /* 9485cee9645SThomas Gleixner * This is called by do_exit or de_thread, only when there are no more 9495cee9645SThomas Gleixner * references to the shared signal_struct. 9505cee9645SThomas Gleixner */ 9515cee9645SThomas Gleixner void exit_itimers(struct signal_struct *sig) 9525cee9645SThomas Gleixner { 9535cee9645SThomas Gleixner struct k_itimer *tmr; 9545cee9645SThomas Gleixner 9555cee9645SThomas Gleixner while (!list_empty(&sig->posix_timers)) { 9565cee9645SThomas Gleixner tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); 9575cee9645SThomas Gleixner itimer_delete(tmr); 9585cee9645SThomas Gleixner } 9595cee9645SThomas Gleixner } 9605cee9645SThomas Gleixner 9615cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, 9625cee9645SThomas Gleixner const struct timespec __user *, tp) 9635cee9645SThomas Gleixner { 964d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 9650fe6afe3SDeepa Dinamani struct timespec64 new_tp64; 9665cee9645SThomas Gleixner struct timespec new_tp; 9675cee9645SThomas Gleixner 9685cee9645SThomas Gleixner if (!kc || !kc->clock_set) 9695cee9645SThomas Gleixner return -EINVAL; 9705cee9645SThomas Gleixner 9715cee9645SThomas Gleixner if (copy_from_user(&new_tp, tp, sizeof (*tp))) 9725cee9645SThomas Gleixner return -EFAULT; 9730fe6afe3SDeepa Dinamani new_tp64 = timespec_to_timespec64(new_tp); 9745cee9645SThomas Gleixner 9750fe6afe3SDeepa Dinamani return kc->clock_set(which_clock, &new_tp64); 9765cee9645SThomas Gleixner } 9775cee9645SThomas Gleixner 9785cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, 9795cee9645SThomas Gleixner struct timespec __user *,tp) 9805cee9645SThomas Gleixner { 981d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 9823c9c12f4SDeepa Dinamani struct timespec64 kernel_tp64; 9835cee9645SThomas Gleixner struct timespec kernel_tp; 9845cee9645SThomas Gleixner int error; 9855cee9645SThomas Gleixner 9865cee9645SThomas Gleixner if (!kc) 9875cee9645SThomas Gleixner return -EINVAL; 9885cee9645SThomas Gleixner 9893c9c12f4SDeepa Dinamani error = kc->clock_get(which_clock, &kernel_tp64); 9903c9c12f4SDeepa Dinamani kernel_tp = timespec64_to_timespec(kernel_tp64); 9915cee9645SThomas Gleixner 9925cee9645SThomas Gleixner if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp))) 9935cee9645SThomas Gleixner error = -EFAULT; 9945cee9645SThomas Gleixner 9955cee9645SThomas Gleixner return error; 9965cee9645SThomas Gleixner } 9975cee9645SThomas Gleixner 9985cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, 9995cee9645SThomas Gleixner struct timex __user *, utx) 10005cee9645SThomas Gleixner { 1001d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10025cee9645SThomas Gleixner struct timex ktx; 10035cee9645SThomas Gleixner int err; 10045cee9645SThomas Gleixner 10055cee9645SThomas Gleixner if (!kc) 10065cee9645SThomas Gleixner return -EINVAL; 10075cee9645SThomas Gleixner if (!kc->clock_adj) 10085cee9645SThomas Gleixner return -EOPNOTSUPP; 10095cee9645SThomas Gleixner 10105cee9645SThomas Gleixner if (copy_from_user(&ktx, utx, sizeof(ktx))) 10115cee9645SThomas Gleixner return -EFAULT; 10125cee9645SThomas Gleixner 10135cee9645SThomas Gleixner err = kc->clock_adj(which_clock, &ktx); 10145cee9645SThomas Gleixner 10155cee9645SThomas Gleixner if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) 10165cee9645SThomas Gleixner return -EFAULT; 10175cee9645SThomas Gleixner 10185cee9645SThomas Gleixner return err; 10195cee9645SThomas Gleixner } 10205cee9645SThomas Gleixner 10213a4d44b6SAl Viro #ifdef CONFIG_COMPAT 10223a4d44b6SAl Viro 10233a4d44b6SAl Viro COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, 10243a4d44b6SAl Viro struct compat_timex __user *, utp) 10253a4d44b6SAl Viro { 10263a4d44b6SAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 10273a4d44b6SAl Viro struct timex ktx; 10283a4d44b6SAl Viro int err; 10293a4d44b6SAl Viro 10303a4d44b6SAl Viro if (!kc) 10313a4d44b6SAl Viro return -EINVAL; 10323a4d44b6SAl Viro if (!kc->clock_adj) 10333a4d44b6SAl Viro return -EOPNOTSUPP; 10343a4d44b6SAl Viro 10353a4d44b6SAl Viro err = compat_get_timex(&ktx, utp); 10363a4d44b6SAl Viro if (err) 10373a4d44b6SAl Viro return err; 10383a4d44b6SAl Viro 10393a4d44b6SAl Viro err = kc->clock_adj(which_clock, &ktx); 10403a4d44b6SAl Viro 10413a4d44b6SAl Viro if (err >= 0) 10423a4d44b6SAl Viro err = compat_put_timex(utp, &ktx); 10433a4d44b6SAl Viro 10443a4d44b6SAl Viro return err; 10453a4d44b6SAl Viro } 10463a4d44b6SAl Viro #endif 10473a4d44b6SAl Viro 10485cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, 10495cee9645SThomas Gleixner struct timespec __user *, tp) 10505cee9645SThomas Gleixner { 1051d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1052d2e3e0caSDeepa Dinamani struct timespec64 rtn_tp64; 10535cee9645SThomas Gleixner struct timespec rtn_tp; 10545cee9645SThomas Gleixner int error; 10555cee9645SThomas Gleixner 10565cee9645SThomas Gleixner if (!kc) 10575cee9645SThomas Gleixner return -EINVAL; 10585cee9645SThomas Gleixner 1059d2e3e0caSDeepa Dinamani error = kc->clock_getres(which_clock, &rtn_tp64); 1060d2e3e0caSDeepa Dinamani rtn_tp = timespec64_to_timespec(rtn_tp64); 10615cee9645SThomas Gleixner 10625cee9645SThomas Gleixner if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) 10635cee9645SThomas Gleixner error = -EFAULT; 10645cee9645SThomas Gleixner 10655cee9645SThomas Gleixner return error; 10665cee9645SThomas Gleixner } 10675cee9645SThomas Gleixner 10685cee9645SThomas Gleixner /* 10695cee9645SThomas Gleixner * nanosleep for monotonic and realtime clocks 10705cee9645SThomas Gleixner */ 10715cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags, 107299e6c0e6SAl Viro struct timespec64 *tsave) 10735cee9645SThomas Gleixner { 1074192a82f9SAl Viro return hrtimer_nanosleep(tsave, flags & TIMER_ABSTIME ? 10755cee9645SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL, 10765cee9645SThomas Gleixner which_clock); 10775cee9645SThomas Gleixner } 10785cee9645SThomas Gleixner 10795cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, 10805cee9645SThomas Gleixner const struct timespec __user *, rqtp, 10815cee9645SThomas Gleixner struct timespec __user *, rmtp) 10825cee9645SThomas Gleixner { 1083d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1084ad196384SDeepa Dinamani struct timespec64 t64; 10855cee9645SThomas Gleixner struct timespec t; 10865cee9645SThomas Gleixner 10875cee9645SThomas Gleixner if (!kc) 10885cee9645SThomas Gleixner return -EINVAL; 10895cee9645SThomas Gleixner if (!kc->nsleep) 10905cee9645SThomas Gleixner return -ENANOSLEEP_NOTSUP; 10915cee9645SThomas Gleixner 10925cee9645SThomas Gleixner if (copy_from_user(&t, rqtp, sizeof (struct timespec))) 10935cee9645SThomas Gleixner return -EFAULT; 10945cee9645SThomas Gleixner 1095ad196384SDeepa Dinamani t64 = timespec_to_timespec64(t); 1096ad196384SDeepa Dinamani if (!timespec64_valid(&t64)) 10975cee9645SThomas Gleixner return -EINVAL; 109899e6c0e6SAl Viro if (flags & TIMER_ABSTIME) 109999e6c0e6SAl Viro rmtp = NULL; 1100edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; 110199e6c0e6SAl Viro current->restart_block.nanosleep.rmtp = rmtp; 11025cee9645SThomas Gleixner 110399e6c0e6SAl Viro return kc->nsleep(which_clock, flags, &t64); 11045cee9645SThomas Gleixner } 11055cee9645SThomas Gleixner 1106edbeda46SAl Viro #ifdef CONFIG_COMPAT 1107edbeda46SAl Viro COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags, 1108edbeda46SAl Viro struct compat_timespec __user *, rqtp, 1109edbeda46SAl Viro struct compat_timespec __user *, rmtp) 11105cee9645SThomas Gleixner { 1111d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1112edbeda46SAl Viro struct timespec64 t64; 1113edbeda46SAl Viro struct timespec t; 11145cee9645SThomas Gleixner 1115edbeda46SAl Viro if (!kc) 11165cee9645SThomas Gleixner return -EINVAL; 1117edbeda46SAl Viro if (!kc->nsleep) 1118edbeda46SAl Viro return -ENANOSLEEP_NOTSUP; 11195cee9645SThomas Gleixner 1120edbeda46SAl Viro if (compat_get_timespec(&t, rqtp)) 1121edbeda46SAl Viro return -EFAULT; 1122edbeda46SAl Viro 1123edbeda46SAl Viro t64 = timespec_to_timespec64(t); 1124edbeda46SAl Viro if (!timespec64_valid(&t64)) 1125edbeda46SAl Viro return -EINVAL; 1126edbeda46SAl Viro if (flags & TIMER_ABSTIME) 1127edbeda46SAl Viro rmtp = NULL; 1128edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; 1129edbeda46SAl Viro current->restart_block.nanosleep.compat_rmtp = rmtp; 1130edbeda46SAl Viro 1131edbeda46SAl Viro return kc->nsleep(which_clock, flags, &t64); 11325cee9645SThomas Gleixner } 1133edbeda46SAl Viro #endif 11346631fa12SThomas Gleixner 11356631fa12SThomas Gleixner static const struct k_clock clock_realtime = { 11366631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 11376631fa12SThomas Gleixner .clock_get = posix_clock_realtime_get, 11386631fa12SThomas Gleixner .clock_set = posix_clock_realtime_set, 11396631fa12SThomas Gleixner .clock_adj = posix_clock_realtime_adj, 11406631fa12SThomas Gleixner .nsleep = common_nsleep, 11416631fa12SThomas Gleixner .timer_create = common_timer_create, 11426631fa12SThomas Gleixner .timer_set = common_timer_set, 11436631fa12SThomas Gleixner .timer_get = common_timer_get, 11446631fa12SThomas Gleixner .timer_del = common_timer_del, 1145f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 114691d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 114791d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1148eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1149eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 11506631fa12SThomas Gleixner }; 11516631fa12SThomas Gleixner 11526631fa12SThomas Gleixner static const struct k_clock clock_monotonic = { 11536631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 11546631fa12SThomas Gleixner .clock_get = posix_ktime_get_ts, 11556631fa12SThomas Gleixner .nsleep = common_nsleep, 11566631fa12SThomas Gleixner .timer_create = common_timer_create, 11576631fa12SThomas Gleixner .timer_set = common_timer_set, 11586631fa12SThomas Gleixner .timer_get = common_timer_get, 11596631fa12SThomas Gleixner .timer_del = common_timer_del, 1160f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 116191d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 116291d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1163eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1164eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 11656631fa12SThomas Gleixner }; 11666631fa12SThomas Gleixner 11676631fa12SThomas Gleixner static const struct k_clock clock_monotonic_raw = { 11686631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 11696631fa12SThomas Gleixner .clock_get = posix_get_monotonic_raw, 11706631fa12SThomas Gleixner }; 11716631fa12SThomas Gleixner 11726631fa12SThomas Gleixner static const struct k_clock clock_realtime_coarse = { 11736631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 11746631fa12SThomas Gleixner .clock_get = posix_get_realtime_coarse, 11756631fa12SThomas Gleixner }; 11766631fa12SThomas Gleixner 11776631fa12SThomas Gleixner static const struct k_clock clock_monotonic_coarse = { 11786631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 11796631fa12SThomas Gleixner .clock_get = posix_get_monotonic_coarse, 11806631fa12SThomas Gleixner }; 11816631fa12SThomas Gleixner 11826631fa12SThomas Gleixner static const struct k_clock clock_tai = { 11836631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 11846631fa12SThomas Gleixner .clock_get = posix_get_tai, 11856631fa12SThomas Gleixner .nsleep = common_nsleep, 11866631fa12SThomas Gleixner .timer_create = common_timer_create, 11876631fa12SThomas Gleixner .timer_set = common_timer_set, 11886631fa12SThomas Gleixner .timer_get = common_timer_get, 11896631fa12SThomas Gleixner .timer_del = common_timer_del, 1190f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 119191d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 119291d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1193eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1194eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 11956631fa12SThomas Gleixner }; 11966631fa12SThomas Gleixner 11976631fa12SThomas Gleixner static const struct k_clock clock_boottime = { 11986631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 11996631fa12SThomas Gleixner .clock_get = posix_get_boottime, 12006631fa12SThomas Gleixner .nsleep = common_nsleep, 12016631fa12SThomas Gleixner .timer_create = common_timer_create, 12026631fa12SThomas Gleixner .timer_set = common_timer_set, 12036631fa12SThomas Gleixner .timer_get = common_timer_get, 12046631fa12SThomas Gleixner .timer_del = common_timer_del, 1205f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 120691d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 120791d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1208eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1209eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 12106631fa12SThomas Gleixner }; 12116631fa12SThomas Gleixner 12126631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[] = { 12136631fa12SThomas Gleixner [CLOCK_REALTIME] = &clock_realtime, 12146631fa12SThomas Gleixner [CLOCK_MONOTONIC] = &clock_monotonic, 12156631fa12SThomas Gleixner [CLOCK_PROCESS_CPUTIME_ID] = &clock_process, 12166631fa12SThomas Gleixner [CLOCK_THREAD_CPUTIME_ID] = &clock_thread, 12176631fa12SThomas Gleixner [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw, 12186631fa12SThomas Gleixner [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse, 12196631fa12SThomas Gleixner [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse, 12206631fa12SThomas Gleixner [CLOCK_BOOTTIME] = &clock_boottime, 12216631fa12SThomas Gleixner [CLOCK_REALTIME_ALARM] = &alarm_clock, 12226631fa12SThomas Gleixner [CLOCK_BOOTTIME_ALARM] = &alarm_clock, 12236631fa12SThomas Gleixner [CLOCK_TAI] = &clock_tai, 12246631fa12SThomas Gleixner }; 12256631fa12SThomas Gleixner 12266631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id) 12276631fa12SThomas Gleixner { 12286631fa12SThomas Gleixner if (id < 0) 12296631fa12SThomas Gleixner return (id & CLOCKFD_MASK) == CLOCKFD ? 12306631fa12SThomas Gleixner &clock_posix_dynamic : &clock_posix_cpu; 12316631fa12SThomas Gleixner 12326631fa12SThomas Gleixner if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id]) 12336631fa12SThomas Gleixner return NULL; 12346631fa12SThomas Gleixner return posix_clocks[id]; 12356631fa12SThomas Gleixner } 1236