eventfd.c 11 KB

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