1 // SPDX-License-Identifier: GPL-2.0+
3 * 2002-10-15 Posix Clocks & timers
4 * by George Anzinger george@mvista.com
5 * Copyright (C) 2002 2003 by MontaVista Software.
7 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
8 * Copyright (C) 2004 Boris Hu
10 * These are all the functions necessary to implement POSIX clocks & timers
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/time.h>
16 #include <linux/mutex.h>
17 #include <linux/sched/task.h>
19 #include <linux/uaccess.h>
20 #include <linux/list.h>
21 #include <linux/init.h>
22 #include <linux/compiler.h>
23 #include <linux/hash.h>
24 #include <linux/posix-clock.h>
25 #include <linux/posix-timers.h>
26 #include <linux/syscalls.h>
27 #include <linux/wait.h>
28 #include <linux/workqueue.h>
29 #include <linux/export.h>
30 #include <linux/hashtable.h>
31 #include <linux/compat.h>
32 #include <linux/nospec.h>
34 #include "timekeeping.h"
35 #include "posix-timers.h"
38 * Management arrays for POSIX timers. Timers are now kept in static hash table
40 * Timer ids are allocated by local routine, which selects proper hash head by
41 * key, constructed from current->signal address and per signal struct counter.
42 * This keeps timer ids unique per process, but now they can intersect between
47 * Lets keep our timers in a slab cache :-)
49 static struct kmem_cache *posix_timers_cache;
51 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
52 static DEFINE_SPINLOCK(hash_lock);
54 static const struct k_clock * const posix_clocks[];
55 static const struct k_clock *clockid_to_kclock(const clockid_t id);
56 static const struct k_clock clock_realtime, clock_monotonic;
59 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
60 * SIGEV values. Here we put out an error if this assumption fails.
62 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
63 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
64 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
68 * The timer ID is turned into a timer address by idr_find().
69 * Verifying a valid ID consists of:
71 * a) checking that idr_find() returns other than -1.
72 * b) checking that the timer id matches the one in the timer itself.
73 * c) that the timer owner is in the callers thread group.
77 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
78 * to implement others. This structure defines the various
81 * RESOLUTION: Clock resolution is used to round up timer and interval
82 * times, NOT to report clock times, which are reported with as
83 * much resolution as the system can muster. In some cases this
84 * resolution may depend on the underlying clock hardware and
85 * may not be quantifiable until run time, and only then is the
86 * necessary code is written. The standard says we should say
87 * something about this issue in the documentation...
89 * FUNCTIONS: The CLOCKs structure defines possible functions to
90 * handle various clock functions.
92 * The standard POSIX timer management code assumes the
93 * following: 1.) The k_itimer struct (sched.h) is used for
94 * the timer. 2.) The list, it_lock, it_clock, it_id and
95 * it_pid fields are not modified by timer code.
97 * Permissions: It is assumed that the clock_settime() function defined
98 * for each clock will take care of permission checks. Some
99 * clocks may be set able by any user (i.e. local process
100 * clocks) others not. Currently the only set able clock we
101 * have is CLOCK_REALTIME and its high res counter part, both of
102 * which we beg off on and pass to do_sys_settimeofday().
104 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
106 #define lock_timer(tid, flags) \
107 ({ struct k_itimer *__timr; \
108 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
112 static int hash(struct signal_struct *sig, unsigned int nr)
114 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
117 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
118 struct signal_struct *sig,
121 struct k_itimer *timer;
123 hlist_for_each_entry_rcu(timer, head, t_hash) {
124 if ((timer->it_signal == sig) && (timer->it_id == id))
130 static struct k_itimer *posix_timer_by_id(timer_t id)
132 struct signal_struct *sig = current->signal;
133 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
135 return __posix_timers_find(head, sig, id);
138 static int posix_timer_add(struct k_itimer *timer)
140 struct signal_struct *sig = current->signal;
141 struct hlist_head *head;
142 unsigned int cnt, id;
145 * FIXME: Replace this by a per signal struct xarray once there is
146 * a plan to handle the resulting CRIU regression gracefully.
148 for (cnt = 0; cnt <= INT_MAX; cnt++) {
149 spin_lock(&hash_lock);
150 id = sig->next_posix_timer_id;
152 /* Write the next ID back. Clamp it to the positive space */
153 sig->next_posix_timer_id = (id + 1) & INT_MAX;
155 head = &posix_timers_hashtable[hash(sig, id)];
156 if (!__posix_timers_find(head, sig, id)) {
157 hlist_add_head_rcu(&timer->t_hash, head);
158 spin_unlock(&hash_lock);
161 spin_unlock(&hash_lock);
163 /* POSIX return code when no timer ID could be allocated */
167 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
169 spin_unlock_irqrestore(&timr->it_lock, flags);
172 /* Get clock_realtime */
173 static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
175 ktime_get_real_ts64(tp);
179 /* Set clock_realtime */
180 static int posix_clock_realtime_set(const clockid_t which_clock,
181 const struct timespec64 *tp)
183 return do_sys_settimeofday64(tp, NULL);
186 static int posix_clock_realtime_adj(const clockid_t which_clock,
187 struct __kernel_timex *t)
189 return do_adjtimex(t);
193 * Get monotonic time for posix timers
195 static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
202 * Get monotonic-raw time for posix timers
204 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
206 ktime_get_raw_ts64(tp);
211 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
213 ktime_get_coarse_real_ts64(tp);
217 static int posix_get_monotonic_coarse(clockid_t which_clock,
218 struct timespec64 *tp)
220 ktime_get_coarse_ts64(tp);
224 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
226 *tp = ktime_to_timespec64(KTIME_LOW_RES);
230 static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
232 ktime_get_boottime_ts64(tp);
236 static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
238 ktime_get_clocktai_ts64(tp);
242 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
245 tp->tv_nsec = hrtimer_resolution;
250 * Initialize everything, well, just everything in Posix clocks/timers ;)
252 static __init int init_posix_timers(void)
254 posix_timers_cache = kmem_cache_create("posix_timers_cache",
255 sizeof (struct k_itimer), 0, SLAB_PANIC,
259 __initcall(init_posix_timers);
262 * The siginfo si_overrun field and the return value of timer_getoverrun(2)
263 * are of type int. Clamp the overrun value to INT_MAX
265 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
267 s64 sum = timr->it_overrun_last + (s64)baseval;
269 return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
272 static void common_hrtimer_rearm(struct k_itimer *timr)
274 struct hrtimer *timer = &timr->it.real.timer;
276 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
278 hrtimer_restart(timer);
282 * This function is exported for use by the signal deliver code. It is
283 * called just prior to the info block being released and passes that
284 * block to us. It's function is to update the overrun entry AND to
285 * restart the timer. It should only be called if the timer is to be
286 * restarted (i.e. we have flagged this in the sys_private entry of the
289 * To protect against the timer going away while the interrupt is queued,
290 * we require that the it_requeue_pending flag be set.
292 void posixtimer_rearm(struct kernel_siginfo *info)
294 struct k_itimer *timr;
297 timr = lock_timer(info->si_tid, &flags);
301 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
302 timr->kclock->timer_rearm(timr);
305 timr->it_overrun_last = timr->it_overrun;
306 timr->it_overrun = -1LL;
307 ++timr->it_requeue_pending;
309 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
312 unlock_timer(timr, flags);
315 int posix_timer_event(struct k_itimer *timr, int si_private)
320 * FIXME: if ->sigq is queued we can race with
321 * dequeue_signal()->posixtimer_rearm().
323 * If dequeue_signal() sees the "right" value of
324 * si_sys_private it calls posixtimer_rearm().
325 * We re-queue ->sigq and drop ->it_lock().
326 * posixtimer_rearm() locks the timer
327 * and re-schedules it while ->sigq is pending.
328 * Not really bad, but not that we want.
330 timr->sigq->info.si_sys_private = si_private;
332 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
333 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
334 /* If we failed to send the signal the timer stops. */
339 * This function gets called when a POSIX.1b interval timer expires. It
340 * is used as a callback from the kernel internal timer. The
341 * run_timer_list code ALWAYS calls with interrupts on.
343 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
345 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
347 struct k_itimer *timr;
350 enum hrtimer_restart ret = HRTIMER_NORESTART;
352 timr = container_of(timer, struct k_itimer, it.real.timer);
353 spin_lock_irqsave(&timr->it_lock, flags);
356 if (timr->it_interval != 0)
357 si_private = ++timr->it_requeue_pending;
359 if (posix_timer_event(timr, si_private)) {
361 * signal was not sent because of sig_ignor
362 * we will not get a call back to restart it AND
363 * it should be restarted.
365 if (timr->it_interval != 0) {
366 ktime_t now = hrtimer_cb_get_time(timer);
369 * FIXME: What we really want, is to stop this
370 * timer completely and restart it in case the
371 * SIG_IGN is removed. This is a non trivial
372 * change which involves sighand locking
373 * (sigh !), which we don't want to do late in
376 * For now we just let timers with an interval
377 * less than a jiffie expire every jiffie to
378 * avoid softirq starvation in case of SIG_IGN
379 * and a very small interval, which would put
380 * the timer right back on the softirq pending
381 * list. By moving now ahead of time we trick
382 * hrtimer_forward() to expire the timer
383 * later, while we still maintain the overrun
384 * accuracy, but have some inconsistency in
385 * the timer_gettime() case. This is at least
386 * better than a starved softirq. A more
387 * complex fix which solves also another related
388 * inconsistency is already in the pipeline.
390 #ifdef CONFIG_HIGH_RES_TIMERS
392 ktime_t kj = NSEC_PER_SEC / HZ;
394 if (timr->it_interval < kj)
395 now = ktime_add(now, kj);
398 timr->it_overrun += hrtimer_forward(timer, now,
400 ret = HRTIMER_RESTART;
401 ++timr->it_requeue_pending;
406 unlock_timer(timr, flags);
410 static struct pid *good_sigevent(sigevent_t * event)
412 struct pid *pid = task_tgid(current);
413 struct task_struct *rtn;
415 switch (event->sigev_notify) {
416 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
417 pid = find_vpid(event->sigev_notify_thread_id);
418 rtn = pid_task(pid, PIDTYPE_PID);
419 if (!rtn || !same_thread_group(rtn, current))
424 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
434 static struct k_itimer * alloc_posix_timer(void)
436 struct k_itimer *tmr;
437 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
440 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
441 kmem_cache_free(posix_timers_cache, tmr);
444 clear_siginfo(&tmr->sigq->info);
448 static void k_itimer_rcu_free(struct rcu_head *head)
450 struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
452 kmem_cache_free(posix_timers_cache, tmr);
456 #define IT_ID_NOT_SET 0
457 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
461 spin_lock_irqsave(&hash_lock, flags);
462 hlist_del_rcu(&tmr->t_hash);
463 spin_unlock_irqrestore(&hash_lock, flags);
465 put_pid(tmr->it_pid);
466 sigqueue_free(tmr->sigq);
467 call_rcu(&tmr->rcu, k_itimer_rcu_free);
470 static int common_timer_create(struct k_itimer *new_timer)
472 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
476 /* Create a POSIX.1b interval timer. */
477 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
478 timer_t __user *created_timer_id)
480 const struct k_clock *kc = clockid_to_kclock(which_clock);
481 struct k_itimer *new_timer;
482 int error, new_timer_id;
483 int it_id_set = IT_ID_NOT_SET;
487 if (!kc->timer_create)
490 new_timer = alloc_posix_timer();
491 if (unlikely(!new_timer))
494 spin_lock_init(&new_timer->it_lock);
495 new_timer_id = posix_timer_add(new_timer);
496 if (new_timer_id < 0) {
497 error = new_timer_id;
501 it_id_set = IT_ID_SET;
502 new_timer->it_id = (timer_t) new_timer_id;
503 new_timer->it_clock = which_clock;
504 new_timer->kclock = kc;
505 new_timer->it_overrun = -1LL;
509 new_timer->it_pid = get_pid(good_sigevent(event));
511 if (!new_timer->it_pid) {
515 new_timer->it_sigev_notify = event->sigev_notify;
516 new_timer->sigq->info.si_signo = event->sigev_signo;
517 new_timer->sigq->info.si_value = event->sigev_value;
519 new_timer->it_sigev_notify = SIGEV_SIGNAL;
520 new_timer->sigq->info.si_signo = SIGALRM;
521 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
522 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
523 new_timer->it_pid = get_pid(task_tgid(current));
526 new_timer->sigq->info.si_tid = new_timer->it_id;
527 new_timer->sigq->info.si_code = SI_TIMER;
529 if (copy_to_user(created_timer_id,
530 &new_timer_id, sizeof (new_timer_id))) {
535 error = kc->timer_create(new_timer);
539 spin_lock_irq(¤t->sighand->siglock);
540 new_timer->it_signal = current->signal;
541 list_add(&new_timer->list, ¤t->signal->posix_timers);
542 spin_unlock_irq(¤t->sighand->siglock);
546 * In the case of the timer belonging to another task, after
547 * the task is unlocked, the timer is owned by the other task
548 * and may cease to exist at any time. Don't use or modify
549 * new_timer after the unlock call.
552 release_posix_timer(new_timer, it_id_set);
556 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
557 struct sigevent __user *, timer_event_spec,
558 timer_t __user *, created_timer_id)
560 if (timer_event_spec) {
563 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
565 return do_timer_create(which_clock, &event, created_timer_id);
567 return do_timer_create(which_clock, NULL, created_timer_id);
571 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
572 struct compat_sigevent __user *, timer_event_spec,
573 timer_t __user *, created_timer_id)
575 if (timer_event_spec) {
578 if (get_compat_sigevent(&event, timer_event_spec))
580 return do_timer_create(which_clock, &event, created_timer_id);
582 return do_timer_create(which_clock, NULL, created_timer_id);
587 * Locking issues: We need to protect the result of the id look up until
588 * we get the timer locked down so it is not deleted under us. The
589 * removal is done under the idr spinlock so we use that here to bridge
590 * the find to the timer lock. To avoid a dead lock, the timer id MUST
591 * be release with out holding the timer lock.
593 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
595 struct k_itimer *timr;
598 * timer_t could be any type >= int and we want to make sure any
599 * @timer_id outside positive int range fails lookup.
601 if ((unsigned long long)timer_id > INT_MAX)
605 timr = posix_timer_by_id(timer_id);
607 spin_lock_irqsave(&timr->it_lock, *flags);
608 if (timr->it_signal == current->signal) {
612 spin_unlock_irqrestore(&timr->it_lock, *flags);
619 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
621 struct hrtimer *timer = &timr->it.real.timer;
623 return __hrtimer_expires_remaining_adjusted(timer, now);
626 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
628 struct hrtimer *timer = &timr->it.real.timer;
630 return hrtimer_forward(timer, now, timr->it_interval);
634 * Get the time remaining on a POSIX.1b interval timer. This function
635 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
638 * We have a couple of messes to clean up here. First there is the case
639 * of a timer that has a requeue pending. These timers should appear to
640 * be in the timer list with an expiry as if we were to requeue them
643 * The second issue is the SIGEV_NONE timer which may be active but is
644 * not really ever put in the timer list (to save system resources).
645 * This timer may be expired, and if so, we will do it here. Otherwise
646 * it is the same as a requeue pending timer WRT to what we should
649 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
651 const struct k_clock *kc = timr->kclock;
652 ktime_t now, remaining, iv;
653 struct timespec64 ts64;
656 sig_none = timr->it_sigev_notify == SIGEV_NONE;
657 iv = timr->it_interval;
659 /* interval timer ? */
661 cur_setting->it_interval = ktime_to_timespec64(iv);
662 } else if (!timr->it_active) {
664 * SIGEV_NONE oneshot timers are never queued. Check them
672 * The timespec64 based conversion is suboptimal, but it's not
673 * worth to implement yet another callback.
675 kc->clock_get(timr->it_clock, &ts64);
676 now = timespec64_to_ktime(ts64);
679 * When a requeue is pending or this is a SIGEV_NONE timer move the
680 * expiry time forward by intervals, so expiry is > now.
682 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
683 timr->it_overrun += kc->timer_forward(timr, now);
685 remaining = kc->timer_remaining(timr, now);
686 /* Return 0 only, when the timer is expired and not pending */
687 if (remaining <= 0) {
689 * A single shot SIGEV_NONE timer must return 0, when
693 cur_setting->it_value.tv_nsec = 1;
695 cur_setting->it_value = ktime_to_timespec64(remaining);
699 /* Get the time remaining on a POSIX.1b interval timer. */
700 static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
702 struct k_itimer *timr;
703 const struct k_clock *kc;
707 timr = lock_timer(timer_id, &flags);
711 memset(setting, 0, sizeof(*setting));
713 if (WARN_ON_ONCE(!kc || !kc->timer_get))
716 kc->timer_get(timr, setting);
718 unlock_timer(timr, flags);
722 /* Get the time remaining on a POSIX.1b interval timer. */
723 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
724 struct __kernel_itimerspec __user *, setting)
726 struct itimerspec64 cur_setting;
728 int ret = do_timer_gettime(timer_id, &cur_setting);
730 if (put_itimerspec64(&cur_setting, setting))
736 #ifdef CONFIG_COMPAT_32BIT_TIME
738 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
739 struct old_itimerspec32 __user *, setting)
741 struct itimerspec64 cur_setting;
743 int ret = do_timer_gettime(timer_id, &cur_setting);
745 if (put_old_itimerspec32(&cur_setting, setting))
754 * Get the number of overruns of a POSIX.1b interval timer. This is to
755 * be the overrun of the timer last delivered. At the same time we are
756 * accumulating overruns on the next timer. The overrun is frozen when
757 * the signal is delivered, either at the notify time (if the info block
758 * is not queued) or at the actual delivery time (as we are informed by
759 * the call back to posixtimer_rearm(). So all we need to do is
760 * to pick up the frozen overrun.
762 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
764 struct k_itimer *timr;
768 timr = lock_timer(timer_id, &flags);
772 overrun = timer_overrun_to_int(timr, 0);
773 unlock_timer(timr, flags);
778 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
779 bool absolute, bool sigev_none)
781 struct hrtimer *timer = &timr->it.real.timer;
782 enum hrtimer_mode mode;
784 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
786 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
787 * clock modifications, so they become CLOCK_MONOTONIC based under the
788 * hood. See hrtimer_init(). Update timr->kclock, so the generic
789 * functions which use timr->kclock->clock_get() work.
791 * Note: it_clock stays unmodified, because the next timer_set() might
792 * use ABSTIME, so it needs to switch back.
794 if (timr->it_clock == CLOCK_REALTIME)
795 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
797 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
798 timr->it.real.timer.function = posix_timer_fn;
801 expires = ktime_add_safe(expires, timer->base->get_time());
802 hrtimer_set_expires(timer, expires);
805 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
808 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
810 return hrtimer_try_to_cancel(&timr->it.real.timer);
813 static void common_timer_wait_running(struct k_itimer *timer)
815 hrtimer_cancel_wait_running(&timer->it.real.timer);
819 * On PREEMPT_RT this prevent priority inversion against softirq kthread in
820 * case it gets preempted while executing a timer callback. See comments in
821 * hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a
824 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
825 unsigned long *flags)
827 const struct k_clock *kc = READ_ONCE(timer->kclock);
828 timer_t timer_id = READ_ONCE(timer->it_id);
830 /* Prevent kfree(timer) after dropping the lock */
832 unlock_timer(timer, *flags);
834 if (!WARN_ON_ONCE(!kc->timer_wait_running))
835 kc->timer_wait_running(timer);
838 /* Relock the timer. It might be not longer hashed. */
839 return lock_timer(timer_id, flags);
842 /* Set a POSIX.1b interval timer. */
843 int common_timer_set(struct k_itimer *timr, int flags,
844 struct itimerspec64 *new_setting,
845 struct itimerspec64 *old_setting)
847 const struct k_clock *kc = timr->kclock;
852 common_timer_get(timr, old_setting);
854 /* Prevent rearming by clearing the interval */
855 timr->it_interval = 0;
857 * Careful here. On SMP systems the timer expiry function could be
858 * active and spinning on timr->it_lock.
860 if (kc->timer_try_to_cancel(timr) < 0)
864 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
866 timr->it_overrun_last = 0;
868 /* Switch off the timer when it_value is zero */
869 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
872 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
873 expires = timespec64_to_ktime(new_setting->it_value);
874 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
876 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
877 timr->it_active = !sigev_none;
881 static int do_timer_settime(timer_t timer_id, int tmr_flags,
882 struct itimerspec64 *new_spec64,
883 struct itimerspec64 *old_spec64)
885 const struct k_clock *kc;
886 struct k_itimer *timr;
890 if (!timespec64_valid(&new_spec64->it_interval) ||
891 !timespec64_valid(&new_spec64->it_value))
895 memset(old_spec64, 0, sizeof(*old_spec64));
897 timr = lock_timer(timer_id, &flags);
903 if (WARN_ON_ONCE(!kc || !kc->timer_set))
906 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
908 if (error == TIMER_RETRY) {
909 // We already got the old time...
911 /* Unlocks and relocks the timer if it still exists */
912 timr = timer_wait_running(timr, &flags);
915 unlock_timer(timr, flags);
920 /* Set a POSIX.1b interval timer */
921 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
922 const struct __kernel_itimerspec __user *, new_setting,
923 struct __kernel_itimerspec __user *, old_setting)
925 struct itimerspec64 new_spec, old_spec;
926 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
932 if (get_itimerspec64(&new_spec, new_setting))
935 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
936 if (!error && old_setting) {
937 if (put_itimerspec64(&old_spec, old_setting))
943 #ifdef CONFIG_COMPAT_32BIT_TIME
944 SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
945 struct old_itimerspec32 __user *, new,
946 struct old_itimerspec32 __user *, old)
948 struct itimerspec64 new_spec, old_spec;
949 struct itimerspec64 *rtn = old ? &old_spec : NULL;
954 if (get_old_itimerspec32(&new_spec, new))
957 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
959 if (put_old_itimerspec32(&old_spec, old))
966 int common_timer_del(struct k_itimer *timer)
968 const struct k_clock *kc = timer->kclock;
970 timer->it_interval = 0;
971 if (kc->timer_try_to_cancel(timer) < 0)
973 timer->it_active = 0;
977 static inline int timer_delete_hook(struct k_itimer *timer)
979 const struct k_clock *kc = timer->kclock;
981 if (WARN_ON_ONCE(!kc || !kc->timer_del))
983 return kc->timer_del(timer);
986 /* Delete a POSIX.1b interval timer. */
987 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
989 struct k_itimer *timer;
992 timer = lock_timer(timer_id, &flags);
998 if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
999 /* Unlocks and relocks the timer if it still exists */
1000 timer = timer_wait_running(timer, &flags);
1004 spin_lock(¤t->sighand->siglock);
1005 list_del(&timer->list);
1006 spin_unlock(¤t->sighand->siglock);
1008 * This keeps any tasks waiting on the spin lock from thinking
1009 * they got something (see the lock code above).
1011 timer->it_signal = NULL;
1013 unlock_timer(timer, flags);
1014 release_posix_timer(timer, IT_ID_SET);
1019 * return timer owned by the process, used by exit_itimers
1021 static void itimer_delete(struct k_itimer *timer)
1024 spin_lock_irq(&timer->it_lock);
1026 if (timer_delete_hook(timer) == TIMER_RETRY) {
1027 spin_unlock_irq(&timer->it_lock);
1030 list_del(&timer->list);
1032 spin_unlock_irq(&timer->it_lock);
1033 release_posix_timer(timer, IT_ID_SET);
1037 * This is called by do_exit or de_thread, only when there are no more
1038 * references to the shared signal_struct.
1040 void exit_itimers(struct signal_struct *sig)
1042 struct k_itimer *tmr;
1044 while (!list_empty(&sig->posix_timers)) {
1045 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1050 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1051 const struct __kernel_timespec __user *, tp)
1053 const struct k_clock *kc = clockid_to_kclock(which_clock);
1054 struct timespec64 new_tp;
1056 if (!kc || !kc->clock_set)
1059 if (get_timespec64(&new_tp, tp))
1062 return kc->clock_set(which_clock, &new_tp);
1065 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1066 struct __kernel_timespec __user *, tp)
1068 const struct k_clock *kc = clockid_to_kclock(which_clock);
1069 struct timespec64 kernel_tp;
1075 error = kc->clock_get(which_clock, &kernel_tp);
1077 if (!error && put_timespec64(&kernel_tp, tp))
1083 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1085 const struct k_clock *kc = clockid_to_kclock(which_clock);
1092 return kc->clock_adj(which_clock, ktx);
1095 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1096 struct __kernel_timex __user *, utx)
1098 struct __kernel_timex ktx;
1101 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1104 err = do_clock_adjtime(which_clock, &ktx);
1106 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1112 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1113 struct __kernel_timespec __user *, tp)
1115 const struct k_clock *kc = clockid_to_kclock(which_clock);
1116 struct timespec64 rtn_tp;
1122 error = kc->clock_getres(which_clock, &rtn_tp);
1124 if (!error && tp && put_timespec64(&rtn_tp, tp))
1130 #ifdef CONFIG_COMPAT_32BIT_TIME
1132 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1133 struct old_timespec32 __user *, tp)
1135 const struct k_clock *kc = clockid_to_kclock(which_clock);
1136 struct timespec64 ts;
1138 if (!kc || !kc->clock_set)
1141 if (get_old_timespec32(&ts, tp))
1144 return kc->clock_set(which_clock, &ts);
1147 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1148 struct old_timespec32 __user *, tp)
1150 const struct k_clock *kc = clockid_to_kclock(which_clock);
1151 struct timespec64 ts;
1157 err = kc->clock_get(which_clock, &ts);
1159 if (!err && put_old_timespec32(&ts, tp))
1165 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1166 struct old_timex32 __user *, utp)
1168 struct __kernel_timex ktx;
1171 err = get_old_timex32(&ktx, utp);
1175 err = do_clock_adjtime(which_clock, &ktx);
1177 if (err >= 0 && put_old_timex32(utp, &ktx))
1183 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1184 struct old_timespec32 __user *, tp)
1186 const struct k_clock *kc = clockid_to_kclock(which_clock);
1187 struct timespec64 ts;
1193 err = kc->clock_getres(which_clock, &ts);
1194 if (!err && tp && put_old_timespec32(&ts, tp))
1203 * nanosleep for monotonic and realtime clocks
1205 static int common_nsleep(const clockid_t which_clock, int flags,
1206 const struct timespec64 *rqtp)
1208 return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1209 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1213 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1214 const struct __kernel_timespec __user *, rqtp,
1215 struct __kernel_timespec __user *, rmtp)
1217 const struct k_clock *kc = clockid_to_kclock(which_clock);
1218 struct timespec64 t;
1225 if (get_timespec64(&t, rqtp))
1228 if (!timespec64_valid(&t))
1230 if (flags & TIMER_ABSTIME)
1232 current->restart_block.fn = do_no_restart_syscall;
1233 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1234 current->restart_block.nanosleep.rmtp = rmtp;
1236 return kc->nsleep(which_clock, flags, &t);
1239 #ifdef CONFIG_COMPAT_32BIT_TIME
1241 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1242 struct old_timespec32 __user *, rqtp,
1243 struct old_timespec32 __user *, rmtp)
1245 const struct k_clock *kc = clockid_to_kclock(which_clock);
1246 struct timespec64 t;
1253 if (get_old_timespec32(&t, rqtp))
1256 if (!timespec64_valid(&t))
1258 if (flags & TIMER_ABSTIME)
1260 current->restart_block.fn = do_no_restart_syscall;
1261 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1262 current->restart_block.nanosleep.compat_rmtp = rmtp;
1264 return kc->nsleep(which_clock, flags, &t);
1269 static const struct k_clock clock_realtime = {
1270 .clock_getres = posix_get_hrtimer_res,
1271 .clock_get = posix_clock_realtime_get,
1272 .clock_set = posix_clock_realtime_set,
1273 .clock_adj = posix_clock_realtime_adj,
1274 .nsleep = common_nsleep,
1275 .timer_create = common_timer_create,
1276 .timer_set = common_timer_set,
1277 .timer_get = common_timer_get,
1278 .timer_del = common_timer_del,
1279 .timer_rearm = common_hrtimer_rearm,
1280 .timer_forward = common_hrtimer_forward,
1281 .timer_remaining = common_hrtimer_remaining,
1282 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1283 .timer_wait_running = common_timer_wait_running,
1284 .timer_arm = common_hrtimer_arm,
1287 static const struct k_clock clock_monotonic = {
1288 .clock_getres = posix_get_hrtimer_res,
1289 .clock_get = posix_ktime_get_ts,
1290 .nsleep = common_nsleep,
1291 .timer_create = common_timer_create,
1292 .timer_set = common_timer_set,
1293 .timer_get = common_timer_get,
1294 .timer_del = common_timer_del,
1295 .timer_rearm = common_hrtimer_rearm,
1296 .timer_forward = common_hrtimer_forward,
1297 .timer_remaining = common_hrtimer_remaining,
1298 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1299 .timer_wait_running = common_timer_wait_running,
1300 .timer_arm = common_hrtimer_arm,
1303 static const struct k_clock clock_monotonic_raw = {
1304 .clock_getres = posix_get_hrtimer_res,
1305 .clock_get = posix_get_monotonic_raw,
1308 static const struct k_clock clock_realtime_coarse = {
1309 .clock_getres = posix_get_coarse_res,
1310 .clock_get = posix_get_realtime_coarse,
1313 static const struct k_clock clock_monotonic_coarse = {
1314 .clock_getres = posix_get_coarse_res,
1315 .clock_get = posix_get_monotonic_coarse,
1318 static const struct k_clock clock_tai = {
1319 .clock_getres = posix_get_hrtimer_res,
1320 .clock_get = posix_get_tai,
1321 .nsleep = common_nsleep,
1322 .timer_create = common_timer_create,
1323 .timer_set = common_timer_set,
1324 .timer_get = common_timer_get,
1325 .timer_del = common_timer_del,
1326 .timer_rearm = common_hrtimer_rearm,
1327 .timer_forward = common_hrtimer_forward,
1328 .timer_remaining = common_hrtimer_remaining,
1329 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1330 .timer_wait_running = common_timer_wait_running,
1331 .timer_arm = common_hrtimer_arm,
1334 static const struct k_clock clock_boottime = {
1335 .clock_getres = posix_get_hrtimer_res,
1336 .clock_get = posix_get_boottime,
1337 .nsleep = common_nsleep,
1338 .timer_create = common_timer_create,
1339 .timer_set = common_timer_set,
1340 .timer_get = common_timer_get,
1341 .timer_del = common_timer_del,
1342 .timer_rearm = common_hrtimer_rearm,
1343 .timer_forward = common_hrtimer_forward,
1344 .timer_remaining = common_hrtimer_remaining,
1345 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1346 .timer_wait_running = common_timer_wait_running,
1347 .timer_arm = common_hrtimer_arm,
1350 static const struct k_clock * const posix_clocks[] = {
1351 [CLOCK_REALTIME] = &clock_realtime,
1352 [CLOCK_MONOTONIC] = &clock_monotonic,
1353 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1354 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1355 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1356 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1357 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1358 [CLOCK_BOOTTIME] = &clock_boottime,
1359 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1360 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1361 [CLOCK_TAI] = &clock_tai,
1364 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1369 return (id & CLOCKFD_MASK) == CLOCKFD ?
1370 &clock_posix_dynamic : &clock_posix_cpu;
1373 if (id >= ARRAY_SIZE(posix_clocks))
1376 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];