timerfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/rcupdate.h>
  26. struct timerfd_ctx {
  27. union {
  28. struct hrtimer tmr;
  29. struct alarm alarm;
  30. } t;
  31. ktime_t tintv;
  32. ktime_t moffs;
  33. wait_queue_head_t wqh;
  34. u64 ticks;
  35. int expired;
  36. int clockid;
  37. struct rcu_head rcu;
  38. struct list_head clist;
  39. spinlock_t cancel_lock;
  40. bool might_cancel;
  41. };
  42. static LIST_HEAD(cancel_list);
  43. static DEFINE_SPINLOCK(cancel_lock);
  44. static inline bool isalarm(struct timerfd_ctx *ctx)
  45. {
  46. return ctx->clockid == CLOCK_REALTIME_ALARM ||
  47. ctx->clockid == CLOCK_BOOTTIME_ALARM;
  48. }
  49. /*
  50. * This gets called when the timer event triggers. We set the "expired"
  51. * flag, but we do not re-arm the timer (in case it's necessary,
  52. * tintv.tv64 != 0) until the timer is accessed.
  53. */
  54. static void timerfd_triggered(struct timerfd_ctx *ctx)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&ctx->wqh.lock, flags);
  58. ctx->expired = 1;
  59. ctx->ticks++;
  60. wake_up_locked(&ctx->wqh);
  61. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  62. }
  63. static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
  64. {
  65. struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
  66. t.tmr);
  67. timerfd_triggered(ctx);
  68. return HRTIMER_NORESTART;
  69. }
  70. static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
  71. ktime_t now)
  72. {
  73. struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
  74. t.alarm);
  75. timerfd_triggered(ctx);
  76. return ALARMTIMER_NORESTART;
  77. }
  78. /*
  79. * Called when the clock was set to cancel the timers in the cancel
  80. * list. This will wake up processes waiting on these timers. The
  81. * wake-up requires ctx->ticks to be non zero, therefore we increment
  82. * it before calling wake_up_locked().
  83. */
  84. void timerfd_clock_was_set(void)
  85. {
  86. ktime_t moffs = ktime_get_monotonic_offset();
  87. struct timerfd_ctx *ctx;
  88. unsigned long flags;
  89. rcu_read_lock();
  90. list_for_each_entry_rcu(ctx, &cancel_list, clist) {
  91. if (!ctx->might_cancel)
  92. continue;
  93. spin_lock_irqsave(&ctx->wqh.lock, flags);
  94. if (ctx->moffs.tv64 != moffs.tv64) {
  95. ctx->moffs.tv64 = KTIME_MAX;
  96. ctx->ticks++;
  97. wake_up_locked(&ctx->wqh);
  98. }
  99. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  100. }
  101. rcu_read_unlock();
  102. }
  103. static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
  104. {
  105. if (ctx->might_cancel) {
  106. ctx->might_cancel = false;
  107. spin_lock(&cancel_lock);
  108. list_del_rcu(&ctx->clist);
  109. spin_unlock(&cancel_lock);
  110. }
  111. }
  112. static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
  113. {
  114. spin_lock(&ctx->cancel_lock);
  115. __timerfd_remove_cancel(ctx);
  116. spin_unlock(&ctx->cancel_lock);
  117. }
  118. static bool timerfd_canceled(struct timerfd_ctx *ctx)
  119. {
  120. if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
  121. return false;
  122. ctx->moffs = ktime_get_monotonic_offset();
  123. return true;
  124. }
  125. static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  126. {
  127. spin_lock(&ctx->cancel_lock);
  128. if ((ctx->clockid == CLOCK_REALTIME ||
  129. ctx->clockid == CLOCK_REALTIME_ALARM) &&
  130. (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
  131. if (!ctx->might_cancel) {
  132. ctx->might_cancel = true;
  133. spin_lock(&cancel_lock);
  134. list_add_rcu(&ctx->clist, &cancel_list);
  135. spin_unlock(&cancel_lock);
  136. }
  137. } else {
  138. __timerfd_remove_cancel(ctx);
  139. }
  140. spin_unlock(&ctx->cancel_lock);
  141. }
  142. static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  143. {
  144. ktime_t remaining;
  145. if (isalarm(ctx))
  146. remaining = alarm_expires_remaining(&ctx->t.alarm);
  147. else
  148. remaining = hrtimer_expires_remaining(&ctx->t.tmr);
  149. return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
  150. }
  151. static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  152. const struct itimerspec *ktmr)
  153. {
  154. enum hrtimer_mode htmode;
  155. ktime_t texp;
  156. int clockid = ctx->clockid;
  157. htmode = (flags & TFD_TIMER_ABSTIME) ?
  158. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  159. texp = timespec_to_ktime(ktmr->it_value);
  160. ctx->expired = 0;
  161. ctx->ticks = 0;
  162. ctx->tintv = timespec_to_ktime(ktmr->it_interval);
  163. if (isalarm(ctx)) {
  164. alarm_init(&ctx->t.alarm,
  165. ctx->clockid == CLOCK_REALTIME_ALARM ?
  166. ALARM_REALTIME : ALARM_BOOTTIME,
  167. timerfd_alarmproc);
  168. } else {
  169. hrtimer_init(&ctx->t.tmr, clockid, htmode);
  170. hrtimer_set_expires(&ctx->t.tmr, texp);
  171. ctx->t.tmr.function = timerfd_tmrproc;
  172. }
  173. if (texp.tv64 != 0) {
  174. if (isalarm(ctx)) {
  175. if (flags & TFD_TIMER_ABSTIME)
  176. alarm_start(&ctx->t.alarm, texp);
  177. else
  178. alarm_start_relative(&ctx->t.alarm, texp);
  179. } else {
  180. hrtimer_start(&ctx->t.tmr, texp, htmode);
  181. }
  182. if (timerfd_canceled(ctx))
  183. return -ECANCELED;
  184. }
  185. return 0;
  186. }
  187. static int timerfd_release(struct inode *inode, struct file *file)
  188. {
  189. struct timerfd_ctx *ctx = file->private_data;
  190. timerfd_remove_cancel(ctx);
  191. if (isalarm(ctx))
  192. alarm_cancel(&ctx->t.alarm);
  193. else
  194. hrtimer_cancel(&ctx->t.tmr);
  195. kfree_rcu(ctx, rcu);
  196. return 0;
  197. }
  198. static unsigned int timerfd_poll(struct file *file, poll_table *wait)
  199. {
  200. struct timerfd_ctx *ctx = file->private_data;
  201. unsigned int events = 0;
  202. unsigned long flags;
  203. poll_wait(file, &ctx->wqh, wait);
  204. spin_lock_irqsave(&ctx->wqh.lock, flags);
  205. if (ctx->ticks)
  206. events |= POLLIN;
  207. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  208. return events;
  209. }
  210. static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
  211. loff_t *ppos)
  212. {
  213. struct timerfd_ctx *ctx = file->private_data;
  214. ssize_t res;
  215. u64 ticks = 0;
  216. if (count < sizeof(ticks))
  217. return -EINVAL;
  218. spin_lock_irq(&ctx->wqh.lock);
  219. if (file->f_flags & O_NONBLOCK)
  220. res = -EAGAIN;
  221. else
  222. res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
  223. /*
  224. * If clock has changed, we do not care about the
  225. * ticks and we do not rearm the timer. Userspace must
  226. * reevaluate anyway.
  227. */
  228. if (timerfd_canceled(ctx)) {
  229. ctx->ticks = 0;
  230. ctx->expired = 0;
  231. res = -ECANCELED;
  232. }
  233. if (ctx->ticks) {
  234. ticks = ctx->ticks;
  235. if (ctx->expired && ctx->tintv.tv64) {
  236. /*
  237. * If tintv.tv64 != 0, this is a periodic timer that
  238. * needs to be re-armed. We avoid doing it in the timer
  239. * callback to avoid DoS attacks specifying a very
  240. * short timer period.
  241. */
  242. if (isalarm(ctx)) {
  243. ticks += alarm_forward_now(
  244. &ctx->t.alarm, ctx->tintv) - 1;
  245. alarm_restart(&ctx->t.alarm);
  246. } else {
  247. ticks += hrtimer_forward_now(&ctx->t.tmr,
  248. ctx->tintv) - 1;
  249. hrtimer_restart(&ctx->t.tmr);
  250. }
  251. }
  252. ctx->expired = 0;
  253. ctx->ticks = 0;
  254. }
  255. spin_unlock_irq(&ctx->wqh.lock);
  256. if (ticks)
  257. res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
  258. return res;
  259. }
  260. static const struct file_operations timerfd_fops = {
  261. .release = timerfd_release,
  262. .poll = timerfd_poll,
  263. .read = timerfd_read,
  264. .llseek = noop_llseek,
  265. };
  266. static struct file *timerfd_fget(int fd, int *fput_needed)
  267. {
  268. struct file *file;
  269. file = fget_light(fd, fput_needed);
  270. if (!file)
  271. return ERR_PTR(-EBADF);
  272. if (file->f_op != &timerfd_fops) {
  273. fput_light(file, *fput_needed);
  274. return ERR_PTR(-EINVAL);
  275. }
  276. return file;
  277. }
  278. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  279. {
  280. int ufd;
  281. struct timerfd_ctx *ctx;
  282. /* Check the TFD_* constants for consistency. */
  283. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  284. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  285. if ((flags & ~TFD_CREATE_FLAGS) ||
  286. (clockid != CLOCK_MONOTONIC &&
  287. clockid != CLOCK_REALTIME &&
  288. clockid != CLOCK_REALTIME_ALARM &&
  289. clockid != CLOCK_BOOTTIME &&
  290. clockid != CLOCK_BOOTTIME_ALARM))
  291. return -EINVAL;
  292. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  293. if (!ctx)
  294. return -ENOMEM;
  295. init_waitqueue_head(&ctx->wqh);
  296. spin_lock_init(&ctx->cancel_lock);
  297. ctx->clockid = clockid;
  298. if (isalarm(ctx))
  299. alarm_init(&ctx->t.alarm,
  300. ctx->clockid == CLOCK_REALTIME_ALARM ?
  301. ALARM_REALTIME : ALARM_BOOTTIME,
  302. timerfd_alarmproc);
  303. else
  304. hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
  305. ctx->moffs = ktime_get_monotonic_offset();
  306. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  307. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  308. if (ufd < 0)
  309. kfree(ctx);
  310. return ufd;
  311. }
  312. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  313. const struct itimerspec __user *, utmr,
  314. struct itimerspec __user *, otmr)
  315. {
  316. struct file *file;
  317. struct timerfd_ctx *ctx;
  318. struct itimerspec ktmr, kotmr;
  319. int ret, fput_needed;
  320. if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
  321. return -EFAULT;
  322. if ((flags & ~TFD_SETTIME_FLAGS) ||
  323. !timespec_valid(&ktmr.it_value) ||
  324. !timespec_valid(&ktmr.it_interval))
  325. return -EINVAL;
  326. file = timerfd_fget(ufd, &fput_needed);
  327. if (IS_ERR(file))
  328. return PTR_ERR(file);
  329. ctx = file->private_data;
  330. timerfd_setup_cancel(ctx, flags);
  331. /*
  332. * We need to stop the existing timer before reprogramming
  333. * it to the new values.
  334. */
  335. for (;;) {
  336. spin_lock_irq(&ctx->wqh.lock);
  337. if (isalarm(ctx)) {
  338. if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  339. break;
  340. } else {
  341. if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  342. break;
  343. }
  344. spin_unlock_irq(&ctx->wqh.lock);
  345. cpu_relax();
  346. }
  347. /*
  348. * If the timer is expired and it's periodic, we need to advance it
  349. * because the caller may want to know the previous expiration time.
  350. * We do not update "ticks" and "expired" since the timer will be
  351. * re-programmed again in the following timerfd_setup() call.
  352. */
  353. if (ctx->expired && ctx->tintv.tv64) {
  354. if (isalarm(ctx))
  355. alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  356. else
  357. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  358. }
  359. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  360. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  361. /*
  362. * Re-program the timer to the new value ...
  363. */
  364. ret = timerfd_setup(ctx, flags, &ktmr);
  365. spin_unlock_irq(&ctx->wqh.lock);
  366. fput_light(file, fput_needed);
  367. if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
  368. return -EFAULT;
  369. return ret;
  370. }
  371. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  372. {
  373. struct file *file;
  374. struct timerfd_ctx *ctx;
  375. struct itimerspec kotmr;
  376. int fput_needed;
  377. file = timerfd_fget(ufd, &fput_needed);
  378. if (IS_ERR(file))
  379. return PTR_ERR(file);
  380. ctx = file->private_data;
  381. spin_lock_irq(&ctx->wqh.lock);
  382. if (ctx->expired && ctx->tintv.tv64) {
  383. ctx->expired = 0;
  384. if (isalarm(ctx)) {
  385. ctx->ticks +=
  386. alarm_forward_now(
  387. &ctx->t.alarm, ctx->tintv) - 1;
  388. alarm_restart(&ctx->t.alarm);
  389. } else {
  390. ctx->ticks +=
  391. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
  392. - 1;
  393. hrtimer_restart(&ctx->t.tmr);
  394. }
  395. }
  396. kotmr.it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  397. kotmr.it_interval = ktime_to_timespec(ctx->tintv);
  398. spin_unlock_irq(&ctx->wqh.lock);
  399. fput_light(file, fput_needed);
  400. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  401. }