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