poll.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/fs.h>
  5. #include <linux/file.h>
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/poll.h>
  9. #include <linux/hashtable.h>
  10. #include <linux/io_uring.h>
  11. #include <trace/events/io_uring.h>
  12. #include <uapi/linux/io_uring.h>
  13. #include "io_uring.h"
  14. #include "alloc_cache.h"
  15. #include "refs.h"
  16. #include "napi.h"
  17. #include "opdef.h"
  18. #include "kbuf.h"
  19. #include "poll.h"
  20. #include "cancel.h"
  21. struct io_poll_update {
  22. struct file *file;
  23. u64 old_user_data;
  24. u64 new_user_data;
  25. __poll_t events;
  26. bool update_events;
  27. bool update_user_data;
  28. };
  29. struct io_poll_table {
  30. struct poll_table_struct pt;
  31. struct io_kiocb *req;
  32. int nr_entries;
  33. int error;
  34. bool owning;
  35. /* output value, set only if arm poll returns >0 */
  36. __poll_t result_mask;
  37. };
  38. #define IO_POLL_CANCEL_FLAG BIT(31)
  39. #define IO_POLL_RETRY_FLAG BIT(30)
  40. #define IO_POLL_REF_MASK GENMASK(29, 0)
  41. /*
  42. * We usually have 1-2 refs taken, 128 is more than enough and we want to
  43. * maximise the margin between this amount and the moment when it overflows.
  44. */
  45. #define IO_POLL_REF_BIAS 128
  46. #define IO_WQE_F_DOUBLE 1
  47. static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
  48. void *key);
  49. static inline struct io_kiocb *wqe_to_req(struct wait_queue_entry *wqe)
  50. {
  51. unsigned long priv = (unsigned long)wqe->private;
  52. return (struct io_kiocb *)(priv & ~IO_WQE_F_DOUBLE);
  53. }
  54. static inline bool wqe_is_double(struct wait_queue_entry *wqe)
  55. {
  56. unsigned long priv = (unsigned long)wqe->private;
  57. return priv & IO_WQE_F_DOUBLE;
  58. }
  59. static bool io_poll_get_ownership_slowpath(struct io_kiocb *req)
  60. {
  61. int v;
  62. /*
  63. * poll_refs are already elevated and we don't have much hope for
  64. * grabbing the ownership. Instead of incrementing set a retry flag
  65. * to notify the loop that there might have been some change.
  66. */
  67. v = atomic_fetch_or(IO_POLL_RETRY_FLAG, &req->poll_refs);
  68. if (v & IO_POLL_REF_MASK)
  69. return false;
  70. return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
  71. }
  72. /*
  73. * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can
  74. * bump it and acquire ownership. It's disallowed to modify requests while not
  75. * owning it, that prevents from races for enqueueing task_work's and b/w
  76. * arming poll and wakeups.
  77. */
  78. static inline bool io_poll_get_ownership(struct io_kiocb *req)
  79. {
  80. if (unlikely(atomic_read(&req->poll_refs) >= IO_POLL_REF_BIAS))
  81. return io_poll_get_ownership_slowpath(req);
  82. return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK);
  83. }
  84. static void io_poll_mark_cancelled(struct io_kiocb *req)
  85. {
  86. atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs);
  87. }
  88. static struct io_poll *io_poll_get_double(struct io_kiocb *req)
  89. {
  90. /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
  91. if (req->opcode == IORING_OP_POLL_ADD)
  92. return req->async_data;
  93. return req->apoll->double_poll;
  94. }
  95. static struct io_poll *io_poll_get_single(struct io_kiocb *req)
  96. {
  97. if (req->opcode == IORING_OP_POLL_ADD)
  98. return io_kiocb_to_cmd(req, struct io_poll);
  99. return &req->apoll->poll;
  100. }
  101. static void io_poll_req_insert(struct io_kiocb *req)
  102. {
  103. struct io_hash_table *table = &req->ctx->cancel_table;
  104. u32 index = hash_long(req->cqe.user_data, table->hash_bits);
  105. lockdep_assert_held(&req->ctx->uring_lock);
  106. hlist_add_head(&req->hash_node, &table->hbs[index].list);
  107. }
  108. static void io_init_poll_iocb(struct io_poll *poll, __poll_t events)
  109. {
  110. poll->head = NULL;
  111. #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
  112. /* mask in events that we always want/need */
  113. poll->events = events | IO_POLL_UNMASK;
  114. INIT_LIST_HEAD(&poll->wait.entry);
  115. init_waitqueue_func_entry(&poll->wait, io_poll_wake);
  116. }
  117. static void io_poll_remove_waitq(struct io_poll *poll)
  118. {
  119. /*
  120. * If the waitqueue is being freed early but someone is already holds
  121. * ownership over it, we have to tear down the request as best we can.
  122. * That means immediately removing the request from its waitqueue and
  123. * preventing all further accesses to the waitqueue via the request.
  124. */
  125. list_del_init(&poll->wait.entry);
  126. /*
  127. * Careful: this *must* be the last step, since as soon as req->head is
  128. * NULL'ed out, the request can be completed and freed, since
  129. * io_poll_remove_entry() will no longer need to take the waitqueue
  130. * lock.
  131. */
  132. smp_store_release(&poll->head, NULL);
  133. }
  134. static inline void io_poll_remove_entry(struct io_poll *poll)
  135. {
  136. struct wait_queue_head *head = smp_load_acquire(&poll->head);
  137. if (head) {
  138. spin_lock_irq(&head->lock);
  139. io_poll_remove_waitq(poll);
  140. spin_unlock_irq(&head->lock);
  141. }
  142. }
  143. static void io_poll_remove_entries(struct io_kiocb *req)
  144. {
  145. /*
  146. * Nothing to do if neither of those flags are set. Avoid dipping
  147. * into the poll/apoll/double cachelines if we can.
  148. */
  149. if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL)))
  150. return;
  151. /*
  152. * While we hold the waitqueue lock and the waitqueue is nonempty,
  153. * wake_up_pollfree() will wait for us. However, taking the waitqueue
  154. * lock in the first place can race with the waitqueue being freed.
  155. *
  156. * We solve this as eventpoll does: by taking advantage of the fact that
  157. * all users of wake_up_pollfree() will RCU-delay the actual free. If
  158. * we enter rcu_read_lock() and see that the pointer to the queue is
  159. * non-NULL, we can then lock it without the memory being freed out from
  160. * under us.
  161. *
  162. * Keep holding rcu_read_lock() as long as we hold the queue lock, in
  163. * case the caller deletes the entry from the queue, leaving it empty.
  164. * In that case, only RCU prevents the queue memory from being freed.
  165. */
  166. rcu_read_lock();
  167. if (req->flags & REQ_F_SINGLE_POLL)
  168. io_poll_remove_entry(io_poll_get_single(req));
  169. if (req->flags & REQ_F_DOUBLE_POLL)
  170. io_poll_remove_entry(io_poll_get_double(req));
  171. rcu_read_unlock();
  172. }
  173. enum {
  174. IOU_POLL_DONE = 0,
  175. IOU_POLL_NO_ACTION = 1,
  176. IOU_POLL_REMOVE_POLL_USE_RES = 2,
  177. IOU_POLL_REISSUE = 3,
  178. IOU_POLL_REQUEUE = 4,
  179. };
  180. static void __io_poll_execute(struct io_kiocb *req, int mask)
  181. {
  182. unsigned flags = 0;
  183. io_req_set_res(req, mask, 0);
  184. req->io_task_work.func = io_poll_task_func;
  185. trace_io_uring_task_add(req, mask);
  186. if (!(req->flags & REQ_F_POLL_NO_LAZY))
  187. flags = IOU_F_TWQ_LAZY_WAKE;
  188. __io_req_task_work_add(req, flags);
  189. }
  190. static inline void io_poll_execute(struct io_kiocb *req, int res)
  191. {
  192. if (io_poll_get_ownership(req))
  193. __io_poll_execute(req, res);
  194. }
  195. /*
  196. * All poll tw should go through this. Checks for poll events, manages
  197. * references, does rewait, etc.
  198. *
  199. * Returns a negative error on failure. IOU_POLL_NO_ACTION when no action
  200. * require, which is either spurious wakeup or multishot CQE is served.
  201. * IOU_POLL_DONE when it's done with the request, then the mask is stored in
  202. * req->cqe.res. IOU_POLL_REMOVE_POLL_USE_RES indicates to remove multishot
  203. * poll and that the result is stored in req->cqe.
  204. */
  205. static int io_poll_check_events(struct io_kiocb *req, io_tw_token_t tw)
  206. {
  207. int v;
  208. if (unlikely(tw.cancel))
  209. return -ECANCELED;
  210. do {
  211. v = atomic_read(&req->poll_refs);
  212. if (unlikely(v != 1)) {
  213. /* tw should be the owner and so have some refs */
  214. if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK)))
  215. return IOU_POLL_NO_ACTION;
  216. if (v & IO_POLL_CANCEL_FLAG)
  217. return -ECANCELED;
  218. /*
  219. * cqe.res contains only events of the first wake up
  220. * and all others are to be lost. Redo vfs_poll() to get
  221. * up to date state.
  222. */
  223. if ((v & IO_POLL_REF_MASK) != 1)
  224. req->cqe.res = 0;
  225. if (v & IO_POLL_RETRY_FLAG) {
  226. req->cqe.res = 0;
  227. /*
  228. * We won't find new events that came in between
  229. * vfs_poll and the ref put unless we clear the
  230. * flag in advance.
  231. */
  232. atomic_andnot(IO_POLL_RETRY_FLAG, &req->poll_refs);
  233. v &= ~IO_POLL_RETRY_FLAG;
  234. }
  235. v &= IO_POLL_REF_MASK;
  236. }
  237. /* the mask was stashed in __io_poll_execute */
  238. if (!req->cqe.res) {
  239. struct poll_table_struct pt = { ._key = req->apoll_events };
  240. req->cqe.res = vfs_poll(req->file, &pt) & req->apoll_events;
  241. /*
  242. * We got woken with a mask, but someone else got to
  243. * it first. The above vfs_poll() doesn't add us back
  244. * to the waitqueue, so if we get nothing back, we
  245. * should be safe and attempt a reissue.
  246. */
  247. if (unlikely(!req->cqe.res)) {
  248. /* Multishot armed need not reissue */
  249. if (!(req->apoll_events & EPOLLONESHOT))
  250. continue;
  251. return IOU_POLL_REISSUE;
  252. }
  253. }
  254. if (req->apoll_events & EPOLLONESHOT)
  255. return IOU_POLL_DONE;
  256. /* multishot, just fill a CQE and proceed */
  257. if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
  258. __poll_t mask = mangle_poll(req->cqe.res &
  259. req->apoll_events);
  260. if (!io_req_post_cqe(req, mask, IORING_CQE_F_MORE)) {
  261. io_req_set_res(req, mask, 0);
  262. return IOU_POLL_REMOVE_POLL_USE_RES;
  263. }
  264. } else {
  265. int ret;
  266. /* multiple refs and HUP, ensure we loop once more */
  267. if ((req->cqe.res & (POLLHUP | POLLRDHUP)) && v != 1)
  268. v--;
  269. ret = io_poll_issue(req, tw);
  270. if (ret == IOU_COMPLETE)
  271. return IOU_POLL_REMOVE_POLL_USE_RES;
  272. else if (ret == IOU_REQUEUE)
  273. return IOU_POLL_REQUEUE;
  274. if (ret != IOU_RETRY && ret < 0)
  275. return ret;
  276. }
  277. /* force the next iteration to vfs_poll() */
  278. req->cqe.res = 0;
  279. /*
  280. * Release all references, retry if someone tried to restart
  281. * task_work while we were executing it.
  282. */
  283. } while (atomic_sub_return(v, &req->poll_refs) & IO_POLL_REF_MASK);
  284. io_napi_add(req);
  285. return IOU_POLL_NO_ACTION;
  286. }
  287. void io_poll_task_func(struct io_tw_req tw_req, io_tw_token_t tw)
  288. {
  289. struct io_kiocb *req = tw_req.req;
  290. int ret;
  291. ret = io_poll_check_events(req, tw);
  292. if (ret == IOU_POLL_NO_ACTION) {
  293. return;
  294. } else if (ret == IOU_POLL_REQUEUE) {
  295. __io_poll_execute(req, 0);
  296. return;
  297. }
  298. io_poll_remove_entries(req);
  299. /* task_work always has ->uring_lock held */
  300. hash_del(&req->hash_node);
  301. if (req->opcode == IORING_OP_POLL_ADD) {
  302. if (ret == IOU_POLL_DONE) {
  303. struct io_poll *poll;
  304. poll = io_kiocb_to_cmd(req, struct io_poll);
  305. req->cqe.res = mangle_poll(req->cqe.res & poll->events);
  306. } else if (ret == IOU_POLL_REISSUE) {
  307. io_req_task_submit(tw_req, tw);
  308. return;
  309. } else if (ret != IOU_POLL_REMOVE_POLL_USE_RES) {
  310. req->cqe.res = ret;
  311. req_set_fail(req);
  312. }
  313. io_req_set_res(req, req->cqe.res, 0);
  314. io_req_task_complete(tw_req, tw);
  315. } else {
  316. io_tw_lock(req->ctx, tw);
  317. if (ret == IOU_POLL_REMOVE_POLL_USE_RES)
  318. io_req_task_complete(tw_req, tw);
  319. else if (ret == IOU_POLL_DONE || ret == IOU_POLL_REISSUE)
  320. io_req_task_submit(tw_req, tw);
  321. else
  322. io_req_defer_failed(req, ret);
  323. }
  324. }
  325. static void io_poll_cancel_req(struct io_kiocb *req)
  326. {
  327. io_poll_mark_cancelled(req);
  328. /* kick tw, which should complete the request */
  329. io_poll_execute(req, 0);
  330. }
  331. #define IO_ASYNC_POLL_COMMON (EPOLLONESHOT | EPOLLPRI)
  332. static __cold int io_pollfree_wake(struct io_kiocb *req, struct io_poll *poll)
  333. {
  334. io_poll_mark_cancelled(req);
  335. /* we have to kick tw in case it's not already */
  336. io_poll_execute(req, 0);
  337. io_poll_remove_waitq(poll);
  338. return 1;
  339. }
  340. static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
  341. void *key)
  342. {
  343. struct io_kiocb *req = wqe_to_req(wait);
  344. struct io_poll *poll = container_of(wait, struct io_poll, wait);
  345. __poll_t mask = key_to_poll(key);
  346. if (unlikely(mask & POLLFREE))
  347. return io_pollfree_wake(req, poll);
  348. /* for instances that support it check for an event match first */
  349. if (mask && !(mask & (poll->events & ~IO_ASYNC_POLL_COMMON)))
  350. return 0;
  351. if (io_poll_get_ownership(req)) {
  352. /*
  353. * If we trigger a multishot poll off our own wakeup path,
  354. * disable multishot as there is a circular dependency between
  355. * CQ posting and triggering the event.
  356. */
  357. if (mask & EPOLL_URING_WAKE)
  358. poll->events |= EPOLLONESHOT;
  359. /* optional, saves extra locking for removal in tw handler */
  360. if (mask && poll->events & EPOLLONESHOT) {
  361. io_poll_remove_waitq(poll);
  362. if (wqe_is_double(wait))
  363. req->flags &= ~REQ_F_DOUBLE_POLL;
  364. else
  365. req->flags &= ~REQ_F_SINGLE_POLL;
  366. }
  367. __io_poll_execute(req, mask);
  368. }
  369. return 1;
  370. }
  371. /* fails only when polling is already completing by the first entry */
  372. static bool io_poll_double_prepare(struct io_kiocb *req)
  373. {
  374. struct wait_queue_head *head;
  375. struct io_poll *poll = io_poll_get_single(req);
  376. /* head is RCU protected, see io_poll_remove_entries() comments */
  377. rcu_read_lock();
  378. head = smp_load_acquire(&poll->head);
  379. /*
  380. * poll arm might not hold ownership and so race for req->flags with
  381. * io_poll_wake(). There is only one poll entry queued, serialise with
  382. * it by taking its head lock. As we're still arming the tw hanlder
  383. * is not going to be run, so there are no races with it.
  384. */
  385. if (head) {
  386. spin_lock_irq(&head->lock);
  387. req->flags |= REQ_F_DOUBLE_POLL;
  388. if (req->opcode == IORING_OP_POLL_ADD)
  389. req->flags |= REQ_F_ASYNC_DATA;
  390. spin_unlock_irq(&head->lock);
  391. }
  392. rcu_read_unlock();
  393. return !!head;
  394. }
  395. static void __io_queue_proc(struct io_poll *poll, struct io_poll_table *pt,
  396. struct wait_queue_head *head,
  397. struct io_poll **poll_ptr)
  398. {
  399. struct io_kiocb *req = pt->req;
  400. unsigned long wqe_private = (unsigned long) req;
  401. /*
  402. * The file being polled uses multiple waitqueues for poll handling
  403. * (e.g. one for read, one for write). Setup a separate io_poll
  404. * if this happens.
  405. */
  406. if (unlikely(pt->nr_entries)) {
  407. struct io_poll *first = poll;
  408. /* double add on the same waitqueue head, ignore */
  409. if (first->head == head)
  410. return;
  411. /* already have a 2nd entry, fail a third attempt */
  412. if (*poll_ptr) {
  413. if ((*poll_ptr)->head == head)
  414. return;
  415. pt->error = -EINVAL;
  416. return;
  417. }
  418. poll = kmalloc_obj(*poll, GFP_ATOMIC);
  419. if (!poll) {
  420. pt->error = -ENOMEM;
  421. return;
  422. }
  423. /* mark as double wq entry */
  424. wqe_private |= IO_WQE_F_DOUBLE;
  425. io_init_poll_iocb(poll, first->events);
  426. if (!io_poll_double_prepare(req)) {
  427. /* the request is completing, just back off */
  428. kfree(poll);
  429. return;
  430. }
  431. *poll_ptr = poll;
  432. } else {
  433. /* fine to modify, there is no poll queued to race with us */
  434. req->flags |= REQ_F_SINGLE_POLL;
  435. }
  436. pt->nr_entries++;
  437. poll->head = head;
  438. poll->wait.private = (void *) wqe_private;
  439. if (poll->events & EPOLLEXCLUSIVE) {
  440. add_wait_queue_exclusive(head, &poll->wait);
  441. } else {
  442. add_wait_queue(head, &poll->wait);
  443. }
  444. }
  445. static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
  446. struct poll_table_struct *p)
  447. {
  448. struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
  449. struct io_poll *poll = io_kiocb_to_cmd(pt->req, struct io_poll);
  450. __io_queue_proc(poll, pt, head,
  451. (struct io_poll **) &pt->req->async_data);
  452. }
  453. static bool io_poll_can_finish_inline(struct io_kiocb *req,
  454. struct io_poll_table *pt)
  455. {
  456. return pt->owning || io_poll_get_ownership(req);
  457. }
  458. static void io_poll_add_hash(struct io_kiocb *req, unsigned int issue_flags)
  459. {
  460. struct io_ring_ctx *ctx = req->ctx;
  461. io_ring_submit_lock(ctx, issue_flags);
  462. io_poll_req_insert(req);
  463. io_ring_submit_unlock(ctx, issue_flags);
  464. }
  465. /*
  466. * Returns 0 when it's handed over for polling. The caller owns the requests if
  467. * it returns non-zero, but otherwise should not touch it. Negative values
  468. * contain an error code. When the result is >0, the polling has completed
  469. * inline and ipt.result_mask is set to the mask.
  470. */
  471. static int __io_arm_poll_handler(struct io_kiocb *req,
  472. struct io_poll *poll,
  473. struct io_poll_table *ipt, __poll_t mask,
  474. unsigned issue_flags)
  475. {
  476. INIT_HLIST_NODE(&req->hash_node);
  477. io_init_poll_iocb(poll, mask);
  478. poll->file = req->file;
  479. req->apoll_events = poll->events;
  480. ipt->pt._key = mask;
  481. ipt->req = req;
  482. ipt->error = 0;
  483. ipt->nr_entries = 0;
  484. /*
  485. * Polling is either completed here or via task_work, so if we're in the
  486. * task context we're naturally serialised with tw by merit of running
  487. * the same task. When it's io-wq, take the ownership to prevent tw
  488. * from running. However, when we're in the task context, skip taking
  489. * it as an optimisation.
  490. *
  491. * Note: even though the request won't be completed/freed, without
  492. * ownership we still can race with io_poll_wake().
  493. * io_poll_can_finish_inline() tries to deal with that.
  494. */
  495. ipt->owning = issue_flags & IO_URING_F_UNLOCKED;
  496. atomic_set(&req->poll_refs, (int)ipt->owning);
  497. /*
  498. * Exclusive waits may only wake a limited amount of entries
  499. * rather than all of them, this may interfere with lazy
  500. * wake if someone does wait(events > 1). Ensure we don't do
  501. * lazy wake for those, as we need to process each one as they
  502. * come in.
  503. */
  504. if (poll->events & EPOLLEXCLUSIVE)
  505. req->flags |= REQ_F_POLL_NO_LAZY;
  506. mask = vfs_poll(req->file, &ipt->pt) & poll->events;
  507. if (unlikely(ipt->error || !ipt->nr_entries)) {
  508. io_poll_remove_entries(req);
  509. if (!io_poll_can_finish_inline(req, ipt)) {
  510. io_poll_mark_cancelled(req);
  511. return 0;
  512. } else if (mask && (poll->events & EPOLLET)) {
  513. ipt->result_mask = mask;
  514. return 1;
  515. }
  516. return ipt->error ?: -EINVAL;
  517. }
  518. if (mask &&
  519. ((poll->events & (EPOLLET|EPOLLONESHOT)) == (EPOLLET|EPOLLONESHOT))) {
  520. if (!io_poll_can_finish_inline(req, ipt)) {
  521. io_poll_add_hash(req, issue_flags);
  522. return 0;
  523. }
  524. io_poll_remove_entries(req);
  525. ipt->result_mask = mask;
  526. /* no one else has access to the req, forget about the ref */
  527. return 1;
  528. }
  529. io_poll_add_hash(req, issue_flags);
  530. if (mask && (poll->events & EPOLLET) &&
  531. io_poll_can_finish_inline(req, ipt)) {
  532. __io_poll_execute(req, mask);
  533. return 0;
  534. }
  535. io_napi_add(req);
  536. if (ipt->owning) {
  537. /*
  538. * Try to release ownership. If we see a change of state, e.g.
  539. * poll was waken up, queue up a tw, it'll deal with it.
  540. */
  541. if (atomic_cmpxchg(&req->poll_refs, 1, 0) != 1)
  542. __io_poll_execute(req, 0);
  543. }
  544. return 0;
  545. }
  546. static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
  547. struct poll_table_struct *p)
  548. {
  549. struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
  550. struct async_poll *apoll = pt->req->apoll;
  551. __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
  552. }
  553. /*
  554. * We can't reliably detect loops in repeated poll triggers and issue
  555. * subsequently failing. But rather than fail these immediately, allow a
  556. * certain amount of retries before we give up. Given that this condition
  557. * should _rarely_ trigger even once, we should be fine with a larger value.
  558. */
  559. #define APOLL_MAX_RETRY 128
  560. static struct async_poll *io_req_alloc_apoll(struct io_kiocb *req,
  561. unsigned issue_flags)
  562. {
  563. struct io_ring_ctx *ctx = req->ctx;
  564. struct async_poll *apoll;
  565. if (req->flags & REQ_F_POLLED) {
  566. apoll = req->apoll;
  567. kfree(apoll->double_poll);
  568. } else {
  569. if (!(issue_flags & IO_URING_F_UNLOCKED))
  570. apoll = io_cache_alloc(&ctx->apoll_cache, GFP_ATOMIC);
  571. else
  572. apoll = kmalloc_obj(*apoll, GFP_ATOMIC);
  573. if (!apoll)
  574. return NULL;
  575. apoll->poll.retries = APOLL_MAX_RETRY;
  576. }
  577. apoll->double_poll = NULL;
  578. req->apoll = apoll;
  579. if (unlikely(!--apoll->poll.retries))
  580. return NULL;
  581. return apoll;
  582. }
  583. int io_arm_apoll(struct io_kiocb *req, unsigned issue_flags, __poll_t mask)
  584. {
  585. struct async_poll *apoll;
  586. struct io_poll_table ipt;
  587. int ret;
  588. mask |= EPOLLET;
  589. if (!io_file_can_poll(req))
  590. return IO_APOLL_ABORTED;
  591. if (!(req->flags & REQ_F_APOLL_MULTISHOT))
  592. mask |= EPOLLONESHOT;
  593. apoll = io_req_alloc_apoll(req, issue_flags);
  594. if (!apoll)
  595. return IO_APOLL_ABORTED;
  596. req->flags &= ~(REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL);
  597. req->flags |= REQ_F_POLLED;
  598. ipt.pt._qproc = io_async_queue_proc;
  599. ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, issue_flags);
  600. if (ret)
  601. return ret > 0 ? IO_APOLL_READY : IO_APOLL_ABORTED;
  602. trace_io_uring_poll_arm(req, mask, apoll->poll.events);
  603. return IO_APOLL_OK;
  604. }
  605. int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags)
  606. {
  607. const struct io_issue_def *def = &io_issue_defs[req->opcode];
  608. __poll_t mask = POLLPRI | POLLERR;
  609. if (!def->pollin && !def->pollout)
  610. return IO_APOLL_ABORTED;
  611. if (!io_file_can_poll(req))
  612. return IO_APOLL_ABORTED;
  613. if (def->pollin) {
  614. mask |= EPOLLIN | EPOLLRDNORM;
  615. /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
  616. if (req->flags & REQ_F_CLEAR_POLLIN)
  617. mask &= ~EPOLLIN;
  618. } else {
  619. mask |= EPOLLOUT | EPOLLWRNORM;
  620. }
  621. if (def->poll_exclusive)
  622. mask |= EPOLLEXCLUSIVE;
  623. return io_arm_apoll(req, issue_flags, mask);
  624. }
  625. /*
  626. * Returns true if we found and killed one or more poll requests
  627. */
  628. __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, struct io_uring_task *tctx,
  629. bool cancel_all)
  630. {
  631. unsigned nr_buckets = 1U << ctx->cancel_table.hash_bits;
  632. struct hlist_node *tmp;
  633. struct io_kiocb *req;
  634. bool found = false;
  635. int i;
  636. lockdep_assert_held(&ctx->uring_lock);
  637. for (i = 0; i < nr_buckets; i++) {
  638. struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
  639. hlist_for_each_entry_safe(req, tmp, &hb->list, hash_node) {
  640. if (io_match_task_safe(req, tctx, cancel_all)) {
  641. hlist_del_init(&req->hash_node);
  642. io_poll_cancel_req(req);
  643. found = true;
  644. }
  645. }
  646. }
  647. return found;
  648. }
  649. static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, bool poll_only,
  650. struct io_cancel_data *cd)
  651. {
  652. struct io_kiocb *req;
  653. u32 index = hash_long(cd->data, ctx->cancel_table.hash_bits);
  654. struct io_hash_bucket *hb = &ctx->cancel_table.hbs[index];
  655. hlist_for_each_entry(req, &hb->list, hash_node) {
  656. if (cd->data != req->cqe.user_data)
  657. continue;
  658. if (poll_only && req->opcode != IORING_OP_POLL_ADD)
  659. continue;
  660. if (cd->flags & IORING_ASYNC_CANCEL_ALL) {
  661. if (io_cancel_match_sequence(req, cd->seq))
  662. continue;
  663. }
  664. return req;
  665. }
  666. return NULL;
  667. }
  668. static struct io_kiocb *io_poll_file_find(struct io_ring_ctx *ctx,
  669. struct io_cancel_data *cd)
  670. {
  671. unsigned nr_buckets = 1U << ctx->cancel_table.hash_bits;
  672. struct io_kiocb *req;
  673. int i;
  674. for (i = 0; i < nr_buckets; i++) {
  675. struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
  676. hlist_for_each_entry(req, &hb->list, hash_node) {
  677. if (io_cancel_req_match(req, cd))
  678. return req;
  679. }
  680. }
  681. return NULL;
  682. }
  683. static int io_poll_disarm(struct io_kiocb *req)
  684. {
  685. if (!req)
  686. return -ENOENT;
  687. if (!io_poll_get_ownership(req))
  688. return -EALREADY;
  689. io_poll_remove_entries(req);
  690. hash_del(&req->hash_node);
  691. return 0;
  692. }
  693. static int __io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd)
  694. {
  695. struct io_kiocb *req;
  696. if (cd->flags & (IORING_ASYNC_CANCEL_FD | IORING_ASYNC_CANCEL_OP |
  697. IORING_ASYNC_CANCEL_ANY))
  698. req = io_poll_file_find(ctx, cd);
  699. else
  700. req = io_poll_find(ctx, false, cd);
  701. if (req) {
  702. io_poll_cancel_req(req);
  703. return 0;
  704. }
  705. return -ENOENT;
  706. }
  707. int io_poll_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd,
  708. unsigned issue_flags)
  709. {
  710. int ret;
  711. io_ring_submit_lock(ctx, issue_flags);
  712. ret = __io_poll_cancel(ctx, cd);
  713. io_ring_submit_unlock(ctx, issue_flags);
  714. return ret;
  715. }
  716. static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
  717. unsigned int flags)
  718. {
  719. u32 events;
  720. events = READ_ONCE(sqe->poll32_events);
  721. #ifdef __BIG_ENDIAN
  722. events = swahw32(events);
  723. #endif
  724. if (!(flags & IORING_POLL_ADD_MULTI))
  725. events |= EPOLLONESHOT;
  726. if (!(flags & IORING_POLL_ADD_LEVEL))
  727. events |= EPOLLET;
  728. return demangle_poll(events) |
  729. (events & (EPOLLEXCLUSIVE|EPOLLONESHOT|EPOLLET));
  730. }
  731. int io_poll_remove_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  732. {
  733. struct io_poll_update *upd = io_kiocb_to_cmd(req, struct io_poll_update);
  734. u32 flags;
  735. if (sqe->buf_index || sqe->splice_fd_in)
  736. return -EINVAL;
  737. flags = READ_ONCE(sqe->len);
  738. if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
  739. IORING_POLL_ADD_MULTI))
  740. return -EINVAL;
  741. /* meaningless without update */
  742. if (flags == IORING_POLL_ADD_MULTI)
  743. return -EINVAL;
  744. upd->old_user_data = READ_ONCE(sqe->addr);
  745. upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
  746. upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
  747. upd->new_user_data = READ_ONCE(sqe->off);
  748. if (!upd->update_user_data && upd->new_user_data)
  749. return -EINVAL;
  750. if (upd->update_events)
  751. upd->events = io_poll_parse_events(sqe, flags);
  752. else if (sqe->poll32_events)
  753. return -EINVAL;
  754. return 0;
  755. }
  756. int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  757. {
  758. struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
  759. u32 flags;
  760. if (sqe->buf_index || sqe->off || sqe->addr)
  761. return -EINVAL;
  762. flags = READ_ONCE(sqe->len);
  763. if (flags & ~IORING_POLL_ADD_MULTI)
  764. return -EINVAL;
  765. if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP))
  766. return -EINVAL;
  767. poll->events = io_poll_parse_events(sqe, flags);
  768. return 0;
  769. }
  770. int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
  771. {
  772. struct io_poll *poll = io_kiocb_to_cmd(req, struct io_poll);
  773. struct io_poll_table ipt;
  774. int ret;
  775. ipt.pt._qproc = io_poll_queue_proc;
  776. ret = __io_arm_poll_handler(req, poll, &ipt, poll->events, issue_flags);
  777. if (ret > 0) {
  778. io_req_set_res(req, ipt.result_mask, 0);
  779. return IOU_COMPLETE;
  780. }
  781. return ret ?: IOU_ISSUE_SKIP_COMPLETE;
  782. }
  783. int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
  784. {
  785. struct io_poll_update *poll_update = io_kiocb_to_cmd(req, struct io_poll_update);
  786. struct io_ring_ctx *ctx = req->ctx;
  787. struct io_cancel_data cd = { .ctx = ctx, .data = poll_update->old_user_data, };
  788. struct io_kiocb *preq;
  789. int ret2, ret = 0;
  790. io_ring_submit_lock(ctx, issue_flags);
  791. preq = io_poll_find(ctx, true, &cd);
  792. ret2 = io_poll_disarm(preq);
  793. if (ret2) {
  794. ret = ret2;
  795. goto out;
  796. }
  797. if (WARN_ON_ONCE(preq->opcode != IORING_OP_POLL_ADD)) {
  798. ret = -EFAULT;
  799. goto out;
  800. }
  801. if (poll_update->update_events || poll_update->update_user_data) {
  802. /* only mask one event flags, keep behavior flags */
  803. if (poll_update->update_events) {
  804. struct io_poll *poll = io_kiocb_to_cmd(preq, struct io_poll);
  805. poll->events &= ~0xffff;
  806. poll->events |= poll_update->events & 0xffff;
  807. poll->events |= IO_POLL_UNMASK;
  808. }
  809. if (poll_update->update_user_data)
  810. preq->cqe.user_data = poll_update->new_user_data;
  811. ret2 = io_poll_add(preq, issue_flags & ~IO_URING_F_UNLOCKED);
  812. /* successfully updated, don't complete poll request */
  813. if (ret2 == IOU_ISSUE_SKIP_COMPLETE)
  814. goto out;
  815. /* request completed as part of the update, complete it */
  816. else if (ret2 == IOU_COMPLETE)
  817. goto complete;
  818. }
  819. io_req_set_res(preq, -ECANCELED, 0);
  820. complete:
  821. if (preq->cqe.res < 0)
  822. req_set_fail(preq);
  823. preq->io_task_work.func = io_req_task_complete;
  824. io_req_task_work_add(preq);
  825. out:
  826. io_ring_submit_unlock(ctx, issue_flags);
  827. if (ret < 0) {
  828. req_set_fail(req);
  829. return ret;
  830. }
  831. /* complete update request, we're done with it */
  832. io_req_set_res(req, ret, 0);
  833. return IOU_COMPLETE;
  834. }