timerfd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/timerfd.c
  4. *
  5. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  6. *
  7. *
  8. * Thanks to Thomas Gleixner for code reviews and useful comments.
  9. *
  10. */
  11. #include <linux/alarmtimer.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/time.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/anon_inodes.h>
  24. #include <linux/timerfd.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/compat.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/time_namespace.h>
  29. struct timerfd_ctx {
  30. union {
  31. struct hrtimer tmr;
  32. struct alarm alarm;
  33. } t;
  34. ktime_t tintv;
  35. ktime_t moffs;
  36. wait_queue_head_t wqh;
  37. u64 ticks;
  38. int clockid;
  39. short unsigned expired;
  40. short unsigned settime_flags; /* to show in fdinfo */
  41. struct rcu_head rcu;
  42. struct list_head clist;
  43. spinlock_t cancel_lock;
  44. bool might_cancel;
  45. };
  46. static LIST_HEAD(cancel_list);
  47. static DEFINE_SPINLOCK(cancel_lock);
  48. static inline bool isalarm(struct timerfd_ctx *ctx)
  49. {
  50. return ctx->clockid == CLOCK_REALTIME_ALARM ||
  51. ctx->clockid == CLOCK_BOOTTIME_ALARM;
  52. }
  53. /*
  54. * This gets called when the timer event triggers. We set the "expired"
  55. * flag, but we do not re-arm the timer (in case it's necessary,
  56. * tintv != 0) until the timer is accessed.
  57. */
  58. static void timerfd_triggered(struct timerfd_ctx *ctx)
  59. {
  60. unsigned long flags;
  61. spin_lock_irqsave(&ctx->wqh.lock, flags);
  62. ctx->expired = 1;
  63. ctx->ticks++;
  64. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  65. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  66. }
  67. static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  68. {
  69. struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
  70. t.tmr);
  71. timerfd_triggered(ctx);
  72. return HRTIMER_NORESTART;
  73. }
  74. static void timerfd_alarmproc(struct alarm *alarm, ktime_t now)
  75. {
  76. struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
  77. t.alarm);
  78. timerfd_triggered(ctx);
  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(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 != moffs) {
  97. ctx->moffs = KTIME_MAX;
  98. ctx->ticks++;
  99. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  100. }
  101. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  102. }
  103. rcu_read_unlock();
  104. }
  105. static void timerfd_resume_work(struct work_struct *work)
  106. {
  107. timerfd_clock_was_set();
  108. }
  109. static DECLARE_WORK(timerfd_work, timerfd_resume_work);
  110. /*
  111. * Invoked from timekeeping_resume(). Defer the actual update to work so
  112. * timerfd_clock_was_set() runs in task context.
  113. */
  114. void timerfd_resume(void)
  115. {
  116. schedule_work(&timerfd_work);
  117. }
  118. static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
  119. {
  120. if (ctx->might_cancel) {
  121. ctx->might_cancel = false;
  122. spin_lock(&cancel_lock);
  123. list_del_rcu(&ctx->clist);
  124. spin_unlock(&cancel_lock);
  125. }
  126. }
  127. static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
  128. {
  129. spin_lock(&ctx->cancel_lock);
  130. __timerfd_remove_cancel(ctx);
  131. spin_unlock(&ctx->cancel_lock);
  132. }
  133. static bool timerfd_canceled(struct timerfd_ctx *ctx)
  134. {
  135. if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
  136. return false;
  137. ctx->moffs = ktime_mono_to_real(0);
  138. return true;
  139. }
  140. static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  141. {
  142. spin_lock(&ctx->cancel_lock);
  143. if ((ctx->clockid == CLOCK_REALTIME ||
  144. ctx->clockid == CLOCK_REALTIME_ALARM) &&
  145. (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
  146. if (!ctx->might_cancel) {
  147. ctx->might_cancel = true;
  148. spin_lock(&cancel_lock);
  149. list_add_rcu(&ctx->clist, &cancel_list);
  150. spin_unlock(&cancel_lock);
  151. }
  152. } else {
  153. __timerfd_remove_cancel(ctx);
  154. }
  155. spin_unlock(&ctx->cancel_lock);
  156. }
  157. static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  158. {
  159. ktime_t remaining;
  160. if (isalarm(ctx))
  161. remaining = alarm_expires_remaining(&ctx->t.alarm);
  162. else
  163. remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
  164. return remaining < 0 ? 0: remaining;
  165. }
  166. static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  167. const struct itimerspec64 *ktmr)
  168. {
  169. enum hrtimer_mode htmode;
  170. ktime_t texp;
  171. int clockid = ctx->clockid;
  172. htmode = (flags & TFD_TIMER_ABSTIME) ?
  173. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  174. texp = timespec64_to_ktime(ktmr->it_value);
  175. ctx->expired = 0;
  176. ctx->ticks = 0;
  177. ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
  178. if (isalarm(ctx)) {
  179. alarm_init(&ctx->t.alarm,
  180. ctx->clockid == CLOCK_REALTIME_ALARM ?
  181. ALARM_REALTIME : ALARM_BOOTTIME,
  182. timerfd_alarmproc);
  183. } else {
  184. hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, htmode);
  185. hrtimer_set_expires(&ctx->t.tmr, texp);
  186. }
  187. if (texp != 0) {
  188. if (flags & TFD_TIMER_ABSTIME)
  189. texp = timens_ktime_to_host(clockid, texp);
  190. if (isalarm(ctx)) {
  191. if (flags & TFD_TIMER_ABSTIME)
  192. alarm_start(&ctx->t.alarm, texp);
  193. else
  194. alarm_start_relative(&ctx->t.alarm, texp);
  195. } else {
  196. hrtimer_start(&ctx->t.tmr, texp, htmode);
  197. }
  198. if (timerfd_canceled(ctx))
  199. return -ECANCELED;
  200. }
  201. ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
  202. return 0;
  203. }
  204. static int timerfd_release(struct inode *inode, struct file *file)
  205. {
  206. struct timerfd_ctx *ctx = file->private_data;
  207. timerfd_remove_cancel(ctx);
  208. if (isalarm(ctx))
  209. alarm_cancel(&ctx->t.alarm);
  210. else
  211. hrtimer_cancel(&ctx->t.tmr);
  212. kfree_rcu(ctx, rcu);
  213. return 0;
  214. }
  215. static __poll_t timerfd_poll(struct file *file, poll_table *wait)
  216. {
  217. struct timerfd_ctx *ctx = file->private_data;
  218. __poll_t events = 0;
  219. unsigned long flags;
  220. poll_wait(file, &ctx->wqh, wait);
  221. spin_lock_irqsave(&ctx->wqh.lock, flags);
  222. if (ctx->ticks)
  223. events |= EPOLLIN;
  224. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  225. return events;
  226. }
  227. static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
  228. {
  229. struct file *file = iocb->ki_filp;
  230. struct timerfd_ctx *ctx = file->private_data;
  231. ssize_t res;
  232. u64 ticks = 0;
  233. if (iov_iter_count(to) < sizeof(ticks))
  234. return -EINVAL;
  235. spin_lock_irq(&ctx->wqh.lock);
  236. if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT)
  237. res = -EAGAIN;
  238. else
  239. res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
  240. /*
  241. * If clock has changed, we do not care about the
  242. * ticks and we do not rearm the timer. Userspace must
  243. * reevaluate anyway.
  244. */
  245. if (timerfd_canceled(ctx)) {
  246. ctx->ticks = 0;
  247. ctx->expired = 0;
  248. res = -ECANCELED;
  249. }
  250. if (ctx->ticks) {
  251. ticks = ctx->ticks;
  252. if (ctx->expired && ctx->tintv) {
  253. /*
  254. * If tintv != 0, this is a periodic timer that
  255. * needs to be re-armed. We avoid doing it in the timer
  256. * callback to avoid DoS attacks specifying a very
  257. * short timer period.
  258. */
  259. if (isalarm(ctx)) {
  260. ticks += alarm_forward_now(
  261. &ctx->t.alarm, ctx->tintv) - 1;
  262. alarm_restart(&ctx->t.alarm);
  263. } else {
  264. ticks += hrtimer_forward_now(&ctx->t.tmr,
  265. ctx->tintv) - 1;
  266. hrtimer_restart(&ctx->t.tmr);
  267. }
  268. }
  269. ctx->expired = 0;
  270. ctx->ticks = 0;
  271. }
  272. spin_unlock_irq(&ctx->wqh.lock);
  273. if (ticks) {
  274. res = copy_to_iter(&ticks, sizeof(ticks), to);
  275. if (!res)
  276. res = -EFAULT;
  277. }
  278. return res;
  279. }
  280. #ifdef CONFIG_PROC_FS
  281. static void timerfd_show(struct seq_file *m, struct file *file)
  282. {
  283. struct timerfd_ctx *ctx = file->private_data;
  284. struct timespec64 value, interval;
  285. spin_lock_irq(&ctx->wqh.lock);
  286. value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  287. interval = ktime_to_timespec64(ctx->tintv);
  288. spin_unlock_irq(&ctx->wqh.lock);
  289. seq_printf(m,
  290. "clockid: %d\n"
  291. "ticks: %llu\n"
  292. "settime flags: 0%o\n"
  293. "it_value: (%llu, %llu)\n"
  294. "it_interval: (%llu, %llu)\n",
  295. ctx->clockid,
  296. (unsigned long long)ctx->ticks,
  297. ctx->settime_flags,
  298. (unsigned long long)value.tv_sec,
  299. (unsigned long long)value.tv_nsec,
  300. (unsigned long long)interval.tv_sec,
  301. (unsigned long long)interval.tv_nsec);
  302. }
  303. #else
  304. #define timerfd_show NULL
  305. #endif
  306. #ifdef CONFIG_CHECKPOINT_RESTORE
  307. static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  308. {
  309. struct timerfd_ctx *ctx = file->private_data;
  310. int ret = 0;
  311. switch (cmd) {
  312. case TFD_IOC_SET_TICKS: {
  313. u64 ticks;
  314. if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
  315. return -EFAULT;
  316. if (!ticks)
  317. return -EINVAL;
  318. spin_lock_irq(&ctx->wqh.lock);
  319. if (!timerfd_canceled(ctx)) {
  320. ctx->ticks = ticks;
  321. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  322. } else
  323. ret = -ECANCELED;
  324. spin_unlock_irq(&ctx->wqh.lock);
  325. break;
  326. }
  327. default:
  328. ret = -ENOTTY;
  329. break;
  330. }
  331. return ret;
  332. }
  333. #else
  334. #define timerfd_ioctl NULL
  335. #endif
  336. static const struct file_operations timerfd_fops = {
  337. .release = timerfd_release,
  338. .poll = timerfd_poll,
  339. .read_iter = timerfd_read_iter,
  340. .llseek = noop_llseek,
  341. .show_fdinfo = timerfd_show,
  342. .unlocked_ioctl = timerfd_ioctl,
  343. };
  344. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  345. {
  346. struct timerfd_ctx *ctx __free(kfree) = NULL;
  347. int ret;
  348. /* Check the TFD_* constants for consistency. */
  349. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  350. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  351. if ((flags & ~TFD_CREATE_FLAGS) ||
  352. (clockid != CLOCK_MONOTONIC &&
  353. clockid != CLOCK_REALTIME &&
  354. clockid != CLOCK_REALTIME_ALARM &&
  355. clockid != CLOCK_BOOTTIME &&
  356. clockid != CLOCK_BOOTTIME_ALARM))
  357. return -EINVAL;
  358. if ((clockid == CLOCK_REALTIME_ALARM ||
  359. clockid == CLOCK_BOOTTIME_ALARM) &&
  360. !capable(CAP_WAKE_ALARM))
  361. return -EPERM;
  362. ctx = kzalloc_obj(*ctx);
  363. if (!ctx)
  364. return -ENOMEM;
  365. init_waitqueue_head(&ctx->wqh);
  366. spin_lock_init(&ctx->cancel_lock);
  367. ctx->clockid = clockid;
  368. if (isalarm(ctx))
  369. alarm_init(&ctx->t.alarm,
  370. ctx->clockid == CLOCK_REALTIME_ALARM ?
  371. ALARM_REALTIME : ALARM_BOOTTIME,
  372. timerfd_alarmproc);
  373. else
  374. hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, HRTIMER_MODE_ABS);
  375. ctx->moffs = ktime_mono_to_real(0);
  376. ret = FD_ADD(flags & TFD_SHARED_FCNTL_FLAGS,
  377. anon_inode_getfile_fmode("[timerfd]", &timerfd_fops, ctx,
  378. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS),
  379. FMODE_NOWAIT));
  380. if (ret >= 0)
  381. retain_and_null_ptr(ctx);
  382. return ret;
  383. }
  384. static int do_timerfd_settime(int ufd, int flags,
  385. const struct itimerspec64 *new,
  386. struct itimerspec64 *old)
  387. {
  388. struct timerfd_ctx *ctx;
  389. int ret;
  390. if ((flags & ~TFD_SETTIME_FLAGS) ||
  391. !itimerspec64_valid(new))
  392. return -EINVAL;
  393. CLASS(fd, f)(ufd);
  394. if (fd_empty(f))
  395. return -EBADF;
  396. if (fd_file(f)->f_op != &timerfd_fops)
  397. return -EINVAL;
  398. ctx = fd_file(f)->private_data;
  399. if (isalarm(ctx) && !capable(CAP_WAKE_ALARM))
  400. return -EPERM;
  401. timerfd_setup_cancel(ctx, flags);
  402. /*
  403. * We need to stop the existing timer before reprogramming
  404. * it to the new values.
  405. */
  406. for (;;) {
  407. spin_lock_irq(&ctx->wqh.lock);
  408. if (isalarm(ctx)) {
  409. if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  410. break;
  411. } else {
  412. if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  413. break;
  414. }
  415. spin_unlock_irq(&ctx->wqh.lock);
  416. if (isalarm(ctx))
  417. hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
  418. else
  419. hrtimer_cancel_wait_running(&ctx->t.tmr);
  420. }
  421. /*
  422. * If the timer is expired and it's periodic, we need to advance it
  423. * because the caller may want to know the previous expiration time.
  424. * We do not update "ticks" and "expired" since the timer will be
  425. * re-programmed again in the following timerfd_setup() call.
  426. */
  427. if (ctx->expired && ctx->tintv) {
  428. if (isalarm(ctx))
  429. alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  430. else
  431. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  432. }
  433. old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  434. old->it_interval = ktime_to_timespec64(ctx->tintv);
  435. /*
  436. * Re-program the timer to the new value ...
  437. */
  438. ret = timerfd_setup(ctx, flags, new);
  439. spin_unlock_irq(&ctx->wqh.lock);
  440. return ret;
  441. }
  442. static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
  443. {
  444. struct timerfd_ctx *ctx;
  445. CLASS(fd, f)(ufd);
  446. if (fd_empty(f))
  447. return -EBADF;
  448. if (fd_file(f)->f_op != &timerfd_fops)
  449. return -EINVAL;
  450. ctx = fd_file(f)->private_data;
  451. spin_lock_irq(&ctx->wqh.lock);
  452. if (ctx->expired && ctx->tintv) {
  453. ctx->expired = 0;
  454. if (isalarm(ctx)) {
  455. ctx->ticks +=
  456. alarm_forward_now(
  457. &ctx->t.alarm, ctx->tintv) - 1;
  458. alarm_restart(&ctx->t.alarm);
  459. } else {
  460. ctx->ticks +=
  461. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
  462. - 1;
  463. hrtimer_restart(&ctx->t.tmr);
  464. }
  465. }
  466. t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  467. t->it_interval = ktime_to_timespec64(ctx->tintv);
  468. spin_unlock_irq(&ctx->wqh.lock);
  469. return 0;
  470. }
  471. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  472. const struct __kernel_itimerspec __user *, utmr,
  473. struct __kernel_itimerspec __user *, otmr)
  474. {
  475. struct itimerspec64 new, old;
  476. int ret;
  477. if (get_itimerspec64(&new, utmr))
  478. return -EFAULT;
  479. ret = do_timerfd_settime(ufd, flags, &new, &old);
  480. if (ret)
  481. return ret;
  482. if (otmr && put_itimerspec64(&old, otmr))
  483. return -EFAULT;
  484. return ret;
  485. }
  486. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
  487. {
  488. struct itimerspec64 kotmr;
  489. int ret = do_timerfd_gettime(ufd, &kotmr);
  490. if (ret)
  491. return ret;
  492. return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
  493. }
  494. #ifdef CONFIG_COMPAT_32BIT_TIME
  495. SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
  496. const struct old_itimerspec32 __user *, utmr,
  497. struct old_itimerspec32 __user *, otmr)
  498. {
  499. struct itimerspec64 new, old;
  500. int ret;
  501. if (get_old_itimerspec32(&new, utmr))
  502. return -EFAULT;
  503. ret = do_timerfd_settime(ufd, flags, &new, &old);
  504. if (ret)
  505. return ret;
  506. if (otmr && put_old_itimerspec32(&old, otmr))
  507. return -EFAULT;
  508. return ret;
  509. }
  510. SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
  511. struct old_itimerspec32 __user *, otmr)
  512. {
  513. struct itimerspec64 kotmr;
  514. int ret = do_timerfd_gettime(ufd, &kotmr);
  515. if (ret)
  516. return ret;
  517. return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
  518. }
  519. #endif