| 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 |  | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 11 | #include <linux/alarmtimer.h> | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 12 | #include <linux/file.h> | 
 | 13 | #include <linux/poll.h> | 
 | 14 | #include <linux/init.h> | 
 | 15 | #include <linux/fs.h> | 
 | 16 | #include <linux/sched.h> | 
 | 17 | #include <linux/kernel.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 19 | #include <linux/list.h> | 
 | 20 | #include <linux/spinlock.h> | 
 | 21 | #include <linux/time.h> | 
 | 22 | #include <linux/hrtimer.h> | 
 | 23 | #include <linux/anon_inodes.h> | 
 | 24 | #include <linux/timerfd.h> | 
| Adrian Bunk | 45cc2b9 | 2008-04-29 00:58:59 -0700 | [diff] [blame] | 25 | #include <linux/syscalls.h> | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 26 | #include <linux/compat.h> | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 27 | #include <linux/rcupdate.h> | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 28 |  | 
 | 29 | struct timerfd_ctx { | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 30 | 	union { | 
 | 31 | 		struct hrtimer tmr; | 
 | 32 | 		struct alarm alarm; | 
 | 33 | 	} t; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 34 | 	ktime_t tintv; | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 35 | 	ktime_t moffs; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 36 | 	wait_queue_head_t wqh; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 37 | 	u64 ticks; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 38 | 	int expired; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 39 | 	int clockid; | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 40 | 	struct rcu_head rcu; | 
 | 41 | 	struct list_head clist; | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 42 | 	bool might_cancel; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 43 | }; | 
 | 44 |  | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 45 | static LIST_HEAD(cancel_list); | 
 | 46 | static DEFINE_SPINLOCK(cancel_lock); | 
 | 47 |  | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 48 | static inline bool isalarm(struct timerfd_ctx *ctx) | 
 | 49 | { | 
 | 50 | 	return ctx->clockid == CLOCK_REALTIME_ALARM || | 
 | 51 | 		ctx->clockid == CLOCK_BOOTTIME_ALARM; | 
 | 52 | } | 
 | 53 |  | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 54 | /* | 
 | 55 |  * This gets called when the timer event triggers. We set the "expired" | 
 | 56 |  * 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] | 57 |  * tintv.tv64 != 0) until the timer is accessed. | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 58 |  */ | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 59 | static void timerfd_triggered(struct timerfd_ctx *ctx) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 60 | { | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 61 | 	unsigned long flags; | 
 | 62 |  | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 63 | 	spin_lock_irqsave(&ctx->wqh.lock, flags); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 64 | 	ctx->expired = 1; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 65 | 	ctx->ticks++; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 66 | 	wake_up_locked(&ctx->wqh); | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 67 | 	spin_unlock_irqrestore(&ctx->wqh.lock, flags); | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 68 | } | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 69 |  | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 70 | static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr) | 
 | 71 | { | 
 | 72 | 	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, | 
 | 73 | 					       t.tmr); | 
 | 74 | 	timerfd_triggered(ctx); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 75 | 	return HRTIMER_NORESTART; | 
 | 76 | } | 
 | 77 |  | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 78 | static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm, | 
 | 79 | 	ktime_t now) | 
 | 80 | { | 
 | 81 | 	struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx, | 
 | 82 | 					       t.alarm); | 
 | 83 | 	timerfd_triggered(ctx); | 
 | 84 | 	return ALARMTIMER_NORESTART; | 
 | 85 | } | 
 | 86 |  | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 87 | /* | 
 | 88 |  * Called when the clock was set to cancel the timers in the cancel | 
| Max Asbock | 1123d939 | 2011-06-13 10:18:32 -0700 | [diff] [blame] | 89 |  * list. This will wake up processes waiting on these timers. The | 
 | 90 |  * wake-up requires ctx->ticks to be non zero, therefore we increment | 
 | 91 |  * it before calling wake_up_locked(). | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 92 |  */ | 
 | 93 | void timerfd_clock_was_set(void) | 
 | 94 | { | 
 | 95 | 	ktime_t moffs = ktime_get_monotonic_offset(); | 
 | 96 | 	struct timerfd_ctx *ctx; | 
 | 97 | 	unsigned long flags; | 
 | 98 |  | 
 | 99 | 	rcu_read_lock(); | 
 | 100 | 	list_for_each_entry_rcu(ctx, &cancel_list, clist) { | 
 | 101 | 		if (!ctx->might_cancel) | 
 | 102 | 			continue; | 
 | 103 | 		spin_lock_irqsave(&ctx->wqh.lock, flags); | 
 | 104 | 		if (ctx->moffs.tv64 != moffs.tv64) { | 
 | 105 | 			ctx->moffs.tv64 = KTIME_MAX; | 
| Max Asbock | 1123d939 | 2011-06-13 10:18:32 -0700 | [diff] [blame] | 106 | 			ctx->ticks++; | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 107 | 			wake_up_locked(&ctx->wqh); | 
 | 108 | 		} | 
 | 109 | 		spin_unlock_irqrestore(&ctx->wqh.lock, flags); | 
 | 110 | 	} | 
 | 111 | 	rcu_read_unlock(); | 
 | 112 | } | 
 | 113 |  | 
 | 114 | static void timerfd_remove_cancel(struct timerfd_ctx *ctx) | 
 | 115 | { | 
 | 116 | 	if (ctx->might_cancel) { | 
 | 117 | 		ctx->might_cancel = false; | 
 | 118 | 		spin_lock(&cancel_lock); | 
 | 119 | 		list_del_rcu(&ctx->clist); | 
 | 120 | 		spin_unlock(&cancel_lock); | 
 | 121 | 	} | 
 | 122 | } | 
 | 123 |  | 
 | 124 | static bool timerfd_canceled(struct timerfd_ctx *ctx) | 
 | 125 | { | 
 | 126 | 	if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX) | 
 | 127 | 		return false; | 
 | 128 | 	ctx->moffs = ktime_get_monotonic_offset(); | 
 | 129 | 	return true; | 
 | 130 | } | 
 | 131 |  | 
 | 132 | static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags) | 
 | 133 | { | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 134 | 	if ((ctx->clockid == CLOCK_REALTIME || | 
 | 135 | 	     ctx->clockid == CLOCK_REALTIME_ALARM) && | 
 | 136 | 	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) { | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 137 | 		if (!ctx->might_cancel) { | 
 | 138 | 			ctx->might_cancel = true; | 
 | 139 | 			spin_lock(&cancel_lock); | 
 | 140 | 			list_add_rcu(&ctx->clist, &cancel_list); | 
 | 141 | 			spin_unlock(&cancel_lock); | 
 | 142 | 		} | 
 | 143 | 	} else if (ctx->might_cancel) { | 
 | 144 | 		timerfd_remove_cancel(ctx); | 
 | 145 | 	} | 
 | 146 | } | 
 | 147 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 148 | static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx) | 
 | 149 | { | 
| Arjan van de Ven | 7636947 | 2008-09-01 15:00:14 -0700 | [diff] [blame] | 150 | 	ktime_t remaining; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 151 |  | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 152 | 	if (isalarm(ctx)) | 
 | 153 | 		remaining = alarm_expires_remaining(&ctx->t.alarm); | 
 | 154 | 	else | 
 | 155 | 		remaining = hrtimer_expires_remaining(&ctx->t.tmr); | 
 | 156 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 157 | 	return remaining.tv64 < 0 ? ktime_set(0, 0): remaining; | 
 | 158 | } | 
 | 159 |  | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 160 | static int timerfd_setup(struct timerfd_ctx *ctx, int flags, | 
 | 161 | 			 const struct itimerspec *ktmr) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 162 | { | 
 | 163 | 	enum hrtimer_mode htmode; | 
 | 164 | 	ktime_t texp; | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 165 | 	int clockid = ctx->clockid; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 166 |  | 
 | 167 | 	htmode = (flags & TFD_TIMER_ABSTIME) ? | 
 | 168 | 		HRTIMER_MODE_ABS: HRTIMER_MODE_REL; | 
 | 169 |  | 
 | 170 | 	texp = timespec_to_ktime(ktmr->it_value); | 
 | 171 | 	ctx->expired = 0; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 172 | 	ctx->ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 173 | 	ctx->tintv = timespec_to_ktime(ktmr->it_interval); | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 174 |  | 
 | 175 | 	if (isalarm(ctx)) { | 
 | 176 | 		alarm_init(&ctx->t.alarm, | 
 | 177 | 			   ctx->clockid == CLOCK_REALTIME_ALARM ? | 
 | 178 | 			   ALARM_REALTIME : ALARM_BOOTTIME, | 
 | 179 | 			   timerfd_alarmproc); | 
 | 180 | 	} else { | 
 | 181 | 		hrtimer_init(&ctx->t.tmr, clockid, htmode); | 
 | 182 | 		hrtimer_set_expires(&ctx->t.tmr, texp); | 
 | 183 | 		ctx->t.tmr.function = timerfd_tmrproc; | 
 | 184 | 	} | 
 | 185 |  | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 186 | 	if (texp.tv64 != 0) { | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 187 | 		if (isalarm(ctx)) { | 
 | 188 | 			if (flags & TFD_TIMER_ABSTIME) | 
 | 189 | 				alarm_start(&ctx->t.alarm, texp); | 
 | 190 | 			else | 
 | 191 | 				alarm_start_relative(&ctx->t.alarm, texp); | 
 | 192 | 		} else { | 
 | 193 | 			hrtimer_start(&ctx->t.tmr, texp, htmode); | 
 | 194 | 		} | 
 | 195 |  | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 196 | 		if (timerfd_canceled(ctx)) | 
 | 197 | 			return -ECANCELED; | 
 | 198 | 	} | 
 | 199 | 	return 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 200 | } | 
 | 201 |  | 
 | 202 | static int timerfd_release(struct inode *inode, struct file *file) | 
 | 203 | { | 
 | 204 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 205 |  | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 206 | 	timerfd_remove_cancel(ctx); | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 207 |  | 
 | 208 | 	if (isalarm(ctx)) | 
 | 209 | 		alarm_cancel(&ctx->t.alarm); | 
 | 210 | 	else | 
 | 211 | 		hrtimer_cancel(&ctx->t.tmr); | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 212 | 	kfree_rcu(ctx, rcu); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 213 | 	return 0; | 
 | 214 | } | 
 | 215 |  | 
 | 216 | static unsigned int timerfd_poll(struct file *file, poll_table *wait) | 
 | 217 | { | 
 | 218 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 219 | 	unsigned int events = 0; | 
 | 220 | 	unsigned long flags; | 
 | 221 |  | 
 | 222 | 	poll_wait(file, &ctx->wqh, wait); | 
 | 223 |  | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 224 | 	spin_lock_irqsave(&ctx->wqh.lock, flags); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 225 | 	if (ctx->ticks) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 226 | 		events |= POLLIN; | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 227 | 	spin_unlock_irqrestore(&ctx->wqh.lock, flags); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 228 |  | 
 | 229 | 	return events; | 
 | 230 | } | 
 | 231 |  | 
 | 232 | static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count, | 
 | 233 | 			    loff_t *ppos) | 
 | 234 | { | 
 | 235 | 	struct timerfd_ctx *ctx = file->private_data; | 
 | 236 | 	ssize_t res; | 
| Davide Libenzi | 09828402 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 237 | 	u64 ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 238 |  | 
 | 239 | 	if (count < sizeof(ticks)) | 
 | 240 | 		return -EINVAL; | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 241 | 	spin_lock_irq(&ctx->wqh.lock); | 
| Michal Nazarewicz | 8120a8a | 2010-05-05 12:53:12 +0200 | [diff] [blame] | 242 | 	if (file->f_flags & O_NONBLOCK) | 
 | 243 | 		res = -EAGAIN; | 
 | 244 | 	else | 
 | 245 | 		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks); | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 246 |  | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 247 | 	/* | 
 | 248 | 	 * If clock has changed, we do not care about the | 
 | 249 | 	 * ticks and we do not rearm the timer. Userspace must | 
 | 250 | 	 * reevaluate anyway. | 
 | 251 | 	 */ | 
 | 252 | 	if (timerfd_canceled(ctx)) { | 
 | 253 | 		ctx->ticks = 0; | 
 | 254 | 		ctx->expired = 0; | 
 | 255 | 		res = -ECANCELED; | 
 | 256 | 	} | 
 | 257 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 258 | 	if (ctx->ticks) { | 
 | 259 | 		ticks = ctx->ticks; | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 260 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 261 | 		if (ctx->expired && ctx->tintv.tv64) { | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 262 | 			/* | 
 | 263 | 			 * If tintv.tv64 != 0, this is a periodic timer that | 
 | 264 | 			 * needs to be re-armed. We avoid doing it in the timer | 
 | 265 | 			 * callback to avoid DoS attacks specifying a very | 
 | 266 | 			 * short timer period. | 
 | 267 | 			 */ | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 268 | 			if (isalarm(ctx)) { | 
 | 269 | 				ticks += alarm_forward_now( | 
 | 270 | 					&ctx->t.alarm, ctx->tintv) - 1; | 
 | 271 | 				alarm_restart(&ctx->t.alarm); | 
 | 272 | 			} else { | 
 | 273 | 				ticks += hrtimer_forward_now(&ctx->t.tmr, | 
 | 274 | 							     ctx->tintv) - 1; | 
 | 275 | 				hrtimer_restart(&ctx->t.tmr); | 
 | 276 | 			} | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 277 | 		} | 
 | 278 | 		ctx->expired = 0; | 
 | 279 | 		ctx->ticks = 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 280 | 	} | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 281 | 	spin_unlock_irq(&ctx->wqh.lock); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 282 | 	if (ticks) | 
| Davide Libenzi | 09828402 | 2007-07-26 10:41:07 -0700 | [diff] [blame] | 283 | 		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 284 | 	return res; | 
 | 285 | } | 
 | 286 |  | 
 | 287 | static const struct file_operations timerfd_fops = { | 
 | 288 | 	.release	= timerfd_release, | 
 | 289 | 	.poll		= timerfd_poll, | 
 | 290 | 	.read		= timerfd_read, | 
| Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 291 | 	.llseek		= noop_llseek, | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 292 | }; | 
 | 293 |  | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 294 | static int timerfd_fget(int fd, struct fd *p) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 295 | { | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 296 | 	struct fd f = fdget(fd); | 
 | 297 | 	if (!f.file) | 
 | 298 | 		return -EBADF; | 
 | 299 | 	if (f.file->f_op != &timerfd_fops) { | 
 | 300 | 		fdput(f); | 
 | 301 | 		return -EINVAL; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 302 | 	} | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 303 | 	*p = f; | 
 | 304 | 	return 0; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 305 | } | 
 | 306 |  | 
| Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 307 | SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 308 | { | 
| Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 309 | 	int ufd; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 310 | 	struct timerfd_ctx *ctx; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 311 |  | 
| Ulrich Drepper | e38b36f | 2008-07-23 21:29:42 -0700 | [diff] [blame] | 312 | 	/* Check the TFD_* constants for consistency.  */ | 
 | 313 | 	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC); | 
 | 314 | 	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK); | 
 | 315 |  | 
| Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 316 | 	if ((flags & ~TFD_CREATE_FLAGS) || | 
 | 317 | 	    (clockid != CLOCK_MONOTONIC && | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 318 | 	     clockid != CLOCK_REALTIME && | 
 | 319 | 	     clockid != CLOCK_REALTIME_ALARM && | 
 | 320 | 	     clockid != CLOCK_BOOTTIME_ALARM)) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 321 | 		return -EINVAL; | 
 | 322 |  | 
 | 323 | 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | 
 | 324 | 	if (!ctx) | 
 | 325 | 		return -ENOMEM; | 
 | 326 |  | 
 | 327 | 	init_waitqueue_head(&ctx->wqh); | 
 | 328 | 	ctx->clockid = clockid; | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 329 |  | 
 | 330 | 	if (isalarm(ctx)) | 
 | 331 | 		alarm_init(&ctx->t.alarm, | 
 | 332 | 			   ctx->clockid == CLOCK_REALTIME_ALARM ? | 
 | 333 | 			   ALARM_REALTIME : ALARM_BOOTTIME, | 
 | 334 | 			   timerfd_alarmproc); | 
 | 335 | 	else | 
 | 336 | 		hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS); | 
 | 337 |  | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 338 | 	ctx->moffs = ktime_get_monotonic_offset(); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 339 |  | 
| Ulrich Drepper | 11fcb6c | 2008-07-23 21:29:26 -0700 | [diff] [blame] | 340 | 	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx, | 
| Roland Dreier | 628ff7c | 2009-12-18 09:41:24 -0800 | [diff] [blame] | 341 | 			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS)); | 
| Al Viro | 2030a42 | 2008-02-23 06:46:49 -0500 | [diff] [blame] | 342 | 	if (ufd < 0) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 343 | 		kfree(ctx); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 344 |  | 
 | 345 | 	return ufd; | 
 | 346 | } | 
 | 347 |  | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 348 | static int do_timerfd_settime(int ufd, int flags,  | 
 | 349 | 		const struct itimerspec *new, | 
 | 350 | 		struct itimerspec *old) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 351 | { | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 352 | 	struct fd f; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 353 | 	struct timerfd_ctx *ctx; | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 354 | 	int ret; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 355 |  | 
| Davide Libenzi | 610d18f | 2009-02-18 14:48:18 -0800 | [diff] [blame] | 356 | 	if ((flags & ~TFD_SETTIME_FLAGS) || | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 357 | 	    !timespec_valid(&new->it_value) || | 
 | 358 | 	    !timespec_valid(&new->it_interval)) | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 359 | 		return -EINVAL; | 
 | 360 |  | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 361 | 	ret = timerfd_fget(ufd, &f); | 
 | 362 | 	if (ret) | 
 | 363 | 		return ret; | 
 | 364 | 	ctx = f.file->private_data; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 365 |  | 
| Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 366 | 	timerfd_setup_cancel(ctx, flags); | 
 | 367 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 368 | 	/* | 
 | 369 | 	 * We need to stop the existing timer before reprogramming | 
 | 370 | 	 * it to the new values. | 
 | 371 | 	 */ | 
 | 372 | 	for (;;) { | 
 | 373 | 		spin_lock_irq(&ctx->wqh.lock); | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 374 |  | 
 | 375 | 		if (isalarm(ctx)) { | 
 | 376 | 			if (alarm_try_to_cancel(&ctx->t.alarm) >= 0) | 
 | 377 | 				break; | 
 | 378 | 		} else { | 
 | 379 | 			if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0) | 
 | 380 | 				break; | 
 | 381 | 		} | 
| Davide Libenzi | 18963c0 | 2007-05-18 12:02:33 -0700 | [diff] [blame] | 382 | 		spin_unlock_irq(&ctx->wqh.lock); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 383 | 		cpu_relax(); | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 384 | 	} | 
 | 385 |  | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 386 | 	/* | 
 | 387 | 	 * If the timer is expired and it's periodic, we need to advance it | 
 | 388 | 	 * because the caller may want to know the previous expiration time. | 
 | 389 | 	 * We do not update "ticks" and "expired" since the timer will be | 
 | 390 | 	 * re-programmed again in the following timerfd_setup() call. | 
 | 391 | 	 */ | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 392 | 	if (ctx->expired && ctx->tintv.tv64) { | 
 | 393 | 		if (isalarm(ctx)) | 
 | 394 | 			alarm_forward_now(&ctx->t.alarm, ctx->tintv); | 
 | 395 | 		else | 
 | 396 | 			hrtimer_forward_now(&ctx->t.tmr, ctx->tintv); | 
 | 397 | 	} | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 398 |  | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 399 | 	old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | 
 | 400 | 	old->it_interval = ktime_to_timespec(ctx->tintv); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 401 |  | 
 | 402 | 	/* | 
 | 403 | 	 * Re-program the timer to the new value ... | 
 | 404 | 	 */ | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 405 | 	ret = timerfd_setup(ctx, flags, new); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 406 |  | 
 | 407 | 	spin_unlock_irq(&ctx->wqh.lock); | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 408 | 	fdput(f); | 
| Thomas Gleixner | 99ee531 | 2011-04-27 14:16:42 +0200 | [diff] [blame] | 409 | 	return ret; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 410 | } | 
 | 411 |  | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 412 | static int do_timerfd_gettime(int ufd, struct itimerspec *t) | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 413 | { | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 414 | 	struct fd f; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 415 | 	struct timerfd_ctx *ctx; | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 416 | 	int ret = timerfd_fget(ufd, &f); | 
 | 417 | 	if (ret) | 
 | 418 | 		return ret; | 
 | 419 | 	ctx = f.file->private_data; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 420 |  | 
 | 421 | 	spin_lock_irq(&ctx->wqh.lock); | 
 | 422 | 	if (ctx->expired && ctx->tintv.tv64) { | 
 | 423 | 		ctx->expired = 0; | 
| Todd Poynor | 11ffa9d | 2013-05-15 14:38:12 -0700 | [diff] [blame] | 424 |  | 
 | 425 | 		if (isalarm(ctx)) { | 
 | 426 | 			ctx->ticks += | 
 | 427 | 				alarm_forward_now( | 
 | 428 | 					&ctx->t.alarm, ctx->tintv) - 1; | 
 | 429 | 			alarm_restart(&ctx->t.alarm); | 
 | 430 | 		} else { | 
 | 431 | 			ctx->ticks += | 
 | 432 | 				hrtimer_forward_now(&ctx->t.tmr, ctx->tintv) | 
 | 433 | 				- 1; | 
 | 434 | 			hrtimer_restart(&ctx->t.tmr); | 
 | 435 | 		} | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 436 | 	} | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 437 | 	t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx)); | 
 | 438 | 	t->it_interval = ktime_to_timespec(ctx->tintv); | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 439 | 	spin_unlock_irq(&ctx->wqh.lock); | 
| Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 440 | 	fdput(f); | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 441 | 	return 0; | 
 | 442 | } | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 443 |  | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 444 | SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, | 
 | 445 | 		const struct itimerspec __user *, utmr, | 
 | 446 | 		struct itimerspec __user *, otmr) | 
 | 447 | { | 
 | 448 | 	struct itimerspec new, old; | 
 | 449 | 	int ret; | 
 | 450 |  | 
 | 451 | 	if (copy_from_user(&new, utmr, sizeof(new))) | 
 | 452 | 		return -EFAULT; | 
 | 453 | 	ret = do_timerfd_settime(ufd, flags, &new, &old); | 
 | 454 | 	if (ret) | 
 | 455 | 		return ret; | 
 | 456 | 	if (otmr && copy_to_user(otmr, &old, sizeof(old))) | 
 | 457 | 		return -EFAULT; | 
 | 458 |  | 
 | 459 | 	return ret; | 
 | 460 | } | 
 | 461 |  | 
 | 462 | SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr) | 
 | 463 | { | 
 | 464 | 	struct itimerspec kotmr; | 
 | 465 | 	int ret = do_timerfd_gettime(ufd, &kotmr); | 
 | 466 | 	if (ret) | 
 | 467 | 		return ret; | 
| Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 468 | 	return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0; | 
| Davide Libenzi | b215e28 | 2007-05-10 22:23:16 -0700 | [diff] [blame] | 469 | } | 
 | 470 |  | 
| Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 471 | #ifdef CONFIG_COMPAT | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 472 | COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags, | 
| Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 473 | 		const struct compat_itimerspec __user *, utmr, | 
 | 474 | 		struct compat_itimerspec __user *, otmr) | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 475 | { | 
 | 476 | 	struct itimerspec new, old; | 
 | 477 | 	int ret; | 
 | 478 |  | 
 | 479 | 	if (get_compat_itimerspec(&new, utmr)) | 
 | 480 | 		return -EFAULT; | 
 | 481 | 	ret = do_timerfd_settime(ufd, flags, &new, &old); | 
 | 482 | 	if (ret) | 
 | 483 | 		return ret; | 
 | 484 | 	if (otmr && put_compat_itimerspec(otmr, &old)) | 
 | 485 | 		return -EFAULT; | 
 | 486 | 	return ret; | 
 | 487 | } | 
 | 488 |  | 
 | 489 | COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd, | 
| Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 490 | 		struct compat_itimerspec __user *, otmr) | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 491 | { | 
 | 492 | 	struct itimerspec kotmr; | 
 | 493 | 	int ret = do_timerfd_gettime(ufd, &kotmr); | 
 | 494 | 	if (ret) | 
 | 495 | 		return ret; | 
| Heiko Carstens | 0e803ba | 2013-03-02 12:26:30 +0100 | [diff] [blame] | 496 | 	return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0; | 
| Al Viro | 9d94b9e | 2012-12-27 16:52:33 -0500 | [diff] [blame] | 497 | } | 
 | 498 | #endif |