xref: /openbmc/linux/kernel/time/itimer.c (revision b2441318)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25cee9645SThomas Gleixner /*
35cee9645SThomas Gleixner  * linux/kernel/itimer.c
45cee9645SThomas Gleixner  *
55cee9645SThomas Gleixner  * Copyright (C) 1992 Darren Senn
65cee9645SThomas Gleixner  */
75cee9645SThomas Gleixner 
85cee9645SThomas Gleixner /* These are all the functions necessary to implement itimers */
95cee9645SThomas Gleixner 
105cee9645SThomas Gleixner #include <linux/mm.h>
115cee9645SThomas Gleixner #include <linux/interrupt.h>
125cee9645SThomas Gleixner #include <linux/syscalls.h>
135cee9645SThomas Gleixner #include <linux/time.h>
143f07c014SIngo Molnar #include <linux/sched/signal.h>
1532ef5517SIngo Molnar #include <linux/sched/cputime.h>
165cee9645SThomas Gleixner #include <linux/posix-timers.h>
175cee9645SThomas Gleixner #include <linux/hrtimer.h>
185cee9645SThomas Gleixner #include <trace/events/timer.h>
1954ad9c46SAl Viro #include <linux/compat.h>
205cee9645SThomas Gleixner 
217c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
225cee9645SThomas Gleixner 
235cee9645SThomas Gleixner /**
245cee9645SThomas Gleixner  * itimer_get_remtime - get remaining time for the timer
255cee9645SThomas Gleixner  *
265cee9645SThomas Gleixner  * @timer: the timer to read
275cee9645SThomas Gleixner  *
285cee9645SThomas Gleixner  * Returns the delta between the expiry time and now, which can be
295cee9645SThomas Gleixner  * less than zero or 1usec for an pending expired timer
305cee9645SThomas Gleixner  */
315cee9645SThomas Gleixner static struct timeval itimer_get_remtime(struct hrtimer *timer)
325cee9645SThomas Gleixner {
3351cbb524SThomas Gleixner 	ktime_t rem = __hrtimer_get_remaining(timer, true);
345cee9645SThomas Gleixner 
355cee9645SThomas Gleixner 	/*
365cee9645SThomas Gleixner 	 * Racy but safe: if the itimer expires after the above
375cee9645SThomas Gleixner 	 * hrtimer_get_remtime() call but before this condition
385cee9645SThomas Gleixner 	 * then we return 0 - which is correct.
395cee9645SThomas Gleixner 	 */
405cee9645SThomas Gleixner 	if (hrtimer_active(timer)) {
412456e855SThomas Gleixner 		if (rem <= 0)
422456e855SThomas Gleixner 			rem = NSEC_PER_USEC;
435cee9645SThomas Gleixner 	} else
442456e855SThomas Gleixner 		rem = 0;
455cee9645SThomas Gleixner 
465cee9645SThomas Gleixner 	return ktime_to_timeval(rem);
475cee9645SThomas Gleixner }
485cee9645SThomas Gleixner 
495cee9645SThomas Gleixner static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
505cee9645SThomas Gleixner 			   struct itimerval *const value)
515cee9645SThomas Gleixner {
52858cf3a8SFrederic Weisbecker 	u64 val, interval;
535cee9645SThomas Gleixner 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
545cee9645SThomas Gleixner 
555cee9645SThomas Gleixner 	spin_lock_irq(&tsk->sighand->siglock);
565cee9645SThomas Gleixner 
57858cf3a8SFrederic Weisbecker 	val = it->expires;
58858cf3a8SFrederic Weisbecker 	interval = it->incr;
59858cf3a8SFrederic Weisbecker 	if (val) {
60ebd7e7fcSFrederic Weisbecker 		struct task_cputime cputime;
61858cf3a8SFrederic Weisbecker 		u64 t;
625cee9645SThomas Gleixner 
635cee9645SThomas Gleixner 		thread_group_cputimer(tsk, &cputime);
645cee9645SThomas Gleixner 		if (clock_id == CPUCLOCK_PROF)
65858cf3a8SFrederic Weisbecker 			t = cputime.utime + cputime.stime;
665cee9645SThomas Gleixner 		else
675cee9645SThomas Gleixner 			/* CPUCLOCK_VIRT */
68858cf3a8SFrederic Weisbecker 			t = cputime.utime;
695cee9645SThomas Gleixner 
70858cf3a8SFrederic Weisbecker 		if (val < t)
715cee9645SThomas Gleixner 			/* about to fire */
72858cf3a8SFrederic Weisbecker 			val = TICK_NSEC;
735cee9645SThomas Gleixner 		else
74858cf3a8SFrederic Weisbecker 			val -= t;
755cee9645SThomas Gleixner 	}
765cee9645SThomas Gleixner 
775cee9645SThomas Gleixner 	spin_unlock_irq(&tsk->sighand->siglock);
785cee9645SThomas Gleixner 
79858cf3a8SFrederic Weisbecker 	value->it_value = ns_to_timeval(val);
80858cf3a8SFrederic Weisbecker 	value->it_interval = ns_to_timeval(interval);
815cee9645SThomas Gleixner }
825cee9645SThomas Gleixner 
835cee9645SThomas Gleixner int do_getitimer(int which, struct itimerval *value)
845cee9645SThomas Gleixner {
855cee9645SThomas Gleixner 	struct task_struct *tsk = current;
865cee9645SThomas Gleixner 
875cee9645SThomas Gleixner 	switch (which) {
885cee9645SThomas Gleixner 	case ITIMER_REAL:
895cee9645SThomas Gleixner 		spin_lock_irq(&tsk->sighand->siglock);
905cee9645SThomas Gleixner 		value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
915cee9645SThomas Gleixner 		value->it_interval =
925cee9645SThomas Gleixner 			ktime_to_timeval(tsk->signal->it_real_incr);
935cee9645SThomas Gleixner 		spin_unlock_irq(&tsk->sighand->siglock);
945cee9645SThomas Gleixner 		break;
955cee9645SThomas Gleixner 	case ITIMER_VIRTUAL:
965cee9645SThomas Gleixner 		get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
975cee9645SThomas Gleixner 		break;
985cee9645SThomas Gleixner 	case ITIMER_PROF:
995cee9645SThomas Gleixner 		get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
1005cee9645SThomas Gleixner 		break;
1015cee9645SThomas Gleixner 	default:
1025cee9645SThomas Gleixner 		return(-EINVAL);
1035cee9645SThomas Gleixner 	}
1045cee9645SThomas Gleixner 	return 0;
1055cee9645SThomas Gleixner }
1065cee9645SThomas Gleixner 
1075cee9645SThomas Gleixner SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
1085cee9645SThomas Gleixner {
1095cee9645SThomas Gleixner 	int error = -EFAULT;
1105cee9645SThomas Gleixner 	struct itimerval get_buffer;
1115cee9645SThomas Gleixner 
1125cee9645SThomas Gleixner 	if (value) {
1135cee9645SThomas Gleixner 		error = do_getitimer(which, &get_buffer);
1145cee9645SThomas Gleixner 		if (!error &&
1155cee9645SThomas Gleixner 		    copy_to_user(value, &get_buffer, sizeof(get_buffer)))
1165cee9645SThomas Gleixner 			error = -EFAULT;
1175cee9645SThomas Gleixner 	}
1185cee9645SThomas Gleixner 	return error;
1195cee9645SThomas Gleixner }
1205cee9645SThomas Gleixner 
12154ad9c46SAl Viro #ifdef CONFIG_COMPAT
12254ad9c46SAl Viro COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
12354ad9c46SAl Viro 		       struct compat_itimerval __user *, it)
12454ad9c46SAl Viro {
12554ad9c46SAl Viro 	struct itimerval kit;
12654ad9c46SAl Viro 	int error = do_getitimer(which, &kit);
12754ad9c46SAl Viro 
12854ad9c46SAl Viro 	if (!error && put_compat_itimerval(it, &kit))
12954ad9c46SAl Viro 		error = -EFAULT;
13054ad9c46SAl Viro 	return error;
13154ad9c46SAl Viro }
13254ad9c46SAl Viro #endif
13354ad9c46SAl Viro 
1345cee9645SThomas Gleixner 
1355cee9645SThomas Gleixner /*
1365cee9645SThomas Gleixner  * The timer is automagically restarted, when interval != 0
1375cee9645SThomas Gleixner  */
1385cee9645SThomas Gleixner enum hrtimer_restart it_real_fn(struct hrtimer *timer)
1395cee9645SThomas Gleixner {
1405cee9645SThomas Gleixner 	struct signal_struct *sig =
1415cee9645SThomas Gleixner 		container_of(timer, struct signal_struct, real_timer);
1425cee9645SThomas Gleixner 
1435cee9645SThomas Gleixner 	trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0);
1445cee9645SThomas Gleixner 	kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
1455cee9645SThomas Gleixner 
1465cee9645SThomas Gleixner 	return HRTIMER_NORESTART;
1475cee9645SThomas Gleixner }
1485cee9645SThomas Gleixner 
1495cee9645SThomas Gleixner static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
1505cee9645SThomas Gleixner 			   const struct itimerval *const value,
1515cee9645SThomas Gleixner 			   struct itimerval *const ovalue)
1525cee9645SThomas Gleixner {
153858cf3a8SFrederic Weisbecker 	u64 oval, nval, ointerval, ninterval;
1545cee9645SThomas Gleixner 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
1555cee9645SThomas Gleixner 
15635eb7258SThomas Gleixner 	/*
15735eb7258SThomas Gleixner 	 * Use the to_ktime conversion because that clamps the maximum
15835eb7258SThomas Gleixner 	 * value to KTIME_MAX and avoid multiplication overflows.
15935eb7258SThomas Gleixner 	 */
16035eb7258SThomas Gleixner 	nval = ktime_to_ns(timeval_to_ktime(value->it_value));
16135eb7258SThomas Gleixner 	ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
1625cee9645SThomas Gleixner 
1635cee9645SThomas Gleixner 	spin_lock_irq(&tsk->sighand->siglock);
1645cee9645SThomas Gleixner 
165858cf3a8SFrederic Weisbecker 	oval = it->expires;
166858cf3a8SFrederic Weisbecker 	ointerval = it->incr;
167858cf3a8SFrederic Weisbecker 	if (oval || nval) {
1685cee9645SThomas Gleixner 		if (nval > 0)
169858cf3a8SFrederic Weisbecker 			nval += TICK_NSEC;
170858cf3a8SFrederic Weisbecker 		set_process_cpu_timer(tsk, clock_id, &nval, &oval);
1715cee9645SThomas Gleixner 	}
1725cee9645SThomas Gleixner 	it->expires = nval;
1735cee9645SThomas Gleixner 	it->incr = ninterval;
1745cee9645SThomas Gleixner 	trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
1755cee9645SThomas Gleixner 			   ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
1765cee9645SThomas Gleixner 
1775cee9645SThomas Gleixner 	spin_unlock_irq(&tsk->sighand->siglock);
1785cee9645SThomas Gleixner 
1795cee9645SThomas Gleixner 	if (ovalue) {
180858cf3a8SFrederic Weisbecker 		ovalue->it_value = ns_to_timeval(oval);
181858cf3a8SFrederic Weisbecker 		ovalue->it_interval = ns_to_timeval(ointerval);
1825cee9645SThomas Gleixner 	}
1835cee9645SThomas Gleixner }
1845cee9645SThomas Gleixner 
1855cee9645SThomas Gleixner /*
1865cee9645SThomas Gleixner  * Returns true if the timeval is in canonical form
1875cee9645SThomas Gleixner  */
1885cee9645SThomas Gleixner #define timeval_valid(t) \
1895cee9645SThomas Gleixner 	(((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
1905cee9645SThomas Gleixner 
1915cee9645SThomas Gleixner int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
1925cee9645SThomas Gleixner {
1935cee9645SThomas Gleixner 	struct task_struct *tsk = current;
1945cee9645SThomas Gleixner 	struct hrtimer *timer;
1955cee9645SThomas Gleixner 	ktime_t expires;
1965cee9645SThomas Gleixner 
1975cee9645SThomas Gleixner 	/*
1985cee9645SThomas Gleixner 	 * Validate the timevals in value.
1995cee9645SThomas Gleixner 	 */
2005cee9645SThomas Gleixner 	if (!timeval_valid(&value->it_value) ||
2015cee9645SThomas Gleixner 	    !timeval_valid(&value->it_interval))
2025cee9645SThomas Gleixner 		return -EINVAL;
2035cee9645SThomas Gleixner 
2045cee9645SThomas Gleixner 	switch (which) {
2055cee9645SThomas Gleixner 	case ITIMER_REAL:
2065cee9645SThomas Gleixner again:
2075cee9645SThomas Gleixner 		spin_lock_irq(&tsk->sighand->siglock);
2085cee9645SThomas Gleixner 		timer = &tsk->signal->real_timer;
2095cee9645SThomas Gleixner 		if (ovalue) {
2105cee9645SThomas Gleixner 			ovalue->it_value = itimer_get_remtime(timer);
2115cee9645SThomas Gleixner 			ovalue->it_interval
2125cee9645SThomas Gleixner 				= ktime_to_timeval(tsk->signal->it_real_incr);
2135cee9645SThomas Gleixner 		}
2145cee9645SThomas Gleixner 		/* We are sharing ->siglock with it_real_fn() */
2155cee9645SThomas Gleixner 		if (hrtimer_try_to_cancel(timer) < 0) {
2165cee9645SThomas Gleixner 			spin_unlock_irq(&tsk->sighand->siglock);
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