eventfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 struct wait_queue_head *
  93. eventfd_get_poll_head(struct file *file, __poll_t events)
  94. {
  95. struct eventfd_ctx *ctx = file->private_data;
  96. return &ctx->wqh;
  97. }
  98. static __poll_t eventfd_poll_mask(struct file *file, __poll_t eventmask)
  99. {
  100. struct eventfd_ctx *ctx = file->private_data;
  101. __poll_t events = 0;
  102. u64 count;
  103. /*
  104. * All writes to ctx->count occur within ctx->wqh.lock. This read
  105. * can be done outside ctx->wqh.lock because we know that poll_wait
  106. * takes that lock (through add_wait_queue) if our caller will sleep.
  107. *
  108. * The read _can_ therefore seep into add_wait_queue's critical
  109. * section, but cannot move above it! add_wait_queue's spin_lock acts
  110. * as an acquire barrier and ensures that the read be ordered properly
  111. * against the writes. The following CAN happen and is safe:
  112. *
  113. * poll write
  114. * ----------------- ------------
  115. * lock ctx->wqh.lock (in poll_wait)
  116. * count = ctx->count
  117. * __add_wait_queue
  118. * unlock ctx->wqh.lock
  119. * lock ctx->qwh.lock
  120. * ctx->count += n
  121. * if (waitqueue_active)
  122. * wake_up_locked_poll
  123. * unlock ctx->qwh.lock
  124. * eventfd_poll returns 0
  125. *
  126. * but the following, which would miss a wakeup, cannot happen:
  127. *
  128. * poll write
  129. * ----------------- ------------
  130. * count = ctx->count (INVALID!)
  131. * lock ctx->qwh.lock
  132. * ctx->count += n
  133. * **waitqueue_active is false**
  134. * **no wake_up_locked_poll!**
  135. * unlock ctx->qwh.lock
  136. * lock ctx->wqh.lock (in poll_wait)
  137. * __add_wait_queue
  138. * unlock ctx->wqh.lock
  139. * eventfd_poll returns 0
  140. */
  141. count = READ_ONCE(ctx->count);
  142. if (count > 0)
  143. events |= (EPOLLIN & eventmask);
  144. if (count == ULLONG_MAX)
  145. events |= EPOLLERR;
  146. if (ULLONG_MAX - 1 > count)
  147. events |= (EPOLLOUT & eventmask);
  148. return events;
  149. }
  150. static void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
  151. {
  152. *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
  153. ctx->count -= *cnt;
  154. }
  155. /**
  156. * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
  157. * @ctx: [in] Pointer to eventfd context.
  158. * @wait: [in] Wait queue to be removed.
  159. * @cnt: [out] Pointer to the 64-bit counter value.
  160. *
  161. * Returns %0 if successful, or the following error codes:
  162. *
  163. * -EAGAIN : The operation would have blocked.
  164. *
  165. * This is used to atomically remove a wait queue entry from the eventfd wait
  166. * queue head, and read/reset the counter value.
  167. */
  168. int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *wait,
  169. __u64 *cnt)
  170. {
  171. unsigned long flags;
  172. spin_lock_irqsave(&ctx->wqh.lock, flags);
  173. eventfd_ctx_do_read(ctx, cnt);
  174. __remove_wait_queue(&ctx->wqh, wait);
  175. if (*cnt != 0 && waitqueue_active(&ctx->wqh))
  176. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  177. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  178. return *cnt != 0 ? 0 : -EAGAIN;
  179. }
  180. EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
  181. static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
  182. loff_t *ppos)
  183. {
  184. struct eventfd_ctx *ctx = file->private_data;
  185. ssize_t res;
  186. __u64 ucnt = 0;
  187. DECLARE_WAITQUEUE(wait, current);
  188. if (count < sizeof(ucnt))
  189. return -EINVAL;
  190. spin_lock_irq(&ctx->wqh.lock);
  191. res = -EAGAIN;
  192. if (ctx->count > 0)
  193. res = sizeof(ucnt);
  194. else if (!(file->f_flags & O_NONBLOCK)) {
  195. __add_wait_queue(&ctx->wqh, &wait);
  196. for (;;) {
  197. set_current_state(TASK_INTERRUPTIBLE);
  198. if (ctx->count > 0) {
  199. res = sizeof(ucnt);
  200. break;
  201. }
  202. if (signal_pending(current)) {
  203. res = -ERESTARTSYS;
  204. break;
  205. }
  206. spin_unlock_irq(&ctx->wqh.lock);
  207. schedule();
  208. spin_lock_irq(&ctx->wqh.lock);
  209. }
  210. __remove_wait_queue(&ctx->wqh, &wait);
  211. __set_current_state(TASK_RUNNING);
  212. }
  213. if (likely(res > 0)) {
  214. eventfd_ctx_do_read(ctx, &ucnt);
  215. if (waitqueue_active(&ctx->wqh))
  216. wake_up_locked_poll(&ctx->wqh, EPOLLOUT);
  217. }
  218. spin_unlock_irq(&ctx->wqh.lock);
  219. if (res > 0 && put_user(ucnt, (__u64 __user *)buf))
  220. return -EFAULT;
  221. return res;
  222. }
  223. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  224. loff_t *ppos)
  225. {
  226. struct eventfd_ctx *ctx = file->private_data;
  227. ssize_t res;
  228. __u64 ucnt;
  229. DECLARE_WAITQUEUE(wait, current);
  230. if (count < sizeof(ucnt))
  231. return -EINVAL;
  232. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  233. return -EFAULT;
  234. if (ucnt == ULLONG_MAX)
  235. return -EINVAL;
  236. spin_lock_irq(&ctx->wqh.lock);
  237. res = -EAGAIN;
  238. if (ULLONG_MAX - ctx->count > ucnt)
  239. res = sizeof(ucnt);
  240. else if (!(file->f_flags & O_NONBLOCK)) {
  241. __add_wait_queue(&ctx->wqh, &wait);
  242. for (res = 0;;) {
  243. set_current_state(TASK_INTERRUPTIBLE);
  244. if (ULLONG_MAX - ctx->count > ucnt) {
  245. res = sizeof(ucnt);
  246. break;
  247. }
  248. if (signal_pending(current)) {
  249. res = -ERESTARTSYS;
  250. break;
  251. }
  252. spin_unlock_irq(&ctx->wqh.lock);
  253. schedule();
  254. spin_lock_irq(&ctx->wqh.lock);
  255. }
  256. __remove_wait_queue(&ctx->wqh, &wait);
  257. __set_current_state(TASK_RUNNING);
  258. }
  259. if (likely(res > 0)) {
  260. ctx->count += ucnt;
  261. if (waitqueue_active(&ctx->wqh))
  262. wake_up_locked_poll(&ctx->wqh, EPOLLIN);
  263. }
  264. spin_unlock_irq(&ctx->wqh.lock);
  265. return res;
  266. }
  267. #ifdef CONFIG_PROC_FS
  268. static void eventfd_show_fdinfo(struct seq_file *m, struct file *f)
  269. {
  270. struct eventfd_ctx *ctx = f->private_data;
  271. spin_lock_irq(&ctx->wqh.lock);
  272. seq_printf(m, "eventfd-count: %16llx\n",
  273. (unsigned long long)ctx->count);
  274. spin_unlock_irq(&ctx->wqh.lock);
  275. }
  276. #endif
  277. static const struct file_operations eventfd_fops = {
  278. #ifdef CONFIG_PROC_FS
  279. .show_fdinfo = eventfd_show_fdinfo,
  280. #endif
  281. .release = eventfd_release,
  282. .get_poll_head = eventfd_get_poll_head,
  283. .poll_mask = eventfd_poll_mask,
  284. .read = eventfd_read,
  285. .write = eventfd_write,
  286. .llseek = noop_llseek,
  287. };
  288. /**
  289. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  290. * @fd: [in] Eventfd file descriptor.
  291. *
  292. * Returns a pointer to the eventfd file structure in case of success, or the
  293. * following error pointer:
  294. *
  295. * -EBADF : Invalid @fd file descriptor.
  296. * -EINVAL : The @fd file descriptor is not an eventfd file.
  297. */
  298. struct file *eventfd_fget(int fd)
  299. {
  300. struct file *file;
  301. file = fget(fd);
  302. if (!file)
  303. return ERR_PTR(-EBADF);
  304. if (file->f_op != &eventfd_fops) {
  305. fput(file);
  306. return ERR_PTR(-EINVAL);
  307. }
  308. return file;
  309. }
  310. EXPORT_SYMBOL_GPL(eventfd_fget);
  311. /**
  312. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  313. * @fd: [in] Eventfd file descriptor.
  314. *
  315. * Returns a pointer to the internal eventfd context, otherwise the error
  316. * pointers returned by the following functions:
  317. *
  318. * eventfd_fget
  319. */
  320. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  321. {
  322. struct eventfd_ctx *ctx;
  323. struct fd f = fdget(fd);
  324. if (!f.file)
  325. return ERR_PTR(-EBADF);
  326. ctx = eventfd_ctx_fileget(f.file);
  327. fdput(f);
  328. return ctx;
  329. }
  330. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  331. /**
  332. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  333. * @file: [in] Eventfd file pointer.
  334. *
  335. * Returns a pointer to the internal eventfd context, otherwise the error
  336. * pointer:
  337. *
  338. * -EINVAL : The @fd file descriptor is not an eventfd file.
  339. */
  340. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  341. {
  342. struct eventfd_ctx *ctx;
  343. if (file->f_op != &eventfd_fops)
  344. return ERR_PTR(-EINVAL);
  345. ctx = file->private_data;
  346. kref_get(&ctx->kref);
  347. return ctx;
  348. }
  349. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  350. static int do_eventfd(unsigned int count, int flags)
  351. {
  352. struct eventfd_ctx *ctx;
  353. int fd;
  354. /* Check the EFD_* constants for consistency. */
  355. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  356. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  357. if (flags & ~EFD_FLAGS_SET)
  358. return -EINVAL;
  359. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  360. if (!ctx)
  361. return -ENOMEM;
  362. kref_init(&ctx->kref);
  363. init_waitqueue_head(&ctx->wqh);
  364. ctx->count = count;
  365. ctx->flags = flags;
  366. fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
  367. O_RDWR | (flags & EFD_SHARED_FCNTL_FLAGS));
  368. if (fd < 0)
  369. eventfd_free_ctx(ctx);
  370. return fd;
  371. }
  372. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  373. {
  374. return do_eventfd(count, flags);
  375. }
  376. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  377. {
  378. return do_eventfd(count, 0);
  379. }