eventfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/eventfd.c
  4. *
  5. * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
  6. *
  7. */
  8. #include <linux/file.h>
  9. #include <linux/poll.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/anon_inodes.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/export.h>
  20. #include <linux/kref.h>
  21. #include <linux/eventfd.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/idr.h>
  25. #include <linux/uio.h>
  26. static DEFINE_IDA(eventfd_ida);
  27. struct eventfd_ctx {
  28. struct kref kref;
  29. wait_queue_head_t wqh;
  30. /*
  31. * Every time that a write(2) is performed on an eventfd, the
  32. * value of the __u64 being written is added to "count" and a
  33. * wakeup is performed on "wqh". If EFD_SEMAPHORE flag was not
  34. * specified, a read(2) will return the "count" value to userspace,
  35. * and will reset "count" to zero. The kernel side eventfd_signal()
  36. * also, adds to the "count" counter and issue a wakeup.
  37. */
  38. __u64 count;
  39. unsigned int flags;
  40. int id;
  41. };
  42. /**
  43. * eventfd_signal_mask - Increment the event counter
  44. * @ctx: [in] Pointer to the eventfd context.
  45. * @mask: [in] poll mask
  46. *
  47. * This function is supposed to be called by the kernel in paths that do not
  48. * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
  49. * value, and we signal this as overflow condition by returning a EPOLLERR
  50. * to poll(2).
  51. */
  52. void eventfd_signal_mask(struct eventfd_ctx *ctx, __poll_t mask)
  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_allowed() before calling this function. If
  60. * it returns false, the eventfd_signal() call should be deferred to a
  61. * safe context.
  62. */
  63. if (WARN_ON_ONCE(current->in_eventfd))
  64. return;
  65. spin_lock_irqsave(&ctx->wqh.lock, flags);
  66. current->in_eventfd = 1;
  67. if (ctx->count < ULLONG_MAX)
  68. ctx->count++;
  69. if (waitqueue_active(&ctx->wqh))
  70. wake_up_locked_poll(&ctx->wqh, EPOLLIN | mask);
  71. current->in_eventfd = 0;
  72. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  73. }
  74. EXPORT_SYMBOL_GPL(eventfd_signal_mask);
  75. static void eventfd_free_ctx(struct eventfd_ctx *ctx)
  76. {
  77. if (ctx->id >= 0)
  78. ida_free(&eventfd_ida, ctx->id);
  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. void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
  159. {
  160. lockdep_assert_held(&ctx->wqh.lock);
  161. *cnt = ((ctx->flags & EFD_SEMAPHORE) && ctx->count) ? 1 : ctx->count;
  162. ctx->count -= *cnt;
  163. }
  164. EXPORT_SYMBOL_GPL(eventfd_ctx_do_read);
  165. /**
  166. * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
  167. * @ctx: [in] Pointer to eventfd context.
  168. * @wait: [in] Wait queue to be removed.
  169. * @cnt: [out] Pointer to the 64-bit counter value.
  170. *
  171. * Returns %0 if successful, or the following error codes:
  172. *
  173. * -EAGAIN : The operation would have blocked.
  174. *
  175. * This is used to atomically remove a wait queue entry from the eventfd wait
  176. * queue head, and read/reset the counter value.
  177. */
  178. int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait,
  179. __u64 *cnt)
  180. {
  181. unsigned long flags;
  182. spin_lock_irqsave(&ctx->wqh.lock, flags);
  183. eventfd_ctx_do_read(ctx, cnt);
  184. __remove_wait_queue(&ctx->wqh, wait);
  185. if (*cnt != 0 && waitqueue_active(&ctx->wqh))
  186. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  187. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  188. return *cnt != 0 ? 0 : -EAGAIN;
  189. }
  190. EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
  191. static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to)
  192. {
  193. struct file *file = iocb->ki_filp;
  194. struct eventfd_ctx *ctx = file->private_data;
  195. __u64 ucnt = 0;
  196. if (iov_iter_count(to) < sizeof(ucnt))
  197. return -EINVAL;
  198. spin_lock_irq(&ctx->wqh.lock);
  199. if (!ctx->count) {
  200. if ((file->f_flags & O_NONBLOCK) ||
  201. (iocb->ki_flags & IOCB_NOWAIT)) {
  202. spin_unlock_irq(&ctx->wqh.lock);
  203. return -EAGAIN;
  204. }
  205. if (wait_event_interruptible_locked_irq(ctx->wqh, ctx->count)) {
  206. spin_unlock_irq(&ctx->wqh.lock);
  207. return -ERESTARTSYS;
  208. }
  209. }
  210. eventfd_ctx_do_read(ctx, &ucnt);
  211. current->in_eventfd = 1;
  212. if (waitqueue_active(&ctx->wqh))
  213. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  214. current->in_eventfd = 0;
  215. spin_unlock_irq(&ctx->wqh.lock);
  216. if (unlikely(copy_to_iter(&ucnt, sizeof(ucnt), to) != sizeof(ucnt)))
  217. return -EFAULT;
  218. return sizeof(ucnt);
  219. }
  220. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  221. loff_t *ppos)
  222. {
  223. struct eventfd_ctx *ctx = file->private_data;
  224. ssize_t res;
  225. __u64 ucnt;
  226. if (count != sizeof(ucnt))
  227. return -EINVAL;
  228. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  229. return -EFAULT;
  230. if (ucnt == ULLONG_MAX)
  231. return -EINVAL;
  232. spin_lock_irq(&ctx->wqh.lock);
  233. res = -EAGAIN;
  234. if (ULLONG_MAX - ctx->count > ucnt)
  235. res = sizeof(ucnt);
  236. else if (!(file->f_flags & O_NONBLOCK)) {
  237. res = wait_event_interruptible_locked_irq(ctx->wqh,
  238. ULLONG_MAX - ctx->count > ucnt);
  239. if (!res)
  240. res = sizeof(ucnt);
  241. }
  242. if (likely(res > 0)) {
  243. ctx->count += ucnt;
  244. current->in_eventfd = 1;
  245. if (waitqueue_active(&ctx->wqh))
  246. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  247. current->in_eventfd = 0;
  248. }
  249. spin_unlock_irq(&ctx->wqh.lock);
  250. return res;
  251. }
  252. #ifdef CONFIG_PROC_FS
  253. static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
  254. {
  255. struct eventfd_ctx *ctx = f->private_data;
  256. __u64 cnt;
  257. spin_lock_irq(&ctx->wqh.lock);
  258. cnt = ctx->count;
  259. spin_unlock_irq(&ctx->wqh.lock);
  260. seq_printf(m,
  261. "eventfd-count: %16llx\n"
  262. "eventfd-id: %d\n"
  263. "eventfd-semaphore: %d\n",
  264. cnt,
  265. ctx->id,
  266. !!(ctx->flags & EFD_SEMAPHORE));
  267. }
  268. #endif
  269. static const struct file_operations eventfd_fops = {
  270. #ifdef CONFIG_PROC_FS
  271. .show_fdinfo = eventfd_show_fdinfo,
  272. #endif
  273. .release = eventfd_release,
  274. .poll = eventfd_poll,
  275. .read_iter = eventfd_read,
  276. .write = eventfd_write,
  277. .llseek = noop_llseek,
  278. };
  279. /**
  280. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  281. * @fd: [in] Eventfd file descriptor.
  282. *
  283. * Returns a pointer to the eventfd file structure in case of success, or the
  284. * following error pointer:
  285. *
  286. * -EBADF : Invalid @fd file descriptor.
  287. * -EINVAL : The @fd file descriptor is not an eventfd file.
  288. */
  289. struct file *eventfd_fget(int fd)
  290. {
  291. struct file *file;
  292. file = fget(fd);
  293. if (!file)
  294. return ERR_PTR(-EBADF);
  295. if (file->f_op != &eventfd_fops) {
  296. fput(file);
  297. return ERR_PTR(-EINVAL);
  298. }
  299. return file;
  300. }
  301. EXPORT_SYMBOL_GPL(eventfd_fget);
  302. /**
  303. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  304. * @fd: [in] Eventfd file descriptor.
  305. *
  306. * Returns a pointer to the internal eventfd context, otherwise the error
  307. * pointers returned by the following functions:
  308. *
  309. * eventfd_fget
  310. */
  311. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  312. {
  313. struct eventfd_ctx *ctx;
  314. struct fd f = fdget(fd);
  315. if (!fd_file(f))
  316. return ERR_PTR(-EBADF);
  317. ctx = eventfd_ctx_fileget(fd_file(f));
  318. fdput(f);
  319. return ctx;
  320. }
  321. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  322. /**
  323. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  324. * @file: [in] Eventfd file pointer.
  325. *
  326. * Returns a pointer to the internal eventfd context, otherwise the error
  327. * pointer:
  328. *
  329. * -EINVAL : The @fd file descriptor is not an eventfd file.
  330. */
  331. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  332. {
  333. struct eventfd_ctx *ctx;
  334. if (file->f_op != &eventfd_fops)
  335. return ERR_PTR(-EINVAL);
  336. ctx = file->private_data;
  337. kref_get(&ctx->kref);
  338. return ctx;
  339. }
  340. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  341. static int do_eventfd(unsigned int count, int flags)
  342. {
  343. struct eventfd_ctx *ctx;
  344. struct file *file;
  345. int fd;
  346. /* Check the EFD_* constants for consistency. */
  347. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  348. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  349. BUILD_BUG_ON(EFD_SEMAPHORE != (1 << 0));
  350. if (flags & ~EFD_FLAGS_SET)
  351. return -EINVAL;
  352. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  353. if (!ctx)
  354. return -ENOMEM;
  355. kref_init(&ctx->kref);
  356. init_waitqueue_head(&ctx->wqh);
  357. ctx->count = count;
  358. ctx->flags = flags;
  359. ctx->id = ida_alloc(&eventfd_ida, GFP_KERNEL);
  360. flags &= EFD_SHARED_FCNTL_FLAGS;
  361. flags |= O_RDWR;
  362. fd = get_unused_fd_flags(flags);
  363. if (fd < 0)
  364. goto err;
  365. file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx, flags);
  366. if (IS_ERR(file)) {
  367. put_unused_fd(fd);
  368. fd = PTR_ERR(file);
  369. goto err;
  370. }
  371. file->f_mode |= FMODE_NOWAIT;
  372. fd_install(fd, file);
  373. return fd;
  374. err:
  375. eventfd_free_ctx(ctx);
  376. return fd;
  377. }
  378. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  379. {
  380. return do_eventfd(count, flags);
  381. }
  382. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  383. {
  384. return do_eventfd(count, 0);
  385. }