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