timerfd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #include <linux/rcupdate.h>
  25. struct timerfd_ctx {
  26. struct hrtimer tmr;
  27. ktime_t tintv;
  28. ktime_t moffs;
  29. wait_queue_head_t wqh;
  30. u64 ticks;
  31. int expired;
  32. int clockid;
  33. struct rcu_head rcu;
  34. struct list_head clist;
  35. bool might_cancel;
  36. };
  37. static LIST_HEAD(cancel_list);
  38. static DEFINE_SPINLOCK(cancel_lock);
  39. /*
  40. * This gets called when the timer event triggers. We set the "expired"
  41. * flag, but we do not re-arm the timer (in case it's necessary,
  42. * tintv.tv64 != 0) until the timer is accessed.
  43. */
  44. static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  45. {
  46. struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx, tmr);
  47. unsigned long flags;
  48. spin_lock_irqsave(&ctx->wqh.lock, flags);
  49. ctx->expired = 1;
  50. ctx->ticks++;
  51. wake_up_locked(&ctx->wqh);
  52. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  53. return HRTIMER_NORESTART;
  54. }
  55. /*
  56. * Called when the clock was set to cancel the timers in the cancel
  57. * list. This will wake up processes waiting on these timers. The
  58. * wake-up requires ctx->ticks to be non zero, therefore we increment
  59. * it before calling wake_up_locked().
  60. */
  61. void timerfd_clock_was_set(void)
  62. {
  63. ktime_t moffs = ktime_get_monotonic_offset();
  64. struct timerfd_ctx *ctx;
  65. unsigned long flags;
  66. rcu_read_lock();
  67. list_for_each_entry_rcu(ctx, &cancel_list, clist) {
  68. if (!ctx->might_cancel)
  69. continue;
  70. spin_lock_irqsave(&ctx->wqh.lock, flags);
  71. if (ctx->moffs.tv64 != moffs.tv64) {
  72. ctx->moffs.tv64 = KTIME_MAX;
  73. ctx->ticks++;
  74. wake_up_locked(&ctx->wqh);
  75. }
  76. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  77. }
  78. rcu_read_unlock();
  79. }
  80. static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
  81. {
  82. if (ctx->might_cancel) {
  83. ctx->might_cancel = false;
  84. spin_lock(&cancel_lock);
  85. list_del_rcu(&ctx->clist);
  86. spin_unlock(&cancel_lock);
  87. }
  88. }
  89. static bool timerfd_canceled(struct timerfd_ctx *ctx)
  90. {
  91. if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
  92. return false;
  93. ctx->moffs = ktime_get_monotonic_offset();
  94. return true;
  95. }
  96. static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  97. {
  98. if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
  99. (flags & TFD_TIMER_CANCEL_ON_SET)) {
  100. if (!ctx->might_cancel) {
  101. ctx->might_cancel = true;
  102. spin_lock(&cancel_lock);
  103. list_add_rcu(&ctx->clist, &cancel_list);
  104. spin_unlock(&cancel_lock);
  105. }
  106. } else if (ctx->might_cancel) {
  107. timerfd_remove_cancel(ctx);
  108. }
  109. }
  110. static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  111. {
  112. ktime_t remaining;
  113. remaining = hrtimer_expires_remaining(&ctx->tmr);
  114. return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
  115. }
  116. static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  117. const struct itimerspec *ktmr)
  118. {
  119. enum hrtimer_mode htmode;
  120. ktime_t texp;
  121. int clockid = ctx->clockid;
  122. htmode = (flags & TFD_TIMER_ABSTIME) ?
  123. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  124. texp = timespec_to_ktime(ktmr->it_value);
  125. ctx->expired = 0;
  126. ctx->ticks = 0;
  127. ctx->tintv = timespec_to_ktime(ktmr->it_interval);
  128. hrtimer_init(&ctx->tmr, clockid, htmode);
  129. hrtimer_set_expires(&ctx->tmr, texp);
  130. ctx->tmr.function = timerfd_tmrproc;
  131. if (texp.tv64 != 0) {
  132. hrtimer_start(&ctx->tmr, texp, htmode);
  133. if (timerfd_canceled(ctx))
  134. return -ECANCELED;
  135. }
  136. return 0;
  137. }
  138. static int timerfd_release(struct inode *inode, struct file *file)
  139. {
  140. struct timerfd_ctx *ctx = file->private_data;
  141. timerfd_remove_cancel(ctx);
  142. hrtimer_cancel(&ctx->tmr);
  143. kfree_rcu(ctx, rcu);
  144. return 0;
  145. }
  146. static unsigned int timerfd_poll(struct file *file, poll_table *wait)
  147. {
  148. struct timerfd_ctx *ctx = file->private_data;
  149. unsigned int events = 0;
  150. unsigned long flags;
  151. poll_wait(file, &ctx->wqh, wait);
  152. spin_lock_irqsave(&ctx->wqh.lock, flags);
  153. if (ctx->ticks)
  154. events |= POLLIN;
  155. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  156. return events;
  157. }
  158. static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
  159. loff_t *ppos)
  160. {
  161. struct timerfd_ctx *ctx = file->private_data;
  162. ssize_t res;
  163. u64 ticks = 0;
  164. if (count < sizeof(ticks))
  165. return -EINVAL;
  166. spin_lock_irq(&ctx->wqh.lock);
  167. if (file->f_flags & O_NONBLOCK)
  168. res = -EAGAIN;
  169. else
  170. res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
  171. /*
  172. * If clock has changed, we do not care about the
  173. * ticks and we do not rearm the timer. Userspace must
  174. * reevaluate anyway.
  175. */
  176. if (timerfd_canceled(ctx)) {
  177. ctx->ticks = 0;
  178. ctx->expired = 0;
  179. res = -ECANCELED;
  180. }
  181. if (ctx->ticks) {
  182. ticks = ctx->ticks;
  183. if (ctx->expired && ctx->tintv.tv64) {
  184. /*
  185. * If tintv.tv64 != 0, this is a periodic timer that
  186. * needs to be re-armed. We avoid doing it in the timer
  187. * callback to avoid DoS attacks specifying a very
  188. * short timer period.
  189. */
  190. ticks += hrtimer_forward_now(&ctx->tmr,
  191. ctx->tintv) - 1;
  192. hrtimer_restart(&ctx->tmr);
  193. }
  194. ctx->expired = 0;
  195. ctx->ticks = 0;
  196. }
  197. spin_unlock_irq(&ctx->wqh.lock);
  198. if (ticks)
  199. res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
  200. return res;
  201. }
  202. static const struct file_operations timerfd_fops = {
  203. .release = timerfd_release,
  204. .poll = timerfd_poll,
  205. .read = timerfd_read,
  206. .llseek = noop_llseek,
  207. };
  208. static struct file *timerfd_fget(int fd)
  209. {
  210. struct file *file;
  211. file = fget(fd);
  212. if (!file)
  213. return ERR_PTR(-EBADF);
  214. if (file->f_op != &timerfd_fops) {
  215. fput(file);
  216. return ERR_PTR(-EINVAL);
  217. }
  218. return file;
  219. }
  220. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  221. {
  222. int ufd;
  223. struct timerfd_ctx *ctx;
  224. /* Check the TFD_* constants for consistency. */
  225. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  226. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  227. if ((flags & ~TFD_CREATE_FLAGS) ||
  228. (clockid != CLOCK_MONOTONIC &&
  229. clockid != CLOCK_REALTIME))
  230. return -EINVAL;
  231. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  232. if (!ctx)
  233. return -ENOMEM;
  234. init_waitqueue_head(&ctx->wqh);
  235. ctx->clockid = clockid;
  236. hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
  237. ctx->moffs = ktime_get_monotonic_offset();
  238. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  239. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  240. if (ufd < 0)
  241. kfree(ctx);
  242. return ufd;
  243. }
  244. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  245. const struct itimerspec __user *, utmr,
  246. struct itimerspec __user *, otmr)
  247. {
  248. struct file *file;
  249. struct timerfd_ctx *ctx;
  250. struct itimerspec ktmr, kotmr;
  251. int ret;
  252. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  253. return -EFAULT;
  254. if ((flags & ~TFD_SETTIME_FLAGS) ||
  255. !timespec_valid(&ktmr.it_value) ||
  256. !timespec_valid(&ktmr.it_interval))
  257. return -EINVAL;
  258. file = timerfd_fget(ufd);
  259. if (IS_ERR(file))
  260. return PTR_ERR(file);
  261. ctx = file->private_data;
  262. timerfd_setup_cancel(ctx, flags);
  263. /*
  264. * We need to stop the existing timer before reprogramming
  265. * it to the new values.
  266. */
  267. for (;;) {
  268. spin_lock_irq(&ctx->wqh.lock);
  269. if (hrtimer_try_to_cancel(&ctx->tmr) >= 0)
  270. break;
  271. spin_unlock_irq(&ctx->wqh.lock);
  272. cpu_relax();
  273. }
  274. /*
  275. * If the timer is expired and it's periodic, we need to advance it
  276. * because the caller may want to know the previous expiration time.
  277. * We do not update "ticks" and "expired" since the timer will be
  278. * re-programmed again in the following timerfd_setup() call.
  279. */
  280. if (ctx->expired && ctx->tintv.tv64)
  281. hrtimer_forward_now(&ctx->tmr, ctx->tintv);
  282. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  283. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  284. /*
  285. * Re-program the timer to the new value ...
  286. */
  287. ret = timerfd_setup(ctx, flags, &ktmr);
  288. spin_unlock_irq(&ctx->wqh.lock);
  289. fput(file);
  290. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  291. return -EFAULT;
  292. return ret;
  293. }
  294. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  295. {
  296. struct file *file;
  297. struct timerfd_ctx *ctx;
  298. struct itimerspec kotmr;
  299. file = timerfd_fget(ufd);
  300. if (IS_ERR(file))
  301. return PTR_ERR(file);
  302. ctx = file->private_data;
  303. spin_lock_irq(&ctx->wqh.lock);
  304. if (ctx->expired && ctx->tintv.tv64) {
  305. ctx->expired = 0;
  306. ctx->ticks +=
  307. hrtimer_forward_now(&ctx->tmr, ctx->tintv) - 1;
  308. hrtimer_restart(&ctx->tmr);
  309. }
  310. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  311. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  312. spin_unlock_irq(&ctx->wqh.lock);
  313. fput(file);
  314. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  315. }