eventfd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_ctx(struct eventfd_ctx *ctx)
  66. {
  67. kfree(ctx);
  68. }
  69. static void eventfd_free(struct kref *kref)
  70. {
  71. struct eventfd_ctx *ctx = container_of(kref, struct eventfd_ctx, kref);
  72. eventfd_free_ctx(ctx);
  73. }
  74. /**
  75. * eventfd_ctx_get - Acquires a reference to the internal eventfd context.
  76. * @ctx: [in] Pointer to the eventfd context.
  77. *
  78. * Returns: In case of success, returns a pointer to the eventfd context.
  79. */
  80. struct eventfd_ctx *eventfd_ctx_get(struct eventfd_ctx *ctx)
  81. {
  82. kref_get(&ctx->kref);
  83. return ctx;
  84. }
  85. EXPORT_SYMBOL_GPL(eventfd_ctx_get);
  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_get() or eventfd_ctx_fdget()).
  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, POLLHUP);
  102. eventfd_ctx_put(ctx);
  103. return 0;
  104. }
  105. static unsigned int eventfd_poll(struct file *file, poll_table *wait)
  106. {
  107. struct eventfd_ctx *ctx = file->private_data;
  108. unsigned int events = 0;
  109. unsigned long flags;
  110. poll_wait(file, &ctx->wqh, wait);
  111. spin_lock_irqsave(&ctx->wqh.lock, flags);
  112. if (ctx->count > 0)
  113. events |= POLLIN;
  114. if (ctx->count == ULLONG_MAX)
  115. events |= POLLERR;
  116. if (ULLONG_MAX - 1 > ctx->count)
  117. events |= POLLOUT;
  118. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  119. return events;
  120. }
  121. static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
  122. loff_t *ppos)
  123. {
  124. struct eventfd_ctx *ctx = file->private_data;
  125. ssize_t res;
  126. __u64 ucnt = 0;
  127. DECLARE_WAITQUEUE(wait, current);
  128. if (count < sizeof(ucnt))
  129. return -EINVAL;
  130. spin_lock_irq(&ctx->wqh.lock);
  131. res = -EAGAIN;
  132. if (ctx->count > 0)
  133. res = sizeof(ucnt);
  134. else if (!(file->f_flags & O_NONBLOCK)) {
  135. __add_wait_queue(&ctx->wqh, &wait);
  136. for (res = 0;;) {
  137. set_current_state(TASK_INTERRUPTIBLE);
  138. if (ctx->count > 0) {
  139. res = sizeof(ucnt);
  140. break;
  141. }
  142. if (signal_pending(current)) {
  143. res = -ERESTARTSYS;
  144. break;
  145. }
  146. spin_unlock_irq(&ctx->wqh.lock);
  147. schedule();
  148. spin_lock_irq(&ctx->wqh.lock);
  149. }
  150. __remove_wait_queue(&ctx->wqh, &wait);
  151. __set_current_state(TASK_RUNNING);
  152. }
  153. if (likely(res > 0)) {
  154. ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
  155. ctx->count -= ucnt;
  156. if (waitqueue_active(&ctx->wqh))
  157. wake_up_locked_poll(&ctx->wqh, POLLOUT);
  158. }
  159. spin_unlock_irq(&ctx->wqh.lock);
  160. if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
  161. return -EFAULT;
  162. return res;
  163. }
  164. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  165. loff_t *ppos)
  166. {
  167. struct eventfd_ctx *ctx = file->private_data;
  168. ssize_t res;
  169. __u64 ucnt;
  170. DECLARE_WAITQUEUE(wait, current);
  171. if (count < sizeof(ucnt))
  172. return -EINVAL;
  173. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  174. return -EFAULT;
  175. if (ucnt == ULLONG_MAX)
  176. return -EINVAL;
  177. spin_lock_irq(&ctx->wqh.lock);
  178. res = -EAGAIN;
  179. if (ULLONG_MAX - ctx->count > ucnt)
  180. res = sizeof(ucnt);
  181. else if (!(file->f_flags & O_NONBLOCK)) {
  182. __add_wait_queue(&ctx->wqh, &wait);
  183. for (res = 0;;) {
  184. set_current_state(TASK_INTERRUPTIBLE);
  185. if (ULLONG_MAX - ctx->count > ucnt) {
  186. res = sizeof(ucnt);
  187. break;
  188. }
  189. if (signal_pending(current)) {
  190. res = -ERESTARTSYS;
  191. break;
  192. }
  193. spin_unlock_irq(&ctx->wqh.lock);
  194. schedule();
  195. spin_lock_irq(&ctx->wqh.lock);
  196. }
  197. __remove_wait_queue(&ctx->wqh, &wait);
  198. __set_current_state(TASK_RUNNING);
  199. }
  200. if (likely(res > 0)) {
  201. ctx->count += ucnt;
  202. if (waitqueue_active(&ctx->wqh))
  203. wake_up_locked_poll(&ctx->wqh, POLLIN);
  204. }
  205. spin_unlock_irq(&ctx->wqh.lock);
  206. return res;
  207. }
  208. static const struct file_operations eventfd_fops = {
  209. .release = eventfd_release,
  210. .poll = eventfd_poll,
  211. .read = eventfd_read,
  212. .write = eventfd_write,
  213. };
  214. /**
  215. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  216. * @fd: [in] Eventfd file descriptor.
  217. *
  218. * Returns a pointer to the eventfd file structure in case of success, or the
  219. * following error pointer:
  220. *
  221. * -EBADF : Invalid @fd file descriptor.
  222. * -EINVAL : The @fd file descriptor is not an eventfd file.
  223. */
  224. struct file *eventfd_fget(int fd)
  225. {
  226. struct file *file;
  227. file = fget(fd);
  228. if (!file)
  229. return ERR_PTR(-EBADF);
  230. if (file->f_op != &eventfd_fops) {
  231. fput(file);
  232. return ERR_PTR(-EINVAL);
  233. }
  234. return file;
  235. }
  236. EXPORT_SYMBOL_GPL(eventfd_fget);
  237. /**
  238. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  239. * @fd: [in] Eventfd file descriptor.
  240. *
  241. * Returns a pointer to the internal eventfd context, otherwise the error
  242. * pointers returned by the following functions:
  243. *
  244. * eventfd_fget
  245. */
  246. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  247. {
  248. struct file *file;
  249. struct eventfd_ctx *ctx;
  250. file = eventfd_fget(fd);
  251. if (IS_ERR(file))
  252. return (struct eventfd_ctx *) file;
  253. ctx = eventfd_ctx_get(file->private_data);
  254. fput(file);
  255. return ctx;
  256. }
  257. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  258. /**
  259. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  260. * @file: [in] Eventfd file pointer.
  261. *
  262. * Returns a pointer to the internal eventfd context, otherwise the error
  263. * pointer:
  264. *
  265. * -EINVAL : The @fd file descriptor is not an eventfd file.
  266. */
  267. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  268. {
  269. if (file->f_op != &eventfd_fops)
  270. return ERR_PTR(-EINVAL);
  271. return eventfd_ctx_get(file->private_data);
  272. }
  273. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  274. /**
  275. * eventfd_file_create - Creates an eventfd file pointer.
  276. * @count: Initial eventfd counter value.
  277. * @flags: Flags for the eventfd file.
  278. *
  279. * This function creates an eventfd file pointer, w/out installing it into
  280. * the fd table. This is useful when the eventfd file is used during the
  281. * initialization of data structures that require extra setup after the eventfd
  282. * creation. So the eventfd creation is split into the file pointer creation
  283. * phase, and the file descriptor installation phase.
  284. * In this way races with userspace closing the newly installed file descriptor
  285. * can be avoided.
  286. * Returns an eventfd file pointer, or a proper error pointer.
  287. */
  288. struct file *eventfd_file_create(unsigned int count, int flags)
  289. {
  290. struct file *file;
  291. struct eventfd_ctx *ctx;
  292. /* Check the EFD_* constants for consistency. */
  293. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  294. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  295. if (flags & ~EFD_FLAGS_SET)
  296. return ERR_PTR(-EINVAL);
  297. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  298. if (!ctx)
  299. return ERR_PTR(-ENOMEM);
  300. kref_init(&ctx->kref);
  301. init_waitqueue_head(&ctx->wqh);
  302. ctx->count = count;
  303. ctx->flags = flags;
  304. file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx,
  305. flags & EFD_SHARED_FCNTL_FLAGS);
  306. if (IS_ERR(file))
  307. eventfd_free_ctx(ctx);
  308. return file;
  309. }
  310. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  311. {
  312. int fd, error;
  313. struct file *file;
  314. error = get_unused_fd_flags(flags & EFD_SHARED_FCNTL_FLAGS);
  315. if (error < 0)
  316. return error;
  317. fd = error;
  318. file = eventfd_file_create(count, flags);
  319. if (IS_ERR(file)) {
  320. error = PTR_ERR(file);
  321. goto err_put_unused_fd;
  322. }
  323. fd_install(fd, file);
  324. return fd;
  325. err_put_unused_fd:
  326. put_unused_fd(fd);
  327. return error;
  328. }
  329. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  330. {
  331. return sys_eventfd2(count, 0);
  332. }