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