blob: 8fd709c9bb5843bbad0e7e3fd1e1b00075413b29 [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{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100158 cputime_t utime, stime;
159
160 task_cputime(p, &utime, &stime);
161
162 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164static inline cputime_t virt_ticks(struct task_struct *p)
165{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100166 cputime_t utime;
167
168 task_cputime(p, &utime, NULL);
169
170 return utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000173static int
174posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int error = check_clock(which_clock);
177 if (!error) {
178 tp->tv_sec = 0;
179 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
180 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
181 /*
182 * If sched_clock is using a cycle counter, we
183 * don't have any idea of its true resolution
184 * exported, but it is much more than 1s/HZ.
185 */
186 tp->tv_nsec = 1;
187 }
188 }
189 return error;
190}
191
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000192static int
193posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 /*
196 * You can never reset a CPU clock, but we check for other errors
197 * in the call before failing with EPERM.
198 */
199 int error = check_clock(which_clock);
200 if (error == 0) {
201 error = -EPERM;
202 }
203 return error;
204}
205
206
207/*
208 * Sample a per-thread clock for the given task.
209 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800210static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 union cpu_time_count *cpu)
212{
213 switch (CPUCLOCK_WHICH(which_clock)) {
214 default:
215 return -EINVAL;
216 case CPUCLOCK_PROF:
217 cpu->cpu = prof_ticks(p);
218 break;
219 case CPUCLOCK_VIRT:
220 cpu->cpu = virt_ticks(p);
221 break;
222 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900223 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 }
226 return 0;
227}
228
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100229static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
230{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100231 if (b->utime > a->utime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100232 a->utime = b->utime;
233
Martin Schwidefsky64861632011-12-15 14:56:09 +0100234 if (b->stime > a->stime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100235 a->stime = b->stime;
236
237 if (b->sum_exec_runtime > a->sum_exec_runtime)
238 a->sum_exec_runtime = b->sum_exec_runtime;
239}
240
241void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
242{
243 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
244 struct task_cputime sum;
245 unsigned long flags;
246
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100247 if (!cputimer->running) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100248 /*
249 * The POSIX timer interface allows for absolute time expiry
250 * values through the TIMER_ABSTIME flag, therefore we have
251 * to synchronize the timer to the clock every time we start
252 * it.
253 */
254 thread_group_cputime(tsk, &sum);
Linus Torvalds3cfef952011-10-26 16:17:32 +0200255 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200256 cputimer->running = 1;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100257 update_gt_cputime(&cputimer->cputime, &sum);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200258 } else
Linus Torvalds3cfef952011-10-26 16:17:32 +0200259 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100260 *times = cputimer->cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200261 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
265 * Sample a process (thread group) clock for the given group_leader task.
266 * Must be called with tasklist_lock held for reading.
267 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800268static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct task_struct *p,
270 union cpu_time_count *cpu)
271{
Frank Mayharbb34d922008-09-12 09:54:39 -0700272 struct task_cputime cputime;
273
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100274 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700275 default:
276 return -EINVAL;
277 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900278 thread_group_cputime(p, &cputime);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100279 cpu->cpu = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700280 break;
281 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900282 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700283 cpu->cpu = cputime.utime;
284 break;
285 case CPUCLOCK_SCHED:
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200286 thread_group_cputime(p, &cputime);
287 cpu->sched = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700288 break;
289 }
290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000294static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 const pid_t pid = CPUCLOCK_PID(which_clock);
297 int error = -EINVAL;
298 union cpu_time_count rtn;
299
300 if (pid == 0) {
301 /*
302 * Special case constant value for our own clocks.
303 * We don't have to do any lookup to find ourselves.
304 */
305 if (CPUCLOCK_PERTHREAD(which_clock)) {
306 /*
307 * Sampling just ourselves we can do with no locking.
308 */
309 error = cpu_clock_sample(which_clock,
310 current, &rtn);
311 } else {
312 read_lock(&tasklist_lock);
313 error = cpu_clock_sample_group(which_clock,
314 current, &rtn);
315 read_unlock(&tasklist_lock);
316 }
317 } else {
318 /*
319 * Find the given PID, and validate that the caller
320 * should be able to see it.
321 */
322 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800323 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800324 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (p) {
326 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700327 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 error = cpu_clock_sample(which_clock,
329 p, &rtn);
330 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800331 } else {
332 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700333 if (thread_group_leader(p) && p->sighand) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800334 error =
335 cpu_clock_sample_group(which_clock,
336 p, &rtn);
337 }
338 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800341 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 if (error)
345 return error;
346 sample_to_timespec(which_clock, rtn, tp);
347 return 0;
348}
349
350
351/*
352 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800353 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
354 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000356static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int ret = 0;
359 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
360 struct task_struct *p;
361
362 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
363 return -EINVAL;
364
365 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200367 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
369 if (pid == 0) {
370 p = current;
371 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800372 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700373 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 p = NULL;
375 }
376 } else {
377 if (pid == 0) {
378 p = current->group_leader;
379 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800380 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200381 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 p = NULL;
383 }
384 }
385 new_timer->it.cpu.task = p;
386 if (p) {
387 get_task_struct(p);
388 } else {
389 ret = -EINVAL;
390 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return ret;
394}
395
396/*
397 * Clean up a CPU-clock timer that is about to be destroyed.
398 * This is called from timer deletion with the timer already locked.
399 * If we return TIMER_RETRY, it's necessary to release the timer's lock
400 * and try again. (This happens when the timer is in the middle of firing.)
401 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000402static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400405 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Oleg Nesterov108150e2005-10-23 20:25:39 +0400407 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700408 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700409 if (unlikely(p->sighand == NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700410 /*
411 * We raced with the reaping of the task.
412 * The deletion should have cleared us off the list.
413 */
414 BUG_ON(!list_empty(&timer->it.cpu.entry));
415 } else {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700416 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400417 if (timer->it.cpu.firing)
418 ret = TIMER_RETRY;
419 else
420 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700421 spin_unlock(&p->sighand->siglock);
422 }
423 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Oleg Nesterov108150e2005-10-23 20:25:39 +0400425 if (!ret)
426 put_task_struct(p);
427 }
428
429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/*
433 * Clean out CPU timers still ticking when a thread exited. The task
434 * pointer is cleared, and the expiry time is replaced with the residual
435 * time for later timer_gettime calls to return.
436 * This must be called with the siglock held.
437 */
438static void cleanup_timers(struct list_head *head,
439 cputime_t utime, cputime_t stime,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200440 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct cpu_timer_list *timer, *next;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100443 cputime_t ptime = utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100447 if (timer->expires.cpu < ptime) {
448 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100450 timer->expires.cpu -= ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453
454 ++head;
455 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100457 if (timer->expires.cpu < utime) {
458 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100460 timer->expires.cpu -= utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 }
463
464 ++head;
465 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200467 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 timer->expires.sched = 0;
469 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200470 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 }
473}
474
475/*
476 * These are both called with the siglock held, when the current thread
477 * is being reaped. When the final (leader) thread in the group is reaped,
478 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
479 */
480void posix_cpu_timers_exit(struct task_struct *tsk)
481{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100482 cputime_t utime, stime;
483
Nick Kossifidis61337052012-12-16 22:18:11 -0500484 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
485 sizeof(unsigned long long));
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100486 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 cleanup_timers(tsk->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100488 utime, stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490}
491void posix_cpu_timers_exit_group(struct task_struct *tsk)
492{
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700493 struct signal_struct *const sig = tsk->signal;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100494 cputime_t utime, stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700495
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100496 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 cleanup_timers(tsk->signal->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100498 utime + sig->utime, stime + sig->stime,
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700499 tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
503{
504 /*
505 * That's all for this thread or process.
506 * We leave our residual in expires to be reported.
507 */
508 put_task_struct(timer->it.cpu.task);
509 timer->it.cpu.task = NULL;
510 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
511 timer->it.cpu.expires,
512 now);
513}
514
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200515static inline int expires_gt(cputime_t expires, cputime_t new_exp)
516{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100517 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
521 * Insert the timer on the appropriate list before any timers that
522 * expire later. This must be called with the tasklist_lock held
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800523 * for reading, interrupts disabled and p->sighand->siglock taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800525static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct task_struct *p = timer->it.cpu.task;
528 struct list_head *head, *listpos;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800529 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct cpu_timer_list *const nt = &timer->it.cpu;
531 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800533 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
534 head = p->cpu_timers;
535 cputime_expires = &p->cputime_expires;
536 } else {
537 head = p->signal->cpu_timers;
538 cputime_expires = &p->signal->cputime_expires;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 head += CPUCLOCK_WHICH(timer->it_clock);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800543 list_for_each_entry(next, head, entry) {
544 if (cpu_time_before(timer->it_clock, nt->expires, next->expires))
545 break;
546 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 list_add(&nt->entry, listpos);
549
550 if (listpos == head) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800551 union cpu_time_count *exp = &nt->expires;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800554 * We are the new earliest-expiring POSIX 1.b timer, hence
555 * need to update expiration cache. Take into account that
556 * for process timers we share expiration cache with itimers
557 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800560 switch (CPUCLOCK_WHICH(timer->it_clock)) {
561 case CPUCLOCK_PROF:
562 if (expires_gt(cputime_expires->prof_exp, exp->cpu))
563 cputime_expires->prof_exp = exp->cpu;
564 break;
565 case CPUCLOCK_VIRT:
566 if (expires_gt(cputime_expires->virt_exp, exp->cpu))
567 cputime_expires->virt_exp = exp->cpu;
568 break;
569 case CPUCLOCK_SCHED:
570 if (cputime_expires->sched_exp == 0 ||
571 cputime_expires->sched_exp > exp->sched)
572 cputime_expires->sched_exp = exp->sched;
573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/*
579 * The timer is locked, fire it and arrange for its reload.
580 */
581static void cpu_timer_fire(struct k_itimer *timer)
582{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800583 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
584 /*
585 * User don't want any signal.
586 */
587 timer->it.cpu.expires.sched = 0;
588 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
590 * This a special case for clock_nanosleep,
591 * not a normal timer from sys_timer_create.
592 */
593 wake_up_process(timer->it_process);
594 timer->it.cpu.expires.sched = 0;
595 } else if (timer->it.cpu.incr.sched == 0) {
596 /*
597 * One-shot timer. Clear it as soon as it's fired.
598 */
599 posix_timer_event(timer, 0);
600 timer->it.cpu.expires.sched = 0;
601 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
602 /*
603 * The signal did not get queued because the signal
604 * was ignored, so we won't get any callback to
605 * reload the timer. But we need to keep it
606 * ticking in case the signal is deliverable next time.
607 */
608 posix_cpu_timer_schedule(timer);
609 }
610}
611
612/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100613 * Sample a process (thread group) timer for the given group_leader task.
614 * Must be called with tasklist_lock held for reading.
615 */
616static int cpu_timer_sample_group(const clockid_t which_clock,
617 struct task_struct *p,
618 union cpu_time_count *cpu)
619{
620 struct task_cputime cputime;
621
622 thread_group_cputimer(p, &cputime);
623 switch (CPUCLOCK_WHICH(which_clock)) {
624 default:
625 return -EINVAL;
626 case CPUCLOCK_PROF:
Martin Schwidefsky64861632011-12-15 14:56:09 +0100627 cpu->cpu = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100628 break;
629 case CPUCLOCK_VIRT:
630 cpu->cpu = cputime.utime;
631 break;
632 case CPUCLOCK_SCHED:
633 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
634 break;
635 }
636 return 0;
637}
638
639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Guts of sys_timer_settime for CPU timers.
641 * This is called with the timer locked and interrupts disabled.
642 * If we return TIMER_RETRY, it's necessary to release the timer's lock
643 * and try again. (This happens when the timer is in the middle of firing.)
644 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000645static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
646 struct itimerspec *new, struct itimerspec *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct task_struct *p = timer->it.cpu.task;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800649 union cpu_time_count old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int ret;
651
652 if (unlikely(p == NULL)) {
653 /*
654 * Timer refers to a dead task's clock.
655 */
656 return -ESRCH;
657 }
658
659 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
660
661 read_lock(&tasklist_lock);
662 /*
663 * We need the tasklist_lock to protect against reaping that
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700664 * clears p->sighand. If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * longer get any information about it at all.
666 */
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700667 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 read_unlock(&tasklist_lock);
669 put_task_struct(p);
670 timer->it.cpu.task = NULL;
671 return -ESRCH;
672 }
673
674 /*
675 * Disarm any old timer after extracting its expiry time.
676 */
677 BUG_ON(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400678
679 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800680 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_lock(&p->sighand->siglock);
682 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400683 if (unlikely(timer->it.cpu.firing)) {
684 timer->it.cpu.firing = -1;
685 ret = TIMER_RETRY;
686 } else
687 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /*
690 * We need to sample the current value to convert the new
691 * value from to relative and absolute, and to convert the
692 * old value from absolute to relative. To set a process
693 * timer, we need a sample to balance the thread expiry
694 * times (in arm_timer). With an absolute time, we must
695 * check if it's already passed. In short, we need a sample.
696 */
697 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
698 cpu_clock_sample(timer->it_clock, p, &val);
699 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100700 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 if (old) {
704 if (old_expires.sched == 0) {
705 old->it_value.tv_sec = 0;
706 old->it_value.tv_nsec = 0;
707 } else {
708 /*
709 * Update the timer in case it has
710 * overrun already. If it has,
711 * we'll report it as having overrun
712 * and with the next reloaded timer
713 * already ticking, though we are
714 * swallowing that pending
715 * notification here to install the
716 * new setting.
717 */
718 bump_cpu_timer(timer, val);
719 if (cpu_time_before(timer->it_clock, val,
720 timer->it.cpu.expires)) {
721 old_expires = cpu_time_sub(
722 timer->it_clock,
723 timer->it.cpu.expires, val);
724 sample_to_timespec(timer->it_clock,
725 old_expires,
726 &old->it_value);
727 } else {
728 old->it_value.tv_nsec = 1;
729 old->it_value.tv_sec = 0;
730 }
731 }
732 }
733
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400734 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
736 * We are colliding with the timer actually firing.
737 * Punt after filling in the timer's old value, and
738 * disable this firing since we are already reporting
739 * it as an overrun (thanks to bump_cpu_timer above).
740 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800741 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto out;
744 }
745
746 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
747 cpu_time_add(timer->it_clock, &new_expires, val);
748 }
749
750 /*
751 * Install the new expiry time (or zero).
752 * For a timer with no notification action, we don't actually
753 * arm the timer (we'll just fake it for timer_gettime).
754 */
755 timer->it.cpu.expires = new_expires;
756 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cpu_time_before(timer->it_clock, val, new_expires)) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800758 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800761 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 read_unlock(&tasklist_lock);
763
764 /*
765 * Install the new reload setting, and
766 * set up the signal and overrun bookkeeping.
767 */
768 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
769 &new->it_interval);
770
771 /*
772 * This acts as a modification timestamp for the timer,
773 * so any automatic reload attempt will punt on seeing
774 * that we have reset the timer manually.
775 */
776 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
777 ~REQUEUE_PENDING;
778 timer->it_overrun_last = 0;
779 timer->it_overrun = -1;
780
781 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 !cpu_time_before(timer->it_clock, val, new_expires)) {
783 /*
784 * The designated time already passed, so we notify
785 * immediately, even if the thread never runs to
786 * accumulate more time on this clock.
787 */
788 cpu_timer_fire(timer);
789 }
790
791 ret = 0;
792 out:
793 if (old) {
794 sample_to_timespec(timer->it_clock,
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800795 old_incr, &old->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 return ret;
798}
799
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000800static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 union cpu_time_count now;
803 struct task_struct *p = timer->it.cpu.task;
804 int clear_dead;
805
806 /*
807 * Easy part: convert the reload time.
808 */
809 sample_to_timespec(timer->it_clock,
810 timer->it.cpu.incr, &itp->it_interval);
811
812 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
813 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
814 return;
815 }
816
817 if (unlikely(p == NULL)) {
818 /*
819 * This task already died and the timer will never fire.
820 * In this case, expires is actually the dead value.
821 */
822 dead:
823 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
824 &itp->it_value);
825 return;
826 }
827
828 /*
829 * Sample the clock to take the difference with the expiry time.
830 */
831 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
832 cpu_clock_sample(timer->it_clock, p, &now);
833 clear_dead = p->exit_state;
834 } else {
835 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700836 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /*
838 * The process has been reaped.
839 * We can't even collect a sample any more.
840 * Call the timer disarmed, nothing else to do.
841 */
842 put_task_struct(p);
843 timer->it.cpu.task = NULL;
844 timer->it.cpu.expires.sched = 0;
845 read_unlock(&tasklist_lock);
846 goto dead;
847 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100848 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 clear_dead = (unlikely(p->exit_state) &&
850 thread_group_empty(p));
851 }
852 read_unlock(&tasklist_lock);
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (unlikely(clear_dead)) {
856 /*
857 * We've noticed that the thread is dead, but
858 * not yet reaped. Take this opportunity to
859 * drop our task ref.
860 */
861 clear_dead_task(timer, now);
862 goto dead;
863 }
864
865 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
866 sample_to_timespec(timer->it_clock,
867 cpu_time_sub(timer->it_clock,
868 timer->it.cpu.expires, now),
869 &itp->it_value);
870 } else {
871 /*
872 * The timer should have expired already, but the firing
873 * hasn't taken place yet. Say it's just about to expire.
874 */
875 itp->it_value.tv_nsec = 1;
876 itp->it_value.tv_sec = 0;
877 }
878}
879
880/*
881 * Check for any per-thread CPU timers that have fired and move them off
882 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
883 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
884 */
885static void check_thread_timers(struct task_struct *tsk,
886 struct list_head *firing)
887{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700888 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100890 struct signal_struct *const sig = tsk->signal;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800891 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvaldse80eda92005-10-23 10:02:50 -0700893 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100894 tsk->cputime_expires.prof_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700896 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct cpu_timer_list,
898 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100899 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700900 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902 }
903 t->firing = 1;
904 list_move_tail(&t->entry, firing);
905 }
906
907 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700908 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100909 tsk->cputime_expires.virt_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700911 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct cpu_timer_list,
913 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100914 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700915 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 }
918 t->firing = 1;
919 list_move_tail(&t->entry, firing);
920 }
921
922 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700923 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700924 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700926 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct cpu_timer_list,
928 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200929 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700930 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
932 }
933 t->firing = 1;
934 list_move_tail(&t->entry, firing);
935 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100936
937 /*
938 * Check for the special case thread timers.
939 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800940 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800941 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800942 unsigned long hard =
943 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100944
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100945 if (hard != RLIM_INFINITY &&
946 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100947 /*
948 * At the hard limit, we just die.
949 * No need to calculate anything else now.
950 */
951 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
952 return;
953 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800954 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100955 /*
956 * At the soft limit, send a SIGXCPU every second.
957 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800958 if (soft < hard) {
959 soft += USEC_PER_SEC;
960 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100961 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -0700962 printk(KERN_INFO
963 "RT Watchdog Timeout: %s[%d]\n",
964 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100965 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
966 }
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800970static void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100971{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800972 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100973 unsigned long flags;
974
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200975 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100976 cputimer->running = 0;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200977 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100978}
979
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200980static u32 onecputick;
981
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200982static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
983 cputime_t *expires, cputime_t cur_time, int signo)
984{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100985 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200986 return;
987
Martin Schwidefsky64861632011-12-15 14:56:09 +0100988 if (cur_time >= it->expires) {
989 if (it->incr) {
990 it->expires += it->incr;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200991 it->error += it->incr_error;
992 if (it->error >= onecputick) {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100993 it->expires -= cputime_one_jiffy;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +0200994 it->error -= onecputick;
995 }
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800996 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100997 it->expires = 0;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800998 }
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200999
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001000 trace_itimer_expire(signo == SIGPROF ?
1001 ITIMER_PROF : ITIMER_VIRTUAL,
1002 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001003 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
1004 }
1005
Martin Schwidefsky64861632011-12-15 14:56:09 +01001006 if (it->expires && (!*expires || it->expires < *expires)) {
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001007 *expires = it->expires;
1008 }
1009}
1010
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001011/**
1012 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1013 *
1014 * @cputime: The struct to compare.
1015 *
1016 * Checks @cputime to see if all fields are zero. Returns true if all fields
1017 * are zero, false if any field is nonzero.
1018 */
1019static inline int task_cputime_zero(const struct task_cputime *cputime)
1020{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001021 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001022 return 1;
1023 return 0;
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * Check for any per-thread CPU timers that have fired and move them
1028 * off the tsk->*_timers list onto the firing list. Per-thread timers
1029 * have already been taken off.
1030 */
1031static void check_process_timers(struct task_struct *tsk,
1032 struct list_head *firing)
1033{
Linus Torvaldse80eda92005-10-23 10:02:50 -07001034 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -07001036 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +02001037 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -07001039 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001040 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * Collect the current process totals.
1044 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001045 thread_group_cputimer(tsk, &cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001046 utime = cputime.utime;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001047 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001048 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001049 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001050 prof_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001052 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct cpu_timer_list,
1054 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001055 if (!--maxfire || ptime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001056 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058 }
WANG Congee7dd202008-04-04 20:54:10 +02001059 tl->firing = 1;
1060 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001064 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001065 virt_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001067 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct cpu_timer_list,
1069 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001070 if (!--maxfire || utime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001071 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 }
WANG Congee7dd202008-04-04 20:54:10 +02001074 tl->firing = 1;
1075 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001079 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 sched_expires = 0;
1081 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001082 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct cpu_timer_list,
1084 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001085 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1086 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
1088 }
WANG Congee7dd202008-04-04 20:54:10 +02001089 tl->firing = 1;
1090 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 /*
1094 * Check for the special case process timers.
1095 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001096 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
1097 SIGPROF);
1098 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
1099 SIGVTALRM);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001100 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001101 if (soft != RLIM_INFINITY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unsigned long psecs = cputime_to_secs(ptime);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001103 unsigned long hard =
1104 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 cputime_t x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001106 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /*
1108 * At the hard limit, we just die.
1109 * No need to calculate anything else now.
1110 */
1111 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1112 return;
1113 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001114 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * At the soft limit, send a SIGXCPU every second.
1117 */
1118 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001119 if (soft < hard) {
1120 soft++;
1121 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001124 x = secs_to_cputime(soft);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001125 if (!prof_expires || x < prof_expires) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 prof_expires = x;
1127 }
1128 }
1129
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001130 sig->cputime_expires.prof_exp = prof_expires;
1131 sig->cputime_expires.virt_exp = virt_expires;
1132 sig->cputime_expires.sched_exp = sched_expires;
1133 if (task_cputime_zero(&sig->cputime_expires))
1134 stop_process_timers(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * This is called from the signal code (via do_schedule_next_timer)
1139 * when the last timer signal was delivered and we have to reload the timer.
1140 */
1141void posix_cpu_timer_schedule(struct k_itimer *timer)
1142{
1143 struct task_struct *p = timer->it.cpu.task;
1144 union cpu_time_count now;
1145
1146 if (unlikely(p == NULL))
1147 /*
1148 * The task was cleaned up already, no future firings.
1149 */
Roland McGrath708f430d2005-10-30 15:03:13 -08001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /*
1153 * Fetch the current sample and update the timer's expiry time.
1154 */
1155 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1156 cpu_clock_sample(timer->it_clock, p, &now);
1157 bump_cpu_timer(timer, now);
1158 if (unlikely(p->exit_state)) {
1159 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 read_lock(&tasklist_lock); /* arm_timer needs it. */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001163 spin_lock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 } else {
1165 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -07001166 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
1168 * The process has been reaped.
1169 * We can't even collect a sample any more.
1170 */
1171 put_task_struct(p);
1172 timer->it.cpu.task = p = NULL;
1173 timer->it.cpu.expires.sched = 0;
Roland McGrath708f430d2005-10-30 15:03:13 -08001174 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1176 /*
1177 * We've noticed that the thread is dead, but
1178 * not yet reaped. Take this opportunity to
1179 * drop our task ref.
1180 */
1181 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001182 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001184 spin_lock(&p->sighand->siglock);
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001185 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 bump_cpu_timer(timer, now);
1187 /* Leave the tasklist_lock locked for the call below. */
1188 }
1189
1190 /*
1191 * Now re-arm for the new expiry time.
1192 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001193 BUG_ON(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001194 arm_timer(timer);
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001195 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Roland McGrath708f430d2005-10-30 15:03:13 -08001197out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 read_unlock(&tasklist_lock);
Roland McGrath708f430d2005-10-30 15:03:13 -08001199
1200out:
1201 timer->it_overrun_last = timer->it_overrun;
1202 timer->it_overrun = -1;
1203 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Frank Mayharf06febc2008-09-12 09:54:39 -07001206/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001207 * task_cputime_expired - Compare two task_cputime entities.
1208 *
1209 * @sample: The task_cputime structure to be checked for expiration.
1210 * @expires: Expiration times, against which @sample will be checked.
1211 *
1212 * Checks @sample against @expires to see if any field of @sample has expired.
1213 * Returns true if any field of the former is greater than the corresponding
1214 * field of the latter if the latter field is set. Otherwise returns false.
1215 */
1216static inline int task_cputime_expired(const struct task_cputime *sample,
1217 const struct task_cputime *expires)
1218{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001219 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001220 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001221 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001222 return 1;
1223 if (expires->sum_exec_runtime != 0 &&
1224 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1225 return 1;
1226 return 0;
1227}
1228
1229/**
1230 * fastpath_timer_check - POSIX CPU timers fast path.
1231 *
1232 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001233 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001234 * Check the task and thread group timers. If both are zero (there are no
1235 * timers set) return false. Otherwise snapshot the task and thread group
1236 * timers and compare them with the corresponding expiration times. Return
1237 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001238 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001239static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001240{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001241 struct signal_struct *sig;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001242 cputime_t utime, stime;
1243
1244 task_cputime(tsk, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001245
Frank Mayharbb34d922008-09-12 09:54:39 -07001246 if (!task_cputime_zero(&tsk->cputime_expires)) {
1247 struct task_cputime task_sample = {
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001248 .utime = utime,
1249 .stime = stime,
Frank Mayharbb34d922008-09-12 09:54:39 -07001250 .sum_exec_runtime = tsk->se.sum_exec_runtime
1251 };
1252
1253 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1254 return 1;
1255 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001256
1257 sig = tsk->signal;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001258 if (sig->cputimer.running) {
Frank Mayharbb34d922008-09-12 09:54:39 -07001259 struct task_cputime group_sample;
1260
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001261 raw_spin_lock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001262 group_sample = sig->cputimer.cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001263 raw_spin_unlock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001264
Frank Mayharbb34d922008-09-12 09:54:39 -07001265 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1266 return 1;
1267 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001268
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001269 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * This is called from the timer interrupt handler. The irq handler has
1274 * already updated our counts. We need to check if any timers fire now.
1275 * Interrupts are disabled.
1276 */
1277void run_posix_cpu_timers(struct task_struct *tsk)
1278{
1279 LIST_HEAD(firing);
1280 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001281 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 BUG_ON(!irqs_disabled());
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001286 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001287 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001289 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001290 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001291
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001292 if (!lock_task_sighand(tsk, &flags))
1293 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001294 /*
1295 * Here we take off tsk->signal->cpu_timers[N] and
1296 * tsk->cpu_timers[N] all the timers that are firing, and
1297 * put them on the firing list.
1298 */
1299 check_thread_timers(tsk, &firing);
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001300 /*
1301 * If there are any active process wide timers (POSIX 1.b, itimers,
1302 * RLIMIT_CPU) cputimer must be running.
1303 */
1304 if (tsk->signal->cputimer.running)
1305 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Frank Mayharbb34d922008-09-12 09:54:39 -07001307 /*
1308 * We must release these locks before taking any timer's lock.
1309 * There is a potential race with timer deletion here, as the
1310 * siglock now protects our private firing list. We have set
1311 * the firing flag in each timer, so that a deletion attempt
1312 * that gets the timer lock before we do will give it up and
1313 * spin until we've taken care of that timer below.
1314 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001315 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /*
1318 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001319 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * each timer's lock before clearing its firing flag, so no
1321 * timer call will interfere.
1322 */
1323 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001324 int cpu_firing;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 spin_lock(&timer->it_lock);
1327 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001328 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 timer->it.cpu.firing = 0;
1330 /*
1331 * The firing flag is -1 if we collided with a reset
1332 * of the timer, which already reported this
1333 * almost-firing as an overrun. So don't generate an event.
1334 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001335 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 spin_unlock(&timer->it_lock);
1338 }
1339}
1340
1341/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001342 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001343 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
1345void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1346 cputime_t *newval, cputime_t *oldval)
1347{
1348 union cpu_time_count now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001351 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001354 /*
1355 * We are setting itimer. The *oldval is absolute and we update
1356 * it to be relative, *newval argument is relative and we update
1357 * it to be absolute.
1358 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001359 if (*oldval) {
1360 if (*oldval <= now.cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* Just about to fire. */
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001362 *oldval = cputime_one_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001364 *oldval -= now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 }
1367
Martin Schwidefsky64861632011-12-15 14:56:09 +01001368 if (!*newval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001370 *newval += now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
1373 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001374 * Update expiration cache if we are the earliest timer, or eventually
1375 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001377 switch (clock_idx) {
1378 case CPUCLOCK_PROF:
1379 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001380 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001381 break;
1382 case CPUCLOCK_VIRT:
1383 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001384 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387}
1388
Toyo Abee4b76552006-09-29 02:00:29 -07001389static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1390 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct k_itimer timer;
1393 int error;
1394
1395 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * Set up a temporary timer and then wait for it to go off.
1397 */
1398 memset(&timer, 0, sizeof timer);
1399 spin_lock_init(&timer.it_lock);
1400 timer.it_clock = which_clock;
1401 timer.it_overrun = -1;
1402 error = posix_cpu_timer_create(&timer);
1403 timer.it_process = current;
1404 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001406
1407 memset(it, 0, sizeof *it);
1408 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001411 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (error) {
1413 spin_unlock_irq(&timer.it_lock);
1414 return error;
1415 }
1416
1417 while (!signal_pending(current)) {
1418 if (timer.it.cpu.expires.sched == 0) {
1419 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001420 * Our timer fired and was reset, below
1421 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001423 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 spin_unlock_irq(&timer.it_lock);
1425 return 0;
1426 }
1427
1428 /*
1429 * Block until cpu_timer_fire (or a signal) wakes us.
1430 */
1431 __set_current_state(TASK_INTERRUPTIBLE);
1432 spin_unlock_irq(&timer.it_lock);
1433 schedule();
1434 spin_lock_irq(&timer.it_lock);
1435 }
1436
1437 /*
1438 * We were interrupted by a signal.
1439 */
1440 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001441 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
1442 if (!error) {
1443 /*
1444 * Timer is now unarmed, deletion can not fail.
1445 */
1446 posix_cpu_timer_del(&timer);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 spin_unlock_irq(&timer.it_lock);
1449
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001450 while (error == TIMER_RETRY) {
1451 /*
1452 * We need to handle case when timer was or is in the
1453 * middle of firing. In other cases we already freed
1454 * resources.
1455 */
1456 spin_lock_irq(&timer.it_lock);
1457 error = posix_cpu_timer_del(&timer);
1458 spin_unlock_irq(&timer.it_lock);
1459 }
1460
Toyo Abee4b76552006-09-29 02:00:29 -07001461 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * It actually did fire already.
1464 */
1465 return 0;
1466 }
1467
Toyo Abee4b76552006-09-29 02:00:29 -07001468 error = -ERESTART_RESTARTBLOCK;
1469 }
1470
1471 return error;
1472}
1473
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001474static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1475
1476static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1477 struct timespec *rqtp, struct timespec __user *rmtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001478{
1479 struct restart_block *restart_block =
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001480 &current_thread_info()->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001481 struct itimerspec it;
1482 int error;
1483
1484 /*
1485 * Diagnose required errors first.
1486 */
1487 if (CPUCLOCK_PERTHREAD(which_clock) &&
1488 (CPUCLOCK_PID(which_clock) == 0 ||
1489 CPUCLOCK_PID(which_clock) == current->pid))
1490 return -EINVAL;
1491
1492 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1493
1494 if (error == -ERESTART_RESTARTBLOCK) {
1495
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001496 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001497 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001499 * Report back to the user the time still remaining.
1500 */
1501 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EFAULT;
1503
Toyo Abe1711ef32006-09-29 02:00:28 -07001504 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001505 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001506 restart_block->nanosleep.rmtp = rmtp;
1507 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return error;
1510}
1511
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001512static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001514 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001515 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001516 struct itimerspec it;
1517 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001518
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001519 t = ns_to_timespec(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001520
Toyo Abee4b76552006-09-29 02:00:29 -07001521 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1522
1523 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001524 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001525 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001526 * Report back to the user the time still remaining.
1527 */
1528 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Toyo Abee4b76552006-09-29 02:00:29 -07001529 return -EFAULT;
1530
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001531 restart_block->nanosleep.expires = timespec_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001532 }
1533 return error;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1538#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1539
Thomas Gleixnera924b042006-01-09 20:52:27 -08001540static int process_cpu_clock_getres(const clockid_t which_clock,
1541 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1544}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001545static int process_cpu_clock_get(const clockid_t which_clock,
1546 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1549}
1550static int process_cpu_timer_create(struct k_itimer *timer)
1551{
1552 timer->it_clock = PROCESS_CLOCK;
1553 return posix_cpu_timer_create(timer);
1554}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001555static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001556 struct timespec *rqtp,
1557 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001559 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
Toyo Abe1711ef32006-09-29 02:00:28 -07001561static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1562{
1563 return -EINVAL;
1564}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001565static int thread_cpu_clock_getres(const clockid_t which_clock,
1566 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1569}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001570static int thread_cpu_clock_get(const clockid_t which_clock,
1571 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1574}
1575static int thread_cpu_timer_create(struct k_itimer *timer)
1576{
1577 timer->it_clock = THREAD_CLOCK;
1578 return posix_cpu_timer_create(timer);
1579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Thomas Gleixner19769452011-02-01 13:51:06 +00001581struct k_clock clock_posix_cpu = {
1582 .clock_getres = posix_cpu_clock_getres,
1583 .clock_set = posix_cpu_clock_set,
1584 .clock_get = posix_cpu_clock_get,
1585 .timer_create = posix_cpu_timer_create,
1586 .nsleep = posix_cpu_nsleep,
1587 .nsleep_restart = posix_cpu_nsleep_restart,
1588 .timer_set = posix_cpu_timer_set,
1589 .timer_del = posix_cpu_timer_del,
1590 .timer_get = posix_cpu_timer_get,
1591};
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static __init int init_posix_cpu_timers(void)
1594{
1595 struct k_clock process = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001596 .clock_getres = process_cpu_clock_getres,
1597 .clock_get = process_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001598 .timer_create = process_cpu_timer_create,
1599 .nsleep = process_cpu_nsleep,
1600 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 };
1602 struct k_clock thread = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001603 .clock_getres = thread_cpu_clock_getres,
1604 .clock_get = thread_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001605 .timer_create = thread_cpu_timer_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 };
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001607 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Thomas Gleixner52708732011-02-02 12:10:09 +01001609 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1610 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001612 cputime_to_timespec(cputime_one_jiffy, &ts);
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001613 onecputick = ts.tv_nsec;
1614 WARN_ON(ts.tv_sec != 0);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617}
1618__initcall(init_posix_cpu_timers);