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