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