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