eventfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * fs/eventfd.c
  3. *
  4. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  5. *
  6. */
  7. #include <linux/file.h>
  8. #include <linux/poll.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/anon_inodes.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/export.h>
  19. #include <linux/kref.h>
  20. #include <linux/eventfd.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. DEFINE_PER_CPU(int, eventfd_wake_count);
  24. struct eventfd_ctx {
  25. struct kref kref;
  26. wait_queue_head_t wqh;
  27. /*
  28. * Every time that a write(2) is performed on an eventfd, the
  29. * value of the __u64 being written is added to "count" and a
  30. * wakeup is performed on "wqh". A read(2) will return the "count"
  31. * value to userspace, and will reset "count" to zero. The kernel
  32. * side eventfd_signal() also, adds to the "count" counter and
  33. * issue a wakeup.
  34. */
  35. __u64 count;
  36. unsigned int flags;
  37. };
  38. /**
  39. * eventfd_signal - Adds @n to the eventfd counter.
  40. * @ctx: [in] Pointer to the eventfd context.
  41. * @n: [in] Value of the counter to be added to the eventfd internal counter.
  42. * The value cannot be negative.
  43. *
  44. * This function is supposed to be called by the kernel in paths that do not
  45. * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
  46. * value, and we signal this as overflow condition by returning a EPOLLERR
  47. * to poll(2).
  48. *
  49. * Returns the amount by which the counter was incremented. This will be less
  50. * than @n if the counter has overflowed.
  51. */
  52. __u64 eventfd_signal(struct eventfd_ctx *ctx, __u64 n)
  53. {
  54. unsigned long flags;
  55. /*
  56. * Deadlock or stack overflow issues can happen if we recurse here
  57. * through waitqueue wakeup handlers. If the caller users potentially
  58. * nested waitqueues with custom wakeup handlers, then it should
  59. * check eventfd_signal_count() before calling this function. If
  60. * it returns true, the eventfd_signal() call should be deferred to a
  61. * safe context.
  62. */
  63. if (WARN_ON_ONCE(this_cpu_read(eventfd_wake_count)))
  64. return 0;
  65. spin_lock_irqsave(&ctx->wqh.lock, flags);
  66. this_cpu_inc(eventfd_wake_count);
  67. if (ULLONG_MAX - ctx->count < n)
  68. n = ULLONG_MAX - ctx->count;
  69. ctx->count += n;
  70. if (waitqueue_active(&ctx->wqh))
  71. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  72. this_cpu_dec(eventfd_wake_count);
  73. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  74. return n;
  75. }
  76. EXPORT_SYMBOL_GPL(eventfd_signal);
  77. static void eventfd_free_ctx(struct eventfd_ctx *ctx)
  78. {
  79. kfree(ctx);
  80. }
  81. static void eventfd_free(struct kref *kref)
  82. {
  83. struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref);
  84. eventfd_free_ctx(ctx);
  85. }
  86. /**
  87. * eventfd_ctx_put - Releases a reference to the internal eventfd context.
  88. * @ctx: [in] Pointer to eventfd context.
  89. *
  90. * The eventfd context reference must have been previously acquired either
  91. * with eventfd_ctx_fdget() or eventfd_ctx_fileget().
  92. */
  93. void eventfd_ctx_put(struct eventfd_ctx *ctx)
  94. {
  95. kref_put(&ctx->kref, eventfd_free);
  96. }
  97. EXPORT_SYMBOL_GPL(eventfd_ctx_put);
  98. static int eventfd_release(struct inode *inode, struct file *file)
  99. {
  100. struct eventfd_ctx *ctx = file->private_data;
  101. wake_up_poll(&ctx->wqh, EPOLLHUP);
  102. eventfd_ctx_put(ctx);
  103. return 0;
  104. }
  105. static __poll_t eventfd_poll(struct file *file, poll_table *wait)
  106. {
  107. struct eventfd_ctx *ctx = file->private_data;
  108. __poll_t events = 0;
  109. u64 count;
  110. poll_wait(file, &ctx->wqh, wait);
  111. /*
  112. * All writes to ctx->count occur within ctx->wqh.lock. This read
  113. * can be done outside ctx->wqh.lock because we know that poll_wait
  114. * takes that lock (through add_wait_queue) if our caller will sleep.
  115. *
  116. * The read _can_ therefore seep into add_wait_queue's critical
  117. * section, but cannot move above it! add_wait_queue's spin_lock acts
  118. * as an acquire barrier and ensures that the read be ordered properly
  119. * against the writes. The following CAN happen and is safe:
  120. *
  121. * poll write
  122. * ----------------- ------------
  123. * lock ctx->wqh.lock (in poll_wait)
  124. * count = ctx->count
  125. * __add_wait_queue
  126. * unlock ctx->wqh.lock
  127. * lock ctx->qwh.lock
  128. * ctx->count += n
  129. * if (waitqueue_active)
  130. * wake_up_locked_poll
  131. * unlock ctx->qwh.lock
  132. * eventfd_poll returns 0
  133. *
  134. * but the following, which would miss a wakeup, cannot happen:
  135. *
  136. * poll write
  137. * ----------------- ------------
  138. * count = ctx->count (INVALID!)
  139. * lock ctx->qwh.lock
  140. * ctx->count += n
  141. * **waitqueue_active is false**
  142. * **no wake_up_locked_poll!**
  143. * unlock ctx->qwh.lock
  144. * lock ctx->wqh.lock (in poll_wait)
  145. * __add_wait_queue
  146. * unlock ctx->wqh.lock
  147. * eventfd_poll returns 0
  148. */
  149. count = READ_ONCE(ctx->count);
  150. if (count > 0)
  151. events |= EPOLLIN;
  152. if (count == ULLONG_MAX)
  153. events |= EPOLLERR;
  154. if (ULLONG_MAX - 1 > count)
  155. events |= EPOLLOUT;
  156. return events;
  157. }
  158. static void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
  159. {
  160. *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
  161. ctx->count -= *cnt;
  162. }
  163. /**
  164. * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
  165. * @ctx: [in] Pointer to eventfd context.
  166. * @wait: [in] Wait queue to be removed.
  167. * @cnt: [out] Pointer to the 64-bit counter value.
  168. *
  169. * Returns %0 if successful, or the following error codes:
  170. *
  171. * -EAGAIN : The operation would have blocked.
  172. *
  173. * This is used to atomically remove a wait queue entry from the eventfd wait
  174. * queue head, and read/reset the counter value.
  175. */
  176. int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait,
  177. __u64 *cnt)
  178. {
  179. unsigned long flags;
  180. spin_lock_irqsave(&ctx->wqh.lock, flags);
  181. eventfd_ctx_do_read(ctx, cnt);
  182. __remove_wait_queue(&ctx->wqh, wait);
  183. if (*cnt != 0 && waitqueue_active(&ctx->wqh))
  184. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  185. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  186. return *cnt != 0 ? 0 : -EAGAIN;
  187. }
  188. EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
  189. static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
  190. loff_t *ppos)
  191. {
  192. struct eventfd_ctx *ctx = file->private_data;
  193. ssize_t res;
  194. __u64 ucnt = 0;
  195. DECLARE_WAITQUEUE(wait, current);
  196. if (count < sizeof(ucnt))
  197. return -EINVAL;
  198. spin_lock_irq(&ctx->wqh.lock);
  199. res = -EAGAIN;
  200. if (ctx->count > 0)
  201. res = sizeof(ucnt);
  202. else if (!(file->f_flags & O_NONBLOCK)) {
  203. __add_wait_queue(&ctx->wqh, &wait);
  204. for (;;) {
  205. set_current_state(TASK_INTERRUPTIBLE);
  206. if (ctx->count > 0) {
  207. res = sizeof(ucnt);
  208. break;
  209. }
  210. if (signal_pending(current)) {
  211. res = -ERESTARTSYS;
  212. break;
  213. }
  214. spin_unlock_irq(&ctx->wqh.lock);
  215. schedule();
  216. spin_lock_irq(&ctx->wqh.lock);
  217. }
  218. __remove_wait_queue(&ctx->wqh, &wait);
  219. __set_current_state(TASK_RUNNING);
  220. }
  221. if (likely(res > 0)) {
  222. eventfd_ctx_do_read(ctx, &ucnt);
  223. if (waitqueue_active(&ctx->wqh))
  224. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  225. }
  226. spin_unlock_irq(&ctx->wqh.lock);
  227. if (res > 0 && put_user(ucnt, (__u64 __user *)buf))
  228. return -EFAULT;
  229. return res;
  230. }
  231. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  232. loff_t *ppos)
  233. {
  234. struct eventfd_ctx *ctx = file->private_data;
  235. ssize_t res;
  236. __u64 ucnt;
  237. DECLARE_WAITQUEUE(wait, current);
  238. if (count < sizeof(ucnt))
  239. return -EINVAL;
  240. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  241. return -EFAULT;
  242. if (ucnt == ULLONG_MAX)
  243. return -EINVAL;
  244. spin_lock_irq(&ctx->wqh.lock);
  245. res = -EAGAIN;
  246. if (ULLONG_MAX - ctx->count > ucnt)
  247. res = sizeof(ucnt);
  248. else if (!(file->f_flags & O_NONBLOCK)) {
  249. __add_wait_queue(&ctx->wqh, &wait);
  250. for (res = 0;;) {
  251. set_current_state(TASK_INTERRUPTIBLE);
  252. if (ULLONG_MAX - ctx->count > ucnt) {
  253. res = sizeof(ucnt);
  254. break;
  255. }
  256. if (signal_pending(current)) {
  257. res = -ERESTARTSYS;
  258. break;
  259. }
  260. spin_unlock_irq(&ctx->wqh.lock);
  261. schedule();
  262. spin_lock_irq(&ctx->wqh.lock);
  263. }
  264. __remove_wait_queue(&ctx->wqh, &wait);
  265. __set_current_state(TASK_RUNNING);
  266. }
  267. if (likely(res > 0)) {
  268. ctx->count += ucnt;
  269. if (waitqueue_active(&ctx->wqh))
  270. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  271. }
  272. spin_unlock_irq(&ctx->wqh.lock);
  273. return res;
  274. }
  275. #ifdef CONFIG_PROC_FS
  276. static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
  277. {
  278. struct eventfd_ctx *ctx = f->private_data;
  279. spin_lock_irq(&ctx->wqh.lock);
  280. seq_printf(m, "eventfd-count: %16llx\n",
  281. (unsigned long long)ctx->count);
  282. spin_unlock_irq(&ctx->wqh.lock);
  283. }
  284. #endif
  285. static const struct file_operations eventfd_fops = {
  286. #ifdef CONFIG_PROC_FS
  287. .show_fdinfo = eventfd_show_fdinfo,
  288. #endif
  289. .release = eventfd_release,
  290. .poll = eventfd_poll,
  291. .read = eventfd_read,
  292. .write = eventfd_write,
  293. .llseek = noop_llseek,
  294. };
  295. /**
  296. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  297. * @fd: [in] Eventfd file descriptor.
  298. *
  299. * Returns a pointer to the eventfd file structure in case of success, or the
  300. * following error pointer:
  301. *
  302. * -EBADF : Invalid @fd file descriptor.
  303. * -EINVAL : The @fd file descriptor is not an eventfd file.
  304. */
  305. struct file *eventfd_fget(int fd)
  306. {
  307. struct file *file;
  308. file = fget(fd);
  309. if (!file)
  310. return ERR_PTR(-EBADF);
  311. if (file->f_op != &eventfd_fops) {
  312. fput(file);
  313. return ERR_PTR(-EINVAL);
  314. }
  315. return file;
  316. }
  317. EXPORT_SYMBOL_GPL(eventfd_fget);
  318. /**
  319. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  320. * @fd: [in] Eventfd file descriptor.
  321. *
  322. * Returns a pointer to the internal eventfd context, otherwise the error
  323. * pointers returned by the following functions:
  324. *
  325. * eventfd_fget
  326. */
  327. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  328. {
  329. struct eventfd_ctx *ctx;
  330. struct fd f = fdget(fd);
  331. if (!f.file)
  332. return ERR_PTR(-EBADF);
  333. ctx = eventfd_ctx_fileget(f.file);
  334. fdput(f);
  335. return ctx;
  336. }
  337. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  338. /**
  339. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  340. * @file: [in] Eventfd file pointer.
  341. *
  342. * Returns a pointer to the internal eventfd context, otherwise the error
  343. * pointer:
  344. *
  345. * -EINVAL : The @fd file descriptor is not an eventfd file.
  346. */
  347. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  348. {
  349. struct eventfd_ctx *ctx;
  350. if (file->f_op != &eventfd_fops)
  351. return ERR_PTR(-EINVAL);
  352. ctx = file->private_data;
  353. kref_get(&ctx->kref);
  354. return ctx;
  355. }
  356. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  357. static int do_eventfd(unsigned int count, int flags)
  358. {
  359. struct eventfd_ctx *ctx;
  360. int fd;
  361. /* Check the EFD_* constants for consistency. */
  362. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  363. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  364. if (flags & ~EFD_FLAGS_SET)
  365. return -EINVAL;
  366. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  367. if (!ctx)
  368. return -ENOMEM;
  369. kref_init(&ctx->kref);
  370. init_waitqueue_head(&ctx->wqh);
  371. ctx->count = count;
  372. ctx->flags = flags;
  373. fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
  374. O_RDWR | (flags & EFD_SHARED_FCNTL_FLAGS));
  375. if (fd < 0)
  376. eventfd_free_ctx(ctx);
  377. return fd;
  378. }
  379. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  380. {
  381. return do_eventfd(count, flags);
  382. }
  383. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  384. {
  385. return do_eventfd(count, 0);
  386. }