^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 1992 Darren Senn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* These are all the functions necessary to implement itimers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/posix-timers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <trace/events/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * itimer_get_remtime - get remaining time for the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @timer: the timer to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Returns the delta between the expiry time and now, which can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * less than zero or 1usec for an pending expired timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct timespec64 itimer_get_remtime(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ktime_t rem = __hrtimer_get_remaining(timer, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Racy but safe: if the itimer expires after the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * hrtimer_get_remtime() call but before this condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * then we return 0 - which is correct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (hrtimer_active(timer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (rem <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rem = NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) rem = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ktime_to_timespec64(rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct itimerspec64 *const value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u64 val, interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct cpu_itimer *it = &tsk->signal->it[clock_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_lock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) val = it->expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) interval = it->incr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) u64 t, samples[CPUCLOCK_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) thread_group_sample_cputime(tsk, samples);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) t = samples[clock_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (val < t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* about to fire */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) val = TICK_NSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) val -= t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) spin_unlock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) value->it_value = ns_to_timespec64(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) value->it_interval = ns_to_timespec64(interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int do_getitimer(int which, struct itimerspec64 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) switch (which) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case ITIMER_REAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) spin_lock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) value->it_interval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ktime_to_timespec64(tsk->signal->it_real_incr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spin_unlock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case ITIMER_VIRTUAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) case ITIMER_PROF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int put_itimerval(struct __kernel_old_itimerval __user *o,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) const struct itimerspec64 *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct __kernel_old_itimerval v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) v.it_interval.tv_sec = i->it_interval.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) v.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) v.it_value.tv_sec = i->it_value.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) v.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return copy_to_user(o, &v, sizeof(struct __kernel_old_itimerval)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) SYSCALL_DEFINE2(getitimer, int, which, struct __kernel_old_itimerval __user *, value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct itimerspec64 get_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int error = do_getitimer(which, &get_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!error && put_itimerval(value, &get_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct old_itimerval32 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct old_timeval32 it_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct old_timeval32 it_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int put_old_itimerval32(struct old_itimerval32 __user *o,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) const struct itimerspec64 *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct old_itimerval32 v32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) v32.it_interval.tv_sec = i->it_interval.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) v32.it_interval.tv_usec = i->it_interval.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) v32.it_value.tv_sec = i->it_value.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) v32.it_value.tv_usec = i->it_value.tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return copy_to_user(o, &v32, sizeof(struct old_itimerval32)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct old_itimerval32 __user *, value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct itimerspec64 get_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int error = do_getitimer(which, &get_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!error && put_old_itimerval32(value, &get_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * The timer is automagically restarted, when interval != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) enum hrtimer_restart it_real_fn(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct signal_struct *sig =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) container_of(timer, struct signal_struct, real_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) const struct itimerspec64 *const value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct itimerspec64 *const ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) u64 oval, nval, ointerval, ninterval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct cpu_itimer *it = &tsk->signal->it[clock_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) nval = timespec64_to_ns(&value->it_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ninterval = timespec64_to_ns(&value->it_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) spin_lock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) oval = it->expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ointerval = it->incr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (oval || nval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (nval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) nval += TICK_NSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) set_process_cpu_timer(tsk, clock_id, &nval, &oval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) it->expires = nval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) it->incr = ninterval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) spin_unlock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (ovalue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ovalue->it_value = ns_to_timespec64(oval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ovalue->it_interval = ns_to_timespec64(ointerval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Returns true if the timeval is in canonical form
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #define timeval_valid(t) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int do_setitimer(int which, struct itimerspec64 *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct itimerspec64 *ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct hrtimer *timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ktime_t expires;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) switch (which) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) case ITIMER_REAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) spin_lock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) timer = &tsk->signal->real_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (ovalue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ovalue->it_value = itimer_get_remtime(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ovalue->it_interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) = ktime_to_timespec64(tsk->signal->it_real_incr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* We are sharing ->siglock with it_real_fn() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (hrtimer_try_to_cancel(timer) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) hrtimer_cancel_wait_running(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) expires = timespec64_to_ktime(value->it_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (expires != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) tsk->signal->it_real_incr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) timespec64_to_ktime(value->it_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) hrtimer_start(timer, expires, HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) tsk->signal->it_real_incr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) trace_itimer_state(ITIMER_REAL, value, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) spin_unlock_irq(&tsk->sighand->siglock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) case ITIMER_VIRTUAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) case ITIMER_PROF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #ifdef CONFIG_SECURITY_SELINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) void clear_itimer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct itimerspec64 v = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) for (i = 0; i < 3; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) do_setitimer(i, &v, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #ifdef __ARCH_WANT_SYS_ALARM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * alarm_setitimer - set alarm in seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * @seconds: number of seconds until alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * 0 disables the alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Returns the remaining time in seconds of a pending timer or 0 when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * the timer is not active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * negative timeval settings which would cause immediate expiry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static unsigned int alarm_setitimer(unsigned int seconds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct itimerspec64 it_new, it_old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #if BITS_PER_LONG < 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (seconds > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) seconds = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) it_new.it_value.tv_sec = seconds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) it_new.it_value.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) it_new.it_interval.tv_sec = it_new.it_interval.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) do_setitimer(ITIMER_REAL, &it_new, &it_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * We can't return 0 if we have an alarm pending ... And we'd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * better return too much than too little anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if ((!it_old.it_value.tv_sec && it_old.it_value.tv_nsec) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) it_old.it_value.tv_nsec >= (NSEC_PER_SEC / 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) it_old.it_value.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return it_old.it_value.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * For backwards compatibility? This can be done in libc so Alpha
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * and all newer ports shouldn't need it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) SYSCALL_DEFINE1(alarm, unsigned int, seconds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return alarm_setitimer(seconds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int get_itimerval(struct itimerspec64 *o, const struct __kernel_old_itimerval __user *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct __kernel_old_itimerval v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (copy_from_user(&v, i, sizeof(struct __kernel_old_itimerval)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* Validate the timevals in value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!timeval_valid(&v.it_value) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) !timeval_valid(&v.it_interval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) o->it_interval.tv_sec = v.it_interval.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) o->it_interval.tv_nsec = v.it_interval.tv_usec * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) o->it_value.tv_sec = v.it_value.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) o->it_value.tv_nsec = v.it_value.tv_usec * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) SYSCALL_DEFINE3(setitimer, int, which, struct __kernel_old_itimerval __user *, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct __kernel_old_itimerval __user *, ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct itimerspec64 set_buffer, get_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) error = get_itimerval(&set_buffer, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) memset(&set_buffer, 0, sizeof(set_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) " Misfeature support will be removed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (error || !ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (put_itimerval(ovalue, &get_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) #if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int get_old_itimerval32(struct itimerspec64 *o, const struct old_itimerval32 __user *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct old_itimerval32 v32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (copy_from_user(&v32, i, sizeof(struct old_itimerval32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* Validate the timevals in value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!timeval_valid(&v32.it_value) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) !timeval_valid(&v32.it_interval))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) o->it_interval.tv_sec = v32.it_interval.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) o->it_interval.tv_nsec = v32.it_interval.tv_usec * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) o->it_value.tv_sec = v32.it_value.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) o->it_value.tv_nsec = v32.it_value.tv_usec * NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct old_itimerval32 __user *, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct old_itimerval32 __user *, ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct itimerspec64 set_buffer, get_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) error = get_old_itimerval32(&set_buffer, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) memset(&set_buffer, 0, sizeof(set_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) " Misfeature support will be removed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (error || !ovalue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (put_old_itimerval32(ovalue, &get_buffer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #endif