1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/mm.h>
34#include <linux/interrupt.h>
35#include <linux/slab.h>
36#include <linux/time.h>
37#include <linux/mutex.h>
38#include <linux/sched/task.h>
39
40#include <linux/uaccess.h>
41#include <linux/list.h>
42#include <linux/init.h>
43#include <linux/compiler.h>
44#include <linux/hash.h>
45#include <linux/posix-clock.h>
46#include <linux/posix-timers.h>
47#include <linux/syscalls.h>
48#include <linux/wait.h>
49#include <linux/workqueue.h>
50#include <linux/export.h>
51#include <linux/hashtable.h>
52#include <linux/compat.h>
53
54#include "timekeeping.h"
55#include "posix-timers.h"
56
57
58
59
60
61
62
63
64
65
66
67
68
69static struct kmem_cache *posix_timers_cache;
70
71static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
72static DEFINE_SPINLOCK(hash_lock);
73
74static const struct k_clock * const posix_clocks[];
75static const struct k_clock *clockid_to_kclock(const clockid_t id);
76static const struct k_clock clock_realtime, clock_monotonic;
77
78
79
80
81
82#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
83 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
84#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
85#endif
86
87
88
89
90#ifndef ENOTSUP
91# define ENANOSLEEP_NOTSUP EOPNOTSUPP
92#else
93# define ENANOSLEEP_NOTSUP ENOTSUP
94#endif
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
134
135#define lock_timer(tid, flags) \
136({ struct k_itimer *__timr; \
137 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
138 __timr; \
139})
140
141static int hash(struct signal_struct *sig, unsigned int nr)
142{
143 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
144}
145
146static struct k_itimer *__posix_timers_find(struct hlist_head *head,
147 struct signal_struct *sig,
148 timer_t id)
149{
150 struct k_itimer *timer;
151
152 hlist_for_each_entry_rcu(timer, head, t_hash) {
153 if ((timer->it_signal == sig) && (timer->it_id == id))
154 return timer;
155 }
156 return NULL;
157}
158
159static struct k_itimer *posix_timer_by_id(timer_t id)
160{
161 struct signal_struct *sig = current->signal;
162 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
163
164 return __posix_timers_find(head, sig, id);
165}
166
167static int posix_timer_add(struct k_itimer *timer)
168{
169 struct signal_struct *sig = current->signal;
170 int first_free_id = sig->posix_timer_id;
171 struct hlist_head *head;
172 int ret = -ENOENT;
173
174 do {
175 spin_lock(&hash_lock);
176 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
177 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
178 hlist_add_head_rcu(&timer->t_hash, head);
179 ret = sig->posix_timer_id;
180 }
181 if (++sig->posix_timer_id < 0)
182 sig->posix_timer_id = 0;
183 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
184
185 ret = -EAGAIN;
186 spin_unlock(&hash_lock);
187 } while (ret == -ENOENT);
188 return ret;
189}
190
191static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
192{
193 spin_unlock_irqrestore(&timr->it_lock, flags);
194}
195
196
197static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
198{
199 ktime_get_real_ts64(tp);
200 return 0;
201}
202
203
204static int posix_clock_realtime_set(const clockid_t which_clock,
205 const struct timespec64 *tp)
206{
207 return do_sys_settimeofday64(tp, NULL);
208}
209
210static int posix_clock_realtime_adj(const clockid_t which_clock,
211 struct timex *t)
212{
213 return do_adjtimex(t);
214}
215
216
217
218
219static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
220{
221 ktime_get_ts64(tp);
222 return 0;
223}
224
225
226
227
228static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
229{
230 getrawmonotonic64(tp);
231 return 0;
232}
233
234
235static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
236{
237 *tp = current_kernel_time64();
238 return 0;
239}
240
241static int posix_get_monotonic_coarse(clockid_t which_clock,
242 struct timespec64 *tp)
243{
244 *tp = get_monotonic_coarse64();
245 return 0;
246}
247
248static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
249{
250 *tp = ktime_to_timespec64(KTIME_LOW_RES);
251 return 0;
252}
253
254static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
255{
256 get_monotonic_boottime64(tp);
257 return 0;
258}
259
260static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
261{
262 timekeeping_clocktai64(tp);
263 return 0;
264}
265
266static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
267{
268 tp->tv_sec = 0;
269 tp->tv_nsec = hrtimer_resolution;
270 return 0;
271}
272
273
274
275
276static __init int init_posix_timers(void)
277{
278 posix_timers_cache = kmem_cache_create("posix_timers_cache",
279 sizeof (struct k_itimer), 0, SLAB_PANIC,
280 NULL);
281 return 0;
282}
283__initcall(init_posix_timers);
284
285static void common_hrtimer_rearm(struct k_itimer *timr)
286{
287 struct hrtimer *timer = &timr->it.real.timer;
288
289 if (!timr->it_interval)
290 return;
291
292 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
293 timer->base->get_time(),
294 timr->it_interval);
295 hrtimer_restart(timer);
296}
297
298
299
300
301
302
303
304
305
306
307
308
309void posixtimer_rearm(struct siginfo *info)
310{
311 struct k_itimer *timr;
312 unsigned long flags;
313
314 timr = lock_timer(info->si_tid, &flags);
315 if (!timr)
316 return;
317
318 if (timr->it_requeue_pending == info->si_sys_private) {
319 timr->kclock->timer_rearm(timr);
320
321 timr->it_active = 1;
322 timr->it_overrun_last = timr->it_overrun;
323 timr->it_overrun = -1;
324 ++timr->it_requeue_pending;
325
326 info->si_overrun += timr->it_overrun_last;
327 }
328
329 unlock_timer(timr, flags);
330}
331
332int posix_timer_event(struct k_itimer *timr, int si_private)
333{
334 struct task_struct *task;
335 int shared, ret = -1;
336
337
338
339
340
341
342
343
344
345
346
347 timr->sigq->info.si_sys_private = si_private;
348
349 rcu_read_lock();
350 task = pid_task(timr->it_pid, PIDTYPE_PID);
351 if (task) {
352 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
353 ret = send_sigqueue(timr->sigq, task, shared);
354 }
355 rcu_read_unlock();
356
357 return ret > 0;
358}
359
360
361
362
363
364
365
366
367static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
368{
369 struct k_itimer *timr;
370 unsigned long flags;
371 int si_private = 0;
372 enum hrtimer_restart ret = HRTIMER_NORESTART;
373
374 timr = container_of(timer, struct k_itimer, it.real.timer);
375 spin_lock_irqsave(&timr->it_lock, flags);
376
377 timr->it_active = 0;
378 if (timr->it_interval != 0)
379 si_private = ++timr->it_requeue_pending;
380
381 if (posix_timer_event(timr, si_private)) {
382
383
384
385
386
387 if (timr->it_interval != 0) {
388 ktime_t now = hrtimer_cb_get_time(timer);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412#ifdef CONFIG_HIGH_RES_TIMERS
413 {
414 ktime_t kj = NSEC_PER_SEC / HZ;
415
416 if (timr->it_interval < kj)
417 now = ktime_add(now, kj);
418 }
419#endif
420 timr->it_overrun += (unsigned int)
421 hrtimer_forward(timer, now,
422 timr->it_interval);
423 ret = HRTIMER_RESTART;
424 ++timr->it_requeue_pending;
425 timr->it_active = 1;
426 }
427 }
428
429 unlock_timer(timr, flags);
430 return ret;
431}
432
433static struct pid *good_sigevent(sigevent_t * event)
434{
435 struct task_struct *rtn = current->group_leader;
436
437 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
438 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
439 !same_thread_group(rtn, current) ||
440 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
441 return NULL;
442
443 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
444 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
445 return NULL;
446
447 return task_pid(rtn);
448}
449
450static struct k_itimer * alloc_posix_timer(void)
451{
452 struct k_itimer *tmr;
453 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
454 if (!tmr)
455 return tmr;
456 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
457 kmem_cache_free(posix_timers_cache, tmr);
458 return NULL;
459 }
460 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
461 return tmr;
462}
463
464static void k_itimer_rcu_free(struct rcu_head *head)
465{
466 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
467
468 kmem_cache_free(posix_timers_cache, tmr);
469}
470
471#define IT_ID_SET 1
472#define IT_ID_NOT_SET 0
473static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
474{
475 if (it_id_set) {
476 unsigned long flags;
477 spin_lock_irqsave(&hash_lock, flags);
478 hlist_del_rcu(&tmr->t_hash);
479 spin_unlock_irqrestore(&hash_lock, flags);
480 }
481 put_pid(tmr->it_pid);
482 sigqueue_free(tmr->sigq);
483 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
484}
485
486static int common_timer_create(struct k_itimer *new_timer)
487{
488 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
489 return 0;
490}
491
492
493static int do_timer_create(clockid_t which_clock, struct sigevent *event,
494 timer_t __user *created_timer_id)
495{
496 const struct k_clock *kc = clockid_to_kclock(which_clock);
497 struct k_itimer *new_timer;
498 int error, new_timer_id;
499 int it_id_set = IT_ID_NOT_SET;
500
501 if (!kc)
502 return -EINVAL;
503 if (!kc->timer_create)
504 return -EOPNOTSUPP;
505
506 new_timer = alloc_posix_timer();
507 if (unlikely(!new_timer))
508 return -EAGAIN;
509
510 spin_lock_init(&new_timer->it_lock);
511 new_timer_id = posix_timer_add(new_timer);
512 if (new_timer_id < 0) {
513 error = new_timer_id;
514 goto out;
515 }
516
517 it_id_set = IT_ID_SET;
518 new_timer->it_id = (timer_t) new_timer_id;
519 new_timer->it_clock = which_clock;
520 new_timer->kclock = kc;
521 new_timer->it_overrun = -1;
522
523 if (event) {
524 rcu_read_lock();
525 new_timer->it_pid = get_pid(good_sigevent(event));
526 rcu_read_unlock();
527 if (!new_timer->it_pid) {
528 error = -EINVAL;
529 goto out;
530 }
531 new_timer->it_sigev_notify = event->sigev_notify;
532 new_timer->sigq->info.si_signo = event->sigev_signo;
533 new_timer->sigq->info.si_value = event->sigev_value;
534 } else {
535 new_timer->it_sigev_notify = SIGEV_SIGNAL;
536 new_timer->sigq->info.si_signo = SIGALRM;
537 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
538 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
539 new_timer->it_pid = get_pid(task_tgid(current));
540 }
541
542 new_timer->sigq->info.si_tid = new_timer->it_id;
543 new_timer->sigq->info.si_code = SI_TIMER;
544
545 if (copy_to_user(created_timer_id,
546 &new_timer_id, sizeof (new_timer_id))) {
547 error = -EFAULT;
548 goto out;
549 }
550
551 error = kc->timer_create(new_timer);
552 if (error)
553 goto out;
554
555 spin_lock_irq(¤t->sighand->siglock);
556 new_timer->it_signal = current->signal;
557 list_add(&new_timer->list, ¤t->signal->posix_timers);
558 spin_unlock_irq(¤t->sighand->siglock);
559
560 return 0;
561
562
563
564
565
566
567out:
568 release_posix_timer(new_timer, it_id_set);
569 return error;
570}
571
572SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
573 struct sigevent __user *, timer_event_spec,
574 timer_t __user *, created_timer_id)
575{
576 if (timer_event_spec) {
577 sigevent_t event;
578
579 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
580 return -EFAULT;
581 return do_timer_create(which_clock, &event, created_timer_id);
582 }
583 return do_timer_create(which_clock, NULL, created_timer_id);
584}
585
586#ifdef CONFIG_COMPAT
587COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
588 struct compat_sigevent __user *, timer_event_spec,
589 timer_t __user *, created_timer_id)
590{
591 if (timer_event_spec) {
592 sigevent_t event;
593
594 if (get_compat_sigevent(&event, timer_event_spec))
595 return -EFAULT;
596 return do_timer_create(which_clock, &event, created_timer_id);
597 }
598 return do_timer_create(which_clock, NULL, created_timer_id);
599}
600#endif
601
602
603
604
605
606
607
608
609static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
610{
611 struct k_itimer *timr;
612
613
614
615
616
617 if ((unsigned long long)timer_id > INT_MAX)
618 return NULL;
619
620 rcu_read_lock();
621 timr = posix_timer_by_id(timer_id);
622 if (timr) {
623 spin_lock_irqsave(&timr->it_lock, *flags);
624 if (timr->it_signal == current->signal) {
625 rcu_read_unlock();
626 return timr;
627 }
628 spin_unlock_irqrestore(&timr->it_lock, *flags);
629 }
630 rcu_read_unlock();
631
632 return NULL;
633}
634
635static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
636{
637 struct hrtimer *timer = &timr->it.real.timer;
638
639 return __hrtimer_expires_remaining_adjusted(timer, now);
640}
641
642static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
643{
644 struct hrtimer *timer = &timr->it.real.timer;
645
646 return (int)hrtimer_forward(timer, now, timr->it_interval);
647}
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
666{
667 const struct k_clock *kc = timr->kclock;
668 ktime_t now, remaining, iv;
669 struct timespec64 ts64;
670 bool sig_none;
671
672 sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
673 iv = timr->it_interval;
674
675
676 if (iv) {
677 cur_setting->it_interval = ktime_to_timespec64(iv);
678 } else if (!timr->it_active) {
679
680
681
682
683 if (!sig_none)
684 return;
685 }
686
687
688
689
690
691 kc->clock_get(timr->it_clock, &ts64);
692 now = timespec64_to_ktime(ts64);
693
694
695
696
697
698 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
699 timr->it_overrun += kc->timer_forward(timr, now);
700
701 remaining = kc->timer_remaining(timr, now);
702
703 if (remaining <= 0) {
704
705
706
707
708 if (!sig_none)
709 cur_setting->it_value.tv_nsec = 1;
710 } else {
711 cur_setting->it_value = ktime_to_timespec64(remaining);
712 }
713}
714
715
716static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
717{
718 struct k_itimer *timr;
719 const struct k_clock *kc;
720 unsigned long flags;
721 int ret = 0;
722
723 timr = lock_timer(timer_id, &flags);
724 if (!timr)
725 return -EINVAL;
726
727 memset(setting, 0, sizeof(*setting));
728 kc = timr->kclock;
729 if (WARN_ON_ONCE(!kc || !kc->timer_get))
730 ret = -EINVAL;
731 else
732 kc->timer_get(timr, setting);
733
734 unlock_timer(timr, flags);
735 return ret;
736}
737
738
739SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
740 struct itimerspec __user *, setting)
741{
742 struct itimerspec64 cur_setting;
743
744 int ret = do_timer_gettime(timer_id, &cur_setting);
745 if (!ret) {
746 if (put_itimerspec64(&cur_setting, setting))
747 ret = -EFAULT;
748 }
749 return ret;
750}
751
752#ifdef CONFIG_COMPAT
753COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
754 struct compat_itimerspec __user *, setting)
755{
756 struct itimerspec64 cur_setting;
757
758 int ret = do_timer_gettime(timer_id, &cur_setting);
759 if (!ret) {
760 if (put_compat_itimerspec64(&cur_setting, setting))
761 ret = -EFAULT;
762 }
763 return ret;
764}
765#endif
766
767
768
769
770
771
772
773
774
775
776SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
777{
778 struct k_itimer *timr;
779 int overrun;
780 unsigned long flags;
781
782 timr = lock_timer(timer_id, &flags);
783 if (!timr)
784 return -EINVAL;
785
786 overrun = timr->it_overrun_last;
787 unlock_timer(timr, flags);
788
789 return overrun;
790}
791
792static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
793 bool absolute, bool sigev_none)
794{
795 struct hrtimer *timer = &timr->it.real.timer;
796 enum hrtimer_mode mode;
797
798 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
799
800
801
802
803
804
805
806
807
808 if (timr->it_clock == CLOCK_REALTIME)
809 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
810
811 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
812 timr->it.real.timer.function = posix_timer_fn;
813
814 if (!absolute)
815 expires = ktime_add_safe(expires, timer->base->get_time());
816 hrtimer_set_expires(timer, expires);
817
818 if (!sigev_none)
819 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
820}
821
822static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
823{
824 return hrtimer_try_to_cancel(&timr->it.real.timer);
825}
826
827
828int common_timer_set(struct k_itimer *timr, int flags,
829 struct itimerspec64 *new_setting,
830 struct itimerspec64 *old_setting)
831{
832 const struct k_clock *kc = timr->kclock;
833 bool sigev_none;
834 ktime_t expires;
835
836 if (old_setting)
837 common_timer_get(timr, old_setting);
838
839
840 timr->it_interval = 0;
841
842
843
844
845 if (kc->timer_try_to_cancel(timr) < 0)
846 return TIMER_RETRY;
847
848 timr->it_active = 0;
849 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
850 ~REQUEUE_PENDING;
851 timr->it_overrun_last = 0;
852
853
854 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
855 return 0;
856
857 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
858 expires = timespec64_to_ktime(new_setting->it_value);
859 sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
860
861 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
862 timr->it_active = !sigev_none;
863 return 0;
864}
865
866static int do_timer_settime(timer_t timer_id, int flags,
867 struct itimerspec64 *new_spec64,
868 struct itimerspec64 *old_spec64)
869{
870 const struct k_clock *kc;
871 struct k_itimer *timr;
872 unsigned long flag;
873 int error = 0;
874
875 if (!timespec64_valid(&new_spec64->it_interval) ||
876 !timespec64_valid(&new_spec64->it_value))
877 return -EINVAL;
878
879 if (old_spec64)
880 memset(old_spec64, 0, sizeof(*old_spec64));
881retry:
882 timr = lock_timer(timer_id, &flag);
883 if (!timr)
884 return -EINVAL;
885
886 kc = timr->kclock;
887 if (WARN_ON_ONCE(!kc || !kc->timer_set))
888 error = -EINVAL;
889 else
890 error = kc->timer_set(timr, flags, new_spec64, old_spec64);
891
892 unlock_timer(timr, flag);
893 if (error == TIMER_RETRY) {
894 old_spec64 = NULL;
895 goto retry;
896 }
897
898 return error;
899}
900
901
902SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
903 const struct itimerspec __user *, new_setting,
904 struct itimerspec __user *, old_setting)
905{
906 struct itimerspec64 new_spec, old_spec;
907 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
908 int error = 0;
909
910 if (!new_setting)
911 return -EINVAL;
912
913 if (get_itimerspec64(&new_spec, new_setting))
914 return -EFAULT;
915
916 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
917 if (!error && old_setting) {
918 if (put_itimerspec64(&old_spec, old_setting))
919 error = -EFAULT;
920 }
921 return error;
922}
923
924#ifdef CONFIG_COMPAT
925COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
926 struct compat_itimerspec __user *, new,
927 struct compat_itimerspec __user *, old)
928{
929 struct itimerspec64 new_spec, old_spec;
930 struct itimerspec64 *rtn = old ? &old_spec : NULL;
931 int error = 0;
932
933 if (!new)
934 return -EINVAL;
935 if (get_compat_itimerspec64(&new_spec, new))
936 return -EFAULT;
937
938 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
939 if (!error && old) {
940 if (put_compat_itimerspec64(&old_spec, old))
941 error = -EFAULT;
942 }
943 return error;
944}
945#endif
946
947int common_timer_del(struct k_itimer *timer)
948{
949 const struct k_clock *kc = timer->kclock;
950
951 timer->it_interval = 0;
952 if (kc->timer_try_to_cancel(timer) < 0)
953 return TIMER_RETRY;
954 timer->it_active = 0;
955 return 0;
956}
957
958static inline int timer_delete_hook(struct k_itimer *timer)
959{
960 const struct k_clock *kc = timer->kclock;
961
962 if (WARN_ON_ONCE(!kc || !kc->timer_del))
963 return -EINVAL;
964 return kc->timer_del(timer);
965}
966
967
968SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
969{
970 struct k_itimer *timer;
971 unsigned long flags;
972
973retry_delete:
974 timer = lock_timer(timer_id, &flags);
975 if (!timer)
976 return -EINVAL;
977
978 if (timer_delete_hook(timer) == TIMER_RETRY) {
979 unlock_timer(timer, flags);
980 goto retry_delete;
981 }
982
983 spin_lock(¤t->sighand->siglock);
984 list_del(&timer->list);
985 spin_unlock(¤t->sighand->siglock);
986
987
988
989
990 timer->it_signal = NULL;
991
992 unlock_timer(timer, flags);
993 release_posix_timer(timer, IT_ID_SET);
994 return 0;
995}
996
997
998
999
1000static void itimer_delete(struct k_itimer *timer)
1001{
1002 unsigned long flags;
1003
1004retry_delete:
1005 spin_lock_irqsave(&timer->it_lock, flags);
1006
1007 if (timer_delete_hook(timer) == TIMER_RETRY) {
1008 unlock_timer(timer, flags);
1009 goto retry_delete;
1010 }
1011 list_del(&timer->list);
1012
1013
1014
1015
1016 timer->it_signal = NULL;
1017
1018 unlock_timer(timer, flags);
1019 release_posix_timer(timer, IT_ID_SET);
1020}
1021
1022
1023
1024
1025
1026void exit_itimers(struct signal_struct *sig)
1027{
1028 struct k_itimer *tmr;
1029
1030 while (!list_empty(&sig->posix_timers)) {
1031 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1032 itimer_delete(tmr);
1033 }
1034}
1035
1036SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1037 const struct timespec __user *, tp)
1038{
1039 const struct k_clock *kc = clockid_to_kclock(which_clock);
1040 struct timespec64 new_tp;
1041
1042 if (!kc || !kc->clock_set)
1043 return -EINVAL;
1044
1045 if (get_timespec64(&new_tp, tp))
1046 return -EFAULT;
1047
1048 return kc->clock_set(which_clock, &new_tp);
1049}
1050
1051SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1052 struct timespec __user *,tp)
1053{
1054 const struct k_clock *kc = clockid_to_kclock(which_clock);
1055 struct timespec64 kernel_tp;
1056 int error;
1057
1058 if (!kc)
1059 return -EINVAL;
1060
1061 error = kc->clock_get(which_clock, &kernel_tp);
1062
1063 if (!error && put_timespec64(&kernel_tp, tp))
1064 error = -EFAULT;
1065
1066 return error;
1067}
1068
1069SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1070 struct timex __user *, utx)
1071{
1072 const struct k_clock *kc = clockid_to_kclock(which_clock);
1073 struct timex ktx;
1074 int err;
1075
1076 if (!kc)
1077 return -EINVAL;
1078 if (!kc->clock_adj)
1079 return -EOPNOTSUPP;
1080
1081 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1082 return -EFAULT;
1083
1084 err = kc->clock_adj(which_clock, &ktx);
1085
1086 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1087 return -EFAULT;
1088
1089 return err;
1090}
1091
1092SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1093 struct timespec __user *, tp)
1094{
1095 const struct k_clock *kc = clockid_to_kclock(which_clock);
1096 struct timespec64 rtn_tp;
1097 int error;
1098
1099 if (!kc)
1100 return -EINVAL;
1101
1102 error = kc->clock_getres(which_clock, &rtn_tp);
1103
1104 if (!error && tp && put_timespec64(&rtn_tp, tp))
1105 error = -EFAULT;
1106
1107 return error;
1108}
1109
1110#ifdef CONFIG_COMPAT
1111
1112COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1113 struct compat_timespec __user *, tp)
1114{
1115 const struct k_clock *kc = clockid_to_kclock(which_clock);
1116 struct timespec64 ts;
1117
1118 if (!kc || !kc->clock_set)
1119 return -EINVAL;
1120
1121 if (compat_get_timespec64(&ts, tp))
1122 return -EFAULT;
1123
1124 return kc->clock_set(which_clock, &ts);
1125}
1126
1127COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1128 struct compat_timespec __user *, tp)
1129{
1130 const struct k_clock *kc = clockid_to_kclock(which_clock);
1131 struct timespec64 ts;
1132 int err;
1133
1134 if (!kc)
1135 return -EINVAL;
1136
1137 err = kc->clock_get(which_clock, &ts);
1138
1139 if (!err && compat_put_timespec64(&ts, tp))
1140 err = -EFAULT;
1141
1142 return err;
1143}
1144
1145COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1146 struct compat_timex __user *, utp)
1147{
1148 const struct k_clock *kc = clockid_to_kclock(which_clock);
1149 struct timex ktx;
1150 int err;
1151
1152 if (!kc)
1153 return -EINVAL;
1154 if (!kc->clock_adj)
1155 return -EOPNOTSUPP;
1156
1157 err = compat_get_timex(&ktx, utp);
1158 if (err)
1159 return err;
1160
1161 err = kc->clock_adj(which_clock, &ktx);
1162
1163 if (err >= 0)
1164 err = compat_put_timex(utp, &ktx);
1165
1166 return err;
1167}
1168
1169COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1170 struct compat_timespec __user *, tp)
1171{
1172 const struct k_clock *kc = clockid_to_kclock(which_clock);
1173 struct timespec64 ts;
1174 int err;
1175
1176 if (!kc)
1177 return -EINVAL;
1178
1179 err = kc->clock_getres(which_clock, &ts);
1180 if (!err && tp && compat_put_timespec64(&ts, tp))
1181 return -EFAULT;
1182
1183 return err;
1184}
1185
1186#endif
1187
1188
1189
1190
1191static int common_nsleep(const clockid_t which_clock, int flags,
1192 const struct timespec64 *rqtp)
1193{
1194 return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1195 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1196 which_clock);
1197}
1198
1199SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1200 const struct timespec __user *, rqtp,
1201 struct timespec __user *, rmtp)
1202{
1203 const struct k_clock *kc = clockid_to_kclock(which_clock);
1204 struct timespec64 t;
1205
1206 if (!kc)
1207 return -EINVAL;
1208 if (!kc->nsleep)
1209 return -ENANOSLEEP_NOTSUP;
1210
1211 if (get_timespec64(&t, rqtp))
1212 return -EFAULT;
1213
1214 if (!timespec64_valid(&t))
1215 return -EINVAL;
1216 if (flags & TIMER_ABSTIME)
1217 rmtp = NULL;
1218 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1219 current->restart_block.nanosleep.rmtp = rmtp;
1220
1221 return kc->nsleep(which_clock, flags, &t);
1222}
1223
1224#ifdef CONFIG_COMPAT
1225COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1226 struct compat_timespec __user *, rqtp,
1227 struct compat_timespec __user *, rmtp)
1228{
1229 const struct k_clock *kc = clockid_to_kclock(which_clock);
1230 struct timespec64 t;
1231
1232 if (!kc)
1233 return -EINVAL;
1234 if (!kc->nsleep)
1235 return -ENANOSLEEP_NOTSUP;
1236
1237 if (compat_get_timespec64(&t, rqtp))
1238 return -EFAULT;
1239
1240 if (!timespec64_valid(&t))
1241 return -EINVAL;
1242 if (flags & TIMER_ABSTIME)
1243 rmtp = NULL;
1244 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1245 current->restart_block.nanosleep.compat_rmtp = rmtp;
1246
1247 return kc->nsleep(which_clock, flags, &t);
1248}
1249#endif
1250
1251static const struct k_clock clock_realtime = {
1252 .clock_getres = posix_get_hrtimer_res,
1253 .clock_get = posix_clock_realtime_get,
1254 .clock_set = posix_clock_realtime_set,
1255 .clock_adj = posix_clock_realtime_adj,
1256 .nsleep = common_nsleep,
1257 .timer_create = common_timer_create,
1258 .timer_set = common_timer_set,
1259 .timer_get = common_timer_get,
1260 .timer_del = common_timer_del,
1261 .timer_rearm = common_hrtimer_rearm,
1262 .timer_forward = common_hrtimer_forward,
1263 .timer_remaining = common_hrtimer_remaining,
1264 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1265 .timer_arm = common_hrtimer_arm,
1266};
1267
1268static const struct k_clock clock_monotonic = {
1269 .clock_getres = posix_get_hrtimer_res,
1270 .clock_get = posix_ktime_get_ts,
1271 .nsleep = common_nsleep,
1272 .timer_create = common_timer_create,
1273 .timer_set = common_timer_set,
1274 .timer_get = common_timer_get,
1275 .timer_del = common_timer_del,
1276 .timer_rearm = common_hrtimer_rearm,
1277 .timer_forward = common_hrtimer_forward,
1278 .timer_remaining = common_hrtimer_remaining,
1279 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1280 .timer_arm = common_hrtimer_arm,
1281};
1282
1283static const struct k_clock clock_monotonic_raw = {
1284 .clock_getres = posix_get_hrtimer_res,
1285 .clock_get = posix_get_monotonic_raw,
1286};
1287
1288static const struct k_clock clock_realtime_coarse = {
1289 .clock_getres = posix_get_coarse_res,
1290 .clock_get = posix_get_realtime_coarse,
1291};
1292
1293static const struct k_clock clock_monotonic_coarse = {
1294 .clock_getres = posix_get_coarse_res,
1295 .clock_get = posix_get_monotonic_coarse,
1296};
1297
1298static const struct k_clock clock_tai = {
1299 .clock_getres = posix_get_hrtimer_res,
1300 .clock_get = posix_get_tai,
1301 .nsleep = common_nsleep,
1302 .timer_create = common_timer_create,
1303 .timer_set = common_timer_set,
1304 .timer_get = common_timer_get,
1305 .timer_del = common_timer_del,
1306 .timer_rearm = common_hrtimer_rearm,
1307 .timer_forward = common_hrtimer_forward,
1308 .timer_remaining = common_hrtimer_remaining,
1309 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1310 .timer_arm = common_hrtimer_arm,
1311};
1312
1313static const struct k_clock clock_boottime = {
1314 .clock_getres = posix_get_hrtimer_res,
1315 .clock_get = posix_get_boottime,
1316 .nsleep = common_nsleep,
1317 .timer_create = common_timer_create,
1318 .timer_set = common_timer_set,
1319 .timer_get = common_timer_get,
1320 .timer_del = common_timer_del,
1321 .timer_rearm = common_hrtimer_rearm,
1322 .timer_forward = common_hrtimer_forward,
1323 .timer_remaining = common_hrtimer_remaining,
1324 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1325 .timer_arm = common_hrtimer_arm,
1326};
1327
1328static const struct k_clock * const posix_clocks[] = {
1329 [CLOCK_REALTIME] = &clock_realtime,
1330 [CLOCK_MONOTONIC] = &clock_monotonic,
1331 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1332 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1333 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1334 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1335 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1336 [CLOCK_BOOTTIME] = &clock_boottime,
1337 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1338 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1339 [CLOCK_TAI] = &clock_tai,
1340};
1341
1342static const struct k_clock *clockid_to_kclock(const clockid_t id)
1343{
1344 if (id < 0)
1345 return (id & CLOCKFD_MASK) == CLOCKFD ?
1346 &clock_posix_dynamic : &clock_posix_cpu;
1347
1348 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1349 return NULL;
1350 return posix_clocks[id];
1351}
1352