timerfd.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <linux/file.h>
  11. #include <linux/poll.h>
  12. #include <linux/init.h>
  13. #include <linux/fs.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/time.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/anon_inodes.h>
  21. #include <linux/timerfd.h>
  22. struct timerfd_ctx {
  23. struct hrtimer tmr;
  24. ktime_t tintv;
  25. spinlock_t lock;
  26. wait_queue_head_t wqh;
  27. int expired;
  28. };
  29. /*
  30. * This gets called when the timer event triggers. We set the "expired"
  31. * flag, but we do not re-arm the timer (in case it's necessary,
  32. * tintv.tv64 != 0) until the timer is read.
  33. */
  34. static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  35. {
  36. struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
  37. unsigned long flags;
  38. spin_lock_irqsave(&ctx->lock, flags);
  39. ctx->expired = 1;
  40. wake_up_locked(&ctx->wqh);
  41. spin_unlock_irqrestore(&ctx->lock, flags);
  42. return HRTIMER_NORESTART;
  43. }
  44. static void timerfd_setup(struct timerfd_ctx *ctx, int clockid, int flags,
  45. const struct itimerspec *ktmr)
  46. {
  47. enum hrtimer_mode htmode;
  48. ktime_t texp;
  49. htmode = (flags & TFD_TIMER_ABSTIME) ?
  50. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  51. texp = timespec_to_ktime(ktmr->it_value);
  52. ctx->expired = 0;
  53. ctx->tintv = timespec_to_ktime(ktmr->it_interval);
  54. hrtimer_init(&ctx->tmr, clockid, htmode);
  55. ctx->tmr.expires = texp;
  56. ctx->tmr.function = timerfd_tmrproc;
  57. if (texp.tv64 != 0)
  58. hrtimer_start(&ctx->tmr, texp, htmode);
  59. }
  60. static int timerfd_release(struct inode *inode, struct file *file)
  61. {
  62. struct timerfd_ctx *ctx = file->private_data;
  63. hrtimer_cancel(&ctx->tmr);
  64. kfree(ctx);
  65. return 0;
  66. }
  67. static unsigned int timerfd_poll(struct file *file, poll_table *wait)
  68. {
  69. struct timerfd_ctx *ctx = file->private_data;
  70. unsigned int events = 0;
  71. unsigned long flags;
  72. poll_wait(file, &ctx->wqh, wait);
  73. spin_lock_irqsave(&ctx->lock, flags);
  74. if (ctx->expired)
  75. events |= POLLIN;
  76. spin_unlock_irqrestore(&ctx->lock, flags);
  77. return events;
  78. }
  79. static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
  80. loff_t *ppos)
  81. {
  82. struct timerfd_ctx *ctx = file->private_data;
  83. ssize_t res;
  84. u32 ticks = 0;
  85. DECLARE_WAITQUEUE(wait, current);
  86. if (count < sizeof(ticks))
  87. return -EINVAL;
  88. spin_lock_irq(&ctx->lock);
  89. res = -EAGAIN;
  90. if (!ctx->expired && !(file->f_flags & O_NONBLOCK)) {
  91. __add_wait_queue(&ctx->wqh, &wait);
  92. for (res = 0;;) {
  93. set_current_state(TASK_INTERRUPTIBLE);
  94. if (ctx->expired) {
  95. res = 0;
  96. break;
  97. }
  98. if (signal_pending(current)) {
  99. res = -ERESTARTSYS;
  100. break;
  101. }
  102. spin_unlock_irq(&ctx->lock);
  103. schedule();
  104. spin_lock_irq(&ctx->lock);
  105. }
  106. __remove_wait_queue(&ctx->wqh, &wait);
  107. __set_current_state(TASK_RUNNING);
  108. }
  109. if (ctx->expired) {
  110. ctx->expired = 0;
  111. if (ctx->tintv.tv64 != 0) {
  112. /*
  113. * If tintv.tv64 != 0, this is a periodic timer that
  114. * needs to be re-armed. We avoid doing it in the timer
  115. * callback to avoid DoS attacks specifying a very
  116. * short timer period.
  117. */
  118. ticks = (u32)
  119. hrtimer_forward(&ctx->tmr,
  120. hrtimer_cb_get_time(&ctx->tmr),
  121. ctx->tintv);
  122. hrtimer_restart(&ctx->tmr);
  123. } else
  124. ticks = 1;
  125. }
  126. spin_unlock_irq(&ctx->lock);
  127. if (ticks)
  128. res = put_user(ticks, buf) ? -EFAULT: sizeof(ticks);
  129. return res;
  130. }
  131. static const struct file_operations timerfd_fops = {
  132. .release = timerfd_release,
  133. .poll = timerfd_poll,
  134. .read = timerfd_read,
  135. };
  136. asmlinkage long sys_timerfd(int ufd, int clockid, int flags,
  137. const struct itimerspec __user *utmr)
  138. {
  139. int error;
  140. struct timerfd_ctx *ctx;
  141. struct file *file;
  142. struct inode *inode;
  143. struct itimerspec ktmr;
  144. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  145. return -EFAULT;
  146. if (clockid != CLOCK_MONOTONIC &&
  147. clockid != CLOCK_REALTIME)
  148. return -EINVAL;
  149. if (!timespec_valid(&ktmr.it_value) ||
  150. !timespec_valid(&ktmr.it_interval))
  151. return -EINVAL;
  152. if (ufd == -1) {
  153. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  154. if (!ctx)
  155. return -ENOMEM;
  156. init_waitqueue_head(&ctx->wqh);
  157. spin_lock_init(&ctx->lock);
  158. timerfd_setup(ctx, clockid, flags, &ktmr);
  159. /*
  160. * When we call this, the initialization must be complete, since
  161. * anon_inode_getfd() will install the fd.
  162. */
  163. error = anon_inode_getfd(&ufd, &inode, &file, "[timerfd]",
  164. &timerfd_fops, ctx);
  165. if (error)
  166. goto err_tmrcancel;
  167. } else {
  168. file = fget(ufd);
  169. if (!file)
  170. return -EBADF;
  171. ctx = file->private_data;
  172. if (file->f_op != &timerfd_fops) {
  173. fput(file);
  174. return -EINVAL;
  175. }
  176. /*
  177. * We need to stop the existing timer before reprogramming
  178. * it to the new values.
  179. */
  180. for (;;) {
  181. spin_lock_irq(&ctx->lock);
  182. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  183. break;
  184. spin_unlock_irq(&ctx->lock);
  185. cpu_relax();
  186. }
  187. /*
  188. * Re-program the timer to the new value ...
  189. */
  190. timerfd_setup(ctx, clockid, flags, &ktmr);
  191. spin_unlock_irq(&ctx->lock);
  192. fput(file);
  193. }
  194. return ufd;
  195. err_tmrcancel:
  196. hrtimer_cancel(&ctx->tmr);
  197. kfree(ctx);
  198. return error;
  199. }