timerfd.c 9.6 KB

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