summaryrefslogtreecommitdiffstats
path: root/kernel/time/posix-timers.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-07-03 19:14:51 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-07-03 19:14:51 -0400
commit1b044f1cfc65a7d90b209dfabd57e16d98b58c5b (patch)
treead657c911b563f9176b17578c0b88a1ea9916a02 /kernel/time/posix-timers.c
parente0f3e8f14da868047c524a0cf11e08b95fd1b008 (diff)
parent2287d8664fe7345ead891017eccd879fc605305e (diff)
Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull timer updates from Thomas Gleixner: "A rather large update for timers/timekeeping: - compat syscall consolidation (Al Viro) - Posix timer consolidation (Christoph Helwig / Thomas Gleixner) - Cleanup of the device tree based initialization for clockevents and clocksources (Daniel Lezcano) - Consolidation of the FTTMR010 clocksource/event driver (Linus Walleij) - The usual set of small fixes and updates all over the place" * 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (93 commits) timers: Make the cpu base lock raw clocksource/drivers/mips-gic-timer: Fix an error code in 'gic_clocksource_of_init()' clocksource/drivers/fsl_ftm_timer: Unmap region obtained by of_iomap clocksource/drivers/tcb_clksrc: Make IO endian agnostic clocksource/drivers/sun4i: Switch to the timer-of common init clocksource/drivers/timer-of: Fix invalid iomap check Revert "ktime: Simplify ktime_compare implementation" clocksource/drivers: Fix uninitialized variable use in timer_of_init kselftests: timers: Add test for frequency step kselftests: timers: Fix inconsistency-check to not ignore first timestamp time: Add warning about imminent deprecation of CONFIG_GENERIC_TIME_VSYSCALL_OLD time: Clean up CLOCK_MONOTONIC_RAW time handling posix-cpu-timers: Make timespec to nsec conversion safe itimer: Make timeval to nsec conversion range limited timers: Fix parameter description of try_to_del_timer_sync() ktime: Simplify ktime_compare implementation clocksource/drivers/fttmr010: Factor out clock read code clocksource/drivers/fttmr010: Implement delay timer clocksource/drivers: Add timer-of common init routine clocksource/drivers/tcb_clksrc: Save timer context on suspend/resume ...
Diffstat (limited to 'kernel/time/posix-timers.c')
-rw-r--r--kernel/time/posix-timers.c759
1 files changed, 497 insertions, 262 deletions
diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 4d7b2ce09c27..82d67be7d9d1 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -49,8 +49,10 @@
49#include <linux/workqueue.h> 49#include <linux/workqueue.h>
50#include <linux/export.h> 50#include <linux/export.h>
51#include <linux/hashtable.h> 51#include <linux/hashtable.h>
52#include <linux/compat.h>
52 53
53#include "timekeeping.h" 54#include "timekeeping.h"
55#include "posix-timers.h"
54 56
55/* 57/*
56 * Management arrays for POSIX timers. Timers are now kept in static hash table 58 * Management arrays for POSIX timers. Timers are now kept in static hash table
@@ -69,6 +71,10 @@ static struct kmem_cache *posix_timers_cache;
69static DEFINE_HASHTABLE(posix_timers_hashtable, 9); 71static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
70static DEFINE_SPINLOCK(hash_lock); 72static DEFINE_SPINLOCK(hash_lock);
71 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
72/* 78/*
73 * we assume that the new SIGEV_THREAD_ID shares no bits with the other 79 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
74 * SIGEV values. Here we put out an error if this assumption fails. 80 * SIGEV values. Here we put out an error if this assumption fails.
@@ -124,22 +130,6 @@ static DEFINE_SPINLOCK(hash_lock);
124 * have is CLOCK_REALTIME and its high res counter part, both of 130 * have is CLOCK_REALTIME and its high res counter part, both of
125 * which we beg off on and pass to do_sys_settimeofday(). 131 * which we beg off on and pass to do_sys_settimeofday().
126 */ 132 */
127
128static struct k_clock posix_clocks[MAX_CLOCKS];
129
130/*
131 * These ones are defined below.
132 */
133static int common_nsleep(const clockid_t, int flags, struct timespec64 *t,
134 struct timespec __user *rmtp);
135static int common_timer_create(struct k_itimer *new_timer);
136static void common_timer_get(struct k_itimer *, struct itimerspec64 *);
137static int common_timer_set(struct k_itimer *, int,
138 struct itimerspec64 *, struct itimerspec64 *);
139static int common_timer_del(struct k_itimer *timer);
140
141static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
142
143static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); 133static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
144 134
145#define lock_timer(tid, flags) \ 135#define lock_timer(tid, flags) \
@@ -285,91 +275,23 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
285 */ 275 */
286static __init int init_posix_timers(void) 276static __init int init_posix_timers(void)
287{ 277{
288 struct k_clock clock_realtime = {
289 .clock_getres = posix_get_hrtimer_res,
290 .clock_get = posix_clock_realtime_get,
291 .clock_set = posix_clock_realtime_set,
292 .clock_adj = posix_clock_realtime_adj,
293 .nsleep = common_nsleep,
294 .nsleep_restart = hrtimer_nanosleep_restart,
295 .timer_create = common_timer_create,
296 .timer_set = common_timer_set,
297 .timer_get = common_timer_get,
298 .timer_del = common_timer_del,
299 };
300 struct k_clock clock_monotonic = {
301 .clock_getres = posix_get_hrtimer_res,
302 .clock_get = posix_ktime_get_ts,
303 .nsleep = common_nsleep,
304 .nsleep_restart = hrtimer_nanosleep_restart,
305 .timer_create = common_timer_create,
306 .timer_set = common_timer_set,
307 .timer_get = common_timer_get,
308 .timer_del = common_timer_del,
309 };
310 struct k_clock clock_monotonic_raw = {
311 .clock_getres = posix_get_hrtimer_res,
312 .clock_get = posix_get_monotonic_raw,
313 };
314 struct k_clock clock_realtime_coarse = {
315 .clock_getres = posix_get_coarse_res,
316 .clock_get = posix_get_realtime_coarse,
317 };
318 struct k_clock clock_monotonic_coarse = {
319 .clock_getres = posix_get_coarse_res,
320 .clock_get = posix_get_monotonic_coarse,
321 };
322 struct k_clock clock_tai = {
323 .clock_getres = posix_get_hrtimer_res,
324 .clock_get = posix_get_tai,
325 .nsleep = common_nsleep,
326 .nsleep_restart = hrtimer_nanosleep_restart,
327 .timer_create = common_timer_create,
328 .timer_set = common_timer_set,
329 .timer_get = common_timer_get,
330 .timer_del = common_timer_del,
331 };
332 struct k_clock clock_boottime = {
333 .clock_getres = posix_get_hrtimer_res,
334 .clock_get = posix_get_boottime,
335 .nsleep = common_nsleep,
336 .nsleep_restart = hrtimer_nanosleep_restart,
337 .timer_create = common_timer_create,
338 .timer_set = common_timer_set,
339 .timer_get = common_timer_get,
340 .timer_del = common_timer_del,
341 };
342
343 posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
344 posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
345 posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
346 posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
347 posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
348 posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
349 posix_timers_register_clock(CLOCK_TAI, &clock_tai);
350
351 posix_timers_cache = kmem_cache_create("posix_timers_cache", 278 posix_timers_cache = kmem_cache_create("posix_timers_cache",
352 sizeof (struct k_itimer), 0, SLAB_PANIC, 279 sizeof (struct k_itimer), 0, SLAB_PANIC,
353 NULL); 280 NULL);
354 return 0; 281 return 0;
355} 282}
356
357__initcall(init_posix_timers); 283__initcall(init_posix_timers);
358 284
359static void schedule_next_timer(struct k_itimer *timr) 285static void common_hrtimer_rearm(struct k_itimer *timr)
360{ 286{
361 struct hrtimer *timer = &timr->it.real.timer; 287 struct hrtimer *timer = &timr->it.real.timer;
362 288
363 if (timr->it.real.interval == 0) 289 if (!timr->it_interval)
364 return; 290 return;
365 291
366 timr->it_overrun += (unsigned int) hrtimer_forward(timer, 292 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
367 timer->base->get_time(), 293 timer->base->get_time(),
368 timr->it.real.interval); 294 timr->it_interval);
369
370 timr->it_overrun_last = timr->it_overrun;
371 timr->it_overrun = -1;
372 ++timr->it_requeue_pending;
373 hrtimer_restart(timer); 295 hrtimer_restart(timer);
374} 296}
375 297
@@ -384,24 +306,27 @@ static void schedule_next_timer(struct k_itimer *timr)
384 * To protect against the timer going away while the interrupt is queued, 306 * To protect against the timer going away while the interrupt is queued,
385 * we require that the it_requeue_pending flag be set. 307 * we require that the it_requeue_pending flag be set.
386 */ 308 */
387void do_schedule_next_timer(struct siginfo *info) 309void posixtimer_rearm(struct siginfo *info)
388{ 310{
389 struct k_itimer *timr; 311 struct k_itimer *timr;
390 unsigned long flags; 312 unsigned long flags;
391 313
392 timr = lock_timer(info->si_tid, &flags); 314 timr = lock_timer(info->si_tid, &flags);
315 if (!timr)
316 return;
393 317
394 if (timr && timr->it_requeue_pending == info->si_sys_private) { 318 if (timr->it_requeue_pending == info->si_sys_private) {
395 if (timr->it_clock < 0) 319 timr->kclock->timer_rearm(timr);
396 posix_cpu_timer_schedule(timr); 320
397 else 321 timr->it_active = 1;
398 schedule_next_timer(timr); 322 timr->it_overrun_last = timr->it_overrun;
323 timr->it_overrun = -1;
324 ++timr->it_requeue_pending;
399 325
400 info->si_overrun += timr->it_overrun_last; 326 info->si_overrun += timr->it_overrun_last;
401 } 327 }
402 328
403 if (timr) 329 unlock_timer(timr, flags);
404 unlock_timer(timr, flags);
405} 330}
406 331
407int posix_timer_event(struct k_itimer *timr, int si_private) 332int posix_timer_event(struct k_itimer *timr, int si_private)
@@ -410,12 +335,12 @@ int posix_timer_event(struct k_itimer *timr, int si_private)
410 int shared, ret = -1; 335 int shared, ret = -1;
411 /* 336 /*
412 * FIXME: if ->sigq is queued we can race with 337 * FIXME: if ->sigq is queued we can race with
413 * dequeue_signal()->do_schedule_next_timer(). 338 * dequeue_signal()->posixtimer_rearm().
414 * 339 *
415 * If dequeue_signal() sees the "right" value of 340 * If dequeue_signal() sees the "right" value of
416 * si_sys_private it calls do_schedule_next_timer(). 341 * si_sys_private it calls posixtimer_rearm().
417 * We re-queue ->sigq and drop ->it_lock(). 342 * We re-queue ->sigq and drop ->it_lock().
418 * do_schedule_next_timer() locks the timer 343 * posixtimer_rearm() locks the timer
419 * and re-schedules it while ->sigq is pending. 344 * and re-schedules it while ->sigq is pending.
420 * Not really bad, but not that we want. 345 * Not really bad, but not that we want.
421 */ 346 */
@@ -431,7 +356,6 @@ int posix_timer_event(struct k_itimer *timr, int si_private)
431 /* If we failed to send the signal the timer stops. */ 356 /* If we failed to send the signal the timer stops. */
432 return ret > 0; 357 return ret > 0;
433} 358}
434EXPORT_SYMBOL_GPL(posix_timer_event);
435 359
436/* 360/*
437 * This function gets called when a POSIX.1b interval timer expires. It 361 * This function gets called when a POSIX.1b interval timer expires. It
@@ -450,7 +374,8 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
450 timr = container_of(timer, struct k_itimer, it.real.timer); 374 timr = container_of(timer, struct k_itimer, it.real.timer);
451 spin_lock_irqsave(&timr->it_lock, flags); 375 spin_lock_irqsave(&timr->it_lock, flags);
452 376
453 if (timr->it.real.interval != 0) 377 timr->it_active = 0;
378 if (timr->it_interval != 0)
454 si_private = ++timr->it_requeue_pending; 379 si_private = ++timr->it_requeue_pending;
455 380
456 if (posix_timer_event(timr, si_private)) { 381 if (posix_timer_event(timr, si_private)) {
@@ -459,7 +384,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
459 * we will not get a call back to restart it AND 384 * we will not get a call back to restart it AND
460 * it should be restarted. 385 * it should be restarted.
461 */ 386 */
462 if (timr->it.real.interval != 0) { 387 if (timr->it_interval != 0) {
463 ktime_t now = hrtimer_cb_get_time(timer); 388 ktime_t now = hrtimer_cb_get_time(timer);
464 389
465 /* 390 /*
@@ -488,15 +413,16 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
488 { 413 {
489 ktime_t kj = NSEC_PER_SEC / HZ; 414 ktime_t kj = NSEC_PER_SEC / HZ;
490 415
491 if (timr->it.real.interval < kj) 416 if (timr->it_interval < kj)
492 now = ktime_add(now, kj); 417 now = ktime_add(now, kj);
493 } 418 }
494#endif 419#endif
495 timr->it_overrun += (unsigned int) 420 timr->it_overrun += (unsigned int)
496 hrtimer_forward(timer, now, 421 hrtimer_forward(timer, now,
497 timr->it.real.interval); 422 timr->it_interval);
498 ret = HRTIMER_RESTART; 423 ret = HRTIMER_RESTART;
499 ++timr->it_requeue_pending; 424 ++timr->it_requeue_pending;
425 timr->it_active = 1;
500 } 426 }
501 } 427 }
502 428
@@ -521,30 +447,6 @@ static struct pid *good_sigevent(sigevent_t * event)
521 return task_pid(rtn); 447 return task_pid(rtn);
522} 448}
523 449
524void posix_timers_register_clock(const clockid_t clock_id,
525 struct k_clock *new_clock)
526{
527 if ((unsigned) clock_id >= MAX_CLOCKS) {
528 printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
529 clock_id);
530 return;
531 }
532
533 if (!new_clock->clock_get) {
534 printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
535 clock_id);
536 return;
537 }
538 if (!new_clock->clock_getres) {
539 printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
540 clock_id);
541 return;
542 }
543
544 posix_clocks[clock_id] = *new_clock;
545}
546EXPORT_SYMBOL_GPL(posix_timers_register_clock);
547
548static struct k_itimer * alloc_posix_timer(void) 450static struct k_itimer * alloc_posix_timer(void)
549{ 451{
550 struct k_itimer *tmr; 452 struct k_itimer *tmr;
@@ -581,17 +483,6 @@ static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
581 call_rcu(&tmr->it.rcu, k_itimer_rcu_free); 483 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
582} 484}
583 485
584static struct k_clock *clockid_to_kclock(const clockid_t id)
585{
586 if (id < 0)
587 return (id & CLOCKFD_MASK) == CLOCKFD ?
588 &clock_posix_dynamic : &clock_posix_cpu;
589
590 if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
591 return NULL;
592 return &posix_clocks[id];
593}
594
595static int common_timer_create(struct k_itimer *new_timer) 486static int common_timer_create(struct k_itimer *new_timer)
596{ 487{
597 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); 488 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
@@ -599,15 +490,12 @@ static int common_timer_create(struct k_itimer *new_timer)
599} 490}
600 491
601/* Create a POSIX.1b interval timer. */ 492/* Create a POSIX.1b interval timer. */
602 493static int do_timer_create(clockid_t which_clock, struct sigevent *event,
603SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, 494 timer_t __user *created_timer_id)
604 struct sigevent __user *, timer_event_spec,
605 timer_t __user *, created_timer_id)
606{ 495{
607 struct k_clock *kc = clockid_to_kclock(which_clock); 496 const struct k_clock *kc = clockid_to_kclock(which_clock);
608 struct k_itimer *new_timer; 497 struct k_itimer *new_timer;
609 int error, new_timer_id; 498 int error, new_timer_id;
610 sigevent_t event;
611 int it_id_set = IT_ID_NOT_SET; 499 int it_id_set = IT_ID_NOT_SET;
612 500
613 if (!kc) 501 if (!kc)
@@ -629,31 +517,28 @@ SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
629 it_id_set = IT_ID_SET; 517 it_id_set = IT_ID_SET;
630 new_timer->it_id = (timer_t) new_timer_id; 518 new_timer->it_id = (timer_t) new_timer_id;
631 new_timer->it_clock = which_clock; 519 new_timer->it_clock = which_clock;
520 new_timer->kclock = kc;
632 new_timer->it_overrun = -1; 521 new_timer->it_overrun = -1;
633 522
634 if (timer_event_spec) { 523 if (event) {
635 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
636 error = -EFAULT;
637 goto out;
638 }
639 rcu_read_lock(); 524 rcu_read_lock();
640 new_timer->it_pid = get_pid(good_sigevent(&event)); 525 new_timer->it_pid = get_pid(good_sigevent(event));
641 rcu_read_unlock(); 526 rcu_read_unlock();
642 if (!new_timer->it_pid) { 527 if (!new_timer->it_pid) {
643 error = -EINVAL; 528 error = -EINVAL;
644 goto out; 529 goto out;
645 } 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;
646 } else { 534 } else {
647 memset(&event.sigev_value, 0, sizeof(event.sigev_value)); 535 new_timer->it_sigev_notify = SIGEV_SIGNAL;
648 event.sigev_notify = SIGEV_SIGNAL; 536 new_timer->sigq->info.si_signo = SIGALRM;
649 event.sigev_signo = SIGALRM; 537 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
650 event.sigev_value.sival_int = new_timer->it_id; 538 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
651 new_timer->it_pid = get_pid(task_tgid(current)); 539 new_timer->it_pid = get_pid(task_tgid(current));
652 } 540 }
653 541
654 new_timer->it_sigev_notify = event.sigev_notify;
655 new_timer->sigq->info.si_signo = event.sigev_signo;
656 new_timer->sigq->info.si_value = event.sigev_value;
657 new_timer->sigq->info.si_tid = new_timer->it_id; 542 new_timer->sigq->info.si_tid = new_timer->it_id;
658 new_timer->sigq->info.si_code = SI_TIMER; 543 new_timer->sigq->info.si_code = SI_TIMER;
659 544
@@ -684,6 +569,36 @@ out:
684 return error; 569 return error;
685} 570}
686 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
687/* 602/*
688 * Locking issues: We need to protect the result of the id look up until 603 * Locking issues: We need to protect the result of the id look up until
689 * we get the timer locked down so it is not deleted under us. The 604 * we get the timer locked down so it is not deleted under us. The
@@ -717,6 +632,20 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
717 return NULL; 632 return NULL;
718} 633}
719 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
720/* 649/*
721 * Get the time remaining on a POSIX.1b interval timer. This function 650 * Get the time remaining on a POSIX.1b interval timer. This function
722 * is ALWAYS called with spin_lock_irq on the timer, thus it must not 651 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
@@ -733,55 +662,61 @@ static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
733 * it is the same as a requeue pending timer WRT to what we should 662 * it is the same as a requeue pending timer WRT to what we should
734 * report. 663 * report.
735 */ 664 */
736static void 665void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
737common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
738{ 666{
667 const struct k_clock *kc = timr->kclock;
739 ktime_t now, remaining, iv; 668 ktime_t now, remaining, iv;
740 struct hrtimer *timer = &timr->it.real.timer; 669 struct timespec64 ts64;
670 bool sig_none;
741 671
742 memset(cur_setting, 0, sizeof(*cur_setting)); 672 sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
743 673 iv = timr->it_interval;
744 iv = timr->it.real.interval;
745 674
746 /* interval timer ? */ 675 /* interval timer ? */
747 if (iv) 676 if (iv) {
748 cur_setting->it_interval = ktime_to_timespec64(iv); 677 cur_setting->it_interval = ktime_to_timespec64(iv);
749 else if (!hrtimer_active(timer) && 678 } else if (!timr->it_active) {
750 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) 679 /*
751 return; 680 * SIGEV_NONE oneshot timers are never queued. Check them
681 * below.
682 */
683 if (!sig_none)
684 return;
685 }
752 686
753 now = timer->base->get_time(); 687 /*
688 * The timespec64 based conversion is suboptimal, but it's not
689 * worth to implement yet another callback.
690 */
691 kc->clock_get(timr->it_clock, &ts64);
692 now = timespec64_to_ktime(ts64);
754 693
755 /* 694 /*
756 * When a requeue is pending or this is a SIGEV_NONE 695 * When a requeue is pending or this is a SIGEV_NONE timer move the
757 * timer move the expiry time forward by intervals, so 696 * expiry time forward by intervals, so expiry is > now.
758 * expiry is > now.
759 */ 697 */
760 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || 698 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
761 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) 699 timr->it_overrun += kc->timer_forward(timr, now);
762 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
763 700
764 remaining = __hrtimer_expires_remaining_adjusted(timer, now); 701 remaining = kc->timer_remaining(timr, now);
765 /* Return 0 only, when the timer is expired and not pending */ 702 /* Return 0 only, when the timer is expired and not pending */
766 if (remaining <= 0) { 703 if (remaining <= 0) {
767 /* 704 /*
768 * A single shot SIGEV_NONE timer must return 0, when 705 * A single shot SIGEV_NONE timer must return 0, when
769 * it is expired ! 706 * it is expired !
770 */ 707 */
771 if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) 708 if (!sig_none)
772 cur_setting->it_value.tv_nsec = 1; 709 cur_setting->it_value.tv_nsec = 1;
773 } else 710 } else {
774 cur_setting->it_value = ktime_to_timespec64(remaining); 711 cur_setting->it_value = ktime_to_timespec64(remaining);
712 }
775} 713}
776 714
777/* Get the time remaining on a POSIX.1b interval timer. */ 715/* Get the time remaining on a POSIX.1b interval timer. */
778SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, 716static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
779 struct itimerspec __user *, setting)
780{ 717{
781 struct itimerspec64 cur_setting64;
782 struct itimerspec cur_setting;
783 struct k_itimer *timr; 718 struct k_itimer *timr;
784 struct k_clock *kc; 719 const struct k_clock *kc;
785 unsigned long flags; 720 unsigned long flags;
786 int ret = 0; 721 int ret = 0;
787 722
@@ -789,20 +724,49 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
789 if (!timr) 724 if (!timr)
790 return -EINVAL; 725 return -EINVAL;
791 726
792 kc = clockid_to_kclock(timr->it_clock); 727 memset(setting, 0, sizeof(*setting));
728 kc = timr->kclock;
793 if (WARN_ON_ONCE(!kc || !kc->timer_get)) 729 if (WARN_ON_ONCE(!kc || !kc->timer_get))
794 ret = -EINVAL; 730 ret = -EINVAL;
795 else 731 else
796 kc->timer_get(timr, &cur_setting64); 732 kc->timer_get(timr, setting);
797 733
798 unlock_timer(timr, flags); 734 unlock_timer(timr, flags);
735 return ret;
736}
799 737
800 cur_setting = itimerspec64_to_itimerspec(&cur_setting64); 738/* Get the time remaining on a POSIX.1b interval timer. */
801 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) 739SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
802 return -EFAULT; 740 struct itimerspec __user *, setting)
741{
742 struct itimerspec64 cur_setting64;
803 743
744 int ret = do_timer_gettime(timer_id, &cur_setting64);
745 if (!ret) {
746 struct itimerspec cur_setting;
747 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
748 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
749 ret = -EFAULT;
750 }
751 return ret;
752}
753
754#ifdef CONFIG_COMPAT
755COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
756 struct compat_itimerspec __user *, setting)
757{
758 struct itimerspec64 cur_setting64;
759
760 int ret = do_timer_gettime(timer_id, &cur_setting64);
761 if (!ret) {
762 struct itimerspec cur_setting;
763 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
764 if (put_compat_itimerspec(setting, &cur_setting))
765 ret = -EFAULT;
766 }
804 return ret; 767 return ret;
805} 768}
769#endif
806 770
807/* 771/*
808 * Get the number of overruns of a POSIX.1b interval timer. This is to 772 * Get the number of overruns of a POSIX.1b interval timer. This is to
@@ -810,7 +774,7 @@ SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
810 * accumulating overruns on the next timer. The overrun is frozen when 774 * accumulating overruns on the next timer. The overrun is frozen when
811 * the signal is delivered, either at the notify time (if the info block 775 * the signal is delivered, either at the notify time (if the info block
812 * is not queued) or at the actual delivery time (as we are informed by 776 * is not queued) or at the actual delivery time (as we are informed by
813 * the call back to do_schedule_next_timer(). So all we need to do is 777 * the call back to posixtimer_rearm(). So all we need to do is
814 * to pick up the frozen overrun. 778 * to pick up the frozen overrun.
815 */ 779 */
816SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) 780SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
@@ -829,117 +793,183 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
829 return overrun; 793 return overrun;
830} 794}
831 795
832/* Set a POSIX.1b interval timer. */ 796static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
833/* timr->it_lock is taken. */ 797 bool absolute, bool sigev_none)
834static int
835common_timer_set(struct k_itimer *timr, int flags,
836 struct itimerspec64 *new_setting, struct itimerspec64 *old_setting)
837{ 798{
838 struct hrtimer *timer = &timr->it.real.timer; 799 struct hrtimer *timer = &timr->it.real.timer;
839 enum hrtimer_mode mode; 800 enum hrtimer_mode mode;
840 801
802 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
803 /*
804 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
805 * clock modifications, so they become CLOCK_MONOTONIC based under the
806 * hood. See hrtimer_init(). Update timr->kclock, so the generic
807 * functions which use timr->kclock->clock_get() work.
808 *
809 * Note: it_clock stays unmodified, because the next timer_set() might
810 * use ABSTIME, so it needs to switch back.
811 */
812 if (timr->it_clock == CLOCK_REALTIME)
813 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
814
815 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
816 timr->it.real.timer.function = posix_timer_fn;
817
818 if (!absolute)
819 expires = ktime_add_safe(expires, timer->base->get_time());
820 hrtimer_set_expires(timer, expires);
821
822 if (!sigev_none)
823 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
824}
825
826static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
827{
828 return hrtimer_try_to_cancel(&timr->it.real.timer);
829}
830
831/* Set a POSIX.1b interval timer. */
832int common_timer_set(struct k_itimer *timr, int flags,
833 struct itimerspec64 *new_setting,
834 struct itimerspec64 *old_setting)
835{
836 const struct k_clock *kc = timr->kclock;
837 bool sigev_none;
838 ktime_t expires;
839
841 if (old_setting) 840 if (old_setting)
842 common_timer_get(timr, old_setting); 841 common_timer_get(timr, old_setting);
843 842
844 /* disable the timer */ 843 /* Prevent rearming by clearing the interval */
845 timr->it.real.interval = 0; 844 timr->it_interval = 0;
846 /* 845 /*
847 * careful here. If smp we could be in the "fire" routine which will 846 * Careful here. On SMP systems the timer expiry function could be
848 * be spinning as we hold the lock. But this is ONLY an SMP issue. 847 * active and spinning on timr->it_lock.
849 */ 848 */
850 if (hrtimer_try_to_cancel(timer) < 0) 849 if (kc->timer_try_to_cancel(timr) < 0)
851 return TIMER_RETRY; 850 return TIMER_RETRY;
852 851
853 timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 852 timr->it_active = 0;
853 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
854 ~REQUEUE_PENDING; 854 ~REQUEUE_PENDING;
855 timr->it_overrun_last = 0; 855 timr->it_overrun_last = 0;
856 856
857 /* switch off the timer when it_value is zero */ 857 /* Switch off the timer when it_value is zero */
858 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 858 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
859 return 0; 859 return 0;
860 860
861 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; 861 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
862 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); 862 expires = timespec64_to_ktime(new_setting->it_value);
863 timr->it.real.timer.function = posix_timer_fn; 863 sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
864
865 hrtimer_set_expires(timer, timespec64_to_ktime(new_setting->it_value));
866
867 /* Convert interval */
868 timr->it.real.interval = timespec64_to_ktime(new_setting->it_interval);
869
870 /* SIGEV_NONE timers are not queued ! See common_timer_get */
871 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
872 /* Setup correct expiry time for relative timers */
873 if (mode == HRTIMER_MODE_REL) {
874 hrtimer_add_expires(timer, timer->base->get_time());
875 }
876 return 0;
877 }
878 864
879 hrtimer_start_expires(timer, mode); 865 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
866 timr->it_active = !sigev_none;
880 return 0; 867 return 0;
881} 868}
882 869
883/* Set a POSIX.1b interval timer */ 870static int do_timer_settime(timer_t timer_id, int flags,
884SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, 871 struct itimerspec64 *new_spec64,
885 const struct itimerspec __user *, new_setting, 872 struct itimerspec64 *old_spec64)
886 struct itimerspec __user *, old_setting)
887{ 873{
888 struct itimerspec64 new_spec64, old_spec64; 874 const struct k_clock *kc;
889 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
890 struct itimerspec new_spec, old_spec;
891 struct k_itimer *timr; 875 struct k_itimer *timr;
892 unsigned long flag; 876 unsigned long flag;
893 struct k_clock *kc;
894 int error = 0; 877 int error = 0;
895 878
896 if (!new_setting) 879 if (!timespec64_valid(&new_spec64->it_interval) ||
880 !timespec64_valid(&new_spec64->it_value))
897 return -EINVAL; 881 return -EINVAL;
898 882
899 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) 883 if (old_spec64)
900 return -EFAULT; 884 memset(old_spec64, 0, sizeof(*old_spec64));
901 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
902
903 if (!timespec64_valid(&new_spec64.it_interval) ||
904 !timespec64_valid(&new_spec64.it_value))
905 return -EINVAL;
906retry: 885retry:
907 timr = lock_timer(timer_id, &flag); 886 timr = lock_timer(timer_id, &flag);
908 if (!timr) 887 if (!timr)
909 return -EINVAL; 888 return -EINVAL;
910 889
911 kc = clockid_to_kclock(timr->it_clock); 890 kc = timr->kclock;
912 if (WARN_ON_ONCE(!kc || !kc->timer_set)) 891 if (WARN_ON_ONCE(!kc || !kc->timer_set))
913 error = -EINVAL; 892 error = -EINVAL;
914 else 893 else
915 error = kc->timer_set(timr, flags, &new_spec64, rtn); 894 error = kc->timer_set(timr, flags, new_spec64, old_spec64);
916 895
917 unlock_timer(timr, flag); 896 unlock_timer(timr, flag);
918 if (error == TIMER_RETRY) { 897 if (error == TIMER_RETRY) {
919 rtn = NULL; // We already got the old time... 898 old_spec64 = NULL; // We already got the old time...
920 goto retry; 899 goto retry;
921 } 900 }
922 901
923 old_spec = itimerspec64_to_itimerspec(&old_spec64); 902 return error;
924 if (old_setting && !error && 903}
925 copy_to_user(old_setting, &old_spec, sizeof (old_spec))) 904
926 error = -EFAULT; 905/* Set a POSIX.1b interval timer */
906SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
907 const struct itimerspec __user *, new_setting,
908 struct itimerspec __user *, old_setting)
909{
910 struct itimerspec64 new_spec64, old_spec64;
911 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
912 struct itimerspec new_spec;
913 int error = 0;
914
915 if (!new_setting)
916 return -EINVAL;
917
918 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
919 return -EFAULT;
920 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
921
922 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
923 if (!error && old_setting) {
924 struct itimerspec old_spec;
925 old_spec = itimerspec64_to_itimerspec(&old_spec64);
926 if (copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
927 error = -EFAULT;
928 }
929 return error;
930}
931
932#ifdef CONFIG_COMPAT
933COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
934 struct compat_itimerspec __user *, new,
935 struct compat_itimerspec __user *, old)
936{
937 struct itimerspec64 new_spec64, old_spec64;
938 struct itimerspec64 *rtn = old ? &old_spec64 : NULL;
939 struct itimerspec new_spec;
940 int error = 0;
941
942 if (!new)
943 return -EINVAL;
944 if (get_compat_itimerspec(&new_spec, new))
945 return -EFAULT;
927 946
947 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
948 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
949 if (!error && old) {
950 struct itimerspec old_spec;
951 old_spec = itimerspec64_to_itimerspec(&old_spec64);
952 if (put_compat_itimerspec(old, &old_spec))
953 error = -EFAULT;
954 }
928 return error; 955 return error;
929} 956}
957#endif
930 958
931static int common_timer_del(struct k_itimer *timer) 959int common_timer_del(struct k_itimer *timer)
932{ 960{
933 timer->it.real.interval = 0; 961 const struct k_clock *kc = timer->kclock;
934 962
935 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0) 963 timer->it_interval = 0;
964 if (kc->timer_try_to_cancel(timer) < 0)
936 return TIMER_RETRY; 965 return TIMER_RETRY;
966 timer->it_active = 0;
937 return 0; 967 return 0;
938} 968}
939 969
940static inline int timer_delete_hook(struct k_itimer *timer) 970static inline int timer_delete_hook(struct k_itimer *timer)
941{ 971{
942 struct k_clock *kc = clockid_to_kclock(timer->it_clock); 972 const struct k_clock *kc = timer->kclock;
943 973
944 if (WARN_ON_ONCE(!kc || !kc->timer_del)) 974 if (WARN_ON_ONCE(!kc || !kc->timer_del))
945 return -EINVAL; 975 return -EINVAL;
@@ -1018,7 +1048,7 @@ void exit_itimers(struct signal_struct *sig)
1018SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, 1048SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1019 const struct timespec __user *, tp) 1049 const struct timespec __user *, tp)
1020{ 1050{
1021 struct k_clock *kc = clockid_to_kclock(which_clock); 1051 const struct k_clock *kc = clockid_to_kclock(which_clock);
1022 struct timespec64 new_tp64; 1052 struct timespec64 new_tp64;
1023 struct timespec new_tp; 1053 struct timespec new_tp;
1024 1054
@@ -1035,7 +1065,7 @@ SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1035SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, 1065SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1036 struct timespec __user *,tp) 1066 struct timespec __user *,tp)
1037{ 1067{
1038 struct k_clock *kc = clockid_to_kclock(which_clock); 1068 const struct k_clock *kc = clockid_to_kclock(which_clock);
1039 struct timespec64 kernel_tp64; 1069 struct timespec64 kernel_tp64;
1040 struct timespec kernel_tp; 1070 struct timespec kernel_tp;
1041 int error; 1071 int error;
@@ -1055,7 +1085,7 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1055SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, 1085SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1056 struct timex __user *, utx) 1086 struct timex __user *, utx)
1057{ 1087{
1058 struct k_clock *kc = clockid_to_kclock(which_clock); 1088 const struct k_clock *kc = clockid_to_kclock(which_clock);
1059 struct timex ktx; 1089 struct timex ktx;
1060 int err; 1090 int err;
1061 1091
@@ -1078,7 +1108,7 @@ SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1078SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, 1108SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1079 struct timespec __user *, tp) 1109 struct timespec __user *, tp)
1080{ 1110{
1081 struct k_clock *kc = clockid_to_kclock(which_clock); 1111 const struct k_clock *kc = clockid_to_kclock(which_clock);
1082 struct timespec64 rtn_tp64; 1112 struct timespec64 rtn_tp64;
1083 struct timespec rtn_tp; 1113 struct timespec rtn_tp;
1084 int error; 1114 int error;
@@ -1095,13 +1125,98 @@ SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1095 return error; 1125 return error;
1096} 1126}
1097 1127
1128#ifdef CONFIG_COMPAT
1129
1130COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1131 struct compat_timespec __user *, tp)
1132{
1133 const struct k_clock *kc = clockid_to_kclock(which_clock);
1134 struct timespec64 new_tp64;
1135 struct timespec new_tp;
1136
1137 if (!kc || !kc->clock_set)
1138 return -EINVAL;
1139
1140 if (compat_get_timespec(&new_tp, tp))
1141 return -EFAULT;
1142
1143 new_tp64 = timespec_to_timespec64(new_tp);
1144
1145 return kc->clock_set(which_clock, &new_tp64);
1146}
1147
1148COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1149 struct compat_timespec __user *, tp)
1150{
1151 const struct k_clock *kc = clockid_to_kclock(which_clock);
1152 struct timespec64 kernel_tp64;
1153 struct timespec kernel_tp;
1154 int error;
1155
1156 if (!kc)
1157 return -EINVAL;
1158
1159 error = kc->clock_get(which_clock, &kernel_tp64);
1160 kernel_tp = timespec64_to_timespec(kernel_tp64);
1161
1162 if (!error && compat_put_timespec(&kernel_tp, tp))
1163 error = -EFAULT;
1164
1165 return error;
1166}
1167
1168COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1169 struct compat_timex __user *, utp)
1170{
1171 const struct k_clock *kc = clockid_to_kclock(which_clock);
1172 struct timex ktx;
1173 int err;
1174
1175 if (!kc)
1176 return -EINVAL;
1177 if (!kc->clock_adj)
1178 return -EOPNOTSUPP;
1179
1180 err = compat_get_timex(&ktx, utp);
1181 if (err)
1182 return err;
1183
1184 err = kc->clock_adj(which_clock, &ktx);
1185
1186 if (err >= 0)
1187 err = compat_put_timex(utp, &ktx);
1188
1189 return err;
1190}
1191
1192COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1193 struct compat_timespec __user *, tp)
1194{
1195 const struct k_clock *kc = clockid_to_kclock(which_clock);
1196 struct timespec64 rtn_tp64;
1197 struct timespec rtn_tp;
1198 int error;
1199
1200 if (!kc)
1201 return -EINVAL;
1202
1203 error = kc->clock_getres(which_clock, &rtn_tp64);
1204 rtn_tp = timespec64_to_timespec(rtn_tp64);
1205
1206 if (!error && tp && compat_put_timespec(&rtn_tp, tp))
1207 error = -EFAULT;
1208
1209 return error;
1210}
1211#endif
1212
1098/* 1213/*
1099 * nanosleep for monotonic and realtime clocks 1214 * nanosleep for monotonic and realtime clocks
1100 */ 1215 */
1101static int common_nsleep(const clockid_t which_clock, int flags, 1216static int common_nsleep(const clockid_t which_clock, int flags,
1102 struct timespec64 *tsave, struct timespec __user *rmtp) 1217 const struct timespec64 *rqtp)
1103{ 1218{
1104 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ? 1219 return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1105 HRTIMER_MODE_ABS : HRTIMER_MODE_REL, 1220 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1106 which_clock); 1221 which_clock);
1107} 1222}
@@ -1110,7 +1225,7 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1110 const struct timespec __user *, rqtp, 1225 const struct timespec __user *, rqtp,
1111 struct timespec __user *, rmtp) 1226 struct timespec __user *, rmtp)
1112{ 1227{
1113 struct k_clock *kc = clockid_to_kclock(which_clock); 1228 const struct k_clock *kc = clockid_to_kclock(which_clock);
1114 struct timespec64 t64; 1229 struct timespec64 t64;
1115 struct timespec t; 1230 struct timespec t;
1116 1231
@@ -1125,21 +1240,141 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1125 t64 = timespec_to_timespec64(t); 1240 t64 = timespec_to_timespec64(t);
1126 if (!timespec64_valid(&t64)) 1241 if (!timespec64_valid(&t64))
1127 return -EINVAL; 1242 return -EINVAL;
1243 if (flags & TIMER_ABSTIME)
1244 rmtp = NULL;
1245 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1246 current->restart_block.nanosleep.rmtp = rmtp;
1128 1247
1129 return kc->nsleep(which_clock, flags, &t64, rmtp); 1248 return kc->nsleep(which_clock, flags, &t64);
1130} 1249}
1131 1250
1132/* 1251#ifdef CONFIG_COMPAT
1133 * This will restart clock_nanosleep. This is required only by 1252COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1134 * compat_clock_nanosleep_restart for now. 1253 struct compat_timespec __user *, rqtp,
1135 */ 1254 struct compat_timespec __user *, rmtp)
1136long clock_nanosleep_restart(struct restart_block *restart_block)
1137{ 1255{
1138 clockid_t which_clock = restart_block->nanosleep.clockid; 1256 const struct k_clock *kc = clockid_to_kclock(which_clock);
1139 struct k_clock *kc = clockid_to_kclock(which_clock); 1257 struct timespec64 t64;
1258 struct timespec t;
1140 1259
1141 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart)) 1260 if (!kc)
1142 return -EINVAL; 1261 return -EINVAL;
1262 if (!kc->nsleep)
1263 return -ENANOSLEEP_NOTSUP;
1143 1264
1144 return kc->nsleep_restart(restart_block); 1265 if (compat_get_timespec(&t, rqtp))
1266 return -EFAULT;
1267
1268 t64 = timespec_to_timespec64(t);
1269 if (!timespec64_valid(&t64))
1270 return -EINVAL;
1271 if (flags & TIMER_ABSTIME)
1272 rmtp = NULL;
1273 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1274 current->restart_block.nanosleep.compat_rmtp = rmtp;
1275
1276 return kc->nsleep(which_clock, flags, &t64);
1277}
1278#endif
1279
1280static const struct k_clock clock_realtime = {
1281 .clock_getres = posix_get_hrtimer_res,
1282 .clock_get = posix_clock_realtime_get,
1283 .clock_set = posix_clock_realtime_set,
1284 .clock_adj = posix_clock_realtime_adj,
1285 .nsleep = common_nsleep,
1286 .timer_create = common_timer_create,
1287 .timer_set = common_timer_set,
1288 .timer_get = common_timer_get,
1289 .timer_del = common_timer_del,
1290 .timer_rearm = common_hrtimer_rearm,
1291 .timer_forward = common_hrtimer_forward,
1292 .timer_remaining = common_hrtimer_remaining,
1293 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1294 .timer_arm = common_hrtimer_arm,
1295};
1296
1297static const struct k_clock clock_monotonic = {
1298 .clock_getres = posix_get_hrtimer_res,
1299 .clock_get = posix_ktime_get_ts,
1300 .nsleep = common_nsleep,
1301 .timer_create = common_timer_create,
1302 .timer_set = common_timer_set,
1303 .timer_get = common_timer_get,
1304 .timer_del = common_timer_del,
1305 .timer_rearm = common_hrtimer_rearm,
1306 .timer_forward = common_hrtimer_forward,
1307 .timer_remaining = common_hrtimer_remaining,
1308 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1309 .timer_arm = common_hrtimer_arm,
1310};
1311
1312static const struct k_clock clock_monotonic_raw = {
1313 .clock_getres = posix_get_hrtimer_res,
1314 .clock_get = posix_get_monotonic_raw,
1315};
1316
1317static const struct k_clock clock_realtime_coarse = {
1318 .clock_getres = posix_get_coarse_res,
1319 .clock_get = posix_get_realtime_coarse,
1320};
1321
1322static const struct k_clock clock_monotonic_coarse = {
1323 .clock_getres = posix_get_coarse_res,
1324 .clock_get = posix_get_monotonic_coarse,
1325};
1326
1327static const struct k_clock clock_tai = {
1328 .clock_getres = posix_get_hrtimer_res,
1329 .clock_get = posix_get_tai,
1330 .nsleep = common_nsleep,
1331 .timer_create = common_timer_create,
1332 .timer_set = common_timer_set,
1333 .timer_get = common_timer_get,
1334 .timer_del = common_timer_del,
1335 .timer_rearm = common_hrtimer_rearm,
1336 .timer_forward = common_hrtimer_forward,
1337 .timer_remaining = common_hrtimer_remaining,
1338 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1339 .timer_arm = common_hrtimer_arm,
1340};
1341
1342static const struct k_clock clock_boottime = {
1343 .clock_getres = posix_get_hrtimer_res,
1344 .clock_get = posix_get_boottime,
1345 .nsleep = common_nsleep,
1346 .timer_create = common_timer_create,
1347 .timer_set = common_timer_set,
1348 .timer_get = common_timer_get,
1349 .timer_del = common_timer_del,
1350 .timer_rearm = common_hrtimer_rearm,
1351 .timer_forward = common_hrtimer_forward,
1352 .timer_remaining = common_hrtimer_remaining,
1353 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1354 .timer_arm = common_hrtimer_arm,
1355};
1356
1357static const struct k_clock * const posix_clocks[] = {
1358 [CLOCK_REALTIME] = &clock_realtime,
1359 [CLOCK_MONOTONIC] = &clock_monotonic,
1360 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1361 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1362 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1363 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1364 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1365 [CLOCK_BOOTTIME] = &clock_boottime,
1366 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1367 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1368 [CLOCK_TAI] = &clock_tai,
1369};
1370
1371static const struct k_clock *clockid_to_kclock(const clockid_t id)
1372{
1373 if (id < 0)
1374 return (id & CLOCKFD_MASK) == CLOCKFD ?
1375 &clock_posix_dynamic : &clock_posix_cpu;
1376
1377 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1378 return NULL;
1379 return posix_clocks[id];
1145} 1380}