blob: e159b039e44afee3d5033c4dfb80ab0d8a529765 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Implement CPU time clocks for the POSIX clock interface.
4 */
5
Ingo Molnar3f07c012017-02-08 18:51:30 +01006#include <linux/sched/signal.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +01007#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/posix-timers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/errno.h>
Roman Zippelf8bd2252008-05-01 04:34:31 -070010#include <linux/math64.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
Frank Mayharbb34d922008-09-12 09:54:39 -070012#include <linux/kernel_stat.h>
Xiao Guangrong3f0a5252009-08-10 10:52:30 +080013#include <trace/events/timer.h>
Frederic Weisbeckera8572162013-04-18 01:31:13 +020014#include <linux/tick.h>
15#include <linux/workqueue.h>
Al Viroedbeda42017-06-07 09:42:31 +010016#include <linux/compat.h>
Juri Lelli34be3932017-12-12 12:10:24 +010017#include <linux/sched/deadline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020019#include "posix-timers.h"
20
Thomas Gleixner11b84622019-08-21 21:09:07 +020021static inline void temporary_check(void)
22{
23 BUILD_BUG_ON(offsetof(struct task_cputime, stime) !=
24 CPUCLOCK_PROF * sizeof(u64));
25 BUILD_BUG_ON(offsetof(struct task_cputime, utime) !=
26 CPUCLOCK_VIRT * sizeof(u64));
27 BUILD_BUG_ON(offsetof(struct task_cputime, sum_exec_runtime) !=
28 CPUCLOCK_SCHED * sizeof(u64));
29}
30
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +020031static void posix_cpu_timer_rearm(struct k_itimer *timer);
32
Thomas Gleixner3a245c02019-08-21 21:09:06 +020033void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
34{
35 posix_cputimers_init(pct);
36 if (cpu_limit != RLIM_INFINITY)
Thomas Gleixner11b84622019-08-21 21:09:07 +020037 pct->expiries[CPUCLOCK_PROF] = cpu_limit * NSEC_PER_SEC;
Thomas Gleixner3a245c02019-08-21 21:09:06 +020038}
39
Frank Mayharf06febc2008-09-12 09:54:39 -070040/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080041 * Called after updating RLIMIT_CPU to run cpu timer and update
Thomas Gleixner001f7972019-08-21 21:09:13 +020042 * tsk->signal->posix_cputimers.expiries expiration cache if
Thomas Gleixner3a245c02019-08-21 21:09:06 +020043 * necessary. Needs siglock protection since other code may update
44 * expiration cache as well.
Frank Mayharf06febc2008-09-12 09:54:39 -070045 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020046void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070047{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010048 u64 nsecs = rlim_new * NSEC_PER_SEC;
Frank Mayharf06febc2008-09-12 09:54:39 -070049
Jiri Slaby5ab46b32009-08-28 14:05:12 +020050 spin_lock_irq(&task->sighand->siglock);
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010051 set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
Jiri Slaby5ab46b32009-08-28 14:05:12 +020052 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070053}
54
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020055/*
56 * Functions for validating access to tasks.
57 */
58static struct task_struct *lookup_task(const pid_t pid, bool thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020062 if (!pid)
63 return thread ? current : current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020065 p = find_task_by_vpid(pid);
66 if (!p || p == current)
67 return p;
68 if (thread)
69 return same_thread_group(p, current) ? p : NULL;
70 if (p == current)
71 return p;
72 return has_group_leader_pid(p) ? p : NULL;
73}
74
75static struct task_struct *__get_task_for_clock(const clockid_t clock,
76 bool getref)
77{
78 const bool thread = !!CPUCLOCK_PERTHREAD(clock);
79 const pid_t pid = CPUCLOCK_PID(clock);
80 struct task_struct *p;
81
82 if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
83 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020085 rcu_read_lock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020086 p = lookup_task(pid, thread);
87 if (p && getref)
88 get_task_struct(p);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020089 rcu_read_unlock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020090 return p;
91}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020093static inline struct task_struct *get_task_for_clock(const clockid_t clock)
94{
95 return __get_task_for_clock(clock, true);
96}
97
98static inline int validate_clock_permissions(const clockid_t clock)
99{
100 return __get_task_for_clock(clock, false) ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Update expiry time from increment, and increase overrun count,
105 * given the current clock sample.
106 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100107static void bump_cpu_timer(struct k_itimer *timer, u64 now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int i;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100110 u64 delta, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Thomas Gleixner16118792019-01-11 14:33:17 +0100112 if (!timer->it_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return;
114
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000115 if (now < timer->it.cpu.expires)
116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Thomas Gleixner16118792019-01-11 14:33:17 +0100118 incr = timer->it_interval;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000119 delta = now + incr - timer->it.cpu.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000121 /* Don't use (incr*2 < delta), incr*2 might overflow. */
122 for (i = 0; incr < delta - incr; i++)
123 incr = incr << 1;
124
125 for (; i >= 0; incr >>= 1, i--) {
126 if (delta < incr)
127 continue;
128
129 timer->it.cpu.expires += incr;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200130 timer->it_overrun += 1LL << i;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000131 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133}
134
Thomas Gleixner001f7972019-08-21 21:09:13 +0200135static inline bool expiry_cache_is_zero(const u64 *ec)
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200136{
Thomas Gleixner001f7972019-08-21 21:09:13 +0200137 return !(ec[CPUCLOCK_PROF] | ec[CPUCLOCK_VIRT] | ec[CPUCLOCK_SCHED]);
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200138}
139
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000140static int
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700141posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200143 int error = validate_clock_permissions(which_clock);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!error) {
146 tp->tv_sec = 0;
147 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
148 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
149 /*
150 * If sched_clock is using a cycle counter, we
151 * don't have any idea of its true resolution
152 * exported, but it is much more than 1s/HZ.
153 */
154 tp->tv_nsec = 1;
155 }
156 }
157 return error;
158}
159
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000160static int
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200161posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200163 int error = validate_clock_permissions(clock);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * You can never reset a CPU clock, but we check for other errors
167 * in the call before failing with EPERM.
168 */
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200169 return error ? : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200173 * Sample a per-thread clock for the given task. clkid is validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200175static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200177 u64 utime, stime;
178
179 if (clkid == CPUCLOCK_SCHED)
180 return task_sched_runtime(p);
181
182 task_cputime(p, &utime, &stime);
183
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200184 switch (clkid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 case CPUCLOCK_PROF:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200186 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 case CPUCLOCK_VIRT:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200188 return utime;
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200189 default:
190 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Thomas Gleixnerb0d524f2019-08-21 21:09:12 +0200195static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
196{
197 samples[CPUCLOCK_PROF] = stime + utime;
198 samples[CPUCLOCK_VIRT] = utime;
199 samples[CPUCLOCK_SCHED] = rtime;
200}
201
202static void task_sample_cputime(struct task_struct *p, u64 *samples)
203{
204 u64 stime, utime;
205
206 task_cputime(p, &utime, &stime);
207 store_samples(samples, stime, utime, p->se.sum_exec_runtime);
208}
209
210static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
211 u64 *samples)
212{
213 u64 stime, utime, rtime;
214
215 utime = atomic64_read(&at->utime);
216 stime = atomic64_read(&at->stime);
217 rtime = atomic64_read(&at->sum_exec_runtime);
218 store_samples(samples, stime, utime, rtime);
219}
220
Jason Low10180162015-04-28 13:00:22 -0700221/*
222 * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
223 * to avoid race conditions with concurrent updates to cputime.
224 */
225static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100226{
Jason Low10180162015-04-28 13:00:22 -0700227 u64 curr_cputime;
228retry:
229 curr_cputime = atomic64_read(cputime);
230 if (sum_cputime > curr_cputime) {
231 if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
232 goto retry;
233 }
234}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100235
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100236static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum)
Jason Low10180162015-04-28 13:00:22 -0700237{
Jason Low71107442015-04-28 13:00:24 -0700238 __update_gt_cputime(&cputime_atomic->utime, sum->utime);
239 __update_gt_cputime(&cputime_atomic->stime, sum->stime);
240 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
Jason Low10180162015-04-28 13:00:22 -0700241}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100242
Jason Low71107442015-04-28 13:00:24 -0700243/* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100244static inline void sample_cputime_atomic(struct task_cputime *times,
Jason Low71107442015-04-28 13:00:24 -0700245 struct task_cputime_atomic *atomic_times)
Jason Low10180162015-04-28 13:00:22 -0700246{
Jason Low71107442015-04-28 13:00:24 -0700247 times->utime = atomic64_read(&atomic_times->utime);
248 times->stime = atomic64_read(&atomic_times->stime);
249 times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100250}
251
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200252/**
253 * thread_group_sample_cputime - Sample cputime for a given task
254 * @tsk: Task for which cputime needs to be started
255 * @iimes: Storage for time samples
256 *
257 * Called from sys_getitimer() to calculate the expiry time of an active
258 * timer. That means group cputime accounting is already active. Called
259 * with task sighand lock held.
260 *
261 * Updates @times with an uptodate sample of the thread group cputimes.
262 */
263void thread_group_sample_cputime(struct task_struct *tsk,
264 struct task_cputime *times)
265{
266 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
267
268 WARN_ON_ONCE(!cputimer->running);
269
270 sample_cputime_atomic(times, &cputimer->cputime_atomic);
271}
272
Thomas Gleixnerc506bef42019-08-21 21:08:54 +0200273/**
274 * thread_group_start_cputime - Start cputime and return a sample
275 * @tsk: Task for which cputime needs to be started
276 * @iimes: Storage for time samples
277 *
278 * The thread group cputime accouting is avoided when there are no posix
279 * CPU timers armed. Before starting a timer it's required to check whether
280 * the time accounting is active. If not, a full update of the atomic
281 * accounting store needs to be done and the accounting enabled.
282 *
283 * Updates @times with an uptodate sample of the thread group cputimes.
284 */
285static void
286thread_group_start_cputime(struct task_struct *tsk, struct task_cputime *times)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100287{
288 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100289 struct task_cputime sum;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100290
Jason Low10180162015-04-28 13:00:22 -0700291 /* Check if cputimer isn't running. This is accessed without locking. */
292 if (!READ_ONCE(cputimer->running)) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100293 /*
294 * The POSIX timer interface allows for absolute time expiry
295 * values through the TIMER_ABSTIME flag, therefore we have
Jason Low10180162015-04-28 13:00:22 -0700296 * to synchronize the timer to the clock every time we start it.
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100297 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100298 thread_group_cputime(tsk, &sum);
Jason Low71107442015-04-28 13:00:24 -0700299 update_gt_cputime(&cputimer->cputime_atomic, &sum);
Jason Low10180162015-04-28 13:00:22 -0700300
301 /*
302 * We're setting cputimer->running without a lock. Ensure
303 * this only gets written to in one operation. We set
304 * running after update_gt_cputime() as a small optimization,
305 * but barriers are not required because update_gt_cputime()
306 * can handle concurrent updates.
307 */
Jason Lowd5c373e2015-10-14 12:07:55 -0700308 WRITE_ONCE(cputimer->running, true);
Jason Low10180162015-04-28 13:00:22 -0700309 }
Jason Low71107442015-04-28 13:00:24 -0700310 sample_cputime_atomic(times, &cputimer->cputime_atomic);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200314 * Sample a process (thread group) clock for the given task clkid. If the
315 * group's cputime accounting is already enabled, read the atomic
316 * store. Otherwise a full update is required. Task's sighand lock must be
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200317 * held to protect the task traversal on a full update. clkid is already
318 * validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200320static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
321 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200323 struct thread_group_cputimer *cputimer = &p->signal->cputimer;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100324 struct task_cputime cputime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700325
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200326 if (!READ_ONCE(cputimer->running)) {
327 if (start)
328 thread_group_start_cputime(p, &cputime);
329 else
330 thread_group_cputime(p, &cputime);
331 } else {
332 sample_cputime_atomic(&cputime, &cputimer->cputime_atomic);
333 }
334
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200335 switch (clkid) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700336 case CPUCLOCK_PROF:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200337 return cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700338 case CPUCLOCK_VIRT:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200339 return cputime.utime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700340 case CPUCLOCK_SCHED:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200341 return cputime.sum_exec_runtime;
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200342 default:
343 WARN_ON_ONCE(1);
Frank Mayharbb34d922008-09-12 09:54:39 -0700344 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200348static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200349{
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200350 const clockid_t clkid = CPUCLOCK_WHICH(clock);
351 struct task_struct *tsk;
352 u64 t;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200353
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200354 tsk = get_task_for_clock(clock);
355 if (!tsk)
356 return -EINVAL;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200357
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200358 if (CPUCLOCK_PERTHREAD(clock))
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200359 t = cpu_clock_sample(clkid, tsk);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200360 else
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200361 t = cpu_clock_sample_group(clkid, tsk, false);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200362 put_task_struct(tsk);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200363
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200364 *tp = ns_to_timespec64(t);
365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800370 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
371 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000373static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200375 struct task_struct *p = get_task_for_clock(new_timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200377 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -EINVAL;
379
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200380 new_timer->kclock = &clock_posix_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 new_timer->it.cpu.task = p;
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/*
387 * Clean up a CPU-clock timer that is about to be destroyed.
388 * This is called from timer deletion with the timer already locked.
389 * If we return TIMER_RETRY, it's necessary to release the timer's lock
390 * and try again. (This happens when the timer is in the middle of firing.)
391 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000392static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Oleg Nesterov108150e2005-10-23 20:25:39 +0400394 int ret = 0;
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200395 unsigned long flags;
396 struct sighand_struct *sighand;
397 struct task_struct *p = timer->it.cpu.task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Thomas Gleixner692117c2019-08-19 16:31:46 +0200399 if (WARN_ON_ONCE(!p))
400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200402 /*
403 * Protect against sighand release/switch in exit/exec and process/
404 * thread timer list entry concurrent read/writes.
405 */
406 sighand = lock_task_sighand(p, &flags);
407 if (unlikely(sighand == NULL)) {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200408 /*
409 * We raced with the reaping of the task.
410 * The deletion should have cleared us off the list.
411 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +0200412 WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200413 } else {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200414 if (timer->it.cpu.firing)
415 ret = TIMER_RETRY;
416 else
417 list_del(&timer->it.cpu.entry);
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200418
419 unlock_task_sighand(p, &flags);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400420 }
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200421
422 if (!ret)
423 put_task_struct(p);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400424
425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200428static void cleanup_timers_list(struct list_head *head)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000429{
430 struct cpu_timer_list *timer, *next;
431
Frederic Weisbeckera0b20622013-06-28 00:06:43 +0000432 list_for_each_entry_safe(timer, next, head, entry)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000433 list_del_init(&timer->entry);
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
Thomas Gleixner7cb9a942019-08-19 16:31:45 +0200437 * Clean out CPU timers which are still armed when a thread exits. The
438 * timers are only removed from the list. No other updates are done. The
439 * corresponding posix timers are still accessible, but cannot be rearmed.
440 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * This must be called with the siglock held.
442 */
Thomas Gleixner2b699422019-08-21 21:09:04 +0200443static void cleanup_timers(struct posix_cputimers *pct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200445 cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_PROF]);
446 cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_VIRT]);
447 cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_SCHED]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * These are both called with the siglock held, when the current thread
452 * is being reaped. When the final (leader) thread in the group is reaped,
453 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
454 */
455void posix_cpu_timers_exit(struct task_struct *tsk)
456{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200457 cleanup_timers(&tsk->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459void posix_cpu_timers_exit_group(struct task_struct *tsk)
460{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200461 cleanup_timers(&tsk->signal->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100464static inline int expires_gt(u64 expires, u64 new_exp)
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200465{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100466 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
470 * Insert the timer on the appropriate list before any timers that
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200471 * expire later. This must be called with the sighand lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800473static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200475 struct cpu_timer_list *const nt = &timer->it.cpu;
476 int clkidx = CPUCLOCK_WHICH(timer->it_clock);
477 u64 *cpuexp, newexp = timer->it.cpu.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct task_struct *p = timer->it.cpu.task;
479 struct list_head *head, *listpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800482 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200483 head = p->posix_cputimers.cpu_timers + clkidx;
484 cpuexp = p->posix_cputimers.expiries + clkidx;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800485 } else {
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200486 head = p->signal->posix_cputimers.cpu_timers + clkidx;
487 cpuexp = p->signal->posix_cputimers.expiries + clkidx;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 listpos = head;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800491 list_for_each_entry(next, head, entry) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000492 if (nt->expires < next->expires)
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800493 break;
494 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 list_add(&nt->entry, listpos);
497
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200498 if (listpos != head)
499 return;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800500
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200501 /*
502 * We are the new earliest-expiring POSIX 1.b timer, hence
503 * need to update expiration cache. Take into account that
504 * for process timers we share expiration cache with itimers
505 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
506 */
507 if (expires_gt(*cpuexp, newexp))
508 *cpuexp = newexp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200510 if (CPUCLOCK_PERTHREAD(timer->it_clock))
511 tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
512 else
513 tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/*
517 * The timer is locked, fire it and arrange for its reload.
518 */
519static void cpu_timer_fire(struct k_itimer *timer)
520{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800521 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
522 /*
523 * User don't want any signal.
524 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000525 timer->it.cpu.expires = 0;
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800526 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * This a special case for clock_nanosleep,
529 * not a normal timer from sys_timer_create.
530 */
531 wake_up_process(timer->it_process);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000532 timer->it.cpu.expires = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100533 } else if (!timer->it_interval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * One-shot timer. Clear it as soon as it's fired.
536 */
537 posix_timer_event(timer, 0);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000538 timer->it.cpu.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
540 /*
541 * The signal did not get queued because the signal
542 * was ignored, so we won't get any callback to
543 * reload the timer. But we need to keep it
544 * ticking in case the signal is deliverable next time.
545 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200546 posix_cpu_timer_rearm(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200547 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549}
550
551/*
552 * Guts of sys_timer_settime for CPU timers.
553 * This is called with the timer locked and interrupts disabled.
554 * If we return TIMER_RETRY, it's necessary to release the timer's lock
555 * and try again. (This happens when the timer is in the middle of firing.)
556 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200557static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700558 struct itimerspec64 *new, struct itimerspec64 *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200560 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100561 u64 old_expires, new_expires, old_incr, val;
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200562 struct task_struct *p = timer->it.cpu.task;
563 struct sighand_struct *sighand;
564 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int ret;
566
Thomas Gleixner692117c2019-08-19 16:31:46 +0200567 if (WARN_ON_ONCE(!p))
568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Thomas Gleixner098b0e02017-06-20 17:37:36 +0200570 /*
571 * Use the to_ktime conversion because that clamps the maximum
572 * value to KTIME_MAX and avoid multiplication overflows.
573 */
574 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /*
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200577 * Protect against sighand release/switch in exit/exec and p->cpu_timers
578 * and p->signal->cpu_timers read/write in arm_timer()
579 */
580 sighand = lock_task_sighand(p, &flags);
581 /*
582 * If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * longer get any information about it at all.
584 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200585 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -ESRCH;
587 }
588
589 /*
590 * Disarm any old timer after extracting its expiry time.
591 */
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400592
593 ret = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100594 old_incr = timer->it_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400596 if (unlikely(timer->it.cpu.firing)) {
597 timer->it.cpu.firing = -1;
598 ret = TIMER_RETRY;
599 } else
600 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
603 * We need to sample the current value to convert the new
604 * value from to relative and absolute, and to convert the
605 * old value from absolute to relative. To set a process
606 * timer, we need a sample to balance the thread expiry
607 * times (in arm_timer). With an absolute time, we must
608 * check if it's already passed. In short, we need a sample.
609 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200610 if (CPUCLOCK_PERTHREAD(timer->it_clock))
611 val = cpu_clock_sample(clkid, p);
612 else
613 val = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (old) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000616 if (old_expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 old->it_value.tv_sec = 0;
618 old->it_value.tv_nsec = 0;
619 } else {
620 /*
621 * Update the timer in case it has
622 * overrun already. If it has,
623 * we'll report it as having overrun
624 * and with the next reloaded timer
625 * already ticking, though we are
626 * swallowing that pending
627 * notification here to install the
628 * new setting.
629 */
630 bump_cpu_timer(timer, val);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000631 if (val < timer->it.cpu.expires) {
632 old_expires = timer->it.cpu.expires - val;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700633 old->it_value = ns_to_timespec64(old_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else {
635 old->it_value.tv_nsec = 1;
636 old->it_value.tv_sec = 0;
637 }
638 }
639 }
640
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400641 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /*
643 * We are colliding with the timer actually firing.
644 * Punt after filling in the timer's old value, and
645 * disable this firing since we are already reporting
646 * it as an overrun (thanks to bump_cpu_timer above).
647 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200648 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto out;
650 }
651
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200652 if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000653 new_expires += val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
656 /*
657 * Install the new expiry time (or zero).
658 * For a timer with no notification action, we don't actually
659 * arm the timer (we'll just fake it for timer_gettime).
660 */
661 timer->it.cpu.expires = new_expires;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000662 if (new_expires != 0 && val < new_expires) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800663 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200666 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * Install the new reload setting, and
669 * set up the signal and overrun bookkeeping.
670 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100671 timer->it_interval = timespec64_to_ktime(new->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
674 * This acts as a modification timestamp for the timer,
675 * so any automatic reload attempt will punt on seeing
676 * that we have reset the timer manually.
677 */
678 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
679 ~REQUEUE_PENDING;
680 timer->it_overrun_last = 0;
681 timer->it_overrun = -1;
682
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000683 if (new_expires != 0 && !(val < new_expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * The designated time already passed, so we notify
686 * immediately, even if the thread never runs to
687 * accumulate more time on this clock.
688 */
689 cpu_timer_fire(timer);
690 }
691
692 ret = 0;
693 out:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100694 if (old)
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700695 old->it_interval = ns_to_timespec64(old_incr);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return ret;
698}
699
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700700static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Thomas Gleixner99093c52019-08-21 21:08:57 +0200702 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct task_struct *p = timer->it.cpu.task;
Thomas Gleixner692117c2019-08-19 16:31:46 +0200704 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Thomas Gleixner692117c2019-08-19 16:31:46 +0200706 if (WARN_ON_ONCE(!p))
707 return;
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /*
710 * Easy part: convert the reload time.
711 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100712 itp->it_interval = ktime_to_timespec64(timer->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Thomas Gleixnereabdec02017-05-30 23:15:51 +0200714 if (!timer->it.cpu.expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Sample the clock to take the difference with the expiry time.
719 */
720 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200721 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200723 struct sighand_struct *sighand;
724 unsigned long flags;
725
726 /*
727 * Protect against sighand release/switch in exit/exec and
728 * also make timer sampling safe if it ends up calling
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100729 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200730 */
731 sighand = lock_task_sighand(p, &flags);
732 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * The process has been reaped.
735 * We can't even collect a sample any more.
736 * Call the timer disarmed, nothing else to do.
737 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000738 timer->it.cpu.expires = 0;
Alexey Dobriyan2c13ce82016-07-08 01:39:11 +0300739 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200741 now = cpu_clock_sample_group(clkid, p, false);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200742 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000746 if (now < timer->it.cpu.expires) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700747 itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 /*
750 * The timer should have expired already, but the firing
751 * hasn't taken place yet. Say it's just about to expire.
752 */
753 itp->it_value.tv_nsec = 1;
754 itp->it_value.tv_sec = 0;
755 }
756}
757
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000758static unsigned long long
759check_timers_list(struct list_head *timers,
760 struct list_head *firing,
761 unsigned long long curr)
762{
763 int maxfire = 20;
764
765 while (!list_empty(timers)) {
766 struct cpu_timer_list *t;
767
768 t = list_first_entry(timers, struct cpu_timer_list, entry);
769
770 if (!--maxfire || curr < t->expires)
771 return t->expires;
772
773 t->firing = 1;
774 list_move_tail(&t->entry, firing);
775 }
776
777 return 0;
778}
779
Juri Lelli34be3932017-12-12 12:10:24 +0100780static inline void check_dl_overrun(struct task_struct *tsk)
781{
782 if (tsk->dl.dl_overrun) {
783 tsk->dl.dl_overrun = 0;
784 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
785 }
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{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200796 struct list_head *timers = tsk->posix_cputimers.cpu_timers;
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200797 u64 stime, utime, *expires = tsk->posix_cputimers.expiries;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800798 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Juri Lelli34be3932017-12-12 12:10:24 +0100800 if (dl_task(tsk))
801 check_dl_overrun(tsk);
802
Jason Low934715a2015-10-14 12:07:54 -0700803 /*
Thomas Gleixner001f7972019-08-21 21:09:13 +0200804 * If the expiry cache is zero, then there are no active per thread
805 * CPU timers.
Jason Low934715a2015-10-14 12:07:54 -0700806 */
Thomas Gleixner001f7972019-08-21 21:09:13 +0200807 if (expiry_cache_is_zero(tsk->posix_cputimers.expiries))
Jason Low934715a2015-10-14 12:07:54 -0700808 return;
809
Thomas Gleixner0476ff22019-08-21 21:09:02 +0200810 task_cputime(tsk, &utime, &stime);
811
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200812 *expires++ = check_timers_list(timers, firing, utime + stime);
813 *expires++ = check_timers_list(++timers, firing, utime);
814 *expires = check_timers_list(++timers, firing, tsk->se.sum_exec_runtime);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100815
816 /*
817 * Check for the special case thread timers.
818 */
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200819 soft = task_rlimit(tsk, RLIMIT_RTTIME);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800820 if (soft != RLIM_INFINITY) {
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200821 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100822
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100823 if (hard != RLIM_INFINITY &&
824 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100825 /*
826 * At the hard limit, we just die.
827 * No need to calculate anything else now.
828 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200829 if (print_fatal_signals) {
830 pr_info("CPU Watchdog Timeout (hard): %s[%d]\n",
831 tsk->comm, task_pid_nr(tsk));
832 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100833 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
834 return;
835 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800836 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100837 /*
838 * At the soft limit, send a SIGXCPU every second.
839 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800840 if (soft < hard) {
841 soft += USEC_PER_SEC;
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200842 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur =
843 soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100844 }
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200845 if (print_fatal_signals) {
846 pr_info("RT Watchdog Timeout (soft): %s[%d]\n",
847 tsk->comm, task_pid_nr(tsk));
848 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100849 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
850 }
851 }
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200852
Thomas Gleixner001f7972019-08-21 21:09:13 +0200853 if (expiry_cache_is_zero(tsk->posix_cputimers.expiries))
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200854 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Jason Low10180162015-04-28 13:00:22 -0700857static inline void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100858{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800859 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100860
Jason Low10180162015-04-28 13:00:22 -0700861 /* Turn off cputimer->running. This is done without locking. */
Jason Lowd5c373e2015-10-14 12:07:55 -0700862 WRITE_ONCE(cputimer->running, false);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200863 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100864}
865
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200866static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100867 u64 *expires, u64 cur_time, int signo)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200868{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100869 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200870 return;
871
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100872 if (cur_time >= it->expires) {
873 if (it->incr)
Martin Schwidefsky64861632011-12-15 14:56:09 +0100874 it->expires += it->incr;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100875 else
Martin Schwidefsky64861632011-12-15 14:56:09 +0100876 it->expires = 0;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200877
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800878 trace_itimer_expire(signo == SIGPROF ?
879 ITIMER_PROF : ITIMER_VIRTUAL,
Eric W. Biederman6883f812017-06-04 04:32:13 -0500880 task_tgid(tsk), cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200881 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
882 }
883
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100884 if (it->expires && (!*expires || it->expires < *expires))
885 *expires = it->expires;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * Check for any per-thread CPU timers that have fired and move them
890 * off the tsk->*_timers list onto the firing list. Per-thread timers
891 * have already been taken off.
892 */
893static void check_process_timers(struct task_struct *tsk,
894 struct list_head *firing)
895{
896 struct signal_struct *const sig = tsk->signal;
Thomas Gleixner2b699422019-08-21 21:09:04 +0200897 struct list_head *timers = sig->posix_cputimers.cpu_timers;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100898 u64 utime, ptime, virt_expires, prof_expires;
899 u64 sum_sched_runtime, sched_expires;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100900 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800901 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /*
Jason Low934715a2015-10-14 12:07:54 -0700904 * If cputimer is not running, then there are no active
905 * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
906 */
907 if (!READ_ONCE(tsk->signal->cputimer.running))
908 return;
909
Thomas Gleixnera3249562019-08-21 21:08:53 +0200910 /*
Jason Lowc8d75aa2015-10-14 12:07:56 -0700911 * Signify that a thread is checking for process timers.
912 * Write access to this field is protected by the sighand lock.
913 */
914 sig->cputimer.checking_timer = true;
915
Jason Low934715a2015-10-14 12:07:54 -0700916 /*
Thomas Gleixnera3249562019-08-21 21:08:53 +0200917 * Collect the current process totals. Group accounting is active
918 * so the sample can be taken directly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Thomas Gleixnera3249562019-08-21 21:08:53 +0200920 sample_cputime_atomic(&cputime, &sig->cputimer.cputime_atomic);
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);
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200936 soft = task_rlimit(tsk, RLIMIT_CPU);
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);
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200939 unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100940 u64 x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800941 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * At the hard limit, we just die.
944 * No need to calculate anything else now.
945 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200946 if (print_fatal_signals) {
947 pr_info("RT Watchdog Timeout (hard): %s[%d]\n",
948 tsk->comm, task_pid_nr(tsk));
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
951 return;
952 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800953 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * At the soft limit, send a SIGXCPU every second.
956 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200957 if (print_fatal_signals) {
958 pr_info("CPU Watchdog Timeout (soft): %s[%d]\n",
959 tsk->comm, task_pid_nr(tsk));
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800962 if (soft < hard) {
963 soft++;
964 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 }
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100967 x = soft * NSEC_PER_SEC;
968 if (!prof_expires || x < prof_expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 prof_expires = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200972 sig->posix_cputimers.expiries[CPUCLOCK_PROF] = prof_expires;
973 sig->posix_cputimers.expiries[CPUCLOCK_VIRT] = virt_expires;
974 sig->posix_cputimers.expiries[CPUCLOCK_SCHED] = sched_expires;
975
Thomas Gleixner001f7972019-08-21 21:09:13 +0200976 if (expiry_cache_is_zero(sig->posix_cputimers.expiries))
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -0700977 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{
Thomas Gleixnerda020ce2019-08-21 21:08:58 +0200988 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Thomas Gleixner692117c2019-08-19 16:31:46 +0200989 struct task_struct *p = timer->it.cpu.task;
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200990 struct sighand_struct *sighand;
991 unsigned long flags;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100992 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Thomas Gleixner692117c2019-08-19 16:31:46 +0200994 if (WARN_ON_ONCE(!p))
995 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
998 * Fetch the current sample and update the timer's expiry time.
999 */
1000 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +02001001 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 bump_cpu_timer(timer, now);
Frederic Weisbecker724a3712013-10-10 16:55:57 +02001003 if (unlikely(p->exit_state))
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001004 return;
Frederic Weisbecker724a3712013-10-10 16:55:57 +02001005
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001006 /* Protect timer list r/w in arm_timer() */
1007 sighand = lock_task_sighand(p, &flags);
1008 if (!sighand)
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001009 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001011 /*
1012 * Protect arm_timer() and timer sampling in case of call to
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001013 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001014 */
1015 sighand = lock_task_sighand(p, &flags);
1016 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * The process has been reaped.
1019 * We can't even collect a sample any more.
1020 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001021 timer->it.cpu.expires = 0;
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001024 /* If the process is dying, no need to rearm */
1025 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +02001027 now = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 bump_cpu_timer(timer, now);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001029 /* Leave the sighand locked for the call below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 /*
1033 * Now re-arm for the new expiry time.
1034 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001035 arm_timer(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001036unlock:
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001037 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Frank Mayharf06febc2008-09-12 09:54:39 -07001040/**
Thomas Gleixner001f7972019-08-21 21:09:13 +02001041 * task_cputimers_expired - Compare two task_cputime entities.
Frank Mayharf06febc2008-09-12 09:54:39 -07001042 *
Thomas Gleixner001f7972019-08-21 21:09:13 +02001043 * @samples: Array of current samples for the CPUCLOCK clocks
1044 * @expiries: Array of expiry values for the CPUCLOCK clocks
Frank Mayharf06febc2008-09-12 09:54:39 -07001045 *
Thomas Gleixner001f7972019-08-21 21:09:13 +02001046 * Returns true if any mmember of @samples is greater than the corresponding
1047 * member of @expiries if that member is non zero. False otherwise
Frank Mayharf06febc2008-09-12 09:54:39 -07001048 */
Thomas Gleixner001f7972019-08-21 21:09:13 +02001049static inline bool task_cputimers_expired(const u64 *sample, const u64 *expiries)
Frank Mayharf06febc2008-09-12 09:54:39 -07001050{
Thomas Gleixner001f7972019-08-21 21:09:13 +02001051 int i;
1052
1053 for (i = 0; i < CPUCLOCK_MAX; i++) {
1054 if (expiries[i] && sample[i] >= expiries[i])
1055 return true;
1056 }
1057 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001058}
1059
1060/**
1061 * fastpath_timer_check - POSIX CPU timers fast path.
1062 *
1063 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001064 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001065 * Check the task and thread group timers. If both are zero (there are no
1066 * timers set) return false. Otherwise snapshot the task and thread group
1067 * timers and compare them with the corresponding expiration times. Return
1068 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001069 */
Thomas Gleixner001f7972019-08-21 21:09:13 +02001070static inline bool fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001071{
Thomas Gleixner001f7972019-08-21 21:09:13 +02001072 u64 *expiries = tsk->posix_cputimers.expiries;
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001073 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001074
Thomas Gleixner001f7972019-08-21 21:09:13 +02001075 if (!expiry_cache_is_zero(expiries)) {
1076 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001077
Thomas Gleixner001f7972019-08-21 21:09:13 +02001078 task_sample_cputime(tsk, samples);
1079 if (task_cputimers_expired(samples, expiries))
1080 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001081 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001082
1083 sig = tsk->signal;
Jason Lowc8d75aa2015-10-14 12:07:56 -07001084 /*
1085 * Check if thread group timers expired when the cputimer is
1086 * running and no other thread in the group is already checking
1087 * for thread group cputimers. These fields are read without the
1088 * sighand lock. However, this is fine because this is meant to
1089 * be a fastpath heuristic to determine whether we should try to
1090 * acquire the sighand lock to check/handle timers.
1091 *
1092 * In the worst case scenario, if 'running' or 'checking_timer' gets
1093 * set but the current thread doesn't see the change yet, we'll wait
1094 * until the next thread in the group gets a scheduler interrupt to
1095 * handle the timer. This isn't an issue in practice because these
1096 * types of delays with signals actually getting sent are expected.
1097 */
1098 if (READ_ONCE(sig->cputimer.running) &&
1099 !READ_ONCE(sig->cputimer.checking_timer)) {
Thomas Gleixner001f7972019-08-21 21:09:13 +02001100 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001101
Thomas Gleixner001f7972019-08-21 21:09:13 +02001102 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
1103 samples);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001104
Thomas Gleixner001f7972019-08-21 21:09:13 +02001105 if (task_cputimers_expired(samples,
1106 sig->posix_cputimers.expiries))
1107 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001108 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001109
Juri Lelli34be3932017-12-12 12:10:24 +01001110 if (dl_task(tsk) && tsk->dl.dl_overrun)
Thomas Gleixner001f7972019-08-21 21:09:13 +02001111 return true;
Juri Lelli34be3932017-12-12 12:10:24 +01001112
Thomas Gleixner001f7972019-08-21 21:09:13 +02001113 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * This is called from the timer interrupt handler. The irq handler has
1118 * already updated our counts. We need to check if any timers fire now.
1119 * Interrupts are disabled.
1120 */
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001121void run_posix_cpu_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001123 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001125 unsigned long flags;
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001126 LIST_HEAD(firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Frederic Weisbeckera6968222017-11-06 16:01:28 +01001128 lockdep_assert_irqs_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001131 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001132 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001134 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001135 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001136
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001137 if (!lock_task_sighand(tsk, &flags))
1138 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001139 /*
1140 * Here we take off tsk->signal->cpu_timers[N] and
1141 * tsk->cpu_timers[N] all the timers that are firing, and
1142 * put them on the firing list.
1143 */
1144 check_thread_timers(tsk, &firing);
Jason Low934715a2015-10-14 12:07:54 -07001145
1146 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Frank Mayharbb34d922008-09-12 09:54:39 -07001148 /*
1149 * We must release these locks before taking any timer's lock.
1150 * There is a potential race with timer deletion here, as the
1151 * siglock now protects our private firing list. We have set
1152 * the firing flag in each timer, so that a deletion attempt
1153 * that gets the timer lock before we do will give it up and
1154 * spin until we've taken care of that timer below.
1155 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001156 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /*
1159 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001160 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * each timer's lock before clearing its firing flag, so no
1162 * timer call will interfere.
1163 */
1164 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001165 int cpu_firing;
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_lock(&timer->it_lock);
1168 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001169 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 timer->it.cpu.firing = 0;
1171 /*
1172 * The firing flag is -1 if we collided with a reset
1173 * of the timer, which already reported this
1174 * almost-firing as an overrun. So don't generate an event.
1175 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001176 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_unlock(&timer->it_lock);
1179 }
1180}
1181
1182/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001183 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001184 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 */
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001186void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001187 u64 *newval, u64 *oldval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001189 u64 now, *expiry = tsk->signal->posix_cputimers.expiries + clkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001191 if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
Thomas Gleixner692117c2019-08-19 16:31:46 +02001192 return;
1193
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001194 now = cpu_clock_sample_group(clkid, tsk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Thomas Gleixner5405d002019-08-21 21:08:59 +02001196 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001197 /*
1198 * We are setting itimer. The *oldval is absolute and we update
1199 * it to be relative, *newval argument is relative and we update
1200 * it to be absolute.
1201 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001202 if (*oldval) {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001203 if (*oldval <= now) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Just about to fire. */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001205 *oldval = TICK_NSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 } else {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001207 *oldval -= now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 }
1210
Martin Schwidefsky64861632011-12-15 14:56:09 +01001211 if (!*newval)
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001212 return;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001213 *newval += now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 /*
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001217 * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
1218 * expiry cache is also used by RLIMIT_CPU!.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001220 if (expires_gt(*expiry, *newval))
1221 *expiry = *newval;
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001222
1223 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Toyo Abee4b76552006-09-29 02:00:29 -07001226static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001227 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Al Viro86a9c442017-06-07 09:42:26 +01001229 struct itimerspec64 it;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001230 struct k_itimer timer;
1231 u64 expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 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;
Al Viroedbeda42017-06-07 09:42:31 +01001245 struct restart_block *restart;
Toyo Abee4b76552006-09-29 02:00:29 -07001246
Al Viroedbeda42017-06-07 09:42:31 +01001247 memset(&it, 0, sizeof(it));
Al Viro86a9c442017-06-07 09:42:26 +01001248 it.it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 spin_lock_irq(&timer.it_lock);
Al Viro86a9c442017-06-07 09:42:26 +01001251 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (error) {
1253 spin_unlock_irq(&timer.it_lock);
1254 return error;
1255 }
1256
1257 while (!signal_pending(current)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001258 if (timer.it.cpu.expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001260 * Our timer fired and was reset, below
1261 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001263 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 spin_unlock_irq(&timer.it_lock);
1265 return 0;
1266 }
1267
1268 /*
1269 * Block until cpu_timer_fire (or a signal) wakes us.
1270 */
1271 __set_current_state(TASK_INTERRUPTIBLE);
1272 spin_unlock_irq(&timer.it_lock);
1273 schedule();
1274 spin_lock_irq(&timer.it_lock);
1275 }
1276
1277 /*
1278 * We were interrupted by a signal.
1279 */
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001280 expires = timer.it.cpu.expires;
Al Viro86a9c442017-06-07 09:42:26 +01001281 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001282 if (!error) {
1283 /*
1284 * Timer is now unarmed, deletion can not fail.
1285 */
1286 posix_cpu_timer_del(&timer);
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 spin_unlock_irq(&timer.it_lock);
1289
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001290 while (error == TIMER_RETRY) {
1291 /*
1292 * We need to handle case when timer was or is in the
1293 * middle of firing. In other cases we already freed
1294 * resources.
1295 */
1296 spin_lock_irq(&timer.it_lock);
1297 error = posix_cpu_timer_del(&timer);
1298 spin_unlock_irq(&timer.it_lock);
1299 }
1300
Al Viro86a9c442017-06-07 09:42:26 +01001301 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * It actually did fire already.
1304 */
1305 return 0;
1306 }
1307
Toyo Abee4b76552006-09-29 02:00:29 -07001308 error = -ERESTART_RESTARTBLOCK;
Al Viro86a9c442017-06-07 09:42:26 +01001309 /*
1310 * Report back to the user the time still remaining.
1311 */
Al Viroedbeda42017-06-07 09:42:31 +01001312 restart = &current->restart_block;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001313 restart->nanosleep.expires = expires;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001314 if (restart->nanosleep.type != TT_NONE)
1315 error = nanosleep_copyout(restart, &it.it_value);
Toyo Abee4b76552006-09-29 02:00:29 -07001316 }
1317
1318 return error;
1319}
1320
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001321static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1322
1323static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001324 const struct timespec64 *rqtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001325{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001326 struct restart_block *restart_block = &current->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001327 int error;
1328
1329 /*
1330 * Diagnose required errors first.
1331 */
1332 if (CPUCLOCK_PERTHREAD(which_clock) &&
1333 (CPUCLOCK_PID(which_clock) == 0 ||
Eric W. Biederman01a21972017-04-13 10:32:16 -05001334 CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
Toyo Abee4b76552006-09-29 02:00:29 -07001335 return -EINVAL;
1336
Al Viro86a9c442017-06-07 09:42:26 +01001337 error = do_cpu_nanosleep(which_clock, flags, rqtp);
Toyo Abee4b76552006-09-29 02:00:29 -07001338
1339 if (error == -ERESTART_RESTARTBLOCK) {
1340
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001341 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001342 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return error;
1348}
1349
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001350static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001352 clockid_t which_clock = restart_block->nanosleep.clockid;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001353 struct timespec64 t;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001354
Deepa Dinamaniad196382017-03-26 12:04:18 -07001355 t = ns_to_timespec64(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001356
Al Viro86a9c442017-06-07 09:42:26 +01001357 return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Nick Desaulniers29f1b2b2017-12-28 22:11:36 -05001360#define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
1361#define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Thomas Gleixnera924b042006-01-09 20:52:27 -08001363static int process_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001364 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1367}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001368static int process_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001369 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1372}
1373static int process_cpu_timer_create(struct k_itimer *timer)
1374{
1375 timer->it_clock = PROCESS_CLOCK;
1376 return posix_cpu_timer_create(timer);
1377}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001378static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001379 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Al Viro99e6c0e2017-06-07 09:42:30 +01001381 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001383static int thread_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001384 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1387}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001388static int thread_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001389 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1392}
1393static int thread_cpu_timer_create(struct k_itimer *timer)
1394{
1395 timer->it_clock = THREAD_CLOCK;
1396 return posix_cpu_timer_create(timer);
1397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001399const struct k_clock clock_posix_cpu = {
Thomas Gleixner19769452011-02-01 13:51:06 +00001400 .clock_getres = posix_cpu_clock_getres,
1401 .clock_set = posix_cpu_clock_set,
1402 .clock_get = posix_cpu_clock_get,
1403 .timer_create = posix_cpu_timer_create,
1404 .nsleep = posix_cpu_nsleep,
Thomas Gleixner19769452011-02-01 13:51:06 +00001405 .timer_set = posix_cpu_timer_set,
1406 .timer_del = posix_cpu_timer_del,
1407 .timer_get = posix_cpu_timer_get,
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +02001408 .timer_rearm = posix_cpu_timer_rearm,
Thomas Gleixner19769452011-02-01 13:51:06 +00001409};
1410
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001411const struct k_clock clock_process = {
1412 .clock_getres = process_cpu_clock_getres,
1413 .clock_get = process_cpu_clock_get,
1414 .timer_create = process_cpu_timer_create,
1415 .nsleep = process_cpu_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001416};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001418const struct k_clock clock_thread = {
1419 .clock_getres = thread_cpu_clock_getres,
1420 .clock_get = thread_cpu_clock_get,
1421 .timer_create = thread_cpu_timer_create,
1422};