timerfd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 != 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(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(&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 != KTIME_MAX)
  123. return false;
  124. ctx->moffs = ktime_mono_to_real(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 < 0 ? 0: remaining;
  152. }
  153. static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  154. const struct itimerspec64 *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 = timespec64_to_ktime(ktmr->it_value);
  162. ctx->expired = 0;
  163. ctx->ticks = 0;
  164. ctx->tintv = timespec64_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 != 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) {
  239. /*
  240. * If tintv != 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. if ((clockid == CLOCK_REALTIME_ALARM ||
  354. clockid == CLOCK_BOOTTIME_ALARM) &&
  355. !capable(CAP_WAKE_ALARM))
  356. return -EPERM;
  357. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  358. if (!ctx)
  359. return -ENOMEM;
  360. init_waitqueue_head(&ctx->wqh);
  361. spin_lock_init(&ctx->cancel_lock);
  362. ctx->clockid = clockid;
  363. if (isalarm(ctx))
  364. alarm_init(&ctx->t.alarm,
  365. ctx->clockid == CLOCK_REALTIME_ALARM ?
  366. ALARM_REALTIME : ALARM_BOOTTIME,
  367. timerfd_alarmproc);
  368. else
  369. hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
  370. ctx->moffs = ktime_mono_to_real(0);
  371. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  372. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  373. if (ufd < 0)
  374. kfree(ctx);
  375. return ufd;
  376. }
  377. static int do_timerfd_settime(int ufd, int flags,
  378. const struct itimerspec64 *new,
  379. struct itimerspec64 *old)
  380. {
  381. struct fd f;
  382. struct timerfd_ctx *ctx;
  383. int ret;
  384. if ((flags & ~TFD_SETTIME_FLAGS) ||
  385. !itimerspec64_valid(new))
  386. return -EINVAL;
  387. ret = timerfd_fget(ufd, &f);
  388. if (ret)
  389. return ret;
  390. ctx = f.file->private_data;
  391. if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
  392. fdput(f);
  393. return -EPERM;
  394. }
  395. timerfd_setup_cancel(ctx, flags);
  396. /*
  397. * We need to stop the existing timer before reprogramming
  398. * it to the new values.
  399. */
  400. for (;;) {
  401. spin_lock_irq(&ctx->wqh.lock);
  402. if (isalarm(ctx)) {
  403. if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  404. break;
  405. } else {
  406. if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  407. break;
  408. }
  409. spin_unlock_irq(&ctx->wqh.lock);
  410. cpu_relax();
  411. }
  412. /*
  413. * If the timer is expired and it's periodic, we need to advance it
  414. * because the caller may want to know the previous expiration time.
  415. * We do not update "ticks" and "expired" since the timer will be
  416. * re-programmed again in the following timerfd_setup() call.
  417. */
  418. if (ctx->expired && ctx->tintv) {
  419. if (isalarm(ctx))
  420. alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  421. else
  422. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  423. }
  424. old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  425. old->it_interval = ktime_to_timespec64(ctx->tintv);
  426. /*
  427. * Re-program the timer to the new value ...
  428. */
  429. ret = timerfd_setup(ctx, flags, new);
  430. spin_unlock_irq(&ctx->wqh.lock);
  431. fdput(f);
  432. return ret;
  433. }
  434. static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
  435. {
  436. struct fd f;
  437. struct timerfd_ctx *ctx;
  438. int ret = timerfd_fget(ufd, &f);
  439. if (ret)
  440. return ret;
  441. ctx = f.file->private_data;
  442. spin_lock_irq(&ctx->wqh.lock);
  443. if (ctx->expired && ctx->tintv) {
  444. ctx->expired = 0;
  445. if (isalarm(ctx)) {
  446. ctx->ticks +=
  447. alarm_forward_now(
  448. &ctx->t.alarm, ctx->tintv) - 1;
  449. alarm_restart(&ctx->t.alarm);
  450. } else {
  451. ctx->ticks +=
  452. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
  453. - 1;
  454. hrtimer_restart(&ctx->t.tmr);
  455. }
  456. }
  457. t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
  458. t->it_interval = ktime_to_timespec64(ctx->tintv);
  459. spin_unlock_irq(&ctx->wqh.lock);
  460. fdput(f);
  461. return 0;
  462. }
  463. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  464. const struct itimerspec __user *, utmr,
  465. struct itimerspec __user *, otmr)
  466. {
  467. struct itimerspec64 new, old;
  468. int ret;
  469. if (get_itimerspec64(&new, utmr))
  470. return -EFAULT;
  471. ret = do_timerfd_settime(ufd, flags, &new, &old);
  472. if (ret)
  473. return ret;
  474. if (otmr && put_itimerspec64(&old, otmr))
  475. return -EFAULT;
  476. return ret;
  477. }
  478. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  479. {
  480. struct itimerspec64 kotmr;
  481. int ret = do_timerfd_gettime(ufd, &kotmr);
  482. if (ret)
  483. return ret;
  484. return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
  485. }
  486. #ifdef CONFIG_COMPAT
  487. COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  488. const struct compat_itimerspec __user *, utmr,
  489. struct compat_itimerspec __user *, otmr)
  490. {
  491. struct itimerspec64 new, old;
  492. int ret;
  493. if (get_compat_itimerspec64(&new, utmr))
  494. return -EFAULT;
  495. ret = do_timerfd_settime(ufd, flags, &new, &old);
  496. if (ret)
  497. return ret;
  498. if (otmr && put_compat_itimerspec64(&old, otmr))
  499. return -EFAULT;
  500. return ret;
  501. }
  502. COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
  503. struct compat_itimerspec __user *, otmr)
  504. {
  505. struct itimerspec64 kotmr;
  506. int ret = do_timerfd_gettime(ufd, &kotmr);
  507. if (ret)
  508. return ret;
  509. return put_compat_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
  510. }
  511. #endif