135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0+ 25cee9645SThomas Gleixner /* 35cee9645SThomas Gleixner * 2002-10-15 Posix Clocks & timers 45cee9645SThomas Gleixner * by George Anzinger george@mvista.com 55cee9645SThomas Gleixner * Copyright (C) 2002 2003 by MontaVista Software. 65cee9645SThomas Gleixner * 75cee9645SThomas Gleixner * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug. 85cee9645SThomas Gleixner * Copyright (C) 2004 Boris Hu 95cee9645SThomas Gleixner * 100141de74SThomas Gleixner * These are all the functions necessary to implement POSIX clocks & timers 115cee9645SThomas Gleixner */ 125cee9645SThomas Gleixner #include <linux/mm.h> 135cee9645SThomas Gleixner #include <linux/interrupt.h> 145cee9645SThomas Gleixner #include <linux/slab.h> 155cee9645SThomas Gleixner #include <linux/time.h> 165cee9645SThomas Gleixner #include <linux/mutex.h> 1761855b6bSIngo Molnar #include <linux/sched/task.h> 185cee9645SThomas Gleixner 197c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 205cee9645SThomas Gleixner #include <linux/list.h> 215cee9645SThomas Gleixner #include <linux/init.h> 225cee9645SThomas Gleixner #include <linux/compiler.h> 235cee9645SThomas Gleixner #include <linux/hash.h> 245cee9645SThomas Gleixner #include <linux/posix-clock.h> 255cee9645SThomas Gleixner #include <linux/posix-timers.h> 265cee9645SThomas Gleixner #include <linux/syscalls.h> 275cee9645SThomas Gleixner #include <linux/wait.h> 285cee9645SThomas Gleixner #include <linux/workqueue.h> 295cee9645SThomas Gleixner #include <linux/export.h> 305cee9645SThomas Gleixner #include <linux/hashtable.h> 31edbeda46SAl Viro #include <linux/compat.h> 3219b558dbSThomas Gleixner #include <linux/nospec.h> 335cee9645SThomas Gleixner 348b094cd0SThomas Gleixner #include "timekeeping.h" 35bab0aae9SThomas Gleixner #include "posix-timers.h" 368b094cd0SThomas Gleixner 375cee9645SThomas Gleixner /* 385cee9645SThomas Gleixner * Management arrays for POSIX timers. Timers are now kept in static hash table 395cee9645SThomas Gleixner * with 512 entries. 405cee9645SThomas Gleixner * Timer ids are allocated by local routine, which selects proper hash head by 415cee9645SThomas Gleixner * key, constructed from current->signal address and per signal struct counter. 425cee9645SThomas Gleixner * This keeps timer ids unique per process, but now they can intersect between 435cee9645SThomas Gleixner * processes. 445cee9645SThomas Gleixner */ 455cee9645SThomas Gleixner 465cee9645SThomas Gleixner /* 475cee9645SThomas Gleixner * Lets keep our timers in a slab cache :-) 485cee9645SThomas Gleixner */ 495cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache; 505cee9645SThomas Gleixner 515cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9); 525cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock); 535cee9645SThomas Gleixner 546631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[]; 556631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id); 5667edab48SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic; 576631fa12SThomas Gleixner 585cee9645SThomas Gleixner /* 595cee9645SThomas Gleixner * we assume that the new SIGEV_THREAD_ID shares no bits with the other 605cee9645SThomas Gleixner * SIGEV values. Here we put out an error if this assumption fails. 615cee9645SThomas Gleixner */ 625cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ 635cee9645SThomas Gleixner ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD)) 645cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!" 655cee9645SThomas Gleixner #endif 665cee9645SThomas Gleixner 675cee9645SThomas Gleixner /* 685cee9645SThomas Gleixner * The timer ID is turned into a timer address by idr_find(). 695cee9645SThomas Gleixner * Verifying a valid ID consists of: 705cee9645SThomas Gleixner * 715cee9645SThomas Gleixner * a) checking that idr_find() returns other than -1. 725cee9645SThomas Gleixner * b) checking that the timer id matches the one in the timer itself. 735cee9645SThomas Gleixner * c) that the timer owner is in the callers thread group. 745cee9645SThomas Gleixner */ 755cee9645SThomas Gleixner 765cee9645SThomas Gleixner /* 775cee9645SThomas Gleixner * CLOCKs: The POSIX standard calls for a couple of clocks and allows us 785cee9645SThomas Gleixner * to implement others. This structure defines the various 795cee9645SThomas Gleixner * clocks. 805cee9645SThomas Gleixner * 815cee9645SThomas Gleixner * RESOLUTION: Clock resolution is used to round up timer and interval 825cee9645SThomas Gleixner * times, NOT to report clock times, which are reported with as 835cee9645SThomas Gleixner * much resolution as the system can muster. In some cases this 845cee9645SThomas Gleixner * resolution may depend on the underlying clock hardware and 855cee9645SThomas Gleixner * may not be quantifiable until run time, and only then is the 865cee9645SThomas Gleixner * necessary code is written. The standard says we should say 875cee9645SThomas Gleixner * something about this issue in the documentation... 885cee9645SThomas Gleixner * 895cee9645SThomas Gleixner * FUNCTIONS: The CLOCKs structure defines possible functions to 905cee9645SThomas Gleixner * handle various clock functions. 915cee9645SThomas Gleixner * 925cee9645SThomas Gleixner * The standard POSIX timer management code assumes the 935cee9645SThomas Gleixner * following: 1.) The k_itimer struct (sched.h) is used for 945cee9645SThomas Gleixner * the timer. 2.) The list, it_lock, it_clock, it_id and 955cee9645SThomas Gleixner * it_pid fields are not modified by timer code. 965cee9645SThomas Gleixner * 975cee9645SThomas Gleixner * Permissions: It is assumed that the clock_settime() function defined 985cee9645SThomas Gleixner * for each clock will take care of permission checks. Some 995cee9645SThomas Gleixner * clocks may be set able by any user (i.e. local process 1005cee9645SThomas Gleixner * clocks) others not. Currently the only set able clock we 1015cee9645SThomas Gleixner * have is CLOCK_REALTIME and its high res counter part, both of 1025cee9645SThomas Gleixner * which we beg off on and pass to do_sys_settimeofday(). 1035cee9645SThomas Gleixner */ 1045cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); 1055cee9645SThomas Gleixner 1065cee9645SThomas Gleixner #define lock_timer(tid, flags) \ 1075cee9645SThomas Gleixner ({ struct k_itimer *__timr; \ 1085cee9645SThomas Gleixner __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \ 1095cee9645SThomas Gleixner __timr; \ 1105cee9645SThomas Gleixner }) 1115cee9645SThomas Gleixner 1125cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr) 1135cee9645SThomas Gleixner { 1145cee9645SThomas Gleixner return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable)); 1155cee9645SThomas Gleixner } 1165cee9645SThomas Gleixner 1175cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head, 1185cee9645SThomas Gleixner struct signal_struct *sig, 1195cee9645SThomas Gleixner timer_t id) 1205cee9645SThomas Gleixner { 1215cee9645SThomas Gleixner struct k_itimer *timer; 1225cee9645SThomas Gleixner 1235cee9645SThomas Gleixner hlist_for_each_entry_rcu(timer, head, t_hash) { 1245cee9645SThomas Gleixner if ((timer->it_signal == sig) && (timer->it_id == id)) 1255cee9645SThomas Gleixner return timer; 1265cee9645SThomas Gleixner } 1275cee9645SThomas Gleixner return NULL; 1285cee9645SThomas Gleixner } 1295cee9645SThomas Gleixner 1305cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id) 1315cee9645SThomas Gleixner { 1325cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1335cee9645SThomas Gleixner struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)]; 1345cee9645SThomas Gleixner 1355cee9645SThomas Gleixner return __posix_timers_find(head, sig, id); 1365cee9645SThomas Gleixner } 1375cee9645SThomas Gleixner 1385cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer) 1395cee9645SThomas Gleixner { 1405cee9645SThomas Gleixner struct signal_struct *sig = current->signal; 1415cee9645SThomas Gleixner int first_free_id = sig->posix_timer_id; 1425cee9645SThomas Gleixner struct hlist_head *head; 1435cee9645SThomas Gleixner int ret = -ENOENT; 1445cee9645SThomas Gleixner 1455cee9645SThomas Gleixner do { 1465cee9645SThomas Gleixner spin_lock(&hash_lock); 1475cee9645SThomas Gleixner head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; 1485cee9645SThomas Gleixner if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { 1495cee9645SThomas Gleixner hlist_add_head_rcu(&timer->t_hash, head); 1505cee9645SThomas Gleixner ret = sig->posix_timer_id; 1515cee9645SThomas Gleixner } 1525cee9645SThomas Gleixner if (++sig->posix_timer_id < 0) 1535cee9645SThomas Gleixner sig->posix_timer_id = 0; 1545cee9645SThomas Gleixner if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT)) 1555cee9645SThomas Gleixner /* Loop over all possible ids completed */ 1565cee9645SThomas Gleixner ret = -EAGAIN; 1575cee9645SThomas Gleixner spin_unlock(&hash_lock); 1585cee9645SThomas Gleixner } while (ret == -ENOENT); 1595cee9645SThomas Gleixner return ret; 1605cee9645SThomas Gleixner } 1615cee9645SThomas Gleixner 1625cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) 1635cee9645SThomas Gleixner { 1645cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, flags); 1655cee9645SThomas Gleixner } 1665cee9645SThomas Gleixner 1675cee9645SThomas Gleixner /* Get clock_realtime */ 1683c9c12f4SDeepa Dinamani static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp) 1695cee9645SThomas Gleixner { 1703c9c12f4SDeepa Dinamani ktime_get_real_ts64(tp); 1715cee9645SThomas Gleixner return 0; 1725cee9645SThomas Gleixner } 1735cee9645SThomas Gleixner 1745cee9645SThomas Gleixner /* Set clock_realtime */ 1755cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock, 1760fe6afe3SDeepa Dinamani const struct timespec64 *tp) 1775cee9645SThomas Gleixner { 1780fe6afe3SDeepa Dinamani return do_sys_settimeofday64(tp, NULL); 1795cee9645SThomas Gleixner } 1805cee9645SThomas Gleixner 1815cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock, 182ead25417SDeepa Dinamani struct __kernel_timex *t) 1835cee9645SThomas Gleixner { 1845cee9645SThomas Gleixner return do_adjtimex(t); 1855cee9645SThomas Gleixner } 1865cee9645SThomas Gleixner 1875cee9645SThomas Gleixner /* 1885cee9645SThomas Gleixner * Get monotonic time for posix timers 1895cee9645SThomas Gleixner */ 1903c9c12f4SDeepa Dinamani static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) 1915cee9645SThomas Gleixner { 1923c9c12f4SDeepa Dinamani ktime_get_ts64(tp); 1935cee9645SThomas Gleixner return 0; 1945cee9645SThomas Gleixner } 1955cee9645SThomas Gleixner 1965cee9645SThomas Gleixner /* 1975cee9645SThomas Gleixner * Get monotonic-raw time for posix timers 1985cee9645SThomas Gleixner */ 1993c9c12f4SDeepa Dinamani static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) 2005cee9645SThomas Gleixner { 20158a10456SArnd Bergmann ktime_get_raw_ts64(tp); 2025cee9645SThomas Gleixner return 0; 2035cee9645SThomas Gleixner } 2045cee9645SThomas Gleixner 2055cee9645SThomas Gleixner 2063c9c12f4SDeepa Dinamani static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp) 2075cee9645SThomas Gleixner { 20858a10456SArnd Bergmann ktime_get_coarse_real_ts64(tp); 2095cee9645SThomas Gleixner return 0; 2105cee9645SThomas Gleixner } 2115cee9645SThomas Gleixner 2125cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock, 2133c9c12f4SDeepa Dinamani struct timespec64 *tp) 2145cee9645SThomas Gleixner { 21558a10456SArnd Bergmann ktime_get_coarse_ts64(tp); 2165cee9645SThomas Gleixner return 0; 2175cee9645SThomas Gleixner } 2185cee9645SThomas Gleixner 219d2e3e0caSDeepa Dinamani static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp) 2205cee9645SThomas Gleixner { 221d2e3e0caSDeepa Dinamani *tp = ktime_to_timespec64(KTIME_LOW_RES); 2225cee9645SThomas Gleixner return 0; 2235cee9645SThomas Gleixner } 2245cee9645SThomas Gleixner 225a3ed0e43SThomas Gleixner static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp) 2265cee9645SThomas Gleixner { 22758a10456SArnd Bergmann ktime_get_boottime_ts64(tp); 2285cee9645SThomas Gleixner return 0; 2295cee9645SThomas Gleixner } 2305cee9645SThomas Gleixner 231a3ed0e43SThomas Gleixner static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp) 23272199320SThomas Gleixner { 23358a10456SArnd Bergmann ktime_get_clocktai_ts64(tp); 23472199320SThomas Gleixner return 0; 23572199320SThomas Gleixner } 23672199320SThomas Gleixner 237d2e3e0caSDeepa Dinamani static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp) 238056a3cacSThomas Gleixner { 239056a3cacSThomas Gleixner tp->tv_sec = 0; 240056a3cacSThomas Gleixner tp->tv_nsec = hrtimer_resolution; 241056a3cacSThomas Gleixner return 0; 242056a3cacSThomas Gleixner } 243056a3cacSThomas Gleixner 244d3ba5a9aSChristoph Hellwig /* 245d3ba5a9aSChristoph Hellwig * Initialize everything, well, just everything in Posix clocks/timers ;) 246d3ba5a9aSChristoph Hellwig */ 247d3ba5a9aSChristoph Hellwig static __init int init_posix_timers(void) 248d3ba5a9aSChristoph Hellwig { 2495cee9645SThomas Gleixner posix_timers_cache = kmem_cache_create("posix_timers_cache", 2505cee9645SThomas Gleixner sizeof (struct k_itimer), 0, SLAB_PANIC, 2515cee9645SThomas Gleixner NULL); 2525cee9645SThomas Gleixner return 0; 2535cee9645SThomas Gleixner } 2545cee9645SThomas Gleixner __initcall(init_posix_timers); 2555cee9645SThomas Gleixner 25678c9c4dfSThomas Gleixner /* 25778c9c4dfSThomas Gleixner * The siginfo si_overrun field and the return value of timer_getoverrun(2) 25878c9c4dfSThomas Gleixner * are of type int. Clamp the overrun value to INT_MAX 25978c9c4dfSThomas Gleixner */ 26078c9c4dfSThomas Gleixner static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval) 26178c9c4dfSThomas Gleixner { 26278c9c4dfSThomas Gleixner s64 sum = timr->it_overrun_last + (s64)baseval; 26378c9c4dfSThomas Gleixner 26478c9c4dfSThomas Gleixner return sum > (s64)INT_MAX ? INT_MAX : (int)sum; 26578c9c4dfSThomas Gleixner } 26678c9c4dfSThomas Gleixner 267f37fb0aaSThomas Gleixner static void common_hrtimer_rearm(struct k_itimer *timr) 2685cee9645SThomas Gleixner { 2695cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 2705cee9645SThomas Gleixner 27178c9c4dfSThomas Gleixner timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(), 27280105cd0SThomas Gleixner timr->it_interval); 2735cee9645SThomas Gleixner hrtimer_restart(timer); 2745cee9645SThomas Gleixner } 2755cee9645SThomas Gleixner 2765cee9645SThomas Gleixner /* 2775cee9645SThomas Gleixner * This function is exported for use by the signal deliver code. It is 2785cee9645SThomas Gleixner * called just prior to the info block being released and passes that 2795cee9645SThomas Gleixner * block to us. It's function is to update the overrun entry AND to 2805cee9645SThomas Gleixner * restart the timer. It should only be called if the timer is to be 2815cee9645SThomas Gleixner * restarted (i.e. we have flagged this in the sys_private entry of the 2825cee9645SThomas Gleixner * info block). 2835cee9645SThomas Gleixner * 2845cee9645SThomas Gleixner * To protect against the timer going away while the interrupt is queued, 2855cee9645SThomas Gleixner * we require that the it_requeue_pending flag be set. 2865cee9645SThomas Gleixner */ 287ae7795bcSEric W. Biederman void posixtimer_rearm(struct kernel_siginfo *info) 2885cee9645SThomas Gleixner { 2895cee9645SThomas Gleixner struct k_itimer *timr; 2905cee9645SThomas Gleixner unsigned long flags; 2915cee9645SThomas Gleixner 2925cee9645SThomas Gleixner timr = lock_timer(info->si_tid, &flags); 293af888d67SThomas Gleixner if (!timr) 294af888d67SThomas Gleixner return; 2955cee9645SThomas Gleixner 2960e334db6SThomas Gleixner if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) { 297f37fb0aaSThomas Gleixner timr->kclock->timer_rearm(timr); 2985cee9645SThomas Gleixner 29921e55c1fSThomas Gleixner timr->it_active = 1; 300af888d67SThomas Gleixner timr->it_overrun_last = timr->it_overrun; 30178c9c4dfSThomas Gleixner timr->it_overrun = -1LL; 302af888d67SThomas Gleixner ++timr->it_requeue_pending; 303af888d67SThomas Gleixner 30478c9c4dfSThomas Gleixner info->si_overrun = timer_overrun_to_int(timr, info->si_overrun); 3055cee9645SThomas Gleixner } 3065cee9645SThomas Gleixner 3075cee9645SThomas Gleixner unlock_timer(timr, flags); 3085cee9645SThomas Gleixner } 3095cee9645SThomas Gleixner 3105cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private) 3115cee9645SThomas Gleixner { 31224122c7fSEric W. Biederman enum pid_type type; 31324122c7fSEric W. Biederman int ret = -1; 3145cee9645SThomas Gleixner /* 3155cee9645SThomas Gleixner * FIXME: if ->sigq is queued we can race with 31696fe3b07SThomas Gleixner * dequeue_signal()->posixtimer_rearm(). 3175cee9645SThomas Gleixner * 3185cee9645SThomas Gleixner * If dequeue_signal() sees the "right" value of 31996fe3b07SThomas Gleixner * si_sys_private it calls posixtimer_rearm(). 3205cee9645SThomas Gleixner * We re-queue ->sigq and drop ->it_lock(). 32196fe3b07SThomas Gleixner * posixtimer_rearm() locks the timer 3225cee9645SThomas Gleixner * and re-schedules it while ->sigq is pending. 3235cee9645SThomas Gleixner * Not really bad, but not that we want. 3245cee9645SThomas Gleixner */ 3255cee9645SThomas Gleixner timr->sigq->info.si_sys_private = si_private; 3265cee9645SThomas Gleixner 32724122c7fSEric W. Biederman type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID; 32824122c7fSEric W. Biederman ret = send_sigqueue(timr->sigq, timr->it_pid, type); 3295cee9645SThomas Gleixner /* If we failed to send the signal the timer stops. */ 3305cee9645SThomas Gleixner return ret > 0; 3315cee9645SThomas Gleixner } 3325cee9645SThomas Gleixner 3335cee9645SThomas Gleixner /* 3345cee9645SThomas Gleixner * This function gets called when a POSIX.1b interval timer expires. It 3355cee9645SThomas Gleixner * is used as a callback from the kernel internal timer. The 3365cee9645SThomas Gleixner * run_timer_list code ALWAYS calls with interrupts on. 3375cee9645SThomas Gleixner 3385cee9645SThomas Gleixner * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. 3395cee9645SThomas Gleixner */ 3405cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) 3415cee9645SThomas Gleixner { 3425cee9645SThomas Gleixner struct k_itimer *timr; 3435cee9645SThomas Gleixner unsigned long flags; 3445cee9645SThomas Gleixner int si_private = 0; 3455cee9645SThomas Gleixner enum hrtimer_restart ret = HRTIMER_NORESTART; 3465cee9645SThomas Gleixner 3475cee9645SThomas Gleixner timr = container_of(timer, struct k_itimer, it.real.timer); 3485cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, flags); 3495cee9645SThomas Gleixner 35021e55c1fSThomas Gleixner timr->it_active = 0; 35180105cd0SThomas Gleixner if (timr->it_interval != 0) 3525cee9645SThomas Gleixner si_private = ++timr->it_requeue_pending; 3535cee9645SThomas Gleixner 3545cee9645SThomas Gleixner if (posix_timer_event(timr, si_private)) { 3555cee9645SThomas Gleixner /* 3565cee9645SThomas Gleixner * signal was not sent because of sig_ignor 3575cee9645SThomas Gleixner * we will not get a call back to restart it AND 3585cee9645SThomas Gleixner * it should be restarted. 3595cee9645SThomas Gleixner */ 36080105cd0SThomas Gleixner if (timr->it_interval != 0) { 3615cee9645SThomas Gleixner ktime_t now = hrtimer_cb_get_time(timer); 3625cee9645SThomas Gleixner 3635cee9645SThomas Gleixner /* 3645cee9645SThomas Gleixner * FIXME: What we really want, is to stop this 3655cee9645SThomas Gleixner * timer completely and restart it in case the 3665cee9645SThomas Gleixner * SIG_IGN is removed. This is a non trivial 3675cee9645SThomas Gleixner * change which involves sighand locking 3685cee9645SThomas Gleixner * (sigh !), which we don't want to do late in 3695cee9645SThomas Gleixner * the release cycle. 3705cee9645SThomas Gleixner * 3715cee9645SThomas Gleixner * For now we just let timers with an interval 3725cee9645SThomas Gleixner * less than a jiffie expire every jiffie to 3735cee9645SThomas Gleixner * avoid softirq starvation in case of SIG_IGN 3745cee9645SThomas Gleixner * and a very small interval, which would put 3755cee9645SThomas Gleixner * the timer right back on the softirq pending 3765cee9645SThomas Gleixner * list. By moving now ahead of time we trick 3775cee9645SThomas Gleixner * hrtimer_forward() to expire the timer 3785cee9645SThomas Gleixner * later, while we still maintain the overrun 3795cee9645SThomas Gleixner * accuracy, but have some inconsistency in 3805cee9645SThomas Gleixner * the timer_gettime() case. This is at least 3815cee9645SThomas Gleixner * better than a starved softirq. A more 3825cee9645SThomas Gleixner * complex fix which solves also another related 3835cee9645SThomas Gleixner * inconsistency is already in the pipeline. 3845cee9645SThomas Gleixner */ 3855cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 3865cee9645SThomas Gleixner { 3878b0e1953SThomas Gleixner ktime_t kj = NSEC_PER_SEC / HZ; 3885cee9645SThomas Gleixner 38980105cd0SThomas Gleixner if (timr->it_interval < kj) 3905cee9645SThomas Gleixner now = ktime_add(now, kj); 3915cee9645SThomas Gleixner } 3925cee9645SThomas Gleixner #endif 39378c9c4dfSThomas Gleixner timr->it_overrun += hrtimer_forward(timer, now, 39480105cd0SThomas Gleixner timr->it_interval); 3955cee9645SThomas Gleixner ret = HRTIMER_RESTART; 3965cee9645SThomas Gleixner ++timr->it_requeue_pending; 39721e55c1fSThomas Gleixner timr->it_active = 1; 3985cee9645SThomas Gleixner } 3995cee9645SThomas Gleixner } 4005cee9645SThomas Gleixner 4015cee9645SThomas Gleixner unlock_timer(timr, flags); 4025cee9645SThomas Gleixner return ret; 4035cee9645SThomas Gleixner } 4045cee9645SThomas Gleixner 4055cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event) 4065cee9645SThomas Gleixner { 4072118e1f5SEric W. Biederman struct pid *pid = task_tgid(current); 4082118e1f5SEric W. Biederman struct task_struct *rtn; 4095cee9645SThomas Gleixner 410cef31d9aSThomas Gleixner switch (event->sigev_notify) { 411cef31d9aSThomas Gleixner case SIGEV_SIGNAL | SIGEV_THREAD_ID: 4122118e1f5SEric W. Biederman pid = find_vpid(event->sigev_notify_thread_id); 4132118e1f5SEric W. Biederman rtn = pid_task(pid, PIDTYPE_PID); 414cef31d9aSThomas Gleixner if (!rtn || !same_thread_group(rtn, current)) 4155cee9645SThomas Gleixner return NULL; 416cef31d9aSThomas Gleixner /* FALLTHRU */ 417cef31d9aSThomas Gleixner case SIGEV_SIGNAL: 418cef31d9aSThomas Gleixner case SIGEV_THREAD: 419cef31d9aSThomas Gleixner if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX) 4205cee9645SThomas Gleixner return NULL; 421cef31d9aSThomas Gleixner /* FALLTHRU */ 422cef31d9aSThomas Gleixner case SIGEV_NONE: 4232118e1f5SEric W. Biederman return pid; 424cef31d9aSThomas Gleixner default: 425cef31d9aSThomas Gleixner return NULL; 426cef31d9aSThomas Gleixner } 4275cee9645SThomas Gleixner } 4285cee9645SThomas Gleixner 4295cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void) 4305cee9645SThomas Gleixner { 4315cee9645SThomas Gleixner struct k_itimer *tmr; 4325cee9645SThomas Gleixner tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); 4335cee9645SThomas Gleixner if (!tmr) 4345cee9645SThomas Gleixner return tmr; 4355cee9645SThomas Gleixner if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { 4365cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4375cee9645SThomas Gleixner return NULL; 4385cee9645SThomas Gleixner } 4393b10db2bSEric W. Biederman clear_siginfo(&tmr->sigq->info); 4405cee9645SThomas Gleixner return tmr; 4415cee9645SThomas Gleixner } 4425cee9645SThomas Gleixner 4435cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head) 4445cee9645SThomas Gleixner { 4455cee9645SThomas Gleixner struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu); 4465cee9645SThomas Gleixner 4475cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr); 4485cee9645SThomas Gleixner } 4495cee9645SThomas Gleixner 4505cee9645SThomas Gleixner #define IT_ID_SET 1 4515cee9645SThomas Gleixner #define IT_ID_NOT_SET 0 4525cee9645SThomas Gleixner static void release_posix_timer(struct k_itimer *tmr, int it_id_set) 4535cee9645SThomas Gleixner { 4545cee9645SThomas Gleixner if (it_id_set) { 4555cee9645SThomas Gleixner unsigned long flags; 4565cee9645SThomas Gleixner spin_lock_irqsave(&hash_lock, flags); 4575cee9645SThomas Gleixner hlist_del_rcu(&tmr->t_hash); 4585cee9645SThomas Gleixner spin_unlock_irqrestore(&hash_lock, flags); 4595cee9645SThomas Gleixner } 4605cee9645SThomas Gleixner put_pid(tmr->it_pid); 4615cee9645SThomas Gleixner sigqueue_free(tmr->sigq); 4625cee9645SThomas Gleixner call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 4635cee9645SThomas Gleixner } 4645cee9645SThomas Gleixner 4655cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer) 4665cee9645SThomas Gleixner { 4675cee9645SThomas Gleixner hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); 4685cee9645SThomas Gleixner return 0; 4695cee9645SThomas Gleixner } 4705cee9645SThomas Gleixner 4715cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */ 4722482097cSAl Viro static int do_timer_create(clockid_t which_clock, struct sigevent *event, 4732482097cSAl Viro timer_t __user *created_timer_id) 4745cee9645SThomas Gleixner { 475d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 4765cee9645SThomas Gleixner struct k_itimer *new_timer; 4775cee9645SThomas Gleixner int error, new_timer_id; 4785cee9645SThomas Gleixner int it_id_set = IT_ID_NOT_SET; 4795cee9645SThomas Gleixner 4805cee9645SThomas Gleixner if (!kc) 4815cee9645SThomas Gleixner return -EINVAL; 4825cee9645SThomas Gleixner if (!kc->timer_create) 4835cee9645SThomas Gleixner return -EOPNOTSUPP; 4845cee9645SThomas Gleixner 4855cee9645SThomas Gleixner new_timer = alloc_posix_timer(); 4865cee9645SThomas Gleixner if (unlikely(!new_timer)) 4875cee9645SThomas Gleixner return -EAGAIN; 4885cee9645SThomas Gleixner 4895cee9645SThomas Gleixner spin_lock_init(&new_timer->it_lock); 4905cee9645SThomas Gleixner new_timer_id = posix_timer_add(new_timer); 4915cee9645SThomas Gleixner if (new_timer_id < 0) { 4925cee9645SThomas Gleixner error = new_timer_id; 4935cee9645SThomas Gleixner goto out; 4945cee9645SThomas Gleixner } 4955cee9645SThomas Gleixner 4965cee9645SThomas Gleixner it_id_set = IT_ID_SET; 4975cee9645SThomas Gleixner new_timer->it_id = (timer_t) new_timer_id; 4985cee9645SThomas Gleixner new_timer->it_clock = which_clock; 499d97bb75dSThomas Gleixner new_timer->kclock = kc; 50078c9c4dfSThomas Gleixner new_timer->it_overrun = -1LL; 5015cee9645SThomas Gleixner 5022482097cSAl Viro if (event) { 5035cee9645SThomas Gleixner rcu_read_lock(); 5042482097cSAl Viro new_timer->it_pid = get_pid(good_sigevent(event)); 5055cee9645SThomas Gleixner rcu_read_unlock(); 5065cee9645SThomas Gleixner if (!new_timer->it_pid) { 5075cee9645SThomas Gleixner error = -EINVAL; 5085cee9645SThomas Gleixner goto out; 5095cee9645SThomas Gleixner } 5102482097cSAl Viro new_timer->it_sigev_notify = event->sigev_notify; 5112482097cSAl Viro new_timer->sigq->info.si_signo = event->sigev_signo; 5122482097cSAl Viro new_timer->sigq->info.si_value = event->sigev_value; 5135cee9645SThomas Gleixner } else { 5142482097cSAl Viro new_timer->it_sigev_notify = SIGEV_SIGNAL; 5152482097cSAl Viro new_timer->sigq->info.si_signo = SIGALRM; 5162482097cSAl Viro memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); 5172482097cSAl Viro new_timer->sigq->info.si_value.sival_int = new_timer->it_id; 5185cee9645SThomas Gleixner new_timer->it_pid = get_pid(task_tgid(current)); 5195cee9645SThomas Gleixner } 5205cee9645SThomas Gleixner 5215cee9645SThomas Gleixner new_timer->sigq->info.si_tid = new_timer->it_id; 5225cee9645SThomas Gleixner new_timer->sigq->info.si_code = SI_TIMER; 5235cee9645SThomas Gleixner 5245cee9645SThomas Gleixner if (copy_to_user(created_timer_id, 5255cee9645SThomas Gleixner &new_timer_id, sizeof (new_timer_id))) { 5265cee9645SThomas Gleixner error = -EFAULT; 5275cee9645SThomas Gleixner goto out; 5285cee9645SThomas Gleixner } 5295cee9645SThomas Gleixner 5305cee9645SThomas Gleixner error = kc->timer_create(new_timer); 5315cee9645SThomas Gleixner if (error) 5325cee9645SThomas Gleixner goto out; 5335cee9645SThomas Gleixner 5345cee9645SThomas Gleixner spin_lock_irq(¤t->sighand->siglock); 5355cee9645SThomas Gleixner new_timer->it_signal = current->signal; 5365cee9645SThomas Gleixner list_add(&new_timer->list, ¤t->signal->posix_timers); 5375cee9645SThomas Gleixner spin_unlock_irq(¤t->sighand->siglock); 5385cee9645SThomas Gleixner 5395cee9645SThomas Gleixner return 0; 5405cee9645SThomas Gleixner /* 5415cee9645SThomas Gleixner * In the case of the timer belonging to another task, after 5425cee9645SThomas Gleixner * the task is unlocked, the timer is owned by the other task 5435cee9645SThomas Gleixner * and may cease to exist at any time. Don't use or modify 5445cee9645SThomas Gleixner * new_timer after the unlock call. 5455cee9645SThomas Gleixner */ 5465cee9645SThomas Gleixner out: 5475cee9645SThomas Gleixner release_posix_timer(new_timer, it_id_set); 5485cee9645SThomas Gleixner return error; 5495cee9645SThomas Gleixner } 5505cee9645SThomas Gleixner 5512482097cSAl Viro SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, 5522482097cSAl Viro struct sigevent __user *, timer_event_spec, 5532482097cSAl Viro timer_t __user *, created_timer_id) 5542482097cSAl Viro { 5552482097cSAl Viro if (timer_event_spec) { 5562482097cSAl Viro sigevent_t event; 5572482097cSAl Viro 5582482097cSAl Viro if (copy_from_user(&event, timer_event_spec, sizeof (event))) 5592482097cSAl Viro return -EFAULT; 5602482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id); 5612482097cSAl Viro } 5622482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id); 5632482097cSAl Viro } 5642482097cSAl Viro 5652482097cSAl Viro #ifdef CONFIG_COMPAT 5662482097cSAl Viro COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, 5672482097cSAl Viro struct compat_sigevent __user *, timer_event_spec, 5682482097cSAl Viro timer_t __user *, created_timer_id) 5692482097cSAl Viro { 5702482097cSAl Viro if (timer_event_spec) { 5712482097cSAl Viro sigevent_t event; 5722482097cSAl Viro 5732482097cSAl Viro if (get_compat_sigevent(&event, timer_event_spec)) 5742482097cSAl Viro return -EFAULT; 5752482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id); 5762482097cSAl Viro } 5772482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id); 5782482097cSAl Viro } 5792482097cSAl Viro #endif 5802482097cSAl Viro 5815cee9645SThomas Gleixner /* 5825cee9645SThomas Gleixner * Locking issues: We need to protect the result of the id look up until 5835cee9645SThomas Gleixner * we get the timer locked down so it is not deleted under us. The 5845cee9645SThomas Gleixner * removal is done under the idr spinlock so we use that here to bridge 5855cee9645SThomas Gleixner * the find to the timer lock. To avoid a dead lock, the timer id MUST 5865cee9645SThomas Gleixner * be release with out holding the timer lock. 5875cee9645SThomas Gleixner */ 5885cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) 5895cee9645SThomas Gleixner { 5905cee9645SThomas Gleixner struct k_itimer *timr; 5915cee9645SThomas Gleixner 5925cee9645SThomas Gleixner /* 5935cee9645SThomas Gleixner * timer_t could be any type >= int and we want to make sure any 5945cee9645SThomas Gleixner * @timer_id outside positive int range fails lookup. 5955cee9645SThomas Gleixner */ 5965cee9645SThomas Gleixner if ((unsigned long long)timer_id > INT_MAX) 5975cee9645SThomas Gleixner return NULL; 5985cee9645SThomas Gleixner 5995cee9645SThomas Gleixner rcu_read_lock(); 6005cee9645SThomas Gleixner timr = posix_timer_by_id(timer_id); 6015cee9645SThomas Gleixner if (timr) { 6025cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, *flags); 6035cee9645SThomas Gleixner if (timr->it_signal == current->signal) { 6045cee9645SThomas Gleixner rcu_read_unlock(); 6055cee9645SThomas Gleixner return timr; 6065cee9645SThomas Gleixner } 6075cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, *flags); 6085cee9645SThomas Gleixner } 6095cee9645SThomas Gleixner rcu_read_unlock(); 6105cee9645SThomas Gleixner 6115cee9645SThomas Gleixner return NULL; 6125cee9645SThomas Gleixner } 6135cee9645SThomas Gleixner 61491d57baeSThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now) 61591d57baeSThomas Gleixner { 61691d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 61791d57baeSThomas Gleixner 61891d57baeSThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, now); 61991d57baeSThomas Gleixner } 62091d57baeSThomas Gleixner 6216fec64e1SThomas Gleixner static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now) 62291d57baeSThomas Gleixner { 62391d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 62491d57baeSThomas Gleixner 6256fec64e1SThomas Gleixner return hrtimer_forward(timer, now, timr->it_interval); 62691d57baeSThomas Gleixner } 62791d57baeSThomas Gleixner 6285cee9645SThomas Gleixner /* 6295cee9645SThomas Gleixner * Get the time remaining on a POSIX.1b interval timer. This function 6305cee9645SThomas Gleixner * is ALWAYS called with spin_lock_irq on the timer, thus it must not 6315cee9645SThomas Gleixner * mess with irq. 6325cee9645SThomas Gleixner * 6335cee9645SThomas Gleixner * We have a couple of messes to clean up here. First there is the case 6345cee9645SThomas Gleixner * of a timer that has a requeue pending. These timers should appear to 6355cee9645SThomas Gleixner * be in the timer list with an expiry as if we were to requeue them 6365cee9645SThomas Gleixner * now. 6375cee9645SThomas Gleixner * 6385cee9645SThomas Gleixner * The second issue is the SIGEV_NONE timer which may be active but is 6395cee9645SThomas Gleixner * not really ever put in the timer list (to save system resources). 6405cee9645SThomas Gleixner * This timer may be expired, and if so, we will do it here. Otherwise 6415cee9645SThomas Gleixner * it is the same as a requeue pending timer WRT to what we should 6425cee9645SThomas Gleixner * report. 6435cee9645SThomas Gleixner */ 644f2c45807SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) 6455cee9645SThomas Gleixner { 64691d57baeSThomas Gleixner const struct k_clock *kc = timr->kclock; 6475cee9645SThomas Gleixner ktime_t now, remaining, iv; 64891d57baeSThomas Gleixner struct timespec64 ts64; 64991d57baeSThomas Gleixner bool sig_none; 6505cee9645SThomas Gleixner 651cef31d9aSThomas Gleixner sig_none = timr->it_sigev_notify == SIGEV_NONE; 65280105cd0SThomas Gleixner iv = timr->it_interval; 6535cee9645SThomas Gleixner 6545cee9645SThomas Gleixner /* interval timer ? */ 65591d57baeSThomas Gleixner if (iv) { 6565f252b32SDeepa Dinamani cur_setting->it_interval = ktime_to_timespec64(iv); 65791d57baeSThomas Gleixner } else if (!timr->it_active) { 65891d57baeSThomas Gleixner /* 65991d57baeSThomas Gleixner * SIGEV_NONE oneshot timers are never queued. Check them 66091d57baeSThomas Gleixner * below. 66191d57baeSThomas Gleixner */ 66291d57baeSThomas Gleixner if (!sig_none) 6635cee9645SThomas Gleixner return; 66491d57baeSThomas Gleixner } 6655cee9645SThomas Gleixner 6665cee9645SThomas Gleixner /* 66791d57baeSThomas Gleixner * The timespec64 based conversion is suboptimal, but it's not 66891d57baeSThomas Gleixner * worth to implement yet another callback. 6695cee9645SThomas Gleixner */ 67091d57baeSThomas Gleixner kc->clock_get(timr->it_clock, &ts64); 67191d57baeSThomas Gleixner now = timespec64_to_ktime(ts64); 6725cee9645SThomas Gleixner 67391d57baeSThomas Gleixner /* 67491d57baeSThomas Gleixner * When a requeue is pending or this is a SIGEV_NONE timer move the 67591d57baeSThomas Gleixner * expiry time forward by intervals, so expiry is > now. 67691d57baeSThomas Gleixner */ 67791d57baeSThomas Gleixner if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) 67878c9c4dfSThomas Gleixner timr->it_overrun += kc->timer_forward(timr, now); 67991d57baeSThomas Gleixner 68091d57baeSThomas Gleixner remaining = kc->timer_remaining(timr, now); 6815cee9645SThomas Gleixner /* Return 0 only, when the timer is expired and not pending */ 6822456e855SThomas Gleixner if (remaining <= 0) { 6835cee9645SThomas Gleixner /* 6845cee9645SThomas Gleixner * A single shot SIGEV_NONE timer must return 0, when 6855cee9645SThomas Gleixner * it is expired ! 6865cee9645SThomas Gleixner */ 68791d57baeSThomas Gleixner if (!sig_none) 6885cee9645SThomas Gleixner cur_setting->it_value.tv_nsec = 1; 68991d57baeSThomas Gleixner } else { 6905f252b32SDeepa Dinamani cur_setting->it_value = ktime_to_timespec64(remaining); 6915cee9645SThomas Gleixner } 69291d57baeSThomas Gleixner } 6935cee9645SThomas Gleixner 6945cee9645SThomas Gleixner /* Get the time remaining on a POSIX.1b interval timer. */ 695b0dc1242SAl Viro static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting) 6965cee9645SThomas Gleixner { 6975cee9645SThomas Gleixner struct k_itimer *timr; 698d3ba5a9aSChristoph Hellwig const struct k_clock *kc; 6995cee9645SThomas Gleixner unsigned long flags; 7005cee9645SThomas Gleixner int ret = 0; 7015cee9645SThomas Gleixner 7025cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7035cee9645SThomas Gleixner if (!timr) 7045cee9645SThomas Gleixner return -EINVAL; 7055cee9645SThomas Gleixner 706b0dc1242SAl Viro memset(setting, 0, sizeof(*setting)); 707d97bb75dSThomas Gleixner kc = timr->kclock; 7085cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_get)) 7095cee9645SThomas Gleixner ret = -EINVAL; 7105cee9645SThomas Gleixner else 711b0dc1242SAl Viro kc->timer_get(timr, setting); 7125cee9645SThomas Gleixner 7135cee9645SThomas Gleixner unlock_timer(timr, flags); 7145cee9645SThomas Gleixner return ret; 7155cee9645SThomas Gleixner } 7165cee9645SThomas Gleixner 717b0dc1242SAl Viro /* Get the time remaining on a POSIX.1b interval timer. */ 718b0dc1242SAl Viro SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 7196ff84735SDeepa Dinamani struct __kernel_itimerspec __user *, setting) 720b0dc1242SAl Viro { 721725816e8SDeepa Dinamani struct itimerspec64 cur_setting; 722b0dc1242SAl Viro 723725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting); 724b0dc1242SAl Viro if (!ret) { 725725816e8SDeepa Dinamani if (put_itimerspec64(&cur_setting, setting)) 726b0dc1242SAl Viro ret = -EFAULT; 727b0dc1242SAl Viro } 728b0dc1242SAl Viro return ret; 729b0dc1242SAl Viro } 730b0dc1242SAl Viro 7316ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 7326ff84735SDeepa Dinamani 7338dabe724SArnd Bergmann SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id, 7349afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, setting) 735b0dc1242SAl Viro { 736725816e8SDeepa Dinamani struct itimerspec64 cur_setting; 737b0dc1242SAl Viro 738725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting); 739b0dc1242SAl Viro if (!ret) { 7409afc5eeeSArnd Bergmann if (put_old_itimerspec32(&cur_setting, setting)) 741b0dc1242SAl Viro ret = -EFAULT; 742b0dc1242SAl Viro } 743b0dc1242SAl Viro return ret; 744b0dc1242SAl Viro } 7456ff84735SDeepa Dinamani 746b0dc1242SAl Viro #endif 747b0dc1242SAl Viro 7485cee9645SThomas Gleixner /* 7495cee9645SThomas Gleixner * Get the number of overruns of a POSIX.1b interval timer. This is to 7505cee9645SThomas Gleixner * be the overrun of the timer last delivered. At the same time we are 7515cee9645SThomas Gleixner * accumulating overruns on the next timer. The overrun is frozen when 7525cee9645SThomas Gleixner * the signal is delivered, either at the notify time (if the info block 7535cee9645SThomas Gleixner * is not queued) or at the actual delivery time (as we are informed by 75496fe3b07SThomas Gleixner * the call back to posixtimer_rearm(). So all we need to do is 7555cee9645SThomas Gleixner * to pick up the frozen overrun. 7565cee9645SThomas Gleixner */ 7575cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) 7585cee9645SThomas Gleixner { 7595cee9645SThomas Gleixner struct k_itimer *timr; 7605cee9645SThomas Gleixner int overrun; 7615cee9645SThomas Gleixner unsigned long flags; 7625cee9645SThomas Gleixner 7635cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags); 7645cee9645SThomas Gleixner if (!timr) 7655cee9645SThomas Gleixner return -EINVAL; 7665cee9645SThomas Gleixner 76778c9c4dfSThomas Gleixner overrun = timer_overrun_to_int(timr, 0); 7685cee9645SThomas Gleixner unlock_timer(timr, flags); 7695cee9645SThomas Gleixner 7705cee9645SThomas Gleixner return overrun; 7715cee9645SThomas Gleixner } 7725cee9645SThomas Gleixner 773eae1c4aeSThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires, 774eae1c4aeSThomas Gleixner bool absolute, bool sigev_none) 7755cee9645SThomas Gleixner { 7765cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer; 7775cee9645SThomas Gleixner enum hrtimer_mode mode; 7785cee9645SThomas Gleixner 779eae1c4aeSThomas Gleixner mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; 78067edab48SThomas Gleixner /* 78167edab48SThomas Gleixner * Posix magic: Relative CLOCK_REALTIME timers are not affected by 78267edab48SThomas Gleixner * clock modifications, so they become CLOCK_MONOTONIC based under the 78367edab48SThomas Gleixner * hood. See hrtimer_init(). Update timr->kclock, so the generic 78467edab48SThomas Gleixner * functions which use timr->kclock->clock_get() work. 78567edab48SThomas Gleixner * 78667edab48SThomas Gleixner * Note: it_clock stays unmodified, because the next timer_set() might 78767edab48SThomas Gleixner * use ABSTIME, so it needs to switch back. 78867edab48SThomas Gleixner */ 78967edab48SThomas Gleixner if (timr->it_clock == CLOCK_REALTIME) 79067edab48SThomas Gleixner timr->kclock = absolute ? &clock_realtime : &clock_monotonic; 79167edab48SThomas Gleixner 792eae1c4aeSThomas Gleixner hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); 793eae1c4aeSThomas Gleixner timr->it.real.timer.function = posix_timer_fn; 794eae1c4aeSThomas Gleixner 795eae1c4aeSThomas Gleixner if (!absolute) 796eae1c4aeSThomas Gleixner expires = ktime_add_safe(expires, timer->base->get_time()); 797eae1c4aeSThomas Gleixner hrtimer_set_expires(timer, expires); 798eae1c4aeSThomas Gleixner 799eae1c4aeSThomas Gleixner if (!sigev_none) 800eae1c4aeSThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 801eae1c4aeSThomas Gleixner } 802eae1c4aeSThomas Gleixner 803eae1c4aeSThomas Gleixner static int common_hrtimer_try_to_cancel(struct k_itimer *timr) 804eae1c4aeSThomas Gleixner { 805eae1c4aeSThomas Gleixner return hrtimer_try_to_cancel(&timr->it.real.timer); 806eae1c4aeSThomas Gleixner } 807eae1c4aeSThomas Gleixner 8086945e5c2SThomas Gleixner static struct k_itimer *timer_wait_running(struct k_itimer *timer, 8096945e5c2SThomas Gleixner unsigned long *flags) 8106945e5c2SThomas Gleixner { 8116945e5c2SThomas Gleixner timer_t timer_id = READ_ONCE(timer->it_id); 8126945e5c2SThomas Gleixner 8136945e5c2SThomas Gleixner unlock_timer(timer, *flags); 8146945e5c2SThomas Gleixner cpu_relax(); 8156945e5c2SThomas Gleixner /* Relock the timer. It might be not longer hashed. */ 8166945e5c2SThomas Gleixner return lock_timer(timer_id, flags); 8176945e5c2SThomas Gleixner } 8186945e5c2SThomas Gleixner 819eae1c4aeSThomas Gleixner /* Set a POSIX.1b interval timer. */ 820f2c45807SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags, 821eae1c4aeSThomas Gleixner struct itimerspec64 *new_setting, 822eae1c4aeSThomas Gleixner struct itimerspec64 *old_setting) 823eae1c4aeSThomas Gleixner { 824eae1c4aeSThomas Gleixner const struct k_clock *kc = timr->kclock; 825eae1c4aeSThomas Gleixner bool sigev_none; 826eae1c4aeSThomas Gleixner ktime_t expires; 827eae1c4aeSThomas Gleixner 8285cee9645SThomas Gleixner if (old_setting) 8295cee9645SThomas Gleixner common_timer_get(timr, old_setting); 8305cee9645SThomas Gleixner 831eae1c4aeSThomas Gleixner /* Prevent rearming by clearing the interval */ 83280105cd0SThomas Gleixner timr->it_interval = 0; 8335cee9645SThomas Gleixner /* 834eae1c4aeSThomas Gleixner * Careful here. On SMP systems the timer expiry function could be 835eae1c4aeSThomas Gleixner * active and spinning on timr->it_lock. 8365cee9645SThomas Gleixner */ 837eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timr) < 0) 8385cee9645SThomas Gleixner return TIMER_RETRY; 8395cee9645SThomas Gleixner 84021e55c1fSThomas Gleixner timr->it_active = 0; 8415cee9645SThomas Gleixner timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 8425cee9645SThomas Gleixner ~REQUEUE_PENDING; 8435cee9645SThomas Gleixner timr->it_overrun_last = 0; 8445cee9645SThomas Gleixner 845eae1c4aeSThomas Gleixner /* Switch off the timer when it_value is zero */ 8465cee9645SThomas Gleixner if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 8475cee9645SThomas Gleixner return 0; 8485cee9645SThomas Gleixner 84980105cd0SThomas Gleixner timr->it_interval = timespec64_to_ktime(new_setting->it_interval); 850eae1c4aeSThomas Gleixner expires = timespec64_to_ktime(new_setting->it_value); 851cef31d9aSThomas Gleixner sigev_none = timr->it_sigev_notify == SIGEV_NONE; 8525cee9645SThomas Gleixner 853eae1c4aeSThomas Gleixner kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); 854eae1c4aeSThomas Gleixner timr->it_active = !sigev_none; 8555cee9645SThomas Gleixner return 0; 8565cee9645SThomas Gleixner } 8575cee9645SThomas Gleixner 85821670ee4SThomas Gleixner static int do_timer_settime(timer_t timer_id, int tmr_flags, 8591acbe770SAl Viro struct itimerspec64 *new_spec64, 8601acbe770SAl Viro struct itimerspec64 *old_spec64) 8615cee9645SThomas Gleixner { 8621acbe770SAl Viro const struct k_clock *kc; 8635f252b32SDeepa Dinamani struct k_itimer *timr; 86421670ee4SThomas Gleixner unsigned long flags; 8655f252b32SDeepa Dinamani int error = 0; 8665cee9645SThomas Gleixner 8671acbe770SAl Viro if (!timespec64_valid(&new_spec64->it_interval) || 8681acbe770SAl Viro !timespec64_valid(&new_spec64->it_value)) 8695cee9645SThomas Gleixner return -EINVAL; 8705cee9645SThomas Gleixner 8711acbe770SAl Viro if (old_spec64) 8721acbe770SAl Viro memset(old_spec64, 0, sizeof(*old_spec64)); 8736945e5c2SThomas Gleixner 87421670ee4SThomas Gleixner timr = lock_timer(timer_id, &flags); 8756945e5c2SThomas Gleixner retry: 8765cee9645SThomas Gleixner if (!timr) 8775cee9645SThomas Gleixner return -EINVAL; 8785cee9645SThomas Gleixner 879d97bb75dSThomas Gleixner kc = timr->kclock; 8805cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_set)) 8815cee9645SThomas Gleixner error = -EINVAL; 8825cee9645SThomas Gleixner else 88321670ee4SThomas Gleixner error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64); 8845cee9645SThomas Gleixner 8855cee9645SThomas Gleixner if (error == TIMER_RETRY) { 8866945e5c2SThomas Gleixner // We already got the old time... 8876945e5c2SThomas Gleixner old_spec64 = NULL; 8886945e5c2SThomas Gleixner /* Unlocks and relocks the timer if it still exists */ 8896945e5c2SThomas Gleixner timr = timer_wait_running(timr, &flags); 8905cee9645SThomas Gleixner goto retry; 8915cee9645SThomas Gleixner } 8926945e5c2SThomas Gleixner unlock_timer(timr, flags); 8935cee9645SThomas Gleixner 8945cee9645SThomas Gleixner return error; 8955cee9645SThomas Gleixner } 8965cee9645SThomas Gleixner 8971acbe770SAl Viro /* Set a POSIX.1b interval timer */ 8981acbe770SAl Viro SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 8996ff84735SDeepa Dinamani const struct __kernel_itimerspec __user *, new_setting, 9006ff84735SDeepa Dinamani struct __kernel_itimerspec __user *, old_setting) 9011acbe770SAl Viro { 902725816e8SDeepa Dinamani struct itimerspec64 new_spec, old_spec; 903725816e8SDeepa Dinamani struct itimerspec64 *rtn = old_setting ? &old_spec : NULL; 9041acbe770SAl Viro int error = 0; 9051acbe770SAl Viro 9061acbe770SAl Viro if (!new_setting) 9071acbe770SAl Viro return -EINVAL; 9081acbe770SAl Viro 909725816e8SDeepa Dinamani if (get_itimerspec64(&new_spec, new_setting)) 9101acbe770SAl Viro return -EFAULT; 9111acbe770SAl Viro 912725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn); 9131acbe770SAl Viro if (!error && old_setting) { 914725816e8SDeepa Dinamani if (put_itimerspec64(&old_spec, old_setting)) 9151acbe770SAl Viro error = -EFAULT; 9161acbe770SAl Viro } 9171acbe770SAl Viro return error; 9181acbe770SAl Viro } 9191acbe770SAl Viro 9206ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 9218dabe724SArnd Bergmann SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags, 9229afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, new, 9239afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, old) 9241acbe770SAl Viro { 925725816e8SDeepa Dinamani struct itimerspec64 new_spec, old_spec; 926725816e8SDeepa Dinamani struct itimerspec64 *rtn = old ? &old_spec : NULL; 9271acbe770SAl Viro int error = 0; 9281acbe770SAl Viro 9291acbe770SAl Viro if (!new) 9301acbe770SAl Viro return -EINVAL; 9319afc5eeeSArnd Bergmann if (get_old_itimerspec32(&new_spec, new)) 9321acbe770SAl Viro return -EFAULT; 9331acbe770SAl Viro 934725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn); 9351acbe770SAl Viro if (!error && old) { 9369afc5eeeSArnd Bergmann if (put_old_itimerspec32(&old_spec, old)) 9371acbe770SAl Viro error = -EFAULT; 9381acbe770SAl Viro } 9391acbe770SAl Viro return error; 9401acbe770SAl Viro } 9411acbe770SAl Viro #endif 9421acbe770SAl Viro 943f2c45807SThomas Gleixner int common_timer_del(struct k_itimer *timer) 9445cee9645SThomas Gleixner { 945eae1c4aeSThomas Gleixner const struct k_clock *kc = timer->kclock; 9465cee9645SThomas Gleixner 947eae1c4aeSThomas Gleixner timer->it_interval = 0; 948eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timer) < 0) 9495cee9645SThomas Gleixner return TIMER_RETRY; 95021e55c1fSThomas Gleixner timer->it_active = 0; 9515cee9645SThomas Gleixner return 0; 9525cee9645SThomas Gleixner } 9535cee9645SThomas Gleixner 9545cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer) 9555cee9645SThomas Gleixner { 956d97bb75dSThomas Gleixner const struct k_clock *kc = timer->kclock; 9575cee9645SThomas Gleixner 9585cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_del)) 9595cee9645SThomas Gleixner return -EINVAL; 9605cee9645SThomas Gleixner return kc->timer_del(timer); 9615cee9645SThomas Gleixner } 9625cee9645SThomas Gleixner 9635cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */ 9645cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) 9655cee9645SThomas Gleixner { 9665cee9645SThomas Gleixner struct k_itimer *timer; 9675cee9645SThomas Gleixner unsigned long flags; 9685cee9645SThomas Gleixner 9695cee9645SThomas Gleixner timer = lock_timer(timer_id, &flags); 9706945e5c2SThomas Gleixner 9716945e5c2SThomas Gleixner retry_delete: 9725cee9645SThomas Gleixner if (!timer) 9735cee9645SThomas Gleixner return -EINVAL; 9745cee9645SThomas Gleixner 9756945e5c2SThomas Gleixner if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) { 9766945e5c2SThomas Gleixner /* Unlocks and relocks the timer if it still exists */ 9776945e5c2SThomas Gleixner timer = timer_wait_running(timer, &flags); 9785cee9645SThomas Gleixner goto retry_delete; 9795cee9645SThomas Gleixner } 9805cee9645SThomas Gleixner 9815cee9645SThomas Gleixner spin_lock(¤t->sighand->siglock); 9825cee9645SThomas Gleixner list_del(&timer->list); 9835cee9645SThomas Gleixner spin_unlock(¤t->sighand->siglock); 9845cee9645SThomas Gleixner /* 9855cee9645SThomas Gleixner * This keeps any tasks waiting on the spin lock from thinking 9865cee9645SThomas Gleixner * they got something (see the lock code above). 9875cee9645SThomas Gleixner */ 9885cee9645SThomas Gleixner timer->it_signal = NULL; 9895cee9645SThomas Gleixner 9905cee9645SThomas Gleixner unlock_timer(timer, flags); 9915cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 9925cee9645SThomas Gleixner return 0; 9935cee9645SThomas Gleixner } 9945cee9645SThomas Gleixner 9955cee9645SThomas Gleixner /* 9965cee9645SThomas Gleixner * return timer owned by the process, used by exit_itimers 9975cee9645SThomas Gleixner */ 9985cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer) 9995cee9645SThomas Gleixner { 10005cee9645SThomas Gleixner retry_delete: 10017586addbSSebastian Andrzej Siewior spin_lock_irq(&timer->it_lock); 10025cee9645SThomas Gleixner 10035cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) { 10047586addbSSebastian Andrzej Siewior spin_unlock_irq(&timer->it_lock); 10055cee9645SThomas Gleixner goto retry_delete; 10065cee9645SThomas Gleixner } 10075cee9645SThomas Gleixner list_del(&timer->list); 10085cee9645SThomas Gleixner 10097586addbSSebastian Andrzej Siewior spin_unlock_irq(&timer->it_lock); 10105cee9645SThomas Gleixner release_posix_timer(timer, IT_ID_SET); 10115cee9645SThomas Gleixner } 10125cee9645SThomas Gleixner 10135cee9645SThomas Gleixner /* 10145cee9645SThomas Gleixner * This is called by do_exit or de_thread, only when there are no more 10155cee9645SThomas Gleixner * references to the shared signal_struct. 10165cee9645SThomas Gleixner */ 10175cee9645SThomas Gleixner void exit_itimers(struct signal_struct *sig) 10185cee9645SThomas Gleixner { 10195cee9645SThomas Gleixner struct k_itimer *tmr; 10205cee9645SThomas Gleixner 10215cee9645SThomas Gleixner while (!list_empty(&sig->posix_timers)) { 10225cee9645SThomas Gleixner tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); 10235cee9645SThomas Gleixner itimer_delete(tmr); 10245cee9645SThomas Gleixner } 10255cee9645SThomas Gleixner } 10265cee9645SThomas Gleixner 10275cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, 10286d5b8413SDeepa Dinamani const struct __kernel_timespec __user *, tp) 10295cee9645SThomas Gleixner { 1030d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10315c499410SDeepa Dinamani struct timespec64 new_tp; 10325cee9645SThomas Gleixner 10335cee9645SThomas Gleixner if (!kc || !kc->clock_set) 10345cee9645SThomas Gleixner return -EINVAL; 10355cee9645SThomas Gleixner 10365c499410SDeepa Dinamani if (get_timespec64(&new_tp, tp)) 10375cee9645SThomas Gleixner return -EFAULT; 10385cee9645SThomas Gleixner 10395c499410SDeepa Dinamani return kc->clock_set(which_clock, &new_tp); 10405cee9645SThomas Gleixner } 10415cee9645SThomas Gleixner 10425cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, 10436d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp) 10445cee9645SThomas Gleixner { 1045d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10465c499410SDeepa Dinamani struct timespec64 kernel_tp; 10475cee9645SThomas Gleixner int error; 10485cee9645SThomas Gleixner 10495cee9645SThomas Gleixner if (!kc) 10505cee9645SThomas Gleixner return -EINVAL; 10515cee9645SThomas Gleixner 10525c499410SDeepa Dinamani error = kc->clock_get(which_clock, &kernel_tp); 10535cee9645SThomas Gleixner 10545c499410SDeepa Dinamani if (!error && put_timespec64(&kernel_tp, tp)) 10555cee9645SThomas Gleixner error = -EFAULT; 10565cee9645SThomas Gleixner 10575cee9645SThomas Gleixner return error; 10585cee9645SThomas Gleixner } 10595cee9645SThomas Gleixner 1060ead25417SDeepa Dinamani int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx) 10615cee9645SThomas Gleixner { 1062d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 10635cee9645SThomas Gleixner 10645cee9645SThomas Gleixner if (!kc) 10655cee9645SThomas Gleixner return -EINVAL; 10665cee9645SThomas Gleixner if (!kc->clock_adj) 10675cee9645SThomas Gleixner return -EOPNOTSUPP; 10685cee9645SThomas Gleixner 10691a596398SArnd Bergmann return kc->clock_adj(which_clock, ktx); 10701a596398SArnd Bergmann } 10711a596398SArnd Bergmann 10721a596398SArnd Bergmann SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, 10733876ced4SDeepa Dinamani struct __kernel_timex __user *, utx) 10741a596398SArnd Bergmann { 1075ead25417SDeepa Dinamani struct __kernel_timex ktx; 10761a596398SArnd Bergmann int err; 10771a596398SArnd Bergmann 10785cee9645SThomas Gleixner if (copy_from_user(&ktx, utx, sizeof(ktx))) 10795cee9645SThomas Gleixner return -EFAULT; 10805cee9645SThomas Gleixner 10811a596398SArnd Bergmann err = do_clock_adjtime(which_clock, &ktx); 10825cee9645SThomas Gleixner 10835cee9645SThomas Gleixner if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) 10845cee9645SThomas Gleixner return -EFAULT; 10855cee9645SThomas Gleixner 10865cee9645SThomas Gleixner return err; 10875cee9645SThomas Gleixner } 10885cee9645SThomas Gleixner 1089d822cdccSAl Viro SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, 10906d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp) 1091d822cdccSAl Viro { 1092d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 10935c499410SDeepa Dinamani struct timespec64 rtn_tp; 1094d822cdccSAl Viro int error; 1095d822cdccSAl Viro 1096d822cdccSAl Viro if (!kc) 1097d822cdccSAl Viro return -EINVAL; 1098d822cdccSAl Viro 10995c499410SDeepa Dinamani error = kc->clock_getres(which_clock, &rtn_tp); 1100d822cdccSAl Viro 11015c499410SDeepa Dinamani if (!error && tp && put_timespec64(&rtn_tp, tp)) 1102d822cdccSAl Viro error = -EFAULT; 1103d822cdccSAl Viro 1104d822cdccSAl Viro return error; 1105d822cdccSAl Viro } 1106d822cdccSAl Viro 1107b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 11083a4d44b6SAl Viro 11098dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock, 11109afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp) 1111d822cdccSAl Viro { 1112d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11135c499410SDeepa Dinamani struct timespec64 ts; 1114d822cdccSAl Viro 1115d822cdccSAl Viro if (!kc || !kc->clock_set) 1116d822cdccSAl Viro return -EINVAL; 1117d822cdccSAl Viro 11189afc5eeeSArnd Bergmann if (get_old_timespec32(&ts, tp)) 1119d822cdccSAl Viro return -EFAULT; 1120d822cdccSAl Viro 11215c499410SDeepa Dinamani return kc->clock_set(which_clock, &ts); 1122d822cdccSAl Viro } 1123d822cdccSAl Viro 11248dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock, 11259afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp) 1126d822cdccSAl Viro { 1127d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock); 11285c499410SDeepa Dinamani struct timespec64 ts; 11295c499410SDeepa Dinamani int err; 1130d822cdccSAl Viro 1131d822cdccSAl Viro if (!kc) 1132d822cdccSAl Viro return -EINVAL; 1133d822cdccSAl Viro 11345c499410SDeepa Dinamani err = kc->clock_get(which_clock, &ts); 1135d822cdccSAl Viro 11369afc5eeeSArnd Bergmann if (!err && put_old_timespec32(&ts, tp)) 11375c499410SDeepa Dinamani err = -EFAULT; 1138d822cdccSAl Viro 11395c499410SDeepa Dinamani return err; 1140d822cdccSAl Viro } 1141d822cdccSAl Viro 11428dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock, 11434d5f007eSArnd Bergmann struct old_timex32 __user *, utp) 11443a4d44b6SAl Viro { 1145ead25417SDeepa Dinamani struct __kernel_timex ktx; 11463a4d44b6SAl Viro int err; 11473a4d44b6SAl Viro 11484d5f007eSArnd Bergmann err = get_old_timex32(&ktx, utp); 11493a4d44b6SAl Viro if (err) 11503a4d44b6SAl Viro return err; 11513a4d44b6SAl Viro 11521a596398SArnd Bergmann err = do_clock_adjtime(which_clock, &ktx); 11533a4d44b6SAl Viro 11543a4d44b6SAl Viro if (err >= 0) 11554d5f007eSArnd Bergmann err = put_old_timex32(utp, &ktx); 11563a4d44b6SAl Viro 11573a4d44b6SAl Viro return err; 11583a4d44b6SAl Viro } 11593a4d44b6SAl Viro 11608dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock, 11619afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp) 11625cee9645SThomas Gleixner { 1163d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 11645c499410SDeepa Dinamani struct timespec64 ts; 11655c499410SDeepa Dinamani int err; 11665cee9645SThomas Gleixner 11675cee9645SThomas Gleixner if (!kc) 11685cee9645SThomas Gleixner return -EINVAL; 11695cee9645SThomas Gleixner 11705c499410SDeepa Dinamani err = kc->clock_getres(which_clock, &ts); 11719afc5eeeSArnd Bergmann if (!err && tp && put_old_timespec32(&ts, tp)) 11725c499410SDeepa Dinamani return -EFAULT; 11735cee9645SThomas Gleixner 11745c499410SDeepa Dinamani return err; 11755cee9645SThomas Gleixner } 11765c499410SDeepa Dinamani 1177d822cdccSAl Viro #endif 11785cee9645SThomas Gleixner 11795cee9645SThomas Gleixner /* 11805cee9645SThomas Gleixner * nanosleep for monotonic and realtime clocks 11815cee9645SThomas Gleixner */ 11825cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags, 1183938e7cf2SThomas Gleixner const struct timespec64 *rqtp) 11845cee9645SThomas Gleixner { 1185938e7cf2SThomas Gleixner return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ? 11865cee9645SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL, 11875cee9645SThomas Gleixner which_clock); 11885cee9645SThomas Gleixner } 11895cee9645SThomas Gleixner 11905cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, 119101909974SDeepa Dinamani const struct __kernel_timespec __user *, rqtp, 119201909974SDeepa Dinamani struct __kernel_timespec __user *, rmtp) 11935cee9645SThomas Gleixner { 1194d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1195c0edd7c9SDeepa Dinamani struct timespec64 t; 11965cee9645SThomas Gleixner 11975cee9645SThomas Gleixner if (!kc) 11985cee9645SThomas Gleixner return -EINVAL; 11995cee9645SThomas Gleixner if (!kc->nsleep) 120093cb8e20SHelge Deller return -EOPNOTSUPP; 12015cee9645SThomas Gleixner 1202c0edd7c9SDeepa Dinamani if (get_timespec64(&t, rqtp)) 12035cee9645SThomas Gleixner return -EFAULT; 12045cee9645SThomas Gleixner 1205c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t)) 12065cee9645SThomas Gleixner return -EINVAL; 120799e6c0e6SAl Viro if (flags & TIMER_ABSTIME) 120899e6c0e6SAl Viro rmtp = NULL; 1209edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; 121099e6c0e6SAl Viro current->restart_block.nanosleep.rmtp = rmtp; 12115cee9645SThomas Gleixner 1212c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t); 12135cee9645SThomas Gleixner } 12145cee9645SThomas Gleixner 1215b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME 1216b5793b0dSDeepa Dinamani 12178dabe724SArnd Bergmann SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, 12189afc5eeeSArnd Bergmann struct old_timespec32 __user *, rqtp, 12199afc5eeeSArnd Bergmann struct old_timespec32 __user *, rmtp) 12205cee9645SThomas Gleixner { 1221d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock); 1222c0edd7c9SDeepa Dinamani struct timespec64 t; 12235cee9645SThomas Gleixner 1224edbeda46SAl Viro if (!kc) 12255cee9645SThomas Gleixner return -EINVAL; 1226edbeda46SAl Viro if (!kc->nsleep) 122793cb8e20SHelge Deller return -EOPNOTSUPP; 12285cee9645SThomas Gleixner 12299afc5eeeSArnd Bergmann if (get_old_timespec32(&t, rqtp)) 1230edbeda46SAl Viro return -EFAULT; 1231edbeda46SAl Viro 1232c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t)) 1233edbeda46SAl Viro return -EINVAL; 1234edbeda46SAl Viro if (flags & TIMER_ABSTIME) 1235edbeda46SAl Viro rmtp = NULL; 1236edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; 1237edbeda46SAl Viro current->restart_block.nanosleep.compat_rmtp = rmtp; 1238edbeda46SAl Viro 1239c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t); 12405cee9645SThomas Gleixner } 1241b5793b0dSDeepa Dinamani 1242edbeda46SAl Viro #endif 12436631fa12SThomas Gleixner 12446631fa12SThomas Gleixner static const struct k_clock clock_realtime = { 12456631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12466631fa12SThomas Gleixner .clock_get = posix_clock_realtime_get, 12476631fa12SThomas Gleixner .clock_set = posix_clock_realtime_set, 12486631fa12SThomas Gleixner .clock_adj = posix_clock_realtime_adj, 12496631fa12SThomas Gleixner .nsleep = common_nsleep, 12506631fa12SThomas Gleixner .timer_create = common_timer_create, 12516631fa12SThomas Gleixner .timer_set = common_timer_set, 12526631fa12SThomas Gleixner .timer_get = common_timer_get, 12536631fa12SThomas Gleixner .timer_del = common_timer_del, 1254f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 125591d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 125691d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1257eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1258eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 12596631fa12SThomas Gleixner }; 12606631fa12SThomas Gleixner 12616631fa12SThomas Gleixner static const struct k_clock clock_monotonic = { 12626631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12636631fa12SThomas Gleixner .clock_get = posix_ktime_get_ts, 12646631fa12SThomas Gleixner .nsleep = common_nsleep, 12656631fa12SThomas Gleixner .timer_create = common_timer_create, 12666631fa12SThomas Gleixner .timer_set = common_timer_set, 12676631fa12SThomas Gleixner .timer_get = common_timer_get, 12686631fa12SThomas Gleixner .timer_del = common_timer_del, 1269f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 127091d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 127191d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1272eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1273eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 12746631fa12SThomas Gleixner }; 12756631fa12SThomas Gleixner 12766631fa12SThomas Gleixner static const struct k_clock clock_monotonic_raw = { 12776631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12786631fa12SThomas Gleixner .clock_get = posix_get_monotonic_raw, 12796631fa12SThomas Gleixner }; 12806631fa12SThomas Gleixner 12816631fa12SThomas Gleixner static const struct k_clock clock_realtime_coarse = { 12826631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 12836631fa12SThomas Gleixner .clock_get = posix_get_realtime_coarse, 12846631fa12SThomas Gleixner }; 12856631fa12SThomas Gleixner 12866631fa12SThomas Gleixner static const struct k_clock clock_monotonic_coarse = { 12876631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res, 12886631fa12SThomas Gleixner .clock_get = posix_get_monotonic_coarse, 12896631fa12SThomas Gleixner }; 12906631fa12SThomas Gleixner 12916631fa12SThomas Gleixner static const struct k_clock clock_tai = { 12926631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 12936631fa12SThomas Gleixner .clock_get = posix_get_tai, 12946631fa12SThomas Gleixner .nsleep = common_nsleep, 12956631fa12SThomas Gleixner .timer_create = common_timer_create, 12966631fa12SThomas Gleixner .timer_set = common_timer_set, 12976631fa12SThomas Gleixner .timer_get = common_timer_get, 12986631fa12SThomas Gleixner .timer_del = common_timer_del, 1299f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm, 130091d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward, 130191d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1302eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1303eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm, 13046631fa12SThomas Gleixner }; 13056631fa12SThomas Gleixner 1306a3ed0e43SThomas Gleixner static const struct k_clock clock_boottime = { 13076631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res, 1308a3ed0e43SThomas Gleixner .clock_get = posix_get_boottime, 1309a3ed0e43SThomas Gleixner .nsleep = common_nsleep, 1310a3ed0e43SThomas Gleixner .timer_create = common_timer_create, 1311a3ed0e43SThomas Gleixner .timer_set = common_timer_set, 1312a3ed0e43SThomas Gleixner .timer_get = common_timer_get, 1313a3ed0e43SThomas Gleixner .timer_del = common_timer_del, 1314a3ed0e43SThomas Gleixner .timer_rearm = common_hrtimer_rearm, 1315a3ed0e43SThomas Gleixner .timer_forward = common_hrtimer_forward, 1316a3ed0e43SThomas Gleixner .timer_remaining = common_hrtimer_remaining, 1317a3ed0e43SThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel, 1318a3ed0e43SThomas Gleixner .timer_arm = common_hrtimer_arm, 13196631fa12SThomas Gleixner }; 13206631fa12SThomas Gleixner 13216631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[] = { 13226631fa12SThomas Gleixner [CLOCK_REALTIME] = &clock_realtime, 13236631fa12SThomas Gleixner [CLOCK_MONOTONIC] = &clock_monotonic, 13246631fa12SThomas Gleixner [CLOCK_PROCESS_CPUTIME_ID] = &clock_process, 13256631fa12SThomas Gleixner [CLOCK_THREAD_CPUTIME_ID] = &clock_thread, 13266631fa12SThomas Gleixner [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw, 13276631fa12SThomas Gleixner [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse, 13286631fa12SThomas Gleixner [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse, 1329a3ed0e43SThomas Gleixner [CLOCK_BOOTTIME] = &clock_boottime, 13306631fa12SThomas Gleixner [CLOCK_REALTIME_ALARM] = &alarm_clock, 13316631fa12SThomas Gleixner [CLOCK_BOOTTIME_ALARM] = &alarm_clock, 13326631fa12SThomas Gleixner [CLOCK_TAI] = &clock_tai, 13336631fa12SThomas Gleixner }; 13346631fa12SThomas Gleixner 13356631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id) 13366631fa12SThomas Gleixner { 133719b558dbSThomas Gleixner clockid_t idx = id; 133819b558dbSThomas Gleixner 133919b558dbSThomas Gleixner if (id < 0) { 13406631fa12SThomas Gleixner return (id & CLOCKFD_MASK) == CLOCKFD ? 13416631fa12SThomas Gleixner &clock_posix_dynamic : &clock_posix_cpu; 134219b558dbSThomas Gleixner } 13436631fa12SThomas Gleixner 134419b558dbSThomas Gleixner if (id >= ARRAY_SIZE(posix_clocks)) 13456631fa12SThomas Gleixner return NULL; 134619b558dbSThomas Gleixner 134719b558dbSThomas Gleixner return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))]; 13486631fa12SThomas Gleixner } 1349