blob: 52f4c99c1d6057ba615e530abffa149251828b5c [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 Gleixnerbab0aae92017-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);
Thomas Gleixner244d49e2019-08-21 21:09:24 +020026 if (cpu_limit != RLIM_INFINITY) {
Thomas Gleixner87dc6442019-08-26 20:22:24 +020027 pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
Thomas Gleixner244d49e2019-08-21 21:09:24 +020028 pct->timers_active = true;
29 }
Thomas Gleixner3a245c02019-08-21 21:09:06 +020030}
31
Frank Mayharf06febc2008-09-12 09:54:39 -070032/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -080033 * Called after updating RLIMIT_CPU to run cpu timer and update
Thomas Gleixner87dc6442019-08-26 20:22:24 +020034 * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
35 * necessary. Needs siglock protection since other code may update the
Thomas Gleixner3a245c02019-08-21 21:09:06 +020036 * expiration cache as well.
Frank Mayharf06febc2008-09-12 09:54:39 -070037 */
Jiri Slaby5ab46b32009-08-28 14:05:12 +020038void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
Frank Mayharf06febc2008-09-12 09:54:39 -070039{
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010040 u64 nsecs = rlim_new * NSEC_PER_SEC;
Frank Mayharf06febc2008-09-12 09:54:39 -070041
Jiri Slaby5ab46b32009-08-28 14:05:12 +020042 spin_lock_irq(&task->sighand->siglock);
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +010043 set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
Jiri Slaby5ab46b32009-08-28 14:05:12 +020044 spin_unlock_irq(&task->sighand->siglock);
Frank Mayharf06febc2008-09-12 09:54:39 -070045}
46
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020047/*
48 * Functions for validating access to tasks.
49 */
50static struct task_struct *lookup_task(const pid_t pid, bool thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020054 if (!pid)
55 return thread ? current : current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020057 p = find_task_by_vpid(pid);
58 if (!p || p == current)
59 return p;
60 if (thread)
61 return same_thread_group(p, current) ? p : NULL;
62 if (p == current)
63 return p;
64 return has_group_leader_pid(p) ? p : NULL;
65}
66
67static struct task_struct *__get_task_for_clock(const clockid_t clock,
68 bool getref)
69{
70 const bool thread = !!CPUCLOCK_PERTHREAD(clock);
71 const pid_t pid = CPUCLOCK_PID(clock);
72 struct task_struct *p;
73
74 if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
75 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020077 rcu_read_lock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020078 p = lookup_task(pid, thread);
79 if (p && getref)
80 get_task_struct(p);
Sergey Senozhatskyc0deae82010-11-03 18:52:56 +020081 rcu_read_unlock();
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020082 return p;
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Thomas Gleixner6ae40e32019-08-21 21:08:48 +020085static inline struct task_struct *get_task_for_clock(const clockid_t clock)
86{
87 return __get_task_for_clock(clock, true);
88}
89
90static inline int validate_clock_permissions(const clockid_t clock)
91{
92 return __get_task_for_clock(clock, false) ? 0 : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Update expiry time from increment, and increase overrun count,
97 * given the current clock sample.
98 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +010099static void bump_cpu_timer(struct k_itimer *timer, u64 now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int i;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100102 u64 delta, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Thomas Gleixner16118792019-01-11 14:33:17 +0100104 if (!timer->it_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return;
106
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000107 if (now < timer->it.cpu.expires)
108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Thomas Gleixner16118792019-01-11 14:33:17 +0100110 incr = timer->it_interval;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000111 delta = now + incr - timer->it.cpu.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000113 /* Don't use (incr*2 < delta), incr*2 might overflow. */
114 for (i = 0; incr < delta - incr; i++)
115 incr = incr << 1;
116
117 for (; i >= 0; incr >>= 1, i--) {
118 if (delta < incr)
119 continue;
120
121 timer->it.cpu.expires += incr;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200122 timer->it_overrun += 1LL << i;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000123 delta -= incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125}
126
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200127/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
128static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200129{
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200130 return !(~pct->bases[CPUCLOCK_PROF].nextevt |
131 ~pct->bases[CPUCLOCK_VIRT].nextevt |
132 ~pct->bases[CPUCLOCK_SCHED].nextevt);
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200133}
134
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000135static int
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700136posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200138 int error = validate_clock_permissions(which_clock);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!error) {
141 tp->tv_sec = 0;
142 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
143 if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
144 /*
145 * If sched_clock is using a cycle counter, we
146 * don't have any idea of its true resolution
147 * exported, but it is much more than 1s/HZ.
148 */
149 tp->tv_nsec = 1;
150 }
151 }
152 return error;
153}
154
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000155static int
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200156posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200158 int error = validate_clock_permissions(clock);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /*
161 * You can never reset a CPU clock, but we check for other errors
162 * in the call before failing with EPERM.
163 */
Thomas Gleixner6ae40e32019-08-21 21:08:48 +0200164 return error ? : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200168 * Sample a per-thread clock for the given task. clkid is validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200170static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200172 u64 utime, stime;
173
174 if (clkid == CPUCLOCK_SCHED)
175 return task_sched_runtime(p);
176
177 task_cputime(p, &utime, &stime);
178
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200179 switch (clkid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 case CPUCLOCK_PROF:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200181 return utime + stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case CPUCLOCK_VIRT:
Thomas Gleixnerab693c52019-08-21 21:09:03 +0200183 return utime;
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200184 default:
185 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Thomas Gleixnerb0d524f2019-08-21 21:09:12 +0200190static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
191{
192 samples[CPUCLOCK_PROF] = stime + utime;
193 samples[CPUCLOCK_VIRT] = utime;
194 samples[CPUCLOCK_SCHED] = rtime;
195}
196
197static void task_sample_cputime(struct task_struct *p, u64 *samples)
198{
199 u64 stime, utime;
200
201 task_cputime(p, &utime, &stime);
202 store_samples(samples, stime, utime, p->se.sum_exec_runtime);
203}
204
205static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
206 u64 *samples)
207{
208 u64 stime, utime, rtime;
209
210 utime = atomic64_read(&at->utime);
211 stime = atomic64_read(&at->stime);
212 rtime = atomic64_read(&at->sum_exec_runtime);
213 store_samples(samples, stime, utime, rtime);
214}
215
Jason Low10180162015-04-28 13:00:22 -0700216/*
217 * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
218 * to avoid race conditions with concurrent updates to cputime.
219 */
220static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100221{
Jason Low10180162015-04-28 13:00:22 -0700222 u64 curr_cputime;
223retry:
224 curr_cputime = atomic64_read(cputime);
225 if (sum_cputime > curr_cputime) {
226 if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
227 goto retry;
228 }
229}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100230
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200231static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
232 struct task_cputime *sum)
Jason Low10180162015-04-28 13:00:22 -0700233{
Jason Low71107442015-04-28 13:00:24 -0700234 __update_gt_cputime(&cputime_atomic->utime, sum->utime);
235 __update_gt_cputime(&cputime_atomic->stime, sum->stime);
236 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
Jason Low10180162015-04-28 13:00:22 -0700237}
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100238
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200239/**
240 * thread_group_sample_cputime - Sample cputime for a given task
241 * @tsk: Task for which cputime needs to be started
242 * @iimes: Storage for time samples
243 *
244 * Called from sys_getitimer() to calculate the expiry time of an active
245 * timer. That means group cputime accounting is already active. Called
246 * with task sighand lock held.
247 *
248 * Updates @times with an uptodate sample of the thread group cputimes.
249 */
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200250void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200251{
252 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200253 struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200254
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200255 WARN_ON_ONCE(!pct->timers_active);
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200256
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200257 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
Thomas Gleixner19298fb2019-08-21 21:08:51 +0200258}
259
Thomas Gleixnerc506bef42019-08-21 21:08:54 +0200260/**
261 * thread_group_start_cputime - Start cputime and return a sample
262 * @tsk: Task for which cputime needs to be started
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200263 * @samples: Storage for time samples
Thomas Gleixnerc506bef42019-08-21 21:08:54 +0200264 *
265 * The thread group cputime accouting is avoided when there are no posix
266 * CPU timers armed. Before starting a timer it's required to check whether
267 * the time accounting is active. If not, a full update of the atomic
268 * accounting store needs to be done and the accounting enabled.
269 *
270 * Updates @times with an uptodate sample of the thread group cputimes.
271 */
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200272static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100273{
274 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200275 struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100276
Jason Low10180162015-04-28 13:00:22 -0700277 /* Check if cputimer isn't running. This is accessed without locking. */
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200278 if (!READ_ONCE(pct->timers_active)) {
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200279 struct task_cputime sum;
280
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100281 /*
282 * The POSIX timer interface allows for absolute time expiry
283 * values through the TIMER_ABSTIME flag, therefore we have
Jason Low10180162015-04-28 13:00:22 -0700284 * to synchronize the timer to the clock every time we start it.
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100285 */
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100286 thread_group_cputime(tsk, &sum);
Jason Low71107442015-04-28 13:00:24 -0700287 update_gt_cputime(&cputimer->cputime_atomic, &sum);
Jason Low10180162015-04-28 13:00:22 -0700288
289 /*
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200290 * We're setting timers_active without a lock. Ensure this
291 * only gets written to in one operation. We set it after
292 * update_gt_cputime() as a small optimization, but
293 * barriers are not required because update_gt_cputime()
Jason Low10180162015-04-28 13:00:22 -0700294 * can handle concurrent updates.
295 */
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200296 WRITE_ONCE(pct->timers_active, true);
Jason Low10180162015-04-28 13:00:22 -0700297 }
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200298 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
299}
300
301static void __thread_group_cputime(struct task_struct *tsk, u64 *samples)
302{
303 struct task_cputime ct;
304
305 thread_group_cputime(tsk, &ct);
306 store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime);
Peter Zijlstra4da94d492009-02-11 11:30:27 +0100307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200310 * Sample a process (thread group) clock for the given task clkid. If the
311 * group's cputime accounting is already enabled, read the atomic
312 * store. Otherwise a full update is required. Task's sighand lock must be
Thomas Gleixner2092c1d42019-08-21 21:09:00 +0200313 * held to protect the task traversal on a full update. clkid is already
314 * validated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200316static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
317 bool start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200319 struct thread_group_cputimer *cputimer = &p->signal->cputimer;
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200320 struct posix_cputimers *pct = &p->signal->posix_cputimers;
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200321 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -0700322
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200323 if (!READ_ONCE(pct->timers_active)) {
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200324 if (start)
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200325 thread_group_start_cputime(p, samples);
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200326 else
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200327 __thread_group_cputime(p, samples);
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200328 } else {
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200329 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
Thomas Gleixner24ab7f52019-08-21 21:08:55 +0200330 }
331
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200332 return samples[clkid];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200335static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200336{
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200337 const clockid_t clkid = CPUCLOCK_WHICH(clock);
338 struct task_struct *tsk;
339 u64 t;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200340
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200341 tsk = get_task_for_clock(clock);
342 if (!tsk)
343 return -EINVAL;
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200344
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200345 if (CPUCLOCK_PERTHREAD(clock))
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200346 t = cpu_clock_sample(clkid, tsk);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200347 else
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200348 t = cpu_clock_sample_group(clkid, tsk, false);
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200349 put_task_struct(tsk);
Frederic Weisbecker33ab0fe2013-10-11 17:41:11 +0200350
Thomas Gleixnerbfcf3e92019-08-21 21:08:49 +0200351 *tp = ns_to_timespec64(t);
352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
356 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
Stanislaw Gruszkaba5ea952009-11-17 14:14:13 -0800357 * This is called from sys_timer_create() and do_cpu_nanosleep() with the
358 * new timer already all-zeros initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000360static int posix_cpu_timer_create(struct k_itimer *new_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200362 struct task_struct *p = get_task_for_clock(new_timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200364 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -EINVAL;
366
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200367 new_timer->kclock = &clock_posix_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 INIT_LIST_HEAD(&new_timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 new_timer->it.cpu.task = p;
Thomas Gleixnere5a8b652019-08-21 21:08:50 +0200370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/*
374 * Clean up a CPU-clock timer that is about to be destroyed.
375 * This is called from timer deletion with the timer already locked.
376 * If we return TIMER_RETRY, it's necessary to release the timer's lock
377 * and try again. (This happens when the timer is in the middle of firing.)
378 */
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +0000379static int posix_cpu_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Oleg Nesterov108150e2005-10-23 20:25:39 +0400381 int ret = 0;
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200382 unsigned long flags;
383 struct sighand_struct *sighand;
384 struct task_struct *p = timer->it.cpu.task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Thomas Gleixner692117c2019-08-19 16:31:46 +0200386 if (WARN_ON_ONCE(!p))
387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200389 /*
390 * Protect against sighand release/switch in exit/exec and process/
391 * thread timer list entry concurrent read/writes.
392 */
393 sighand = lock_task_sighand(p, &flags);
394 if (unlikely(sighand == NULL)) {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200395 /*
396 * We raced with the reaping of the task.
397 * The deletion should have cleared us off the list.
398 */
Frederic Weisbecker531f64f2013-10-11 17:58:08 +0200399 WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry));
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200400 } else {
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200401 if (timer->it.cpu.firing)
402 ret = TIMER_RETRY;
403 else
404 list_del(&timer->it.cpu.entry);
Frederic Weisbecker3d7a1422013-10-11 17:41:11 +0200405
406 unlock_task_sighand(p, &flags);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400407 }
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200408
409 if (!ret)
410 put_task_struct(p);
Oleg Nesterov108150e2005-10-23 20:25:39 +0400411
412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Frederic Weisbeckeraf82eb32013-10-11 16:11:43 +0200415static void cleanup_timers_list(struct list_head *head)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000416{
417 struct cpu_timer_list *timer, *next;
418
Frederic Weisbeckera0b20622013-06-28 00:06:43 +0000419 list_for_each_entry_safe(timer, next, head, entry)
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000420 list_del_init(&timer->entry);
Frederic Weisbecker1a7fa512013-06-28 00:06:42 +0000421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
Thomas Gleixner7cb9a942019-08-19 16:31:45 +0200424 * Clean out CPU timers which are still armed when a thread exits. The
425 * timers are only removed from the list. No other updates are done. The
426 * corresponding posix timers are still accessible, but cannot be rearmed.
427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * This must be called with the siglock held.
429 */
Thomas Gleixner2b699422019-08-21 21:09:04 +0200430static void cleanup_timers(struct posix_cputimers *pct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200432 cleanup_timers_list(&pct->bases[CPUCLOCK_PROF].cpu_timers);
433 cleanup_timers_list(&pct->bases[CPUCLOCK_VIRT].cpu_timers);
434 cleanup_timers_list(&pct->bases[CPUCLOCK_SCHED].cpu_timers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
438 * These are both called with the siglock held, when the current thread
439 * is being reaped. When the final (leader) thread in the group is reaped,
440 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
441 */
442void posix_cpu_timers_exit(struct task_struct *tsk)
443{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200444 cleanup_timers(&tsk->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446void posix_cpu_timers_exit_group(struct task_struct *tsk)
447{
Thomas Gleixner2b699422019-08-21 21:09:04 +0200448 cleanup_timers(&tsk->signal->posix_cputimers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
452 * Insert the timer on the appropriate list before any timers that
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200453 * expire later. This must be called with the sighand lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800455static void arm_timer(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200457 struct cpu_timer_list *const nt = &timer->it.cpu;
458 int clkidx = CPUCLOCK_WHICH(timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct task_struct *p = timer->it.cpu.task;
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200460 u64 newexp = timer->it.cpu.expires;
461 struct posix_cputimer_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct list_head *head, *listpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct cpu_timer_list *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200465 if (CPUCLOCK_PERTHREAD(timer->it_clock))
466 base = p->posix_cputimers.bases + clkidx;
467 else
468 base = p->signal->posix_cputimers.bases + clkidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200470 listpos = head = &base->cpu_timers;
471 list_for_each_entry(next,head, entry) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000472 if (nt->expires < next->expires)
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800473 break;
474 listpos = &next->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 list_add(&nt->entry, listpos);
477
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200478 if (listpos != head)
479 return;
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800480
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200481 /*
482 * We are the new earliest-expiring POSIX 1.b timer, hence
483 * need to update expiration cache. Take into account that
484 * for process timers we share expiration cache with itimers
485 * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
486 */
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200487 if (newexp < base->nextevt)
Thomas Gleixner87dc6442019-08-26 20:22:24 +0200488 base->nextevt = newexp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Thomas Gleixner3b495b22019-08-21 21:09:08 +0200490 if (CPUCLOCK_PERTHREAD(timer->it_clock))
491 tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
492 else
493 tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/*
497 * The timer is locked, fire it and arrange for its reload.
498 */
499static void cpu_timer_fire(struct k_itimer *timer)
500{
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800501 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
502 /*
503 * User don't want any signal.
504 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000505 timer->it.cpu.expires = 0;
Stanislaw Gruszka1f169f82010-03-11 14:04:41 -0800506 } else if (unlikely(timer->sigq == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * This a special case for clock_nanosleep,
509 * not a normal timer from sys_timer_create.
510 */
511 wake_up_process(timer->it_process);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000512 timer->it.cpu.expires = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100513 } else if (!timer->it_interval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * One-shot timer. Clear it as soon as it's fired.
516 */
517 posix_timer_event(timer, 0);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000518 timer->it.cpu.expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
520 /*
521 * The signal did not get queued because the signal
522 * was ignored, so we won't get any callback to
523 * reload the timer. But we need to keep it
524 * ticking in case the signal is deliverable next time.
525 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200526 posix_cpu_timer_rearm(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200527 ++timer->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
531/*
532 * Guts of sys_timer_settime for CPU timers.
533 * This is called with the timer locked and interrupts disabled.
534 * If we return TIMER_RETRY, it's necessary to release the timer's lock
535 * and try again. (This happens when the timer is in the middle of firing.)
536 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200537static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700538 struct itimerspec64 *new, struct itimerspec64 *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200540 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100541 u64 old_expires, new_expires, old_incr, val;
Thomas Gleixnerc7a37c62019-08-21 21:08:56 +0200542 struct task_struct *p = timer->it.cpu.task;
543 struct sighand_struct *sighand;
544 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int ret;
546
Thomas Gleixner692117c2019-08-19 16:31:46 +0200547 if (WARN_ON_ONCE(!p))
548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Thomas Gleixner098b0e02017-06-20 17:37:36 +0200550 /*
551 * Use the to_ktime conversion because that clamps the maximum
552 * value to KTIME_MAX and avoid multiplication overflows.
553 */
554 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200557 * Protect against sighand release/switch in exit/exec and p->cpu_timers
558 * and p->signal->cpu_timers read/write in arm_timer()
559 */
560 sighand = lock_task_sighand(p, &flags);
561 /*
562 * If p has just been reaped, we can no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * longer get any information about it at all.
564 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200565 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -ESRCH;
567 }
568
569 /*
570 * Disarm any old timer after extracting its expiry time.
571 */
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400572
573 ret = 0;
Thomas Gleixner16118792019-01-11 14:33:17 +0100574 old_incr = timer->it_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 old_expires = timer->it.cpu.expires;
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400576 if (unlikely(timer->it.cpu.firing)) {
577 timer->it.cpu.firing = -1;
578 ret = TIMER_RETRY;
579 } else
580 list_del_init(&timer->it.cpu.entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /*
583 * We need to sample the current value to convert the new
584 * value from to relative and absolute, and to convert the
585 * old value from absolute to relative. To set a process
586 * timer, we need a sample to balance the thread expiry
587 * times (in arm_timer). With an absolute time, we must
588 * check if it's already passed. In short, we need a sample.
589 */
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200590 if (CPUCLOCK_PERTHREAD(timer->it_clock))
591 val = cpu_clock_sample(clkid, p);
592 else
593 val = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (old) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000596 if (old_expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 old->it_value.tv_sec = 0;
598 old->it_value.tv_nsec = 0;
599 } else {
600 /*
601 * Update the timer in case it has
602 * overrun already. If it has,
603 * we'll report it as having overrun
604 * and with the next reloaded timer
605 * already ticking, though we are
606 * swallowing that pending
607 * notification here to install the
608 * new setting.
609 */
610 bump_cpu_timer(timer, val);
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000611 if (val < timer->it.cpu.expires) {
612 old_expires = timer->it.cpu.expires - val;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700613 old->it_value = ns_to_timespec64(old_expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else {
615 old->it_value.tv_nsec = 1;
616 old->it_value.tv_sec = 0;
617 }
618 }
619 }
620
Oleg Nesterova69ac4a2005-10-24 18:29:58 +0400621 if (unlikely(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * We are colliding with the timer actually firing.
624 * Punt after filling in the timer's old value, and
625 * disable this firing since we are already reporting
626 * it as an overrun (thanks to bump_cpu_timer above).
627 */
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200628 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto out;
630 }
631
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200632 if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000633 new_expires += val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /*
637 * Install the new expiry time (or zero).
638 * For a timer with no notification action, we don't actually
639 * arm the timer (we'll just fake it for timer_gettime).
640 */
641 timer->it.cpu.expires = new_expires;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000642 if (new_expires != 0 && val < new_expires) {
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800643 arm_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200646 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Install the new reload setting, and
649 * set up the signal and overrun bookkeeping.
650 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100651 timer->it_interval = timespec64_to_ktime(new->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /*
654 * This acts as a modification timestamp for the timer,
655 * so any automatic reload attempt will punt on seeing
656 * that we have reset the timer manually.
657 */
658 timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
659 ~REQUEUE_PENDING;
660 timer->it_overrun_last = 0;
661 timer->it_overrun = -1;
662
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000663 if (new_expires != 0 && !(val < new_expires)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * The designated time already passed, so we notify
666 * immediately, even if the thread never runs to
667 * accumulate more time on this clock.
668 */
669 cpu_timer_fire(timer);
670 }
671
672 ret = 0;
673 out:
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100674 if (old)
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700675 old->it_interval = ns_to_timespec64(old_incr);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return ret;
678}
679
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700680static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Thomas Gleixner99093c52019-08-21 21:08:57 +0200682 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct task_struct *p = timer->it.cpu.task;
Thomas Gleixner692117c2019-08-19 16:31:46 +0200684 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Thomas Gleixner692117c2019-08-19 16:31:46 +0200686 if (WARN_ON_ONCE(!p))
687 return;
Frederic Weisbeckera3222f82013-10-11 00:37:39 +0200688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * Easy part: convert the reload time.
691 */
Thomas Gleixner16118792019-01-11 14:33:17 +0100692 itp->it_interval = ktime_to_timespec64(timer->it_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Thomas Gleixnereabdec02017-05-30 23:15:51 +0200694 if (!timer->it.cpu.expires)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * Sample the clock to take the difference with the expiry time.
699 */
700 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200701 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200703 struct sighand_struct *sighand;
704 unsigned long flags;
705
706 /*
707 * Protect against sighand release/switch in exit/exec and
708 * also make timer sampling safe if it ends up calling
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100709 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200710 */
711 sighand = lock_task_sighand(p, &flags);
712 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * The process has been reaped.
715 * We can't even collect a sample any more.
716 * Call the timer disarmed, nothing else to do.
717 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000718 timer->it.cpu.expires = 0;
Alexey Dobriyan2c13ce82016-07-08 01:39:11 +0300719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 } else {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200721 now = cpu_clock_sample_group(clkid, p, false);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200722 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000726 if (now < timer->it.cpu.expires) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700727 itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else {
729 /*
730 * The timer should have expired already, but the firing
731 * hasn't taken place yet. Say it's just about to expire.
732 */
733 itp->it_value.tv_nsec = 1;
734 itp->it_value.tv_sec = 0;
735 }
736}
737
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000738static unsigned long long
739check_timers_list(struct list_head *timers,
740 struct list_head *firing,
741 unsigned long long curr)
742{
743 int maxfire = 20;
744
745 while (!list_empty(timers)) {
746 struct cpu_timer_list *t;
747
748 t = list_first_entry(timers, struct cpu_timer_list, entry);
749
750 if (!--maxfire || curr < t->expires)
751 return t->expires;
752
753 t->firing = 1;
754 list_move_tail(&t->entry, firing);
755 }
756
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200757 return U64_MAX;
Frederic Weisbecker2473f3e2013-06-28 00:06:43 +0000758}
759
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200760static void collect_posix_cputimers(struct posix_cputimers *pct,
761 u64 *samples, struct list_head *firing)
762{
763 struct posix_cputimer_base *base = pct->bases;
764 int i;
765
766 for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
767 base->nextevt = check_timers_list(&base->cpu_timers, firing,
768 samples[i]);
769 }
770}
771
Juri Lelli34be3932017-12-12 12:10:24 +0100772static inline void check_dl_overrun(struct task_struct *tsk)
773{
774 if (tsk->dl.dl_overrun) {
775 tsk->dl.dl_overrun = 0;
776 __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
777 }
778}
779
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200780static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
781{
782 if (time < limit)
783 return false;
784
785 if (print_fatal_signals) {
786 pr_info("%s Watchdog Timeout (%s): %s[%d]\n",
787 rt ? "RT" : "CPU", hard ? "hard" : "soft",
788 current->comm, task_pid_nr(current));
789 }
790 __group_send_sig_info(signo, SEND_SIG_PRIV, current);
791 return true;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/*
795 * Check for any per-thread CPU timers that have fired and move them off
796 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
797 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
798 */
799static void check_thread_timers(struct task_struct *tsk,
800 struct list_head *firing)
801{
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200802 struct posix_cputimers *pct = &tsk->posix_cputimers;
803 u64 samples[CPUCLOCK_MAX];
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800804 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Juri Lelli34be3932017-12-12 12:10:24 +0100806 if (dl_task(tsk))
807 check_dl_overrun(tsk);
808
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200809 if (expiry_cache_is_inactive(pct))
Jason Low934715a2015-10-14 12:07:54 -0700810 return;
811
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200812 task_sample_cputime(tsk, samples);
813 collect_posix_cputimers(pct, samples, firing);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100814
815 /*
816 * Check for the special case thread timers.
817 */
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200818 soft = task_rlimit(tsk, RLIMIT_RTTIME);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800819 if (soft != RLIM_INFINITY) {
Thomas Gleixner8ea1de92019-08-21 21:09:21 +0200820 /* Task RT timeout is accounted in jiffies. RTTIME is usec */
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200821 unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200822 unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100823
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200824 /* At the hard limit, send SIGKILL. No further action. */
825 if (hard != RLIM_INFINITY &&
826 check_rlimit(rttime, hard, SIGKILL, true, true))
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100827 return;
Thomas Gleixnerdd670222019-08-21 21:09:22 +0200828
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200829 /* At the soft limit, send a SIGXCPU every second */
830 if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
Thomas Gleixnerdd670222019-08-21 21:09:22 +0200831 soft += USEC_PER_SEC;
832 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
Peter Zijlstra78f2c7d2008-01-25 21:08:27 +0100833 }
834 }
Thomas Gleixnerc02b0782019-08-21 21:09:10 +0200835
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200836 if (expiry_cache_is_inactive(pct))
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200837 tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Jason Low10180162015-04-28 13:00:22 -0700840static inline void stop_process_timers(struct signal_struct *sig)
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100841{
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200842 struct posix_cputimers *pct = &sig->posix_cputimers;
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100843
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200844 /* Turn off the active flag. This is done without locking. */
845 WRITE_ONCE(pct->timers_active, false);
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200846 tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
Peter Zijlstra3fccfd62009-02-10 16:37:31 +0100847}
848
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200849static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100850 u64 *expires, u64 cur_time, int signo)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200851{
Martin Schwidefsky64861632011-12-15 14:56:09 +0100852 if (!it->expires)
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200853 return;
854
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100855 if (cur_time >= it->expires) {
856 if (it->incr)
Martin Schwidefsky64861632011-12-15 14:56:09 +0100857 it->expires += it->incr;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100858 else
Martin Schwidefsky64861632011-12-15 14:56:09 +0100859 it->expires = 0;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200860
Xiao Guangrong3f0a5252009-08-10 10:52:30 +0800861 trace_itimer_expire(signo == SIGPROF ?
862 ITIMER_PROF : ITIMER_VIRTUAL,
Eric W. Biederman6883f812017-06-04 04:32:13 -0500863 task_tgid(tsk), cur_time);
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200864 __group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
865 }
866
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +0200867 if (it->expires && it->expires < *expires)
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +0100868 *expires = it->expires;
Stanislaw Gruszka42c4ab42009-07-29 12:15:26 +0200869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * Check for any per-thread CPU timers that have fired and move them
873 * off the tsk->*_timers list onto the firing list. Per-thread timers
874 * have already been taken off.
875 */
876static void check_process_timers(struct task_struct *tsk,
877 struct list_head *firing)
878{
879 struct signal_struct *const sig = tsk->signal;
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200880 struct posix_cputimers *pct = &sig->posix_cputimers;
881 u64 samples[CPUCLOCK_MAX];
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800882 unsigned long soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /*
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200885 * If there are no active process wide timers (POSIX 1.b, itimers,
886 * RLIMIT_CPU) nothing to check.
Jason Low934715a2015-10-14 12:07:54 -0700887 */
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200888 if (!READ_ONCE(pct->timers_active))
Jason Low934715a2015-10-14 12:07:54 -0700889 return;
890
Thomas Gleixnera3249562019-08-21 21:08:53 +0200891 /*
Jason Lowc8d75aa2015-10-14 12:07:56 -0700892 * Signify that a thread is checking for process timers.
893 * Write access to this field is protected by the sighand lock.
894 */
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200895 pct->timers_active = true;
Jason Lowc8d75aa2015-10-14 12:07:56 -0700896
Jason Low934715a2015-10-14 12:07:54 -0700897 /*
Thomas Gleixnera3249562019-08-21 21:08:53 +0200898 * Collect the current process totals. Group accounting is active
899 * so the sample can be taken directly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200901 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200902 collect_posix_cputimers(pct, samples, firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /*
905 * Check for the special case process timers.
906 */
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200907 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
908 &pct->bases[CPUCLOCK_PROF].nextevt,
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200909 samples[CPUCLOCK_PROF], SIGPROF);
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200910 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
911 &pct->bases[CPUCLOCK_VIRT].nextevt,
912 samples[CPUCLOCK_VIRT], SIGVTALRM);
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200913
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200914 soft = task_rlimit(tsk, RLIMIT_CPU);
Jiri Slabyd4bb52742010-03-05 13:42:53 -0800915 if (soft != RLIM_INFINITY) {
Thomas Gleixner8ea1de92019-08-21 21:09:21 +0200916 /* RLIMIT_CPU is in seconds. Samples are nanoseconds */
Krzysztof Opasiak3cf29492017-07-05 19:25:48 +0200917 unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
Thomas Gleixner8ea1de92019-08-21 21:09:21 +0200918 u64 ptime = samples[CPUCLOCK_PROF];
919 u64 softns = (u64)soft * NSEC_PER_SEC;
920 u64 hardns = (u64)hard * NSEC_PER_SEC;
Thomas Gleixnerb7be4ef2019-08-21 21:09:16 +0200921
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200922 /* At the hard limit, send SIGKILL. No further action. */
923 if (hard != RLIM_INFINITY &&
924 check_rlimit(ptime, hardns, SIGKILL, false, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return;
Thomas Gleixnerdd670222019-08-21 21:09:22 +0200926
Thomas Gleixner8991afe2019-08-21 21:09:23 +0200927 /* At the soft limit, send a SIGXCPU every second */
928 if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
Thomas Gleixnerdd670222019-08-21 21:09:22 +0200929 sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
930 softns += NSEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Thomas Gleixner8ea1de92019-08-21 21:09:21 +0200932
933 /* Update the expiry cache */
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200934 if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
935 pct->bases[CPUCLOCK_PROF].nextevt = softns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Thomas Gleixner1cd07c0b2019-08-21 21:09:20 +0200938 if (expiry_cache_is_inactive(pct))
Stanislaw Gruszka29f87b72010-04-27 14:12:15 -0700939 stop_process_timers(sig);
Jason Lowc8d75aa2015-10-14 12:07:56 -0700940
Thomas Gleixner244d49e2019-08-21 21:09:24 +0200941 pct->expiry_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200945 * This is called from the signal code (via posixtimer_rearm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * when the last timer signal was delivered and we have to reload the timer.
947 */
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200948static void posix_cpu_timer_rearm(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Thomas Gleixnerda020ce2019-08-21 21:08:58 +0200950 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
Thomas Gleixner692117c2019-08-19 16:31:46 +0200951 struct task_struct *p = timer->it.cpu.task;
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200952 struct sighand_struct *sighand;
953 unsigned long flags;
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100954 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Thomas Gleixner692117c2019-08-19 16:31:46 +0200956 if (WARN_ON_ONCE(!p))
957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
960 * Fetch the current sample and update the timer's expiry time.
961 */
962 if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200963 now = cpu_clock_sample(clkid, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 bump_cpu_timer(timer, now);
Frederic Weisbecker724a37132013-10-10 16:55:57 +0200965 if (unlikely(p->exit_state))
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200966 return;
Frederic Weisbecker724a37132013-10-10 16:55:57 +0200967
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200968 /* Protect timer list r/w in arm_timer() */
969 sighand = lock_task_sighand(p, &flags);
970 if (!sighand)
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else {
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200973 /*
974 * Protect arm_timer() and timer sampling in case of call to
Frederic Weisbeckerebd7e7f2017-01-31 04:09:34 +0100975 * thread_group_cputime().
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200976 */
977 sighand = lock_task_sighand(p, &flags);
978 if (unlikely(sighand == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /*
980 * The process has been reaped.
981 * We can't even collect a sample any more.
982 */
Frederic Weisbecker55ccb612013-06-28 00:06:42 +0000983 timer->it.cpu.expires = 0;
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200984 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200986 /* If the process is dying, no need to rearm */
987 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Thomas Gleixner8c2d74f2019-08-21 21:09:01 +0200989 now = cpu_clock_sample_group(clkid, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 bump_cpu_timer(timer, now);
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200991 /* Leave the sighand locked for the call below. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994 /*
995 * Now re-arm for the new expiry time.
996 */
Stanislaw Gruszka5eb9aa62010-03-11 14:04:38 -0800997 arm_timer(timer);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200998unlock:
Frederic Weisbeckere73d84e2013-10-11 18:56:49 +0200999 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Frank Mayharf06febc2008-09-12 09:54:39 -07001002/**
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001003 * task_cputimers_expired - Check whether posix CPU timers are expired
Frank Mayharf06febc2008-09-12 09:54:39 -07001004 *
Thomas Gleixner001f7972019-08-21 21:09:13 +02001005 * @samples: Array of current samples for the CPUCLOCK clocks
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001006 * @pct: Pointer to a posix_cputimers container
Frank Mayharf06febc2008-09-12 09:54:39 -07001007 *
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001008 * Returns true if any member of @samples is greater than the corresponding
1009 * member of @pct->bases[CLK].nextevt. False otherwise
Frank Mayharf06febc2008-09-12 09:54:39 -07001010 */
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001011static inline bool
1012task_cputimers_expired(const u64 *sample, struct posix_cputimers *pct)
Frank Mayharf06febc2008-09-12 09:54:39 -07001013{
Thomas Gleixner001f7972019-08-21 21:09:13 +02001014 int i;
1015
1016 for (i = 0; i < CPUCLOCK_MAX; i++) {
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +02001017 if (sample[i] >= pct->bases[i].nextevt)
Thomas Gleixner001f7972019-08-21 21:09:13 +02001018 return true;
1019 }
1020 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001021}
1022
1023/**
1024 * fastpath_timer_check - POSIX CPU timers fast path.
1025 *
1026 * @tsk: The task (thread) being checked.
Frank Mayharf06febc2008-09-12 09:54:39 -07001027 *
Frank Mayharbb34d922008-09-12 09:54:39 -07001028 * Check the task and thread group timers. If both are zero (there are no
1029 * timers set) return false. Otherwise snapshot the task and thread group
1030 * timers and compare them with the corresponding expiration times. Return
1031 * true if a timer has expired, else return false.
Frank Mayharf06febc2008-09-12 09:54:39 -07001032 */
Thomas Gleixner001f7972019-08-21 21:09:13 +02001033static inline bool fastpath_timer_check(struct task_struct *tsk)
Frank Mayharf06febc2008-09-12 09:54:39 -07001034{
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001035 struct posix_cputimers *pct = &tsk->posix_cputimers;
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001036 struct signal_struct *sig;
Frank Mayharf06febc2008-09-12 09:54:39 -07001037
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001038 if (!expiry_cache_is_inactive(pct)) {
Thomas Gleixner001f7972019-08-21 21:09:13 +02001039 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001040
Thomas Gleixner001f7972019-08-21 21:09:13 +02001041 task_sample_cputime(tsk, samples);
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001042 if (task_cputimers_expired(samples, pct))
Thomas Gleixner001f7972019-08-21 21:09:13 +02001043 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001044 }
Oleg Nesterovad133ba2008-11-17 15:39:47 +01001045
1046 sig = tsk->signal;
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001047 pct = &sig->posix_cputimers;
Jason Lowc8d75aa2015-10-14 12:07:56 -07001048 /*
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001049 * Check if thread group timers expired when timers are active and
1050 * no other thread in the group is already handling expiry for
1051 * thread group cputimers. These fields are read without the
1052 * sighand lock. However, this is fine because this is meant to be
1053 * a fastpath heuristic to determine whether we should try to
1054 * acquire the sighand lock to handle timer expiry.
Jason Lowc8d75aa2015-10-14 12:07:56 -07001055 *
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001056 * In the worst case scenario, if concurrently timers_active is set
1057 * or expiry_active is cleared, but the current thread doesn't see
1058 * the change yet, the timer checks are delayed until the next
1059 * thread in the group gets a scheduler interrupt to handle the
1060 * timer. This isn't an issue in practice because these types of
1061 * delays with signals actually getting sent are expected.
Jason Lowc8d75aa2015-10-14 12:07:56 -07001062 */
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001063 if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
Thomas Gleixner001f7972019-08-21 21:09:13 +02001064 u64 samples[CPUCLOCK_MAX];
Frank Mayharbb34d922008-09-12 09:54:39 -07001065
Thomas Gleixner001f7972019-08-21 21:09:13 +02001066 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
1067 samples);
Oleg Nesterov8d1f4312010-06-11 20:04:46 +02001068
Thomas Gleixner244d49e2019-08-21 21:09:24 +02001069 if (task_cputimers_expired(samples, pct))
Thomas Gleixner001f7972019-08-21 21:09:13 +02001070 return true;
Frank Mayharbb34d922008-09-12 09:54:39 -07001071 }
Oleg Nesterov37bebc72009-03-23 20:34:11 +01001072
Juri Lelli34be3932017-12-12 12:10:24 +01001073 if (dl_task(tsk) && tsk->dl.dl_overrun)
Thomas Gleixner001f7972019-08-21 21:09:13 +02001074 return true;
Juri Lelli34be3932017-12-12 12:10:24 +01001075
Thomas Gleixner001f7972019-08-21 21:09:13 +02001076 return false;
Frank Mayharf06febc2008-09-12 09:54:39 -07001077}
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079/*
1080 * This is called from the timer interrupt handler. The irq handler has
1081 * already updated our counts. We need to check if any timers fire now.
1082 * Interrupts are disabled.
1083 */
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001084void run_posix_cpu_timers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001086 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct k_itimer *timer, *next;
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001088 unsigned long flags;
Thomas Gleixnerdce3e8f2019-08-19 16:31:47 +02001089 LIST_HEAD(firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Frederic Weisbeckera6968222017-11-06 16:01:28 +01001091 lockdep_assert_irqs_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
Frank Mayharf06febc2008-09-12 09:54:39 -07001094 * The fast path checks that there are no expired thread or thread
Frank Mayharbb34d922008-09-12 09:54:39 -07001095 * group timers. If that's so, just return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
Frank Mayharbb34d922008-09-12 09:54:39 -07001097 if (!fastpath_timer_check(tsk))
Frank Mayharf06febc2008-09-12 09:54:39 -07001098 return;
Ingo Molnar5ce73a42008-09-14 17:11:46 +02001099
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001100 if (!lock_task_sighand(tsk, &flags))
1101 return;
Frank Mayharbb34d922008-09-12 09:54:39 -07001102 /*
1103 * Here we take off tsk->signal->cpu_timers[N] and
1104 * tsk->cpu_timers[N] all the timers that are firing, and
1105 * put them on the firing list.
1106 */
1107 check_thread_timers(tsk, &firing);
Jason Low934715a2015-10-14 12:07:54 -07001108
1109 check_process_timers(tsk, &firing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Frank Mayharbb34d922008-09-12 09:54:39 -07001111 /*
1112 * We must release these locks before taking any timer's lock.
1113 * There is a potential race with timer deletion here, as the
1114 * siglock now protects our private firing list. We have set
1115 * the firing flag in each timer, so that a deletion attempt
1116 * that gets the timer lock before we do will give it up and
1117 * spin until we've taken care of that timer below.
1118 */
Oleg Nesterov0bdd2ed2010-06-11 01:10:18 +02001119 unlock_task_sighand(tsk, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /*
1122 * Now that all the timers on our list have the firing flag,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001123 * no one will touch their list entries but us. We'll take
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * each timer's lock before clearing its firing flag, so no
1125 * timer call will interfere.
1126 */
1127 list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001128 int cpu_firing;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_lock(&timer->it_lock);
1131 list_del_init(&timer->it.cpu.entry);
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001132 cpu_firing = timer->it.cpu.firing;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 timer->it.cpu.firing = 0;
1134 /*
1135 * The firing flag is -1 if we collided with a reset
1136 * of the timer, which already reported this
1137 * almost-firing as an overrun. So don't generate an event.
1138 */
H Hartley Sweeten6e85c5b2009-04-29 19:14:32 -04001139 if (likely(cpu_firing >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 cpu_timer_fire(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 spin_unlock(&timer->it_lock);
1142 }
1143}
1144
1145/*
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001146 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
Frank Mayharf06febc2008-09-12 09:54:39 -07001147 * The tsk->sighand->siglock must be held by the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001149void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001150 u64 *newval, u64 *oldval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001152 u64 now, *nextevt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001154 if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
Thomas Gleixner692117c2019-08-19 16:31:46 +02001155 return;
1156
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001157 nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001158 now = cpu_clock_sample_group(clkid, tsk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Thomas Gleixner5405d002019-08-21 21:08:59 +02001160 if (oldval) {
Stanislaw Gruszkaf55db602010-03-11 14:04:37 -08001161 /*
1162 * We are setting itimer. The *oldval is absolute and we update
1163 * it to be relative, *newval argument is relative and we update
1164 * it to be absolute.
1165 */
Martin Schwidefsky64861632011-12-15 14:56:09 +01001166 if (*oldval) {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001167 if (*oldval <= now) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* Just about to fire. */
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001169 *oldval = TICK_NSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else {
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001171 *oldval -= now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174
Martin Schwidefsky64861632011-12-15 14:56:09 +01001175 if (!*newval)
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001176 return;
Frederic Weisbecker858cf3a2017-01-31 04:09:35 +01001177 *newval += now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
1180 /*
Thomas Gleixner1b0dd962019-08-21 21:09:09 +02001181 * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
1182 * expiry cache is also used by RLIMIT_CPU!.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Thomas Gleixner2bbdbda2019-08-21 21:09:19 +02001184 if (*newval < *nextevt)
Thomas Gleixner87dc6442019-08-26 20:22:24 +02001185 *nextevt = *newval;
Frederic Weisbeckerb7878302015-07-17 22:25:49 +02001186
1187 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Toyo Abee4b76552006-09-29 02:00:29 -07001190static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001191 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Al Viro86a9c442017-06-07 09:42:26 +01001193 struct itimerspec64 it;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001194 struct k_itimer timer;
1195 u64 expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int error;
1197
1198 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * Set up a temporary timer and then wait for it to go off.
1200 */
1201 memset(&timer, 0, sizeof timer);
1202 spin_lock_init(&timer.it_lock);
1203 timer.it_clock = which_clock;
1204 timer.it_overrun = -1;
1205 error = posix_cpu_timer_create(&timer);
1206 timer.it_process = current;
1207 if (!error) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -07001208 static struct itimerspec64 zero_it;
Al Viroedbeda42017-06-07 09:42:31 +01001209 struct restart_block *restart;
Toyo Abee4b76552006-09-29 02:00:29 -07001210
Al Viroedbeda42017-06-07 09:42:31 +01001211 memset(&it, 0, sizeof(it));
Al Viro86a9c442017-06-07 09:42:26 +01001212 it.it_value = *rqtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 spin_lock_irq(&timer.it_lock);
Al Viro86a9c442017-06-07 09:42:26 +01001215 error = posix_cpu_timer_set(&timer, flags, &it, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (error) {
1217 spin_unlock_irq(&timer.it_lock);
1218 return error;
1219 }
1220
1221 while (!signal_pending(current)) {
Frederic Weisbecker55ccb612013-06-28 00:06:42 +00001222 if (timer.it.cpu.expires == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /*
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001224 * Our timer fired and was reset, below
1225 * deletion can not fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001227 posix_cpu_timer_del(&timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 spin_unlock_irq(&timer.it_lock);
1229 return 0;
1230 }
1231
1232 /*
1233 * Block until cpu_timer_fire (or a signal) wakes us.
1234 */
1235 __set_current_state(TASK_INTERRUPTIBLE);
1236 spin_unlock_irq(&timer.it_lock);
1237 schedule();
1238 spin_lock_irq(&timer.it_lock);
1239 }
1240
1241 /*
1242 * We were interrupted by a signal.
1243 */
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001244 expires = timer.it.cpu.expires;
Al Viro86a9c442017-06-07 09:42:26 +01001245 error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001246 if (!error) {
1247 /*
1248 * Timer is now unarmed, deletion can not fail.
1249 */
1250 posix_cpu_timer_del(&timer);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 spin_unlock_irq(&timer.it_lock);
1253
Stanislaw Gruszkae6c42c22013-02-15 11:08:11 +01001254 while (error == TIMER_RETRY) {
1255 /*
1256 * We need to handle case when timer was or is in the
1257 * middle of firing. In other cases we already freed
1258 * resources.
1259 */
1260 spin_lock_irq(&timer.it_lock);
1261 error = posix_cpu_timer_del(&timer);
1262 spin_unlock_irq(&timer.it_lock);
1263 }
1264
Al Viro86a9c442017-06-07 09:42:26 +01001265 if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * It actually did fire already.
1268 */
1269 return 0;
1270 }
1271
Toyo Abee4b76552006-09-29 02:00:29 -07001272 error = -ERESTART_RESTARTBLOCK;
Al Viro86a9c442017-06-07 09:42:26 +01001273 /*
1274 * Report back to the user the time still remaining.
1275 */
Al Viroedbeda42017-06-07 09:42:31 +01001276 restart = &current->restart_block;
Thomas Gleixner343d8fc2017-06-13 23:29:14 +02001277 restart->nanosleep.expires = expires;
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001278 if (restart->nanosleep.type != TT_NONE)
1279 error = nanosleep_copyout(restart, &it.it_value);
Toyo Abee4b76552006-09-29 02:00:29 -07001280 }
1281
1282 return error;
1283}
1284
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001285static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
1286
1287static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001288 const struct timespec64 *rqtp)
Toyo Abee4b76552006-09-29 02:00:29 -07001289{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08001290 struct restart_block *restart_block = &current->restart_block;
Toyo Abee4b76552006-09-29 02:00:29 -07001291 int error;
1292
1293 /*
1294 * Diagnose required errors first.
1295 */
1296 if (CPUCLOCK_PERTHREAD(which_clock) &&
1297 (CPUCLOCK_PID(which_clock) == 0 ||
Eric W. Biederman01a21972017-04-13 10:32:16 -05001298 CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
Toyo Abee4b76552006-09-29 02:00:29 -07001299 return -EINVAL;
1300
Al Viro86a9c442017-06-07 09:42:26 +01001301 error = do_cpu_nanosleep(which_clock, flags, rqtp);
Toyo Abee4b76552006-09-29 02:00:29 -07001302
1303 if (error == -ERESTART_RESTARTBLOCK) {
1304
Thomas Gleixner3751f9f2011-02-01 13:51:20 +00001305 if (flags & TIMER_ABSTIME)
Toyo Abee4b76552006-09-29 02:00:29 -07001306 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Toyo Abe1711ef32006-09-29 02:00:28 -07001308 restart_block->fn = posix_cpu_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001309 restart_block->nanosleep.clockid = which_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return error;
1312}
1313
Thomas Gleixnerbc2c8ea2011-02-01 13:52:12 +00001314static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001316 clockid_t which_clock = restart_block->nanosleep.clockid;
Deepa Dinamaniad196382017-03-26 12:04:18 -07001317 struct timespec64 t;
Thomas Gleixner97735f22006-01-09 20:52:37 -08001318
Deepa Dinamaniad196382017-03-26 12:04:18 -07001319 t = ns_to_timespec64(restart_block->nanosleep.expires);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001320
Al Viro86a9c442017-06-07 09:42:26 +01001321 return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Nick Desaulniers29f1b2b2017-12-28 22:11:36 -05001324#define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
1325#define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Thomas Gleixnera924b042006-01-09 20:52:27 -08001327static int process_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001328 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1331}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001332static int process_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001333 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1336}
1337static int process_cpu_timer_create(struct k_itimer *timer)
1338{
1339 timer->it_clock = PROCESS_CLOCK;
1340 return posix_cpu_timer_create(timer);
1341}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001342static int process_cpu_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001343 const struct timespec64 *rqtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Al Viro99e6c0e2017-06-07 09:42:30 +01001345 return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001347static int thread_cpu_clock_getres(const clockid_t which_clock,
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001348 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1351}
Thomas Gleixnera924b042006-01-09 20:52:27 -08001352static int thread_cpu_clock_get(const clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001353 struct timespec64 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 return posix_cpu_clock_get(THREAD_CLOCK, tp);
1356}
1357static int thread_cpu_timer_create(struct k_itimer *timer)
1358{
1359 timer->it_clock = THREAD_CLOCK;
1360 return posix_cpu_timer_create(timer);
1361}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001363const struct k_clock clock_posix_cpu = {
Thomas Gleixner19769452011-02-01 13:51:06 +00001364 .clock_getres = posix_cpu_clock_getres,
1365 .clock_set = posix_cpu_clock_set,
1366 .clock_get = posix_cpu_clock_get,
1367 .timer_create = posix_cpu_timer_create,
1368 .nsleep = posix_cpu_nsleep,
Thomas Gleixner19769452011-02-01 13:51:06 +00001369 .timer_set = posix_cpu_timer_set,
1370 .timer_del = posix_cpu_timer_del,
1371 .timer_get = posix_cpu_timer_get,
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +02001372 .timer_rearm = posix_cpu_timer_rearm,
Thomas Gleixner19769452011-02-01 13:51:06 +00001373};
1374
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001375const struct k_clock clock_process = {
1376 .clock_getres = process_cpu_clock_getres,
1377 .clock_get = process_cpu_clock_get,
1378 .timer_create = process_cpu_timer_create,
1379 .nsleep = process_cpu_nsleep,
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001380};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001382const struct k_clock clock_thread = {
1383 .clock_getres = thread_cpu_clock_getres,
1384 .clock_get = thread_cpu_clock_get,
1385 .timer_create = thread_cpu_timer_create,
1386};