blob: f07827a4e281b5658d585aa2d786009dc2bd818c [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>
Nick Kossifidis61337052012-12-16 22:18:11 -050012#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Frank Mayharf06febc2008-09-12 09:54:39 -070014/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080015 * Called after updating RLIMIT_CPU to run cpu timer and update
16 * tsk->signal->cputime_expires expiration cache if necessary. Needs
17 * siglock protection since other code may update expiration cache as
18 * well.
Frank Mayharf06febc2008-09-12 09:54:39 -070019 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020020void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070021{
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020022 cputime_t cputime = secs_to_cputime(rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -070023
Jiri Slaby5ab46b32009-08-28 14:05:12 +020024 spin_lock_irq(&task->sighand->siglock);
25 set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL);
26 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070027}
28
Thomas Gleixnera924b042006-01-09 20:52:27 -080029static int check_clock(const clockid_t which_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 int error = 0;
32 struct task_struct *p;
33 const pid_t pid = CPUCLOCK_PID(which_clock);
34
35 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
36 return -EINVAL;
37
38 if (pid == 0)
39 return 0;
40
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020041 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -080042 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -070043 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020044 same_thread_group(p, current) : has_group_leader_pid(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 error = -EINVAL;
46 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020047 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 return error;
50}
51
52static inline union cpu_time_count
Thomas Gleixnera924b042006-01-09 20:52:27 -080053timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 union cpu_time_count ret;
56 ret.sched = 0; /* high half always zero when .cpu used */
57 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
Oleg Nesterovee500f22005-11-28 13:43:55 -080058 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 } else {
60 ret.cpu = timespec_to_cputime(tp);
61 }
62 return ret;
63}
64
Thomas Gleixnera924b042006-01-09 20:52:27 -080065static void sample_to_timespec(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 union cpu_time_count cpu,
67 struct timespec *tp)
68{
Roman Zippelf8bd2252008-05-01 04:34:31 -070069 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
70 *tp = ns_to_timespec(cpu.sched);
71 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 cputime_to_timespec(cpu.cpu, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Thomas Gleixnera924b042006-01-09 20:52:27 -080075static inline int cpu_time_before(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 union cpu_time_count now,
77 union cpu_time_count then)
78{
79 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
80 return now.sched < then.sched;
81 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +010082 return now.cpu < then.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
Thomas Gleixnera924b042006-01-09 20:52:27 -080085static inline void cpu_time_add(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 union cpu_time_count *acc,
87 union cpu_time_count val)
88{
89 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
90 acc->sched += val.sched;
91 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +010092 acc->cpu += val.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94}
Thomas Gleixnera924b042006-01-09 20:52:27 -080095static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 union cpu_time_count a,
97 union cpu_time_count b)
98{
99 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
100 a.sched -= b.sched;
101 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100102 a.cpu -= b.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 return a;
105}
106
107/*
108 * Update expiry time from increment, and increase overrun count,
109 * given the current clock sample.
110 */
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400111static void bump_cpu_timer(struct k_itimer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 union cpu_time_count now)
113{
114 int i;
115
116 if (timer->it.cpu.incr.sched == 0)
117 return;
118
119 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
120 unsigned long long delta, incr;
121
122 if (now.sched < timer->it.cpu.expires.sched)
123 return;
124 incr = timer->it.cpu.incr.sched;
125 delta = now.sched + incr - timer->it.cpu.expires.sched;
126 /* Don't use (incr*2 < delta), incr*2 might overflow. */
127 for (i = 0; incr < delta - incr; i++)
128 incr = incr << 1;
129 for (; i >= 0; incr >>= 1, i--) {
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400130 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 continue;
132 timer->it.cpu.expires.sched += incr;
133 timer->it_overrun += 1 << i;
134 delta -= incr;
135 }
136 } else {
137 cputime_t delta, incr;
138
Martin Schwidefsky64861632011-12-15 14:56:09 +0100139 if (now.cpu < timer->it.cpu.expires.cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return;
141 incr = timer->it.cpu.incr.cpu;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100142 delta = now.cpu + incr - timer->it.cpu.expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Don't use (incr*2 < delta), incr*2 might overflow. */
Martin Schwidefsky64861632011-12-15 14:56:09 +0100144 for (i = 0; incr < delta - incr; i++)
145 incr += incr;
146 for (; i >= 0; incr = incr >> 1, i--) {
147 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 continue;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100149 timer->it.cpu.expires.cpu += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 timer->it_overrun += 1 << i;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100151 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
154}
155
156static inline cputime_t prof_ticks(struct task_struct *p)
157{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100158 return p->utime + p->stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160static inline cputime_t virt_ticks(struct task_struct *p)
161{
162 return p->utime;
163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000165static int
166posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int error = check_clock(which_clock);
169 if (!error) {
170 tp->tv_sec = 0;
171 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
172 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
173 /*
174 * If sched_clock is using a cycle counter, we
175 * don't have any idea of its true resolution
176 * exported, but it is much more than 1s/HZ.
177 */
178 tp->tv_nsec = 1;
179 }
180 }
181 return error;
182}
183
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000184static int
185posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 /*
188 * You can never reset a CPU clock, but we check for other errors
189 * in the call before failing with EPERM.
190 */
191 int error = check_clock(which_clock);
192 if (error == 0) {
193 error = -EPERM;
194 }
195 return error;
196}
197
198
199/*
200 * Sample a per-thread clock for the given task.
201 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800202static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 union cpu_time_count *cpu)
204{
205 switch (CPUCLOCK_WHICH(which_clock)) {
206 default:
207 return -EINVAL;
208 case CPUCLOCK_PROF:
209 cpu->cpu = prof_ticks(p);
210 break;
211 case CPUCLOCK_VIRT:
212 cpu->cpu = virt_ticks(p);
213 break;
214 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900215 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 }
218 return 0;
219}
220
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100221void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
222{
Oleg Nesterovbfac7002010-06-11 01:09:56 +0200223 struct signal_struct *sig = tsk->signal;
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100224 struct task_struct *t;
225
Oleg Nesterovbfac7002010-06-11 01:09:56 +0200226 times->utime = sig->utime;
227 times->stime = sig->stime;
228 times->sum_exec_runtime = sig->sum_sched_runtime;
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100229
230 rcu_read_lock();
Oleg Nesterovbfac7002010-06-11 01:09:56 +0200231 /* make sure we can trust tsk->thread_group list */
232 if (!likely(pid_alive(tsk)))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100233 goto out;
234
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100235 t = tsk;
236 do {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100237 times->utime += t->utime;
238 times->stime += t->stime;
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200239 times->sum_exec_runtime += task_sched_runtime(t);
Oleg Nesterovbfac7002010-06-11 01:09:56 +0200240 } while_each_thread(tsk, t);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100241out:
242 rcu_read_unlock();
243}
244
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100245static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
246{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100247 if (b->utime > a->utime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100248 a->utime = b->utime;
249
Martin Schwidefsky64861632011-12-15 14:56:09 +0100250 if (b->stime > a->stime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100251 a->stime = b->stime;
252
253 if (b->sum_exec_runtime > a->sum_exec_runtime)
254 a->sum_exec_runtime = b->sum_exec_runtime;
255}
256
257void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
258{
259 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
260 struct task_cputime sum;
261 unsigned long flags;
262
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100263 if (!cputimer->running) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100264 /*
265 * The POSIX timer interface allows for absolute time expiry
266 * values through the TIMER_ABSTIME flag, therefore we have
267 * to synchronize the timer to the clock every time we start
268 * it.
269 */
270 thread_group_cputime(tsk, &sum);
Linus Torvalds3cfef952011-10-26 16:17:32 +0200271 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200272 cputimer->running = 1;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100273 update_gt_cputime(&cputimer->cputime, &sum);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200274 } else
Linus Torvalds3cfef952011-10-26 16:17:32 +0200275 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100276 *times = cputimer->cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200277 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * Sample a process (thread group) clock for the given group_leader task.
282 * Must be called with tasklist_lock held for reading.
283 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800284static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct task_struct *p,
286 union cpu_time_count *cpu)
287{
Frank Mayharbb34d922008-09-12 09:54:39 -0700288 struct task_cputime cputime;
289
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100290 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700291 default:
292 return -EINVAL;
293 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900294 thread_group_cputime(p, &cputime);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100295 cpu->cpu = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700296 break;
297 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900298 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700299 cpu->cpu = cputime.utime;
300 break;
301 case CPUCLOCK_SCHED:
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200302 thread_group_cputime(p, &cputime);
303 cpu->sched = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700304 break;
305 }
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000310static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 const pid_t pid = CPUCLOCK_PID(which_clock);
313 int error = -EINVAL;
314 union cpu_time_count rtn;
315
316 if (pid == 0) {
317 /*
318 * Special case constant value for our own clocks.
319 * We don't have to do any lookup to find ourselves.
320 */
321 if (CPUCLOCK_PERTHREAD(which_clock)) {
322 /*
323 * Sampling just ourselves we can do with no locking.
324 */
325 error = cpu_clock_sample(which_clock,
326 current, &rtn);
327 } else {
328 read_lock(&tasklist_lock);
329 error = cpu_clock_sample_group(which_clock,
330 current, &rtn);
331 read_unlock(&tasklist_lock);
332 }
333 } else {
334 /*
335 * Find the given PID, and validate that the caller
336 * should be able to see it.
337 */
338 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800339 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800340 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (p) {
342 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700343 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 error = cpu_clock_sample(which_clock,
345 p, &rtn);
346 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800347 } else {
348 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700349 if (thread_group_leader(p) && p->sighand) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800350 error =
351 cpu_clock_sample_group(which_clock,
352 p, &rtn);
353 }
354 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800357 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 if (error)
361 return error;
362 sample_to_timespec(which_clock, rtn, tp);
363 return 0;
364}
365
366
367/*
368 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800369 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
370 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000372static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int ret = 0;
375 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
376 struct task_struct *p;
377
378 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
379 return -EINVAL;
380
381 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200383 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
385 if (pid == 0) {
386 p = current;
387 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800388 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700389 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 p = NULL;
391 }
392 } else {
393 if (pid == 0) {
394 p = current->group_leader;
395 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800396 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200397 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 p = NULL;
399 }
400 }
401 new_timer->it.cpu.task = p;
402 if (p) {
403 get_task_struct(p);
404 } else {
405 ret = -EINVAL;
406 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 return ret;
410}
411
412/*
413 * Clean up a CPU-clock timer that is about to be destroyed.
414 * This is called from timer deletion with the timer already locked.
415 * If we return TIMER_RETRY, it's necessary to release the timer's lock
416 * and try again. (This happens when the timer is in the middle of firing.)
417 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000418static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400421 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Oleg Nesterov108150e2005-10-23 20:25:39 +0400423 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700424 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700425 if (unlikely(p->sighand == NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700426 /*
427 * We raced with the reaping of the task.
428 * The deletion should have cleared us off the list.
429 */
430 BUG_ON(!list_empty(&timer->it.cpu.entry));
431 } else {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700432 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400433 if (timer->it.cpu.firing)
434 ret = TIMER_RETRY;
435 else
436 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700437 spin_unlock(&p->sighand->siglock);
438 }
439 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Oleg Nesterov108150e2005-10-23 20:25:39 +0400441 if (!ret)
442 put_task_struct(p);
443 }
444
445 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Clean out CPU timers still ticking when a thread exited. The task
450 * pointer is cleared, and the expiry time is replaced with the residual
451 * time for later timer_gettime calls to return.
452 * This must be called with the siglock held.
453 */
454static void cleanup_timers(struct list_head *head,
455 cputime_t utime, cputime_t stime,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200456 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct cpu_timer_list *timer, *next;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100459 cputime_t ptime = utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100463 if (timer->expires.cpu < ptime) {
464 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100466 timer->expires.cpu -= ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 }
469
470 ++head;
471 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100473 if (timer->expires.cpu < utime) {
474 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100476 timer->expires.cpu -= utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479
480 ++head;
481 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200483 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 timer->expires.sched = 0;
485 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200486 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 }
489}
490
491/*
492 * These are both called with the siglock held, when the current thread
493 * is being reaped. When the final (leader) thread in the group is reaped,
494 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
495 */
496void posix_cpu_timers_exit(struct task_struct *tsk)
497{
Nick Kossifidis61337052012-12-16 22:18:11 -0500498 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
499 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 cleanup_timers(tsk->cpu_timers,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200501 tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503}
504void posix_cpu_timers_exit_group(struct task_struct *tsk)
505{
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700506 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 cleanup_timers(tsk->signal->cpu_timers,
Martin Schwidefsky64861632011-12-15 14:56:09 +0100509 tsk->utime + sig->utime, tsk->stime + sig->stime,
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700510 tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
514{
515 /*
516 * That's all for this thread or process.
517 * We leave our residual in expires to be reported.
518 */
519 put_task_struct(timer->it.cpu.task);
520 timer->it.cpu.task = NULL;
521 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
522 timer->it.cpu.expires,
523 now);
524}
525
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200526static inline int expires_gt(cputime_t expires, cputime_t new_exp)
527{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100528 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * Insert the timer on the appropriate list before any timers that
533 * expire later. This must be called with the tasklist_lock held
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800534 * for reading, interrupts disabled and p->sighand->siglock taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800536static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct task_struct *p = timer->it.cpu.task;
539 struct list_head *head, *listpos;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800540 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct cpu_timer_list *const nt = &timer->it.cpu;
542 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800544 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
545 head = p->cpu_timers;
546 cputime_expires = &p->cputime_expires;
547 } else {
548 head = p->signal->cpu_timers;
549 cputime_expires = &p->signal->cputime_expires;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 head += CPUCLOCK_WHICH(timer->it_clock);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800554 list_for_each_entry(next, head, entry) {
555 if (cpu_time_before(timer->it_clock, nt->expires, next->expires))
556 break;
557 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 list_add(&nt->entry, listpos);
560
561 if (listpos == head) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800562 union cpu_time_count *exp = &nt->expires;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800565 * We are the new earliest-expiring POSIX 1.b timer, hence
566 * need to update expiration cache. Take into account that
567 * for process timers we share expiration cache with itimers
568 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
570
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800571 switch (CPUCLOCK_WHICH(timer->it_clock)) {
572 case CPUCLOCK_PROF:
573 if (expires_gt(cputime_expires->prof_exp, exp->cpu))
574 cputime_expires->prof_exp = exp->cpu;
575 break;
576 case CPUCLOCK_VIRT:
577 if (expires_gt(cputime_expires->virt_exp, exp->cpu))
578 cputime_expires->virt_exp = exp->cpu;
579 break;
580 case CPUCLOCK_SCHED:
581 if (cputime_expires->sched_exp == 0 ||
582 cputime_expires->sched_exp > exp->sched)
583 cputime_expires->sched_exp = exp->sched;
584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * The timer is locked, fire it and arrange for its reload.
591 */
592static void cpu_timer_fire(struct k_itimer *timer)
593{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800594 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
595 /*
596 * User don't want any signal.
597 */
598 timer->it.cpu.expires.sched = 0;
599 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * This a special case for clock_nanosleep,
602 * not a normal timer from sys_timer_create.
603 */
604 wake_up_process(timer->it_process);
605 timer->it.cpu.expires.sched = 0;
606 } else if (timer->it.cpu.incr.sched == 0) {
607 /*
608 * One-shot timer. Clear it as soon as it's fired.
609 */
610 posix_timer_event(timer, 0);
611 timer->it.cpu.expires.sched = 0;
612 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
613 /*
614 * The signal did not get queued because the signal
615 * was ignored, so we won't get any callback to
616 * reload the timer. But we need to keep it
617 * ticking in case the signal is deliverable next time.
618 */
619 posix_cpu_timer_schedule(timer);
620 }
621}
622
623/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100624 * Sample a process (thread group) timer for the given group_leader task.
625 * Must be called with tasklist_lock held for reading.
626 */
627static int cpu_timer_sample_group(const clockid_t which_clock,
628 struct task_struct *p,
629 union cpu_time_count *cpu)
630{
631 struct task_cputime cputime;
632
633 thread_group_cputimer(p, &cputime);
634 switch (CPUCLOCK_WHICH(which_clock)) {
635 default:
636 return -EINVAL;
637 case CPUCLOCK_PROF:
Martin Schwidefsky64861632011-12-15 14:56:09 +0100638 cpu->cpu = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100639 break;
640 case CPUCLOCK_VIRT:
641 cpu->cpu = cputime.utime;
642 break;
643 case CPUCLOCK_SCHED:
644 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
645 break;
646 }
647 return 0;
648}
649
650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * Guts of sys_timer_settime for CPU timers.
652 * This is called with the timer locked and interrupts disabled.
653 * If we return TIMER_RETRY, it's necessary to release the timer's lock
654 * and try again. (This happens when the timer is in the middle of firing.)
655 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000656static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
657 struct itimerspec *new, struct itimerspec *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct task_struct *p = timer->it.cpu.task;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800660 union cpu_time_count old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int ret;
662
663 if (unlikely(p == NULL)) {
664 /*
665 * Timer refers to a dead task's clock.
666 */
667 return -ESRCH;
668 }
669
670 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
671
672 read_lock(&tasklist_lock);
673 /*
674 * We need the tasklist_lock to protect against reaping that
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700675 * clears p->sighand. If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * longer get any information about it at all.
677 */
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700678 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 read_unlock(&tasklist_lock);
680 put_task_struct(p);
681 timer->it.cpu.task = NULL;
682 return -ESRCH;
683 }
684
685 /*
686 * Disarm any old timer after extracting its expiry time.
687 */
688 BUG_ON(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400689
690 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800691 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_lock(&p->sighand->siglock);
693 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400694 if (unlikely(timer->it.cpu.firing)) {
695 timer->it.cpu.firing = -1;
696 ret = TIMER_RETRY;
697 } else
698 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /*
701 * We need to sample the current value to convert the new
702 * value from to relative and absolute, and to convert the
703 * old value from absolute to relative. To set a process
704 * timer, we need a sample to balance the thread expiry
705 * times (in arm_timer). With an absolute time, we must
706 * check if it's already passed. In short, we need a sample.
707 */
708 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
709 cpu_clock_sample(timer->it_clock, p, &val);
710 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100711 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
714 if (old) {
715 if (old_expires.sched == 0) {
716 old->it_value.tv_sec = 0;
717 old->it_value.tv_nsec = 0;
718 } else {
719 /*
720 * Update the timer in case it has
721 * overrun already. If it has,
722 * we'll report it as having overrun
723 * and with the next reloaded timer
724 * already ticking, though we are
725 * swallowing that pending
726 * notification here to install the
727 * new setting.
728 */
729 bump_cpu_timer(timer, val);
730 if (cpu_time_before(timer->it_clock, val,
731 timer->it.cpu.expires)) {
732 old_expires = cpu_time_sub(
733 timer->it_clock,
734 timer->it.cpu.expires, val);
735 sample_to_timespec(timer->it_clock,
736 old_expires,
737 &old->it_value);
738 } else {
739 old->it_value.tv_nsec = 1;
740 old->it_value.tv_sec = 0;
741 }
742 }
743 }
744
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400745 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * We are colliding with the timer actually firing.
748 * Punt after filling in the timer's old value, and
749 * disable this firing since we are already reporting
750 * it as an overrun (thanks to bump_cpu_timer above).
751 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800752 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto out;
755 }
756
757 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
758 cpu_time_add(timer->it_clock, &new_expires, val);
759 }
760
761 /*
762 * Install the new expiry time (or zero).
763 * For a timer with no notification action, we don't actually
764 * arm the timer (we'll just fake it for timer_gettime).
765 */
766 timer->it.cpu.expires = new_expires;
767 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 cpu_time_before(timer->it_clock, val, new_expires)) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800769 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800772 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 read_unlock(&tasklist_lock);
774
775 /*
776 * Install the new reload setting, and
777 * set up the signal and overrun bookkeeping.
778 */
779 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
780 &new->it_interval);
781
782 /*
783 * This acts as a modification timestamp for the timer,
784 * so any automatic reload attempt will punt on seeing
785 * that we have reset the timer manually.
786 */
787 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
788 ~REQUEUE_PENDING;
789 timer->it_overrun_last = 0;
790 timer->it_overrun = -1;
791
792 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 !cpu_time_before(timer->it_clock, val, new_expires)) {
794 /*
795 * The designated time already passed, so we notify
796 * immediately, even if the thread never runs to
797 * accumulate more time on this clock.
798 */
799 cpu_timer_fire(timer);
800 }
801
802 ret = 0;
803 out:
804 if (old) {
805 sample_to_timespec(timer->it_clock,
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800806 old_incr, &old->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 return ret;
809}
810
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000811static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 union cpu_time_count now;
814 struct task_struct *p = timer->it.cpu.task;
815 int clear_dead;
816
817 /*
818 * Easy part: convert the reload time.
819 */
820 sample_to_timespec(timer->it_clock,
821 timer->it.cpu.incr, &itp->it_interval);
822
823 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
824 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
825 return;
826 }
827
828 if (unlikely(p == NULL)) {
829 /*
830 * This task already died and the timer will never fire.
831 * In this case, expires is actually the dead value.
832 */
833 dead:
834 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
835 &itp->it_value);
836 return;
837 }
838
839 /*
840 * Sample the clock to take the difference with the expiry time.
841 */
842 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
843 cpu_clock_sample(timer->it_clock, p, &now);
844 clear_dead = p->exit_state;
845 } else {
846 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700847 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * The process has been reaped.
850 * We can't even collect a sample any more.
851 * Call the timer disarmed, nothing else to do.
852 */
853 put_task_struct(p);
854 timer->it.cpu.task = NULL;
855 timer->it.cpu.expires.sched = 0;
856 read_unlock(&tasklist_lock);
857 goto dead;
858 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100859 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 clear_dead = (unlikely(p->exit_state) &&
861 thread_group_empty(p));
862 }
863 read_unlock(&tasklist_lock);
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (unlikely(clear_dead)) {
867 /*
868 * We've noticed that the thread is dead, but
869 * not yet reaped. Take this opportunity to
870 * drop our task ref.
871 */
872 clear_dead_task(timer, now);
873 goto dead;
874 }
875
876 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
877 sample_to_timespec(timer->it_clock,
878 cpu_time_sub(timer->it_clock,
879 timer->it.cpu.expires, now),
880 &itp->it_value);
881 } else {
882 /*
883 * The timer should have expired already, but the firing
884 * hasn't taken place yet. Say it's just about to expire.
885 */
886 itp->it_value.tv_nsec = 1;
887 itp->it_value.tv_sec = 0;
888 }
889}
890
891/*
892 * Check for any per-thread CPU timers that have fired and move them off
893 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
894 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
895 */
896static void check_thread_timers(struct task_struct *tsk,
897 struct list_head *firing)
898{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700899 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100901 struct signal_struct *const sig = tsk->signal;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800902 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvaldse80eda92005-10-23 10:02:50 -0700904 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100905 tsk->cputime_expires.prof_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700907 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct cpu_timer_list,
909 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100910 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700911 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
913 }
914 t->firing = 1;
915 list_move_tail(&t->entry, firing);
916 }
917
918 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700919 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100920 tsk->cputime_expires.virt_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700922 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct cpu_timer_list,
924 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100925 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700926 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 }
929 t->firing = 1;
930 list_move_tail(&t->entry, firing);
931 }
932
933 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700934 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700935 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700937 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct cpu_timer_list,
939 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200940 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700941 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
943 }
944 t->firing = 1;
945 list_move_tail(&t->entry, firing);
946 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100947
948 /*
949 * Check for the special case thread timers.
950 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800951 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800952 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800953 unsigned long hard =
954 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100955
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100956 if (hard != RLIM_INFINITY &&
957 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100958 /*
959 * At the hard limit, we just die.
960 * No need to calculate anything else now.
961 */
962 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
963 return;
964 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800965 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100966 /*
967 * At the soft limit, send a SIGXCPU every second.
968 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800969 if (soft < hard) {
970 soft += USEC_PER_SEC;
971 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100972 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -0700973 printk(KERN_INFO
974 "RT Watchdog Timeout: %s[%d]\n",
975 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100976 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
977 }
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800981static void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100982{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800983 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100984 unsigned long flags;
985
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200986 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100987 cputimer->running = 0;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200988 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100989}
990
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200991static u32 onecputick;
992
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200993static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
994 cputime_t *expires, cputime_t cur_time, int signo)
995{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100996 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200997 return;
998
Martin Schwidefsky64861632011-12-15 14:56:09 +0100999 if (cur_time >= it->expires) {
1000 if (it->incr) {
1001 it->expires += it->incr;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001002 it->error += it->incr_error;
1003 if (it->error >= onecputick) {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001004 it->expires -= cputime_one_jiffy;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001005 it->error -= onecputick;
1006 }
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001007 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001008 it->expires = 0;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001009 }
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001010
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001011 trace_itimer_expire(signo == SIGPROF ?
1012 ITIMER_PROF : ITIMER_VIRTUAL,
1013 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001014 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
1015 }
1016
Martin Schwidefsky64861632011-12-15 14:56:09 +01001017 if (it->expires && (!*expires || it->expires < *expires)) {
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001018 *expires = it->expires;
1019 }
1020}
1021
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001022/**
1023 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1024 *
1025 * @cputime: The struct to compare.
1026 *
1027 * Checks @cputime to see if all fields are zero. Returns true if all fields
1028 * are zero, false if any field is nonzero.
1029 */
1030static inline int task_cputime_zero(const struct task_cputime *cputime)
1031{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001032 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001033 return 1;
1034 return 0;
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
1038 * Check for any per-thread CPU timers that have fired and move them
1039 * off the tsk->*_timers list onto the firing list. Per-thread timers
1040 * have already been taken off.
1041 */
1042static void check_process_timers(struct task_struct *tsk,
1043 struct list_head *firing)
1044{
Linus Torvaldse80eda92005-10-23 10:02:50 -07001045 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -07001047 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +02001048 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -07001050 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001051 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * Collect the current process totals.
1055 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001056 thread_group_cputimer(tsk, &cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001057 utime = cputime.utime;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001058 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001059 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001060 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001061 prof_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001063 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct cpu_timer_list,
1065 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001066 if (!--maxfire || ptime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001067 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
1069 }
WANG Congee7dd202008-04-04 20:54:10 +02001070 tl->firing = 1;
1071 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001075 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001076 virt_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001078 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct cpu_timer_list,
1080 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001081 if (!--maxfire || utime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001082 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084 }
WANG Congee7dd202008-04-04 20:54:10 +02001085 tl->firing = 1;
1086 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001090 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 sched_expires = 0;
1092 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001093 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct cpu_timer_list,
1095 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001096 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1097 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 }
WANG Congee7dd202008-04-04 20:54:10 +02001100 tl->firing = 1;
1101 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
1104 /*
1105 * Check for the special case process timers.
1106 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001107 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
1108 SIGPROF);
1109 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
1110 SIGVTALRM);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001111 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001112 if (soft != RLIM_INFINITY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 unsigned long psecs = cputime_to_secs(ptime);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001114 unsigned long hard =
1115 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 cputime_t x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001117 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * At the hard limit, we just die.
1120 * No need to calculate anything else now.
1121 */
1122 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1123 return;
1124 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001125 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * At the soft limit, send a SIGXCPU every second.
1128 */
1129 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001130 if (soft < hard) {
1131 soft++;
1132 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001135 x = secs_to_cputime(soft);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001136 if (!prof_expires || x < prof_expires) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 prof_expires = x;
1138 }
1139 }
1140
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001141 sig->cputime_expires.prof_exp = prof_expires;
1142 sig->cputime_expires.virt_exp = virt_expires;
1143 sig->cputime_expires.sched_exp = sched_expires;
1144 if (task_cputime_zero(&sig->cputime_expires))
1145 stop_process_timers(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/*
1149 * This is called from the signal code (via do_schedule_next_timer)
1150 * when the last timer signal was delivered and we have to reload the timer.
1151 */
1152void posix_cpu_timer_schedule(struct k_itimer *timer)
1153{
1154 struct task_struct *p = timer->it.cpu.task;
1155 union cpu_time_count now;
1156
1157 if (unlikely(p == NULL))
1158 /*
1159 * The task was cleaned up already, no future firings.
1160 */
Roland McGrath708f430d2005-10-30 15:03:13 -08001161 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /*
1164 * Fetch the current sample and update the timer's expiry time.
1165 */
1166 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1167 cpu_clock_sample(timer->it_clock, p, &now);
1168 bump_cpu_timer(timer, now);
1169 if (unlikely(p->exit_state)) {
1170 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 read_lock(&tasklist_lock); /* arm_timer needs it. */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001174 spin_lock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } else {
1176 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -07001177 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * The process has been reaped.
1180 * We can't even collect a sample any more.
1181 */
1182 put_task_struct(p);
1183 timer->it.cpu.task = p = NULL;
1184 timer->it.cpu.expires.sched = 0;
Roland McGrath708f430d2005-10-30 15:03:13 -08001185 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1187 /*
1188 * We've noticed that the thread is dead, but
1189 * not yet reaped. Take this opportunity to
1190 * drop our task ref.
1191 */
1192 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001193 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001195 spin_lock(&p->sighand->siglock);
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001196 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 bump_cpu_timer(timer, now);
1198 /* Leave the tasklist_lock locked for the call below. */
1199 }
1200
1201 /*
1202 * Now re-arm for the new expiry time.
1203 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001204 BUG_ON(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001205 arm_timer(timer);
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001206 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Roland McGrath708f430d2005-10-30 15:03:13 -08001208out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 read_unlock(&tasklist_lock);
Roland McGrath708f430d2005-10-30 15:03:13 -08001210
1211out:
1212 timer->it_overrun_last = timer->it_overrun;
1213 timer->it_overrun = -1;
1214 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Frank Mayharf06febc2008-09-12 09:54:39 -07001217/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001218 * task_cputime_expired - Compare two task_cputime entities.
1219 *
1220 * @sample: The task_cputime structure to be checked for expiration.
1221 * @expires: Expiration times, against which @sample will be checked.
1222 *
1223 * Checks @sample against @expires to see if any field of @sample has expired.
1224 * Returns true if any field of the former is greater than the corresponding
1225 * field of the latter if the latter field is set. Otherwise returns false.
1226 */
1227static inline int task_cputime_expired(const struct task_cputime *sample,
1228 const struct task_cputime *expires)
1229{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001230 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001231 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001232 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001233 return 1;
1234 if (expires->sum_exec_runtime != 0 &&
1235 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1236 return 1;
1237 return 0;
1238}
1239
1240/**
1241 * fastpath_timer_check - POSIX CPU timers fast path.
1242 *
1243 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001244 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001245 * Check the task and thread group timers. If both are zero (there are no
1246 * timers set) return false. Otherwise snapshot the task and thread group
1247 * timers and compare them with the corresponding expiration times. Return
1248 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001249 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001250static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001251{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001252 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001253
Frank Mayharbb34d922008-09-12 09:54:39 -07001254 if (!task_cputime_zero(&tsk->cputime_expires)) {
1255 struct task_cputime task_sample = {
1256 .utime = tsk->utime,
1257 .stime = tsk->stime,
1258 .sum_exec_runtime = tsk->se.sum_exec_runtime
1259 };
1260
1261 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1262 return 1;
1263 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001264
1265 sig = tsk->signal;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001266 if (sig->cputimer.running) {
Frank Mayharbb34d922008-09-12 09:54:39 -07001267 struct task_cputime group_sample;
1268
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001269 raw_spin_lock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001270 group_sample = sig->cputimer.cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001271 raw_spin_unlock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001272
Frank Mayharbb34d922008-09-12 09:54:39 -07001273 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1274 return 1;
1275 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001276
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001277 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/*
1281 * This is called from the timer interrupt handler. The irq handler has
1282 * already updated our counts. We need to check if any timers fire now.
1283 * Interrupts are disabled.
1284 */
1285void run_posix_cpu_timers(struct task_struct *tsk)
1286{
1287 LIST_HEAD(firing);
1288 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001289 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 BUG_ON(!irqs_disabled());
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001294 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001295 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001297 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001298 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001299
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001300 if (!lock_task_sighand(tsk, &flags))
1301 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001302 /*
1303 * Here we take off tsk->signal->cpu_timers[N] and
1304 * tsk->cpu_timers[N] all the timers that are firing, and
1305 * put them on the firing list.
1306 */
1307 check_thread_timers(tsk, &firing);
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001308 /*
1309 * If there are any active process wide timers (POSIX 1.b, itimers,
1310 * RLIMIT_CPU) cputimer must be running.
1311 */
1312 if (tsk->signal->cputimer.running)
1313 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Frank Mayharbb34d922008-09-12 09:54:39 -07001315 /*
1316 * We must release these locks before taking any timer's lock.
1317 * There is a potential race with timer deletion here, as the
1318 * siglock now protects our private firing list. We have set
1319 * the firing flag in each timer, so that a deletion attempt
1320 * that gets the timer lock before we do will give it up and
1321 * spin until we've taken care of that timer below.
1322 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001323 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /*
1326 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001327 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * each timer's lock before clearing its firing flag, so no
1329 * timer call will interfere.
1330 */
1331 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001332 int cpu_firing;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_lock(&timer->it_lock);
1335 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001336 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 timer->it.cpu.firing = 0;
1338 /*
1339 * The firing flag is -1 if we collided with a reset
1340 * of the timer, which already reported this
1341 * almost-firing as an overrun. So don't generate an event.
1342 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001343 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_unlock(&timer->it_lock);
1346 }
1347}
1348
1349/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001350 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001351 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
1353void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1354 cputime_t *newval, cputime_t *oldval)
1355{
1356 union cpu_time_count now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001359 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001362 /*
1363 * We are setting itimer. The *oldval is absolute and we update
1364 * it to be relative, *newval argument is relative and we update
1365 * it to be absolute.
1366 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001367 if (*oldval) {
1368 if (*oldval <= now.cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* Just about to fire. */
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001370 *oldval = cputime_one_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001372 *oldval -= now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 }
1375
Martin Schwidefsky64861632011-12-15 14:56:09 +01001376 if (!*newval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001378 *newval += now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
1381 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001382 * Update expiration cache if we are the earliest timer, or eventually
1383 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001385 switch (clock_idx) {
1386 case CPUCLOCK_PROF:
1387 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001388 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001389 break;
1390 case CPUCLOCK_VIRT:
1391 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001392 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395}
1396
Toyo Abee4b76552006-09-29 02:00:29 -07001397static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1398 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct k_itimer timer;
1401 int error;
1402
1403 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * Set up a temporary timer and then wait for it to go off.
1405 */
1406 memset(&timer, 0, sizeof timer);
1407 spin_lock_init(&timer.it_lock);
1408 timer.it_clock = which_clock;
1409 timer.it_overrun = -1;
1410 error = posix_cpu_timer_create(&timer);
1411 timer.it_process = current;
1412 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001414
1415 memset(it, 0, sizeof *it);
1416 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001419 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (error) {
1421 spin_unlock_irq(&timer.it_lock);
1422 return error;
1423 }
1424
1425 while (!signal_pending(current)) {
1426 if (timer.it.cpu.expires.sched == 0) {
1427 /*
1428 * Our timer fired and was reset.
1429 */
1430 spin_unlock_irq(&timer.it_lock);
1431 return 0;
1432 }
1433
1434 /*
1435 * Block until cpu_timer_fire (or a signal) wakes us.
1436 */
1437 __set_current_state(TASK_INTERRUPTIBLE);
1438 spin_unlock_irq(&timer.it_lock);
1439 schedule();
1440 spin_lock_irq(&timer.it_lock);
1441 }
1442
1443 /*
1444 * We were interrupted by a signal.
1445 */
1446 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
Toyo Abee4b76552006-09-29 02:00:29 -07001447 posix_cpu_timer_set(&timer, 0, &zero_it, it);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 spin_unlock_irq(&timer.it_lock);
1449
Toyo Abee4b76552006-09-29 02:00:29 -07001450 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * It actually did fire already.
1453 */
1454 return 0;
1455 }
1456
Toyo Abee4b76552006-09-29 02:00:29 -07001457 error = -ERESTART_RESTARTBLOCK;
1458 }
1459
1460 return error;
1461}
1462
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001463static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1464
1465static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1466 struct timespec *rqtp, struct timespec __user *rmtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001467{
1468 struct restart_block *restart_block =
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001469 &current_thread_info()->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001470 struct itimerspec it;
1471 int error;
1472
1473 /*
1474 * Diagnose required errors first.
1475 */
1476 if (CPUCLOCK_PERTHREAD(which_clock) &&
1477 (CPUCLOCK_PID(which_clock) == 0 ||
1478 CPUCLOCK_PID(which_clock) == current->pid))
1479 return -EINVAL;
1480
1481 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1482
1483 if (error == -ERESTART_RESTARTBLOCK) {
1484
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001485 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001486 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -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))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EFAULT;
1492
Toyo Abe1711ef382006-09-29 02:00:28 -07001493 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001494 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001495 restart_block->nanosleep.rmtp = rmtp;
1496 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return error;
1499}
1500
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001501static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001503 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001504 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001505 struct itimerspec it;
1506 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001507
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001508 t = ns_to_timespec(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001509
Toyo Abee4b76552006-09-29 02:00:29 -07001510 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1511
1512 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001513 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001514 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001515 * Report back to the user the time still remaining.
1516 */
1517 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Toyo Abee4b76552006-09-29 02:00:29 -07001518 return -EFAULT;
1519
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001520 restart_block->nanosleep.expires = timespec_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001521 }
1522 return error;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526#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 Abe1711ef382006-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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Gleixner19769452011-02-01 13:51:06 +00001570struct k_clock clock_posix_cpu = {
1571 .clock_getres = posix_cpu_clock_getres,
1572 .clock_set = posix_cpu_clock_set,
1573 .clock_get = posix_cpu_clock_get,
1574 .timer_create = posix_cpu_timer_create,
1575 .nsleep = posix_cpu_nsleep,
1576 .nsleep_restart = posix_cpu_nsleep_restart,
1577 .timer_set = posix_cpu_timer_set,
1578 .timer_del = posix_cpu_timer_del,
1579 .timer_get = posix_cpu_timer_get,
1580};
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582static __init int init_posix_cpu_timers(void)
1583{
1584 struct k_clock process = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001585 .clock_getres = process_cpu_clock_getres,
1586 .clock_get = process_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001587 .timer_create = process_cpu_timer_create,
1588 .nsleep = process_cpu_nsleep,
1589 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 };
1591 struct k_clock thread = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001592 .clock_getres = thread_cpu_clock_getres,
1593 .clock_get = thread_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001594 .timer_create = thread_cpu_timer_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 };
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001596 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Thomas Gleixner52708732011-02-02 12:10:09 +01001598 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1599 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001601 cputime_to_timespec(cputime_one_jiffy, &ts);
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001602 onecputick = ts.tv_nsec;
1603 WARN_ON(ts.tv_sec != 0);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
1606}
1607__initcall(init_posix_cpu_timers);