blob: d73840271dcee993936b48cc9e7f7d5f66a5c546 [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>
Xiao Guangrong3f0a5252009-08-10 10:52:30 +080011#include <trace/events/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Frank Mayharf06febc2008-09-12 09:54:39 -070013/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080014 * 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.
Frank Mayharf06febc2008-09-12 09:54:39 -070018 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020019void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070020{
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020021 cputime_t cputime = secs_to_cputime(rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -070022
Jiri Slaby5ab46b32009-08-28 14:05:12 +020023 spin_lock_irq(&task->sighand->siglock);
24 set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL);
25 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070026}
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
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020040 rcu_read_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) ?
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020043 same_thread_group(p, current) : has_group_leader_pid(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 error = -EINVAL;
45 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020046 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
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 {
Martin Schwidefsky64861632011-12-15 14:56:09 +010081 return now.cpu < then.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
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 {
Martin Schwidefsky64861632011-12-15 14:56:09 +010091 acc->cpu += val.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
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 {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100101 a.cpu -= b.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 return a;
104}
105
106/*
107 * Update expiry time from increment, and increase overrun count,
108 * given the current clock sample.
109 */
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400110static void bump_cpu_timer(struct k_itimer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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--) {
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400129 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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
Martin Schwidefsky64861632011-12-15 14:56:09 +0100138 if (now.cpu < timer->it.cpu.expires.cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return;
140 incr = timer->it.cpu.incr.cpu;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100141 delta = now.cpu + incr - timer->it.cpu.expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Don't use (incr*2 < delta), incr*2 might overflow. */
Martin Schwidefsky64861632011-12-15 14:56:09 +0100143 for (i = 0; incr < delta - incr; i++)
144 incr += incr;
145 for (; i >= 0; incr = incr >> 1, i--) {
146 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 continue;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100148 timer->it.cpu.expires.cpu += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 timer->it_overrun += 1 << i;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100150 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 }
153}
154
155static inline cputime_t prof_ticks(struct task_struct *p)
156{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100157 return p->utime + p->stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159static inline cputime_t virt_ticks(struct task_struct *p)
160{
161 return p->utime;
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000164static int
165posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
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
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000183static int
184posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
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 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800201static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 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:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900214 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 }
217 return 0;
218}
219
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100220static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
221{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100222 if (b->utime > a->utime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100223 a->utime = b->utime;
224
Martin Schwidefsky64861632011-12-15 14:56:09 +0100225 if (b->stime > a->stime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100226 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
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100238 if (!cputimer->running) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100239 /*
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);
Linus Torvalds3cfef952011-10-26 16:17:32 +0200246 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200247 cputimer->running = 1;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100248 update_gt_cputime(&cputimer->cputime, &sum);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200249 } else
Linus Torvalds3cfef952011-10-26 16:17:32 +0200250 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100251 *times = cputimer->cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200252 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * Sample a process (thread group) clock for the given group_leader task.
257 * Must be called with tasklist_lock held for reading.
258 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800259static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct task_struct *p,
261 union cpu_time_count *cpu)
262{
Frank Mayharbb34d922008-09-12 09:54:39 -0700263 struct task_cputime cputime;
264
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100265 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700266 default:
267 return -EINVAL;
268 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900269 thread_group_cputime(p, &cputime);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100270 cpu->cpu = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700271 break;
272 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900273 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700274 cpu->cpu = cputime.utime;
275 break;
276 case CPUCLOCK_SCHED:
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200277 thread_group_cputime(p, &cputime);
278 cpu->sched = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700279 break;
280 }
281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000285static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
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;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800314 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800315 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (p) {
317 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700318 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 error = cpu_clock_sample(which_clock,
320 p, &rtn);
321 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800322 } else {
323 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700324 if (thread_group_leader(p) && p->sighand) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800325 error =
326 cpu_clock_sample_group(which_clock,
327 p, &rtn);
328 }
329 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800332 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
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.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800344 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
345 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000347static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200358 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
360 if (pid == 0) {
361 p = current;
362 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800363 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700364 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 p = NULL;
366 }
367 } else {
368 if (pid == 0) {
369 p = current->group_leader;
370 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800371 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200372 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 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 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200382 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
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 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000393static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400396 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Oleg Nesterov108150e2005-10-23 20:25:39 +0400398 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700399 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700400 if (unlikely(p->sighand == NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700401 /*
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 {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700407 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400408 if (timer->it.cpu.firing)
409 ret = TIMER_RETRY;
410 else
411 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700412 spin_unlock(&p->sighand->siglock);
413 }
414 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Oleg Nesterov108150e2005-10-23 20:25:39 +0400416 if (!ret)
417 put_task_struct(p);
418 }
419
420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
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,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200431 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct cpu_timer_list *timer, *next;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100434 cputime_t ptime = utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100438 if (timer->expires.cpu < ptime) {
439 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100441 timer->expires.cpu -= ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444
445 ++head;
446 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100448 if (timer->expires.cpu < utime) {
449 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100451 timer->expires.cpu -= utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 }
454
455 ++head;
456 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200458 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 timer->expires.sched = 0;
460 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200461 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
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,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200474 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476}
477void posix_cpu_timers_exit_group(struct task_struct *tsk)
478{
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700479 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 cleanup_timers(tsk->signal->cpu_timers,
Martin Schwidefsky64861632011-12-15 14:56:09 +0100482 tsk->utime + sig->utime, tsk->stime + sig->stime,
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700483 tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
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
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200499static inline int expires_gt(cputime_t expires, cputime_t new_exp)
500{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100501 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
505 * Insert the timer on the appropriate list before any timers that
506 * expire later. This must be called with the tasklist_lock held
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800507 * for reading, interrupts disabled and p->sighand->siglock taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800509static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct task_struct *p = timer->it.cpu.task;
512 struct list_head *head, *listpos;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800513 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct cpu_timer_list *const nt = &timer->it.cpu;
515 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800517 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 head += CPUCLOCK_WHICH(timer->it_clock);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800527 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 list_add(&nt->entry, listpos);
533
534 if (listpos == head) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800535 union cpu_time_count *exp = &nt->expires;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800538 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800544 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
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{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800567 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /*
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
596/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100597 * 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:
Martin Schwidefsky64861632011-12-15 14:56:09 +0100611 cpu->cpu = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100612 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
623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * 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 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000629static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
630 struct itimerspec *new, struct itimerspec *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct task_struct *p = timer->it.cpu.task;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800633 union cpu_time_count old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 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
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700648 * clears p->sighand. If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * longer get any information about it at all.
650 */
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700651 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 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());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400662
663 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800664 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock(&p->sighand->siglock);
666 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400667 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
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 {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100684 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
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
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400718 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
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 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800725 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 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 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 cpu_time_before(timer->it_clock, val, new_expires)) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800742 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800745 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 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 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 !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,
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800779 old_incr, &old->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781 return ret;
782}
783
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000784static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
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);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700820 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
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 {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100832 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 clear_dead = (unlikely(p->exit_state) &&
834 thread_group_empty(p));
835 }
836 read_unlock(&tasklist_lock);
837 }
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 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{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700872 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100874 struct signal_struct *const sig = tsk->signal;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800875 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvaldse80eda92005-10-23 10:02:50 -0700877 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100878 tsk->cputime_expires.prof_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700880 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct cpu_timer_list,
882 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100883 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700884 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
886 }
887 t->firing = 1;
888 list_move_tail(&t->entry, firing);
889 }
890
891 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700892 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100893 tsk->cputime_expires.virt_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700895 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct cpu_timer_list,
897 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100898 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700899 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901 }
902 t->firing = 1;
903 list_move_tail(&t->entry, firing);
904 }
905
906 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700907 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700908 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700910 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct cpu_timer_list,
912 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200913 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700914 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 }
917 t->firing = 1;
918 list_move_tail(&t->entry, firing);
919 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100920
921 /*
922 * Check for the special case thread timers.
923 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800924 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800925 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800926 unsigned long hard =
927 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100928
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100929 if (hard != RLIM_INFINITY &&
930 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100931 /*
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 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800938 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100939 /*
940 * At the soft limit, send a SIGXCPU every second.
941 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800942 if (soft < hard) {
943 soft += USEC_PER_SEC;
944 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100945 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -0700946 printk(KERN_INFO
947 "RT Watchdog Timeout: %s[%d]\n",
948 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100949 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
950 }
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800954static void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100955{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800956 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100957 unsigned long flags;
958
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200959 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100960 cputimer->running = 0;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200961 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100962}
963
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200964static u32 onecputick;
965
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200966static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
967 cputime_t *expires, cputime_t cur_time, int signo)
968{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100969 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200970 return;
971
Martin Schwidefsky64861632011-12-15 14:56:09 +0100972 if (cur_time >= it->expires) {
973 if (it->incr) {
974 it->expires += it->incr;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200975 it->error += it->incr_error;
976 if (it->error >= onecputick) {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100977 it->expires -= cputime_one_jiffy;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200978 it->error -= onecputick;
979 }
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800980 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100981 it->expires = 0;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800982 }
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200983
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800984 trace_itimer_expire(signo == SIGPROF ?
985 ITIMER_PROF : ITIMER_VIRTUAL,
986 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200987 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
988 }
989
Martin Schwidefsky64861632011-12-15 14:56:09 +0100990 if (it->expires && (!*expires || it->expires < *expires)) {
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200991 *expires = it->expires;
992 }
993}
994
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -0700995/**
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{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001005 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001006 return 1;
1007 return 0;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
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{
Linus Torvaldse80eda92005-10-23 10:02:50 -07001018 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -07001020 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +02001021 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -07001023 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001024 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * Collect the current process totals.
1028 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001029 thread_group_cputimer(tsk, &cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001030 utime = cputime.utime;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001031 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001032 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001033 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001034 prof_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001036 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct cpu_timer_list,
1038 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001039 if (!--maxfire || ptime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001040 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042 }
WANG Congee7dd202008-04-04 20:54:10 +02001043 tl->firing = 1;
1044 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
1047 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001048 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001049 virt_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001051 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct cpu_timer_list,
1053 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001054 if (!--maxfire || utime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001055 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057 }
WANG Congee7dd202008-04-04 20:54:10 +02001058 tl->firing = 1;
1059 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061
1062 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001063 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sched_expires = 0;
1065 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001066 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct cpu_timer_list,
1068 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001069 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1070 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072 }
WANG Congee7dd202008-04-04 20:54:10 +02001073 tl->firing = 1;
1074 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 /*
1078 * Check for the special case process timers.
1079 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001080 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);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001084 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001085 if (soft != RLIM_INFINITY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 unsigned long psecs = cputime_to_secs(ptime);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001087 unsigned long hard =
1088 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 cputime_t x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001090 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /*
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 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001098 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * At the soft limit, send a SIGXCPU every second.
1101 */
1102 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001103 if (soft < hard) {
1104 soft++;
1105 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001108 x = secs_to_cputime(soft);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001109 if (!prof_expires || x < prof_expires) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 prof_expires = x;
1111 }
1112 }
1113
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001114 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
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 */
Roland McGrath708f4302005-10-30 15:03:13 -08001134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
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);
Roland McGrath708f4302005-10-30 15:03:13 -08001144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 read_lock(&tasklist_lock); /* arm_timer needs it. */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001147 spin_lock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 } else {
1149 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -07001150 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
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 }
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001168 spin_lock(&p->sighand->siglock);
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001169 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 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 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001177 BUG_ON(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001178 arm_timer(timer);
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001179 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Roland McGrath708f4302005-10-30 15:03:13 -08001181out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 read_unlock(&tasklist_lock);
Roland McGrath708f4302005-10-30 15:03:13 -08001183
1184out:
1185 timer->it_overrun_last = timer->it_overrun;
1186 timer->it_overrun = -1;
1187 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Frank Mayharf06febc2008-09-12 09:54:39 -07001190/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001191 * 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{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001203 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001204 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001205 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001206 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.
Frank Mayharf06febc2008-09-12 09:54:39 -07001217 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001218 * 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.
Frank Mayharf06febc2008-09-12 09:54:39 -07001222 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001223static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001224{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001225 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001226
Frank Mayharbb34d922008-09-12 09:54:39 -07001227 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 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001237
1238 sig = tsk->signal;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001239 if (sig->cputimer.running) {
Frank Mayharbb34d922008-09-12 09:54:39 -07001240 struct task_cputime group_sample;
1241
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001242 raw_spin_lock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001243 group_sample = sig->cputimer.cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001244 raw_spin_unlock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001245
Frank Mayharbb34d922008-09-12 09:54:39 -07001246 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1247 return 1;
1248 }
Oleg Nesterov37bebc702009-03-23 20:34:11 +01001249
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001250 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/*
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;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001262 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 BUG_ON(!irqs_disabled());
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001267 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001268 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001270 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001271 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001272
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001273 if (!lock_task_sighand(tsk, &flags))
1274 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001275 /*
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);
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001281 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Frank Mayharbb34d922008-09-12 09:54:39 -07001288 /*
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 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001296 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /*
1299 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001300 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * 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) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001305 int cpu_firing;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 spin_lock(&timer->it_lock);
1308 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001309 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 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 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001316 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 spin_unlock(&timer->it_lock);
1319 }
1320}
1321
1322/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001323 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001324 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001332 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001335 /*
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 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001340 if (*oldval) {
1341 if (*oldval <= now.cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /* Just about to fire. */
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001343 *oldval = cputime_one_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001345 *oldval -= now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 }
1348
Martin Schwidefsky64861632011-12-15 14:56:09 +01001349 if (!*newval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001351 *newval += now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
1354 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001355 * Update expiration cache if we are the earliest timer, or eventually
1356 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001358 switch (clock_idx) {
1359 case CPUCLOCK_PROF:
1360 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001361 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001362 break;
1363 case CPUCLOCK_VIRT:
1364 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001365 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368}
1369
Toyo Abee4b76552006-09-29 02:00:29 -07001370static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1371 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct k_itimer timer;
1374 int error;
1375
1376 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001387
1388 memset(it, 0, sizeof *it);
1389 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001392 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 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);
Toyo Abee4b76552006-09-29 02:00:29 -07001420 posix_cpu_timer_set(&timer, 0, &zero_it, it);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 spin_unlock_irq(&timer.it_lock);
1422
Toyo Abee4b76552006-09-29 02:00:29 -07001423 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /*
1425 * It actually did fire already.
1426 */
1427 return 0;
1428 }
1429
Toyo Abee4b76552006-09-29 02:00:29 -07001430 error = -ERESTART_RESTARTBLOCK;
1431 }
1432
1433 return error;
1434}
1435
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001436static 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)
Toyo Abee4b76552006-09-29 02:00:29 -07001440{
1441 struct restart_block *restart_block =
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001442 &current_thread_info()->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001443 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
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001458 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001459 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001461 * Report back to the user the time still remaining.
1462 */
1463 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EFAULT;
1465
Toyo Abe1711ef32006-09-29 02:00:28 -07001466 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001467 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001468 restart_block->nanosleep.rmtp = rmtp;
1469 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return error;
1472}
1473
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001474static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001476 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001477 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001478 struct itimerspec it;
1479 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001480
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001481 t = ns_to_timespec(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001482
Toyo Abee4b76552006-09-29 02:00:29 -07001483 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1484
1485 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001486 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001487 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001488 * Report back to the user the time still remaining.
1489 */
1490 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Toyo Abee4b76552006-09-29 02:00:29 -07001491 return -EFAULT;
1492
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001493 restart_block->nanosleep.expires = timespec_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001494 }
1495 return error;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1500#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1501
Thomas Gleixnera924b042006-01-09 20:52:27 -08001502static int process_cpu_clock_getres(const clockid_t which_clock,
1503 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1506}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001507static int process_cpu_clock_get(const clockid_t which_clock,
1508 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
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}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001517static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001518 struct timespec *rqtp,
1519 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001521 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
Toyo Abe1711ef32006-09-29 02:00:28 -07001523static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1524{
1525 return -EINVAL;
1526}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001527static int thread_cpu_clock_getres(const clockid_t which_clock,
1528 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1531}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001532static int thread_cpu_clock_get(const clockid_t which_clock,
1533 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Gleixner19769452011-02-01 13:51:06 +00001543struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555static __init int init_posix_cpu_timers(void)
1556{
1557 struct k_clock process = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001558 .clock_getres = process_cpu_clock_getres,
1559 .clock_get = process_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001560 .timer_create = process_cpu_timer_create,
1561 .nsleep = process_cpu_nsleep,
1562 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 };
1564 struct k_clock thread = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001565 .clock_getres = thread_cpu_clock_getres,
1566 .clock_get = thread_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001567 .timer_create = thread_cpu_timer_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 };
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001569 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Thomas Gleixner52708732011-02-02 12:10:09 +01001571 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1572 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001574 cputime_to_timespec(cputime_one_jiffy, &ts);
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001575 onecputick = ts.tv_nsec;
1576 WARN_ON(ts.tv_sec != 0);
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580__initcall(init_posix_cpu_timers);