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