]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/timerfd.c
switch timerfd_[sg]ettime(2) to fget_light()
[mirror_ubuntu-artful-kernel.git] / fs / timerfd.c
CommitLineData
b215e283
DL
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>
5a0e3ad6 17#include <linux/slab.h>
b215e283
DL
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>
45cc2b96 24#include <linux/syscalls.h>
9ec26907 25#include <linux/rcupdate.h>
b215e283
DL
26
27struct timerfd_ctx {
28 struct hrtimer tmr;
29 ktime_t tintv;
99ee5315 30 ktime_t moffs;
b215e283 31 wait_queue_head_t wqh;
4d672e7a 32 u64 ticks;
b215e283 33 int expired;
4d672e7a 34 int clockid;
9ec26907
TG
35 struct rcu_head rcu;
36 struct list_head clist;
99ee5315 37 bool might_cancel;
b215e283
DL
38};
39
9ec26907
TG
40static LIST_HEAD(cancel_list);
41static DEFINE_SPINLOCK(cancel_lock);
42
b215e283
DL
43/*
44 * This gets called when the timer event triggers. We set the "expired"
45 * flag, but we do not re-arm the timer (in case it's necessary,
4d672e7a 46 * tintv.tv64 != 0) until the timer is accessed.
b215e283
DL
47 */
48static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
49{
50 struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
51 unsigned long flags;
52
18963c01 53 spin_lock_irqsave(&ctx->wqh.lock, flags);
b215e283 54 ctx->expired = 1;
4d672e7a 55 ctx->ticks++;
b215e283 56 wake_up_locked(&ctx->wqh);
18963c01 57 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
b215e283
DL
58
59 return HRTIMER_NORESTART;
60}
61
9ec26907
TG
62/*
63 * Called when the clock was set to cancel the timers in the cancel
1123d939
MA
64 * list. This will wake up processes waiting on these timers. The
65 * wake-up requires ctx->ticks to be non zero, therefore we increment
66 * it before calling wake_up_locked().
9ec26907
TG
67 */
68void timerfd_clock_was_set(void)
4d672e7a 69{
9ec26907
TG
70 ktime_t moffs = ktime_get_monotonic_offset();
71 struct timerfd_ctx *ctx;
72 unsigned long flags;
4d672e7a 73
9ec26907
TG
74 rcu_read_lock();
75 list_for_each_entry_rcu(ctx, &cancel_list, clist) {
76 if (!ctx->might_cancel)
77 continue;
78 spin_lock_irqsave(&ctx->wqh.lock, flags);
79 if (ctx->moffs.tv64 != moffs.tv64) {
80 ctx->moffs.tv64 = KTIME_MAX;
1123d939 81 ctx->ticks++;
9ec26907
TG
82 wake_up_locked(&ctx->wqh);
83 }
84 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
85 }
86 rcu_read_unlock();
4d672e7a
DL
87}
88
9ec26907 89static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
99ee5315 90{
9ec26907
TG
91 if (ctx->might_cancel) {
92 ctx->might_cancel = false;
93 spin_lock(&cancel_lock);
94 list_del_rcu(&ctx->clist);
95 spin_unlock(&cancel_lock);
96 }
97}
99ee5315 98
9ec26907
TG
99static bool timerfd_canceled(struct timerfd_ctx *ctx)
100{
101 if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
99ee5315 102 return false;
9ec26907
TG
103 ctx->moffs = ktime_get_monotonic_offset();
104 return true;
105}
99ee5315 106
9ec26907
TG
107static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
108{
109 if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
110 (flags & TFD_TIMER_CANCEL_ON_SET)) {
111 if (!ctx->might_cancel) {
112 ctx->might_cancel = true;
113 spin_lock(&cancel_lock);
114 list_add_rcu(&ctx->clist, &cancel_list);
115 spin_unlock(&cancel_lock);
116 }
117 } else if (ctx->might_cancel) {
118 timerfd_remove_cancel(ctx);
119 }
120}
99ee5315 121
9ec26907
TG
122static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
123{
124 ktime_t remaining;
99ee5315 125
9ec26907
TG
126 remaining = hrtimer_expires_remaining(&ctx->tmr);
127 return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
99ee5315
TG
128}
129
130static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
131 const struct itimerspec *ktmr)
b215e283
DL
132{
133 enum hrtimer_mode htmode;
134 ktime_t texp;
99ee5315 135 int clockid = ctx->clockid;
b215e283
DL
136
137 htmode = (flags & TFD_TIMER_ABSTIME) ?
138 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
139
140 texp = timespec_to_ktime(ktmr->it_value);
141 ctx->expired = 0;
4d672e7a 142 ctx->ticks = 0;
b215e283 143 ctx->tintv = timespec_to_ktime(ktmr->it_interval);
99ee5315 144 hrtimer_init(&ctx->tmr, clockid, htmode);
76369470 145 hrtimer_set_expires(&ctx->tmr, texp);
b215e283 146 ctx->tmr.function = timerfd_tmrproc;
99ee5315 147 if (texp.tv64 != 0) {
b215e283 148 hrtimer_start(&ctx->tmr, texp, htmode);
99ee5315
TG
149 if (timerfd_canceled(ctx))
150 return -ECANCELED;
151 }
152 return 0;
b215e283
DL
153}
154
155static int timerfd_release(struct inode *inode, struct file *file)
156{
157 struct timerfd_ctx *ctx = file->private_data;
158
9ec26907 159 timerfd_remove_cancel(ctx);
b215e283 160 hrtimer_cancel(&ctx->tmr);
9ec26907 161 kfree_rcu(ctx, rcu);
b215e283
DL
162 return 0;
163}
164
165static unsigned int timerfd_poll(struct file *file, poll_table *wait)
166{
167 struct timerfd_ctx *ctx = file->private_data;
168 unsigned int events = 0;
169 unsigned long flags;
170
171 poll_wait(file, &ctx->wqh, wait);
172
18963c01 173 spin_lock_irqsave(&ctx->wqh.lock, flags);
4d672e7a 174 if (ctx->ticks)
b215e283 175 events |= POLLIN;
18963c01 176 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
b215e283
DL
177
178 return events;
179}
180
181static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
182 loff_t *ppos)
183{
184 struct timerfd_ctx *ctx = file->private_data;
185 ssize_t res;
09828402 186 u64 ticks = 0;
b215e283
DL
187
188 if (count < sizeof(ticks))
189 return -EINVAL;
18963c01 190 spin_lock_irq(&ctx->wqh.lock);
8120a8aa
MN
191 if (file->f_flags & O_NONBLOCK)
192 res = -EAGAIN;
193 else
194 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
99ee5315 195
9ec26907
TG
196 /*
197 * If clock has changed, we do not care about the
198 * ticks and we do not rearm the timer. Userspace must
199 * reevaluate anyway.
200 */
201 if (timerfd_canceled(ctx)) {
202 ctx->ticks = 0;
203 ctx->expired = 0;
204 res = -ECANCELED;
205 }
206
4d672e7a
DL
207 if (ctx->ticks) {
208 ticks = ctx->ticks;
99ee5315 209
4d672e7a 210 if (ctx->expired && ctx->tintv.tv64) {
b215e283
DL
211 /*
212 * If tintv.tv64 != 0, this is a periodic timer that
213 * needs to be re-armed. We avoid doing it in the timer
214 * callback to avoid DoS attacks specifying a very
215 * short timer period.
216 */
4d672e7a
DL
217 ticks += hrtimer_forward_now(&ctx->tmr,
218 ctx->tintv) - 1;
b215e283 219 hrtimer_restart(&ctx->tmr);
4d672e7a
DL
220 }
221 ctx->expired = 0;
222 ctx->ticks = 0;
b215e283 223 }
18963c01 224 spin_unlock_irq(&ctx->wqh.lock);
b215e283 225 if (ticks)
09828402 226 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
b215e283
DL
227 return res;
228}
229
230static const struct file_operations timerfd_fops = {
231 .release = timerfd_release,
232 .poll = timerfd_poll,
233 .read = timerfd_read,
6038f373 234 .llseek = noop_llseek,
b215e283
DL
235};
236
4109633f 237static struct file *timerfd_fget(int fd, int *fput_needed)
4d672e7a
DL
238{
239 struct file *file;
240
4109633f 241 file = fget_light(fd, fput_needed);
4d672e7a
DL
242 if (!file)
243 return ERR_PTR(-EBADF);
244 if (file->f_op != &timerfd_fops) {
4109633f 245 fput_light(file, *fput_needed);
4d672e7a
DL
246 return ERR_PTR(-EINVAL);
247 }
248
249 return file;
250}
251
836f92ad 252SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
b215e283 253{
2030a42c 254 int ufd;
b215e283 255 struct timerfd_ctx *ctx;
b215e283 256
e38b36f3
UD
257 /* Check the TFD_* constants for consistency. */
258 BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
259 BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
260
610d18f4
DL
261 if ((flags & ~TFD_CREATE_FLAGS) ||
262 (clockid != CLOCK_MONOTONIC &&
263 clockid != CLOCK_REALTIME))
b215e283 264 return -EINVAL;
4d672e7a
DL
265
266 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
267 if (!ctx)
268 return -ENOMEM;
269
270 init_waitqueue_head(&ctx->wqh);
271 ctx->clockid = clockid;
272 hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
99ee5315 273 ctx->moffs = ktime_get_monotonic_offset();
4d672e7a 274
11fcb6c1 275 ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
628ff7c1 276 O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
2030a42c 277 if (ufd < 0)
4d672e7a 278 kfree(ctx);
4d672e7a
DL
279
280 return ufd;
281}
282
836f92ad
HC
283SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
284 const struct itimerspec __user *, utmr,
285 struct itimerspec __user *, otmr)
4d672e7a
DL
286{
287 struct file *file;
288 struct timerfd_ctx *ctx;
289 struct itimerspec ktmr, kotmr;
4109633f 290 int ret, fput_needed;
4d672e7a
DL
291
292 if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
293 return -EFAULT;
294
610d18f4
DL
295 if ((flags & ~TFD_SETTIME_FLAGS) ||
296 !timespec_valid(&ktmr.it_value) ||
b215e283
DL
297 !timespec_valid(&ktmr.it_interval))
298 return -EINVAL;
299
4109633f 300 file = timerfd_fget(ufd, &fput_needed);
4d672e7a
DL
301 if (IS_ERR(file))
302 return PTR_ERR(file);
303 ctx = file->private_data;
b215e283 304
9ec26907
TG
305 timerfd_setup_cancel(ctx, flags);
306
4d672e7a
DL
307 /*
308 * We need to stop the existing timer before reprogramming
309 * it to the new values.
310 */
311 for (;;) {
312 spin_lock_irq(&ctx->wqh.lock);
313 if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
314 break;
18963c01 315 spin_unlock_irq(&ctx->wqh.lock);
4d672e7a 316 cpu_relax();
b215e283
DL
317 }
318
4d672e7a
DL
319 /*
320 * If the timer is expired and it's periodic, we need to advance it
321 * because the caller may want to know the previous expiration time.
322 * We do not update "ticks" and "expired" since the timer will be
323 * re-programmed again in the following timerfd_setup() call.
324 */
325 if (ctx->expired && ctx->tintv.tv64)
326 hrtimer_forward_now(&ctx->tmr, ctx->tintv);
b215e283 327
4d672e7a
DL
328 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
329 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
330
331 /*
332 * Re-program the timer to the new value ...
333 */
99ee5315 334 ret = timerfd_setup(ctx, flags, &ktmr);
4d672e7a
DL
335
336 spin_unlock_irq(&ctx->wqh.lock);
4109633f 337 fput_light(file, fput_needed);
4d672e7a
DL
338 if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
339 return -EFAULT;
340
99ee5315 341 return ret;
4d672e7a
DL
342}
343
d4e82042 344SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
4d672e7a
DL
345{
346 struct file *file;
347 struct timerfd_ctx *ctx;
348 struct itimerspec kotmr;
4109633f 349 int fput_needed;
4d672e7a 350
4109633f 351 file = timerfd_fget(ufd, &fput_needed);
4d672e7a
DL
352 if (IS_ERR(file))
353 return PTR_ERR(file);
354 ctx = file->private_data;
355
356 spin_lock_irq(&ctx->wqh.lock);
357 if (ctx->expired && ctx->tintv.tv64) {
358 ctx->expired = 0;
359 ctx->ticks +=
360 hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
361 hrtimer_restart(&ctx->tmr);
362 }
363 kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
364 kotmr.it_interval = ktime_to_timespec(ctx->tintv);
365 spin_unlock_irq(&ctx->wqh.lock);
4109633f 366 fput_light(file, fput_needed);
4d672e7a
DL
367
368 return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
b215e283
DL
369}
370