]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/time/posix-cpu-timers.c
timers/posix-timers: Convert internals to use nsecs
[linux.git] / kernel / time / posix-cpu-timers.c
1 /*
2  * Implement CPU time clocks for the POSIX clock interface.
3  */
4
5 #include <linux/sched.h>
6 #include <linux/posix-timers.h>
7 #include <linux/errno.h>
8 #include <linux/math64.h>
9 #include <linux/uaccess.h>
10 #include <linux/kernel_stat.h>
11 #include <trace/events/timer.h>
12 #include <linux/tick.h>
13 #include <linux/workqueue.h>
14
15 /*
16  * Called after updating RLIMIT_CPU to run cpu timer and update
17  * tsk->signal->cputime_expires expiration cache if necessary. Needs
18  * siglock protection since other code may update expiration cache as
19  * well.
20  */
21 void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
22 {
23         cputime_t cputime = secs_to_cputime(rlim_new);
24
25         spin_lock_irq(&task->sighand->siglock);
26         set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL);
27         spin_unlock_irq(&task->sighand->siglock);
28 }
29
30 static int check_clock(const clockid_t which_clock)
31 {
32         int error = 0;
33         struct task_struct *p;
34         const pid_t pid = CPUCLOCK_PID(which_clock);
35
36         if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
37                 return -EINVAL;
38
39         if (pid == 0)
40                 return 0;
41
42         rcu_read_lock();
43         p = find_task_by_vpid(pid);
44         if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
45                    same_thread_group(p, current) : has_group_leader_pid(p))) {
46                 error = -EINVAL;
47         }
48         rcu_read_unlock();
49
50         return error;
51 }
52
53 /*
54  * Update expiry time from increment, and increase overrun count,
55  * given the current clock sample.
56  */
57 static void bump_cpu_timer(struct k_itimer *timer, u64 now)
58 {
59         int i;
60         u64 delta, incr;
61
62         if (timer->it.cpu.incr == 0)
63                 return;
64
65         if (now < timer->it.cpu.expires)
66                 return;
67
68         incr = timer->it.cpu.incr;
69         delta = now + incr - timer->it.cpu.expires;
70
71         /* Don't use (incr*2 < delta), incr*2 might overflow. */
72         for (i = 0; incr < delta - incr; i++)
73                 incr = incr << 1;
74
75         for (; i >= 0; incr >>= 1, i--) {
76                 if (delta < incr)
77                         continue;
78
79                 timer->it.cpu.expires += incr;
80                 timer->it_overrun += 1 << i;
81                 delta -= incr;
82         }
83 }
84
85 /**
86  * task_cputime_zero - Check a task_cputime struct for all zero fields.
87  *
88  * @cputime:    The struct to compare.
89  *
90  * Checks @cputime to see if all fields are zero.  Returns true if all fields
91  * are zero, false if any field is nonzero.
92  */
93 static inline int task_cputime_zero(const struct task_cputime *cputime)
94 {
95         if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
96                 return 1;
97         return 0;
98 }
99
100 static inline u64 prof_ticks(struct task_struct *p)
101 {
102         u64 utime, stime;
103
104         task_cputime(p, &utime, &stime);
105
106         return utime + stime;
107 }
108 static inline u64 virt_ticks(struct task_struct *p)
109 {
110         u64 utime, stime;
111
112         task_cputime(p, &utime, &stime);
113
114         return utime;
115 }
116
117 static int
118 posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
119 {
120         int error = check_clock(which_clock);
121         if (!error) {
122                 tp->tv_sec = 0;
123                 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
124                 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
125                         /*
126                          * If sched_clock is using a cycle counter, we
127                          * don't have any idea of its true resolution
128                          * exported, but it is much more than 1s/HZ.
129                          */
130                         tp->tv_nsec = 1;
131                 }
132         }
133         return error;
134 }
135
136 static int
137 posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
138 {
139         /*
140          * You can never reset a CPU clock, but we check for other errors
141          * in the call before failing with EPERM.
142          */
143         int error = check_clock(which_clock);
144         if (error == 0) {
145                 error = -EPERM;
146         }
147         return error;
148 }
149
150
151 /*
152  * Sample a per-thread clock for the given task.
153  */
154 static int cpu_clock_sample(const clockid_t which_clock,
155                             struct task_struct *p, u64 *sample)
156 {
157         switch (CPUCLOCK_WHICH(which_clock)) {
158         default:
159                 return -EINVAL;
160         case CPUCLOCK_PROF:
161                 *sample = prof_ticks(p);
162                 break;
163         case CPUCLOCK_VIRT:
164                 *sample = virt_ticks(p);
165                 break;
166         case CPUCLOCK_SCHED:
167                 *sample = task_sched_runtime(p);
168                 break;
169         }
170         return 0;
171 }
172
173 /*
174  * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
175  * to avoid race conditions with concurrent updates to cputime.
176  */
177 static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
178 {
179         u64 curr_cputime;
180 retry:
181         curr_cputime = atomic64_read(cputime);
182         if (sum_cputime > curr_cputime) {
183                 if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
184                         goto retry;
185         }
186 }
187
188 static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum)
189 {
190         __update_gt_cputime(&cputime_atomic->utime, sum->utime);
191         __update_gt_cputime(&cputime_atomic->stime, sum->stime);
192         __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
193 }
194
195 /* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */
196 static inline void sample_cputime_atomic(struct task_cputime *times,
197                                          struct task_cputime_atomic *atomic_times)
198 {
199         times->utime = atomic64_read(&atomic_times->utime);
200         times->stime = atomic64_read(&atomic_times->stime);
201         times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime);
202 }
203
204 void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
205 {
206         struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
207         struct task_cputime sum;
208
209         /* Check if cputimer isn't running. This is accessed without locking. */
210         if (!READ_ONCE(cputimer->running)) {
211                 /*
212                  * The POSIX timer interface allows for absolute time expiry
213                  * values through the TIMER_ABSTIME flag, therefore we have
214                  * to synchronize the timer to the clock every time we start it.
215                  */
216                 thread_group_cputime(tsk, &sum);
217                 update_gt_cputime(&cputimer->cputime_atomic, &sum);
218
219                 /*
220                  * We're setting cputimer->running without a lock. Ensure
221                  * this only gets written to in one operation. We set
222                  * running after update_gt_cputime() as a small optimization,
223                  * but barriers are not required because update_gt_cputime()
224                  * can handle concurrent updates.
225                  */
226                 WRITE_ONCE(cputimer->running, true);
227         }
228         sample_cputime_atomic(times, &cputimer->cputime_atomic);
229 }
230
231 /*
232  * Sample a process (thread group) clock for the given group_leader task.
233  * Must be called with task sighand lock held for safe while_each_thread()
234  * traversal.
235  */
236 static int cpu_clock_sample_group(const clockid_t which_clock,
237                                   struct task_struct *p,
238                                   u64 *sample)
239 {
240         struct task_cputime cputime;
241
242         switch (CPUCLOCK_WHICH(which_clock)) {
243         default:
244                 return -EINVAL;
245         case CPUCLOCK_PROF:
246                 thread_group_cputime(p, &cputime);
247                 *sample = cputime.utime + cputime.stime;
248                 break;
249         case CPUCLOCK_VIRT:
250                 thread_group_cputime(p, &cputime);
251                 *sample = cputime.utime;
252                 break;
253         case CPUCLOCK_SCHED:
254                 thread_group_cputime(p, &cputime);
255                 *sample = cputime.sum_exec_runtime;
256                 break;
257         }
258         return 0;
259 }
260
261 static int posix_cpu_clock_get_task(struct task_struct *tsk,
262                                     const clockid_t which_clock,
263                                     struct timespec *tp)
264 {
265         int err = -EINVAL;
266         u64 rtn;
267
268         if (CPUCLOCK_PERTHREAD(which_clock)) {
269                 if (same_thread_group(tsk, current))
270                         err = cpu_clock_sample(which_clock, tsk, &rtn);
271         } else {
272                 if (tsk == current || thread_group_leader(tsk))
273                         err = cpu_clock_sample_group(which_clock, tsk, &rtn);
274         }
275
276         if (!err)
277                 *tp = ns_to_timespec(rtn);
278
279         return err;
280 }
281
282
283 static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
284 {
285         const pid_t pid = CPUCLOCK_PID(which_clock);
286         int err = -EINVAL;
287
288         if (pid == 0) {
289                 /*
290                  * Special case constant value for our own clocks.
291                  * We don't have to do any lookup to find ourselves.
292                  */
293                 err = posix_cpu_clock_get_task(current, which_clock, tp);
294         } else {
295                 /*
296                  * Find the given PID, and validate that the caller
297                  * should be able to see it.
298                  */
299                 struct task_struct *p;
300                 rcu_read_lock();
301                 p = find_task_by_vpid(pid);
302                 if (p)
303                         err = posix_cpu_clock_get_task(p, which_clock, tp);
304                 rcu_read_unlock();
305         }
306
307         return err;
308 }
309
310 /*
311  * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
312  * This is called from sys_timer_create() and do_cpu_nanosleep() with the
313  * new timer already all-zeros initialized.
314  */
315 static int posix_cpu_timer_create(struct k_itimer *new_timer)
316 {
317         int ret = 0;
318         const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
319         struct task_struct *p;
320
321         if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
322                 return -EINVAL;
323
324         INIT_LIST_HEAD(&new_timer->it.cpu.entry);
325
326         rcu_read_lock();
327         if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
328                 if (pid == 0) {
329                         p = current;
330                 } else {
331                         p = find_task_by_vpid(pid);
332                         if (p && !same_thread_group(p, current))
333                                 p = NULL;
334                 }
335         } else {
336                 if (pid == 0) {
337                         p = current->group_leader;
338                 } else {
339                         p = find_task_by_vpid(pid);
340                         if (p && !has_group_leader_pid(p))
341                                 p = NULL;
342                 }
343         }
344         new_timer->it.cpu.task = p;
345         if (p) {
346                 get_task_struct(p);
347         } else {
348                 ret = -EINVAL;
349         }
350         rcu_read_unlock();
351
352         return ret;
353 }
354
355 /*
356  * Clean up a CPU-clock timer that is about to be destroyed.
357  * This is called from timer deletion with the timer already locked.
358  * If we return TIMER_RETRY, it's necessary to release the timer's lock
359  * and try again.  (This happens when the timer is in the middle of firing.)
360  */
361 static int posix_cpu_timer_del(struct k_itimer *timer)
362 {
363         int ret = 0;
364         unsigned long flags;
365         struct sighand_struct *sighand;
366         struct task_struct *p = timer->it.cpu.task;
367
368         WARN_ON_ONCE(p == NULL);
369
370         /*
371          * Protect against sighand release/switch in exit/exec and process/
372          * thread timer list entry concurrent read/writes.
373          */
374         sighand = lock_task_sighand(p, &flags);
375         if (unlikely(sighand == NULL)) {
376                 /*
377                  * We raced with the reaping of the task.
378                  * The deletion should have cleared us off the list.
379                  */
380                 WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
381         } else {
382                 if (timer->it.cpu.firing)
383                         ret = TIMER_RETRY;
384                 else
385                         list_del(&timer->it.cpu.entry);
386
387                 unlock_task_sighand(p, &flags);
388         }
389
390         if (!ret)
391                 put_task_struct(p);
392
393         return ret;
394 }
395
396 static void cleanup_timers_list(struct list_head *head)
397 {
398         struct cpu_timer_list *timer, *next;
399
400         list_for_each_entry_safe(timer, next, head, entry)
401                 list_del_init(&timer->entry);
402 }
403
404 /*
405  * Clean out CPU timers still ticking when a thread exited.  The task
406  * pointer is cleared, and the expiry time is replaced with the residual
407  * time for later timer_gettime calls to return.
408  * This must be called with the siglock held.
409  */
410 static void cleanup_timers(struct list_head *head)
411 {
412         cleanup_timers_list(head);
413         cleanup_timers_list(++head);
414         cleanup_timers_list(++head);
415 }
416
417 /*
418  * These are both called with the siglock held, when the current thread
419  * is being reaped.  When the final (leader) thread in the group is reaped,
420  * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
421  */
422 void posix_cpu_timers_exit(struct task_struct *tsk)
423 {
424         cleanup_timers(tsk->cpu_timers);
425 }
426 void posix_cpu_timers_exit_group(struct task_struct *tsk)
427 {
428         cleanup_timers(tsk->signal->cpu_timers);
429 }
430
431 static inline int expires_gt(u64 expires, u64 new_exp)
432 {
433         return expires == 0 || expires > new_exp;
434 }
435
436 /*
437  * Insert the timer on the appropriate list before any timers that
438  * expire later.  This must be called with the sighand lock held.
439  */
440 static void arm_timer(struct k_itimer *timer)
441 {
442         struct task_struct *p = timer->it.cpu.task;
443         struct list_head *head, *listpos;
444         struct task_cputime *cputime_expires;
445         struct cpu_timer_list *const nt = &timer->it.cpu;
446         struct cpu_timer_list *next;
447
448         if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
449                 head = p->cpu_timers;
450                 cputime_expires = &p->cputime_expires;
451         } else {
452                 head = p->signal->cpu_timers;
453                 cputime_expires = &p->signal->cputime_expires;
454         }
455         head += CPUCLOCK_WHICH(timer->it_clock);
456
457         listpos = head;
458         list_for_each_entry(next, head, entry) {
459                 if (nt->expires < next->expires)
460                         break;
461                 listpos = &next->entry;
462         }
463         list_add(&nt->entry, listpos);
464
465         if (listpos == head) {
466                 u64 exp = nt->expires;
467
468                 /*
469                  * We are the new earliest-expiring POSIX 1.b timer, hence
470                  * need to update expiration cache. Take into account that
471                  * for process timers we share expiration cache with itimers
472                  * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
473                  */
474
475                 switch (CPUCLOCK_WHICH(timer->it_clock)) {
476                 case CPUCLOCK_PROF:
477                         if (expires_gt(cputime_expires->prof_exp, exp))
478                                 cputime_expires->prof_exp = exp;
479                         break;
480                 case CPUCLOCK_VIRT:
481                         if (expires_gt(cputime_expires->virt_exp, exp))
482                                 cputime_expires->virt_exp = exp;
483                         break;
484                 case CPUCLOCK_SCHED:
485                         if (expires_gt(cputime_expires->sched_exp, exp))
486                                 cputime_expires->sched_exp = exp;
487                         break;
488                 }
489                 if (CPUCLOCK_PERTHREAD(timer->it_clock))
490                         tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
491                 else
492                         tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
493         }
494 }
495
496 /*
497  * The timer is locked, fire it and arrange for its reload.
498  */
499 static void cpu_timer_fire(struct k_itimer *timer)
500 {
501         if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
502                 /*
503                  * User don't want any signal.
504                  */
505                 timer->it.cpu.expires = 0;
506         } else if (unlikely(timer->sigq == NULL)) {
507                 /*
508                  * This a special case for clock_nanosleep,
509                  * not a normal timer from sys_timer_create.
510                  */
511                 wake_up_process(timer->it_process);
512                 timer->it.cpu.expires = 0;
513         } else if (timer->it.cpu.incr == 0) {
514                 /*
515                  * One-shot timer.  Clear it as soon as it's fired.
516                  */
517                 posix_timer_event(timer, 0);
518                 timer->it.cpu.expires = 0;
519         } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
520                 /*
521                  * The signal did not get queued because the signal
522                  * was ignored, so we won't get any callback to
523                  * reload the timer.  But we need to keep it
524                  * ticking in case the signal is deliverable next time.
525                  */
526                 posix_cpu_timer_schedule(timer);
527         }
528 }
529
530 /*
531  * Sample a process (thread group) timer for the given group_leader task.
532  * Must be called with task sighand lock held for safe while_each_thread()
533  * traversal.
534  */
535 static int cpu_timer_sample_group(const clockid_t which_clock,
536                                   struct task_struct *p, u64 *sample)
537 {
538         struct task_cputime cputime;
539
540         thread_group_cputimer(p, &cputime);
541         switch (CPUCLOCK_WHICH(which_clock)) {
542         default:
543                 return -EINVAL;
544         case CPUCLOCK_PROF:
545                 *sample = cputime.utime + cputime.stime;
546                 break;
547         case CPUCLOCK_VIRT:
548                 *sample = cputime.utime;
549                 break;
550         case CPUCLOCK_SCHED:
551                 *sample = cputime.sum_exec_runtime;
552                 break;
553         }
554         return 0;
555 }
556
557 /*
558  * Guts of sys_timer_settime for CPU timers.
559  * This is called with the timer locked and interrupts disabled.
560  * If we return TIMER_RETRY, it's necessary to release the timer's lock
561  * and try again.  (This happens when the timer is in the middle of firing.)
562  */
563 static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
564                                struct itimerspec *new, struct itimerspec *old)
565 {
566         unsigned long flags;
567         struct sighand_struct *sighand;
568         struct task_struct *p = timer->it.cpu.task;
569         u64 old_expires, new_expires, old_incr, val;
570         int ret;
571
572         WARN_ON_ONCE(p == NULL);
573
574         new_expires = timespec_to_ns(&new->it_value);
575
576         /*
577          * Protect against sighand release/switch in exit/exec and p->cpu_timers
578          * and p->signal->cpu_timers read/write in arm_timer()
579          */
580         sighand = lock_task_sighand(p, &flags);
581         /*
582          * If p has just been reaped, we can no
583          * longer get any information about it at all.
584          */
585         if (unlikely(sighand == NULL)) {
586                 return -ESRCH;
587         }
588
589         /*
590          * Disarm any old timer after extracting its expiry time.
591          */
592         WARN_ON_ONCE(!irqs_disabled());
593
594         ret = 0;
595         old_incr = timer->it.cpu.incr;
596         old_expires = timer->it.cpu.expires;
597         if (unlikely(timer->it.cpu.firing)) {
598                 timer->it.cpu.firing = -1;
599                 ret = TIMER_RETRY;
600         } else
601                 list_del_init(&timer->it.cpu.entry);
602
603         /*
604          * We need to sample the current value to convert the new
605          * value from to relative and absolute, and to convert the
606          * old value from absolute to relative.  To set a process
607          * timer, we need a sample to balance the thread expiry
608          * times (in arm_timer).  With an absolute time, we must
609          * check if it's already passed.  In short, we need a sample.
610          */
611         if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
612                 cpu_clock_sample(timer->it_clock, p, &val);
613         } else {
614                 cpu_timer_sample_group(timer->it_clock, p, &val);
615         }
616
617         if (old) {
618                 if (old_expires == 0) {
619                         old->it_value.tv_sec = 0;
620                         old->it_value.tv_nsec = 0;
621                 } else {
622                         /*
623                          * Update the timer in case it has
624                          * overrun already.  If it has,
625                          * we'll report it as having overrun
626                          * and with the next reloaded timer
627                          * already ticking, though we are
628                          * swallowing that pending
629                          * notification here to install the
630                          * new setting.
631                          */
632                         bump_cpu_timer(timer, val);
633                         if (val < timer->it.cpu.expires) {
634                                 old_expires = timer->it.cpu.expires - val;
635                                 old->it_value = ns_to_timespec(old_expires);
636                         } else {
637                                 old->it_value.tv_nsec = 1;
638                                 old->it_value.tv_sec = 0;
639                         }
640                 }
641         }
642
643         if (unlikely(ret)) {
644                 /*
645                  * We are colliding with the timer actually firing.
646                  * Punt after filling in the timer's old value, and
647                  * disable this firing since we are already reporting
648                  * it as an overrun (thanks to bump_cpu_timer above).
649                  */
650                 unlock_task_sighand(p, &flags);
651                 goto out;
652         }
653
654         if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
655                 new_expires += val;
656         }
657
658         /*
659          * Install the new expiry time (or zero).
660          * For a timer with no notification action, we don't actually
661          * arm the timer (we'll just fake it for timer_gettime).
662          */
663         timer->it.cpu.expires = new_expires;
664         if (new_expires != 0 && val < new_expires) {
665                 arm_timer(timer);
666         }
667
668         unlock_task_sighand(p, &flags);
669         /*
670          * Install the new reload setting, and
671          * set up the signal and overrun bookkeeping.
672          */
673         timer->it.cpu.incr = timespec_to_ns(&new->it_interval);
674
675         /*
676          * This acts as a modification timestamp for the timer,
677          * so any automatic reload attempt will punt on seeing
678          * that we have reset the timer manually.
679          */
680         timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
681                 ~REQUEUE_PENDING;
682         timer->it_overrun_last = 0;
683         timer->it_overrun = -1;
684
685         if (new_expires != 0 && !(val < new_expires)) {
686                 /*
687                  * The designated time already passed, so we notify
688                  * immediately, even if the thread never runs to
689                  * accumulate more time on this clock.
690                  */
691                 cpu_timer_fire(timer);
692         }
693
694         ret = 0;
695  out:
696         if (old)
697                 old->it_interval = ns_to_timespec(old_incr);
698
699         return ret;
700 }
701
702 static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
703 {
704         u64 now;
705         struct task_struct *p = timer->it.cpu.task;
706
707         WARN_ON_ONCE(p == NULL);
708
709         /*
710          * Easy part: convert the reload time.
711          */
712         itp->it_interval = ns_to_timespec(timer->it.cpu.incr);
713
714         if (timer->it.cpu.expires == 0) {       /* Timer not armed at all.  */
715                 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
716                 return;
717         }
718
719         /*
720          * Sample the clock to take the difference with the expiry time.
721          */
722         if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
723                 cpu_clock_sample(timer->it_clock, p, &now);
724         } else {
725                 struct sighand_struct *sighand;
726                 unsigned long flags;
727
728                 /*
729                  * Protect against sighand release/switch in exit/exec and
730                  * also make timer sampling safe if it ends up calling
731                  * thread_group_cputime().
732                  */
733                 sighand = lock_task_sighand(p, &flags);
734                 if (unlikely(sighand == NULL)) {
735                         /*
736                          * The process has been reaped.
737                          * We can't even collect a sample any more.
738                          * Call the timer disarmed, nothing else to do.
739                          */
740                         timer->it.cpu.expires = 0;
741                         itp->it_value = ns_to_timespec(timer->it.cpu.expires);
742                         return;
743                 } else {
744                         cpu_timer_sample_group(timer->it_clock, p, &now);
745                         unlock_task_sighand(p, &flags);
746                 }
747         }
748
749         if (now < timer->it.cpu.expires) {
750                 itp->it_value = ns_to_timespec(timer->it.cpu.expires - now);
751         } else {
752                 /*
753                  * The timer should have expired already, but the firing
754                  * hasn't taken place yet.  Say it's just about to expire.
755                  */
756                 itp->it_value.tv_nsec = 1;
757                 itp->it_value.tv_sec = 0;
758         }
759 }
760
761 static unsigned long long
762 check_timers_list(struct list_head *timers,
763                   struct list_head *firing,
764                   unsigned long long curr)
765 {
766         int maxfire = 20;
767
768         while (!list_empty(timers)) {
769                 struct cpu_timer_list *t;
770
771                 t = list_first_entry(timers, struct cpu_timer_list, entry);
772
773                 if (!--maxfire || curr < t->expires)
774                         return t->expires;
775
776                 t->firing = 1;
777                 list_move_tail(&t->entry, firing);
778         }
779
780         return 0;
781 }
782
783 /*
784  * Check for any per-thread CPU timers that have fired and move them off
785  * the tsk->cpu_timers[N] list onto the firing list.  Here we update the
786  * tsk->it_*_expires values to reflect the remaining thread CPU timers.
787  */
788 static void check_thread_timers(struct task_struct *tsk,
789                                 struct list_head *firing)
790 {
791         struct list_head *timers = tsk->cpu_timers;
792         struct signal_struct *const sig = tsk->signal;
793         struct task_cputime *tsk_expires = &tsk->cputime_expires;
794         u64 expires;
795         unsigned long soft;
796
797         /*
798          * If cputime_expires is zero, then there are no active
799          * per thread CPU timers.
800          */
801         if (task_cputime_zero(&tsk->cputime_expires))
802                 return;
803
804         expires = check_timers_list(timers, firing, prof_ticks(tsk));
805         tsk_expires->prof_exp = expires;
806
807         expires = check_timers_list(++timers, firing, virt_ticks(tsk));
808         tsk_expires->virt_exp = expires;
809
810         tsk_expires->sched_exp = check_timers_list(++timers, firing,
811                                                    tsk->se.sum_exec_runtime);
812
813         /*
814          * Check for the special case thread timers.
815          */
816         soft = READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
817         if (soft != RLIM_INFINITY) {
818                 unsigned long hard =
819                         READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
820
821                 if (hard != RLIM_INFINITY &&
822                     tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
823                         /*
824                          * At the hard limit, we just die.
825                          * No need to calculate anything else now.
826                          */
827                         __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
828                         return;
829                 }
830                 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
831                         /*
832                          * At the soft limit, send a SIGXCPU every second.
833                          */
834                         if (soft < hard) {
835                                 soft += USEC_PER_SEC;
836                                 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
837                         }
838                         printk(KERN_INFO
839                                 "RT Watchdog Timeout: %s[%d]\n",
840                                 tsk->comm, task_pid_nr(tsk));
841                         __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
842                 }
843         }
844         if (task_cputime_zero(tsk_expires))
845                 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
846 }
847
848 static inline void stop_process_timers(struct signal_struct *sig)
849 {
850         struct thread_group_cputimer *cputimer = &sig->cputimer;
851
852         /* Turn off cputimer->running. This is done without locking. */
853         WRITE_ONCE(cputimer->running, false);
854         tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
855 }
856
857 static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
858                              u64 *expires, u64 cur_time, int signo)
859 {
860         if (!it->expires)
861                 return;
862
863         if (cur_time >= cputime_to_nsecs(it->expires)) {
864                 if (it->incr) {
865                         it->expires += it->incr;
866                         it->error += it->incr_error;
867                         if (it->error >= TICK_NSEC) {
868                                 it->expires -= cputime_one_jiffy;
869                                 it->error -= TICK_NSEC;
870                         }
871                 } else {
872                         it->expires = 0;
873                 }
874
875                 trace_itimer_expire(signo == SIGPROF ?
876                                     ITIMER_PROF : ITIMER_VIRTUAL,
877                                     tsk->signal->leader_pid, cur_time);
878                 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
879         }
880
881         if (it->expires && (!*expires || cputime_to_nsecs(it->expires) < *expires)) {
882                 *expires = cputime_to_nsecs(it->expires);
883         }
884 }
885
886 /*
887  * Check for any per-thread CPU timers that have fired and move them
888  * off the tsk->*_timers list onto the firing list.  Per-thread timers
889  * have already been taken off.
890  */
891 static void check_process_timers(struct task_struct *tsk,
892                                  struct list_head *firing)
893 {
894         struct signal_struct *const sig = tsk->signal;
895         u64 utime, ptime, virt_expires, prof_expires;
896         u64 sum_sched_runtime, sched_expires;
897         struct list_head *timers = sig->cpu_timers;
898         struct task_cputime cputime;
899         unsigned long soft;
900
901         /*
902          * If cputimer is not running, then there are no active
903          * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
904          */
905         if (!READ_ONCE(tsk->signal->cputimer.running))
906                 return;
907
908         /*
909          * Signify that a thread is checking for process timers.
910          * Write access to this field is protected by the sighand lock.
911          */
912         sig->cputimer.checking_timer = true;
913
914         /*
915          * Collect the current process totals.
916          */
917         thread_group_cputimer(tsk, &cputime);
918         utime = cputime.utime;
919         ptime = utime + cputime.stime;
920         sum_sched_runtime = cputime.sum_exec_runtime;
921
922         prof_expires = check_timers_list(timers, firing, ptime);
923         virt_expires = check_timers_list(++timers, firing, utime);
924         sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
925
926         /*
927          * Check for the special case process timers.
928          */
929         check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
930                          SIGPROF);
931         check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
932                          SIGVTALRM);
933         soft = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
934         if (soft != RLIM_INFINITY) {
935                 unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
936                 unsigned long hard =
937                         READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
938                 u64 x;
939                 if (psecs >= hard) {
940                         /*
941                          * At the hard limit, we just die.
942                          * No need to calculate anything else now.
943                          */
944                         __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
945                         return;
946                 }
947                 if (psecs >= soft) {
948                         /*
949                          * At the soft limit, send a SIGXCPU every second.
950                          */
951                         __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
952                         if (soft < hard) {
953                                 soft++;
954                                 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
955                         }
956                 }
957                 x = soft * NSEC_PER_SEC;
958                 if (!prof_expires || x < prof_expires)
959                         prof_expires = x;
960         }
961
962         sig->cputime_expires.prof_exp = prof_expires;
963         sig->cputime_expires.virt_exp = virt_expires;
964         sig->cputime_expires.sched_exp = sched_expires;
965         if (task_cputime_zero(&sig->cputime_expires))
966                 stop_process_timers(sig);
967
968         sig->cputimer.checking_timer = false;
969 }
970
971 /*
972  * This is called from the signal code (via do_schedule_next_timer)
973  * when the last timer signal was delivered and we have to reload the timer.
974  */
975 void posix_cpu_timer_schedule(struct k_itimer *timer)
976 {
977         struct sighand_struct *sighand;
978         unsigned long flags;
979         struct task_struct *p = timer->it.cpu.task;
980         u64 now;
981
982         WARN_ON_ONCE(p == NULL);
983
984         /*
985          * Fetch the current sample and update the timer's expiry time.
986          */
987         if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
988                 cpu_clock_sample(timer->it_clock, p, &now);
989                 bump_cpu_timer(timer, now);
990                 if (unlikely(p->exit_state))
991                         goto out;
992
993                 /* Protect timer list r/w in arm_timer() */
994                 sighand = lock_task_sighand(p, &flags);
995                 if (!sighand)
996                         goto out;
997         } else {
998                 /*
999                  * Protect arm_timer() and timer sampling in case of call to
1000                  * thread_group_cputime().
1001                  */
1002                 sighand = lock_task_sighand(p, &flags);
1003                 if (unlikely(sighand == NULL)) {
1004                         /*
1005                          * The process has been reaped.
1006                          * We can't even collect a sample any more.
1007                          */
1008                         timer->it.cpu.expires = 0;
1009                         goto out;
1010                 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1011                         unlock_task_sighand(p, &flags);
1012                         /* Optimizations: if the process is dying, no need to rearm */
1013                         goto out;
1014                 }
1015                 cpu_timer_sample_group(timer->it_clock, p, &now);
1016                 bump_cpu_timer(timer, now);
1017                 /* Leave the sighand locked for the call below.  */
1018         }
1019
1020         /*
1021          * Now re-arm for the new expiry time.
1022          */
1023         WARN_ON_ONCE(!irqs_disabled());
1024         arm_timer(timer);
1025         unlock_task_sighand(p, &flags);
1026
1027 out:
1028         timer->it_overrun_last = timer->it_overrun;
1029         timer->it_overrun = -1;
1030         ++timer->it_requeue_pending;
1031 }
1032
1033 /**
1034  * task_cputime_expired - Compare two task_cputime entities.
1035  *
1036  * @sample:     The task_cputime structure to be checked for expiration.
1037  * @expires:    Expiration times, against which @sample will be checked.
1038  *
1039  * Checks @sample against @expires to see if any field of @sample has expired.
1040  * Returns true if any field of the former is greater than the corresponding
1041  * field of the latter if the latter field is set.  Otherwise returns false.
1042  */
1043 static inline int task_cputime_expired(const struct task_cputime *sample,
1044                                         const struct task_cputime *expires)
1045 {
1046         if (expires->utime && sample->utime >= expires->utime)
1047                 return 1;
1048         if (expires->stime && sample->utime + sample->stime >= expires->stime)
1049                 return 1;
1050         if (expires->sum_exec_runtime != 0 &&
1051             sample->sum_exec_runtime >= expires->sum_exec_runtime)
1052                 return 1;
1053         return 0;
1054 }
1055
1056 /**
1057  * fastpath_timer_check - POSIX CPU timers fast path.
1058  *
1059  * @tsk:        The task (thread) being checked.
1060  *
1061  * Check the task and thread group timers.  If both are zero (there are no
1062  * timers set) return false.  Otherwise snapshot the task and thread group
1063  * timers and compare them with the corresponding expiration times.  Return
1064  * true if a timer has expired, else return false.
1065  */
1066 static inline int fastpath_timer_check(struct task_struct *tsk)
1067 {
1068         struct signal_struct *sig;
1069
1070         if (!task_cputime_zero(&tsk->cputime_expires)) {
1071                 struct task_cputime task_sample;
1072
1073                 task_cputime(tsk, &task_sample.utime, &task_sample.stime);
1074                 task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
1075                 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1076                         return 1;
1077         }
1078
1079         sig = tsk->signal;
1080         /*
1081          * Check if thread group timers expired when the cputimer is
1082          * running and no other thread in the group is already checking
1083          * for thread group cputimers. These fields are read without the
1084          * sighand lock. However, this is fine because this is meant to
1085          * be a fastpath heuristic to determine whether we should try to
1086          * acquire the sighand lock to check/handle timers.
1087          *
1088          * In the worst case scenario, if 'running' or 'checking_timer' gets
1089          * set but the current thread doesn't see the change yet, we'll wait
1090          * until the next thread in the group gets a scheduler interrupt to
1091          * handle the timer. This isn't an issue in practice because these
1092          * types of delays with signals actually getting sent are expected.
1093          */
1094         if (READ_ONCE(sig->cputimer.running) &&
1095             !READ_ONCE(sig->cputimer.checking_timer)) {
1096                 struct task_cputime group_sample;
1097
1098                 sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
1099
1100                 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1101                         return 1;
1102         }
1103
1104         return 0;
1105 }
1106
1107 /*
1108  * This is called from the timer interrupt handler.  The irq handler has
1109  * already updated our counts.  We need to check if any timers fire now.
1110  * Interrupts are disabled.
1111  */
1112 void run_posix_cpu_timers(struct task_struct *tsk)
1113 {
1114         LIST_HEAD(firing);
1115         struct k_itimer *timer, *next;
1116         unsigned long flags;
1117
1118         WARN_ON_ONCE(!irqs_disabled());
1119
1120         /*
1121          * The fast path checks that there are no expired thread or thread
1122          * group timers.  If that's so, just return.
1123          */
1124         if (!fastpath_timer_check(tsk))
1125                 return;
1126
1127         if (!lock_task_sighand(tsk, &flags))
1128                 return;
1129         /*
1130          * Here we take off tsk->signal->cpu_timers[N] and
1131          * tsk->cpu_timers[N] all the timers that are firing, and
1132          * put them on the firing list.
1133          */
1134         check_thread_timers(tsk, &firing);
1135
1136         check_process_timers(tsk, &firing);
1137
1138         /*
1139          * We must release these locks before taking any timer's lock.
1140          * There is a potential race with timer deletion here, as the
1141          * siglock now protects our private firing list.  We have set
1142          * the firing flag in each timer, so that a deletion attempt
1143          * that gets the timer lock before we do will give it up and
1144          * spin until we've taken care of that timer below.
1145          */
1146         unlock_task_sighand(tsk, &flags);
1147
1148         /*
1149          * Now that all the timers on our list have the firing flag,
1150          * no one will touch their list entries but us.  We'll take
1151          * each timer's lock before clearing its firing flag, so no
1152          * timer call will interfere.
1153          */
1154         list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1155                 int cpu_firing;
1156
1157                 spin_lock(&timer->it_lock);
1158                 list_del_init(&timer->it.cpu.entry);
1159                 cpu_firing = timer->it.cpu.firing;
1160                 timer->it.cpu.firing = 0;
1161                 /*
1162                  * The firing flag is -1 if we collided with a reset
1163                  * of the timer, which already reported this
1164                  * almost-firing as an overrun.  So don't generate an event.
1165                  */
1166                 if (likely(cpu_firing >= 0))
1167                         cpu_timer_fire(timer);
1168                 spin_unlock(&timer->it_lock);
1169         }
1170 }
1171
1172 /*
1173  * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1174  * The tsk->sighand->siglock must be held by the caller.
1175  */
1176 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1177                            cputime_t *newval, cputime_t *oldval)
1178 {
1179         u64 now, new;
1180
1181         WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
1182         cpu_timer_sample_group(clock_idx, tsk, &now);
1183
1184         if (oldval) {
1185                 /*
1186                  * We are setting itimer. The *oldval is absolute and we update
1187                  * it to be relative, *newval argument is relative and we update
1188                  * it to be absolute.
1189                  */
1190                 if (*oldval) {
1191                         if (cputime_to_nsecs(*oldval) <= now) {
1192                                 /* Just about to fire. */
1193                                 *oldval = cputime_one_jiffy;
1194                         } else {
1195                                 *oldval -= nsecs_to_cputime(now);
1196                         }
1197                 }
1198
1199                 if (!*newval)
1200                         return;
1201                 *newval += nsecs_to_cputime(now);
1202         }
1203
1204         new = cputime_to_nsecs(*newval);
1205
1206         /*
1207          * Update expiration cache if we are the earliest timer, or eventually
1208          * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
1209          */
1210         switch (clock_idx) {
1211         case CPUCLOCK_PROF:
1212                 if (expires_gt(tsk->signal->cputime_expires.prof_exp, new))
1213                         tsk->signal->cputime_expires.prof_exp = new;
1214                 break;
1215         case CPUCLOCK_VIRT:
1216                 if (expires_gt(tsk->signal->cputime_expires.virt_exp, new))
1217                         tsk->signal->cputime_expires.virt_exp = new;
1218                 break;
1219         }
1220
1221         tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
1222 }
1223
1224 static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1225                             struct timespec *rqtp, struct itimerspec *it)
1226 {
1227         struct k_itimer timer;
1228         int error;
1229
1230         /*
1231          * Set up a temporary timer and then wait for it to go off.
1232          */
1233         memset(&timer, 0, sizeof timer);
1234         spin_lock_init(&timer.it_lock);
1235         timer.it_clock = which_clock;
1236         timer.it_overrun = -1;
1237         error = posix_cpu_timer_create(&timer);
1238         timer.it_process = current;
1239         if (!error) {
1240                 static struct itimerspec zero_it;
1241
1242                 memset(it, 0, sizeof *it);
1243                 it->it_value = *rqtp;
1244
1245                 spin_lock_irq(&timer.it_lock);
1246                 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1247                 if (error) {
1248                         spin_unlock_irq(&timer.it_lock);
1249                         return error;
1250                 }
1251
1252                 while (!signal_pending(current)) {
1253                         if (timer.it.cpu.expires == 0) {
1254                                 /*
1255                                  * Our timer fired and was reset, below
1256                                  * deletion can not fail.
1257                                  */
1258                                 posix_cpu_timer_del(&timer);
1259                                 spin_unlock_irq(&timer.it_lock);
1260                                 return 0;
1261                         }
1262
1263                         /*
1264                          * Block until cpu_timer_fire (or a signal) wakes us.
1265                          */
1266                         __set_current_state(TASK_INTERRUPTIBLE);
1267                         spin_unlock_irq(&timer.it_lock);
1268                         schedule();
1269                         spin_lock_irq(&timer.it_lock);
1270                 }
1271
1272                 /*
1273                  * We were interrupted by a signal.
1274                  */
1275                 *rqtp = ns_to_timespec(timer.it.cpu.expires);
1276                 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
1277                 if (!error) {
1278                         /*
1279                          * Timer is now unarmed, deletion can not fail.
1280                          */
1281                         posix_cpu_timer_del(&timer);
1282                 }
1283                 spin_unlock_irq(&timer.it_lock);
1284
1285                 while (error == TIMER_RETRY) {
1286                         /*
1287                          * We need to handle case when timer was or is in the
1288                          * middle of firing. In other cases we already freed
1289                          * resources.
1290                          */
1291                         spin_lock_irq(&timer.it_lock);
1292                         error = posix_cpu_timer_del(&timer);
1293                         spin_unlock_irq(&timer.it_lock);
1294                 }
1295
1296                 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1297                         /*
1298                          * It actually did fire already.
1299                          */
1300                         return 0;
1301                 }
1302
1303                 error = -ERESTART_RESTARTBLOCK;
1304         }
1305
1306         return error;
1307 }
1308
1309 static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1310
1311 static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1312                             struct timespec *rqtp, struct timespec __user *rmtp)
1313 {
1314         struct restart_block *restart_block = &current->restart_block;
1315         struct itimerspec it;
1316         int error;
1317
1318         /*
1319          * Diagnose required errors first.
1320          */
1321         if (CPUCLOCK_PERTHREAD(which_clock) &&
1322             (CPUCLOCK_PID(which_clock) == 0 ||
1323              CPUCLOCK_PID(which_clock) == current->pid))
1324                 return -EINVAL;
1325
1326         error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1327
1328         if (error == -ERESTART_RESTARTBLOCK) {
1329
1330                 if (flags & TIMER_ABSTIME)
1331                         return -ERESTARTNOHAND;
1332                 /*
1333                  * Report back to the user the time still remaining.
1334                  */
1335                 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1336                         return -EFAULT;
1337
1338                 restart_block->fn = posix_cpu_nsleep_restart;
1339                 restart_block->nanosleep.clockid = which_clock;
1340                 restart_block->nanosleep.rmtp = rmtp;
1341                 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
1342         }
1343         return error;
1344 }
1345
1346 static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1347 {
1348         clockid_t which_clock = restart_block->nanosleep.clockid;
1349         struct timespec t;
1350         struct itimerspec it;
1351         int error;
1352
1353         t = ns_to_timespec(restart_block->nanosleep.expires);
1354
1355         error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1356
1357         if (error == -ERESTART_RESTARTBLOCK) {
1358                 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
1359                 /*
1360                  * Report back to the user the time still remaining.
1361                  */
1362                 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1363                         return -EFAULT;
1364
1365                 restart_block->nanosleep.expires = timespec_to_ns(&t);
1366         }
1367         return error;
1368
1369 }
1370
1371 #define PROCESS_CLOCK   MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1372 #define THREAD_CLOCK    MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1373
1374 static int process_cpu_clock_getres(const clockid_t which_clock,
1375                                     struct timespec *tp)
1376 {
1377         return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1378 }
1379 static int process_cpu_clock_get(const clockid_t which_clock,
1380                                  struct timespec *tp)
1381 {
1382         return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1383 }
1384 static int process_cpu_timer_create(struct k_itimer *timer)
1385 {
1386         timer->it_clock = PROCESS_CLOCK;
1387         return posix_cpu_timer_create(timer);
1388 }
1389 static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1390                               struct timespec *rqtp,
1391                               struct timespec __user *rmtp)
1392 {
1393         return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1394 }
1395 static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1396 {
1397         return -EINVAL;
1398 }
1399 static int thread_cpu_clock_getres(const clockid_t which_clock,
1400                                    struct timespec *tp)
1401 {
1402         return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1403 }
1404 static int thread_cpu_clock_get(const clockid_t which_clock,
1405                                 struct timespec *tp)
1406 {
1407         return posix_cpu_clock_get(THREAD_CLOCK, tp);
1408 }
1409 static int thread_cpu_timer_create(struct k_itimer *timer)
1410 {
1411         timer->it_clock = THREAD_CLOCK;
1412         return posix_cpu_timer_create(timer);
1413 }
1414
1415 struct k_clock clock_posix_cpu = {
1416         .clock_getres   = posix_cpu_clock_getres,
1417         .clock_set      = posix_cpu_clock_set,
1418         .clock_get      = posix_cpu_clock_get,
1419         .timer_create   = posix_cpu_timer_create,
1420         .nsleep         = posix_cpu_nsleep,
1421         .nsleep_restart = posix_cpu_nsleep_restart,
1422         .timer_set      = posix_cpu_timer_set,
1423         .timer_del      = posix_cpu_timer_del,
1424         .timer_get      = posix_cpu_timer_get,
1425 };
1426
1427 static __init int init_posix_cpu_timers(void)
1428 {
1429         struct k_clock process = {
1430                 .clock_getres   = process_cpu_clock_getres,
1431                 .clock_get      = process_cpu_clock_get,
1432                 .timer_create   = process_cpu_timer_create,
1433                 .nsleep         = process_cpu_nsleep,
1434                 .nsleep_restart = process_cpu_nsleep_restart,
1435         };
1436         struct k_clock thread = {
1437                 .clock_getres   = thread_cpu_clock_getres,
1438                 .clock_get      = thread_cpu_clock_get,
1439                 .timer_create   = thread_cpu_timer_create,
1440         };
1441
1442         posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1443         posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1444
1445         return 0;
1446 }
1447 __initcall(init_posix_cpu_timers);