eventfd.c 11 KB

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