timerfd.c 11 KB

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