timerfd.c 13.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 *  fs/timerfd.c
 *
 *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
 *
 *
 *  Thanks to Thomas Gleixner for code reviews and useful comments.
 *
 */

T
Todd Poynor 已提交
11
#include <linux/alarmtimer.h>
12 13 14 15 16 17
#include <linux/file.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/kernel.h>
18
#include <linux/slab.h>
19 20 21 22 23 24
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/time.h>
#include <linux/hrtimer.h>
#include <linux/anon_inodes.h>
#include <linux/timerfd.h>
25
#include <linux/syscalls.h>
26
#include <linux/compat.h>
27
#include <linux/rcupdate.h>
28 29

struct timerfd_ctx {
T
Todd Poynor 已提交
30 31 32 33
	union {
		struct hrtimer tmr;
		struct alarm alarm;
	} t;
34
	ktime_t tintv;
35
	ktime_t moffs;
36
	wait_queue_head_t wqh;
D
Davide Libenzi 已提交
37 38
	u64 ticks;
	int clockid;
39 40
	short unsigned expired;
	short unsigned settime_flags;	/* to show in fdinfo */
41 42
	struct rcu_head rcu;
	struct list_head clist;
43
	bool might_cancel;
44 45
};

46 47 48
static LIST_HEAD(cancel_list);
static DEFINE_SPINLOCK(cancel_lock);

T
Todd Poynor 已提交
49 50 51 52 53 54
static inline bool isalarm(struct timerfd_ctx *ctx)
{
	return ctx->clockid == CLOCK_REALTIME_ALARM ||
		ctx->clockid == CLOCK_BOOTTIME_ALARM;
}

55 56 57
/*
 * This gets called when the timer event triggers. We set the "expired"
 * flag, but we do not re-arm the timer (in case it's necessary,
D
Davide Libenzi 已提交
58
 * tintv.tv64 != 0) until the timer is accessed.
59
 */
T
Todd Poynor 已提交
60
static void timerfd_triggered(struct timerfd_ctx *ctx)
61 62 63
{
	unsigned long flags;

D
Davide Libenzi 已提交
64
	spin_lock_irqsave(&ctx->wqh.lock, flags);
65
	ctx->expired = 1;
D
Davide Libenzi 已提交
66
	ctx->ticks++;
67
	wake_up_locked(&ctx->wqh);
D
Davide Libenzi 已提交
68
	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
T
Todd Poynor 已提交
69
}
70

T
Todd Poynor 已提交
71 72 73 74 75
static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
{
	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
					       t.tmr);
	timerfd_triggered(ctx);
76 77 78
	return HRTIMER_NORESTART;
}

T
Todd Poynor 已提交
79 80 81 82 83 84 85 86 87
static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
	ktime_t now)
{
	struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
					       t.alarm);
	timerfd_triggered(ctx);
	return ALARMTIMER_NORESTART;
}

88 89
/*
 * Called when the clock was set to cancel the timers in the cancel
90 91 92
 * list. This will wake up processes waiting on these timers. The
 * wake-up requires ctx->ticks to be non zero, therefore we increment
 * it before calling wake_up_locked().
93 94
 */
void timerfd_clock_was_set(void)
D
Davide Libenzi 已提交
95
{
96
	ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
97 98
	struct timerfd_ctx *ctx;
	unsigned long flags;
D
Davide Libenzi 已提交
99

100 101 102 103 104 105 106
	rcu_read_lock();
	list_for_each_entry_rcu(ctx, &cancel_list, clist) {
		if (!ctx->might_cancel)
			continue;
		spin_lock_irqsave(&ctx->wqh.lock, flags);
		if (ctx->moffs.tv64 != moffs.tv64) {
			ctx->moffs.tv64 = KTIME_MAX;
107
			ctx->ticks++;
108 109 110 111 112
			wake_up_locked(&ctx->wqh);
		}
		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
	}
	rcu_read_unlock();
D
Davide Libenzi 已提交
113 114
}

115
static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
116
{
117 118 119 120 121 122 123
	if (ctx->might_cancel) {
		ctx->might_cancel = false;
		spin_lock(&cancel_lock);
		list_del_rcu(&ctx->clist);
		spin_unlock(&cancel_lock);
	}
}
124

125 126 127
static bool timerfd_canceled(struct timerfd_ctx *ctx)
{
	if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
128
		return false;
129
	ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
130 131
	return true;
}
132

133 134
static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
{
T
Todd Poynor 已提交
135 136 137
	if ((ctx->clockid == CLOCK_REALTIME ||
	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
138 139 140 141 142 143 144 145 146 147
		if (!ctx->might_cancel) {
			ctx->might_cancel = true;
			spin_lock(&cancel_lock);
			list_add_rcu(&ctx->clist, &cancel_list);
			spin_unlock(&cancel_lock);
		}
	} else if (ctx->might_cancel) {
		timerfd_remove_cancel(ctx);
	}
}
148

149 150 151
static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
{
	ktime_t remaining;
152

T
Todd Poynor 已提交
153 154 155 156 157
	if (isalarm(ctx))
		remaining = alarm_expires_remaining(&ctx->t.alarm);
	else
		remaining = hrtimer_expires_remaining(&ctx->t.tmr);

158
	return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
159 160 161 162
}

static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
			 const struct itimerspec *ktmr)
163 164 165
{
	enum hrtimer_mode htmode;
	ktime_t texp;
166
	int clockid = ctx->clockid;
167 168 169 170 171 172

	htmode = (flags & TFD_TIMER_ABSTIME) ?
		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;

	texp = timespec_to_ktime(ktmr->it_value);
	ctx->expired = 0;
D
Davide Libenzi 已提交
173
	ctx->ticks = 0;
174
	ctx->tintv = timespec_to_ktime(ktmr->it_interval);
T
Todd Poynor 已提交
175 176 177 178 179 180 181 182 183 184 185 186

	if (isalarm(ctx)) {
		alarm_init(&ctx->t.alarm,
			   ctx->clockid == CLOCK_REALTIME_ALARM ?
			   ALARM_REALTIME : ALARM_BOOTTIME,
			   timerfd_alarmproc);
	} else {
		hrtimer_init(&ctx->t.tmr, clockid, htmode);
		hrtimer_set_expires(&ctx->t.tmr, texp);
		ctx->t.tmr.function = timerfd_tmrproc;
	}

187
	if (texp.tv64 != 0) {
T
Todd Poynor 已提交
188 189 190 191 192 193 194 195 196
		if (isalarm(ctx)) {
			if (flags & TFD_TIMER_ABSTIME)
				alarm_start(&ctx->t.alarm, texp);
			else
				alarm_start_relative(&ctx->t.alarm, texp);
		} else {
			hrtimer_start(&ctx->t.tmr, texp, htmode);
		}

197 198 199
		if (timerfd_canceled(ctx))
			return -ECANCELED;
	}
200 201

	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
202
	return 0;
203 204 205 206 207 208
}

static int timerfd_release(struct inode *inode, struct file *file)
{
	struct timerfd_ctx *ctx = file->private_data;

209
	timerfd_remove_cancel(ctx);
T
Todd Poynor 已提交
210 211 212 213 214

	if (isalarm(ctx))
		alarm_cancel(&ctx->t.alarm);
	else
		hrtimer_cancel(&ctx->t.tmr);
215
	kfree_rcu(ctx, rcu);
216 217 218 219 220 221 222 223 224 225 226
	return 0;
}

static unsigned int timerfd_poll(struct file *file, poll_table *wait)
{
	struct timerfd_ctx *ctx = file->private_data;
	unsigned int events = 0;
	unsigned long flags;

	poll_wait(file, &ctx->wqh, wait);

D
Davide Libenzi 已提交
227
	spin_lock_irqsave(&ctx->wqh.lock, flags);
D
Davide Libenzi 已提交
228
	if (ctx->ticks)
229
		events |= POLLIN;
D
Davide Libenzi 已提交
230
	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
231 232 233 234 235 236 237 238 239

	return events;
}

static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
			    loff_t *ppos)
{
	struct timerfd_ctx *ctx = file->private_data;
	ssize_t res;
240
	u64 ticks = 0;
241 242 243

	if (count < sizeof(ticks))
		return -EINVAL;
D
Davide Libenzi 已提交
244
	spin_lock_irq(&ctx->wqh.lock);
245 246 247 248
	if (file->f_flags & O_NONBLOCK)
		res = -EAGAIN;
	else
		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
249

250 251 252 253 254 255 256 257 258 259 260
	/*
	 * If clock has changed, we do not care about the
	 * ticks and we do not rearm the timer. Userspace must
	 * reevaluate anyway.
	 */
	if (timerfd_canceled(ctx)) {
		ctx->ticks = 0;
		ctx->expired = 0;
		res = -ECANCELED;
	}

D
Davide Libenzi 已提交
261 262
	if (ctx->ticks) {
		ticks = ctx->ticks;
263

D
Davide Libenzi 已提交
264
		if (ctx->expired && ctx->tintv.tv64) {
265 266 267 268 269 270
			/*
			 * If tintv.tv64 != 0, this is a periodic timer that
			 * needs to be re-armed. We avoid doing it in the timer
			 * callback to avoid DoS attacks specifying a very
			 * short timer period.
			 */
T
Todd Poynor 已提交
271 272 273 274 275 276 277 278 279
			if (isalarm(ctx)) {
				ticks += alarm_forward_now(
					&ctx->t.alarm, ctx->tintv) - 1;
				alarm_restart(&ctx->t.alarm);
			} else {
				ticks += hrtimer_forward_now(&ctx->t.tmr,
							     ctx->tintv) - 1;
				hrtimer_restart(&ctx->t.tmr);
			}
D
Davide Libenzi 已提交
280 281 282
		}
		ctx->expired = 0;
		ctx->ticks = 0;
283
	}
D
Davide Libenzi 已提交
284
	spin_unlock_irq(&ctx->wqh.lock);
285
	if (ticks)
286
		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
287 288 289
	return res;
}

290
#ifdef CONFIG_PROC_FS
291
static void timerfd_show(struct seq_file *m, struct file *file)
292 293 294 295 296 297 298 299 300
{
	struct timerfd_ctx *ctx = file->private_data;
	struct itimerspec t;

	spin_lock_irq(&ctx->wqh.lock);
	t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
	t.it_interval = ktime_to_timespec(ctx->tintv);
	spin_unlock_irq(&ctx->wqh.lock);

301 302 303 304 305 306 307 308 309 310 311 312 313
	seq_printf(m,
		   "clockid: %d\n"
		   "ticks: %llu\n"
		   "settime flags: 0%o\n"
		   "it_value: (%llu, %llu)\n"
		   "it_interval: (%llu, %llu)\n",
		   ctx->clockid,
		   (unsigned long long)ctx->ticks,
		   ctx->settime_flags,
		   (unsigned long long)t.it_value.tv_sec,
		   (unsigned long long)t.it_value.tv_nsec,
		   (unsigned long long)t.it_interval.tv_sec,
		   (unsigned long long)t.it_interval.tv_nsec);
314 315 316 317 318
}
#else
#define timerfd_show NULL
#endif

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#ifdef CONFIG_CHECKPOINT_RESTORE
static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct timerfd_ctx *ctx = file->private_data;
	int ret = 0;

	switch (cmd) {
	case TFD_IOC_SET_TICKS: {
		u64 ticks;

		if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
			return -EFAULT;
		if (!ticks)
			return -EINVAL;

		spin_lock_irq(&ctx->wqh.lock);
		if (!timerfd_canceled(ctx)) {
			ctx->ticks = ticks;
337
			wake_up_locked(&ctx->wqh);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		} else
			ret = -ECANCELED;
		spin_unlock_irq(&ctx->wqh.lock);
		break;
	}
	default:
		ret = -ENOTTY;
		break;
	}

	return ret;
}
#else
#define timerfd_ioctl NULL
#endif

354 355 356 357
static const struct file_operations timerfd_fops = {
	.release	= timerfd_release,
	.poll		= timerfd_poll,
	.read		= timerfd_read,
358
	.llseek		= noop_llseek,
359
	.show_fdinfo	= timerfd_show,
360
	.unlocked_ioctl	= timerfd_ioctl,
361 362
};

363
static int timerfd_fget(int fd, struct fd *p)
D
Davide Libenzi 已提交
364
{
365 366 367 368 369 370
	struct fd f = fdget(fd);
	if (!f.file)
		return -EBADF;
	if (f.file->f_op != &timerfd_fops) {
		fdput(f);
		return -EINVAL;
D
Davide Libenzi 已提交
371
	}
372 373
	*p = f;
	return 0;
D
Davide Libenzi 已提交
374 375
}

376
SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
377
{
A
Al Viro 已提交
378
	int ufd;
379 380
	struct timerfd_ctx *ctx;

381 382 383 384
	/* Check the TFD_* constants for consistency.  */
	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);

D
Davide Libenzi 已提交
385 386
	if ((flags & ~TFD_CREATE_FLAGS) ||
	    (clockid != CLOCK_MONOTONIC &&
T
Todd Poynor 已提交
387 388
	     clockid != CLOCK_REALTIME &&
	     clockid != CLOCK_REALTIME_ALARM &&
389
	     clockid != CLOCK_BOOTTIME &&
T
Todd Poynor 已提交
390
	     clockid != CLOCK_BOOTTIME_ALARM))
391
		return -EINVAL;
D
Davide Libenzi 已提交
392 393 394 395 396 397 398

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	init_waitqueue_head(&ctx->wqh);
	ctx->clockid = clockid;
T
Todd Poynor 已提交
399 400 401 402 403 404 405 406 407

	if (isalarm(ctx))
		alarm_init(&ctx->t.alarm,
			   ctx->clockid == CLOCK_REALTIME_ALARM ?
			   ALARM_REALTIME : ALARM_BOOTTIME,
			   timerfd_alarmproc);
	else
		hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);

408
	ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
D
Davide Libenzi 已提交
409

U
Ulrich Drepper 已提交
410
	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
411
			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
A
Al Viro 已提交
412
	if (ufd < 0)
D
Davide Libenzi 已提交
413 414 415 416 417
		kfree(ctx);

	return ufd;
}

418 419 420
static int do_timerfd_settime(int ufd, int flags, 
		const struct itimerspec *new,
		struct itimerspec *old)
D
Davide Libenzi 已提交
421
{
422
	struct fd f;
D
Davide Libenzi 已提交
423
	struct timerfd_ctx *ctx;
424
	int ret;
D
Davide Libenzi 已提交
425

D
Davide Libenzi 已提交
426
	if ((flags & ~TFD_SETTIME_FLAGS) ||
427 428
	    !timespec_valid(&new->it_value) ||
	    !timespec_valid(&new->it_interval))
429 430
		return -EINVAL;

431 432 433 434
	ret = timerfd_fget(ufd, &f);
	if (ret)
		return ret;
	ctx = f.file->private_data;
435

436 437
	timerfd_setup_cancel(ctx, flags);

D
Davide Libenzi 已提交
438 439 440 441 442 443
	/*
	 * We need to stop the existing timer before reprogramming
	 * it to the new values.
	 */
	for (;;) {
		spin_lock_irq(&ctx->wqh.lock);
T
Todd Poynor 已提交
444 445 446 447 448 449 450 451

		if (isalarm(ctx)) {
			if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
				break;
		} else {
			if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
				break;
		}
D
Davide Libenzi 已提交
452
		spin_unlock_irq(&ctx->wqh.lock);
D
Davide Libenzi 已提交
453
		cpu_relax();
454 455
	}

D
Davide Libenzi 已提交
456 457 458 459 460 461
	/*
	 * If the timer is expired and it's periodic, we need to advance it
	 * because the caller may want to know the previous expiration time.
	 * We do not update "ticks" and "expired" since the timer will be
	 * re-programmed again in the following timerfd_setup() call.
	 */
T
Todd Poynor 已提交
462 463 464 465 466 467
	if (ctx->expired && ctx->tintv.tv64) {
		if (isalarm(ctx))
			alarm_forward_now(&ctx->t.alarm, ctx->tintv);
		else
			hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
	}
468

469 470
	old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
	old->it_interval = ktime_to_timespec(ctx->tintv);
D
Davide Libenzi 已提交
471 472 473 474

	/*
	 * Re-program the timer to the new value ...
	 */
475
	ret = timerfd_setup(ctx, flags, new);
D
Davide Libenzi 已提交
476 477

	spin_unlock_irq(&ctx->wqh.lock);
478
	fdput(f);
479
	return ret;
D
Davide Libenzi 已提交
480 481
}

482
static int do_timerfd_gettime(int ufd, struct itimerspec *t)
D
Davide Libenzi 已提交
483
{
484
	struct fd f;
D
Davide Libenzi 已提交
485
	struct timerfd_ctx *ctx;
486 487 488 489
	int ret = timerfd_fget(ufd, &f);
	if (ret)
		return ret;
	ctx = f.file->private_data;
D
Davide Libenzi 已提交
490 491 492 493

	spin_lock_irq(&ctx->wqh.lock);
	if (ctx->expired && ctx->tintv.tv64) {
		ctx->expired = 0;
T
Todd Poynor 已提交
494 495 496 497 498 499 500 501 502 503 504 505

		if (isalarm(ctx)) {
			ctx->ticks +=
				alarm_forward_now(
					&ctx->t.alarm, ctx->tintv) - 1;
			alarm_restart(&ctx->t.alarm);
		} else {
			ctx->ticks +=
				hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
				- 1;
			hrtimer_restart(&ctx->t.tmr);
		}
D
Davide Libenzi 已提交
506
	}
507 508
	t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
	t->it_interval = ktime_to_timespec(ctx->tintv);
D
Davide Libenzi 已提交
509
	spin_unlock_irq(&ctx->wqh.lock);
510
	fdput(f);
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	return 0;
}

SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
		const struct itimerspec __user *, utmr,
		struct itimerspec __user *, otmr)
{
	struct itimerspec new, old;
	int ret;

	if (copy_from_user(&new, utmr, sizeof(new)))
		return -EFAULT;
	ret = do_timerfd_settime(ufd, flags, &new, &old);
	if (ret)
		return ret;
	if (otmr && copy_to_user(otmr, &old, sizeof(old)))
		return -EFAULT;

	return ret;
}
D
Davide Libenzi 已提交
531

532 533 534 535 536 537
SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
{
	struct itimerspec kotmr;
	int ret = do_timerfd_gettime(ufd, &kotmr);
	if (ret)
		return ret;
D
Davide Libenzi 已提交
538
	return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
539 540
}

541
#ifdef CONFIG_COMPAT
542
COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
543 544
		const struct compat_itimerspec __user *, utmr,
		struct compat_itimerspec __user *, otmr)
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
{
	struct itimerspec new, old;
	int ret;

	if (get_compat_itimerspec(&new, utmr))
		return -EFAULT;
	ret = do_timerfd_settime(ufd, flags, &new, &old);
	if (ret)
		return ret;
	if (otmr && put_compat_itimerspec(otmr, &old))
		return -EFAULT;
	return ret;
}

COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
560
		struct compat_itimerspec __user *, otmr)
561 562 563 564 565
{
	struct itimerspec kotmr;
	int ret = do_timerfd_gettime(ufd, &kotmr);
	if (ret)
		return ret;
566
	return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
567 568
}
#endif