xref: /linux/kernel/time/itimer.c (revision 54ad9c46)
15cee9645SThomas Gleixner /*
25cee9645SThomas Gleixner  * linux/kernel/itimer.c
35cee9645SThomas Gleixner  *
45cee9645SThomas Gleixner  * Copyright (C) 1992 Darren Senn
55cee9645SThomas Gleixner  */
65cee9645SThomas Gleixner 
75cee9645SThomas Gleixner /* These are all the functions necessary to implement itimers */
85cee9645SThomas Gleixner 
95cee9645SThomas Gleixner #include <linux/mm.h>
105cee9645SThomas Gleixner #include <linux/interrupt.h>
115cee9645SThomas Gleixner #include <linux/syscalls.h>
125cee9645SThomas Gleixner #include <linux/time.h>
133f07c014SIngo Molnar #include <linux/sched/signal.h>
1432ef5517SIngo Molnar #include <linux/sched/cputime.h>
155cee9645SThomas Gleixner #include <linux/posix-timers.h>
165cee9645SThomas Gleixner #include <linux/hrtimer.h>
175cee9645SThomas Gleixner #include <trace/events/timer.h>
18*54ad9c46SAl Viro #include <linux/compat.h>
195cee9645SThomas Gleixner 
207c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
215cee9645SThomas Gleixner 
225cee9645SThomas Gleixner /**
235cee9645SThomas Gleixner  * itimer_get_remtime - get remaining time for the timer
245cee9645SThomas Gleixner  *
255cee9645SThomas Gleixner  * @timer: the timer to read
265cee9645SThomas Gleixner  *
275cee9645SThomas Gleixner  * Returns the delta between the expiry time and now, which can be
285cee9645SThomas Gleixner  * less than zero or 1usec for an pending expired timer
295cee9645SThomas Gleixner  */
305cee9645SThomas Gleixner static struct timeval itimer_get_remtime(struct hrtimer *timer)
315cee9645SThomas Gleixner {
3251cbb524SThomas Gleixner 	ktime_t rem = __hrtimer_get_remaining(timer, true);
335cee9645SThomas Gleixner 
345cee9645SThomas Gleixner 	/*
355cee9645SThomas Gleixner 	 * Racy but safe: if the itimer expires after the above
365cee9645SThomas Gleixner 	 * hrtimer_get_remtime() call but before this condition
375cee9645SThomas Gleixner 	 * then we return 0 - which is correct.
385cee9645SThomas Gleixner 	 */
395cee9645SThomas Gleixner 	if (hrtimer_active(timer)) {
402456e855SThomas Gleixner 		if (rem <= 0)
412456e855SThomas Gleixner 			rem = NSEC_PER_USEC;
425cee9645SThomas Gleixner 	} else
432456e855SThomas Gleixner 		rem = 0;
445cee9645SThomas Gleixner 
455cee9645SThomas Gleixner 	return ktime_to_timeval(rem);
465cee9645SThomas Gleixner }
475cee9645SThomas Gleixner 
485cee9645SThomas Gleixner static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
495cee9645SThomas Gleixner 			   struct itimerval *const value)
505cee9645SThomas Gleixner {
51858cf3a8SFrederic Weisbecker 	u64 val, interval;
525cee9645SThomas Gleixner 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
535cee9645SThomas Gleixner 
545cee9645SThomas Gleixner 	spin_lock_irq(&tsk->sighand->siglock);
555cee9645SThomas Gleixner 
56858cf3a8SFrederic Weisbecker 	val = it->expires;
57858cf3a8SFrederic Weisbecker 	interval = it->incr;
58858cf3a8SFrederic Weisbecker 	if (val) {
59ebd7e7fcSFrederic Weisbecker 		struct task_cputime cputime;
60858cf3a8SFrederic Weisbecker 		u64 t;
615cee9645SThomas Gleixner 
625cee9645SThomas Gleixner 		thread_group_cputimer(tsk, &cputime);
635cee9645SThomas Gleixner 		if (clock_id == CPUCLOCK_PROF)
64858cf3a8SFrederic Weisbecker 			t = cputime.utime + cputime.stime;
655cee9645SThomas Gleixner 		else
665cee9645SThomas Gleixner 			/* CPUCLOCK_VIRT */
67858cf3a8SFrederic Weisbecker 			t = cputime.utime;
685cee9645SThomas Gleixner 
69858cf3a8SFrederic Weisbecker 		if (val < t)
705cee9645SThomas Gleixner 			/* about to fire */
71858cf3a8SFrederic Weisbecker 			val = TICK_NSEC;
725cee9645SThomas Gleixner 		else
73858cf3a8SFrederic Weisbecker 			val -= t;
745cee9645SThomas Gleixner 	}
755cee9645SThomas Gleixner 
765cee9645SThomas Gleixner 	spin_unlock_irq(&tsk->sighand->siglock);
775cee9645SThomas Gleixner 
78858cf3a8SFrederic Weisbecker 	value->it_value = ns_to_timeval(val);
79858cf3a8SFrederic Weisbecker 	value->it_interval = ns_to_timeval(interval);
805cee9645SThomas Gleixner }
815cee9645SThomas Gleixner 
825cee9645SThomas Gleixner int do_getitimer(int which, struct itimerval *value)
835cee9645SThomas Gleixner {
845cee9645SThomas Gleixner 	struct task_struct *tsk = current;
855cee9645SThomas Gleixner 
865cee9645SThomas Gleixner 	switch (which) {
875cee9645SThomas Gleixner 	case ITIMER_REAL:
885cee9645SThomas Gleixner 		spin_lock_irq(&tsk->sighand->siglock);
895cee9645SThomas Gleixner 		value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
905cee9645SThomas Gleixner 		value->it_interval =
915cee9645SThomas Gleixner 			ktime_to_timeval(tsk->signal->it_real_incr);
925cee9645SThomas Gleixner 		spin_unlock_irq(&tsk->sighand->siglock);
935cee9645SThomas Gleixner 		break;
945cee9645SThomas Gleixner 	case ITIMER_VIRTUAL:
955cee9645SThomas Gleixner 		get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
965cee9645SThomas Gleixner 		break;
975cee9645SThomas Gleixner 	case ITIMER_PROF:
985cee9645SThomas Gleixner 		get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
995cee9645SThomas Gleixner 		break;
1005cee9645SThomas Gleixner 	default:
1015cee9645SThomas Gleixner 		return(-EINVAL);
1025cee9645SThomas Gleixner 	}
1035cee9645SThomas Gleixner 	return 0;
1045cee9645SThomas Gleixner }
1055cee9645SThomas Gleixner 
1065cee9645SThomas Gleixner SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
1075cee9645SThomas Gleixner {
1085cee9645SThomas Gleixner 	int error = -EFAULT;
1095cee9645SThomas Gleixner 	struct itimerval get_buffer;
1105cee9645SThomas Gleixner 
1115cee9645SThomas Gleixner 	if (value) {
1125cee9645SThomas Gleixner 		error = do_getitimer(which, &get_buffer);
1135cee9645SThomas Gleixner 		if (!error &&
1145cee9645SThomas Gleixner 		    copy_to_user(value, &get_buffer, sizeof(get_buffer)))
1155cee9645SThomas Gleixner 			error = -EFAULT;
1165cee9645SThomas Gleixner 	}
1175cee9645SThomas Gleixner 	return error;
1185cee9645SThomas Gleixner }
1195cee9645SThomas Gleixner 
120*54ad9c46SAl Viro #ifdef CONFIG_COMPAT
121*54ad9c46SAl Viro COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
122*54ad9c46SAl Viro 		       struct compat_itimerval __user *, it)
123*54ad9c46SAl Viro {
124*54ad9c46SAl Viro 	struct itimerval kit;
125*54ad9c46SAl Viro 	int error = do_getitimer(which, &kit);
126*54ad9c46SAl Viro 
127*54ad9c46SAl Viro 	if (!error && put_compat_itimerval(it, &kit))
128*54ad9c46SAl Viro 		error = -EFAULT;
129*54ad9c46SAl Viro 	return error;
130*54ad9c46SAl Viro }
131*54ad9c46SAl Viro #endif
132*54ad9c46SAl Viro 
1335cee9645SThomas Gleixner 
1345cee9645SThomas Gleixner /*
1355cee9645SThomas Gleixner  * The timer is automagically restarted, when interval != 0
1365cee9645SThomas Gleixner  */
1375cee9645SThomas Gleixner enum hrtimer_restart it_real_fn(struct hrtimer *timer)
1385cee9645SThomas Gleixner {
1395cee9645SThomas Gleixner 	struct signal_struct *sig =
1405cee9645SThomas Gleixner 		container_of(timer, struct signal_struct, real_timer);
1415cee9645SThomas Gleixner 
1425cee9645SThomas Gleixner 	trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0);
1435cee9645SThomas Gleixner 	kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->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 
155858cf3a8SFrederic Weisbecker 	nval = timeval_to_ns(&value->it_value);
156858cf3a8SFrederic Weisbecker 	ninterval = timeval_to_ns(&value->it_interval);
1575cee9645SThomas Gleixner 
1585cee9645SThomas Gleixner 	spin_lock_irq(&tsk->sighand->siglock);
1595cee9645SThomas Gleixner 
160858cf3a8SFrederic Weisbecker 	oval = it->expires;
161858cf3a8SFrederic Weisbecker 	ointerval = it->incr;
162858cf3a8SFrederic Weisbecker 	if (oval || nval) {
1635cee9645SThomas Gleixner 		if (nval > 0)
164858cf3a8SFrederic Weisbecker 			nval += TICK_NSEC;
165858cf3a8SFrederic Weisbecker 		set_process_cpu_timer(tsk, clock_id, &nval, &oval);
1665cee9645SThomas Gleixner 	}
1675cee9645SThomas Gleixner 	it->expires = nval;
1685cee9645SThomas Gleixner 	it->incr = ninterval;
1695cee9645SThomas Gleixner 	trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
1705cee9645SThomas Gleixner 			   ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
1715cee9645SThomas Gleixner 
1725cee9645SThomas Gleixner 	spin_unlock_irq(&tsk->sighand->siglock);
1735cee9645SThomas Gleixner 
1745cee9645SThomas Gleixner 	if (ovalue) {
175858cf3a8SFrederic Weisbecker 		ovalue->it_value = ns_to_timeval(oval);
176858cf3a8SFrederic Weisbecker 		ovalue->it_interval = ns_to_timeval(ointerval);
1775cee9645SThomas Gleixner 	}
1785cee9645SThomas Gleixner }
1795cee9645SThomas Gleixner 
1805cee9645SThomas Gleixner /*
1815cee9645SThomas Gleixner  * Returns true if the timeval is in canonical form
1825cee9645SThomas Gleixner  */
1835cee9645SThomas Gleixner #define timeval_valid(t) \
1845cee9645SThomas Gleixner 	(((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
1855cee9645SThomas Gleixner 
1865cee9645SThomas Gleixner int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
1875cee9645SThomas Gleixner {
1885cee9645SThomas Gleixner 	struct task_struct *tsk = current;
1895cee9645SThomas Gleixner 	struct hrtimer *timer;
1905cee9645SThomas Gleixner 	ktime_t expires;
1915cee9645SThomas Gleixner 
1925cee9645SThomas Gleixner 	/*
1935cee9645SThomas Gleixner 	 * Validate the timevals in value.
1945cee9645SThomas Gleixner 	 */
1955cee9645SThomas Gleixner 	if (!timeval_valid(&value->it_value) ||
1965cee9645SThomas Gleixner 	    !timeval_valid(&value->it_interval))
1975cee9645SThomas Gleixner 		return -EINVAL;
1985cee9645SThomas Gleixner 
1995cee9645SThomas Gleixner 	switch (which) {
2005cee9645SThomas Gleixner 	case ITIMER_REAL:
2015cee9645SThomas Gleixner again:
2025cee9645SThomas Gleixner 		spin_lock_irq(&tsk->sighand->siglock);
2035cee9645SThomas Gleixner 		timer = &tsk->signal->real_timer;
2045cee9645SThomas Gleixner 		if (ovalue) {
2055cee9645SThomas Gleixner 			ovalue->it_value = itimer_get_remtime(timer);
2065cee9645SThomas Gleixner 			ovalue->it_interval
2075cee9645SThomas Gleixner 				= ktime_to_timeval(tsk->signal->it_real_incr);
2085cee9645SThomas Gleixner 		}
2095cee9645SThomas Gleixner 		/* We are sharing ->siglock with it_real_fn() */
2105cee9645SThomas Gleixner 		if (hrtimer_try_to_cancel(timer) < 0) {
2115cee9645SThomas Gleixner 			spin_unlock_irq(&tsk->sighand->siglock);
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 }
311*54ad9c46SAl Viro 
312*54ad9c46SAl Viro #ifdef CONFIG_COMPAT
313*54ad9c46SAl Viro COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
314*54ad9c46SAl Viro 		       struct compat_itimerval __user *, in,
315*54ad9c46SAl Viro 		       struct compat_itimerval __user *, out)
316*54ad9c46SAl Viro {
317*54ad9c46SAl Viro 	struct itimerval kin, kout;
318*54ad9c46SAl Viro 	int error;
319*54ad9c46SAl Viro 
320*54ad9c46SAl Viro 	if (in) {
321*54ad9c46SAl Viro 		if (get_compat_itimerval(&kin, in))
322*54ad9c46SAl Viro 			return -EFAULT;
323*54ad9c46SAl Viro 	} else {
324*54ad9c46SAl Viro 		memset(&kin, 0, sizeof(kin));
325*54ad9c46SAl Viro 	}
326*54ad9c46SAl Viro 
327*54ad9c46SAl Viro 	error = do_setitimer(which, &kin, out ? &kout : NULL);
328*54ad9c46SAl Viro 	if (error || !out)
329*54ad9c46SAl Viro 		return error;
330*54ad9c46SAl Viro 	if (put_compat_itimerval(out, &kout))
331*54ad9c46SAl Viro 		return -EFAULT;
332*54ad9c46SAl Viro 	return 0;
333*54ad9c46SAl Viro }
334*54ad9c46SAl Viro #endif
335