timerfd.c 5.0 KB

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