| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  fs/timerfd.c | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org> | 
 | 5 |  * | 
 | 6 |  * | 
 | 7 |  *  Thanks to Thomas Gleixner for code reviews and useful comments. | 
 | 8 |  * | 
 | 9 |  */ | 
 | 10 |  | 
 | 11 | #include <linux/file.h> | 
 | 12 | #include <linux/poll.h> | 
 | 13 | #include <linux/init.h> | 
 | 14 | #include <linux/fs.h> | 
 | 15 | #include <linux/sched.h> | 
 | 16 | #include <linux/kernel.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 18 | #include <linux/list.h> | 
 | 19 | #include <linux/spinlock.h> | 
 | 20 | #include <linux/time.h> | 
 | 21 | #include <linux/hrtimer.h> | 
 | 22 | #include <linux/anon_inodes.h> | 
 | 23 | #include <linux/timerfd.h> | 
| Adrian Bunk | 45cc2b9 | 2008-04-29 00:58:59 -0700 | [diff] [blame] | 24 | #include <linux/syscalls.h> | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 25 |  | 
 | 26 | struct timerfd_ctx { | 
 | 27 | 	struct hrtimer tmr; | 
 | 28 | 	ktime_t tintv; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 29 | 	wait_queue_head_t wqh; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 30 | 	u64 ticks; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 31 | 	int expired; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 32 | 	int clockid; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 33 | }; | 
 | 34 |  | 
 | 35 | /* | 
 | 36 |  * This gets called when the timer event triggers. We set the "expired" | 
 | 37 |  * flag, but we do not re-arm the timer (in case it's necessary, | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 38 |  * tintv.tv64 != 0) until the timer is accessed. | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 39 |  */ | 
 | 40 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) | 
 | 41 | { | 
 | 42 | 	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr); | 
 | 43 | 	unsigned long flags; | 
 | 44 |  | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 45 | 	spin_lock_irqsave(&ctx->wqh.lock, flags); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 46 | 	ctx->expired = 1; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 47 | 	ctx->ticks++; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 48 | 	wake_up_locked(&ctx->wqh); | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 49 | 	spin_unlock_irqrestore(&ctx->wqh.lock, flags); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 50 |  | 
 | 51 | 	return HRTIMER_NORESTART; | 
 | 52 | } | 
 | 53 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 54 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) | 
 | 55 | { | 
| Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 56 | 	ktime_t remaining; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 57 |  | 
| Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 58 | 	remaining = hrtimer_expires_remaining(&ctx->tmr); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 59 | 	return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; | 
 | 60 | } | 
 | 61 |  | 
 | 62 | static void timerfd_setup(struct timerfd_ctx *ctx, int flags, | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 63 | 			  const struct itimerspec *ktmr) | 
 | 64 | { | 
 | 65 | 	enum hrtimer_mode htmode; | 
 | 66 | 	ktime_t texp; | 
 | 67 |  | 
 | 68 | 	htmode = (flags & TFD_TIMER_ABSTIME) ? | 
 | 69 | 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL; | 
 | 70 |  | 
 | 71 | 	texp = timespec_to_ktime(ktmr->it_value); | 
 | 72 | 	ctx->expired = 0; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 73 | 	ctx->ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 74 | 	ctx->tintv = timespec_to_ktime(ktmr->it_interval); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 75 | 	hrtimer_init(&ctx->tmr, ctx->clockid, htmode); | 
| Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 76 | 	hrtimer_set_expires(&ctx->tmr, texp); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 77 | 	ctx->tmr.function = timerfd_tmrproc; | 
 | 78 | 	if (texp.tv64 != 0) | 
 | 79 | 		hrtimer_start(&ctx->tmr, texp, htmode); | 
 | 80 | } | 
 | 81 |  | 
 | 82 | static int timerfd_release(struct inode *inode, struct file *file) | 
 | 83 | { | 
 | 84 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 85 |  | 
 | 86 | 	hrtimer_cancel(&ctx->tmr); | 
 | 87 | 	kfree(ctx); | 
 | 88 | 	return 0; | 
 | 89 | } | 
 | 90 |  | 
 | 91 | static unsigned int timerfd_poll(struct file *file, poll_table *wait) | 
 | 92 | { | 
 | 93 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 94 | 	unsigned int events = 0; | 
 | 95 | 	unsigned long flags; | 
 | 96 |  | 
 | 97 | 	poll_wait(file, &ctx->wqh, wait); | 
 | 98 |  | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 99 | 	spin_lock_irqsave(&ctx->wqh.lock, flags); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 100 | 	if (ctx->ticks) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 101 | 		events |= POLLIN; | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 102 | 	spin_unlock_irqrestore(&ctx->wqh.lock, flags); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 103 |  | 
 | 104 | 	return events; | 
 | 105 | } | 
 | 106 |  | 
 | 107 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, | 
 | 108 | 			    loff_t *ppos) | 
 | 109 | { | 
 | 110 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 111 | 	ssize_t res; | 
| Davide Libenzi | 09828402 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 112 | 	u64 ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 113 |  | 
 | 114 | 	if (count < sizeof(ticks)) | 
 | 115 | 		return -EINVAL; | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 116 | 	spin_lock_irq(&ctx->wqh.lock); | 
| Michal Nazarewicz | 8120a8a | 2010-05-05 12:53:12 +0200 | [diff] [blame] | 117 | 	if (file->f_flags & O_NONBLOCK) | 
 | 118 | 		res = -EAGAIN; | 
 | 119 | 	else | 
 | 120 | 		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 121 | 	if (ctx->ticks) { | 
 | 122 | 		ticks = ctx->ticks; | 
 | 123 | 		if (ctx->expired && ctx->tintv.tv64) { | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 124 | 			/* | 
 | 125 | 			 * If tintv.tv64 != 0, this is a periodic timer that | 
 | 126 | 			 * needs to be re-armed. We avoid doing it in the timer | 
 | 127 | 			 * callback to avoid DoS attacks specifying a very | 
 | 128 | 			 * short timer period. | 
 | 129 | 			 */ | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 130 | 			ticks += hrtimer_forward_now(&ctx->tmr, | 
 | 131 | 						     ctx->tintv) - 1; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 132 | 			hrtimer_restart(&ctx->tmr); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 133 | 		} | 
 | 134 | 		ctx->expired = 0; | 
 | 135 | 		ctx->ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 136 | 	} | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 137 | 	spin_unlock_irq(&ctx->wqh.lock); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 138 | 	if (ticks) | 
| Davide Libenzi | 09828402 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 139 | 		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 140 | 	return res; | 
 | 141 | } | 
 | 142 |  | 
 | 143 | static const struct file_operations timerfd_fops = { | 
 | 144 | 	.release	= timerfd_release, | 
 | 145 | 	.poll		= timerfd_poll, | 
 | 146 | 	.read		= timerfd_read, | 
 | 147 | }; | 
 | 148 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 149 | static struct file *timerfd_fget(int fd) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 150 | { | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 151 | 	struct file *file; | 
 | 152 |  | 
 | 153 | 	file = fget(fd); | 
 | 154 | 	if (!file) | 
 | 155 | 		return ERR_PTR(-EBADF); | 
 | 156 | 	if (file->f_op != &timerfd_fops) { | 
 | 157 | 		fput(file); | 
 | 158 | 		return ERR_PTR(-EINVAL); | 
 | 159 | 	} | 
 | 160 |  | 
 | 161 | 	return file; | 
 | 162 | } | 
 | 163 |  | 
| Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 164 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 165 | { | 
| Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 166 | 	int ufd; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 167 | 	struct timerfd_ctx *ctx; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 168 |  | 
| Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 169 | 	/* Check the TFD_* constants for consistency.  */ | 
 | 170 | 	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); | 
 | 171 | 	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); | 
 | 172 |  | 
| Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 173 | 	if ((flags & ~TFD_CREATE_FLAGS) || | 
 | 174 | 	    (clockid != CLOCK_MONOTONIC && | 
 | 175 | 	     clockid != CLOCK_REALTIME)) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 176 | 		return -EINVAL; | 
 | 177 |  | 
 | 178 | 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | 
 | 179 | 	if (!ctx) | 
 | 180 | 		return -ENOMEM; | 
 | 181 |  | 
 | 182 | 	init_waitqueue_head(&ctx->wqh); | 
 | 183 | 	ctx->clockid = clockid; | 
 | 184 | 	hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS); | 
 | 185 |  | 
| Ulrich Drepper | 11fcb6c | 2008-07-23 21:29:26 -0700 | [diff] [blame] | 186 | 	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, | 
| Roland Dreier | 628ff7c | 2009-12-18 09:41:24 -0800 | [diff] [blame] | 187 | 			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); | 
| Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 188 | 	if (ufd < 0) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 189 | 		kfree(ctx); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 190 |  | 
 | 191 | 	return ufd; | 
 | 192 | } | 
 | 193 |  | 
| Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 194 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, | 
 | 195 | 		const struct itimerspec __user *, utmr, | 
 | 196 | 		struct itimerspec __user *, otmr) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 197 | { | 
 | 198 | 	struct file *file; | 
 | 199 | 	struct timerfd_ctx *ctx; | 
 | 200 | 	struct itimerspec ktmr, kotmr; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 201 |  | 
 | 202 | 	if (copy_from_user(&ktmr, utmr, sizeof(ktmr))) | 
 | 203 | 		return -EFAULT; | 
 | 204 |  | 
| Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 205 | 	if ((flags & ~TFD_SETTIME_FLAGS) || | 
 | 206 | 	    !timespec_valid(&ktmr.it_value) || | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 207 | 	    !timespec_valid(&ktmr.it_interval)) | 
 | 208 | 		return -EINVAL; | 
 | 209 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 210 | 	file = timerfd_fget(ufd); | 
 | 211 | 	if (IS_ERR(file)) | 
 | 212 | 		return PTR_ERR(file); | 
 | 213 | 	ctx = file->private_data; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 214 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 215 | 	/* | 
 | 216 | 	 * We need to stop the existing timer before reprogramming | 
 | 217 | 	 * it to the new values. | 
 | 218 | 	 */ | 
 | 219 | 	for (;;) { | 
 | 220 | 		spin_lock_irq(&ctx->wqh.lock); | 
 | 221 | 		if (hrtimer_try_to_cancel(&ctx->tmr) >= 0) | 
 | 222 | 			break; | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 223 | 		spin_unlock_irq(&ctx->wqh.lock); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 224 | 		cpu_relax(); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 225 | 	} | 
 | 226 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 227 | 	/* | 
 | 228 | 	 * If the timer is expired and it's periodic, we need to advance it | 
 | 229 | 	 * because the caller may want to know the previous expiration time. | 
 | 230 | 	 * We do not update "ticks" and "expired" since the timer will be | 
 | 231 | 	 * re-programmed again in the following timerfd_setup() call. | 
 | 232 | 	 */ | 
 | 233 | 	if (ctx->expired && ctx->tintv.tv64) | 
 | 234 | 		hrtimer_forward_now(&ctx->tmr, ctx->tintv); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 235 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 236 | 	kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | 
 | 237 | 	kotmr.it_interval = ktime_to_timespec(ctx->tintv); | 
 | 238 |  | 
 | 239 | 	/* | 
 | 240 | 	 * Re-program the timer to the new value ... | 
 | 241 | 	 */ | 
 | 242 | 	timerfd_setup(ctx, flags, &ktmr); | 
 | 243 |  | 
 | 244 | 	spin_unlock_irq(&ctx->wqh.lock); | 
 | 245 | 	fput(file); | 
 | 246 | 	if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr))) | 
 | 247 | 		return -EFAULT; | 
 | 248 |  | 
 | 249 | 	return 0; | 
 | 250 | } | 
 | 251 |  | 
| Heiko Carstens | d4e8204 | 2009-01-14 14:14:34 +0100 | [diff] [blame] | 252 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 253 | { | 
 | 254 | 	struct file *file; | 
 | 255 | 	struct timerfd_ctx *ctx; | 
 | 256 | 	struct itimerspec kotmr; | 
 | 257 |  | 
 | 258 | 	file = timerfd_fget(ufd); | 
 | 259 | 	if (IS_ERR(file)) | 
 | 260 | 		return PTR_ERR(file); | 
 | 261 | 	ctx = file->private_data; | 
 | 262 |  | 
 | 263 | 	spin_lock_irq(&ctx->wqh.lock); | 
 | 264 | 	if (ctx->expired && ctx->tintv.tv64) { | 
 | 265 | 		ctx->expired = 0; | 
 | 266 | 		ctx->ticks += | 
 | 267 | 			hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1; | 
 | 268 | 		hrtimer_restart(&ctx->tmr); | 
 | 269 | 	} | 
 | 270 | 	kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | 
 | 271 | 	kotmr.it_interval = ktime_to_timespec(ctx->tintv); | 
 | 272 | 	spin_unlock_irq(&ctx->wqh.lock); | 
 | 273 | 	fput(file); | 
 | 274 |  | 
 | 275 | 	return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 276 | } | 
 | 277 |  |