blob: 42670e9b44e0830f92c4238236906904dd505b51 [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>
Frederic Weisbeckera8572162013-04-18 01:31:13 +020013#include <linux/tick.h>
14#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Frank Mayharf06febc2008-09-12 09:54:39 -070016/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080017 * Called after updating RLIMIT_CPU to run cpu timer and update
18 * tsk->signal->cputime_expires expiration cache if necessary. Needs
19 * siglock protection since other code may update expiration cache as
20 * well.
Frank Mayharf06febc2008-09-12 09:54:39 -070021 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020022void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070023{
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +020024 cputime_t cputime = secs_to_cputime(rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -070025
Jiri Slaby5ab46b32009-08-28 14:05:12 +020026 spin_lock_irq(&task->sighand->siglock);
27 set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL);
28 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070029}
30
Thomas Gleixnera924b042006-01-09 20:52:27 -080031static int check_clock(const clockid_t which_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 int error = 0;
34 struct task_struct *p;
35 const pid_t pid = CPUCLOCK_PID(which_clock);
36
37 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
38 return -EINVAL;
39
40 if (pid == 0)
41 return 0;
42
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020043 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -080044 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -070045 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020046 same_thread_group(p, current) : has_group_leader_pid(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 error = -EINVAL;
48 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020049 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 return error;
52}
53
54static inline union cpu_time_count
Thomas Gleixnera924b042006-01-09 20:52:27 -080055timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 union cpu_time_count ret;
58 ret.sched = 0; /* high half always zero when .cpu used */
59 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
Oleg Nesterovee500f22005-11-28 13:43:55 -080060 ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 } else {
62 ret.cpu = timespec_to_cputime(tp);
63 }
64 return ret;
65}
66
Thomas Gleixnera924b042006-01-09 20:52:27 -080067static void sample_to_timespec(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 union cpu_time_count cpu,
69 struct timespec *tp)
70{
Roman Zippelf8bd2252008-05-01 04:34:31 -070071 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
72 *tp = ns_to_timespec(cpu.sched);
73 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 cputime_to_timespec(cpu.cpu, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Thomas Gleixnera924b042006-01-09 20:52:27 -080077static inline int cpu_time_before(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 union cpu_time_count now,
79 union cpu_time_count then)
80{
81 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
82 return now.sched < then.sched;
83 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +010084 return now.cpu < then.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86}
Thomas Gleixnera924b042006-01-09 20:52:27 -080087static inline void cpu_time_add(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 union cpu_time_count *acc,
89 union cpu_time_count val)
90{
91 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
92 acc->sched += val.sched;
93 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +010094 acc->cpu += val.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96}
Thomas Gleixnera924b042006-01-09 20:52:27 -080097static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 union cpu_time_count a,
99 union cpu_time_count b)
100{
101 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
102 a.sched -= b.sched;
103 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100104 a.cpu -= b.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106 return a;
107}
108
109/*
110 * Update expiry time from increment, and increase overrun count,
111 * given the current clock sample.
112 */
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400113static void bump_cpu_timer(struct k_itimer *timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 union cpu_time_count now)
115{
116 int i;
117
118 if (timer->it.cpu.incr.sched == 0)
119 return;
120
121 if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
122 unsigned long long delta, incr;
123
124 if (now.sched < timer->it.cpu.expires.sched)
125 return;
126 incr = timer->it.cpu.incr.sched;
127 delta = now.sched + incr - timer->it.cpu.expires.sched;
128 /* Don't use (incr*2 < delta), incr*2 might overflow. */
129 for (i = 0; incr < delta - incr; i++)
130 incr = incr << 1;
131 for (; i >= 0; incr >>= 1, i--) {
Oleg Nesterov7a4ed932005-10-26 20:26:53 +0400132 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 continue;
134 timer->it.cpu.expires.sched += incr;
135 timer->it_overrun += 1 << i;
136 delta -= incr;
137 }
138 } else {
139 cputime_t delta, incr;
140
Martin Schwidefsky64861632011-12-15 14:56:09 +0100141 if (now.cpu < timer->it.cpu.expires.cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return;
143 incr = timer->it.cpu.incr.cpu;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100144 delta = now.cpu + incr - timer->it.cpu.expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Don't use (incr*2 < delta), incr*2 might overflow. */
Martin Schwidefsky64861632011-12-15 14:56:09 +0100146 for (i = 0; incr < delta - incr; i++)
147 incr += incr;
148 for (; i >= 0; incr = incr >> 1, i--) {
149 if (delta < incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 continue;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100151 timer->it.cpu.expires.cpu += incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 timer->it_overrun += 1 << i;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100153 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156}
157
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200158/**
159 * task_cputime_zero - Check a task_cputime struct for all zero fields.
160 *
161 * @cputime: The struct to compare.
162 *
163 * Checks @cputime to see if all fields are zero. Returns true if all fields
164 * are zero, false if any field is nonzero.
165 */
166static inline int task_cputime_zero(const struct task_cputime *cputime)
167{
168 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
169 return 1;
170 return 0;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static inline cputime_t prof_ticks(struct task_struct *p)
174{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100175 cputime_t utime, stime;
176
177 task_cputime(p, &utime, &stime);
178
179 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181static inline cputime_t virt_ticks(struct task_struct *p)
182{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100183 cputime_t utime;
184
185 task_cputime(p, &utime, NULL);
186
187 return utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000190static int
191posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int error = check_clock(which_clock);
194 if (!error) {
195 tp->tv_sec = 0;
196 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
197 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
198 /*
199 * If sched_clock is using a cycle counter, we
200 * don't have any idea of its true resolution
201 * exported, but it is much more than 1s/HZ.
202 */
203 tp->tv_nsec = 1;
204 }
205 }
206 return error;
207}
208
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000209static int
210posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 /*
213 * You can never reset a CPU clock, but we check for other errors
214 * in the call before failing with EPERM.
215 */
216 int error = check_clock(which_clock);
217 if (error == 0) {
218 error = -EPERM;
219 }
220 return error;
221}
222
223
224/*
225 * Sample a per-thread clock for the given task.
226 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800227static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 union cpu_time_count *cpu)
229{
230 switch (CPUCLOCK_WHICH(which_clock)) {
231 default:
232 return -EINVAL;
233 case CPUCLOCK_PROF:
234 cpu->cpu = prof_ticks(p);
235 break;
236 case CPUCLOCK_VIRT:
237 cpu->cpu = virt_ticks(p);
238 break;
239 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900240 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 }
243 return 0;
244}
245
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100246static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
247{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100248 if (b->utime > a->utime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100249 a->utime = b->utime;
250
Martin Schwidefsky64861632011-12-15 14:56:09 +0100251 if (b->stime > a->stime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100252 a->stime = b->stime;
253
254 if (b->sum_exec_runtime > a->sum_exec_runtime)
255 a->sum_exec_runtime = b->sum_exec_runtime;
256}
257
258void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
259{
260 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
261 struct task_cputime sum;
262 unsigned long flags;
263
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100264 if (!cputimer->running) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100265 /*
266 * The POSIX timer interface allows for absolute time expiry
267 * values through the TIMER_ABSTIME flag, therefore we have
268 * to synchronize the timer to the clock every time we start
269 * it.
270 */
271 thread_group_cputime(tsk, &sum);
Linus Torvalds3cfef952011-10-26 16:17:32 +0200272 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200273 cputimer->running = 1;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100274 update_gt_cputime(&cputimer->cputime, &sum);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200275 } else
Linus Torvalds3cfef952011-10-26 16:17:32 +0200276 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100277 *times = cputimer->cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200278 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * Sample a process (thread group) clock for the given group_leader task.
283 * Must be called with tasklist_lock held for reading.
284 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800285static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct task_struct *p,
287 union cpu_time_count *cpu)
288{
Frank Mayharbb34d922008-09-12 09:54:39 -0700289 struct task_cputime cputime;
290
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100291 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700292 default:
293 return -EINVAL;
294 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900295 thread_group_cputime(p, &cputime);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100296 cpu->cpu = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700297 break;
298 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900299 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700300 cpu->cpu = cputime.utime;
301 break;
302 case CPUCLOCK_SCHED:
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200303 thread_group_cputime(p, &cputime);
304 cpu->sched = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700305 break;
306 }
307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000311static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 const pid_t pid = CPUCLOCK_PID(which_clock);
314 int error = -EINVAL;
315 union cpu_time_count rtn;
316
317 if (pid == 0) {
318 /*
319 * Special case constant value for our own clocks.
320 * We don't have to do any lookup to find ourselves.
321 */
322 if (CPUCLOCK_PERTHREAD(which_clock)) {
323 /*
324 * Sampling just ourselves we can do with no locking.
325 */
326 error = cpu_clock_sample(which_clock,
327 current, &rtn);
328 } else {
329 read_lock(&tasklist_lock);
330 error = cpu_clock_sample_group(which_clock,
331 current, &rtn);
332 read_unlock(&tasklist_lock);
333 }
334 } else {
335 /*
336 * Find the given PID, and validate that the caller
337 * should be able to see it.
338 */
339 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800340 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800341 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (p) {
343 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700344 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 error = cpu_clock_sample(which_clock,
346 p, &rtn);
347 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800348 } else {
349 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700350 if (thread_group_leader(p) && p->sighand) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800351 error =
352 cpu_clock_sample_group(which_clock,
353 p, &rtn);
354 }
355 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800358 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 if (error)
362 return error;
363 sample_to_timespec(which_clock, rtn, tp);
364 return 0;
365}
366
367
368/*
369 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800370 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
371 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000373static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int ret = 0;
376 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
377 struct task_struct *p;
378
379 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
380 return -EINVAL;
381
382 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200384 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
386 if (pid == 0) {
387 p = current;
388 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800389 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700390 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 p = NULL;
392 }
393 } else {
394 if (pid == 0) {
395 p = current->group_leader;
396 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800397 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200398 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 p = NULL;
400 }
401 }
402 new_timer->it.cpu.task = p;
403 if (p) {
404 get_task_struct(p);
405 } else {
406 ret = -EINVAL;
407 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200408 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 return ret;
411}
412
413/*
414 * Clean up a CPU-clock timer that is about to be destroyed.
415 * This is called from timer deletion with the timer already locked.
416 * If we return TIMER_RETRY, it's necessary to release the timer's lock
417 * and try again. (This happens when the timer is in the middle of firing.)
418 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000419static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400422 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Oleg Nesterov108150e2005-10-23 20:25:39 +0400424 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700425 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700426 if (unlikely(p->sighand == NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700427 /*
428 * We raced with the reaping of the task.
429 * The deletion should have cleared us off the list.
430 */
431 BUG_ON(!list_empty(&timer->it.cpu.entry));
432 } else {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700433 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400434 if (timer->it.cpu.firing)
435 ret = TIMER_RETRY;
436 else
437 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700438 spin_unlock(&p->sighand->siglock);
439 }
440 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Oleg Nesterov108150e2005-10-23 20:25:39 +0400442 if (!ret)
443 put_task_struct(p);
444 }
445
446 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Clean out CPU timers still ticking when a thread exited. The task
451 * pointer is cleared, and the expiry time is replaced with the residual
452 * time for later timer_gettime calls to return.
453 * This must be called with the siglock held.
454 */
455static void cleanup_timers(struct list_head *head,
456 cputime_t utime, cputime_t stime,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200457 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct cpu_timer_list *timer, *next;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100460 cputime_t ptime = utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100464 if (timer->expires.cpu < ptime) {
465 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100467 timer->expires.cpu -= ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470
471 ++head;
472 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100474 if (timer->expires.cpu < utime) {
475 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100477 timer->expires.cpu -= utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 }
480
481 ++head;
482 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200484 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 timer->expires.sched = 0;
486 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200487 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 }
490}
491
492/*
493 * These are both called with the siglock held, when the current thread
494 * is being reaped. When the final (leader) thread in the group is reaped,
495 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
496 */
497void posix_cpu_timers_exit(struct task_struct *tsk)
498{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100499 cputime_t utime, stime;
500
Nick Kossifidis61337052012-12-16 22:18:11 -0500501 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
502 sizeof(unsigned long long));
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100503 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 cleanup_timers(tsk->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100505 utime, stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507}
508void posix_cpu_timers_exit_group(struct task_struct *tsk)
509{
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700510 struct signal_struct *const sig = tsk->signal;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100511 cputime_t utime, stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700512
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100513 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 cleanup_timers(tsk->signal->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100515 utime + sig->utime, stime + sig->stime,
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700516 tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
520{
521 /*
522 * That's all for this thread or process.
523 * We leave our residual in expires to be reported.
524 */
525 put_task_struct(timer->it.cpu.task);
526 timer->it.cpu.task = NULL;
527 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
528 timer->it.cpu.expires,
529 now);
530}
531
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200532static inline int expires_gt(cputime_t expires, cputime_t new_exp)
533{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100534 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
538 * Insert the timer on the appropriate list before any timers that
539 * expire later. This must be called with the tasklist_lock held
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800540 * for reading, interrupts disabled and p->sighand->siglock taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800542static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct task_struct *p = timer->it.cpu.task;
545 struct list_head *head, *listpos;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800546 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct cpu_timer_list *const nt = &timer->it.cpu;
548 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800550 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
551 head = p->cpu_timers;
552 cputime_expires = &p->cputime_expires;
553 } else {
554 head = p->signal->cpu_timers;
555 cputime_expires = &p->signal->cputime_expires;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 head += CPUCLOCK_WHICH(timer->it_clock);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800560 list_for_each_entry(next, head, entry) {
561 if (cpu_time_before(timer->it_clock, nt->expires, next->expires))
562 break;
563 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 list_add(&nt->entry, listpos);
566
567 if (listpos == head) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800568 union cpu_time_count *exp = &nt->expires;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800571 * We are the new earliest-expiring POSIX 1.b timer, hence
572 * need to update expiration cache. Take into account that
573 * for process timers we share expiration cache with itimers
574 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
576
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800577 switch (CPUCLOCK_WHICH(timer->it_clock)) {
578 case CPUCLOCK_PROF:
579 if (expires_gt(cputime_expires->prof_exp, exp->cpu))
580 cputime_expires->prof_exp = exp->cpu;
581 break;
582 case CPUCLOCK_VIRT:
583 if (expires_gt(cputime_expires->virt_exp, exp->cpu))
584 cputime_expires->virt_exp = exp->cpu;
585 break;
586 case CPUCLOCK_SCHED:
587 if (cputime_expires->sched_exp == 0 ||
588 cputime_expires->sched_exp > exp->sched)
589 cputime_expires->sched_exp = exp->sched;
590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595/*
596 * The timer is locked, fire it and arrange for its reload.
597 */
598static void cpu_timer_fire(struct k_itimer *timer)
599{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800600 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
601 /*
602 * User don't want any signal.
603 */
604 timer->it.cpu.expires.sched = 0;
605 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * This a special case for clock_nanosleep,
608 * not a normal timer from sys_timer_create.
609 */
610 wake_up_process(timer->it_process);
611 timer->it.cpu.expires.sched = 0;
612 } else if (timer->it.cpu.incr.sched == 0) {
613 /*
614 * One-shot timer. Clear it as soon as it's fired.
615 */
616 posix_timer_event(timer, 0);
617 timer->it.cpu.expires.sched = 0;
618 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
619 /*
620 * The signal did not get queued because the signal
621 * was ignored, so we won't get any callback to
622 * reload the timer. But we need to keep it
623 * ticking in case the signal is deliverable next time.
624 */
625 posix_cpu_timer_schedule(timer);
626 }
627}
628
629/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100630 * Sample a process (thread group) timer for the given group_leader task.
631 * Must be called with tasklist_lock held for reading.
632 */
633static int cpu_timer_sample_group(const clockid_t which_clock,
634 struct task_struct *p,
635 union cpu_time_count *cpu)
636{
637 struct task_cputime cputime;
638
639 thread_group_cputimer(p, &cputime);
640 switch (CPUCLOCK_WHICH(which_clock)) {
641 default:
642 return -EINVAL;
643 case CPUCLOCK_PROF:
Martin Schwidefsky64861632011-12-15 14:56:09 +0100644 cpu->cpu = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100645 break;
646 case CPUCLOCK_VIRT:
647 cpu->cpu = cputime.utime;
648 break;
649 case CPUCLOCK_SCHED:
650 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
651 break;
652 }
653 return 0;
654}
655
Frederic Weisbeckera8572162013-04-18 01:31:13 +0200656#ifdef CONFIG_NO_HZ_FULL
657static void nohz_kick_work_fn(struct work_struct *work)
658{
659 tick_nohz_full_kick_all();
660}
661
662static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn);
663
664/*
665 * We need the IPIs to be sent from sane process context.
666 * The posix cpu timers are always set with irqs disabled.
667 */
668static void posix_cpu_timer_kick_nohz(void)
669{
670 schedule_work(&nohz_kick_work);
671}
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200672
673bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk)
674{
675 if (!task_cputime_zero(&tsk->cputime_expires))
Frederic Weisbecker6ac29172013-04-21 20:28:38 +0200676 return false;
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200677
678 if (tsk->signal->cputimer.running)
Frederic Weisbecker6ac29172013-04-21 20:28:38 +0200679 return false;
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200680
Frederic Weisbecker6ac29172013-04-21 20:28:38 +0200681 return true;
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200682}
Frederic Weisbeckera8572162013-04-18 01:31:13 +0200683#else
684static inline void posix_cpu_timer_kick_nohz(void) { }
685#endif
686
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100687/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * Guts of sys_timer_settime for CPU timers.
689 * This is called with the timer locked and interrupts disabled.
690 * If we return TIMER_RETRY, it's necessary to release the timer's lock
691 * and try again. (This happens when the timer is in the middle of firing.)
692 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000693static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
694 struct itimerspec *new, struct itimerspec *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct task_struct *p = timer->it.cpu.task;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800697 union cpu_time_count old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int ret;
699
700 if (unlikely(p == NULL)) {
701 /*
702 * Timer refers to a dead task's clock.
703 */
704 return -ESRCH;
705 }
706
707 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
708
709 read_lock(&tasklist_lock);
710 /*
711 * We need the tasklist_lock to protect against reaping that
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700712 * clears p->sighand. If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * longer get any information about it at all.
714 */
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700715 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 read_unlock(&tasklist_lock);
717 put_task_struct(p);
718 timer->it.cpu.task = NULL;
719 return -ESRCH;
720 }
721
722 /*
723 * Disarm any old timer after extracting its expiry time.
724 */
725 BUG_ON(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400726
727 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800728 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_lock(&p->sighand->siglock);
730 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400731 if (unlikely(timer->it.cpu.firing)) {
732 timer->it.cpu.firing = -1;
733 ret = TIMER_RETRY;
734 } else
735 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * We need to sample the current value to convert the new
739 * value from to relative and absolute, and to convert the
740 * old value from absolute to relative. To set a process
741 * timer, we need a sample to balance the thread expiry
742 * times (in arm_timer). With an absolute time, we must
743 * check if it's already passed. In short, we need a sample.
744 */
745 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
746 cpu_clock_sample(timer->it_clock, p, &val);
747 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100748 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751 if (old) {
752 if (old_expires.sched == 0) {
753 old->it_value.tv_sec = 0;
754 old->it_value.tv_nsec = 0;
755 } else {
756 /*
757 * Update the timer in case it has
758 * overrun already. If it has,
759 * we'll report it as having overrun
760 * and with the next reloaded timer
761 * already ticking, though we are
762 * swallowing that pending
763 * notification here to install the
764 * new setting.
765 */
766 bump_cpu_timer(timer, val);
767 if (cpu_time_before(timer->it_clock, val,
768 timer->it.cpu.expires)) {
769 old_expires = cpu_time_sub(
770 timer->it_clock,
771 timer->it.cpu.expires, val);
772 sample_to_timespec(timer->it_clock,
773 old_expires,
774 &old->it_value);
775 } else {
776 old->it_value.tv_nsec = 1;
777 old->it_value.tv_sec = 0;
778 }
779 }
780 }
781
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400782 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * We are colliding with the timer actually firing.
785 * Punt after filling in the timer's old value, and
786 * disable this firing since we are already reporting
787 * it as an overrun (thanks to bump_cpu_timer above).
788 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800789 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto out;
792 }
793
794 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
795 cpu_time_add(timer->it_clock, &new_expires, val);
796 }
797
798 /*
799 * Install the new expiry time (or zero).
800 * For a timer with no notification action, we don't actually
801 * arm the timer (we'll just fake it for timer_gettime).
802 */
803 timer->it.cpu.expires = new_expires;
804 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 cpu_time_before(timer->it_clock, val, new_expires)) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800806 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800809 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 read_unlock(&tasklist_lock);
811
812 /*
813 * Install the new reload setting, and
814 * set up the signal and overrun bookkeeping.
815 */
816 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
817 &new->it_interval);
818
819 /*
820 * This acts as a modification timestamp for the timer,
821 * so any automatic reload attempt will punt on seeing
822 * that we have reset the timer manually.
823 */
824 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
825 ~REQUEUE_PENDING;
826 timer->it_overrun_last = 0;
827 timer->it_overrun = -1;
828
829 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 !cpu_time_before(timer->it_clock, val, new_expires)) {
831 /*
832 * The designated time already passed, so we notify
833 * immediately, even if the thread never runs to
834 * accumulate more time on this clock.
835 */
836 cpu_timer_fire(timer);
837 }
838
839 ret = 0;
840 out:
841 if (old) {
842 sample_to_timespec(timer->it_clock,
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800843 old_incr, &old->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +0200845 if (!ret)
846 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return ret;
848}
849
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000850static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 union cpu_time_count now;
853 struct task_struct *p = timer->it.cpu.task;
854 int clear_dead;
855
856 /*
857 * Easy part: convert the reload time.
858 */
859 sample_to_timespec(timer->it_clock,
860 timer->it.cpu.incr, &itp->it_interval);
861
862 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
863 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
864 return;
865 }
866
867 if (unlikely(p == NULL)) {
868 /*
869 * This task already died and the timer will never fire.
870 * In this case, expires is actually the dead value.
871 */
872 dead:
873 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
874 &itp->it_value);
875 return;
876 }
877
878 /*
879 * Sample the clock to take the difference with the expiry time.
880 */
881 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
882 cpu_clock_sample(timer->it_clock, p, &now);
883 clear_dead = p->exit_state;
884 } else {
885 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700886 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
888 * The process has been reaped.
889 * We can't even collect a sample any more.
890 * Call the timer disarmed, nothing else to do.
891 */
892 put_task_struct(p);
893 timer->it.cpu.task = NULL;
894 timer->it.cpu.expires.sched = 0;
895 read_unlock(&tasklist_lock);
896 goto dead;
897 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100898 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 clear_dead = (unlikely(p->exit_state) &&
900 thread_group_empty(p));
901 }
902 read_unlock(&tasklist_lock);
903 }
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (unlikely(clear_dead)) {
906 /*
907 * We've noticed that the thread is dead, but
908 * not yet reaped. Take this opportunity to
909 * drop our task ref.
910 */
911 clear_dead_task(timer, now);
912 goto dead;
913 }
914
915 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
916 sample_to_timespec(timer->it_clock,
917 cpu_time_sub(timer->it_clock,
918 timer->it.cpu.expires, now),
919 &itp->it_value);
920 } else {
921 /*
922 * The timer should have expired already, but the firing
923 * hasn't taken place yet. Say it's just about to expire.
924 */
925 itp->it_value.tv_nsec = 1;
926 itp->it_value.tv_sec = 0;
927 }
928}
929
930/*
931 * Check for any per-thread CPU timers that have fired and move them off
932 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
933 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
934 */
935static void check_thread_timers(struct task_struct *tsk,
936 struct list_head *firing)
937{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700938 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100940 struct signal_struct *const sig = tsk->signal;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800941 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvaldse80eda92005-10-23 10:02:50 -0700943 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100944 tsk->cputime_expires.prof_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700946 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct cpu_timer_list,
948 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100949 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700950 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952 }
953 t->firing = 1;
954 list_move_tail(&t->entry, firing);
955 }
956
957 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700958 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100959 tsk->cputime_expires.virt_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700961 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct cpu_timer_list,
963 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100964 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700965 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
967 }
968 t->firing = 1;
969 list_move_tail(&t->entry, firing);
970 }
971
972 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700973 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700974 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700976 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct cpu_timer_list,
978 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200979 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700980 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 }
983 t->firing = 1;
984 list_move_tail(&t->entry, firing);
985 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100986
987 /*
988 * Check for the special case thread timers.
989 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800990 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800991 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800992 unsigned long hard =
993 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100994
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100995 if (hard != RLIM_INFINITY &&
996 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100997 /*
998 * At the hard limit, we just die.
999 * No need to calculate anything else now.
1000 */
1001 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1002 return;
1003 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001004 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001005 /*
1006 * At the soft limit, send a SIGXCPU every second.
1007 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001008 if (soft < hard) {
1009 soft += USEC_PER_SEC;
1010 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001011 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -07001012 printk(KERN_INFO
1013 "RT Watchdog Timeout: %s[%d]\n",
1014 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +01001015 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1016 }
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Stanislaw Gruszka15365c12010-03-11 14:04:31 -08001020static void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001021{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -08001022 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001023 unsigned long flags;
1024
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001025 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001026 cputimer->running = 0;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001027 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001028}
1029
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001030static u32 onecputick;
1031
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001032static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
1033 cputime_t *expires, cputime_t cur_time, int signo)
1034{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001035 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001036 return;
1037
Martin Schwidefsky64861632011-12-15 14:56:09 +01001038 if (cur_time >= it->expires) {
1039 if (it->incr) {
1040 it->expires += it->incr;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001041 it->error += it->incr_error;
1042 if (it->error >= onecputick) {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001043 it->expires -= cputime_one_jiffy;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001044 it->error -= onecputick;
1045 }
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001046 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001047 it->expires = 0;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001048 }
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001049
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001050 trace_itimer_expire(signo == SIGPROF ?
1051 ITIMER_PROF : ITIMER_VIRTUAL,
1052 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001053 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
1054 }
1055
Martin Schwidefsky64861632011-12-15 14:56:09 +01001056 if (it->expires && (!*expires || it->expires < *expires)) {
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001057 *expires = it->expires;
1058 }
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/*
1062 * Check for any per-thread CPU timers that have fired and move them
1063 * off the tsk->*_timers list onto the firing list. Per-thread timers
1064 * have already been taken off.
1065 */
1066static void check_process_timers(struct task_struct *tsk,
1067 struct list_head *firing)
1068{
Linus Torvaldse80eda92005-10-23 10:02:50 -07001069 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -07001071 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +02001072 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -07001074 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001075 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * Collect the current process totals.
1079 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001080 thread_group_cputimer(tsk, &cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001081 utime = cputime.utime;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001082 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001083 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001084 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001085 prof_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001087 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct cpu_timer_list,
1089 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001090 if (!--maxfire || ptime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001091 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 }
WANG Congee7dd202008-04-04 20:54:10 +02001094 tl->firing = 1;
1095 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001099 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001100 virt_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001102 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct cpu_timer_list,
1104 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001105 if (!--maxfire || utime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001106 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
1108 }
WANG Congee7dd202008-04-04 20:54:10 +02001109 tl->firing = 1;
1110 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
1113 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001114 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 sched_expires = 0;
1116 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001117 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct cpu_timer_list,
1119 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001120 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1121 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123 }
WANG Congee7dd202008-04-04 20:54:10 +02001124 tl->firing = 1;
1125 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 /*
1129 * Check for the special case process timers.
1130 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001131 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
1132 SIGPROF);
1133 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
1134 SIGVTALRM);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001135 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001136 if (soft != RLIM_INFINITY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 unsigned long psecs = cputime_to_secs(ptime);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001138 unsigned long hard =
1139 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 cputime_t x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001141 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * At the hard limit, we just die.
1144 * No need to calculate anything else now.
1145 */
1146 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1147 return;
1148 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001149 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * At the soft limit, send a SIGXCPU every second.
1152 */
1153 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001154 if (soft < hard) {
1155 soft++;
1156 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001159 x = secs_to_cputime(soft);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001160 if (!prof_expires || x < prof_expires) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 prof_expires = x;
1162 }
1163 }
1164
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001165 sig->cputime_expires.prof_exp = prof_expires;
1166 sig->cputime_expires.virt_exp = virt_expires;
1167 sig->cputime_expires.sched_exp = sched_expires;
1168 if (task_cputime_zero(&sig->cputime_expires))
1169 stop_process_timers(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/*
1173 * This is called from the signal code (via do_schedule_next_timer)
1174 * when the last timer signal was delivered and we have to reload the timer.
1175 */
1176void posix_cpu_timer_schedule(struct k_itimer *timer)
1177{
1178 struct task_struct *p = timer->it.cpu.task;
1179 union cpu_time_count now;
1180
1181 if (unlikely(p == NULL))
1182 /*
1183 * The task was cleaned up already, no future firings.
1184 */
Roland McGrath708f4302005-10-30 15:03:13 -08001185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /*
1188 * Fetch the current sample and update the timer's expiry time.
1189 */
1190 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1191 cpu_clock_sample(timer->it_clock, p, &now);
1192 bump_cpu_timer(timer, now);
1193 if (unlikely(p->exit_state)) {
1194 clear_dead_task(timer, now);
Roland McGrath708f4302005-10-30 15:03:13 -08001195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 read_lock(&tasklist_lock); /* arm_timer needs it. */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001198 spin_lock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
1200 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -07001201 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * The process has been reaped.
1204 * We can't even collect a sample any more.
1205 */
1206 put_task_struct(p);
1207 timer->it.cpu.task = p = NULL;
1208 timer->it.cpu.expires.sched = 0;
Roland McGrath708f4302005-10-30 15:03:13 -08001209 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1211 /*
1212 * We've noticed that the thread is dead, but
1213 * not yet reaped. Take this opportunity to
1214 * drop our task ref.
1215 */
1216 clear_dead_task(timer, now);
Roland McGrath708f4302005-10-30 15:03:13 -08001217 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001219 spin_lock(&p->sighand->siglock);
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001220 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 bump_cpu_timer(timer, now);
1222 /* Leave the tasklist_lock locked for the call below. */
1223 }
1224
1225 /*
1226 * Now re-arm for the new expiry time.
1227 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001228 BUG_ON(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001229 arm_timer(timer);
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001230 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Roland McGrath708f4302005-10-30 15:03:13 -08001232out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 read_unlock(&tasklist_lock);
Roland McGrath708f4302005-10-30 15:03:13 -08001234
1235out:
1236 timer->it_overrun_last = timer->it_overrun;
1237 timer->it_overrun = -1;
1238 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Frank Mayharf06febc2008-09-12 09:54:39 -07001241/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001242 * task_cputime_expired - Compare two task_cputime entities.
1243 *
1244 * @sample: The task_cputime structure to be checked for expiration.
1245 * @expires: Expiration times, against which @sample will be checked.
1246 *
1247 * Checks @sample against @expires to see if any field of @sample has expired.
1248 * Returns true if any field of the former is greater than the corresponding
1249 * field of the latter if the latter field is set. Otherwise returns false.
1250 */
1251static inline int task_cputime_expired(const struct task_cputime *sample,
1252 const struct task_cputime *expires)
1253{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001254 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001255 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001256 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001257 return 1;
1258 if (expires->sum_exec_runtime != 0 &&
1259 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1260 return 1;
1261 return 0;
1262}
1263
1264/**
1265 * fastpath_timer_check - POSIX CPU timers fast path.
1266 *
1267 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001268 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001269 * Check the task and thread group timers. If both are zero (there are no
1270 * timers set) return false. Otherwise snapshot the task and thread group
1271 * timers and compare them with the corresponding expiration times. Return
1272 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001273 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001274static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001275{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001276 struct signal_struct *sig;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001277 cputime_t utime, stime;
1278
1279 task_cputime(tsk, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001280
Frank Mayharbb34d922008-09-12 09:54:39 -07001281 if (!task_cputime_zero(&tsk->cputime_expires)) {
1282 struct task_cputime task_sample = {
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001283 .utime = utime,
1284 .stime = stime,
Frank Mayharbb34d922008-09-12 09:54:39 -07001285 .sum_exec_runtime = tsk->se.sum_exec_runtime
1286 };
1287
1288 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1289 return 1;
1290 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001291
1292 sig = tsk->signal;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001293 if (sig->cputimer.running) {
Frank Mayharbb34d922008-09-12 09:54:39 -07001294 struct task_cputime group_sample;
1295
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001296 raw_spin_lock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001297 group_sample = sig->cputimer.cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001298 raw_spin_unlock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001299
Frank Mayharbb34d922008-09-12 09:54:39 -07001300 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1301 return 1;
1302 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001303
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001304 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001305}
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/*
1308 * This is called from the timer interrupt handler. The irq handler has
1309 * already updated our counts. We need to check if any timers fire now.
1310 * Interrupts are disabled.
1311 */
1312void run_posix_cpu_timers(struct task_struct *tsk)
1313{
1314 LIST_HEAD(firing);
1315 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001316 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 BUG_ON(!irqs_disabled());
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001321 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001322 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001324 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001325 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001326
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001327 if (!lock_task_sighand(tsk, &flags))
1328 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001329 /*
1330 * Here we take off tsk->signal->cpu_timers[N] and
1331 * tsk->cpu_timers[N] all the timers that are firing, and
1332 * put them on the firing list.
1333 */
1334 check_thread_timers(tsk, &firing);
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001335 /*
1336 * If there are any active process wide timers (POSIX 1.b, itimers,
1337 * RLIMIT_CPU) cputimer must be running.
1338 */
1339 if (tsk->signal->cputimer.running)
1340 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Frank Mayharbb34d922008-09-12 09:54:39 -07001342 /*
1343 * We must release these locks before taking any timer's lock.
1344 * There is a potential race with timer deletion here, as the
1345 * siglock now protects our private firing list. We have set
1346 * the firing flag in each timer, so that a deletion attempt
1347 * that gets the timer lock before we do will give it up and
1348 * spin until we've taken care of that timer below.
1349 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001350 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /*
1353 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001354 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * each timer's lock before clearing its firing flag, so no
1356 * timer call will interfere.
1357 */
1358 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001359 int cpu_firing;
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_lock(&timer->it_lock);
1362 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001363 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 timer->it.cpu.firing = 0;
1365 /*
1366 * The firing flag is -1 if we collided with a reset
1367 * of the timer, which already reported this
1368 * almost-firing as an overrun. So don't generate an event.
1369 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001370 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 spin_unlock(&timer->it_lock);
1373 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001374
1375 /*
1376 * In case some timers were rescheduled after the queue got emptied,
1377 * wake up full dynticks CPUs.
1378 */
1379 if (tsk->signal->cputimer.running)
1380 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001384 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001385 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
1387void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1388 cputime_t *newval, cputime_t *oldval)
1389{
1390 union cpu_time_count now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001393 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001396 /*
1397 * We are setting itimer. The *oldval is absolute and we update
1398 * it to be relative, *newval argument is relative and we update
1399 * it to be absolute.
1400 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001401 if (*oldval) {
1402 if (*oldval <= now.cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Just about to fire. */
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001404 *oldval = cputime_one_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001406 *oldval -= now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 }
1409
Martin Schwidefsky64861632011-12-15 14:56:09 +01001410 if (!*newval)
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001411 goto out;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001412 *newval += now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001416 * Update expiration cache if we are the earliest timer, or eventually
1417 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001419 switch (clock_idx) {
1420 case CPUCLOCK_PROF:
1421 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001422 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001423 break;
1424 case CPUCLOCK_VIRT:
1425 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001426 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001429out:
1430 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Toyo Abee4b76552006-09-29 02:00:29 -07001433static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1434 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct k_itimer timer;
1437 int error;
1438
1439 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * Set up a temporary timer and then wait for it to go off.
1441 */
1442 memset(&timer, 0, sizeof timer);
1443 spin_lock_init(&timer.it_lock);
1444 timer.it_clock = which_clock;
1445 timer.it_overrun = -1;
1446 error = posix_cpu_timer_create(&timer);
1447 timer.it_process = current;
1448 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001450
1451 memset(it, 0, sizeof *it);
1452 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001455 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (error) {
1457 spin_unlock_irq(&timer.it_lock);
1458 return error;
1459 }
1460
1461 while (!signal_pending(current)) {
1462 if (timer.it.cpu.expires.sched == 0) {
1463 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001464 * Our timer fired and was reset, below
1465 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001467 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 spin_unlock_irq(&timer.it_lock);
1469 return 0;
1470 }
1471
1472 /*
1473 * Block until cpu_timer_fire (or a signal) wakes us.
1474 */
1475 __set_current_state(TASK_INTERRUPTIBLE);
1476 spin_unlock_irq(&timer.it_lock);
1477 schedule();
1478 spin_lock_irq(&timer.it_lock);
1479 }
1480
1481 /*
1482 * We were interrupted by a signal.
1483 */
1484 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001485 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
1486 if (!error) {
1487 /*
1488 * Timer is now unarmed, deletion can not fail.
1489 */
1490 posix_cpu_timer_del(&timer);
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 spin_unlock_irq(&timer.it_lock);
1493
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001494 while (error == TIMER_RETRY) {
1495 /*
1496 * We need to handle case when timer was or is in the
1497 * middle of firing. In other cases we already freed
1498 * resources.
1499 */
1500 spin_lock_irq(&timer.it_lock);
1501 error = posix_cpu_timer_del(&timer);
1502 spin_unlock_irq(&timer.it_lock);
1503 }
1504
Toyo Abee4b76552006-09-29 02:00:29 -07001505 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * It actually did fire already.
1508 */
1509 return 0;
1510 }
1511
Toyo Abee4b76552006-09-29 02:00:29 -07001512 error = -ERESTART_RESTARTBLOCK;
1513 }
1514
1515 return error;
1516}
1517
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001518static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1519
1520static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1521 struct timespec *rqtp, struct timespec __user *rmtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001522{
1523 struct restart_block *restart_block =
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001524 &current_thread_info()->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001525 struct itimerspec it;
1526 int error;
1527
1528 /*
1529 * Diagnose required errors first.
1530 */
1531 if (CPUCLOCK_PERTHREAD(which_clock) &&
1532 (CPUCLOCK_PID(which_clock) == 0 ||
1533 CPUCLOCK_PID(which_clock) == current->pid))
1534 return -EINVAL;
1535
1536 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1537
1538 if (error == -ERESTART_RESTARTBLOCK) {
1539
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001540 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001541 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001543 * Report back to the user the time still remaining.
1544 */
1545 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return -EFAULT;
1547
Toyo Abe1711ef32006-09-29 02:00:28 -07001548 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001549 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001550 restart_block->nanosleep.rmtp = rmtp;
1551 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return error;
1554}
1555
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001556static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001558 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001559 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001560 struct itimerspec it;
1561 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001562
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001563 t = ns_to_timespec(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001564
Toyo Abee4b76552006-09-29 02:00:29 -07001565 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1566
1567 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001568 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001569 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001570 * Report back to the user the time still remaining.
1571 */
1572 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Toyo Abee4b76552006-09-29 02:00:29 -07001573 return -EFAULT;
1574
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001575 restart_block->nanosleep.expires = timespec_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001576 }
1577 return error;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1582#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1583
Thomas Gleixnera924b042006-01-09 20:52:27 -08001584static int process_cpu_clock_getres(const clockid_t which_clock,
1585 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1588}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001589static int process_cpu_clock_get(const clockid_t which_clock,
1590 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1593}
1594static int process_cpu_timer_create(struct k_itimer *timer)
1595{
1596 timer->it_clock = PROCESS_CLOCK;
1597 return posix_cpu_timer_create(timer);
1598}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001599static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001600 struct timespec *rqtp,
1601 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001603 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
Toyo Abe1711ef32006-09-29 02:00:28 -07001605static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1606{
1607 return -EINVAL;
1608}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001609static int thread_cpu_clock_getres(const clockid_t which_clock,
1610 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1613}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001614static int thread_cpu_clock_get(const clockid_t which_clock,
1615 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1618}
1619static int thread_cpu_timer_create(struct k_itimer *timer)
1620{
1621 timer->it_clock = THREAD_CLOCK;
1622 return posix_cpu_timer_create(timer);
1623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Thomas Gleixner19769452011-02-01 13:51:06 +00001625struct k_clock clock_posix_cpu = {
1626 .clock_getres = posix_cpu_clock_getres,
1627 .clock_set = posix_cpu_clock_set,
1628 .clock_get = posix_cpu_clock_get,
1629 .timer_create = posix_cpu_timer_create,
1630 .nsleep = posix_cpu_nsleep,
1631 .nsleep_restart = posix_cpu_nsleep_restart,
1632 .timer_set = posix_cpu_timer_set,
1633 .timer_del = posix_cpu_timer_del,
1634 .timer_get = posix_cpu_timer_get,
1635};
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637static __init int init_posix_cpu_timers(void)
1638{
1639 struct k_clock process = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001640 .clock_getres = process_cpu_clock_getres,
1641 .clock_get = process_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001642 .timer_create = process_cpu_timer_create,
1643 .nsleep = process_cpu_nsleep,
1644 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 };
1646 struct k_clock thread = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001647 .clock_getres = thread_cpu_clock_getres,
1648 .clock_get = thread_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001649 .timer_create = thread_cpu_timer_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 };
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001651 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Thomas Gleixner52708732011-02-02 12:10:09 +01001653 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1654 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001656 cputime_to_timespec(cputime_one_jiffy, &ts);
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001657 onecputick = ts.tv_nsec;
1658 WARN_ON(ts.tv_sec != 0);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return 0;
1661}
1662__initcall(init_posix_cpu_timers);