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