Lines Matching +full:protect +full:- +full:exec
1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/posix-timers.h>
20 #include "posix-timers.h"
28 pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC; in posix_cputimers_group_init()
29 pct->timers_active = true; in posix_cputimers_group_init()
35 * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
39 * Returns 0 on success, -ESRCH on failure. Can fail if the task is exiting and
48 return -ESRCH; in update_rlimit_cpu()
102 ret = pid_for_clock(clock, false) ? 0 : -EINVAL; in validate_clock_permissions()
115 return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock)); in cpu_timer_task_rcu()
124 u64 delta, incr, expires = timer->it.cpu.node.expires; in bump_cpu_timer()
127 if (!timer->it_interval) in bump_cpu_timer()
133 incr = timer->it_interval; in bump_cpu_timer()
134 delta = now + incr - expires; in bump_cpu_timer()
137 for (i = 0; incr < delta - incr; i++) in bump_cpu_timer()
140 for (; i >= 0; incr >>= 1, i--) { in bump_cpu_timer()
144 timer->it.cpu.node.expires += incr; in bump_cpu_timer()
145 timer->it_overrun += 1LL << i; in bump_cpu_timer()
146 delta -= incr; in bump_cpu_timer()
148 return timer->it.cpu.node.expires; in bump_cpu_timer()
154 return !(~pct->bases[CPUCLOCK_PROF].nextevt | in expiry_cache_is_inactive()
155 ~pct->bases[CPUCLOCK_VIRT].nextevt | in expiry_cache_is_inactive()
156 ~pct->bases[CPUCLOCK_SCHED].nextevt); in expiry_cache_is_inactive()
165 tp->tv_sec = 0; in posix_cpu_clock_getres()
166 tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); in posix_cpu_clock_getres()
173 tp->tv_nsec = 1; in posix_cpu_clock_getres()
188 return error ? : -EPERM; in posix_cpu_clock_set()
192 * Sample a per-thread clock for the given task. clkid is validated.
226 store_samples(samples, stime, utime, p->se.sum_exec_runtime); in task_sample_cputime()
234 utime = atomic64_read(&at->utime); in proc_sample_cputime_atomic()
235 stime = atomic64_read(&at->stime); in proc_sample_cputime_atomic()
236 rtime = atomic64_read(&at->sum_exec_runtime); in proc_sample_cputime_atomic()
257 __update_gt_cputime(&cputime_atomic->utime, sum->utime); in update_gt_cputime()
258 __update_gt_cputime(&cputime_atomic->stime, sum->stime); in update_gt_cputime()
259 __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); in update_gt_cputime()
263 * thread_group_sample_cputime - Sample cputime for a given task
275 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; in thread_group_sample_cputime()
276 struct posix_cputimers *pct = &tsk->signal->posix_cputimers; in thread_group_sample_cputime()
278 WARN_ON_ONCE(!pct->timers_active); in thread_group_sample_cputime()
280 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in thread_group_sample_cputime()
284 * thread_group_start_cputime - Start cputime and return a sample
297 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; in thread_group_start_cputime()
298 struct posix_cputimers *pct = &tsk->signal->posix_cputimers; in thread_group_start_cputime()
303 if (!READ_ONCE(pct->timers_active)) { in thread_group_start_cputime()
312 update_gt_cputime(&cputimer->cputime_atomic, &sum); in thread_group_start_cputime()
321 WRITE_ONCE(pct->timers_active, true); in thread_group_start_cputime()
323 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in thread_group_start_cputime()
342 struct thread_group_cputimer *cputimer = &p->signal->cputimer; in cpu_clock_sample_group()
343 struct posix_cputimers *pct = &p->signal->posix_cputimers; in cpu_clock_sample_group()
346 if (!READ_ONCE(pct->timers_active)) { in cpu_clock_sample_group()
352 proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); in cpu_clock_sample_group()
368 return -EINVAL; in posix_cpu_clock_get()
382 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
384 * new timer already all-zeros initialized.
392 pid = pid_for_clock(new_timer->it_clock, false); in posix_cpu_timer_create()
395 return -EINVAL; in posix_cpu_timer_create()
407 lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key); in posix_cpu_timer_create()
409 new_timer->kclock = &clock_posix_cpu; in posix_cpu_timer_create()
410 timerqueue_init(&new_timer->it.cpu.node); in posix_cpu_timer_create()
411 new_timer->it.cpu.pid = get_pid(pid); in posix_cpu_timer_create()
419 int clkidx = CPUCLOCK_WHICH(timer->it_clock); in timer_base()
421 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in timer_base()
422 return tsk->posix_cputimers.bases + clkidx; in timer_base()
424 return tsk->signal->posix_cputimers.bases + clkidx; in timer_base()
429 * This will also re-evaluate the need to keep around the process wide
438 base->nextevt = 0; in trigger_base_recalc_expires()
453 struct cpu_timer *ctmr = &timer->it.cpu; in disarm_timer()
460 if (cpu_timer_getexpires(ctmr) == base->nextevt) in disarm_timer()
466 * Clean up a CPU-clock timer that is about to be destroyed.
473 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_del()
485 * Protect against sighand release/switch in exit/exec and process/ in posix_cpu_timer_del()
494 WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); in posix_cpu_timer_del()
496 if (timer->it.cpu.firing) { in posix_cpu_timer_del()
500 * by sighand->lock. The delivery path is waiting for in posix_cpu_timer_del()
503 timer->it.cpu.firing = false; in posix_cpu_timer_del()
515 put_pid(ctmr->pid); in posix_cpu_timer_del()
516 timer->it_status = POSIX_TIMER_DISARMED; in posix_cpu_timer_del()
529 ctmr->head = NULL; in cleanup_timerqueue()
542 cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead); in cleanup_timers()
543 cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead); in cleanup_timers()
544 cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead); in cleanup_timers()
554 cleanup_timers(&tsk->posix_cputimers); in posix_cpu_timers_exit()
558 cleanup_timers(&tsk->signal->posix_cputimers); in posix_cpu_timers_exit_group()
568 struct cpu_timer *ctmr = &timer->it.cpu; in arm_timer()
571 timer->it_status = POSIX_TIMER_ARMED; in arm_timer()
572 if (!cpu_timer_enqueue(&base->tqhead, ctmr)) in arm_timer()
576 * We are the new earliest-expiring POSIX 1.b timer, hence in arm_timer()
581 if (newexp < base->nextevt) in arm_timer()
582 base->nextevt = newexp; in arm_timer()
584 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in arm_timer()
595 struct cpu_timer *ctmr = &timer->it.cpu; in cpu_timer_fire()
597 timer->it_status = POSIX_TIMER_DISARMED; in cpu_timer_fire()
599 if (unlikely(ctmr->nanosleep)) { in cpu_timer_fire()
604 wake_up_process(timer->it_process); in cpu_timer_fire()
609 if (!timer->it_interval) in cpu_timer_fire()
625 bool sigev_none = timer->it_sigev_notify == SIGEV_NONE; in posix_cpu_timer_set()
626 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_set()
627 struct cpu_timer *ctmr = &timer->it.cpu; in posix_cpu_timer_set()
642 return -ESRCH; in posix_cpu_timer_set()
649 new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value)); in posix_cpu_timer_set()
652 * Protect against sighand release/switch in exit/exec and p->cpu_timers in posix_cpu_timer_set()
653 * and p->signal->cpu_timers read/write in arm_timer() in posix_cpu_timer_set()
662 return -ESRCH; in posix_cpu_timer_set()
668 if (unlikely(timer->it.cpu.firing)) { in posix_cpu_timer_set()
672 * by sighand->lock. The delivery path is waiting for in posix_cpu_timer_set()
675 timer->it.cpu.firing = false; in posix_cpu_timer_set()
679 timer->it_status = POSIX_TIMER_DISARMED; in posix_cpu_timer_set()
686 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_set()
693 old->it_value = (struct timespec64){ }; in posix_cpu_timer_set()
743 bool sigev_none = timer->it_sigev_notify == SIGEV_NONE; in __posix_cpu_timer_get()
744 u64 expires, iv = timer->it_interval; in __posix_cpu_timer_get()
749 * - SIGEV_NONE timers which are never armed in __posix_cpu_timer_get()
750 * - Timers which expired, but the signal has not yet been in __posix_cpu_timer_get()
753 if (iv && timer->it_status != POSIX_TIMER_ARMED) in __posix_cpu_timer_get()
756 expires = cpu_timer_getexpires(&timer->it.cpu); in __posix_cpu_timer_get()
764 itp->it_value = ns_to_timespec64(expires - now); in __posix_cpu_timer_get()
773 itp->it_value.tv_nsec = 1; in __posix_cpu_timer_get()
779 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_get()
785 if (p && cpu_timer_getexpires(&timer->it.cpu)) { in posix_cpu_timer_get()
786 itp->it_interval = ktime_to_timespec64(timer->it_interval); in posix_cpu_timer_get()
788 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_get()
816 ctmr->firing = true; in collect_timerqueue()
818 rcu_assign_pointer(ctmr->handling, current); in collect_timerqueue()
820 list_add_tail(&ctmr->elist, firing); in collect_timerqueue()
829 struct posix_cputimer_base *base = pct->bases; in collect_posix_cputimers()
833 base->nextevt = collect_timerqueue(&base->tqhead, firing, in collect_posix_cputimers()
840 if (tsk->dl.dl_overrun) { in check_dl_overrun()
841 tsk->dl.dl_overrun = 0; in check_dl_overrun()
854 current->comm, task_pid_nr(current)); in check_rlimit()
861 * Check for any per-thread CPU timers that have fired and move them off
862 * the tsk->cpu_timers[N] list onto the firing list. Here we update the
863 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
868 struct posix_cputimers *pct = &tsk->posix_cputimers; in check_thread_timers()
887 unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ); in check_thread_timers()
898 tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft; in check_thread_timers()
908 struct posix_cputimers *pct = &sig->posix_cputimers; in stop_process_timers()
911 WRITE_ONCE(pct->timers_active, false); in stop_process_timers()
918 if (!it->expires) in check_cpu_itimer()
921 if (cur_time >= it->expires) { in check_cpu_itimer()
922 if (it->incr) in check_cpu_itimer()
923 it->expires += it->incr; in check_cpu_itimer()
925 it->expires = 0; in check_cpu_itimer()
933 if (it->expires && it->expires < *expires) in check_cpu_itimer()
934 *expires = it->expires; in check_cpu_itimer()
938 * Check for any per-thread CPU timers that have fired and move them
939 * off the tsk->*_timers list onto the firing list. Per-thread timers
945 struct signal_struct *const sig = tsk->signal; in check_process_timers()
946 struct posix_cputimers *pct = &sig->posix_cputimers; in check_process_timers()
955 if (!READ_ONCE(pct->timers_active) || pct->expiry_active) in check_process_timers()
962 pct->expiry_active = true; in check_process_timers()
968 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples); in check_process_timers()
974 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], in check_process_timers()
975 &pct->bases[CPUCLOCK_PROF].nextevt, in check_process_timers()
977 check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], in check_process_timers()
978 &pct->bases[CPUCLOCK_VIRT].nextevt, in check_process_timers()
996 sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1; in check_process_timers()
1001 if (softns < pct->bases[CPUCLOCK_PROF].nextevt) in check_process_timers()
1002 pct->bases[CPUCLOCK_PROF].nextevt = softns; in check_process_timers()
1008 pct->expiry_active = false; in check_process_timers()
1017 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); in posix_cpu_timer_rearm()
1028 /* Protect timer list r/w in arm_timer() */ in posix_cpu_timer_rearm()
1036 if (CPUCLOCK_PERTHREAD(timer->it_clock)) in posix_cpu_timer_rearm()
1044 * Now re-arm for the new expiry time. in posix_cpu_timer_rearm()
1053 * task_cputimers_expired - Check whether posix CPU timers are expired
1059 * member of @pct->bases[CLK].nextevt. False otherwise
1067 if (samples[i] >= pct->bases[i].nextevt) in task_cputimers_expired()
1074 * fastpath_timer_check - POSIX CPU timers fast path.
1085 struct posix_cputimers *pct = &tsk->posix_cputimers; in fastpath_timer_check()
1096 sig = tsk->signal; in fastpath_timer_check()
1097 pct = &sig->posix_cputimers; in fastpath_timer_check()
1113 if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) { in fastpath_timer_check()
1116 proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, in fastpath_timer_check()
1123 if (dl_task(tsk) && tsk->dl.dl_overrun) in fastpath_timer_check()
1136 mutex_lock(&cw->mutex); in posix_cpu_timers_work()
1138 mutex_unlock(&cw->mutex); in posix_cpu_timers_work()
1142 * Invoked from the posix-timer core when a cancel operation failed because
1148 struct task_struct *tsk = rcu_dereference(timr->it.cpu.handling); in posix_cpu_timer_wait_running()
1159 mutex_lock(&tsk->posix_cputimers_work.mutex); in posix_cpu_timer_wait_running()
1161 mutex_unlock(&tsk->posix_cputimers_work.mutex); in posix_cpu_timer_wait_running()
1170 /* Ensure that timr->it.cpu.handling task cannot go away */ in posix_cpu_timer_wait_running_nsleep()
1172 spin_unlock_irq(&timr->it_lock); in posix_cpu_timer_wait_running_nsleep()
1176 spin_lock_irq(&timr->it_lock); in posix_cpu_timer_wait_running_nsleep()
1188 memset(&p->posix_cputimers_work.work, 0, in clear_posix_cputimers_work()
1189 sizeof(p->posix_cputimers_work.work)); in clear_posix_cputimers_work()
1190 init_task_work(&p->posix_cputimers_work.work, in clear_posix_cputimers_work()
1192 mutex_init(&p->posix_cputimers_work.mutex); in clear_posix_cputimers_work()
1193 p->posix_cputimers_work.scheduled = false; in clear_posix_cputimers_work()
1206 * Note: All operations on tsk->posix_cputimer_work.scheduled happen either
1213 return tsk->posix_cputimers_work.scheduled; in posix_cpu_timers_work_scheduled()
1218 if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled)) in __run_posix_cpu_timers()
1222 tsk->posix_cputimers_work.scheduled = true; in __run_posix_cpu_timers()
1223 task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME); in __run_posix_cpu_timers()
1237 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1258 tsk->posix_cputimers_work.scheduled = false; in posix_cpu_timers_enable_work()
1278 spin_unlock_irq(&timr->it_lock); in posix_cpu_timer_wait_running_nsleep()
1280 spin_lock_irq(&timr->it_lock); in posix_cpu_timer_wait_running_nsleep()
1314 * Here we take off tsk->signal->cpu_timers[N] and in handle_posix_cpu_timers()
1315 * tsk->cpu_timers[N] all the timers that are firing, and in handle_posix_cpu_timers()
1338 * - On !RT kernels no tick can have happened on this CPU in handle_posix_cpu_timers()
1343 * - On RT kernels ticks might have happened but the tick in handle_posix_cpu_timers()
1380 spin_lock(&timer->it_lock); in handle_posix_cpu_timers()
1381 list_del_init(&timer->it.cpu.elist); in handle_posix_cpu_timers()
1382 cpu_firing = timer->it.cpu.firing; in handle_posix_cpu_timers()
1383 timer->it.cpu.firing = false; in handle_posix_cpu_timers()
1392 rcu_assign_pointer(timer->it.cpu.handling, NULL); in handle_posix_cpu_timers()
1393 spin_unlock(&timer->it_lock); in handle_posix_cpu_timers()
1426 * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
1427 * The tsk->sighand->siglock must be held by the caller.
1437 nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt; in set_process_cpu_timer()
1451 *oldval -= now; in set_process_cpu_timer()
1483 timer.it_overrun = -1; in do_cpu_nanosleep()
1546 error = -ERESTART_RESTARTBLOCK; in do_cpu_nanosleep()
1550 restart = ¤t->restart_block; in do_cpu_nanosleep()
1551 restart->nanosleep.expires = expires; in do_cpu_nanosleep()
1552 if (restart->nanosleep.type != TT_NONE) in do_cpu_nanosleep()
1564 struct restart_block *restart_block = ¤t->restart_block; in posix_cpu_nsleep()
1573 return -EINVAL; in posix_cpu_nsleep()
1577 if (error == -ERESTART_RESTARTBLOCK) { in posix_cpu_nsleep()
1580 return -ERESTARTNOHAND; in posix_cpu_nsleep()
1582 restart_block->nanosleep.clockid = which_clock; in posix_cpu_nsleep()
1590 clockid_t which_clock = restart_block->nanosleep.clockid; in posix_cpu_nsleep_restart()
1593 t = ns_to_timespec64(restart_block->nanosleep.expires); in posix_cpu_nsleep_restart()
1613 timer->it_clock = PROCESS_CLOCK; in process_cpu_timer_create()
1633 timer->it_clock = THREAD_CLOCK; in thread_cpu_timer_create()