blob: 0bc33561a4355b1cb1d90efd6a07fbe177735a1f [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 Emelyanovbac0abd62007-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
158static inline cputime_t prof_ticks(struct task_struct *p)
159{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100160 cputime_t utime, stime;
161
162 task_cputime(p, &utime, &stime);
163
164 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166static inline cputime_t virt_ticks(struct task_struct *p)
167{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100168 cputime_t utime;
169
170 task_cputime(p, &utime, NULL);
171
172 return utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000175static int
176posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int error = check_clock(which_clock);
179 if (!error) {
180 tp->tv_sec = 0;
181 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
182 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
183 /*
184 * If sched_clock is using a cycle counter, we
185 * don't have any idea of its true resolution
186 * exported, but it is much more than 1s/HZ.
187 */
188 tp->tv_nsec = 1;
189 }
190 }
191 return error;
192}
193
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000194static int
195posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 /*
198 * You can never reset a CPU clock, but we check for other errors
199 * in the call before failing with EPERM.
200 */
201 int error = check_clock(which_clock);
202 if (error == 0) {
203 error = -EPERM;
204 }
205 return error;
206}
207
208
209/*
210 * Sample a per-thread clock for the given task.
211 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800212static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 union cpu_time_count *cpu)
214{
215 switch (CPUCLOCK_WHICH(which_clock)) {
216 default:
217 return -EINVAL;
218 case CPUCLOCK_PROF:
219 cpu->cpu = prof_ticks(p);
220 break;
221 case CPUCLOCK_VIRT:
222 cpu->cpu = virt_ticks(p);
223 break;
224 case CPUCLOCK_SCHED:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900225 cpu->sched = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 }
228 return 0;
229}
230
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100231static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
232{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100233 if (b->utime > a->utime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100234 a->utime = b->utime;
235
Martin Schwidefsky64861632011-12-15 14:56:09 +0100236 if (b->stime > a->stime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100237 a->stime = b->stime;
238
239 if (b->sum_exec_runtime > a->sum_exec_runtime)
240 a->sum_exec_runtime = b->sum_exec_runtime;
241}
242
243void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
244{
245 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
246 struct task_cputime sum;
247 unsigned long flags;
248
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100249 if (!cputimer->running) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100250 /*
251 * The POSIX timer interface allows for absolute time expiry
252 * values through the TIMER_ABSTIME flag, therefore we have
253 * to synchronize the timer to the clock every time we start
254 * it.
255 */
256 thread_group_cputime(tsk, &sum);
Linus Torvalds3cfef952011-10-26 16:17:32 +0200257 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200258 cputimer->running = 1;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100259 update_gt_cputime(&cputimer->cputime, &sum);
Peter Zijlstrabcd5cff2011-10-17 11:50:30 +0200260 } else
Linus Torvalds3cfef952011-10-26 16:17:32 +0200261 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100262 *times = cputimer->cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200263 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
267 * Sample a process (thread group) clock for the given group_leader task.
268 * Must be called with tasklist_lock held for reading.
269 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800270static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct task_struct *p,
272 union cpu_time_count *cpu)
273{
Frank Mayharbb34d922008-09-12 09:54:39 -0700274 struct task_cputime cputime;
275
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100276 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700277 default:
278 return -EINVAL;
279 case CPUCLOCK_PROF:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900280 thread_group_cputime(p, &cputime);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100281 cpu->cpu = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700282 break;
283 case CPUCLOCK_VIRT:
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +0900284 thread_group_cputime(p, &cputime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700285 cpu->cpu = cputime.utime;
286 break;
287 case CPUCLOCK_SCHED:
Peter Zijlstrad670ec12011-09-01 12:42:04 +0200288 thread_group_cputime(p, &cputime);
289 cpu->sched = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700290 break;
291 }
292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000296static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 const pid_t pid = CPUCLOCK_PID(which_clock);
299 int error = -EINVAL;
300 union cpu_time_count rtn;
301
302 if (pid == 0) {
303 /*
304 * Special case constant value for our own clocks.
305 * We don't have to do any lookup to find ourselves.
306 */
307 if (CPUCLOCK_PERTHREAD(which_clock)) {
308 /*
309 * Sampling just ourselves we can do with no locking.
310 */
311 error = cpu_clock_sample(which_clock,
312 current, &rtn);
313 } else {
314 read_lock(&tasklist_lock);
315 error = cpu_clock_sample_group(which_clock,
316 current, &rtn);
317 read_unlock(&tasklist_lock);
318 }
319 } else {
320 /*
321 * Find the given PID, and validate that the caller
322 * should be able to see it.
323 */
324 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800325 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800326 p = find_task_by_vpid(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (p) {
328 if (CPUCLOCK_PERTHREAD(which_clock)) {
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -0700329 if (same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 error = cpu_clock_sample(which_clock,
331 p, &rtn);
332 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800333 } else {
334 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700335 if (thread_group_leader(p) && p->sighand) {
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800336 error =
337 cpu_clock_sample_group(which_clock,
338 p, &rtn);
339 }
340 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 }
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800343 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 if (error)
347 return error;
348 sample_to_timespec(which_clock, rtn, tp);
349 return 0;
350}
351
352
353/*
354 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800355 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
356 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000358static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 int ret = 0;
361 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
362 struct task_struct *p;
363
364 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
365 return -EINVAL;
366
367 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200369 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
371 if (pid == 0) {
372 p = current;
373 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800374 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -0700375 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 p = NULL;
377 }
378 } else {
379 if (pid == 0) {
380 p = current->group_leader;
381 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800382 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200383 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 p = NULL;
385 }
386 }
387 new_timer->it.cpu.task = p;
388 if (p) {
389 get_task_struct(p);
390 } else {
391 ret = -EINVAL;
392 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200393 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return ret;
396}
397
398/*
399 * Clean up a CPU-clock timer that is about to be destroyed.
400 * This is called from timer deletion with the timer already locked.
401 * If we return TIMER_RETRY, it's necessary to release the timer's lock
402 * and try again. (This happens when the timer is in the middle of firing.)
403 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000404static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct task_struct *p = timer->it.cpu.task;
Oleg Nesterov108150e2005-10-23 20:25:39 +0400407 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Oleg Nesterov108150e2005-10-23 20:25:39 +0400409 if (likely(p != NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700410 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700411 if (unlikely(p->sighand == NULL)) {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700412 /*
413 * We raced with the reaping of the task.
414 * The deletion should have cleared us off the list.
415 */
416 BUG_ON(!list_empty(&timer->it.cpu.entry));
417 } else {
Linus Torvalds9465bee2005-10-21 15:36:00 -0700418 spin_lock(&p->sighand->siglock);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400419 if (timer->it.cpu.firing)
420 ret = TIMER_RETRY;
421 else
422 list_del(&timer->it.cpu.entry);
Linus Torvalds9465bee2005-10-21 15:36:00 -0700423 spin_unlock(&p->sighand->siglock);
424 }
425 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Oleg Nesterov108150e2005-10-23 20:25:39 +0400427 if (!ret)
428 put_task_struct(p);
429 }
430
431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * Clean out CPU timers still ticking when a thread exited. The task
436 * pointer is cleared, and the expiry time is replaced with the residual
437 * time for later timer_gettime calls to return.
438 * This must be called with the siglock held.
439 */
440static void cleanup_timers(struct list_head *head,
441 cputime_t utime, cputime_t stime,
Ingo Molnar41b86e92007-07-09 18:51:58 +0200442 unsigned long long sum_exec_runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct cpu_timer_list *timer, *next;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100445 cputime_t ptime = utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100449 if (timer->expires.cpu < ptime) {
450 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100452 timer->expires.cpu -= ptime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455
456 ++head;
457 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 list_del_init(&timer->entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100459 if (timer->expires.cpu < utime) {
460 timer->expires.cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +0100462 timer->expires.cpu -= utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 }
465
466 ++head;
467 list_for_each_entry_safe(timer, next, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 list_del_init(&timer->entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200469 if (timer->expires.sched < sum_exec_runtime) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 timer->expires.sched = 0;
471 } else {
Ingo Molnar41b86e92007-07-09 18:51:58 +0200472 timer->expires.sched -= sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 }
475}
476
477/*
478 * These are both called with the siglock held, when the current thread
479 * is being reaped. When the final (leader) thread in the group is reaped,
480 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
481 */
482void posix_cpu_timers_exit(struct task_struct *tsk)
483{
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100484 cputime_t utime, stime;
485
Nick Kossifidis61337052012-12-16 22:18:11 -0500486 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
487 sizeof(unsigned long long));
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100488 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 cleanup_timers(tsk->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100490 utime, stime, tsk->se.sum_exec_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492}
493void posix_cpu_timers_exit_group(struct task_struct *tsk)
494{
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700495 struct signal_struct *const sig = tsk->signal;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100496 cputime_t utime, stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700497
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100498 task_cputime(tsk, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 cleanup_timers(tsk->signal->cpu_timers,
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100500 utime + sig->utime, stime + sig->stime,
Stanislaw Gruszka17d42c12009-08-06 16:03:30 -0700501 tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
505{
506 /*
507 * That's all for this thread or process.
508 * We leave our residual in expires to be reported.
509 */
510 put_task_struct(timer->it.cpu.task);
511 timer->it.cpu.task = NULL;
512 timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
513 timer->it.cpu.expires,
514 now);
515}
516
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200517static inline int expires_gt(cputime_t expires, cputime_t new_exp)
518{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100519 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * Insert the timer on the appropriate list before any timers that
524 * expire later. This must be called with the tasklist_lock held
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800525 * for reading, interrupts disabled and p->sighand->siglock taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800527static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct task_struct *p = timer->it.cpu.task;
530 struct list_head *head, *listpos;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800531 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct cpu_timer_list *const nt = &timer->it.cpu;
533 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800535 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
536 head = p->cpu_timers;
537 cputime_expires = &p->cputime_expires;
538 } else {
539 head = p->signal->cpu_timers;
540 cputime_expires = &p->signal->cputime_expires;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 head += CPUCLOCK_WHICH(timer->it_clock);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800545 list_for_each_entry(next, head, entry) {
546 if (cpu_time_before(timer->it_clock, nt->expires, next->expires))
547 break;
548 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 list_add(&nt->entry, listpos);
551
552 if (listpos == head) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800553 union cpu_time_count *exp = &nt->expires;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800556 * We are the new earliest-expiring POSIX 1.b timer, hence
557 * need to update expiration cache. Take into account that
558 * for process timers we share expiration cache with itimers
559 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800562 switch (CPUCLOCK_WHICH(timer->it_clock)) {
563 case CPUCLOCK_PROF:
564 if (expires_gt(cputime_expires->prof_exp, exp->cpu))
565 cputime_expires->prof_exp = exp->cpu;
566 break;
567 case CPUCLOCK_VIRT:
568 if (expires_gt(cputime_expires->virt_exp, exp->cpu))
569 cputime_expires->virt_exp = exp->cpu;
570 break;
571 case CPUCLOCK_SCHED:
572 if (cputime_expires->sched_exp == 0 ||
573 cputime_expires->sched_exp > exp->sched)
574 cputime_expires->sched_exp = exp->sched;
575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
581 * The timer is locked, fire it and arrange for its reload.
582 */
583static void cpu_timer_fire(struct k_itimer *timer)
584{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800585 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
586 /*
587 * User don't want any signal.
588 */
589 timer->it.cpu.expires.sched = 0;
590 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * This a special case for clock_nanosleep,
593 * not a normal timer from sys_timer_create.
594 */
595 wake_up_process(timer->it_process);
596 timer->it.cpu.expires.sched = 0;
597 } else if (timer->it.cpu.incr.sched == 0) {
598 /*
599 * One-shot timer. Clear it as soon as it's fired.
600 */
601 posix_timer_event(timer, 0);
602 timer->it.cpu.expires.sched = 0;
603 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
604 /*
605 * The signal did not get queued because the signal
606 * was ignored, so we won't get any callback to
607 * reload the timer. But we need to keep it
608 * ticking in case the signal is deliverable next time.
609 */
610 posix_cpu_timer_schedule(timer);
611 }
612}
613
614/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100615 * Sample a process (thread group) timer for the given group_leader task.
616 * Must be called with tasklist_lock held for reading.
617 */
618static int cpu_timer_sample_group(const clockid_t which_clock,
619 struct task_struct *p,
620 union cpu_time_count *cpu)
621{
622 struct task_cputime cputime;
623
624 thread_group_cputimer(p, &cputime);
625 switch (CPUCLOCK_WHICH(which_clock)) {
626 default:
627 return -EINVAL;
628 case CPUCLOCK_PROF:
Martin Schwidefsky64861632011-12-15 14:56:09 +0100629 cpu->cpu = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100630 break;
631 case CPUCLOCK_VIRT:
632 cpu->cpu = cputime.utime;
633 break;
634 case CPUCLOCK_SCHED:
635 cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p);
636 break;
637 }
638 return 0;
639}
640
Frederic Weisbeckera8572162013-04-18 01:31:13 +0200641#ifdef CONFIG_NO_HZ_FULL
642static void nohz_kick_work_fn(struct work_struct *work)
643{
644 tick_nohz_full_kick_all();
645}
646
647static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn);
648
649/*
650 * We need the IPIs to be sent from sane process context.
651 * The posix cpu timers are always set with irqs disabled.
652 */
653static void posix_cpu_timer_kick_nohz(void)
654{
655 schedule_work(&nohz_kick_work);
656}
657#else
658static inline void posix_cpu_timer_kick_nohz(void) { }
659#endif
660
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100661/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * Guts of sys_timer_settime for CPU timers.
663 * This is called with the timer locked and interrupts disabled.
664 * If we return TIMER_RETRY, it's necessary to release the timer's lock
665 * and try again. (This happens when the timer is in the middle of firing.)
666 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000667static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
668 struct itimerspec *new, struct itimerspec *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct task_struct *p = timer->it.cpu.task;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800671 union cpu_time_count old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int ret;
673
674 if (unlikely(p == NULL)) {
675 /*
676 * Timer refers to a dead task's clock.
677 */
678 return -ESRCH;
679 }
680
681 new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
682
683 read_lock(&tasklist_lock);
684 /*
685 * We need the tasklist_lock to protect against reaping that
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700686 * clears p->sighand. If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * longer get any information about it at all.
688 */
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700689 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 read_unlock(&tasklist_lock);
691 put_task_struct(p);
692 timer->it.cpu.task = NULL;
693 return -ESRCH;
694 }
695
696 /*
697 * Disarm any old timer after extracting its expiry time.
698 */
699 BUG_ON(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400700
701 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800702 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 spin_lock(&p->sighand->siglock);
704 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400705 if (unlikely(timer->it.cpu.firing)) {
706 timer->it.cpu.firing = -1;
707 ret = TIMER_RETRY;
708 } else
709 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /*
712 * We need to sample the current value to convert the new
713 * value from to relative and absolute, and to convert the
714 * old value from absolute to relative. To set a process
715 * timer, we need a sample to balance the thread expiry
716 * times (in arm_timer). With an absolute time, we must
717 * check if it's already passed. In short, we need a sample.
718 */
719 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
720 cpu_clock_sample(timer->it_clock, p, &val);
721 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100722 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 if (old) {
726 if (old_expires.sched == 0) {
727 old->it_value.tv_sec = 0;
728 old->it_value.tv_nsec = 0;
729 } else {
730 /*
731 * Update the timer in case it has
732 * overrun already. If it has,
733 * we'll report it as having overrun
734 * and with the next reloaded timer
735 * already ticking, though we are
736 * swallowing that pending
737 * notification here to install the
738 * new setting.
739 */
740 bump_cpu_timer(timer, val);
741 if (cpu_time_before(timer->it_clock, val,
742 timer->it.cpu.expires)) {
743 old_expires = cpu_time_sub(
744 timer->it_clock,
745 timer->it.cpu.expires, val);
746 sample_to_timespec(timer->it_clock,
747 old_expires,
748 &old->it_value);
749 } else {
750 old->it_value.tv_nsec = 1;
751 old->it_value.tv_sec = 0;
752 }
753 }
754 }
755
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400756 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * We are colliding with the timer actually firing.
759 * Punt after filling in the timer's old value, and
760 * disable this firing since we are already reporting
761 * it as an overrun (thanks to bump_cpu_timer above).
762 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800763 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 }
767
768 if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
769 cpu_time_add(timer->it_clock, &new_expires, val);
770 }
771
772 /*
773 * Install the new expiry time (or zero).
774 * For a timer with no notification action, we don't actually
775 * arm the timer (we'll just fake it for timer_gettime).
776 */
777 timer->it.cpu.expires = new_expires;
778 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 cpu_time_before(timer->it_clock, val, new_expires)) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800780 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Stanislaw Gruszkac2873932010-03-11 14:04:42 -0800783 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 read_unlock(&tasklist_lock);
785
786 /*
787 * Install the new reload setting, and
788 * set up the signal and overrun bookkeeping.
789 */
790 timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
791 &new->it_interval);
792
793 /*
794 * This acts as a modification timestamp for the timer,
795 * so any automatic reload attempt will punt on seeing
796 * that we have reset the timer manually.
797 */
798 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
799 ~REQUEUE_PENDING;
800 timer->it_overrun_last = 0;
801 timer->it_overrun = -1;
802
803 if (new_expires.sched != 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 !cpu_time_before(timer->it_clock, val, new_expires)) {
805 /*
806 * The designated time already passed, so we notify
807 * immediately, even if the thread never runs to
808 * accumulate more time on this clock.
809 */
810 cpu_timer_fire(timer);
811 }
812
813 ret = 0;
814 out:
815 if (old) {
816 sample_to_timespec(timer->it_clock,
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800817 old_incr, &old->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +0200819 if (!ret)
820 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return ret;
822}
823
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000824static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 union cpu_time_count now;
827 struct task_struct *p = timer->it.cpu.task;
828 int clear_dead;
829
830 /*
831 * Easy part: convert the reload time.
832 */
833 sample_to_timespec(timer->it_clock,
834 timer->it.cpu.incr, &itp->it_interval);
835
836 if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */
837 itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
838 return;
839 }
840
841 if (unlikely(p == NULL)) {
842 /*
843 * This task already died and the timer will never fire.
844 * In this case, expires is actually the dead value.
845 */
846 dead:
847 sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
848 &itp->it_value);
849 return;
850 }
851
852 /*
853 * Sample the clock to take the difference with the expiry time.
854 */
855 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
856 cpu_clock_sample(timer->it_clock, p, &now);
857 clear_dead = p->exit_state;
858 } else {
859 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -0700860 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * The process has been reaped.
863 * We can't even collect a sample any more.
864 * Call the timer disarmed, nothing else to do.
865 */
866 put_task_struct(p);
867 timer->it.cpu.task = NULL;
868 timer->it.cpu.expires.sched = 0;
869 read_unlock(&tasklist_lock);
870 goto dead;
871 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100872 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 clear_dead = (unlikely(p->exit_state) &&
874 thread_group_empty(p));
875 }
876 read_unlock(&tasklist_lock);
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (unlikely(clear_dead)) {
880 /*
881 * We've noticed that the thread is dead, but
882 * not yet reaped. Take this opportunity to
883 * drop our task ref.
884 */
885 clear_dead_task(timer, now);
886 goto dead;
887 }
888
889 if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
890 sample_to_timespec(timer->it_clock,
891 cpu_time_sub(timer->it_clock,
892 timer->it.cpu.expires, now),
893 &itp->it_value);
894 } else {
895 /*
896 * The timer should have expired already, but the firing
897 * hasn't taken place yet. Say it's just about to expire.
898 */
899 itp->it_value.tv_nsec = 1;
900 itp->it_value.tv_sec = 0;
901 }
902}
903
904/*
905 * Check for any per-thread CPU timers that have fired and move them off
906 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
907 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
908 */
909static void check_thread_timers(struct task_struct *tsk,
910 struct list_head *firing)
911{
Linus Torvaldse80eda92005-10-23 10:02:50 -0700912 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100914 struct signal_struct *const sig = tsk->signal;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800915 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvaldse80eda92005-10-23 10:02:50 -0700917 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100918 tsk->cputime_expires.prof_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700920 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct cpu_timer_list,
922 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100923 if (!--maxfire || prof_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700924 tsk->cputime_expires.prof_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 break;
926 }
927 t->firing = 1;
928 list_move_tail(&t->entry, firing);
929 }
930
931 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700932 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +0100933 tsk->cputime_expires.virt_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700935 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct cpu_timer_list,
937 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +0100938 if (!--maxfire || virt_ticks(tsk) < t->expires.cpu) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700939 tsk->cputime_expires.virt_exp = t->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941 }
942 t->firing = 1;
943 list_move_tail(&t->entry, firing);
944 }
945
946 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -0700947 maxfire = 20;
Frank Mayharf06febc2008-09-12 09:54:39 -0700948 tsk->cputime_expires.sched_exp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 while (!list_empty(timers)) {
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700950 struct cpu_timer_list *t = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct cpu_timer_list,
952 entry);
Ingo Molnar41b86e92007-07-09 18:51:58 +0200953 if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
Frank Mayharf06febc2008-09-12 09:54:39 -0700954 tsk->cputime_expires.sched_exp = t->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 }
957 t->firing = 1;
958 list_move_tail(&t->entry, firing);
959 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100960
961 /*
962 * Check for the special case thread timers.
963 */
Jiri Slaby78d7d402010-03-05 13:42:54 -0800964 soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800965 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800966 unsigned long hard =
967 ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100968
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100969 if (hard != RLIM_INFINITY &&
970 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100971 /*
972 * At the hard limit, we just die.
973 * No need to calculate anything else now.
974 */
975 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
976 return;
977 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800978 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100979 /*
980 * At the soft limit, send a SIGXCPU every second.
981 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800982 if (soft < hard) {
983 soft += USEC_PER_SEC;
984 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100985 }
Hiroshi Shimamoto81d50bb2008-05-15 19:42:49 -0700986 printk(KERN_INFO
987 "RT Watchdog Timeout: %s[%d]\n",
988 tsk->comm, task_pid_nr(tsk));
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100989 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
990 }
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800994static void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100995{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800996 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100997 unsigned long flags;
998
Thomas Gleixneree30a7b2009-07-25 18:56:56 +0200999 raw_spin_lock_irqsave(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001000 cputimer->running = 0;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001001 raw_spin_unlock_irqrestore(&cputimer->lock, flags);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +01001002}
1003
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001004static u32 onecputick;
1005
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001006static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
1007 cputime_t *expires, cputime_t cur_time, int signo)
1008{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001009 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001010 return;
1011
Martin Schwidefsky64861632011-12-15 14:56:09 +01001012 if (cur_time >= it->expires) {
1013 if (it->incr) {
1014 it->expires += it->incr;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001015 it->error += it->incr_error;
1016 if (it->error >= onecputick) {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001017 it->expires -= cputime_one_jiffy;
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001018 it->error -= onecputick;
1019 }
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001020 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001021 it->expires = 0;
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001022 }
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001023
Xiao Guangrong3f0a5252009-08-10 10:52:30 +08001024 trace_itimer_expire(signo == SIGPROF ?
1025 ITIMER_PROF : ITIMER_VIRTUAL,
1026 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001027 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
1028 }
1029
Martin Schwidefsky64861632011-12-15 14:56:09 +01001030 if (it->expires && (!*expires || it->expires < *expires)) {
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001031 *expires = it->expires;
1032 }
1033}
1034
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001035/**
1036 * task_cputime_zero - Check a task_cputime struct for all zero fields.
1037 *
1038 * @cputime: The struct to compare.
1039 *
1040 * Checks @cputime to see if all fields are zero. Returns true if all fields
1041 * are zero, false if any field is nonzero.
1042 */
1043static inline int task_cputime_zero(const struct task_cputime *cputime)
1044{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001045 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001046 return 1;
1047 return 0;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
1051 * Check for any per-thread CPU timers that have fired and move them
1052 * off the tsk->*_timers list onto the firing list. Per-thread timers
1053 * have already been taken off.
1054 */
1055static void check_process_timers(struct task_struct *tsk,
1056 struct list_head *firing)
1057{
Linus Torvaldse80eda92005-10-23 10:02:50 -07001058 int maxfire;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct signal_struct *const sig = tsk->signal;
Frank Mayharf06febc2008-09-12 09:54:39 -07001060 cputime_t utime, ptime, virt_expires, prof_expires;
Ingo Molnar41b86e92007-07-09 18:51:58 +02001061 unsigned long long sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct list_head *timers = sig->cpu_timers;
Frank Mayharf06febc2008-09-12 09:54:39 -07001063 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001064 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * Collect the current process totals.
1068 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001069 thread_group_cputimer(tsk, &cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001070 utime = cputime.utime;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001071 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -07001072 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001073 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001074 prof_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001076 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct cpu_timer_list,
1078 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001079 if (!--maxfire || ptime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001080 prof_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 break;
1082 }
WANG Congee7dd202008-04-04 20:54:10 +02001083 tl->firing = 1;
1084 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
1087 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001088 maxfire = 20;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001089 virt_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001091 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct cpu_timer_list,
1093 entry);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001094 if (!--maxfire || utime < tl->expires.cpu) {
WANG Congee7dd202008-04-04 20:54:10 +02001095 virt_expires = tl->expires.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 }
WANG Congee7dd202008-04-04 20:54:10 +02001098 tl->firing = 1;
1099 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 ++timers;
Linus Torvaldse80eda92005-10-23 10:02:50 -07001103 maxfire = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 sched_expires = 0;
1105 while (!list_empty(timers)) {
WANG Congee7dd202008-04-04 20:54:10 +02001106 struct cpu_timer_list *tl = list_first_entry(timers,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct cpu_timer_list,
1108 entry);
WANG Congee7dd202008-04-04 20:54:10 +02001109 if (!--maxfire || sum_sched_runtime < tl->expires.sched) {
1110 sched_expires = tl->expires.sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 }
WANG Congee7dd202008-04-04 20:54:10 +02001113 tl->firing = 1;
1114 list_move_tail(&tl->entry, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
1117 /*
1118 * Check for the special case process timers.
1119 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +02001120 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
1121 SIGPROF);
1122 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
1123 SIGVTALRM);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001124 soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001125 if (soft != RLIM_INFINITY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned long psecs = cputime_to_secs(ptime);
Jiri Slaby78d7d402010-03-05 13:42:54 -08001127 unsigned long hard =
1128 ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 cputime_t x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001130 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * At the hard limit, we just die.
1133 * No need to calculate anything else now.
1134 */
1135 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1136 return;
1137 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001138 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * At the soft limit, send a SIGXCPU every second.
1141 */
1142 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001143 if (soft < hard) {
1144 soft++;
1145 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -08001148 x = secs_to_cputime(soft);
Martin Schwidefsky64861632011-12-15 14:56:09 +01001149 if (!prof_expires || x < prof_expires) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 prof_expires = x;
1151 }
1152 }
1153
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001154 sig->cputime_expires.prof_exp = prof_expires;
1155 sig->cputime_expires.virt_exp = virt_expires;
1156 sig->cputime_expires.sched_exp = sched_expires;
1157 if (task_cputime_zero(&sig->cputime_expires))
1158 stop_process_timers(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161/*
1162 * This is called from the signal code (via do_schedule_next_timer)
1163 * when the last timer signal was delivered and we have to reload the timer.
1164 */
1165void posix_cpu_timer_schedule(struct k_itimer *timer)
1166{
1167 struct task_struct *p = timer->it.cpu.task;
1168 union cpu_time_count now;
1169
1170 if (unlikely(p == NULL))
1171 /*
1172 * The task was cleaned up already, no future firings.
1173 */
Roland McGrath708f430d2005-10-30 15:03:13 -08001174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
1177 * Fetch the current sample and update the timer's expiry time.
1178 */
1179 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1180 cpu_clock_sample(timer->it_clock, p, &now);
1181 bump_cpu_timer(timer, now);
1182 if (unlikely(p->exit_state)) {
1183 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 read_lock(&tasklist_lock); /* arm_timer needs it. */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001187 spin_lock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else {
1189 read_lock(&tasklist_lock);
Oleg Nesterovd30fda32010-05-26 14:43:13 -07001190 if (unlikely(p->sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * The process has been reaped.
1193 * We can't even collect a sample any more.
1194 */
1195 put_task_struct(p);
1196 timer->it.cpu.task = p = NULL;
1197 timer->it.cpu.expires.sched = 0;
Roland McGrath708f430d2005-10-30 15:03:13 -08001198 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1200 /*
1201 * We've noticed that the thread is dead, but
1202 * not yet reaped. Take this opportunity to
1203 * drop our task ref.
1204 */
1205 clear_dead_task(timer, now);
Roland McGrath708f430d2005-10-30 15:03:13 -08001206 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001208 spin_lock(&p->sighand->siglock);
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001209 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 bump_cpu_timer(timer, now);
1211 /* Leave the tasklist_lock locked for the call below. */
1212 }
1213
1214 /*
1215 * Now re-arm for the new expiry time.
1216 */
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001217 BUG_ON(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001218 arm_timer(timer);
Stanislaw Gruszkac2873932010-03-11 14:04:42 -08001219 spin_unlock(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Roland McGrath708f430d2005-10-30 15:03:13 -08001221out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 read_unlock(&tasklist_lock);
Roland McGrath708f430d2005-10-30 15:03:13 -08001223
1224out:
1225 timer->it_overrun_last = timer->it_overrun;
1226 timer->it_overrun = -1;
1227 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Frank Mayharf06febc2008-09-12 09:54:39 -07001230/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001231 * task_cputime_expired - Compare two task_cputime entities.
1232 *
1233 * @sample: The task_cputime structure to be checked for expiration.
1234 * @expires: Expiration times, against which @sample will be checked.
1235 *
1236 * Checks @sample against @expires to see if any field of @sample has expired.
1237 * Returns true if any field of the former is greater than the corresponding
1238 * field of the latter if the latter field is set. Otherwise returns false.
1239 */
1240static inline int task_cputime_expired(const struct task_cputime *sample,
1241 const struct task_cputime *expires)
1242{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001243 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001244 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001245 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001246 return 1;
1247 if (expires->sum_exec_runtime != 0 &&
1248 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1249 return 1;
1250 return 0;
1251}
1252
1253/**
1254 * fastpath_timer_check - POSIX CPU timers fast path.
1255 *
1256 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001257 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001258 * Check the task and thread group timers. If both are zero (there are no
1259 * timers set) return false. Otherwise snapshot the task and thread group
1260 * timers and compare them with the corresponding expiration times. Return
1261 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001262 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001263static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001264{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001265 struct signal_struct *sig;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001266 cputime_t utime, stime;
1267
1268 task_cputime(tsk, &utime, &stime);
Frank Mayharf06febc2008-09-12 09:54:39 -07001269
Frank Mayharbb34d922008-09-12 09:54:39 -07001270 if (!task_cputime_zero(&tsk->cputime_expires)) {
1271 struct task_cputime task_sample = {
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001272 .utime = utime,
1273 .stime = stime,
Frank Mayharbb34d922008-09-12 09:54:39 -07001274 .sum_exec_runtime = tsk->se.sum_exec_runtime
1275 };
1276
1277 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1278 return 1;
1279 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001280
1281 sig = tsk->signal;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001282 if (sig->cputimer.running) {
Frank Mayharbb34d922008-09-12 09:54:39 -07001283 struct task_cputime group_sample;
1284
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001285 raw_spin_lock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001286 group_sample = sig->cputimer.cputime;
Thomas Gleixneree30a7b2009-07-25 18:56:56 +02001287 raw_spin_unlock(&sig->cputimer.lock);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001288
Frank Mayharbb34d922008-09-12 09:54:39 -07001289 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1290 return 1;
1291 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001292
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001293 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/*
1297 * This is called from the timer interrupt handler. The irq handler has
1298 * already updated our counts. We need to check if any timers fire now.
1299 * Interrupts are disabled.
1300 */
1301void run_posix_cpu_timers(struct task_struct *tsk)
1302{
1303 LIST_HEAD(firing);
1304 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001305 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 BUG_ON(!irqs_disabled());
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001310 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001311 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001313 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001314 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001315
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001316 if (!lock_task_sighand(tsk, &flags))
1317 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001318 /*
1319 * Here we take off tsk->signal->cpu_timers[N] and
1320 * tsk->cpu_timers[N] all the timers that are firing, and
1321 * put them on the firing list.
1322 */
1323 check_thread_timers(tsk, &firing);
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -07001324 /*
1325 * If there are any active process wide timers (POSIX 1.b, itimers,
1326 * RLIMIT_CPU) cputimer must be running.
1327 */
1328 if (tsk->signal->cputimer.running)
1329 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Frank Mayharbb34d922008-09-12 09:54:39 -07001331 /*
1332 * We must release these locks before taking any timer's lock.
1333 * There is a potential race with timer deletion here, as the
1334 * siglock now protects our private firing list. We have set
1335 * the firing flag in each timer, so that a deletion attempt
1336 * that gets the timer lock before we do will give it up and
1337 * spin until we've taken care of that timer below.
1338 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001339 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /*
1342 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001343 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * each timer's lock before clearing its firing flag, so no
1345 * timer call will interfere.
1346 */
1347 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001348 int cpu_firing;
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 spin_lock(&timer->it_lock);
1351 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001352 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 timer->it.cpu.firing = 0;
1354 /*
1355 * The firing flag is -1 if we collided with a reset
1356 * of the timer, which already reported this
1357 * almost-firing as an overrun. So don't generate an event.
1358 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001359 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_unlock(&timer->it_lock);
1362 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001363
1364 /*
1365 * In case some timers were rescheduled after the queue got emptied,
1366 * wake up full dynticks CPUs.
1367 */
1368 if (tsk->signal->cputimer.running)
1369 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001373 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001374 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
1376void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1377 cputime_t *newval, cputime_t *oldval)
1378{
1379 union cpu_time_count now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 BUG_ON(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001382 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001385 /*
1386 * We are setting itimer. The *oldval is absolute and we update
1387 * it to be relative, *newval argument is relative and we update
1388 * it to be absolute.
1389 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001390 if (*oldval) {
1391 if (*oldval <= now.cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Just about to fire. */
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001393 *oldval = cputime_one_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 } else {
Martin Schwidefsky64861632011-12-15 14:56:09 +01001395 *oldval -= now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 }
1398
Martin Schwidefsky64861632011-12-15 14:56:09 +01001399 if (!*newval)
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001400 goto out;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001401 *newval += now.cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001405 * Update expiration cache if we are the earliest timer, or eventually
1406 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001408 switch (clock_idx) {
1409 case CPUCLOCK_PROF:
1410 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001411 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001412 break;
1413 case CPUCLOCK_VIRT:
1414 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
Frank Mayharf06febc2008-09-12 09:54:39 -07001415 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Frederic Weisbeckera8572162013-04-18 01:31:13 +02001418out:
1419 posix_cpu_timer_kick_nohz();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Toyo Abee4b76552006-09-29 02:00:29 -07001422static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1423 struct timespec *rqtp, struct itimerspec *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct k_itimer timer;
1426 int error;
1427
1428 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * Set up a temporary timer and then wait for it to go off.
1430 */
1431 memset(&timer, 0, sizeof timer);
1432 spin_lock_init(&timer.it_lock);
1433 timer.it_clock = which_clock;
1434 timer.it_overrun = -1;
1435 error = posix_cpu_timer_create(&timer);
1436 timer.it_process = current;
1437 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 static struct itimerspec zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001439
1440 memset(it, 0, sizeof *it);
1441 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 spin_lock_irq(&timer.it_lock);
Toyo Abee4b76552006-09-29 02:00:29 -07001444 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (error) {
1446 spin_unlock_irq(&timer.it_lock);
1447 return error;
1448 }
1449
1450 while (!signal_pending(current)) {
1451 if (timer.it.cpu.expires.sched == 0) {
1452 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001453 * Our timer fired and was reset, below
1454 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001456 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 spin_unlock_irq(&timer.it_lock);
1458 return 0;
1459 }
1460
1461 /*
1462 * Block until cpu_timer_fire (or a signal) wakes us.
1463 */
1464 __set_current_state(TASK_INTERRUPTIBLE);
1465 spin_unlock_irq(&timer.it_lock);
1466 schedule();
1467 spin_lock_irq(&timer.it_lock);
1468 }
1469
1470 /*
1471 * We were interrupted by a signal.
1472 */
1473 sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001474 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
1475 if (!error) {
1476 /*
1477 * Timer is now unarmed, deletion can not fail.
1478 */
1479 posix_cpu_timer_del(&timer);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 spin_unlock_irq(&timer.it_lock);
1482
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001483 while (error == TIMER_RETRY) {
1484 /*
1485 * We need to handle case when timer was or is in the
1486 * middle of firing. In other cases we already freed
1487 * resources.
1488 */
1489 spin_lock_irq(&timer.it_lock);
1490 error = posix_cpu_timer_del(&timer);
1491 spin_unlock_irq(&timer.it_lock);
1492 }
1493
Toyo Abee4b76552006-09-29 02:00:29 -07001494 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /*
1496 * It actually did fire already.
1497 */
1498 return 0;
1499 }
1500
Toyo Abee4b76552006-09-29 02:00:29 -07001501 error = -ERESTART_RESTARTBLOCK;
1502 }
1503
1504 return error;
1505}
1506
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001507static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1508
1509static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1510 struct timespec *rqtp, struct timespec __user *rmtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001511{
1512 struct restart_block *restart_block =
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001513 &current_thread_info()->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001514 struct itimerspec it;
1515 int error;
1516
1517 /*
1518 * Diagnose required errors first.
1519 */
1520 if (CPUCLOCK_PERTHREAD(which_clock) &&
1521 (CPUCLOCK_PID(which_clock) == 0 ||
1522 CPUCLOCK_PID(which_clock) == current->pid))
1523 return -EINVAL;
1524
1525 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1526
1527 if (error == -ERESTART_RESTARTBLOCK) {
1528
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001529 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001530 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001532 * Report back to the user the time still remaining.
1533 */
1534 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EFAULT;
1536
Toyo Abe1711ef32006-09-29 02:00:28 -07001537 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001538 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001539 restart_block->nanosleep.rmtp = rmtp;
1540 restart_block->nanosleep.expires = timespec_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return error;
1543}
1544
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001545static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001547 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001548 struct timespec t;
Toyo Abee4b76552006-09-29 02:00:29 -07001549 struct itimerspec it;
1550 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001551
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001552 t = ns_to_timespec(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001553
Toyo Abee4b76552006-09-29 02:00:29 -07001554 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1555
1556 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001557 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001558 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001559 * Report back to the user the time still remaining.
1560 */
1561 if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
Toyo Abee4b76552006-09-29 02:00:29 -07001562 return -EFAULT;
1563
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001564 restart_block->nanosleep.expires = timespec_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001565 }
1566 return error;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1571#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1572
Thomas Gleixnera924b042006-01-09 20:52:27 -08001573static int process_cpu_clock_getres(const clockid_t which_clock,
1574 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1577}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001578static int process_cpu_clock_get(const clockid_t which_clock,
1579 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1582}
1583static int process_cpu_timer_create(struct k_itimer *timer)
1584{
1585 timer->it_clock = PROCESS_CLOCK;
1586 return posix_cpu_timer_create(timer);
1587}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001588static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001589 struct timespec *rqtp,
1590 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001592 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
Toyo Abe1711ef32006-09-29 02:00:28 -07001594static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1595{
1596 return -EINVAL;
1597}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001598static int thread_cpu_clock_getres(const clockid_t which_clock,
1599 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1602}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001603static int thread_cpu_clock_get(const clockid_t which_clock,
1604 struct timespec *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1607}
1608static int thread_cpu_timer_create(struct k_itimer *timer)
1609{
1610 timer->it_clock = THREAD_CLOCK;
1611 return posix_cpu_timer_create(timer);
1612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Thomas Gleixner19769452011-02-01 13:51:06 +00001614struct k_clock clock_posix_cpu = {
1615 .clock_getres = posix_cpu_clock_getres,
1616 .clock_set = posix_cpu_clock_set,
1617 .clock_get = posix_cpu_clock_get,
1618 .timer_create = posix_cpu_timer_create,
1619 .nsleep = posix_cpu_nsleep,
1620 .nsleep_restart = posix_cpu_nsleep_restart,
1621 .timer_set = posix_cpu_timer_set,
1622 .timer_del = posix_cpu_timer_del,
1623 .timer_get = posix_cpu_timer_get,
1624};
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static __init int init_posix_cpu_timers(void)
1627{
1628 struct k_clock process = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001629 .clock_getres = process_cpu_clock_getres,
1630 .clock_get = process_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001631 .timer_create = process_cpu_timer_create,
1632 .nsleep = process_cpu_nsleep,
1633 .nsleep_restart = process_cpu_nsleep_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 };
1635 struct k_clock thread = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001636 .clock_getres = thread_cpu_clock_getres,
1637 .clock_get = thread_cpu_clock_get,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +00001638 .timer_create = thread_cpu_timer_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 };
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001640 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Thomas Gleixner52708732011-02-02 12:10:09 +01001642 posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1643 posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02001645 cputime_to_timespec(cputime_one_jiffy, &ts);
Stanislaw Gruszka8356b5f2009-07-29 12:15:27 +02001646 onecputick = ts.tv_nsec;
1647 WARN_ON(ts.tv_sec != 0);
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650}
1651__initcall(init_posix_cpu_timers);