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