timerfd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. DECLARE_WAITQUEUE(wait, current);
  97. if (count < sizeof(ticks))
  98. return -EINVAL;
  99. spin_lock_irq(&ctx->wqh.lock);
  100. res = -EAGAIN;
  101. if (!ctx->ticks && !(file->f_flags & O_NONBLOCK)) {
  102. __add_wait_queue(&ctx->wqh, &wait);
  103. for (res = 0;;) {
  104. set_current_state(TASK_INTERRUPTIBLE);
  105. if (ctx->ticks) {
  106. res = 0;
  107. break;
  108. }
  109. if (signal_pending(current)) {
  110. res = -ERESTARTSYS;
  111. break;
  112. }
  113. spin_unlock_irq(&ctx->wqh.lock);
  114. schedule();
  115. spin_lock_irq(&ctx->wqh.lock);
  116. }
  117. __remove_wait_queue(&ctx->wqh, &wait);
  118. __set_current_state(TASK_RUNNING);
  119. }
  120. if (ctx->ticks) {
  121. ticks = ctx->ticks;
  122. if (ctx->expired && ctx->tintv.tv64) {
  123. /*
  124. * If tintv.tv64 != 0, this is a periodic timer that
  125. * needs to be re-armed. We avoid doing it in the timer
  126. * callback to avoid DoS attacks specifying a very
  127. * short timer period.
  128. */
  129. ticks += hrtimer_forward_now(&ctx->tmr,
  130. ctx->tintv) - 1;
  131. hrtimer_restart(&ctx->tmr);
  132. }
  133. ctx->expired = 0;
  134. ctx->ticks = 0;
  135. }
  136. spin_unlock_irq(&ctx->wqh.lock);
  137. if (ticks)
  138. res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
  139. return res;
  140. }
  141. static const struct file_operations timerfd_fops = {
  142. .release = timerfd_release,
  143. .poll = timerfd_poll,
  144. .read = timerfd_read,
  145. };
  146. static struct file *timerfd_fget(int fd)
  147. {
  148. struct file *file;
  149. file = fget(fd);
  150. if (!file)
  151. return ERR_PTR(-EBADF);
  152. if (file->f_op != &timerfd_fops) {
  153. fput(file);
  154. return ERR_PTR(-EINVAL);
  155. }
  156. return file;
  157. }
  158. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  159. {
  160. int ufd;
  161. struct timerfd_ctx *ctx;
  162. /* Check the TFD_* constants for consistency. */
  163. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  164. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  165. if ((flags & ~TFD_CREATE_FLAGS) ||
  166. (clockid != CLOCK_MONOTONIC &&
  167. clockid != CLOCK_REALTIME))
  168. return -EINVAL;
  169. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  170. if (!ctx)
  171. return -ENOMEM;
  172. init_waitqueue_head(&ctx->wqh);
  173. ctx->clockid = clockid;
  174. hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
  175. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  176. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  177. if (ufd < 0)
  178. kfree(ctx);
  179. return ufd;
  180. }
  181. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  182. const struct itimerspec __user *, utmr,
  183. struct itimerspec __user *, otmr)
  184. {
  185. struct file *file;
  186. struct timerfd_ctx *ctx;
  187. struct itimerspec ktmr, kotmr;
  188. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  189. return -EFAULT;
  190. if ((flags & ~TFD_SETTIME_FLAGS) ||
  191. !timespec_valid(&ktmr.it_value) ||
  192. !timespec_valid(&ktmr.it_interval))
  193. return -EINVAL;
  194. file = timerfd_fget(ufd);
  195. if (IS_ERR(file))
  196. return PTR_ERR(file);
  197. ctx = file->private_data;
  198. /*
  199. * We need to stop the existing timer before reprogramming
  200. * it to the new values.
  201. */
  202. for (;;) {
  203. spin_lock_irq(&ctx->wqh.lock);
  204. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  205. break;
  206. spin_unlock_irq(&ctx->wqh.lock);
  207. cpu_relax();
  208. }
  209. /*
  210. * If the timer is expired and it's periodic, we need to advance it
  211. * because the caller may want to know the previous expiration time.
  212. * We do not update "ticks" and "expired" since the timer will be
  213. * re-programmed again in the following timerfd_setup() call.
  214. */
  215. if (ctx->expired && ctx->tintv.tv64)
  216. hrtimer_forward_now(&ctx->tmr, ctx->tintv);
  217. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  218. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  219. /*
  220. * Re-program the timer to the new value ...
  221. */
  222. timerfd_setup(ctx, flags, &ktmr);
  223. spin_unlock_irq(&ctx->wqh.lock);
  224. fput(file);
  225. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  226. return -EFAULT;
  227. return 0;
  228. }
  229. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  230. {
  231. struct file *file;
  232. struct timerfd_ctx *ctx;
  233. struct itimerspec kotmr;
  234. file = timerfd_fget(ufd);
  235. if (IS_ERR(file))
  236. return PTR_ERR(file);
  237. ctx = file->private_data;
  238. spin_lock_irq(&ctx->wqh.lock);
  239. if (ctx->expired && ctx->tintv.tv64) {
  240. ctx->expired = 0;
  241. ctx->ticks +=
  242. hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  243. hrtimer_restart(&ctx->tmr);
  244. }
  245. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  246. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  247. spin_unlock_irq(&ctx->wqh.lock);
  248. fput(file);
  249. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  250. }