timerfd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/time.h>
  20. #include <linux/hrtimer.h>
  21. #include <linux/anon_inodes.h>
  22. #include <linux/timerfd.h>
  23. #include <linux/syscalls.h>
  24. struct timerfd_ctx {
  25. struct hrtimer tmr;
  26. ktime_t tintv;
  27. wait_queue_head_t wqh;
  28. u64 ticks;
  29. int expired;
  30. int clockid;
  31. };
  32. /*
  33. * This gets called when the timer event triggers. We set the "expired"
  34. * flag, but we do not re-arm the timer (in case it's necessary,
  35. * tintv.tv64 != 0) until the timer is accessed.
  36. */
  37. static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  38. {
  39. struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
  40. unsigned long flags;
  41. spin_lock_irqsave(&ctx->wqh.lock, flags);
  42. ctx->expired = 1;
  43. ctx->ticks++;
  44. wake_up_locked(&ctx->wqh);
  45. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  46. return HRTIMER_NORESTART;
  47. }
  48. static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  49. {
  50. ktime_t remaining;
  51. remaining = hrtimer_expires_remaining(&ctx->tmr);
  52. return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
  53. }
  54. static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
  55. const struct itimerspec *ktmr)
  56. {
  57. enum hrtimer_mode htmode;
  58. ktime_t texp;
  59. htmode = (flags & TFD_TIMER_ABSTIME) ?
  60. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  61. texp = timespec_to_ktime(ktmr->it_value);
  62. ctx->expired = 0;
  63. ctx->ticks = 0;
  64. ctx->tintv = timespec_to_ktime(ktmr->it_interval);
  65. hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
  66. hrtimer_set_expires(&ctx->tmr, texp);
  67. ctx->tmr.function = timerfd_tmrproc;
  68. if (texp.tv64 != 0)
  69. hrtimer_start(&ctx->tmr, texp, htmode);
  70. }
  71. static int timerfd_release(struct inode *inode, struct file *file)
  72. {
  73. struct timerfd_ctx *ctx = file->private_data;
  74. hrtimer_cancel(&ctx->tmr);
  75. kfree(ctx);
  76. return 0;
  77. }
  78. static unsigned int timerfd_poll(struct file *file, poll_table *wait)
  79. {
  80. struct timerfd_ctx *ctx = file->private_data;
  81. unsigned int events = 0;
  82. unsigned long flags;
  83. poll_wait(file, &ctx->wqh, wait);
  84. spin_lock_irqsave(&ctx->wqh.lock, flags);
  85. if (ctx->ticks)
  86. events |= POLLIN;
  87. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  88. return events;
  89. }
  90. static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
  91. loff_t *ppos)
  92. {
  93. struct timerfd_ctx *ctx = file->private_data;
  94. ssize_t res;
  95. u64 ticks = 0;
  96. if (count < sizeof(ticks))
  97. return -EINVAL;
  98. spin_lock_irq(&ctx->wqh.lock);
  99. if (file->f_flags & O_NONBLOCK)
  100. res = -EAGAIN;
  101. else
  102. res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
  103. if (ctx->ticks) {
  104. ticks = ctx->ticks;
  105. if (ctx->expired && ctx->tintv.tv64) {
  106. /*
  107. * If tintv.tv64 != 0, this is a periodic timer that
  108. * needs to be re-armed. We avoid doing it in the timer
  109. * callback to avoid DoS attacks specifying a very
  110. * short timer period.
  111. */
  112. ticks += hrtimer_forward_now(&ctx->tmr,
  113. ctx->tintv) - 1;
  114. hrtimer_restart(&ctx->tmr);
  115. }
  116. ctx->expired = 0;
  117. ctx->ticks = 0;
  118. }
  119. spin_unlock_irq(&ctx->wqh.lock);
  120. if (ticks)
  121. res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
  122. return res;
  123. }
  124. static const struct file_operations timerfd_fops = {
  125. .release = timerfd_release,
  126. .poll = timerfd_poll,
  127. .read = timerfd_read,
  128. };
  129. static struct file *timerfd_fget(int fd)
  130. {
  131. struct file *file;
  132. file = fget(fd);
  133. if (!file)
  134. return ERR_PTR(-EBADF);
  135. if (file->f_op != &timerfd_fops) {
  136. fput(file);
  137. return ERR_PTR(-EINVAL);
  138. }
  139. return file;
  140. }
  141. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  142. {
  143. int ufd;
  144. struct timerfd_ctx *ctx;
  145. /* Check the TFD_* constants for consistency. */
  146. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  147. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  148. if ((flags & ~TFD_CREATE_FLAGS) ||
  149. (clockid != CLOCK_MONOTONIC &&
  150. clockid != CLOCK_REALTIME))
  151. return -EINVAL;
  152. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  153. if (!ctx)
  154. return -ENOMEM;
  155. init_waitqueue_head(&ctx->wqh);
  156. ctx->clockid = clockid;
  157. hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
  158. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  159. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  160. if (ufd < 0)
  161. kfree(ctx);
  162. return ufd;
  163. }
  164. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  165. const struct itimerspec __user *, utmr,
  166. struct itimerspec __user *, otmr)
  167. {
  168. struct file *file;
  169. struct timerfd_ctx *ctx;
  170. struct itimerspec ktmr, kotmr;
  171. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  172. return -EFAULT;
  173. if ((flags & ~TFD_SETTIME_FLAGS) ||
  174. !timespec_valid(&ktmr.it_value) ||
  175. !timespec_valid(&ktmr.it_interval))
  176. return -EINVAL;
  177. file = timerfd_fget(ufd);
  178. if (IS_ERR(file))
  179. return PTR_ERR(file);
  180. ctx = file->private_data;
  181. /*
  182. * We need to stop the existing timer before reprogramming
  183. * it to the new values.
  184. */
  185. for (;;) {
  186. spin_lock_irq(&ctx->wqh.lock);
  187. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  188. break;
  189. spin_unlock_irq(&ctx->wqh.lock);
  190. cpu_relax();
  191. }
  192. /*
  193. * If the timer is expired and it's periodic, we need to advance it
  194. * because the caller may want to know the previous expiration time.
  195. * We do not update "ticks" and "expired" since the timer will be
  196. * re-programmed again in the following timerfd_setup() call.
  197. */
  198. if (ctx->expired && ctx->tintv.tv64)
  199. hrtimer_forward_now(&ctx->tmr, ctx->tintv);
  200. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  201. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  202. /*
  203. * Re-program the timer to the new value ...
  204. */
  205. timerfd_setup(ctx, flags, &ktmr);
  206. spin_unlock_irq(&ctx->wqh.lock);
  207. fput(file);
  208. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  209. return -EFAULT;
  210. return 0;
  211. }
  212. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  213. {
  214. struct file *file;
  215. struct timerfd_ctx *ctx;
  216. struct itimerspec kotmr;
  217. file = timerfd_fget(ufd);
  218. if (IS_ERR(file))
  219. return PTR_ERR(file);
  220. ctx = file->private_data;
  221. spin_lock_irq(&ctx->wqh.lock);
  222. if (ctx->expired && ctx->tintv.tv64) {
  223. ctx->expired = 0;
  224. ctx->ticks +=
  225. hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  226. hrtimer_restart(&ctx->tmr);
  227. }
  228. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  229. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  230. spin_unlock_irq(&ctx->wqh.lock);
  231. fput(file);
  232. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  233. }