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