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