blob: cb4a4eb44279be6837e21f41d19ccb52040c80b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implement CPU time clocks for the POSIX clock interface.
3 */
4
Ingo Molnar3f07c012017-02-08 18:51:30 +01005#include <linux/sched/signal.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +01006#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/posix-timers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -07009#include <linux/math64.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080010#include <linux/uaccess.h>
Frank Mayharbb34d922008-09-12 09:54:39 -070011#include <linux/kernel_stat.h>
Xiao Guangrong3f0a5252009-08-10 10:52:30 +080012#include <trace/events/timer.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
Thomas Gleixnerbab0aae92017-05-30 23:15:41 +020016#include "posix-timers.h"
17
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +020018static void posix_cpu_timer_rearm(struct k_itimer *timer);
19
Frank Mayharf06febc2008-09-12 09:54:39 -070020/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080021 * Called after updating RLIMIT_CPU to run cpu timer and update
22 * tsk->signal->cputime_expires expiration cache if necessary. Needs
23 * siglock protection since other code may update expiration cache as
24 * well.
Frank Mayharf06febc2008-09-12 09:54:39 -070025 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020026void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070027{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010028 u64 nsecs = rlim_new * NSEC_PER_SEC;
Frank Mayharf06febc2008-09-12 09:54:39 -070029
Jiri Slaby5ab46b32009-08-28 14:05:12 +020030 spin_lock_irq(&task->sighand->siglock);
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010031 set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
Jiri Slaby5ab46b32009-08-28 14:05:12 +020032 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070033}
34
Thomas Gleixnera924b042006-01-09 20:52:27 -080035static int check_clock(const clockid_t which_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int error = 0;
38 struct task_struct *p;
39 const pid_t pid = CPUCLOCK_PID(which_clock);
40
41 if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
42 return -EINVAL;
43
44 if (pid == 0)
45 return 0;
46
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020047 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -080048 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -070049 if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020050 same_thread_group(p, current) : has_group_leader_pid(p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 error = -EINVAL;
52 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020053 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 return error;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Update expiry time from increment, and increase overrun count,
60 * given the current clock sample.
61 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010062static void bump_cpu_timer(struct k_itimer *timer, u64 now)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 int i;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010065 u64 delta, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000067 if (timer->it.cpu.incr == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return;
69
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000070 if (now < timer->it.cpu.expires)
71 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000073 incr = timer->it.cpu.incr;
74 delta = now + incr - timer->it.cpu.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000076 /* Don't use (incr*2 < delta), incr*2 might overflow. */
77 for (i = 0; incr < delta - incr; i++)
78 incr = incr << 1;
79
80 for (; i >= 0; incr >>= 1, i--) {
81 if (delta < incr)
82 continue;
83
84 timer->it.cpu.expires += incr;
85 timer->it_overrun += 1 << i;
86 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88}
89
Frederic Weisbecker555347f2013-04-19 16:17:38 +020090/**
91 * task_cputime_zero - Check a task_cputime struct for all zero fields.
92 *
93 * @cputime: The struct to compare.
94 *
95 * Checks @cputime to see if all fields are zero. Returns true if all fields
96 * are zero, false if any field is nonzero.
97 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010098static inline int task_cputime_zero(const struct task_cputime *cputime)
Frederic Weisbecker555347f2013-04-19 16:17:38 +020099{
100 if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
101 return 1;
102 return 0;
103}
104
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100105static inline u64 prof_ticks(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100107 u64 utime, stime;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100108
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100109 task_cputime(p, &utime, &stime);
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100110
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100111 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100113static inline u64 virt_ticks(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100115 u64 utime, stime;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100116
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100117 task_cputime(p, &utime, &stime);
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100118
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100119 return utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000122static int
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700123posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int error = check_clock(which_clock);
126 if (!error) {
127 tp->tv_sec = 0;
128 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
129 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
130 /*
131 * If sched_clock is using a cycle counter, we
132 * don't have any idea of its true resolution
133 * exported, but it is much more than 1s/HZ.
134 */
135 tp->tv_nsec = 1;
136 }
137 }
138 return error;
139}
140
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000141static int
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700142posix_cpu_clock_set(const clockid_t which_clock, const struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 /*
145 * You can never reset a CPU clock, but we check for other errors
146 * in the call before failing with EPERM.
147 */
148 int error = check_clock(which_clock);
149 if (error == 0) {
150 error = -EPERM;
151 }
152 return error;
153}
154
155
156/*
157 * Sample a per-thread clock for the given task.
158 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100159static int cpu_clock_sample(const clockid_t which_clock,
160 struct task_struct *p, u64 *sample)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 switch (CPUCLOCK_WHICH(which_clock)) {
163 default:
164 return -EINVAL;
165 case CPUCLOCK_PROF:
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000166 *sample = prof_ticks(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 case CPUCLOCK_VIRT:
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000169 *sample = virt_ticks(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 break;
171 case CPUCLOCK_SCHED:
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000172 *sample = task_sched_runtime(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 }
175 return 0;
176}
177
Jason Low10180162015-04-28 13:00:22 -0700178/*
179 * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
180 * to avoid race conditions with concurrent updates to cputime.
181 */
182static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100183{
Jason Low10180162015-04-28 13:00:22 -0700184 u64 curr_cputime;
185retry:
186 curr_cputime = atomic64_read(cputime);
187 if (sum_cputime > curr_cputime) {
188 if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
189 goto retry;
190 }
191}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100192
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100193static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum)
Jason Low10180162015-04-28 13:00:22 -0700194{
Jason Low71107442015-04-28 13:00:24 -0700195 __update_gt_cputime(&cputime_atomic->utime, sum->utime);
196 __update_gt_cputime(&cputime_atomic->stime, sum->stime);
197 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
Jason Low10180162015-04-28 13:00:22 -0700198}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100199
Jason Low71107442015-04-28 13:00:24 -0700200/* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100201static inline void sample_cputime_atomic(struct task_cputime *times,
Jason Low71107442015-04-28 13:00:24 -0700202 struct task_cputime_atomic *atomic_times)
Jason Low10180162015-04-28 13:00:22 -0700203{
Jason Low71107442015-04-28 13:00:24 -0700204 times->utime = atomic64_read(&atomic_times->utime);
205 times->stime = atomic64_read(&atomic_times->stime);
206 times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100207}
208
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100209void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100210{
211 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100212 struct task_cputime sum;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100213
Jason Low10180162015-04-28 13:00:22 -0700214 /* Check if cputimer isn't running. This is accessed without locking. */
215 if (!READ_ONCE(cputimer->running)) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100216 /*
217 * The POSIX timer interface allows for absolute time expiry
218 * values through the TIMER_ABSTIME flag, therefore we have
Jason Low10180162015-04-28 13:00:22 -0700219 * to synchronize the timer to the clock every time we start it.
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100220 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100221 thread_group_cputime(tsk, &sum);
Jason Low71107442015-04-28 13:00:24 -0700222 update_gt_cputime(&cputimer->cputime_atomic, &sum);
Jason Low10180162015-04-28 13:00:22 -0700223
224 /*
225 * We're setting cputimer->running without a lock. Ensure
226 * this only gets written to in one operation. We set
227 * running after update_gt_cputime() as a small optimization,
228 * but barriers are not required because update_gt_cputime()
229 * can handle concurrent updates.
230 */
Jason Lowd5c373e2015-10-14 12:07:55 -0700231 WRITE_ONCE(cputimer->running, true);
Jason Low10180162015-04-28 13:00:22 -0700232 }
Jason Low71107442015-04-28 13:00:24 -0700233 sample_cputime_atomic(times, &cputimer->cputime_atomic);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * Sample a process (thread group) clock for the given group_leader task.
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200238 * Must be called with task sighand lock held for safe while_each_thread()
239 * traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
Thomas Gleixnera924b042006-01-09 20:52:27 -0800241static int cpu_clock_sample_group(const clockid_t which_clock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct task_struct *p,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100243 u64 *sample)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100245 struct task_cputime cputime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700246
Petr Tesarikeccdaea2008-11-24 15:46:31 +0100247 switch (CPUCLOCK_WHICH(which_clock)) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700248 default:
249 return -EINVAL;
250 case CPUCLOCK_PROF:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100251 thread_group_cputime(p, &cputime);
252 *sample = cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700253 break;
254 case CPUCLOCK_VIRT:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100255 thread_group_cputime(p, &cputime);
256 *sample = cputime.utime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700257 break;
258 case CPUCLOCK_SCHED:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100259 thread_group_cputime(p, &cputime);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000260 *sample = cputime.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700261 break;
262 }
263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200266static int posix_cpu_clock_get_task(struct task_struct *tsk,
267 const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700268 struct timespec64 *tp)
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200269{
270 int err = -EINVAL;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100271 u64 rtn;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200272
273 if (CPUCLOCK_PERTHREAD(which_clock)) {
274 if (same_thread_group(tsk, current))
275 err = cpu_clock_sample(which_clock, tsk, &rtn);
276 } else {
Frederic Weisbecker50875782013-10-11 17:41:11 +0200277 if (tsk == current || thread_group_leader(tsk))
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200278 err = cpu_clock_sample_group(which_clock, tsk, &rtn);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200279 }
280
281 if (!err)
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700282 *tp = ns_to_timespec64(rtn);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200283
284 return err;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700288static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 const pid_t pid = CPUCLOCK_PID(which_clock);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200291 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (pid == 0) {
294 /*
295 * Special case constant value for our own clocks.
296 * We don't have to do any lookup to find ourselves.
297 */
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200298 err = posix_cpu_clock_get_task(current, which_clock, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 /*
301 * Find the given PID, and validate that the caller
302 * should be able to see it.
303 */
304 struct task_struct *p;
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800305 rcu_read_lock();
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800306 p = find_task_by_vpid(pid);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200307 if (p)
308 err = posix_cpu_clock_get_task(p, which_clock, tp);
Paul E. McKenney1f2ea082007-02-16 01:28:22 -0800309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800317 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
318 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000320static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int ret = 0;
323 const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
324 struct task_struct *p;
325
326 if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
327 return -EINVAL;
328
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200329 new_timer->kclock = &clock_posix_cpu;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200333 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
335 if (pid == 0) {
336 p = current;
337 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800338 p = find_task_by_vpid(pid);
Pavel Emelyanovbac0abd62007-10-18 23:40:18 -0700339 if (p && !same_thread_group(p, current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 p = NULL;
341 }
342 } else {
343 if (pid == 0) {
344 p = current->group_leader;
345 } else {
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800346 p = find_task_by_vpid(pid);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200347 if (p && !has_group_leader_pid(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 p = NULL;
349 }
350 }
351 new_timer->it.cpu.task = p;
352 if (p) {
353 get_task_struct(p);
354 } else {
355 ret = -EINVAL;
356 }
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +0200357 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return ret;
360}
361
362/*
363 * Clean up a CPU-clock timer that is about to be destroyed.
364 * This is called from timer deletion with the timer already locked.
365 * If we return TIMER_RETRY, it's necessary to release the timer's lock
366 * and try again. (This happens when the timer is in the middle of firing.)
367 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000368static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Oleg Nesterov108150e2005-10-23 20:25:39 +0400370 int ret = 0;
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200371 unsigned long flags;
372 struct sighand_struct *sighand;
373 struct task_struct *p = timer->it.cpu.task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200375 WARN_ON_ONCE(p == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200377 /*
378 * Protect against sighand release/switch in exit/exec and process/
379 * thread timer list entry concurrent read/writes.
380 */
381 sighand = lock_task_sighand(p, &flags);
382 if (unlikely(sighand == NULL)) {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200383 /*
384 * We raced with the reaping of the task.
385 * The deletion should have cleared us off the list.
386 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +0200387 WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200388 } else {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200389 if (timer->it.cpu.firing)
390 ret = TIMER_RETRY;
391 else
392 list_del(&timer->it.cpu.entry);
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200393
394 unlock_task_sighand(p, &flags);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400395 }
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200396
397 if (!ret)
398 put_task_struct(p);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400399
400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200403static void cleanup_timers_list(struct list_head *head)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000404{
405 struct cpu_timer_list *timer, *next;
406
Frederic Weisbeckera0b20622013-06-28 00:06:43 +0000407 list_for_each_entry_safe(timer, next, head, entry)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000408 list_del_init(&timer->entry);
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * Clean out CPU timers still ticking when a thread exited. The task
413 * pointer is cleared, and the expiry time is replaced with the residual
414 * time for later timer_gettime calls to return.
415 * This must be called with the siglock held.
416 */
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200417static void cleanup_timers(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200419 cleanup_timers_list(head);
420 cleanup_timers_list(++head);
421 cleanup_timers_list(++head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
425 * These are both called with the siglock held, when the current thread
426 * is being reaped. When the final (leader) thread in the group is reaped,
427 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
428 */
429void posix_cpu_timers_exit(struct task_struct *tsk)
430{
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200431 cleanup_timers(tsk->cpu_timers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433void posix_cpu_timers_exit_group(struct task_struct *tsk)
434{
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200435 cleanup_timers(tsk->signal->cpu_timers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100438static inline int expires_gt(u64 expires, u64 new_exp)
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200439{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100440 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * Insert the timer on the appropriate list before any timers that
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200445 * expire later. This must be called with the sighand lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800447static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct task_struct *p = timer->it.cpu.task;
450 struct list_head *head, *listpos;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100451 struct task_cputime *cputime_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct cpu_timer_list *const nt = &timer->it.cpu;
453 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800455 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
456 head = p->cpu_timers;
457 cputime_expires = &p->cputime_expires;
458 } else {
459 head = p->signal->cpu_timers;
460 cputime_expires = &p->signal->cputime_expires;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 head += CPUCLOCK_WHICH(timer->it_clock);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800465 list_for_each_entry(next, head, entry) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000466 if (nt->expires < next->expires)
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800467 break;
468 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 list_add(&nt->entry, listpos);
471
472 if (listpos == head) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100473 u64 exp = nt->expires;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800476 * We are the new earliest-expiring POSIX 1.b timer, hence
477 * need to update expiration cache. Take into account that
478 * for process timers we share expiration cache with itimers
479 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800482 switch (CPUCLOCK_WHICH(timer->it_clock)) {
483 case CPUCLOCK_PROF:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100484 if (expires_gt(cputime_expires->prof_exp, exp))
485 cputime_expires->prof_exp = exp;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800486 break;
487 case CPUCLOCK_VIRT:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100488 if (expires_gt(cputime_expires->virt_exp, exp))
489 cputime_expires->virt_exp = exp;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800490 break;
491 case CPUCLOCK_SCHED:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100492 if (expires_gt(cputime_expires->sched_exp, exp))
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000493 cputime_expires->sched_exp = exp;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200496 if (CPUCLOCK_PERTHREAD(timer->it_clock))
497 tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
498 else
499 tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
504 * The timer is locked, fire it and arrange for its reload.
505 */
506static void cpu_timer_fire(struct k_itimer *timer)
507{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800508 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
509 /*
510 * User don't want any signal.
511 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000512 timer->it.cpu.expires = 0;
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800513 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * This a special case for clock_nanosleep,
516 * not a normal timer from sys_timer_create.
517 */
518 wake_up_process(timer->it_process);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000519 timer->it.cpu.expires = 0;
520 } else if (timer->it.cpu.incr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * One-shot timer. Clear it as soon as it's fired.
523 */
524 posix_timer_event(timer, 0);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000525 timer->it.cpu.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
527 /*
528 * The signal did not get queued because the signal
529 * was ignored, so we won't get any callback to
530 * reload the timer. But we need to keep it
531 * ticking in case the signal is deliverable next time.
532 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200533 posix_cpu_timer_rearm(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200534 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536}
537
538/*
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100539 * Sample a process (thread group) timer for the given group_leader task.
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200540 * Must be called with task sighand lock held for safe while_each_thread()
541 * traversal.
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100542 */
543static int cpu_timer_sample_group(const clockid_t which_clock,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100544 struct task_struct *p, u64 *sample)
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100545{
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100546 struct task_cputime cputime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100547
548 thread_group_cputimer(p, &cputime);
549 switch (CPUCLOCK_WHICH(which_clock)) {
550 default:
551 return -EINVAL;
552 case CPUCLOCK_PROF:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100553 *sample = cputime.utime + cputime.stime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100554 break;
555 case CPUCLOCK_VIRT:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100556 *sample = cputime.utime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100557 break;
558 case CPUCLOCK_SCHED:
Peter Zijlstra23cfa362014-11-12 12:37:37 +0100559 *sample = cputime.sum_exec_runtime;
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100560 break;
561 }
562 return 0;
563}
564
565/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * Guts of sys_timer_settime for CPU timers.
567 * This is called with the timer locked and interrupts disabled.
568 * If we return TIMER_RETRY, it's necessary to release the timer's lock
569 * and try again. (This happens when the timer is in the middle of firing.)
570 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200571static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700572 struct itimerspec64 *new, struct itimerspec64 *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200574 unsigned long flags;
575 struct sighand_struct *sighand;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct task_struct *p = timer->it.cpu.task;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100577 u64 old_expires, new_expires, old_incr, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int ret;
579
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200580 WARN_ON_ONCE(p == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700582 new_expires = timespec64_to_ns(&new->it_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200585 * Protect against sighand release/switch in exit/exec and p->cpu_timers
586 * and p->signal->cpu_timers read/write in arm_timer()
587 */
588 sighand = lock_task_sighand(p, &flags);
589 /*
590 * If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * longer get any information about it at all.
592 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200593 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -ESRCH;
595 }
596
597 /*
598 * Disarm any old timer after extracting its expiry time.
599 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +0200600 WARN_ON_ONCE(!irqs_disabled());
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400601
602 ret = 0;
Stanislaw Gruszkaae1a78e2010-03-11 14:04:39 -0800603 old_incr = timer->it.cpu.incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400605 if (unlikely(timer->it.cpu.firing)) {
606 timer->it.cpu.firing = -1;
607 ret = TIMER_RETRY;
608 } else
609 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * We need to sample the current value to convert the new
613 * value from to relative and absolute, and to convert the
614 * old value from absolute to relative. To set a process
615 * timer, we need a sample to balance the thread expiry
616 * times (in arm_timer). With an absolute time, we must
617 * check if it's already passed. In short, we need a sample.
618 */
619 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
620 cpu_clock_sample(timer->it_clock, p, &val);
621 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100622 cpu_timer_sample_group(timer->it_clock, p, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 if (old) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000626 if (old_expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 old->it_value.tv_sec = 0;
628 old->it_value.tv_nsec = 0;
629 } else {
630 /*
631 * Update the timer in case it has
632 * overrun already. If it has,
633 * we'll report it as having overrun
634 * and with the next reloaded timer
635 * already ticking, though we are
636 * swallowing that pending
637 * notification here to install the
638 * new setting.
639 */
640 bump_cpu_timer(timer, val);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000641 if (val < timer->it.cpu.expires) {
642 old_expires = timer->it.cpu.expires - val;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700643 old->it_value = ns_to_timespec64(old_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 } else {
645 old->it_value.tv_nsec = 1;
646 old->it_value.tv_sec = 0;
647 }
648 }
649 }
650
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400651 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * We are colliding with the timer actually firing.
654 * Punt after filling in the timer's old value, and
655 * disable this firing since we are already reporting
656 * it as an overrun (thanks to bump_cpu_timer above).
657 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200658 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto out;
660 }
661
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200662 if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000663 new_expires += val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 /*
667 * Install the new expiry time (or zero).
668 * For a timer with no notification action, we don't actually
669 * arm the timer (we'll just fake it for timer_gettime).
670 */
671 timer->it.cpu.expires = new_expires;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000672 if (new_expires != 0 && val < new_expires) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800673 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200676 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Install the new reload setting, and
679 * set up the signal and overrun bookkeeping.
680 */
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700681 timer->it.cpu.incr = timespec64_to_ns(&new->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /*
684 * This acts as a modification timestamp for the timer,
685 * so any automatic reload attempt will punt on seeing
686 * that we have reset the timer manually.
687 */
688 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
689 ~REQUEUE_PENDING;
690 timer->it_overrun_last = 0;
691 timer->it_overrun = -1;
692
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000693 if (new_expires != 0 && !(val < new_expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /*
695 * The designated time already passed, so we notify
696 * immediately, even if the thread never runs to
697 * accumulate more time on this clock.
698 */
699 cpu_timer_fire(timer);
700 }
701
702 ret = 0;
703 out:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100704 if (old)
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700705 old->it_interval = ns_to_timespec64(old_incr);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return ret;
708}
709
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700710static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100712 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct task_struct *p = timer->it.cpu.task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200715 WARN_ON_ONCE(p == NULL);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Easy part: convert the reload time.
719 */
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700720 itp->it_interval = ns_to_timespec64(timer->it.cpu.incr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Thomas Gleixnereabdec02017-05-30 23:15:51 +0200722 if (!timer->it.cpu.expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Sample the clock to take the difference with the expiry time.
727 */
728 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
729 cpu_clock_sample(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200731 struct sighand_struct *sighand;
732 unsigned long flags;
733
734 /*
735 * Protect against sighand release/switch in exit/exec and
736 * also make timer sampling safe if it ends up calling
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100737 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200738 */
739 sighand = lock_task_sighand(p, &flags);
740 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
742 * The process has been reaped.
743 * We can't even collect a sample any more.
744 * Call the timer disarmed, nothing else to do.
745 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000746 timer->it.cpu.expires = 0;
Alexey Dobriyan2c13ce82016-07-08 01:39:11 +0300747 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
Peter Zijlstra3997ad32009-02-12 15:00:52 +0100749 cpu_timer_sample_group(timer->it_clock, p, &now);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200750 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000754 if (now < timer->it.cpu.expires) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700755 itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else {
757 /*
758 * The timer should have expired already, but the firing
759 * hasn't taken place yet. Say it's just about to expire.
760 */
761 itp->it_value.tv_nsec = 1;
762 itp->it_value.tv_sec = 0;
763 }
764}
765
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000766static unsigned long long
767check_timers_list(struct list_head *timers,
768 struct list_head *firing,
769 unsigned long long curr)
770{
771 int maxfire = 20;
772
773 while (!list_empty(timers)) {
774 struct cpu_timer_list *t;
775
776 t = list_first_entry(timers, struct cpu_timer_list, entry);
777
778 if (!--maxfire || curr < t->expires)
779 return t->expires;
780
781 t->firing = 1;
782 list_move_tail(&t->entry, firing);
783 }
784
785 return 0;
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/*
789 * Check for any per-thread CPU timers that have fired and move them off
790 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
791 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
792 */
793static void check_thread_timers(struct task_struct *tsk,
794 struct list_head *firing)
795{
796 struct list_head *timers = tsk->cpu_timers;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100797 struct signal_struct *const sig = tsk->signal;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100798 struct task_cputime *tsk_expires = &tsk->cputime_expires;
799 u64 expires;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800800 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jason Low934715a2015-10-14 12:07:54 -0700802 /*
803 * If cputime_expires is zero, then there are no active
804 * per thread CPU timers.
805 */
806 if (task_cputime_zero(&tsk->cputime_expires))
807 return;
808
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000809 expires = check_timers_list(timers, firing, prof_ticks(tsk));
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100810 tsk_expires->prof_exp = expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000812 expires = check_timers_list(++timers, firing, virt_ticks(tsk));
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100813 tsk_expires->virt_exp = expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000815 tsk_expires->sched_exp = check_timers_list(++timers, firing,
816 tsk->se.sum_exec_runtime);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100817
818 /*
819 * Check for the special case thread timers.
820 */
Jason Low316c1608d2015-04-28 13:00:20 -0700821 soft = READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800822 if (soft != RLIM_INFINITY) {
Jiri Slaby78d7d402010-03-05 13:42:54 -0800823 unsigned long hard =
Jason Low316c1608d2015-04-28 13:00:20 -0700824 READ_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100825
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100826 if (hard != RLIM_INFINITY &&
827 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100828 /*
829 * At the hard limit, we just die.
830 * No need to calculate anything else now.
831 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200832 if (print_fatal_signals) {
833 pr_info("CPU Watchdog Timeout (hard): %s[%d]\n",
834 tsk->comm, task_pid_nr(tsk));
835 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100836 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
837 return;
838 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800839 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100840 /*
841 * At the soft limit, send a SIGXCPU every second.
842 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800843 if (soft < hard) {
844 soft += USEC_PER_SEC;
845 sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100846 }
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200847 if (print_fatal_signals) {
848 pr_info("RT Watchdog Timeout (soft): %s[%d]\n",
849 tsk->comm, task_pid_nr(tsk));
850 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100851 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
852 }
853 }
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200854 if (task_cputime_zero(tsk_expires))
855 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Jason Low10180162015-04-28 13:00:22 -0700858static inline void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100859{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800860 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100861
Jason Low10180162015-04-28 13:00:22 -0700862 /* Turn off cputimer->running. This is done without locking. */
Jason Lowd5c373e2015-10-14 12:07:55 -0700863 WRITE_ONCE(cputimer->running, false);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200864 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100865}
866
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200867static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100868 u64 *expires, u64 cur_time, int signo)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200869{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100870 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200871 return;
872
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100873 if (cur_time >= it->expires) {
874 if (it->incr)
Martin Schwidefsky64861632011-12-15 14:56:09 +0100875 it->expires += it->incr;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100876 else
Martin Schwidefsky64861632011-12-15 14:56:09 +0100877 it->expires = 0;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200878
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800879 trace_itimer_expire(signo == SIGPROF ?
880 ITIMER_PROF : ITIMER_VIRTUAL,
881 tsk->signal->leader_pid, cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200882 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
883 }
884
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100885 if (it->expires && (!*expires || it->expires < *expires))
886 *expires = it->expires;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*
890 * Check for any per-thread CPU timers that have fired and move them
891 * off the tsk->*_timers list onto the firing list. Per-thread timers
892 * have already been taken off.
893 */
894static void check_process_timers(struct task_struct *tsk,
895 struct list_head *firing)
896{
897 struct signal_struct *const sig = tsk->signal;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100898 u64 utime, ptime, virt_expires, prof_expires;
899 u64 sum_sched_runtime, sched_expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct list_head *timers = sig->cpu_timers;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100901 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800902 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /*
Jason Low934715a2015-10-14 12:07:54 -0700905 * If cputimer is not running, then there are no active
906 * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
907 */
908 if (!READ_ONCE(tsk->signal->cputimer.running))
909 return;
910
Jason Lowc8d75aa2015-10-14 12:07:56 -0700911 /*
912 * Signify that a thread is checking for process timers.
913 * Write access to this field is protected by the sighand lock.
914 */
915 sig->cputimer.checking_timer = true;
916
Jason Low934715a2015-10-14 12:07:54 -0700917 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * Collect the current process totals.
919 */
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100920 thread_group_cputimer(tsk, &cputime);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100921 utime = cputime.utime;
922 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700923 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000925 prof_expires = check_timers_list(timers, firing, ptime);
926 virt_expires = check_timers_list(++timers, firing, utime);
927 sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /*
930 * Check for the special case process timers.
931 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200932 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
933 SIGPROF);
934 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
935 SIGVTALRM);
Jason Low316c1608d2015-04-28 13:00:20 -0700936 soft = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800937 if (soft != RLIM_INFINITY) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100938 unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
Jiri Slaby78d7d402010-03-05 13:42:54 -0800939 unsigned long hard =
Jason Low316c1608d2015-04-28 13:00:20 -0700940 READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100941 u64 x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800942 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * At the hard limit, we just die.
945 * No need to calculate anything else now.
946 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200947 if (print_fatal_signals) {
948 pr_info("RT Watchdog Timeout (hard): %s[%d]\n",
949 tsk->comm, task_pid_nr(tsk));
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
952 return;
953 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800954 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * At the soft limit, send a SIGXCPU every second.
957 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200958 if (print_fatal_signals) {
959 pr_info("CPU Watchdog Timeout (soft): %s[%d]\n",
960 tsk->comm, task_pid_nr(tsk));
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800963 if (soft < hard) {
964 soft++;
965 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 }
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100968 x = soft * NSEC_PER_SEC;
969 if (!prof_expires || x < prof_expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 prof_expires = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100973 sig->cputime_expires.prof_exp = prof_expires;
974 sig->cputime_expires.virt_exp = virt_expires;
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -0700975 sig->cputime_expires.sched_exp = sched_expires;
976 if (task_cputime_zero(&sig->cputime_expires))
977 stop_process_timers(sig);
Jason Lowc8d75aa2015-10-14 12:07:56 -0700978
979 sig->cputimer.checking_timer = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200983 * This is called from the signal code (via posixtimer_rearm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * when the last timer signal was delivered and we have to reload the timer.
985 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200986static void posix_cpu_timer_rearm(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200988 struct sighand_struct *sighand;
989 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 struct task_struct *p = timer->it.cpu.task;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100991 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200993 WARN_ON_ONCE(p == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * Fetch the current sample and update the timer's expiry time.
997 */
998 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
999 cpu_clock_sample(timer->it_clock, p, &now);
1000 bump_cpu_timer(timer, now);
Frederic Weisbecker724a37132013-10-10 16:55:57 +02001001 if (unlikely(p->exit_state))
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001002 return;
Frederic Weisbecker724a37132013-10-10 16:55:57 +02001003
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001004 /* Protect timer list r/w in arm_timer() */
1005 sighand = lock_task_sighand(p, &flags);
1006 if (!sighand)
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001009 /*
1010 * Protect arm_timer() and timer sampling in case of call to
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001011 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001012 */
1013 sighand = lock_task_sighand(p, &flags);
1014 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * The process has been reaped.
1017 * We can't even collect a sample any more.
1018 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001019 timer->it.cpu.expires = 0;
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001020 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001022 /* If the process is dying, no need to rearm */
1023 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Peter Zijlstra3997ad32009-02-12 15:00:52 +01001025 cpu_timer_sample_group(timer->it_clock, p, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 bump_cpu_timer(timer, now);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001027 /* Leave the sighand locked for the call below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 /*
1031 * Now re-arm for the new expiry time.
1032 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +02001033 WARN_ON_ONCE(!irqs_disabled());
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001034 arm_timer(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001035unlock:
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001036 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Frank Mayharf06febc2008-09-12 09:54:39 -07001039/**
Frank Mayharf06febc2008-09-12 09:54:39 -07001040 * task_cputime_expired - Compare two task_cputime entities.
1041 *
1042 * @sample: The task_cputime structure to be checked for expiration.
1043 * @expires: Expiration times, against which @sample will be checked.
1044 *
1045 * Checks @sample against @expires to see if any field of @sample has expired.
1046 * Returns true if any field of the former is greater than the corresponding
1047 * field of the latter if the latter field is set. Otherwise returns false.
1048 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001049static inline int task_cputime_expired(const struct task_cputime *sample,
1050 const struct task_cputime *expires)
Frank Mayharf06febc2008-09-12 09:54:39 -07001051{
Martin Schwidefsky64861632011-12-15 14:56:09 +01001052 if (expires->utime && sample->utime >= expires->utime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001053 return 1;
Martin Schwidefsky64861632011-12-15 14:56:09 +01001054 if (expires->stime && sample->utime + sample->stime >= expires->stime)
Frank Mayharf06febc2008-09-12 09:54:39 -07001055 return 1;
1056 if (expires->sum_exec_runtime != 0 &&
1057 sample->sum_exec_runtime >= expires->sum_exec_runtime)
1058 return 1;
1059 return 0;
1060}
1061
1062/**
1063 * fastpath_timer_check - POSIX CPU timers fast path.
1064 *
1065 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001066 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001067 * Check the task and thread group timers. If both are zero (there are no
1068 * timers set) return false. Otherwise snapshot the task and thread group
1069 * timers and compare them with the corresponding expiration times. Return
1070 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001071 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001072static inline int fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001073{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001074 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001075
Frank Mayharbb34d922008-09-12 09:54:39 -07001076 if (!task_cputime_zero(&tsk->cputime_expires)) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001077 struct task_cputime task_sample;
Frank Mayharbb34d922008-09-12 09:54:39 -07001078
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001079 task_cputime(tsk, &task_sample.utime, &task_sample.stime);
Jason Low7c177d92015-10-14 12:07:53 -07001080 task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime;
Frank Mayharbb34d922008-09-12 09:54:39 -07001081 if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
1082 return 1;
1083 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001084
1085 sig = tsk->signal;
Jason Lowc8d75aa2015-10-14 12:07:56 -07001086 /*
1087 * Check if thread group timers expired when the cputimer is
1088 * running and no other thread in the group is already checking
1089 * for thread group cputimers. These fields are read without the
1090 * sighand lock. However, this is fine because this is meant to
1091 * be a fastpath heuristic to determine whether we should try to
1092 * acquire the sighand lock to check/handle timers.
1093 *
1094 * In the worst case scenario, if 'running' or 'checking_timer' gets
1095 * set but the current thread doesn't see the change yet, we'll wait
1096 * until the next thread in the group gets a scheduler interrupt to
1097 * handle the timer. This isn't an issue in practice because these
1098 * types of delays with signals actually getting sent are expected.
1099 */
1100 if (READ_ONCE(sig->cputimer.running) &&
1101 !READ_ONCE(sig->cputimer.checking_timer)) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001102 struct task_cputime group_sample;
Frank Mayharbb34d922008-09-12 09:54:39 -07001103
Jason Low71107442015-04-28 13:00:24 -07001104 sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001105
Frank Mayharbb34d922008-09-12 09:54:39 -07001106 if (task_cputime_expired(&group_sample, &sig->cputime_expires))
1107 return 1;
1108 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001109
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001110 return 0;
Frank Mayharf06febc2008-09-12 09:54:39 -07001111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
1114 * This is called from the timer interrupt handler. The irq handler has
1115 * already updated our counts. We need to check if any timers fire now.
1116 * Interrupts are disabled.
1117 */
1118void run_posix_cpu_timers(struct task_struct *tsk)
1119{
1120 LIST_HEAD(firing);
1121 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001122 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Frederic Weisbecker531f64f2013-10-11 17:58:08 +02001124 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001127 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001128 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001130 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001131 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001132
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001133 if (!lock_task_sighand(tsk, &flags))
1134 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001135 /*
1136 * Here we take off tsk->signal->cpu_timers[N] and
1137 * tsk->cpu_timers[N] all the timers that are firing, and
1138 * put them on the firing list.
1139 */
1140 check_thread_timers(tsk, &firing);
Jason Low934715a2015-10-14 12:07:54 -07001141
1142 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Frank Mayharbb34d922008-09-12 09:54:39 -07001144 /*
1145 * We must release these locks before taking any timer's lock.
1146 * There is a potential race with timer deletion here, as the
1147 * siglock now protects our private firing list. We have set
1148 * the firing flag in each timer, so that a deletion attempt
1149 * that gets the timer lock before we do will give it up and
1150 * spin until we've taken care of that timer below.
1151 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001152 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /*
1155 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001156 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * each timer's lock before clearing its firing flag, so no
1158 * timer call will interfere.
1159 */
1160 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001161 int cpu_firing;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 spin_lock(&timer->it_lock);
1164 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001165 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 timer->it.cpu.firing = 0;
1167 /*
1168 * The firing flag is -1 if we collided with a reset
1169 * of the timer, which already reported this
1170 * almost-firing as an overrun. So don't generate an event.
1171 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001172 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 spin_unlock(&timer->it_lock);
1175 }
1176}
1177
1178/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001179 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001180 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
1182void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001183 u64 *newval, u64 *oldval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001185 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Frederic Weisbecker531f64f2013-10-11 17:58:08 +02001187 WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +01001188 cpu_timer_sample_group(clock_idx, tsk, &now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001191 /*
1192 * We are setting itimer. The *oldval is absolute and we update
1193 * it to be relative, *newval argument is relative and we update
1194 * it to be absolute.
1195 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001196 if (*oldval) {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001197 if (*oldval <= now) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Just about to fire. */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001199 *oldval = TICK_NSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001201 *oldval -= now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 }
1204
Martin Schwidefsky64861632011-12-15 14:56:09 +01001205 if (!*newval)
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001206 return;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001207 *newval += now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 /*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001211 * Update expiration cache if we are the earliest timer, or eventually
1212 * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001214 switch (clock_idx) {
1215 case CPUCLOCK_PROF:
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001216 if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
1217 tsk->signal->cputime_expires.prof_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001218 break;
1219 case CPUCLOCK_VIRT:
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001220 if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
1221 tsk->signal->cputime_expires.virt_exp = *newval;
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001224
1225 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Toyo Abee4b76552006-09-29 02:00:29 -07001228static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -07001229 struct timespec64 *rqtp, struct itimerspec64 *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct k_itimer timer;
1232 int error;
1233
1234 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * Set up a temporary timer and then wait for it to go off.
1236 */
1237 memset(&timer, 0, sizeof timer);
1238 spin_lock_init(&timer.it_lock);
1239 timer.it_clock = which_clock;
1240 timer.it_overrun = -1;
1241 error = posix_cpu_timer_create(&timer);
1242 timer.it_process = current;
1243 if (!error) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -07001244 static struct itimerspec64 zero_it;
Toyo Abee4b76552006-09-29 02:00:29 -07001245
1246 memset(it, 0, sizeof *it);
1247 it->it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 spin_lock_irq(&timer.it_lock);
Deepa Dinamaniad196382017-03-26 12:04:18 -07001250 error = posix_cpu_timer_set(&timer, flags, it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (error) {
1252 spin_unlock_irq(&timer.it_lock);
1253 return error;
1254 }
1255
1256 while (!signal_pending(current)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001257 if (timer.it.cpu.expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001259 * Our timer fired and was reset, below
1260 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001262 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 spin_unlock_irq(&timer.it_lock);
1264 return 0;
1265 }
1266
1267 /*
1268 * Block until cpu_timer_fire (or a signal) wakes us.
1269 */
1270 __set_current_state(TASK_INTERRUPTIBLE);
1271 spin_unlock_irq(&timer.it_lock);
1272 schedule();
1273 spin_lock_irq(&timer.it_lock);
1274 }
1275
1276 /*
1277 * We were interrupted by a signal.
1278 */
Deepa Dinamaniad196382017-03-26 12:04:18 -07001279 *rqtp = ns_to_timespec64(timer.it.cpu.expires);
1280 error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001281 if (!error) {
1282 /*
1283 * Timer is now unarmed, deletion can not fail.
1284 */
1285 posix_cpu_timer_del(&timer);
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 spin_unlock_irq(&timer.it_lock);
1288
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001289 while (error == TIMER_RETRY) {
1290 /*
1291 * We need to handle case when timer was or is in the
1292 * middle of firing. In other cases we already freed
1293 * resources.
1294 */
1295 spin_lock_irq(&timer.it_lock);
1296 error = posix_cpu_timer_del(&timer);
1297 spin_unlock_irq(&timer.it_lock);
1298 }
1299
Toyo Abee4b76552006-09-29 02:00:29 -07001300 if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * It actually did fire already.
1303 */
1304 return 0;
1305 }
1306
Toyo Abee4b76552006-09-29 02:00:29 -07001307 error = -ERESTART_RESTARTBLOCK;
1308 }
1309
1310 return error;
1311}
1312
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001313static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1314
1315static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -07001316 struct timespec64 *rqtp, struct timespec __user *rmtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001317{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001318 struct restart_block *restart_block = &current->restart_block;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001319 struct itimerspec64 it;
1320 struct timespec ts;
Toyo Abee4b76552006-09-29 02:00:29 -07001321 int error;
1322
1323 /*
1324 * Diagnose required errors first.
1325 */
1326 if (CPUCLOCK_PERTHREAD(which_clock) &&
1327 (CPUCLOCK_PID(which_clock) == 0 ||
Eric W. Biederman01a21972017-04-13 10:32:16 -05001328 CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
Toyo Abee4b76552006-09-29 02:00:29 -07001329 return -EINVAL;
1330
1331 error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1332
1333 if (error == -ERESTART_RESTARTBLOCK) {
1334
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001335 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001336 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001338 * Report back to the user the time still remaining.
1339 */
Deepa Dinamaniad196382017-03-26 12:04:18 -07001340 ts = timespec64_to_timespec(it.it_value);
1341 if (rmtp && copy_to_user(rmtp, &ts, sizeof(*rmtp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EFAULT;
1343
Toyo Abe1711ef32006-09-29 02:00:28 -07001344 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001345 restart_block->nanosleep.clockid = which_clock;
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001346 restart_block->nanosleep.rmtp = rmtp;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001347 restart_block->nanosleep.expires = timespec64_to_ns(rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return error;
1350}
1351
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001352static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001354 clockid_t which_clock = restart_block->nanosleep.clockid;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001355 struct itimerspec64 it;
1356 struct timespec64 t;
1357 struct timespec tmp;
Toyo Abee4b76552006-09-29 02:00:29 -07001358 int error;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001359
Deepa Dinamaniad196382017-03-26 12:04:18 -07001360 t = ns_to_timespec64(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001361
Toyo Abee4b76552006-09-29 02:00:29 -07001362 error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1363
1364 if (error == -ERESTART_RESTARTBLOCK) {
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001365 struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
Toyo Abee4b76552006-09-29 02:00:29 -07001366 /*
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001367 * Report back to the user the time still remaining.
1368 */
Deepa Dinamaniad196382017-03-26 12:04:18 -07001369 tmp = timespec64_to_timespec(it.it_value);
1370 if (rmtp && copy_to_user(rmtp, &tmp, sizeof(*rmtp)))
Toyo Abee4b76552006-09-29 02:00:29 -07001371 return -EFAULT;
1372
Deepa Dinamaniad196382017-03-26 12:04:18 -07001373 restart_block->nanosleep.expires = timespec64_to_ns(&t);
Toyo Abee4b76552006-09-29 02:00:29 -07001374 }
1375 return error;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1380#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1381
Thomas Gleixnera924b042006-01-09 20:52:27 -08001382static int process_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001383 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1386}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001387static int process_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001388 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1391}
1392static int process_cpu_timer_create(struct k_itimer *timer)
1393{
1394 timer->it_clock = PROCESS_CLOCK;
1395 return posix_cpu_timer_create(timer);
1396}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001397static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -07001398 struct timespec64 *rqtp,
Thomas Gleixner97735f22006-01-09 20:52:37 -08001399 struct timespec __user *rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Thomas Gleixner97735f22006-01-09 20:52:37 -08001401 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
Toyo Abe1711ef32006-09-29 02:00:28 -07001403static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1404{
1405 return -EINVAL;
1406}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001407static int thread_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001408 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1411}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001412static int thread_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001413 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1416}
1417static int thread_cpu_timer_create(struct k_itimer *timer)
1418{
1419 timer->it_clock = THREAD_CLOCK;
1420 return posix_cpu_timer_create(timer);
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001423const struct k_clock clock_posix_cpu = {
Thomas Gleixner19769452011-02-01 13:51:06 +00001424 .clock_getres = posix_cpu_clock_getres,
1425 .clock_set = posix_cpu_clock_set,
1426 .clock_get = posix_cpu_clock_get,
1427 .timer_create = posix_cpu_timer_create,
1428 .nsleep = posix_cpu_nsleep,
1429 .nsleep_restart = posix_cpu_nsleep_restart,
1430 .timer_set = posix_cpu_timer_set,
1431 .timer_del = posix_cpu_timer_del,
1432 .timer_get = posix_cpu_timer_get,
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +02001433 .timer_rearm = posix_cpu_timer_rearm,
Thomas Gleixner19769452011-02-01 13:51:06 +00001434};
1435
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001436const struct k_clock clock_process = {
1437 .clock_getres = process_cpu_clock_getres,
1438 .clock_get = process_cpu_clock_get,
1439 .timer_create = process_cpu_timer_create,
1440 .nsleep = process_cpu_nsleep,
1441 .nsleep_restart = process_cpu_nsleep_restart,
1442};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001444const struct k_clock clock_thread = {
1445 .clock_getres = thread_cpu_clock_getres,
1446 .clock_get = thread_cpu_clock_get,
1447 .timer_create = thread_cpu_timer_create,
1448};