timerfd.c 13 KB

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