timerfd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 void 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. 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,
  270. (unsigned long long)ctx->ticks,
  271. ctx->settime_flags,
  272. (unsigned long long)t.it_value.tv_sec,
  273. (unsigned long long)t.it_value.tv_nsec,
  274. (unsigned long long)t.it_interval.tv_sec,
  275. (unsigned long long)t.it_interval.tv_nsec);
  276. }
  277. #else
  278. #define timerfd_show NULL
  279. #endif
  280. #ifdef CONFIG_CHECKPOINT_RESTORE
  281. static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  282. {
  283. struct timerfd_ctx *ctx = file->private_data;
  284. int ret = 0;
  285. switch (cmd) {
  286. case TFD_IOC_SET_TICKS: {
  287. u64 ticks;
  288. if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
  289. return -EFAULT;
  290. if (!ticks)
  291. return -EINVAL;
  292. spin_lock_irq(&ctx->wqh.lock);
  293. if (!timerfd_canceled(ctx)) {
  294. ctx->ticks = ticks;
  295. wake_up_locked(&ctx->wqh);
  296. } else
  297. ret = -ECANCELED;
  298. spin_unlock_irq(&ctx->wqh.lock);
  299. break;
  300. }
  301. default:
  302. ret = -ENOTTY;
  303. break;
  304. }
  305. return ret;
  306. }
  307. #else
  308. #define timerfd_ioctl NULL
  309. #endif
  310. static const struct file_operations timerfd_fops = {
  311. .release = timerfd_release,
  312. .poll = timerfd_poll,
  313. .read = timerfd_read,
  314. .llseek = noop_llseek,
  315. .show_fdinfo = timerfd_show,
  316. .unlocked_ioctl = timerfd_ioctl,
  317. };
  318. static int timerfd_fget(int fd, struct fd *p)
  319. {
  320. struct fd f = fdget(fd);
  321. if (!f.file)
  322. return -EBADF;
  323. if (f.file->f_op != &timerfd_fops) {
  324. fdput(f);
  325. return -EINVAL;
  326. }
  327. *p = f;
  328. return 0;
  329. }
  330. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  331. {
  332. int ufd;
  333. struct timerfd_ctx *ctx;
  334. /* Check the TFD_* constants for consistency. */
  335. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  336. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  337. if ((flags & ~TFD_CREATE_FLAGS) ||
  338. (clockid != CLOCK_MONOTONIC &&
  339. clockid != CLOCK_REALTIME &&
  340. clockid != CLOCK_REALTIME_ALARM &&
  341. clockid != CLOCK_BOOTTIME &&
  342. clockid != CLOCK_BOOTTIME_ALARM))
  343. return -EINVAL;
  344. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  345. if (!ctx)
  346. return -ENOMEM;
  347. init_waitqueue_head(&ctx->wqh);
  348. ctx->clockid = clockid;
  349. if (isalarm(ctx))
  350. alarm_init(&ctx->t.alarm,
  351. ctx->clockid == CLOCK_REALTIME_ALARM ?
  352. ALARM_REALTIME : ALARM_BOOTTIME,
  353. timerfd_alarmproc);
  354. else
  355. hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
  356. ctx->moffs = ktime_mono_to_real((ktime_t){ .tv64 = 0 });
  357. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  358. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  359. if (ufd < 0)
  360. kfree(ctx);
  361. return ufd;
  362. }
  363. static int do_timerfd_settime(int ufd, int flags,
  364. const struct itimerspec *new,
  365. struct itimerspec *old)
  366. {
  367. struct fd f;
  368. struct timerfd_ctx *ctx;
  369. int ret;
  370. if ((flags & ~TFD_SETTIME_FLAGS) ||
  371. !timespec_valid(&new->it_value) ||
  372. !timespec_valid(&new->it_interval))
  373. return -EINVAL;
  374. ret = timerfd_fget(ufd, &f);
  375. if (ret)
  376. return ret;
  377. ctx = f.file->private_data;
  378. timerfd_setup_cancel(ctx, flags);
  379. /*
  380. * We need to stop the existing timer before reprogramming
  381. * it to the new values.
  382. */
  383. for (;;) {
  384. spin_lock_irq(&ctx->wqh.lock);
  385. if (isalarm(ctx)) {
  386. if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  387. break;
  388. } else {
  389. if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  390. break;
  391. }
  392. spin_unlock_irq(&ctx->wqh.lock);
  393. cpu_relax();
  394. }
  395. /*
  396. * If the timer is expired and it's periodic, we need to advance it
  397. * because the caller may want to know the previous expiration time.
  398. * We do not update "ticks" and "expired" since the timer will be
  399. * re-programmed again in the following timerfd_setup() call.
  400. */
  401. if (ctx->expired && ctx->tintv.tv64) {
  402. if (isalarm(ctx))
  403. alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  404. else
  405. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  406. }
  407. old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  408. old->it_interval = ktime_to_timespec(ctx->tintv);
  409. /*
  410. * Re-program the timer to the new value ...
  411. */
  412. ret = timerfd_setup(ctx, flags, new);
  413. spin_unlock_irq(&ctx->wqh.lock);
  414. fdput(f);
  415. return ret;
  416. }
  417. static int do_timerfd_gettime(int ufd, struct itimerspec *t)
  418. {
  419. struct fd f;
  420. struct timerfd_ctx *ctx;
  421. int ret = timerfd_fget(ufd, &f);
  422. if (ret)
  423. return ret;
  424. ctx = f.file->private_data;
  425. spin_lock_irq(&ctx->wqh.lock);
  426. if (ctx->expired && ctx->tintv.tv64) {
  427. ctx->expired = 0;
  428. if (isalarm(ctx)) {
  429. ctx->ticks +=
  430. alarm_forward_now(
  431. &ctx->t.alarm, ctx->tintv) - 1;
  432. alarm_restart(&ctx->t.alarm);
  433. } else {
  434. ctx->ticks +=
  435. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
  436. - 1;
  437. hrtimer_restart(&ctx->t.tmr);
  438. }
  439. }
  440. t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  441. t->it_interval = ktime_to_timespec(ctx->tintv);
  442. spin_unlock_irq(&ctx->wqh.lock);
  443. fdput(f);
  444. return 0;
  445. }
  446. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  447. const struct itimerspec __user *, utmr,
  448. struct itimerspec __user *, otmr)
  449. {
  450. struct itimerspec new, old;
  451. int ret;
  452. if (copy_from_user(&new, utmr, sizeof(new)))
  453. return -EFAULT;
  454. ret = do_timerfd_settime(ufd, flags, &new, &old);
  455. if (ret)
  456. return ret;
  457. if (otmr && copy_to_user(otmr, &old, sizeof(old)))
  458. return -EFAULT;
  459. return ret;
  460. }
  461. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  462. {
  463. struct itimerspec kotmr;
  464. int ret = do_timerfd_gettime(ufd, &kotmr);
  465. if (ret)
  466. return ret;
  467. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  468. }
  469. #ifdef CONFIG_COMPAT
  470. COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  471. const struct compat_itimerspec __user *, utmr,
  472. struct compat_itimerspec __user *, otmr)
  473. {
  474. struct itimerspec new, old;
  475. int ret;
  476. if (get_compat_itimerspec(&new, utmr))
  477. return -EFAULT;
  478. ret = do_timerfd_settime(ufd, flags, &new, &old);
  479. if (ret)
  480. return ret;
  481. if (otmr && put_compat_itimerspec(otmr, &old))
  482. return -EFAULT;
  483. return ret;
  484. }
  485. COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
  486. struct compat_itimerspec __user *, otmr)
  487. {
  488. struct itimerspec kotmr;
  489. int ret = do_timerfd_gettime(ufd, &kotmr);
  490. if (ret)
  491. return ret;
  492. return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
  493. }
  494. #endif