OSDN Git Service

f2fs: Add a small clarification to CONFIG_FS_F2FS_FS_SECURITY
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / fs / timerfd.c
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/alarmtimer.h>
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>
18 #include <linux/slab.h>
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>
25 #include <linux/syscalls.h>
26 #include <linux/compat.h>
27 #include <linux/rcupdate.h>
28
29 struct timerfd_ctx {
30         union {
31                 struct hrtimer tmr;
32                 struct alarm alarm;
33         } t;
34         ktime_t tintv;
35         ktime_t moffs;
36         wait_queue_head_t wqh;
37         u64 ticks;
38         int clockid;
39         short unsigned expired;
40         short unsigned settime_flags;   /* to show in fdinfo */
41         struct rcu_head rcu;
42         struct list_head clist;
43         spinlock_t cancel_lock;
44         bool might_cancel;
45 };
46
47 static atomic_t instance_count = ATOMIC_INIT(0);
48
49 static LIST_HEAD(cancel_list);
50 static DEFINE_SPINLOCK(cancel_lock);
51
52 static inline bool isalarm(struct timerfd_ctx *ctx)
53 {
54         return ctx->clockid == CLOCK_REALTIME_ALARM ||
55                 ctx->clockid == CLOCK_BOOTTIME_ALARM;
56 }
57
58 /*
59  * This gets called when the timer event triggers. We set the "expired"
60  * flag, but we do not re-arm the timer (in case it's necessary,
61  * tintv.tv64 != 0) until the timer is accessed.
62  */
63 static void timerfd_triggered(struct timerfd_ctx *ctx)
64 {
65         unsigned long flags;
66
67         spin_lock_irqsave(&ctx->wqh.lock, flags);
68         ctx->expired = 1;
69         ctx->ticks++;
70         wake_up_locked(&ctx->wqh);
71         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
72 }
73
74 static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
75 {
76         struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
77                                                t.tmr);
78         timerfd_triggered(ctx);
79         return HRTIMER_NORESTART;
80 }
81
82 static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
83         ktime_t now)
84 {
85         struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
86                                                t.alarm);
87         timerfd_triggered(ctx);
88         return ALARMTIMER_NORESTART;
89 }
90
91 /*
92  * Called when the clock was set to cancel the timers in the cancel
93  * list. This will wake up processes waiting on these timers. The
94  * wake-up requires ctx->ticks to be non zero, therefore we increment
95  * it before calling wake_up_locked().
96  */
97 void timerfd_clock_was_set(void)
98 {
99         ktime_t moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
100         struct timerfd_ctx *ctx;
101         unsigned long flags;
102
103         rcu_read_lock();
104         list_for_each_entry_rcu(ctx, &cancel_list, clist) {
105                 if (!ctx->might_cancel)
106                         continue;
107                 spin_lock_irqsave(&ctx->wqh.lock, flags);
108                 if (ctx->moffs.tv64 != moffs.tv64) {
109                         ctx->moffs.tv64 = KTIME_MAX;
110                         ctx->ticks++;
111                         wake_up_locked(&ctx->wqh);
112                 }
113                 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
114         }
115         rcu_read_unlock();
116 }
117
118 static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
119 {
120         if (ctx->might_cancel) {
121                 ctx->might_cancel = false;
122                 spin_lock(&cancel_lock);
123                 list_del_rcu(&ctx->clist);
124                 spin_unlock(&cancel_lock);
125         }
126 }
127
128 static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
129 {
130         spin_lock(&ctx->cancel_lock);
131         __timerfd_remove_cancel(ctx);
132         spin_unlock(&ctx->cancel_lock);
133 }
134
135 static bool timerfd_canceled(struct timerfd_ctx *ctx)
136 {
137         if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
138                 return false;
139         ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
140         return true;
141 }
142
143 static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
144 {
145         spin_lock(&ctx->cancel_lock);
146         if ((ctx->clockid == CLOCK_REALTIME ||
147              ctx->clockid == CLOCK_REALTIME_ALARM) &&
148             (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
149                 if (!ctx->might_cancel) {
150                         ctx->might_cancel = true;
151                         spin_lock(&cancel_lock);
152                         list_add_rcu(&ctx->clist, &cancel_list);
153                         spin_unlock(&cancel_lock);
154                 }
155         } else {
156                 __timerfd_remove_cancel(ctx);
157         }
158         spin_unlock(&ctx->cancel_lock);
159 }
160
161 static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
162 {
163         ktime_t remaining;
164
165         if (isalarm(ctx))
166                 remaining = alarm_expires_remaining(&ctx->t.alarm);
167         else
168                 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
169
170         return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
171 }
172
173 static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
174                          const struct itimerspec *ktmr)
175 {
176         enum hrtimer_mode htmode;
177         ktime_t texp;
178         int clockid = ctx->clockid;
179
180         htmode = (flags & TFD_TIMER_ABSTIME) ?
181                 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
182
183         texp = timespec_to_ktime(ktmr->it_value);
184         ctx->expired = 0;
185         ctx->ticks = 0;
186         ctx->tintv = timespec_to_ktime(ktmr->it_interval);
187
188         if (isalarm(ctx)) {
189                 alarm_init(&ctx->t.alarm,
190                            ctx->clockid == CLOCK_REALTIME_ALARM ?
191                            ALARM_REALTIME : ALARM_BOOTTIME,
192                            timerfd_alarmproc);
193         } else {
194                 hrtimer_init(&ctx->t.tmr, clockid, htmode);
195                 hrtimer_set_expires(&ctx->t.tmr, texp);
196                 ctx->t.tmr.function = timerfd_tmrproc;
197         }
198
199         if (texp.tv64 != 0) {
200                 if (isalarm(ctx)) {
201                         if (flags & TFD_TIMER_ABSTIME)
202                                 alarm_start(&ctx->t.alarm, texp);
203                         else
204                                 alarm_start_relative(&ctx->t.alarm, texp);
205                 } else {
206                         hrtimer_start(&ctx->t.tmr, texp, htmode);
207                 }
208
209                 if (timerfd_canceled(ctx))
210                         return -ECANCELED;
211         }
212
213         ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
214         return 0;
215 }
216
217 static int timerfd_release(struct inode *inode, struct file *file)
218 {
219         struct timerfd_ctx *ctx = file->private_data;
220
221         timerfd_remove_cancel(ctx);
222
223         if (isalarm(ctx))
224                 alarm_cancel(&ctx->t.alarm);
225         else
226                 hrtimer_cancel(&ctx->t.tmr);
227         kfree_rcu(ctx, rcu);
228         return 0;
229 }
230
231 static unsigned int timerfd_poll(struct file *file, poll_table *wait)
232 {
233         struct timerfd_ctx *ctx = file->private_data;
234         unsigned int events = 0;
235         unsigned long flags;
236
237         poll_wait(file, &ctx->wqh, wait);
238
239         spin_lock_irqsave(&ctx->wqh.lock, flags);
240         if (ctx->ticks)
241                 events |= POLLIN;
242         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
243
244         return events;
245 }
246
247 static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
248                             loff_t *ppos)
249 {
250         struct timerfd_ctx *ctx = file->private_data;
251         ssize_t res;
252         u64 ticks = 0;
253
254         if (count < sizeof(ticks))
255                 return -EINVAL;
256         spin_lock_irq(&ctx->wqh.lock);
257         if (file->f_flags & O_NONBLOCK)
258                 res = -EAGAIN;
259         else
260                 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
261
262         /*
263          * If clock has changed, we do not care about the
264          * ticks and we do not rearm the timer. Userspace must
265          * reevaluate anyway.
266          */
267         if (timerfd_canceled(ctx)) {
268                 ctx->ticks = 0;
269                 ctx->expired = 0;
270                 res = -ECANCELED;
271         }
272
273         if (ctx->ticks) {
274                 ticks = ctx->ticks;
275
276                 if (ctx->expired && ctx->tintv.tv64) {
277                         /*
278                          * If tintv.tv64 != 0, this is a periodic timer that
279                          * needs to be re-armed. We avoid doing it in the timer
280                          * callback to avoid DoS attacks specifying a very
281                          * short timer period.
282                          */
283                         if (isalarm(ctx)) {
284                                 ticks += alarm_forward_now(
285                                         &ctx->t.alarm, ctx->tintv) - 1;
286                                 alarm_restart(&ctx->t.alarm);
287                         } else {
288                                 ticks += hrtimer_forward_now(&ctx->t.tmr,
289                                                              ctx->tintv) - 1;
290                                 hrtimer_restart(&ctx->t.tmr);
291                         }
292                 }
293                 ctx->expired = 0;
294                 ctx->ticks = 0;
295         }
296         spin_unlock_irq(&ctx->wqh.lock);
297         if (ticks)
298                 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
299         return res;
300 }
301
302 #ifdef CONFIG_PROC_FS
303 static void timerfd_show(struct seq_file *m, struct file *file)
304 {
305         struct timerfd_ctx *ctx = file->private_data;
306         struct itimerspec t;
307
308         spin_lock_irq(&ctx->wqh.lock);
309         t.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
310         t.it_interval = ktime_to_timespec(ctx->tintv);
311         spin_unlock_irq(&ctx->wqh.lock);
312
313         seq_printf(m,
314                    "clockid: %d\n"
315                    "ticks: %llu\n"
316                    "settime flags: 0%o\n"
317                    "it_value: (%llu, %llu)\n"
318                    "it_interval: (%llu, %llu)\n",
319                    ctx->clockid,
320                    (unsigned long long)ctx->ticks,
321                    ctx->settime_flags,
322                    (unsigned long long)t.it_value.tv_sec,
323                    (unsigned long long)t.it_value.tv_nsec,
324                    (unsigned long long)t.it_interval.tv_sec,
325                    (unsigned long long)t.it_interval.tv_nsec);
326 }
327 #else
328 #define timerfd_show NULL
329 #endif
330
331 #ifdef CONFIG_CHECKPOINT_RESTORE
332 static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
333 {
334         struct timerfd_ctx *ctx = file->private_data;
335         int ret = 0;
336
337         switch (cmd) {
338         case TFD_IOC_SET_TICKS: {
339                 u64 ticks;
340
341                 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
342                         return -EFAULT;
343                 if (!ticks)
344                         return -EINVAL;
345
346                 spin_lock_irq(&ctx->wqh.lock);
347                 if (!timerfd_canceled(ctx)) {
348                         ctx->ticks = ticks;
349                         wake_up_locked(&ctx->wqh);
350                 } else
351                         ret = -ECANCELED;
352                 spin_unlock_irq(&ctx->wqh.lock);
353                 break;
354         }
355         default:
356                 ret = -ENOTTY;
357                 break;
358         }
359
360         return ret;
361 }
362 #else
363 #define timerfd_ioctl NULL
364 #endif
365
366 static const struct file_operations timerfd_fops = {
367         .release        = timerfd_release,
368         .poll           = timerfd_poll,
369         .read           = timerfd_read,
370         .llseek         = noop_llseek,
371         .show_fdinfo    = timerfd_show,
372         .unlocked_ioctl = timerfd_ioctl,
373 };
374
375 static int timerfd_fget(int fd, struct fd *p)
376 {
377         struct fd f = fdget(fd);
378         if (!f.file)
379                 return -EBADF;
380         if (f.file->f_op != &timerfd_fops) {
381                 fdput(f);
382                 return -EINVAL;
383         }
384         *p = f;
385         return 0;
386 }
387
388 SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
389 {
390         int ufd;
391         struct timerfd_ctx *ctx;
392         char task_comm_buf[TASK_COMM_LEN];
393         char file_name_buf[32];
394         int instance;
395
396         /* Check the TFD_* constants for consistency.  */
397         BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
398         BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
399
400         if ((flags & ~TFD_CREATE_FLAGS) ||
401             (clockid != CLOCK_MONOTONIC &&
402              clockid != CLOCK_REALTIME &&
403              clockid != CLOCK_REALTIME_ALARM &&
404              clockid != CLOCK_BOOTTIME &&
405              clockid != CLOCK_BOOTTIME_ALARM))
406                 return -EINVAL;
407
408         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
409         if (!ctx)
410                 return -ENOMEM;
411
412         init_waitqueue_head(&ctx->wqh);
413         spin_lock_init(&ctx->cancel_lock);
414         ctx->clockid = clockid;
415
416         if (isalarm(ctx))
417                 alarm_init(&ctx->t.alarm,
418                            ctx->clockid == CLOCK_REALTIME_ALARM ?
419                            ALARM_REALTIME : ALARM_BOOTTIME,
420                            timerfd_alarmproc);
421         else
422                 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
423
424         ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
425
426         instance = atomic_inc_return(&instance_count);
427         get_task_comm(task_comm_buf, current);
428         snprintf(file_name_buf, sizeof(file_name_buf), "[timerfd%d_%.*s]",
429                  instance, (int)sizeof(task_comm_buf), task_comm_buf);
430
431         ufd = anon_inode_getfd(file_name_buf, &timerfd_fops, ctx,
432                                O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
433         if (ufd < 0)
434                 kfree(ctx);
435
436         return ufd;
437 }
438
439 static int do_timerfd_settime(int ufd, int flags, 
440                 const struct itimerspec *new,
441                 struct itimerspec *old)
442 {
443         struct fd f;
444         struct timerfd_ctx *ctx;
445         int ret;
446
447         if ((flags & ~TFD_SETTIME_FLAGS) ||
448             !timespec_valid(&new->it_value) ||
449             !timespec_valid(&new->it_interval))
450                 return -EINVAL;
451
452         ret = timerfd_fget(ufd, &f);
453         if (ret)
454                 return ret;
455         ctx = f.file->private_data;
456
457         timerfd_setup_cancel(ctx, flags);
458
459         /*
460          * We need to stop the existing timer before reprogramming
461          * it to the new values.
462          */
463         for (;;) {
464                 spin_lock_irq(&ctx->wqh.lock);
465
466                 if (isalarm(ctx)) {
467                         if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
468                                 break;
469                 } else {
470                         if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
471                                 break;
472                 }
473                 spin_unlock_irq(&ctx->wqh.lock);
474                 cpu_relax();
475         }
476
477         /*
478          * If the timer is expired and it's periodic, we need to advance it
479          * because the caller may want to know the previous expiration time.
480          * We do not update "ticks" and "expired" since the timer will be
481          * re-programmed again in the following timerfd_setup() call.
482          */
483         if (ctx->expired && ctx->tintv.tv64) {
484                 if (isalarm(ctx))
485                         alarm_forward_now(&ctx->t.alarm, ctx->tintv);
486                 else
487                         hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
488         }
489
490         old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
491         old->it_interval = ktime_to_timespec(ctx->tintv);
492
493         /*
494          * Re-program the timer to the new value ...
495          */
496         ret = timerfd_setup(ctx, flags, new);
497
498         spin_unlock_irq(&ctx->wqh.lock);
499         fdput(f);
500         return ret;
501 }
502
503 static int do_timerfd_gettime(int ufd, struct itimerspec *t)
504 {
505         struct fd f;
506         struct timerfd_ctx *ctx;
507         int ret = timerfd_fget(ufd, &f);
508         if (ret)
509                 return ret;
510         ctx = f.file->private_data;
511
512         spin_lock_irq(&ctx->wqh.lock);
513         if (ctx->expired && ctx->tintv.tv64) {
514                 ctx->expired = 0;
515
516                 if (isalarm(ctx)) {
517                         ctx->ticks +=
518                                 alarm_forward_now(
519                                         &ctx->t.alarm, ctx->tintv) - 1;
520                         alarm_restart(&ctx->t.alarm);
521                 } else {
522                         ctx->ticks +=
523                                 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
524                                 - 1;
525                         hrtimer_restart(&ctx->t.tmr);
526                 }
527         }
528         t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
529         t->it_interval = ktime_to_timespec(ctx->tintv);
530         spin_unlock_irq(&ctx->wqh.lock);
531         fdput(f);
532         return 0;
533 }
534
535 SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
536                 const struct itimerspec __user *, utmr,
537                 struct itimerspec __user *, otmr)
538 {
539         struct itimerspec new, old;
540         int ret;
541
542         if (copy_from_user(&new, utmr, sizeof(new)))
543                 return -EFAULT;
544         ret = do_timerfd_settime(ufd, flags, &new, &old);
545         if (ret)
546                 return ret;
547         if (otmr && copy_to_user(otmr, &old, sizeof(old)))
548                 return -EFAULT;
549
550         return ret;
551 }
552
553 SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
554 {
555         struct itimerspec kotmr;
556         int ret = do_timerfd_gettime(ufd, &kotmr);
557         if (ret)
558                 return ret;
559         return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
560 }
561
562 #ifdef CONFIG_COMPAT
563 COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
564                 const struct compat_itimerspec __user *, utmr,
565                 struct compat_itimerspec __user *, otmr)
566 {
567         struct itimerspec new, old;
568         int ret;
569
570         if (get_compat_itimerspec(&new, utmr))
571                 return -EFAULT;
572         ret = do_timerfd_settime(ufd, flags, &new, &old);
573         if (ret)
574                 return ret;
575         if (otmr && put_compat_itimerspec(otmr, &old))
576                 return -EFAULT;
577         return ret;
578 }
579
580 COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
581                 struct compat_itimerspec __user *, otmr)
582 {
583         struct itimerspec kotmr;
584         int ret = do_timerfd_gettime(ufd, &kotmr);
585         if (ret)
586                 return ret;
587         return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
588 }
589 #endif