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