GNU Linux-libre 5.10.217-gnu1
[releases.git] / kernel / time / posix-timers.c
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 /*
39  * Management arrays for POSIX timers. Timers are now kept in static hash table
40  * with 512 entries.
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
44  * processes.
45  */
46
47 /*
48  * Lets keep our timers in a slab cache :-)
49  */
50 static struct kmem_cache *posix_timers_cache;
51
52 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
53 static DEFINE_SPINLOCK(hash_lock);
54
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;
58
59 /*
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.
62  */
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!"
66 #endif
67
68 /*
69  * The timer ID is turned into a timer address by idr_find().
70  * Verifying a valid ID consists of:
71  *
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.
75  */
76
77 /*
78  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
79  *          to implement others.  This structure defines the various
80  *          clocks.
81  *
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...
89  *
90  * FUNCTIONS: The CLOCKs structure defines possible functions to
91  *          handle various clock functions.
92  *
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.
97  *
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().
104  */
105 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
106
107 #define lock_timer(tid, flags)                                             \
108 ({      struct k_itimer *__timr;                                           \
109         __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags));  \
110         __timr;                                                            \
111 })
112
113 static int hash(struct signal_struct *sig, unsigned int nr)
114 {
115         return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
116 }
117
118 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
119                                             struct signal_struct *sig,
120                                             timer_t id)
121 {
122         struct k_itimer *timer;
123
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))
127                         return timer;
128         }
129         return NULL;
130 }
131
132 static struct k_itimer *posix_timer_by_id(timer_t id)
133 {
134         struct signal_struct *sig = current->signal;
135         struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
136
137         return __posix_timers_find(head, sig, id);
138 }
139
140 static int posix_timer_add(struct k_itimer *timer)
141 {
142         struct signal_struct *sig = current->signal;
143         struct hlist_head *head;
144         unsigned int cnt, id;
145
146         /*
147          * FIXME: Replace this by a per signal struct xarray once there is
148          * a plan to handle the resulting CRIU regression gracefully.
149          */
150         for (cnt = 0; cnt <= INT_MAX; cnt++) {
151                 spin_lock(&hash_lock);
152                 id = sig->next_posix_timer_id;
153
154                 /* Write the next ID back. Clamp it to the positive space */
155                 sig->next_posix_timer_id = (id + 1) & INT_MAX;
156
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);
161                         return id;
162                 }
163                 spin_unlock(&hash_lock);
164         }
165         /* POSIX return code when no timer ID could be allocated */
166         return -EAGAIN;
167 }
168
169 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
170 {
171         spin_unlock_irqrestore(&timr->it_lock, flags);
172 }
173
174 /* Get clock_realtime */
175 static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
176 {
177         ktime_get_real_ts64(tp);
178         return 0;
179 }
180
181 static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
182 {
183         return ktime_get_real();
184 }
185
186 /* Set clock_realtime */
187 static int posix_clock_realtime_set(const clockid_t which_clock,
188                                     const struct timespec64 *tp)
189 {
190         return do_sys_settimeofday64(tp, NULL);
191 }
192
193 static int posix_clock_realtime_adj(const clockid_t which_clock,
194                                     struct __kernel_timex *t)
195 {
196         return do_adjtimex(t);
197 }
198
199 /*
200  * Get monotonic time for posix timers
201  */
202 static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
203 {
204         ktime_get_ts64(tp);
205         timens_add_monotonic(tp);
206         return 0;
207 }
208
209 static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
210 {
211         return ktime_get();
212 }
213
214 /*
215  * Get monotonic-raw time for posix timers
216  */
217 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
218 {
219         ktime_get_raw_ts64(tp);
220         timens_add_monotonic(tp);
221         return 0;
222 }
223
224
225 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
226 {
227         ktime_get_coarse_real_ts64(tp);
228         return 0;
229 }
230
231 static int posix_get_monotonic_coarse(clockid_t which_clock,
232                                                 struct timespec64 *tp)
233 {
234         ktime_get_coarse_ts64(tp);
235         timens_add_monotonic(tp);
236         return 0;
237 }
238
239 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
240 {
241         *tp = ktime_to_timespec64(KTIME_LOW_RES);
242         return 0;
243 }
244
245 static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
246 {
247         ktime_get_boottime_ts64(tp);
248         timens_add_boottime(tp);
249         return 0;
250 }
251
252 static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
253 {
254         return ktime_get_boottime();
255 }
256
257 static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
258 {
259         ktime_get_clocktai_ts64(tp);
260         return 0;
261 }
262
263 static ktime_t posix_get_tai_ktime(clockid_t which_clock)
264 {
265         return ktime_get_clocktai();
266 }
267
268 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
269 {
270         tp->tv_sec = 0;
271         tp->tv_nsec = hrtimer_resolution;
272         return 0;
273 }
274
275 /*
276  * Initialize everything, well, just everything in Posix clocks/timers ;)
277  */
278 static __init int init_posix_timers(void)
279 {
280         posix_timers_cache = kmem_cache_create("posix_timers_cache",
281                                         sizeof (struct k_itimer), 0, SLAB_PANIC,
282                                         NULL);
283         return 0;
284 }
285 __initcall(init_posix_timers);
286
287 /*
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
290  */
291 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
292 {
293         s64 sum = timr->it_overrun_last + (s64)baseval;
294
295         return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
296 }
297
298 static void common_hrtimer_rearm(struct k_itimer *timr)
299 {
300         struct hrtimer *timer = &timr->it.real.timer;
301
302         timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
303                                             timr->it_interval);
304         hrtimer_restart(timer);
305 }
306
307 /*
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
313  * info block).
314  *
315  * To protect against the timer going away while the interrupt is queued,
316  * we require that the it_requeue_pending flag be set.
317  */
318 void posixtimer_rearm(struct kernel_siginfo *info)
319 {
320         struct k_itimer *timr;
321         unsigned long flags;
322
323         timr = lock_timer(info->si_tid, &flags);
324         if (!timr)
325                 return;
326
327         if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
328                 timr->kclock->timer_rearm(timr);
329
330                 timr->it_active = 1;
331                 timr->it_overrun_last = timr->it_overrun;
332                 timr->it_overrun = -1LL;
333                 ++timr->it_requeue_pending;
334
335                 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
336         }
337
338         unlock_timer(timr, flags);
339 }
340
341 int posix_timer_event(struct k_itimer *timr, int si_private)
342 {
343         enum pid_type type;
344         int ret = -1;
345         /*
346          * FIXME: if ->sigq is queued we can race with
347          * dequeue_signal()->posixtimer_rearm().
348          *
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.
355          */
356         timr->sigq->info.si_sys_private = si_private;
357
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. */
361         return ret > 0;
362 }
363
364 /*
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.
368
369  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
370  */
371 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
372 {
373         struct k_itimer *timr;
374         unsigned long flags;
375         int si_private = 0;
376         enum hrtimer_restart ret = HRTIMER_NORESTART;
377
378         timr = container_of(timer, struct k_itimer, it.real.timer);
379         spin_lock_irqsave(&timr->it_lock, flags);
380
381         timr->it_active = 0;
382         if (timr->it_interval != 0)
383                 si_private = ++timr->it_requeue_pending;
384
385         if (posix_timer_event(timr, si_private)) {
386                 /*
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.
390                  */
391                 if (timr->it_interval != 0) {
392                         ktime_t now = hrtimer_cb_get_time(timer);
393
394                         /*
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
400                          * the release cycle.
401                          *
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.
415                          */
416 #ifdef CONFIG_HIGH_RES_TIMERS
417                         {
418                                 ktime_t kj = NSEC_PER_SEC / HZ;
419
420                                 if (timr->it_interval < kj)
421                                         now = ktime_add(now, kj);
422                         }
423 #endif
424                         timr->it_overrun += hrtimer_forward(timer, now,
425                                                             timr->it_interval);
426                         ret = HRTIMER_RESTART;
427                         ++timr->it_requeue_pending;
428                         timr->it_active = 1;
429                 }
430         }
431
432         unlock_timer(timr, flags);
433         return ret;
434 }
435
436 static struct pid *good_sigevent(sigevent_t * event)
437 {
438         struct pid *pid = task_tgid(current);
439         struct task_struct *rtn;
440
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))
446                         return NULL;
447                 fallthrough;
448         case SIGEV_SIGNAL:
449         case SIGEV_THREAD:
450                 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
451                         return NULL;
452                 fallthrough;
453         case SIGEV_NONE:
454                 return pid;
455         default:
456                 return NULL;
457         }
458 }
459
460 static struct k_itimer * alloc_posix_timer(void)
461 {
462         struct k_itimer *tmr;
463         tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
464         if (!tmr)
465                 return tmr;
466         if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
467                 kmem_cache_free(posix_timers_cache, tmr);
468                 return NULL;
469         }
470         clear_siginfo(&tmr->sigq->info);
471         return tmr;
472 }
473
474 static void k_itimer_rcu_free(struct rcu_head *head)
475 {
476         struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
477
478         kmem_cache_free(posix_timers_cache, tmr);
479 }
480
481 #define IT_ID_SET       1
482 #define IT_ID_NOT_SET   0
483 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
484 {
485         if (it_id_set) {
486                 unsigned long flags;
487                 spin_lock_irqsave(&hash_lock, flags);
488                 hlist_del_rcu(&tmr->t_hash);
489                 spin_unlock_irqrestore(&hash_lock, flags);
490         }
491         put_pid(tmr->it_pid);
492         sigqueue_free(tmr->sigq);
493         call_rcu(&tmr->rcu, k_itimer_rcu_free);
494 }
495
496 static int common_timer_create(struct k_itimer *new_timer)
497 {
498         hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
499         return 0;
500 }
501
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)
505 {
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;
510
511         if (!kc)
512                 return -EINVAL;
513         if (!kc->timer_create)
514                 return -EOPNOTSUPP;
515
516         new_timer = alloc_posix_timer();
517         if (unlikely(!new_timer))
518                 return -EAGAIN;
519
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;
524                 goto out;
525         }
526
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;
532
533         if (event) {
534                 rcu_read_lock();
535                 new_timer->it_pid = get_pid(good_sigevent(event));
536                 rcu_read_unlock();
537                 if (!new_timer->it_pid) {
538                         error = -EINVAL;
539                         goto out;
540                 }
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;
544         } else {
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));
550         }
551
552         new_timer->sigq->info.si_tid   = new_timer->it_id;
553         new_timer->sigq->info.si_code  = SI_TIMER;
554
555         if (copy_to_user(created_timer_id,
556                          &new_timer_id, sizeof (new_timer_id))) {
557                 error = -EFAULT;
558                 goto out;
559         }
560
561         error = kc->timer_create(new_timer);
562         if (error)
563                 goto out;
564
565         spin_lock_irq(&current->sighand->siglock);
566         new_timer->it_signal = current->signal;
567         list_add(&new_timer->list, &current->signal->posix_timers);
568         spin_unlock_irq(&current->sighand->siglock);
569
570         return 0;
571         /*
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.
576          */
577 out:
578         release_posix_timer(new_timer, it_id_set);
579         return error;
580 }
581
582 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
583                 struct sigevent __user *, timer_event_spec,
584                 timer_t __user *, created_timer_id)
585 {
586         if (timer_event_spec) {
587                 sigevent_t event;
588
589                 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
590                         return -EFAULT;
591                 return do_timer_create(which_clock, &event, created_timer_id);
592         }
593         return do_timer_create(which_clock, NULL, created_timer_id);
594 }
595
596 #ifdef CONFIG_COMPAT
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)
600 {
601         if (timer_event_spec) {
602                 sigevent_t event;
603
604                 if (get_compat_sigevent(&event, timer_event_spec))
605                         return -EFAULT;
606                 return do_timer_create(which_clock, &event, created_timer_id);
607         }
608         return do_timer_create(which_clock, NULL, created_timer_id);
609 }
610 #endif
611
612 /*
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.
618  */
619 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
620 {
621         struct k_itimer *timr;
622
623         /*
624          * timer_t could be any type >= int and we want to make sure any
625          * @timer_id outside positive int range fails lookup.
626          */
627         if ((unsigned long long)timer_id > INT_MAX)
628                 return NULL;
629
630         rcu_read_lock();
631         timr = posix_timer_by_id(timer_id);
632         if (timr) {
633                 spin_lock_irqsave(&timr->it_lock, *flags);
634                 if (timr->it_signal == current->signal) {
635                         rcu_read_unlock();
636                         return timr;
637                 }
638                 spin_unlock_irqrestore(&timr->it_lock, *flags);
639         }
640         rcu_read_unlock();
641
642         return NULL;
643 }
644
645 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
646 {
647         struct hrtimer *timer = &timr->it.real.timer;
648
649         return __hrtimer_expires_remaining_adjusted(timer, now);
650 }
651
652 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
653 {
654         struct hrtimer *timer = &timr->it.real.timer;
655
656         return hrtimer_forward(timer, now, timr->it_interval);
657 }
658
659 /*
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
662  * mess with irq.
663  *
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
667  * now.
668  *
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
673  * report.
674  */
675 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
676 {
677         const struct k_clock *kc = timr->kclock;
678         ktime_t now, remaining, iv;
679         bool sig_none;
680
681         sig_none = timr->it_sigev_notify == SIGEV_NONE;
682         iv = timr->it_interval;
683
684         /* interval timer ? */
685         if (iv) {
686                 cur_setting->it_interval = ktime_to_timespec64(iv);
687         } else if (!timr->it_active) {
688                 /*
689                  * SIGEV_NONE oneshot timers are never queued. Check them
690                  * below.
691                  */
692                 if (!sig_none)
693                         return;
694         }
695
696         now = kc->clock_get_ktime(timr->it_clock);
697
698         /*
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.
701          */
702         if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
703                 timr->it_overrun += kc->timer_forward(timr, now);
704
705         remaining = kc->timer_remaining(timr, now);
706         /* Return 0 only, when the timer is expired and not pending */
707         if (remaining <= 0) {
708                 /*
709                  * A single shot SIGEV_NONE timer must return 0, when
710                  * it is expired !
711                  */
712                 if (!sig_none)
713                         cur_setting->it_value.tv_nsec = 1;
714         } else {
715                 cur_setting->it_value = ktime_to_timespec64(remaining);
716         }
717 }
718
719 /* Get the time remaining on a POSIX.1b interval timer. */
720 static int do_timer_gettime(timer_t timer_id,  struct itimerspec64 *setting)
721 {
722         struct k_itimer *timr;
723         const struct k_clock *kc;
724         unsigned long flags;
725         int ret = 0;
726
727         timr = lock_timer(timer_id, &flags);
728         if (!timr)
729                 return -EINVAL;
730
731         memset(setting, 0, sizeof(*setting));
732         kc = timr->kclock;
733         if (WARN_ON_ONCE(!kc || !kc->timer_get))
734                 ret = -EINVAL;
735         else
736                 kc->timer_get(timr, setting);
737
738         unlock_timer(timr, flags);
739         return ret;
740 }
741
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)
745 {
746         struct itimerspec64 cur_setting;
747
748         int ret = do_timer_gettime(timer_id, &cur_setting);
749         if (!ret) {
750                 if (put_itimerspec64(&cur_setting, setting))
751                         ret = -EFAULT;
752         }
753         return ret;
754 }
755
756 #ifdef CONFIG_COMPAT_32BIT_TIME
757
758 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
759                 struct old_itimerspec32 __user *, setting)
760 {
761         struct itimerspec64 cur_setting;
762
763         int ret = do_timer_gettime(timer_id, &cur_setting);
764         if (!ret) {
765                 if (put_old_itimerspec32(&cur_setting, setting))
766                         ret = -EFAULT;
767         }
768         return ret;
769 }
770
771 #endif
772
773 /*
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.
781  */
782 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
783 {
784         struct k_itimer *timr;
785         int overrun;
786         unsigned long flags;
787
788         timr = lock_timer(timer_id, &flags);
789         if (!timr)
790                 return -EINVAL;
791
792         overrun = timer_overrun_to_int(timr, 0);
793         unlock_timer(timr, flags);
794
795         return overrun;
796 }
797
798 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
799                                bool absolute, bool sigev_none)
800 {
801         struct hrtimer *timer = &timr->it.real.timer;
802         enum hrtimer_mode mode;
803
804         mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
805         /*
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.
810          *
811          * Note: it_clock stays unmodified, because the next timer_set() might
812          * use ABSTIME, so it needs to switch back.
813          */
814         if (timr->it_clock == CLOCK_REALTIME)
815                 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
816
817         hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
818         timr->it.real.timer.function = posix_timer_fn;
819
820         if (!absolute)
821                 expires = ktime_add_safe(expires, timer->base->get_time());
822         hrtimer_set_expires(timer, expires);
823
824         if (!sigev_none)
825                 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
826 }
827
828 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
829 {
830         return hrtimer_try_to_cancel(&timr->it.real.timer);
831 }
832
833 static void common_timer_wait_running(struct k_itimer *timer)
834 {
835         hrtimer_cancel_wait_running(&timer->it.real.timer);
836 }
837
838 /*
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
842  * cpu_relax().
843  */
844 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
845                                            unsigned long *flags)
846 {
847         const struct k_clock *kc = READ_ONCE(timer->kclock);
848         timer_t timer_id = READ_ONCE(timer->it_id);
849
850         /* Prevent kfree(timer) after dropping the lock */
851         rcu_read_lock();
852         unlock_timer(timer, *flags);
853
854         /*
855          * kc->timer_wait_running() might drop RCU lock. So @timer
856          * cannot be touched anymore after the function returns!
857          */
858         if (!WARN_ON_ONCE(!kc->timer_wait_running))
859                 kc->timer_wait_running(timer);
860
861         rcu_read_unlock();
862         /* Relock the timer. It might be not longer hashed. */
863         return lock_timer(timer_id, flags);
864 }
865
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)
870 {
871         const struct k_clock *kc = timr->kclock;
872         bool sigev_none;
873         ktime_t expires;
874
875         if (old_setting)
876                 common_timer_get(timr, old_setting);
877
878         /* Prevent rearming by clearing the interval */
879         timr->it_interval = 0;
880         /*
881          * Careful here. On SMP systems the timer expiry function could be
882          * active and spinning on timr->it_lock.
883          */
884         if (kc->timer_try_to_cancel(timr) < 0)
885                 return TIMER_RETRY;
886
887         timr->it_active = 0;
888         timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
889                 ~REQUEUE_PENDING;
890         timr->it_overrun_last = 0;
891
892         /* Switch off the timer when it_value is zero */
893         if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
894                 return 0;
895
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;
901
902         kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
903         timr->it_active = !sigev_none;
904         return 0;
905 }
906
907 static int do_timer_settime(timer_t timer_id, int tmr_flags,
908                             struct itimerspec64 *new_spec64,
909                             struct itimerspec64 *old_spec64)
910 {
911         const struct k_clock *kc;
912         struct k_itimer *timr;
913         unsigned long flags;
914         int error = 0;
915
916         if (!timespec64_valid(&new_spec64->it_interval) ||
917             !timespec64_valid(&new_spec64->it_value))
918                 return -EINVAL;
919
920         if (old_spec64)
921                 memset(old_spec64, 0, sizeof(*old_spec64));
922
923         timr = lock_timer(timer_id, &flags);
924 retry:
925         if (!timr)
926                 return -EINVAL;
927
928         kc = timr->kclock;
929         if (WARN_ON_ONCE(!kc || !kc->timer_set))
930                 error = -EINVAL;
931         else
932                 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
933
934         if (error == TIMER_RETRY) {
935                 // We already got the old time...
936                 old_spec64 = NULL;
937                 /* Unlocks and relocks the timer if it still exists */
938                 timr = timer_wait_running(timr, &flags);
939                 goto retry;
940         }
941         unlock_timer(timr, flags);
942
943         return error;
944 }
945
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)
950 {
951         struct itimerspec64 new_spec, old_spec;
952         struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
953         int error = 0;
954
955         if (!new_setting)
956                 return -EINVAL;
957
958         if (get_itimerspec64(&new_spec, new_setting))
959                 return -EFAULT;
960
961         error = do_timer_settime(timer_id, flags, &new_spec, rtn);
962         if (!error && old_setting) {
963                 if (put_itimerspec64(&old_spec, old_setting))
964                         error = -EFAULT;
965         }
966         return error;
967 }
968
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)
973 {
974         struct itimerspec64 new_spec, old_spec;
975         struct itimerspec64 *rtn = old ? &old_spec : NULL;
976         int error = 0;
977
978         if (!new)
979                 return -EINVAL;
980         if (get_old_itimerspec32(&new_spec, new))
981                 return -EFAULT;
982
983         error = do_timer_settime(timer_id, flags, &new_spec, rtn);
984         if (!error && old) {
985                 if (put_old_itimerspec32(&old_spec, old))
986                         error = -EFAULT;
987         }
988         return error;
989 }
990 #endif
991
992 int common_timer_del(struct k_itimer *timer)
993 {
994         const struct k_clock *kc = timer->kclock;
995
996         timer->it_interval = 0;
997         if (kc->timer_try_to_cancel(timer) < 0)
998                 return TIMER_RETRY;
999         timer->it_active = 0;
1000         return 0;
1001 }
1002
1003 static inline int timer_delete_hook(struct k_itimer *timer)
1004 {
1005         const struct k_clock *kc = timer->kclock;
1006
1007         if (WARN_ON_ONCE(!kc || !kc->timer_del))
1008                 return -EINVAL;
1009         return kc->timer_del(timer);
1010 }
1011
1012 /* Delete a POSIX.1b interval timer. */
1013 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
1014 {
1015         struct k_itimer *timer;
1016         unsigned long flags;
1017
1018         timer = lock_timer(timer_id, &flags);
1019
1020 retry_delete:
1021         if (!timer)
1022                 return -EINVAL;
1023
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);
1027                 goto retry_delete;
1028         }
1029
1030         spin_lock(&current->sighand->siglock);
1031         list_del(&timer->list);
1032         spin_unlock(&current->sighand->siglock);
1033         /*
1034          * This keeps any tasks waiting on the spin lock from thinking
1035          * they got something (see the lock code above).
1036          */
1037         timer->it_signal = NULL;
1038
1039         unlock_timer(timer, flags);
1040         release_posix_timer(timer, IT_ID_SET);
1041         return 0;
1042 }
1043
1044 /*
1045  * Delete a timer if it is armed, remove it from the hash and schedule it
1046  * for RCU freeing.
1047  */
1048 static void itimer_delete(struct k_itimer *timer)
1049 {
1050         unsigned long flags;
1051
1052         /*
1053          * irqsave is required to make timer_wait_running() work.
1054          */
1055         spin_lock_irqsave(&timer->it_lock, flags);
1056
1057 retry_delete:
1058         /*
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.
1063          */
1064         if (timer_delete_hook(timer) == TIMER_RETRY) {
1065                 /*
1066                  * Timer is expired concurrently, prevent livelocks
1067                  * and pointless spinning on RT.
1068                  *
1069                  * timer_wait_running() drops timer::it_lock, which opens
1070                  * the possibility for another task to delete the timer.
1071                  *
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.
1075                  */
1076                 if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
1077                         return;
1078
1079                 goto retry_delete;
1080         }
1081         list_del(&timer->list);
1082
1083         spin_unlock_irqrestore(&timer->it_lock, flags);
1084         release_posix_timer(timer, IT_ID_SET);
1085 }
1086
1087 /*
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
1090  * task anymore.
1091  */
1092 void exit_itimers(struct task_struct *tsk)
1093 {
1094         struct list_head timers;
1095         struct k_itimer *tmr;
1096
1097         if (list_empty(&tsk->signal->posix_timers))
1098                 return;
1099
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);
1104
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);
1108                 itimer_delete(tmr);
1109         }
1110 }
1111
1112 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1113                 const struct __kernel_timespec __user *, tp)
1114 {
1115         const struct k_clock *kc = clockid_to_kclock(which_clock);
1116         struct timespec64 new_tp;
1117
1118         if (!kc || !kc->clock_set)
1119                 return -EINVAL;
1120
1121         if (get_timespec64(&new_tp, tp))
1122                 return -EFAULT;
1123
1124         return kc->clock_set(which_clock, &new_tp);
1125 }
1126
1127 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1128                 struct __kernel_timespec __user *, tp)
1129 {
1130         const struct k_clock *kc = clockid_to_kclock(which_clock);
1131         struct timespec64 kernel_tp;
1132         int error;
1133
1134         if (!kc)
1135                 return -EINVAL;
1136
1137         error = kc->clock_get_timespec(which_clock, &kernel_tp);
1138
1139         if (!error && put_timespec64(&kernel_tp, tp))
1140                 error = -EFAULT;
1141
1142         return error;
1143 }
1144
1145 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1146 {
1147         const struct k_clock *kc = clockid_to_kclock(which_clock);
1148
1149         if (!kc)
1150                 return -EINVAL;
1151         if (!kc->clock_adj)
1152                 return -EOPNOTSUPP;
1153
1154         return kc->clock_adj(which_clock, ktx);
1155 }
1156
1157 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1158                 struct __kernel_timex __user *, utx)
1159 {
1160         struct __kernel_timex ktx;
1161         int err;
1162
1163         if (copy_from_user(&ktx, utx, sizeof(ktx)))
1164                 return -EFAULT;
1165
1166         err = do_clock_adjtime(which_clock, &ktx);
1167
1168         if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1169                 return -EFAULT;
1170
1171         return err;
1172 }
1173
1174 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1175                 struct __kernel_timespec __user *, tp)
1176 {
1177         const struct k_clock *kc = clockid_to_kclock(which_clock);
1178         struct timespec64 rtn_tp;
1179         int error;
1180
1181         if (!kc)
1182                 return -EINVAL;
1183
1184         error = kc->clock_getres(which_clock, &rtn_tp);
1185
1186         if (!error && tp && put_timespec64(&rtn_tp, tp))
1187                 error = -EFAULT;
1188
1189         return error;
1190 }
1191
1192 #ifdef CONFIG_COMPAT_32BIT_TIME
1193
1194 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1195                 struct old_timespec32 __user *, tp)
1196 {
1197         const struct k_clock *kc = clockid_to_kclock(which_clock);
1198         struct timespec64 ts;
1199
1200         if (!kc || !kc->clock_set)
1201                 return -EINVAL;
1202
1203         if (get_old_timespec32(&ts, tp))
1204                 return -EFAULT;
1205
1206         return kc->clock_set(which_clock, &ts);
1207 }
1208
1209 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1210                 struct old_timespec32 __user *, tp)
1211 {
1212         const struct k_clock *kc = clockid_to_kclock(which_clock);
1213         struct timespec64 ts;
1214         int err;
1215
1216         if (!kc)
1217                 return -EINVAL;
1218
1219         err = kc->clock_get_timespec(which_clock, &ts);
1220
1221         if (!err && put_old_timespec32(&ts, tp))
1222                 err = -EFAULT;
1223
1224         return err;
1225 }
1226
1227 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1228                 struct old_timex32 __user *, utp)
1229 {
1230         struct __kernel_timex ktx;
1231         int err;
1232
1233         err = get_old_timex32(&ktx, utp);
1234         if (err)
1235                 return err;
1236
1237         err = do_clock_adjtime(which_clock, &ktx);
1238
1239         if (err >= 0 && put_old_timex32(utp, &ktx))
1240                 return -EFAULT;
1241
1242         return err;
1243 }
1244
1245 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1246                 struct old_timespec32 __user *, tp)
1247 {
1248         const struct k_clock *kc = clockid_to_kclock(which_clock);
1249         struct timespec64 ts;
1250         int err;
1251
1252         if (!kc)
1253                 return -EINVAL;
1254
1255         err = kc->clock_getres(which_clock, &ts);
1256         if (!err && tp && put_old_timespec32(&ts, tp))
1257                 return -EFAULT;
1258
1259         return err;
1260 }
1261
1262 #endif
1263
1264 /*
1265  * nanosleep for monotonic and realtime clocks
1266  */
1267 static int common_nsleep(const clockid_t which_clock, int flags,
1268                          const struct timespec64 *rqtp)
1269 {
1270         ktime_t texp = timespec64_to_ktime(*rqtp);
1271
1272         return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1273                                  HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1274                                  which_clock);
1275 }
1276
1277 static int common_nsleep_timens(const clockid_t which_clock, int flags,
1278                          const struct timespec64 *rqtp)
1279 {
1280         ktime_t texp = timespec64_to_ktime(*rqtp);
1281
1282         if (flags & TIMER_ABSTIME)
1283                 texp = timens_ktime_to_host(which_clock, texp);
1284
1285         return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1286                                  HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1287                                  which_clock);
1288 }
1289
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)
1293 {
1294         const struct k_clock *kc = clockid_to_kclock(which_clock);
1295         struct timespec64 t;
1296
1297         if (!kc)
1298                 return -EINVAL;
1299         if (!kc->nsleep)
1300                 return -EOPNOTSUPP;
1301
1302         if (get_timespec64(&t, rqtp))
1303                 return -EFAULT;
1304
1305         if (!timespec64_valid(&t))
1306                 return -EINVAL;
1307         if (flags & TIMER_ABSTIME)
1308                 rmtp = NULL;
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;
1312
1313         return kc->nsleep(which_clock, flags, &t);
1314 }
1315
1316 #ifdef CONFIG_COMPAT_32BIT_TIME
1317
1318 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1319                 struct old_timespec32 __user *, rqtp,
1320                 struct old_timespec32 __user *, rmtp)
1321 {
1322         const struct k_clock *kc = clockid_to_kclock(which_clock);
1323         struct timespec64 t;
1324
1325         if (!kc)
1326                 return -EINVAL;
1327         if (!kc->nsleep)
1328                 return -EOPNOTSUPP;
1329
1330         if (get_old_timespec32(&t, rqtp))
1331                 return -EFAULT;
1332
1333         if (!timespec64_valid(&t))
1334                 return -EINVAL;
1335         if (flags & TIMER_ABSTIME)
1336                 rmtp = NULL;
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;
1340
1341         return kc->nsleep(which_clock, flags, &t);
1342 }
1343
1344 #endif
1345
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,
1363 };
1364
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,
1380 };
1381
1382 static const struct k_clock clock_monotonic_raw = {
1383         .clock_getres           = posix_get_hrtimer_res,
1384         .clock_get_timespec     = posix_get_monotonic_raw,
1385 };
1386
1387 static const struct k_clock clock_realtime_coarse = {
1388         .clock_getres           = posix_get_coarse_res,
1389         .clock_get_timespec     = posix_get_realtime_coarse,
1390 };
1391
1392 static const struct k_clock clock_monotonic_coarse = {
1393         .clock_getres           = posix_get_coarse_res,
1394         .clock_get_timespec     = posix_get_monotonic_coarse,
1395 };
1396
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,
1412 };
1413
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,
1429 };
1430
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,
1443 };
1444
1445 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1446 {
1447         clockid_t idx = id;
1448
1449         if (id < 0) {
1450                 return (id & CLOCKFD_MASK) == CLOCKFD ?
1451                         &clock_posix_dynamic : &clock_posix_cpu;
1452         }
1453
1454         if (id >= ARRAY_SIZE(posix_clocks))
1455                 return NULL;
1456
1457         return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1458 }