eventfd.c 11 KB

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