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