eventfd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/anon_inodes.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/module.h>
  18. #include <linux/kref.h>
  19. #include <linux/eventfd.h>
  20. struct eventfd_ctx {
  21. struct kref kref;
  22. wait_queue_head_t wqh;
  23. /*
  24. * Every time that a write(2) is performed on an eventfd, the
  25. * value of the __u64 being written is added to "count" and a
  26. * wakeup is performed on "wqh". A read(2) will return the "count"
  27. * value to userspace, and will reset "count" to zero. The kernel
  28. * side eventfd_signal() also, adds to the "count" counter and
  29. * issue a wakeup.
  30. */
  31. __u64 count;
  32. unsigned int flags;
  33. };
  34. /**
  35. * eventfd_signal - Adds @n to the eventfd counter.
  36. * @ctx: [in] Pointer to the eventfd context.
  37. * @n: [in] Value of the counter to be added to the eventfd internal counter.
  38. * The value cannot be negative.
  39. *
  40. * This function is supposed to be called by the kernel in paths that do not
  41. * allow sleeping. In this function we allow the counter to reach the ULLONG_MAX
  42. * value, and we signal this as overflow condition by returining a POLLERR
  43. * to poll(2).
  44. *
  45. * Returns @n in case of success, a non-negative number lower than @n in case
  46. * of overflow, or the following error codes:
  47. *
  48. * -EINVAL : The value of @n is negative.
  49. */
  50. int eventfd_signal(struct eventfd_ctx *ctx, int n)
  51. {
  52. unsigned long flags;
  53. if (n < 0)
  54. return -EINVAL;
  55. spin_lock_irqsave(&ctx->wqh.lock, flags);
  56. if (ULLONG_MAX - ctx->count < n)
  57. n = (int) (ULLONG_MAX - ctx->count);
  58. ctx->count += n;
  59. if (waitqueue_active(&ctx->wqh))
  60. wake_up_locked_poll(&ctx->wqh, POLLIN);
  61. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  62. return n;
  63. }
  64. EXPORT_SYMBOL_GPL(eventfd_signal);
  65. static void eventfd_free(struct kref *kref)
  66. {
  67. struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref);
  68. kfree(ctx);
  69. }
  70. /**
  71. * eventfd_ctx_get - Acquires a reference to the internal eventfd context.
  72. * @ctx: [in] Pointer to the eventfd context.
  73. *
  74. * Returns: In case of success, returns a pointer to the eventfd context.
  75. */
  76. struct eventfd_ctx *eventfd_ctx_get(struct eventfd_ctx *ctx)
  77. {
  78. kref_get(&ctx->kref);
  79. return ctx;
  80. }
  81. EXPORT_SYMBOL_GPL(eventfd_ctx_get);
  82. /**
  83. * eventfd_ctx_put - Releases a reference to the internal eventfd context.
  84. * @ctx: [in] Pointer to eventfd context.
  85. *
  86. * The eventfd context reference must have been previously acquired either
  87. * with eventfd_ctx_get() or eventfd_ctx_fdget()).
  88. */
  89. void eventfd_ctx_put(struct eventfd_ctx *ctx)
  90. {
  91. kref_put(&ctx->kref, eventfd_free);
  92. }
  93. EXPORT_SYMBOL_GPL(eventfd_ctx_put);
  94. static int eventfd_release(struct inode *inode, struct file *file)
  95. {
  96. struct eventfd_ctx *ctx = file->private_data;
  97. wake_up_poll(&ctx->wqh, POLLHUP);
  98. eventfd_ctx_put(ctx);
  99. return 0;
  100. }
  101. static unsigned int eventfd_poll(struct file *file, poll_table *wait)
  102. {
  103. struct eventfd_ctx *ctx = file->private_data;
  104. unsigned int events = 0;
  105. unsigned long flags;
  106. poll_wait(file, &ctx->wqh, wait);
  107. spin_lock_irqsave(&ctx->wqh.lock, flags);
  108. if (ctx->count > 0)
  109. events |= POLLIN;
  110. if (ctx->count == ULLONG_MAX)
  111. events |= POLLERR;
  112. if (ULLONG_MAX - 1 > ctx->count)
  113. events |= POLLOUT;
  114. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  115. return events;
  116. }
  117. static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
  118. loff_t *ppos)
  119. {
  120. struct eventfd_ctx *ctx = file->private_data;
  121. ssize_t res;
  122. __u64 ucnt = 0;
  123. DECLARE_WAITQUEUE(wait, current);
  124. if (count < sizeof(ucnt))
  125. return -EINVAL;
  126. spin_lock_irq(&ctx->wqh.lock);
  127. res = -EAGAIN;
  128. if (ctx->count > 0)
  129. res = sizeof(ucnt);
  130. else if (!(file->f_flags & O_NONBLOCK)) {
  131. __add_wait_queue(&ctx->wqh, &wait);
  132. for (res = 0;;) {
  133. set_current_state(TASK_INTERRUPTIBLE);
  134. if (ctx->count > 0) {
  135. res = sizeof(ucnt);
  136. break;
  137. }
  138. if (signal_pending(current)) {
  139. res = -ERESTARTSYS;
  140. break;
  141. }
  142. spin_unlock_irq(&ctx->wqh.lock);
  143. schedule();
  144. spin_lock_irq(&ctx->wqh.lock);
  145. }
  146. __remove_wait_queue(&ctx->wqh, &wait);
  147. __set_current_state(TASK_RUNNING);
  148. }
  149. if (likely(res > 0)) {
  150. ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
  151. ctx->count -= ucnt;
  152. if (waitqueue_active(&ctx->wqh))
  153. wake_up_locked_poll(&ctx->wqh, POLLOUT);
  154. }
  155. spin_unlock_irq(&ctx->wqh.lock);
  156. if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
  157. return -EFAULT;
  158. return res;
  159. }
  160. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  161. loff_t *ppos)
  162. {
  163. struct eventfd_ctx *ctx = file->private_data;
  164. ssize_t res;
  165. __u64 ucnt;
  166. DECLARE_WAITQUEUE(wait, current);
  167. if (count < sizeof(ucnt))
  168. return -EINVAL;
  169. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  170. return -EFAULT;
  171. if (ucnt == ULLONG_MAX)
  172. return -EINVAL;
  173. spin_lock_irq(&ctx->wqh.lock);
  174. res = -EAGAIN;
  175. if (ULLONG_MAX - ctx->count > ucnt)
  176. res = sizeof(ucnt);
  177. else if (!(file->f_flags & O_NONBLOCK)) {
  178. __add_wait_queue(&ctx->wqh, &wait);
  179. for (res = 0;;) {
  180. set_current_state(TASK_INTERRUPTIBLE);
  181. if (ULLONG_MAX - ctx->count > ucnt) {
  182. res = sizeof(ucnt);
  183. break;
  184. }
  185. if (signal_pending(current)) {
  186. res = -ERESTARTSYS;
  187. break;
  188. }
  189. spin_unlock_irq(&ctx->wqh.lock);
  190. schedule();
  191. spin_lock_irq(&ctx->wqh.lock);
  192. }
  193. __remove_wait_queue(&ctx->wqh, &wait);
  194. __set_current_state(TASK_RUNNING);
  195. }
  196. if (likely(res > 0)) {
  197. ctx->count += ucnt;
  198. if (waitqueue_active(&ctx->wqh))
  199. wake_up_locked_poll(&ctx->wqh, POLLIN);
  200. }
  201. spin_unlock_irq(&ctx->wqh.lock);
  202. return res;
  203. }
  204. static const struct file_operations eventfd_fops = {
  205. .release = eventfd_release,
  206. .poll = eventfd_poll,
  207. .read = eventfd_read,
  208. .write = eventfd_write,
  209. };
  210. /**
  211. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  212. * @fd: [in] Eventfd file descriptor.
  213. *
  214. * Returns a pointer to the eventfd file structure in case of success, or the
  215. * following error pointer:
  216. *
  217. * -EBADF : Invalid @fd file descriptor.
  218. * -EINVAL : The @fd file descriptor is not an eventfd file.
  219. */
  220. struct file *eventfd_fget(int fd)
  221. {
  222. struct file *file;
  223. file = fget(fd);
  224. if (!file)
  225. return ERR_PTR(-EBADF);
  226. if (file->f_op != &eventfd_fops) {
  227. fput(file);
  228. return ERR_PTR(-EINVAL);
  229. }
  230. return file;
  231. }
  232. EXPORT_SYMBOL_GPL(eventfd_fget);
  233. /**
  234. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  235. * @fd: [in] Eventfd file descriptor.
  236. *
  237. * Returns a pointer to the internal eventfd context, otherwise the error
  238. * pointers returned by the following functions:
  239. *
  240. * eventfd_fget
  241. */
  242. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  243. {
  244. struct file *file;
  245. struct eventfd_ctx *ctx;
  246. file = eventfd_fget(fd);
  247. if (IS_ERR(file))
  248. return (struct eventfd_ctx *) file;
  249. ctx = eventfd_ctx_get(file->private_data);
  250. fput(file);
  251. return ctx;
  252. }
  253. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  254. /**
  255. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  256. * @file: [in] Eventfd file pointer.
  257. *
  258. * Returns a pointer to the internal eventfd context, otherwise the error
  259. * pointer:
  260. *
  261. * -EINVAL : The @fd file descriptor is not an eventfd file.
  262. */
  263. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  264. {
  265. if (file->f_op != &eventfd_fops)
  266. return ERR_PTR(-EINVAL);
  267. return eventfd_ctx_get(file->private_data);
  268. }
  269. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  270. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  271. {
  272. int fd;
  273. struct eventfd_ctx *ctx;
  274. /* Check the EFD_* constants for consistency. */
  275. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  276. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  277. if (flags & ~EFD_FLAGS_SET)
  278. return -EINVAL;
  279. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  280. if (!ctx)
  281. return -ENOMEM;
  282. kref_init(&ctx->kref);
  283. init_waitqueue_head(&ctx->wqh);
  284. ctx->count = count;
  285. ctx->flags = flags;
  286. /*
  287. * When we call this, the initialization must be complete, since
  288. * anon_inode_getfd() will install the fd.
  289. */
  290. fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
  291. flags & EFD_SHARED_FCNTL_FLAGS);
  292. if (fd < 0)
  293. kfree(ctx);
  294. return fd;
  295. }
  296. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  297. {
  298. return sys_eventfd2(count, 0);
  299. }