GNU Linux-libre 5.4.274-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
34 #include "timekeeping.h"
35 #include "posix-timers.h"
36
37 /*
38  * Management arrays for POSIX timers. Timers are now kept in static hash table
39  * with 512 entries.
40  * Timer ids are allocated by local routine, which selects proper hash head by
41  * key, constructed from current->signal address and per signal struct counter.
42  * This keeps timer ids unique per process, but now they can intersect between
43  * processes.
44  */
45
46 /*
47  * Lets keep our timers in a slab cache :-)
48  */
49 static struct kmem_cache *posix_timers_cache;
50
51 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
52 static DEFINE_SPINLOCK(hash_lock);
53
54 static const struct k_clock * const posix_clocks[];
55 static const struct k_clock *clockid_to_kclock(const clockid_t id);
56 static const struct k_clock clock_realtime, clock_monotonic;
57
58 /*
59  * we assume that the new SIGEV_THREAD_ID shares no bits with the other
60  * SIGEV values.  Here we put out an error if this assumption fails.
61  */
62 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
63                        ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
64 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
65 #endif
66
67 /*
68  * The timer ID is turned into a timer address by idr_find().
69  * Verifying a valid ID consists of:
70  *
71  * a) checking that idr_find() returns other than -1.
72  * b) checking that the timer id matches the one in the timer itself.
73  * c) that the timer owner is in the callers thread group.
74  */
75
76 /*
77  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
78  *          to implement others.  This structure defines the various
79  *          clocks.
80  *
81  * RESOLUTION: Clock resolution is used to round up timer and interval
82  *          times, NOT to report clock times, which are reported with as
83  *          much resolution as the system can muster.  In some cases this
84  *          resolution may depend on the underlying clock hardware and
85  *          may not be quantifiable until run time, and only then is the
86  *          necessary code is written.  The standard says we should say
87  *          something about this issue in the documentation...
88  *
89  * FUNCTIONS: The CLOCKs structure defines possible functions to
90  *          handle various clock functions.
91  *
92  *          The standard POSIX timer management code assumes the
93  *          following: 1.) The k_itimer struct (sched.h) is used for
94  *          the timer.  2.) The list, it_lock, it_clock, it_id and
95  *          it_pid fields are not modified by timer code.
96  *
97  * Permissions: It is assumed that the clock_settime() function defined
98  *          for each clock will take care of permission checks.  Some
99  *          clocks may be set able by any user (i.e. local process
100  *          clocks) others not.  Currently the only set able clock we
101  *          have is CLOCK_REALTIME and its high res counter part, both of
102  *          which we beg off on and pass to do_sys_settimeofday().
103  */
104 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
105
106 #define lock_timer(tid, flags)                                             \
107 ({      struct k_itimer *__timr;                                           \
108         __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags));  \
109         __timr;                                                            \
110 })
111
112 static int hash(struct signal_struct *sig, unsigned int nr)
113 {
114         return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
115 }
116
117 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
118                                             struct signal_struct *sig,
119                                             timer_t id)
120 {
121         struct k_itimer *timer;
122
123         hlist_for_each_entry_rcu(timer, head, t_hash) {
124                 if ((timer->it_signal == sig) && (timer->it_id == id))
125                         return timer;
126         }
127         return NULL;
128 }
129
130 static struct k_itimer *posix_timer_by_id(timer_t id)
131 {
132         struct signal_struct *sig = current->signal;
133         struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
134
135         return __posix_timers_find(head, sig, id);
136 }
137
138 static int posix_timer_add(struct k_itimer *timer)
139 {
140         struct signal_struct *sig = current->signal;
141         struct hlist_head *head;
142         unsigned int cnt, id;
143
144         /*
145          * FIXME: Replace this by a per signal struct xarray once there is
146          * a plan to handle the resulting CRIU regression gracefully.
147          */
148         for (cnt = 0; cnt <= INT_MAX; cnt++) {
149                 spin_lock(&hash_lock);
150                 id = sig->next_posix_timer_id;
151
152                 /* Write the next ID back. Clamp it to the positive space */
153                 sig->next_posix_timer_id = (id + 1) & INT_MAX;
154
155                 head = &posix_timers_hashtable[hash(sig, id)];
156                 if (!__posix_timers_find(head, sig, id)) {
157                         hlist_add_head_rcu(&timer->t_hash, head);
158                         spin_unlock(&hash_lock);
159                         return id;
160                 }
161                 spin_unlock(&hash_lock);
162         }
163         /* POSIX return code when no timer ID could be allocated */
164         return -EAGAIN;
165 }
166
167 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
168 {
169         spin_unlock_irqrestore(&timr->it_lock, flags);
170 }
171
172 /* Get clock_realtime */
173 static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
174 {
175         ktime_get_real_ts64(tp);
176         return 0;
177 }
178
179 /* Set clock_realtime */
180 static int posix_clock_realtime_set(const clockid_t which_clock,
181                                     const struct timespec64 *tp)
182 {
183         return do_sys_settimeofday64(tp, NULL);
184 }
185
186 static int posix_clock_realtime_adj(const clockid_t which_clock,
187                                     struct __kernel_timex *t)
188 {
189         return do_adjtimex(t);
190 }
191
192 /*
193  * Get monotonic time for posix timers
194  */
195 static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
196 {
197         ktime_get_ts64(tp);
198         return 0;
199 }
200
201 /*
202  * Get monotonic-raw time for posix timers
203  */
204 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
205 {
206         ktime_get_raw_ts64(tp);
207         return 0;
208 }
209
210
211 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
212 {
213         ktime_get_coarse_real_ts64(tp);
214         return 0;
215 }
216
217 static int posix_get_monotonic_coarse(clockid_t which_clock,
218                                                 struct timespec64 *tp)
219 {
220         ktime_get_coarse_ts64(tp);
221         return 0;
222 }
223
224 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
225 {
226         *tp = ktime_to_timespec64(KTIME_LOW_RES);
227         return 0;
228 }
229
230 static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
231 {
232         ktime_get_boottime_ts64(tp);
233         return 0;
234 }
235
236 static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
237 {
238         ktime_get_clocktai_ts64(tp);
239         return 0;
240 }
241
242 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
243 {
244         tp->tv_sec = 0;
245         tp->tv_nsec = hrtimer_resolution;
246         return 0;
247 }
248
249 /*
250  * Initialize everything, well, just everything in Posix clocks/timers ;)
251  */
252 static __init int init_posix_timers(void)
253 {
254         posix_timers_cache = kmem_cache_create("posix_timers_cache",
255                                         sizeof (struct k_itimer), 0, SLAB_PANIC,
256                                         NULL);
257         return 0;
258 }
259 __initcall(init_posix_timers);
260
261 /*
262  * The siginfo si_overrun field and the return value of timer_getoverrun(2)
263  * are of type int. Clamp the overrun value to INT_MAX
264  */
265 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
266 {
267         s64 sum = timr->it_overrun_last + (s64)baseval;
268
269         return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
270 }
271
272 static void common_hrtimer_rearm(struct k_itimer *timr)
273 {
274         struct hrtimer *timer = &timr->it.real.timer;
275
276         timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
277                                             timr->it_interval);
278         hrtimer_restart(timer);
279 }
280
281 /*
282  * This function is exported for use by the signal deliver code.  It is
283  * called just prior to the info block being released and passes that
284  * block to us.  It's function is to update the overrun entry AND to
285  * restart the timer.  It should only be called if the timer is to be
286  * restarted (i.e. we have flagged this in the sys_private entry of the
287  * info block).
288  *
289  * To protect against the timer going away while the interrupt is queued,
290  * we require that the it_requeue_pending flag be set.
291  */
292 void posixtimer_rearm(struct kernel_siginfo *info)
293 {
294         struct k_itimer *timr;
295         unsigned long flags;
296
297         timr = lock_timer(info->si_tid, &flags);
298         if (!timr)
299                 return;
300
301         if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
302                 timr->kclock->timer_rearm(timr);
303
304                 timr->it_active = 1;
305                 timr->it_overrun_last = timr->it_overrun;
306                 timr->it_overrun = -1LL;
307                 ++timr->it_requeue_pending;
308
309                 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
310         }
311
312         unlock_timer(timr, flags);
313 }
314
315 int posix_timer_event(struct k_itimer *timr, int si_private)
316 {
317         enum pid_type type;
318         int ret = -1;
319         /*
320          * FIXME: if ->sigq is queued we can race with
321          * dequeue_signal()->posixtimer_rearm().
322          *
323          * If dequeue_signal() sees the "right" value of
324          * si_sys_private it calls posixtimer_rearm().
325          * We re-queue ->sigq and drop ->it_lock().
326          * posixtimer_rearm() locks the timer
327          * and re-schedules it while ->sigq is pending.
328          * Not really bad, but not that we want.
329          */
330         timr->sigq->info.si_sys_private = si_private;
331
332         type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
333         ret = send_sigqueue(timr->sigq, timr->it_pid, type);
334         /* If we failed to send the signal the timer stops. */
335         return ret > 0;
336 }
337
338 /*
339  * This function gets called when a POSIX.1b interval timer expires.  It
340  * is used as a callback from the kernel internal timer.  The
341  * run_timer_list code ALWAYS calls with interrupts on.
342
343  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
344  */
345 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
346 {
347         struct k_itimer *timr;
348         unsigned long flags;
349         int si_private = 0;
350         enum hrtimer_restart ret = HRTIMER_NORESTART;
351
352         timr = container_of(timer, struct k_itimer, it.real.timer);
353         spin_lock_irqsave(&timr->it_lock, flags);
354
355         timr->it_active = 0;
356         if (timr->it_interval != 0)
357                 si_private = ++timr->it_requeue_pending;
358
359         if (posix_timer_event(timr, si_private)) {
360                 /*
361                  * signal was not sent because of sig_ignor
362                  * we will not get a call back to restart it AND
363                  * it should be restarted.
364                  */
365                 if (timr->it_interval != 0) {
366                         ktime_t now = hrtimer_cb_get_time(timer);
367
368                         /*
369                          * FIXME: What we really want, is to stop this
370                          * timer completely and restart it in case the
371                          * SIG_IGN is removed. This is a non trivial
372                          * change which involves sighand locking
373                          * (sigh !), which we don't want to do late in
374                          * the release cycle.
375                          *
376                          * For now we just let timers with an interval
377                          * less than a jiffie expire every jiffie to
378                          * avoid softirq starvation in case of SIG_IGN
379                          * and a very small interval, which would put
380                          * the timer right back on the softirq pending
381                          * list. By moving now ahead of time we trick
382                          * hrtimer_forward() to expire the timer
383                          * later, while we still maintain the overrun
384                          * accuracy, but have some inconsistency in
385                          * the timer_gettime() case. This is at least
386                          * better than a starved softirq. A more
387                          * complex fix which solves also another related
388                          * inconsistency is already in the pipeline.
389                          */
390 #ifdef CONFIG_HIGH_RES_TIMERS
391                         {
392                                 ktime_t kj = NSEC_PER_SEC / HZ;
393
394                                 if (timr->it_interval < kj)
395                                         now = ktime_add(now, kj);
396                         }
397 #endif
398                         timr->it_overrun += hrtimer_forward(timer, now,
399                                                             timr->it_interval);
400                         ret = HRTIMER_RESTART;
401                         ++timr->it_requeue_pending;
402                         timr->it_active = 1;
403                 }
404         }
405
406         unlock_timer(timr, flags);
407         return ret;
408 }
409
410 static struct pid *good_sigevent(sigevent_t * event)
411 {
412         struct pid *pid = task_tgid(current);
413         struct task_struct *rtn;
414
415         switch (event->sigev_notify) {
416         case SIGEV_SIGNAL | SIGEV_THREAD_ID:
417                 pid = find_vpid(event->sigev_notify_thread_id);
418                 rtn = pid_task(pid, PIDTYPE_PID);
419                 if (!rtn || !same_thread_group(rtn, current))
420                         return NULL;
421                 /* FALLTHRU */
422         case SIGEV_SIGNAL:
423         case SIGEV_THREAD:
424                 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
425                         return NULL;
426                 /* FALLTHRU */
427         case SIGEV_NONE:
428                 return pid;
429         default:
430                 return NULL;
431         }
432 }
433
434 static struct k_itimer * alloc_posix_timer(void)
435 {
436         struct k_itimer *tmr;
437         tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
438         if (!tmr)
439                 return tmr;
440         if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
441                 kmem_cache_free(posix_timers_cache, tmr);
442                 return NULL;
443         }
444         clear_siginfo(&tmr->sigq->info);
445         return tmr;
446 }
447
448 static void k_itimer_rcu_free(struct rcu_head *head)
449 {
450         struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
451
452         kmem_cache_free(posix_timers_cache, tmr);
453 }
454
455 #define IT_ID_SET       1
456 #define IT_ID_NOT_SET   0
457 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
458 {
459         if (it_id_set) {
460                 unsigned long flags;
461                 spin_lock_irqsave(&hash_lock, flags);
462                 hlist_del_rcu(&tmr->t_hash);
463                 spin_unlock_irqrestore(&hash_lock, flags);
464         }
465         put_pid(tmr->it_pid);
466         sigqueue_free(tmr->sigq);
467         call_rcu(&tmr->rcu, k_itimer_rcu_free);
468 }
469
470 static int common_timer_create(struct k_itimer *new_timer)
471 {
472         hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
473         return 0;
474 }
475
476 /* Create a POSIX.1b interval timer. */
477 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
478                            timer_t __user *created_timer_id)
479 {
480         const struct k_clock *kc = clockid_to_kclock(which_clock);
481         struct k_itimer *new_timer;
482         int error, new_timer_id;
483         int it_id_set = IT_ID_NOT_SET;
484
485         if (!kc)
486                 return -EINVAL;
487         if (!kc->timer_create)
488                 return -EOPNOTSUPP;
489
490         new_timer = alloc_posix_timer();
491         if (unlikely(!new_timer))
492                 return -EAGAIN;
493
494         spin_lock_init(&new_timer->it_lock);
495         new_timer_id = posix_timer_add(new_timer);
496         if (new_timer_id < 0) {
497                 error = new_timer_id;
498                 goto out;
499         }
500
501         it_id_set = IT_ID_SET;
502         new_timer->it_id = (timer_t) new_timer_id;
503         new_timer->it_clock = which_clock;
504         new_timer->kclock = kc;
505         new_timer->it_overrun = -1LL;
506
507         if (event) {
508                 rcu_read_lock();
509                 new_timer->it_pid = get_pid(good_sigevent(event));
510                 rcu_read_unlock();
511                 if (!new_timer->it_pid) {
512                         error = -EINVAL;
513                         goto out;
514                 }
515                 new_timer->it_sigev_notify     = event->sigev_notify;
516                 new_timer->sigq->info.si_signo = event->sigev_signo;
517                 new_timer->sigq->info.si_value = event->sigev_value;
518         } else {
519                 new_timer->it_sigev_notify     = SIGEV_SIGNAL;
520                 new_timer->sigq->info.si_signo = SIGALRM;
521                 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
522                 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
523                 new_timer->it_pid = get_pid(task_tgid(current));
524         }
525
526         new_timer->sigq->info.si_tid   = new_timer->it_id;
527         new_timer->sigq->info.si_code  = SI_TIMER;
528
529         if (copy_to_user(created_timer_id,
530                          &new_timer_id, sizeof (new_timer_id))) {
531                 error = -EFAULT;
532                 goto out;
533         }
534
535         error = kc->timer_create(new_timer);
536         if (error)
537                 goto out;
538
539         spin_lock_irq(&current->sighand->siglock);
540         new_timer->it_signal = current->signal;
541         list_add(&new_timer->list, &current->signal->posix_timers);
542         spin_unlock_irq(&current->sighand->siglock);
543
544         return 0;
545         /*
546          * In the case of the timer belonging to another task, after
547          * the task is unlocked, the timer is owned by the other task
548          * and may cease to exist at any time.  Don't use or modify
549          * new_timer after the unlock call.
550          */
551 out:
552         release_posix_timer(new_timer, it_id_set);
553         return error;
554 }
555
556 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
557                 struct sigevent __user *, timer_event_spec,
558                 timer_t __user *, created_timer_id)
559 {
560         if (timer_event_spec) {
561                 sigevent_t event;
562
563                 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
564                         return -EFAULT;
565                 return do_timer_create(which_clock, &event, created_timer_id);
566         }
567         return do_timer_create(which_clock, NULL, created_timer_id);
568 }
569
570 #ifdef CONFIG_COMPAT
571 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
572                        struct compat_sigevent __user *, timer_event_spec,
573                        timer_t __user *, created_timer_id)
574 {
575         if (timer_event_spec) {
576                 sigevent_t event;
577
578                 if (get_compat_sigevent(&event, timer_event_spec))
579                         return -EFAULT;
580                 return do_timer_create(which_clock, &event, created_timer_id);
581         }
582         return do_timer_create(which_clock, NULL, created_timer_id);
583 }
584 #endif
585
586 /*
587  * Locking issues: We need to protect the result of the id look up until
588  * we get the timer locked down so it is not deleted under us.  The
589  * removal is done under the idr spinlock so we use that here to bridge
590  * the find to the timer lock.  To avoid a dead lock, the timer id MUST
591  * be release with out holding the timer lock.
592  */
593 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
594 {
595         struct k_itimer *timr;
596
597         /*
598          * timer_t could be any type >= int and we want to make sure any
599          * @timer_id outside positive int range fails lookup.
600          */
601         if ((unsigned long long)timer_id > INT_MAX)
602                 return NULL;
603
604         rcu_read_lock();
605         timr = posix_timer_by_id(timer_id);
606         if (timr) {
607                 spin_lock_irqsave(&timr->it_lock, *flags);
608                 if (timr->it_signal == current->signal) {
609                         rcu_read_unlock();
610                         return timr;
611                 }
612                 spin_unlock_irqrestore(&timr->it_lock, *flags);
613         }
614         rcu_read_unlock();
615
616         return NULL;
617 }
618
619 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
620 {
621         struct hrtimer *timer = &timr->it.real.timer;
622
623         return __hrtimer_expires_remaining_adjusted(timer, now);
624 }
625
626 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
627 {
628         struct hrtimer *timer = &timr->it.real.timer;
629
630         return hrtimer_forward(timer, now, timr->it_interval);
631 }
632
633 /*
634  * Get the time remaining on a POSIX.1b interval timer.  This function
635  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
636  * mess with irq.
637  *
638  * We have a couple of messes to clean up here.  First there is the case
639  * of a timer that has a requeue pending.  These timers should appear to
640  * be in the timer list with an expiry as if we were to requeue them
641  * now.
642  *
643  * The second issue is the SIGEV_NONE timer which may be active but is
644  * not really ever put in the timer list (to save system resources).
645  * This timer may be expired, and if so, we will do it here.  Otherwise
646  * it is the same as a requeue pending timer WRT to what we should
647  * report.
648  */
649 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
650 {
651         const struct k_clock *kc = timr->kclock;
652         ktime_t now, remaining, iv;
653         struct timespec64 ts64;
654         bool sig_none;
655
656         sig_none = timr->it_sigev_notify == SIGEV_NONE;
657         iv = timr->it_interval;
658
659         /* interval timer ? */
660         if (iv) {
661                 cur_setting->it_interval = ktime_to_timespec64(iv);
662         } else if (!timr->it_active) {
663                 /*
664                  * SIGEV_NONE oneshot timers are never queued. Check them
665                  * below.
666                  */
667                 if (!sig_none)
668                         return;
669         }
670
671         /*
672          * The timespec64 based conversion is suboptimal, but it's not
673          * worth to implement yet another callback.
674          */
675         kc->clock_get(timr->it_clock, &ts64);
676         now = timespec64_to_ktime(ts64);
677
678         /*
679          * When a requeue is pending or this is a SIGEV_NONE timer move the
680          * expiry time forward by intervals, so expiry is > now.
681          */
682         if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
683                 timr->it_overrun += kc->timer_forward(timr, now);
684
685         remaining = kc->timer_remaining(timr, now);
686         /* Return 0 only, when the timer is expired and not pending */
687         if (remaining <= 0) {
688                 /*
689                  * A single shot SIGEV_NONE timer must return 0, when
690                  * it is expired !
691                  */
692                 if (!sig_none)
693                         cur_setting->it_value.tv_nsec = 1;
694         } else {
695                 cur_setting->it_value = ktime_to_timespec64(remaining);
696         }
697 }
698
699 /* Get the time remaining on a POSIX.1b interval timer. */
700 static int do_timer_gettime(timer_t timer_id,  struct itimerspec64 *setting)
701 {
702         struct k_itimer *timr;
703         const struct k_clock *kc;
704         unsigned long flags;
705         int ret = 0;
706
707         timr = lock_timer(timer_id, &flags);
708         if (!timr)
709                 return -EINVAL;
710
711         memset(setting, 0, sizeof(*setting));
712         kc = timr->kclock;
713         if (WARN_ON_ONCE(!kc || !kc->timer_get))
714                 ret = -EINVAL;
715         else
716                 kc->timer_get(timr, setting);
717
718         unlock_timer(timr, flags);
719         return ret;
720 }
721
722 /* Get the time remaining on a POSIX.1b interval timer. */
723 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
724                 struct __kernel_itimerspec __user *, setting)
725 {
726         struct itimerspec64 cur_setting;
727
728         int ret = do_timer_gettime(timer_id, &cur_setting);
729         if (!ret) {
730                 if (put_itimerspec64(&cur_setting, setting))
731                         ret = -EFAULT;
732         }
733         return ret;
734 }
735
736 #ifdef CONFIG_COMPAT_32BIT_TIME
737
738 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
739                 struct old_itimerspec32 __user *, setting)
740 {
741         struct itimerspec64 cur_setting;
742
743         int ret = do_timer_gettime(timer_id, &cur_setting);
744         if (!ret) {
745                 if (put_old_itimerspec32(&cur_setting, setting))
746                         ret = -EFAULT;
747         }
748         return ret;
749 }
750
751 #endif
752
753 /*
754  * Get the number of overruns of a POSIX.1b interval timer.  This is to
755  * be the overrun of the timer last delivered.  At the same time we are
756  * accumulating overruns on the next timer.  The overrun is frozen when
757  * the signal is delivered, either at the notify time (if the info block
758  * is not queued) or at the actual delivery time (as we are informed by
759  * the call back to posixtimer_rearm().  So all we need to do is
760  * to pick up the frozen overrun.
761  */
762 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
763 {
764         struct k_itimer *timr;
765         int overrun;
766         unsigned long flags;
767
768         timr = lock_timer(timer_id, &flags);
769         if (!timr)
770                 return -EINVAL;
771
772         overrun = timer_overrun_to_int(timr, 0);
773         unlock_timer(timr, flags);
774
775         return overrun;
776 }
777
778 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
779                                bool absolute, bool sigev_none)
780 {
781         struct hrtimer *timer = &timr->it.real.timer;
782         enum hrtimer_mode mode;
783
784         mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
785         /*
786          * Posix magic: Relative CLOCK_REALTIME timers are not affected by
787          * clock modifications, so they become CLOCK_MONOTONIC based under the
788          * hood. See hrtimer_init(). Update timr->kclock, so the generic
789          * functions which use timr->kclock->clock_get() work.
790          *
791          * Note: it_clock stays unmodified, because the next timer_set() might
792          * use ABSTIME, so it needs to switch back.
793          */
794         if (timr->it_clock == CLOCK_REALTIME)
795                 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
796
797         hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
798         timr->it.real.timer.function = posix_timer_fn;
799
800         if (!absolute)
801                 expires = ktime_add_safe(expires, timer->base->get_time());
802         hrtimer_set_expires(timer, expires);
803
804         if (!sigev_none)
805                 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
806 }
807
808 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
809 {
810         return hrtimer_try_to_cancel(&timr->it.real.timer);
811 }
812
813 static void common_timer_wait_running(struct k_itimer *timer)
814 {
815         hrtimer_cancel_wait_running(&timer->it.real.timer);
816 }
817
818 /*
819  * On PREEMPT_RT this prevent priority inversion against softirq kthread in
820  * case it gets preempted while executing a timer callback. See comments in
821  * hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a
822  * cpu_relax().
823  */
824 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
825                                            unsigned long *flags)
826 {
827         const struct k_clock *kc = READ_ONCE(timer->kclock);
828         timer_t timer_id = READ_ONCE(timer->it_id);
829
830         /* Prevent kfree(timer) after dropping the lock */
831         rcu_read_lock();
832         unlock_timer(timer, *flags);
833
834         if (!WARN_ON_ONCE(!kc->timer_wait_running))
835                 kc->timer_wait_running(timer);
836
837         rcu_read_unlock();
838         /* Relock the timer. It might be not longer hashed. */
839         return lock_timer(timer_id, flags);
840 }
841
842 /* Set a POSIX.1b interval timer. */
843 int common_timer_set(struct k_itimer *timr, int flags,
844                      struct itimerspec64 *new_setting,
845                      struct itimerspec64 *old_setting)
846 {
847         const struct k_clock *kc = timr->kclock;
848         bool sigev_none;
849         ktime_t expires;
850
851         if (old_setting)
852                 common_timer_get(timr, old_setting);
853
854         /* Prevent rearming by clearing the interval */
855         timr->it_interval = 0;
856         /*
857          * Careful here. On SMP systems the timer expiry function could be
858          * active and spinning on timr->it_lock.
859          */
860         if (kc->timer_try_to_cancel(timr) < 0)
861                 return TIMER_RETRY;
862
863         timr->it_active = 0;
864         timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
865                 ~REQUEUE_PENDING;
866         timr->it_overrun_last = 0;
867
868         /* Switch off the timer when it_value is zero */
869         if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
870                 return 0;
871
872         timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
873         expires = timespec64_to_ktime(new_setting->it_value);
874         sigev_none = timr->it_sigev_notify == SIGEV_NONE;
875
876         kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
877         timr->it_active = !sigev_none;
878         return 0;
879 }
880
881 static int do_timer_settime(timer_t timer_id, int tmr_flags,
882                             struct itimerspec64 *new_spec64,
883                             struct itimerspec64 *old_spec64)
884 {
885         const struct k_clock *kc;
886         struct k_itimer *timr;
887         unsigned long flags;
888         int error = 0;
889
890         if (!timespec64_valid(&new_spec64->it_interval) ||
891             !timespec64_valid(&new_spec64->it_value))
892                 return -EINVAL;
893
894         if (old_spec64)
895                 memset(old_spec64, 0, sizeof(*old_spec64));
896
897         timr = lock_timer(timer_id, &flags);
898 retry:
899         if (!timr)
900                 return -EINVAL;
901
902         kc = timr->kclock;
903         if (WARN_ON_ONCE(!kc || !kc->timer_set))
904                 error = -EINVAL;
905         else
906                 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
907
908         if (error == TIMER_RETRY) {
909                 // We already got the old time...
910                 old_spec64 = NULL;
911                 /* Unlocks and relocks the timer if it still exists */
912                 timr = timer_wait_running(timr, &flags);
913                 goto retry;
914         }
915         unlock_timer(timr, flags);
916
917         return error;
918 }
919
920 /* Set a POSIX.1b interval timer */
921 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
922                 const struct __kernel_itimerspec __user *, new_setting,
923                 struct __kernel_itimerspec __user *, old_setting)
924 {
925         struct itimerspec64 new_spec, old_spec;
926         struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
927         int error = 0;
928
929         if (!new_setting)
930                 return -EINVAL;
931
932         if (get_itimerspec64(&new_spec, new_setting))
933                 return -EFAULT;
934
935         error = do_timer_settime(timer_id, flags, &new_spec, rtn);
936         if (!error && old_setting) {
937                 if (put_itimerspec64(&old_spec, old_setting))
938                         error = -EFAULT;
939         }
940         return error;
941 }
942
943 #ifdef CONFIG_COMPAT_32BIT_TIME
944 SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
945                 struct old_itimerspec32 __user *, new,
946                 struct old_itimerspec32 __user *, old)
947 {
948         struct itimerspec64 new_spec, old_spec;
949         struct itimerspec64 *rtn = old ? &old_spec : NULL;
950         int error = 0;
951
952         if (!new)
953                 return -EINVAL;
954         if (get_old_itimerspec32(&new_spec, new))
955                 return -EFAULT;
956
957         error = do_timer_settime(timer_id, flags, &new_spec, rtn);
958         if (!error && old) {
959                 if (put_old_itimerspec32(&old_spec, old))
960                         error = -EFAULT;
961         }
962         return error;
963 }
964 #endif
965
966 int common_timer_del(struct k_itimer *timer)
967 {
968         const struct k_clock *kc = timer->kclock;
969
970         timer->it_interval = 0;
971         if (kc->timer_try_to_cancel(timer) < 0)
972                 return TIMER_RETRY;
973         timer->it_active = 0;
974         return 0;
975 }
976
977 static inline int timer_delete_hook(struct k_itimer *timer)
978 {
979         const struct k_clock *kc = timer->kclock;
980
981         if (WARN_ON_ONCE(!kc || !kc->timer_del))
982                 return -EINVAL;
983         return kc->timer_del(timer);
984 }
985
986 /* Delete a POSIX.1b interval timer. */
987 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
988 {
989         struct k_itimer *timer;
990         unsigned long flags;
991
992         timer = lock_timer(timer_id, &flags);
993
994 retry_delete:
995         if (!timer)
996                 return -EINVAL;
997
998         if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
999                 /* Unlocks and relocks the timer if it still exists */
1000                 timer = timer_wait_running(timer, &flags);
1001                 goto retry_delete;
1002         }
1003
1004         spin_lock(&current->sighand->siglock);
1005         list_del(&timer->list);
1006         spin_unlock(&current->sighand->siglock);
1007         /*
1008          * This keeps any tasks waiting on the spin lock from thinking
1009          * they got something (see the lock code above).
1010          */
1011         timer->it_signal = NULL;
1012
1013         unlock_timer(timer, flags);
1014         release_posix_timer(timer, IT_ID_SET);
1015         return 0;
1016 }
1017
1018 /*
1019  * return timer owned by the process, used by exit_itimers
1020  */
1021 static void itimer_delete(struct k_itimer *timer)
1022 {
1023 retry_delete:
1024         spin_lock_irq(&timer->it_lock);
1025
1026         if (timer_delete_hook(timer) == TIMER_RETRY) {
1027                 spin_unlock_irq(&timer->it_lock);
1028                 goto retry_delete;
1029         }
1030         list_del(&timer->list);
1031
1032         spin_unlock_irq(&timer->it_lock);
1033         release_posix_timer(timer, IT_ID_SET);
1034 }
1035
1036 /*
1037  * This is called by do_exit or de_thread, only when there are no more
1038  * references to the shared signal_struct.
1039  */
1040 void exit_itimers(struct signal_struct *sig)
1041 {
1042         struct k_itimer *tmr;
1043
1044         while (!list_empty(&sig->posix_timers)) {
1045                 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1046                 itimer_delete(tmr);
1047         }
1048 }
1049
1050 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1051                 const struct __kernel_timespec __user *, tp)
1052 {
1053         const struct k_clock *kc = clockid_to_kclock(which_clock);
1054         struct timespec64 new_tp;
1055
1056         if (!kc || !kc->clock_set)
1057                 return -EINVAL;
1058
1059         if (get_timespec64(&new_tp, tp))
1060                 return -EFAULT;
1061
1062         return kc->clock_set(which_clock, &new_tp);
1063 }
1064
1065 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1066                 struct __kernel_timespec __user *, tp)
1067 {
1068         const struct k_clock *kc = clockid_to_kclock(which_clock);
1069         struct timespec64 kernel_tp;
1070         int error;
1071
1072         if (!kc)
1073                 return -EINVAL;
1074
1075         error = kc->clock_get(which_clock, &kernel_tp);
1076
1077         if (!error && put_timespec64(&kernel_tp, tp))
1078                 error = -EFAULT;
1079
1080         return error;
1081 }
1082
1083 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1084 {
1085         const struct k_clock *kc = clockid_to_kclock(which_clock);
1086
1087         if (!kc)
1088                 return -EINVAL;
1089         if (!kc->clock_adj)
1090                 return -EOPNOTSUPP;
1091
1092         return kc->clock_adj(which_clock, ktx);
1093 }
1094
1095 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1096                 struct __kernel_timex __user *, utx)
1097 {
1098         struct __kernel_timex ktx;
1099         int err;
1100
1101         if (copy_from_user(&ktx, utx, sizeof(ktx)))
1102                 return -EFAULT;
1103
1104         err = do_clock_adjtime(which_clock, &ktx);
1105
1106         if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1107                 return -EFAULT;
1108
1109         return err;
1110 }
1111
1112 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1113                 struct __kernel_timespec __user *, tp)
1114 {
1115         const struct k_clock *kc = clockid_to_kclock(which_clock);
1116         struct timespec64 rtn_tp;
1117         int error;
1118
1119         if (!kc)
1120                 return -EINVAL;
1121
1122         error = kc->clock_getres(which_clock, &rtn_tp);
1123
1124         if (!error && tp && put_timespec64(&rtn_tp, tp))
1125                 error = -EFAULT;
1126
1127         return error;
1128 }
1129
1130 #ifdef CONFIG_COMPAT_32BIT_TIME
1131
1132 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1133                 struct old_timespec32 __user *, tp)
1134 {
1135         const struct k_clock *kc = clockid_to_kclock(which_clock);
1136         struct timespec64 ts;
1137
1138         if (!kc || !kc->clock_set)
1139                 return -EINVAL;
1140
1141         if (get_old_timespec32(&ts, tp))
1142                 return -EFAULT;
1143
1144         return kc->clock_set(which_clock, &ts);
1145 }
1146
1147 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1148                 struct old_timespec32 __user *, tp)
1149 {
1150         const struct k_clock *kc = clockid_to_kclock(which_clock);
1151         struct timespec64 ts;
1152         int err;
1153
1154         if (!kc)
1155                 return -EINVAL;
1156
1157         err = kc->clock_get(which_clock, &ts);
1158
1159         if (!err && put_old_timespec32(&ts, tp))
1160                 err = -EFAULT;
1161
1162         return err;
1163 }
1164
1165 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1166                 struct old_timex32 __user *, utp)
1167 {
1168         struct __kernel_timex ktx;
1169         int err;
1170
1171         err = get_old_timex32(&ktx, utp);
1172         if (err)
1173                 return err;
1174
1175         err = do_clock_adjtime(which_clock, &ktx);
1176
1177         if (err >= 0 && put_old_timex32(utp, &ktx))
1178                 return -EFAULT;
1179
1180         return err;
1181 }
1182
1183 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1184                 struct old_timespec32 __user *, tp)
1185 {
1186         const struct k_clock *kc = clockid_to_kclock(which_clock);
1187         struct timespec64 ts;
1188         int err;
1189
1190         if (!kc)
1191                 return -EINVAL;
1192
1193         err = kc->clock_getres(which_clock, &ts);
1194         if (!err && tp && put_old_timespec32(&ts, tp))
1195                 return -EFAULT;
1196
1197         return err;
1198 }
1199
1200 #endif
1201
1202 /*
1203  * nanosleep for monotonic and realtime clocks
1204  */
1205 static int common_nsleep(const clockid_t which_clock, int flags,
1206                          const struct timespec64 *rqtp)
1207 {
1208         return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1209                                  HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1210                                  which_clock);
1211 }
1212
1213 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1214                 const struct __kernel_timespec __user *, rqtp,
1215                 struct __kernel_timespec __user *, rmtp)
1216 {
1217         const struct k_clock *kc = clockid_to_kclock(which_clock);
1218         struct timespec64 t;
1219
1220         if (!kc)
1221                 return -EINVAL;
1222         if (!kc->nsleep)
1223                 return -EOPNOTSUPP;
1224
1225         if (get_timespec64(&t, rqtp))
1226                 return -EFAULT;
1227
1228         if (!timespec64_valid(&t))
1229                 return -EINVAL;
1230         if (flags & TIMER_ABSTIME)
1231                 rmtp = NULL;
1232         current->restart_block.fn = do_no_restart_syscall;
1233         current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1234         current->restart_block.nanosleep.rmtp = rmtp;
1235
1236         return kc->nsleep(which_clock, flags, &t);
1237 }
1238
1239 #ifdef CONFIG_COMPAT_32BIT_TIME
1240
1241 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1242                 struct old_timespec32 __user *, rqtp,
1243                 struct old_timespec32 __user *, rmtp)
1244 {
1245         const struct k_clock *kc = clockid_to_kclock(which_clock);
1246         struct timespec64 t;
1247
1248         if (!kc)
1249                 return -EINVAL;
1250         if (!kc->nsleep)
1251                 return -EOPNOTSUPP;
1252
1253         if (get_old_timespec32(&t, rqtp))
1254                 return -EFAULT;
1255
1256         if (!timespec64_valid(&t))
1257                 return -EINVAL;
1258         if (flags & TIMER_ABSTIME)
1259                 rmtp = NULL;
1260         current->restart_block.fn = do_no_restart_syscall;
1261         current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1262         current->restart_block.nanosleep.compat_rmtp = rmtp;
1263
1264         return kc->nsleep(which_clock, flags, &t);
1265 }
1266
1267 #endif
1268
1269 static const struct k_clock clock_realtime = {
1270         .clock_getres           = posix_get_hrtimer_res,
1271         .clock_get              = posix_clock_realtime_get,
1272         .clock_set              = posix_clock_realtime_set,
1273         .clock_adj              = posix_clock_realtime_adj,
1274         .nsleep                 = common_nsleep,
1275         .timer_create           = common_timer_create,
1276         .timer_set              = common_timer_set,
1277         .timer_get              = common_timer_get,
1278         .timer_del              = common_timer_del,
1279         .timer_rearm            = common_hrtimer_rearm,
1280         .timer_forward          = common_hrtimer_forward,
1281         .timer_remaining        = common_hrtimer_remaining,
1282         .timer_try_to_cancel    = common_hrtimer_try_to_cancel,
1283         .timer_wait_running     = common_timer_wait_running,
1284         .timer_arm              = common_hrtimer_arm,
1285 };
1286
1287 static const struct k_clock clock_monotonic = {
1288         .clock_getres           = posix_get_hrtimer_res,
1289         .clock_get              = posix_ktime_get_ts,
1290         .nsleep                 = common_nsleep,
1291         .timer_create           = common_timer_create,
1292         .timer_set              = common_timer_set,
1293         .timer_get              = common_timer_get,
1294         .timer_del              = common_timer_del,
1295         .timer_rearm            = common_hrtimer_rearm,
1296         .timer_forward          = common_hrtimer_forward,
1297         .timer_remaining        = common_hrtimer_remaining,
1298         .timer_try_to_cancel    = common_hrtimer_try_to_cancel,
1299         .timer_wait_running     = common_timer_wait_running,
1300         .timer_arm              = common_hrtimer_arm,
1301 };
1302
1303 static const struct k_clock clock_monotonic_raw = {
1304         .clock_getres           = posix_get_hrtimer_res,
1305         .clock_get              = posix_get_monotonic_raw,
1306 };
1307
1308 static const struct k_clock clock_realtime_coarse = {
1309         .clock_getres           = posix_get_coarse_res,
1310         .clock_get              = posix_get_realtime_coarse,
1311 };
1312
1313 static const struct k_clock clock_monotonic_coarse = {
1314         .clock_getres           = posix_get_coarse_res,
1315         .clock_get              = posix_get_monotonic_coarse,
1316 };
1317
1318 static const struct k_clock clock_tai = {
1319         .clock_getres           = posix_get_hrtimer_res,
1320         .clock_get              = posix_get_tai,
1321         .nsleep                 = common_nsleep,
1322         .timer_create           = common_timer_create,
1323         .timer_set              = common_timer_set,
1324         .timer_get              = common_timer_get,
1325         .timer_del              = common_timer_del,
1326         .timer_rearm            = common_hrtimer_rearm,
1327         .timer_forward          = common_hrtimer_forward,
1328         .timer_remaining        = common_hrtimer_remaining,
1329         .timer_try_to_cancel    = common_hrtimer_try_to_cancel,
1330         .timer_wait_running     = common_timer_wait_running,
1331         .timer_arm              = common_hrtimer_arm,
1332 };
1333
1334 static const struct k_clock clock_boottime = {
1335         .clock_getres           = posix_get_hrtimer_res,
1336         .clock_get              = posix_get_boottime,
1337         .nsleep                 = common_nsleep,
1338         .timer_create           = common_timer_create,
1339         .timer_set              = common_timer_set,
1340         .timer_get              = common_timer_get,
1341         .timer_del              = common_timer_del,
1342         .timer_rearm            = common_hrtimer_rearm,
1343         .timer_forward          = common_hrtimer_forward,
1344         .timer_remaining        = common_hrtimer_remaining,
1345         .timer_try_to_cancel    = common_hrtimer_try_to_cancel,
1346         .timer_wait_running     = common_timer_wait_running,
1347         .timer_arm              = common_hrtimer_arm,
1348 };
1349
1350 static const struct k_clock * const posix_clocks[] = {
1351         [CLOCK_REALTIME]                = &clock_realtime,
1352         [CLOCK_MONOTONIC]               = &clock_monotonic,
1353         [CLOCK_PROCESS_CPUTIME_ID]      = &clock_process,
1354         [CLOCK_THREAD_CPUTIME_ID]       = &clock_thread,
1355         [CLOCK_MONOTONIC_RAW]           = &clock_monotonic_raw,
1356         [CLOCK_REALTIME_COARSE]         = &clock_realtime_coarse,
1357         [CLOCK_MONOTONIC_COARSE]        = &clock_monotonic_coarse,
1358         [CLOCK_BOOTTIME]                = &clock_boottime,
1359         [CLOCK_REALTIME_ALARM]          = &alarm_clock,
1360         [CLOCK_BOOTTIME_ALARM]          = &alarm_clock,
1361         [CLOCK_TAI]                     = &clock_tai,
1362 };
1363
1364 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1365 {
1366         clockid_t idx = id;
1367
1368         if (id < 0) {
1369                 return (id & CLOCKFD_MASK) == CLOCKFD ?
1370                         &clock_posix_dynamic : &clock_posix_cpu;
1371         }
1372
1373         if (id >= ARRAY_SIZE(posix_clocks))
1374                 return NULL;
1375
1376         return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1377 }