1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 25cee9645SThomas Gleixner /* 35cee9645SThomas Gleixner * Copyright (C) 1992 Darren Senn 45cee9645SThomas Gleixner */ 55cee9645SThomas Gleixner 65cee9645SThomas Gleixner /* These are all the functions necessary to implement itimers */ 75cee9645SThomas Gleixner 85cee9645SThomas Gleixner #include <linux/mm.h> 95cee9645SThomas Gleixner #include <linux/interrupt.h> 105cee9645SThomas Gleixner #include <linux/syscalls.h> 115cee9645SThomas Gleixner #include <linux/time.h> 123f07c014SIngo Molnar #include <linux/sched/signal.h> 1332ef5517SIngo Molnar #include <linux/sched/cputime.h> 145cee9645SThomas Gleixner #include <linux/posix-timers.h> 155cee9645SThomas Gleixner #include <linux/hrtimer.h> 165cee9645SThomas Gleixner #include <trace/events/timer.h> 1754ad9c46SAl Viro #include <linux/compat.h> 185cee9645SThomas Gleixner 197c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 205cee9645SThomas Gleixner 215cee9645SThomas Gleixner /** 225cee9645SThomas Gleixner * itimer_get_remtime - get remaining time for the timer 235cee9645SThomas Gleixner * 245cee9645SThomas Gleixner * @timer: the timer to read 255cee9645SThomas Gleixner * 265cee9645SThomas Gleixner * Returns the delta between the expiry time and now, which can be 275cee9645SThomas Gleixner * less than zero or 1usec for an pending expired timer 285cee9645SThomas Gleixner */ 295cee9645SThomas Gleixner static struct timeval itimer_get_remtime(struct hrtimer *timer) 305cee9645SThomas Gleixner { 3151cbb524SThomas Gleixner ktime_t rem = __hrtimer_get_remaining(timer, true); 325cee9645SThomas Gleixner 335cee9645SThomas Gleixner /* 345cee9645SThomas Gleixner * Racy but safe: if the itimer expires after the above 355cee9645SThomas Gleixner * hrtimer_get_remtime() call but before this condition 365cee9645SThomas Gleixner * then we return 0 - which is correct. 375cee9645SThomas Gleixner */ 385cee9645SThomas Gleixner if (hrtimer_active(timer)) { 392456e855SThomas Gleixner if (rem <= 0) 402456e855SThomas Gleixner rem = NSEC_PER_USEC; 415cee9645SThomas Gleixner } else 422456e855SThomas Gleixner rem = 0; 435cee9645SThomas Gleixner 445cee9645SThomas Gleixner return ktime_to_timeval(rem); 455cee9645SThomas Gleixner } 465cee9645SThomas Gleixner 475cee9645SThomas Gleixner static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, 485cee9645SThomas Gleixner struct itimerval *const value) 495cee9645SThomas Gleixner { 50858cf3a8SFrederic Weisbecker u64 val, interval; 515cee9645SThomas Gleixner struct cpu_itimer *it = &tsk->signal->it[clock_id]; 525cee9645SThomas Gleixner 535cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 545cee9645SThomas Gleixner 55858cf3a8SFrederic Weisbecker val = it->expires; 56858cf3a8SFrederic Weisbecker interval = it->incr; 57858cf3a8SFrederic Weisbecker if (val) { 58ebd7e7fcSFrederic Weisbecker struct task_cputime cputime; 59858cf3a8SFrederic Weisbecker u64 t; 605cee9645SThomas Gleixner 615cee9645SThomas Gleixner thread_group_cputimer(tsk, &cputime); 625cee9645SThomas Gleixner if (clock_id == CPUCLOCK_PROF) 63858cf3a8SFrederic Weisbecker t = cputime.utime + cputime.stime; 645cee9645SThomas Gleixner else 655cee9645SThomas Gleixner /* CPUCLOCK_VIRT */ 66858cf3a8SFrederic Weisbecker t = cputime.utime; 675cee9645SThomas Gleixner 68858cf3a8SFrederic Weisbecker if (val < t) 695cee9645SThomas Gleixner /* about to fire */ 70858cf3a8SFrederic Weisbecker val = TICK_NSEC; 715cee9645SThomas Gleixner else 72858cf3a8SFrederic Weisbecker val -= t; 735cee9645SThomas Gleixner } 745cee9645SThomas Gleixner 755cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 765cee9645SThomas Gleixner 77858cf3a8SFrederic Weisbecker value->it_value = ns_to_timeval(val); 78858cf3a8SFrederic Weisbecker value->it_interval = ns_to_timeval(interval); 795cee9645SThomas Gleixner } 805cee9645SThomas Gleixner 815cee9645SThomas Gleixner int do_getitimer(int which, struct itimerval *value) 825cee9645SThomas Gleixner { 835cee9645SThomas Gleixner struct task_struct *tsk = current; 845cee9645SThomas Gleixner 855cee9645SThomas Gleixner switch (which) { 865cee9645SThomas Gleixner case ITIMER_REAL: 875cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 885cee9645SThomas Gleixner value->it_value = itimer_get_remtime(&tsk->signal->real_timer); 895cee9645SThomas Gleixner value->it_interval = 905cee9645SThomas Gleixner ktime_to_timeval(tsk->signal->it_real_incr); 915cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 925cee9645SThomas Gleixner break; 935cee9645SThomas Gleixner case ITIMER_VIRTUAL: 945cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_VIRT, value); 955cee9645SThomas Gleixner break; 965cee9645SThomas Gleixner case ITIMER_PROF: 975cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_PROF, value); 985cee9645SThomas Gleixner break; 995cee9645SThomas Gleixner default: 1005cee9645SThomas Gleixner return(-EINVAL); 1015cee9645SThomas Gleixner } 1025cee9645SThomas Gleixner return 0; 1035cee9645SThomas Gleixner } 1045cee9645SThomas Gleixner 1055cee9645SThomas Gleixner SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value) 1065cee9645SThomas Gleixner { 1075cee9645SThomas Gleixner int error = -EFAULT; 1085cee9645SThomas Gleixner struct itimerval get_buffer; 1095cee9645SThomas Gleixner 1105cee9645SThomas Gleixner if (value) { 1115cee9645SThomas Gleixner error = do_getitimer(which, &get_buffer); 1125cee9645SThomas Gleixner if (!error && 1135cee9645SThomas Gleixner copy_to_user(value, &get_buffer, sizeof(get_buffer))) 1145cee9645SThomas Gleixner error = -EFAULT; 1155cee9645SThomas Gleixner } 1165cee9645SThomas Gleixner return error; 1175cee9645SThomas Gleixner } 1185cee9645SThomas Gleixner 11954ad9c46SAl Viro #ifdef CONFIG_COMPAT 12054ad9c46SAl Viro COMPAT_SYSCALL_DEFINE2(getitimer, int, which, 12154ad9c46SAl Viro struct compat_itimerval __user *, it) 12254ad9c46SAl Viro { 12354ad9c46SAl Viro struct itimerval kit; 12454ad9c46SAl Viro int error = do_getitimer(which, &kit); 12554ad9c46SAl Viro 12654ad9c46SAl Viro if (!error && put_compat_itimerval(it, &kit)) 12754ad9c46SAl Viro error = -EFAULT; 12854ad9c46SAl Viro return error; 12954ad9c46SAl Viro } 13054ad9c46SAl Viro #endif 13154ad9c46SAl Viro 1325cee9645SThomas Gleixner 1335cee9645SThomas Gleixner /* 1345cee9645SThomas Gleixner * The timer is automagically restarted, when interval != 0 1355cee9645SThomas Gleixner */ 1365cee9645SThomas Gleixner enum hrtimer_restart it_real_fn(struct hrtimer *timer) 1375cee9645SThomas Gleixner { 1385cee9645SThomas Gleixner struct signal_struct *sig = 1395cee9645SThomas Gleixner container_of(timer, struct signal_struct, real_timer); 1406883f81aSEric W. Biederman struct pid *leader_pid = sig->pids[PIDTYPE_TGID]; 1415cee9645SThomas Gleixner 1426883f81aSEric W. Biederman trace_itimer_expire(ITIMER_REAL, leader_pid, 0); 1436883f81aSEric W. Biederman kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid); 1445cee9645SThomas Gleixner 1455cee9645SThomas Gleixner return HRTIMER_NORESTART; 1465cee9645SThomas Gleixner } 1475cee9645SThomas Gleixner 1485cee9645SThomas Gleixner static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, 1495cee9645SThomas Gleixner const struct itimerval *const value, 1505cee9645SThomas Gleixner struct itimerval *const ovalue) 1515cee9645SThomas Gleixner { 152858cf3a8SFrederic Weisbecker u64 oval, nval, ointerval, ninterval; 1535cee9645SThomas Gleixner struct cpu_itimer *it = &tsk->signal->it[clock_id]; 1545cee9645SThomas Gleixner 15535eb7258SThomas Gleixner /* 15635eb7258SThomas Gleixner * Use the to_ktime conversion because that clamps the maximum 15735eb7258SThomas Gleixner * value to KTIME_MAX and avoid multiplication overflows. 15835eb7258SThomas Gleixner */ 15935eb7258SThomas Gleixner nval = ktime_to_ns(timeval_to_ktime(value->it_value)); 16035eb7258SThomas Gleixner ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval)); 1615cee9645SThomas Gleixner 1625cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 1635cee9645SThomas Gleixner 164858cf3a8SFrederic Weisbecker oval = it->expires; 165858cf3a8SFrederic Weisbecker ointerval = it->incr; 166858cf3a8SFrederic Weisbecker if (oval || nval) { 1675cee9645SThomas Gleixner if (nval > 0) 168858cf3a8SFrederic Weisbecker nval += TICK_NSEC; 169858cf3a8SFrederic Weisbecker set_process_cpu_timer(tsk, clock_id, &nval, &oval); 1705cee9645SThomas Gleixner } 1715cee9645SThomas Gleixner it->expires = nval; 1725cee9645SThomas Gleixner it->incr = ninterval; 1735cee9645SThomas Gleixner trace_itimer_state(clock_id == CPUCLOCK_VIRT ? 1745cee9645SThomas Gleixner ITIMER_VIRTUAL : ITIMER_PROF, value, nval); 1755cee9645SThomas Gleixner 1765cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 1775cee9645SThomas Gleixner 1785cee9645SThomas Gleixner if (ovalue) { 179858cf3a8SFrederic Weisbecker ovalue->it_value = ns_to_timeval(oval); 180858cf3a8SFrederic Weisbecker ovalue->it_interval = ns_to_timeval(ointerval); 1815cee9645SThomas Gleixner } 1825cee9645SThomas Gleixner } 1835cee9645SThomas Gleixner 1845cee9645SThomas Gleixner /* 1855cee9645SThomas Gleixner * Returns true if the timeval is in canonical form 1865cee9645SThomas Gleixner */ 1875cee9645SThomas Gleixner #define timeval_valid(t) \ 1885cee9645SThomas Gleixner (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC)) 1895cee9645SThomas Gleixner 1905cee9645SThomas Gleixner int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue) 1915cee9645SThomas Gleixner { 1925cee9645SThomas Gleixner struct task_struct *tsk = current; 1935cee9645SThomas Gleixner struct hrtimer *timer; 1945cee9645SThomas Gleixner ktime_t expires; 1955cee9645SThomas Gleixner 1965cee9645SThomas Gleixner /* 1975cee9645SThomas Gleixner * Validate the timevals in value. 1985cee9645SThomas Gleixner */ 1995cee9645SThomas Gleixner if (!timeval_valid(&value->it_value) || 2005cee9645SThomas Gleixner !timeval_valid(&value->it_interval)) 2015cee9645SThomas Gleixner return -EINVAL; 2025cee9645SThomas Gleixner 2035cee9645SThomas Gleixner switch (which) { 2045cee9645SThomas Gleixner case ITIMER_REAL: 2055cee9645SThomas Gleixner again: 2065cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 2075cee9645SThomas Gleixner timer = &tsk->signal->real_timer; 2085cee9645SThomas Gleixner if (ovalue) { 2095cee9645SThomas Gleixner ovalue->it_value = itimer_get_remtime(timer); 2105cee9645SThomas Gleixner ovalue->it_interval 2115cee9645SThomas Gleixner = ktime_to_timeval(tsk->signal->it_real_incr); 2125cee9645SThomas Gleixner } 2135cee9645SThomas Gleixner /* We are sharing ->siglock with it_real_fn() */ 2145cee9645SThomas Gleixner if (hrtimer_try_to_cancel(timer) < 0) { 2155cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 216c7e6d704SAnna-Maria Gleixner hrtimer_cancel_wait_running(timer); 2175cee9645SThomas Gleixner goto again; 2185cee9645SThomas Gleixner } 2195cee9645SThomas Gleixner expires = timeval_to_ktime(value->it_value); 2202456e855SThomas Gleixner if (expires != 0) { 2215cee9645SThomas Gleixner tsk->signal->it_real_incr = 2225cee9645SThomas Gleixner timeval_to_ktime(value->it_interval); 2235cee9645SThomas Gleixner hrtimer_start(timer, expires, HRTIMER_MODE_REL); 2245cee9645SThomas Gleixner } else 2252456e855SThomas Gleixner tsk->signal->it_real_incr = 0; 2265cee9645SThomas Gleixner 2275cee9645SThomas Gleixner trace_itimer_state(ITIMER_REAL, value, 0); 2285cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 2295cee9645SThomas Gleixner break; 2305cee9645SThomas Gleixner case ITIMER_VIRTUAL: 2315cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue); 2325cee9645SThomas Gleixner break; 2335cee9645SThomas Gleixner case ITIMER_PROF: 2345cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue); 2355cee9645SThomas Gleixner break; 2365cee9645SThomas Gleixner default: 2375cee9645SThomas Gleixner return -EINVAL; 2385cee9645SThomas Gleixner } 2395cee9645SThomas Gleixner return 0; 2405cee9645SThomas Gleixner } 2415cee9645SThomas Gleixner 24274ba181eSNicolas Pitre #ifdef __ARCH_WANT_SYS_ALARM 24374ba181eSNicolas Pitre 2445cee9645SThomas Gleixner /** 2455cee9645SThomas Gleixner * alarm_setitimer - set alarm in seconds 2465cee9645SThomas Gleixner * 2475cee9645SThomas Gleixner * @seconds: number of seconds until alarm 2485cee9645SThomas Gleixner * 0 disables the alarm 2495cee9645SThomas Gleixner * 2505cee9645SThomas Gleixner * Returns the remaining time in seconds of a pending timer or 0 when 2515cee9645SThomas Gleixner * the timer is not active. 2525cee9645SThomas Gleixner * 2535cee9645SThomas Gleixner * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid 2545cee9645SThomas Gleixner * negative timeval settings which would cause immediate expiry. 2555cee9645SThomas Gleixner */ 25674ba181eSNicolas Pitre static unsigned int alarm_setitimer(unsigned int seconds) 2575cee9645SThomas Gleixner { 2585cee9645SThomas Gleixner struct itimerval it_new, it_old; 2595cee9645SThomas Gleixner 2605cee9645SThomas Gleixner #if BITS_PER_LONG < 64 2615cee9645SThomas Gleixner if (seconds > INT_MAX) 2625cee9645SThomas Gleixner seconds = INT_MAX; 2635cee9645SThomas Gleixner #endif 2645cee9645SThomas Gleixner it_new.it_value.tv_sec = seconds; 2655cee9645SThomas Gleixner it_new.it_value.tv_usec = 0; 2665cee9645SThomas Gleixner it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0; 2675cee9645SThomas Gleixner 2685cee9645SThomas Gleixner do_setitimer(ITIMER_REAL, &it_new, &it_old); 2695cee9645SThomas Gleixner 2705cee9645SThomas Gleixner /* 2715cee9645SThomas Gleixner * We can't return 0 if we have an alarm pending ... And we'd 2725cee9645SThomas Gleixner * better return too much than too little anyway 2735cee9645SThomas Gleixner */ 2745cee9645SThomas Gleixner if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) || 2755cee9645SThomas Gleixner it_old.it_value.tv_usec >= 500000) 2765cee9645SThomas Gleixner it_old.it_value.tv_sec++; 2775cee9645SThomas Gleixner 2785cee9645SThomas Gleixner return it_old.it_value.tv_sec; 2795cee9645SThomas Gleixner } 2805cee9645SThomas Gleixner 28174ba181eSNicolas Pitre /* 28274ba181eSNicolas Pitre * For backwards compatibility? This can be done in libc so Alpha 28374ba181eSNicolas Pitre * and all newer ports shouldn't need it. 28474ba181eSNicolas Pitre */ 28574ba181eSNicolas Pitre SYSCALL_DEFINE1(alarm, unsigned int, seconds) 28674ba181eSNicolas Pitre { 28774ba181eSNicolas Pitre return alarm_setitimer(seconds); 28874ba181eSNicolas Pitre } 28974ba181eSNicolas Pitre 29074ba181eSNicolas Pitre #endif 29174ba181eSNicolas Pitre 2925cee9645SThomas Gleixner SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value, 2935cee9645SThomas Gleixner struct itimerval __user *, ovalue) 2945cee9645SThomas Gleixner { 2955cee9645SThomas Gleixner struct itimerval set_buffer, get_buffer; 2965cee9645SThomas Gleixner int error; 2975cee9645SThomas Gleixner 2985cee9645SThomas Gleixner if (value) { 2995cee9645SThomas Gleixner if(copy_from_user(&set_buffer, value, sizeof(set_buffer))) 3005cee9645SThomas Gleixner return -EFAULT; 3015cee9645SThomas Gleixner } else { 3025cee9645SThomas Gleixner memset(&set_buffer, 0, sizeof(set_buffer)); 3035cee9645SThomas Gleixner printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer." 3045cee9645SThomas Gleixner " Misfeature support will be removed\n", 3055cee9645SThomas Gleixner current->comm); 3065cee9645SThomas Gleixner } 3075cee9645SThomas Gleixner 3085cee9645SThomas Gleixner error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL); 3095cee9645SThomas Gleixner if (error || !ovalue) 3105cee9645SThomas Gleixner return error; 3115cee9645SThomas Gleixner 3125cee9645SThomas Gleixner if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer))) 3135cee9645SThomas Gleixner return -EFAULT; 3145cee9645SThomas Gleixner return 0; 3155cee9645SThomas Gleixner } 31654ad9c46SAl Viro 31754ad9c46SAl Viro #ifdef CONFIG_COMPAT 31854ad9c46SAl Viro COMPAT_SYSCALL_DEFINE3(setitimer, int, which, 31954ad9c46SAl Viro struct compat_itimerval __user *, in, 32054ad9c46SAl Viro struct compat_itimerval __user *, out) 32154ad9c46SAl Viro { 32254ad9c46SAl Viro struct itimerval kin, kout; 32354ad9c46SAl Viro int error; 32454ad9c46SAl Viro 32554ad9c46SAl Viro if (in) { 32654ad9c46SAl Viro if (get_compat_itimerval(&kin, in)) 32754ad9c46SAl Viro return -EFAULT; 32854ad9c46SAl Viro } else { 32954ad9c46SAl Viro memset(&kin, 0, sizeof(kin)); 33054ad9c46SAl Viro } 33154ad9c46SAl Viro 33254ad9c46SAl Viro error = do_setitimer(which, &kin, out ? &kout : NULL); 33354ad9c46SAl Viro if (error || !out) 33454ad9c46SAl Viro return error; 33554ad9c46SAl Viro if (put_compat_itimerval(out, &kout)) 33654ad9c46SAl Viro return -EFAULT; 33754ad9c46SAl Viro return 0; 33854ad9c46SAl Viro } 33954ad9c46SAl Viro #endif 340