timerfd.c 14 KB

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