timerfd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 now, remaining;
  50. now = ctx->tmr.base->get_time();
  51. remaining = ktime_sub(ctx->tmr.expires, now);
  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. ctx->tmr.expires = 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. asmlinkage long sys_timerfd_create(int clockid, int flags)
  159. {
  160. int ufd;
  161. struct timerfd_ctx *ctx;
  162. if (flags)
  163. return -EINVAL;
  164. if (clockid != CLOCK_MONOTONIC &&
  165. clockid != CLOCK_REALTIME)
  166. return -EINVAL;
  167. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  168. if (!ctx)
  169. return -ENOMEM;
  170. init_waitqueue_head(&ctx->wqh);
  171. ctx->clockid = clockid;
  172. hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
  173. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx);
  174. if (ufd < 0)
  175. kfree(ctx);
  176. return ufd;
  177. }
  178. asmlinkage long sys_timerfd_settime(int ufd, int flags,
  179. const struct itimerspec __user *utmr,
  180. struct itimerspec __user *otmr)
  181. {
  182. struct file *file;
  183. struct timerfd_ctx *ctx;
  184. struct itimerspec ktmr, kotmr;
  185. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  186. return -EFAULT;
  187. if (!timespec_valid(&ktmr.it_value) ||
  188. !timespec_valid(&ktmr.it_interval))
  189. return -EINVAL;
  190. file = timerfd_fget(ufd);
  191. if (IS_ERR(file))
  192. return PTR_ERR(file);
  193. ctx = file->private_data;
  194. /*
  195. * We need to stop the existing timer before reprogramming
  196. * it to the new values.
  197. */
  198. for (;;) {
  199. spin_lock_irq(&ctx->wqh.lock);
  200. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  201. break;
  202. spin_unlock_irq(&ctx->wqh.lock);
  203. cpu_relax();
  204. }
  205. /*
  206. * If the timer is expired and it's periodic, we need to advance it
  207. * because the caller may want to know the previous expiration time.
  208. * We do not update "ticks" and "expired" since the timer will be
  209. * re-programmed again in the following timerfd_setup() call.
  210. */
  211. if (ctx->expired && ctx->tintv.tv64)
  212. hrtimer_forward_now(&ctx->tmr, ctx->tintv);
  213. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  214. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  215. /*
  216. * Re-program the timer to the new value ...
  217. */
  218. timerfd_setup(ctx, flags, &ktmr);
  219. spin_unlock_irq(&ctx->wqh.lock);
  220. fput(file);
  221. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  222. return -EFAULT;
  223. return 0;
  224. }
  225. asmlinkage long sys_timerfd_gettime(int ufd, struct itimerspec __user *otmr)
  226. {
  227. struct file *file;
  228. struct timerfd_ctx *ctx;
  229. struct itimerspec kotmr;
  230. file = timerfd_fget(ufd);
  231. if (IS_ERR(file))
  232. return PTR_ERR(file);
  233. ctx = file->private_data;
  234. spin_lock_irq(&ctx->wqh.lock);
  235. if (ctx->expired && ctx->tintv.tv64) {
  236. ctx->expired = 0;
  237. ctx->ticks +=
  238. hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  239. hrtimer_restart(&ctx->tmr);
  240. }
  241. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  242. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  243. spin_unlock_irq(&ctx->wqh.lock);
  244. fput(file);
  245. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  246. }