35 #include <linux/slab.h>
36 #include <linux/time.h>
39 #include <asm/uaccess.h>
40 #include <linux/list.h>
42 #include <linux/compiler.h>
47 #include <linux/wait.h>
49 #include <linux/export.h>
73 static struct idr posix_timers_id;
80 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
81 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
82 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
89 # define ENANOSLEEP_NOTSUP EOPNOTSUPP
91 # define ENANOSLEEP_NOTSUP ENOTSUP
139 static int common_timer_create(
struct k_itimer *new_timer);
141 static int common_timer_set(
struct k_itimer *,
int,
149 #define lock_timer(tid, flags) \
150 ({ struct k_itimer *__timr; \
151 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
155 static inline void unlock_timer(
struct k_itimer *timr,
unsigned long flags)
157 spin_unlock_irqrestore(&timr->
it_lock, flags);
168 static int posix_clock_realtime_set(
const clockid_t which_clock,
174 static int posix_clock_realtime_adj(
const clockid_t which_clock,
205 static int posix_get_monotonic_coarse(
clockid_t which_clock,
212 static int posix_get_coarse_res(
const clockid_t which_clock,
struct timespec *tp)
228 static __init int init_posix_timers(
void)
230 struct k_clock clock_realtime = {
232 .clock_get = posix_clock_realtime_get,
233 .clock_set = posix_clock_realtime_set,
234 .clock_adj = posix_clock_realtime_adj,
235 .nsleep = common_nsleep,
237 .timer_create = common_timer_create,
238 .timer_set = common_timer_set,
239 .timer_get = common_timer_get,
240 .timer_del = common_timer_del,
242 struct k_clock clock_monotonic = {
244 .clock_get = posix_ktime_get_ts,
245 .nsleep = common_nsleep,
247 .timer_create = common_timer_create,
248 .timer_set = common_timer_set,
249 .timer_get = common_timer_get,
250 .timer_del = common_timer_del,
252 struct k_clock clock_monotonic_raw = {
254 .clock_get = posix_get_monotonic_raw,
256 struct k_clock clock_realtime_coarse = {
258 .clock_get = posix_get_realtime_coarse,
260 struct k_clock clock_monotonic_coarse = {
262 .clock_get = posix_get_monotonic_coarse,
264 struct k_clock clock_boottime = {
266 .clock_get = posix_get_boottime,
267 .nsleep = common_nsleep,
269 .timer_create = common_timer_create,
270 .timer_set = common_timer_set,
271 .timer_get = common_timer_get,
272 .timer_del = common_timer_del,
291 static void schedule_next_timer(
struct k_itimer *timr)
295 if (timr->
it.
real.interval.tv64 == 0)
299 timer->
base->get_time(),
330 schedule_next_timer(timr);
336 unlock_timer(timr, flags);
354 timr->
sigq->info.si_sys_private = si_private;
385 if (timr->
it.
real.interval.tv64 != 0)
394 if (timr->
it.
real.interval.tv64 != 0) {
395 ktime_t now = hrtimer_cb_get_time(timer);
419 #ifdef CONFIG_HIGH_RES_TIMERS
424 now = ktime_add(now, kj);
435 unlock_timer(timr, flags);
445 !same_thread_group(rtn,
current) ||
453 return task_pid(rtn);
476 posix_clocks[clock_id] = *new_clock;
480 static struct k_itimer * alloc_posix_timer(
void)
483 tmr = kmem_cache_zalloc(posix_timers_cache,
GFP_KERNEL);
502 #define IT_ID_NOT_SET 0
503 static void release_posix_timer(
struct k_itimer *tmr,
int it_id_set)
509 spin_unlock_irqrestore(&idr_lock, flags);
524 return &posix_clocks[
id];
527 static int common_timer_create(
struct k_itimer *new_timer)
536 struct sigevent __user *, timer_event_spec,
537 timer_t __user *, created_timer_id)
539 struct k_clock *
kc = clockid_to_kclock(which_clock);
541 int error, new_timer_id;
550 new_timer = alloc_posix_timer();
560 spin_lock_irq(&idr_lock);
561 error =
idr_get_new(&posix_timers_id, new_timer, &new_timer_id);
562 spin_unlock_irq(&idr_lock);
579 if (timer_event_spec) {
585 new_timer->
it_pid = get_pid(good_sigevent(&event));
594 event.sigev_value.sival_int = new_timer->
it_id;
599 new_timer->
sigq->info.si_signo =
event.sigev_signo;
600 new_timer->
sigq->info.si_value =
event.sigev_value;
601 new_timer->
sigq->info.si_tid = new_timer->
it_id;
605 &new_timer_id,
sizeof (new_timer_id))) {
614 spin_lock_irq(&
current->sighand->siglock);
616 list_add(&new_timer->
list, &
current->signal->posix_timers);
617 spin_unlock_irq(&
current->sighand->siglock);
627 release_posix_timer(new_timer, it_id_set);
638 static struct k_itimer *__lock_timer(
timer_t timer_id,
unsigned long *flags)
643 timr =
idr_find(&posix_timers_id, (
int)timer_id);
650 spin_unlock_irqrestore(&timr->
it_lock, *flags);
681 iv = timr->
it.
real.interval;
686 else if (!hrtimer_active(timer) &&
690 now = timer->
base->get_time();
701 remaining = ktime_sub(hrtimer_get_expires(timer), now);
703 if (remaining.
tv64 <= 0) {
711 cur_setting->
it_value = ktime_to_timespec(remaining);
728 kc = clockid_to_kclock(timr->
it_clock);
734 unlock_timer(timr, flags);
736 if (!ret &&
copy_to_user(setting, &cur_setting,
sizeof (cur_setting)))
762 unlock_timer(timr, flags);
770 common_timer_set(
struct k_itimer *timr,
int flags,
777 common_timer_get(timr, old_setting);
780 timr->
it.
real.interval.tv64 = 0;
798 timr->
it.
real.timer.function = posix_timer_fn;
800 hrtimer_set_expires(timer, timespec_to_ktime(new_setting->
it_value));
809 hrtimer_add_expires(timer, timer->
base->get_time());
814 hrtimer_start_expires(timer, mode);
820 const struct itimerspec __user *, new_setting,
837 !timespec_valid(&new_spec.
it_value))
844 kc = clockid_to_kclock(timr->
it_clock);
848 error = kc->
timer_set(timr, flags, &new_spec, rtn);
850 unlock_timer(timr, flag);
856 if (old_setting && !error &&
857 copy_to_user(old_setting, &old_spec,
sizeof (old_spec)))
863 static int common_timer_del(
struct k_itimer *timer)
865 timer->
it.
real.interval.tv64 = 0;
872 static inline int timer_delete_hook(
struct k_itimer *timer)
893 unlock_timer(timer, flags);
897 spin_lock(&
current->sighand->siglock);
899 spin_unlock(&
current->sighand->siglock);
906 unlock_timer(timer, flags);
914 static void itimer_delete(
struct k_itimer *timer)
922 unlock_timer(timer, flags);
932 unlock_timer(timer, flags);
953 struct k_clock *kc = clockid_to_kclock(which_clock);
962 return kc->
clock_set(which_clock, &new_tp);
968 struct k_clock *kc = clockid_to_kclock(which_clock);
975 error = kc->
clock_get(which_clock, &kernel_tp);
977 if (!error &&
copy_to_user(tp, &kernel_tp,
sizeof (kernel_tp)))
984 struct timex __user *, utx)
986 struct k_clock *kc = clockid_to_kclock(which_clock);
1009 struct k_clock *kc = clockid_to_kclock(which_clock);
1018 if (!error && tp &&
copy_to_user(tp, &rtn_tp,
sizeof (rtn_tp)))
1027 static int common_nsleep(
const clockid_t which_clock,
int flags,
1036 const struct timespec __user *, rqtp,
1039 struct k_clock *kc = clockid_to_kclock(which_clock);
1050 if (!timespec_valid(&t))
1053 return kc->
nsleep(which_clock, flags, &t, rmtp);
1063 struct k_clock *kc = clockid_to_kclock(which_clock);