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>
335a590f35SAndrei Vagin #include <linux/time_namespace.h>
345cee9645SThomas Gleixner
358b094cd0SThomas Gleixner #include "timekeeping.h"
36bab0aae9SThomas Gleixner #include "posix-timers.h"
378b094cd0SThomas Gleixner
385cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache;
395cee9645SThomas Gleixner
408d44b958SThomas Gleixner /*
418d44b958SThomas Gleixner * Timers are managed in a hash table for lockless lookup. The hash key is
428d44b958SThomas Gleixner * constructed from current::signal and the timer ID and the timer is
438d44b958SThomas Gleixner * matched against current::signal and the timer ID when walking the hash
448d44b958SThomas Gleixner * bucket list.
458d44b958SThomas Gleixner *
468d44b958SThomas Gleixner * This allows checkpoint/restore to reconstruct the exact timer IDs for
478d44b958SThomas Gleixner * a process.
488d44b958SThomas Gleixner */
495cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
505cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock);
515cee9645SThomas Gleixner
526631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[];
536631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id);
5467edab48SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic;
556631fa12SThomas Gleixner
56c575689dSThomas Gleixner /* SIGEV_THREAD_ID cannot share a bit with the other SIGEV values. */
575cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
585cee9645SThomas Gleixner ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
595cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
605cee9645SThomas Gleixner #endif
615cee9645SThomas Gleixner
625cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
635cee9645SThomas Gleixner
645cee9645SThomas Gleixner #define lock_timer(tid, flags) \
655cee9645SThomas Gleixner ({ struct k_itimer *__timr; \
665cee9645SThomas Gleixner __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
675cee9645SThomas Gleixner __timr; \
685cee9645SThomas Gleixner })
695cee9645SThomas Gleixner
hash(struct signal_struct * sig,unsigned int nr)705cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr)
715cee9645SThomas Gleixner {
725cee9645SThomas Gleixner return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
735cee9645SThomas Gleixner }
745cee9645SThomas Gleixner
__posix_timers_find(struct hlist_head * head,struct signal_struct * sig,timer_t id)755cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head,
765cee9645SThomas Gleixner struct signal_struct *sig,
775cee9645SThomas Gleixner timer_t id)
785cee9645SThomas Gleixner {
795cee9645SThomas Gleixner struct k_itimer *timer;
805cee9645SThomas Gleixner
81028cf5eaSThomas Gleixner hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)) {
82028cf5eaSThomas Gleixner /* timer->it_signal can be set concurrently */
83028cf5eaSThomas Gleixner if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id))
845cee9645SThomas Gleixner return timer;
855cee9645SThomas Gleixner }
865cee9645SThomas Gleixner return NULL;
875cee9645SThomas Gleixner }
885cee9645SThomas Gleixner
posix_timer_by_id(timer_t id)895cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id)
905cee9645SThomas Gleixner {
915cee9645SThomas Gleixner struct signal_struct *sig = current->signal;
925cee9645SThomas Gleixner struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
935cee9645SThomas Gleixner
945cee9645SThomas Gleixner return __posix_timers_find(head, sig, id);
955cee9645SThomas Gleixner }
965cee9645SThomas Gleixner
posix_timer_add(struct k_itimer * timer)975cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer)
985cee9645SThomas Gleixner {
995cee9645SThomas Gleixner struct signal_struct *sig = current->signal;
1005cee9645SThomas Gleixner struct hlist_head *head;
1018ce8849dSThomas Gleixner unsigned int cnt, id;
1025cee9645SThomas Gleixner
1038ce8849dSThomas Gleixner /*
1048ce8849dSThomas Gleixner * FIXME: Replace this by a per signal struct xarray once there is
1058ce8849dSThomas Gleixner * a plan to handle the resulting CRIU regression gracefully.
1068ce8849dSThomas Gleixner */
1078ce8849dSThomas Gleixner for (cnt = 0; cnt <= INT_MAX; cnt++) {
1085cee9645SThomas Gleixner spin_lock(&hash_lock);
1098ce8849dSThomas Gleixner id = sig->next_posix_timer_id;
1108ce8849dSThomas Gleixner
1118ce8849dSThomas Gleixner /* Write the next ID back. Clamp it to the positive space */
1128ce8849dSThomas Gleixner sig->next_posix_timer_id = (id + 1) & INT_MAX;
1138ce8849dSThomas Gleixner
1148ce8849dSThomas Gleixner head = &posix_timers_hashtable[hash(sig, id)];
1158ce8849dSThomas Gleixner if (!__posix_timers_find(head, sig, id)) {
1165cee9645SThomas Gleixner hlist_add_head_rcu(&timer->t_hash, head);
1175cee9645SThomas Gleixner spin_unlock(&hash_lock);
1188ce8849dSThomas Gleixner return id;
1198ce8849dSThomas Gleixner }
1208ce8849dSThomas Gleixner spin_unlock(&hash_lock);
1218ce8849dSThomas Gleixner }
1228ce8849dSThomas Gleixner /* POSIX return code when no timer ID could be allocated */
1238ce8849dSThomas Gleixner return -EAGAIN;
1245cee9645SThomas Gleixner }
1255cee9645SThomas Gleixner
unlock_timer(struct k_itimer * timr,unsigned long flags)1265cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
1275cee9645SThomas Gleixner {
1285cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, flags);
1295cee9645SThomas Gleixner }
1305cee9645SThomas Gleixner
posix_get_realtime_timespec(clockid_t which_clock,struct timespec64 * tp)131eaf80194SAndrei Vagin static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
1325cee9645SThomas Gleixner {
1333c9c12f4SDeepa Dinamani ktime_get_real_ts64(tp);
1345cee9645SThomas Gleixner return 0;
1355cee9645SThomas Gleixner }
1365cee9645SThomas Gleixner
posix_get_realtime_ktime(clockid_t which_clock)1379c71a2e8SAndrei Vagin static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
1389c71a2e8SAndrei Vagin {
1399c71a2e8SAndrei Vagin return ktime_get_real();
1409c71a2e8SAndrei Vagin }
1419c71a2e8SAndrei Vagin
posix_clock_realtime_set(const clockid_t which_clock,const struct timespec64 * tp)1425cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock,
1430fe6afe3SDeepa Dinamani const struct timespec64 *tp)
1445cee9645SThomas Gleixner {
1450fe6afe3SDeepa Dinamani return do_sys_settimeofday64(tp, NULL);
1465cee9645SThomas Gleixner }
1475cee9645SThomas Gleixner
posix_clock_realtime_adj(const clockid_t which_clock,struct __kernel_timex * t)1485cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock,
149ead25417SDeepa Dinamani struct __kernel_timex *t)
1505cee9645SThomas Gleixner {
1515cee9645SThomas Gleixner return do_adjtimex(t);
1525cee9645SThomas Gleixner }
1535cee9645SThomas Gleixner
posix_get_monotonic_timespec(clockid_t which_clock,struct timespec64 * tp)154eaf80194SAndrei Vagin static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
1555cee9645SThomas Gleixner {
1563c9c12f4SDeepa Dinamani ktime_get_ts64(tp);
1575a590f35SAndrei Vagin timens_add_monotonic(tp);
1585cee9645SThomas Gleixner return 0;
1595cee9645SThomas Gleixner }
1605cee9645SThomas Gleixner
posix_get_monotonic_ktime(clockid_t which_clock)1619c71a2e8SAndrei Vagin static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
1629c71a2e8SAndrei Vagin {
1639c71a2e8SAndrei Vagin return ktime_get();
1649c71a2e8SAndrei Vagin }
1659c71a2e8SAndrei Vagin
posix_get_monotonic_raw(clockid_t which_clock,struct timespec64 * tp)1663c9c12f4SDeepa Dinamani static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
1675cee9645SThomas Gleixner {
16858a10456SArnd Bergmann ktime_get_raw_ts64(tp);
1695a590f35SAndrei Vagin timens_add_monotonic(tp);
1705cee9645SThomas Gleixner return 0;
1715cee9645SThomas Gleixner }
1725cee9645SThomas Gleixner
posix_get_realtime_coarse(clockid_t which_clock,struct timespec64 * tp)1733c9c12f4SDeepa Dinamani static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
1745cee9645SThomas Gleixner {
17558a10456SArnd Bergmann ktime_get_coarse_real_ts64(tp);
1765cee9645SThomas Gleixner return 0;
1775cee9645SThomas Gleixner }
1785cee9645SThomas Gleixner
posix_get_monotonic_coarse(clockid_t which_clock,struct timespec64 * tp)1795cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock,
1803c9c12f4SDeepa Dinamani struct timespec64 *tp)
1815cee9645SThomas Gleixner {
18258a10456SArnd Bergmann ktime_get_coarse_ts64(tp);
1835a590f35SAndrei Vagin timens_add_monotonic(tp);
1845cee9645SThomas Gleixner return 0;
1855cee9645SThomas Gleixner }
1865cee9645SThomas Gleixner
posix_get_coarse_res(const clockid_t which_clock,struct timespec64 * tp)187d2e3e0caSDeepa Dinamani static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
1885cee9645SThomas Gleixner {
189d2e3e0caSDeepa Dinamani *tp = ktime_to_timespec64(KTIME_LOW_RES);
1905cee9645SThomas Gleixner return 0;
1915cee9645SThomas Gleixner }
1925cee9645SThomas Gleixner
posix_get_boottime_timespec(const clockid_t which_clock,struct timespec64 * tp)193eaf80194SAndrei Vagin static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
1945cee9645SThomas Gleixner {
19558a10456SArnd Bergmann ktime_get_boottime_ts64(tp);
1965a590f35SAndrei Vagin timens_add_boottime(tp);
1975cee9645SThomas Gleixner return 0;
1985cee9645SThomas Gleixner }
1995cee9645SThomas Gleixner
posix_get_boottime_ktime(const clockid_t which_clock)2009c71a2e8SAndrei Vagin static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
2019c71a2e8SAndrei Vagin {
2029c71a2e8SAndrei Vagin return ktime_get_boottime();
2039c71a2e8SAndrei Vagin }
2049c71a2e8SAndrei Vagin
posix_get_tai_timespec(clockid_t which_clock,struct timespec64 * tp)205eaf80194SAndrei Vagin static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
20672199320SThomas Gleixner {
20758a10456SArnd Bergmann ktime_get_clocktai_ts64(tp);
20872199320SThomas Gleixner return 0;
20972199320SThomas Gleixner }
21072199320SThomas Gleixner
posix_get_tai_ktime(clockid_t which_clock)2119c71a2e8SAndrei Vagin static ktime_t posix_get_tai_ktime(clockid_t which_clock)
2129c71a2e8SAndrei Vagin {
2139c71a2e8SAndrei Vagin return ktime_get_clocktai();
2149c71a2e8SAndrei Vagin }
2159c71a2e8SAndrei Vagin
posix_get_hrtimer_res(clockid_t which_clock,struct timespec64 * tp)216d2e3e0caSDeepa Dinamani static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
217056a3cacSThomas Gleixner {
218056a3cacSThomas Gleixner tp->tv_sec = 0;
219056a3cacSThomas Gleixner tp->tv_nsec = hrtimer_resolution;
220056a3cacSThomas Gleixner return 0;
221056a3cacSThomas Gleixner }
222056a3cacSThomas Gleixner
init_posix_timers(void)223d3ba5a9aSChristoph Hellwig static __init int init_posix_timers(void)
224d3ba5a9aSChristoph Hellwig {
2255cee9645SThomas Gleixner posix_timers_cache = kmem_cache_create("posix_timers_cache",
226c509723eSVasily Averin sizeof(struct k_itimer), 0,
227c509723eSVasily Averin SLAB_PANIC | SLAB_ACCOUNT, NULL);
2285cee9645SThomas Gleixner return 0;
2295cee9645SThomas Gleixner }
2305cee9645SThomas Gleixner __initcall(init_posix_timers);
2315cee9645SThomas Gleixner
23278c9c4dfSThomas Gleixner /*
23378c9c4dfSThomas Gleixner * The siginfo si_overrun field and the return value of timer_getoverrun(2)
23478c9c4dfSThomas Gleixner * are of type int. Clamp the overrun value to INT_MAX
23578c9c4dfSThomas Gleixner */
timer_overrun_to_int(struct k_itimer * timr,int baseval)23678c9c4dfSThomas Gleixner static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
23778c9c4dfSThomas Gleixner {
23878c9c4dfSThomas Gleixner s64 sum = timr->it_overrun_last + (s64)baseval;
23978c9c4dfSThomas Gleixner
24078c9c4dfSThomas Gleixner return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
24178c9c4dfSThomas Gleixner }
24278c9c4dfSThomas Gleixner
common_hrtimer_rearm(struct k_itimer * timr)243f37fb0aaSThomas Gleixner static void common_hrtimer_rearm(struct k_itimer *timr)
2445cee9645SThomas Gleixner {
2455cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
2465cee9645SThomas Gleixner
24778c9c4dfSThomas Gleixner timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
24880105cd0SThomas Gleixner timr->it_interval);
2495cee9645SThomas Gleixner hrtimer_restart(timer);
2505cee9645SThomas Gleixner }
2515cee9645SThomas Gleixner
2525cee9645SThomas Gleixner /*
25302972d79SThomas Gleixner * This function is called from the signal delivery code if
25402972d79SThomas Gleixner * info->si_sys_private is not zero, which indicates that the timer has to
25502972d79SThomas Gleixner * be rearmed. Restart the timer and update info::si_overrun.
2565cee9645SThomas Gleixner */
posixtimer_rearm(struct kernel_siginfo * info)257ae7795bcSEric W. Biederman void posixtimer_rearm(struct kernel_siginfo *info)
2585cee9645SThomas Gleixner {
2595cee9645SThomas Gleixner struct k_itimer *timr;
2605cee9645SThomas Gleixner unsigned long flags;
2615cee9645SThomas Gleixner
2625cee9645SThomas Gleixner timr = lock_timer(info->si_tid, &flags);
263af888d67SThomas Gleixner if (!timr)
264af888d67SThomas Gleixner return;
2655cee9645SThomas Gleixner
2660e334db6SThomas Gleixner if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
267f37fb0aaSThomas Gleixner timr->kclock->timer_rearm(timr);
2685cee9645SThomas Gleixner
26921e55c1fSThomas Gleixner timr->it_active = 1;
270af888d67SThomas Gleixner timr->it_overrun_last = timr->it_overrun;
27178c9c4dfSThomas Gleixner timr->it_overrun = -1LL;
272af888d67SThomas Gleixner ++timr->it_requeue_pending;
273af888d67SThomas Gleixner
27478c9c4dfSThomas Gleixner info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
2755cee9645SThomas Gleixner }
2765cee9645SThomas Gleixner
2775cee9645SThomas Gleixner unlock_timer(timr, flags);
2785cee9645SThomas Gleixner }
2795cee9645SThomas Gleixner
posix_timer_event(struct k_itimer * timr,int si_private)2805cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private)
2815cee9645SThomas Gleixner {
28224122c7fSEric W. Biederman enum pid_type type;
2831dae37c7SColin Ian King int ret;
2845cee9645SThomas Gleixner /*
2855cee9645SThomas Gleixner * FIXME: if ->sigq is queued we can race with
28696fe3b07SThomas Gleixner * dequeue_signal()->posixtimer_rearm().
2875cee9645SThomas Gleixner *
2885cee9645SThomas Gleixner * If dequeue_signal() sees the "right" value of
28996fe3b07SThomas Gleixner * si_sys_private it calls posixtimer_rearm().
2905cee9645SThomas Gleixner * We re-queue ->sigq and drop ->it_lock().
29196fe3b07SThomas Gleixner * posixtimer_rearm() locks the timer
2925cee9645SThomas Gleixner * and re-schedules it while ->sigq is pending.
2935cee9645SThomas Gleixner * Not really bad, but not that we want.
2945cee9645SThomas Gleixner */
2955cee9645SThomas Gleixner timr->sigq->info.si_sys_private = si_private;
2965cee9645SThomas Gleixner
29724122c7fSEric W. Biederman type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
29824122c7fSEric W. Biederman ret = send_sigqueue(timr->sigq, timr->it_pid, type);
2995cee9645SThomas Gleixner /* If we failed to send the signal the timer stops. */
3005cee9645SThomas Gleixner return ret > 0;
3015cee9645SThomas Gleixner }
3025cee9645SThomas Gleixner
3035cee9645SThomas Gleixner /*
30484999b8bSThomas Gleixner * This function gets called when a POSIX.1b interval timer expires from
30584999b8bSThomas Gleixner * the HRTIMER interrupt (soft interrupt on RT kernels).
30684999b8bSThomas Gleixner *
30784999b8bSThomas Gleixner * Handles CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME and CLOCK_TAI
30884999b8bSThomas Gleixner * based timers.
3095cee9645SThomas Gleixner */
posix_timer_fn(struct hrtimer * timer)3105cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
3115cee9645SThomas Gleixner {
312b96ce493SThomas Gleixner enum hrtimer_restart ret = HRTIMER_NORESTART;
3135cee9645SThomas Gleixner struct k_itimer *timr;
3145cee9645SThomas Gleixner unsigned long flags;
3155cee9645SThomas Gleixner int si_private = 0;
3165cee9645SThomas Gleixner
3175cee9645SThomas Gleixner timr = container_of(timer, struct k_itimer, it.real.timer);
3185cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, flags);
3195cee9645SThomas Gleixner
32021e55c1fSThomas Gleixner timr->it_active = 0;
32180105cd0SThomas Gleixner if (timr->it_interval != 0)
3225cee9645SThomas Gleixner si_private = ++timr->it_requeue_pending;
3235cee9645SThomas Gleixner
3245cee9645SThomas Gleixner if (posix_timer_event(timr, si_private)) {
3255cee9645SThomas Gleixner /*
32684999b8bSThomas Gleixner * The signal was not queued due to SIG_IGN. As a
32784999b8bSThomas Gleixner * consequence the timer is not going to be rearmed from
32884999b8bSThomas Gleixner * the signal delivery path. But as a real signal handler
32984999b8bSThomas Gleixner * can be installed later the timer must be rearmed here.
3305cee9645SThomas Gleixner */
33180105cd0SThomas Gleixner if (timr->it_interval != 0) {
3325cee9645SThomas Gleixner ktime_t now = hrtimer_cb_get_time(timer);
3335cee9645SThomas Gleixner
3345cee9645SThomas Gleixner /*
3355cee9645SThomas Gleixner * FIXME: What we really want, is to stop this
3365cee9645SThomas Gleixner * timer completely and restart it in case the
3375cee9645SThomas Gleixner * SIG_IGN is removed. This is a non trivial
33884999b8bSThomas Gleixner * change to the signal handling code.
3395cee9645SThomas Gleixner *
34084999b8bSThomas Gleixner * For now let timers with an interval less than a
34184999b8bSThomas Gleixner * jiffie expire every jiffie and recheck for a
34284999b8bSThomas Gleixner * valid signal handler.
34384999b8bSThomas Gleixner *
34484999b8bSThomas Gleixner * This avoids interrupt starvation in case of a
34584999b8bSThomas Gleixner * very small interval, which would expire the
34684999b8bSThomas Gleixner * timer immediately again.
34784999b8bSThomas Gleixner *
34884999b8bSThomas Gleixner * Moving now ahead of time by one jiffie tricks
34984999b8bSThomas Gleixner * hrtimer_forward() to expire the timer later,
35084999b8bSThomas Gleixner * while it still maintains the overrun accuracy
35184999b8bSThomas Gleixner * for the price of a slight inconsistency in the
35284999b8bSThomas Gleixner * timer_gettime() case. This is at least better
35384999b8bSThomas Gleixner * than a timer storm.
35484999b8bSThomas Gleixner *
35584999b8bSThomas Gleixner * Only required when high resolution timers are
35684999b8bSThomas Gleixner * enabled as the periodic tick based timers are
35784999b8bSThomas Gleixner * automatically aligned to the next tick.
3585cee9645SThomas Gleixner */
359*b9a40f24SLukas Bulwahn if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) {
36084999b8bSThomas Gleixner ktime_t kj = TICK_NSEC;
3615cee9645SThomas Gleixner
36280105cd0SThomas Gleixner if (timr->it_interval < kj)
3635cee9645SThomas Gleixner now = ktime_add(now, kj);
3645cee9645SThomas Gleixner }
36584999b8bSThomas Gleixner
36684999b8bSThomas Gleixner timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval);
3675cee9645SThomas Gleixner ret = HRTIMER_RESTART;
3685cee9645SThomas Gleixner ++timr->it_requeue_pending;
36921e55c1fSThomas Gleixner timr->it_active = 1;
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner }
3725cee9645SThomas Gleixner
3735cee9645SThomas Gleixner unlock_timer(timr, flags);
3745cee9645SThomas Gleixner return ret;
3755cee9645SThomas Gleixner }
3765cee9645SThomas Gleixner
good_sigevent(sigevent_t * event)3775cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event)
3785cee9645SThomas Gleixner {
3792118e1f5SEric W. Biederman struct pid *pid = task_tgid(current);
3802118e1f5SEric W. Biederman struct task_struct *rtn;
3815cee9645SThomas Gleixner
382cef31d9aSThomas Gleixner switch (event->sigev_notify) {
383cef31d9aSThomas Gleixner case SIGEV_SIGNAL | SIGEV_THREAD_ID:
3842118e1f5SEric W. Biederman pid = find_vpid(event->sigev_notify_thread_id);
3852118e1f5SEric W. Biederman rtn = pid_task(pid, PIDTYPE_PID);
386cef31d9aSThomas Gleixner if (!rtn || !same_thread_group(rtn, current))
3875cee9645SThomas Gleixner return NULL;
388df561f66SGustavo A. R. Silva fallthrough;
389cef31d9aSThomas Gleixner case SIGEV_SIGNAL:
390cef31d9aSThomas Gleixner case SIGEV_THREAD:
391cef31d9aSThomas Gleixner if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
3925cee9645SThomas Gleixner return NULL;
393df561f66SGustavo A. R. Silva fallthrough;
394cef31d9aSThomas Gleixner case SIGEV_NONE:
3952118e1f5SEric W. Biederman return pid;
396cef31d9aSThomas Gleixner default:
397cef31d9aSThomas Gleixner return NULL;
398cef31d9aSThomas Gleixner }
3995cee9645SThomas Gleixner }
4005cee9645SThomas Gleixner
alloc_posix_timer(void)4015cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void)
4025cee9645SThomas Gleixner {
403b96ce493SThomas Gleixner struct k_itimer *tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
404b96ce493SThomas Gleixner
4055cee9645SThomas Gleixner if (!tmr)
4065cee9645SThomas Gleixner return tmr;
4075cee9645SThomas Gleixner if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
4085cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr);
4095cee9645SThomas Gleixner return NULL;
4105cee9645SThomas Gleixner }
4113b10db2bSEric W. Biederman clear_siginfo(&tmr->sigq->info);
4125cee9645SThomas Gleixner return tmr;
4135cee9645SThomas Gleixner }
4145cee9645SThomas Gleixner
k_itimer_rcu_free(struct rcu_head * head)4155cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head)
4165cee9645SThomas Gleixner {
4175d99b32aSSebastian Andrzej Siewior struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
4185cee9645SThomas Gleixner
4195cee9645SThomas Gleixner kmem_cache_free(posix_timers_cache, tmr);
4205cee9645SThomas Gleixner }
4215cee9645SThomas Gleixner
posix_timer_free(struct k_itimer * tmr)4228cc96ca2SThomas Gleixner static void posix_timer_free(struct k_itimer *tmr)
4235cee9645SThomas Gleixner {
4245cee9645SThomas Gleixner put_pid(tmr->it_pid);
4255cee9645SThomas Gleixner sigqueue_free(tmr->sigq);
4265d99b32aSSebastian Andrzej Siewior call_rcu(&tmr->rcu, k_itimer_rcu_free);
4275cee9645SThomas Gleixner }
4285cee9645SThomas Gleixner
posix_timer_unhash_and_free(struct k_itimer * tmr)4298cc96ca2SThomas Gleixner static void posix_timer_unhash_and_free(struct k_itimer *tmr)
4308cc96ca2SThomas Gleixner {
4318cc96ca2SThomas Gleixner spin_lock(&hash_lock);
4328cc96ca2SThomas Gleixner hlist_del_rcu(&tmr->t_hash);
4338cc96ca2SThomas Gleixner spin_unlock(&hash_lock);
4348cc96ca2SThomas Gleixner posix_timer_free(tmr);
4358cc96ca2SThomas Gleixner }
4368cc96ca2SThomas Gleixner
common_timer_create(struct k_itimer * new_timer)4375cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer)
4385cee9645SThomas Gleixner {
4395cee9645SThomas Gleixner hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
4405cee9645SThomas Gleixner return 0;
4415cee9645SThomas Gleixner }
4425cee9645SThomas Gleixner
4435cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */
do_timer_create(clockid_t which_clock,struct sigevent * event,timer_t __user * created_timer_id)4442482097cSAl Viro static int do_timer_create(clockid_t which_clock, struct sigevent *event,
4452482097cSAl Viro timer_t __user *created_timer_id)
4465cee9645SThomas Gleixner {
447d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
4485cee9645SThomas Gleixner struct k_itimer *new_timer;
4495cee9645SThomas Gleixner int error, new_timer_id;
4505cee9645SThomas Gleixner
4515cee9645SThomas Gleixner if (!kc)
4525cee9645SThomas Gleixner return -EINVAL;
4535cee9645SThomas Gleixner if (!kc->timer_create)
4545cee9645SThomas Gleixner return -EOPNOTSUPP;
4555cee9645SThomas Gleixner
4565cee9645SThomas Gleixner new_timer = alloc_posix_timer();
4575cee9645SThomas Gleixner if (unlikely(!new_timer))
4585cee9645SThomas Gleixner return -EAGAIN;
4595cee9645SThomas Gleixner
4605cee9645SThomas Gleixner spin_lock_init(&new_timer->it_lock);
461ae88967dSThomas Gleixner
462ae88967dSThomas Gleixner /*
463ae88967dSThomas Gleixner * Add the timer to the hash table. The timer is not yet valid
464ae88967dSThomas Gleixner * because new_timer::it_signal is still NULL. The timer id is also
465ae88967dSThomas Gleixner * not yet visible to user space.
466ae88967dSThomas Gleixner */
4675cee9645SThomas Gleixner new_timer_id = posix_timer_add(new_timer);
4685cee9645SThomas Gleixner if (new_timer_id < 0) {
4698cc96ca2SThomas Gleixner posix_timer_free(new_timer);
4708cc96ca2SThomas Gleixner return new_timer_id;
4715cee9645SThomas Gleixner }
4725cee9645SThomas Gleixner
4735cee9645SThomas Gleixner new_timer->it_id = (timer_t) new_timer_id;
4745cee9645SThomas Gleixner new_timer->it_clock = which_clock;
475d97bb75dSThomas Gleixner new_timer->kclock = kc;
47678c9c4dfSThomas Gleixner new_timer->it_overrun = -1LL;
4775cee9645SThomas Gleixner
4782482097cSAl Viro if (event) {
4795cee9645SThomas Gleixner rcu_read_lock();
4802482097cSAl Viro new_timer->it_pid = get_pid(good_sigevent(event));
4815cee9645SThomas Gleixner rcu_read_unlock();
4825cee9645SThomas Gleixner if (!new_timer->it_pid) {
4835cee9645SThomas Gleixner error = -EINVAL;
4845cee9645SThomas Gleixner goto out;
4855cee9645SThomas Gleixner }
4862482097cSAl Viro new_timer->it_sigev_notify = event->sigev_notify;
4872482097cSAl Viro new_timer->sigq->info.si_signo = event->sigev_signo;
4882482097cSAl Viro new_timer->sigq->info.si_value = event->sigev_value;
4895cee9645SThomas Gleixner } else {
4902482097cSAl Viro new_timer->it_sigev_notify = SIGEV_SIGNAL;
4912482097cSAl Viro new_timer->sigq->info.si_signo = SIGALRM;
4922482097cSAl Viro memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
4932482097cSAl Viro new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
4945cee9645SThomas Gleixner new_timer->it_pid = get_pid(task_tgid(current));
4955cee9645SThomas Gleixner }
4965cee9645SThomas Gleixner
4975cee9645SThomas Gleixner new_timer->sigq->info.si_tid = new_timer->it_id;
4985cee9645SThomas Gleixner new_timer->sigq->info.si_code = SI_TIMER;
4995cee9645SThomas Gleixner
50052f090b1SThomas Gleixner if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id))) {
5015cee9645SThomas Gleixner error = -EFAULT;
5025cee9645SThomas Gleixner goto out;
5035cee9645SThomas Gleixner }
50452f090b1SThomas Gleixner /*
50552f090b1SThomas Gleixner * After succesful copy out, the timer ID is visible to user space
50652f090b1SThomas Gleixner * now but not yet valid because new_timer::signal is still NULL.
50752f090b1SThomas Gleixner *
50852f090b1SThomas Gleixner * Complete the initialization with the clock specific create
50952f090b1SThomas Gleixner * callback.
51052f090b1SThomas Gleixner */
5115cee9645SThomas Gleixner error = kc->timer_create(new_timer);
5125cee9645SThomas Gleixner if (error)
5135cee9645SThomas Gleixner goto out;
5145cee9645SThomas Gleixner
5155cee9645SThomas Gleixner spin_lock_irq(¤t->sighand->siglock);
516ae88967dSThomas Gleixner /* This makes the timer valid in the hash table */
517028cf5eaSThomas Gleixner WRITE_ONCE(new_timer->it_signal, current->signal);
5185cee9645SThomas Gleixner list_add(&new_timer->list, ¤t->signal->posix_timers);
5195cee9645SThomas Gleixner spin_unlock_irq(¤t->sighand->siglock);
5205cee9645SThomas Gleixner /*
52152f090b1SThomas Gleixner * After unlocking sighand::siglock @new_timer is subject to
52252f090b1SThomas Gleixner * concurrent removal and cannot be touched anymore
5235cee9645SThomas Gleixner */
52452f090b1SThomas Gleixner return 0;
5255cee9645SThomas Gleixner out:
5268cc96ca2SThomas Gleixner posix_timer_unhash_and_free(new_timer);
5275cee9645SThomas Gleixner return error;
5285cee9645SThomas Gleixner }
5295cee9645SThomas Gleixner
SYSCALL_DEFINE3(timer_create,const clockid_t,which_clock,struct sigevent __user *,timer_event_spec,timer_t __user *,created_timer_id)5302482097cSAl Viro SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
5312482097cSAl Viro struct sigevent __user *, timer_event_spec,
5322482097cSAl Viro timer_t __user *, created_timer_id)
5332482097cSAl Viro {
5342482097cSAl Viro if (timer_event_spec) {
5352482097cSAl Viro sigevent_t event;
5362482097cSAl Viro
5372482097cSAl Viro if (copy_from_user(&event, timer_event_spec, sizeof (event)))
5382482097cSAl Viro return -EFAULT;
5392482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id);
5402482097cSAl Viro }
5412482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id);
5422482097cSAl Viro }
5432482097cSAl Viro
5442482097cSAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(timer_create,clockid_t,which_clock,struct compat_sigevent __user *,timer_event_spec,timer_t __user *,created_timer_id)5452482097cSAl Viro COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
5462482097cSAl Viro struct compat_sigevent __user *, timer_event_spec,
5472482097cSAl Viro timer_t __user *, created_timer_id)
5482482097cSAl Viro {
5492482097cSAl Viro if (timer_event_spec) {
5502482097cSAl Viro sigevent_t event;
5512482097cSAl Viro
5522482097cSAl Viro if (get_compat_sigevent(&event, timer_event_spec))
5532482097cSAl Viro return -EFAULT;
5542482097cSAl Viro return do_timer_create(which_clock, &event, created_timer_id);
5552482097cSAl Viro }
5562482097cSAl Viro return do_timer_create(which_clock, NULL, created_timer_id);
5572482097cSAl Viro }
5582482097cSAl Viro #endif
5592482097cSAl Viro
__lock_timer(timer_t timer_id,unsigned long * flags)5605cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
5615cee9645SThomas Gleixner {
5625cee9645SThomas Gleixner struct k_itimer *timr;
5635cee9645SThomas Gleixner
5645cee9645SThomas Gleixner /*
5655cee9645SThomas Gleixner * timer_t could be any type >= int and we want to make sure any
5665cee9645SThomas Gleixner * @timer_id outside positive int range fails lookup.
5675cee9645SThomas Gleixner */
5685cee9645SThomas Gleixner if ((unsigned long long)timer_id > INT_MAX)
5695cee9645SThomas Gleixner return NULL;
5705cee9645SThomas Gleixner
571ae88967dSThomas Gleixner /*
572ae88967dSThomas Gleixner * The hash lookup and the timers are RCU protected.
573ae88967dSThomas Gleixner *
574ae88967dSThomas Gleixner * Timers are added to the hash in invalid state where
575ae88967dSThomas Gleixner * timr::it_signal == NULL. timer::it_signal is only set after the
576ae88967dSThomas Gleixner * rest of the initialization succeeded.
577ae88967dSThomas Gleixner *
578ae88967dSThomas Gleixner * Timer destruction happens in steps:
579ae88967dSThomas Gleixner * 1) Set timr::it_signal to NULL with timr::it_lock held
580ae88967dSThomas Gleixner * 2) Release timr::it_lock
581ae88967dSThomas Gleixner * 3) Remove from the hash under hash_lock
582ae88967dSThomas Gleixner * 4) Call RCU for removal after the grace period
583ae88967dSThomas Gleixner *
584ae88967dSThomas Gleixner * Holding rcu_read_lock() accross the lookup ensures that
585ae88967dSThomas Gleixner * the timer cannot be freed.
586ae88967dSThomas Gleixner *
587ae88967dSThomas Gleixner * The lookup validates locklessly that timr::it_signal ==
588ae88967dSThomas Gleixner * current::it_signal and timr::it_id == @timer_id. timr::it_id
589ae88967dSThomas Gleixner * can't change, but timr::it_signal becomes NULL during
590ae88967dSThomas Gleixner * destruction.
591ae88967dSThomas Gleixner */
5925cee9645SThomas Gleixner rcu_read_lock();
5935cee9645SThomas Gleixner timr = posix_timer_by_id(timer_id);
5945cee9645SThomas Gleixner if (timr) {
5955cee9645SThomas Gleixner spin_lock_irqsave(&timr->it_lock, *flags);
596ae88967dSThomas Gleixner /*
597ae88967dSThomas Gleixner * Validate under timr::it_lock that timr::it_signal is
598ae88967dSThomas Gleixner * still valid. Pairs with #1 above.
599ae88967dSThomas Gleixner */
6005cee9645SThomas Gleixner if (timr->it_signal == current->signal) {
6015cee9645SThomas Gleixner rcu_read_unlock();
6025cee9645SThomas Gleixner return timr;
6035cee9645SThomas Gleixner }
6045cee9645SThomas Gleixner spin_unlock_irqrestore(&timr->it_lock, *flags);
6055cee9645SThomas Gleixner }
6065cee9645SThomas Gleixner rcu_read_unlock();
6075cee9645SThomas Gleixner
6085cee9645SThomas Gleixner return NULL;
6095cee9645SThomas Gleixner }
6105cee9645SThomas Gleixner
common_hrtimer_remaining(struct k_itimer * timr,ktime_t now)61191d57baeSThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
61291d57baeSThomas Gleixner {
61391d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
61491d57baeSThomas Gleixner
61591d57baeSThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, now);
61691d57baeSThomas Gleixner }
61791d57baeSThomas Gleixner
common_hrtimer_forward(struct k_itimer * timr,ktime_t now)6186fec64e1SThomas Gleixner static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
61991d57baeSThomas Gleixner {
62091d57baeSThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
62191d57baeSThomas Gleixner
6226fec64e1SThomas Gleixner return hrtimer_forward(timer, now, timr->it_interval);
62391d57baeSThomas Gleixner }
62491d57baeSThomas Gleixner
6255cee9645SThomas Gleixner /*
626a86e9284SThomas Gleixner * Get the time remaining on a POSIX.1b interval timer.
6275cee9645SThomas Gleixner *
628a86e9284SThomas Gleixner * Two issues to handle here:
6295cee9645SThomas Gleixner *
630a86e9284SThomas Gleixner * 1) The timer has a requeue pending. The return value must appear as
631a86e9284SThomas Gleixner * if the timer has been requeued right now.
632a86e9284SThomas Gleixner *
633a86e9284SThomas Gleixner * 2) The timer is a SIGEV_NONE timer. These timers are never enqueued
634a86e9284SThomas Gleixner * into the hrtimer queue and therefore never expired. Emulate expiry
635a86e9284SThomas Gleixner * here taking #1 into account.
6365cee9645SThomas Gleixner */
common_timer_get(struct k_itimer * timr,struct itimerspec64 * cur_setting)637f2c45807SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
6385cee9645SThomas Gleixner {
63991d57baeSThomas Gleixner const struct k_clock *kc = timr->kclock;
6405cee9645SThomas Gleixner ktime_t now, remaining, iv;
64191d57baeSThomas Gleixner bool sig_none;
6425cee9645SThomas Gleixner
643cef31d9aSThomas Gleixner sig_none = timr->it_sigev_notify == SIGEV_NONE;
64480105cd0SThomas Gleixner iv = timr->it_interval;
6455cee9645SThomas Gleixner
6465cee9645SThomas Gleixner /* interval timer ? */
64791d57baeSThomas Gleixner if (iv) {
6485f252b32SDeepa Dinamani cur_setting->it_interval = ktime_to_timespec64(iv);
64991d57baeSThomas Gleixner } else if (!timr->it_active) {
65091d57baeSThomas Gleixner /*
651a86e9284SThomas Gleixner * SIGEV_NONE oneshot timers are never queued and therefore
652a86e9284SThomas Gleixner * timr->it_active is always false. The check below
653a86e9284SThomas Gleixner * vs. remaining time will handle this case.
654a86e9284SThomas Gleixner *
655a86e9284SThomas Gleixner * For all other timers there is nothing to update here, so
656a86e9284SThomas Gleixner * return.
65791d57baeSThomas Gleixner */
65891d57baeSThomas Gleixner if (!sig_none)
6595cee9645SThomas Gleixner return;
66091d57baeSThomas Gleixner }
6615cee9645SThomas Gleixner
662198fa445SAndrei Vagin now = kc->clock_get_ktime(timr->it_clock);
6635cee9645SThomas Gleixner
66491d57baeSThomas Gleixner /*
665a86e9284SThomas Gleixner * If this is an interval timer and either has requeue pending or
666a86e9284SThomas Gleixner * is a SIGEV_NONE timer move the expiry time forward by intervals,
667a86e9284SThomas Gleixner * so expiry is > now.
66891d57baeSThomas Gleixner */
66991d57baeSThomas Gleixner if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
67078c9c4dfSThomas Gleixner timr->it_overrun += kc->timer_forward(timr, now);
67191d57baeSThomas Gleixner
67291d57baeSThomas Gleixner remaining = kc->timer_remaining(timr, now);
673a86e9284SThomas Gleixner /*
674a86e9284SThomas Gleixner * As @now is retrieved before a possible timer_forward() and
675a86e9284SThomas Gleixner * cannot be reevaluated by the compiler @remaining is based on the
676a86e9284SThomas Gleixner * same @now value. Therefore @remaining is consistent vs. @now.
677a86e9284SThomas Gleixner *
678a86e9284SThomas Gleixner * Consequently all interval timers, i.e. @iv > 0, cannot have a
679a86e9284SThomas Gleixner * remaining time <= 0 because timer_forward() guarantees to move
680a86e9284SThomas Gleixner * them forward so that the next timer expiry is > @now.
681a86e9284SThomas Gleixner */
6822456e855SThomas Gleixner if (remaining <= 0) {
6835cee9645SThomas Gleixner /*
684a86e9284SThomas Gleixner * A single shot SIGEV_NONE timer must return 0, when it is
685a86e9284SThomas Gleixner * expired! Timers which have a real signal delivery mode
686a86e9284SThomas Gleixner * must return a remaining time greater than 0 because the
687a86e9284SThomas Gleixner * signal has not yet been delivered.
6885cee9645SThomas Gleixner */
68991d57baeSThomas Gleixner if (!sig_none)
6905cee9645SThomas Gleixner cur_setting->it_value.tv_nsec = 1;
69191d57baeSThomas Gleixner } else {
6925f252b32SDeepa Dinamani cur_setting->it_value = ktime_to_timespec64(remaining);
6935cee9645SThomas Gleixner }
69491d57baeSThomas Gleixner }
6955cee9645SThomas Gleixner
do_timer_gettime(timer_t timer_id,struct itimerspec64 * setting)696b0dc1242SAl Viro static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
6975cee9645SThomas Gleixner {
698d3ba5a9aSChristoph Hellwig const struct k_clock *kc;
699b96ce493SThomas Gleixner struct k_itimer *timr;
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
707b0dc1242SAl Viro memset(setting, 0, sizeof(*setting));
708d97bb75dSThomas Gleixner kc = timr->kclock;
7095cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_get))
7105cee9645SThomas Gleixner ret = -EINVAL;
7115cee9645SThomas Gleixner else
712b0dc1242SAl Viro kc->timer_get(timr, setting);
7135cee9645SThomas Gleixner
7145cee9645SThomas Gleixner unlock_timer(timr, flags);
7155cee9645SThomas Gleixner return ret;
7165cee9645SThomas Gleixner }
7175cee9645SThomas Gleixner
718b0dc1242SAl Viro /* Get the time remaining on a POSIX.1b interval timer. */
SYSCALL_DEFINE2(timer_gettime,timer_t,timer_id,struct __kernel_itimerspec __user *,setting)719b0dc1242SAl Viro SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
7206ff84735SDeepa Dinamani struct __kernel_itimerspec __user *, setting)
721b0dc1242SAl Viro {
722725816e8SDeepa Dinamani struct itimerspec64 cur_setting;
723b0dc1242SAl Viro
724725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting);
725b0dc1242SAl Viro if (!ret) {
726725816e8SDeepa Dinamani if (put_itimerspec64(&cur_setting, setting))
727b0dc1242SAl Viro ret = -EFAULT;
728b0dc1242SAl Viro }
729b0dc1242SAl Viro return ret;
730b0dc1242SAl Viro }
731b0dc1242SAl Viro
7326ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
7336ff84735SDeepa Dinamani
SYSCALL_DEFINE2(timer_gettime32,timer_t,timer_id,struct old_itimerspec32 __user *,setting)7348dabe724SArnd Bergmann SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
7359afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, setting)
736b0dc1242SAl Viro {
737725816e8SDeepa Dinamani struct itimerspec64 cur_setting;
738b0dc1242SAl Viro
739725816e8SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting);
740b0dc1242SAl Viro if (!ret) {
7419afc5eeeSArnd Bergmann if (put_old_itimerspec32(&cur_setting, setting))
742b0dc1242SAl Viro ret = -EFAULT;
743b0dc1242SAl Viro }
744b0dc1242SAl Viro return ret;
745b0dc1242SAl Viro }
7466ff84735SDeepa Dinamani
747b0dc1242SAl Viro #endif
748b0dc1242SAl Viro
74965cade46SThomas Gleixner /**
75065cade46SThomas Gleixner * sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
75165cade46SThomas Gleixner * @timer_id: The timer ID which identifies the timer
75265cade46SThomas Gleixner *
75365cade46SThomas Gleixner * The "overrun count" of a timer is one plus the number of expiration
75465cade46SThomas Gleixner * intervals which have elapsed between the first expiry, which queues the
75565cade46SThomas Gleixner * signal and the actual signal delivery. On signal delivery the "overrun
75665cade46SThomas Gleixner * count" is calculated and cached, so it can be returned directly here.
75765cade46SThomas Gleixner *
75865cade46SThomas Gleixner * As this is relative to the last queued signal the returned overrun count
75965cade46SThomas Gleixner * is meaningless outside of the signal delivery path and even there it
76065cade46SThomas Gleixner * does not accurately reflect the current state when user space evaluates
76165cade46SThomas Gleixner * it.
76265cade46SThomas Gleixner *
76365cade46SThomas Gleixner * Returns:
76465cade46SThomas Gleixner * -EINVAL @timer_id is invalid
76565cade46SThomas Gleixner * 1..INT_MAX The number of overruns related to the last delivered signal
7665cee9645SThomas Gleixner */
SYSCALL_DEFINE1(timer_getoverrun,timer_t,timer_id)7675cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
7685cee9645SThomas Gleixner {
7695cee9645SThomas Gleixner struct k_itimer *timr;
7705cee9645SThomas Gleixner unsigned long flags;
771b96ce493SThomas Gleixner int overrun;
7725cee9645SThomas Gleixner
7735cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags);
7745cee9645SThomas Gleixner if (!timr)
7755cee9645SThomas Gleixner return -EINVAL;
7765cee9645SThomas Gleixner
77778c9c4dfSThomas Gleixner overrun = timer_overrun_to_int(timr, 0);
7785cee9645SThomas Gleixner unlock_timer(timr, flags);
7795cee9645SThomas Gleixner
7805cee9645SThomas Gleixner return overrun;
7815cee9645SThomas Gleixner }
7825cee9645SThomas Gleixner
common_hrtimer_arm(struct k_itimer * timr,ktime_t expires,bool absolute,bool sigev_none)783eae1c4aeSThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
784eae1c4aeSThomas Gleixner bool absolute, bool sigev_none)
7855cee9645SThomas Gleixner {
7865cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
7875cee9645SThomas Gleixner enum hrtimer_mode mode;
7885cee9645SThomas Gleixner
789eae1c4aeSThomas Gleixner mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
79067edab48SThomas Gleixner /*
79167edab48SThomas Gleixner * Posix magic: Relative CLOCK_REALTIME timers are not affected by
79267edab48SThomas Gleixner * clock modifications, so they become CLOCK_MONOTONIC based under the
79367edab48SThomas Gleixner * hood. See hrtimer_init(). Update timr->kclock, so the generic
7949c71a2e8SAndrei Vagin * functions which use timr->kclock->clock_get_*() work.
79567edab48SThomas Gleixner *
79667edab48SThomas Gleixner * Note: it_clock stays unmodified, because the next timer_set() might
79767edab48SThomas Gleixner * use ABSTIME, so it needs to switch back.
79867edab48SThomas Gleixner */
79967edab48SThomas Gleixner if (timr->it_clock == CLOCK_REALTIME)
80067edab48SThomas Gleixner timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
80167edab48SThomas Gleixner
802eae1c4aeSThomas Gleixner hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
803eae1c4aeSThomas Gleixner timr->it.real.timer.function = posix_timer_fn;
804eae1c4aeSThomas Gleixner
805eae1c4aeSThomas Gleixner if (!absolute)
806eae1c4aeSThomas Gleixner expires = ktime_add_safe(expires, timer->base->get_time());
807eae1c4aeSThomas Gleixner hrtimer_set_expires(timer, expires);
808eae1c4aeSThomas Gleixner
809eae1c4aeSThomas Gleixner if (!sigev_none)
810eae1c4aeSThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
811eae1c4aeSThomas Gleixner }
812eae1c4aeSThomas Gleixner
common_hrtimer_try_to_cancel(struct k_itimer * timr)813eae1c4aeSThomas Gleixner static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
814eae1c4aeSThomas Gleixner {
815eae1c4aeSThomas Gleixner return hrtimer_try_to_cancel(&timr->it.real.timer);
816eae1c4aeSThomas Gleixner }
817eae1c4aeSThomas Gleixner
common_timer_wait_running(struct k_itimer * timer)818ec8f954aSThomas Gleixner static void common_timer_wait_running(struct k_itimer *timer)
819ec8f954aSThomas Gleixner {
820ec8f954aSThomas Gleixner hrtimer_cancel_wait_running(&timer->it.real.timer);
821ec8f954aSThomas Gleixner }
822ec8f954aSThomas Gleixner
8230bee3b60SFrederic Weisbecker /*
8247d990902SThomas Gleixner * On PREEMPT_RT this prevents priority inversion and a potential livelock
8257d990902SThomas Gleixner * against the ksoftirqd thread in case that ksoftirqd gets preempted while
8267d990902SThomas Gleixner * executing a hrtimer callback.
8277d990902SThomas Gleixner *
8287d990902SThomas Gleixner * See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
8297d990902SThomas Gleixner * just results in a cpu_relax().
8307d990902SThomas Gleixner *
8317d990902SThomas Gleixner * For POSIX CPU timers with CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n this is
8327d990902SThomas Gleixner * just a cpu_relax(). With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this
8337d990902SThomas Gleixner * prevents spinning on an eventually scheduled out task and a livelock
8347d990902SThomas Gleixner * when the task which tries to delete or disarm the timer has preempted
8357d990902SThomas Gleixner * the task which runs the expiry in task work context.
8360bee3b60SFrederic Weisbecker */
timer_wait_running(struct k_itimer * timer,unsigned long * flags)8376945e5c2SThomas Gleixner static struct k_itimer *timer_wait_running(struct k_itimer *timer,
8386945e5c2SThomas Gleixner unsigned long *flags)
8396945e5c2SThomas Gleixner {
840ec8f954aSThomas Gleixner const struct k_clock *kc = READ_ONCE(timer->kclock);
8416945e5c2SThomas Gleixner timer_t timer_id = READ_ONCE(timer->it_id);
8426945e5c2SThomas Gleixner
843ec8f954aSThomas Gleixner /* Prevent kfree(timer) after dropping the lock */
844ec8f954aSThomas Gleixner rcu_read_lock();
8456945e5c2SThomas Gleixner unlock_timer(timer, *flags);
846ec8f954aSThomas Gleixner
847f7abf14fSThomas Gleixner /*
848f7abf14fSThomas Gleixner * kc->timer_wait_running() might drop RCU lock. So @timer
849f7abf14fSThomas Gleixner * cannot be touched anymore after the function returns!
850f7abf14fSThomas Gleixner */
851ec8f954aSThomas Gleixner if (!WARN_ON_ONCE(!kc->timer_wait_running))
852ec8f954aSThomas Gleixner kc->timer_wait_running(timer);
853ec8f954aSThomas Gleixner
854ec8f954aSThomas Gleixner rcu_read_unlock();
8556945e5c2SThomas Gleixner /* Relock the timer. It might be not longer hashed. */
8566945e5c2SThomas Gleixner return lock_timer(timer_id, flags);
8576945e5c2SThomas Gleixner }
8586945e5c2SThomas Gleixner
859eae1c4aeSThomas Gleixner /* Set a POSIX.1b interval timer. */
common_timer_set(struct k_itimer * timr,int flags,struct itimerspec64 * new_setting,struct itimerspec64 * old_setting)860f2c45807SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags,
861eae1c4aeSThomas Gleixner struct itimerspec64 *new_setting,
862eae1c4aeSThomas Gleixner struct itimerspec64 *old_setting)
863eae1c4aeSThomas Gleixner {
864eae1c4aeSThomas Gleixner const struct k_clock *kc = timr->kclock;
865eae1c4aeSThomas Gleixner bool sigev_none;
866eae1c4aeSThomas Gleixner ktime_t expires;
867eae1c4aeSThomas Gleixner
8685cee9645SThomas Gleixner if (old_setting)
8695cee9645SThomas Gleixner common_timer_get(timr, old_setting);
8705cee9645SThomas Gleixner
871eae1c4aeSThomas Gleixner /* Prevent rearming by clearing the interval */
87280105cd0SThomas Gleixner timr->it_interval = 0;
8735cee9645SThomas Gleixner /*
874eae1c4aeSThomas Gleixner * Careful here. On SMP systems the timer expiry function could be
875eae1c4aeSThomas Gleixner * active and spinning on timr->it_lock.
8765cee9645SThomas Gleixner */
877eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timr) < 0)
8785cee9645SThomas Gleixner return TIMER_RETRY;
8795cee9645SThomas Gleixner
88021e55c1fSThomas Gleixner timr->it_active = 0;
8815cee9645SThomas Gleixner timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
8825cee9645SThomas Gleixner ~REQUEUE_PENDING;
8835cee9645SThomas Gleixner timr->it_overrun_last = 0;
8845cee9645SThomas Gleixner
885eae1c4aeSThomas Gleixner /* Switch off the timer when it_value is zero */
8865cee9645SThomas Gleixner if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
8875cee9645SThomas Gleixner return 0;
8885cee9645SThomas Gleixner
88980105cd0SThomas Gleixner timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
890eae1c4aeSThomas Gleixner expires = timespec64_to_ktime(new_setting->it_value);
8917da8b3a4SAndrei Vagin if (flags & TIMER_ABSTIME)
8927da8b3a4SAndrei Vagin expires = timens_ktime_to_host(timr->it_clock, expires);
893cef31d9aSThomas Gleixner sigev_none = timr->it_sigev_notify == SIGEV_NONE;
8945cee9645SThomas Gleixner
895eae1c4aeSThomas Gleixner kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
896eae1c4aeSThomas Gleixner timr->it_active = !sigev_none;
8975cee9645SThomas Gleixner return 0;
8985cee9645SThomas Gleixner }
8995cee9645SThomas Gleixner
do_timer_settime(timer_t timer_id,int tmr_flags,struct itimerspec64 * new_spec64,struct itimerspec64 * old_spec64)90021670ee4SThomas Gleixner static int do_timer_settime(timer_t timer_id, int tmr_flags,
9011acbe770SAl Viro struct itimerspec64 *new_spec64,
9021acbe770SAl Viro struct itimerspec64 *old_spec64)
9035cee9645SThomas Gleixner {
9041acbe770SAl Viro const struct k_clock *kc;
9055f252b32SDeepa Dinamani struct k_itimer *timr;
90621670ee4SThomas Gleixner unsigned long flags;
9075f252b32SDeepa Dinamani int error = 0;
9085cee9645SThomas Gleixner
9091acbe770SAl Viro if (!timespec64_valid(&new_spec64->it_interval) ||
9101acbe770SAl Viro !timespec64_valid(&new_spec64->it_value))
9115cee9645SThomas Gleixner return -EINVAL;
9125cee9645SThomas Gleixner
9131acbe770SAl Viro if (old_spec64)
9141acbe770SAl Viro memset(old_spec64, 0, sizeof(*old_spec64));
9156945e5c2SThomas Gleixner
91621670ee4SThomas Gleixner timr = lock_timer(timer_id, &flags);
9176945e5c2SThomas Gleixner retry:
9185cee9645SThomas Gleixner if (!timr)
9195cee9645SThomas Gleixner return -EINVAL;
9205cee9645SThomas Gleixner
921d97bb75dSThomas Gleixner kc = timr->kclock;
9225cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_set))
9235cee9645SThomas Gleixner error = -EINVAL;
9245cee9645SThomas Gleixner else
92521670ee4SThomas Gleixner error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
9265cee9645SThomas Gleixner
9275cee9645SThomas Gleixner if (error == TIMER_RETRY) {
9286945e5c2SThomas Gleixner // We already got the old time...
9296945e5c2SThomas Gleixner old_spec64 = NULL;
9306945e5c2SThomas Gleixner /* Unlocks and relocks the timer if it still exists */
9316945e5c2SThomas Gleixner timr = timer_wait_running(timr, &flags);
9325cee9645SThomas Gleixner goto retry;
9335cee9645SThomas Gleixner }
9346945e5c2SThomas Gleixner unlock_timer(timr, flags);
9355cee9645SThomas Gleixner
9365cee9645SThomas Gleixner return error;
9375cee9645SThomas Gleixner }
9385cee9645SThomas Gleixner
9391acbe770SAl Viro /* Set a POSIX.1b interval timer */
SYSCALL_DEFINE4(timer_settime,timer_t,timer_id,int,flags,const struct __kernel_itimerspec __user *,new_setting,struct __kernel_itimerspec __user *,old_setting)9401acbe770SAl Viro SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
9416ff84735SDeepa Dinamani const struct __kernel_itimerspec __user *, new_setting,
9426ff84735SDeepa Dinamani struct __kernel_itimerspec __user *, old_setting)
9431acbe770SAl Viro {
944b96ce493SThomas Gleixner struct itimerspec64 new_spec, old_spec, *rtn;
9451acbe770SAl Viro int error = 0;
9461acbe770SAl Viro
9471acbe770SAl Viro if (!new_setting)
9481acbe770SAl Viro return -EINVAL;
9491acbe770SAl Viro
950725816e8SDeepa Dinamani if (get_itimerspec64(&new_spec, new_setting))
9511acbe770SAl Viro return -EFAULT;
9521acbe770SAl Viro
953b96ce493SThomas Gleixner rtn = old_setting ? &old_spec : NULL;
954725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn);
9551acbe770SAl Viro if (!error && old_setting) {
956725816e8SDeepa Dinamani if (put_itimerspec64(&old_spec, old_setting))
9571acbe770SAl Viro error = -EFAULT;
9581acbe770SAl Viro }
9591acbe770SAl Viro return error;
9601acbe770SAl Viro }
9611acbe770SAl Viro
9626ff84735SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
SYSCALL_DEFINE4(timer_settime32,timer_t,timer_id,int,flags,struct old_itimerspec32 __user *,new,struct old_itimerspec32 __user *,old)9638dabe724SArnd Bergmann SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
9649afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, new,
9659afc5eeeSArnd Bergmann struct old_itimerspec32 __user *, old)
9661acbe770SAl Viro {
967725816e8SDeepa Dinamani struct itimerspec64 new_spec, old_spec;
968725816e8SDeepa Dinamani struct itimerspec64 *rtn = old ? &old_spec : NULL;
9691acbe770SAl Viro int error = 0;
9701acbe770SAl Viro
9711acbe770SAl Viro if (!new)
9721acbe770SAl Viro return -EINVAL;
9739afc5eeeSArnd Bergmann if (get_old_itimerspec32(&new_spec, new))
9741acbe770SAl Viro return -EFAULT;
9751acbe770SAl Viro
976725816e8SDeepa Dinamani error = do_timer_settime(timer_id, flags, &new_spec, rtn);
9771acbe770SAl Viro if (!error && old) {
9789afc5eeeSArnd Bergmann if (put_old_itimerspec32(&old_spec, old))
9791acbe770SAl Viro error = -EFAULT;
9801acbe770SAl Viro }
9811acbe770SAl Viro return error;
9821acbe770SAl Viro }
9831acbe770SAl Viro #endif
9841acbe770SAl Viro
common_timer_del(struct k_itimer * timer)985f2c45807SThomas Gleixner int common_timer_del(struct k_itimer *timer)
9865cee9645SThomas Gleixner {
987eae1c4aeSThomas Gleixner const struct k_clock *kc = timer->kclock;
9885cee9645SThomas Gleixner
989eae1c4aeSThomas Gleixner timer->it_interval = 0;
990eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timer) < 0)
9915cee9645SThomas Gleixner return TIMER_RETRY;
99221e55c1fSThomas Gleixner timer->it_active = 0;
9935cee9645SThomas Gleixner return 0;
9945cee9645SThomas Gleixner }
9955cee9645SThomas Gleixner
timer_delete_hook(struct k_itimer * timer)9965cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer)
9975cee9645SThomas Gleixner {
998d97bb75dSThomas Gleixner const struct k_clock *kc = timer->kclock;
9995cee9645SThomas Gleixner
10005cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_del))
10015cee9645SThomas Gleixner return -EINVAL;
10025cee9645SThomas Gleixner return kc->timer_del(timer);
10035cee9645SThomas Gleixner }
10045cee9645SThomas Gleixner
10055cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */
SYSCALL_DEFINE1(timer_delete,timer_t,timer_id)10065cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
10075cee9645SThomas Gleixner {
10085cee9645SThomas Gleixner struct k_itimer *timer;
10095cee9645SThomas Gleixner unsigned long flags;
10105cee9645SThomas Gleixner
10115cee9645SThomas Gleixner timer = lock_timer(timer_id, &flags);
10126945e5c2SThomas Gleixner
10136945e5c2SThomas Gleixner retry_delete:
10145cee9645SThomas Gleixner if (!timer)
10155cee9645SThomas Gleixner return -EINVAL;
10165cee9645SThomas Gleixner
10176945e5c2SThomas Gleixner if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
10186945e5c2SThomas Gleixner /* Unlocks and relocks the timer if it still exists */
10196945e5c2SThomas Gleixner timer = timer_wait_running(timer, &flags);
10205cee9645SThomas Gleixner goto retry_delete;
10215cee9645SThomas Gleixner }
10225cee9645SThomas Gleixner
10235cee9645SThomas Gleixner spin_lock(¤t->sighand->siglock);
10245cee9645SThomas Gleixner list_del(&timer->list);
10255cee9645SThomas Gleixner spin_unlock(¤t->sighand->siglock);
10265cee9645SThomas Gleixner /*
1027028cf5eaSThomas Gleixner * A concurrent lookup could check timer::it_signal lockless. It
1028028cf5eaSThomas Gleixner * will reevaluate with timer::it_lock held and observe the NULL.
10295cee9645SThomas Gleixner */
1030028cf5eaSThomas Gleixner WRITE_ONCE(timer->it_signal, NULL);
10315cee9645SThomas Gleixner
10325cee9645SThomas Gleixner unlock_timer(timer, flags);
10338cc96ca2SThomas Gleixner posix_timer_unhash_and_free(timer);
10345cee9645SThomas Gleixner return 0;
10355cee9645SThomas Gleixner }
10365cee9645SThomas Gleixner
10375cee9645SThomas Gleixner /*
10389d9e5220SThomas Gleixner * Delete a timer if it is armed, remove it from the hash and schedule it
10399d9e5220SThomas Gleixner * for RCU freeing.
10405cee9645SThomas Gleixner */
itimer_delete(struct k_itimer * timer)10415cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer)
10425cee9645SThomas Gleixner {
10439d9e5220SThomas Gleixner unsigned long flags;
10445cee9645SThomas Gleixner
10459d9e5220SThomas Gleixner /*
10469d9e5220SThomas Gleixner * irqsave is required to make timer_wait_running() work.
10479d9e5220SThomas Gleixner */
10489d9e5220SThomas Gleixner spin_lock_irqsave(&timer->it_lock, flags);
10499d9e5220SThomas Gleixner
10509d9e5220SThomas Gleixner retry_delete:
10519d9e5220SThomas Gleixner /*
10529d9e5220SThomas Gleixner * Even if the timer is not longer accessible from other tasks
10539d9e5220SThomas Gleixner * it still might be armed and queued in the underlying timer
10549d9e5220SThomas Gleixner * mechanism. Worse, that timer mechanism might run the expiry
10559d9e5220SThomas Gleixner * function concurrently.
10569d9e5220SThomas Gleixner */
10575cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) {
10589d9e5220SThomas Gleixner /*
10599d9e5220SThomas Gleixner * Timer is expired concurrently, prevent livelocks
10609d9e5220SThomas Gleixner * and pointless spinning on RT.
10619d9e5220SThomas Gleixner *
10629d9e5220SThomas Gleixner * timer_wait_running() drops timer::it_lock, which opens
10639d9e5220SThomas Gleixner * the possibility for another task to delete the timer.
10649d9e5220SThomas Gleixner *
10659d9e5220SThomas Gleixner * That's not possible here because this is invoked from
10669d9e5220SThomas Gleixner * do_exit() only for the last thread of the thread group.
10679d9e5220SThomas Gleixner * So no other task can access and delete that timer.
10689d9e5220SThomas Gleixner */
10699d9e5220SThomas Gleixner if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
10709d9e5220SThomas Gleixner return;
10719d9e5220SThomas Gleixner
10725cee9645SThomas Gleixner goto retry_delete;
10735cee9645SThomas Gleixner }
10745cee9645SThomas Gleixner list_del(&timer->list);
10755cee9645SThomas Gleixner
107672786ff2SThomas Gleixner /*
107772786ff2SThomas Gleixner * Setting timer::it_signal to NULL is technically not required
107872786ff2SThomas Gleixner * here as nothing can access the timer anymore legitimately via
107972786ff2SThomas Gleixner * the hash table. Set it to NULL nevertheless so that all deletion
108072786ff2SThomas Gleixner * paths are consistent.
108172786ff2SThomas Gleixner */
108272786ff2SThomas Gleixner WRITE_ONCE(timer->it_signal, NULL);
108372786ff2SThomas Gleixner
10849d9e5220SThomas Gleixner spin_unlock_irqrestore(&timer->it_lock, flags);
10858cc96ca2SThomas Gleixner posix_timer_unhash_and_free(timer);
10865cee9645SThomas Gleixner }
10875cee9645SThomas Gleixner
10885cee9645SThomas Gleixner /*
10899d9e5220SThomas Gleixner * Invoked from do_exit() when the last thread of a thread group exits.
10909d9e5220SThomas Gleixner * At that point no other task can access the timers of the dying
10919d9e5220SThomas Gleixner * task anymore.
10925cee9645SThomas Gleixner */
exit_itimers(struct task_struct * tsk)1093d5b36a4dSOleg Nesterov void exit_itimers(struct task_struct *tsk)
10945cee9645SThomas Gleixner {
1095d5b36a4dSOleg Nesterov struct list_head timers;
10965cee9645SThomas Gleixner struct k_itimer *tmr;
10975cee9645SThomas Gleixner
1098d5b36a4dSOleg Nesterov if (list_empty(&tsk->signal->posix_timers))
1099d5b36a4dSOleg Nesterov return;
1100d5b36a4dSOleg Nesterov
11019d9e5220SThomas Gleixner /* Protect against concurrent read via /proc/$PID/timers */
1102d5b36a4dSOleg Nesterov spin_lock_irq(&tsk->sighand->siglock);
1103d5b36a4dSOleg Nesterov list_replace_init(&tsk->signal->posix_timers, &timers);
1104d5b36a4dSOleg Nesterov spin_unlock_irq(&tsk->sighand->siglock);
1105d5b36a4dSOleg Nesterov
11069d9e5220SThomas Gleixner /* The timers are not longer accessible via tsk::signal */
1107d5b36a4dSOleg Nesterov while (!list_empty(&timers)) {
1108d5b36a4dSOleg Nesterov tmr = list_first_entry(&timers, struct k_itimer, list);
11095cee9645SThomas Gleixner itimer_delete(tmr);
11105cee9645SThomas Gleixner }
11115cee9645SThomas Gleixner }
11125cee9645SThomas Gleixner
SYSCALL_DEFINE2(clock_settime,const clockid_t,which_clock,const struct __kernel_timespec __user *,tp)11135cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
11146d5b8413SDeepa Dinamani const struct __kernel_timespec __user *, tp)
11155cee9645SThomas Gleixner {
1116d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
11175c499410SDeepa Dinamani struct timespec64 new_tp;
11185cee9645SThomas Gleixner
11195cee9645SThomas Gleixner if (!kc || !kc->clock_set)
11205cee9645SThomas Gleixner return -EINVAL;
11215cee9645SThomas Gleixner
11225c499410SDeepa Dinamani if (get_timespec64(&new_tp, tp))
11235cee9645SThomas Gleixner return -EFAULT;
11245cee9645SThomas Gleixner
11253561fcb4SThomas Gleixner /*
11263561fcb4SThomas Gleixner * Permission checks have to be done inside the clock specific
11273561fcb4SThomas Gleixner * setter callback.
11283561fcb4SThomas Gleixner */
11295c499410SDeepa Dinamani return kc->clock_set(which_clock, &new_tp);
11305cee9645SThomas Gleixner }
11315cee9645SThomas Gleixner
SYSCALL_DEFINE2(clock_gettime,const clockid_t,which_clock,struct __kernel_timespec __user *,tp)11325cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
11336d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp)
11345cee9645SThomas Gleixner {
1135d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
11365c499410SDeepa Dinamani struct timespec64 kernel_tp;
11375cee9645SThomas Gleixner int error;
11385cee9645SThomas Gleixner
11395cee9645SThomas Gleixner if (!kc)
11405cee9645SThomas Gleixner return -EINVAL;
11415cee9645SThomas Gleixner
1142819a95feSAndrei Vagin error = kc->clock_get_timespec(which_clock, &kernel_tp);
11435cee9645SThomas Gleixner
11445c499410SDeepa Dinamani if (!error && put_timespec64(&kernel_tp, tp))
11455cee9645SThomas Gleixner error = -EFAULT;
11465cee9645SThomas Gleixner
11475cee9645SThomas Gleixner return error;
11485cee9645SThomas Gleixner }
11495cee9645SThomas Gleixner
do_clock_adjtime(const clockid_t which_clock,struct __kernel_timex * ktx)1150ead25417SDeepa Dinamani int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
11515cee9645SThomas Gleixner {
1152d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
11535cee9645SThomas Gleixner
11545cee9645SThomas Gleixner if (!kc)
11555cee9645SThomas Gleixner return -EINVAL;
11565cee9645SThomas Gleixner if (!kc->clock_adj)
11575cee9645SThomas Gleixner return -EOPNOTSUPP;
11585cee9645SThomas Gleixner
11591a596398SArnd Bergmann return kc->clock_adj(which_clock, ktx);
11601a596398SArnd Bergmann }
11611a596398SArnd Bergmann
SYSCALL_DEFINE2(clock_adjtime,const clockid_t,which_clock,struct __kernel_timex __user *,utx)11621a596398SArnd Bergmann SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
11633876ced4SDeepa Dinamani struct __kernel_timex __user *, utx)
11641a596398SArnd Bergmann {
1165ead25417SDeepa Dinamani struct __kernel_timex ktx;
11661a596398SArnd Bergmann int err;
11671a596398SArnd Bergmann
11685cee9645SThomas Gleixner if (copy_from_user(&ktx, utx, sizeof(ktx)))
11695cee9645SThomas Gleixner return -EFAULT;
11705cee9645SThomas Gleixner
11711a596398SArnd Bergmann err = do_clock_adjtime(which_clock, &ktx);
11725cee9645SThomas Gleixner
11735cee9645SThomas Gleixner if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
11745cee9645SThomas Gleixner return -EFAULT;
11755cee9645SThomas Gleixner
11765cee9645SThomas Gleixner return err;
11775cee9645SThomas Gleixner }
11785cee9645SThomas Gleixner
117901679b5dSThomas Gleixner /**
118001679b5dSThomas Gleixner * sys_clock_getres - Get the resolution of a clock
118101679b5dSThomas Gleixner * @which_clock: The clock to get the resolution for
118201679b5dSThomas Gleixner * @tp: Pointer to a a user space timespec64 for storage
118301679b5dSThomas Gleixner *
118401679b5dSThomas Gleixner * POSIX defines:
118501679b5dSThomas Gleixner *
118601679b5dSThomas Gleixner * "The clock_getres() function shall return the resolution of any
118701679b5dSThomas Gleixner * clock. Clock resolutions are implementation-defined and cannot be set by
118801679b5dSThomas Gleixner * a process. If the argument res is not NULL, the resolution of the
118901679b5dSThomas Gleixner * specified clock shall be stored in the location pointed to by res. If
119001679b5dSThomas Gleixner * res is NULL, the clock resolution is not returned. If the time argument
119101679b5dSThomas Gleixner * of clock_settime() is not a multiple of res, then the value is truncated
119201679b5dSThomas Gleixner * to a multiple of res."
119301679b5dSThomas Gleixner *
119401679b5dSThomas Gleixner * Due to the various hardware constraints the real resolution can vary
119501679b5dSThomas Gleixner * wildly and even change during runtime when the underlying devices are
119601679b5dSThomas Gleixner * replaced. The kernel also can use hardware devices with different
119701679b5dSThomas Gleixner * resolutions for reading the time and for arming timers.
119801679b5dSThomas Gleixner *
119901679b5dSThomas Gleixner * The kernel therefore deviates from the POSIX spec in various aspects:
120001679b5dSThomas Gleixner *
120101679b5dSThomas Gleixner * 1) The resolution returned to user space
120201679b5dSThomas Gleixner *
120301679b5dSThomas Gleixner * For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
120401679b5dSThomas Gleixner * CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
120501679b5dSThomas Gleixner * the kernel differentiates only two cases:
120601679b5dSThomas Gleixner *
120701679b5dSThomas Gleixner * I) Low resolution mode:
120801679b5dSThomas Gleixner *
120901679b5dSThomas Gleixner * When high resolution timers are disabled at compile or runtime
121001679b5dSThomas Gleixner * the resolution returned is nanoseconds per tick, which represents
121101679b5dSThomas Gleixner * the precision at which timers expire.
121201679b5dSThomas Gleixner *
121301679b5dSThomas Gleixner * II) High resolution mode:
121401679b5dSThomas Gleixner *
121501679b5dSThomas Gleixner * When high resolution timers are enabled the resolution returned
121601679b5dSThomas Gleixner * is always one nanosecond independent of the actual resolution of
121701679b5dSThomas Gleixner * the underlying hardware devices.
121801679b5dSThomas Gleixner *
121901679b5dSThomas Gleixner * For CLOCK_*_ALARM the actual resolution depends on system
122001679b5dSThomas Gleixner * state. When system is running the resolution is the same as the
122101679b5dSThomas Gleixner * resolution of the other clocks. During suspend the actual
122201679b5dSThomas Gleixner * resolution is the resolution of the underlying RTC device which
122301679b5dSThomas Gleixner * might be way less precise than the clockevent device used during
122401679b5dSThomas Gleixner * running state.
122501679b5dSThomas Gleixner *
122601679b5dSThomas Gleixner * For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
122701679b5dSThomas Gleixner * returned is always nanoseconds per tick.
122801679b5dSThomas Gleixner *
122901679b5dSThomas Gleixner * For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
123001679b5dSThomas Gleixner * returned is always one nanosecond under the assumption that the
123101679b5dSThomas Gleixner * underlying scheduler clock has a better resolution than nanoseconds
123201679b5dSThomas Gleixner * per tick.
123301679b5dSThomas Gleixner *
123401679b5dSThomas Gleixner * For dynamic POSIX clocks (PTP devices) the resolution returned is
123501679b5dSThomas Gleixner * always one nanosecond.
123601679b5dSThomas Gleixner *
123701679b5dSThomas Gleixner * 2) Affect on sys_clock_settime()
123801679b5dSThomas Gleixner *
123901679b5dSThomas Gleixner * The kernel does not truncate the time which is handed in to
124001679b5dSThomas Gleixner * sys_clock_settime(). The kernel internal timekeeping is always using
124101679b5dSThomas Gleixner * nanoseconds precision independent of the clocksource device which is
124201679b5dSThomas Gleixner * used to read the time from. The resolution of that device only
124301679b5dSThomas Gleixner * affects the presicion of the time returned by sys_clock_gettime().
124401679b5dSThomas Gleixner *
124501679b5dSThomas Gleixner * Returns:
124601679b5dSThomas Gleixner * 0 Success. @tp contains the resolution
124701679b5dSThomas Gleixner * -EINVAL @which_clock is not a valid clock ID
124801679b5dSThomas Gleixner * -EFAULT Copying the resolution to @tp faulted
124901679b5dSThomas Gleixner * -ENODEV Dynamic POSIX clock is not backed by a device
125001679b5dSThomas Gleixner * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
125101679b5dSThomas Gleixner */
SYSCALL_DEFINE2(clock_getres,const clockid_t,which_clock,struct __kernel_timespec __user *,tp)1252d822cdccSAl Viro SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
12536d5b8413SDeepa Dinamani struct __kernel_timespec __user *, tp)
1254d822cdccSAl Viro {
1255d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock);
12565c499410SDeepa Dinamani struct timespec64 rtn_tp;
1257d822cdccSAl Viro int error;
1258d822cdccSAl Viro
1259d822cdccSAl Viro if (!kc)
1260d822cdccSAl Viro return -EINVAL;
1261d822cdccSAl Viro
12625c499410SDeepa Dinamani error = kc->clock_getres(which_clock, &rtn_tp);
1263d822cdccSAl Viro
12645c499410SDeepa Dinamani if (!error && tp && put_timespec64(&rtn_tp, tp))
1265d822cdccSAl Viro error = -EFAULT;
1266d822cdccSAl Viro
1267d822cdccSAl Viro return error;
1268d822cdccSAl Viro }
1269d822cdccSAl Viro
1270b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
12713a4d44b6SAl Viro
SYSCALL_DEFINE2(clock_settime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)12728dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
12739afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp)
1274d822cdccSAl Viro {
1275d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock);
12765c499410SDeepa Dinamani struct timespec64 ts;
1277d822cdccSAl Viro
1278d822cdccSAl Viro if (!kc || !kc->clock_set)
1279d822cdccSAl Viro return -EINVAL;
1280d822cdccSAl Viro
12819afc5eeeSArnd Bergmann if (get_old_timespec32(&ts, tp))
1282d822cdccSAl Viro return -EFAULT;
1283d822cdccSAl Viro
12845c499410SDeepa Dinamani return kc->clock_set(which_clock, &ts);
1285d822cdccSAl Viro }
1286d822cdccSAl Viro
SYSCALL_DEFINE2(clock_gettime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)12878dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
12889afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp)
1289d822cdccSAl Viro {
1290d822cdccSAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock);
12915c499410SDeepa Dinamani struct timespec64 ts;
12925c499410SDeepa Dinamani int err;
1293d822cdccSAl Viro
1294d822cdccSAl Viro if (!kc)
1295d822cdccSAl Viro return -EINVAL;
1296d822cdccSAl Viro
1297819a95feSAndrei Vagin err = kc->clock_get_timespec(which_clock, &ts);
1298d822cdccSAl Viro
12999afc5eeeSArnd Bergmann if (!err && put_old_timespec32(&ts, tp))
13005c499410SDeepa Dinamani err = -EFAULT;
1301d822cdccSAl Viro
13025c499410SDeepa Dinamani return err;
1303d822cdccSAl Viro }
1304d822cdccSAl Viro
SYSCALL_DEFINE2(clock_adjtime32,clockid_t,which_clock,struct old_timex32 __user *,utp)13058dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
13064d5f007eSArnd Bergmann struct old_timex32 __user *, utp)
13073a4d44b6SAl Viro {
1308ead25417SDeepa Dinamani struct __kernel_timex ktx;
13093a4d44b6SAl Viro int err;
13103a4d44b6SAl Viro
13114d5f007eSArnd Bergmann err = get_old_timex32(&ktx, utp);
13123a4d44b6SAl Viro if (err)
13133a4d44b6SAl Viro return err;
13143a4d44b6SAl Viro
13151a596398SArnd Bergmann err = do_clock_adjtime(which_clock, &ktx);
13163a4d44b6SAl Viro
13172d036dfaSChen Jun if (err >= 0 && put_old_timex32(utp, &ktx))
13182d036dfaSChen Jun return -EFAULT;
13193a4d44b6SAl Viro
13203a4d44b6SAl Viro return err;
13213a4d44b6SAl Viro }
13223a4d44b6SAl Viro
SYSCALL_DEFINE2(clock_getres_time32,clockid_t,which_clock,struct old_timespec32 __user *,tp)13238dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
13249afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp)
13255cee9645SThomas Gleixner {
1326d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
13275c499410SDeepa Dinamani struct timespec64 ts;
13285c499410SDeepa Dinamani int err;
13295cee9645SThomas Gleixner
13305cee9645SThomas Gleixner if (!kc)
13315cee9645SThomas Gleixner return -EINVAL;
13325cee9645SThomas Gleixner
13335c499410SDeepa Dinamani err = kc->clock_getres(which_clock, &ts);
13349afc5eeeSArnd Bergmann if (!err && tp && put_old_timespec32(&ts, tp))
13355c499410SDeepa Dinamani return -EFAULT;
13365cee9645SThomas Gleixner
13375c499410SDeepa Dinamani return err;
13385cee9645SThomas Gleixner }
13395c499410SDeepa Dinamani
1340d822cdccSAl Viro #endif
13415cee9645SThomas Gleixner
13425cee9645SThomas Gleixner /*
1343640fe745SThomas Gleixner * sys_clock_nanosleep() for CLOCK_REALTIME and CLOCK_TAI
13445cee9645SThomas Gleixner */
common_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)13455cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags,
1346938e7cf2SThomas Gleixner const struct timespec64 *rqtp)
13475cee9645SThomas Gleixner {
1348ea2d1f7fSAndrei Vagin ktime_t texp = timespec64_to_ktime(*rqtp);
1349ea2d1f7fSAndrei Vagin
1350ea2d1f7fSAndrei Vagin return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
13515cee9645SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
13525cee9645SThomas Gleixner which_clock);
13535cee9645SThomas Gleixner }
13545cee9645SThomas Gleixner
1355640fe745SThomas Gleixner /*
1356640fe745SThomas Gleixner * sys_clock_nanosleep() for CLOCK_MONOTONIC and CLOCK_BOOTTIME
1357640fe745SThomas Gleixner *
1358640fe745SThomas Gleixner * Absolute nanosleeps for these clocks are time-namespace adjusted.
1359640fe745SThomas Gleixner */
common_nsleep_timens(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)13601f9b37bfSAndrei Vagin static int common_nsleep_timens(const clockid_t which_clock, int flags,
13611f9b37bfSAndrei Vagin const struct timespec64 *rqtp)
13621f9b37bfSAndrei Vagin {
13631f9b37bfSAndrei Vagin ktime_t texp = timespec64_to_ktime(*rqtp);
13641f9b37bfSAndrei Vagin
13651f9b37bfSAndrei Vagin if (flags & TIMER_ABSTIME)
13661f9b37bfSAndrei Vagin texp = timens_ktime_to_host(which_clock, texp);
13671f9b37bfSAndrei Vagin
13681f9b37bfSAndrei Vagin return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
13691f9b37bfSAndrei Vagin HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
13701f9b37bfSAndrei Vagin which_clock);
13711f9b37bfSAndrei Vagin }
13721f9b37bfSAndrei Vagin
SYSCALL_DEFINE4(clock_nanosleep,const clockid_t,which_clock,int,flags,const struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)13735cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
137401909974SDeepa Dinamani const struct __kernel_timespec __user *, rqtp,
137501909974SDeepa Dinamani struct __kernel_timespec __user *, rmtp)
13765cee9645SThomas Gleixner {
1377d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
1378c0edd7c9SDeepa Dinamani struct timespec64 t;
13795cee9645SThomas Gleixner
13805cee9645SThomas Gleixner if (!kc)
13815cee9645SThomas Gleixner return -EINVAL;
13825cee9645SThomas Gleixner if (!kc->nsleep)
138393cb8e20SHelge Deller return -EOPNOTSUPP;
13845cee9645SThomas Gleixner
1385c0edd7c9SDeepa Dinamani if (get_timespec64(&t, rqtp))
13865cee9645SThomas Gleixner return -EFAULT;
13875cee9645SThomas Gleixner
1388c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t))
13895cee9645SThomas Gleixner return -EINVAL;
139099e6c0e6SAl Viro if (flags & TIMER_ABSTIME)
139199e6c0e6SAl Viro rmtp = NULL;
13929f76d591SJann Horn current->restart_block.fn = do_no_restart_syscall;
1393edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
139499e6c0e6SAl Viro current->restart_block.nanosleep.rmtp = rmtp;
13955cee9645SThomas Gleixner
1396c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t);
13975cee9645SThomas Gleixner }
13985cee9645SThomas Gleixner
1399b5793b0dSDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
1400b5793b0dSDeepa Dinamani
SYSCALL_DEFINE4(clock_nanosleep_time32,clockid_t,which_clock,int,flags,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)14018dabe724SArnd Bergmann SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
14029afc5eeeSArnd Bergmann struct old_timespec32 __user *, rqtp,
14039afc5eeeSArnd Bergmann struct old_timespec32 __user *, rmtp)
14045cee9645SThomas Gleixner {
1405d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
1406c0edd7c9SDeepa Dinamani struct timespec64 t;
14075cee9645SThomas Gleixner
1408edbeda46SAl Viro if (!kc)
14095cee9645SThomas Gleixner return -EINVAL;
1410edbeda46SAl Viro if (!kc->nsleep)
141193cb8e20SHelge Deller return -EOPNOTSUPP;
14125cee9645SThomas Gleixner
14139afc5eeeSArnd Bergmann if (get_old_timespec32(&t, rqtp))
1414edbeda46SAl Viro return -EFAULT;
1415edbeda46SAl Viro
1416c0edd7c9SDeepa Dinamani if (!timespec64_valid(&t))
1417edbeda46SAl Viro return -EINVAL;
1418edbeda46SAl Viro if (flags & TIMER_ABSTIME)
1419edbeda46SAl Viro rmtp = NULL;
14209f76d591SJann Horn current->restart_block.fn = do_no_restart_syscall;
1421edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1422edbeda46SAl Viro current->restart_block.nanosleep.compat_rmtp = rmtp;
1423edbeda46SAl Viro
1424c0edd7c9SDeepa Dinamani return kc->nsleep(which_clock, flags, &t);
14255cee9645SThomas Gleixner }
1426b5793b0dSDeepa Dinamani
1427edbeda46SAl Viro #endif
14286631fa12SThomas Gleixner
14296631fa12SThomas Gleixner static const struct k_clock clock_realtime = {
14306631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res,
1431eaf80194SAndrei Vagin .clock_get_timespec = posix_get_realtime_timespec,
14329c71a2e8SAndrei Vagin .clock_get_ktime = posix_get_realtime_ktime,
14336631fa12SThomas Gleixner .clock_set = posix_clock_realtime_set,
14346631fa12SThomas Gleixner .clock_adj = posix_clock_realtime_adj,
14356631fa12SThomas Gleixner .nsleep = common_nsleep,
14366631fa12SThomas Gleixner .timer_create = common_timer_create,
14376631fa12SThomas Gleixner .timer_set = common_timer_set,
14386631fa12SThomas Gleixner .timer_get = common_timer_get,
14396631fa12SThomas Gleixner .timer_del = common_timer_del,
1440f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm,
144191d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward,
144291d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining,
1443eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1444ec8f954aSThomas Gleixner .timer_wait_running = common_timer_wait_running,
1445eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm,
14466631fa12SThomas Gleixner };
14476631fa12SThomas Gleixner
14486631fa12SThomas Gleixner static const struct k_clock clock_monotonic = {
14496631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res,
1450eaf80194SAndrei Vagin .clock_get_timespec = posix_get_monotonic_timespec,
14519c71a2e8SAndrei Vagin .clock_get_ktime = posix_get_monotonic_ktime,
14521f9b37bfSAndrei Vagin .nsleep = common_nsleep_timens,
14536631fa12SThomas Gleixner .timer_create = common_timer_create,
14546631fa12SThomas Gleixner .timer_set = common_timer_set,
14556631fa12SThomas Gleixner .timer_get = common_timer_get,
14566631fa12SThomas Gleixner .timer_del = common_timer_del,
1457f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm,
145891d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward,
145991d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining,
1460eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1461ec8f954aSThomas Gleixner .timer_wait_running = common_timer_wait_running,
1462eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm,
14636631fa12SThomas Gleixner };
14646631fa12SThomas Gleixner
14656631fa12SThomas Gleixner static const struct k_clock clock_monotonic_raw = {
14666631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res,
1467819a95feSAndrei Vagin .clock_get_timespec = posix_get_monotonic_raw,
14686631fa12SThomas Gleixner };
14696631fa12SThomas Gleixner
14706631fa12SThomas Gleixner static const struct k_clock clock_realtime_coarse = {
14716631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res,
1472819a95feSAndrei Vagin .clock_get_timespec = posix_get_realtime_coarse,
14736631fa12SThomas Gleixner };
14746631fa12SThomas Gleixner
14756631fa12SThomas Gleixner static const struct k_clock clock_monotonic_coarse = {
14766631fa12SThomas Gleixner .clock_getres = posix_get_coarse_res,
1477819a95feSAndrei Vagin .clock_get_timespec = posix_get_monotonic_coarse,
14786631fa12SThomas Gleixner };
14796631fa12SThomas Gleixner
14806631fa12SThomas Gleixner static const struct k_clock clock_tai = {
14816631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res,
14829c71a2e8SAndrei Vagin .clock_get_ktime = posix_get_tai_ktime,
1483eaf80194SAndrei Vagin .clock_get_timespec = posix_get_tai_timespec,
14846631fa12SThomas Gleixner .nsleep = common_nsleep,
14856631fa12SThomas Gleixner .timer_create = common_timer_create,
14866631fa12SThomas Gleixner .timer_set = common_timer_set,
14876631fa12SThomas Gleixner .timer_get = common_timer_get,
14886631fa12SThomas Gleixner .timer_del = common_timer_del,
1489f37fb0aaSThomas Gleixner .timer_rearm = common_hrtimer_rearm,
149091d57baeSThomas Gleixner .timer_forward = common_hrtimer_forward,
149191d57baeSThomas Gleixner .timer_remaining = common_hrtimer_remaining,
1492eae1c4aeSThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1493ec8f954aSThomas Gleixner .timer_wait_running = common_timer_wait_running,
1494eae1c4aeSThomas Gleixner .timer_arm = common_hrtimer_arm,
14956631fa12SThomas Gleixner };
14966631fa12SThomas Gleixner
1497a3ed0e43SThomas Gleixner static const struct k_clock clock_boottime = {
14986631fa12SThomas Gleixner .clock_getres = posix_get_hrtimer_res,
14999c71a2e8SAndrei Vagin .clock_get_ktime = posix_get_boottime_ktime,
1500eaf80194SAndrei Vagin .clock_get_timespec = posix_get_boottime_timespec,
15011f9b37bfSAndrei Vagin .nsleep = common_nsleep_timens,
1502a3ed0e43SThomas Gleixner .timer_create = common_timer_create,
1503a3ed0e43SThomas Gleixner .timer_set = common_timer_set,
1504a3ed0e43SThomas Gleixner .timer_get = common_timer_get,
1505a3ed0e43SThomas Gleixner .timer_del = common_timer_del,
1506a3ed0e43SThomas Gleixner .timer_rearm = common_hrtimer_rearm,
1507a3ed0e43SThomas Gleixner .timer_forward = common_hrtimer_forward,
1508a3ed0e43SThomas Gleixner .timer_remaining = common_hrtimer_remaining,
1509a3ed0e43SThomas Gleixner .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1510ec8f954aSThomas Gleixner .timer_wait_running = common_timer_wait_running,
1511a3ed0e43SThomas Gleixner .timer_arm = common_hrtimer_arm,
15126631fa12SThomas Gleixner };
15136631fa12SThomas Gleixner
15146631fa12SThomas Gleixner static const struct k_clock * const posix_clocks[] = {
15156631fa12SThomas Gleixner [CLOCK_REALTIME] = &clock_realtime,
15166631fa12SThomas Gleixner [CLOCK_MONOTONIC] = &clock_monotonic,
15176631fa12SThomas Gleixner [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
15186631fa12SThomas Gleixner [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
15196631fa12SThomas Gleixner [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
15206631fa12SThomas Gleixner [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
15216631fa12SThomas Gleixner [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1522a3ed0e43SThomas Gleixner [CLOCK_BOOTTIME] = &clock_boottime,
15236631fa12SThomas Gleixner [CLOCK_REALTIME_ALARM] = &alarm_clock,
15246631fa12SThomas Gleixner [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
15256631fa12SThomas Gleixner [CLOCK_TAI] = &clock_tai,
15266631fa12SThomas Gleixner };
15276631fa12SThomas Gleixner
clockid_to_kclock(const clockid_t id)15286631fa12SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id)
15296631fa12SThomas Gleixner {
153019b558dbSThomas Gleixner clockid_t idx = id;
153119b558dbSThomas Gleixner
153219b558dbSThomas Gleixner if (id < 0) {
15336631fa12SThomas Gleixner return (id & CLOCKFD_MASK) == CLOCKFD ?
15346631fa12SThomas Gleixner &clock_posix_dynamic : &clock_posix_cpu;
153519b558dbSThomas Gleixner }
15366631fa12SThomas Gleixner
153719b558dbSThomas Gleixner if (id >= ARRAY_SIZE(posix_clocks))
15386631fa12SThomas Gleixner return NULL;
153919b558dbSThomas Gleixner
154019b558dbSThomas Gleixner return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
15416631fa12SThomas Gleixner }
1542