Lines Matching +full:protect +full:- +full:exec
1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/posix-timers.h>
19 #include "posix-timers.h"
27 pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC; in posix_cputimers_group_init()
28 pct->timers_active = true; in posix_cputimers_group_init()
34 * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
42 spin_lock_irq(&task->sighand->siglock); in update_rlimit_cpu()
44 spin_unlock_irq(&task->sighand->siglock); in update_rlimit_cpu()
95 ret = pid_for_clock(clock, false) ? 0 : -EINVAL; in validate_clock_permissions()
108 return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock)); in cpu_timer_task_rcu()
117 u64 delta, incr, expires = timer->it.cpu.node.expires; in bump_cpu_timer()
120 if (!timer->it_interval) in bump_cpu_timer()
126 incr = timer->it_interval; in bump_cpu_timer()
127 delta = now + incr - expires; in bump_cpu_timer()
130 for (i = 0; incr < delta - incr; i++) in bump_cpu_timer()
133 for (; i >= 0; incr >>= 1, i--) { in bump_cpu_timer()
137 timer->it.cpu.node.expires += incr; in bump_cpu_timer()
138 timer->it_overrun += 1LL << i; in bump_cpu_timer()
139 delta -= incr; in bump_cpu_timer()
141 return timer->it.cpu.node.expires; in bump_cpu_timer()
147 return !(~pct->bases[CPUCLOCK_PROF].nextevt | in expiry_cache_is_inactive()
148 ~pct->bases[CPUCLOCK_VIRT].nextevt | in expiry_cache_is_inactive()
149 ~pct->bases[CPUCLOCK_SCHED].nextevt); in expiry_cache_is_inactive()
158 tp->tv_sec = 0; in posix_cpu_clock_getres()
159 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); in posix_cpu_clock_getres()
166 tp->tv_nsec = 1; in posix_cpu_clock_getres()
181 return error ? : -EPERM; in posix_cpu_clock_set()
185 * Sample a per-thread clock for the given task. clkid is validated.
219 store_samples(samples, stime, utime, p->se.sum_exec_runtime); in task_sample_cputime()
227 utime = atomic64_read(&at->utime); in proc_sample_cputime_atomic()
228 stime = atomic64_read(&at->stime); in proc_sample_cputime_atomic()
229 rtime = atomic64_read(&at->sum_exec_runtime); in proc_sample_cputime_atomic()
251 __update_gt_cputime(&cputime_atomic->utime, sum->utime); in update_gt_cputime()
252 __update_gt_cputime(&cputime_atomic->stime, sum->stime); in update_gt_cputime()
253 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); in update_gt_cputime()
257 * thread_group_sample_cputime - Sample cputime for a given task
269 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; in thread_group_sample_cputime()
270 struct posix_cputimers *pct = &tsk->signal->posix_cputimers; in thread_group_sample_cputime()
272 WARN_ON_ONCE(!pct->timers_active); in thread_group_sample_cputime()
274 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in thread_group_sample_cputime()
278 * thread_group_start_cputime - Start cputime and return a sample
291 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; in thread_group_start_cputime()
292 struct posix_cputimers *pct = &tsk->signal->posix_cputimers; in thread_group_start_cputime()
295 if (!READ_ONCE(pct->timers_active)) { in thread_group_start_cputime()
304 update_gt_cputime(&cputimer->cputime_atomic, &sum); in thread_group_start_cputime()
313 WRITE_ONCE(pct->timers_active, true); in thread_group_start_cputime()
315 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in thread_group_start_cputime()
334 struct thread_group_cputimer *cputimer = &p->signal->cputimer; in cpu_clock_sample_group()
335 struct posix_cputimers *pct = &p->signal->posix_cputimers; in cpu_clock_sample_group()
338 if (!READ_ONCE(pct->timers_active)) { in cpu_clock_sample_group()
344 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in cpu_clock_sample_group()
360 return -EINVAL; in posix_cpu_clock_get()
374 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
376 * new timer already all-zeros initialized.
384 pid = pid_for_clock(new_timer->it_clock, false); in posix_cpu_timer_create()
387 return -EINVAL; in posix_cpu_timer_create()
399 lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key); in posix_cpu_timer_create()
401 new_timer->kclock = &clock_posix_cpu; in posix_cpu_timer_create()
402 timerqueue_init(&new_timer->it.cpu.node); in posix_cpu_timer_create()
403 new_timer->it.cpu.pid = get_pid(pid); in posix_cpu_timer_create()
409 * Clean up a CPU-clock timer that is about to be destroyed.
416 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_del()
428 * Protect against sighand release/switch in exit/exec and process/ in posix_cpu_timer_del()
437 WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); in posix_cpu_timer_del()
439 if (timer->it.cpu.firing) in posix_cpu_timer_del()
450 put_pid(ctmr->pid); in posix_cpu_timer_del()
463 ctmr->head = NULL; in cleanup_timerqueue()
476 cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead); in cleanup_timers()
477 cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead); in cleanup_timers()
478 cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead); in cleanup_timers()
488 cleanup_timers(&tsk->posix_cputimers); in posix_cpu_timers_exit()
492 cleanup_timers(&tsk->signal->posix_cputimers); in posix_cpu_timers_exit_group()
501 int clkidx = CPUCLOCK_WHICH(timer->it_clock); in arm_timer()
502 struct cpu_timer *ctmr = &timer->it.cpu; in arm_timer()
506 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in arm_timer()
507 base = p->posix_cputimers.bases + clkidx; in arm_timer()
509 base = p->signal->posix_cputimers.bases + clkidx; in arm_timer()
511 if (!cpu_timer_enqueue(&base->tqhead, ctmr)) in arm_timer()
515 * We are the new earliest-expiring POSIX 1.b timer, hence in arm_timer()
520 if (newexp < base->nextevt) in arm_timer()
521 base->nextevt = newexp; in arm_timer()
523 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in arm_timer()
526 tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); in arm_timer()
534 struct cpu_timer *ctmr = &timer->it.cpu; in cpu_timer_fire()
536 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { in cpu_timer_fire()
541 } else if (unlikely(timer->sigq == NULL)) { in cpu_timer_fire()
546 wake_up_process(timer->it_process); in cpu_timer_fire()
548 } else if (!timer->it_interval) { in cpu_timer_fire()
550 * One-shot timer. Clear it as soon as it's fired. in cpu_timer_fire()
554 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { in cpu_timer_fire()
562 ++timer->it_requeue_pending; in cpu_timer_fire()
575 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_set()
577 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_set()
591 return -ESRCH; in posix_cpu_timer_set()
598 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value)); in posix_cpu_timer_set()
601 * Protect against sighand release/switch in exit/exec and p->cpu_timers in posix_cpu_timer_set()
602 * and p->signal->cpu_timers read/write in arm_timer() in posix_cpu_timer_set()
611 return -ESRCH; in posix_cpu_timer_set()
617 old_incr = timer->it_interval; in posix_cpu_timer_set()
620 if (unlikely(timer->it.cpu.firing)) { in posix_cpu_timer_set()
621 timer->it.cpu.firing = -1; in posix_cpu_timer_set()
635 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_set()
642 old->it_value.tv_sec = 0; in posix_cpu_timer_set()
643 old->it_value.tv_nsec = 0; in posix_cpu_timer_set()
655 old_expires = exp - val; in posix_cpu_timer_set()
656 old->it_value = ns_to_timespec64(old_expires); in posix_cpu_timer_set()
658 old->it_value.tv_nsec = 1; in posix_cpu_timer_set()
659 old->it_value.tv_sec = 0; in posix_cpu_timer_set()
694 timer->it_interval = timespec64_to_ktime(new->it_interval); in posix_cpu_timer_set()
701 timer->it_requeue_pending = (timer->it_requeue_pending + 2) & in posix_cpu_timer_set()
703 timer->it_overrun_last = 0; in posix_cpu_timer_set()
704 timer->it_overrun = -1; in posix_cpu_timer_set()
719 old->it_interval = ns_to_timespec64(old_incr); in posix_cpu_timer_set()
726 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_get()
727 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_get()
739 itp->it_interval = ktime_to_timespec64(timer->it_interval); in posix_cpu_timer_get()
747 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_get()
753 itp->it_value = ns_to_timespec64(expires - now); in posix_cpu_timer_get()
759 itp->it_value.tv_nsec = 1; in posix_cpu_timer_get()
760 itp->it_value.tv_sec = 0; in posix_cpu_timer_get()
784 ctmr->firing = 1; in collect_timerqueue()
786 list_add_tail(&ctmr->elist, firing); in collect_timerqueue()
795 struct posix_cputimer_base *base = pct->bases; in collect_posix_cputimers()
799 base->nextevt = collect_timerqueue(&base->tqhead, firing, in collect_posix_cputimers()
806 if (tsk->dl.dl_overrun) { in check_dl_overrun()
807 tsk->dl.dl_overrun = 0; in check_dl_overrun()
820 current->comm, task_pid_nr(current)); in check_rlimit()
827 * Check for any per-thread CPU timers that have fired and move them off
828 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
829 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
834 struct posix_cputimers *pct = &tsk->posix_cputimers; in check_thread_timers()
853 unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ); in check_thread_timers()
864 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft; in check_thread_timers()
874 struct posix_cputimers *pct = &sig->posix_cputimers; in stop_process_timers()
877 WRITE_ONCE(pct->timers_active, false); in stop_process_timers()
884 if (!it->expires) in check_cpu_itimer()
887 if (cur_time >= it->expires) { in check_cpu_itimer()
888 if (it->incr) in check_cpu_itimer()
889 it->expires += it->incr; in check_cpu_itimer()
891 it->expires = 0; in check_cpu_itimer()
899 if (it->expires && it->expires < *expires) in check_cpu_itimer()
900 *expires = it->expires; in check_cpu_itimer()
904 * Check for any per-thread CPU timers that have fired and move them
905 * off the tsk->*_timers list onto the firing list. Per-thread timers
911 struct signal_struct *const sig = tsk->signal; in check_process_timers()
912 struct posix_cputimers *pct = &sig->posix_cputimers; in check_process_timers()
921 if (!READ_ONCE(pct->timers_active) || pct->expiry_active) in check_process_timers()
928 pct->expiry_active = true; in check_process_timers()
934 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples); in check_process_timers()
940 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], in check_process_timers()
941 &pct->bases[CPUCLOCK_PROF].nextevt, in check_process_timers()
943 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], in check_process_timers()
944 &pct->bases[CPUCLOCK_VIRT].nextevt, in check_process_timers()
962 sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1; in check_process_timers()
967 if (softns < pct->bases[CPUCLOCK_PROF].nextevt) in check_process_timers()
968 pct->bases[CPUCLOCK_PROF].nextevt = softns; in check_process_timers()
974 pct->expiry_active = false; in check_process_timers()
983 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_rearm()
997 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_rearm()
1004 /* Protect timer list r/w in arm_timer() */ in posix_cpu_timer_rearm()
1010 * Now re-arm for the new expiry time. in posix_cpu_timer_rearm()
1019 * task_cputimers_expired - Check whether posix CPU timers are expired
1025 * member of @pct->bases[CLK].nextevt. False otherwise
1033 if (samples[i] >= pct->bases[i].nextevt) in task_cputimers_expired()
1040 * fastpath_timer_check - POSIX CPU timers fast path.
1051 struct posix_cputimers *pct = &tsk->posix_cputimers; in fastpath_timer_check()
1062 sig = tsk->signal; in fastpath_timer_check()
1063 pct = &sig->posix_cputimers; in fastpath_timer_check()
1079 if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) { in fastpath_timer_check()
1082 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, in fastpath_timer_check()
1089 if (dl_task(tsk) && tsk->dl.dl_overrun) in fastpath_timer_check()
1109 init_task_work(¤t->posix_cputimers_work.work, in posix_cputimers_init_work()
1114 * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
1121 return tsk->posix_cputimers_work.scheduled; in posix_cpu_timers_work_scheduled()
1126 if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled)) in __run_posix_cpu_timers()
1130 tsk->posix_cputimers_work.scheduled = true; in __run_posix_cpu_timers()
1131 task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME); in __run_posix_cpu_timers()
1145 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1166 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1210 * Here we take off tsk->signal->cpu_timers[N] and in handle_posix_cpu_timers()
1211 * tsk->cpu_timers[N] all the timers that are firing, and in handle_posix_cpu_timers()
1234 * - On !RT kernels no tick can have happened on this CPU in handle_posix_cpu_timers()
1239 * - On RT kernels ticks might have happened but the tick in handle_posix_cpu_timers()
1276 spin_lock(&timer->it_lock); in handle_posix_cpu_timers()
1277 list_del_init(&timer->it.cpu.elist); in handle_posix_cpu_timers()
1278 cpu_firing = timer->it.cpu.firing; in handle_posix_cpu_timers()
1279 timer->it.cpu.firing = 0; in handle_posix_cpu_timers()
1281 * The firing flag is -1 if we collided with a reset in handle_posix_cpu_timers()
1283 * almost-firing as an overrun. So don't generate an event. in handle_posix_cpu_timers()
1287 spin_unlock(&timer->it_lock); in handle_posix_cpu_timers()
1320 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1321 * The tsk->sighand->siglock must be held by the caller.
1331 nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt; in set_process_cpu_timer()
1345 *oldval -= now; in set_process_cpu_timer()
1361 tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER); in set_process_cpu_timer()
1378 timer.it_overrun = -1; in do_cpu_nanosleep()
1447 error = -ERESTART_RESTARTBLOCK; in do_cpu_nanosleep()
1451 restart = ¤t->restart_block; in do_cpu_nanosleep()
1452 restart->nanosleep.expires = expires; in do_cpu_nanosleep()
1453 if (restart->nanosleep.type != TT_NONE) in do_cpu_nanosleep()
1465 struct restart_block *restart_block = ¤t->restart_block; in posix_cpu_nsleep()
1474 return -EINVAL; in posix_cpu_nsleep()
1478 if (error == -ERESTART_RESTARTBLOCK) { in posix_cpu_nsleep()
1481 return -ERESTARTNOHAND; in posix_cpu_nsleep()
1483 restart_block->fn = posix_cpu_nsleep_restart; in posix_cpu_nsleep()
1484 restart_block->nanosleep.clockid = which_clock; in posix_cpu_nsleep()
1491 clockid_t which_clock = restart_block->nanosleep.clockid; in posix_cpu_nsleep_restart()
1494 t = ns_to_timespec64(restart_block->nanosleep.expires); in posix_cpu_nsleep_restart()
1514 timer->it_clock = PROCESS_CLOCK; in process_cpu_timer_create()
1534 timer->it_clock = THREAD_CLOCK; in thread_cpu_timer_create()