Blame view

fs/timerfd.c 8.61 KB
b215e2839   Davide Libenzi   signal/timer/even...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   *  fs/timerfd.c
   *
   *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
   *
   *
   *  Thanks to Thomas Gleixner for code reviews and useful comments.
   *
   */
  
  #include <linux/file.h>
  #include <linux/poll.h>
  #include <linux/init.h>
  #include <linux/fs.h>
  #include <linux/sched.h>
  #include <linux/kernel.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
b215e2839   Davide Libenzi   signal/timer/even...
18
19
20
21
22
23
  #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>
45cc2b96f   Adrian Bunk   fs/timerfd.c shou...
24
  #include <linux/syscalls.h>
9ec269075   Thomas Gleixner   timerfd: Manage c...
25
  #include <linux/rcupdate.h>
b215e2839   Davide Libenzi   signal/timer/even...
26
27
28
29
  
  struct timerfd_ctx {
  	struct hrtimer tmr;
  	ktime_t tintv;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
30
  	ktime_t moffs;
b215e2839   Davide Libenzi   signal/timer/even...
31
  	wait_queue_head_t wqh;
4d672e7ac   Davide Libenzi   timerfd: new time...
32
  	u64 ticks;
b215e2839   Davide Libenzi   signal/timer/even...
33
  	int expired;
4d672e7ac   Davide Libenzi   timerfd: new time...
34
  	int clockid;
9ec269075   Thomas Gleixner   timerfd: Manage c...
35
36
  	struct rcu_head rcu;
  	struct list_head clist;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
37
  	bool might_cancel;
b215e2839   Davide Libenzi   signal/timer/even...
38
  };
9ec269075   Thomas Gleixner   timerfd: Manage c...
39
40
  static LIST_HEAD(cancel_list);
  static DEFINE_SPINLOCK(cancel_lock);
b215e2839   Davide Libenzi   signal/timer/even...
41
42
43
  /*
   * 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,
4d672e7ac   Davide Libenzi   timerfd: new time...
44
   * tintv.tv64 != 0) until the timer is accessed.
b215e2839   Davide Libenzi   signal/timer/even...
45
46
47
48
49
   */
  static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  {
  	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
  	unsigned long flags;
18963c01b   Davide Libenzi   timerfd use waitq...
50
  	spin_lock_irqsave(&ctx->wqh.lock, flags);
b215e2839   Davide Libenzi   signal/timer/even...
51
  	ctx->expired = 1;
4d672e7ac   Davide Libenzi   timerfd: new time...
52
  	ctx->ticks++;
b215e2839   Davide Libenzi   signal/timer/even...
53
  	wake_up_locked(&ctx->wqh);
18963c01b   Davide Libenzi   timerfd use waitq...
54
  	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
b215e2839   Davide Libenzi   signal/timer/even...
55
56
57
  
  	return HRTIMER_NORESTART;
  }
9ec269075   Thomas Gleixner   timerfd: Manage c...
58
59
  /*
   * Called when the clock was set to cancel the timers in the cancel
1123d9396   Max Asbock   timerfd: Fix wake...
60
61
62
   * 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().
9ec269075   Thomas Gleixner   timerfd: Manage c...
63
64
   */
  void timerfd_clock_was_set(void)
4d672e7ac   Davide Libenzi   timerfd: new time...
65
  {
9ec269075   Thomas Gleixner   timerfd: Manage c...
66
67
68
  	ktime_t moffs = ktime_get_monotonic_offset();
  	struct timerfd_ctx *ctx;
  	unsigned long flags;
4d672e7ac   Davide Libenzi   timerfd: new time...
69

9ec269075   Thomas Gleixner   timerfd: Manage c...
70
71
72
73
74
75
76
  	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;
1123d9396   Max Asbock   timerfd: Fix wake...
77
  			ctx->ticks++;
9ec269075   Thomas Gleixner   timerfd: Manage c...
78
79
80
81
82
  			wake_up_locked(&ctx->wqh);
  		}
  		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  	}
  	rcu_read_unlock();
4d672e7ac   Davide Libenzi   timerfd: new time...
83
  }
9ec269075   Thomas Gleixner   timerfd: Manage c...
84
  static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
85
  {
9ec269075   Thomas Gleixner   timerfd: Manage c...
86
87
88
89
90
91
92
  	if (ctx->might_cancel) {
  		ctx->might_cancel = false;
  		spin_lock(&cancel_lock);
  		list_del_rcu(&ctx->clist);
  		spin_unlock(&cancel_lock);
  	}
  }
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
93

9ec269075   Thomas Gleixner   timerfd: Manage c...
94
95
96
  static bool timerfd_canceled(struct timerfd_ctx *ctx)
  {
  	if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
97
  		return false;
9ec269075   Thomas Gleixner   timerfd: Manage c...
98
99
100
  	ctx->moffs = ktime_get_monotonic_offset();
  	return true;
  }
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
101

9ec269075   Thomas Gleixner   timerfd: Manage c...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  {
  	if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
  	    (flags & TFD_TIMER_CANCEL_ON_SET)) {
  		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);
  	}
  }
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
116

9ec269075   Thomas Gleixner   timerfd: Manage c...
117
118
119
  static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  {
  	ktime_t remaining;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
120

9ec269075   Thomas Gleixner   timerfd: Manage c...
121
122
  	remaining = hrtimer_expires_remaining(&ctx->tmr);
  	return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
123
124
125
126
  }
  
  static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  			 const struct itimerspec *ktmr)
b215e2839   Davide Libenzi   signal/timer/even...
127
128
129
  {
  	enum hrtimer_mode htmode;
  	ktime_t texp;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
130
  	int clockid = ctx->clockid;
b215e2839   Davide Libenzi   signal/timer/even...
131
132
133
134
135
136
  
  	htmode = (flags & TFD_TIMER_ABSTIME) ?
  		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  
  	texp = timespec_to_ktime(ktmr->it_value);
  	ctx->expired = 0;
4d672e7ac   Davide Libenzi   timerfd: new time...
137
  	ctx->ticks = 0;
b215e2839   Davide Libenzi   signal/timer/even...
138
  	ctx->tintv = timespec_to_ktime(ktmr->it_interval);
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
139
  	hrtimer_init(&ctx->tmr, clockid, htmode);
76369470b   Arjan van de Ven   hrtimer: convert ...
140
  	hrtimer_set_expires(&ctx->tmr, texp);
b215e2839   Davide Libenzi   signal/timer/even...
141
  	ctx->tmr.function = timerfd_tmrproc;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
142
  	if (texp.tv64 != 0) {
b215e2839   Davide Libenzi   signal/timer/even...
143
  		hrtimer_start(&ctx->tmr, texp, htmode);
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
144
145
146
147
  		if (timerfd_canceled(ctx))
  			return -ECANCELED;
  	}
  	return 0;
b215e2839   Davide Libenzi   signal/timer/even...
148
149
150
151
152
  }
  
  static int timerfd_release(struct inode *inode, struct file *file)
  {
  	struct timerfd_ctx *ctx = file->private_data;
9ec269075   Thomas Gleixner   timerfd: Manage c...
153
  	timerfd_remove_cancel(ctx);
b215e2839   Davide Libenzi   signal/timer/even...
154
  	hrtimer_cancel(&ctx->tmr);
9ec269075   Thomas Gleixner   timerfd: Manage c...
155
  	kfree_rcu(ctx, rcu);
b215e2839   Davide Libenzi   signal/timer/even...
156
157
158
159
160
161
162
163
164
165
  	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);
18963c01b   Davide Libenzi   timerfd use waitq...
166
  	spin_lock_irqsave(&ctx->wqh.lock, flags);
4d672e7ac   Davide Libenzi   timerfd: new time...
167
  	if (ctx->ticks)
b215e2839   Davide Libenzi   signal/timer/even...
168
  		events |= POLLIN;
18963c01b   Davide Libenzi   timerfd use waitq...
169
  	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
b215e2839   Davide Libenzi   signal/timer/even...
170
171
172
173
174
175
176
177
178
  
  	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;
098284020   Davide Libenzi   make timerfd retu...
179
  	u64 ticks = 0;
b215e2839   Davide Libenzi   signal/timer/even...
180
181
182
  
  	if (count < sizeof(ticks))
  		return -EINVAL;
18963c01b   Davide Libenzi   timerfd use waitq...
183
  	spin_lock_irq(&ctx->wqh.lock);
8120a8aad   Michal Nazarewicz   fs/timerfd.c: mak...
184
185
186
187
  	if (file->f_flags & O_NONBLOCK)
  		res = -EAGAIN;
  	else
  		res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
188

9ec269075   Thomas Gleixner   timerfd: Manage c...
189
190
191
192
193
194
195
196
197
198
  	/*
  	 * 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;
  	}
4d672e7ac   Davide Libenzi   timerfd: new time...
199
200
  	if (ctx->ticks) {
  		ticks = ctx->ticks;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
201

4d672e7ac   Davide Libenzi   timerfd: new time...
202
  		if (ctx->expired && ctx->tintv.tv64) {
b215e2839   Davide Libenzi   signal/timer/even...
203
204
205
206
207
208
  			/*
  			 * 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.
  			 */
4d672e7ac   Davide Libenzi   timerfd: new time...
209
210
  			ticks += hrtimer_forward_now(&ctx->tmr,
  						     ctx->tintv) - 1;
b215e2839   Davide Libenzi   signal/timer/even...
211
  			hrtimer_restart(&ctx->tmr);
4d672e7ac   Davide Libenzi   timerfd: new time...
212
213
214
  		}
  		ctx->expired = 0;
  		ctx->ticks = 0;
b215e2839   Davide Libenzi   signal/timer/even...
215
  	}
18963c01b   Davide Libenzi   timerfd use waitq...
216
  	spin_unlock_irq(&ctx->wqh.lock);
b215e2839   Davide Libenzi   signal/timer/even...
217
  	if (ticks)
098284020   Davide Libenzi   make timerfd retu...
218
  		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
b215e2839   Davide Libenzi   signal/timer/even...
219
220
221
222
223
224
225
  	return res;
  }
  
  static const struct file_operations timerfd_fops = {
  	.release	= timerfd_release,
  	.poll		= timerfd_poll,
  	.read		= timerfd_read,
6038f373a   Arnd Bergmann   llseek: automatic...
226
  	.llseek		= noop_llseek,
b215e2839   Davide Libenzi   signal/timer/even...
227
  };
4d672e7ac   Davide Libenzi   timerfd: new time...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  static struct file *timerfd_fget(int fd)
  {
  	struct file *file;
  
  	file = fget(fd);
  	if (!file)
  		return ERR_PTR(-EBADF);
  	if (file->f_op != &timerfd_fops) {
  		fput(file);
  		return ERR_PTR(-EINVAL);
  	}
  
  	return file;
  }
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
242
  SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
b215e2839   Davide Libenzi   signal/timer/even...
243
  {
2030a42ce   Al Viro   [PATCH] sanitize ...
244
  	int ufd;
b215e2839   Davide Libenzi   signal/timer/even...
245
  	struct timerfd_ctx *ctx;
b215e2839   Davide Libenzi   signal/timer/even...
246

e38b36f32   Ulrich Drepper   flag parameters: ...
247
248
249
  	/* Check the TFD_* constants for consistency.  */
  	BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  	BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
610d18f41   Davide Libenzi   timerfd: add flag...
250
251
252
  	if ((flags & ~TFD_CREATE_FLAGS) ||
  	    (clockid != CLOCK_MONOTONIC &&
  	     clockid != CLOCK_REALTIME))
b215e2839   Davide Libenzi   signal/timer/even...
253
  		return -EINVAL;
4d672e7ac   Davide Libenzi   timerfd: new time...
254
255
256
257
258
259
260
261
  
  	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
  
  	init_waitqueue_head(&ctx->wqh);
  	ctx->clockid = clockid;
  	hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
262
  	ctx->moffs = ktime_get_monotonic_offset();
4d672e7ac   Davide Libenzi   timerfd: new time...
263

11fcb6c14   Ulrich Drepper   flag parameters: ...
264
  	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
628ff7c1d   Roland Dreier   anonfd: Allow mak...
265
  			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
2030a42ce   Al Viro   [PATCH] sanitize ...
266
  	if (ufd < 0)
4d672e7ac   Davide Libenzi   timerfd: new time...
267
  		kfree(ctx);
4d672e7ac   Davide Libenzi   timerfd: new time...
268
269
270
  
  	return ufd;
  }
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
271
272
273
  SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  		const struct itimerspec __user *, utmr,
  		struct itimerspec __user *, otmr)
4d672e7ac   Davide Libenzi   timerfd: new time...
274
275
276
277
  {
  	struct file *file;
  	struct timerfd_ctx *ctx;
  	struct itimerspec ktmr, kotmr;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
278
  	int ret;
4d672e7ac   Davide Libenzi   timerfd: new time...
279
280
281
  
  	if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  		return -EFAULT;
610d18f41   Davide Libenzi   timerfd: add flag...
282
283
  	if ((flags & ~TFD_SETTIME_FLAGS) ||
  	    !timespec_valid(&ktmr.it_value) ||
b215e2839   Davide Libenzi   signal/timer/even...
284
285
  	    !timespec_valid(&ktmr.it_interval))
  		return -EINVAL;
4d672e7ac   Davide Libenzi   timerfd: new time...
286
287
288
289
  	file = timerfd_fget(ufd);
  	if (IS_ERR(file))
  		return PTR_ERR(file);
  	ctx = file->private_data;
b215e2839   Davide Libenzi   signal/timer/even...
290

9ec269075   Thomas Gleixner   timerfd: Manage c...
291
  	timerfd_setup_cancel(ctx, flags);
4d672e7ac   Davide Libenzi   timerfd: new time...
292
293
294
295
296
297
298
299
  	/*
  	 * We need to stop the existing timer before reprogramming
  	 * it to the new values.
  	 */
  	for (;;) {
  		spin_lock_irq(&ctx->wqh.lock);
  		if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  			break;
18963c01b   Davide Libenzi   timerfd use waitq...
300
  		spin_unlock_irq(&ctx->wqh.lock);
4d672e7ac   Davide Libenzi   timerfd: new time...
301
  		cpu_relax();
b215e2839   Davide Libenzi   signal/timer/even...
302
  	}
4d672e7ac   Davide Libenzi   timerfd: new time...
303
304
305
306
307
308
309
310
  	/*
  	 * 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.
  	 */
  	if (ctx->expired && ctx->tintv.tv64)
  		hrtimer_forward_now(&ctx->tmr, ctx->tintv);
b215e2839   Davide Libenzi   signal/timer/even...
311

4d672e7ac   Davide Libenzi   timerfd: new time...
312
313
314
315
316
317
  	kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  	kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  
  	/*
  	 * Re-program the timer to the new value ...
  	 */
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
318
  	ret = timerfd_setup(ctx, flags, &ktmr);
4d672e7ac   Davide Libenzi   timerfd: new time...
319
320
321
322
323
  
  	spin_unlock_irq(&ctx->wqh.lock);
  	fput(file);
  	if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  		return -EFAULT;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
324
  	return ret;
4d672e7ac   Davide Libenzi   timerfd: new time...
325
  }
d4e82042c   Heiko Carstens   [CVE-2009-0029] S...
326
  SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
4d672e7ac   Davide Libenzi   timerfd: new time...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  {
  	struct file *file;
  	struct timerfd_ctx *ctx;
  	struct itimerspec kotmr;
  
  	file = timerfd_fget(ufd);
  	if (IS_ERR(file))
  		return PTR_ERR(file);
  	ctx = file->private_data;
  
  	spin_lock_irq(&ctx->wqh.lock);
  	if (ctx->expired && ctx->tintv.tv64) {
  		ctx->expired = 0;
  		ctx->ticks +=
  			hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  		hrtimer_restart(&ctx->tmr);
  	}
  	kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  	kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  	spin_unlock_irq(&ctx->wqh.lock);
  	fput(file);
  
  	return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
b215e2839   Davide Libenzi   signal/timer/even...
350
  }