timerfd.c 8.4 KB

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