timerfd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. .llseek = noop_llseek,
  129. };
  130. static struct file *timerfd_fget(int fd)
  131. {
  132. struct file *file;
  133. file = fget(fd);
  134. if (!file)
  135. return ERR_PTR(-EBADF);
  136. if (file->f_op != &timerfd_fops) {
  137. fput(file);
  138. return ERR_PTR(-EINVAL);
  139. }
  140. return file;
  141. }
  142. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  143. {
  144. int ufd;
  145. struct timerfd_ctx *ctx;
  146. /* Check the TFD_* constants for consistency. */
  147. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  148. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  149. if ((flags & ~TFD_CREATE_FLAGS) ||
  150. (clockid != CLOCK_MONOTONIC &&
  151. clockid != CLOCK_REALTIME))
  152. return -EINVAL;
  153. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  154. if (!ctx)
  155. return -ENOMEM;
  156. init_waitqueue_head(&ctx->wqh);
  157. ctx->clockid = clockid;
  158. hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
  159. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  160. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  161. if (ufd < 0)
  162. kfree(ctx);
  163. return ufd;
  164. }
  165. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  166. const struct itimerspec __user *, utmr,
  167. struct itimerspec __user *, otmr)
  168. {
  169. struct file *file;
  170. struct timerfd_ctx *ctx;
  171. struct itimerspec ktmr, kotmr;
  172. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  173. return -EFAULT;
  174. if ((flags & ~TFD_SETTIME_FLAGS) ||
  175. !timespec_valid(&ktmr.it_value) ||
  176. !timespec_valid(&ktmr.it_interval))
  177. return -EINVAL;
  178. file = timerfd_fget(ufd);
  179. if (IS_ERR(file))
  180. return PTR_ERR(file);
  181. ctx = file->private_data;
  182. /*
  183. * We need to stop the existing timer before reprogramming
  184. * it to the new values.
  185. */
  186. for (;;) {
  187. spin_lock_irq(&ctx->wqh.lock);
  188. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  189. break;
  190. spin_unlock_irq(&ctx->wqh.lock);
  191. cpu_relax();
  192. }
  193. /*
  194. * If the timer is expired and it's periodic, we need to advance it
  195. * because the caller may want to know the previous expiration time.
  196. * We do not update "ticks" and "expired" since the timer will be
  197. * re-programmed again in the following timerfd_setup() call.
  198. */
  199. if (ctx->expired && ctx->tintv.tv64)
  200. hrtimer_forward_now(&ctx->tmr, ctx->tintv);
  201. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  202. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  203. /*
  204. * Re-program the timer to the new value ...
  205. */
  206. timerfd_setup(ctx, flags, &ktmr);
  207. spin_unlock_irq(&ctx->wqh.lock);
  208. fput(file);
  209. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  210. return -EFAULT;
  211. return 0;
  212. }
  213. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  214. {
  215. struct file *file;
  216. struct timerfd_ctx *ctx;
  217. struct itimerspec kotmr;
  218. file = timerfd_fget(ufd);
  219. if (IS_ERR(file))
  220. return PTR_ERR(file);
  221. ctx = file->private_data;
  222. spin_lock_irq(&ctx->wqh.lock);
  223. if (ctx->expired && ctx->tintv.tv64) {
  224. ctx->expired = 0;
  225. ctx->ticks +=
  226. hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  227. hrtimer_restart(&ctx->tmr);
  228. }
  229. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  230. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  231. spin_unlock_irq(&ctx->wqh.lock);
  232. fput(file);
  233. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  234. }