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
405cee9645SThomas Gleixner /*
415cee9645SThomas Gleixner * Timers are managed in a hash table for lockless lookup. The hash key is
425cee9645SThomas Gleixner * constructed from current::signal and the timer ID and the timer is
435cee9645SThomas Gleixner * matched against current::signal and the timer ID when walking the hash
445cee9645SThomas Gleixner * bucket list.
455cee9645SThomas Gleixner *
465cee9645SThomas Gleixner * This allows checkpoint/restore to reconstruct the exact timer IDs for
475cee9645SThomas Gleixner * a process.
485cee9645SThomas Gleixner */
495cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
505cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock);
515cee9645SThomas Gleixner
525cee9645SThomas Gleixner static const struct k_clock * const posix_clocks[];
535cee9645SThomas Gleixner static const struct k_clock *clockid_to_kclock(const clockid_t id);
545cee9645SThomas Gleixner static const struct k_clock clock_realtime, clock_monotonic;
556631fa12SThomas Gleixner
566631fa12SThomas Gleixner /* SIGEV_THREAD_ID cannot share a bit with the other SIGEV values. */
5767edab48SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
586631fa12SThomas 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
815cee9645SThomas Gleixner hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)) {
825cee9645SThomas Gleixner /* timer->it_signal can be set concurrently */
835cee9645SThomas 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;
1015cee9645SThomas Gleixner unsigned int cnt, id;
1025cee9645SThomas Gleixner
1035cee9645SThomas Gleixner /*
1045cee9645SThomas Gleixner * FIXME: Replace this by a per signal struct xarray once there is
1055cee9645SThomas Gleixner * a plan to handle the resulting CRIU regression gracefully.
1065cee9645SThomas Gleixner */
1075cee9645SThomas Gleixner for (cnt = 0; cnt <= INT_MAX; cnt++) {
1085cee9645SThomas Gleixner spin_lock(&hash_lock);
1095cee9645SThomas Gleixner id = sig->next_posix_timer_id;
1105cee9645SThomas Gleixner
1115cee9645SThomas Gleixner /* Write the next ID back. Clamp it to the positive space */
1125cee9645SThomas Gleixner sig->next_posix_timer_id = (id + 1) & INT_MAX;
1135cee9645SThomas Gleixner
1145cee9645SThomas Gleixner head = &posix_timers_hashtable[hash(sig, id)];
1155cee9645SThomas Gleixner if (!__posix_timers_find(head, sig, id)) {
1165cee9645SThomas Gleixner hlist_add_head_rcu(&timer->t_hash, head);
1175cee9645SThomas Gleixner spin_unlock(&hash_lock);
1185cee9645SThomas Gleixner return id;
1195cee9645SThomas Gleixner }
1205cee9645SThomas Gleixner spin_unlock(&hash_lock);
1215cee9645SThomas Gleixner }
1225cee9645SThomas Gleixner /* POSIX return code when no timer ID could be allocated */
1235cee9645SThomas Gleixner return -EAGAIN;
1245fb1c2a5SAmol Grover }
1255fb1c2a5SAmol Grover
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)1315cee9645SThomas Gleixner static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
1325cee9645SThomas Gleixner {
1335cee9645SThomas Gleixner ktime_get_real_ts64(tp);
1345cee9645SThomas Gleixner return 0;
1355cee9645SThomas Gleixner }
1365cee9645SThomas Gleixner
posix_get_realtime_ktime(clockid_t which_clock)1375cee9645SThomas Gleixner static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
1385cee9645SThomas Gleixner {
1395cee9645SThomas Gleixner return ktime_get_real();
1405cee9645SThomas Gleixner }
1415cee9645SThomas Gleixner
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,
1435cee9645SThomas Gleixner const struct timespec64 *tp)
1445cee9645SThomas Gleixner {
1455cee9645SThomas Gleixner 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,
1495cee9645SThomas Gleixner 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)1545cee9645SThomas Gleixner static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
1555cee9645SThomas Gleixner {
1565cee9645SThomas Gleixner ktime_get_ts64(tp);
1575cee9645SThomas Gleixner timens_add_monotonic(tp);
1585cee9645SThomas Gleixner return 0;
1595cee9645SThomas Gleixner }
1605cee9645SThomas Gleixner
posix_get_monotonic_ktime(clockid_t which_clock)1615cee9645SThomas Gleixner static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
1625cee9645SThomas Gleixner {
1635cee9645SThomas Gleixner return ktime_get();
1645cee9645SThomas Gleixner }
1655cee9645SThomas Gleixner
posix_get_monotonic_raw(clockid_t which_clock,struct timespec64 * tp)1665cee9645SThomas Gleixner static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
1675cee9645SThomas Gleixner {
1685cee9645SThomas Gleixner ktime_get_raw_ts64(tp);
1695cee9645SThomas Gleixner timens_add_monotonic(tp);
170eaf80194SAndrei Vagin return 0;
1715cee9645SThomas Gleixner }
1723c9c12f4SDeepa Dinamani
posix_get_realtime_coarse(clockid_t which_clock,struct timespec64 * tp)1735cee9645SThomas Gleixner static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
1745cee9645SThomas Gleixner {
1755cee9645SThomas Gleixner ktime_get_coarse_real_ts64(tp);
1769c71a2e8SAndrei Vagin return 0;
1779c71a2e8SAndrei Vagin }
1789c71a2e8SAndrei Vagin
posix_get_monotonic_coarse(clockid_t which_clock,struct timespec64 * tp)1799c71a2e8SAndrei Vagin static int posix_get_monotonic_coarse(clockid_t which_clock,
1809c71a2e8SAndrei Vagin struct timespec64 *tp)
1815cee9645SThomas Gleixner {
1825cee9645SThomas Gleixner ktime_get_coarse_ts64(tp);
1830fe6afe3SDeepa Dinamani timens_add_monotonic(tp);
1845cee9645SThomas Gleixner return 0;
1850fe6afe3SDeepa Dinamani }
1865cee9645SThomas Gleixner
posix_get_coarse_res(const clockid_t which_clock,struct timespec64 * tp)1875cee9645SThomas Gleixner static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
1885cee9645SThomas Gleixner {
189ead25417SDeepa 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)1935cee9645SThomas Gleixner static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
1945cee9645SThomas Gleixner {
1955cee9645SThomas Gleixner ktime_get_boottime_ts64(tp);
1965cee9645SThomas Gleixner timens_add_boottime(tp);
197eaf80194SAndrei Vagin return 0;
1985cee9645SThomas Gleixner }
1993c9c12f4SDeepa Dinamani
posix_get_boottime_ktime(const clockid_t which_clock)2005a590f35SAndrei Vagin static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
2015cee9645SThomas Gleixner {
2025cee9645SThomas Gleixner return ktime_get_boottime();
2035cee9645SThomas Gleixner }
2049c71a2e8SAndrei Vagin
posix_get_tai_timespec(clockid_t which_clock,struct timespec64 * tp)2059c71a2e8SAndrei Vagin static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
2069c71a2e8SAndrei Vagin {
2079c71a2e8SAndrei Vagin ktime_get_clocktai_ts64(tp);
2089c71a2e8SAndrei Vagin return 0;
2095cee9645SThomas Gleixner }
2105cee9645SThomas Gleixner
posix_get_tai_ktime(clockid_t which_clock)2115cee9645SThomas Gleixner static ktime_t posix_get_tai_ktime(clockid_t which_clock)
2123c9c12f4SDeepa Dinamani {
2135cee9645SThomas Gleixner return ktime_get_clocktai();
21458a10456SArnd Bergmann }
2155a590f35SAndrei Vagin
posix_get_hrtimer_res(clockid_t which_clock,struct timespec64 * tp)2165cee9645SThomas Gleixner static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
2175cee9645SThomas Gleixner {
2185cee9645SThomas Gleixner tp->tv_sec = 0;
2195cee9645SThomas Gleixner tp->tv_nsec = hrtimer_resolution;
2203c9c12f4SDeepa Dinamani return 0;
2215cee9645SThomas Gleixner }
22258a10456SArnd Bergmann
init_posix_timers(void)2235cee9645SThomas Gleixner static __init int init_posix_timers(void)
2245cee9645SThomas Gleixner {
2255cee9645SThomas Gleixner posix_timers_cache = kmem_cache_create("posix_timers_cache",
2265cee9645SThomas Gleixner sizeof(struct k_itimer), 0,
2273c9c12f4SDeepa Dinamani SLAB_PANIC | SLAB_ACCOUNT, NULL);
2285cee9645SThomas Gleixner return 0;
22958a10456SArnd Bergmann }
2305a590f35SAndrei Vagin __initcall(init_posix_timers);
2315cee9645SThomas Gleixner
2325cee9645SThomas Gleixner /*
2335cee9645SThomas Gleixner * The siginfo si_overrun field and the return value of timer_getoverrun(2)
234d2e3e0caSDeepa Dinamani * are of type int. Clamp the overrun value to INT_MAX
2355cee9645SThomas Gleixner */
timer_overrun_to_int(struct k_itimer * timr,int baseval)236d2e3e0caSDeepa Dinamani static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
2375cee9645SThomas Gleixner {
2385cee9645SThomas Gleixner s64 sum = timr->it_overrun_last + (s64)baseval;
2395cee9645SThomas Gleixner
240eaf80194SAndrei Vagin return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
2415cee9645SThomas Gleixner }
24258a10456SArnd Bergmann
common_hrtimer_rearm(struct k_itimer * timr)2435a590f35SAndrei Vagin static void common_hrtimer_rearm(struct k_itimer *timr)
2445cee9645SThomas Gleixner {
2455cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
2465cee9645SThomas Gleixner
2479c71a2e8SAndrei Vagin timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
2489c71a2e8SAndrei Vagin timr->it_interval);
2499c71a2e8SAndrei Vagin hrtimer_restart(timer);
2509c71a2e8SAndrei Vagin }
2519c71a2e8SAndrei Vagin
252eaf80194SAndrei Vagin /*
25372199320SThomas Gleixner * This function is called from the signal delivery code if
25458a10456SArnd Bergmann * info->si_sys_private is not zero, which indicates that the timer has to
25572199320SThomas Gleixner * be rearmed. Restart the timer and update info::si_overrun.
25672199320SThomas Gleixner */
posixtimer_rearm(struct kernel_siginfo * info)25772199320SThomas Gleixner void posixtimer_rearm(struct kernel_siginfo *info)
2589c71a2e8SAndrei Vagin {
2599c71a2e8SAndrei Vagin struct k_itimer *timr;
2609c71a2e8SAndrei Vagin unsigned long flags;
2619c71a2e8SAndrei Vagin
2629c71a2e8SAndrei Vagin timr = lock_timer(info->si_tid, &flags);
263d2e3e0caSDeepa Dinamani if (!timr)
264056a3cacSThomas Gleixner return;
265056a3cacSThomas Gleixner
266056a3cacSThomas Gleixner if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
267056a3cacSThomas Gleixner timr->kclock->timer_rearm(timr);
268056a3cacSThomas Gleixner
269056a3cacSThomas Gleixner timr->it_active = 1;
270d3ba5a9aSChristoph Hellwig timr->it_overrun_last = timr->it_overrun;
271d3ba5a9aSChristoph Hellwig timr->it_overrun = -1LL;
272d3ba5a9aSChristoph Hellwig ++timr->it_requeue_pending;
273d3ba5a9aSChristoph Hellwig
274d3ba5a9aSChristoph Hellwig info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
2755cee9645SThomas Gleixner }
276c509723eSVasily Averin
277c509723eSVasily Averin 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 {
28278c9c4dfSThomas Gleixner enum pid_type type;
28378c9c4dfSThomas Gleixner int ret;
28478c9c4dfSThomas Gleixner /*
28578c9c4dfSThomas Gleixner * FIXME: if ->sigq is queued we can race with
28678c9c4dfSThomas Gleixner * dequeue_signal()->posixtimer_rearm().
28778c9c4dfSThomas Gleixner *
28878c9c4dfSThomas Gleixner * If dequeue_signal() sees the "right" value of
28978c9c4dfSThomas Gleixner * si_sys_private it calls posixtimer_rearm().
29078c9c4dfSThomas Gleixner * We re-queue ->sigq and drop ->it_lock().
29178c9c4dfSThomas Gleixner * posixtimer_rearm() locks the timer
29278c9c4dfSThomas Gleixner * and re-schedules it while ->sigq is pending.
293f37fb0aaSThomas Gleixner * Not really bad, but not that we want.
2945cee9645SThomas Gleixner */
2955cee9645SThomas Gleixner timr->sigq->info.si_sys_private = si_private;
2965cee9645SThomas Gleixner
29778c9c4dfSThomas Gleixner type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
29880105cd0SThomas Gleixner 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 /*
3045cee9645SThomas Gleixner * This function gets called when a POSIX.1b interval timer expires from
3055cee9645SThomas Gleixner * the HRTIMER interrupt (soft interrupt on RT kernels).
3065cee9645SThomas Gleixner *
3075cee9645SThomas Gleixner * Handles CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME and CLOCK_TAI
3085cee9645SThomas 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 {
3125cee9645SThomas Gleixner enum hrtimer_restart ret = HRTIMER_NORESTART;
313ae7795bcSEric W. Biederman 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);
319af888d67SThomas Gleixner
320af888d67SThomas Gleixner timr->it_active = 0;
3215cee9645SThomas Gleixner if (timr->it_interval != 0)
3220e334db6SThomas Gleixner si_private = ++timr->it_requeue_pending;
323f37fb0aaSThomas Gleixner
3245cee9645SThomas Gleixner if (posix_timer_event(timr, si_private)) {
32521e55c1fSThomas Gleixner /*
326af888d67SThomas Gleixner * The signal was not queued due to SIG_IGN. As a
32778c9c4dfSThomas Gleixner * consequence the timer is not going to be rearmed from
328af888d67SThomas Gleixner * the signal delivery path. But as a real signal handler
329af888d67SThomas Gleixner * can be installed later the timer must be rearmed here.
33078c9c4dfSThomas Gleixner */
3315cee9645SThomas 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
33824122c7fSEric W. Biederman * change to the signal handling code.
3391dae37c7SColin Ian King *
3405cee9645SThomas Gleixner * For now let timers with an interval less than a
3415cee9645SThomas Gleixner * jiffie expire every jiffie and recheck for a
34296fe3b07SThomas Gleixner * valid signal handler.
3435cee9645SThomas Gleixner *
3445cee9645SThomas Gleixner * This avoids interrupt starvation in case of a
34596fe3b07SThomas Gleixner * very small interval, which would expire the
3465cee9645SThomas Gleixner * timer immediately again.
34796fe3b07SThomas Gleixner *
3485cee9645SThomas Gleixner * Moving now ahead of time by one jiffie tricks
3495cee9645SThomas Gleixner * hrtimer_forward() to expire the timer later,
3505cee9645SThomas Gleixner * while it still maintains the overrun accuracy
3515cee9645SThomas Gleixner * for the price of a slight inconsistency in the
3525cee9645SThomas Gleixner * timer_gettime() case. This is at least better
35324122c7fSEric W. Biederman * than a timer storm.
35424122c7fSEric W. Biederman *
3555cee9645SThomas Gleixner * Only required when high resolution timers are
3565cee9645SThomas Gleixner * enabled as the periodic tick based timers are
3575cee9645SThomas Gleixner * automatically aligned to the next tick.
3585cee9645SThomas Gleixner */
3595cee9645SThomas Gleixner if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) {
3605cee9645SThomas Gleixner ktime_t kj = TICK_NSEC;
3615cee9645SThomas Gleixner
3625cee9645SThomas Gleixner if (timr->it_interval < kj)
3635cee9645SThomas Gleixner now = ktime_add(now, kj);
3645cee9645SThomas Gleixner }
3655cee9645SThomas Gleixner
3665cee9645SThomas Gleixner timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval);
3675cee9645SThomas Gleixner ret = HRTIMER_RESTART;
3685cee9645SThomas Gleixner ++timr->it_requeue_pending;
3695cee9645SThomas Gleixner timr->it_active = 1;
3705cee9645SThomas Gleixner }
3715cee9645SThomas Gleixner }
3725cee9645SThomas Gleixner
3735cee9645SThomas Gleixner unlock_timer(timr, flags);
3745cee9645SThomas Gleixner return ret;
3755cee9645SThomas Gleixner }
37621e55c1fSThomas Gleixner
good_sigevent(sigevent_t * event)37780105cd0SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event)
3785cee9645SThomas Gleixner {
3795cee9645SThomas Gleixner struct pid *pid = task_tgid(current);
3805cee9645SThomas Gleixner struct task_struct *rtn;
3815cee9645SThomas Gleixner
3825cee9645SThomas Gleixner switch (event->sigev_notify) {
3835cee9645SThomas Gleixner case SIGEV_SIGNAL | SIGEV_THREAD_ID:
3845cee9645SThomas Gleixner pid = find_vpid(event->sigev_notify_thread_id);
3855cee9645SThomas Gleixner rtn = pid_task(pid, PIDTYPE_PID);
38680105cd0SThomas Gleixner if (!rtn || !same_thread_group(rtn, current))
3875cee9645SThomas Gleixner return NULL;
3885cee9645SThomas Gleixner fallthrough;
3895cee9645SThomas Gleixner case SIGEV_SIGNAL:
3905cee9645SThomas Gleixner case SIGEV_THREAD:
3915cee9645SThomas Gleixner if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
3925cee9645SThomas Gleixner return NULL;
3935cee9645SThomas Gleixner fallthrough;
3945cee9645SThomas Gleixner case SIGEV_NONE:
3955cee9645SThomas Gleixner return pid;
3965cee9645SThomas Gleixner default:
3975cee9645SThomas Gleixner return NULL;
3985cee9645SThomas Gleixner }
3995cee9645SThomas Gleixner }
4005cee9645SThomas Gleixner
alloc_posix_timer(void)4015cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void)
4025cee9645SThomas Gleixner {
4035cee9645SThomas Gleixner struct k_itimer *tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
4045cee9645SThomas 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 }
4115cee9645SThomas Gleixner clear_siginfo(&tmr->sigq->info);
4125cee9645SThomas Gleixner return tmr;
4138b0e1953SThomas Gleixner }
4145cee9645SThomas Gleixner
k_itimer_rcu_free(struct rcu_head * head)41580105cd0SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head)
4165cee9645SThomas Gleixner {
4175cee9645SThomas Gleixner struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
4185cee9645SThomas Gleixner
41978c9c4dfSThomas Gleixner kmem_cache_free(posix_timers_cache, tmr);
42080105cd0SThomas Gleixner }
4215cee9645SThomas Gleixner
posix_timer_free(struct k_itimer * tmr)4225cee9645SThomas Gleixner static void posix_timer_free(struct k_itimer *tmr)
42321e55c1fSThomas Gleixner {
4245cee9645SThomas Gleixner put_pid(tmr->it_pid);
4255cee9645SThomas Gleixner sigqueue_free(tmr->sigq);
4265cee9645SThomas Gleixner call_rcu(&tmr->rcu, k_itimer_rcu_free);
4275cee9645SThomas Gleixner }
4285cee9645SThomas Gleixner
posix_timer_unhash_and_free(struct k_itimer * tmr)4295cee9645SThomas Gleixner static void posix_timer_unhash_and_free(struct k_itimer *tmr)
4305cee9645SThomas Gleixner {
4315cee9645SThomas Gleixner spin_lock(&hash_lock);
4325cee9645SThomas Gleixner hlist_del_rcu(&tmr->t_hash);
4332118e1f5SEric W. Biederman spin_unlock(&hash_lock);
4342118e1f5SEric W. Biederman posix_timer_free(tmr);
4355cee9645SThomas Gleixner }
436cef31d9aSThomas Gleixner
common_timer_create(struct k_itimer * new_timer)437cef31d9aSThomas Gleixner static int common_timer_create(struct k_itimer *new_timer)
4382118e1f5SEric W. Biederman {
4392118e1f5SEric W. Biederman hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
440cef31d9aSThomas Gleixner return 0;
4415cee9645SThomas Gleixner }
442df561f66SGustavo A. R. Silva
443cef31d9aSThomas Gleixner /* Create a POSIX.1b interval timer. */
do_timer_create(clockid_t which_clock,struct sigevent * event,timer_t __user * created_timer_id)444cef31d9aSThomas Gleixner static int do_timer_create(clockid_t which_clock, struct sigevent *event,
445cef31d9aSThomas Gleixner timer_t __user *created_timer_id)
4465cee9645SThomas Gleixner {
447df561f66SGustavo A. R. Silva const struct k_clock *kc = clockid_to_kclock(which_clock);
448cef31d9aSThomas Gleixner struct k_itimer *new_timer;
4492118e1f5SEric W. Biederman int error, new_timer_id;
450cef31d9aSThomas Gleixner
451cef31d9aSThomas Gleixner if (!kc)
452cef31d9aSThomas 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);
4615cee9645SThomas Gleixner
4625cee9645SThomas Gleixner /*
4635cee9645SThomas Gleixner * Add the timer to the hash table. The timer is not yet valid
4645cee9645SThomas Gleixner * because new_timer::it_signal is still NULL. The timer id is also
4653b10db2bSEric W. Biederman * not yet visible to user space.
4665cee9645SThomas Gleixner */
4675cee9645SThomas Gleixner new_timer_id = posix_timer_add(new_timer);
4685cee9645SThomas Gleixner if (new_timer_id < 0) {
4695cee9645SThomas Gleixner posix_timer_free(new_timer);
4705cee9645SThomas Gleixner return new_timer_id;
4715d99b32aSSebastian Andrzej Siewior }
4725cee9645SThomas Gleixner
4735cee9645SThomas Gleixner new_timer->it_id = (timer_t) new_timer_id;
4745cee9645SThomas Gleixner new_timer->it_clock = which_clock;
4755cee9645SThomas Gleixner new_timer->kclock = kc;
4765cee9645SThomas Gleixner new_timer->it_overrun = -1LL;
4775cee9645SThomas Gleixner
4785cee9645SThomas Gleixner if (event) {
4795cee9645SThomas Gleixner rcu_read_lock();
4805cee9645SThomas Gleixner 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 }
4865cee9645SThomas Gleixner new_timer->it_sigev_notify = event->sigev_notify;
4875cee9645SThomas Gleixner new_timer->sigq->info.si_signo = event->sigev_signo;
4885d99b32aSSebastian Andrzej Siewior new_timer->sigq->info.si_value = event->sigev_value;
4895cee9645SThomas Gleixner } else {
4905cee9645SThomas Gleixner new_timer->it_sigev_notify = SIGEV_SIGNAL;
4915cee9645SThomas Gleixner new_timer->sigq->info.si_signo = SIGALRM;
4925cee9645SThomas Gleixner memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
4935cee9645SThomas Gleixner 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;
4982482097cSAl Viro new_timer->sigq->info.si_code = SI_TIMER;
4992482097cSAl Viro
5005cee9645SThomas Gleixner if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id))) {
501d3ba5a9aSChristoph Hellwig error = -EFAULT;
5025cee9645SThomas Gleixner goto out;
5035cee9645SThomas Gleixner }
5045cee9645SThomas Gleixner /*
5055cee9645SThomas Gleixner * After succesful copy out, the timer ID is visible to user space
5065cee9645SThomas Gleixner * now but not yet valid because new_timer::signal is still NULL.
5075cee9645SThomas Gleixner *
5085cee9645SThomas Gleixner * Complete the initialization with the clock specific create
5095cee9645SThomas Gleixner * callback.
5105cee9645SThomas 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);
5165cee9645SThomas Gleixner /* This makes the timer valid in the hash table */
5175cee9645SThomas 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 /*
5215cee9645SThomas Gleixner * After unlocking sighand::siglock @new_timer is subject to
5225cee9645SThomas Gleixner * concurrent removal and cannot be touched anymore
5235cee9645SThomas Gleixner */
5245cee9645SThomas Gleixner return 0;
525d97bb75dSThomas Gleixner out:
52678c9c4dfSThomas Gleixner posix_timer_unhash_and_free(new_timer);
5275cee9645SThomas Gleixner return error;
5282482097cSAl Viro }
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,
5315cee9645SThomas Gleixner struct sigevent __user *, timer_event_spec,
5325cee9645SThomas Gleixner timer_t __user *, created_timer_id)
5335cee9645SThomas Gleixner {
5345cee9645SThomas Gleixner if (timer_event_spec) {
5355cee9645SThomas Gleixner sigevent_t event;
5362482097cSAl Viro
5372482097cSAl Viro if (copy_from_user(&event, timer_event_spec, sizeof (event)))
5382482097cSAl Viro return -EFAULT;
5395cee9645SThomas Gleixner 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
5445cee9645SThomas Gleixner #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)5455cee9645SThomas Gleixner COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
5465cee9645SThomas Gleixner struct compat_sigevent __user *, timer_event_spec,
5475cee9645SThomas Gleixner timer_t __user *, created_timer_id)
5485cee9645SThomas Gleixner {
5495cee9645SThomas Gleixner if (timer_event_spec) {
5505cee9645SThomas Gleixner sigevent_t event;
5515cee9645SThomas Gleixner
5525cee9645SThomas Gleixner if (get_compat_sigevent(&event, timer_event_spec))
5535cee9645SThomas Gleixner return -EFAULT;
5545cee9645SThomas Gleixner return do_timer_create(which_clock, &event, created_timer_id);
5555cee9645SThomas Gleixner }
5565cee9645SThomas Gleixner return do_timer_create(which_clock, NULL, created_timer_id);
5575cee9645SThomas Gleixner }
5585cee9645SThomas Gleixner #endif
5595cee9645SThomas Gleixner
__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
5715cee9645SThomas Gleixner /*
5725cee9645SThomas Gleixner * The hash lookup and the timers are RCU protected.
5735cee9645SThomas Gleixner *
5745cee9645SThomas Gleixner * Timers are added to the hash in invalid state where
5755cee9645SThomas Gleixner * timr::it_signal == NULL. timer::it_signal is only set after the
5765cee9645SThomas Gleixner * rest of the initialization succeeded.
5772482097cSAl Viro *
5782482097cSAl Viro * Timer destruction happens in steps:
5792482097cSAl Viro * 1) Set timr::it_signal to NULL with timr::it_lock held
5802482097cSAl Viro * 2) Release timr::it_lock
5812482097cSAl Viro * 3) Remove from the hash under hash_lock
5822482097cSAl Viro * 4) Call RCU for removal after the grace period
5832482097cSAl Viro *
5842482097cSAl Viro * Holding rcu_read_lock() accross the lookup ensures that
5852482097cSAl Viro * the timer cannot be freed.
5862482097cSAl Viro *
5872482097cSAl Viro * The lookup validates locklessly that timr::it_signal ==
5882482097cSAl Viro * current::it_signal and timr::it_id == @timer_id. timr::it_id
5892482097cSAl Viro * can't change, but timr::it_signal becomes NULL during
5902482097cSAl Viro * destruction.
5912482097cSAl Viro */
5922482097cSAl Viro rcu_read_lock();
5932482097cSAl Viro timr = posix_timer_by_id(timer_id);
5942482097cSAl Viro if (timr) {
5952482097cSAl Viro spin_lock_irqsave(&timr->it_lock, *flags);
5962482097cSAl Viro /*
5972482097cSAl Viro * Validate under timr::it_lock that timr::it_signal is
5982482097cSAl Viro * still valid. Pairs with #1 above.
5992482097cSAl Viro */
6002482097cSAl Viro if (timr->it_signal == current->signal) {
6012482097cSAl Viro rcu_read_unlock();
6022482097cSAl Viro return timr;
6032482097cSAl Viro }
6042482097cSAl Viro spin_unlock_irqrestore(&timr->it_lock, *flags);
6052482097cSAl Viro }
6062482097cSAl Viro rcu_read_unlock();
6075cee9645SThomas Gleixner
6085cee9645SThomas Gleixner return NULL;
6095cee9645SThomas Gleixner }
6105cee9645SThomas Gleixner
common_hrtimer_remaining(struct k_itimer * timr,ktime_t now)6115cee9645SThomas Gleixner static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
6125cee9645SThomas Gleixner {
6135cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
6145cee9645SThomas Gleixner
6155cee9645SThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, now);
6165cee9645SThomas Gleixner }
6175cee9645SThomas Gleixner
common_hrtimer_forward(struct k_itimer * timr,ktime_t now)6185cee9645SThomas Gleixner static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
6195cee9645SThomas Gleixner {
6205cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
6215cee9645SThomas Gleixner
6225cee9645SThomas Gleixner return hrtimer_forward(timer, now, timr->it_interval);
6235cee9645SThomas Gleixner }
6245cee9645SThomas Gleixner
6255cee9645SThomas Gleixner /*
6265cee9645SThomas Gleixner * Get the time remaining on a POSIX.1b interval timer.
6275cee9645SThomas Gleixner *
6285cee9645SThomas Gleixner * Two issues to handle here:
6295cee9645SThomas Gleixner *
6305cee9645SThomas Gleixner * 1) The timer has a requeue pending. The return value must appear as
6315cee9645SThomas Gleixner * if the timer has been requeued right now.
6325cee9645SThomas Gleixner *
6335cee9645SThomas Gleixner * 2) The timer is a SIGEV_NONE timer. These timers are never enqueued
6345cee9645SThomas Gleixner * into the hrtimer queue and therefore never expired. Emulate expiry
6355cee9645SThomas Gleixner * here taking #1 into account.
6365cee9645SThomas Gleixner */
common_timer_get(struct k_itimer * timr,struct itimerspec64 * cur_setting)6375cee9645SThomas Gleixner void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
6385cee9645SThomas Gleixner {
6395cee9645SThomas Gleixner const struct k_clock *kc = timr->kclock;
64091d57baeSThomas Gleixner ktime_t now, remaining, iv;
64191d57baeSThomas Gleixner bool sig_none;
64291d57baeSThomas Gleixner
64391d57baeSThomas Gleixner sig_none = timr->it_sigev_notify == SIGEV_NONE;
64491d57baeSThomas Gleixner iv = timr->it_interval;
64591d57baeSThomas Gleixner
64691d57baeSThomas Gleixner /* interval timer ? */
6476fec64e1SThomas Gleixner if (iv) {
64891d57baeSThomas Gleixner cur_setting->it_interval = ktime_to_timespec64(iv);
64991d57baeSThomas Gleixner } else if (!timr->it_active) {
65091d57baeSThomas Gleixner /*
6516fec64e1SThomas Gleixner * SIGEV_NONE oneshot timers are never queued and therefore
65291d57baeSThomas Gleixner * timr->it_active is always false. The check below
65391d57baeSThomas Gleixner * vs. remaining time will handle this case.
6545cee9645SThomas Gleixner *
6555cee9645SThomas Gleixner * For all other timers there is nothing to update here, so
6565cee9645SThomas Gleixner * return.
6575cee9645SThomas Gleixner */
6585cee9645SThomas Gleixner if (!sig_none)
6595cee9645SThomas Gleixner return;
6605cee9645SThomas Gleixner }
6615cee9645SThomas Gleixner
6625cee9645SThomas Gleixner now = kc->clock_get_ktime(timr->it_clock);
6635cee9645SThomas Gleixner
6645cee9645SThomas Gleixner /*
6655cee9645SThomas Gleixner * If this is an interval timer and either has requeue pending or
6665cee9645SThomas Gleixner * is a SIGEV_NONE timer move the expiry time forward by intervals,
6675cee9645SThomas Gleixner * so expiry is > now.
6685cee9645SThomas Gleixner */
6695cee9645SThomas Gleixner if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
670f2c45807SThomas Gleixner timr->it_overrun += kc->timer_forward(timr, now);
6715cee9645SThomas Gleixner
67291d57baeSThomas Gleixner remaining = kc->timer_remaining(timr, now);
6735cee9645SThomas Gleixner /*
67491d57baeSThomas Gleixner * As @now is retrieved before a possible timer_forward() and
6755cee9645SThomas Gleixner * cannot be reevaluated by the compiler @remaining is based on the
676cef31d9aSThomas Gleixner * same @now value. Therefore @remaining is consistent vs. @now.
67780105cd0SThomas Gleixner *
6785cee9645SThomas Gleixner * Consequently all interval timers, i.e. @iv > 0, cannot have a
6795cee9645SThomas Gleixner * remaining time <= 0 because timer_forward() guarantees to move
68091d57baeSThomas Gleixner * them forward so that the next timer expiry is > @now.
6815f252b32SDeepa Dinamani */
68291d57baeSThomas Gleixner if (remaining <= 0) {
68391d57baeSThomas Gleixner /*
68491d57baeSThomas Gleixner * A single shot SIGEV_NONE timer must return 0, when it is
68591d57baeSThomas Gleixner * expired! Timers which have a real signal delivery mode
68691d57baeSThomas Gleixner * must return a remaining time greater than 0 because the
68791d57baeSThomas Gleixner * signal has not yet been delivered.
6885cee9645SThomas Gleixner */
68991d57baeSThomas Gleixner if (!sig_none)
6905cee9645SThomas Gleixner cur_setting->it_value.tv_nsec = 1;
691198fa445SAndrei Vagin } else {
6925cee9645SThomas Gleixner cur_setting->it_value = ktime_to_timespec64(remaining);
69391d57baeSThomas Gleixner }
69491d57baeSThomas Gleixner }
69591d57baeSThomas Gleixner
do_timer_gettime(timer_t timer_id,struct itimerspec64 * setting)69691d57baeSThomas Gleixner static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
69791d57baeSThomas Gleixner {
69878c9c4dfSThomas Gleixner const struct k_clock *kc;
69991d57baeSThomas Gleixner struct k_itimer *timr;
70091d57baeSThomas Gleixner unsigned long flags;
7015cee9645SThomas Gleixner int ret = 0;
7022456e855SThomas Gleixner
7035cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags);
7045cee9645SThomas Gleixner if (!timr)
7055cee9645SThomas Gleixner return -EINVAL;
7065cee9645SThomas Gleixner
70791d57baeSThomas Gleixner memset(setting, 0, sizeof(*setting));
7085cee9645SThomas Gleixner kc = timr->kclock;
70991d57baeSThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_get))
7105f252b32SDeepa Dinamani ret = -EINVAL;
7115cee9645SThomas Gleixner else
71291d57baeSThomas Gleixner kc->timer_get(timr, setting);
7135cee9645SThomas Gleixner
7145cee9645SThomas Gleixner unlock_timer(timr, flags);
715b0dc1242SAl Viro return ret;
7165cee9645SThomas Gleixner }
7175cee9645SThomas Gleixner
718d3ba5a9aSChristoph Hellwig /* Get the time remaining on a POSIX.1b interval timer. */
SYSCALL_DEFINE2(timer_gettime,timer_t,timer_id,struct __kernel_itimerspec __user *,setting)7195cee9645SThomas Gleixner SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
7205cee9645SThomas Gleixner struct __kernel_itimerspec __user *, setting)
7215cee9645SThomas Gleixner {
7225cee9645SThomas Gleixner struct itimerspec64 cur_setting;
7235cee9645SThomas Gleixner
7245cee9645SThomas Gleixner int ret = do_timer_gettime(timer_id, &cur_setting);
7255cee9645SThomas Gleixner if (!ret) {
726b0dc1242SAl Viro if (put_itimerspec64(&cur_setting, setting))
727d97bb75dSThomas Gleixner ret = -EFAULT;
7285cee9645SThomas Gleixner }
7295cee9645SThomas Gleixner return ret;
7305cee9645SThomas Gleixner }
731b0dc1242SAl Viro
7325cee9645SThomas Gleixner #ifdef CONFIG_COMPAT_32BIT_TIME
7335cee9645SThomas Gleixner
SYSCALL_DEFINE2(timer_gettime32,timer_t,timer_id,struct old_itimerspec32 __user *,setting)7345cee9645SThomas Gleixner SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
7355cee9645SThomas Gleixner struct old_itimerspec32 __user *, setting)
7365cee9645SThomas Gleixner {
737b0dc1242SAl Viro struct itimerspec64 cur_setting;
738b0dc1242SAl Viro
7396ff84735SDeepa Dinamani int ret = do_timer_gettime(timer_id, &cur_setting);
740b0dc1242SAl Viro if (!ret) {
741725816e8SDeepa Dinamani if (put_old_itimerspec32(&cur_setting, setting))
742b0dc1242SAl Viro ret = -EFAULT;
743725816e8SDeepa Dinamani }
744b0dc1242SAl Viro return ret;
745725816e8SDeepa Dinamani }
746b0dc1242SAl Viro
747b0dc1242SAl Viro #endif
748b0dc1242SAl Viro
749b0dc1242SAl Viro /**
750b0dc1242SAl Viro * sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
7516ff84735SDeepa Dinamani * @timer_id: The timer ID which identifies the timer
7526ff84735SDeepa Dinamani *
7538dabe724SArnd Bergmann * The "overrun count" of a timer is one plus the number of expiration
7549afc5eeeSArnd Bergmann * intervals which have elapsed between the first expiry, which queues the
755b0dc1242SAl Viro * signal and the actual signal delivery. On signal delivery the "overrun
756725816e8SDeepa Dinamani * count" is calculated and cached, so it can be returned directly here.
757b0dc1242SAl Viro *
758725816e8SDeepa Dinamani * As this is relative to the last queued signal the returned overrun count
759b0dc1242SAl Viro * is meaningless outside of the signal delivery path and even there it
7609afc5eeeSArnd Bergmann * does not accurately reflect the current state when user space evaluates
761b0dc1242SAl Viro * it.
762b0dc1242SAl Viro *
763b0dc1242SAl Viro * Returns:
764b0dc1242SAl Viro * -EINVAL @timer_id is invalid
7656ff84735SDeepa Dinamani * 1..INT_MAX The number of overruns related to the last delivered signal
766b0dc1242SAl Viro */
SYSCALL_DEFINE1(timer_getoverrun,timer_t,timer_id)767b0dc1242SAl Viro SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
7685cee9645SThomas Gleixner {
7695cee9645SThomas Gleixner struct k_itimer *timr;
7705cee9645SThomas Gleixner unsigned long flags;
7715cee9645SThomas Gleixner int overrun;
7725cee9645SThomas Gleixner
7735cee9645SThomas Gleixner timr = lock_timer(timer_id, &flags);
77496fe3b07SThomas Gleixner if (!timr)
7755cee9645SThomas Gleixner return -EINVAL;
7765cee9645SThomas Gleixner
7775cee9645SThomas 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)7835cee9645SThomas Gleixner static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
7845cee9645SThomas Gleixner bool absolute, bool sigev_none)
7855cee9645SThomas Gleixner {
7865cee9645SThomas Gleixner struct hrtimer *timer = &timr->it.real.timer;
78778c9c4dfSThomas Gleixner enum hrtimer_mode mode;
7885cee9645SThomas Gleixner
7895cee9645SThomas Gleixner mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
7905cee9645SThomas Gleixner /*
7915cee9645SThomas Gleixner * Posix magic: Relative CLOCK_REALTIME timers are not affected by
7925cee9645SThomas Gleixner * clock modifications, so they become CLOCK_MONOTONIC based under the
793eae1c4aeSThomas Gleixner * hood. See hrtimer_init(). Update timr->kclock, so the generic
794eae1c4aeSThomas Gleixner * functions which use timr->kclock->clock_get_*() work.
7955cee9645SThomas Gleixner *
7965cee9645SThomas Gleixner * Note: it_clock stays unmodified, because the next timer_set() might
7975cee9645SThomas Gleixner * use ABSTIME, so it needs to switch back.
7985cee9645SThomas Gleixner */
799eae1c4aeSThomas Gleixner if (timr->it_clock == CLOCK_REALTIME)
80067edab48SThomas Gleixner timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
80167edab48SThomas Gleixner
80267edab48SThomas Gleixner hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
80367edab48SThomas Gleixner timr->it.real.timer.function = posix_timer_fn;
8049c71a2e8SAndrei Vagin
80567edab48SThomas Gleixner if (!absolute)
80667edab48SThomas Gleixner expires = ktime_add_safe(expires, timer->base->get_time());
80767edab48SThomas Gleixner hrtimer_set_expires(timer, expires);
80867edab48SThomas Gleixner
80967edab48SThomas Gleixner if (!sigev_none)
81067edab48SThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
81167edab48SThomas 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)818eae1c4aeSThomas Gleixner static void common_timer_wait_running(struct k_itimer *timer)
819eae1c4aeSThomas Gleixner {
820eae1c4aeSThomas Gleixner hrtimer_cancel_wait_running(&timer->it.real.timer);
821eae1c4aeSThomas Gleixner }
822eae1c4aeSThomas Gleixner
823eae1c4aeSThomas Gleixner /*
824eae1c4aeSThomas Gleixner * On PREEMPT_RT this prevents priority inversion and a potential livelock
825eae1c4aeSThomas Gleixner * against the ksoftirqd thread in case that ksoftirqd gets preempted while
826eae1c4aeSThomas Gleixner * executing a hrtimer callback.
827eae1c4aeSThomas Gleixner *
828ec8f954aSThomas Gleixner * See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
829ec8f954aSThomas Gleixner * just results in a cpu_relax().
830ec8f954aSThomas Gleixner *
831ec8f954aSThomas Gleixner * For POSIX CPU timers with CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n this is
832ec8f954aSThomas Gleixner * just a cpu_relax(). With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this
8330bee3b60SFrederic Weisbecker * prevents spinning on an eventually scheduled out task and a livelock
8340bee3b60SFrederic Weisbecker * when the task which tries to delete or disarm the timer has preempted
8350bee3b60SFrederic Weisbecker * the task which runs the expiry in task work context.
8360bee3b60SFrederic Weisbecker */
timer_wait_running(struct k_itimer * timer,unsigned long * flags)8370bee3b60SFrederic Weisbecker static struct k_itimer *timer_wait_running(struct k_itimer *timer,
8380bee3b60SFrederic Weisbecker unsigned long *flags)
8396945e5c2SThomas Gleixner {
8406945e5c2SThomas Gleixner const struct k_clock *kc = READ_ONCE(timer->kclock);
8416945e5c2SThomas Gleixner timer_t timer_id = READ_ONCE(timer->it_id);
842ec8f954aSThomas Gleixner
8436945e5c2SThomas Gleixner /* Prevent kfree(timer) after dropping the lock */
8446945e5c2SThomas Gleixner rcu_read_lock();
845ec8f954aSThomas Gleixner unlock_timer(timer, *flags);
846ec8f954aSThomas Gleixner
8476945e5c2SThomas Gleixner /*
848ec8f954aSThomas Gleixner * kc->timer_wait_running() might drop RCU lock. So @timer
849*f7abf14fSThomas Gleixner * cannot be touched anymore after the function returns!
850*f7abf14fSThomas Gleixner */
851*f7abf14fSThomas Gleixner if (!WARN_ON_ONCE(!kc->timer_wait_running))
852*f7abf14fSThomas Gleixner kc->timer_wait_running(timer);
853ec8f954aSThomas Gleixner
854ec8f954aSThomas Gleixner rcu_read_unlock();
855ec8f954aSThomas Gleixner /* Relock the timer. It might be not longer hashed. */
856ec8f954aSThomas Gleixner return lock_timer(timer_id, flags);
8576945e5c2SThomas Gleixner }
8586945e5c2SThomas Gleixner
8596945e5c2SThomas Gleixner /* Set a POSIX.1b interval timer. */
common_timer_set(struct k_itimer * timr,int flags,struct itimerspec64 * new_setting,struct itimerspec64 * old_setting)8606945e5c2SThomas Gleixner int common_timer_set(struct k_itimer *timr, int flags,
861eae1c4aeSThomas Gleixner struct itimerspec64 *new_setting,
862f2c45807SThomas 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
868eae1c4aeSThomas Gleixner if (old_setting)
869eae1c4aeSThomas Gleixner common_timer_get(timr, old_setting);
8705cee9645SThomas Gleixner
8715cee9645SThomas Gleixner /* Prevent rearming by clearing the interval */
8725cee9645SThomas Gleixner timr->it_interval = 0;
873eae1c4aeSThomas Gleixner /*
87480105cd0SThomas Gleixner * Careful here. On SMP systems the timer expiry function could be
8755cee9645SThomas Gleixner * active and spinning on timr->it_lock.
876eae1c4aeSThomas Gleixner */
877eae1c4aeSThomas Gleixner if (kc->timer_try_to_cancel(timr) < 0)
8785cee9645SThomas Gleixner return TIMER_RETRY;
879eae1c4aeSThomas Gleixner
8805cee9645SThomas Gleixner timr->it_active = 0;
8815cee9645SThomas Gleixner timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
88221e55c1fSThomas Gleixner ~REQUEUE_PENDING;
8835cee9645SThomas Gleixner timr->it_overrun_last = 0;
8845cee9645SThomas Gleixner
8855cee9645SThomas 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)
887eae1c4aeSThomas Gleixner return 0;
8885cee9645SThomas Gleixner
8895cee9645SThomas Gleixner timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
8905cee9645SThomas Gleixner expires = timespec64_to_ktime(new_setting->it_value);
89180105cd0SThomas Gleixner if (flags & TIMER_ABSTIME)
892eae1c4aeSThomas Gleixner expires = timens_ktime_to_host(timr->it_clock, expires);
8937da8b3a4SAndrei Vagin sigev_none = timr->it_sigev_notify == SIGEV_NONE;
8947da8b3a4SAndrei Vagin
895cef31d9aSThomas Gleixner kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
8965cee9645SThomas Gleixner timr->it_active = !sigev_none;
897eae1c4aeSThomas Gleixner return 0;
898eae1c4aeSThomas Gleixner }
8995cee9645SThomas Gleixner
do_timer_settime(timer_t timer_id,int tmr_flags,struct itimerspec64 * new_spec64,struct itimerspec64 * old_spec64)9005cee9645SThomas Gleixner static int do_timer_settime(timer_t timer_id, int tmr_flags,
9015cee9645SThomas Gleixner struct itimerspec64 *new_spec64,
90221670ee4SThomas Gleixner struct itimerspec64 *old_spec64)
9031acbe770SAl Viro {
9041acbe770SAl Viro const struct k_clock *kc;
9055cee9645SThomas Gleixner struct k_itimer *timr;
9061acbe770SAl Viro unsigned long flags;
9075f252b32SDeepa Dinamani int error = 0;
90821670ee4SThomas Gleixner
9095f252b32SDeepa Dinamani if (!timespec64_valid(&new_spec64->it_interval) ||
9105cee9645SThomas Gleixner !timespec64_valid(&new_spec64->it_value))
9111acbe770SAl Viro return -EINVAL;
9121acbe770SAl Viro
9135cee9645SThomas Gleixner if (old_spec64)
9145cee9645SThomas Gleixner memset(old_spec64, 0, sizeof(*old_spec64));
9151acbe770SAl Viro
9161acbe770SAl Viro timr = lock_timer(timer_id, &flags);
9176945e5c2SThomas Gleixner retry:
91821670ee4SThomas Gleixner if (!timr)
9196945e5c2SThomas Gleixner return -EINVAL;
9205cee9645SThomas Gleixner
9215cee9645SThomas Gleixner kc = timr->kclock;
9225cee9645SThomas Gleixner if (WARN_ON_ONCE(!kc || !kc->timer_set))
923d97bb75dSThomas Gleixner error = -EINVAL;
9245cee9645SThomas Gleixner else
9255cee9645SThomas Gleixner error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
9265cee9645SThomas Gleixner
92721670ee4SThomas Gleixner if (error == TIMER_RETRY) {
9285cee9645SThomas Gleixner // We already got the old time...
9295cee9645SThomas Gleixner old_spec64 = NULL;
9306945e5c2SThomas Gleixner /* Unlocks and relocks the timer if it still exists */
9316945e5c2SThomas Gleixner timr = timer_wait_running(timr, &flags);
9326945e5c2SThomas Gleixner goto retry;
9336945e5c2SThomas Gleixner }
9345cee9645SThomas Gleixner unlock_timer(timr, flags);
9355cee9645SThomas Gleixner
9366945e5c2SThomas Gleixner return error;
9375cee9645SThomas Gleixner }
9385cee9645SThomas Gleixner
9395cee9645SThomas Gleixner /* 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)9405cee9645SThomas Gleixner SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
9411acbe770SAl Viro const struct __kernel_itimerspec __user *, new_setting,
9421acbe770SAl Viro struct __kernel_itimerspec __user *, old_setting)
9436ff84735SDeepa Dinamani {
9446ff84735SDeepa Dinamani struct itimerspec64 new_spec, old_spec, *rtn;
9451acbe770SAl Viro int error = 0;
946725816e8SDeepa Dinamani
947725816e8SDeepa Dinamani if (!new_setting)
9481acbe770SAl Viro return -EINVAL;
9491acbe770SAl Viro
9501acbe770SAl Viro if (get_itimerspec64(&new_spec, new_setting))
9511acbe770SAl Viro return -EFAULT;
9521acbe770SAl Viro
953725816e8SDeepa Dinamani rtn = old_setting ? &old_spec : NULL;
9541acbe770SAl Viro 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;
958725816e8SDeepa Dinamani }
9591acbe770SAl Viro return error;
9601acbe770SAl Viro }
9611acbe770SAl Viro
9621acbe770SAl Viro #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)9631acbe770SAl Viro SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
9646ff84735SDeepa Dinamani struct old_itimerspec32 __user *, new,
9658dabe724SArnd Bergmann struct old_itimerspec32 __user *, old)
9669afc5eeeSArnd Bergmann {
9679afc5eeeSArnd Bergmann struct itimerspec64 new_spec, old_spec;
9681acbe770SAl Viro struct itimerspec64 *rtn = old ? &old_spec : NULL;
969725816e8SDeepa Dinamani int error = 0;
970725816e8SDeepa Dinamani
9711acbe770SAl Viro if (!new)
9721acbe770SAl Viro return -EINVAL;
9731acbe770SAl Viro if (get_old_itimerspec32(&new_spec, new))
9741acbe770SAl Viro return -EFAULT;
9759afc5eeeSArnd Bergmann
9761acbe770SAl Viro error = do_timer_settime(timer_id, flags, &new_spec, rtn);
9771acbe770SAl Viro if (!error && old) {
978725816e8SDeepa Dinamani if (put_old_itimerspec32(&old_spec, old))
9791acbe770SAl Viro error = -EFAULT;
9809afc5eeeSArnd Bergmann }
9811acbe770SAl Viro return error;
9821acbe770SAl Viro }
9831acbe770SAl Viro #endif
9841acbe770SAl Viro
common_timer_del(struct k_itimer * timer)9851acbe770SAl Viro int common_timer_del(struct k_itimer *timer)
9861acbe770SAl Viro {
987f2c45807SThomas Gleixner const struct k_clock *kc = timer->kclock;
9885cee9645SThomas Gleixner
989eae1c4aeSThomas Gleixner timer->it_interval = 0;
9905cee9645SThomas Gleixner if (kc->timer_try_to_cancel(timer) < 0)
991eae1c4aeSThomas Gleixner return TIMER_RETRY;
992eae1c4aeSThomas Gleixner timer->it_active = 0;
9935cee9645SThomas Gleixner return 0;
99421e55c1fSThomas Gleixner }
9955cee9645SThomas Gleixner
timer_delete_hook(struct k_itimer * timer)9965cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer)
9975cee9645SThomas Gleixner {
9985cee9645SThomas Gleixner const struct k_clock *kc = timer->kclock;
9995cee9645SThomas Gleixner
1000d97bb75dSThomas 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);
10125cee9645SThomas Gleixner
10135cee9645SThomas Gleixner retry_delete:
10146945e5c2SThomas Gleixner if (!timer)
10156945e5c2SThomas Gleixner return -EINVAL;
10165cee9645SThomas Gleixner
10175cee9645SThomas Gleixner if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
10185cee9645SThomas Gleixner /* Unlocks and relocks the timer if it still exists */
10196945e5c2SThomas Gleixner timer = timer_wait_running(timer, &flags);
10206945e5c2SThomas Gleixner goto retry_delete;
10216945e5c2SThomas 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 /*
10275cee9645SThomas Gleixner * A concurrent lookup could check timer::it_signal lockless. It
10285cee9645SThomas Gleixner * will reevaluate with timer::it_lock held and observe the NULL.
10295cee9645SThomas Gleixner */
10305cee9645SThomas Gleixner WRITE_ONCE(timer->it_signal, NULL);
10315cee9645SThomas Gleixner
10325cee9645SThomas Gleixner unlock_timer(timer, flags);
10335cee9645SThomas Gleixner posix_timer_unhash_and_free(timer);
10345cee9645SThomas Gleixner return 0;
10355cee9645SThomas Gleixner }
10365cee9645SThomas Gleixner
10375cee9645SThomas Gleixner /*
10385cee9645SThomas Gleixner * Delete a timer if it is armed, remove it from the hash and schedule it
10395cee9645SThomas Gleixner * for RCU freeing.
10405cee9645SThomas Gleixner */
itimer_delete(struct k_itimer * timer)10415cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer)
10425cee9645SThomas Gleixner {
10435cee9645SThomas Gleixner unsigned long flags;
10445cee9645SThomas Gleixner
10457586addbSSebastian Andrzej Siewior /*
10465cee9645SThomas Gleixner * irqsave is required to make timer_wait_running() work.
10475cee9645SThomas Gleixner */
10487586addbSSebastian Andrzej Siewior spin_lock_irqsave(&timer->it_lock, flags);
10495cee9645SThomas Gleixner
10505cee9645SThomas Gleixner retry_delete:
10515cee9645SThomas Gleixner /*
10525cee9645SThomas Gleixner * Even if the timer is not longer accessible from other tasks
10537586addbSSebastian Andrzej Siewior * it still might be armed and queued in the underlying timer
10545cee9645SThomas Gleixner * mechanism. Worse, that timer mechanism might run the expiry
10555cee9645SThomas Gleixner * function concurrently.
10565cee9645SThomas Gleixner */
10575cee9645SThomas Gleixner if (timer_delete_hook(timer) == TIMER_RETRY) {
1058d5b36a4dSOleg Nesterov /*
1059d5b36a4dSOleg Nesterov * Timer is expired concurrently, prevent livelocks
1060d5b36a4dSOleg Nesterov * and pointless spinning on RT.
10615cee9645SThomas Gleixner *
1062d5b36a4dSOleg Nesterov * timer_wait_running() drops timer::it_lock, which opens
10635cee9645SThomas Gleixner * the possibility for another task to delete the timer.
1064d5b36a4dSOleg Nesterov *
10655cee9645SThomas Gleixner * That's not possible here because this is invoked from
10665cee9645SThomas Gleixner * do_exit() only for the last thread of the thread group.
1067d5b36a4dSOleg Nesterov * So no other task can access and delete that timer.
1068d5b36a4dSOleg Nesterov */
1069d5b36a4dSOleg Nesterov if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
1070d5b36a4dSOleg Nesterov return;
1071d5b36a4dSOleg Nesterov
1072d5b36a4dSOleg Nesterov goto retry_delete;
1073d5b36a4dSOleg Nesterov }
1074d5b36a4dSOleg Nesterov list_del(&timer->list);
1075d5b36a4dSOleg Nesterov
10765cee9645SThomas Gleixner /*
10775cee9645SThomas Gleixner * Setting timer::it_signal to NULL is technically not required
10785cee9645SThomas Gleixner * here as nothing can access the timer anymore legitimately via
10795cee9645SThomas Gleixner * the hash table. Set it to NULL nevertheless so that all deletion
10805cee9645SThomas Gleixner * paths are consistent.
10816d5b8413SDeepa Dinamani */
10825cee9645SThomas Gleixner WRITE_ONCE(timer->it_signal, NULL);
1083d3ba5a9aSChristoph Hellwig
10845c499410SDeepa Dinamani spin_unlock_irqrestore(&timer->it_lock, flags);
10855cee9645SThomas Gleixner posix_timer_unhash_and_free(timer);
10865cee9645SThomas Gleixner }
10875cee9645SThomas Gleixner
10885cee9645SThomas Gleixner /*
10895c499410SDeepa Dinamani * Invoked from do_exit() when the last thread of a thread group exits.
10905cee9645SThomas Gleixner * At that point no other task can access the timers of the dying
10915cee9645SThomas Gleixner * task anymore.
10925c499410SDeepa Dinamani */
exit_itimers(struct task_struct * tsk)10935cee9645SThomas Gleixner void exit_itimers(struct task_struct *tsk)
10945cee9645SThomas Gleixner {
10955cee9645SThomas Gleixner struct list_head timers;
10966d5b8413SDeepa Dinamani struct k_itimer *tmr;
10975cee9645SThomas Gleixner
1098d3ba5a9aSChristoph Hellwig if (list_empty(&tsk->signal->posix_timers))
10995c499410SDeepa Dinamani return;
11005cee9645SThomas Gleixner
11015cee9645SThomas Gleixner /* Protect against concurrent read via /proc/$PID/timers */
11025cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock);
11035cee9645SThomas Gleixner list_replace_init(&tsk->signal->posix_timers, &timers);
11045cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock);
1105819a95feSAndrei Vagin
11065cee9645SThomas Gleixner /* The timers are not longer accessible via tsk::signal */
11075c499410SDeepa Dinamani while (!list_empty(&timers)) {
11085cee9645SThomas Gleixner 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)1113ead25417SDeepa Dinamani SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
11145cee9645SThomas Gleixner const struct __kernel_timespec __user *, tp)
1115d3ba5a9aSChristoph Hellwig {
11165cee9645SThomas Gleixner const struct k_clock *kc = clockid_to_kclock(which_clock);
11175cee9645SThomas Gleixner struct timespec64 new_tp;
11185cee9645SThomas Gleixner
11195cee9645SThomas Gleixner if (!kc || !kc->clock_set)
11205cee9645SThomas Gleixner return -EINVAL;
11215cee9645SThomas Gleixner
11221a596398SArnd Bergmann if (get_timespec64(&new_tp, tp))
11231a596398SArnd Bergmann return -EFAULT;
11241a596398SArnd Bergmann
11251a596398SArnd Bergmann /*
11263876ced4SDeepa Dinamani * Permission checks have to be done inside the clock specific
11271a596398SArnd Bergmann * setter callback.
1128ead25417SDeepa Dinamani */
11291a596398SArnd Bergmann return kc->clock_set(which_clock, &new_tp);
11301a596398SArnd Bergmann }
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,
11335cee9645SThomas Gleixner struct __kernel_timespec __user *, tp)
11341a596398SArnd Bergmann {
11355cee9645SThomas Gleixner const struct k_clock *kc = clockid_to_kclock(which_clock);
11365cee9645SThomas Gleixner struct timespec64 kernel_tp;
11375cee9645SThomas Gleixner int error;
11385cee9645SThomas Gleixner
11395cee9645SThomas Gleixner if (!kc)
11405cee9645SThomas Gleixner return -EINVAL;
11415cee9645SThomas Gleixner
1142d822cdccSAl Viro error = kc->clock_get_timespec(which_clock, &kernel_tp);
11436d5b8413SDeepa Dinamani
1144d822cdccSAl Viro if (!error && put_timespec64(&kernel_tp, tp))
1145d822cdccSAl Viro error = -EFAULT;
11465c499410SDeepa Dinamani
1147d822cdccSAl Viro return error;
1148d822cdccSAl Viro }
1149d822cdccSAl Viro
do_clock_adjtime(const clockid_t which_clock,struct __kernel_timex * ktx)1150d822cdccSAl Viro int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1151d822cdccSAl Viro {
11525c499410SDeepa Dinamani const struct k_clock *kc = clockid_to_kclock(which_clock);
1153d822cdccSAl Viro
11545c499410SDeepa Dinamani if (!kc)
1155d822cdccSAl Viro return -EINVAL;
1156d822cdccSAl Viro if (!kc->clock_adj)
1157d822cdccSAl Viro return -EOPNOTSUPP;
1158d822cdccSAl Viro
1159d822cdccSAl Viro return kc->clock_adj(which_clock, ktx);
1160b5793b0dSDeepa Dinamani }
11613a4d44b6SAl Viro
SYSCALL_DEFINE2(clock_adjtime,const clockid_t,which_clock,struct __kernel_timex __user *,utx)11628dabe724SArnd Bergmann SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
11639afc5eeeSArnd Bergmann struct __kernel_timex __user *, utx)
1164d822cdccSAl Viro {
1165d822cdccSAl Viro struct __kernel_timex ktx;
11665c499410SDeepa Dinamani int err;
1167d822cdccSAl Viro
1168d822cdccSAl Viro if (copy_from_user(&ktx, utx, sizeof(ktx)))
1169d822cdccSAl Viro return -EFAULT;
1170d822cdccSAl Viro
11719afc5eeeSArnd Bergmann err = do_clock_adjtime(which_clock, &ktx);
1172d822cdccSAl Viro
1173d822cdccSAl Viro if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
11745c499410SDeepa Dinamani return -EFAULT;
1175d822cdccSAl Viro
1176d822cdccSAl Viro return err;
11778dabe724SArnd Bergmann }
11789afc5eeeSArnd Bergmann
1179d822cdccSAl Viro /**
1180d822cdccSAl Viro * sys_clock_getres - Get the resolution of a clock
11815c499410SDeepa Dinamani * @which_clock: The clock to get the resolution for
11825c499410SDeepa Dinamani * @tp: Pointer to a a user space timespec64 for storage
1183d822cdccSAl Viro *
1184d822cdccSAl Viro * POSIX defines:
1185d822cdccSAl Viro *
1186d822cdccSAl Viro * "The clock_getres() function shall return the resolution of any
1187819a95feSAndrei Vagin * clock. Clock resolutions are implementation-defined and cannot be set by
1188d822cdccSAl Viro * a process. If the argument res is not NULL, the resolution of the
11899afc5eeeSArnd Bergmann * specified clock shall be stored in the location pointed to by res. If
11905c499410SDeepa Dinamani * res is NULL, the clock resolution is not returned. If the time argument
1191d822cdccSAl Viro * of clock_settime() is not a multiple of res, then the value is truncated
11925c499410SDeepa Dinamani * to a multiple of res."
1193d822cdccSAl Viro *
1194d822cdccSAl Viro * Due to the various hardware constraints the real resolution can vary
11958dabe724SArnd Bergmann * wildly and even change during runtime when the underlying devices are
11964d5f007eSArnd Bergmann * replaced. The kernel also can use hardware devices with different
11973a4d44b6SAl Viro * resolutions for reading the time and for arming timers.
1198ead25417SDeepa Dinamani *
11993a4d44b6SAl Viro * The kernel therefore deviates from the POSIX spec in various aspects:
12003a4d44b6SAl Viro *
12014d5f007eSArnd Bergmann * 1) The resolution returned to user space
12023a4d44b6SAl Viro *
12033a4d44b6SAl Viro * For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
12043a4d44b6SAl Viro * CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
12051a596398SArnd Bergmann * the kernel differentiates only two cases:
12063a4d44b6SAl Viro *
12072d036dfaSChen Jun * I) Low resolution mode:
12082d036dfaSChen Jun *
12093a4d44b6SAl Viro * When high resolution timers are disabled at compile or runtime
12103a4d44b6SAl Viro * the resolution returned is nanoseconds per tick, which represents
12113a4d44b6SAl Viro * the precision at which timers expire.
12123a4d44b6SAl Viro *
12138dabe724SArnd Bergmann * II) High resolution mode:
12149afc5eeeSArnd Bergmann *
12155cee9645SThomas Gleixner * When high resolution timers are enabled the resolution returned
1216d3ba5a9aSChristoph Hellwig * is always one nanosecond independent of the actual resolution of
12175c499410SDeepa Dinamani * the underlying hardware devices.
12185c499410SDeepa Dinamani *
12195cee9645SThomas Gleixner * For CLOCK_*_ALARM the actual resolution depends on system
12205cee9645SThomas Gleixner * state. When system is running the resolution is the same as the
12215cee9645SThomas Gleixner * resolution of the other clocks. During suspend the actual
12225cee9645SThomas Gleixner * resolution is the resolution of the underlying RTC device which
12235c499410SDeepa Dinamani * might be way less precise than the clockevent device used during
12249afc5eeeSArnd Bergmann * running state.
12255c499410SDeepa Dinamani *
12265cee9645SThomas Gleixner * For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
12275c499410SDeepa Dinamani * returned is always nanoseconds per tick.
12285cee9645SThomas Gleixner *
12295c499410SDeepa Dinamani * For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
1230d822cdccSAl Viro * returned is always one nanosecond under the assumption that the
12315cee9645SThomas Gleixner * underlying scheduler clock has a better resolution than nanoseconds
12325cee9645SThomas Gleixner * per tick.
12335cee9645SThomas Gleixner *
12345cee9645SThomas Gleixner * For dynamic POSIX clocks (PTP devices) the resolution returned is
12355cee9645SThomas Gleixner * always one nanosecond.
1236938e7cf2SThomas Gleixner *
12375cee9645SThomas Gleixner * 2) Affect on sys_clock_settime()
1238ea2d1f7fSAndrei Vagin *
1239ea2d1f7fSAndrei Vagin * The kernel does not truncate the time which is handed in to
1240ea2d1f7fSAndrei Vagin * sys_clock_settime(). The kernel internal timekeeping is always using
12415cee9645SThomas Gleixner * nanoseconds precision independent of the clocksource device which is
12425cee9645SThomas Gleixner * used to read the time from. The resolution of that device only
12435cee9645SThomas Gleixner * affects the presicion of the time returned by sys_clock_gettime().
12445cee9645SThomas Gleixner *
12451f9b37bfSAndrei Vagin * Returns:
12461f9b37bfSAndrei Vagin * 0 Success. @tp contains the resolution
12471f9b37bfSAndrei Vagin * -EINVAL @which_clock is not a valid clock ID
12481f9b37bfSAndrei Vagin * -EFAULT Copying the resolution to @tp faulted
12491f9b37bfSAndrei Vagin * -ENODEV Dynamic POSIX clock is not backed by a device
12501f9b37bfSAndrei Vagin * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
12511f9b37bfSAndrei Vagin */
SYSCALL_DEFINE2(clock_getres,const clockid_t,which_clock,struct __kernel_timespec __user *,tp)12521f9b37bfSAndrei Vagin SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
12531f9b37bfSAndrei Vagin struct __kernel_timespec __user *, tp)
12541f9b37bfSAndrei Vagin {
12551f9b37bfSAndrei Vagin const struct k_clock *kc = clockid_to_kclock(which_clock);
12561f9b37bfSAndrei Vagin struct timespec64 rtn_tp;
12571f9b37bfSAndrei Vagin int error;
12585cee9645SThomas Gleixner
125901909974SDeepa Dinamani if (!kc)
126001909974SDeepa Dinamani return -EINVAL;
12615cee9645SThomas Gleixner
1262d3ba5a9aSChristoph Hellwig error = kc->clock_getres(which_clock, &rtn_tp);
1263c0edd7c9SDeepa Dinamani
12645cee9645SThomas Gleixner if (!error && tp && put_timespec64(&rtn_tp, tp))
12655cee9645SThomas Gleixner error = -EFAULT;
12665cee9645SThomas Gleixner
12675cee9645SThomas Gleixner return error;
126893cb8e20SHelge Deller }
12695cee9645SThomas Gleixner
1270c0edd7c9SDeepa Dinamani #ifdef CONFIG_COMPAT_32BIT_TIME
12715cee9645SThomas Gleixner
SYSCALL_DEFINE2(clock_settime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)12725cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1273c0edd7c9SDeepa Dinamani struct old_timespec32 __user *, tp)
12745cee9645SThomas Gleixner {
127599e6c0e6SAl Viro const struct k_clock *kc = clockid_to_kclock(which_clock);
127699e6c0e6SAl Viro struct timespec64 ts;
12779f76d591SJann Horn
1278edbeda46SAl Viro if (!kc || !kc->clock_set)
127999e6c0e6SAl Viro return -EINVAL;
12805cee9645SThomas Gleixner
1281c0edd7c9SDeepa Dinamani if (get_old_timespec32(&ts, tp))
12825cee9645SThomas Gleixner return -EFAULT;
12835cee9645SThomas Gleixner
1284b5793b0dSDeepa Dinamani return kc->clock_set(which_clock, &ts);
1285b5793b0dSDeepa Dinamani }
12868dabe724SArnd Bergmann
SYSCALL_DEFINE2(clock_gettime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)12879afc5eeeSArnd Bergmann SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
12889afc5eeeSArnd Bergmann struct old_timespec32 __user *, tp)
12895cee9645SThomas Gleixner {
1290d3ba5a9aSChristoph Hellwig const struct k_clock *kc = clockid_to_kclock(which_clock);
1291c0edd7c9SDeepa Dinamani struct timespec64 ts;
12925cee9645SThomas Gleixner int err;
1293edbeda46SAl Viro
12945cee9645SThomas Gleixner if (!kc)
1295edbeda46SAl Viro return -EINVAL;
129693cb8e20SHelge Deller
12975cee9645SThomas Gleixner err = kc->clock_get_timespec(which_clock, &ts);
12989afc5eeeSArnd Bergmann
1299edbeda46SAl Viro if (!err && put_old_timespec32(&ts, tp))
1300edbeda46SAl Viro err = -EFAULT;
1301c0edd7c9SDeepa Dinamani
1302edbeda46SAl Viro return err;
1303edbeda46SAl Viro }
1304edbeda46SAl Viro
SYSCALL_DEFINE2(clock_adjtime32,clockid_t,which_clock,struct old_timex32 __user *,utp)13059f76d591SJann Horn SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1306edbeda46SAl Viro struct old_timex32 __user *, utp)
1307edbeda46SAl Viro {
1308edbeda46SAl Viro struct __kernel_timex ktx;
1309c0edd7c9SDeepa Dinamani int err;
13105cee9645SThomas Gleixner
1311b5793b0dSDeepa Dinamani err = get_old_timex32(&ktx, utp);
1312edbeda46SAl Viro if (err)
13136631fa12SThomas Gleixner return err;
13146631fa12SThomas Gleixner
13156631fa12SThomas Gleixner err = do_clock_adjtime(which_clock, &ktx);
1316eaf80194SAndrei Vagin
13179c71a2e8SAndrei Vagin if (err >= 0 && put_old_timex32(utp, &ktx))
13186631fa12SThomas Gleixner return -EFAULT;
13196631fa12SThomas Gleixner
13206631fa12SThomas Gleixner return err;
13216631fa12SThomas Gleixner }
13226631fa12SThomas Gleixner
SYSCALL_DEFINE2(clock_getres_time32,clockid_t,which_clock,struct old_timespec32 __user *,tp)13236631fa12SThomas Gleixner SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
13246631fa12SThomas Gleixner struct old_timespec32 __user *, tp)
1325f37fb0aaSThomas Gleixner {
132691d57baeSThomas Gleixner const struct k_clock *kc = clockid_to_kclock(which_clock);
132791d57baeSThomas Gleixner struct timespec64 ts;
1328eae1c4aeSThomas Gleixner int err;
1329ec8f954aSThomas Gleixner
1330eae1c4aeSThomas Gleixner if (!kc)
13316631fa12SThomas Gleixner return -EINVAL;
13326631fa12SThomas Gleixner
13336631fa12SThomas Gleixner err = kc->clock_getres(which_clock, &ts);
13346631fa12SThomas Gleixner if (!err && tp && put_old_timespec32(&ts, tp))
1335eaf80194SAndrei Vagin return -EFAULT;
13369c71a2e8SAndrei Vagin
13371f9b37bfSAndrei Vagin return err;
13386631fa12SThomas Gleixner }
13396631fa12SThomas Gleixner
13406631fa12SThomas Gleixner #endif
13416631fa12SThomas Gleixner
1342f37fb0aaSThomas Gleixner /*
134391d57baeSThomas Gleixner * sys_clock_nanosleep() for CLOCK_REALTIME and CLOCK_TAI
134491d57baeSThomas Gleixner */
common_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)1345eae1c4aeSThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags,
1346ec8f954aSThomas Gleixner const struct timespec64 *rqtp)
1347eae1c4aeSThomas Gleixner {
13486631fa12SThomas Gleixner ktime_t texp = timespec64_to_ktime(*rqtp);
13496631fa12SThomas Gleixner
13506631fa12SThomas Gleixner return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
13516631fa12SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1352819a95feSAndrei Vagin which_clock);
13536631fa12SThomas Gleixner }
13546631fa12SThomas Gleixner
13556631fa12SThomas Gleixner /*
13566631fa12SThomas Gleixner * sys_clock_nanosleep() for CLOCK_MONOTONIC and CLOCK_BOOTTIME
1357819a95feSAndrei Vagin *
13586631fa12SThomas Gleixner * Absolute nanosleeps for these clocks are time-namespace adjusted.
13596631fa12SThomas Gleixner */
common_nsleep_timens(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)13606631fa12SThomas Gleixner static int common_nsleep_timens(const clockid_t which_clock, int flags,
13616631fa12SThomas Gleixner const struct timespec64 *rqtp)
1362819a95feSAndrei Vagin {
13636631fa12SThomas Gleixner ktime_t texp = timespec64_to_ktime(*rqtp);
13646631fa12SThomas Gleixner
13656631fa12SThomas Gleixner if (flags & TIMER_ABSTIME)
13666631fa12SThomas Gleixner texp = timens_ktime_to_host(which_clock, texp);
13679c71a2e8SAndrei Vagin
1368eaf80194SAndrei Vagin return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
13696631fa12SThomas Gleixner HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
13706631fa12SThomas Gleixner which_clock);
13716631fa12SThomas Gleixner }
13726631fa12SThomas Gleixner
SYSCALL_DEFINE4(clock_nanosleep,const clockid_t,which_clock,int,flags,const struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)13736631fa12SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1374f37fb0aaSThomas Gleixner const struct __kernel_timespec __user *, rqtp,
137591d57baeSThomas Gleixner struct __kernel_timespec __user *, rmtp)
137691d57baeSThomas Gleixner {
1377eae1c4aeSThomas Gleixner const struct k_clock *kc = clockid_to_kclock(which_clock);
1378ec8f954aSThomas Gleixner struct timespec64 t;
1379eae1c4aeSThomas Gleixner
13806631fa12SThomas Gleixner if (!kc)
13816631fa12SThomas Gleixner return -EINVAL;
1382a3ed0e43SThomas Gleixner if (!kc->nsleep)
13836631fa12SThomas Gleixner return -EOPNOTSUPP;
13849c71a2e8SAndrei Vagin
1385eaf80194SAndrei Vagin if (get_timespec64(&t, rqtp))
13861f9b37bfSAndrei Vagin return -EFAULT;
1387a3ed0e43SThomas Gleixner
1388a3ed0e43SThomas Gleixner if (!timespec64_valid(&t))
1389a3ed0e43SThomas Gleixner return -EINVAL;
1390a3ed0e43SThomas Gleixner if (flags & TIMER_ABSTIME)
1391a3ed0e43SThomas Gleixner rmtp = NULL;
1392a3ed0e43SThomas Gleixner current->restart_block.fn = do_no_restart_syscall;
1393a3ed0e43SThomas Gleixner current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1394a3ed0e43SThomas Gleixner current->restart_block.nanosleep.rmtp = rmtp;
1395ec8f954aSThomas Gleixner
1396a3ed0e43SThomas Gleixner return kc->nsleep(which_clock, flags, &t);
13976631fa12SThomas Gleixner }
13986631fa12SThomas Gleixner
13996631fa12SThomas Gleixner #ifdef CONFIG_COMPAT_32BIT_TIME
14006631fa12SThomas Gleixner
SYSCALL_DEFINE4(clock_nanosleep_time32,clockid_t,which_clock,int,flags,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)14016631fa12SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
14026631fa12SThomas Gleixner struct old_timespec32 __user *, rqtp,
14036631fa12SThomas Gleixner struct old_timespec32 __user *, rmtp)
14046631fa12SThomas Gleixner {
14056631fa12SThomas Gleixner const struct k_clock *kc = clockid_to_kclock(which_clock);
14066631fa12SThomas Gleixner struct timespec64 t;
1407a3ed0e43SThomas Gleixner
14086631fa12SThomas Gleixner if (!kc)
14096631fa12SThomas Gleixner return -EINVAL;
14106631fa12SThomas Gleixner if (!kc->nsleep)
14116631fa12SThomas Gleixner return -EOPNOTSUPP;
14126631fa12SThomas Gleixner
14136631fa12SThomas Gleixner if (get_old_timespec32(&t, rqtp))
14146631fa12SThomas Gleixner return -EFAULT;
141519b558dbSThomas Gleixner
141619b558dbSThomas Gleixner if (!timespec64_valid(&t))
141719b558dbSThomas Gleixner return -EINVAL;
14186631fa12SThomas Gleixner if (flags & TIMER_ABSTIME)
14196631fa12SThomas Gleixner rmtp = NULL;
142019b558dbSThomas Gleixner current->restart_block.fn = do_no_restart_syscall;
14216631fa12SThomas Gleixner current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
142219b558dbSThomas Gleixner current->restart_block.nanosleep.compat_rmtp = rmtp;
14236631fa12SThomas Gleixner
142419b558dbSThomas Gleixner return kc->nsleep(which_clock, flags, &t);
142519b558dbSThomas Gleixner }
14266631fa12SThomas Gleixner
1427 #endif
1428
1429 static const struct k_clock clock_realtime = {
1430 .clock_getres = posix_get_hrtimer_res,
1431 .clock_get_timespec = posix_get_realtime_timespec,
1432 .clock_get_ktime = posix_get_realtime_ktime,
1433 .clock_set = posix_clock_realtime_set,
1434 .clock_adj = posix_clock_realtime_adj,
1435 .nsleep = common_nsleep,
1436 .timer_create = common_timer_create,
1437 .timer_set = common_timer_set,
1438 .timer_get = common_timer_get,
1439 .timer_del = common_timer_del,
1440 .timer_rearm = common_hrtimer_rearm,
1441 .timer_forward = common_hrtimer_forward,
1442 .timer_remaining = common_hrtimer_remaining,
1443 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1444 .timer_wait_running = common_timer_wait_running,
1445 .timer_arm = common_hrtimer_arm,
1446 };
1447
1448 static const struct k_clock clock_monotonic = {
1449 .clock_getres = posix_get_hrtimer_res,
1450 .clock_get_timespec = posix_get_monotonic_timespec,
1451 .clock_get_ktime = posix_get_monotonic_ktime,
1452 .nsleep = common_nsleep_timens,
1453 .timer_create = common_timer_create,
1454 .timer_set = common_timer_set,
1455 .timer_get = common_timer_get,
1456 .timer_del = common_timer_del,
1457 .timer_rearm = common_hrtimer_rearm,
1458 .timer_forward = common_hrtimer_forward,
1459 .timer_remaining = common_hrtimer_remaining,
1460 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1461 .timer_wait_running = common_timer_wait_running,
1462 .timer_arm = common_hrtimer_arm,
1463 };
1464
1465 static const struct k_clock clock_monotonic_raw = {
1466 .clock_getres = posix_get_hrtimer_res,
1467 .clock_get_timespec = posix_get_monotonic_raw,
1468 };
1469
1470 static const struct k_clock clock_realtime_coarse = {
1471 .clock_getres = posix_get_coarse_res,
1472 .clock_get_timespec = posix_get_realtime_coarse,
1473 };
1474
1475 static const struct k_clock clock_monotonic_coarse = {
1476 .clock_getres = posix_get_coarse_res,
1477 .clock_get_timespec = posix_get_monotonic_coarse,
1478 };
1479
1480 static const struct k_clock clock_tai = {
1481 .clock_getres = posix_get_hrtimer_res,
1482 .clock_get_ktime = posix_get_tai_ktime,
1483 .clock_get_timespec = posix_get_tai_timespec,
1484 .nsleep = common_nsleep,
1485 .timer_create = common_timer_create,
1486 .timer_set = common_timer_set,
1487 .timer_get = common_timer_get,
1488 .timer_del = common_timer_del,
1489 .timer_rearm = common_hrtimer_rearm,
1490 .timer_forward = common_hrtimer_forward,
1491 .timer_remaining = common_hrtimer_remaining,
1492 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1493 .timer_wait_running = common_timer_wait_running,
1494 .timer_arm = common_hrtimer_arm,
1495 };
1496
1497 static const struct k_clock clock_boottime = {
1498 .clock_getres = posix_get_hrtimer_res,
1499 .clock_get_ktime = posix_get_boottime_ktime,
1500 .clock_get_timespec = posix_get_boottime_timespec,
1501 .nsleep = common_nsleep_timens,
1502 .timer_create = common_timer_create,
1503 .timer_set = common_timer_set,
1504 .timer_get = common_timer_get,
1505 .timer_del = common_timer_del,
1506 .timer_rearm = common_hrtimer_rearm,
1507 .timer_forward = common_hrtimer_forward,
1508 .timer_remaining = common_hrtimer_remaining,
1509 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1510 .timer_wait_running = common_timer_wait_running,
1511 .timer_arm = common_hrtimer_arm,
1512 };
1513
1514 static const struct k_clock * const posix_clocks[] = {
1515 [CLOCK_REALTIME] = &clock_realtime,
1516 [CLOCK_MONOTONIC] = &clock_monotonic,
1517 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1518 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1519 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1520 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1521 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1522 [CLOCK_BOOTTIME] = &clock_boottime,
1523 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1524 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1525 [CLOCK_TAI] = &clock_tai,
1526 };
1527
clockid_to_kclock(const clockid_t id)1528 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1529 {
1530 clockid_t idx = id;
1531
1532 if (id < 0) {
1533 return (id & CLOCKFD_MASK) == CLOCKFD ?
1534 &clock_posix_dynamic : &clock_posix_cpu;
1535 }
1536
1537 if (id >= ARRAY_SIZE(posix_clocks))
1538 return NULL;
1539
1540 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1541 }
1542