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>
33 #include <linux/time_namespace.h>
35 #include "timekeeping.h"
36 #include "posix-timers.h"
39 * Management arrays for POSIX timers. Timers are now kept in static hash table
41 * Timer ids are allocated by local routine, which selects proper hash head by
42 * key, constructed from current->signal address and per signal struct counter.
43 * This keeps timer ids unique per process, but now they can intersect between
48 * Lets keep our timers in a slab cache :-)
50 static struct kmem_cache *posix_timers_cache;
52 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
53 static DEFINE_SPINLOCK(hash_lock);
55 static const struct k_clock * const posix_clocks[];
56 static const struct k_clock *clockid_to_kclock(const clockid_t id);
57 static const struct k_clock clock_realtime, clock_monotonic;
60 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
61 * SIGEV values. Here we put out an error if this assumption fails.
63 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
64 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
65 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
69 * The timer ID is turned into a timer address by idr_find().
70 * Verifying a valid ID consists of:
72 * a) checking that idr_find() returns other than -1.
73 * b) checking that the timer id matches the one in the timer itself.
74 * c) that the timer owner is in the callers thread group.
78 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
79 * to implement others. This structure defines the various
82 * RESOLUTION: Clock resolution is used to round up timer and interval
83 * times, NOT to report clock times, which are reported with as
84 * much resolution as the system can muster. In some cases this
85 * resolution may depend on the underlying clock hardware and
86 * may not be quantifiable until run time, and only then is the
87 * necessary code is written. The standard says we should say
88 * something about this issue in the documentation...
90 * FUNCTIONS: The CLOCKs structure defines possible functions to
91 * handle various clock functions.
93 * The standard POSIX timer management code assumes the
94 * following: 1.) The k_itimer struct (sched.h) is used for
95 * the timer. 2.) The list, it_lock, it_clock, it_id and
96 * it_pid fields are not modified by timer code.
98 * Permissions: It is assumed that the clock_settime() function defined
99 * for each clock will take care of permission checks. Some
100 * clocks may be set able by any user (i.e. local process
101 * clocks) others not. Currently the only set able clock we
102 * have is CLOCK_REALTIME and its high res counter part, both of
103 * which we beg off on and pass to do_sys_settimeofday().
105 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
107 #define lock_timer(tid, flags) \
108 ({ struct k_itimer *__timr; \
109 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
113 static int hash(struct signal_struct *sig, unsigned int nr)
115 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
118 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
119 struct signal_struct *sig,
122 struct k_itimer *timer;
124 hlist_for_each_entry_rcu(timer, head, t_hash,
125 lockdep_is_held(&hash_lock)) {
126 if ((timer->it_signal == sig) && (timer->it_id == id))
132 static struct k_itimer *posix_timer_by_id(timer_t id)
134 struct signal_struct *sig = current->signal;
135 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
137 return __posix_timers_find(head, sig, id);
140 static int posix_timer_add(struct k_itimer *timer)
142 struct signal_struct *sig = current->signal;
143 struct hlist_head *head;
144 unsigned int cnt, id;
147 * FIXME: Replace this by a per signal struct xarray once there is
148 * a plan to handle the resulting CRIU regression gracefully.
150 for (cnt = 0; cnt <= INT_MAX; cnt++) {
151 spin_lock(&hash_lock);
152 id = sig->next_posix_timer_id;
154 /* Write the next ID back. Clamp it to the positive space */
155 sig->next_posix_timer_id = (id + 1) & INT_MAX;
157 head = &posix_timers_hashtable[hash(sig, id)];
158 if (!__posix_timers_find(head, sig, id)) {
159 hlist_add_head_rcu(&timer->t_hash, head);
160 spin_unlock(&hash_lock);
163 spin_unlock(&hash_lock);
165 /* POSIX return code when no timer ID could be allocated */
169 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
171 spin_unlock_irqrestore(&timr->it_lock, flags);
174 /* Get clock_realtime */
175 static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
177 ktime_get_real_ts64(tp);
181 static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
183 return ktime_get_real();
186 /* Set clock_realtime */
187 static int posix_clock_realtime_set(const clockid_t which_clock,
188 const struct timespec64 *tp)
190 return do_sys_settimeofday64(tp, NULL);
193 static int posix_clock_realtime_adj(const clockid_t which_clock,
194 struct __kernel_timex *t)
196 return do_adjtimex(t);
200 * Get monotonic time for posix timers
202 static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
205 timens_add_monotonic(tp);
209 static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
215 * Get monotonic-raw time for posix timers
217 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
219 ktime_get_raw_ts64(tp);
220 timens_add_monotonic(tp);
225 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
227 ktime_get_coarse_real_ts64(tp);
231 static int posix_get_monotonic_coarse(clockid_t which_clock,
232 struct timespec64 *tp)
234 ktime_get_coarse_ts64(tp);
235 timens_add_monotonic(tp);
239 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
241 *tp = ktime_to_timespec64(KTIME_LOW_RES);
245 static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
247 ktime_get_boottime_ts64(tp);
248 timens_add_boottime(tp);
252 static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
254 return ktime_get_boottime();
257 static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
259 ktime_get_clocktai_ts64(tp);
263 static ktime_t posix_get_tai_ktime(clockid_t which_clock)
265 return ktime_get_clocktai();
268 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
271 tp->tv_nsec = hrtimer_resolution;
276 * Initialize everything, well, just everything in Posix clocks/timers ;)
278 static __init int init_posix_timers(void)
280 posix_timers_cache = kmem_cache_create("posix_timers_cache",
281 sizeof (struct k_itimer), 0, SLAB_PANIC,
285 __initcall(init_posix_timers);
288 * The siginfo si_overrun field and the return value of timer_getoverrun(2)
289 * are of type int. Clamp the overrun value to INT_MAX
291 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
293 s64 sum = timr->it_overrun_last + (s64)baseval;
295 return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
298 static void common_hrtimer_rearm(struct k_itimer *timr)
300 struct hrtimer *timer = &timr->it.real.timer;
302 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
304 hrtimer_restart(timer);
308 * This function is exported for use by the signal deliver code. It is
309 * called just prior to the info block being released and passes that
310 * block to us. It's function is to update the overrun entry AND to
311 * restart the timer. It should only be called if the timer is to be
312 * restarted (i.e. we have flagged this in the sys_private entry of the
315 * To protect against the timer going away while the interrupt is queued,
316 * we require that the it_requeue_pending flag be set.
318 void posixtimer_rearm(struct kernel_siginfo *info)
320 struct k_itimer *timr;
323 timr = lock_timer(info->si_tid, &flags);
327 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
328 timr->kclock->timer_rearm(timr);
331 timr->it_overrun_last = timr->it_overrun;
332 timr->it_overrun = -1LL;
333 ++timr->it_requeue_pending;
335 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
338 unlock_timer(timr, flags);
341 int posix_timer_event(struct k_itimer *timr, int si_private)
346 * FIXME: if ->sigq is queued we can race with
347 * dequeue_signal()->posixtimer_rearm().
349 * If dequeue_signal() sees the "right" value of
350 * si_sys_private it calls posixtimer_rearm().
351 * We re-queue ->sigq and drop ->it_lock().
352 * posixtimer_rearm() locks the timer
353 * and re-schedules it while ->sigq is pending.
354 * Not really bad, but not that we want.
356 timr->sigq->info.si_sys_private = si_private;
358 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
359 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
360 /* If we failed to send the signal the timer stops. */
365 * This function gets called when a POSIX.1b interval timer expires. It
366 * is used as a callback from the kernel internal timer. The
367 * run_timer_list code ALWAYS calls with interrupts on.
369 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
371 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
373 struct k_itimer *timr;
376 enum hrtimer_restart ret = HRTIMER_NORESTART;
378 timr = container_of(timer, struct k_itimer, it.real.timer);
379 spin_lock_irqsave(&timr->it_lock, flags);
382 if (timr->it_interval != 0)
383 si_private = ++timr->it_requeue_pending;
385 if (posix_timer_event(timr, si_private)) {
387 * signal was not sent because of sig_ignor
388 * we will not get a call back to restart it AND
389 * it should be restarted.
391 if (timr->it_interval != 0) {
392 ktime_t now = hrtimer_cb_get_time(timer);
395 * FIXME: What we really want, is to stop this
396 * timer completely and restart it in case the
397 * SIG_IGN is removed. This is a non trivial
398 * change which involves sighand locking
399 * (sigh !), which we don't want to do late in
402 * For now we just let timers with an interval
403 * less than a jiffie expire every jiffie to
404 * avoid softirq starvation in case of SIG_IGN
405 * and a very small interval, which would put
406 * the timer right back on the softirq pending
407 * list. By moving now ahead of time we trick
408 * hrtimer_forward() to expire the timer
409 * later, while we still maintain the overrun
410 * accuracy, but have some inconsistency in
411 * the timer_gettime() case. This is at least
412 * better than a starved softirq. A more
413 * complex fix which solves also another related
414 * inconsistency is already in the pipeline.
416 #ifdef CONFIG_HIGH_RES_TIMERS
418 ktime_t kj = NSEC_PER_SEC / HZ;
420 if (timr->it_interval < kj)
421 now = ktime_add(now, kj);
424 timr->it_overrun += hrtimer_forward(timer, now,
426 ret = HRTIMER_RESTART;
427 ++timr->it_requeue_pending;
432 unlock_timer(timr, flags);
436 static struct pid *good_sigevent(sigevent_t * event)
438 struct pid *pid = task_tgid(current);
439 struct task_struct *rtn;
441 switch (event->sigev_notify) {
442 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
443 pid = find_vpid(event->sigev_notify_thread_id);
444 rtn = pid_task(pid, PIDTYPE_PID);
445 if (!rtn || !same_thread_group(rtn, current))
450 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
460 static struct k_itimer * alloc_posix_timer(void)
462 struct k_itimer *tmr;
463 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
466 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
467 kmem_cache_free(posix_timers_cache, tmr);
470 clear_siginfo(&tmr->sigq->info);
474 static void k_itimer_rcu_free(struct rcu_head *head)
476 struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
478 kmem_cache_free(posix_timers_cache, tmr);
482 #define IT_ID_NOT_SET 0
483 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
487 spin_lock_irqsave(&hash_lock, flags);
488 hlist_del_rcu(&tmr->t_hash);
489 spin_unlock_irqrestore(&hash_lock, flags);
491 put_pid(tmr->it_pid);
492 sigqueue_free(tmr->sigq);
493 call_rcu(&tmr->rcu, k_itimer_rcu_free);
496 static int common_timer_create(struct k_itimer *new_timer)
498 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
502 /* Create a POSIX.1b interval timer. */
503 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
504 timer_t __user *created_timer_id)
506 const struct k_clock *kc = clockid_to_kclock(which_clock);
507 struct k_itimer *new_timer;
508 int error, new_timer_id;
509 int it_id_set = IT_ID_NOT_SET;
513 if (!kc->timer_create)
516 new_timer = alloc_posix_timer();
517 if (unlikely(!new_timer))
520 spin_lock_init(&new_timer->it_lock);
521 new_timer_id = posix_timer_add(new_timer);
522 if (new_timer_id < 0) {
523 error = new_timer_id;
527 it_id_set = IT_ID_SET;
528 new_timer->it_id = (timer_t) new_timer_id;
529 new_timer->it_clock = which_clock;
530 new_timer->kclock = kc;
531 new_timer->it_overrun = -1LL;
535 new_timer->it_pid = get_pid(good_sigevent(event));
537 if (!new_timer->it_pid) {
541 new_timer->it_sigev_notify = event->sigev_notify;
542 new_timer->sigq->info.si_signo = event->sigev_signo;
543 new_timer->sigq->info.si_value = event->sigev_value;
545 new_timer->it_sigev_notify = SIGEV_SIGNAL;
546 new_timer->sigq->info.si_signo = SIGALRM;
547 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
548 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
549 new_timer->it_pid = get_pid(task_tgid(current));
552 new_timer->sigq->info.si_tid = new_timer->it_id;
553 new_timer->sigq->info.si_code = SI_TIMER;
555 if (copy_to_user(created_timer_id,
556 &new_timer_id, sizeof (new_timer_id))) {
561 error = kc->timer_create(new_timer);
565 spin_lock_irq(¤t->sighand->siglock);
566 new_timer->it_signal = current->signal;
567 list_add(&new_timer->list, ¤t->signal->posix_timers);
568 spin_unlock_irq(¤t->sighand->siglock);
572 * In the case of the timer belonging to another task, after
573 * the task is unlocked, the timer is owned by the other task
574 * and may cease to exist at any time. Don't use or modify
575 * new_timer after the unlock call.
578 release_posix_timer(new_timer, it_id_set);
582 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
583 struct sigevent __user *, timer_event_spec,
584 timer_t __user *, created_timer_id)
586 if (timer_event_spec) {
589 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
591 return do_timer_create(which_clock, &event, created_timer_id);
593 return do_timer_create(which_clock, NULL, created_timer_id);
597 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
598 struct compat_sigevent __user *, timer_event_spec,
599 timer_t __user *, created_timer_id)
601 if (timer_event_spec) {
604 if (get_compat_sigevent(&event, timer_event_spec))
606 return do_timer_create(which_clock, &event, created_timer_id);
608 return do_timer_create(which_clock, NULL, created_timer_id);
613 * Locking issues: We need to protect the result of the id look up until
614 * we get the timer locked down so it is not deleted under us. The
615 * removal is done under the idr spinlock so we use that here to bridge
616 * the find to the timer lock. To avoid a dead lock, the timer id MUST
617 * be release with out holding the timer lock.
619 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
621 struct k_itimer *timr;
624 * timer_t could be any type >= int and we want to make sure any
625 * @timer_id outside positive int range fails lookup.
627 if ((unsigned long long)timer_id > INT_MAX)
631 timr = posix_timer_by_id(timer_id);
633 spin_lock_irqsave(&timr->it_lock, *flags);
634 if (timr->it_signal == current->signal) {
638 spin_unlock_irqrestore(&timr->it_lock, *flags);
645 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
647 struct hrtimer *timer = &timr->it.real.timer;
649 return __hrtimer_expires_remaining_adjusted(timer, now);
652 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
654 struct hrtimer *timer = &timr->it.real.timer;
656 return hrtimer_forward(timer, now, timr->it_interval);
660 * Get the time remaining on a POSIX.1b interval timer. This function
661 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
664 * We have a couple of messes to clean up here. First there is the case
665 * of a timer that has a requeue pending. These timers should appear to
666 * be in the timer list with an expiry as if we were to requeue them
669 * The second issue is the SIGEV_NONE timer which may be active but is
670 * not really ever put in the timer list (to save system resources).
671 * This timer may be expired, and if so, we will do it here. Otherwise
672 * it is the same as a requeue pending timer WRT to what we should
675 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
677 const struct k_clock *kc = timr->kclock;
678 ktime_t now, remaining, iv;
681 sig_none = timr->it_sigev_notify == SIGEV_NONE;
682 iv = timr->it_interval;
684 /* interval timer ? */
686 cur_setting->it_interval = ktime_to_timespec64(iv);
687 } else if (!timr->it_active) {
689 * SIGEV_NONE oneshot timers are never queued. Check them
696 now = kc->clock_get_ktime(timr->it_clock);
699 * When a requeue is pending or this is a SIGEV_NONE timer move the
700 * expiry time forward by intervals, so expiry is > now.
702 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
703 timr->it_overrun += kc->timer_forward(timr, now);
705 remaining = kc->timer_remaining(timr, now);
706 /* Return 0 only, when the timer is expired and not pending */
707 if (remaining <= 0) {
709 * A single shot SIGEV_NONE timer must return 0, when
713 cur_setting->it_value.tv_nsec = 1;
715 cur_setting->it_value = ktime_to_timespec64(remaining);
719 /* Get the time remaining on a POSIX.1b interval timer. */
720 static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
722 struct k_itimer *timr;
723 const struct k_clock *kc;
727 timr = lock_timer(timer_id, &flags);
731 memset(setting, 0, sizeof(*setting));
733 if (WARN_ON_ONCE(!kc || !kc->timer_get))
736 kc->timer_get(timr, setting);
738 unlock_timer(timr, flags);
742 /* Get the time remaining on a POSIX.1b interval timer. */
743 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
744 struct __kernel_itimerspec __user *, setting)
746 struct itimerspec64 cur_setting;
748 int ret = do_timer_gettime(timer_id, &cur_setting);
750 if (put_itimerspec64(&cur_setting, setting))
756 #ifdef CONFIG_COMPAT_32BIT_TIME
758 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
759 struct old_itimerspec32 __user *, setting)
761 struct itimerspec64 cur_setting;
763 int ret = do_timer_gettime(timer_id, &cur_setting);
765 if (put_old_itimerspec32(&cur_setting, setting))
774 * Get the number of overruns of a POSIX.1b interval timer. This is to
775 * be the overrun of the timer last delivered. At the same time we are
776 * accumulating overruns on the next timer. The overrun is frozen when
777 * the signal is delivered, either at the notify time (if the info block
778 * is not queued) or at the actual delivery time (as we are informed by
779 * the call back to posixtimer_rearm(). So all we need to do is
780 * to pick up the frozen overrun.
782 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
784 struct k_itimer *timr;
788 timr = lock_timer(timer_id, &flags);
792 overrun = timer_overrun_to_int(timr, 0);
793 unlock_timer(timr, flags);
798 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
799 bool absolute, bool sigev_none)
801 struct hrtimer *timer = &timr->it.real.timer;
802 enum hrtimer_mode mode;
804 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
806 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
807 * clock modifications, so they become CLOCK_MONOTONIC based under the
808 * hood. See hrtimer_init(). Update timr->kclock, so the generic
809 * functions which use timr->kclock->clock_get_*() work.
811 * Note: it_clock stays unmodified, because the next timer_set() might
812 * use ABSTIME, so it needs to switch back.
814 if (timr->it_clock == CLOCK_REALTIME)
815 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
817 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
818 timr->it.real.timer.function = posix_timer_fn;
821 expires = ktime_add_safe(expires, timer->base->get_time());
822 hrtimer_set_expires(timer, expires);
825 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
828 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
830 return hrtimer_try_to_cancel(&timr->it.real.timer);
833 static void common_timer_wait_running(struct k_itimer *timer)
835 hrtimer_cancel_wait_running(&timer->it.real.timer);
839 * On PREEMPT_RT this prevent priority inversion against softirq kthread in
840 * case it gets preempted while executing a timer callback. See comments in
841 * hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a
844 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
845 unsigned long *flags)
847 const struct k_clock *kc = READ_ONCE(timer->kclock);
848 timer_t timer_id = READ_ONCE(timer->it_id);
850 /* Prevent kfree(timer) after dropping the lock */
852 unlock_timer(timer, *flags);
855 * kc->timer_wait_running() might drop RCU lock. So @timer
856 * cannot be touched anymore after the function returns!
858 if (!WARN_ON_ONCE(!kc->timer_wait_running))
859 kc->timer_wait_running(timer);
862 /* Relock the timer. It might be not longer hashed. */
863 return lock_timer(timer_id, flags);
866 /* Set a POSIX.1b interval timer. */
867 int common_timer_set(struct k_itimer *timr, int flags,
868 struct itimerspec64 *new_setting,
869 struct itimerspec64 *old_setting)
871 const struct k_clock *kc = timr->kclock;
876 common_timer_get(timr, old_setting);
878 /* Prevent rearming by clearing the interval */
879 timr->it_interval = 0;
881 * Careful here. On SMP systems the timer expiry function could be
882 * active and spinning on timr->it_lock.
884 if (kc->timer_try_to_cancel(timr) < 0)
888 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
890 timr->it_overrun_last = 0;
892 /* Switch off the timer when it_value is zero */
893 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
896 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
897 expires = timespec64_to_ktime(new_setting->it_value);
898 if (flags & TIMER_ABSTIME)
899 expires = timens_ktime_to_host(timr->it_clock, expires);
900 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
902 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
903 timr->it_active = !sigev_none;
907 static int do_timer_settime(timer_t timer_id, int tmr_flags,
908 struct itimerspec64 *new_spec64,
909 struct itimerspec64 *old_spec64)
911 const struct k_clock *kc;
912 struct k_itimer *timr;
916 if (!timespec64_valid(&new_spec64->it_interval) ||
917 !timespec64_valid(&new_spec64->it_value))
921 memset(old_spec64, 0, sizeof(*old_spec64));
923 timr = lock_timer(timer_id, &flags);
929 if (WARN_ON_ONCE(!kc || !kc->timer_set))
932 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
934 if (error == TIMER_RETRY) {
935 // We already got the old time...
937 /* Unlocks and relocks the timer if it still exists */
938 timr = timer_wait_running(timr, &flags);
941 unlock_timer(timr, flags);
946 /* Set a POSIX.1b interval timer */
947 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
948 const struct __kernel_itimerspec __user *, new_setting,
949 struct __kernel_itimerspec __user *, old_setting)
951 struct itimerspec64 new_spec, old_spec;
952 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
958 if (get_itimerspec64(&new_spec, new_setting))
961 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
962 if (!error && old_setting) {
963 if (put_itimerspec64(&old_spec, old_setting))
969 #ifdef CONFIG_COMPAT_32BIT_TIME
970 SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
971 struct old_itimerspec32 __user *, new,
972 struct old_itimerspec32 __user *, old)
974 struct itimerspec64 new_spec, old_spec;
975 struct itimerspec64 *rtn = old ? &old_spec : NULL;
980 if (get_old_itimerspec32(&new_spec, new))
983 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
985 if (put_old_itimerspec32(&old_spec, old))
992 int common_timer_del(struct k_itimer *timer)
994 const struct k_clock *kc = timer->kclock;
996 timer->it_interval = 0;
997 if (kc->timer_try_to_cancel(timer) < 0)
999 timer->it_active = 0;
1003 static inline int timer_delete_hook(struct k_itimer *timer)
1005 const struct k_clock *kc = timer->kclock;
1007 if (WARN_ON_ONCE(!kc || !kc->timer_del))
1009 return kc->timer_del(timer);
1012 /* Delete a POSIX.1b interval timer. */
1013 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
1015 struct k_itimer *timer;
1016 unsigned long flags;
1018 timer = lock_timer(timer_id, &flags);
1024 if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
1025 /* Unlocks and relocks the timer if it still exists */
1026 timer = timer_wait_running(timer, &flags);
1030 spin_lock(¤t->sighand->siglock);
1031 list_del(&timer->list);
1032 spin_unlock(¤t->sighand->siglock);
1034 * This keeps any tasks waiting on the spin lock from thinking
1035 * they got something (see the lock code above).
1037 timer->it_signal = NULL;
1039 unlock_timer(timer, flags);
1040 release_posix_timer(timer, IT_ID_SET);
1045 * Delete a timer if it is armed, remove it from the hash and schedule it
1048 static void itimer_delete(struct k_itimer *timer)
1050 unsigned long flags;
1053 * irqsave is required to make timer_wait_running() work.
1055 spin_lock_irqsave(&timer->it_lock, flags);
1059 * Even if the timer is not longer accessible from other tasks
1060 * it still might be armed and queued in the underlying timer
1061 * mechanism. Worse, that timer mechanism might run the expiry
1062 * function concurrently.
1064 if (timer_delete_hook(timer) == TIMER_RETRY) {
1066 * Timer is expired concurrently, prevent livelocks
1067 * and pointless spinning on RT.
1069 * timer_wait_running() drops timer::it_lock, which opens
1070 * the possibility for another task to delete the timer.
1072 * That's not possible here because this is invoked from
1073 * do_exit() only for the last thread of the thread group.
1074 * So no other task can access and delete that timer.
1076 if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
1081 list_del(&timer->list);
1083 spin_unlock_irqrestore(&timer->it_lock, flags);
1084 release_posix_timer(timer, IT_ID_SET);
1088 * Invoked from do_exit() when the last thread of a thread group exits.
1089 * At that point no other task can access the timers of the dying
1092 void exit_itimers(struct task_struct *tsk)
1094 struct list_head timers;
1095 struct k_itimer *tmr;
1097 if (list_empty(&tsk->signal->posix_timers))
1100 /* Protect against concurrent read via /proc/$PID/timers */
1101 spin_lock_irq(&tsk->sighand->siglock);
1102 list_replace_init(&tsk->signal->posix_timers, &timers);
1103 spin_unlock_irq(&tsk->sighand->siglock);
1105 /* The timers are not longer accessible via tsk::signal */
1106 while (!list_empty(&timers)) {
1107 tmr = list_first_entry(&timers, struct k_itimer, list);
1112 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1113 const struct __kernel_timespec __user *, tp)
1115 const struct k_clock *kc = clockid_to_kclock(which_clock);
1116 struct timespec64 new_tp;
1118 if (!kc || !kc->clock_set)
1121 if (get_timespec64(&new_tp, tp))
1124 return kc->clock_set(which_clock, &new_tp);
1127 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1128 struct __kernel_timespec __user *, tp)
1130 const struct k_clock *kc = clockid_to_kclock(which_clock);
1131 struct timespec64 kernel_tp;
1137 error = kc->clock_get_timespec(which_clock, &kernel_tp);
1139 if (!error && put_timespec64(&kernel_tp, tp))
1145 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1147 const struct k_clock *kc = clockid_to_kclock(which_clock);
1154 return kc->clock_adj(which_clock, ktx);
1157 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1158 struct __kernel_timex __user *, utx)
1160 struct __kernel_timex ktx;
1163 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1166 err = do_clock_adjtime(which_clock, &ktx);
1168 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1174 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1175 struct __kernel_timespec __user *, tp)
1177 const struct k_clock *kc = clockid_to_kclock(which_clock);
1178 struct timespec64 rtn_tp;
1184 error = kc->clock_getres(which_clock, &rtn_tp);
1186 if (!error && tp && put_timespec64(&rtn_tp, tp))
1192 #ifdef CONFIG_COMPAT_32BIT_TIME
1194 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1195 struct old_timespec32 __user *, tp)
1197 const struct k_clock *kc = clockid_to_kclock(which_clock);
1198 struct timespec64 ts;
1200 if (!kc || !kc->clock_set)
1203 if (get_old_timespec32(&ts, tp))
1206 return kc->clock_set(which_clock, &ts);
1209 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1210 struct old_timespec32 __user *, tp)
1212 const struct k_clock *kc = clockid_to_kclock(which_clock);
1213 struct timespec64 ts;
1219 err = kc->clock_get_timespec(which_clock, &ts);
1221 if (!err && put_old_timespec32(&ts, tp))
1227 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1228 struct old_timex32 __user *, utp)
1230 struct __kernel_timex ktx;
1233 err = get_old_timex32(&ktx, utp);
1237 err = do_clock_adjtime(which_clock, &ktx);
1239 if (err >= 0 && put_old_timex32(utp, &ktx))
1245 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1246 struct old_timespec32 __user *, tp)
1248 const struct k_clock *kc = clockid_to_kclock(which_clock);
1249 struct timespec64 ts;
1255 err = kc->clock_getres(which_clock, &ts);
1256 if (!err && tp && put_old_timespec32(&ts, tp))
1265 * nanosleep for monotonic and realtime clocks
1267 static int common_nsleep(const clockid_t which_clock, int flags,
1268 const struct timespec64 *rqtp)
1270 ktime_t texp = timespec64_to_ktime(*rqtp);
1272 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1273 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1277 static int common_nsleep_timens(const clockid_t which_clock, int flags,
1278 const struct timespec64 *rqtp)
1280 ktime_t texp = timespec64_to_ktime(*rqtp);
1282 if (flags & TIMER_ABSTIME)
1283 texp = timens_ktime_to_host(which_clock, texp);
1285 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1286 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1290 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1291 const struct __kernel_timespec __user *, rqtp,
1292 struct __kernel_timespec __user *, rmtp)
1294 const struct k_clock *kc = clockid_to_kclock(which_clock);
1295 struct timespec64 t;
1302 if (get_timespec64(&t, rqtp))
1305 if (!timespec64_valid(&t))
1307 if (flags & TIMER_ABSTIME)
1309 current->restart_block.fn = do_no_restart_syscall;
1310 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1311 current->restart_block.nanosleep.rmtp = rmtp;
1313 return kc->nsleep(which_clock, flags, &t);
1316 #ifdef CONFIG_COMPAT_32BIT_TIME
1318 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1319 struct old_timespec32 __user *, rqtp,
1320 struct old_timespec32 __user *, rmtp)
1322 const struct k_clock *kc = clockid_to_kclock(which_clock);
1323 struct timespec64 t;
1330 if (get_old_timespec32(&t, rqtp))
1333 if (!timespec64_valid(&t))
1335 if (flags & TIMER_ABSTIME)
1337 current->restart_block.fn = do_no_restart_syscall;
1338 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1339 current->restart_block.nanosleep.compat_rmtp = rmtp;
1341 return kc->nsleep(which_clock, flags, &t);
1346 static const struct k_clock clock_realtime = {
1347 .clock_getres = posix_get_hrtimer_res,
1348 .clock_get_timespec = posix_get_realtime_timespec,
1349 .clock_get_ktime = posix_get_realtime_ktime,
1350 .clock_set = posix_clock_realtime_set,
1351 .clock_adj = posix_clock_realtime_adj,
1352 .nsleep = common_nsleep,
1353 .timer_create = common_timer_create,
1354 .timer_set = common_timer_set,
1355 .timer_get = common_timer_get,
1356 .timer_del = common_timer_del,
1357 .timer_rearm = common_hrtimer_rearm,
1358 .timer_forward = common_hrtimer_forward,
1359 .timer_remaining = common_hrtimer_remaining,
1360 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1361 .timer_wait_running = common_timer_wait_running,
1362 .timer_arm = common_hrtimer_arm,
1365 static const struct k_clock clock_monotonic = {
1366 .clock_getres = posix_get_hrtimer_res,
1367 .clock_get_timespec = posix_get_monotonic_timespec,
1368 .clock_get_ktime = posix_get_monotonic_ktime,
1369 .nsleep = common_nsleep_timens,
1370 .timer_create = common_timer_create,
1371 .timer_set = common_timer_set,
1372 .timer_get = common_timer_get,
1373 .timer_del = common_timer_del,
1374 .timer_rearm = common_hrtimer_rearm,
1375 .timer_forward = common_hrtimer_forward,
1376 .timer_remaining = common_hrtimer_remaining,
1377 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1378 .timer_wait_running = common_timer_wait_running,
1379 .timer_arm = common_hrtimer_arm,
1382 static const struct k_clock clock_monotonic_raw = {
1383 .clock_getres = posix_get_hrtimer_res,
1384 .clock_get_timespec = posix_get_monotonic_raw,
1387 static const struct k_clock clock_realtime_coarse = {
1388 .clock_getres = posix_get_coarse_res,
1389 .clock_get_timespec = posix_get_realtime_coarse,
1392 static const struct k_clock clock_monotonic_coarse = {
1393 .clock_getres = posix_get_coarse_res,
1394 .clock_get_timespec = posix_get_monotonic_coarse,
1397 static const struct k_clock clock_tai = {
1398 .clock_getres = posix_get_hrtimer_res,
1399 .clock_get_ktime = posix_get_tai_ktime,
1400 .clock_get_timespec = posix_get_tai_timespec,
1401 .nsleep = common_nsleep,
1402 .timer_create = common_timer_create,
1403 .timer_set = common_timer_set,
1404 .timer_get = common_timer_get,
1405 .timer_del = common_timer_del,
1406 .timer_rearm = common_hrtimer_rearm,
1407 .timer_forward = common_hrtimer_forward,
1408 .timer_remaining = common_hrtimer_remaining,
1409 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1410 .timer_wait_running = common_timer_wait_running,
1411 .timer_arm = common_hrtimer_arm,
1414 static const struct k_clock clock_boottime = {
1415 .clock_getres = posix_get_hrtimer_res,
1416 .clock_get_ktime = posix_get_boottime_ktime,
1417 .clock_get_timespec = posix_get_boottime_timespec,
1418 .nsleep = common_nsleep_timens,
1419 .timer_create = common_timer_create,
1420 .timer_set = common_timer_set,
1421 .timer_get = common_timer_get,
1422 .timer_del = common_timer_del,
1423 .timer_rearm = common_hrtimer_rearm,
1424 .timer_forward = common_hrtimer_forward,
1425 .timer_remaining = common_hrtimer_remaining,
1426 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1427 .timer_wait_running = common_timer_wait_running,
1428 .timer_arm = common_hrtimer_arm,
1431 static const struct k_clock * const posix_clocks[] = {
1432 [CLOCK_REALTIME] = &clock_realtime,
1433 [CLOCK_MONOTONIC] = &clock_monotonic,
1434 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1435 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1436 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1437 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1438 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1439 [CLOCK_BOOTTIME] = &clock_boottime,
1440 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1441 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1442 [CLOCK_TAI] = &clock_tai,
1445 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1450 return (id & CLOCKFD_MASK) == CLOCKFD ?
1451 &clock_posix_dynamic : &clock_posix_cpu;
1454 if (id >= ARRAY_SIZE(posix_clocks))
1457 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];