timerfd.c 14 KB

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