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