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