timerfd.c 13 KB

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