Blame view

fs/timerfd.c 13.6 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
b215e2839   Davide Libenzi   signal/timer/even...
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.
   *
   */
11ffa9d60   Todd Poynor   timerfd: Add alar...
11
  #include <linux/alarmtimer.h>
b215e2839   Davide Libenzi   signal/timer/even...
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>
5a0e3ad6a   Tejun Heo   include cleanup: ...
18
  #include <linux/slab.h>
b215e2839   Davide Libenzi   signal/timer/even...
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>
45cc2b96f   Adrian Bunk   fs/timerfd.c shou...
25
  #include <linux/syscalls.h>
9d94b9e2f   Al Viro   switch timerfd co...
26
  #include <linux/compat.h>
9ec269075   Thomas Gleixner   timerfd: Manage c...
27
  #include <linux/rcupdate.h>
6cd889d43   Andrei Vagin   timerfd: Make tim...
28
  #include <linux/time_namespace.h>
b215e2839   Davide Libenzi   signal/timer/even...
29
30
  
  struct timerfd_ctx {
11ffa9d60   Todd Poynor   timerfd: Add alar...
31
32
33
34
  	union {
  		struct hrtimer tmr;
  		struct alarm alarm;
  	} t;
b215e2839   Davide Libenzi   signal/timer/even...
35
  	ktime_t tintv;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
36
  	ktime_t moffs;
b215e2839   Davide Libenzi   signal/timer/even...
37
  	wait_queue_head_t wqh;
4d672e7ac   Davide Libenzi   timerfd: new time...
38
  	u64 ticks;
4d672e7ac   Davide Libenzi   timerfd: new time...
39
  	int clockid;
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
40
41
  	short unsigned expired;
  	short unsigned settime_flags;	/* to show in fdinfo */
9ec269075   Thomas Gleixner   timerfd: Manage c...
42
43
  	struct rcu_head rcu;
  	struct list_head clist;
1e38da300   Thomas Gleixner   timerfd: Protect ...
44
  	spinlock_t cancel_lock;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
45
  	bool might_cancel;
b215e2839   Davide Libenzi   signal/timer/even...
46
  };
9ec269075   Thomas Gleixner   timerfd: Manage c...
47
48
  static LIST_HEAD(cancel_list);
  static DEFINE_SPINLOCK(cancel_lock);
11ffa9d60   Todd Poynor   timerfd: Add alar...
49
50
51
52
53
  static inline bool isalarm(struct timerfd_ctx *ctx)
  {
  	return ctx->clockid == CLOCK_REALTIME_ALARM ||
  		ctx->clockid == CLOCK_BOOTTIME_ALARM;
  }
b215e2839   Davide Libenzi   signal/timer/even...
54
55
56
  /*
   * 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,
2456e8553   Thomas Gleixner   ktime: Get rid of...
57
   * tintv != 0) until the timer is accessed.
b215e2839   Davide Libenzi   signal/timer/even...
58
   */
11ffa9d60   Todd Poynor   timerfd: Add alar...
59
  static void timerfd_triggered(struct timerfd_ctx *ctx)
b215e2839   Davide Libenzi   signal/timer/even...
60
  {
b215e2839   Davide Libenzi   signal/timer/even...
61
  	unsigned long flags;
18963c01b   Davide Libenzi   timerfd use waitq...
62
  	spin_lock_irqsave(&ctx->wqh.lock, flags);
b215e2839   Davide Libenzi   signal/timer/even...
63
  	ctx->expired = 1;
4d672e7ac   Davide Libenzi   timerfd: new time...
64
  	ctx->ticks++;
7dda71281   Christoph Hellwig   timerfd: add supp...
65
  	wake_up_locked_poll(&ctx->wqh, EPOLLIN);
18963c01b   Davide Libenzi   timerfd use waitq...
66
  	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
11ffa9d60   Todd Poynor   timerfd: Add alar...
67
  }
b215e2839   Davide Libenzi   signal/timer/even...
68

11ffa9d60   Todd Poynor   timerfd: Add alar...
69
70
71
72
73
  static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  {
  	struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
  					       t.tmr);
  	timerfd_triggered(ctx);
b215e2839   Davide Libenzi   signal/timer/even...
74
75
  	return HRTIMER_NORESTART;
  }
11ffa9d60   Todd Poynor   timerfd: Add alar...
76
77
78
79
80
81
82
83
  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;
  }
9ec269075   Thomas Gleixner   timerfd: Manage c...
84
85
  /*
   * Called when the clock was set to cancel the timers in the cancel
1123d9396   Max Asbock   timerfd: Fix wake...
86
87
88
   * 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...
89
90
   */
  void timerfd_clock_was_set(void)
4d672e7ac   Davide Libenzi   timerfd: new time...
91
  {
2456e8553   Thomas Gleixner   ktime: Get rid of...
92
  	ktime_t moffs = ktime_mono_to_real(0);
9ec269075   Thomas Gleixner   timerfd: Manage c...
93
94
  	struct timerfd_ctx *ctx;
  	unsigned long flags;
4d672e7ac   Davide Libenzi   timerfd: new time...
95

9ec269075   Thomas Gleixner   timerfd: Manage c...
96
97
98
99
100
  	rcu_read_lock();
  	list_for_each_entry_rcu(ctx, &cancel_list, clist) {
  		if (!ctx->might_cancel)
  			continue;
  		spin_lock_irqsave(&ctx->wqh.lock, flags);
2456e8553   Thomas Gleixner   ktime: Get rid of...
101
102
  		if (ctx->moffs != moffs) {
  			ctx->moffs = KTIME_MAX;
1123d9396   Max Asbock   timerfd: Fix wake...
103
  			ctx->ticks++;
7dda71281   Christoph Hellwig   timerfd: add supp...
104
  			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
9ec269075   Thomas Gleixner   timerfd: Manage c...
105
106
107
108
  		}
  		spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  	}
  	rcu_read_unlock();
4d672e7ac   Davide Libenzi   timerfd: new time...
109
  }
1e38da300   Thomas Gleixner   timerfd: Protect ...
110
  static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
111
  {
9ec269075   Thomas Gleixner   timerfd: Manage c...
112
113
114
115
116
117
118
  	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...
119

1e38da300   Thomas Gleixner   timerfd: Protect ...
120
121
122
123
124
125
  static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
  {
  	spin_lock(&ctx->cancel_lock);
  	__timerfd_remove_cancel(ctx);
  	spin_unlock(&ctx->cancel_lock);
  }
9ec269075   Thomas Gleixner   timerfd: Manage c...
126
127
  static bool timerfd_canceled(struct timerfd_ctx *ctx)
  {
2456e8553   Thomas Gleixner   ktime: Get rid of...
128
  	if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
129
  		return false;
2456e8553   Thomas Gleixner   ktime: Get rid of...
130
  	ctx->moffs = ktime_mono_to_real(0);
9ec269075   Thomas Gleixner   timerfd: Manage c...
131
132
  	return true;
  }
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
133

9ec269075   Thomas Gleixner   timerfd: Manage c...
134
135
  static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  {
1e38da300   Thomas Gleixner   timerfd: Protect ...
136
  	spin_lock(&ctx->cancel_lock);
11ffa9d60   Todd Poynor   timerfd: Add alar...
137
138
139
  	if ((ctx->clockid == CLOCK_REALTIME ||
  	     ctx->clockid == CLOCK_REALTIME_ALARM) &&
  	    (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
9ec269075   Thomas Gleixner   timerfd: Manage c...
140
141
142
143
144
145
  		if (!ctx->might_cancel) {
  			ctx->might_cancel = true;
  			spin_lock(&cancel_lock);
  			list_add_rcu(&ctx->clist, &cancel_list);
  			spin_unlock(&cancel_lock);
  		}
1e38da300   Thomas Gleixner   timerfd: Protect ...
146
147
  	} else {
  		__timerfd_remove_cancel(ctx);
9ec269075   Thomas Gleixner   timerfd: Manage c...
148
  	}
1e38da300   Thomas Gleixner   timerfd: Protect ...
149
  	spin_unlock(&ctx->cancel_lock);
9ec269075   Thomas Gleixner   timerfd: Manage c...
150
  }
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
151

9ec269075   Thomas Gleixner   timerfd: Manage c...
152
153
154
  static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  {
  	ktime_t remaining;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
155

11ffa9d60   Todd Poynor   timerfd: Add alar...
156
157
158
  	if (isalarm(ctx))
  		remaining = alarm_expires_remaining(&ctx->t.alarm);
  	else
b62526ed1   Thomas Gleixner   timerfd: Handle r...
159
  		remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
11ffa9d60   Todd Poynor   timerfd: Add alar...
160

8b0e19531   Thomas Gleixner   ktime: Cleanup kt...
161
  	return remaining < 0 ? 0: remaining;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
162
163
164
  }
  
  static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
bff412036   Deepa Dinamani   timerfd: Use get_...
165
  			 const struct itimerspec64 *ktmr)
b215e2839   Davide Libenzi   signal/timer/even...
166
167
168
  {
  	enum hrtimer_mode htmode;
  	ktime_t texp;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
169
  	int clockid = ctx->clockid;
b215e2839   Davide Libenzi   signal/timer/even...
170
171
172
  
  	htmode = (flags & TFD_TIMER_ABSTIME) ?
  		HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
bff412036   Deepa Dinamani   timerfd: Use get_...
173
  	texp = timespec64_to_ktime(ktmr->it_value);
b215e2839   Davide Libenzi   signal/timer/even...
174
  	ctx->expired = 0;
4d672e7ac   Davide Libenzi   timerfd: new time...
175
  	ctx->ticks = 0;
bff412036   Deepa Dinamani   timerfd: Use get_...
176
  	ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
11ffa9d60   Todd Poynor   timerfd: Add alar...
177
178
179
180
181
182
183
184
185
186
187
  
  	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;
  	}
2456e8553   Thomas Gleixner   ktime: Get rid of...
188
  	if (texp != 0) {
6cd889d43   Andrei Vagin   timerfd: Make tim...
189
190
  		if (flags & TFD_TIMER_ABSTIME)
  			texp = timens_ktime_to_host(clockid, texp);
11ffa9d60   Todd Poynor   timerfd: Add alar...
191
192
193
194
195
196
197
198
  		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);
  		}
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
199
200
201
  		if (timerfd_canceled(ctx))
  			return -ECANCELED;
  	}
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
202
203
  
  	ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
204
  	return 0;
b215e2839   Davide Libenzi   signal/timer/even...
205
206
207
208
209
  }
  
  static int timerfd_release(struct inode *inode, struct file *file)
  {
  	struct timerfd_ctx *ctx = file->private_data;
9ec269075   Thomas Gleixner   timerfd: Manage c...
210
  	timerfd_remove_cancel(ctx);
11ffa9d60   Todd Poynor   timerfd: Add alar...
211
212
213
214
215
  
  	if (isalarm(ctx))
  		alarm_cancel(&ctx->t.alarm);
  	else
  		hrtimer_cancel(&ctx->t.tmr);
9ec269075   Thomas Gleixner   timerfd: Manage c...
216
  	kfree_rcu(ctx, rcu);
b215e2839   Davide Libenzi   signal/timer/even...
217
218
  	return 0;
  }
a11e1d432   Linus Torvalds   Revert changes to...
219
220
  
  static __poll_t timerfd_poll(struct file *file, poll_table *wait)
b215e2839   Davide Libenzi   signal/timer/even...
221
222
  {
  	struct timerfd_ctx *ctx = file->private_data;
a11e1d432   Linus Torvalds   Revert changes to...
223
224
  	__poll_t events = 0;
  	unsigned long flags;
b215e2839   Davide Libenzi   signal/timer/even...
225

a11e1d432   Linus Torvalds   Revert changes to...
226
  	poll_wait(file, &ctx->wqh, wait);
b215e2839   Davide Libenzi   signal/timer/even...
227

a11e1d432   Linus Torvalds   Revert changes to...
228
229
230
231
  	spin_lock_irqsave(&ctx->wqh.lock, flags);
  	if (ctx->ticks)
  		events |= EPOLLIN;
  	spin_unlock_irqrestore(&ctx->wqh.lock, flags);
b215e2839   Davide Libenzi   signal/timer/even...
232

a11e1d432   Linus Torvalds   Revert changes to...
233
  	return events;
b215e2839   Davide Libenzi   signal/timer/even...
234
235
236
237
238
239
240
  }
  
  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...
241
  	u64 ticks = 0;
b215e2839   Davide Libenzi   signal/timer/even...
242
243
244
  
  	if (count < sizeof(ticks))
  		return -EINVAL;
18963c01b   Davide Libenzi   timerfd use waitq...
245
  	spin_lock_irq(&ctx->wqh.lock);
8120a8aad   Michal Nazarewicz   fs/timerfd.c: mak...
246
247
248
249
  	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...
250

9ec269075   Thomas Gleixner   timerfd: Manage c...
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;
  	}
4d672e7ac   Davide Libenzi   timerfd: new time...
261
262
  	if (ctx->ticks) {
  		ticks = ctx->ticks;
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
263

2456e8553   Thomas Gleixner   ktime: Get rid of...
264
  		if (ctx->expired && ctx->tintv) {
b215e2839   Davide Libenzi   signal/timer/even...
265
  			/*
2456e8553   Thomas Gleixner   ktime: Get rid of...
266
  			 * If tintv != 0, this is a periodic timer that
b215e2839   Davide Libenzi   signal/timer/even...
267
268
269
270
  			 * needs to be re-armed. We avoid doing it in the timer
  			 * callback to avoid DoS attacks specifying a very
  			 * short timer period.
  			 */
11ffa9d60   Todd Poynor   timerfd: Add alar...
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);
  			}
4d672e7ac   Davide Libenzi   timerfd: new time...
280
281
282
  		}
  		ctx->expired = 0;
  		ctx->ticks = 0;
b215e2839   Davide Libenzi   signal/timer/even...
283
  	}
18963c01b   Davide Libenzi   timerfd use waitq...
284
  	spin_unlock_irq(&ctx->wqh.lock);
b215e2839   Davide Libenzi   signal/timer/even...
285
  	if (ticks)
098284020   Davide Libenzi   make timerfd retu...
286
  		res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
b215e2839   Davide Libenzi   signal/timer/even...
287
288
  	return res;
  }
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
289
  #ifdef CONFIG_PROC_FS
a3816ab0e   Joe Perches   fs: Convert show_...
290
  static void timerfd_show(struct seq_file *m, struct file *file)
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
291
292
  {
  	struct timerfd_ctx *ctx = file->private_data;
bde9e963a   Arnd Bergmann   y2038: timerfd: U...
293
  	struct timespec64 value, interval;
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
294
295
  
  	spin_lock_irq(&ctx->wqh.lock);
bde9e963a   Arnd Bergmann   y2038: timerfd: U...
296
297
  	value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  	interval = ktime_to_timespec64(ctx->tintv);
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
298
  	spin_unlock_irq(&ctx->wqh.lock);
a3816ab0e   Joe Perches   fs: Convert show_...
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  	seq_printf(m,
  		   "clockid: %d
  "
  		   "ticks: %llu
  "
  		   "settime flags: 0%o
  "
  		   "it_value: (%llu, %llu)
  "
  		   "it_interval: (%llu, %llu)
  ",
  		   ctx->clockid,
  		   (unsigned long long)ctx->ticks,
  		   ctx->settime_flags,
bde9e963a   Arnd Bergmann   y2038: timerfd: U...
313
314
315
316
  		   (unsigned long long)value.tv_sec,
  		   (unsigned long long)value.tv_nsec,
  		   (unsigned long long)interval.tv_sec,
  		   (unsigned long long)interval.tv_nsec);
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
317
318
319
320
  }
  #else
  #define timerfd_show NULL
  #endif
5442e9fbd   Cyrill Gorcunov   timerfd: Implemen...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  #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;
7dda71281   Christoph Hellwig   timerfd: add supp...
339
  			wake_up_locked_poll(&ctx->wqh, EPOLLIN);
5442e9fbd   Cyrill Gorcunov   timerfd: Implemen...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  		} else
  			ret = -ECANCELED;
  		spin_unlock_irq(&ctx->wqh.lock);
  		break;
  	}
  	default:
  		ret = -ENOTTY;
  		break;
  	}
  
  	return ret;
  }
  #else
  #define timerfd_ioctl NULL
  #endif
b215e2839   Davide Libenzi   signal/timer/even...
355
356
  static const struct file_operations timerfd_fops = {
  	.release	= timerfd_release,
a11e1d432   Linus Torvalds   Revert changes to...
357
  	.poll		= timerfd_poll,
b215e2839   Davide Libenzi   signal/timer/even...
358
  	.read		= timerfd_read,
6038f373a   Arnd Bergmann   llseek: automatic...
359
  	.llseek		= noop_llseek,
af9c4957c   Cyrill Gorcunov   timerfd: Implemen...
360
  	.show_fdinfo	= timerfd_show,
5442e9fbd   Cyrill Gorcunov   timerfd: Implemen...
361
  	.unlocked_ioctl	= timerfd_ioctl,
b215e2839   Davide Libenzi   signal/timer/even...
362
  };
2903ff019   Al Viro   switch simple cas...
363
  static int timerfd_fget(int fd, struct fd *p)
4d672e7ac   Davide Libenzi   timerfd: new time...
364
  {
2903ff019   Al Viro   switch simple cas...
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;
4d672e7ac   Davide Libenzi   timerfd: new time...
371
  	}
2903ff019   Al Viro   switch simple cas...
372
373
  	*p = f;
  	return 0;
4d672e7ac   Davide Libenzi   timerfd: new time...
374
  }
836f92adf   Heiko Carstens   [CVE-2009-0029] S...
375
  SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
b215e2839   Davide Libenzi   signal/timer/even...
376
  {
2030a42ce   Al Viro   [PATCH] sanitize ...
377
  	int ufd;
b215e2839   Davide Libenzi   signal/timer/even...
378
  	struct timerfd_ctx *ctx;
b215e2839   Davide Libenzi   signal/timer/even...
379

e38b36f32   Ulrich Drepper   flag parameters: ...
380
381
382
  	/* 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...
383
384
  	if ((flags & ~TFD_CREATE_FLAGS) ||
  	    (clockid != CLOCK_MONOTONIC &&
11ffa9d60   Todd Poynor   timerfd: Add alar...
385
386
  	     clockid != CLOCK_REALTIME &&
  	     clockid != CLOCK_REALTIME_ALARM &&
4a2378a94   Greg Hackmann   timerfd: support ...
387
  	     clockid != CLOCK_BOOTTIME &&
11ffa9d60   Todd Poynor   timerfd: Add alar...
388
  	     clockid != CLOCK_BOOTTIME_ALARM))
b215e2839   Davide Libenzi   signal/timer/even...
389
  		return -EINVAL;
4d672e7ac   Davide Libenzi   timerfd: new time...
390

25b68a8f0   Stephen Smalley   timerfd: Only che...
391
392
393
  	if ((clockid == CLOCK_REALTIME_ALARM ||
  	     clockid == CLOCK_BOOTTIME_ALARM) &&
  	    !capable(CAP_WAKE_ALARM))
2895a5e5b   Eric Caruso   timerfd: Reject A...
394
  		return -EPERM;
4d672e7ac   Davide Libenzi   timerfd: new time...
395
396
397
398
399
  	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
  
  	init_waitqueue_head(&ctx->wqh);
1e38da300   Thomas Gleixner   timerfd: Protect ...
400
  	spin_lock_init(&ctx->cancel_lock);
4d672e7ac   Davide Libenzi   timerfd: new time...
401
  	ctx->clockid = clockid;
11ffa9d60   Todd Poynor   timerfd: Add alar...
402
403
404
405
406
407
408
409
  
  	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);
2456e8553   Thomas Gleixner   ktime: Get rid of...
410
  	ctx->moffs = ktime_mono_to_real(0);
4d672e7ac   Davide Libenzi   timerfd: new time...
411

11fcb6c14   Ulrich Drepper   flag parameters: ...
412
  	ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
628ff7c1d   Roland Dreier   anonfd: Allow mak...
413
  			       O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
2030a42ce   Al Viro   [PATCH] sanitize ...
414
  	if (ufd < 0)
4d672e7ac   Davide Libenzi   timerfd: new time...
415
  		kfree(ctx);
4d672e7ac   Davide Libenzi   timerfd: new time...
416
417
418
  
  	return ufd;
  }
9d94b9e2f   Al Viro   switch timerfd co...
419
  static int do_timerfd_settime(int ufd, int flags, 
bff412036   Deepa Dinamani   timerfd: Use get_...
420
421
  		const struct itimerspec64 *new,
  		struct itimerspec64 *old)
4d672e7ac   Davide Libenzi   timerfd: new time...
422
  {
2903ff019   Al Viro   switch simple cas...
423
  	struct fd f;
4d672e7ac   Davide Libenzi   timerfd: new time...
424
  	struct timerfd_ctx *ctx;
2903ff019   Al Viro   switch simple cas...
425
  	int ret;
4d672e7ac   Davide Libenzi   timerfd: new time...
426

610d18f41   Davide Libenzi   timerfd: add flag...
427
  	if ((flags & ~TFD_SETTIME_FLAGS) ||
bff412036   Deepa Dinamani   timerfd: Use get_...
428
  		 !itimerspec64_valid(new))
b215e2839   Davide Libenzi   signal/timer/even...
429
  		return -EINVAL;
2903ff019   Al Viro   switch simple cas...
430
431
432
433
  	ret = timerfd_fget(ufd, &f);
  	if (ret)
  		return ret;
  	ctx = f.file->private_data;
b215e2839   Davide Libenzi   signal/timer/even...
434

25b68a8f0   Stephen Smalley   timerfd: Only che...
435
  	if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
2895a5e5b   Eric Caruso   timerfd: Reject A...
436
437
438
  		fdput(f);
  		return -EPERM;
  	}
9ec269075   Thomas Gleixner   timerfd: Manage c...
439
  	timerfd_setup_cancel(ctx, flags);
4d672e7ac   Davide Libenzi   timerfd: new time...
440
441
442
443
444
445
  	/*
  	 * We need to stop the existing timer before reprogramming
  	 * it to the new values.
  	 */
  	for (;;) {
  		spin_lock_irq(&ctx->wqh.lock);
11ffa9d60   Todd Poynor   timerfd: Add alar...
446
447
448
449
450
451
452
453
  
  		if (isalarm(ctx)) {
  			if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  				break;
  		} else {
  			if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  				break;
  		}
18963c01b   Davide Libenzi   timerfd use waitq...
454
  		spin_unlock_irq(&ctx->wqh.lock);
a125ecc16   Anna-Maria Gleixner   timerfd: Prepare ...
455
456
457
458
459
  
  		if (isalarm(ctx))
  			hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
  		else
  			hrtimer_cancel_wait_running(&ctx->t.tmr);
b215e2839   Davide Libenzi   signal/timer/even...
460
  	}
4d672e7ac   Davide Libenzi   timerfd: new time...
461
462
463
464
465
466
  	/*
  	 * 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.
  	 */
2456e8553   Thomas Gleixner   ktime: Get rid of...
467
  	if (ctx->expired && ctx->tintv) {
11ffa9d60   Todd Poynor   timerfd: Add alar...
468
469
470
471
472
  		if (isalarm(ctx))
  			alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  		else
  			hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  	}
b215e2839   Davide Libenzi   signal/timer/even...
473

bff412036   Deepa Dinamani   timerfd: Use get_...
474
475
  	old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  	old->it_interval = ktime_to_timespec64(ctx->tintv);
4d672e7ac   Davide Libenzi   timerfd: new time...
476
477
478
479
  
  	/*
  	 * Re-program the timer to the new value ...
  	 */
9d94b9e2f   Al Viro   switch timerfd co...
480
  	ret = timerfd_setup(ctx, flags, new);
4d672e7ac   Davide Libenzi   timerfd: new time...
481
482
  
  	spin_unlock_irq(&ctx->wqh.lock);
2903ff019   Al Viro   switch simple cas...
483
  	fdput(f);
99ee5315d   Thomas Gleixner   timerfd: Allow ti...
484
  	return ret;
4d672e7ac   Davide Libenzi   timerfd: new time...
485
  }
bff412036   Deepa Dinamani   timerfd: Use get_...
486
  static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
4d672e7ac   Davide Libenzi   timerfd: new time...
487
  {
2903ff019   Al Viro   switch simple cas...
488
  	struct fd f;
4d672e7ac   Davide Libenzi   timerfd: new time...
489
  	struct timerfd_ctx *ctx;
2903ff019   Al Viro   switch simple cas...
490
491
492
493
  	int ret = timerfd_fget(ufd, &f);
  	if (ret)
  		return ret;
  	ctx = f.file->private_data;
4d672e7ac   Davide Libenzi   timerfd: new time...
494
495
  
  	spin_lock_irq(&ctx->wqh.lock);
2456e8553   Thomas Gleixner   ktime: Get rid of...
496
  	if (ctx->expired && ctx->tintv) {
4d672e7ac   Davide Libenzi   timerfd: new time...
497
  		ctx->expired = 0;
11ffa9d60   Todd Poynor   timerfd: Add alar...
498
499
500
501
502
503
504
505
506
507
508
509
  
  		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);
  		}
4d672e7ac   Davide Libenzi   timerfd: new time...
510
  	}
bff412036   Deepa Dinamani   timerfd: Use get_...
511
512
  	t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  	t->it_interval = ktime_to_timespec64(ctx->tintv);
4d672e7ac   Davide Libenzi   timerfd: new time...
513
  	spin_unlock_irq(&ctx->wqh.lock);
2903ff019   Al Viro   switch simple cas...
514
  	fdput(f);
9d94b9e2f   Al Viro   switch timerfd co...
515
516
517
518
  	return 0;
  }
  
  SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
6ff847350   Deepa Dinamani   time: Change type...
519
520
  		const struct __kernel_itimerspec __user *, utmr,
  		struct __kernel_itimerspec __user *, otmr)
9d94b9e2f   Al Viro   switch timerfd co...
521
  {
bff412036   Deepa Dinamani   timerfd: Use get_...
522
  	struct itimerspec64 new, old;
9d94b9e2f   Al Viro   switch timerfd co...
523
  	int ret;
bff412036   Deepa Dinamani   timerfd: Use get_...
524
  	if (get_itimerspec64(&new, utmr))
9d94b9e2f   Al Viro   switch timerfd co...
525
526
527
528
  		return -EFAULT;
  	ret = do_timerfd_settime(ufd, flags, &new, &old);
  	if (ret)
  		return ret;
bff412036   Deepa Dinamani   timerfd: Use get_...
529
  	if (otmr && put_itimerspec64(&old, otmr))
9d94b9e2f   Al Viro   switch timerfd co...
530
531
532
533
  		return -EFAULT;
  
  	return ret;
  }
4d672e7ac   Davide Libenzi   timerfd: new time...
534

6ff847350   Deepa Dinamani   time: Change type...
535
  SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
9d94b9e2f   Al Viro   switch timerfd co...
536
  {
bff412036   Deepa Dinamani   timerfd: Use get_...
537
  	struct itimerspec64 kotmr;
9d94b9e2f   Al Viro   switch timerfd co...
538
539
540
  	int ret = do_timerfd_gettime(ufd, &kotmr);
  	if (ret)
  		return ret;
bff412036   Deepa Dinamani   timerfd: Use get_...
541
  	return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
b215e2839   Davide Libenzi   signal/timer/even...
542
  }
6ff847350   Deepa Dinamani   time: Change type...
543
  #ifdef CONFIG_COMPAT_32BIT_TIME
8dabe7245   Arnd Bergmann   y2038: syscalls: ...
544
  SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
9afc5eee6   Arnd Bergmann   y2038: globally r...
545
546
  		const struct old_itimerspec32 __user *, utmr,
  		struct old_itimerspec32 __user *, otmr)
9d94b9e2f   Al Viro   switch timerfd co...
547
  {
bff412036   Deepa Dinamani   timerfd: Use get_...
548
  	struct itimerspec64 new, old;
9d94b9e2f   Al Viro   switch timerfd co...
549
  	int ret;
9afc5eee6   Arnd Bergmann   y2038: globally r...
550
  	if (get_old_itimerspec32(&new, utmr))
9d94b9e2f   Al Viro   switch timerfd co...
551
552
553
554
  		return -EFAULT;
  	ret = do_timerfd_settime(ufd, flags, &new, &old);
  	if (ret)
  		return ret;
9afc5eee6   Arnd Bergmann   y2038: globally r...
555
  	if (otmr && put_old_itimerspec32(&old, otmr))
9d94b9e2f   Al Viro   switch timerfd co...
556
557
558
  		return -EFAULT;
  	return ret;
  }
8dabe7245   Arnd Bergmann   y2038: syscalls: ...
559
  SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
9afc5eee6   Arnd Bergmann   y2038: globally r...
560
  		struct old_itimerspec32 __user *, otmr)
9d94b9e2f   Al Viro   switch timerfd co...
561
  {
bff412036   Deepa Dinamani   timerfd: Use get_...
562
  	struct itimerspec64 kotmr;
9d94b9e2f   Al Viro   switch timerfd co...
563
564
565
  	int ret = do_timerfd_gettime(ufd, &kotmr);
  	if (ret)
  		return ret;
9afc5eee6   Arnd Bergmann   y2038: globally r...
566
  	return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
9d94b9e2f   Al Viro   switch timerfd co...
567
568
  }
  #endif