Merge tag 'mips_4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan/mips
[sfrench/cifs-2.6.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         u64 nsecs = rlim_new * NSEC_PER_SEC;
24
25         spin_lock_irq(&task->sighand->siglock);
26         set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, 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 >= it->expires) {
864                 if (it->incr)
865                         it->expires += it->incr;
866                 else
867                         it->expires = 0;
868
869                 trace_itimer_expire(signo == SIGPROF ?
870                                     ITIMER_PROF : ITIMER_VIRTUAL,
871                                     tsk->signal->leader_pid, cur_time);
872                 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
873         }
874
875         if (it->expires && (!*expires || it->expires < *expires))
876                 *expires = it->expires;
877 }
878
879 /*
880  * Check for any per-thread CPU timers that have fired and move them
881  * off the tsk->*_timers list onto the firing list.  Per-thread timers
882  * have already been taken off.
883  */
884 static void check_process_timers(struct task_struct *tsk,
885                                  struct list_head *firing)
886 {
887         struct signal_struct *const sig = tsk->signal;
888         u64 utime, ptime, virt_expires, prof_expires;
889         u64 sum_sched_runtime, sched_expires;
890         struct list_head *timers = sig->cpu_timers;
891         struct task_cputime cputime;
892         unsigned long soft;
893
894         /*
895          * If cputimer is not running, then there are no active
896          * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
897          */
898         if (!READ_ONCE(tsk->signal->cputimer.running))
899                 return;
900
901         /*
902          * Signify that a thread is checking for process timers.
903          * Write access to this field is protected by the sighand lock.
904          */
905         sig->cputimer.checking_timer = true;
906
907         /*
908          * Collect the current process totals.
909          */
910         thread_group_cputimer(tsk, &cputime);
911         utime = cputime.utime;
912         ptime = utime + cputime.stime;
913         sum_sched_runtime = cputime.sum_exec_runtime;
914
915         prof_expires = check_timers_list(timers, firing, ptime);
916         virt_expires = check_timers_list(++timers, firing, utime);
917         sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
918
919         /*
920          * Check for the special case process timers.
921          */
922         check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
923                          SIGPROF);
924         check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
925                          SIGVTALRM);
926         soft = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
927         if (soft != RLIM_INFINITY) {
928                 unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
929                 unsigned long hard =
930                         READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
931                 u64 x;
932                 if (psecs >= hard) {
933                         /*
934                          * At the hard limit, we just die.
935                          * No need to calculate anything else now.
936                          */
937                         __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
938                         return;
939                 }
940                 if (psecs >= soft) {
941                         /*
942                          * At the soft limit, send a SIGXCPU every second.
943                          */
944                         __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
945                         if (soft < hard) {
946                                 soft++;
947                                 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
948                         }
949                 }
950                 x = soft * NSEC_PER_SEC;
951                 if (!prof_expires || x < prof_expires)
952                         prof_expires = x;
953         }
954
955         sig->cputime_expires.prof_exp = prof_expires;
956         sig->cputime_expires.virt_exp = virt_expires;
957         sig->cputime_expires.sched_exp = sched_expires;
958         if (task_cputime_zero(&sig->cputime_expires))
959                 stop_process_timers(sig);
960
961         sig->cputimer.checking_timer = false;
962 }
963
964 /*
965  * This is called from the signal code (via do_schedule_next_timer)
966  * when the last timer signal was delivered and we have to reload the timer.
967  */
968 void posix_cpu_timer_schedule(struct k_itimer *timer)
969 {
970         struct sighand_struct *sighand;
971         unsigned long flags;
972         struct task_struct *p = timer->it.cpu.task;
973         u64 now;
974
975         WARN_ON_ONCE(p == NULL);
976
977         /*
978          * Fetch the current sample and update the timer's expiry time.
979          */
980         if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
981                 cpu_clock_sample(timer->it_clock, p, &now);
982                 bump_cpu_timer(timer, now);
983                 if (unlikely(p->exit_state))
984                         goto out;
985
986                 /* Protect timer list r/w in arm_timer() */
987                 sighand = lock_task_sighand(p, &flags);
988                 if (!sighand)
989                         goto out;
990         } else {
991                 /*
992                  * Protect arm_timer() and timer sampling in case of call to
993                  * thread_group_cputime().
994                  */
995                 sighand = lock_task_sighand(p, &flags);
996                 if (unlikely(sighand == NULL)) {
997                         /*
998                          * The process has been reaped.
999                          * We can't even collect a sample any more.
1000                          */
1001                         timer->it.cpu.expires = 0;
1002                         goto out;
1003                 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1004                         unlock_task_sighand(p, &flags);
1005                         /* Optimizations: if the process is dying, no need to rearm */
1006                         goto out;
1007                 }
1008                 cpu_timer_sample_group(timer->it_clock, p, &now);
1009                 bump_cpu_timer(timer, now);
1010                 /* Leave the sighand locked for the call below.  */
1011         }
1012
1013         /*
1014          * Now re-arm for the new expiry time.
1015          */
1016         WARN_ON_ONCE(!irqs_disabled());
1017         arm_timer(timer);
1018         unlock_task_sighand(p, &flags);
1019
1020 out:
1021         timer->it_overrun_last = timer->it_overrun;
1022         timer->it_overrun = -1;
1023         ++timer->it_requeue_pending;
1024 }
1025
1026 /**
1027  * task_cputime_expired - Compare two task_cputime entities.
1028  *
1029  * @sample:     The task_cputime structure to be checked for expiration.
1030  * @expires:    Expiration times, against which @sample will be checked.
1031  *
1032  * Checks @sample against @expires to see if any field of @sample has expired.
1033  * Returns true if any field of the former is greater than the corresponding
1034  * field of the latter if the latter field is set.  Otherwise returns false.
1035  */
1036 static inline int task_cputime_expired(const struct task_cputime *sample,
1037                                         const struct task_cputime *expires)
1038 {
1039         if (expires->utime && sample->utime >= expires->utime)
1040                 return 1;
1041         if (expires->stime && sample->utime + sample->stime >= expires->stime)
1042                 return 1;
1043         if (expires->sum_exec_runtime != 0 &&
1044             sample->sum_exec_runtime >= expires->sum_exec_runtime)
1045                 return 1;
1046         return 0;
1047 }
1048
1049 /**
1050  * fastpath_timer_check - POSIX CPU timers fast path.
1051  *
1052  * @tsk:        The task (thread) being checked.
1053  *
1054  * Check the task and thread group timers.  If both are zero (there are no
1055  * timers set) return false.  Otherwise snapshot the task and thread group
1056  * timers and compare them with the corresponding expiration times.  Return
1057  * true if a timer has expired, else return false.
1058  */
1059 static inline int fastpath_timer_check(struct task_struct *tsk)
1060 {
1061         struct signal_struct *sig;
1062
1063         if (!task_cputime_zero(&tsk->cputime_expires)) {
1064                 struct task_cputime task_sample;
1065
1066                 task_cputime(tsk, &task_sample.utime, &task_sample.stime);
1067                 task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
1068                 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1069                         return 1;
1070         }
1071
1072         sig = tsk->signal;
1073         /*
1074          * Check if thread group timers expired when the cputimer is
1075          * running and no other thread in the group is already checking
1076          * for thread group cputimers. These fields are read without the
1077          * sighand lock. However, this is fine because this is meant to
1078          * be a fastpath heuristic to determine whether we should try to
1079          * acquire the sighand lock to check/handle timers.
1080          *
1081          * In the worst case scenario, if 'running' or 'checking_timer' gets
1082          * set but the current thread doesn't see the change yet, we'll wait
1083          * until the next thread in the group gets a scheduler interrupt to
1084          * handle the timer. This isn't an issue in practice because these
1085          * types of delays with signals actually getting sent are expected.
1086          */
1087         if (READ_ONCE(sig->cputimer.running) &&
1088             !READ_ONCE(sig->cputimer.checking_timer)) {
1089                 struct task_cputime group_sample;
1090
1091                 sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
1092
1093                 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1094                         return 1;
1095         }
1096
1097         return 0;
1098 }
1099
1100 /*
1101  * This is called from the timer interrupt handler.  The irq handler has
1102  * already updated our counts.  We need to check if any timers fire now.
1103  * Interrupts are disabled.
1104  */
1105 void run_posix_cpu_timers(struct task_struct *tsk)
1106 {
1107         LIST_HEAD(firing);
1108         struct k_itimer *timer, *next;
1109         unsigned long flags;
1110
1111         WARN_ON_ONCE(!irqs_disabled());
1112
1113         /*
1114          * The fast path checks that there are no expired thread or thread
1115          * group timers.  If that's so, just return.
1116          */
1117         if (!fastpath_timer_check(tsk))
1118                 return;
1119
1120         if (!lock_task_sighand(tsk, &flags))
1121                 return;
1122         /*
1123          * Here we take off tsk->signal->cpu_timers[N] and
1124          * tsk->cpu_timers[N] all the timers that are firing, and
1125          * put them on the firing list.
1126          */
1127         check_thread_timers(tsk, &firing);
1128
1129         check_process_timers(tsk, &firing);
1130
1131         /*
1132          * We must release these locks before taking any timer's lock.
1133          * There is a potential race with timer deletion here, as the
1134          * siglock now protects our private firing list.  We have set
1135          * the firing flag in each timer, so that a deletion attempt
1136          * that gets the timer lock before we do will give it up and
1137          * spin until we've taken care of that timer below.
1138          */
1139         unlock_task_sighand(tsk, &flags);
1140
1141         /*
1142          * Now that all the timers on our list have the firing flag,
1143          * no one will touch their list entries but us.  We'll take
1144          * each timer's lock before clearing its firing flag, so no
1145          * timer call will interfere.
1146          */
1147         list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1148                 int cpu_firing;
1149
1150                 spin_lock(&timer->it_lock);
1151                 list_del_init(&timer->it.cpu.entry);
1152                 cpu_firing = timer->it.cpu.firing;
1153                 timer->it.cpu.firing = 0;
1154                 /*
1155                  * The firing flag is -1 if we collided with a reset
1156                  * of the timer, which already reported this
1157                  * almost-firing as an overrun.  So don't generate an event.
1158                  */
1159                 if (likely(cpu_firing >= 0))
1160                         cpu_timer_fire(timer);
1161                 spin_unlock(&timer->it_lock);
1162         }
1163 }
1164
1165 /*
1166  * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1167  * The tsk->sighand->siglock must be held by the caller.
1168  */
1169 void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1170                            u64 *newval, u64 *oldval)
1171 {
1172         u64 now;
1173
1174         WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
1175         cpu_timer_sample_group(clock_idx, tsk, &now);
1176
1177         if (oldval) {
1178                 /*
1179                  * We are setting itimer. The *oldval is absolute and we update
1180                  * it to be relative, *newval argument is relative and we update
1181                  * it to be absolute.
1182                  */
1183                 if (*oldval) {
1184                         if (*oldval <= now) {
1185                                 /* Just about to fire. */
1186                                 *oldval = TICK_NSEC;
1187                         } else {
1188                                 *oldval -= now;
1189                         }
1190                 }
1191
1192                 if (!*newval)
1193                         return;
1194                 *newval += now;
1195         }
1196
1197         /*
1198          * Update expiration cache if we are the earliest timer, or eventually
1199          * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
1200          */
1201         switch (clock_idx) {
1202         case CPUCLOCK_PROF:
1203                 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
1204                         tsk->signal->cputime_expires.prof_exp = *newval;
1205                 break;
1206         case CPUCLOCK_VIRT:
1207                 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
1208                         tsk->signal->cputime_expires.virt_exp = *newval;
1209                 break;
1210         }
1211
1212         tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
1213 }
1214
1215 static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1216                             struct timespec *rqtp, struct itimerspec *it)
1217 {
1218         struct k_itimer timer;
1219         int error;
1220
1221         /*
1222          * Set up a temporary timer and then wait for it to go off.
1223          */
1224         memset(&timer, 0, sizeof timer);
1225         spin_lock_init(&timer.it_lock);
1226         timer.it_clock = which_clock;
1227         timer.it_overrun = -1;
1228         error = posix_cpu_timer_create(&timer);
1229         timer.it_process = current;
1230         if (!error) {
1231                 static struct itimerspec zero_it;
1232
1233                 memset(it, 0, sizeof *it);
1234                 it->it_value = *rqtp;
1235
1236                 spin_lock_irq(&timer.it_lock);
1237                 error = posix_cpu_timer_set(&timer, flags, it, NULL);
1238                 if (error) {
1239                         spin_unlock_irq(&timer.it_lock);
1240                         return error;
1241                 }
1242
1243                 while (!signal_pending(current)) {
1244                         if (timer.it.cpu.expires == 0) {
1245                                 /*
1246                                  * Our timer fired and was reset, below
1247                                  * deletion can not fail.
1248                                  */
1249                                 posix_cpu_timer_del(&timer);
1250                                 spin_unlock_irq(&timer.it_lock);
1251                                 return 0;
1252                         }
1253
1254                         /*
1255                          * Block until cpu_timer_fire (or a signal) wakes us.
1256                          */
1257                         __set_current_state(TASK_INTERRUPTIBLE);
1258                         spin_unlock_irq(&timer.it_lock);
1259                         schedule();
1260                         spin_lock_irq(&timer.it_lock);
1261                 }
1262
1263                 /*
1264                  * We were interrupted by a signal.
1265                  */
1266                 *rqtp = ns_to_timespec(timer.it.cpu.expires);
1267                 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
1268                 if (!error) {
1269                         /*
1270                          * Timer is now unarmed, deletion can not fail.
1271                          */
1272                         posix_cpu_timer_del(&timer);
1273                 }
1274                 spin_unlock_irq(&timer.it_lock);
1275
1276                 while (error == TIMER_RETRY) {
1277                         /*
1278                          * We need to handle case when timer was or is in the
1279                          * middle of firing. In other cases we already freed
1280                          * resources.
1281                          */
1282                         spin_lock_irq(&timer.it_lock);
1283                         error = posix_cpu_timer_del(&timer);
1284                         spin_unlock_irq(&timer.it_lock);
1285                 }
1286
1287                 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1288                         /*
1289                          * It actually did fire already.
1290                          */
1291                         return 0;
1292                 }
1293
1294                 error = -ERESTART_RESTARTBLOCK;
1295         }
1296
1297         return error;
1298 }
1299
1300 static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1301
1302 static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1303                             struct timespec *rqtp, struct timespec __user *rmtp)
1304 {
1305         struct restart_block *restart_block = &current->restart_block;
1306         struct itimerspec it;
1307         int error;
1308
1309         /*
1310          * Diagnose required errors first.
1311          */
1312         if (CPUCLOCK_PERTHREAD(which_clock) &&
1313             (CPUCLOCK_PID(which_clock) == 0 ||
1314              CPUCLOCK_PID(which_clock) == current->pid))
1315                 return -EINVAL;
1316
1317         error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1318
1319         if (error == -ERESTART_RESTARTBLOCK) {
1320
1321                 if (flags & TIMER_ABSTIME)
1322                         return -ERESTARTNOHAND;
1323                 /*
1324                  * Report back to the user the time still remaining.
1325                  */
1326                 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1327                         return -EFAULT;
1328
1329                 restart_block->fn = posix_cpu_nsleep_restart;
1330                 restart_block->nanosleep.clockid = which_clock;
1331                 restart_block->nanosleep.rmtp = rmtp;
1332                 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
1333         }
1334         return error;
1335 }
1336
1337 static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1338 {
1339         clockid_t which_clock = restart_block->nanosleep.clockid;
1340         struct timespec t;
1341         struct itimerspec it;
1342         int error;
1343
1344         t = ns_to_timespec(restart_block->nanosleep.expires);
1345
1346         error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1347
1348         if (error == -ERESTART_RESTARTBLOCK) {
1349                 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
1350                 /*
1351                  * Report back to the user the time still remaining.
1352                  */
1353                 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1354                         return -EFAULT;
1355
1356                 restart_block->nanosleep.expires = timespec_to_ns(&t);
1357         }
1358         return error;
1359
1360 }
1361
1362 #define PROCESS_CLOCK   MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1363 #define THREAD_CLOCK    MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1364
1365 static int process_cpu_clock_getres(const clockid_t which_clock,
1366                                     struct timespec *tp)
1367 {
1368         return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1369 }
1370 static int process_cpu_clock_get(const clockid_t which_clock,
1371                                  struct timespec *tp)
1372 {
1373         return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1374 }
1375 static int process_cpu_timer_create(struct k_itimer *timer)
1376 {
1377         timer->it_clock = PROCESS_CLOCK;
1378         return posix_cpu_timer_create(timer);
1379 }
1380 static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1381                               struct timespec *rqtp,
1382                               struct timespec __user *rmtp)
1383 {
1384         return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1385 }
1386 static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1387 {
1388         return -EINVAL;
1389 }
1390 static int thread_cpu_clock_getres(const clockid_t which_clock,
1391                                    struct timespec *tp)
1392 {
1393         return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1394 }
1395 static int thread_cpu_clock_get(const clockid_t which_clock,
1396                                 struct timespec *tp)
1397 {
1398         return posix_cpu_clock_get(THREAD_CLOCK, tp);
1399 }
1400 static int thread_cpu_timer_create(struct k_itimer *timer)
1401 {
1402         timer->it_clock = THREAD_CLOCK;
1403         return posix_cpu_timer_create(timer);
1404 }
1405
1406 struct k_clock clock_posix_cpu = {
1407         .clock_getres   = posix_cpu_clock_getres,
1408         .clock_set      = posix_cpu_clock_set,
1409         .clock_get      = posix_cpu_clock_get,
1410         .timer_create   = posix_cpu_timer_create,
1411         .nsleep         = posix_cpu_nsleep,
1412         .nsleep_restart = posix_cpu_nsleep_restart,
1413         .timer_set      = posix_cpu_timer_set,
1414         .timer_del      = posix_cpu_timer_del,
1415         .timer_get      = posix_cpu_timer_get,
1416 };
1417
1418 static __init int init_posix_cpu_timers(void)
1419 {
1420         struct k_clock process = {
1421                 .clock_getres   = process_cpu_clock_getres,
1422                 .clock_get      = process_cpu_clock_get,
1423                 .timer_create   = process_cpu_timer_create,
1424                 .nsleep         = process_cpu_nsleep,
1425                 .nsleep_restart = process_cpu_nsleep_restart,
1426         };
1427         struct k_clock thread = {
1428                 .clock_getres   = thread_cpu_clock_getres,
1429                 .clock_get      = thread_cpu_clock_get,
1430                 .timer_create   = thread_cpu_timer_create,
1431         };
1432
1433         posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1434         posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1435
1436         return 0;
1437 }
1438 __initcall(init_posix_cpu_timers);