timerfd.c 6.8 KB

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