blob: 9ac601abc4c4904a4f1853b6bf2d5811adebd174 [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 Gleixnerf37fb0a2017-05-30 23:15:47 +020021static void posix_cpu_timer_rearm(struct k_itimer *timer);
22
Thomas Gleixner3a245c02019-08-21 21:09:06 +020023void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
24{
25 posix_cputimers_init(pct);
26 if (cpu_limit != RLIM_INFINITY)
Thomas Gleixner87dc6442019-08-26 20:22:24 +020027 pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
Thomas Gleixner3a245c02019-08-21 21:09:06 +020028}
29
Frank Mayharf06febc2008-09-12 09:54:39 -070030/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080031 * Called after updating RLIMIT_CPU to run cpu timer and update
Thomas Gleixner87dc6442019-08-26 20:22:24 +020032 * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
33 * necessary. Needs siglock protection since other code may update the
Thomas Gleixner3a245c02019-08-21 21:09:06 +020034 * expiration cache as well.
Frank Mayharf06febc2008-09-12 09:54:39 -070035 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020036void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070037{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010038 u64 nsecs = rlim_new * NSEC_PER_SEC;
Frank Mayharf06febc2008-09-12 09:54:39 -070039
Jiri Slaby5ab46b32009-08-28 14:05:12 +020040 spin_lock_irq(&task->sighand->siglock);
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010041 set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
Jiri Slaby5ab46b32009-08-28 14:05:12 +020042 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070043}
44
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020045/*
46 * Functions for validating access to tasks.
47 */
48static struct task_struct *lookup_task(const pid_t pid, bool thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020052 if (!pid)
53 return thread ? current : current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020055 p = find_task_by_vpid(pid);
56 if (!p || p == current)
57 return p;
58 if (thread)
59 return same_thread_group(p, current) ? p : NULL;
60 if (p == current)
61 return p;
62 return has_group_leader_pid(p) ? p : NULL;
63}
64
65static struct task_struct *__get_task_for_clock(const clockid_t clock,
66 bool getref)
67{
68 const bool thread = !!CPUCLOCK_PERTHREAD(clock);
69 const pid_t pid = CPUCLOCK_PID(clock);
70 struct task_struct *p;
71
72 if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
73 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020075 rcu_read_lock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020076 p = lookup_task(pid, thread);
77 if (p && getref)
78 get_task_struct(p);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020079 rcu_read_unlock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020080 return p;
81}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020083static inline struct task_struct *get_task_for_clock(const clockid_t clock)
84{
85 return __get_task_for_clock(clock, true);
86}
87
88static inline int validate_clock_permissions(const clockid_t clock)
89{
90 return __get_task_for_clock(clock, false) ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * Update expiry time from increment, and increase overrun count,
95 * given the current clock sample.
96 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010097static void bump_cpu_timer(struct k_itimer *timer, u64 now)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int i;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100100 u64 delta, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Gleixner16118792019-01-11 14:33:17 +0100102 if (!timer->it_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return;
104
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000105 if (now < timer->it.cpu.expires)
106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Thomas Gleixner16118792019-01-11 14:33:17 +0100108 incr = timer->it_interval;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000109 delta = now + incr - timer->it.cpu.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000111 /* Don't use (incr*2 < delta), incr*2 might overflow. */
112 for (i = 0; incr < delta - incr; i++)
113 incr = incr << 1;
114
115 for (; i >= 0; incr >>= 1, i--) {
116 if (delta < incr)
117 continue;
118
119 timer->it.cpu.expires += incr;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200120 timer->it_overrun += 1LL << i;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000121 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123}
124
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200125static inline bool expiry_cache_is_zero(const struct posix_cputimers *pct)
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200126{
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200127 return !(pct->bases[CPUCLOCK_PROF].nextevt |
128 pct->bases[CPUCLOCK_VIRT].nextevt |
129 pct->bases[CPUCLOCK_SCHED].nextevt);
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200130}
131
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000132static int
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700133posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200135 int error = validate_clock_permissions(which_clock);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!error) {
138 tp->tv_sec = 0;
139 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
140 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
141 /*
142 * If sched_clock is using a cycle counter, we
143 * don't have any idea of its true resolution
144 * exported, but it is much more than 1s/HZ.
145 */
146 tp->tv_nsec = 1;
147 }
148 }
149 return error;
150}
151
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000152static int
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200153posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200155 int error = validate_clock_permissions(clock);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * You can never reset a CPU clock, but we check for other errors
159 * in the call before failing with EPERM.
160 */
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200161 return error ? : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200165 * Sample a per-thread clock for the given task. clkid is validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200167static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200169 u64 utime, stime;
170
171 if (clkid == CPUCLOCK_SCHED)
172 return task_sched_runtime(p);
173
174 task_cputime(p, &utime, &stime);
175
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200176 switch (clkid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 case CPUCLOCK_PROF:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200178 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 case CPUCLOCK_VIRT:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200180 return utime;
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200181 default:
182 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Thomas Gleixnerb0d524f2019-08-21 21:09:12 +0200187static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
188{
189 samples[CPUCLOCK_PROF] = stime + utime;
190 samples[CPUCLOCK_VIRT] = utime;
191 samples[CPUCLOCK_SCHED] = rtime;
192}
193
194static void task_sample_cputime(struct task_struct *p, u64 *samples)
195{
196 u64 stime, utime;
197
198 task_cputime(p, &utime, &stime);
199 store_samples(samples, stime, utime, p->se.sum_exec_runtime);
200}
201
202static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
203 u64 *samples)
204{
205 u64 stime, utime, rtime;
206
207 utime = atomic64_read(&at->utime);
208 stime = atomic64_read(&at->stime);
209 rtime = atomic64_read(&at->sum_exec_runtime);
210 store_samples(samples, stime, utime, rtime);
211}
212
Jason Low10180162015-04-28 13:00:22 -0700213/*
214 * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
215 * to avoid race conditions with concurrent updates to cputime.
216 */
217static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100218{
Jason Low10180162015-04-28 13:00:22 -0700219 u64 curr_cputime;
220retry:
221 curr_cputime = atomic64_read(cputime);
222 if (sum_cputime > curr_cputime) {
223 if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
224 goto retry;
225 }
226}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100227
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100228static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum)
Jason Low10180162015-04-28 13:00:22 -0700229{
Jason Low71107442015-04-28 13:00:24 -0700230 __update_gt_cputime(&cputime_atomic->utime, sum->utime);
231 __update_gt_cputime(&cputime_atomic->stime, sum->stime);
232 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
Jason Low10180162015-04-28 13:00:22 -0700233}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100234
Jason Low71107442015-04-28 13:00:24 -0700235/* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100236static inline void sample_cputime_atomic(struct task_cputime *times,
Jason Low71107442015-04-28 13:00:24 -0700237 struct task_cputime_atomic *atomic_times)
Jason Low10180162015-04-28 13:00:22 -0700238{
Jason Low71107442015-04-28 13:00:24 -0700239 times->utime = atomic64_read(&atomic_times->utime);
240 times->stime = atomic64_read(&atomic_times->stime);
241 times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100242}
243
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200244/**
245 * thread_group_sample_cputime - Sample cputime for a given task
246 * @tsk: Task for which cputime needs to be started
247 * @iimes: Storage for time samples
248 *
249 * Called from sys_getitimer() to calculate the expiry time of an active
250 * timer. That means group cputime accounting is already active. Called
251 * with task sighand lock held.
252 *
253 * Updates @times with an uptodate sample of the thread group cputimes.
254 */
255void thread_group_sample_cputime(struct task_struct *tsk,
256 struct task_cputime *times)
257{
258 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
259
260 WARN_ON_ONCE(!cputimer->running);
261
262 sample_cputime_atomic(times, &cputimer->cputime_atomic);
263}
264
Thomas Gleixnerc506bef42019-08-21 21:08:54 +0200265/**
266 * thread_group_start_cputime - Start cputime and return a sample
267 * @tsk: Task for which cputime needs to be started
268 * @iimes: Storage for time samples
269 *
270 * The thread group cputime accouting is avoided when there are no posix
271 * CPU timers armed. Before starting a timer it's required to check whether
272 * the time accounting is active. If not, a full update of the atomic
273 * accounting store needs to be done and the accounting enabled.
274 *
275 * Updates @times with an uptodate sample of the thread group cputimes.
276 */
277static void
278thread_group_start_cputime(struct task_struct *tsk, struct task_cputime *times)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100279{
280 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100281 struct task_cputime sum;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100282
Jason Low10180162015-04-28 13:00:22 -0700283 /* Check if cputimer isn't running. This is accessed without locking. */
284 if (!READ_ONCE(cputimer->running)) {
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100285 /*
286 * The POSIX timer interface allows for absolute time expiry
287 * values through the TIMER_ABSTIME flag, therefore we have
Jason Low10180162015-04-28 13:00:22 -0700288 * to synchronize the timer to the clock every time we start it.
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100289 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100290 thread_group_cputime(tsk, &sum);
Jason Low71107442015-04-28 13:00:24 -0700291 update_gt_cputime(&cputimer->cputime_atomic, &sum);
Jason Low10180162015-04-28 13:00:22 -0700292
293 /*
294 * We're setting cputimer->running without a lock. Ensure
295 * this only gets written to in one operation. We set
296 * running after update_gt_cputime() as a small optimization,
297 * but barriers are not required because update_gt_cputime()
298 * can handle concurrent updates.
299 */
Jason Lowd5c373e2015-10-14 12:07:55 -0700300 WRITE_ONCE(cputimer->running, true);
Jason Low10180162015-04-28 13:00:22 -0700301 }
Jason Low71107442015-04-28 13:00:24 -0700302 sample_cputime_atomic(times, &cputimer->cputime_atomic);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200306 * Sample a process (thread group) clock for the given task clkid. If the
307 * group's cputime accounting is already enabled, read the atomic
308 * store. Otherwise a full update is required. Task's sighand lock must be
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200309 * held to protect the task traversal on a full update. clkid is already
310 * validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200312static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
313 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200315 struct thread_group_cputimer *cputimer = &p->signal->cputimer;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100316 struct task_cputime cputime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700317
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200318 if (!READ_ONCE(cputimer->running)) {
319 if (start)
320 thread_group_start_cputime(p, &cputime);
321 else
322 thread_group_cputime(p, &cputime);
323 } else {
324 sample_cputime_atomic(&cputime, &cputimer->cputime_atomic);
325 }
326
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200327 switch (clkid) {
Frank Mayharbb34d922008-09-12 09:54:39 -0700328 case CPUCLOCK_PROF:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200329 return cputime.utime + cputime.stime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700330 case CPUCLOCK_VIRT:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200331 return cputime.utime;
Frank Mayharbb34d922008-09-12 09:54:39 -0700332 case CPUCLOCK_SCHED:
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200333 return cputime.sum_exec_runtime;
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200334 default:
335 WARN_ON_ONCE(1);
Frank Mayharbb34d922008-09-12 09:54:39 -0700336 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200340static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200341{
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200342 const clockid_t clkid = CPUCLOCK_WHICH(clock);
343 struct task_struct *tsk;
344 u64 t;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200345
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200346 tsk = get_task_for_clock(clock);
347 if (!tsk)
348 return -EINVAL;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200349
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200350 if (CPUCLOCK_PERTHREAD(clock))
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200351 t = cpu_clock_sample(clkid, tsk);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200352 else
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200353 t = cpu_clock_sample_group(clkid, tsk, false);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200354 put_task_struct(tsk);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200355
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200356 *tp = ns_to_timespec64(t);
357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800362 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
363 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000365static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200367 struct task_struct *p = get_task_for_clock(new_timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200369 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -EINVAL;
371
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200372 new_timer->kclock = &clock_posix_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 new_timer->it.cpu.task = p;
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/*
379 * Clean up a CPU-clock timer that is about to be destroyed.
380 * This is called from timer deletion with the timer already locked.
381 * If we return TIMER_RETRY, it's necessary to release the timer's lock
382 * and try again. (This happens when the timer is in the middle of firing.)
383 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000384static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Oleg Nesterov108150e2005-10-23 20:25:39 +0400386 int ret = 0;
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200387 unsigned long flags;
388 struct sighand_struct *sighand;
389 struct task_struct *p = timer->it.cpu.task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Thomas Gleixner692117c2019-08-19 16:31:46 +0200391 if (WARN_ON_ONCE(!p))
392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200394 /*
395 * Protect against sighand release/switch in exit/exec and process/
396 * thread timer list entry concurrent read/writes.
397 */
398 sighand = lock_task_sighand(p, &flags);
399 if (unlikely(sighand == NULL)) {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200400 /*
401 * We raced with the reaping of the task.
402 * The deletion should have cleared us off the list.
403 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +0200404 WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200405 } else {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200406 if (timer->it.cpu.firing)
407 ret = TIMER_RETRY;
408 else
409 list_del(&timer->it.cpu.entry);
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200410
411 unlock_task_sighand(p, &flags);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400412 }
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200413
414 if (!ret)
415 put_task_struct(p);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400416
417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200420static void cleanup_timers_list(struct list_head *head)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000421{
422 struct cpu_timer_list *timer, *next;
423
Frederic Weisbeckera0b20622013-06-28 00:06:43 +0000424 list_for_each_entry_safe(timer, next, head, entry)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000425 list_del_init(&timer->entry);
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Thomas Gleixner7cb9a942019-08-19 16:31:45 +0200429 * Clean out CPU timers which are still armed when a thread exits. The
430 * timers are only removed from the list. No other updates are done. The
431 * corresponding posix timers are still accessible, but cannot be rearmed.
432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * This must be called with the siglock held.
434 */
Thomas Gleixner2b699422019-08-21 21:09:04 +0200435static void cleanup_timers(struct posix_cputimers *pct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200437 cleanup_timers_list(&pct->bases[CPUCLOCK_PROF].cpu_timers);
438 cleanup_timers_list(&pct->bases[CPUCLOCK_VIRT].cpu_timers);
439 cleanup_timers_list(&pct->bases[CPUCLOCK_SCHED].cpu_timers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*
443 * These are both called with the siglock held, when the current thread
444 * is being reaped. When the final (leader) thread in the group is reaped,
445 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
446 */
447void posix_cpu_timers_exit(struct task_struct *tsk)
448{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200449 cleanup_timers(&tsk->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451void posix_cpu_timers_exit_group(struct task_struct *tsk)
452{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200453 cleanup_timers(&tsk->signal->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100456static inline int expires_gt(u64 expires, u64 new_exp)
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200457{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100458 return expires == 0 || expires > new_exp;
Stanislaw Gruszkad1e3b6d2009-07-29 12:15:28 +0200459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Insert the timer on the appropriate list before any timers that
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200463 * expire later. This must be called with the sighand lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800465static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200467 struct cpu_timer_list *const nt = &timer->it.cpu;
468 int clkidx = CPUCLOCK_WHICH(timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct task_struct *p = timer->it.cpu.task;
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200470 u64 newexp = timer->it.cpu.expires;
471 struct posix_cputimer_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct list_head *head, *listpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200475 if (CPUCLOCK_PERTHREAD(timer->it_clock))
476 base = p->posix_cputimers.bases + clkidx;
477 else
478 base = p->signal->posix_cputimers.bases + clkidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200480 listpos = head = &base->cpu_timers;
481 list_for_each_entry(next,head, entry) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000482 if (nt->expires < next->expires)
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800483 break;
484 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 list_add(&nt->entry, listpos);
487
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200488 if (listpos != head)
489 return;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800490
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200491 /*
492 * We are the new earliest-expiring POSIX 1.b timer, hence
493 * need to update expiration cache. Take into account that
494 * for process timers we share expiration cache with itimers
495 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
496 */
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200497 if (expires_gt(base->nextevt, newexp))
498 base->nextevt = newexp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200500 if (CPUCLOCK_PERTHREAD(timer->it_clock))
501 tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
502 else
503 tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/*
507 * The timer is locked, fire it and arrange for its reload.
508 */
509static void cpu_timer_fire(struct k_itimer *timer)
510{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800511 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
512 /*
513 * User don't want any signal.
514 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000515 timer->it.cpu.expires = 0;
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800516 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * This a special case for clock_nanosleep,
519 * not a normal timer from sys_timer_create.
520 */
521 wake_up_process(timer->it_process);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000522 timer->it.cpu.expires = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100523 } else if (!timer->it_interval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /*
525 * One-shot timer. Clear it as soon as it's fired.
526 */
527 posix_timer_event(timer, 0);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000528 timer->it.cpu.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
530 /*
531 * The signal did not get queued because the signal
532 * was ignored, so we won't get any callback to
533 * reload the timer. But we need to keep it
534 * ticking in case the signal is deliverable next time.
535 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200536 posix_cpu_timer_rearm(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200537 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539}
540
541/*
542 * Guts of sys_timer_settime for CPU timers.
543 * This is called with the timer locked and interrupts disabled.
544 * If we return TIMER_RETRY, it's necessary to release the timer's lock
545 * and try again. (This happens when the timer is in the middle of firing.)
546 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200547static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700548 struct itimerspec64 *new, struct itimerspec64 *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200550 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100551 u64 old_expires, new_expires, old_incr, val;
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200552 struct task_struct *p = timer->it.cpu.task;
553 struct sighand_struct *sighand;
554 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int ret;
556
Thomas Gleixner692117c2019-08-19 16:31:46 +0200557 if (WARN_ON_ONCE(!p))
558 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Thomas Gleixner098b0e02017-06-20 17:37:36 +0200560 /*
561 * Use the to_ktime conversion because that clamps the maximum
562 * value to KTIME_MAX and avoid multiplication overflows.
563 */
564 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /*
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200567 * Protect against sighand release/switch in exit/exec and p->cpu_timers
568 * and p->signal->cpu_timers read/write in arm_timer()
569 */
570 sighand = lock_task_sighand(p, &flags);
571 /*
572 * If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * longer get any information about it at all.
574 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200575 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -ESRCH;
577 }
578
579 /*
580 * Disarm any old timer after extracting its expiry time.
581 */
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400582
583 ret = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100584 old_incr = timer->it_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400586 if (unlikely(timer->it.cpu.firing)) {
587 timer->it.cpu.firing = -1;
588 ret = TIMER_RETRY;
589 } else
590 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /*
593 * We need to sample the current value to convert the new
594 * value from to relative and absolute, and to convert the
595 * old value from absolute to relative. To set a process
596 * timer, we need a sample to balance the thread expiry
597 * times (in arm_timer). With an absolute time, we must
598 * check if it's already passed. In short, we need a sample.
599 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200600 if (CPUCLOCK_PERTHREAD(timer->it_clock))
601 val = cpu_clock_sample(clkid, p);
602 else
603 val = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (old) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000606 if (old_expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 old->it_value.tv_sec = 0;
608 old->it_value.tv_nsec = 0;
609 } else {
610 /*
611 * Update the timer in case it has
612 * overrun already. If it has,
613 * we'll report it as having overrun
614 * and with the next reloaded timer
615 * already ticking, though we are
616 * swallowing that pending
617 * notification here to install the
618 * new setting.
619 */
620 bump_cpu_timer(timer, val);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000621 if (val < timer->it.cpu.expires) {
622 old_expires = timer->it.cpu.expires - val;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700623 old->it_value = ns_to_timespec64(old_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } else {
625 old->it_value.tv_nsec = 1;
626 old->it_value.tv_sec = 0;
627 }
628 }
629 }
630
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400631 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * We are colliding with the timer actually firing.
634 * Punt after filling in the timer's old value, and
635 * disable this firing since we are already reporting
636 * it as an overrun (thanks to bump_cpu_timer above).
637 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200638 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
640 }
641
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200642 if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000643 new_expires += val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 /*
647 * Install the new expiry time (or zero).
648 * For a timer with no notification action, we don't actually
649 * arm the timer (we'll just fake it for timer_gettime).
650 */
651 timer->it.cpu.expires = new_expires;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000652 if (new_expires != 0 && val < new_expires) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800653 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200656 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Install the new reload setting, and
659 * set up the signal and overrun bookkeeping.
660 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100661 timer->it_interval = timespec64_to_ktime(new->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*
664 * This acts as a modification timestamp for the timer,
665 * so any automatic reload attempt will punt on seeing
666 * that we have reset the timer manually.
667 */
668 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
669 ~REQUEUE_PENDING;
670 timer->it_overrun_last = 0;
671 timer->it_overrun = -1;
672
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000673 if (new_expires != 0 && !(val < new_expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * The designated time already passed, so we notify
676 * immediately, even if the thread never runs to
677 * accumulate more time on this clock.
678 */
679 cpu_timer_fire(timer);
680 }
681
682 ret = 0;
683 out:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100684 if (old)
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700685 old->it_interval = ns_to_timespec64(old_incr);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return ret;
688}
689
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700690static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Thomas Gleixner99093c52019-08-21 21:08:57 +0200692 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct task_struct *p = timer->it.cpu.task;
Thomas Gleixner692117c2019-08-19 16:31:46 +0200694 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Thomas Gleixner692117c2019-08-19 16:31:46 +0200696 if (WARN_ON_ONCE(!p))
697 return;
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /*
700 * Easy part: convert the reload time.
701 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100702 itp->it_interval = ktime_to_timespec64(timer->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Gleixnereabdec02017-05-30 23:15:51 +0200704 if (!timer->it.cpu.expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * Sample the clock to take the difference with the expiry time.
709 */
710 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200711 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200713 struct sighand_struct *sighand;
714 unsigned long flags;
715
716 /*
717 * Protect against sighand release/switch in exit/exec and
718 * also make timer sampling safe if it ends up calling
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100719 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200720 */
721 sighand = lock_task_sighand(p, &flags);
722 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * The process has been reaped.
725 * We can't even collect a sample any more.
726 * Call the timer disarmed, nothing else to do.
727 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000728 timer->it.cpu.expires = 0;
Alexey Dobriyan2c13ce82016-07-08 01:39:11 +0300729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200731 now = cpu_clock_sample_group(clkid, p, false);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200732 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000736 if (now < timer->it.cpu.expires) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700737 itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 } else {
739 /*
740 * The timer should have expired already, but the firing
741 * hasn't taken place yet. Say it's just about to expire.
742 */
743 itp->it_value.tv_nsec = 1;
744 itp->it_value.tv_sec = 0;
745 }
746}
747
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000748static unsigned long long
749check_timers_list(struct list_head *timers,
750 struct list_head *firing,
751 unsigned long long curr)
752{
753 int maxfire = 20;
754
755 while (!list_empty(timers)) {
756 struct cpu_timer_list *t;
757
758 t = list_first_entry(timers, struct cpu_timer_list, entry);
759
760 if (!--maxfire || curr < t->expires)
761 return t->expires;
762
763 t->firing = 1;
764 list_move_tail(&t->entry, firing);
765 }
766
767 return 0;
768}
769
Juri Lelli34be3932017-12-12 12:10:24 +0100770static inline void check_dl_overrun(struct task_struct *tsk)
771{
772 if (tsk->dl.dl_overrun) {
773 tsk->dl.dl_overrun = 0;
774 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
775 }
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * Check for any per-thread CPU timers that have fired and move them off
780 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
781 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
782 */
783static void check_thread_timers(struct task_struct *tsk,
784 struct list_head *firing)
785{
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200786 struct posix_cputimer_base *base = tsk->posix_cputimers.bases;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800787 unsigned long soft;
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200788 u64 stime, utime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Juri Lelli34be3932017-12-12 12:10:24 +0100790 if (dl_task(tsk))
791 check_dl_overrun(tsk);
792
Jason Low934715a2015-10-14 12:07:54 -0700793 /*
Thomas Gleixner001f7972019-08-21 21:09:13 +0200794 * If the expiry cache is zero, then there are no active per thread
795 * CPU timers.
Jason Low934715a2015-10-14 12:07:54 -0700796 */
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200797 if (expiry_cache_is_zero(&tsk->posix_cputimers))
Jason Low934715a2015-10-14 12:07:54 -0700798 return;
799
Thomas Gleixner0476ff22019-08-21 21:09:02 +0200800 task_cputime(tsk, &utime, &stime);
801
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200802 base->nextevt = check_timers_list(&base->cpu_timers, firing,
803 utime + stime);
804 base++;
805 base->nextevt = check_timers_list(&base->cpu_timers, firing, utime);
806 base++;
807 base->nextevt = check_timers_list(&base->cpu_timers, firing,
808 tsk->se.sum_exec_runtime);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100809
810 /*
811 * Check for the special case thread timers.
812 */
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200813 soft = task_rlimit(tsk, RLIMIT_RTTIME);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800814 if (soft != RLIM_INFINITY) {
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200815 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100816
Peter Zijlstra5a52dd52008-01-25 21:08:32 +0100817 if (hard != RLIM_INFINITY &&
818 tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100819 /*
820 * At the hard limit, we just die.
821 * No need to calculate anything else now.
822 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200823 if (print_fatal_signals) {
824 pr_info("CPU Watchdog Timeout (hard): %s[%d]\n",
825 tsk->comm, task_pid_nr(tsk));
826 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100827 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
828 return;
829 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800830 if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100831 /*
832 * At the soft limit, send a SIGXCPU every second.
833 */
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800834 if (soft < hard) {
835 soft += USEC_PER_SEC;
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200836 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur =
837 soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100838 }
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200839 if (print_fatal_signals) {
840 pr_info("RT Watchdog Timeout (soft): %s[%d]\n",
841 tsk->comm, task_pid_nr(tsk));
842 }
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100843 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
844 }
845 }
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200846
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200847 if (expiry_cache_is_zero(&tsk->posix_cputimers))
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200848 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Jason Low10180162015-04-28 13:00:22 -0700851static inline void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100852{
Stanislaw Gruszka15365c12010-03-11 14:04:31 -0800853 struct thread_group_cputimer *cputimer = &sig->cputimer;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100854
Jason Low10180162015-04-28 13:00:22 -0700855 /* Turn off cputimer->running. This is done without locking. */
Jason Lowd5c373e2015-10-14 12:07:55 -0700856 WRITE_ONCE(cputimer->running, false);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200857 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100858}
859
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200860static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100861 u64 *expires, u64 cur_time, int signo)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200862{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100863 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200864 return;
865
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100866 if (cur_time >= it->expires) {
867 if (it->incr)
Martin Schwidefsky64861632011-12-15 14:56:09 +0100868 it->expires += it->incr;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100869 else
Martin Schwidefsky64861632011-12-15 14:56:09 +0100870 it->expires = 0;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200871
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800872 trace_itimer_expire(signo == SIGPROF ?
873 ITIMER_PROF : ITIMER_VIRTUAL,
Eric W. Biederman6883f812017-06-04 04:32:13 -0500874 task_tgid(tsk), cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200875 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
876 }
877
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100878 if (it->expires && (!*expires || it->expires < *expires))
879 *expires = it->expires;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/*
883 * Check for any per-thread CPU timers that have fired and move them
884 * off the tsk->*_timers list onto the firing list. Per-thread timers
885 * have already been taken off.
886 */
887static void check_process_timers(struct task_struct *tsk,
888 struct list_head *firing)
889{
890 struct signal_struct *const sig = tsk->signal;
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200891 struct posix_cputimer_base *base = sig->posix_cputimers.bases;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100892 u64 utime, ptime, virt_expires, prof_expires;
893 u64 sum_sched_runtime, sched_expires;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100894 struct task_cputime cputime;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800895 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
Jason Low934715a2015-10-14 12:07:54 -0700898 * If cputimer is not running, then there are no active
899 * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU).
900 */
901 if (!READ_ONCE(tsk->signal->cputimer.running))
902 return;
903
Thomas Gleixnera3249562019-08-21 21:08:53 +0200904 /*
Jason Lowc8d75aa2015-10-14 12:07:56 -0700905 * Signify that a thread is checking for process timers.
906 * Write access to this field is protected by the sighand lock.
907 */
908 sig->cputimer.checking_timer = true;
909
Jason Low934715a2015-10-14 12:07:54 -0700910 /*
Thomas Gleixnera3249562019-08-21 21:08:53 +0200911 * Collect the current process totals. Group accounting is active
912 * so the sample can be taken directly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Thomas Gleixnera3249562019-08-21 21:08:53 +0200914 sample_cputime_atomic(&cputime, &sig->cputimer.cputime_atomic);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100915 utime = cputime.utime;
916 ptime = utime + cputime.stime;
Frank Mayharf06febc2008-09-12 09:54:39 -0700917 sum_sched_runtime = cputime.sum_exec_runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200919 prof_expires = check_timers_list(&base[CPUCLOCK_PROF].cpu_timers,
920 firing, ptime);
921 virt_expires = check_timers_list(&base[CPUCLOCK_VIRT].cpu_timers,
922 firing, utime);
923 sched_expires = check_timers_list(&base[CPUCLOCK_SCHED].cpu_timers,
924 firing, sum_sched_runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /*
927 * Check for the special case process timers.
928 */
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200929 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
930 SIGPROF);
931 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
932 SIGVTALRM);
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200933 soft = task_rlimit(tsk, RLIMIT_CPU);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800934 if (soft != RLIM_INFINITY) {
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100935 unsigned long psecs = div_u64(ptime, NSEC_PER_SEC);
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200936 unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100937 u64 x;
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800938 if (psecs >= hard) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
940 * At the hard limit, we just die.
941 * No need to calculate anything else now.
942 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200943 if (print_fatal_signals) {
944 pr_info("RT Watchdog Timeout (hard): %s[%d]\n",
945 tsk->comm, task_pid_nr(tsk));
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
948 return;
949 }
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800950 if (psecs >= soft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /*
952 * At the soft limit, send a SIGXCPU every second.
953 */
Thomas Gleixner43fe8b82017-05-23 23:27:38 +0200954 if (print_fatal_signals) {
955 pr_info("CPU Watchdog Timeout (soft): %s[%d]\n",
956 tsk->comm, task_pid_nr(tsk));
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800959 if (soft < hard) {
960 soft++;
961 sig->rlim[RLIMIT_CPU].rlim_cur = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100964 x = soft * NSEC_PER_SEC;
965 if (!prof_expires || x < prof_expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 prof_expires = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200969 base[CPUCLOCK_PROF].nextevt = prof_expires;
970 base[CPUCLOCK_VIRT].nextevt = virt_expires;
971 base[CPUCLOCK_SCHED].nextevt = sched_expires;
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200972
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200973 if (expiry_cache_is_zero(&sig->posix_cputimers))
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -0700974 stop_process_timers(sig);
Jason Lowc8d75aa2015-10-14 12:07:56 -0700975
976 sig->cputimer.checking_timer = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200980 * This is called from the signal code (via posixtimer_rearm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * when the last timer signal was delivered and we have to reload the timer.
982 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200983static void posix_cpu_timer_rearm(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Thomas Gleixnerda020ce2019-08-21 21:08:58 +0200985 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Thomas Gleixner692117c2019-08-19 16:31:46 +0200986 struct task_struct *p = timer->it.cpu.task;
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200987 struct sighand_struct *sighand;
988 unsigned long flags;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100989 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Thomas Gleixner692117c2019-08-19 16:31:46 +0200991 if (WARN_ON_ONCE(!p))
992 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * Fetch the current sample and update the timer's expiry time.
996 */
997 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200998 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 bump_cpu_timer(timer, now);
Frederic Weisbecker724a3712013-10-10 16:55:57 +02001000 if (unlikely(p->exit_state))
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001001 return;
Frederic Weisbecker724a3712013-10-10 16:55:57 +02001002
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001003 /* Protect timer list r/w in arm_timer() */
1004 sighand = lock_task_sighand(p, &flags);
1005 if (!sighand)
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001006 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001008 /*
1009 * Protect arm_timer() and timer sampling in case of call to
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +01001010 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001011 */
1012 sighand = lock_task_sighand(p, &flags);
1013 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /*
1015 * The process has been reaped.
1016 * We can't even collect a sample any more.
1017 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001018 timer->it.cpu.expires = 0;
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001019 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001021 /* If the process is dying, no need to rearm */
1022 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +02001024 now = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 bump_cpu_timer(timer, now);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001026 /* Leave the sighand locked for the call below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029 /*
1030 * Now re-arm for the new expiry time.
1031 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -08001032 arm_timer(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +02001033unlock:
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +02001034 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Frank Mayharf06febc2008-09-12 09:54:39 -07001037/**
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001038 * task_cputimers_expired - Check whether posix CPU timers are expired
Frank Mayharf06febc2008-09-12 09:54:39 -07001039 *
Thomas Gleixner001f7972019-08-21 21:09:13 +02001040 * @samples: Array of current samples for the CPUCLOCK clocks
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001041 * @pct: Pointer to a posix_cputimers container
Frank Mayharf06febc2008-09-12 09:54:39 -07001042 *
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001043 * Returns true if any member of @samples is greater than the corresponding
1044 * member of @pct->bases[CLK].nextevt. False otherwise
Frank Mayharf06febc2008-09-12 09:54:39 -07001045 */
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001046static inline bool
1047task_cputimers_expired(const u64 *sample, struct posix_cputimers *pct)
Frank Mayharf06febc2008-09-12 09:54:39 -07001048{
Thomas Gleixner001f7972019-08-21 21:09:13 +02001049 int i;
1050
1051 for (i = 0; i < CPUCLOCK_MAX; i++) {
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001052 if (pct->bases[i].nextevt && sample[i] >= pct->bases[i].nextevt)
Thomas Gleixner001f7972019-08-21 21:09:13 +02001053 return true;
1054 }
1055 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001056}
1057
1058/**
1059 * fastpath_timer_check - POSIX CPU timers fast path.
1060 *
1061 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001062 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001063 * Check the task and thread group timers. If both are zero (there are no
1064 * timers set) return false. Otherwise snapshot the task and thread group
1065 * timers and compare them with the corresponding expiration times. Return
1066 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001067 */
Thomas Gleixner001f7972019-08-21 21:09:13 +02001068static inline bool fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001069{
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001070 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001071
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001072 if (!expiry_cache_is_zero(&tsk->posix_cputimers)) {
Thomas Gleixner001f7972019-08-21 21:09:13 +02001073 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001074
Thomas Gleixner001f7972019-08-21 21:09:13 +02001075 task_sample_cputime(tsk, samples);
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001076 if (task_cputimers_expired(samples, &tsk->posix_cputimers))
Thomas Gleixner001f7972019-08-21 21:09:13 +02001077 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001078 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001079
1080 sig = tsk->signal;
Jason Lowc8d75aa2015-10-14 12:07:56 -07001081 /*
1082 * Check if thread group timers expired when the cputimer is
1083 * running and no other thread in the group is already checking
1084 * for thread group cputimers. These fields are read without the
1085 * sighand lock. However, this is fine because this is meant to
1086 * be a fastpath heuristic to determine whether we should try to
1087 * acquire the sighand lock to check/handle timers.
1088 *
1089 * In the worst case scenario, if 'running' or 'checking_timer' gets
1090 * set but the current thread doesn't see the change yet, we'll wait
1091 * until the next thread in the group gets a scheduler interrupt to
1092 * handle the timer. This isn't an issue in practice because these
1093 * types of delays with signals actually getting sent are expected.
1094 */
1095 if (READ_ONCE(sig->cputimer.running) &&
1096 !READ_ONCE(sig->cputimer.checking_timer)) {
Thomas Gleixner001f7972019-08-21 21:09:13 +02001097 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001098
Thomas Gleixner001f7972019-08-21 21:09:13 +02001099 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
1100 samples);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001101
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001102 if (task_cputimers_expired(samples, &sig->posix_cputimers))
Thomas Gleixner001f7972019-08-21 21:09:13 +02001103 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001104 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001105
Juri Lelli34be3932017-12-12 12:10:24 +01001106 if (dl_task(tsk) && tsk->dl.dl_overrun)
Thomas Gleixner001f7972019-08-21 21:09:13 +02001107 return true;
Juri Lelli34be3932017-12-12 12:10:24 +01001108
Thomas Gleixner001f7972019-08-21 21:09:13 +02001109 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*
1113 * This is called from the timer interrupt handler. The irq handler has
1114 * already updated our counts. We need to check if any timers fire now.
1115 * Interrupts are disabled.
1116 */
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001117void run_posix_cpu_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001119 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001121 unsigned long flags;
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001122 LIST_HEAD(firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Frederic Weisbeckera6968222017-11-06 16:01:28 +01001124 lockdep_assert_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 */
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001182void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001183 u64 *newval, u64 *oldval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001185 u64 now, *nextevt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001187 if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
Thomas Gleixner692117c2019-08-19 16:31:46 +02001188 return;
1189
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001190 nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001191 now = cpu_clock_sample_group(clkid, tsk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Thomas Gleixner5405d002019-08-21 21:08:59 +02001193 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001194 /*
1195 * We are setting itimer. The *oldval is absolute and we update
1196 * it to be relative, *newval argument is relative and we update
1197 * it to be absolute.
1198 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001199 if (*oldval) {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001200 if (*oldval <= now) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Just about to fire. */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001202 *oldval = TICK_NSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 } else {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001204 *oldval -= now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
1207
Martin Schwidefsky64861632011-12-15 14:56:09 +01001208 if (!*newval)
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001209 return;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001210 *newval += now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 /*
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001214 * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
1215 * expiry cache is also used by RLIMIT_CPU!.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001217 if (expires_gt(*nextevt, *newval))
1218 *nextevt = *newval;
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001219
1220 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Toyo Abee4b76552006-09-29 02:00:29 -07001223static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001224 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Al Viro86a9c442017-06-07 09:42:26 +01001226 struct itimerspec64 it;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001227 struct k_itimer timer;
1228 u64 expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int error;
1230
1231 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * Set up a temporary timer and then wait for it to go off.
1233 */
1234 memset(&timer, 0, sizeof timer);
1235 spin_lock_init(&timer.it_lock);
1236 timer.it_clock = which_clock;
1237 timer.it_overrun = -1;
1238 error = posix_cpu_timer_create(&timer);
1239 timer.it_process = current;
1240 if (!error) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -07001241 static struct itimerspec64 zero_it;
Al Viroedbeda42017-06-07 09:42:31 +01001242 struct restart_block *restart;
Toyo Abee4b76552006-09-29 02:00:29 -07001243
Al Viroedbeda42017-06-07 09:42:31 +01001244 memset(&it, 0, sizeof(it));
Al Viro86a9c442017-06-07 09:42:26 +01001245 it.it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 spin_lock_irq(&timer.it_lock);
Al Viro86a9c442017-06-07 09:42:26 +01001248 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (error) {
1250 spin_unlock_irq(&timer.it_lock);
1251 return error;
1252 }
1253
1254 while (!signal_pending(current)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001255 if (timer.it.cpu.expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001257 * Our timer fired and was reset, below
1258 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001260 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 spin_unlock_irq(&timer.it_lock);
1262 return 0;
1263 }
1264
1265 /*
1266 * Block until cpu_timer_fire (or a signal) wakes us.
1267 */
1268 __set_current_state(TASK_INTERRUPTIBLE);
1269 spin_unlock_irq(&timer.it_lock);
1270 schedule();
1271 spin_lock_irq(&timer.it_lock);
1272 }
1273
1274 /*
1275 * We were interrupted by a signal.
1276 */
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001277 expires = timer.it.cpu.expires;
Al Viro86a9c442017-06-07 09:42:26 +01001278 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001279 if (!error) {
1280 /*
1281 * Timer is now unarmed, deletion can not fail.
1282 */
1283 posix_cpu_timer_del(&timer);
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 spin_unlock_irq(&timer.it_lock);
1286
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001287 while (error == TIMER_RETRY) {
1288 /*
1289 * We need to handle case when timer was or is in the
1290 * middle of firing. In other cases we already freed
1291 * resources.
1292 */
1293 spin_lock_irq(&timer.it_lock);
1294 error = posix_cpu_timer_del(&timer);
1295 spin_unlock_irq(&timer.it_lock);
1296 }
1297
Al Viro86a9c442017-06-07 09:42:26 +01001298 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * It actually did fire already.
1301 */
1302 return 0;
1303 }
1304
Toyo Abee4b76552006-09-29 02:00:29 -07001305 error = -ERESTART_RESTARTBLOCK;
Al Viro86a9c442017-06-07 09:42:26 +01001306 /*
1307 * Report back to the user the time still remaining.
1308 */
Al Viroedbeda42017-06-07 09:42:31 +01001309 restart = &current->restart_block;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001310 restart->nanosleep.expires = expires;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001311 if (restart->nanosleep.type != TT_NONE)
1312 error = nanosleep_copyout(restart, &it.it_value);
Toyo Abee4b76552006-09-29 02:00:29 -07001313 }
1314
1315 return error;
1316}
1317
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001318static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1319
1320static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001321 const struct timespec64 *rqtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001322{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001323 struct restart_block *restart_block = &current->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001324 int error;
1325
1326 /*
1327 * Diagnose required errors first.
1328 */
1329 if (CPUCLOCK_PERTHREAD(which_clock) &&
1330 (CPUCLOCK_PID(which_clock) == 0 ||
Eric W. Biederman01a21972017-04-13 10:32:16 -05001331 CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
Toyo Abee4b76552006-09-29 02:00:29 -07001332 return -EINVAL;
1333
Al Viro86a9c442017-06-07 09:42:26 +01001334 error = do_cpu_nanosleep(which_clock, flags, rqtp);
Toyo Abee4b76552006-09-29 02:00:29 -07001335
1336 if (error == -ERESTART_RESTARTBLOCK) {
1337
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001338 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001339 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Toyo Abe1711ef32006-09-29 02:00:28 -07001341 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001342 restart_block->nanosleep.clockid = which_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return error;
1345}
1346
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001347static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001349 clockid_t which_clock = restart_block->nanosleep.clockid;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001350 struct timespec64 t;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001351
Deepa Dinamaniad196382017-03-26 12:04:18 -07001352 t = ns_to_timespec64(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001353
Al Viro86a9c442017-06-07 09:42:26 +01001354 return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Nick Desaulniers29f1b2b2017-12-28 22:11:36 -05001357#define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
1358#define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Thomas Gleixnera924b042006-01-09 20:52:27 -08001360static int process_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001361 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1364}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001365static int process_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001366 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1369}
1370static int process_cpu_timer_create(struct k_itimer *timer)
1371{
1372 timer->it_clock = PROCESS_CLOCK;
1373 return posix_cpu_timer_create(timer);
1374}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001375static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001376 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Al Viro99e6c0e2017-06-07 09:42:30 +01001378 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001380static int thread_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001381 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1384}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001385static int thread_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001386 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1389}
1390static int thread_cpu_timer_create(struct k_itimer *timer)
1391{
1392 timer->it_clock = THREAD_CLOCK;
1393 return posix_cpu_timer_create(timer);
1394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001396const struct k_clock clock_posix_cpu = {
Thomas Gleixner19769452011-02-01 13:51:06 +00001397 .clock_getres = posix_cpu_clock_getres,
1398 .clock_set = posix_cpu_clock_set,
1399 .clock_get = posix_cpu_clock_get,
1400 .timer_create = posix_cpu_timer_create,
1401 .nsleep = posix_cpu_nsleep,
Thomas Gleixner19769452011-02-01 13:51:06 +00001402 .timer_set = posix_cpu_timer_set,
1403 .timer_del = posix_cpu_timer_del,
1404 .timer_get = posix_cpu_timer_get,
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +02001405 .timer_rearm = posix_cpu_timer_rearm,
Thomas Gleixner19769452011-02-01 13:51:06 +00001406};
1407
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001408const struct k_clock clock_process = {
1409 .clock_getres = process_cpu_clock_getres,
1410 .clock_get = process_cpu_clock_get,
1411 .timer_create = process_cpu_timer_create,
1412 .nsleep = process_cpu_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001413};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001415const struct k_clock clock_thread = {
1416 .clock_getres = thread_cpu_clock_getres,
1417 .clock_get = thread_cpu_clock_get,
1418 .timer_create = thread_cpu_timer_create,
1419};