timerfd.c 13 KB

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