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) { 58b7be4ef1SThomas Gleixner u64 t, samples[CPUCLOCK_MAX]; 595cee9645SThomas Gleixner 60b7be4ef1SThomas Gleixner thread_group_sample_cputime(tsk, samples); 61b7be4ef1SThomas Gleixner t = samples[clock_id]; 625cee9645SThomas Gleixner 63858cf3a8SFrederic Weisbecker if (val < t) 645cee9645SThomas Gleixner /* about to fire */ 65858cf3a8SFrederic Weisbecker val = TICK_NSEC; 665cee9645SThomas Gleixner else 67858cf3a8SFrederic Weisbecker val -= t; 685cee9645SThomas Gleixner } 695cee9645SThomas Gleixner 705cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 715cee9645SThomas Gleixner 72858cf3a8SFrederic Weisbecker value->it_value = ns_to_timeval(val); 73858cf3a8SFrederic Weisbecker value->it_interval = ns_to_timeval(interval); 745cee9645SThomas Gleixner } 755cee9645SThomas Gleixner 765cee9645SThomas Gleixner int do_getitimer(int which, struct itimerval *value) 775cee9645SThomas Gleixner { 785cee9645SThomas Gleixner struct task_struct *tsk = current; 795cee9645SThomas Gleixner 805cee9645SThomas Gleixner switch (which) { 815cee9645SThomas Gleixner case ITIMER_REAL: 825cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 835cee9645SThomas Gleixner value->it_value = itimer_get_remtime(&tsk->signal->real_timer); 845cee9645SThomas Gleixner value->it_interval = 855cee9645SThomas Gleixner ktime_to_timeval(tsk->signal->it_real_incr); 865cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 875cee9645SThomas Gleixner break; 885cee9645SThomas Gleixner case ITIMER_VIRTUAL: 895cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_VIRT, value); 905cee9645SThomas Gleixner break; 915cee9645SThomas Gleixner case ITIMER_PROF: 925cee9645SThomas Gleixner get_cpu_itimer(tsk, CPUCLOCK_PROF, value); 935cee9645SThomas Gleixner break; 945cee9645SThomas Gleixner default: 955cee9645SThomas Gleixner return(-EINVAL); 965cee9645SThomas Gleixner } 975cee9645SThomas Gleixner return 0; 985cee9645SThomas Gleixner } 995cee9645SThomas Gleixner 1005cee9645SThomas Gleixner SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value) 1015cee9645SThomas Gleixner { 1025cee9645SThomas Gleixner int error = -EFAULT; 1035cee9645SThomas Gleixner struct itimerval get_buffer; 1045cee9645SThomas Gleixner 1055cee9645SThomas Gleixner if (value) { 1065cee9645SThomas Gleixner error = do_getitimer(which, &get_buffer); 1075cee9645SThomas Gleixner if (!error && 1085cee9645SThomas Gleixner copy_to_user(value, &get_buffer, sizeof(get_buffer))) 1095cee9645SThomas Gleixner error = -EFAULT; 1105cee9645SThomas Gleixner } 1115cee9645SThomas Gleixner return error; 1125cee9645SThomas Gleixner } 1135cee9645SThomas Gleixner 11454ad9c46SAl Viro #ifdef CONFIG_COMPAT 11554ad9c46SAl Viro COMPAT_SYSCALL_DEFINE2(getitimer, int, which, 11654ad9c46SAl Viro struct compat_itimerval __user *, it) 11754ad9c46SAl Viro { 11854ad9c46SAl Viro struct itimerval kit; 11954ad9c46SAl Viro int error = do_getitimer(which, &kit); 12054ad9c46SAl Viro 12154ad9c46SAl Viro if (!error && put_compat_itimerval(it, &kit)) 12254ad9c46SAl Viro error = -EFAULT; 12354ad9c46SAl Viro return error; 12454ad9c46SAl Viro } 12554ad9c46SAl Viro #endif 12654ad9c46SAl Viro 1275cee9645SThomas Gleixner 1285cee9645SThomas Gleixner /* 1295cee9645SThomas Gleixner * The timer is automagically restarted, when interval != 0 1305cee9645SThomas Gleixner */ 1315cee9645SThomas Gleixner enum hrtimer_restart it_real_fn(struct hrtimer *timer) 1325cee9645SThomas Gleixner { 1335cee9645SThomas Gleixner struct signal_struct *sig = 1345cee9645SThomas Gleixner container_of(timer, struct signal_struct, real_timer); 1356883f81aSEric W. Biederman struct pid *leader_pid = sig->pids[PIDTYPE_TGID]; 1365cee9645SThomas Gleixner 1376883f81aSEric W. Biederman trace_itimer_expire(ITIMER_REAL, leader_pid, 0); 1386883f81aSEric W. Biederman kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid); 1395cee9645SThomas Gleixner 1405cee9645SThomas Gleixner return HRTIMER_NORESTART; 1415cee9645SThomas Gleixner } 1425cee9645SThomas Gleixner 1435cee9645SThomas Gleixner static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, 1445cee9645SThomas Gleixner const struct itimerval *const value, 1455cee9645SThomas Gleixner struct itimerval *const ovalue) 1465cee9645SThomas Gleixner { 147858cf3a8SFrederic Weisbecker u64 oval, nval, ointerval, ninterval; 1485cee9645SThomas Gleixner struct cpu_itimer *it = &tsk->signal->it[clock_id]; 1495cee9645SThomas Gleixner 15035eb7258SThomas Gleixner /* 15135eb7258SThomas Gleixner * Use the to_ktime conversion because that clamps the maximum 15235eb7258SThomas Gleixner * value to KTIME_MAX and avoid multiplication overflows. 15335eb7258SThomas Gleixner */ 15435eb7258SThomas Gleixner nval = ktime_to_ns(timeval_to_ktime(value->it_value)); 15535eb7258SThomas Gleixner ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval)); 1565cee9645SThomas Gleixner 1575cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 1585cee9645SThomas Gleixner 159858cf3a8SFrederic Weisbecker oval = it->expires; 160858cf3a8SFrederic Weisbecker ointerval = it->incr; 161858cf3a8SFrederic Weisbecker if (oval || nval) { 1625cee9645SThomas Gleixner if (nval > 0) 163858cf3a8SFrederic Weisbecker nval += TICK_NSEC; 164858cf3a8SFrederic Weisbecker set_process_cpu_timer(tsk, clock_id, &nval, &oval); 1655cee9645SThomas Gleixner } 1665cee9645SThomas Gleixner it->expires = nval; 1675cee9645SThomas Gleixner it->incr = ninterval; 1685cee9645SThomas Gleixner trace_itimer_state(clock_id == CPUCLOCK_VIRT ? 1695cee9645SThomas Gleixner ITIMER_VIRTUAL : ITIMER_PROF, value, nval); 1705cee9645SThomas Gleixner 1715cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 1725cee9645SThomas Gleixner 1735cee9645SThomas Gleixner if (ovalue) { 174858cf3a8SFrederic Weisbecker ovalue->it_value = ns_to_timeval(oval); 175858cf3a8SFrederic Weisbecker ovalue->it_interval = ns_to_timeval(ointerval); 1765cee9645SThomas Gleixner } 1775cee9645SThomas Gleixner } 1785cee9645SThomas Gleixner 1795cee9645SThomas Gleixner /* 1805cee9645SThomas Gleixner * Returns true if the timeval is in canonical form 1815cee9645SThomas Gleixner */ 1825cee9645SThomas Gleixner #define timeval_valid(t) \ 1835cee9645SThomas Gleixner (((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC)) 1845cee9645SThomas Gleixner 1855cee9645SThomas Gleixner int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue) 1865cee9645SThomas Gleixner { 1875cee9645SThomas Gleixner struct task_struct *tsk = current; 1885cee9645SThomas Gleixner struct hrtimer *timer; 1895cee9645SThomas Gleixner ktime_t expires; 1905cee9645SThomas Gleixner 1915cee9645SThomas Gleixner /* 1925cee9645SThomas Gleixner * Validate the timevals in value. 1935cee9645SThomas Gleixner */ 1945cee9645SThomas Gleixner if (!timeval_valid(&value->it_value) || 1955cee9645SThomas Gleixner !timeval_valid(&value->it_interval)) 1965cee9645SThomas Gleixner return -EINVAL; 1975cee9645SThomas Gleixner 1985cee9645SThomas Gleixner switch (which) { 1995cee9645SThomas Gleixner case ITIMER_REAL: 2005cee9645SThomas Gleixner again: 2015cee9645SThomas Gleixner spin_lock_irq(&tsk->sighand->siglock); 2025cee9645SThomas Gleixner timer = &tsk->signal->real_timer; 2035cee9645SThomas Gleixner if (ovalue) { 2045cee9645SThomas Gleixner ovalue->it_value = itimer_get_remtime(timer); 2055cee9645SThomas Gleixner ovalue->it_interval 2065cee9645SThomas Gleixner = ktime_to_timeval(tsk->signal->it_real_incr); 2075cee9645SThomas Gleixner } 2085cee9645SThomas Gleixner /* We are sharing ->siglock with it_real_fn() */ 2095cee9645SThomas Gleixner if (hrtimer_try_to_cancel(timer) < 0) { 2105cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 211c7e6d704SAnna-Maria Gleixner hrtimer_cancel_wait_running(timer); 2125cee9645SThomas Gleixner goto again; 2135cee9645SThomas Gleixner } 2145cee9645SThomas Gleixner expires = timeval_to_ktime(value->it_value); 2152456e855SThomas Gleixner if (expires != 0) { 2165cee9645SThomas Gleixner tsk->signal->it_real_incr = 2175cee9645SThomas Gleixner timeval_to_ktime(value->it_interval); 2185cee9645SThomas Gleixner hrtimer_start(timer, expires, HRTIMER_MODE_REL); 2195cee9645SThomas Gleixner } else 2202456e855SThomas Gleixner tsk->signal->it_real_incr = 0; 2215cee9645SThomas Gleixner 2225cee9645SThomas Gleixner trace_itimer_state(ITIMER_REAL, value, 0); 2235cee9645SThomas Gleixner spin_unlock_irq(&tsk->sighand->siglock); 2245cee9645SThomas Gleixner break; 2255cee9645SThomas Gleixner case ITIMER_VIRTUAL: 2265cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue); 2275cee9645SThomas Gleixner break; 2285cee9645SThomas Gleixner case ITIMER_PROF: 2295cee9645SThomas Gleixner set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue); 2305cee9645SThomas Gleixner break; 2315cee9645SThomas Gleixner default: 2325cee9645SThomas Gleixner return -EINVAL; 2335cee9645SThomas Gleixner } 2345cee9645SThomas Gleixner return 0; 2355cee9645SThomas Gleixner } 2365cee9645SThomas Gleixner 23774ba181eSNicolas Pitre #ifdef __ARCH_WANT_SYS_ALARM 23874ba181eSNicolas Pitre 2395cee9645SThomas Gleixner /** 2405cee9645SThomas Gleixner * alarm_setitimer - set alarm in seconds 2415cee9645SThomas Gleixner * 2425cee9645SThomas Gleixner * @seconds: number of seconds until alarm 2435cee9645SThomas Gleixner * 0 disables the alarm 2445cee9645SThomas Gleixner * 2455cee9645SThomas Gleixner * Returns the remaining time in seconds of a pending timer or 0 when 2465cee9645SThomas Gleixner * the timer is not active. 2475cee9645SThomas Gleixner * 2485cee9645SThomas Gleixner * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid 2495cee9645SThomas Gleixner * negative timeval settings which would cause immediate expiry. 2505cee9645SThomas Gleixner */ 25174ba181eSNicolas Pitre static unsigned int alarm_setitimer(unsigned int seconds) 2525cee9645SThomas Gleixner { 2535cee9645SThomas Gleixner struct itimerval it_new, it_old; 2545cee9645SThomas Gleixner 2555cee9645SThomas Gleixner #if BITS_PER_LONG < 64 2565cee9645SThomas Gleixner if (seconds > INT_MAX) 2575cee9645SThomas Gleixner seconds = INT_MAX; 2585cee9645SThomas Gleixner #endif 2595cee9645SThomas Gleixner it_new.it_value.tv_sec = seconds; 2605cee9645SThomas Gleixner it_new.it_value.tv_usec = 0; 2615cee9645SThomas Gleixner it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0; 2625cee9645SThomas Gleixner 2635cee9645SThomas Gleixner do_setitimer(ITIMER_REAL, &it_new, &it_old); 2645cee9645SThomas Gleixner 2655cee9645SThomas Gleixner /* 2665cee9645SThomas Gleixner * We can't return 0 if we have an alarm pending ... And we'd 2675cee9645SThomas Gleixner * better return too much than too little anyway 2685cee9645SThomas Gleixner */ 2695cee9645SThomas Gleixner if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) || 2705cee9645SThomas Gleixner it_old.it_value.tv_usec >= 500000) 2715cee9645SThomas Gleixner it_old.it_value.tv_sec++; 2725cee9645SThomas Gleixner 2735cee9645SThomas Gleixner return it_old.it_value.tv_sec; 2745cee9645SThomas Gleixner } 2755cee9645SThomas Gleixner 27674ba181eSNicolas Pitre /* 27774ba181eSNicolas Pitre * For backwards compatibility? This can be done in libc so Alpha 27874ba181eSNicolas Pitre * and all newer ports shouldn't need it. 27974ba181eSNicolas Pitre */ 28074ba181eSNicolas Pitre SYSCALL_DEFINE1(alarm, unsigned int, seconds) 28174ba181eSNicolas Pitre { 28274ba181eSNicolas Pitre return alarm_setitimer(seconds); 28374ba181eSNicolas Pitre } 28474ba181eSNicolas Pitre 28574ba181eSNicolas Pitre #endif 28674ba181eSNicolas Pitre 2875cee9645SThomas Gleixner SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value, 2885cee9645SThomas Gleixner struct itimerval __user *, ovalue) 2895cee9645SThomas Gleixner { 2905cee9645SThomas Gleixner struct itimerval set_buffer, get_buffer; 2915cee9645SThomas Gleixner int error; 2925cee9645SThomas Gleixner 2935cee9645SThomas Gleixner if (value) { 2945cee9645SThomas Gleixner if(copy_from_user(&set_buffer, value, sizeof(set_buffer))) 2955cee9645SThomas Gleixner return -EFAULT; 2965cee9645SThomas Gleixner } else { 2975cee9645SThomas Gleixner memset(&set_buffer, 0, sizeof(set_buffer)); 2985cee9645SThomas Gleixner printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer." 2995cee9645SThomas Gleixner " Misfeature support will be removed\n", 3005cee9645SThomas Gleixner current->comm); 3015cee9645SThomas Gleixner } 3025cee9645SThomas Gleixner 3035cee9645SThomas Gleixner error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL); 3045cee9645SThomas Gleixner if (error || !ovalue) 3055cee9645SThomas Gleixner return error; 3065cee9645SThomas Gleixner 3075cee9645SThomas Gleixner if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer))) 3085cee9645SThomas Gleixner return -EFAULT; 3095cee9645SThomas Gleixner return 0; 3105cee9645SThomas Gleixner } 31154ad9c46SAl Viro 31254ad9c46SAl Viro #ifdef CONFIG_COMPAT 31354ad9c46SAl Viro COMPAT_SYSCALL_DEFINE3(setitimer, int, which, 31454ad9c46SAl Viro struct compat_itimerval __user *, in, 31554ad9c46SAl Viro struct compat_itimerval __user *, out) 31654ad9c46SAl Viro { 31754ad9c46SAl Viro struct itimerval kin, kout; 31854ad9c46SAl Viro int error; 31954ad9c46SAl Viro 32054ad9c46SAl Viro if (in) { 32154ad9c46SAl Viro if (get_compat_itimerval(&kin, in)) 32254ad9c46SAl Viro return -EFAULT; 32354ad9c46SAl Viro } else { 32454ad9c46SAl Viro memset(&kin, 0, sizeof(kin)); 32554ad9c46SAl Viro } 32654ad9c46SAl Viro 32754ad9c46SAl Viro error = do_setitimer(which, &kin, out ? &kout : NULL); 32854ad9c46SAl Viro if (error || !out) 32954ad9c46SAl Viro return error; 33054ad9c46SAl Viro if (put_compat_itimerval(out, &kout)) 33154ad9c46SAl Viro return -EFAULT; 33254ad9c46SAl Viro return 0; 33354ad9c46SAl Viro } 33454ad9c46SAl Viro #endif 335