Lines Matching +full:no +full:- +full:tick +full:- +full:in +full:- +full:suspend
1 // SPDX-License-Identifier: GPL-2.0+
3 * 2002-10-15 Posix Clocks & timers
7 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
24 #include <linux/posix-clock.h>
25 #include <linux/posix-timers.h>
36 #include "posix-timers.h"
41 * Timers are managed in a hash table for lockless lookup. The hash key is
66 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
82 /* timer->it_signal can be set concurrently */ in __posix_timers_find()
83 if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id)) in __posix_timers_find()
91 struct signal_struct *sig = current->signal; in posix_timer_by_id()
99 struct signal_struct *sig = current->signal; in posix_timer_add()
109 id = sig->next_posix_timer_id; in posix_timer_add()
112 sig->next_posix_timer_id = (id + 1) & INT_MAX; in posix_timer_add()
116 hlist_add_head_rcu(&timer->t_hash, head); in posix_timer_add()
122 /* POSIX return code when no timer ID could be allocated */ in posix_timer_add()
123 return -EAGAIN; in posix_timer_add()
128 spin_unlock_irqrestore(&timr->it_lock, flags); in unlock_timer()
218 tp->tv_sec = 0; in posix_get_hrtimer_res()
219 tp->tv_nsec = hrtimer_resolution; in posix_get_hrtimer_res()
238 s64 sum = timr->it_overrun_last + (s64)baseval; in timer_overrun_to_int()
245 struct hrtimer *timer = &timr->it.real.timer; in common_hrtimer_rearm()
247 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(), in common_hrtimer_rearm()
248 timr->it_interval); in common_hrtimer_rearm()
254 * info->si_sys_private is not zero, which indicates that the timer has to
262 timr = lock_timer(info->si_tid, &flags); in posixtimer_rearm()
266 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) { in posixtimer_rearm()
267 timr->kclock->timer_rearm(timr); in posixtimer_rearm()
269 timr->it_active = 1; in posixtimer_rearm()
270 timr->it_overrun_last = timr->it_overrun; in posixtimer_rearm()
271 timr->it_overrun = -1LL; in posixtimer_rearm()
272 ++timr->it_requeue_pending; in posixtimer_rearm()
274 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun); in posixtimer_rearm()
285 lockdep_assert_held(&timr->it_lock); in posix_timer_queue_signal()
287 timr->it_active = 0; in posix_timer_queue_signal()
288 if (timr->it_interval) in posix_timer_queue_signal()
289 si_private = ++timr->it_requeue_pending; in posix_timer_queue_signal()
292 * FIXME: if ->sigq is queued we can race with in posix_timer_queue_signal()
293 * dequeue_signal()->posixtimer_rearm(). in posix_timer_queue_signal()
297 * We re-queue ->sigq and drop ->it_lock(). in posix_timer_queue_signal()
299 * and re-schedules it while ->sigq is pending. in posix_timer_queue_signal()
302 timr->sigq->info.si_sys_private = si_private; in posix_timer_queue_signal()
304 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID; in posix_timer_queue_signal()
305 ret = send_sigqueue(timr->sigq, timr->it_pid, type); in posix_timer_queue_signal()
323 spin_lock_irqsave(&timr->it_lock, flags); in posix_timer_fn()
332 if (timr->it_interval != 0) { in posix_timer_fn()
337 * timer completely and restart it in case the in posix_timer_fn()
345 * This avoids interrupt starvation in case of a in posix_timer_fn()
352 * for the price of a slight inconsistency in the in posix_timer_fn()
357 * enabled as the periodic tick based timers are in posix_timer_fn()
358 * automatically aligned to the next tick. in posix_timer_fn()
363 if (timr->it_interval < kj) in posix_timer_fn()
367 timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval); in posix_timer_fn()
369 ++timr->it_requeue_pending; in posix_timer_fn()
370 timr->it_active = 1; in posix_timer_fn()
383 switch (event->sigev_notify) { in good_sigevent()
385 pid = find_vpid(event->sigev_notify_thread_id); in good_sigevent()
392 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX) in good_sigevent()
408 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { in alloc_posix_timer()
412 clear_siginfo(&tmr->sigq->info); in alloc_posix_timer()
425 put_pid(tmr->it_pid); in posix_timer_free()
426 sigqueue_free(tmr->sigq); in posix_timer_free()
427 call_rcu(&tmr->rcu, k_itimer_rcu_free); in posix_timer_free()
433 hlist_del_rcu(&tmr->t_hash); in posix_timer_unhash_and_free()
440 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); in common_timer_create()
453 return -EINVAL; in do_timer_create()
454 if (!kc->timer_create) in do_timer_create()
455 return -EOPNOTSUPP; in do_timer_create()
459 return -EAGAIN; in do_timer_create()
461 spin_lock_init(&new_timer->it_lock); in do_timer_create()
474 new_timer->it_id = (timer_t) new_timer_id; in do_timer_create()
475 new_timer->it_clock = which_clock; in do_timer_create()
476 new_timer->kclock = kc; in do_timer_create()
477 new_timer->it_overrun = -1LL; in do_timer_create()
481 new_timer->it_pid = get_pid(good_sigevent(event)); in do_timer_create()
483 if (!new_timer->it_pid) { in do_timer_create()
484 error = -EINVAL; in do_timer_create()
487 new_timer->it_sigev_notify = event->sigev_notify; in do_timer_create()
488 new_timer->sigq->info.si_signo = event->sigev_signo; in do_timer_create()
489 new_timer->sigq->info.si_value = event->sigev_value; in do_timer_create()
491 new_timer->it_sigev_notify = SIGEV_SIGNAL; in do_timer_create()
492 new_timer->sigq->info.si_signo = SIGALRM; in do_timer_create()
493 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); in do_timer_create()
494 new_timer->sigq->info.si_value.sival_int = new_timer->it_id; in do_timer_create()
495 new_timer->it_pid = get_pid(task_tgid(current)); in do_timer_create()
498 new_timer->sigq->info.si_tid = new_timer->it_id; in do_timer_create()
499 new_timer->sigq->info.si_code = SI_TIMER; in do_timer_create()
502 error = -EFAULT; in do_timer_create()
512 error = kc->timer_create(new_timer); in do_timer_create()
516 spin_lock_irq(¤t->sighand->siglock); in do_timer_create()
517 /* This makes the timer valid in the hash table */ in do_timer_create()
518 WRITE_ONCE(new_timer->it_signal, current->signal); in do_timer_create()
519 hlist_add_head(&new_timer->list, ¤t->signal->posix_timers); in do_timer_create()
520 spin_unlock_irq(¤t->sighand->siglock); in do_timer_create()
539 return -EFAULT; in SYSCALL_DEFINE3()
554 return -EFAULT; in COMPAT_SYSCALL_DEFINE3()
575 * Timers are added to the hash in invalid state where in __lock_timer()
579 * Timer destruction happens in steps: in __lock_timer()
596 spin_lock_irqsave(&timr->it_lock, *flags); in __lock_timer()
601 if (timr->it_signal == current->signal) { in __lock_timer()
605 spin_unlock_irqrestore(&timr->it_lock, *flags); in __lock_timer()
614 struct hrtimer *timer = &timr->it.real.timer; in common_hrtimer_remaining()
621 struct hrtimer *timer = &timr->it.real.timer; in common_hrtimer_forward()
623 return hrtimer_forward(timer, now, timr->it_interval); in common_hrtimer_forward()
640 const struct k_clock *kc = timr->kclock; in common_timer_get()
644 sig_none = timr->it_sigev_notify == SIGEV_NONE; in common_timer_get()
645 iv = timr->it_interval; in common_timer_get()
649 cur_setting->it_interval = ktime_to_timespec64(iv); in common_timer_get()
650 } else if (!timr->it_active) { in common_timer_get()
653 * timr->it_active is always false. The check below in common_timer_get()
663 now = kc->clock_get_ktime(timr->it_clock); in common_timer_get()
670 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) in common_timer_get()
671 timr->it_overrun += kc->timer_forward(timr, now); in common_timer_get()
673 remaining = kc->timer_remaining(timr, now); in common_timer_get()
691 cur_setting->it_value.tv_nsec = 1; in common_timer_get()
693 cur_setting->it_value = ktime_to_timespec64(remaining); in common_timer_get()
706 return -EINVAL; in do_timer_gettime()
709 kc = timr->kclock; in do_timer_gettime()
710 if (WARN_ON_ONCE(!kc || !kc->timer_get)) in do_timer_gettime()
711 ret = -EINVAL; in do_timer_gettime()
713 kc->timer_get(timr, setting); in do_timer_gettime()
728 ret = -EFAULT; in SYSCALL_DEFINE2()
743 ret = -EFAULT; in SYSCALL_DEFINE2()
751 * sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
765 * -EINVAL @timer_id is invalid
776 return -EINVAL; in SYSCALL_DEFINE1()
787 struct hrtimer *timer = &timr->it.real.timer; in common_hrtimer_arm()
794 * hood. See hrtimer_init(). Update timr->kclock, so the generic in common_hrtimer_arm()
795 * functions which use timr->kclock->clock_get_*() work. in common_hrtimer_arm()
800 if (timr->it_clock == CLOCK_REALTIME) in common_hrtimer_arm()
801 timr->kclock = absolute ? &clock_realtime : &clock_monotonic; in common_hrtimer_arm()
803 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); in common_hrtimer_arm()
804 timr->it.real.timer.function = posix_timer_fn; in common_hrtimer_arm()
807 expires = ktime_add_safe(expires, timer->base->get_time()); in common_hrtimer_arm()
816 return hrtimer_try_to_cancel(&timr->it.real.timer); in common_hrtimer_try_to_cancel()
821 hrtimer_cancel_wait_running(&timer->it.real.timer); in common_timer_wait_running()
826 * against the ksoftirqd thread in case that ksoftirqd gets preempted while
829 * See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
830 * just results in a cpu_relax().
836 * the task which runs the expiry in task work context.
841 const struct k_clock *kc = READ_ONCE(timer->kclock); in timer_wait_running()
842 timer_t timer_id = READ_ONCE(timer->it_id); in timer_wait_running()
849 * kc->timer_wait_running() might drop RCU lock. So @timer in timer_wait_running()
852 if (!WARN_ON_ONCE(!kc->timer_wait_running)) in timer_wait_running()
853 kc->timer_wait_running(timer); in timer_wait_running()
865 if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec) in posix_timer_set_common()
866 timer->it_interval = timespec64_to_ktime(new_setting->it_interval); in posix_timer_set_common()
868 timer->it_interval = 0; in posix_timer_set_common()
870 /* Prevent reloading in case there is a signal pending */ in posix_timer_set_common()
871 timer->it_requeue_pending = (timer->it_requeue_pending + 2) & ~REQUEUE_PENDING; in posix_timer_set_common()
873 timer->it_overrun_last = 0; in posix_timer_set_common()
874 timer->it_overrun = -1LL; in posix_timer_set_common()
882 const struct k_clock *kc = timr->kclock; in common_timer_set()
890 timr->it_interval = 0; in common_timer_set()
893 * active and spinning on timr->it_lock. in common_timer_set()
895 if (kc->timer_try_to_cancel(timr) < 0) in common_timer_set()
898 timr->it_active = 0; in common_timer_set()
902 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) in common_timer_set()
905 expires = timespec64_to_ktime(new_setting->it_value); in common_timer_set()
907 expires = timens_ktime_to_host(timr->it_clock, expires); in common_timer_set()
908 sigev_none = timr->it_sigev_notify == SIGEV_NONE; in common_timer_set()
910 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); in common_timer_set()
911 timr->it_active = !sigev_none; in common_timer_set()
924 if (!timespec64_valid(&new_spec64->it_interval) || in do_timer_settime()
925 !timespec64_valid(&new_spec64->it_value)) in do_timer_settime()
926 return -EINVAL; in do_timer_settime()
934 return -EINVAL; in do_timer_settime()
937 old_spec64->it_interval = ktime_to_timespec64(timr->it_interval); in do_timer_settime()
939 kc = timr->kclock; in do_timer_settime()
940 if (WARN_ON_ONCE(!kc || !kc->timer_set)) in do_timer_settime()
941 error = -EINVAL; in do_timer_settime()
943 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64); in do_timer_settime()
966 return -EINVAL; in SYSCALL_DEFINE4()
969 return -EFAULT; in SYSCALL_DEFINE4()
975 error = -EFAULT; in SYSCALL_DEFINE4()
990 return -EINVAL; in SYSCALL_DEFINE4()
992 return -EFAULT; in SYSCALL_DEFINE4()
997 error = -EFAULT; in SYSCALL_DEFINE4()
1005 const struct k_clock *kc = timer->kclock; in common_timer_del()
1007 timer->it_interval = 0; in common_timer_del()
1008 if (kc->timer_try_to_cancel(timer) < 0) in common_timer_del()
1010 timer->it_active = 0; in common_timer_del()
1016 const struct k_clock *kc = timer->kclock; in timer_delete_hook()
1018 if (WARN_ON_ONCE(!kc || !kc->timer_del)) in timer_delete_hook()
1019 return -EINVAL; in timer_delete_hook()
1020 return kc->timer_del(timer); in timer_delete_hook()
1033 return -EINVAL; in SYSCALL_DEFINE1()
1041 spin_lock(¤t->sighand->siglock); in SYSCALL_DEFINE1()
1042 hlist_del(&timer->list); in SYSCALL_DEFINE1()
1043 spin_unlock(¤t->sighand->siglock); in SYSCALL_DEFINE1()
1048 WRITE_ONCE(timer->it_signal, NULL); in SYSCALL_DEFINE1()
1066 spin_lock_irqsave(&timer->it_lock, flags); in itimer_delete()
1071 * it still might be armed and queued in the underlying timer in itimer_delete()
1085 * So no other task can access and delete that timer. in itimer_delete()
1092 hlist_del(&timer->list); in itimer_delete()
1100 WRITE_ONCE(timer->it_signal, NULL); in itimer_delete()
1102 spin_unlock_irqrestore(&timer->it_lock, flags); in itimer_delete()
1108 * At that point no other task can access the timers of the dying
1115 if (hlist_empty(&tsk->signal->posix_timers)) in exit_itimers()
1119 spin_lock_irq(&tsk->sighand->siglock); in exit_itimers()
1120 hlist_move_list(&tsk->signal->posix_timers, &timers); in exit_itimers()
1121 spin_unlock_irq(&tsk->sighand->siglock); in exit_itimers()
1134 if (!kc || !kc->clock_set) in SYSCALL_DEFINE2()
1135 return -EINVAL; in SYSCALL_DEFINE2()
1138 return -EFAULT; in SYSCALL_DEFINE2()
1144 return kc->clock_set(which_clock, &new_tp); in SYSCALL_DEFINE2()
1155 return -EINVAL; in SYSCALL_DEFINE2()
1157 error = kc->clock_get_timespec(which_clock, &kernel_tp); in SYSCALL_DEFINE2()
1160 error = -EFAULT; in SYSCALL_DEFINE2()
1170 return -EINVAL; in do_clock_adjtime()
1171 if (!kc->clock_adj) in do_clock_adjtime()
1172 return -EOPNOTSUPP; in do_clock_adjtime()
1174 return kc->clock_adj(which_clock, ktx); in do_clock_adjtime()
1184 return -EFAULT; in SYSCALL_DEFINE2()
1189 return -EFAULT; in SYSCALL_DEFINE2()
1195 * sys_clock_getres - Get the resolution of a clock
1202 * clock. Clock resolutions are implementation-defined and cannot be set by
1204 * specified clock shall be stored in the location pointed to by res. If
1214 * The kernel therefore deviates from the POSIX spec in various aspects:
1225 * the resolution returned is nanoseconds per tick, which represents
1236 * resolution of the other clocks. During suspend the actual
1242 * returned is always nanoseconds per tick.
1247 * per tick.
1254 * The kernel does not truncate the time which is handed in to
1262 * -EINVAL @which_clock is not a valid clock ID
1263 * -EFAULT Copying the resolution to @tp faulted
1264 * -ENODEV Dynamic POSIX clock is not backed by a device
1265 * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
1275 return -EINVAL; in SYSCALL_DEFINE2()
1277 error = kc->clock_getres(which_clock, &rtn_tp); in SYSCALL_DEFINE2()
1280 error = -EFAULT; in SYSCALL_DEFINE2()
1293 if (!kc || !kc->clock_set) in SYSCALL_DEFINE2()
1294 return -EINVAL; in SYSCALL_DEFINE2()
1297 return -EFAULT; in SYSCALL_DEFINE2()
1299 return kc->clock_set(which_clock, &ts); in SYSCALL_DEFINE2()
1310 return -EINVAL; in SYSCALL_DEFINE2()
1312 err = kc->clock_get_timespec(which_clock, &ts); in SYSCALL_DEFINE2()
1315 err = -EFAULT; in SYSCALL_DEFINE2()
1333 return -EFAULT; in SYSCALL_DEFINE2()
1346 return -EINVAL; in SYSCALL_DEFINE2()
1348 err = kc->clock_getres(which_clock, &ts); in SYSCALL_DEFINE2()
1350 return -EFAULT; in SYSCALL_DEFINE2()
1373 * Absolute nanosleeps for these clocks are time-namespace adjusted.
1396 return -EINVAL; in SYSCALL_DEFINE4()
1397 if (!kc->nsleep) in SYSCALL_DEFINE4()
1398 return -EOPNOTSUPP; in SYSCALL_DEFINE4()
1401 return -EFAULT; in SYSCALL_DEFINE4()
1404 return -EINVAL; in SYSCALL_DEFINE4()
1407 current->restart_block.fn = do_no_restart_syscall; in SYSCALL_DEFINE4()
1408 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; in SYSCALL_DEFINE4()
1409 current->restart_block.nanosleep.rmtp = rmtp; in SYSCALL_DEFINE4()
1411 return kc->nsleep(which_clock, flags, &t); in SYSCALL_DEFINE4()
1424 return -EINVAL; in SYSCALL_DEFINE4()
1425 if (!kc->nsleep) in SYSCALL_DEFINE4()
1426 return -EOPNOTSUPP; in SYSCALL_DEFINE4()
1429 return -EFAULT; in SYSCALL_DEFINE4()
1432 return -EINVAL; in SYSCALL_DEFINE4()
1435 current->restart_block.fn = do_no_restart_syscall; in SYSCALL_DEFINE4()
1436 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; in SYSCALL_DEFINE4()
1437 current->restart_block.nanosleep.compat_rmtp = rmtp; in SYSCALL_DEFINE4()
1439 return kc->nsleep(which_clock, flags, &t); in SYSCALL_DEFINE4()