0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/mm.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/slab.h>
0015 #include <linux/time.h>
0016 #include <linux/mutex.h>
0017 #include <linux/sched/task.h>
0018
0019 #include <linux/uaccess.h>
0020 #include <linux/list.h>
0021 #include <linux/init.h>
0022 #include <linux/compiler.h>
0023 #include <linux/hash.h>
0024 #include <linux/posix-clock.h>
0025 #include <linux/posix-timers.h>
0026 #include <linux/syscalls.h>
0027 #include <linux/wait.h>
0028 #include <linux/workqueue.h>
0029 #include <linux/export.h>
0030 #include <linux/hashtable.h>
0031 #include <linux/compat.h>
0032 #include <linux/nospec.h>
0033 #include <linux/time_namespace.h>
0034
0035 #include "timekeeping.h"
0036 #include "posix-timers.h"
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 static struct kmem_cache *posix_timers_cache;
0051
0052 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
0053 static DEFINE_SPINLOCK(hash_lock);
0054
0055 static const struct k_clock * const posix_clocks[];
0056 static const struct k_clock *clockid_to_kclock(const clockid_t id);
0057 static const struct k_clock clock_realtime, clock_monotonic;
0058
0059
0060
0061
0062
0063 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
0064 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
0065 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
0066 #endif
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
0106
0107 #define lock_timer(tid, flags) \
0108 ({ struct k_itimer *__timr; \
0109 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
0110 __timr; \
0111 })
0112
0113 static int hash(struct signal_struct *sig, unsigned int nr)
0114 {
0115 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
0116 }
0117
0118 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
0119 struct signal_struct *sig,
0120 timer_t id)
0121 {
0122 struct k_itimer *timer;
0123
0124 hlist_for_each_entry_rcu(timer, head, t_hash,
0125 lockdep_is_held(&hash_lock)) {
0126 if ((timer->it_signal == sig) && (timer->it_id == id))
0127 return timer;
0128 }
0129 return NULL;
0130 }
0131
0132 static struct k_itimer *posix_timer_by_id(timer_t id)
0133 {
0134 struct signal_struct *sig = current->signal;
0135 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
0136
0137 return __posix_timers_find(head, sig, id);
0138 }
0139
0140 static int posix_timer_add(struct k_itimer *timer)
0141 {
0142 struct signal_struct *sig = current->signal;
0143 int first_free_id = sig->posix_timer_id;
0144 struct hlist_head *head;
0145 int ret = -ENOENT;
0146
0147 do {
0148 spin_lock(&hash_lock);
0149 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
0150 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
0151 hlist_add_head_rcu(&timer->t_hash, head);
0152 ret = sig->posix_timer_id;
0153 }
0154 if (++sig->posix_timer_id < 0)
0155 sig->posix_timer_id = 0;
0156 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
0157
0158 ret = -EAGAIN;
0159 spin_unlock(&hash_lock);
0160 } while (ret == -ENOENT);
0161 return ret;
0162 }
0163
0164 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
0165 {
0166 spin_unlock_irqrestore(&timr->it_lock, flags);
0167 }
0168
0169
0170 static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
0171 {
0172 ktime_get_real_ts64(tp);
0173 return 0;
0174 }
0175
0176 static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
0177 {
0178 return ktime_get_real();
0179 }
0180
0181
0182 static int posix_clock_realtime_set(const clockid_t which_clock,
0183 const struct timespec64 *tp)
0184 {
0185 return do_sys_settimeofday64(tp, NULL);
0186 }
0187
0188 static int posix_clock_realtime_adj(const clockid_t which_clock,
0189 struct __kernel_timex *t)
0190 {
0191 return do_adjtimex(t);
0192 }
0193
0194
0195
0196
0197 static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
0198 {
0199 ktime_get_ts64(tp);
0200 timens_add_monotonic(tp);
0201 return 0;
0202 }
0203
0204 static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
0205 {
0206 return ktime_get();
0207 }
0208
0209
0210
0211
0212 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
0213 {
0214 ktime_get_raw_ts64(tp);
0215 timens_add_monotonic(tp);
0216 return 0;
0217 }
0218
0219
0220 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
0221 {
0222 ktime_get_coarse_real_ts64(tp);
0223 return 0;
0224 }
0225
0226 static int posix_get_monotonic_coarse(clockid_t which_clock,
0227 struct timespec64 *tp)
0228 {
0229 ktime_get_coarse_ts64(tp);
0230 timens_add_monotonic(tp);
0231 return 0;
0232 }
0233
0234 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
0235 {
0236 *tp = ktime_to_timespec64(KTIME_LOW_RES);
0237 return 0;
0238 }
0239
0240 static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
0241 {
0242 ktime_get_boottime_ts64(tp);
0243 timens_add_boottime(tp);
0244 return 0;
0245 }
0246
0247 static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
0248 {
0249 return ktime_get_boottime();
0250 }
0251
0252 static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
0253 {
0254 ktime_get_clocktai_ts64(tp);
0255 return 0;
0256 }
0257
0258 static ktime_t posix_get_tai_ktime(clockid_t which_clock)
0259 {
0260 return ktime_get_clocktai();
0261 }
0262
0263 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
0264 {
0265 tp->tv_sec = 0;
0266 tp->tv_nsec = hrtimer_resolution;
0267 return 0;
0268 }
0269
0270
0271
0272
0273 static __init int init_posix_timers(void)
0274 {
0275 posix_timers_cache = kmem_cache_create("posix_timers_cache",
0276 sizeof(struct k_itimer), 0,
0277 SLAB_PANIC | SLAB_ACCOUNT, NULL);
0278 return 0;
0279 }
0280 __initcall(init_posix_timers);
0281
0282
0283
0284
0285
0286 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
0287 {
0288 s64 sum = timr->it_overrun_last + (s64)baseval;
0289
0290 return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
0291 }
0292
0293 static void common_hrtimer_rearm(struct k_itimer *timr)
0294 {
0295 struct hrtimer *timer = &timr->it.real.timer;
0296
0297 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
0298 timr->it_interval);
0299 hrtimer_restart(timer);
0300 }
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 void posixtimer_rearm(struct kernel_siginfo *info)
0314 {
0315 struct k_itimer *timr;
0316 unsigned long flags;
0317
0318 timr = lock_timer(info->si_tid, &flags);
0319 if (!timr)
0320 return;
0321
0322 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
0323 timr->kclock->timer_rearm(timr);
0324
0325 timr->it_active = 1;
0326 timr->it_overrun_last = timr->it_overrun;
0327 timr->it_overrun = -1LL;
0328 ++timr->it_requeue_pending;
0329
0330 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
0331 }
0332
0333 unlock_timer(timr, flags);
0334 }
0335
0336 int posix_timer_event(struct k_itimer *timr, int si_private)
0337 {
0338 enum pid_type type;
0339 int ret;
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351 timr->sigq->info.si_sys_private = si_private;
0352
0353 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
0354 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
0355
0356 return ret > 0;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
0367 {
0368 struct k_itimer *timr;
0369 unsigned long flags;
0370 int si_private = 0;
0371 enum hrtimer_restart ret = HRTIMER_NORESTART;
0372
0373 timr = container_of(timer, struct k_itimer, it.real.timer);
0374 spin_lock_irqsave(&timr->it_lock, flags);
0375
0376 timr->it_active = 0;
0377 if (timr->it_interval != 0)
0378 si_private = ++timr->it_requeue_pending;
0379
0380 if (posix_timer_event(timr, si_private)) {
0381
0382
0383
0384
0385
0386 if (timr->it_interval != 0) {
0387 ktime_t now = hrtimer_cb_get_time(timer);
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 #ifdef CONFIG_HIGH_RES_TIMERS
0412 {
0413 ktime_t kj = NSEC_PER_SEC / HZ;
0414
0415 if (timr->it_interval < kj)
0416 now = ktime_add(now, kj);
0417 }
0418 #endif
0419 timr->it_overrun += hrtimer_forward(timer, now,
0420 timr->it_interval);
0421 ret = HRTIMER_RESTART;
0422 ++timr->it_requeue_pending;
0423 timr->it_active = 1;
0424 }
0425 }
0426
0427 unlock_timer(timr, flags);
0428 return ret;
0429 }
0430
0431 static struct pid *good_sigevent(sigevent_t * event)
0432 {
0433 struct pid *pid = task_tgid(current);
0434 struct task_struct *rtn;
0435
0436 switch (event->sigev_notify) {
0437 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
0438 pid = find_vpid(event->sigev_notify_thread_id);
0439 rtn = pid_task(pid, PIDTYPE_PID);
0440 if (!rtn || !same_thread_group(rtn, current))
0441 return NULL;
0442 fallthrough;
0443 case SIGEV_SIGNAL:
0444 case SIGEV_THREAD:
0445 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
0446 return NULL;
0447 fallthrough;
0448 case SIGEV_NONE:
0449 return pid;
0450 default:
0451 return NULL;
0452 }
0453 }
0454
0455 static struct k_itimer * alloc_posix_timer(void)
0456 {
0457 struct k_itimer *tmr;
0458 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
0459 if (!tmr)
0460 return tmr;
0461 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
0462 kmem_cache_free(posix_timers_cache, tmr);
0463 return NULL;
0464 }
0465 clear_siginfo(&tmr->sigq->info);
0466 return tmr;
0467 }
0468
0469 static void k_itimer_rcu_free(struct rcu_head *head)
0470 {
0471 struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
0472
0473 kmem_cache_free(posix_timers_cache, tmr);
0474 }
0475
0476 #define IT_ID_SET 1
0477 #define IT_ID_NOT_SET 0
0478 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
0479 {
0480 if (it_id_set) {
0481 unsigned long flags;
0482 spin_lock_irqsave(&hash_lock, flags);
0483 hlist_del_rcu(&tmr->t_hash);
0484 spin_unlock_irqrestore(&hash_lock, flags);
0485 }
0486 put_pid(tmr->it_pid);
0487 sigqueue_free(tmr->sigq);
0488 call_rcu(&tmr->rcu, k_itimer_rcu_free);
0489 }
0490
0491 static int common_timer_create(struct k_itimer *new_timer)
0492 {
0493 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
0494 return 0;
0495 }
0496
0497
0498 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
0499 timer_t __user *created_timer_id)
0500 {
0501 const struct k_clock *kc = clockid_to_kclock(which_clock);
0502 struct k_itimer *new_timer;
0503 int error, new_timer_id;
0504 int it_id_set = IT_ID_NOT_SET;
0505
0506 if (!kc)
0507 return -EINVAL;
0508 if (!kc->timer_create)
0509 return -EOPNOTSUPP;
0510
0511 new_timer = alloc_posix_timer();
0512 if (unlikely(!new_timer))
0513 return -EAGAIN;
0514
0515 spin_lock_init(&new_timer->it_lock);
0516 new_timer_id = posix_timer_add(new_timer);
0517 if (new_timer_id < 0) {
0518 error = new_timer_id;
0519 goto out;
0520 }
0521
0522 it_id_set = IT_ID_SET;
0523 new_timer->it_id = (timer_t) new_timer_id;
0524 new_timer->it_clock = which_clock;
0525 new_timer->kclock = kc;
0526 new_timer->it_overrun = -1LL;
0527
0528 if (event) {
0529 rcu_read_lock();
0530 new_timer->it_pid = get_pid(good_sigevent(event));
0531 rcu_read_unlock();
0532 if (!new_timer->it_pid) {
0533 error = -EINVAL;
0534 goto out;
0535 }
0536 new_timer->it_sigev_notify = event->sigev_notify;
0537 new_timer->sigq->info.si_signo = event->sigev_signo;
0538 new_timer->sigq->info.si_value = event->sigev_value;
0539 } else {
0540 new_timer->it_sigev_notify = SIGEV_SIGNAL;
0541 new_timer->sigq->info.si_signo = SIGALRM;
0542 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
0543 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
0544 new_timer->it_pid = get_pid(task_tgid(current));
0545 }
0546
0547 new_timer->sigq->info.si_tid = new_timer->it_id;
0548 new_timer->sigq->info.si_code = SI_TIMER;
0549
0550 if (copy_to_user(created_timer_id,
0551 &new_timer_id, sizeof (new_timer_id))) {
0552 error = -EFAULT;
0553 goto out;
0554 }
0555
0556 error = kc->timer_create(new_timer);
0557 if (error)
0558 goto out;
0559
0560 spin_lock_irq(¤t->sighand->siglock);
0561 new_timer->it_signal = current->signal;
0562 list_add(&new_timer->list, ¤t->signal->posix_timers);
0563 spin_unlock_irq(¤t->sighand->siglock);
0564
0565 return 0;
0566
0567
0568
0569
0570
0571
0572 out:
0573 release_posix_timer(new_timer, it_id_set);
0574 return error;
0575 }
0576
0577 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
0578 struct sigevent __user *, timer_event_spec,
0579 timer_t __user *, created_timer_id)
0580 {
0581 if (timer_event_spec) {
0582 sigevent_t event;
0583
0584 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
0585 return -EFAULT;
0586 return do_timer_create(which_clock, &event, created_timer_id);
0587 }
0588 return do_timer_create(which_clock, NULL, created_timer_id);
0589 }
0590
0591 #ifdef CONFIG_COMPAT
0592 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
0593 struct compat_sigevent __user *, timer_event_spec,
0594 timer_t __user *, created_timer_id)
0595 {
0596 if (timer_event_spec) {
0597 sigevent_t event;
0598
0599 if (get_compat_sigevent(&event, timer_event_spec))
0600 return -EFAULT;
0601 return do_timer_create(which_clock, &event, created_timer_id);
0602 }
0603 return do_timer_create(which_clock, NULL, created_timer_id);
0604 }
0605 #endif
0606
0607
0608
0609
0610
0611
0612
0613
0614 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
0615 {
0616 struct k_itimer *timr;
0617
0618
0619
0620
0621
0622 if ((unsigned long long)timer_id > INT_MAX)
0623 return NULL;
0624
0625 rcu_read_lock();
0626 timr = posix_timer_by_id(timer_id);
0627 if (timr) {
0628 spin_lock_irqsave(&timr->it_lock, *flags);
0629 if (timr->it_signal == current->signal) {
0630 rcu_read_unlock();
0631 return timr;
0632 }
0633 spin_unlock_irqrestore(&timr->it_lock, *flags);
0634 }
0635 rcu_read_unlock();
0636
0637 return NULL;
0638 }
0639
0640 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
0641 {
0642 struct hrtimer *timer = &timr->it.real.timer;
0643
0644 return __hrtimer_expires_remaining_adjusted(timer, now);
0645 }
0646
0647 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
0648 {
0649 struct hrtimer *timer = &timr->it.real.timer;
0650
0651 return hrtimer_forward(timer, now, timr->it_interval);
0652 }
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
0671 {
0672 const struct k_clock *kc = timr->kclock;
0673 ktime_t now, remaining, iv;
0674 bool sig_none;
0675
0676 sig_none = timr->it_sigev_notify == SIGEV_NONE;
0677 iv = timr->it_interval;
0678
0679
0680 if (iv) {
0681 cur_setting->it_interval = ktime_to_timespec64(iv);
0682 } else if (!timr->it_active) {
0683
0684
0685
0686
0687 if (!sig_none)
0688 return;
0689 }
0690
0691 now = kc->clock_get_ktime(timr->it_clock);
0692
0693
0694
0695
0696
0697 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
0698 timr->it_overrun += kc->timer_forward(timr, now);
0699
0700 remaining = kc->timer_remaining(timr, now);
0701
0702 if (remaining <= 0) {
0703
0704
0705
0706
0707 if (!sig_none)
0708 cur_setting->it_value.tv_nsec = 1;
0709 } else {
0710 cur_setting->it_value = ktime_to_timespec64(remaining);
0711 }
0712 }
0713
0714
0715 static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
0716 {
0717 struct k_itimer *timr;
0718 const struct k_clock *kc;
0719 unsigned long flags;
0720 int ret = 0;
0721
0722 timr = lock_timer(timer_id, &flags);
0723 if (!timr)
0724 return -EINVAL;
0725
0726 memset(setting, 0, sizeof(*setting));
0727 kc = timr->kclock;
0728 if (WARN_ON_ONCE(!kc || !kc->timer_get))
0729 ret = -EINVAL;
0730 else
0731 kc->timer_get(timr, setting);
0732
0733 unlock_timer(timr, flags);
0734 return ret;
0735 }
0736
0737
0738 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
0739 struct __kernel_itimerspec __user *, setting)
0740 {
0741 struct itimerspec64 cur_setting;
0742
0743 int ret = do_timer_gettime(timer_id, &cur_setting);
0744 if (!ret) {
0745 if (put_itimerspec64(&cur_setting, setting))
0746 ret = -EFAULT;
0747 }
0748 return ret;
0749 }
0750
0751 #ifdef CONFIG_COMPAT_32BIT_TIME
0752
0753 SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
0754 struct old_itimerspec32 __user *, setting)
0755 {
0756 struct itimerspec64 cur_setting;
0757
0758 int ret = do_timer_gettime(timer_id, &cur_setting);
0759 if (!ret) {
0760 if (put_old_itimerspec32(&cur_setting, setting))
0761 ret = -EFAULT;
0762 }
0763 return ret;
0764 }
0765
0766 #endif
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
0778 {
0779 struct k_itimer *timr;
0780 int overrun;
0781 unsigned long flags;
0782
0783 timr = lock_timer(timer_id, &flags);
0784 if (!timr)
0785 return -EINVAL;
0786
0787 overrun = timer_overrun_to_int(timr, 0);
0788 unlock_timer(timr, flags);
0789
0790 return overrun;
0791 }
0792
0793 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
0794 bool absolute, bool sigev_none)
0795 {
0796 struct hrtimer *timer = &timr->it.real.timer;
0797 enum hrtimer_mode mode;
0798
0799 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809 if (timr->it_clock == CLOCK_REALTIME)
0810 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
0811
0812 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
0813 timr->it.real.timer.function = posix_timer_fn;
0814
0815 if (!absolute)
0816 expires = ktime_add_safe(expires, timer->base->get_time());
0817 hrtimer_set_expires(timer, expires);
0818
0819 if (!sigev_none)
0820 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
0821 }
0822
0823 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
0824 {
0825 return hrtimer_try_to_cancel(&timr->it.real.timer);
0826 }
0827
0828 static void common_timer_wait_running(struct k_itimer *timer)
0829 {
0830 hrtimer_cancel_wait_running(&timer->it.real.timer);
0831 }
0832
0833
0834
0835
0836
0837
0838
0839 static struct k_itimer *timer_wait_running(struct k_itimer *timer,
0840 unsigned long *flags)
0841 {
0842 const struct k_clock *kc = READ_ONCE(timer->kclock);
0843 timer_t timer_id = READ_ONCE(timer->it_id);
0844
0845
0846 rcu_read_lock();
0847 unlock_timer(timer, *flags);
0848
0849 if (!WARN_ON_ONCE(!kc->timer_wait_running))
0850 kc->timer_wait_running(timer);
0851
0852 rcu_read_unlock();
0853
0854 return lock_timer(timer_id, flags);
0855 }
0856
0857
0858 int common_timer_set(struct k_itimer *timr, int flags,
0859 struct itimerspec64 *new_setting,
0860 struct itimerspec64 *old_setting)
0861 {
0862 const struct k_clock *kc = timr->kclock;
0863 bool sigev_none;
0864 ktime_t expires;
0865
0866 if (old_setting)
0867 common_timer_get(timr, old_setting);
0868
0869
0870 timr->it_interval = 0;
0871
0872
0873
0874
0875 if (kc->timer_try_to_cancel(timr) < 0)
0876 return TIMER_RETRY;
0877
0878 timr->it_active = 0;
0879 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
0880 ~REQUEUE_PENDING;
0881 timr->it_overrun_last = 0;
0882
0883
0884 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
0885 return 0;
0886
0887 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
0888 expires = timespec64_to_ktime(new_setting->it_value);
0889 if (flags & TIMER_ABSTIME)
0890 expires = timens_ktime_to_host(timr->it_clock, expires);
0891 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
0892
0893 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
0894 timr->it_active = !sigev_none;
0895 return 0;
0896 }
0897
0898 static int do_timer_settime(timer_t timer_id, int tmr_flags,
0899 struct itimerspec64 *new_spec64,
0900 struct itimerspec64 *old_spec64)
0901 {
0902 const struct k_clock *kc;
0903 struct k_itimer *timr;
0904 unsigned long flags;
0905 int error = 0;
0906
0907 if (!timespec64_valid(&new_spec64->it_interval) ||
0908 !timespec64_valid(&new_spec64->it_value))
0909 return -EINVAL;
0910
0911 if (old_spec64)
0912 memset(old_spec64, 0, sizeof(*old_spec64));
0913
0914 timr = lock_timer(timer_id, &flags);
0915 retry:
0916 if (!timr)
0917 return -EINVAL;
0918
0919 kc = timr->kclock;
0920 if (WARN_ON_ONCE(!kc || !kc->timer_set))
0921 error = -EINVAL;
0922 else
0923 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
0924
0925 if (error == TIMER_RETRY) {
0926
0927 old_spec64 = NULL;
0928
0929 timr = timer_wait_running(timr, &flags);
0930 goto retry;
0931 }
0932 unlock_timer(timr, flags);
0933
0934 return error;
0935 }
0936
0937
0938 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
0939 const struct __kernel_itimerspec __user *, new_setting,
0940 struct __kernel_itimerspec __user *, old_setting)
0941 {
0942 struct itimerspec64 new_spec, old_spec;
0943 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
0944 int error = 0;
0945
0946 if (!new_setting)
0947 return -EINVAL;
0948
0949 if (get_itimerspec64(&new_spec, new_setting))
0950 return -EFAULT;
0951
0952 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
0953 if (!error && old_setting) {
0954 if (put_itimerspec64(&old_spec, old_setting))
0955 error = -EFAULT;
0956 }
0957 return error;
0958 }
0959
0960 #ifdef CONFIG_COMPAT_32BIT_TIME
0961 SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
0962 struct old_itimerspec32 __user *, new,
0963 struct old_itimerspec32 __user *, old)
0964 {
0965 struct itimerspec64 new_spec, old_spec;
0966 struct itimerspec64 *rtn = old ? &old_spec : NULL;
0967 int error = 0;
0968
0969 if (!new)
0970 return -EINVAL;
0971 if (get_old_itimerspec32(&new_spec, new))
0972 return -EFAULT;
0973
0974 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
0975 if (!error && old) {
0976 if (put_old_itimerspec32(&old_spec, old))
0977 error = -EFAULT;
0978 }
0979 return error;
0980 }
0981 #endif
0982
0983 int common_timer_del(struct k_itimer *timer)
0984 {
0985 const struct k_clock *kc = timer->kclock;
0986
0987 timer->it_interval = 0;
0988 if (kc->timer_try_to_cancel(timer) < 0)
0989 return TIMER_RETRY;
0990 timer->it_active = 0;
0991 return 0;
0992 }
0993
0994 static inline int timer_delete_hook(struct k_itimer *timer)
0995 {
0996 const struct k_clock *kc = timer->kclock;
0997
0998 if (WARN_ON_ONCE(!kc || !kc->timer_del))
0999 return -EINVAL;
1000 return kc->timer_del(timer);
1001 }
1002
1003
1004 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
1005 {
1006 struct k_itimer *timer;
1007 unsigned long flags;
1008
1009 timer = lock_timer(timer_id, &flags);
1010
1011 retry_delete:
1012 if (!timer)
1013 return -EINVAL;
1014
1015 if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
1016
1017 timer = timer_wait_running(timer, &flags);
1018 goto retry_delete;
1019 }
1020
1021 spin_lock(¤t->sighand->siglock);
1022 list_del(&timer->list);
1023 spin_unlock(¤t->sighand->siglock);
1024
1025
1026
1027
1028 timer->it_signal = NULL;
1029
1030 unlock_timer(timer, flags);
1031 release_posix_timer(timer, IT_ID_SET);
1032 return 0;
1033 }
1034
1035
1036
1037
1038 static void itimer_delete(struct k_itimer *timer)
1039 {
1040 retry_delete:
1041 spin_lock_irq(&timer->it_lock);
1042
1043 if (timer_delete_hook(timer) == TIMER_RETRY) {
1044 spin_unlock_irq(&timer->it_lock);
1045 goto retry_delete;
1046 }
1047 list_del(&timer->list);
1048
1049 spin_unlock_irq(&timer->it_lock);
1050 release_posix_timer(timer, IT_ID_SET);
1051 }
1052
1053
1054
1055
1056
1057
1058 void exit_itimers(struct task_struct *tsk)
1059 {
1060 struct list_head timers;
1061 struct k_itimer *tmr;
1062
1063 if (list_empty(&tsk->signal->posix_timers))
1064 return;
1065
1066 spin_lock_irq(&tsk->sighand->siglock);
1067 list_replace_init(&tsk->signal->posix_timers, &timers);
1068 spin_unlock_irq(&tsk->sighand->siglock);
1069
1070 while (!list_empty(&timers)) {
1071 tmr = list_first_entry(&timers, struct k_itimer, list);
1072 itimer_delete(tmr);
1073 }
1074 }
1075
1076 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1077 const struct __kernel_timespec __user *, tp)
1078 {
1079 const struct k_clock *kc = clockid_to_kclock(which_clock);
1080 struct timespec64 new_tp;
1081
1082 if (!kc || !kc->clock_set)
1083 return -EINVAL;
1084
1085 if (get_timespec64(&new_tp, tp))
1086 return -EFAULT;
1087
1088 return kc->clock_set(which_clock, &new_tp);
1089 }
1090
1091 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1092 struct __kernel_timespec __user *, tp)
1093 {
1094 const struct k_clock *kc = clockid_to_kclock(which_clock);
1095 struct timespec64 kernel_tp;
1096 int error;
1097
1098 if (!kc)
1099 return -EINVAL;
1100
1101 error = kc->clock_get_timespec(which_clock, &kernel_tp);
1102
1103 if (!error && put_timespec64(&kernel_tp, tp))
1104 error = -EFAULT;
1105
1106 return error;
1107 }
1108
1109 int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
1110 {
1111 const struct k_clock *kc = clockid_to_kclock(which_clock);
1112
1113 if (!kc)
1114 return -EINVAL;
1115 if (!kc->clock_adj)
1116 return -EOPNOTSUPP;
1117
1118 return kc->clock_adj(which_clock, ktx);
1119 }
1120
1121 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1122 struct __kernel_timex __user *, utx)
1123 {
1124 struct __kernel_timex ktx;
1125 int err;
1126
1127 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1128 return -EFAULT;
1129
1130 err = do_clock_adjtime(which_clock, &ktx);
1131
1132 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1133 return -EFAULT;
1134
1135 return err;
1136 }
1137
1138 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1139 struct __kernel_timespec __user *, tp)
1140 {
1141 const struct k_clock *kc = clockid_to_kclock(which_clock);
1142 struct timespec64 rtn_tp;
1143 int error;
1144
1145 if (!kc)
1146 return -EINVAL;
1147
1148 error = kc->clock_getres(which_clock, &rtn_tp);
1149
1150 if (!error && tp && put_timespec64(&rtn_tp, tp))
1151 error = -EFAULT;
1152
1153 return error;
1154 }
1155
1156 #ifdef CONFIG_COMPAT_32BIT_TIME
1157
1158 SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1159 struct old_timespec32 __user *, tp)
1160 {
1161 const struct k_clock *kc = clockid_to_kclock(which_clock);
1162 struct timespec64 ts;
1163
1164 if (!kc || !kc->clock_set)
1165 return -EINVAL;
1166
1167 if (get_old_timespec32(&ts, tp))
1168 return -EFAULT;
1169
1170 return kc->clock_set(which_clock, &ts);
1171 }
1172
1173 SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1174 struct old_timespec32 __user *, tp)
1175 {
1176 const struct k_clock *kc = clockid_to_kclock(which_clock);
1177 struct timespec64 ts;
1178 int err;
1179
1180 if (!kc)
1181 return -EINVAL;
1182
1183 err = kc->clock_get_timespec(which_clock, &ts);
1184
1185 if (!err && put_old_timespec32(&ts, tp))
1186 err = -EFAULT;
1187
1188 return err;
1189 }
1190
1191 SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1192 struct old_timex32 __user *, utp)
1193 {
1194 struct __kernel_timex ktx;
1195 int err;
1196
1197 err = get_old_timex32(&ktx, utp);
1198 if (err)
1199 return err;
1200
1201 err = do_clock_adjtime(which_clock, &ktx);
1202
1203 if (err >= 0 && put_old_timex32(utp, &ktx))
1204 return -EFAULT;
1205
1206 return err;
1207 }
1208
1209 SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1210 struct old_timespec32 __user *, tp)
1211 {
1212 const struct k_clock *kc = clockid_to_kclock(which_clock);
1213 struct timespec64 ts;
1214 int err;
1215
1216 if (!kc)
1217 return -EINVAL;
1218
1219 err = kc->clock_getres(which_clock, &ts);
1220 if (!err && tp && put_old_timespec32(&ts, tp))
1221 return -EFAULT;
1222
1223 return err;
1224 }
1225
1226 #endif
1227
1228
1229
1230
1231 static int common_nsleep(const clockid_t which_clock, int flags,
1232 const struct timespec64 *rqtp)
1233 {
1234 ktime_t texp = timespec64_to_ktime(*rqtp);
1235
1236 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1237 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1238 which_clock);
1239 }
1240
1241 static int common_nsleep_timens(const clockid_t which_clock, int flags,
1242 const struct timespec64 *rqtp)
1243 {
1244 ktime_t texp = timespec64_to_ktime(*rqtp);
1245
1246 if (flags & TIMER_ABSTIME)
1247 texp = timens_ktime_to_host(which_clock, texp);
1248
1249 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1250 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1251 which_clock);
1252 }
1253
1254 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1255 const struct __kernel_timespec __user *, rqtp,
1256 struct __kernel_timespec __user *, rmtp)
1257 {
1258 const struct k_clock *kc = clockid_to_kclock(which_clock);
1259 struct timespec64 t;
1260
1261 if (!kc)
1262 return -EINVAL;
1263 if (!kc->nsleep)
1264 return -EOPNOTSUPP;
1265
1266 if (get_timespec64(&t, rqtp))
1267 return -EFAULT;
1268
1269 if (!timespec64_valid(&t))
1270 return -EINVAL;
1271 if (flags & TIMER_ABSTIME)
1272 rmtp = NULL;
1273 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1274 current->restart_block.nanosleep.rmtp = rmtp;
1275
1276 return kc->nsleep(which_clock, flags, &t);
1277 }
1278
1279 #ifdef CONFIG_COMPAT_32BIT_TIME
1280
1281 SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1282 struct old_timespec32 __user *, rqtp,
1283 struct old_timespec32 __user *, rmtp)
1284 {
1285 const struct k_clock *kc = clockid_to_kclock(which_clock);
1286 struct timespec64 t;
1287
1288 if (!kc)
1289 return -EINVAL;
1290 if (!kc->nsleep)
1291 return -EOPNOTSUPP;
1292
1293 if (get_old_timespec32(&t, rqtp))
1294 return -EFAULT;
1295
1296 if (!timespec64_valid(&t))
1297 return -EINVAL;
1298 if (flags & TIMER_ABSTIME)
1299 rmtp = NULL;
1300 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1301 current->restart_block.nanosleep.compat_rmtp = rmtp;
1302
1303 return kc->nsleep(which_clock, flags, &t);
1304 }
1305
1306 #endif
1307
1308 static const struct k_clock clock_realtime = {
1309 .clock_getres = posix_get_hrtimer_res,
1310 .clock_get_timespec = posix_get_realtime_timespec,
1311 .clock_get_ktime = posix_get_realtime_ktime,
1312 .clock_set = posix_clock_realtime_set,
1313 .clock_adj = posix_clock_realtime_adj,
1314 .nsleep = common_nsleep,
1315 .timer_create = common_timer_create,
1316 .timer_set = common_timer_set,
1317 .timer_get = common_timer_get,
1318 .timer_del = common_timer_del,
1319 .timer_rearm = common_hrtimer_rearm,
1320 .timer_forward = common_hrtimer_forward,
1321 .timer_remaining = common_hrtimer_remaining,
1322 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1323 .timer_wait_running = common_timer_wait_running,
1324 .timer_arm = common_hrtimer_arm,
1325 };
1326
1327 static const struct k_clock clock_monotonic = {
1328 .clock_getres = posix_get_hrtimer_res,
1329 .clock_get_timespec = posix_get_monotonic_timespec,
1330 .clock_get_ktime = posix_get_monotonic_ktime,
1331 .nsleep = common_nsleep_timens,
1332 .timer_create = common_timer_create,
1333 .timer_set = common_timer_set,
1334 .timer_get = common_timer_get,
1335 .timer_del = common_timer_del,
1336 .timer_rearm = common_hrtimer_rearm,
1337 .timer_forward = common_hrtimer_forward,
1338 .timer_remaining = common_hrtimer_remaining,
1339 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1340 .timer_wait_running = common_timer_wait_running,
1341 .timer_arm = common_hrtimer_arm,
1342 };
1343
1344 static const struct k_clock clock_monotonic_raw = {
1345 .clock_getres = posix_get_hrtimer_res,
1346 .clock_get_timespec = posix_get_monotonic_raw,
1347 };
1348
1349 static const struct k_clock clock_realtime_coarse = {
1350 .clock_getres = posix_get_coarse_res,
1351 .clock_get_timespec = posix_get_realtime_coarse,
1352 };
1353
1354 static const struct k_clock clock_monotonic_coarse = {
1355 .clock_getres = posix_get_coarse_res,
1356 .clock_get_timespec = posix_get_monotonic_coarse,
1357 };
1358
1359 static const struct k_clock clock_tai = {
1360 .clock_getres = posix_get_hrtimer_res,
1361 .clock_get_ktime = posix_get_tai_ktime,
1362 .clock_get_timespec = posix_get_tai_timespec,
1363 .nsleep = common_nsleep,
1364 .timer_create = common_timer_create,
1365 .timer_set = common_timer_set,
1366 .timer_get = common_timer_get,
1367 .timer_del = common_timer_del,
1368 .timer_rearm = common_hrtimer_rearm,
1369 .timer_forward = common_hrtimer_forward,
1370 .timer_remaining = common_hrtimer_remaining,
1371 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1372 .timer_wait_running = common_timer_wait_running,
1373 .timer_arm = common_hrtimer_arm,
1374 };
1375
1376 static const struct k_clock clock_boottime = {
1377 .clock_getres = posix_get_hrtimer_res,
1378 .clock_get_ktime = posix_get_boottime_ktime,
1379 .clock_get_timespec = posix_get_boottime_timespec,
1380 .nsleep = common_nsleep_timens,
1381 .timer_create = common_timer_create,
1382 .timer_set = common_timer_set,
1383 .timer_get = common_timer_get,
1384 .timer_del = common_timer_del,
1385 .timer_rearm = common_hrtimer_rearm,
1386 .timer_forward = common_hrtimer_forward,
1387 .timer_remaining = common_hrtimer_remaining,
1388 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1389 .timer_wait_running = common_timer_wait_running,
1390 .timer_arm = common_hrtimer_arm,
1391 };
1392
1393 static const struct k_clock * const posix_clocks[] = {
1394 [CLOCK_REALTIME] = &clock_realtime,
1395 [CLOCK_MONOTONIC] = &clock_monotonic,
1396 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1397 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1398 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1399 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1400 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1401 [CLOCK_BOOTTIME] = &clock_boottime,
1402 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1403 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1404 [CLOCK_TAI] = &clock_tai,
1405 };
1406
1407 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1408 {
1409 clockid_t idx = id;
1410
1411 if (id < 0) {
1412 return (id & CLOCKFD_MASK) == CLOCKFD ?
1413 &clock_posix_dynamic : &clock_posix_cpu;
1414 }
1415
1416 if (id >= ARRAY_SIZE(posix_clocks))
1417 return NULL;
1418
1419 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1420 }