blob: 4318c3085788a07b3870b386abe0726edbb502df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implement CPU time clocks for the POSIX clock interface.
3 */
4
5#include <linux/sched.h>
6#include <linux/posix-timers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/errno.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -07008#include <linux/math64.h>
9#include <asm/uaccess.h>
Frank Mayharbb34d922008-09-12 09:54:39 -070010#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Frank Mayharf06febc2008-09-12 09:54:39 -070012/*
Frank Mayharf06febc2008-09-12 09:54:39 -070013 * Called after updating RLIMIT_CPU to set timer expiration if necessary.
14 */
15void update_rlimit_cpu(unsigned long rlim_new)
16{
17 cputime_t cputime;
18
19 cputime = secs_to_cputime(rlim_new);
20 if (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
Ingo Molnar5ce73a42008-09-14 17:11:46 +020021 cputime_lt(current->signal->it_prof_expires, cputime)) {
Frank Mayharf06febc2008-09-12 09:54:39 -070022 spin_lock_irq(&current->sighand->siglock);
23 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
24 spin_unlock_irq(&current->sighand->siglock);
25 }
26}
27
Thomas Gleixnera924b042006-01-09 20:52:27 -080028static int check_clock(const clockid_t which_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
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
40 read_lock(&tasklist_lock);
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -080041 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -070042 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
43 same_thread_group(p, current) : thread_group_leader(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 error = -EINVAL;
45 }
46 read_unlock(&tasklist_lock);
47
48 return error;
49}
50
51static inline union cpu_time_count
Thomas Gleixnera924b042006-01-09 20:52:27 -080052timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
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) {
Oleg Nesterovee500f22005-11-28 13:43:55 -080057 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 } else {
59 ret.cpu = timespec_to_cputime(tp);
60 }
61 return ret;
62}
63
Thomas Gleixnera924b042006-01-09 20:52:27 -080064static void sample_to_timespec(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 union cpu_time_count cpu,
66 struct timespec *tp)
67{
Roman Zippelf8bd2252008-05-01 04:34:31 -070068 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
69 *tp = ns_to_timespec(cpu.sched);
70 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cputime_to_timespec(cpu.cpu, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Thomas Gleixnera924b042006-01-09 20:52:27 -080074static inline int cpu_time_before(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 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 {
81 return cputime_lt(now.cpu, then.cpu);
82 }
83}
Thomas Gleixnera924b042006-01-09 20:52:27 -080084static inline void cpu_time_add(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 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 {
91 acc->cpu = cputime_add(acc->cpu, val.cpu);
92 }
93}
Thomas Gleixnera924b042006-01-09 20:52:27 -080094static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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 {
101 a.cpu = cputime_sub(a.cpu, b.cpu);
102 }
103 return a;
104}
105
106/*
Thomas Gleixnerac08c262006-10-17 00:09:39 -0700107 * Divide and limit the result to res >= 1
108 *
109 * This is necessary to prevent signal delivery starvation, when the result of
110 * the division would be rounded down to 0.
111 */
112static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
113{
114 cputime_t res = cputime_div(time, div);
115
116 return max_t(cputime_t, res, 1);
117}
118
119/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * Update expiry time from increment, and increase overrun count,
121 * given the current clock sample.
122 */
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400123static void bump_cpu_timer(struct k_itimer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 union cpu_time_count now)
125{
126 int i;
127
128 if (timer->it.cpu.incr.sched == 0)
129 return;
130
131 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
132 unsigned long long delta, incr;
133
134 if (now.sched < timer->it.cpu.expires.sched)
135 return;
136 incr = timer->it.cpu.incr.sched;
137 delta = now.sched + incr - timer->it.cpu.expires.sched;
138 /* Don't use (incr*2 < delta), incr*2 might overflow. */
139 for (i = 0; incr < delta - incr; i++)
140 incr = incr << 1;
141 for (; i >= 0; incr >>= 1, i--) {
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400142 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 continue;
144 timer->it.cpu.expires.sched += incr;
145 timer->it_overrun += 1 << i;
146 delta -= incr;
147 }
148 } else {
149 cputime_t delta, incr;
150
151 if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
152 return;
153 incr = timer->it.cpu.incr.cpu;
154 delta = cputime_sub(cputime_add(now.cpu, incr),
155 timer->it.cpu.expires.cpu);
156 /* Don't use (incr*2 < delta), incr*2 might overflow. */
157 for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
158 incr = cputime_add(incr, incr);
159 for (; i >= 0; incr = cputime_halve(incr), i--) {
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400160 if (cputime_lt(delta, incr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 continue;
162 timer->it.cpu.expires.cpu =
163 cputime_add(timer->it.cpu.expires.cpu, incr);
164 timer->it_overrun += 1 << i;
165 delta = cputime_sub(delta, incr);
166 }
167 }
168}
169
170static inline cputime_t prof_ticks(struct task_struct *p)
171{
172 return cputime_add(p->utime, p->stime);
173}
174static inline cputime_t virt_ticks(struct task_struct *p)
175{
176 return p->utime;
177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Thomas Gleixnera924b042006-01-09 20:52:27 -0800179int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int error = check_clock(which_clock);
182 if (!error) {
183 tp->tv_sec = 0;
184 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
185 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
186 /*
187 * If sched_clock is using a cycle counter, we
188 * don't have any idea of its true resolution
189 * exported, but it is much more than 1s/HZ.
190 */
191 tp->tv_nsec = 1;
192 }
193 }
194 return error;
195}
196
Thomas Gleixnera924b042006-01-09 20:52:27 -0800197int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 /*
200 * You can never reset a CPU clock, but we check for other errors
201 * in the call before failing with EPERM.
202 */
203 int error = check_clock(which_clock);
204 if (error == 0) {
205 error = -EPERM;
206 }
207 return error;
208}
209
210
211/*
212 * Sample a per-thread clock for the given task.
213 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800214static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 union cpu_time_count *cpu)
216{
217 switch (CPUCLOCK_WHICH(which_clock)) {
218 default:
219 return -EINVAL;
220 case CPUCLOCK_PROF:
221 cpu->cpu = prof_ticks(p);
222 break;
223 case CPUCLOCK_VIRT:
224 cpu->cpu = virt_ticks(p);
225 break;
226 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900227 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229 }
230 return 0;
231}
232
233/*
234 * Sample a process (thread group) clock for the given group_leader task.
235 * Must be called with tasklist_lock held for reading.
236 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800237static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct task_struct *p,
239 union cpu_time_count *cpu)
240{
Frank Mayharbb34d922008-09-12 09:54:39 -0700241 struct task_cputime cputime;
242
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100243 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700244 default:
245 return -EINVAL;
246 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900247 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700248 cpu->cpu = cputime_add(cputime.utime, cputime.stime);
249 break;
250 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900251 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700252 cpu->cpu = cputime.utime;
253 break;
254 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900255 cpu->sched = thread_group_sched_runtime(p);
Frank Mayharbb34d922008-09-12 09:54:39 -0700256 break;
257 }
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261
Thomas Gleixnera924b042006-01-09 20:52:27 -0800262int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 const pid_t pid = CPUCLOCK_PID(which_clock);
265 int error = -EINVAL;
266 union cpu_time_count rtn;
267
268 if (pid == 0) {
269 /*
270 * Special case constant value for our own clocks.
271 * We don't have to do any lookup to find ourselves.
272 */
273 if (CPUCLOCK_PERTHREAD(which_clock)) {
274 /*
275 * Sampling just ourselves we can do with no locking.
276 */
277 error = cpu_clock_sample(which_clock,
278 current, &rtn);
279 } else {
280 read_lock(&tasklist_lock);
281 error = cpu_clock_sample_group(which_clock,
282 current, &rtn);
283 read_unlock(&tasklist_lock);
284 }
285 } else {
286 /*
287 * Find the given PID, and validate that the caller
288 * should be able to see it.
289 */
290 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800291 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800292 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (p) {
294 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700295 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 error = cpu_clock_sample(which_clock,
297 p, &rtn);
298 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800299 } else {
300 read_lock(&tasklist_lock);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700301 if (thread_group_leader(p) && p->signal) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800302 error =
303 cpu_clock_sample_group(which_clock,
304 p, &rtn);
305 }
306 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
312 if (error)
313 return error;
314 sample_to_timespec(which_clock, rtn, tp);
315 return 0;
316}
317
318
319/*
320 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
321 * This is called from sys_timer_create with the new timer already locked.
322 */
323int posix_cpu_timer_create(struct k_itimer *new_timer)
324{
325 int ret = 0;
326 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
327 struct task_struct *p;
328
329 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
330 return -EINVAL;
331
332 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
333 new_timer->it.cpu.incr.sched = 0;
334 new_timer->it.cpu.expires.sched = 0;
335
336 read_lock(&tasklist_lock);
337 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
338 if (pid == 0) {
339 p = current;
340 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800341 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700342 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 p = NULL;
344 }
345 } else {
346 if (pid == 0) {
347 p = current->group_leader;
348 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800349 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700350 if (p && !thread_group_leader(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 p = NULL;
352 }
353 }
354 new_timer->it.cpu.task = p;
355 if (p) {
356 get_task_struct(p);
357 } else {
358 ret = -EINVAL;
359 }
360 read_unlock(&tasklist_lock);
361
362 return ret;
363}
364
365/*
366 * Clean up a CPU-clock timer that is about to be destroyed.
367 * This is called from timer deletion with the timer already locked.
368 * If we return TIMER_RETRY, it's necessary to release the timer's lock
369 * and try again. (This happens when the timer is in the middle of firing.)
370 */
371int posix_cpu_timer_del(struct k_itimer *timer)
372{
373 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400374 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Oleg Nesterov108150e2005-10-23 20:25:39 +0400376 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700377 read_lock(&tasklist_lock);
378 if (unlikely(p->signal == NULL)) {
379 /*
380 * We raced with the reaping of the task.
381 * The deletion should have cleared us off the list.
382 */
383 BUG_ON(!list_empty(&timer->it.cpu.entry));
384 } else {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700385 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400386 if (timer->it.cpu.firing)
387 ret = TIMER_RETRY;
388 else
389 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700390 spin_unlock(&p->sighand->siglock);
391 }
392 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Oleg Nesterov108150e2005-10-23 20:25:39 +0400394 if (!ret)
395 put_task_struct(p);
396 }
397
398 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Clean out CPU timers still ticking when a thread exited. The task
403 * pointer is cleared, and the expiry time is replaced with the residual
404 * time for later timer_gettime calls to return.
405 * This must be called with the siglock held.
406 */
407static void cleanup_timers(struct list_head *head,
408 cputime_t utime, cputime_t stime,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200409 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct cpu_timer_list *timer, *next;
412 cputime_t ptime = cputime_add(utime, stime);
413
414 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 list_del_init(&timer->entry);
416 if (cputime_lt(timer->expires.cpu, ptime)) {
417 timer->expires.cpu = cputime_zero;
418 } else {
419 timer->expires.cpu = cputime_sub(timer->expires.cpu,
420 ptime);
421 }
422 }
423
424 ++head;
425 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 list_del_init(&timer->entry);
427 if (cputime_lt(timer->expires.cpu, utime)) {
428 timer->expires.cpu = cputime_zero;
429 } else {
430 timer->expires.cpu = cputime_sub(timer->expires.cpu,
431 utime);
432 }
433 }
434
435 ++head;
436 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200438 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 timer->expires.sched = 0;
440 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200441 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444}
445
446/*
447 * These are both called with the siglock held, when the current thread
448 * is being reaped. When the final (leader) thread in the group is reaped,
449 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
450 */
451void posix_cpu_timers_exit(struct task_struct *tsk)
452{
453 cleanup_timers(tsk->cpu_timers,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200454 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456}
457void posix_cpu_timers_exit_group(struct task_struct *tsk)
458{
Frank Mayharf06febc2008-09-12 09:54:39 -0700459 struct task_cputime cputime;
460
461 thread_group_cputime(tsk, &cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 cleanup_timers(tsk->signal->cpu_timers,
Frank Mayharf06febc2008-09-12 09:54:39 -0700463 cputime.utime, cputime.stime, cputime.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
467{
468 /*
469 * That's all for this thread or process.
470 * We leave our residual in expires to be reported.
471 */
472 put_task_struct(timer->it.cpu.task);
473 timer->it.cpu.task = NULL;
474 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
475 timer->it.cpu.expires,
476 now);
477}
478
479/*
480 * Insert the timer on the appropriate list before any timers that
481 * expire later. This must be called with the tasklist_lock held
482 * for reading, and interrupts disabled.
483 */
484static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
485{
486 struct task_struct *p = timer->it.cpu.task;
487 struct list_head *head, *listpos;
488 struct cpu_timer_list *const nt = &timer->it.cpu;
489 struct cpu_timer_list *next;
490 unsigned long i;
491
492 head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
493 p->cpu_timers : p->signal->cpu_timers);
494 head += CPUCLOCK_WHICH(timer->it_clock);
495
496 BUG_ON(!irqs_disabled());
497 spin_lock(&p->sighand->siglock);
498
499 listpos = head;
500 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
501 list_for_each_entry(next, head, entry) {
Linus Torvalds70ab81c2005-10-26 11:23:06 -0700502 if (next->expires.sched > nt->expires.sched)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
Linus Torvalds70ab81c2005-10-26 11:23:06 -0700504 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 } else {
507 list_for_each_entry(next, head, entry) {
Linus Torvalds70ab81c2005-10-26 11:23:06 -0700508 if (cputime_gt(next->expires.cpu, nt->expires.cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
Linus Torvalds70ab81c2005-10-26 11:23:06 -0700510 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
513 list_add(&nt->entry, listpos);
514
515 if (listpos == head) {
516 /*
517 * We are the new earliest-expiring timer.
518 * If we are a thread timer, there can always
519 * be a process timer telling us to stop earlier.
520 */
521
522 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
523 switch (CPUCLOCK_WHICH(timer->it_clock)) {
524 default:
525 BUG();
526 case CPUCLOCK_PROF:
Frank Mayharf06febc2008-09-12 09:54:39 -0700527 if (cputime_eq(p->cputime_expires.prof_exp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 cputime_zero) ||
Frank Mayharf06febc2008-09-12 09:54:39 -0700529 cputime_gt(p->cputime_expires.prof_exp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 nt->expires.cpu))
Frank Mayharf06febc2008-09-12 09:54:39 -0700531 p->cputime_expires.prof_exp =
532 nt->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534 case CPUCLOCK_VIRT:
Frank Mayharf06febc2008-09-12 09:54:39 -0700535 if (cputime_eq(p->cputime_expires.virt_exp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cputime_zero) ||
Frank Mayharf06febc2008-09-12 09:54:39 -0700537 cputime_gt(p->cputime_expires.virt_exp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 nt->expires.cpu))
Frank Mayharf06febc2008-09-12 09:54:39 -0700539 p->cputime_expires.virt_exp =
540 nt->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 case CPUCLOCK_SCHED:
Frank Mayharf06febc2008-09-12 09:54:39 -0700543 if (p->cputime_expires.sched_exp == 0 ||
544 p->cputime_expires.sched_exp >
545 nt->expires.sched)
546 p->cputime_expires.sched_exp =
547 nt->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 }
550 } else {
551 /*
Frank Mayharf06febc2008-09-12 09:54:39 -0700552 * For a process timer, set the cached expiration time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554 switch (CPUCLOCK_WHICH(timer->it_clock)) {
555 default:
556 BUG();
557 case CPUCLOCK_VIRT:
558 if (!cputime_eq(p->signal->it_virt_expires,
559 cputime_zero) &&
560 cputime_lt(p->signal->it_virt_expires,
561 timer->it.cpu.expires.cpu))
562 break;
Frank Mayharf06febc2008-09-12 09:54:39 -0700563 p->signal->cputime_expires.virt_exp =
564 timer->it.cpu.expires.cpu;
565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 case CPUCLOCK_PROF:
567 if (!cputime_eq(p->signal->it_prof_expires,
568 cputime_zero) &&
569 cputime_lt(p->signal->it_prof_expires,
570 timer->it.cpu.expires.cpu))
571 break;
572 i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
573 if (i != RLIM_INFINITY &&
574 i <= cputime_to_secs(timer->it.cpu.expires.cpu))
575 break;
Frank Mayharf06febc2008-09-12 09:54:39 -0700576 p->signal->cputime_expires.prof_exp =
577 timer->it.cpu.expires.cpu;
578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case CPUCLOCK_SCHED:
Frank Mayharf06febc2008-09-12 09:54:39 -0700580 p->signal->cputime_expires.sched_exp =
581 timer->it.cpu.expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 }
584 }
585 }
586
587 spin_unlock(&p->sighand->siglock);
588}
589
590/*
591 * The timer is locked, fire it and arrange for its reload.
592 */
593static void cpu_timer_fire(struct k_itimer *timer)
594{
595 if (unlikely(timer->sigq == NULL)) {
596 /*
597 * This a special case for clock_nanosleep,
598 * not a normal timer from sys_timer_create.
599 */
600 wake_up_process(timer->it_process);
601 timer->it.cpu.expires.sched = 0;
602 } else if (timer->it.cpu.incr.sched == 0) {
603 /*
604 * One-shot timer. Clear it as soon as it's fired.
605 */
606 posix_timer_event(timer, 0);
607 timer->it.cpu.expires.sched = 0;
608 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
609 /*
610 * The signal did not get queued because the signal
611 * was ignored, so we won't get any callback to
612 * reload the timer. But we need to keep it
613 * ticking in case the signal is deliverable next time.
614 */
615 posix_cpu_timer_schedule(timer);
616 }
617}
618
619/*
620 * Guts of sys_timer_settime for CPU timers.
621 * This is called with the timer locked and interrupts disabled.
622 * If we return TIMER_RETRY, it's necessary to release the timer's lock
623 * and try again. (This happens when the timer is in the middle of firing.)
624 */
625int posix_cpu_timer_set(struct k_itimer *timer, int flags,
626 struct itimerspec *new, struct itimerspec *old)
627{
628 struct task_struct *p = timer->it.cpu.task;
629 union cpu_time_count old_expires, new_expires, val;
630 int ret;
631
632 if (unlikely(p == NULL)) {
633 /*
634 * Timer refers to a dead task's clock.
635 */
636 return -ESRCH;
637 }
638
639 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
640
641 read_lock(&tasklist_lock);
642 /*
643 * We need the tasklist_lock to protect against reaping that
644 * clears p->signal. If p has just been reaped, we can no
645 * longer get any information about it at all.
646 */
647 if (unlikely(p->signal == NULL)) {
648 read_unlock(&tasklist_lock);
649 put_task_struct(p);
650 timer->it.cpu.task = NULL;
651 return -ESRCH;
652 }
653
654 /*
655 * Disarm any old timer after extracting its expiry time.
656 */
657 BUG_ON(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400658
659 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 spin_lock(&p->sighand->siglock);
661 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400662 if (unlikely(timer->it.cpu.firing)) {
663 timer->it.cpu.firing = -1;
664 ret = TIMER_RETRY;
665 } else
666 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 spin_unlock(&p->sighand->siglock);
668
669 /*
670 * We need to sample the current value to convert the new
671 * value from to relative and absolute, and to convert the
672 * old value from absolute to relative. To set a process
673 * timer, we need a sample to balance the thread expiry
674 * times (in arm_timer). With an absolute time, we must
675 * check if it's already passed. In short, we need a sample.
676 */
677 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
678 cpu_clock_sample(timer->it_clock, p, &val);
679 } else {
680 cpu_clock_sample_group(timer->it_clock, p, &val);
681 }
682
683 if (old) {
684 if (old_expires.sched == 0) {
685 old->it_value.tv_sec = 0;
686 old->it_value.tv_nsec = 0;
687 } else {
688 /*
689 * Update the timer in case it has
690 * overrun already. If it has,
691 * we'll report it as having overrun
692 * and with the next reloaded timer
693 * already ticking, though we are
694 * swallowing that pending
695 * notification here to install the
696 * new setting.
697 */
698 bump_cpu_timer(timer, val);
699 if (cpu_time_before(timer->it_clock, val,
700 timer->it.cpu.expires)) {
701 old_expires = cpu_time_sub(
702 timer->it_clock,
703 timer->it.cpu.expires, val);
704 sample_to_timespec(timer->it_clock,
705 old_expires,
706 &old->it_value);
707 } else {
708 old->it_value.tv_nsec = 1;
709 old->it_value.tv_sec = 0;
710 }
711 }
712 }
713
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400714 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * We are colliding with the timer actually firing.
717 * Punt after filling in the timer's old value, and
718 * disable this firing since we are already reporting
719 * it as an overrun (thanks to bump_cpu_timer above).
720 */
721 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto out;
723 }
724
725 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
726 cpu_time_add(timer->it_clock, &new_expires, val);
727 }
728
729 /*
730 * Install the new expiry time (or zero).
731 * For a timer with no notification action, we don't actually
732 * arm the timer (we'll just fake it for timer_gettime).
733 */
734 timer->it.cpu.expires = new_expires;
735 if (new_expires.sched != 0 &&
736 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
737 cpu_time_before(timer->it_clock, val, new_expires)) {
738 arm_timer(timer, val);
739 }
740
741 read_unlock(&tasklist_lock);
742
743 /*
744 * Install the new reload setting, and
745 * set up the signal and overrun bookkeeping.
746 */
747 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
748 &new->it_interval);
749
750 /*
751 * This acts as a modification timestamp for the timer,
752 * so any automatic reload attempt will punt on seeing
753 * that we have reset the timer manually.
754 */
755 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
756 ~REQUEUE_PENDING;
757 timer->it_overrun_last = 0;
758 timer->it_overrun = -1;
759
760 if (new_expires.sched != 0 &&
761 (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
762 !cpu_time_before(timer->it_clock, val, new_expires)) {
763 /*
764 * The designated time already passed, so we notify
765 * immediately, even if the thread never runs to
766 * accumulate more time on this clock.
767 */
768 cpu_timer_fire(timer);
769 }
770
771 ret = 0;
772 out:
773 if (old) {
774 sample_to_timespec(timer->it_clock,
775 timer->it.cpu.incr, &old->it_interval);
776 }
777 return ret;
778}
779
780void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
781{
782 union cpu_time_count now;
783 struct task_struct *p = timer->it.cpu.task;
784 int clear_dead;
785
786 /*
787 * Easy part: convert the reload time.
788 */
789 sample_to_timespec(timer->it_clock,
790 timer->it.cpu.incr, &itp->it_interval);
791
792 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
793 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
794 return;
795 }
796
797 if (unlikely(p == NULL)) {
798 /*
799 * This task already died and the timer will never fire.
800 * In this case, expires is actually the dead value.
801 */
802 dead:
803 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
804 &itp->it_value);
805 return;
806 }
807
808 /*
809 * Sample the clock to take the difference with the expiry time.
810 */
811 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
812 cpu_clock_sample(timer->it_clock, p, &now);
813 clear_dead = p->exit_state;
814 } else {
815 read_lock(&tasklist_lock);
816 if (unlikely(p->signal == NULL)) {
817 /*
818 * The process has been reaped.
819 * We can't even collect a sample any more.
820 * Call the timer disarmed, nothing else to do.
821 */
822 put_task_struct(p);
823 timer->it.cpu.task = NULL;
824 timer->it.cpu.expires.sched = 0;
825 read_unlock(&tasklist_lock);
826 goto dead;
827 } else {
828 cpu_clock_sample_group(timer->it_clock, p, &now);
829 clear_dead = (unlikely(p->exit_state) &&
830 thread_group_empty(p));
831 }
832 read_unlock(&tasklist_lock);
833 }
834
835 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
836 if (timer->it.cpu.incr.sched == 0 &&
837 cpu_time_before(timer->it_clock,
838 timer->it.cpu.expires, now)) {
839 /*
840 * Do-nothing timer expired and has no reload,
841 * so it's as if it was never set.
842 */
843 timer->it.cpu.expires.sched = 0;
844 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
845 return;
846 }
847 /*
848 * Account for any expirations and reloads that should
849 * have happened.
850 */
851 bump_cpu_timer(timer, now);
852 }
853
854 if (unlikely(clear_dead)) {
855 /*
856 * We've noticed that the thread is dead, but
857 * not yet reaped. Take this opportunity to
858 * drop our task ref.
859 */
860 clear_dead_task(timer, now);
861 goto dead;
862 }
863
864 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
865 sample_to_timespec(timer->it_clock,
866 cpu_time_sub(timer->it_clock,
867 timer->it.cpu.expires, now),
868 &itp->it_value);
869 } else {
870 /*
871 * The timer should have expired already, but the firing
872 * hasn't taken place yet. Say it's just about to expire.
873 */
874 itp->it_value.tv_nsec = 1;
875 itp->it_value.tv_sec = 0;
876 }
877}
878
879/*
880 * Check for any per-thread CPU timers that have fired and move them off
881 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
882 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
883 */
884static void check_thread_timers(struct task_struct *tsk,
885 struct list_head *firing)
886{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700887 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100889 struct signal_struct *const sig = tsk->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvaldse80eda92005-10-23 10:02:50 -0700891 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700892 tsk->cputime_expires.prof_exp = cputime_zero;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700894 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct cpu_timer_list,
896 entry);
Linus Torvaldse80eda92005-10-23 10:02:50 -0700897 if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700898 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
900 }
901 t->firing = 1;
902 list_move_tail(&t->entry, firing);
903 }
904
905 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700906 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700907 tsk->cputime_expires.virt_exp = cputime_zero;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700909 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct cpu_timer_list,
911 entry);
Linus Torvaldse80eda92005-10-23 10:02:50 -0700912 if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700913 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 break;
915 }
916 t->firing = 1;
917 list_move_tail(&t->entry, firing);
918 }
919
920 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700921 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700922 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700924 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct cpu_timer_list,
926 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200927 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700928 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 }
931 t->firing = 1;
932 list_move_tail(&t->entry, firing);
933 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100934
935 /*
936 * Check for the special case thread timers.
937 */
938 if (sig->rlim[RLIMIT_RTTIME].rlim_cur != RLIM_INFINITY) {
939 unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
940 unsigned long *soft = &sig->rlim[RLIMIT_RTTIME].rlim_cur;
941
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100942 if (hard != RLIM_INFINITY &&
943 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100944 /*
945 * At the hard limit, we just die.
946 * No need to calculate anything else now.
947 */
948 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
949 return;
950 }
951 if (tsk->rt.timeout > DIV_ROUND_UP(*soft, USEC_PER_SEC/HZ)) {
952 /*
953 * At the soft limit, send a SIGXCPU every second.
954 */
955 if (sig->rlim[RLIMIT_RTTIME].rlim_cur
956 < sig->rlim[RLIMIT_RTTIME].rlim_max) {
957 sig->rlim[RLIMIT_RTTIME].rlim_cur +=
958 USEC_PER_SEC;
959 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -0700960 printk(KERN_INFO
961 "RT Watchdog Timeout: %s[%d]\n",
962 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100963 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
964 }
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * Check for any per-thread CPU timers that have fired and move them
970 * off the tsk->*_timers list onto the firing list. Per-thread timers
971 * have already been taken off.
972 */
973static void check_process_timers(struct task_struct *tsk,
974 struct list_head *firing)
975{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700976 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -0700978 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +0200979 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -0700981 struct task_cputime cputime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /*
984 * Don't sample the current process CPU clocks if there are no timers.
985 */
986 if (list_empty(&timers[CPUCLOCK_PROF]) &&
987 cputime_eq(sig->it_prof_expires, cputime_zero) &&
988 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
989 list_empty(&timers[CPUCLOCK_VIRT]) &&
990 cputime_eq(sig->it_virt_expires, cputime_zero) &&
991 list_empty(&timers[CPUCLOCK_SCHED]))
992 return;
993
994 /*
995 * Collect the current process totals.
996 */
Frank Mayharf06febc2008-09-12 09:54:39 -0700997 thread_group_cputime(tsk, &cputime);
998 utime = cputime.utime;
999 ptime = cputime_add(utime, cputime.stime);
1000 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001001 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 prof_expires = cputime_zero;
1003 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001004 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct cpu_timer_list,
1006 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001007 if (!--maxfire || cputime_lt(ptime, tl->expires.cpu)) {
1008 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 break;
1010 }
WANG Congee7dd202008-04-04 20:54:10 +02001011 tl->firing = 1;
1012 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001016 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 virt_expires = cputime_zero;
1018 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001019 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct cpu_timer_list,
1021 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001022 if (!--maxfire || cputime_lt(utime, tl->expires.cpu)) {
1023 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 }
WANG Congee7dd202008-04-04 20:54:10 +02001026 tl->firing = 1;
1027 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001031 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sched_expires = 0;
1033 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001034 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct cpu_timer_list,
1036 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001037 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1038 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040 }
WANG Congee7dd202008-04-04 20:54:10 +02001041 tl->firing = 1;
1042 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
1045 /*
1046 * Check for the special case process timers.
1047 */
1048 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1049 if (cputime_ge(ptime, sig->it_prof_expires)) {
1050 /* ITIMER_PROF fires and reloads. */
1051 sig->it_prof_expires = sig->it_prof_incr;
1052 if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1053 sig->it_prof_expires = cputime_add(
1054 sig->it_prof_expires, ptime);
1055 }
1056 __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1057 }
1058 if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1059 (cputime_eq(prof_expires, cputime_zero) ||
1060 cputime_lt(sig->it_prof_expires, prof_expires))) {
1061 prof_expires = sig->it_prof_expires;
1062 }
1063 }
1064 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1065 if (cputime_ge(utime, sig->it_virt_expires)) {
1066 /* ITIMER_VIRTUAL fires and reloads. */
1067 sig->it_virt_expires = sig->it_virt_incr;
1068 if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1069 sig->it_virt_expires = cputime_add(
1070 sig->it_virt_expires, utime);
1071 }
1072 __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1073 }
1074 if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1075 (cputime_eq(virt_expires, cputime_zero) ||
1076 cputime_lt(sig->it_virt_expires, virt_expires))) {
1077 virt_expires = sig->it_virt_expires;
1078 }
1079 }
1080 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1081 unsigned long psecs = cputime_to_secs(ptime);
1082 cputime_t x;
1083 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1084 /*
1085 * At the hard limit, we just die.
1086 * No need to calculate anything else now.
1087 */
1088 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1089 return;
1090 }
1091 if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1092 /*
1093 * At the soft limit, send a SIGXCPU every second.
1094 */
1095 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1096 if (sig->rlim[RLIMIT_CPU].rlim_cur
1097 < sig->rlim[RLIMIT_CPU].rlim_max) {
1098 sig->rlim[RLIMIT_CPU].rlim_cur++;
1099 }
1100 }
1101 x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1102 if (cputime_eq(prof_expires, cputime_zero) ||
1103 cputime_lt(x, prof_expires)) {
1104 prof_expires = x;
1105 }
1106 }
1107
Frank Mayharf06febc2008-09-12 09:54:39 -07001108 if (!cputime_eq(prof_expires, cputime_zero) &&
1109 (cputime_eq(sig->cputime_expires.prof_exp, cputime_zero) ||
1110 cputime_gt(sig->cputime_expires.prof_exp, prof_expires)))
1111 sig->cputime_expires.prof_exp = prof_expires;
1112 if (!cputime_eq(virt_expires, cputime_zero) &&
1113 (cputime_eq(sig->cputime_expires.virt_exp, cputime_zero) ||
1114 cputime_gt(sig->cputime_expires.virt_exp, virt_expires)))
1115 sig->cputime_expires.virt_exp = virt_expires;
1116 if (sched_expires != 0 &&
1117 (sig->cputime_expires.sched_exp == 0 ||
1118 sig->cputime_expires.sched_exp > sched_expires))
1119 sig->cputime_expires.sched_exp = sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/*
1123 * This is called from the signal code (via do_schedule_next_timer)
1124 * when the last timer signal was delivered and we have to reload the timer.
1125 */
1126void posix_cpu_timer_schedule(struct k_itimer *timer)
1127{
1128 struct task_struct *p = timer->it.cpu.task;
1129 union cpu_time_count now;
1130
1131 if (unlikely(p == NULL))
1132 /*
1133 * The task was cleaned up already, no future firings.
1134 */
Roland McGrath708f4302005-10-30 15:03:13 -08001135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /*
1138 * Fetch the current sample and update the timer's expiry time.
1139 */
1140 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1141 cpu_clock_sample(timer->it_clock, p, &now);
1142 bump_cpu_timer(timer, now);
1143 if (unlikely(p->exit_state)) {
1144 clear_dead_task(timer, now);
Roland McGrath708f4302005-10-30 15:03:13 -08001145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 read_lock(&tasklist_lock); /* arm_timer needs it. */
1148 } else {
1149 read_lock(&tasklist_lock);
1150 if (unlikely(p->signal == NULL)) {
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;
Roland McGrath708f4302005-10-30 15:03:13 -08001158 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 } 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);
Roland McGrath708f4302005-10-30 15:03:13 -08001166 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 cpu_clock_sample_group(timer->it_clock, p, &now);
1169 bump_cpu_timer(timer, now);
1170 /* Leave the tasklist_lock locked for the call below. */
1171 }
1172
1173 /*
1174 * Now re-arm for the new expiry time.
1175 */
1176 arm_timer(timer, now);
1177
Roland McGrath708f4302005-10-30 15:03:13 -08001178out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 read_unlock(&tasklist_lock);
Roland McGrath708f4302005-10-30 15:03:13 -08001180
1181out:
1182 timer->it_overrun_last = timer->it_overrun;
1183 timer->it_overrun = -1;
1184 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Frank Mayharf06febc2008-09-12 09:54:39 -07001187/**
1188 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1189 *
1190 * @cputime: The struct to compare.
1191 *
1192 * Checks @cputime to see if all fields are zero. Returns true if all fields
1193 * are zero, false if any field is nonzero.
1194 */
1195static inline int task_cputime_zero(const struct task_cputime *cputime)
1196{
1197 if (cputime_eq(cputime->utime, cputime_zero) &&
1198 cputime_eq(cputime->stime, cputime_zero) &&
1199 cputime->sum_exec_runtime == 0)
1200 return 1;
1201 return 0;
1202}
1203
1204/**
1205 * task_cputime_expired - Compare two task_cputime entities.
1206 *
1207 * @sample: The task_cputime structure to be checked for expiration.
1208 * @expires: Expiration times, against which @sample will be checked.
1209 *
1210 * Checks @sample against @expires to see if any field of @sample has expired.
1211 * Returns true if any field of the former is greater than the corresponding
1212 * field of the latter if the latter field is set. Otherwise returns false.
1213 */
1214static inline int task_cputime_expired(const struct task_cputime *sample,
1215 const struct task_cputime *expires)
1216{
1217 if (!cputime_eq(expires->utime, cputime_zero) &&
1218 cputime_ge(sample->utime, expires->utime))
1219 return 1;
1220 if (!cputime_eq(expires->stime, cputime_zero) &&
1221 cputime_ge(cputime_add(sample->utime, sample->stime),
1222 expires->stime))
1223 return 1;
1224 if (expires->sum_exec_runtime != 0 &&
1225 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1226 return 1;
1227 return 0;
1228}
1229
1230/**
1231 * fastpath_timer_check - POSIX CPU timers fast path.
1232 *
1233 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001234 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001235 * Check the task and thread group timers. If both are zero (there are no
1236 * timers set) return false. Otherwise snapshot the task and thread group
1237 * timers and compare them with the corresponding expiration times. Return
1238 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001239 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001240static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001241{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001242 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001243
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001244 /* tsk == current, ensure it is safe to use ->signal/sighand */
1245 if (unlikely(tsk->exit_state))
Frank Mayharf06febc2008-09-12 09:54:39 -07001246 return 0;
Frank Mayharbb34d922008-09-12 09:54:39 -07001247
1248 if (!task_cputime_zero(&tsk->cputime_expires)) {
1249 struct task_cputime task_sample = {
1250 .utime = tsk->utime,
1251 .stime = tsk->stime,
1252 .sum_exec_runtime = tsk->se.sum_exec_runtime
1253 };
1254
1255 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1256 return 1;
1257 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001258
1259 sig = tsk->signal;
Frank Mayharbb34d922008-09-12 09:54:39 -07001260 if (!task_cputime_zero(&sig->cputime_expires)) {
1261 struct task_cputime group_sample;
1262
1263 thread_group_cputime(tsk, &group_sample);
1264 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1265 return 1;
1266 }
1267 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271 * This is called from the timer interrupt handler. The irq handler has
1272 * already updated our counts. We need to check if any timers fire now.
1273 * Interrupts are disabled.
1274 */
1275void run_posix_cpu_timers(struct task_struct *tsk)
1276{
1277 LIST_HEAD(firing);
1278 struct k_itimer *timer, *next;
1279
1280 BUG_ON(!irqs_disabled());
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001283 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001284 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001286 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001287 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001288
Frank Mayharbb34d922008-09-12 09:54:39 -07001289 spin_lock(&tsk->sighand->siglock);
1290 /*
1291 * Here we take off tsk->signal->cpu_timers[N] and
1292 * tsk->cpu_timers[N] all the timers that are firing, and
1293 * put them on the firing list.
1294 */
1295 check_thread_timers(tsk, &firing);
1296 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Frank Mayharbb34d922008-09-12 09:54:39 -07001298 /*
1299 * We must release these locks before taking any timer's lock.
1300 * There is a potential race with timer deletion here, as the
1301 * siglock now protects our private firing list. We have set
1302 * the firing flag in each timer, so that a deletion attempt
1303 * that gets the timer lock before we do will give it up and
1304 * spin until we've taken care of that timer below.
1305 */
1306 spin_unlock(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
1309 * Now that all the timers on our list have the firing flag,
1310 * noone will touch their list entries but us. We'll take
1311 * each timer's lock before clearing its firing flag, so no
1312 * timer call will interfere.
1313 */
1314 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1315 int firing;
1316 spin_lock(&timer->it_lock);
1317 list_del_init(&timer->it.cpu.entry);
1318 firing = timer->it.cpu.firing;
1319 timer->it.cpu.firing = 0;
1320 /*
1321 * The firing flag is -1 if we collided with a reset
1322 * of the timer, which already reported this
1323 * almost-firing as an overrun. So don't generate an event.
1324 */
1325 if (likely(firing >= 0)) {
1326 cpu_timer_fire(timer);
1327 }
1328 spin_unlock(&timer->it_lock);
1329 }
1330}
1331
1332/*
1333 * Set one of the process-wide special case CPU timers.
Frank Mayharf06febc2008-09-12 09:54:39 -07001334 * The tsk->sighand->siglock must be held by the caller.
1335 * The *newval argument is relative and we update it to be absolute, *oldval
1336 * is absolute and we update it to be relative.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
1338void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1339 cputime_t *newval, cputime_t *oldval)
1340{
1341 union cpu_time_count now;
1342 struct list_head *head;
1343
1344 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Frank Mayharbb34d922008-09-12 09:54:39 -07001345 cpu_clock_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 if (oldval) {
1348 if (!cputime_eq(*oldval, cputime_zero)) {
1349 if (cputime_le(*oldval, now.cpu)) {
1350 /* Just about to fire. */
1351 *oldval = jiffies_to_cputime(1);
1352 } else {
1353 *oldval = cputime_sub(*oldval, now.cpu);
1354 }
1355 }
1356
1357 if (cputime_eq(*newval, cputime_zero))
1358 return;
1359 *newval = cputime_add(*newval, now.cpu);
1360
1361 /*
1362 * If the RLIMIT_CPU timer will expire before the
1363 * ITIMER_PROF timer, we have nothing else to do.
1364 */
1365 if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1366 < cputime_to_secs(*newval))
1367 return;
1368 }
1369
1370 /*
1371 * Check whether there are any process timers already set to fire
1372 * before this one. If so, we don't have anything more to do.
1373 */
1374 head = &tsk->signal->cpu_timers[clock_idx];
1375 if (list_empty(head) ||
Pavel Emelianovb5e61812007-05-08 00:30:19 -07001376 cputime_ge(list_first_entry(head,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 struct cpu_timer_list, entry)->expires.cpu,
1378 *newval)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07001379 switch (clock_idx) {
1380 case CPUCLOCK_PROF:
1381 tsk->signal->cputime_expires.prof_exp = *newval;
1382 break;
1383 case CPUCLOCK_VIRT:
1384 tsk->signal->cputime_expires.virt_exp = *newval;
1385 break;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388}
1389
Toyo Abee4b76552006-09-29 02:00:29 -07001390static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1391 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct k_itimer timer;
1394 int error;
1395
1396 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 * Set up a temporary timer and then wait for it to go off.
1398 */
1399 memset(&timer, 0, sizeof timer);
1400 spin_lock_init(&timer.it_lock);
1401 timer.it_clock = which_clock;
1402 timer.it_overrun = -1;
1403 error = posix_cpu_timer_create(&timer);
1404 timer.it_process = current;
1405 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001407
1408 memset(it, 0, sizeof *it);
1409 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001412 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (error) {
1414 spin_unlock_irq(&timer.it_lock);
1415 return error;
1416 }
1417
1418 while (!signal_pending(current)) {
1419 if (timer.it.cpu.expires.sched == 0) {
1420 /*
1421 * Our timer fired and was reset.
1422 */
1423 spin_unlock_irq(&timer.it_lock);
1424 return 0;
1425 }
1426
1427 /*
1428 * Block until cpu_timer_fire (or a signal) wakes us.
1429 */
1430 __set_current_state(TASK_INTERRUPTIBLE);
1431 spin_unlock_irq(&timer.it_lock);
1432 schedule();
1433 spin_lock_irq(&timer.it_lock);
1434 }
1435
1436 /*
1437 * We were interrupted by a signal.
1438 */
1439 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
Toyo Abee4b76552006-09-29 02:00:29 -07001440 posix_cpu_timer_set(&timer, 0, &zero_it, it);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 spin_unlock_irq(&timer.it_lock);
1442
Toyo Abee4b76552006-09-29 02:00:29 -07001443 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /*
1445 * It actually did fire already.
1446 */
1447 return 0;
1448 }
1449
Toyo Abee4b76552006-09-29 02:00:29 -07001450 error = -ERESTART_RESTARTBLOCK;
1451 }
1452
1453 return error;
1454}
1455
1456int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1457 struct timespec *rqtp, struct timespec __user *rmtp)
1458{
1459 struct restart_block *restart_block =
1460 &current_thread_info()->restart_block;
1461 struct itimerspec it;
1462 int error;
1463
1464 /*
1465 * Diagnose required errors first.
1466 */
1467 if (CPUCLOCK_PERTHREAD(which_clock) &&
1468 (CPUCLOCK_PID(which_clock) == 0 ||
1469 CPUCLOCK_PID(which_clock) == current->pid))
1470 return -EINVAL;
1471
1472 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1473
1474 if (error == -ERESTART_RESTARTBLOCK) {
1475
1476 if (flags & TIMER_ABSTIME)
1477 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
Toyo Abee4b76552006-09-29 02:00:29 -07001479 * Report back to the user the time still remaining.
1480 */
1481 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483
Toyo Abe1711ef32006-09-29 02:00:28 -07001484 restart_block->fn = posix_cpu_nsleep_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 restart_block->arg0 = which_clock;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001486 restart_block->arg1 = (unsigned long) rmtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 restart_block->arg2 = rqtp->tv_sec;
1488 restart_block->arg3 = rqtp->tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return error;
1491}
1492
Toyo Abe1711ef32006-09-29 02:00:28 -07001493long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 clockid_t which_clock = restart_block->arg0;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001496 struct timespec __user *rmtp;
1497 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001498 struct itimerspec it;
1499 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001500
1501 rmtp = (struct timespec __user *) restart_block->arg1;
1502 t.tv_sec = restart_block->arg2;
1503 t.tv_nsec = restart_block->arg3;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 restart_block->fn = do_no_restart_syscall;
Toyo Abee4b76552006-09-29 02:00:29 -07001506 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1507
1508 if (error == -ERESTART_RESTARTBLOCK) {
1509 /*
1510 * Report back to the user the time still remaining.
1511 */
1512 if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1513 return -EFAULT;
1514
1515 restart_block->fn = posix_cpu_nsleep_restart;
1516 restart_block->arg0 = which_clock;
1517 restart_block->arg1 = (unsigned long) rmtp;
1518 restart_block->arg2 = t.tv_sec;
1519 restart_block->arg3 = t.tv_nsec;
1520 }
1521 return error;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525
1526#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1527#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1528
Thomas Gleixnera924b042006-01-09 20:52:27 -08001529static int process_cpu_clock_getres(const clockid_t which_clock,
1530 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1533}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001534static int process_cpu_clock_get(const clockid_t which_clock,
1535 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1538}
1539static int process_cpu_timer_create(struct k_itimer *timer)
1540{
1541 timer->it_clock = PROCESS_CLOCK;
1542 return posix_cpu_timer_create(timer);
1543}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001544static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001545 struct timespec *rqtp,
1546 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001548 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
Toyo Abe1711ef32006-09-29 02:00:28 -07001550static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1551{
1552 return -EINVAL;
1553}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001554static int thread_cpu_clock_getres(const clockid_t which_clock,
1555 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1558}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001559static int thread_cpu_clock_get(const clockid_t which_clock,
1560 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1563}
1564static int thread_cpu_timer_create(struct k_itimer *timer)
1565{
1566 timer->it_clock = THREAD_CLOCK;
1567 return posix_cpu_timer_create(timer);
1568}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001569static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001570 struct timespec *rqtp, struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 return -EINVAL;
1573}
Toyo Abe1711ef32006-09-29 02:00:28 -07001574static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1575{
1576 return -EINVAL;
1577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579static __init int init_posix_cpu_timers(void)
1580{
1581 struct k_clock process = {
1582 .clock_getres = process_cpu_clock_getres,
1583 .clock_get = process_cpu_clock_get,
1584 .clock_set = do_posix_clock_nosettime,
1585 .timer_create = process_cpu_timer_create,
1586 .nsleep = process_cpu_nsleep,
Toyo Abe1711ef32006-09-29 02:00:28 -07001587 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 };
1589 struct k_clock thread = {
1590 .clock_getres = thread_cpu_clock_getres,
1591 .clock_get = thread_cpu_clock_get,
1592 .clock_set = do_posix_clock_nosettime,
1593 .timer_create = thread_cpu_timer_create,
1594 .nsleep = thread_cpu_nsleep,
Toyo Abe1711ef32006-09-29 02:00:28 -07001595 .nsleep_restart = thread_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 };
1597
1598 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1599 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1600
1601 return 0;
1602}
1603__initcall(init_posix_cpu_timers);