timerfd.c 13 KB

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