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()
297 if (!READ_ONCE(pct->timers_active)) { in thread_group_start_cputime()
306 update_gt_cputime(&cputimer->cputime_atomic, &sum); in thread_group_start_cputime()
315 WRITE_ONCE(pct->timers_active, true); in thread_group_start_cputime()
317 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in thread_group_start_cputime()
336 struct thread_group_cputimer *cputimer = &p->signal->cputimer; in cpu_clock_sample_group()
337 struct posix_cputimers *pct = &p->signal->posix_cputimers; in cpu_clock_sample_group()
340 if (!READ_ONCE(pct->timers_active)) { in cpu_clock_sample_group()
346 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in cpu_clock_sample_group()
362 return -EINVAL; in posix_cpu_clock_get()
376 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
378 * new timer already all-zeros initialized.
386 pid = pid_for_clock(new_timer->it_clock, false); in posix_cpu_timer_create()
389 return -EINVAL; in posix_cpu_timer_create()
401 lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key); in posix_cpu_timer_create()
403 new_timer->kclock = &clock_posix_cpu; in posix_cpu_timer_create()
404 timerqueue_init(&new_timer->it.cpu.node); in posix_cpu_timer_create()
405 new_timer->it.cpu.pid = get_pid(pid); in posix_cpu_timer_create()
413 int clkidx = CPUCLOCK_WHICH(timer->it_clock); in timer_base()
415 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in timer_base()
416 return tsk->posix_cputimers.bases + clkidx; in timer_base()
418 return tsk->signal->posix_cputimers.bases + clkidx; in timer_base()
423 * This will also re-evaluate the need to keep around the process wide
432 base->nextevt = 0; in trigger_base_recalc_expires()
447 struct cpu_timer *ctmr = &timer->it.cpu; in disarm_timer()
454 if (cpu_timer_getexpires(ctmr) == base->nextevt) in disarm_timer()
460 * Clean up a CPU-clock timer that is about to be destroyed.
467 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_del()
479 * Protect against sighand release/switch in exit/exec and process/ in posix_cpu_timer_del()
488 WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); in posix_cpu_timer_del()
490 if (timer->it.cpu.firing) in posix_cpu_timer_del()
501 put_pid(ctmr->pid); in posix_cpu_timer_del()
514 ctmr->head = NULL; in cleanup_timerqueue()
527 cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead); in cleanup_timers()
528 cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead); in cleanup_timers()
529 cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead); in cleanup_timers()
539 cleanup_timers(&tsk->posix_cputimers); in posix_cpu_timers_exit()
543 cleanup_timers(&tsk->signal->posix_cputimers); in posix_cpu_timers_exit_group()
553 struct cpu_timer *ctmr = &timer->it.cpu; in arm_timer()
556 if (!cpu_timer_enqueue(&base->tqhead, ctmr)) in arm_timer()
560 * We are the new earliest-expiring POSIX 1.b timer, hence in arm_timer()
565 if (newexp < base->nextevt) in arm_timer()
566 base->nextevt = newexp; in arm_timer()
568 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in arm_timer()
579 struct cpu_timer *ctmr = &timer->it.cpu; in cpu_timer_fire()
581 if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { in cpu_timer_fire()
586 } else if (unlikely(timer->sigq == NULL)) { in cpu_timer_fire()
591 wake_up_process(timer->it_process); in cpu_timer_fire()
593 } else if (!timer->it_interval) { in cpu_timer_fire()
595 * One-shot timer. Clear it as soon as it's fired. in cpu_timer_fire()
599 } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { in cpu_timer_fire()
607 ++timer->it_requeue_pending; in cpu_timer_fire()
620 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_set()
622 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_set()
636 return -ESRCH; in posix_cpu_timer_set()
643 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value)); in posix_cpu_timer_set()
646 * Protect against sighand release/switch in exit/exec and p->cpu_timers in posix_cpu_timer_set()
647 * and p->signal->cpu_timers read/write in arm_timer() in posix_cpu_timer_set()
656 return -ESRCH; in posix_cpu_timer_set()
662 old_incr = timer->it_interval; in posix_cpu_timer_set()
665 if (unlikely(timer->it.cpu.firing)) { in posix_cpu_timer_set()
666 timer->it.cpu.firing = -1; in posix_cpu_timer_set()
680 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_set()
687 old->it_value.tv_sec = 0; in posix_cpu_timer_set()
688 old->it_value.tv_nsec = 0; in posix_cpu_timer_set()
700 old_expires = exp - val; in posix_cpu_timer_set()
701 old->it_value = ns_to_timespec64(old_expires); in posix_cpu_timer_set()
703 old->it_value.tv_nsec = 1; in posix_cpu_timer_set()
704 old->it_value.tv_sec = 0; in posix_cpu_timer_set()
739 timer->it_interval = timespec64_to_ktime(new->it_interval); in posix_cpu_timer_set()
746 timer->it_requeue_pending = (timer->it_requeue_pending + 2) & in posix_cpu_timer_set()
748 timer->it_overrun_last = 0; in posix_cpu_timer_set()
749 timer->it_overrun = -1; in posix_cpu_timer_set()
777 old->it_interval = ns_to_timespec64(old_incr); in posix_cpu_timer_set()
784 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_get()
785 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_get()
797 itp->it_interval = ktime_to_timespec64(timer->it_interval); in posix_cpu_timer_get()
805 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_get()
811 itp->it_value = ns_to_timespec64(expires - now); in posix_cpu_timer_get()
817 itp->it_value.tv_nsec = 1; in posix_cpu_timer_get()
818 itp->it_value.tv_sec = 0; in posix_cpu_timer_get()
842 ctmr->firing = 1; in collect_timerqueue()
844 list_add_tail(&ctmr->elist, firing); in collect_timerqueue()
853 struct posix_cputimer_base *base = pct->bases; in collect_posix_cputimers()
857 base->nextevt = collect_timerqueue(&base->tqhead, firing, in collect_posix_cputimers()
864 if (tsk->dl.dl_overrun) { in check_dl_overrun()
865 tsk->dl.dl_overrun = 0; in check_dl_overrun()
878 current->comm, task_pid_nr(current)); in check_rlimit()
885 * Check for any per-thread CPU timers that have fired and move them off
886 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
887 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
892 struct posix_cputimers *pct = &tsk->posix_cputimers; in check_thread_timers()
911 unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ); in check_thread_timers()
922 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft; in check_thread_timers()
932 struct posix_cputimers *pct = &sig->posix_cputimers; in stop_process_timers()
935 WRITE_ONCE(pct->timers_active, false); in stop_process_timers()
942 if (!it->expires) in check_cpu_itimer()
945 if (cur_time >= it->expires) { in check_cpu_itimer()
946 if (it->incr) in check_cpu_itimer()
947 it->expires += it->incr; in check_cpu_itimer()
949 it->expires = 0; in check_cpu_itimer()
957 if (it->expires && it->expires < *expires) in check_cpu_itimer()
958 *expires = it->expires; in check_cpu_itimer()
962 * Check for any per-thread CPU timers that have fired and move them
963 * off the tsk->*_timers list onto the firing list. Per-thread timers
969 struct signal_struct *const sig = tsk->signal; in check_process_timers()
970 struct posix_cputimers *pct = &sig->posix_cputimers; in check_process_timers()
979 if (!READ_ONCE(pct->timers_active) || pct->expiry_active) in check_process_timers()
986 pct->expiry_active = true; in check_process_timers()
992 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples); in check_process_timers()
998 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], in check_process_timers()
999 &pct->bases[CPUCLOCK_PROF].nextevt, in check_process_timers()
1001 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], in check_process_timers()
1002 &pct->bases[CPUCLOCK_VIRT].nextevt, in check_process_timers()
1020 sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1; in check_process_timers()
1025 if (softns < pct->bases[CPUCLOCK_PROF].nextevt) in check_process_timers()
1026 pct->bases[CPUCLOCK_PROF].nextevt = softns; in check_process_timers()
1032 pct->expiry_active = false; in check_process_timers()
1041 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_rearm()
1052 /* Protect timer list r/w in arm_timer() */ in posix_cpu_timer_rearm()
1060 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_rearm()
1068 * Now re-arm for the new expiry time. in posix_cpu_timer_rearm()
1077 * task_cputimers_expired - Check whether posix CPU timers are expired
1083 * member of @pct->bases[CLK].nextevt. False otherwise
1091 if (samples[i] >= pct->bases[i].nextevt) in task_cputimers_expired()
1098 * fastpath_timer_check - POSIX CPU timers fast path.
1109 struct posix_cputimers *pct = &tsk->posix_cputimers; in fastpath_timer_check()
1120 sig = tsk->signal; in fastpath_timer_check()
1121 pct = &sig->posix_cputimers; in fastpath_timer_check()
1137 if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) { in fastpath_timer_check()
1140 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, in fastpath_timer_check()
1147 if (dl_task(tsk) && tsk->dl.dl_overrun) in fastpath_timer_check()
1167 init_task_work(&current->posix_cputimers_work.work, in posix_cputimers_init_work()
1172 * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
1179 return tsk->posix_cputimers_work.scheduled; in posix_cpu_timers_work_scheduled()
1184 if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled)) in __run_posix_cpu_timers()
1188 tsk->posix_cputimers_work.scheduled = true; in __run_posix_cpu_timers()
1189 task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME); in __run_posix_cpu_timers()
1203 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1224 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1268 * Here we take off tsk->signal->cpu_timers[N] and in handle_posix_cpu_timers()
1269 * tsk->cpu_timers[N] all the timers that are firing, and in handle_posix_cpu_timers()
1292 * - On !RT kernels no tick can have happened on this CPU in handle_posix_cpu_timers()
1297 * - On RT kernels ticks might have happened but the tick in handle_posix_cpu_timers()
1334 spin_lock(&timer->it_lock); in handle_posix_cpu_timers()
1335 list_del_init(&timer->it.cpu.elist); in handle_posix_cpu_timers()
1336 cpu_firing = timer->it.cpu.firing; in handle_posix_cpu_timers()
1337 timer->it.cpu.firing = 0; in handle_posix_cpu_timers()
1339 * The firing flag is -1 if we collided with a reset in handle_posix_cpu_timers()
1341 * almost-firing as an overrun. So don't generate an event. in handle_posix_cpu_timers()
1345 spin_unlock(&timer->it_lock); in handle_posix_cpu_timers()
1378 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1379 * The tsk->sighand->siglock must be held by the caller.
1389 nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt; in set_process_cpu_timer()
1403 *oldval -= now; in set_process_cpu_timer()
1435 timer.it_overrun = -1; in do_cpu_nanosleep()
1504 error = -ERESTART_RESTARTBLOCK; in do_cpu_nanosleep()
1508 restart = &current->restart_block; in do_cpu_nanosleep()
1509 restart->nanosleep.expires = expires; in do_cpu_nanosleep()
1510 if (restart->nanosleep.type != TT_NONE) in do_cpu_nanosleep()
1522 struct restart_block *restart_block = &current->restart_block; in posix_cpu_nsleep()
1531 return -EINVAL; in posix_cpu_nsleep()
1535 if (error == -ERESTART_RESTARTBLOCK) { in posix_cpu_nsleep()
1538 return -ERESTARTNOHAND; in posix_cpu_nsleep()
1540 restart_block->nanosleep.clockid = which_clock; in posix_cpu_nsleep()
1548 clockid_t which_clock = restart_block->nanosleep.clockid; in posix_cpu_nsleep_restart()
1551 t = ns_to_timespec64(restart_block->nanosleep.expires); in posix_cpu_nsleep_restart()
1571 timer->it_clock = PROCESS_CLOCK; in process_cpu_timer_create()
1591 timer->it_clock = THREAD_CLOCK; in thread_cpu_timer_create()