itimer.c 8.99 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 1992 Darren Senn
 */

/* These are all the functions necessary to implement itimers */

#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/syscalls.h>
#include <linux/time.h>
12
#include <linux/sched/signal.h>
13
#include <linux/sched/cputime.h>
Linus Torvalds's avatar
Linus Torvalds committed
14
#include <linux/posix-timers.h>
15
#include <linux/hrtimer.h>
16
#include <trace/events/timer.h>
17
#include <linux/compat.h>
Linus Torvalds's avatar
Linus Torvalds committed
18

19
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
20

21 22 23 24 25 26 27 28 29
/**
 * itimer_get_remtime - get remaining time for the timer
 *
 * @timer: the timer to read
 *
 * Returns the delta between the expiry time and now, which can be
 * less than zero or 1usec for an pending expired timer
 */
static struct timeval itimer_get_remtime(struct hrtimer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
30
{
31
	ktime_t rem = __hrtimer_get_remaining(timer, true);
Linus Torvalds's avatar
Linus Torvalds committed
32

33 34 35 36 37 38
	/*
	 * Racy but safe: if the itimer expires after the above
	 * hrtimer_get_remtime() call but before this condition
	 * then we return 0 - which is correct.
	 */
	if (hrtimer_active(timer)) {
Thomas Gleixner's avatar
Thomas Gleixner committed
39 40
		if (rem <= 0)
			rem = NSEC_PER_USEC;
41
	} else
Thomas Gleixner's avatar
Thomas Gleixner committed
42
		rem = 0;
43 44

	return ktime_to_timeval(rem);
Linus Torvalds's avatar
Linus Torvalds committed
45 46
}

47
static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
48
			   struct itimerval *const value)
49
{
50
	u64 val, interval;
51 52 53 54
	struct cpu_itimer *it = &tsk->signal->it[clock_id];

	spin_lock_irq(&tsk->sighand->siglock);

55 56 57
	val = it->expires;
	interval = it->incr;
	if (val) {
58
		u64 t, samples[CPUCLOCK_MAX];
59

60 61
		thread_group_sample_cputime(tsk, samples);
		t = samples[clock_id];
62

63
		if (val < t)
64
			/* about to fire */
65
			val = TICK_NSEC;
66
		else
67
			val -= t;
68 69 70 71
	}

	spin_unlock_irq(&tsk->sighand->siglock);

72 73
	value->it_value = ns_to_timeval(val);
	value->it_interval = ns_to_timeval(interval);
74 75
}

76
static int do_getitimer(int which, struct itimerval *value)
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81
{
	struct task_struct *tsk = current;

	switch (which) {
	case ITIMER_REAL:
82
		spin_lock_irq(&tsk->sighand->siglock);
83 84 85
		value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
		value->it_interval =
			ktime_to_timeval(tsk->signal->it_real_incr);
86
		spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
87 88
		break;
	case ITIMER_VIRTUAL:
89
		get_cpu_itimer(tsk, CPUCLOCK_VIRT, value);
Linus Torvalds's avatar
Linus Torvalds committed
90 91
		break;
	case ITIMER_PROF:
92
		get_cpu_itimer(tsk, CPUCLOCK_PROF, value);
Linus Torvalds's avatar
Linus Torvalds committed
93 94 95 96 97 98 99
		break;
	default:
		return(-EINVAL);
	}
	return 0;
}

100
SYSCALL_DEFINE2(getitimer, int, which, struct itimerval __user *, value)
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104 105 106 107 108 109 110 111 112 113
{
	int error = -EFAULT;
	struct itimerval get_buffer;

	if (value) {
		error = do_getitimer(which, &get_buffer);
		if (!error &&
		    copy_to_user(value, &get_buffer, sizeof(get_buffer)))
			error = -EFAULT;
	}
	return error;
}

114
#if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
struct old_itimerval32 {
	struct old_timeval32	it_interval;
	struct old_timeval32	it_value;
};

static int put_old_itimerval32(struct old_itimerval32 __user *o, const struct itimerval *i)
{
	struct old_itimerval32 v32;

	v32.it_interval.tv_sec = i->it_interval.tv_sec;
	v32.it_interval.tv_usec = i->it_interval.tv_usec;
	v32.it_value.tv_sec = i->it_value.tv_sec;
	v32.it_value.tv_usec = i->it_value.tv_usec;
	return copy_to_user(o, &v32, sizeof(struct old_itimerval32)) ? -EFAULT : 0;
}

131
COMPAT_SYSCALL_DEFINE2(getitimer, int, which,
132
		       struct old_itimerval32 __user *, it)
133 134 135 136
{
	struct itimerval kit;
	int error = do_getitimer(which, &kit);

137
	if (!error && put_old_itimerval32(it, &kit))
138 139 140 141 142
		error = -EFAULT;
	return error;
}
#endif

143 144 145
/*
 * The timer is automagically restarted, when interval != 0
 */
146
enum hrtimer_restart it_real_fn(struct hrtimer *timer)
Linus Torvalds's avatar
Linus Torvalds committed
147
{
148
	struct signal_struct *sig =
149
		container_of(timer, struct signal_struct, real_timer);
150
	struct pid *leader_pid = sig->pids[PIDTYPE_TGID];
Linus Torvalds's avatar
Linus Torvalds committed
151

152 153
	trace_itimer_expire(ITIMER_REAL, leader_pid, 0);
	kill_pid_info(SIGALRM, SEND_SIG_PRIV, leader_pid);
154 155

	return HRTIMER_NORESTART;
Linus Torvalds's avatar
Linus Torvalds committed
156 157
}

158
static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
159 160
			   const struct itimerval *const value,
			   struct itimerval *const ovalue)
161
{
162
	u64 oval, nval, ointerval, ninterval;
163 164
	struct cpu_itimer *it = &tsk->signal->it[clock_id];

165 166 167 168 169 170
	/*
	 * Use the to_ktime conversion because that clamps the maximum
	 * value to KTIME_MAX and avoid multiplication overflows.
	 */
	nval = ktime_to_ns(timeval_to_ktime(value->it_value));
	ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
171 172 173

	spin_lock_irq(&tsk->sighand->siglock);

174 175 176
	oval = it->expires;
	ointerval = it->incr;
	if (oval || nval) {
177
		if (nval > 0)
178 179
			nval += TICK_NSEC;
		set_process_cpu_timer(tsk, clock_id, &nval, &oval);
180 181 182
	}
	it->expires = nval;
	it->incr = ninterval;
183 184
	trace_itimer_state(clock_id == CPUCLOCK_VIRT ?
			   ITIMER_VIRTUAL : ITIMER_PROF, value, nval);
185 186 187 188

	spin_unlock_irq(&tsk->sighand->siglock);

	if (ovalue) {
189 190
		ovalue->it_value = ns_to_timeval(oval);
		ovalue->it_interval = ns_to_timeval(ointerval);
191 192 193
	}
}

194 195 196 197 198 199
/*
 * Returns true if the timeval is in canonical form
 */
#define timeval_valid(t) \
	(((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))

200
static int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
Linus Torvalds's avatar
Linus Torvalds committed
201 202
{
	struct task_struct *tsk = current;
203 204
	struct hrtimer *timer;
	ktime_t expires;
Linus Torvalds's avatar
Linus Torvalds committed
205

206 207 208
	/*
	 * Validate the timevals in value.
	 */
209 210 211
	if (!timeval_valid(&value->it_value) ||
	    !timeval_valid(&value->it_interval))
		return -EINVAL;
212

Linus Torvalds's avatar
Linus Torvalds committed
213 214
	switch (which) {
	case ITIMER_REAL:
215 216
again:
		spin_lock_irq(&tsk->sighand->siglock);
217
		timer = &tsk->signal->real_timer;
Linus Torvalds's avatar
Linus Torvalds committed
218
		if (ovalue) {
219 220 221
			ovalue->it_value = itimer_get_remtime(timer);
			ovalue->it_interval
				= ktime_to_timeval(tsk->signal->it_real_incr);
Linus Torvalds's avatar
Linus Torvalds committed
222
		}
223 224 225
		/* We are sharing ->siglock with it_real_fn() */
		if (hrtimer_try_to_cancel(timer) < 0) {
			spin_unlock_irq(&tsk->sighand->siglock);
226
			hrtimer_cancel_wait_running(timer);
227 228
			goto again;
		}
229
		expires = timeval_to_ktime(value->it_value);
Thomas Gleixner's avatar
Thomas Gleixner committed
230
		if (expires != 0) {
231 232
			tsk->signal->it_real_incr =
				timeval_to_ktime(value->it_interval);
233
			hrtimer_start(timer, expires, HRTIMER_MODE_REL);
234
		} else
Thomas Gleixner's avatar
Thomas Gleixner committed
235
			tsk->signal->it_real_incr = 0;
236

237
		trace_itimer_state(ITIMER_REAL, value, 0);
238
		spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
239 240
		break;
	case ITIMER_VIRTUAL:
241
		set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue);
Linus Torvalds's avatar
Linus Torvalds committed
242 243
		break;
	case ITIMER_PROF:
244
		set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue);
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

252 253 254 255 256 257 258 259 260 261 262
#ifdef CONFIG_SECURITY_SELINUX
void clear_itimer(void)
{
	struct itimerval v = {};
	int i;

	for (i = 0; i < 3; i++)
		do_setitimer(i, &v, NULL);
}
#endif

263 264
#ifdef __ARCH_WANT_SYS_ALARM

265 266 267 268 269 270 271 272 273 274 275 276
/**
 * alarm_setitimer - set alarm in seconds
 *
 * @seconds:	number of seconds until alarm
 *		0 disables the alarm
 *
 * Returns the remaining time in seconds of a pending timer or 0 when
 * the timer is not active.
 *
 * On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
 * negative timeval settings which would cause immediate expiry.
 */
277
static unsigned int alarm_setitimer(unsigned int seconds)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
	struct itimerval it_new, it_old;

#if BITS_PER_LONG < 64
	if (seconds > INT_MAX)
		seconds = INT_MAX;
#endif
	it_new.it_value.tv_sec = seconds;
	it_new.it_value.tv_usec = 0;
	it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;

	do_setitimer(ITIMER_REAL, &it_new, &it_old);

	/*
	 * We can't return 0 if we have an alarm pending ...  And we'd
	 * better return too much than too little anyway
	 */
	if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
	      it_old.it_value.tv_usec >= 500000)
		it_old.it_value.tv_sec++;

	return it_old.it_value.tv_sec;
}

302 303 304 305 306 307 308 309 310 311 312
/*
 * For backwards compatibility?  This can be done in libc so Alpha
 * and all newer ports shouldn't need it.
 */
SYSCALL_DEFINE1(alarm, unsigned int, seconds)
{
	return alarm_setitimer(seconds);
}

#endif

313 314
SYSCALL_DEFINE3(setitimer, int, which, struct itimerval __user *, value,
		struct itimerval __user *, ovalue)
Linus Torvalds's avatar
Linus Torvalds committed
315 316 317 318 319 320 321
{
	struct itimerval set_buffer, get_buffer;
	int error;

	if (value) {
		if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
			return -EFAULT;
322
	} else {
323 324 325 326
		memset(&set_buffer, 0, sizeof(set_buffer));
		printk_once(KERN_WARNING "%s calls setitimer() with new_value NULL pointer."
			    " Misfeature support will be removed\n",
			    current->comm);
327
	}
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330 331 332 333

	error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
	if (error || !ovalue)
		return error;

	if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
334
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
335 336
	return 0;
}
337

338
#if defined(CONFIG_COMPAT) || defined(CONFIG_ALPHA)
339 340 341 342 343 344 345 346 347 348 349 350 351
static int get_old_itimerval32(struct itimerval *o, const struct old_itimerval32 __user *i)
{
	struct old_itimerval32 v32;

	if (copy_from_user(&v32, i, sizeof(struct old_itimerval32)))
		return -EFAULT;
	o->it_interval.tv_sec = v32.it_interval.tv_sec;
	o->it_interval.tv_usec = v32.it_interval.tv_usec;
	o->it_value.tv_sec = v32.it_value.tv_sec;
	o->it_value.tv_usec = v32.it_value.tv_usec;
	return 0;
}

352
COMPAT_SYSCALL_DEFINE3(setitimer, int, which,
353 354
		       struct old_itimerval32 __user *, in,
		       struct old_itimerval32 __user *, out)
355 356 357 358 359
{
	struct itimerval kin, kout;
	int error;

	if (in) {
360
		if (get_old_itimerval32(&kin, in))
361 362 363 364 365 366 367 368
			return -EFAULT;
	} else {
		memset(&kin, 0, sizeof(kin));
	}

	error = do_setitimer(which, &kin, out ? &kout : NULL);
	if (error || !out)
		return error;
369
	if (put_old_itimerval32(out, &kout))
370 371 372 373
		return -EFAULT;
	return 0;
}
#endif