1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * 2002-10-15 Posix Clocks & timers
4 * by George Anzinger george@mvista.com
5 * Copyright (C) 2002 2003 by MontaVista Software.
6 *
7 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
8 * Copyright (C) 2004 Boris Hu
9 *
10 * These are all the functions necessary to implement POSIX clocks & timers
11 */
12 #include <linux/mm.h>
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>
18
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>
34
35 #include "timekeeping.h"
36 #include "posix-timers.h"
37
38 static struct kmem_cache *posix_timers_cache;
39
40 /*
41 * Timers are managed in a hash table for lockless lookup. The hash key is
42 * constructed from current::signal and the timer ID and the timer is
43 * matched against current::signal and the timer ID when walking the hash
44 * bucket list.
45 *
46 * This allows checkpoint/restore to reconstruct the exact timer IDs for
47 * a process.
48 */
49 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
50 static DEFINE_SPINLOCK(hash_lock);
51
52 static const struct k_clock * const posix_clocks[];
53 static const struct k_clock *clockid_to_kclock(const clockid_t id);
54 static const struct k_clock clock_realtime, clock_monotonic;
55
56 /* SIGEV_THREAD_ID cannot share a bit with the other SIGEV values. */
57 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
58 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
59 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
60 #endif
61
62 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
63
64 #define lock_timer(tid, flags) \
65 ({ struct k_itimer *__timr; \
66 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
67 __timr; \
68 })
69
hash(struct signal_struct * sig,unsigned int nr)70 static int hash(struct signal_struct *sig, unsigned int nr)
71 {
72 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
73 }
74
__posix_timers_find(struct hlist_head * head,struct signal_struct * sig,timer_t id)75 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
76 struct signal_struct *sig,
77 timer_t id)
78 {
79 struct k_itimer *timer;
80
81 hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)) {
82 /* timer->it_signal can be set concurrently */
83 if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id))
84 return timer;
85 }
86 return NULL;
87 }
88
posix_timer_by_id(timer_t id)89 static struct k_itimer *posix_timer_by_id(timer_t id)
90 {
91 struct signal_struct *sig = current->signal;
92 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
93
94 return __posix_timers_find(head, sig, id);
95 }
96
posix_timer_add(struct k_itimer * timer)97 static int posix_timer_add(struct k_itimer *timer)
98 {
99 struct signal_struct *sig = current->signal;
100 struct hlist_head *head;
101 unsigned int cnt, id;
102
103 /*
104 * FIXME: Replace this by a per signal struct xarray once there is
105 * a plan to handle the resulting CRIU regression gracefully.
106 */
107 for (cnt = 0; cnt <= INT_MAX; cnt++) {
108 spin_lock(&hash_lock);
109 id = sig->next_posix_timer_id;
110
111 /* Write the next ID back. Clamp it to the positive space */
112 sig->next_posix_timer_id = (id + 1) & INT_MAX;
113
114 head = &posix_timers_hashtable[hash(sig, id)];
115 if (!__posix_timers_find(head, sig, id)) {
116 hlist_add_head_rcu(&timer->t_hash, head);
117 spin_unlock(&hash_lock);
118 return id;
119 }
120 spin_unlock(&hash_lock);
121 cond_resched();
122 }
123 /* POSIX return code when no timer ID could be allocated */
124 return -EAGAIN;
125 }
126
unlock_timer(struct k_itimer * timr,unsigned long flags)127 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
128 {
129 spin_unlock_irqrestore(&timr->it_lock, flags);
130 }
131
posix_get_realtime_timespec(clockid_t which_clock,struct timespec64 * tp)132 static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
133 {
134 ktime_get_real_ts64(tp);
135 return 0;
136 }
137
posix_get_realtime_ktime(clockid_t which_clock)138 static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
139 {
140 return ktime_get_real();
141 }
142
posix_clock_realtime_set(const clockid_t which_clock,const struct timespec64 * tp)143 static int posix_clock_realtime_set(const clockid_t which_clock,
144 const struct timespec64 *tp)
145 {
146 return do_sys_settimeofday64(tp, NULL);
147 }
148
posix_clock_realtime_adj(const clockid_t which_clock,struct __kernel_timex * t)149 static int posix_clock_realtime_adj(const clockid_t which_clock,
150 struct __kernel_timex *t)
151 {
152 return do_adjtimex(t);
153 }
154
posix_get_monotonic_timespec(clockid_t which_clock,struct timespec64 * tp)155 static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
156 {
157 ktime_get_ts64(tp);
158 timens_add_monotonic(tp);
159 return 0;
160 }
161
posix_get_monotonic_ktime(clockid_t which_clock)162 static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
163 {
164 return ktime_get();
165 }
166
posix_get_monotonic_raw(clockid_t which_clock,struct timespec64 * tp)167 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
168 {
169 ktime_get_raw_ts64(tp);
170 timens_add_monotonic(tp);
171 return 0;
172 }
173
posix_get_realtime_coarse(clockid_t which_clock,struct timespec64 * tp)174 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
175 {
176 ktime_get_coarse_real_ts64(tp);
177 return 0;
178 }
179
posix_get_monotonic_coarse(clockid_t which_clock,struct timespec64 * tp)180 static int posix_get_monotonic_coarse(clockid_t which_clock,
181 struct timespec64 *tp)
182 {
183 ktime_get_coarse_ts64(tp);
184 timens_add_monotonic(tp);
185 return 0;
186 }
187
posix_get_coarse_res(const clockid_t which_clock,struct timespec64 * tp)188 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
189 {
190 *tp = ktime_to_timespec64(KTIME_LOW_RES);
191 return 0;
192 }
193
posix_get_boottime_timespec(const clockid_t which_clock,struct timespec64 * tp)194 static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
195 {
196 ktime_get_boottime_ts64(tp);
197 timens_add_boottime(tp);
198 return 0;
199 }
200
posix_get_boottime_ktime(const clockid_t which_clock)201 static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
202 {
203 return ktime_get_boottime();
204 }
205
posix_get_tai_timespec(clockid_t which_clock,struct timespec64 * tp)206 static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
207 {
208 ktime_get_clocktai_ts64(tp);
209 return 0;
210 }
211
posix_get_tai_ktime(clockid_t which_clock)212 static ktime_t posix_get_tai_ktime(clockid_t which_clock)
213 {
214 return ktime_get_clocktai();
215 }
216
posix_get_hrtimer_res(clockid_t which_clock,struct timespec64 * tp)217 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
218 {
219 tp->tv_sec = 0;
220 tp->tv_nsec = hrtimer_resolution;
221 return 0;
222 }
223
init_posix_timers(void)224 static __init int init_posix_timers(void)
225 {
226 posix_timers_cache = kmem_cache_create("posix_timers_cache",
227 sizeof(struct k_itimer), 0,
228 SLAB_PANIC | SLAB_ACCOUNT, NULL);
229 return 0;
230 }
231 __initcall(init_posix_timers);
232
233 /*
234 * The siginfo si_overrun field and the return value of timer_getoverrun(2)
235 * are of type int. Clamp the overrun value to INT_MAX
236 */
timer_overrun_to_int(struct k_itimer * timr,int baseval)237 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
238 {
239 s64 sum = timr->it_overrun_last + (s64)baseval;
240
241 return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
242 }
243
common_hrtimer_rearm(struct k_itimer * timr)244 static void common_hrtimer_rearm(struct k_itimer *timr)
245 {
246 struct hrtimer *timer = &timr->it.real.timer;
247
248 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
249 timr->it_interval);
250 hrtimer_restart(timer);
251 }
252
253 /*
254 * This function is called from the signal delivery code if
255 * info->si_sys_private is not zero, which indicates that the timer has to
256 * be rearmed. Restart the timer and update info::si_overrun.
257 */
posixtimer_rearm(struct kernel_siginfo * info)258 void posixtimer_rearm(struct kernel_siginfo *info)
259 {
260 struct k_itimer *timr;
261 unsigned long flags;
262
263 timr = lock_timer(info->si_tid, &flags);
264 if (!timr)
265 return;
266
267 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
268 timr->kclock->timer_rearm(timr);
269
270 timr->it_active = 1;
271 timr->it_overrun_last = timr->it_overrun;
272 timr->it_overrun = -1LL;
273 ++timr->it_requeue_pending;
274
275 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
276 }
277
278 unlock_timer(timr, flags);
279 }
280
posix_timer_event(struct k_itimer * timr,int si_private)281 int posix_timer_event(struct k_itimer *timr, int si_private)
282 {
283 enum pid_type type;
284 int ret;
285 /*
286 * FIXME: if ->sigq is queued we can race with
287 * dequeue_signal()->posixtimer_rearm().
288 *
289 * If dequeue_signal() sees the "right" value of
290 * si_sys_private it calls posixtimer_rearm().
291 * We re-queue ->sigq and drop ->it_lock().
292 * posixtimer_rearm() locks the timer
293 * and re-schedules it while ->sigq is pending.
294 * Not really bad, but not that we want.
295 */
296 timr->sigq->info.si_sys_private = si_private;
297
298 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
299 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
300 /* If we failed to send the signal the timer stops. */
301 return ret > 0;
302 }
303
304 /*
305 * This function gets called when a POSIX.1b interval timer expires from
306 * the HRTIMER interrupt (soft interrupt on RT kernels).
307 *
308 * Handles CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME and CLOCK_TAI
309 * based timers.
310 */
posix_timer_fn(struct hrtimer * timer)311 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
312 {
313 enum hrtimer_restart ret = HRTIMER_NORESTART;
314 struct k_itimer *timr;
315 unsigned long flags;
316 int si_private = 0;
317
318 timr = container_of(timer, struct k_itimer, it.real.timer);
319 spin_lock_irqsave(&timr->it_lock, flags);
320
321 timr->it_active = 0;
322 if (timr->it_interval != 0)
323 si_private = ++timr->it_requeue_pending;
324
325 if (posix_timer_event(timr, si_private)) {
326 /*
327 * The signal was not queued due to SIG_IGN. As a
328 * consequence the timer is not going to be rearmed from
329 * the signal delivery path. But as a real signal handler
330 * can be installed later the timer must be rearmed here.
331 */
332 if (timr->it_interval != 0) {
333 ktime_t now = hrtimer_cb_get_time(timer);
334
335 /*
336 * FIXME: What we really want, is to stop this
337 * timer completely and restart it in case the
338 * SIG_IGN is removed. This is a non trivial
339 * change to the signal handling code.
340 *
341 * For now let timers with an interval less than a
342 * jiffie expire every jiffie and recheck for a
343 * valid signal handler.
344 *
345 * This avoids interrupt starvation in case of a
346 * very small interval, which would expire the
347 * timer immediately again.
348 *
349 * Moving now ahead of time by one jiffie tricks
350 * hrtimer_forward() to expire the timer later,
351 * while it still maintains the overrun accuracy
352 * for the price of a slight inconsistency in the
353 * timer_gettime() case. This is at least better
354 * than a timer storm.
355 *
356 * Only required when high resolution timers are
357 * enabled as the periodic tick based timers are
358 * automatically aligned to the next tick.
359 */
360 if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) {
361 ktime_t kj = TICK_NSEC;
362
363 if (timr->it_interval < kj)
364 now = ktime_add(now, kj);
365 }
366
367 timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval);
368 ret = HRTIMER_RESTART;
369 ++timr->it_requeue_pending;
370 timr->it_active = 1;
371 }
372 }
373
374 unlock_timer(timr, flags);
375 return ret;
376 }
377
good_sigevent(sigevent_t * event)378 static struct pid *good_sigevent(sigevent_t * event)
379 {
380 struct pid *pid = task_tgid(current);
381 struct task_struct *rtn;
382
383 switch (event->sigev_notify) {
384 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
385 pid = find_vpid(event->sigev_notify_thread_id);
386 rtn = pid_task(pid, PIDTYPE_PID);
387 if (!rtn || !same_thread_group(rtn, current))
388 return NULL;
389 fallthrough;
390 case SIGEV_SIGNAL:
391 case SIGEV_THREAD:
392 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
393 return NULL;
394 fallthrough;
395 case SIGEV_NONE:
396 return pid;
397 default:
398 return NULL;
399 }
400 }
401
alloc_posix_timer(void)402 static struct k_itimer * alloc_posix_timer(void)
403 {
404 struct k_itimer *tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
405
406 if (!tmr)
407 return tmr;
408 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
409 kmem_cache_free(posix_timers_cache, tmr);
410 return NULL;
411 }
412 clear_siginfo(&tmr->sigq->info);
413 return tmr;
414 }
415
k_itimer_rcu_free(struct rcu_head * head)416 static void k_itimer_rcu_free(struct rcu_head *head)
417 {
418 struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
419
420 kmem_cache_free(posix_timers_cache, tmr);
421 }
422
posix_timer_free(struct k_itimer * tmr)423 static void posix_timer_free(struct k_itimer *tmr)
424 {
425 put_pid(tmr->it_pid);
426 sigqueue_free(tmr->sigq);
427 call_rcu(&tmr->rcu, k_itimer_rcu_free);
428 }
429
posix_timer_unhash_and_free(struct k_itimer * tmr)430 static void posix_timer_unhash_and_free(struct k_itimer *tmr)
431 {
432 spin_lock(&hash_lock);
433 hlist_del_rcu(&tmr->t_hash);
434 spin_unlock(&hash_lock);
435 posix_timer_free(tmr);
436 }
437
common_timer_create(struct k_itimer * new_timer)438 static int common_timer_create(struct k_itimer *new_timer)
439 {
440 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
441 return 0;
442 }
443
444 /* Create a POSIX.1b interval timer. */
do_timer_create(clockid_t which_clock,struct sigevent * event,timer_t __user * created_timer_id)445 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
446 timer_t __user *created_timer_id)
447 {
448 const struct k_clock *kc = clockid_to_kclock(which_clock);
449 struct k_itimer *new_timer;
450 int error, new_timer_id;
451
452 if (!kc)
453 return -EINVAL;
454 if (!kc->timer_create)
455 return -EOPNOTSUPP;
456
457 new_timer = alloc_posix_timer();
458 if (unlikely(!new_timer))
459 return -EAGAIN;
460
461 spin_lock_init(&new_timer->it_lock);
462
463 /*
464 * Add the timer to the hash table. The timer is not yet valid
465 * because new_timer::it_signal is still NULL. The timer id is also
466 * not yet visible to user space.
467 */
468 new_timer_id = posix_timer_add(new_timer);
469 if (new_timer_id < 0) {
470 posix_timer_free(new_timer);
471 return new_timer_id;
472 }
473
474 new_timer->it_id = (timer_t) new_timer_id;
475 new_timer->it_clock = which_clock;
476 new_timer->kclock = kc;
477 new_timer->it_overrun = -1LL;
478
479 if (event) {
480 rcu_read_lock();
481 new_timer->it_pid = get_pid(good_sigevent(event));
482 rcu_read_unlock();
483 if (!new_timer->it_pid) {
484 error = -EINVAL;
485 goto out;
486 }
487 new_timer->it_sigev_notify = event->sigev_notify;
488 new_timer->sigq->info.si_signo = event->sigev_signo;
489 new_timer->sigq->info.si_value = event->sigev_value;
490 } else {
491 new_timer->it_sigev_notify = SIGEV_SIGNAL;
492 new_timer->sigq->info.si_signo = SIGALRM;
493 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
494 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
495 new_timer->it_pid = get_pid(task_tgid(current));
496 }
497
498 new_timer->sigq->info.si_tid = new_timer->it_id;
499 new_timer->sigq->info.si_code = SI_TIMER;
500
501 if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id))) {
502 error = -EFAULT;
503 goto out;
504 }
505 /*
506 * After succesful copy out, the timer ID is visible to user space
507 * now but not yet valid because new_timer::signal is still NULL.
508 *
509 * Complete the initialization with the clock specific create
510 * callback.
511 */
512 error = kc->timer_create(new_timer);
513 if (error)
514 goto out;
515
516 spin_lock_irq(¤t->sighand->siglock);
517 /* This makes the timer valid in the hash table */
518 WRITE_ONCE(new_timer->it_signal, current->signal);
519 list_add(&new_timer->list, ¤t->signal->posix_timers);
520 spin_unlock_irq(¤t->sighand->siglock);
521 /*
522 * After unlocking sighand::siglock @new_timer is subject to
523 * concurrent removal and cannot be touched anymore
524 */
525 return 0;
526 out:
527 posix_timer_unhash_and_free(new_timer);
528 return error;
529 }
530
SYSCALL_DEFINE3(timer_create,const clockid_t,which_clock,struct sigevent __user *,timer_event_spec,timer_t __user *,created_timer_id)531 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
532 struct sigevent __user *, timer_event_spec,
533 timer_t __user *, created_timer_id)
534 {
535 if (timer_event_spec) {
536 sigevent_t event;
537
538 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
539 return -EFAULT;
540 return do_timer_create(which_clock, &event, created_timer_id);
541 }
542 return do_timer_create(which_clock, NULL, created_timer_id);
543 }
544
545 #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)546 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
547 struct compat_sigevent __user *, timer_event_spec,
548 timer_t __user *, created_timer_id)
549 {
550 if (timer_event_spec) {
551 sigevent_t event;
552
553 if (get_compat_sigevent(&event, timer_event_spec))
554 return -EFAULT;
555 return do_timer_create(which_clock, &event, created_timer_id);
556 }
557 return do_timer_create(which_clock, NULL, created_timer_id);
558 }
559 #endif
560
__lock_timer(timer_t timer_id,unsigned long * flags)561 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
562 {
563 struct k_itimer *timr;
564
565 /*
566 * timer_t could be any type >= int and we want to make sure any
567 * @timer_id outside positive int range fails lookup.
568 */
569 if ((unsigned long long)timer_id > INT_MAX)
570 return NULL;
571
572 /*
573 * The hash lookup and the timers are RCU protected.
574 *
575 * Timers are added to the hash in invalid state where
576 * timr::it_signal == NULL. timer::it_signal is only set after the
577 * rest of the initialization succeeded.
578 *
579 * Timer destruction happens in steps:
580 * 1) Set timr::it_signal to NULL with timr::it_lock held
581 * 2) Release timr::it_lock
582 * 3) Remove from the hash under hash_lock
583 * 4) Call RCU for removal after the grace period
584 *
585 * Holding rcu_read_lock() accross the lookup ensures that
586 * the timer cannot be freed.
587 *
588 * The lookup validates locklessly that timr::it_signal ==
589 * current::it_signal and timr::it_id == @timer_id. timr::it_id
590 * can't change, but timr::it_signal becomes NULL during
591 * destruction.
592 */
593 rcu_read_lock();
594 timr = posix_timer_by_id(timer_id);
595 if (timr) {
596 spin_lock_irqsave(&timr->it_lock, *flags);
597 /*
598 * Validate under timr::it_lock that timr::it_signal is
599 * still valid. Pairs with #1 above.
600 */
601 if (timr->it_signal == current->signal) {
602 rcu_read_unlock();
603 return timr;
604 }
605 spin_unlock_irqrestore(&timr->it_lock, *flags);
606 }
607 rcu_read_unlock();
608
609 return NULL;
610 }
611
common_hrtimer_remaining(struct k_itimer * timr,ktime_t now)612 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
613 {
614 struct hrtimer *timer = &timr->it.real.timer;
615
616 return __hrtimer_expires_remaining_adjusted(timer, now);
617 }
618
common_hrtimer_forward(struct k_itimer * timr,ktime_t now)619 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
620 {
621 struct hrtimer *timer = &timr->it.real.timer;
622
623 return hrtimer_forward(timer, now, timr->it_interval);
624 }
625
626 /*
627 * Get the time remaining on a POSIX.1b interval timer.
628 *
629 * Two issues to handle here:
630 *
631 * 1) The timer has a requeue pending. The return value must appear as
632 * if the timer has been requeued right now.
633 *
634 * 2) The timer is a SIGEV_NONE timer. These timers are never enqueued
635 * into the hrtimer queue and therefore never expired. Emulate expiry
636 * here taking #1 into account.
637 */
common_timer_get(struct k_itimer * timr,struct itimerspec64 * cur_setting)638 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
639 {
640 const struct k_clock *kc = timr->kclock;
641 ktime_t now, remaining, iv;
642 bool sig_none;
643
644 sig_none = timr->it_sigev_notify == SIGEV_NONE;
645 iv = timr->it_interval;
646
647 /* interval timer ? */
648 if (iv) {
649 cur_setting->it_interval = ktime_to_timespec64(iv);
650 } else if (!timr->it_active) {
651 /*
652 * SIGEV_NONE oneshot timers are never queued and therefore
653 * timr->it_active is always false. The check below
654 * vs. remaining time will handle this case.
655 *
656 * For all other timers there is nothing to update here, so
657 * return.
658 */
659 if (!sig_none)
660 return;
661 }
662
663 now = kc->clock_get_ktime(timr->it_clock);
664
665 /*
666 * If this is an interval timer and either has requeue pending or
667 * is a SIGEV_NONE timer move the expiry time forward by intervals,
668 * so expiry is > now.
669 */
670 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
671 timr->it_overrun += kc->timer_forward(timr, now);
672
673 remaining = kc->timer_remaining(timr, now);
674 /*
675 * As @now is retrieved before a possible timer_forward() and
676 * cannot be reevaluated by the compiler @remaining is based on the
677 * same @now value. Therefore @remaining is consistent vs. @now.
678 *
679 * Consequently all interval timers, i.e. @iv > 0, cannot have a
680 * remaining time <= 0 because timer_forward() guarantees to move
681 * them forward so that the next timer expiry is > @now.
682 */
683 if (remaining <= 0) {
684 /*
685 * A single shot SIGEV_NONE timer must return 0, when it is
686 * expired! Timers which have a real signal delivery mode
687 * must return a remaining time greater than 0 because the
688 * signal has not yet been delivered.
689 */
690 if (!sig_none)
691 cur_setting->it_value.tv_nsec = 1;
692 } else {
693 cur_setting->it_value = ktime_to_timespec64(remaining);
694 }
695 }
696
do_timer_gettime(timer_t timer_id,struct itimerspec64 * setting)697 static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
698 {
699 const struct k_clock *kc;
700 struct k_itimer *timr;
701 unsigned long flags;
702 int ret = 0;
703
704 timr = lock_timer(timer_id, &flags);
705 if (!timr)
706 return -EINVAL;
707
708 memset(setting, 0, sizeof(*setting));
709 kc = timr->kclock;
710 if (WARN_ON_ONCE(!kc || !kc->timer_get))
711 ret = -EINVAL;
712 else
713 kc->timer_get(timr, setting);
714
715 unlock_timer(timr, flags);
716 return ret;
717 }
718
719 /* Get the time remaining on a POSIX.1b interval timer. */
SYSCALL_DEFINE2(timer_gettime,timer_t,timer_id,struct __kernel_itimerspec __user *,setting)720 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
721 struct __kernel_itimerspec __user *, setting)
722 {
723 struct itimerspec64 cur_setting;
724
725 int ret = do_timer_gettime(timer_id, &cur_setting);
726 if (!ret) {
727 if (put_itimerspec64(&cur_setting, setting))
728 ret = -EFAULT;
729 }
730 return ret;
731 }
732
733 #ifdef CONFIG_COMPAT_32BIT_TIME
734
SYSCALL_DEFINE2(timer_gettime32,timer_t,timer_id,struct old_itimerspec32 __user *,setting)735 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
736 struct old_itimerspec32 __user *, setting)
737 {
738 struct itimerspec64 cur_setting;
739
740 int ret = do_timer_gettime(timer_id, &cur_setting);
741 if (!ret) {
742 if (put_old_itimerspec32(&cur_setting, setting))
743 ret = -EFAULT;
744 }
745 return ret;
746 }
747
748 #endif
749
750 /**
751 * sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
752 * @timer_id: The timer ID which identifies the timer
753 *
754 * The "overrun count" of a timer is one plus the number of expiration
755 * intervals which have elapsed between the first expiry, which queues the
756 * signal and the actual signal delivery. On signal delivery the "overrun
757 * count" is calculated and cached, so it can be returned directly here.
758 *
759 * As this is relative to the last queued signal the returned overrun count
760 * is meaningless outside of the signal delivery path and even there it
761 * does not accurately reflect the current state when user space evaluates
762 * it.
763 *
764 * Returns:
765 * -EINVAL @timer_id is invalid
766 * 1..INT_MAX The number of overruns related to the last delivered signal
767 */
SYSCALL_DEFINE1(timer_getoverrun,timer_t,timer_id)768 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
769 {
770 struct k_itimer *timr;
771 unsigned long flags;
772 int overrun;
773
774 timr = lock_timer(timer_id, &flags);
775 if (!timr)
776 return -EINVAL;
777
778 overrun = timer_overrun_to_int(timr, 0);
779 unlock_timer(timr, flags);
780
781 return overrun;
782 }
783
common_hrtimer_arm(struct k_itimer * timr,ktime_t expires,bool absolute,bool sigev_none)784 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
785 bool absolute, bool sigev_none)
786 {
787 struct hrtimer *timer = &timr->it.real.timer;
788 enum hrtimer_mode mode;
789
790 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
791 /*
792 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
793 * clock modifications, so they become CLOCK_MONOTONIC based under the
794 * hood. See hrtimer_init(). Update timr->kclock, so the generic
795 * functions which use timr->kclock->clock_get_*() work.
796 *
797 * Note: it_clock stays unmodified, because the next timer_set() might
798 * use ABSTIME, so it needs to switch back.
799 */
800 if (timr->it_clock == CLOCK_REALTIME)
801 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
802
803 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
804 timr->it.real.timer.function = posix_timer_fn;
805
806 if (!absolute)
807 expires = ktime_add_safe(expires, timer->base->get_time());
808 hrtimer_set_expires(timer, expires);
809
810 if (!sigev_none)
811 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
812 }
813
common_hrtimer_try_to_cancel(struct k_itimer * timr)814 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
815 {
816 return hrtimer_try_to_cancel(&timr->it.real.timer);
817 }
818
common_timer_wait_running(struct k_itimer * timer)819 static void common_timer_wait_running(struct k_itimer *timer)
820 {
821 hrtimer_cancel_wait_running(&timer->it.real.timer);
822 }
823
824 /*
825 * On PREEMPT_RT this prevents priority inversion and a potential livelock
826 * against the ksoftirqd thread in case that ksoftirqd gets preempted while
827 * executing a hrtimer callback.
828 *
829 * See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
830 * just results in a cpu_relax().
831 *
832 * For POSIX CPU timers with CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n this is
833 * just a cpu_relax(). With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this
834 * prevents spinning on an eventually scheduled out task and a livelock
835 * when the task which tries to delete or disarm the timer has preempted
836 * the task which runs the expiry in task work context.
837 */
timer_wait_running(struct k_itimer * timer,unsigned long * flags)838 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
839 unsigned long *flags)
840 {
841 const struct k_clock *kc = READ_ONCE(timer->kclock);
842 timer_t timer_id = READ_ONCE(timer->it_id);
843
844 /* Prevent kfree(timer) after dropping the lock */
845 rcu_read_lock();
846 unlock_timer(timer, *flags);
847
848 /*
849 * kc->timer_wait_running() might drop RCU lock. So @timer
850 * cannot be touched anymore after the function returns!
851 */
852 if (!WARN_ON_ONCE(!kc->timer_wait_running))
853 kc->timer_wait_running(timer);
854
855 rcu_read_unlock();
856 /* Relock the timer. It might be not longer hashed. */
857 return lock_timer(timer_id, flags);
858 }
859
860 /* Set a POSIX.1b interval timer. */
common_timer_set(struct k_itimer * timr,int flags,struct itimerspec64 * new_setting,struct itimerspec64 * old_setting)861 int common_timer_set(struct k_itimer *timr, int flags,
862 struct itimerspec64 *new_setting,
863 struct itimerspec64 *old_setting)
864 {
865 const struct k_clock *kc = timr->kclock;
866 bool sigev_none;
867 ktime_t expires;
868
869 if (old_setting)
870 common_timer_get(timr, old_setting);
871
872 /* Prevent rearming by clearing the interval */
873 timr->it_interval = 0;
874 /*
875 * Careful here. On SMP systems the timer expiry function could be
876 * active and spinning on timr->it_lock.
877 */
878 if (kc->timer_try_to_cancel(timr) < 0)
879 return TIMER_RETRY;
880
881 timr->it_active = 0;
882 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
883 ~REQUEUE_PENDING;
884 timr->it_overrun_last = 0;
885
886 /* Switch off the timer when it_value is zero */
887 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
888 return 0;
889
890 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
891 expires = timespec64_to_ktime(new_setting->it_value);
892 if (flags & TIMER_ABSTIME)
893 expires = timens_ktime_to_host(timr->it_clock, expires);
894 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
895
896 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
897 timr->it_active = !sigev_none;
898 return 0;
899 }
900
do_timer_settime(timer_t timer_id,int tmr_flags,struct itimerspec64 * new_spec64,struct itimerspec64 * old_spec64)901 static int do_timer_settime(timer_t timer_id, int tmr_flags,
902 struct itimerspec64 *new_spec64,
903 struct itimerspec64 *old_spec64)
904 {
905 const struct k_clock *kc;
906 struct k_itimer *timr;
907 unsigned long flags;
908 int error = 0;
909
910 if (!timespec64_valid(&new_spec64->it_interval) ||
911 !timespec64_valid(&new_spec64->it_value))
912 return -EINVAL;
913
914 if (old_spec64)
915 memset(old_spec64, 0, sizeof(*old_spec64));
916
917 timr = lock_timer(timer_id, &flags);
918 retry:
919 if (!timr)
920 return -EINVAL;
921
922 kc = timr->kclock;
923 if (WARN_ON_ONCE(!kc || !kc->timer_set))
924 error = -EINVAL;
925 else
926 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
927
928 if (error == TIMER_RETRY) {
929 // We already got the old time...
930 old_spec64 = NULL;
931 /* Unlocks and relocks the timer if it still exists */
932 timr = timer_wait_running(timr, &flags);
933 goto retry;
934 }
935 unlock_timer(timr, flags);
936
937 return error;
938 }
939
940 /* 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)941 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
942 const struct __kernel_itimerspec __user *, new_setting,
943 struct __kernel_itimerspec __user *, old_setting)
944 {
945 struct itimerspec64 new_spec, old_spec, *rtn;
946 int error = 0;
947
948 if (!new_setting)
949 return -EINVAL;
950
951 if (get_itimerspec64(&new_spec, new_setting))
952 return -EFAULT;
953
954 rtn = old_setting ? &old_spec : NULL;
955 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
956 if (!error && old_setting) {
957 if (put_itimerspec64(&old_spec, old_setting))
958 error = -EFAULT;
959 }
960 return error;
961 }
962
963 #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)964 SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
965 struct old_itimerspec32 __user *, new,
966 struct old_itimerspec32 __user *, old)
967 {
968 struct itimerspec64 new_spec, old_spec;
969 struct itimerspec64 *rtn = old ? &old_spec : NULL;
970 int error = 0;
971
972 if (!new)
973 return -EINVAL;
974 if (get_old_itimerspec32(&new_spec, new))
975 return -EFAULT;
976
977 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
978 if (!error && old) {
979 if (put_old_itimerspec32(&old_spec, old))
980 error = -EFAULT;
981 }
982 return error;
983 }
984 #endif
985
common_timer_del(struct k_itimer * timer)986 int common_timer_del(struct k_itimer *timer)
987 {
988 const struct k_clock *kc = timer->kclock;
989
990 timer->it_interval = 0;
991 if (kc->timer_try_to_cancel(timer) < 0)
992 return TIMER_RETRY;
993 timer->it_active = 0;
994 return 0;
995 }
996
timer_delete_hook(struct k_itimer * timer)997 static inline int timer_delete_hook(struct k_itimer *timer)
998 {
999 const struct k_clock *kc = timer->kclock;
1000
1001 if (WARN_ON_ONCE(!kc || !kc->timer_del))
1002 return -EINVAL;
1003 return kc->timer_del(timer);
1004 }
1005
1006 /* Delete a POSIX.1b interval timer. */
SYSCALL_DEFINE1(timer_delete,timer_t,timer_id)1007 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
1008 {
1009 struct k_itimer *timer;
1010 unsigned long flags;
1011
1012 timer = lock_timer(timer_id, &flags);
1013
1014 retry_delete:
1015 if (!timer)
1016 return -EINVAL;
1017
1018 if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
1019 /* Unlocks and relocks the timer if it still exists */
1020 timer = timer_wait_running(timer, &flags);
1021 goto retry_delete;
1022 }
1023
1024 spin_lock(¤t->sighand->siglock);
1025 list_del(&timer->list);
1026 spin_unlock(¤t->sighand->siglock);
1027 /*
1028 * A concurrent lookup could check timer::it_signal lockless. It
1029 * will reevaluate with timer::it_lock held and observe the NULL.
1030 */
1031 WRITE_ONCE(timer->it_signal, NULL);
1032
1033 unlock_timer(timer, flags);
1034 posix_timer_unhash_and_free(timer);
1035 return 0;
1036 }
1037
1038 /*
1039 * Delete a timer if it is armed, remove it from the hash and schedule it
1040 * for RCU freeing.
1041 */
itimer_delete(struct k_itimer * timer)1042 static void itimer_delete(struct k_itimer *timer)
1043 {
1044 unsigned long flags;
1045
1046 /*
1047 * irqsave is required to make timer_wait_running() work.
1048 */
1049 spin_lock_irqsave(&timer->it_lock, flags);
1050
1051 retry_delete:
1052 /*
1053 * Even if the timer is not longer accessible from other tasks
1054 * it still might be armed and queued in the underlying timer
1055 * mechanism. Worse, that timer mechanism might run the expiry
1056 * function concurrently.
1057 */
1058 if (timer_delete_hook(timer) == TIMER_RETRY) {
1059 /*
1060 * Timer is expired concurrently, prevent livelocks
1061 * and pointless spinning on RT.
1062 *
1063 * timer_wait_running() drops timer::it_lock, which opens
1064 * the possibility for another task to delete the timer.
1065 *
1066 * That's not possible here because this is invoked from
1067 * do_exit() only for the last thread of the thread group.
1068 * So no other task can access and delete that timer.
1069 */
1070 if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
1071 return;
1072
1073 goto retry_delete;
1074 }
1075 list_del(&timer->list);
1076
1077 /*
1078 * Setting timer::it_signal to NULL is technically not required
1079 * here as nothing can access the timer anymore legitimately via
1080 * the hash table. Set it to NULL nevertheless so that all deletion
1081 * paths are consistent.
1082 */
1083 WRITE_ONCE(timer->it_signal, NULL);
1084
1085 spin_unlock_irqrestore(&timer->it_lock, flags);
1086 posix_timer_unhash_and_free(timer);
1087 }
1088
1089 /*
1090 * Invoked from do_exit() when the last thread of a thread group exits.
1091 * At that point no other task can access the timers of the dying
1092 * task anymore.
1093 */
exit_itimers(struct task_struct * tsk)1094 void exit_itimers(struct task_struct *tsk)
1095 {
1096 struct list_head timers;
1097 struct k_itimer *tmr;
1098
1099 if (list_empty(&tsk->signal->posix_timers))
1100 return;
1101
1102 /* Protect against concurrent read via /proc/$PID/timers */
1103 spin_lock_irq(&tsk->sighand->siglock);
1104 list_replace_init(&tsk->signal->posix_timers, &timers);
1105 spin_unlock_irq(&tsk->sighand->siglock);
1106
1107 /* The timers are not longer accessible via tsk::signal */
1108 while (!list_empty(&timers)) {
1109 tmr = list_first_entry(&timers, struct k_itimer, list);
1110 itimer_delete(tmr);
1111 }
1112 }
1113
SYSCALL_DEFINE2(clock_settime,const clockid_t,which_clock,const struct __kernel_timespec __user *,tp)1114 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1115 const struct __kernel_timespec __user *, tp)
1116 {
1117 const struct k_clock *kc = clockid_to_kclock(which_clock);
1118 struct timespec64 new_tp;
1119
1120 if (!kc || !kc->clock_set)
1121 return -EINVAL;
1122
1123 if (get_timespec64(&new_tp, tp))
1124 return -EFAULT;
1125
1126 /*
1127 * Permission checks have to be done inside the clock specific
1128 * setter callback.
1129 */
1130 return kc->clock_set(which_clock, &new_tp);
1131 }
1132
SYSCALL_DEFINE2(clock_gettime,const clockid_t,which_clock,struct __kernel_timespec __user *,tp)1133 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1134 struct __kernel_timespec __user *, tp)
1135 {
1136 const struct k_clock *kc = clockid_to_kclock(which_clock);
1137 struct timespec64 kernel_tp;
1138 int error;
1139
1140 if (!kc)
1141 return -EINVAL;
1142
1143 error = kc->clock_get_timespec(which_clock, &kernel_tp);
1144
1145 if (!error && put_timespec64(&kernel_tp, tp))
1146 error = -EFAULT;
1147
1148 return error;
1149 }
1150
do_clock_adjtime(const clockid_t which_clock,struct __kernel_timex * ktx)1151 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1152 {
1153 const struct k_clock *kc = clockid_to_kclock(which_clock);
1154
1155 if (!kc)
1156 return -EINVAL;
1157 if (!kc->clock_adj)
1158 return -EOPNOTSUPP;
1159
1160 return kc->clock_adj(which_clock, ktx);
1161 }
1162
SYSCALL_DEFINE2(clock_adjtime,const clockid_t,which_clock,struct __kernel_timex __user *,utx)1163 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1164 struct __kernel_timex __user *, utx)
1165 {
1166 struct __kernel_timex ktx;
1167 int err;
1168
1169 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1170 return -EFAULT;
1171
1172 err = do_clock_adjtime(which_clock, &ktx);
1173
1174 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1175 return -EFAULT;
1176
1177 return err;
1178 }
1179
1180 /**
1181 * sys_clock_getres - Get the resolution of a clock
1182 * @which_clock: The clock to get the resolution for
1183 * @tp: Pointer to a a user space timespec64 for storage
1184 *
1185 * POSIX defines:
1186 *
1187 * "The clock_getres() function shall return the resolution of any
1188 * clock. Clock resolutions are implementation-defined and cannot be set by
1189 * a process. If the argument res is not NULL, the resolution of the
1190 * specified clock shall be stored in the location pointed to by res. If
1191 * res is NULL, the clock resolution is not returned. If the time argument
1192 * of clock_settime() is not a multiple of res, then the value is truncated
1193 * to a multiple of res."
1194 *
1195 * Due to the various hardware constraints the real resolution can vary
1196 * wildly and even change during runtime when the underlying devices are
1197 * replaced. The kernel also can use hardware devices with different
1198 * resolutions for reading the time and for arming timers.
1199 *
1200 * The kernel therefore deviates from the POSIX spec in various aspects:
1201 *
1202 * 1) The resolution returned to user space
1203 *
1204 * For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
1205 * CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
1206 * the kernel differentiates only two cases:
1207 *
1208 * I) Low resolution mode:
1209 *
1210 * When high resolution timers are disabled at compile or runtime
1211 * the resolution returned is nanoseconds per tick, which represents
1212 * the precision at which timers expire.
1213 *
1214 * II) High resolution mode:
1215 *
1216 * When high resolution timers are enabled the resolution returned
1217 * is always one nanosecond independent of the actual resolution of
1218 * the underlying hardware devices.
1219 *
1220 * For CLOCK_*_ALARM the actual resolution depends on system
1221 * state. When system is running the resolution is the same as the
1222 * resolution of the other clocks. During suspend the actual
1223 * resolution is the resolution of the underlying RTC device which
1224 * might be way less precise than the clockevent device used during
1225 * running state.
1226 *
1227 * For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
1228 * returned is always nanoseconds per tick.
1229 *
1230 * For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
1231 * returned is always one nanosecond under the assumption that the
1232 * underlying scheduler clock has a better resolution than nanoseconds
1233 * per tick.
1234 *
1235 * For dynamic POSIX clocks (PTP devices) the resolution returned is
1236 * always one nanosecond.
1237 *
1238 * 2) Affect on sys_clock_settime()
1239 *
1240 * The kernel does not truncate the time which is handed in to
1241 * sys_clock_settime(). The kernel internal timekeeping is always using
1242 * nanoseconds precision independent of the clocksource device which is
1243 * used to read the time from. The resolution of that device only
1244 * affects the presicion of the time returned by sys_clock_gettime().
1245 *
1246 * Returns:
1247 * 0 Success. @tp contains the resolution
1248 * -EINVAL @which_clock is not a valid clock ID
1249 * -EFAULT Copying the resolution to @tp faulted
1250 * -ENODEV Dynamic POSIX clock is not backed by a device
1251 * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
1252 */
SYSCALL_DEFINE2(clock_getres,const clockid_t,which_clock,struct __kernel_timespec __user *,tp)1253 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1254 struct __kernel_timespec __user *, tp)
1255 {
1256 const struct k_clock *kc = clockid_to_kclock(which_clock);
1257 struct timespec64 rtn_tp;
1258 int error;
1259
1260 if (!kc)
1261 return -EINVAL;
1262
1263 error = kc->clock_getres(which_clock, &rtn_tp);
1264
1265 if (!error && tp && put_timespec64(&rtn_tp, tp))
1266 error = -EFAULT;
1267
1268 return error;
1269 }
1270
1271 #ifdef CONFIG_COMPAT_32BIT_TIME
1272
SYSCALL_DEFINE2(clock_settime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)1273 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1274 struct old_timespec32 __user *, tp)
1275 {
1276 const struct k_clock *kc = clockid_to_kclock(which_clock);
1277 struct timespec64 ts;
1278
1279 if (!kc || !kc->clock_set)
1280 return -EINVAL;
1281
1282 if (get_old_timespec32(&ts, tp))
1283 return -EFAULT;
1284
1285 return kc->clock_set(which_clock, &ts);
1286 }
1287
SYSCALL_DEFINE2(clock_gettime32,clockid_t,which_clock,struct old_timespec32 __user *,tp)1288 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1289 struct old_timespec32 __user *, tp)
1290 {
1291 const struct k_clock *kc = clockid_to_kclock(which_clock);
1292 struct timespec64 ts;
1293 int err;
1294
1295 if (!kc)
1296 return -EINVAL;
1297
1298 err = kc->clock_get_timespec(which_clock, &ts);
1299
1300 if (!err && put_old_timespec32(&ts, tp))
1301 err = -EFAULT;
1302
1303 return err;
1304 }
1305
SYSCALL_DEFINE2(clock_adjtime32,clockid_t,which_clock,struct old_timex32 __user *,utp)1306 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1307 struct old_timex32 __user *, utp)
1308 {
1309 struct __kernel_timex ktx;
1310 int err;
1311
1312 err = get_old_timex32(&ktx, utp);
1313 if (err)
1314 return err;
1315
1316 err = do_clock_adjtime(which_clock, &ktx);
1317
1318 if (err >= 0 && put_old_timex32(utp, &ktx))
1319 return -EFAULT;
1320
1321 return err;
1322 }
1323
SYSCALL_DEFINE2(clock_getres_time32,clockid_t,which_clock,struct old_timespec32 __user *,tp)1324 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1325 struct old_timespec32 __user *, tp)
1326 {
1327 const struct k_clock *kc = clockid_to_kclock(which_clock);
1328 struct timespec64 ts;
1329 int err;
1330
1331 if (!kc)
1332 return -EINVAL;
1333
1334 err = kc->clock_getres(which_clock, &ts);
1335 if (!err && tp && put_old_timespec32(&ts, tp))
1336 return -EFAULT;
1337
1338 return err;
1339 }
1340
1341 #endif
1342
1343 /*
1344 * sys_clock_nanosleep() for CLOCK_REALTIME and CLOCK_TAI
1345 */
common_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)1346 static int common_nsleep(const clockid_t which_clock, int flags,
1347 const struct timespec64 *rqtp)
1348 {
1349 ktime_t texp = timespec64_to_ktime(*rqtp);
1350
1351 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1352 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1353 which_clock);
1354 }
1355
1356 /*
1357 * sys_clock_nanosleep() for CLOCK_MONOTONIC and CLOCK_BOOTTIME
1358 *
1359 * Absolute nanosleeps for these clocks are time-namespace adjusted.
1360 */
common_nsleep_timens(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)1361 static int common_nsleep_timens(const clockid_t which_clock, int flags,
1362 const struct timespec64 *rqtp)
1363 {
1364 ktime_t texp = timespec64_to_ktime(*rqtp);
1365
1366 if (flags & TIMER_ABSTIME)
1367 texp = timens_ktime_to_host(which_clock, texp);
1368
1369 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1370 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1371 which_clock);
1372 }
1373
SYSCALL_DEFINE4(clock_nanosleep,const clockid_t,which_clock,int,flags,const struct __kernel_timespec __user *,rqtp,struct __kernel_timespec __user *,rmtp)1374 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1375 const struct __kernel_timespec __user *, rqtp,
1376 struct __kernel_timespec __user *, rmtp)
1377 {
1378 const struct k_clock *kc = clockid_to_kclock(which_clock);
1379 struct timespec64 t;
1380
1381 if (!kc)
1382 return -EINVAL;
1383 if (!kc->nsleep)
1384 return -EOPNOTSUPP;
1385
1386 if (get_timespec64(&t, rqtp))
1387 return -EFAULT;
1388
1389 if (!timespec64_valid(&t))
1390 return -EINVAL;
1391 if (flags & TIMER_ABSTIME)
1392 rmtp = NULL;
1393 current->restart_block.fn = do_no_restart_syscall;
1394 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1395 current->restart_block.nanosleep.rmtp = rmtp;
1396
1397 return kc->nsleep(which_clock, flags, &t);
1398 }
1399
1400 #ifdef CONFIG_COMPAT_32BIT_TIME
1401
SYSCALL_DEFINE4(clock_nanosleep_time32,clockid_t,which_clock,int,flags,struct old_timespec32 __user *,rqtp,struct old_timespec32 __user *,rmtp)1402 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1403 struct old_timespec32 __user *, rqtp,
1404 struct old_timespec32 __user *, rmtp)
1405 {
1406 const struct k_clock *kc = clockid_to_kclock(which_clock);
1407 struct timespec64 t;
1408
1409 if (!kc)
1410 return -EINVAL;
1411 if (!kc->nsleep)
1412 return -EOPNOTSUPP;
1413
1414 if (get_old_timespec32(&t, rqtp))
1415 return -EFAULT;
1416
1417 if (!timespec64_valid(&t))
1418 return -EINVAL;
1419 if (flags & TIMER_ABSTIME)
1420 rmtp = NULL;
1421 current->restart_block.fn = do_no_restart_syscall;
1422 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1423 current->restart_block.nanosleep.compat_rmtp = rmtp;
1424
1425 return kc->nsleep(which_clock, flags, &t);
1426 }
1427
1428 #endif
1429
1430 static const struct k_clock clock_realtime = {
1431 .clock_getres = posix_get_hrtimer_res,
1432 .clock_get_timespec = posix_get_realtime_timespec,
1433 .clock_get_ktime = posix_get_realtime_ktime,
1434 .clock_set = posix_clock_realtime_set,
1435 .clock_adj = posix_clock_realtime_adj,
1436 .nsleep = common_nsleep,
1437 .timer_create = common_timer_create,
1438 .timer_set = common_timer_set,
1439 .timer_get = common_timer_get,
1440 .timer_del = common_timer_del,
1441 .timer_rearm = common_hrtimer_rearm,
1442 .timer_forward = common_hrtimer_forward,
1443 .timer_remaining = common_hrtimer_remaining,
1444 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1445 .timer_wait_running = common_timer_wait_running,
1446 .timer_arm = common_hrtimer_arm,
1447 };
1448
1449 static const struct k_clock clock_monotonic = {
1450 .clock_getres = posix_get_hrtimer_res,
1451 .clock_get_timespec = posix_get_monotonic_timespec,
1452 .clock_get_ktime = posix_get_monotonic_ktime,
1453 .nsleep = common_nsleep_timens,
1454 .timer_create = common_timer_create,
1455 .timer_set = common_timer_set,
1456 .timer_get = common_timer_get,
1457 .timer_del = common_timer_del,
1458 .timer_rearm = common_hrtimer_rearm,
1459 .timer_forward = common_hrtimer_forward,
1460 .timer_remaining = common_hrtimer_remaining,
1461 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1462 .timer_wait_running = common_timer_wait_running,
1463 .timer_arm = common_hrtimer_arm,
1464 };
1465
1466 static const struct k_clock clock_monotonic_raw = {
1467 .clock_getres = posix_get_hrtimer_res,
1468 .clock_get_timespec = posix_get_monotonic_raw,
1469 };
1470
1471 static const struct k_clock clock_realtime_coarse = {
1472 .clock_getres = posix_get_coarse_res,
1473 .clock_get_timespec = posix_get_realtime_coarse,
1474 };
1475
1476 static const struct k_clock clock_monotonic_coarse = {
1477 .clock_getres = posix_get_coarse_res,
1478 .clock_get_timespec = posix_get_monotonic_coarse,
1479 };
1480
1481 static const struct k_clock clock_tai = {
1482 .clock_getres = posix_get_hrtimer_res,
1483 .clock_get_ktime = posix_get_tai_ktime,
1484 .clock_get_timespec = posix_get_tai_timespec,
1485 .nsleep = common_nsleep,
1486 .timer_create = common_timer_create,
1487 .timer_set = common_timer_set,
1488 .timer_get = common_timer_get,
1489 .timer_del = common_timer_del,
1490 .timer_rearm = common_hrtimer_rearm,
1491 .timer_forward = common_hrtimer_forward,
1492 .timer_remaining = common_hrtimer_remaining,
1493 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1494 .timer_wait_running = common_timer_wait_running,
1495 .timer_arm = common_hrtimer_arm,
1496 };
1497
1498 static const struct k_clock clock_boottime = {
1499 .clock_getres = posix_get_hrtimer_res,
1500 .clock_get_ktime = posix_get_boottime_ktime,
1501 .clock_get_timespec = posix_get_boottime_timespec,
1502 .nsleep = common_nsleep_timens,
1503 .timer_create = common_timer_create,
1504 .timer_set = common_timer_set,
1505 .timer_get = common_timer_get,
1506 .timer_del = common_timer_del,
1507 .timer_rearm = common_hrtimer_rearm,
1508 .timer_forward = common_hrtimer_forward,
1509 .timer_remaining = common_hrtimer_remaining,
1510 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1511 .timer_wait_running = common_timer_wait_running,
1512 .timer_arm = common_hrtimer_arm,
1513 };
1514
1515 static const struct k_clock * const posix_clocks[] = {
1516 [CLOCK_REALTIME] = &clock_realtime,
1517 [CLOCK_MONOTONIC] = &clock_monotonic,
1518 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1519 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1520 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1521 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1522 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1523 [CLOCK_BOOTTIME] = &clock_boottime,
1524 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1525 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1526 [CLOCK_TAI] = &clock_tai,
1527 };
1528
clockid_to_kclock(const clockid_t id)1529 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1530 {
1531 clockid_t idx = id;
1532
1533 if (id < 0) {
1534 return (id & CLOCKFD_MASK) == CLOCKFD ?
1535 &clock_posix_dynamic : &clock_posix_cpu;
1536 }
1537
1538 if (id >= ARRAY_SIZE(posix_clocks))
1539 return NULL;
1540
1541 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1542 }
1543