timerfd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 expired;
  37. int clockid;
  38. struct rcu_head rcu;
  39. struct list_head clist;
  40. spinlock_t cancel_lock;
  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_get_monotonic_offset();
  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 void timerfd_remove_cancel(struct timerfd_ctx *ctx)
  114. {
  115. spin_lock(&ctx->cancel_lock);
  116. __timerfd_remove_cancel(ctx);
  117. spin_unlock(&ctx->cancel_lock);
  118. }
  119. static bool timerfd_canceled(struct timerfd_ctx *ctx)
  120. {
  121. if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
  122. return false;
  123. ctx->moffs = ktime_get_monotonic_offset();
  124. return true;
  125. }
  126. static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
  127. {
  128. spin_lock(&ctx->cancel_lock);
  129. if ((ctx->clockid == CLOCK_REALTIME ||
  130. ctx->clockid == CLOCK_REALTIME_ALARM) &&
  131. (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
  132. if (!ctx->might_cancel) {
  133. ctx->might_cancel = true;
  134. spin_lock(&cancel_lock);
  135. list_add_rcu(&ctx->clist, &cancel_list);
  136. spin_unlock(&cancel_lock);
  137. }
  138. } else {
  139. __timerfd_remove_cancel(ctx);
  140. }
  141. spin_unlock(&ctx->cancel_lock);
  142. }
  143. static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
  144. {
  145. ktime_t remaining;
  146. if (isalarm(ctx))
  147. remaining = alarm_expires_remaining(&ctx->t.alarm);
  148. else
  149. remaining = hrtimer_expires_remaining(&ctx->t.tmr);
  150. return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
  151. }
  152. static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
  153. const struct itimerspec *ktmr)
  154. {
  155. enum hrtimer_mode htmode;
  156. ktime_t texp;
  157. int clockid = ctx->clockid;
  158. htmode = (flags & TFD_TIMER_ABSTIME) ?
  159. HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
  160. texp = timespec_to_ktime(ktmr->it_value);
  161. ctx->expired = 0;
  162. ctx->ticks = 0;
  163. ctx->tintv = timespec_to_ktime(ktmr->it_interval);
  164. if (isalarm(ctx)) {
  165. alarm_init(&ctx->t.alarm,
  166. ctx->clockid == CLOCK_REALTIME_ALARM ?
  167. ALARM_REALTIME : ALARM_BOOTTIME,
  168. timerfd_alarmproc);
  169. } else {
  170. hrtimer_init(&ctx->t.tmr, clockid, htmode);
  171. hrtimer_set_expires(&ctx->t.tmr, texp);
  172. ctx->t.tmr.function = timerfd_tmrproc;
  173. }
  174. if (texp.tv64 != 0) {
  175. if (isalarm(ctx)) {
  176. if (flags & TFD_TIMER_ABSTIME)
  177. alarm_start(&ctx->t.alarm, texp);
  178. else
  179. alarm_start_relative(&ctx->t.alarm, texp);
  180. } else {
  181. hrtimer_start(&ctx->t.tmr, texp, htmode);
  182. }
  183. if (timerfd_canceled(ctx))
  184. return -ECANCELED;
  185. }
  186. return 0;
  187. }
  188. static int timerfd_release(struct inode *inode, struct file *file)
  189. {
  190. struct timerfd_ctx *ctx = file->private_data;
  191. timerfd_remove_cancel(ctx);
  192. if (isalarm(ctx))
  193. alarm_cancel(&ctx->t.alarm);
  194. else
  195. hrtimer_cancel(&ctx->t.tmr);
  196. kfree_rcu(ctx, rcu);
  197. return 0;
  198. }
  199. static unsigned int timerfd_poll(struct file *file, poll_table *wait)
  200. {
  201. struct timerfd_ctx *ctx = file->private_data;
  202. unsigned int events = 0;
  203. unsigned long flags;
  204. poll_wait(file, &ctx->wqh, wait);
  205. spin_lock_irqsave(&ctx->wqh.lock, flags);
  206. if (ctx->ticks)
  207. events |= POLLIN;
  208. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  209. return events;
  210. }
  211. static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
  212. loff_t *ppos)
  213. {
  214. struct timerfd_ctx *ctx = file->private_data;
  215. ssize_t res;
  216. u64 ticks = 0;
  217. if (count < sizeof(ticks))
  218. return -EINVAL;
  219. spin_lock_irq(&ctx->wqh.lock);
  220. if (file->f_flags & O_NONBLOCK)
  221. res = -EAGAIN;
  222. else
  223. res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
  224. /*
  225. * If clock has changed, we do not care about the
  226. * ticks and we do not rearm the timer. Userspace must
  227. * reevaluate anyway.
  228. */
  229. if (timerfd_canceled(ctx)) {
  230. ctx->ticks = 0;
  231. ctx->expired = 0;
  232. res = -ECANCELED;
  233. }
  234. if (ctx->ticks) {
  235. ticks = ctx->ticks;
  236. if (ctx->expired && ctx->tintv.tv64) {
  237. /*
  238. * If tintv.tv64 != 0, this is a periodic timer that
  239. * needs to be re-armed. We avoid doing it in the timer
  240. * callback to avoid DoS attacks specifying a very
  241. * short timer period.
  242. */
  243. if (isalarm(ctx)) {
  244. ticks += alarm_forward_now(
  245. &ctx->t.alarm, ctx->tintv) - 1;
  246. alarm_restart(&ctx->t.alarm);
  247. } else {
  248. ticks += hrtimer_forward_now(&ctx->t.tmr,
  249. ctx->tintv) - 1;
  250. hrtimer_restart(&ctx->t.tmr);
  251. }
  252. }
  253. ctx->expired = 0;
  254. ctx->ticks = 0;
  255. }
  256. spin_unlock_irq(&ctx->wqh.lock);
  257. if (ticks)
  258. res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
  259. return res;
  260. }
  261. static const struct file_operations timerfd_fops = {
  262. .release = timerfd_release,
  263. .poll = timerfd_poll,
  264. .read = timerfd_read,
  265. .llseek = noop_llseek,
  266. };
  267. static struct file *timerfd_fget(int fd, int *fput_needed)
  268. {
  269. struct file *file;
  270. file = fget_light(fd, fput_needed);
  271. if (!file)
  272. return ERR_PTR(-EBADF);
  273. if (file->f_op != &timerfd_fops) {
  274. fput_light(file, *fput_needed);
  275. return ERR_PTR(-EINVAL);
  276. }
  277. return file;
  278. }
  279. SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
  280. {
  281. int ufd;
  282. struct timerfd_ctx *ctx;
  283. /* Check the TFD_* constants for consistency. */
  284. BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
  285. BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
  286. if ((flags & ~TFD_CREATE_FLAGS) ||
  287. (clockid != CLOCK_MONOTONIC &&
  288. clockid != CLOCK_REALTIME &&
  289. clockid != CLOCK_REALTIME_ALARM &&
  290. clockid != CLOCK_BOOTTIME &&
  291. clockid != CLOCK_BOOTTIME_ALARM))
  292. return -EINVAL;
  293. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  294. if (!ctx)
  295. return -ENOMEM;
  296. init_waitqueue_head(&ctx->wqh);
  297. spin_lock_init(&ctx->cancel_lock);
  298. ctx->clockid = clockid;
  299. if (isalarm(ctx))
  300. alarm_init(&ctx->t.alarm,
  301. ctx->clockid == CLOCK_REALTIME_ALARM ?
  302. ALARM_REALTIME : ALARM_BOOTTIME,
  303. timerfd_alarmproc);
  304. else
  305. hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
  306. ctx->moffs = ktime_get_monotonic_offset();
  307. ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
  308. O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
  309. if (ufd < 0)
  310. kfree(ctx);
  311. return ufd;
  312. }
  313. static int do_timerfd_settime(int ufd, int flags,
  314. const struct itimerspec *new,
  315. struct itimerspec *old)
  316. {
  317. struct file *file;
  318. struct timerfd_ctx *ctx;
  319. int ret, fput_needed;
  320. if ((flags & ~TFD_SETTIME_FLAGS) ||
  321. !timespec_valid(&new->it_value) ||
  322. !timespec_valid(&new->it_interval))
  323. return -EINVAL;
  324. file = timerfd_fget(ufd, &fput_needed);
  325. if (IS_ERR(file))
  326. return PTR_ERR(file);
  327. ctx = file->private_data;
  328. timerfd_setup_cancel(ctx, flags);
  329. /*
  330. * We need to stop the existing timer before reprogramming
  331. * it to the new values.
  332. */
  333. for (;;) {
  334. spin_lock_irq(&ctx->wqh.lock);
  335. if (isalarm(ctx)) {
  336. if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
  337. break;
  338. } else {
  339. if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
  340. break;
  341. }
  342. spin_unlock_irq(&ctx->wqh.lock);
  343. cpu_relax();
  344. }
  345. /*
  346. * If the timer is expired and it's periodic, we need to advance it
  347. * because the caller may want to know the previous expiration time.
  348. * We do not update "ticks" and "expired" since the timer will be
  349. * re-programmed again in the following timerfd_setup() call.
  350. */
  351. if (ctx->expired && ctx->tintv.tv64) {
  352. if (isalarm(ctx))
  353. alarm_forward_now(&ctx->t.alarm, ctx->tintv);
  354. else
  355. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
  356. }
  357. old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  358. old->it_interval = ktime_to_timespec(ctx->tintv);
  359. /*
  360. * Re-program the timer to the new value ...
  361. */
  362. ret = timerfd_setup(ctx, flags, new);
  363. spin_unlock_irq(&ctx->wqh.lock);
  364. fput_light(file, fput_needed);
  365. return ret;
  366. }
  367. static int do_timerfd_gettime(int ufd, struct itimerspec *t)
  368. {
  369. struct file *file;
  370. struct timerfd_ctx *ctx;
  371. int fput_needed;
  372. file = timerfd_fget(ufd, &fput_needed);
  373. if (IS_ERR(file))
  374. return PTR_ERR(file);
  375. ctx = file->private_data;
  376. spin_lock_irq(&ctx->wqh.lock);
  377. if (ctx->expired && ctx->tintv.tv64) {
  378. ctx->expired = 0;
  379. if (isalarm(ctx)) {
  380. ctx->ticks +=
  381. alarm_forward_now(
  382. &ctx->t.alarm, ctx->tintv) - 1;
  383. alarm_restart(&ctx->t.alarm);
  384. } else {
  385. ctx->ticks +=
  386. hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
  387. - 1;
  388. hrtimer_restart(&ctx->t.tmr);
  389. }
  390. }
  391. t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
  392. t->it_interval = ktime_to_timespec(ctx->tintv);
  393. spin_unlock_irq(&ctx->wqh.lock);
  394. fput_light(file, fput_needed);
  395. return 0;
  396. }
  397. SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  398. const struct itimerspec __user *, utmr,
  399. struct itimerspec __user *, otmr)
  400. {
  401. struct itimerspec new, old;
  402. int ret;
  403. if (copy_from_user(&new, utmr, sizeof(new)))
  404. return -EFAULT;
  405. ret = do_timerfd_settime(ufd, flags, &new, &old);
  406. if (ret)
  407. return ret;
  408. if (otmr && copy_to_user(otmr, &old, sizeof(old)))
  409. return -EFAULT;
  410. return ret;
  411. }
  412. SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
  413. {
  414. struct itimerspec kotmr;
  415. int ret = do_timerfd_gettime(ufd, &kotmr);
  416. if (ret)
  417. return ret;
  418. return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
  419. }
  420. #ifdef CONFIG_COMPAT
  421. COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
  422. const struct compat_itimerspec __user *, utmr,
  423. struct compat_itimerspec __user *, otmr)
  424. {
  425. struct itimerspec new, old;
  426. int ret;
  427. if (get_compat_itimerspec(&new, utmr))
  428. return -EFAULT;
  429. ret = do_timerfd_settime(ufd, flags, &new, &old);
  430. if (ret)
  431. return ret;
  432. if (otmr && put_compat_itimerspec(otmr, &old))
  433. return -EFAULT;
  434. return ret;
  435. }
  436. COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
  437. struct compat_itimerspec __user *, otmr)
  438. {
  439. struct itimerspec kotmr;
  440. int ret = do_timerfd_gettime(ufd, &kotmr);
  441. if (ret)
  442. return ret;
  443. return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
  444. }
  445. #endif