eventfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 void eventfd_ctx_do_read(struct eventfd_ctx *ctx, __u64 *cnt)
  122. {
  123. *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
  124. ctx->count -= *cnt;
  125. }
  126. /**
  127. * eventfd_ctx_remove_wait_queue - Read the current counter and removes wait queue.
  128. * @ctx: [in] Pointer to eventfd context.
  129. * @wait: [in] Wait queue to be removed.
  130. * @cnt: [out] Pointer to the 64bit conter value.
  131. *
  132. * Returns zero if successful, or the following error codes:
  133. *
  134. * -EAGAIN : The operation would have blocked.
  135. *
  136. * This is used to atomically remove a wait queue entry from the eventfd wait
  137. * queue head, and read/reset the counter value.
  138. */
  139. int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_t *wait,
  140. __u64 *cnt)
  141. {
  142. unsigned long flags;
  143. spin_lock_irqsave(&ctx->wqh.lock, flags);
  144. eventfd_ctx_do_read(ctx, cnt);
  145. __remove_wait_queue(&ctx->wqh, wait);
  146. if (*cnt != 0 && waitqueue_active(&ctx->wqh))
  147. wake_up_locked_poll(&ctx->wqh, POLLOUT);
  148. spin_unlock_irqrestore(&ctx->wqh.lock, flags);
  149. return *cnt != 0 ? 0 : -EAGAIN;
  150. }
  151. EXPORT_SYMBOL_GPL(eventfd_ctx_remove_wait_queue);
  152. /**
  153. * eventfd_ctx_read - Reads the eventfd counter or wait if it is zero.
  154. * @ctx: [in] Pointer to eventfd context.
  155. * @no_wait: [in] Different from zero if the operation should not block.
  156. * @cnt: [out] Pointer to the 64bit conter value.
  157. *
  158. * Returns zero if successful, or the following error codes:
  159. *
  160. * -EAGAIN : The operation would have blocked but @no_wait was nonzero.
  161. * -ERESTARTSYS : A signal interrupted the wait operation.
  162. *
  163. * If @no_wait is zero, the function might sleep until the eventfd internal
  164. * counter becomes greater than zero.
  165. */
  166. ssize_t eventfd_ctx_read(struct eventfd_ctx *ctx, int no_wait, __u64 *cnt)
  167. {
  168. ssize_t res;
  169. DECLARE_WAITQUEUE(wait, current);
  170. spin_lock_irq(&ctx->wqh.lock);
  171. *cnt = 0;
  172. res = -EAGAIN;
  173. if (ctx->count > 0)
  174. res = 0;
  175. else if (!no_wait) {
  176. __add_wait_queue(&ctx->wqh, &wait);
  177. for (;;) {
  178. set_current_state(TASK_INTERRUPTIBLE);
  179. if (ctx->count > 0) {
  180. res = 0;
  181. break;
  182. }
  183. if (signal_pending(current)) {
  184. res = -ERESTARTSYS;
  185. break;
  186. }
  187. spin_unlock_irq(&ctx->wqh.lock);
  188. schedule();
  189. spin_lock_irq(&ctx->wqh.lock);
  190. }
  191. __remove_wait_queue(&ctx->wqh, &wait);
  192. __set_current_state(TASK_RUNNING);
  193. }
  194. if (likely(res == 0)) {
  195. eventfd_ctx_do_read(ctx, cnt);
  196. if (waitqueue_active(&ctx->wqh))
  197. wake_up_locked_poll(&ctx->wqh, POLLOUT);
  198. }
  199. spin_unlock_irq(&ctx->wqh.lock);
  200. return res;
  201. }
  202. EXPORT_SYMBOL_GPL(eventfd_ctx_read);
  203. static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
  204. loff_t *ppos)
  205. {
  206. struct eventfd_ctx *ctx = file->private_data;
  207. ssize_t res;
  208. __u64 cnt;
  209. if (count < sizeof(cnt))
  210. return -EINVAL;
  211. res = eventfd_ctx_read(ctx, file->f_flags & O_NONBLOCK, &cnt);
  212. if (res < 0)
  213. return res;
  214. return put_user(cnt, (__u64 __user *) buf) ? -EFAULT : sizeof(cnt);
  215. }
  216. static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
  217. loff_t *ppos)
  218. {
  219. struct eventfd_ctx *ctx = file->private_data;
  220. ssize_t res;
  221. __u64 ucnt;
  222. DECLARE_WAITQUEUE(wait, current);
  223. if (count < sizeof(ucnt))
  224. return -EINVAL;
  225. if (copy_from_user(&ucnt, buf, sizeof(ucnt)))
  226. return -EFAULT;
  227. if (ucnt == ULLONG_MAX)
  228. return -EINVAL;
  229. spin_lock_irq(&ctx->wqh.lock);
  230. res = -EAGAIN;
  231. if (ULLONG_MAX - ctx->count > ucnt)
  232. res = sizeof(ucnt);
  233. else if (!(file->f_flags & O_NONBLOCK)) {
  234. __add_wait_queue(&ctx->wqh, &wait);
  235. for (res = 0;;) {
  236. set_current_state(TASK_INTERRUPTIBLE);
  237. if (ULLONG_MAX - ctx->count > ucnt) {
  238. res = sizeof(ucnt);
  239. break;
  240. }
  241. if (signal_pending(current)) {
  242. res = -ERESTARTSYS;
  243. break;
  244. }
  245. spin_unlock_irq(&ctx->wqh.lock);
  246. schedule();
  247. spin_lock_irq(&ctx->wqh.lock);
  248. }
  249. __remove_wait_queue(&ctx->wqh, &wait);
  250. __set_current_state(TASK_RUNNING);
  251. }
  252. if (likely(res > 0)) {
  253. ctx->count += ucnt;
  254. if (waitqueue_active(&ctx->wqh))
  255. wake_up_locked_poll(&ctx->wqh, POLLIN);
  256. }
  257. spin_unlock_irq(&ctx->wqh.lock);
  258. return res;
  259. }
  260. static const struct file_operations eventfd_fops = {
  261. .release = eventfd_release,
  262. .poll = eventfd_poll,
  263. .read = eventfd_read,
  264. .write = eventfd_write,
  265. };
  266. /**
  267. * eventfd_fget - Acquire a reference of an eventfd file descriptor.
  268. * @fd: [in] Eventfd file descriptor.
  269. *
  270. * Returns a pointer to the eventfd file structure in case of success, or the
  271. * following error pointer:
  272. *
  273. * -EBADF : Invalid @fd file descriptor.
  274. * -EINVAL : The @fd file descriptor is not an eventfd file.
  275. */
  276. struct file *eventfd_fget(int fd)
  277. {
  278. struct file *file;
  279. file = fget(fd);
  280. if (!file)
  281. return ERR_PTR(-EBADF);
  282. if (file->f_op != &eventfd_fops) {
  283. fput(file);
  284. return ERR_PTR(-EINVAL);
  285. }
  286. return file;
  287. }
  288. EXPORT_SYMBOL_GPL(eventfd_fget);
  289. /**
  290. * eventfd_ctx_fdget - Acquires a reference to the internal eventfd context.
  291. * @fd: [in] Eventfd file descriptor.
  292. *
  293. * Returns a pointer to the internal eventfd context, otherwise the error
  294. * pointers returned by the following functions:
  295. *
  296. * eventfd_fget
  297. */
  298. struct eventfd_ctx *eventfd_ctx_fdget(int fd)
  299. {
  300. struct file *file;
  301. struct eventfd_ctx *ctx;
  302. file = eventfd_fget(fd);
  303. if (IS_ERR(file))
  304. return (struct eventfd_ctx *) file;
  305. ctx = eventfd_ctx_get(file->private_data);
  306. fput(file);
  307. return ctx;
  308. }
  309. EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);
  310. /**
  311. * eventfd_ctx_fileget - Acquires a reference to the internal eventfd context.
  312. * @file: [in] Eventfd file pointer.
  313. *
  314. * Returns a pointer to the internal eventfd context, otherwise the error
  315. * pointer:
  316. *
  317. * -EINVAL : The @fd file descriptor is not an eventfd file.
  318. */
  319. struct eventfd_ctx *eventfd_ctx_fileget(struct file *file)
  320. {
  321. if (file->f_op != &eventfd_fops)
  322. return ERR_PTR(-EINVAL);
  323. return eventfd_ctx_get(file->private_data);
  324. }
  325. EXPORT_SYMBOL_GPL(eventfd_ctx_fileget);
  326. /**
  327. * eventfd_file_create - Creates an eventfd file pointer.
  328. * @count: Initial eventfd counter value.
  329. * @flags: Flags for the eventfd file.
  330. *
  331. * This function creates an eventfd file pointer, w/out installing it into
  332. * the fd table. This is useful when the eventfd file is used during the
  333. * initialization of data structures that require extra setup after the eventfd
  334. * creation. So the eventfd creation is split into the file pointer creation
  335. * phase, and the file descriptor installation phase.
  336. * In this way races with userspace closing the newly installed file descriptor
  337. * can be avoided.
  338. * Returns an eventfd file pointer, or a proper error pointer.
  339. */
  340. struct file *eventfd_file_create(unsigned int count, int flags)
  341. {
  342. struct file *file;
  343. struct eventfd_ctx *ctx;
  344. /* Check the EFD_* constants for consistency. */
  345. BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
  346. BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
  347. if (flags & ~EFD_FLAGS_SET)
  348. return ERR_PTR(-EINVAL);
  349. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  350. if (!ctx)
  351. return ERR_PTR(-ENOMEM);
  352. kref_init(&ctx->kref);
  353. init_waitqueue_head(&ctx->wqh);
  354. ctx->count = count;
  355. ctx->flags = flags;
  356. file = anon_inode_getfile("[eventfd]", &eventfd_fops, ctx,
  357. O_RDWR | (flags & EFD_SHARED_FCNTL_FLAGS));
  358. if (IS_ERR(file))
  359. eventfd_free_ctx(ctx);
  360. return file;
  361. }
  362. SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
  363. {
  364. int fd, error;
  365. struct file *file;
  366. error = get_unused_fd_flags(flags & EFD_SHARED_FCNTL_FLAGS);
  367. if (error < 0)
  368. return error;
  369. fd = error;
  370. file = eventfd_file_create(count, flags);
  371. if (IS_ERR(file)) {
  372. error = PTR_ERR(file);
  373. goto err_put_unused_fd;
  374. }
  375. fd_install(fd, file);
  376. return fd;
  377. err_put_unused_fd:
  378. put_unused_fd(fd);
  379. return error;
  380. }
  381. SYSCALL_DEFINE1(eventfd, unsigned int, count)
  382. {
  383. return sys_eventfd2(count, 0);
  384. }