Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * Implement CPU time clocks for the POSIX clock interface. | |
4 | */ | |
5 | ||
3f07c014 | 6 | #include <linux/sched/signal.h> |
32ef5517 | 7 | #include <linux/sched/cputime.h> |
1da177e4 | 8 | #include <linux/posix-timers.h> |
1da177e4 | 9 | #include <linux/errno.h> |
f8bd2258 | 10 | #include <linux/math64.h> |
7c0f6ba6 | 11 | #include <linux/uaccess.h> |
bb34d92f | 12 | #include <linux/kernel_stat.h> |
3f0a525e | 13 | #include <trace/events/timer.h> |
a8572160 FW |
14 | #include <linux/tick.h> |
15 | #include <linux/workqueue.h> | |
edbeda46 | 16 | #include <linux/compat.h> |
34be3930 | 17 | #include <linux/sched/deadline.h> |
1da177e4 | 18 | |
bab0aae9 TG |
19 | #include "posix-timers.h" |
20 | ||
f37fb0aa TG |
21 | static void posix_cpu_timer_rearm(struct k_itimer *timer); |
22 | ||
f06febc9 | 23 | /* |
f55db609 SG |
24 | * Called after updating RLIMIT_CPU to run cpu timer and update |
25 | * tsk->signal->cputime_expires expiration cache if necessary. Needs | |
26 | * siglock protection since other code may update expiration cache as | |
27 | * well. | |
f06febc9 | 28 | */ |
5ab46b34 | 29 | void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new) |
f06febc9 | 30 | { |
858cf3a8 | 31 | u64 nsecs = rlim_new * NSEC_PER_SEC; |
f06febc9 | 32 | |
5ab46b34 | 33 | spin_lock_irq(&task->sighand->siglock); |
858cf3a8 | 34 | set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL); |
5ab46b34 | 35 | spin_unlock_irq(&task->sighand->siglock); |
f06febc9 FM |
36 | } |
37 | ||
6ae40e3f TG |
38 | /* |
39 | * Functions for validating access to tasks. | |
40 | */ | |
41 | static struct task_struct *lookup_task(const pid_t pid, bool thread) | |
1da177e4 | 42 | { |
1da177e4 | 43 | struct task_struct *p; |
1da177e4 | 44 | |
6ae40e3f TG |
45 | if (!pid) |
46 | return thread ? current : current->group_leader; | |
1da177e4 | 47 | |
6ae40e3f TG |
48 | p = find_task_by_vpid(pid); |
49 | if (!p || p == current) | |
50 | return p; | |
51 | if (thread) | |
52 | return same_thread_group(p, current) ? p : NULL; | |
53 | if (p == current) | |
54 | return p; | |
55 | return has_group_leader_pid(p) ? p : NULL; | |
56 | } | |
57 | ||
58 | static struct task_struct *__get_task_for_clock(const clockid_t clock, | |
59 | bool getref) | |
60 | { | |
61 | const bool thread = !!CPUCLOCK_PERTHREAD(clock); | |
62 | const pid_t pid = CPUCLOCK_PID(clock); | |
63 | struct task_struct *p; | |
64 | ||
65 | if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX) | |
66 | return NULL; | |
1da177e4 | 67 | |
c0deae8c | 68 | rcu_read_lock(); |
6ae40e3f TG |
69 | p = lookup_task(pid, thread); |
70 | if (p && getref) | |
71 | get_task_struct(p); | |
c0deae8c | 72 | rcu_read_unlock(); |
6ae40e3f TG |
73 | return p; |
74 | } | |
1da177e4 | 75 | |
6ae40e3f TG |
76 | static inline struct task_struct *get_task_for_clock(const clockid_t clock) |
77 | { | |
78 | return __get_task_for_clock(clock, true); | |
79 | } | |
80 | ||
81 | static inline int validate_clock_permissions(const clockid_t clock) | |
82 | { | |
83 | return __get_task_for_clock(clock, false) ? 0 : -EINVAL; | |
1da177e4 LT |
84 | } |
85 | ||
1da177e4 LT |
86 | /* |
87 | * Update expiry time from increment, and increase overrun count, | |
88 | * given the current clock sample. | |
89 | */ | |
ebd7e7fc | 90 | static void bump_cpu_timer(struct k_itimer *timer, u64 now) |
1da177e4 LT |
91 | { |
92 | int i; | |
ebd7e7fc | 93 | u64 delta, incr; |
1da177e4 | 94 | |
16118794 | 95 | if (!timer->it_interval) |
1da177e4 LT |
96 | return; |
97 | ||
55ccb616 FW |
98 | if (now < timer->it.cpu.expires) |
99 | return; | |
1da177e4 | 100 | |
16118794 | 101 | incr = timer->it_interval; |
55ccb616 | 102 | delta = now + incr - timer->it.cpu.expires; |
1da177e4 | 103 | |
55ccb616 FW |
104 | /* Don't use (incr*2 < delta), incr*2 might overflow. */ |
105 | for (i = 0; incr < delta - incr; i++) | |
106 | incr = incr << 1; | |
107 | ||
108 | for (; i >= 0; incr >>= 1, i--) { | |
109 | if (delta < incr) | |
110 | continue; | |
111 | ||
112 | timer->it.cpu.expires += incr; | |
78c9c4df | 113 | timer->it_overrun += 1LL << i; |
55ccb616 | 114 | delta -= incr; |
1da177e4 LT |
115 | } |
116 | } | |
117 | ||
555347f6 FW |
118 | /** |
119 | * task_cputime_zero - Check a task_cputime struct for all zero fields. | |
120 | * | |
121 | * @cputime: The struct to compare. | |
122 | * | |
123 | * Checks @cputime to see if all fields are zero. Returns true if all fields | |
124 | * are zero, false if any field is nonzero. | |
125 | */ | |
ebd7e7fc | 126 | static inline int task_cputime_zero(const struct task_cputime *cputime) |
555347f6 FW |
127 | { |
128 | if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime) | |
129 | return 1; | |
130 | return 0; | |
131 | } | |
132 | ||
bc2c8ea4 | 133 | static int |
d2e3e0ca | 134 | posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp) |
1da177e4 | 135 | { |
6ae40e3f TG |
136 | int error = validate_clock_permissions(which_clock); |
137 | ||
1da177e4 LT |
138 | if (!error) { |
139 | tp->tv_sec = 0; | |
140 | tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); | |
141 | if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) { | |
142 | /* | |
143 | * If sched_clock is using a cycle counter, we | |
144 | * don't have any idea of its true resolution | |
145 | * exported, but it is much more than 1s/HZ. | |
146 | */ | |
147 | tp->tv_nsec = 1; | |
148 | } | |
149 | } | |
150 | return error; | |
151 | } | |
152 | ||
bc2c8ea4 | 153 | static int |
6ae40e3f | 154 | posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp) |
1da177e4 | 155 | { |
6ae40e3f TG |
156 | int error = validate_clock_permissions(clock); |
157 | ||
1da177e4 LT |
158 | /* |
159 | * You can never reset a CPU clock, but we check for other errors | |
160 | * in the call before failing with EPERM. | |
161 | */ | |
6ae40e3f | 162 | return error ? : -EPERM; |
1da177e4 LT |
163 | } |
164 | ||
1da177e4 | 165 | /* |
2092c1d4 | 166 | * Sample a per-thread clock for the given task. clkid is validated. |
1da177e4 | 167 | */ |
8c2d74f0 | 168 | static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p) |
1da177e4 | 169 | { |
ab693c5a TG |
170 | u64 utime, stime; |
171 | ||
172 | if (clkid == CPUCLOCK_SCHED) | |
173 | return task_sched_runtime(p); | |
174 | ||
175 | task_cputime(p, &utime, &stime); | |
176 | ||
2092c1d4 | 177 | switch (clkid) { |
1da177e4 | 178 | case CPUCLOCK_PROF: |
ab693c5a | 179 | return utime + stime; |
1da177e4 | 180 | case CPUCLOCK_VIRT: |
ab693c5a | 181 | return utime; |
2092c1d4 TG |
182 | default: |
183 | WARN_ON_ONCE(1); | |
1da177e4 | 184 | } |
8c2d74f0 | 185 | return 0; |
1da177e4 LT |
186 | } |
187 | ||
1018016c JL |
188 | /* |
189 | * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg | |
190 | * to avoid race conditions with concurrent updates to cputime. | |
191 | */ | |
192 | static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime) | |
4da94d49 | 193 | { |
1018016c JL |
194 | u64 curr_cputime; |
195 | retry: | |
196 | curr_cputime = atomic64_read(cputime); | |
197 | if (sum_cputime > curr_cputime) { | |
198 | if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime) | |
199 | goto retry; | |
200 | } | |
201 | } | |
4da94d49 | 202 | |
ebd7e7fc | 203 | static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum) |
1018016c | 204 | { |
71107445 JL |
205 | __update_gt_cputime(&cputime_atomic->utime, sum->utime); |
206 | __update_gt_cputime(&cputime_atomic->stime, sum->stime); | |
207 | __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); | |
1018016c | 208 | } |
4da94d49 | 209 | |
71107445 | 210 | /* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */ |
ebd7e7fc | 211 | static inline void sample_cputime_atomic(struct task_cputime *times, |
71107445 | 212 | struct task_cputime_atomic *atomic_times) |
1018016c | 213 | { |
71107445 JL |
214 | times->utime = atomic64_read(&atomic_times->utime); |
215 | times->stime = atomic64_read(&atomic_times->stime); | |
216 | times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime); | |
4da94d49 PZ |
217 | } |
218 | ||
19298fbf TG |
219 | /** |
220 | * thread_group_sample_cputime - Sample cputime for a given task | |
221 | * @tsk: Task for which cputime needs to be started | |
222 | * @iimes: Storage for time samples | |
223 | * | |
224 | * Called from sys_getitimer() to calculate the expiry time of an active | |
225 | * timer. That means group cputime accounting is already active. Called | |
226 | * with task sighand lock held. | |
227 | * | |
228 | * Updates @times with an uptodate sample of the thread group cputimes. | |
229 | */ | |
230 | void thread_group_sample_cputime(struct task_struct *tsk, | |
231 | struct task_cputime *times) | |
232 | { | |
233 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; | |
234 | ||
235 | WARN_ON_ONCE(!cputimer->running); | |
236 | ||
237 | sample_cputime_atomic(times, &cputimer->cputime_atomic); | |
238 | } | |
239 | ||
c506bef4 TG |
240 | /** |
241 | * thread_group_start_cputime - Start cputime and return a sample | |
242 | * @tsk: Task for which cputime needs to be started | |
243 | * @iimes: Storage for time samples | |
244 | * | |
245 | * The thread group cputime accouting is avoided when there are no posix | |
246 | * CPU timers armed. Before starting a timer it's required to check whether | |
247 | * the time accounting is active. If not, a full update of the atomic | |
248 | * accounting store needs to be done and the accounting enabled. | |
249 | * | |
250 | * Updates @times with an uptodate sample of the thread group cputimes. | |
251 | */ | |
252 | static void | |
253 | thread_group_start_cputime(struct task_struct *tsk, struct task_cputime *times) | |
4da94d49 PZ |
254 | { |
255 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; | |
ebd7e7fc | 256 | struct task_cputime sum; |
4da94d49 | 257 | |
1018016c JL |
258 | /* Check if cputimer isn't running. This is accessed without locking. */ |
259 | if (!READ_ONCE(cputimer->running)) { | |
4da94d49 PZ |
260 | /* |
261 | * The POSIX timer interface allows for absolute time expiry | |
262 | * values through the TIMER_ABSTIME flag, therefore we have | |
1018016c | 263 | * to synchronize the timer to the clock every time we start it. |
4da94d49 | 264 | */ |
ebd7e7fc | 265 | thread_group_cputime(tsk, &sum); |
71107445 | 266 | update_gt_cputime(&cputimer->cputime_atomic, &sum); |
1018016c JL |
267 | |
268 | /* | |
269 | * We're setting cputimer->running without a lock. Ensure | |
270 | * this only gets written to in one operation. We set | |
271 | * running after update_gt_cputime() as a small optimization, | |
272 | * but barriers are not required because update_gt_cputime() | |
273 | * can handle concurrent updates. | |
274 | */ | |
d5c373eb | 275 | WRITE_ONCE(cputimer->running, true); |
1018016c | 276 | } |
71107445 | 277 | sample_cputime_atomic(times, &cputimer->cputime_atomic); |
4da94d49 PZ |
278 | } |
279 | ||
1da177e4 | 280 | /* |
24ab7f5a TG |
281 | * Sample a process (thread group) clock for the given task clkid. If the |
282 | * group's cputime accounting is already enabled, read the atomic | |
283 | * store. Otherwise a full update is required. Task's sighand lock must be | |
2092c1d4 TG |
284 | * held to protect the task traversal on a full update. clkid is already |
285 | * validated. | |
1da177e4 | 286 | */ |
8c2d74f0 TG |
287 | static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p, |
288 | bool start) | |
1da177e4 | 289 | { |
24ab7f5a | 290 | struct thread_group_cputimer *cputimer = &p->signal->cputimer; |
ebd7e7fc | 291 | struct task_cputime cputime; |
f06febc9 | 292 | |
24ab7f5a TG |
293 | if (!READ_ONCE(cputimer->running)) { |
294 | if (start) | |
295 | thread_group_start_cputime(p, &cputime); | |
296 | else | |
297 | thread_group_cputime(p, &cputime); | |
298 | } else { | |
299 | sample_cputime_atomic(&cputime, &cputimer->cputime_atomic); | |
300 | } | |
301 | ||
2092c1d4 | 302 | switch (clkid) { |
1da177e4 | 303 | case CPUCLOCK_PROF: |
8c2d74f0 | 304 | return cputime.utime + cputime.stime; |
1da177e4 | 305 | case CPUCLOCK_VIRT: |
8c2d74f0 | 306 | return cputime.utime; |
1da177e4 | 307 | case CPUCLOCK_SCHED: |
8c2d74f0 | 308 | return cputime.sum_exec_runtime; |
2092c1d4 TG |
309 | default: |
310 | WARN_ON_ONCE(1); | |
1da177e4 | 311 | } |
8c2d74f0 | 312 | return 0; |
1da177e4 LT |
313 | } |
314 | ||
bfcf3e92 | 315 | static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp) |
33ab0fec | 316 | { |
bfcf3e92 TG |
317 | const clockid_t clkid = CPUCLOCK_WHICH(clock); |
318 | struct task_struct *tsk; | |
319 | u64 t; | |
33ab0fec | 320 | |
bfcf3e92 TG |
321 | tsk = get_task_for_clock(clock); |
322 | if (!tsk) | |
323 | return -EINVAL; | |
1da177e4 | 324 | |
bfcf3e92 | 325 | if (CPUCLOCK_PERTHREAD(clock)) |
8c2d74f0 | 326 | t = cpu_clock_sample(clkid, tsk); |
bfcf3e92 | 327 | else |
8c2d74f0 | 328 | t = cpu_clock_sample_group(clkid, tsk, false); |
bfcf3e92 | 329 | put_task_struct(tsk); |
1da177e4 | 330 | |
bfcf3e92 TG |
331 | *tp = ns_to_timespec64(t); |
332 | return 0; | |
1da177e4 LT |
333 | } |
334 | ||
1da177e4 LT |
335 | /* |
336 | * Validate the clockid_t for a new CPU-clock timer, and initialize the timer. | |
ba5ea951 SG |
337 | * This is called from sys_timer_create() and do_cpu_nanosleep() with the |
338 | * new timer already all-zeros initialized. | |
1da177e4 | 339 | */ |
bc2c8ea4 | 340 | static int posix_cpu_timer_create(struct k_itimer *new_timer) |
1da177e4 | 341 | { |
e5a8b65b | 342 | struct task_struct *p = get_task_for_clock(new_timer->it_clock); |
1da177e4 | 343 | |
e5a8b65b | 344 | if (!p) |
1da177e4 LT |
345 | return -EINVAL; |
346 | ||
d97bb75d | 347 | new_timer->kclock = &clock_posix_cpu; |
1da177e4 | 348 | INIT_LIST_HEAD(&new_timer->it.cpu.entry); |
1da177e4 | 349 | new_timer->it.cpu.task = p; |
e5a8b65b | 350 | return 0; |
1da177e4 LT |
351 | } |
352 | ||
353 | /* | |
354 | * Clean up a CPU-clock timer that is about to be destroyed. | |
355 | * This is called from timer deletion with the timer already locked. | |
356 | * If we return TIMER_RETRY, it's necessary to release the timer's lock | |
357 | * and try again. (This happens when the timer is in the middle of firing.) | |
358 | */ | |
bc2c8ea4 | 359 | static int posix_cpu_timer_del(struct k_itimer *timer) |
1da177e4 | 360 | { |
108150ea | 361 | int ret = 0; |
3d7a1427 FW |
362 | unsigned long flags; |
363 | struct sighand_struct *sighand; | |
364 | struct task_struct *p = timer->it.cpu.task; | |
1da177e4 | 365 | |
692117c1 TG |
366 | if (WARN_ON_ONCE(!p)) |
367 | return -EINVAL; | |
108150ea | 368 | |
3d7a1427 FW |
369 | /* |
370 | * Protect against sighand release/switch in exit/exec and process/ | |
371 | * thread timer list entry concurrent read/writes. | |
372 | */ | |
373 | sighand = lock_task_sighand(p, &flags); | |
374 | if (unlikely(sighand == NULL)) { | |
a3222f88 FW |
375 | /* |
376 | * We raced with the reaping of the task. | |
377 | * The deletion should have cleared us off the list. | |
378 | */ | |
531f64fd | 379 | WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry)); |
a3222f88 | 380 | } else { |
a3222f88 FW |
381 | if (timer->it.cpu.firing) |
382 | ret = TIMER_RETRY; | |
383 | else | |
384 | list_del(&timer->it.cpu.entry); | |
3d7a1427 FW |
385 | |
386 | unlock_task_sighand(p, &flags); | |
1da177e4 | 387 | } |
a3222f88 FW |
388 | |
389 | if (!ret) | |
390 | put_task_struct(p); | |
1da177e4 | 391 | |
108150ea | 392 | return ret; |
1da177e4 LT |
393 | } |
394 | ||
af82eb3c | 395 | static void cleanup_timers_list(struct list_head *head) |
1a7fa510 FW |
396 | { |
397 | struct cpu_timer_list *timer, *next; | |
398 | ||
a0b2062b | 399 | list_for_each_entry_safe(timer, next, head, entry) |
1a7fa510 | 400 | list_del_init(&timer->entry); |
1a7fa510 FW |
401 | } |
402 | ||
1da177e4 | 403 | /* |
7cb9a94c TG |
404 | * Clean out CPU timers which are still armed when a thread exits. The |
405 | * timers are only removed from the list. No other updates are done. The | |
406 | * corresponding posix timers are still accessible, but cannot be rearmed. | |
407 | * | |
1da177e4 LT |
408 | * This must be called with the siglock held. |
409 | */ | |
2b69942f | 410 | static void cleanup_timers(struct posix_cputimers *pct) |
1da177e4 | 411 | { |
2b69942f TG |
412 | cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_PROF]); |
413 | cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_VIRT]); | |
414 | cleanup_timers_list(&pct->cpu_timers[CPUCLOCK_SCHED]); | |
1da177e4 LT |
415 | } |
416 | ||
417 | /* | |
418 | * These are both called with the siglock held, when the current thread | |
419 | * is being reaped. When the final (leader) thread in the group is reaped, | |
420 | * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit. | |
421 | */ | |
422 | void posix_cpu_timers_exit(struct task_struct *tsk) | |
423 | { | |
2b69942f | 424 | cleanup_timers(&tsk->posix_cputimers); |
1da177e4 LT |
425 | } |
426 | void posix_cpu_timers_exit_group(struct task_struct *tsk) | |
427 | { | |
2b69942f | 428 | cleanup_timers(&tsk->signal->posix_cputimers); |
1da177e4 LT |
429 | } |
430 | ||
ebd7e7fc | 431 | static inline int expires_gt(u64 expires, u64 new_exp) |
d1e3b6d1 | 432 | { |
64861634 | 433 | return expires == 0 || expires > new_exp; |
d1e3b6d1 SG |
434 | } |
435 | ||
1da177e4 LT |
436 | /* |
437 | * Insert the timer on the appropriate list before any timers that | |
e73d84e3 | 438 | * expire later. This must be called with the sighand lock held. |
1da177e4 | 439 | */ |
5eb9aa64 | 440 | static void arm_timer(struct k_itimer *timer) |
1da177e4 LT |
441 | { |
442 | struct task_struct *p = timer->it.cpu.task; | |
443 | struct list_head *head, *listpos; | |
ebd7e7fc | 444 | struct task_cputime *cputime_expires; |
1da177e4 LT |
445 | struct cpu_timer_list *const nt = &timer->it.cpu; |
446 | struct cpu_timer_list *next; | |
1da177e4 | 447 | |
5eb9aa64 | 448 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
2b69942f | 449 | head = p->posix_cputimers.cpu_timers; |
5eb9aa64 SG |
450 | cputime_expires = &p->cputime_expires; |
451 | } else { | |
2b69942f | 452 | head = p->signal->posix_cputimers.cpu_timers; |
5eb9aa64 SG |
453 | cputime_expires = &p->signal->cputime_expires; |
454 | } | |
1da177e4 LT |
455 | head += CPUCLOCK_WHICH(timer->it_clock); |
456 | ||
1da177e4 | 457 | listpos = head; |
5eb9aa64 | 458 | list_for_each_entry(next, head, entry) { |
55ccb616 | 459 | if (nt->expires < next->expires) |
5eb9aa64 SG |
460 | break; |
461 | listpos = &next->entry; | |
1da177e4 LT |
462 | } |
463 | list_add(&nt->entry, listpos); | |
464 | ||
465 | if (listpos == head) { | |
ebd7e7fc | 466 | u64 exp = nt->expires; |
5eb9aa64 | 467 | |
1da177e4 | 468 | /* |
5eb9aa64 SG |
469 | * We are the new earliest-expiring POSIX 1.b timer, hence |
470 | * need to update expiration cache. Take into account that | |
471 | * for process timers we share expiration cache with itimers | |
472 | * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. | |
1da177e4 LT |
473 | */ |
474 | ||
5eb9aa64 SG |
475 | switch (CPUCLOCK_WHICH(timer->it_clock)) { |
476 | case CPUCLOCK_PROF: | |
ebd7e7fc FW |
477 | if (expires_gt(cputime_expires->prof_exp, exp)) |
478 | cputime_expires->prof_exp = exp; | |
5eb9aa64 SG |
479 | break; |
480 | case CPUCLOCK_VIRT: | |
ebd7e7fc FW |
481 | if (expires_gt(cputime_expires->virt_exp, exp)) |
482 | cputime_expires->virt_exp = exp; | |
5eb9aa64 SG |
483 | break; |
484 | case CPUCLOCK_SCHED: | |
ebd7e7fc | 485 | if (expires_gt(cputime_expires->sched_exp, exp)) |
55ccb616 | 486 | cputime_expires->sched_exp = exp; |
5eb9aa64 | 487 | break; |
1da177e4 | 488 | } |
b7878300 FW |
489 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
490 | tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); | |
491 | else | |
492 | tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); | |
1da177e4 | 493 | } |
1da177e4 LT |
494 | } |
495 | ||
496 | /* | |
497 | * The timer is locked, fire it and arrange for its reload. | |
498 | */ | |
499 | static void cpu_timer_fire(struct k_itimer *timer) | |
500 | { | |
1f169f84 SG |
501 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { |
502 | /* | |
503 | * User don't want any signal. | |
504 | */ | |
55ccb616 | 505 | timer->it.cpu.expires = 0; |
1f169f84 | 506 | } else if (unlikely(timer->sigq == NULL)) { |
1da177e4 LT |
507 | /* |
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); | |
55ccb616 | 512 | timer->it.cpu.expires = 0; |
16118794 | 513 | } else if (!timer->it_interval) { |
1da177e4 LT |
514 | /* |
515 | * One-shot timer. Clear it as soon as it's fired. | |
516 | */ | |
517 | posix_timer_event(timer, 0); | |
55ccb616 | 518 | timer->it.cpu.expires = 0; |
1da177e4 LT |
519 | } 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 | */ | |
f37fb0aa | 526 | posix_cpu_timer_rearm(timer); |
af888d67 | 527 | ++timer->it_requeue_pending; |
1da177e4 LT |
528 | } |
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 | */ | |
e73d84e3 | 537 | static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, |
5f252b32 | 538 | struct itimerspec64 *new, struct itimerspec64 *old) |
1da177e4 | 539 | { |
c7a37c6f | 540 | clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
ebd7e7fc | 541 | u64 old_expires, new_expires, old_incr, val; |
c7a37c6f TG |
542 | struct task_struct *p = timer->it.cpu.task; |
543 | struct sighand_struct *sighand; | |
544 | unsigned long flags; | |
1da177e4 LT |
545 | int ret; |
546 | ||
692117c1 TG |
547 | if (WARN_ON_ONCE(!p)) |
548 | return -EINVAL; | |
1da177e4 | 549 | |
098b0e01 TG |
550 | /* |
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)); | |
1da177e4 | 555 | |
1da177e4 | 556 | /* |
e73d84e3 FW |
557 | * 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 | |
1da177e4 LT |
563 | * longer get any information about it at all. |
564 | */ | |
e73d84e3 | 565 | if (unlikely(sighand == NULL)) { |
1da177e4 LT |
566 | return -ESRCH; |
567 | } | |
568 | ||
569 | /* | |
570 | * Disarm any old timer after extracting its expiry time. | |
571 | */ | |
a69ac4a7 ON |
572 | |
573 | ret = 0; | |
16118794 | 574 | old_incr = timer->it_interval; |
1da177e4 | 575 | old_expires = timer->it.cpu.expires; |
a69ac4a7 ON |
576 | 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); | |
1da177e4 LT |
581 | |
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 | */ | |
8c2d74f0 TG |
590 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
591 | val = cpu_clock_sample(clkid, p); | |
592 | else | |
593 | val = cpu_clock_sample_group(clkid, p, true); | |
1da177e4 LT |
594 | |
595 | if (old) { | |
55ccb616 | 596 | if (old_expires == 0) { |
1da177e4 LT |
597 | 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); | |
55ccb616 FW |
611 | if (val < timer->it.cpu.expires) { |
612 | old_expires = timer->it.cpu.expires - val; | |
5f252b32 | 613 | old->it_value = ns_to_timespec64(old_expires); |
1da177e4 LT |
614 | } else { |
615 | old->it_value.tv_nsec = 1; | |
616 | old->it_value.tv_sec = 0; | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
a69ac4a7 | 621 | if (unlikely(ret)) { |
1da177e4 LT |
622 | /* |
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 | */ | |
e73d84e3 | 628 | unlock_task_sighand(p, &flags); |
1da177e4 LT |
629 | goto out; |
630 | } | |
631 | ||
e73d84e3 | 632 | if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) { |
55ccb616 | 633 | new_expires += val; |
1da177e4 LT |
634 | } |
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; | |
55ccb616 | 642 | if (new_expires != 0 && val < new_expires) { |
5eb9aa64 | 643 | arm_timer(timer); |
1da177e4 LT |
644 | } |
645 | ||
e73d84e3 | 646 | unlock_task_sighand(p, &flags); |
1da177e4 LT |
647 | /* |
648 | * Install the new reload setting, and | |
649 | * set up the signal and overrun bookkeeping. | |
650 | */ | |
16118794 | 651 | timer->it_interval = timespec64_to_ktime(new->it_interval); |
1da177e4 LT |
652 | |
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 | ||
55ccb616 | 663 | if (new_expires != 0 && !(val < new_expires)) { |
1da177e4 LT |
664 | /* |
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: | |
ebd7e7fc | 674 | if (old) |
5f252b32 | 675 | old->it_interval = ns_to_timespec64(old_incr); |
b7878300 | 676 | |
1da177e4 LT |
677 | return ret; |
678 | } | |
679 | ||
5f252b32 | 680 | static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp) |
1da177e4 | 681 | { |
99093c5b | 682 | clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
1da177e4 | 683 | struct task_struct *p = timer->it.cpu.task; |
692117c1 | 684 | u64 now; |
1da177e4 | 685 | |
692117c1 TG |
686 | if (WARN_ON_ONCE(!p)) |
687 | return; | |
a3222f88 | 688 | |
1da177e4 LT |
689 | /* |
690 | * Easy part: convert the reload time. | |
691 | */ | |
16118794 | 692 | itp->it_interval = ktime_to_timespec64(timer->it_interval); |
1da177e4 | 693 | |
eabdec04 | 694 | if (!timer->it.cpu.expires) |
1da177e4 | 695 | return; |
1da177e4 | 696 | |
1da177e4 LT |
697 | /* |
698 | * Sample the clock to take the difference with the expiry time. | |
699 | */ | |
700 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | |
8c2d74f0 | 701 | now = cpu_clock_sample(clkid, p); |
1da177e4 | 702 | } else { |
e73d84e3 FW |
703 | 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 | |
ebd7e7fc | 709 | * thread_group_cputime(). |
e73d84e3 FW |
710 | */ |
711 | sighand = lock_task_sighand(p, &flags); | |
712 | if (unlikely(sighand == NULL)) { | |
1da177e4 LT |
713 | /* |
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 | */ | |
55ccb616 | 718 | timer->it.cpu.expires = 0; |
2c13ce8f | 719 | return; |
1da177e4 | 720 | } else { |
8c2d74f0 | 721 | now = cpu_clock_sample_group(clkid, p, false); |
e73d84e3 | 722 | unlock_task_sighand(p, &flags); |
1da177e4 | 723 | } |
1da177e4 LT |
724 | } |
725 | ||
55ccb616 | 726 | if (now < timer->it.cpu.expires) { |
5f252b32 | 727 | itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now); |
1da177e4 LT |
728 | } 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 | ||
2473f3e7 FW |
738 | static unsigned long long |
739 | check_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 | ||
757 | return 0; | |
758 | } | |
759 | ||
34be3930 JL |
760 | static inline void check_dl_overrun(struct task_struct *tsk) |
761 | { | |
762 | if (tsk->dl.dl_overrun) { | |
763 | tsk->dl.dl_overrun = 0; | |
764 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); | |
765 | } | |
766 | } | |
767 | ||
1da177e4 LT |
768 | /* |
769 | * Check for any per-thread CPU timers that have fired and move them off | |
770 | * the tsk->cpu_timers[N] list onto the firing list. Here we update the | |
771 | * tsk->it_*_expires values to reflect the remaining thread CPU timers. | |
772 | */ | |
773 | static void check_thread_timers(struct task_struct *tsk, | |
774 | struct list_head *firing) | |
775 | { | |
2b69942f | 776 | struct list_head *timers = tsk->posix_cputimers.cpu_timers; |
ebd7e7fc | 777 | struct task_cputime *tsk_expires = &tsk->cputime_expires; |
0476ff2c | 778 | u64 expires, stime, utime; |
d4bb5274 | 779 | unsigned long soft; |
1da177e4 | 780 | |
34be3930 JL |
781 | if (dl_task(tsk)) |
782 | check_dl_overrun(tsk); | |
783 | ||
934715a1 JL |
784 | /* |
785 | * If cputime_expires is zero, then there are no active | |
786 | * per thread CPU timers. | |
787 | */ | |
788 | if (task_cputime_zero(&tsk->cputime_expires)) | |
789 | return; | |
790 | ||
0476ff2c TG |
791 | task_cputime(tsk, &utime, &stime); |
792 | ||
793 | expires = check_timers_list(timers, firing, utime + stime); | |
ebd7e7fc | 794 | tsk_expires->prof_exp = expires; |
1da177e4 | 795 | |
0476ff2c | 796 | expires = check_timers_list(++timers, firing, utime); |
ebd7e7fc | 797 | tsk_expires->virt_exp = expires; |
1da177e4 | 798 | |
2473f3e7 FW |
799 | tsk_expires->sched_exp = check_timers_list(++timers, firing, |
800 | tsk->se.sum_exec_runtime); | |
78f2c7db PZ |
801 | |
802 | /* | |
803 | * Check for the special case thread timers. | |
804 | */ | |
3cf29496 | 805 | soft = task_rlimit(tsk, RLIMIT_RTTIME); |
d4bb5274 | 806 | if (soft != RLIM_INFINITY) { |
3cf29496 | 807 | unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME); |
78f2c7db | 808 | |
5a52dd50 PZ |
809 | if (hard != RLIM_INFINITY && |
810 | tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { | |
78f2c7db PZ |
811 | /* |
812 | * At the hard limit, we just die. | |
813 | * No need to calculate anything else now. | |
814 | */ | |
43fe8b8e TG |
815 | if (print_fatal_signals) { |
816 | pr_info("CPU Watchdog Timeout (hard): %s[%d]\n", | |
817 | tsk->comm, task_pid_nr(tsk)); | |
818 | } | |
78f2c7db PZ |
819 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
820 | return; | |
821 | } | |
d4bb5274 | 822 | if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) { |
78f2c7db PZ |
823 | /* |
824 | * At the soft limit, send a SIGXCPU every second. | |
825 | */ | |
d4bb5274 JS |
826 | if (soft < hard) { |
827 | soft += USEC_PER_SEC; | |
3cf29496 KO |
828 | tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = |
829 | soft; | |
78f2c7db | 830 | } |
43fe8b8e TG |
831 | if (print_fatal_signals) { |
832 | pr_info("RT Watchdog Timeout (soft): %s[%d]\n", | |
833 | tsk->comm, task_pid_nr(tsk)); | |
834 | } | |
78f2c7db PZ |
835 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
836 | } | |
837 | } | |
b7878300 FW |
838 | if (task_cputime_zero(tsk_expires)) |
839 | tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER); | |
1da177e4 LT |
840 | } |
841 | ||
1018016c | 842 | static inline void stop_process_timers(struct signal_struct *sig) |
3fccfd67 | 843 | { |
15365c10 | 844 | struct thread_group_cputimer *cputimer = &sig->cputimer; |
3fccfd67 | 845 | |
1018016c | 846 | /* Turn off cputimer->running. This is done without locking. */ |
d5c373eb | 847 | WRITE_ONCE(cputimer->running, false); |
b7878300 | 848 | tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER); |
3fccfd67 PZ |
849 | } |
850 | ||
42c4ab41 | 851 | static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, |
ebd7e7fc | 852 | u64 *expires, u64 cur_time, int signo) |
42c4ab41 | 853 | { |
64861634 | 854 | if (!it->expires) |
42c4ab41 SG |
855 | return; |
856 | ||
858cf3a8 FW |
857 | if (cur_time >= it->expires) { |
858 | if (it->incr) | |
64861634 | 859 | it->expires += it->incr; |
858cf3a8 | 860 | else |
64861634 | 861 | it->expires = 0; |
42c4ab41 | 862 | |
3f0a525e XG |
863 | trace_itimer_expire(signo == SIGPROF ? |
864 | ITIMER_PROF : ITIMER_VIRTUAL, | |
6883f81a | 865 | task_tgid(tsk), cur_time); |
42c4ab41 SG |
866 | __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); |
867 | } | |
868 | ||
858cf3a8 FW |
869 | if (it->expires && (!*expires || it->expires < *expires)) |
870 | *expires = it->expires; | |
42c4ab41 SG |
871 | } |
872 | ||
1da177e4 LT |
873 | /* |
874 | * Check for any per-thread CPU timers that have fired and move them | |
875 | * off the tsk->*_timers list onto the firing list. Per-thread timers | |
876 | * have already been taken off. | |
877 | */ | |
878 | static void check_process_timers(struct task_struct *tsk, | |
879 | struct list_head *firing) | |
880 | { | |
881 | struct signal_struct *const sig = tsk->signal; | |
2b69942f | 882 | struct list_head *timers = sig->posix_cputimers.cpu_timers; |
ebd7e7fc FW |
883 | u64 utime, ptime, virt_expires, prof_expires; |
884 | u64 sum_sched_runtime, sched_expires; | |
ebd7e7fc | 885 | struct task_cputime cputime; |
d4bb5274 | 886 | unsigned long soft; |
1da177e4 | 887 | |
934715a1 JL |
888 | /* |
889 | * If cputimer is not running, then there are no active | |
890 | * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU). | |
891 | */ | |
892 | if (!READ_ONCE(tsk->signal->cputimer.running)) | |
893 | return; | |
894 | ||
a324956f | 895 | /* |
c8d75aa4 JL |
896 | * Signify that a thread is checking for process timers. |
897 | * Write access to this field is protected by the sighand lock. | |
898 | */ | |
899 | sig->cputimer.checking_timer = true; | |
900 | ||
1da177e4 | 901 | /* |
a324956f TG |
902 | * Collect the current process totals. Group accounting is active |
903 | * so the sample can be taken directly. | |
1da177e4 | 904 | */ |
a324956f | 905 | sample_cputime_atomic(&cputime, &sig->cputimer.cputime_atomic); |
ebd7e7fc FW |
906 | utime = cputime.utime; |
907 | ptime = utime + cputime.stime; | |
f06febc9 | 908 | sum_sched_runtime = cputime.sum_exec_runtime; |
1da177e4 | 909 | |
2473f3e7 FW |
910 | prof_expires = check_timers_list(timers, firing, ptime); |
911 | virt_expires = check_timers_list(++timers, firing, utime); | |
912 | sched_expires = check_timers_list(++timers, firing, sum_sched_runtime); | |
1da177e4 LT |
913 | |
914 | /* | |
915 | * Check for the special case process timers. | |
916 | */ | |
42c4ab41 SG |
917 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, |
918 | SIGPROF); | |
919 | check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, | |
920 | SIGVTALRM); | |
3cf29496 | 921 | soft = task_rlimit(tsk, RLIMIT_CPU); |
d4bb5274 | 922 | if (soft != RLIM_INFINITY) { |
ebd7e7fc | 923 | unsigned long psecs = div_u64(ptime, NSEC_PER_SEC); |
3cf29496 | 924 | unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU); |
ebd7e7fc | 925 | u64 x; |
d4bb5274 | 926 | if (psecs >= hard) { |
1da177e4 LT |
927 | /* |
928 | * At the hard limit, we just die. | |
929 | * No need to calculate anything else now. | |
930 | */ | |
43fe8b8e TG |
931 | if (print_fatal_signals) { |
932 | pr_info("RT Watchdog Timeout (hard): %s[%d]\n", | |
933 | tsk->comm, task_pid_nr(tsk)); | |
934 | } | |
1da177e4 LT |
935 | __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
936 | return; | |
937 | } | |
d4bb5274 | 938 | if (psecs >= soft) { |
1da177e4 LT |
939 | /* |
940 | * At the soft limit, send a SIGXCPU every second. | |
941 | */ | |
43fe8b8e TG |
942 | if (print_fatal_signals) { |
943 | pr_info("CPU Watchdog Timeout (soft): %s[%d]\n", | |
944 | tsk->comm, task_pid_nr(tsk)); | |
945 | } | |
1da177e4 | 946 | __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
d4bb5274 JS |
947 | if (soft < hard) { |
948 | soft++; | |
949 | sig->rlim[RLIMIT_CPU].rlim_cur = soft; | |
1da177e4 LT |
950 | } |
951 | } | |
ebd7e7fc FW |
952 | x = soft * NSEC_PER_SEC; |
953 | if (!prof_expires || x < prof_expires) | |
1da177e4 | 954 | prof_expires = x; |
1da177e4 LT |
955 | } |
956 | ||
ebd7e7fc FW |
957 | sig->cputime_expires.prof_exp = prof_expires; |
958 | sig->cputime_expires.virt_exp = virt_expires; | |
29f87b79 SG |
959 | sig->cputime_expires.sched_exp = sched_expires; |
960 | if (task_cputime_zero(&sig->cputime_expires)) | |
961 | stop_process_timers(sig); | |
c8d75aa4 JL |
962 | |
963 | sig->cputimer.checking_timer = false; | |
1da177e4 LT |
964 | } |
965 | ||
966 | /* | |
96fe3b07 | 967 | * This is called from the signal code (via posixtimer_rearm) |
1da177e4 LT |
968 | * when the last timer signal was delivered and we have to reload the timer. |
969 | */ | |
f37fb0aa | 970 | static void posix_cpu_timer_rearm(struct k_itimer *timer) |
1da177e4 | 971 | { |
da020ce4 | 972 | clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
692117c1 | 973 | struct task_struct *p = timer->it.cpu.task; |
e73d84e3 FW |
974 | struct sighand_struct *sighand; |
975 | unsigned long flags; | |
ebd7e7fc | 976 | u64 now; |
1da177e4 | 977 | |
692117c1 TG |
978 | if (WARN_ON_ONCE(!p)) |
979 | return; | |
1da177e4 LT |
980 | |
981 | /* | |
982 | * Fetch the current sample and update the timer's expiry time. | |
983 | */ | |
984 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | |
8c2d74f0 | 985 | now = cpu_clock_sample(clkid, p); |
1da177e4 | 986 | bump_cpu_timer(timer, now); |
724a3713 | 987 | if (unlikely(p->exit_state)) |
af888d67 | 988 | return; |
724a3713 | 989 | |
e73d84e3 FW |
990 | /* Protect timer list r/w in arm_timer() */ |
991 | sighand = lock_task_sighand(p, &flags); | |
992 | if (!sighand) | |
af888d67 | 993 | return; |
1da177e4 | 994 | } else { |
e73d84e3 FW |
995 | /* |
996 | * Protect arm_timer() and timer sampling in case of call to | |
ebd7e7fc | 997 | * thread_group_cputime(). |
e73d84e3 FW |
998 | */ |
999 | sighand = lock_task_sighand(p, &flags); | |
1000 | if (unlikely(sighand == NULL)) { | |
1da177e4 LT |
1001 | /* |
1002 | * The process has been reaped. | |
1003 | * We can't even collect a sample any more. | |
1004 | */ | |
55ccb616 | 1005 | timer->it.cpu.expires = 0; |
af888d67 | 1006 | return; |
1da177e4 | 1007 | } else if (unlikely(p->exit_state) && thread_group_empty(p)) { |
af888d67 TG |
1008 | /* If the process is dying, no need to rearm */ |
1009 | goto unlock; | |
1da177e4 | 1010 | } |
8c2d74f0 | 1011 | now = cpu_clock_sample_group(clkid, p, true); |
1da177e4 | 1012 | bump_cpu_timer(timer, now); |
e73d84e3 | 1013 | /* Leave the sighand locked for the call below. */ |
1da177e4 LT |
1014 | } |
1015 | ||
1016 | /* | |
1017 | * Now re-arm for the new expiry time. | |
1018 | */ | |
5eb9aa64 | 1019 | arm_timer(timer); |
af888d67 | 1020 | unlock: |
e73d84e3 | 1021 | unlock_task_sighand(p, &flags); |
1da177e4 LT |
1022 | } |
1023 | ||
f06febc9 FM |
1024 | /** |
1025 | * task_cputime_expired - Compare two task_cputime entities. | |
1026 | * | |
1027 | * @sample: The task_cputime structure to be checked for expiration. | |
1028 | * @expires: Expiration times, against which @sample will be checked. | |
1029 | * | |
1030 | * Checks @sample against @expires to see if any field of @sample has expired. | |
1031 | * Returns true if any field of the former is greater than the corresponding | |
1032 | * field of the latter if the latter field is set. Otherwise returns false. | |
1033 | */ | |
ebd7e7fc FW |
1034 | static inline int task_cputime_expired(const struct task_cputime *sample, |
1035 | const struct task_cputime *expires) | |
f06febc9 | 1036 | { |
64861634 | 1037 | if (expires->utime && sample->utime >= expires->utime) |
f06febc9 | 1038 | return 1; |
64861634 | 1039 | if (expires->stime && sample->utime + sample->stime >= expires->stime) |
f06febc9 FM |
1040 | return 1; |
1041 | if (expires->sum_exec_runtime != 0 && | |
1042 | sample->sum_exec_runtime >= expires->sum_exec_runtime) | |
1043 | return 1; | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | /** | |
1048 | * fastpath_timer_check - POSIX CPU timers fast path. | |
1049 | * | |
1050 | * @tsk: The task (thread) being checked. | |
f06febc9 | 1051 | * |
bb34d92f FM |
1052 | * Check the task and thread group timers. If both are zero (there are no |
1053 | * timers set) return false. Otherwise snapshot the task and thread group | |
1054 | * timers and compare them with the corresponding expiration times. Return | |
1055 | * true if a timer has expired, else return false. | |
f06febc9 | 1056 | */ |
bb34d92f | 1057 | static inline int fastpath_timer_check(struct task_struct *tsk) |
f06febc9 | 1058 | { |
ad133ba3 | 1059 | struct signal_struct *sig; |
bb34d92f | 1060 | |
bb34d92f | 1061 | if (!task_cputime_zero(&tsk->cputime_expires)) { |
ebd7e7fc | 1062 | struct task_cputime task_sample; |
bb34d92f | 1063 | |
ebd7e7fc | 1064 | task_cputime(tsk, &task_sample.utime, &task_sample.stime); |
7c177d99 | 1065 | task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime; |
bb34d92f FM |
1066 | if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) |
1067 | return 1; | |
1068 | } | |
ad133ba3 ON |
1069 | |
1070 | sig = tsk->signal; | |
c8d75aa4 JL |
1071 | /* |
1072 | * Check if thread group timers expired when the cputimer is | |
1073 | * running and no other thread in the group is already checking | |
1074 | * for thread group cputimers. These fields are read without the | |
1075 | * sighand lock. However, this is fine because this is meant to | |
1076 | * be a fastpath heuristic to determine whether we should try to | |
1077 | * acquire the sighand lock to check/handle timers. | |
1078 | * | |
1079 | * In the worst case scenario, if 'running' or 'checking_timer' gets | |
1080 | * set but the current thread doesn't see the change yet, we'll wait | |
1081 | * until the next thread in the group gets a scheduler interrupt to | |
1082 | * handle the timer. This isn't an issue in practice because these | |
1083 | * types of delays with signals actually getting sent are expected. | |
1084 | */ | |
1085 | if (READ_ONCE(sig->cputimer.running) && | |
1086 | !READ_ONCE(sig->cputimer.checking_timer)) { | |
ebd7e7fc | 1087 | struct task_cputime group_sample; |
bb34d92f | 1088 | |
71107445 | 1089 | sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic); |
8d1f431c | 1090 | |
bb34d92f FM |
1091 | if (task_cputime_expired(&group_sample, &sig->cputime_expires)) |
1092 | return 1; | |
1093 | } | |
37bebc70 | 1094 | |
34be3930 JL |
1095 | if (dl_task(tsk) && tsk->dl.dl_overrun) |
1096 | return 1; | |
1097 | ||
f55db609 | 1098 | return 0; |
f06febc9 FM |
1099 | } |
1100 | ||
1da177e4 LT |
1101 | /* |
1102 | * This is called from the timer interrupt handler. The irq handler has | |
1103 | * already updated our counts. We need to check if any timers fire now. | |
1104 | * Interrupts are disabled. | |
1105 | */ | |
dce3e8fd | 1106 | void run_posix_cpu_timers(void) |
1da177e4 | 1107 | { |
dce3e8fd | 1108 | struct task_struct *tsk = current; |
1da177e4 | 1109 | struct k_itimer *timer, *next; |
0bdd2ed4 | 1110 | unsigned long flags; |
dce3e8fd | 1111 | LIST_HEAD(firing); |
1da177e4 | 1112 | |
a6968220 | 1113 | lockdep_assert_irqs_disabled(); |
1da177e4 | 1114 | |
1da177e4 | 1115 | /* |
f06febc9 | 1116 | * The fast path checks that there are no expired thread or thread |
bb34d92f | 1117 | * group timers. If that's so, just return. |
1da177e4 | 1118 | */ |
bb34d92f | 1119 | if (!fastpath_timer_check(tsk)) |
f06febc9 | 1120 | return; |
5ce73a4a | 1121 | |
0bdd2ed4 ON |
1122 | if (!lock_task_sighand(tsk, &flags)) |
1123 | return; | |
bb34d92f FM |
1124 | /* |
1125 | * Here we take off tsk->signal->cpu_timers[N] and | |
1126 | * tsk->cpu_timers[N] all the timers that are firing, and | |
1127 | * put them on the firing list. | |
1128 | */ | |
1129 | check_thread_timers(tsk, &firing); | |
934715a1 JL |
1130 | |
1131 | check_process_timers(tsk, &firing); | |
1da177e4 | 1132 | |
bb34d92f FM |
1133 | /* |
1134 | * We must release these locks before taking any timer's lock. | |
1135 | * There is a potential race with timer deletion here, as the | |
1136 | * siglock now protects our private firing list. We have set | |
1137 | * the firing flag in each timer, so that a deletion attempt | |
1138 | * that gets the timer lock before we do will give it up and | |
1139 | * spin until we've taken care of that timer below. | |
1140 | */ | |
0bdd2ed4 | 1141 | unlock_task_sighand(tsk, &flags); |
1da177e4 LT |
1142 | |
1143 | /* | |
1144 | * Now that all the timers on our list have the firing flag, | |
25985edc | 1145 | * no one will touch their list entries but us. We'll take |
1da177e4 LT |
1146 | * each timer's lock before clearing its firing flag, so no |
1147 | * timer call will interfere. | |
1148 | */ | |
1149 | list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) { | |
6e85c5ba HS |
1150 | int cpu_firing; |
1151 | ||
1da177e4 LT |
1152 | spin_lock(&timer->it_lock); |
1153 | list_del_init(&timer->it.cpu.entry); | |
6e85c5ba | 1154 | cpu_firing = timer->it.cpu.firing; |
1da177e4 LT |
1155 | timer->it.cpu.firing = 0; |
1156 | /* | |
1157 | * The firing flag is -1 if we collided with a reset | |
1158 | * of the timer, which already reported this | |
1159 | * almost-firing as an overrun. So don't generate an event. | |
1160 | */ | |
6e85c5ba | 1161 | if (likely(cpu_firing >= 0)) |
1da177e4 | 1162 | cpu_timer_fire(timer); |
1da177e4 LT |
1163 | spin_unlock(&timer->it_lock); |
1164 | } | |
1165 | } | |
1166 | ||
1167 | /* | |
f55db609 | 1168 | * Set one of the process-wide special case CPU timers or RLIMIT_CPU. |
f06febc9 | 1169 | * The tsk->sighand->siglock must be held by the caller. |
1da177e4 LT |
1170 | */ |
1171 | void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, | |
858cf3a8 | 1172 | u64 *newval, u64 *oldval) |
1da177e4 | 1173 | { |
858cf3a8 | 1174 | u64 now; |
1da177e4 | 1175 | |
692117c1 TG |
1176 | if (WARN_ON_ONCE(clock_idx >= CPUCLOCK_SCHED)) |
1177 | return; | |
1178 | ||
8c2d74f0 | 1179 | now = cpu_clock_sample_group(clock_idx, tsk, true); |
1da177e4 | 1180 | |
5405d005 | 1181 | if (oldval) { |
f55db609 SG |
1182 | /* |
1183 | * We are setting itimer. The *oldval is absolute and we update | |
1184 | * it to be relative, *newval argument is relative and we update | |
1185 | * it to be absolute. | |
1186 | */ | |
64861634 | 1187 | if (*oldval) { |
858cf3a8 | 1188 | if (*oldval <= now) { |
1da177e4 | 1189 | /* Just about to fire. */ |
858cf3a8 | 1190 | *oldval = TICK_NSEC; |
1da177e4 | 1191 | } else { |
858cf3a8 | 1192 | *oldval -= now; |
1da177e4 LT |
1193 | } |
1194 | } | |
1195 | ||
64861634 | 1196 | if (!*newval) |
b7878300 | 1197 | return; |
858cf3a8 | 1198 | *newval += now; |
1da177e4 LT |
1199 | } |
1200 | ||
1201 | /* | |
f55db609 SG |
1202 | * Update expiration cache if we are the earliest timer, or eventually |
1203 | * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire. | |
1da177e4 | 1204 | */ |
f55db609 SG |
1205 | switch (clock_idx) { |
1206 | case CPUCLOCK_PROF: | |
858cf3a8 FW |
1207 | if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval)) |
1208 | tsk->signal->cputime_expires.prof_exp = *newval; | |
f55db609 SG |
1209 | break; |
1210 | case CPUCLOCK_VIRT: | |
858cf3a8 FW |
1211 | if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval)) |
1212 | tsk->signal->cputime_expires.virt_exp = *newval; | |
f55db609 | 1213 | break; |
1da177e4 | 1214 | } |
b7878300 FW |
1215 | |
1216 | tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER); | |
1da177e4 LT |
1217 | } |
1218 | ||
e4b76555 | 1219 | static int do_cpu_nanosleep(const clockid_t which_clock, int flags, |
343d8fc2 | 1220 | const struct timespec64 *rqtp) |
1da177e4 | 1221 | { |
86a9c446 | 1222 | struct itimerspec64 it; |
343d8fc2 TG |
1223 | struct k_itimer timer; |
1224 | u64 expires; | |
1da177e4 LT |
1225 | int error; |
1226 | ||
1da177e4 LT |
1227 | /* |
1228 | * Set up a temporary timer and then wait for it to go off. | |
1229 | */ | |
1230 | memset(&timer, 0, sizeof timer); | |
1231 | spin_lock_init(&timer.it_lock); | |
1232 | timer.it_clock = which_clock; | |
1233 | timer.it_overrun = -1; | |
1234 | error = posix_cpu_timer_create(&timer); | |
1235 | timer.it_process = current; | |
1236 | if (!error) { | |
5f252b32 | 1237 | static struct itimerspec64 zero_it; |
edbeda46 | 1238 | struct restart_block *restart; |
e4b76555 | 1239 | |
edbeda46 | 1240 | memset(&it, 0, sizeof(it)); |
86a9c446 | 1241 | it.it_value = *rqtp; |
1da177e4 LT |
1242 | |
1243 | spin_lock_irq(&timer.it_lock); | |
86a9c446 | 1244 | error = posix_cpu_timer_set(&timer, flags, &it, NULL); |
1da177e4 LT |
1245 | if (error) { |
1246 | spin_unlock_irq(&timer.it_lock); | |
1247 | return error; | |
1248 | } | |
1249 | ||
1250 | while (!signal_pending(current)) { | |
55ccb616 | 1251 | if (timer.it.cpu.expires == 0) { |
1da177e4 | 1252 | /* |
e6c42c29 SG |
1253 | * Our timer fired and was reset, below |
1254 | * deletion can not fail. | |
1da177e4 | 1255 | */ |
e6c42c29 | 1256 | posix_cpu_timer_del(&timer); |
1da177e4 LT |
1257 | spin_unlock_irq(&timer.it_lock); |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * Block until cpu_timer_fire (or a signal) wakes us. | |
1263 | */ | |
1264 | __set_current_state(TASK_INTERRUPTIBLE); | |
1265 | spin_unlock_irq(&timer.it_lock); | |
1266 | schedule(); | |
1267 | spin_lock_irq(&timer.it_lock); | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * We were interrupted by a signal. | |
1272 | */ | |
343d8fc2 | 1273 | expires = timer.it.cpu.expires; |
86a9c446 | 1274 | error = posix_cpu_timer_set(&timer, 0, &zero_it, &it); |
e6c42c29 SG |
1275 | if (!error) { |
1276 | /* | |
1277 | * Timer is now unarmed, deletion can not fail. | |
1278 | */ | |
1279 | posix_cpu_timer_del(&timer); | |
1280 | } | |
1da177e4 LT |
1281 | spin_unlock_irq(&timer.it_lock); |
1282 | ||
e6c42c29 SG |
1283 | while (error == TIMER_RETRY) { |
1284 | /* | |
1285 | * We need to handle case when timer was or is in the | |
1286 | * middle of firing. In other cases we already freed | |
1287 | * resources. | |
1288 | */ | |
1289 | spin_lock_irq(&timer.it_lock); | |
1290 | error = posix_cpu_timer_del(&timer); | |
1291 | spin_unlock_irq(&timer.it_lock); | |
1292 | } | |
1293 | ||
86a9c446 | 1294 | if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) { |
1da177e4 LT |
1295 | /* |
1296 | * It actually did fire already. | |
1297 | */ | |
1298 | return 0; | |
1299 | } | |
1300 | ||
e4b76555 | 1301 | error = -ERESTART_RESTARTBLOCK; |
86a9c446 AV |
1302 | /* |
1303 | * Report back to the user the time still remaining. | |
1304 | */ | |
edbeda46 | 1305 | restart = ¤t->restart_block; |
343d8fc2 | 1306 | restart->nanosleep.expires = expires; |
c0edd7c9 DD |
1307 | if (restart->nanosleep.type != TT_NONE) |
1308 | error = nanosleep_copyout(restart, &it.it_value); | |
e4b76555 TA |
1309 | } |
1310 | ||
1311 | return error; | |
1312 | } | |
1313 | ||
bc2c8ea4 TG |
1314 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block); |
1315 | ||
1316 | static int posix_cpu_nsleep(const clockid_t which_clock, int flags, | |
938e7cf2 | 1317 | const struct timespec64 *rqtp) |
e4b76555 | 1318 | { |
f56141e3 | 1319 | struct restart_block *restart_block = ¤t->restart_block; |
e4b76555 TA |
1320 | int error; |
1321 | ||
1322 | /* | |
1323 | * Diagnose required errors first. | |
1324 | */ | |
1325 | if (CPUCLOCK_PERTHREAD(which_clock) && | |
1326 | (CPUCLOCK_PID(which_clock) == 0 || | |
01a21974 | 1327 | CPUCLOCK_PID(which_clock) == task_pid_vnr(current))) |
e4b76555 TA |
1328 | return -EINVAL; |
1329 | ||
86a9c446 | 1330 | error = do_cpu_nanosleep(which_clock, flags, rqtp); |
e4b76555 TA |
1331 | |
1332 | if (error == -ERESTART_RESTARTBLOCK) { | |
1333 | ||
3751f9f2 | 1334 | if (flags & TIMER_ABSTIME) |
e4b76555 | 1335 | return -ERESTARTNOHAND; |
1da177e4 | 1336 | |
1711ef38 | 1337 | restart_block->fn = posix_cpu_nsleep_restart; |
ab8177bc | 1338 | restart_block->nanosleep.clockid = which_clock; |
1da177e4 | 1339 | } |
1da177e4 LT |
1340 | return error; |
1341 | } | |
1342 | ||
bc2c8ea4 | 1343 | static long posix_cpu_nsleep_restart(struct restart_block *restart_block) |
1da177e4 | 1344 | { |
ab8177bc | 1345 | clockid_t which_clock = restart_block->nanosleep.clockid; |
ad196384 | 1346 | struct timespec64 t; |
97735f25 | 1347 | |
ad196384 | 1348 | t = ns_to_timespec64(restart_block->nanosleep.expires); |
97735f25 | 1349 | |
86a9c446 | 1350 | return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t); |
1da177e4 LT |
1351 | } |
1352 | ||
29f1b2b0 ND |
1353 | #define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED) |
1354 | #define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED) | |
1da177e4 | 1355 | |
a924b04d | 1356 | static int process_cpu_clock_getres(const clockid_t which_clock, |
d2e3e0ca | 1357 | struct timespec64 *tp) |
1da177e4 LT |
1358 | { |
1359 | return posix_cpu_clock_getres(PROCESS_CLOCK, tp); | |
1360 | } | |
a924b04d | 1361 | static int process_cpu_clock_get(const clockid_t which_clock, |
3c9c12f4 | 1362 | struct timespec64 *tp) |
1da177e4 LT |
1363 | { |
1364 | return posix_cpu_clock_get(PROCESS_CLOCK, tp); | |
1365 | } | |
1366 | static int process_cpu_timer_create(struct k_itimer *timer) | |
1367 | { | |
1368 | timer->it_clock = PROCESS_CLOCK; | |
1369 | return posix_cpu_timer_create(timer); | |
1370 | } | |
a924b04d | 1371 | static int process_cpu_nsleep(const clockid_t which_clock, int flags, |
938e7cf2 | 1372 | const struct timespec64 *rqtp) |
1da177e4 | 1373 | { |
99e6c0e6 | 1374 | return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp); |
1da177e4 | 1375 | } |
a924b04d | 1376 | static int thread_cpu_clock_getres(const clockid_t which_clock, |
d2e3e0ca | 1377 | struct timespec64 *tp) |
1da177e4 LT |
1378 | { |
1379 | return posix_cpu_clock_getres(THREAD_CLOCK, tp); | |
1380 | } | |
a924b04d | 1381 | static int thread_cpu_clock_get(const clockid_t which_clock, |
3c9c12f4 | 1382 | struct timespec64 *tp) |
1da177e4 LT |
1383 | { |
1384 | return posix_cpu_clock_get(THREAD_CLOCK, tp); | |
1385 | } | |
1386 | static int thread_cpu_timer_create(struct k_itimer *timer) | |
1387 | { | |
1388 | timer->it_clock = THREAD_CLOCK; | |
1389 | return posix_cpu_timer_create(timer); | |
1390 | } | |
1da177e4 | 1391 | |
d3ba5a9a | 1392 | const struct k_clock clock_posix_cpu = { |
1976945e TG |
1393 | .clock_getres = posix_cpu_clock_getres, |
1394 | .clock_set = posix_cpu_clock_set, | |
1395 | .clock_get = posix_cpu_clock_get, | |
1396 | .timer_create = posix_cpu_timer_create, | |
1397 | .nsleep = posix_cpu_nsleep, | |
1976945e TG |
1398 | .timer_set = posix_cpu_timer_set, |
1399 | .timer_del = posix_cpu_timer_del, | |
1400 | .timer_get = posix_cpu_timer_get, | |
f37fb0aa | 1401 | .timer_rearm = posix_cpu_timer_rearm, |
1976945e TG |
1402 | }; |
1403 | ||
d3ba5a9a CH |
1404 | const struct k_clock clock_process = { |
1405 | .clock_getres = process_cpu_clock_getres, | |
1406 | .clock_get = process_cpu_clock_get, | |
1407 | .timer_create = process_cpu_timer_create, | |
1408 | .nsleep = process_cpu_nsleep, | |
d3ba5a9a | 1409 | }; |
1da177e4 | 1410 | |
d3ba5a9a CH |
1411 | const struct k_clock clock_thread = { |
1412 | .clock_getres = thread_cpu_clock_getres, | |
1413 | .clock_get = thread_cpu_clock_get, | |
1414 | .timer_create = thread_cpu_timer_create, | |
1415 | }; |