rw.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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/blk-mq.h>
  7. #include <linux/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/fsnotify.h>
  10. #include <linux/poll.h>
  11. #include <linux/nospec.h>
  12. #include <linux/compat.h>
  13. #include <linux/io_uring/cmd.h>
  14. #include <linux/indirect_call_wrapper.h>
  15. #include <uapi/linux/io_uring.h>
  16. #include "filetable.h"
  17. #include "io_uring.h"
  18. #include "opdef.h"
  19. #include "kbuf.h"
  20. #include "alloc_cache.h"
  21. #include "rsrc.h"
  22. #include "poll.h"
  23. #include "rw.h"
  24. static void io_complete_rw(struct kiocb *kiocb, long res);
  25. static void io_complete_rw_iopoll(struct kiocb *kiocb, long res);
  26. struct io_rw {
  27. /* NOTE: kiocb has the file as the first member, so don't do it here */
  28. struct kiocb kiocb;
  29. u64 addr;
  30. u32 len;
  31. rwf_t flags;
  32. };
  33. static bool io_file_supports_nowait(struct io_kiocb *req, __poll_t mask)
  34. {
  35. /* If FMODE_NOWAIT is set for a file, we're golden */
  36. if (req->flags & REQ_F_SUPPORT_NOWAIT)
  37. return true;
  38. /* No FMODE_NOWAIT, if we can poll, check the status */
  39. if (io_file_can_poll(req)) {
  40. struct poll_table_struct pt = { ._key = mask };
  41. return vfs_poll(req->file, &pt) & mask;
  42. }
  43. /* No FMODE_NOWAIT support, and file isn't pollable. Tough luck. */
  44. return false;
  45. }
  46. static int io_iov_compat_buffer_select_prep(struct io_rw *rw)
  47. {
  48. struct compat_iovec __user *uiov = u64_to_user_ptr(rw->addr);
  49. struct compat_iovec iov;
  50. if (copy_from_user(&iov, uiov, sizeof(iov)))
  51. return -EFAULT;
  52. rw->len = iov.iov_len;
  53. return 0;
  54. }
  55. static int io_iov_buffer_select_prep(struct io_kiocb *req)
  56. {
  57. struct iovec __user *uiov;
  58. struct iovec iov;
  59. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  60. if (rw->len != 1)
  61. return -EINVAL;
  62. if (io_is_compat(req->ctx))
  63. return io_iov_compat_buffer_select_prep(rw);
  64. uiov = u64_to_user_ptr(rw->addr);
  65. if (copy_from_user(&iov, uiov, sizeof(*uiov)))
  66. return -EFAULT;
  67. rw->len = iov.iov_len;
  68. return 0;
  69. }
  70. static int io_import_vec(int ddir, struct io_kiocb *req,
  71. struct io_async_rw *io,
  72. const struct iovec __user *uvec,
  73. size_t uvec_segs)
  74. {
  75. int ret, nr_segs;
  76. struct iovec *iov;
  77. if (io->vec.iovec) {
  78. nr_segs = io->vec.nr;
  79. iov = io->vec.iovec;
  80. } else {
  81. nr_segs = 1;
  82. iov = &io->fast_iov;
  83. }
  84. ret = __import_iovec(ddir, uvec, uvec_segs, nr_segs, &iov, &io->iter,
  85. io_is_compat(req->ctx));
  86. if (unlikely(ret < 0))
  87. return ret;
  88. if (iov) {
  89. req->flags |= REQ_F_NEED_CLEANUP;
  90. io_vec_reset_iovec(&io->vec, iov, io->iter.nr_segs);
  91. }
  92. return 0;
  93. }
  94. static int __io_import_rw_buffer(int ddir, struct io_kiocb *req,
  95. struct io_async_rw *io, struct io_br_sel *sel,
  96. unsigned int issue_flags)
  97. {
  98. const struct io_issue_def *def = &io_issue_defs[req->opcode];
  99. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  100. size_t sqe_len = rw->len;
  101. sel->addr = u64_to_user_ptr(rw->addr);
  102. if (def->vectored && !(req->flags & REQ_F_BUFFER_SELECT))
  103. return io_import_vec(ddir, req, io, sel->addr, sqe_len);
  104. if (io_do_buffer_select(req)) {
  105. *sel = io_buffer_select(req, &sqe_len, io->buf_group, issue_flags);
  106. if (!sel->addr)
  107. return -ENOBUFS;
  108. rw->addr = (unsigned long) sel->addr;
  109. rw->len = sqe_len;
  110. }
  111. return import_ubuf(ddir, sel->addr, sqe_len, &io->iter);
  112. }
  113. static inline int io_import_rw_buffer(int rw, struct io_kiocb *req,
  114. struct io_async_rw *io,
  115. struct io_br_sel *sel,
  116. unsigned int issue_flags)
  117. {
  118. int ret;
  119. ret = __io_import_rw_buffer(rw, req, io, sel, issue_flags);
  120. if (unlikely(ret < 0))
  121. return ret;
  122. iov_iter_save_state(&io->iter, &io->iter_state);
  123. return 0;
  124. }
  125. static bool io_rw_recycle(struct io_kiocb *req, unsigned int issue_flags)
  126. {
  127. struct io_async_rw *rw = req->async_data;
  128. if (unlikely(issue_flags & IO_URING_F_UNLOCKED))
  129. return false;
  130. io_alloc_cache_vec_kasan(&rw->vec);
  131. if (rw->vec.nr > IO_VEC_CACHE_SOFT_CAP)
  132. io_vec_free(&rw->vec);
  133. if (io_alloc_cache_put(&req->ctx->rw_cache, rw)) {
  134. io_req_async_data_clear(req, 0);
  135. return true;
  136. }
  137. return false;
  138. }
  139. static void io_req_rw_cleanup(struct io_kiocb *req, unsigned int issue_flags)
  140. {
  141. /*
  142. * Disable quick recycling for anything that's gone through io-wq.
  143. * In theory, this should be fine to cleanup. However, some read or
  144. * write iter handling touches the iovec AFTER having called into the
  145. * handler, eg to reexpand or revert. This means we can have:
  146. *
  147. * task io-wq
  148. * issue
  149. * punt to io-wq
  150. * issue
  151. * blkdev_write_iter()
  152. * ->ki_complete()
  153. * io_complete_rw()
  154. * queue tw complete
  155. * run tw
  156. * req_rw_cleanup
  157. * iov_iter_count() <- look at iov_iter again
  158. *
  159. * which can lead to a UAF. This is only possible for io-wq offload
  160. * as the cleanup can run in parallel. As io-wq is not the fast path,
  161. * just leave cleanup to the end.
  162. *
  163. * This is really a bug in the core code that does this, any issue
  164. * path should assume that a successful (or -EIOCBQUEUED) return can
  165. * mean that the underlying data can be gone at any time. But that
  166. * should be fixed separately, and then this check could be killed.
  167. */
  168. if (!(req->flags & (REQ_F_REISSUE | REQ_F_REFCOUNT))) {
  169. req->flags &= ~REQ_F_NEED_CLEANUP;
  170. if (!io_rw_recycle(req, issue_flags)) {
  171. struct io_async_rw *rw = req->async_data;
  172. io_vec_free(&rw->vec);
  173. }
  174. }
  175. }
  176. static int io_rw_alloc_async(struct io_kiocb *req)
  177. {
  178. struct io_ring_ctx *ctx = req->ctx;
  179. struct io_async_rw *rw;
  180. rw = io_uring_alloc_async_data(&ctx->rw_cache, req);
  181. if (!rw)
  182. return -ENOMEM;
  183. if (rw->vec.iovec)
  184. req->flags |= REQ_F_NEED_CLEANUP;
  185. rw->bytes_done = 0;
  186. return 0;
  187. }
  188. static inline void io_meta_save_state(struct io_async_rw *io)
  189. {
  190. io->meta_state.seed = io->meta.seed;
  191. iov_iter_save_state(&io->meta.iter, &io->meta_state.iter_meta);
  192. }
  193. static inline void io_meta_restore(struct io_async_rw *io, struct kiocb *kiocb)
  194. {
  195. if (kiocb->ki_flags & IOCB_HAS_METADATA) {
  196. io->meta.seed = io->meta_state.seed;
  197. iov_iter_restore(&io->meta.iter, &io->meta_state.iter_meta);
  198. }
  199. }
  200. static int io_prep_rw_pi(struct io_kiocb *req, struct io_rw *rw, int ddir,
  201. u64 attr_ptr, u64 attr_type_mask)
  202. {
  203. struct io_uring_attr_pi pi_attr;
  204. struct io_async_rw *io;
  205. int ret;
  206. if (copy_from_user(&pi_attr, u64_to_user_ptr(attr_ptr),
  207. sizeof(pi_attr)))
  208. return -EFAULT;
  209. if (pi_attr.rsvd)
  210. return -EINVAL;
  211. io = req->async_data;
  212. io->meta.flags = pi_attr.flags;
  213. io->meta.app_tag = pi_attr.app_tag;
  214. io->meta.seed = pi_attr.seed;
  215. ret = import_ubuf(ddir, u64_to_user_ptr(pi_attr.addr),
  216. pi_attr.len, &io->meta.iter);
  217. if (unlikely(ret < 0))
  218. return ret;
  219. req->flags |= REQ_F_HAS_METADATA;
  220. io_meta_save_state(io);
  221. return ret;
  222. }
  223. static int __io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
  224. int ddir)
  225. {
  226. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  227. struct io_async_rw *io;
  228. unsigned ioprio;
  229. u64 attr_type_mask;
  230. int ret;
  231. if (io_rw_alloc_async(req))
  232. return -ENOMEM;
  233. io = req->async_data;
  234. rw->kiocb.ki_pos = READ_ONCE(sqe->off);
  235. /* used for fixed read/write too - just read unconditionally */
  236. req->buf_index = READ_ONCE(sqe->buf_index);
  237. io->buf_group = req->buf_index;
  238. ioprio = READ_ONCE(sqe->ioprio);
  239. if (ioprio) {
  240. ret = ioprio_check_cap(ioprio);
  241. if (ret)
  242. return ret;
  243. rw->kiocb.ki_ioprio = ioprio;
  244. } else {
  245. rw->kiocb.ki_ioprio = get_current_ioprio();
  246. }
  247. rw->kiocb.ki_flags = 0;
  248. rw->kiocb.ki_write_stream = READ_ONCE(sqe->write_stream);
  249. if (req->ctx->flags & IORING_SETUP_IOPOLL)
  250. rw->kiocb.ki_complete = io_complete_rw_iopoll;
  251. else
  252. rw->kiocb.ki_complete = io_complete_rw;
  253. rw->addr = READ_ONCE(sqe->addr);
  254. rw->len = READ_ONCE(sqe->len);
  255. rw->flags = (__force rwf_t) READ_ONCE(sqe->rw_flags);
  256. attr_type_mask = READ_ONCE(sqe->attr_type_mask);
  257. if (attr_type_mask) {
  258. u64 attr_ptr;
  259. /* only PI attribute is supported currently */
  260. if (attr_type_mask != IORING_RW_ATTR_FLAG_PI)
  261. return -EINVAL;
  262. attr_ptr = READ_ONCE(sqe->attr_ptr);
  263. return io_prep_rw_pi(req, rw, ddir, attr_ptr, attr_type_mask);
  264. }
  265. return 0;
  266. }
  267. static int io_rw_do_import(struct io_kiocb *req, int ddir)
  268. {
  269. struct io_br_sel sel = { };
  270. if (io_do_buffer_select(req))
  271. return 0;
  272. return io_import_rw_buffer(ddir, req, req->async_data, &sel, 0);
  273. }
  274. static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
  275. int ddir)
  276. {
  277. int ret;
  278. ret = __io_prep_rw(req, sqe, ddir);
  279. if (unlikely(ret))
  280. return ret;
  281. return io_rw_do_import(req, ddir);
  282. }
  283. int io_prep_read(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  284. {
  285. return io_prep_rw(req, sqe, ITER_DEST);
  286. }
  287. int io_prep_write(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  288. {
  289. return io_prep_rw(req, sqe, ITER_SOURCE);
  290. }
  291. static int io_prep_rwv(struct io_kiocb *req, const struct io_uring_sqe *sqe,
  292. int ddir)
  293. {
  294. int ret;
  295. ret = io_prep_rw(req, sqe, ddir);
  296. if (unlikely(ret))
  297. return ret;
  298. if (!(req->flags & REQ_F_BUFFER_SELECT))
  299. return 0;
  300. /*
  301. * Have to do this validation here, as this is in io_read() rw->len
  302. * might have changed due to buffer selection
  303. */
  304. return io_iov_buffer_select_prep(req);
  305. }
  306. int io_prep_readv(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  307. {
  308. return io_prep_rwv(req, sqe, ITER_DEST);
  309. }
  310. int io_prep_writev(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  311. {
  312. return io_prep_rwv(req, sqe, ITER_SOURCE);
  313. }
  314. static int io_init_rw_fixed(struct io_kiocb *req, unsigned int issue_flags,
  315. int ddir)
  316. {
  317. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  318. struct io_async_rw *io = req->async_data;
  319. int ret;
  320. if (io->bytes_done)
  321. return 0;
  322. ret = io_import_reg_buf(req, &io->iter, rw->addr, rw->len, ddir,
  323. issue_flags);
  324. iov_iter_save_state(&io->iter, &io->iter_state);
  325. return ret;
  326. }
  327. int io_prep_read_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  328. {
  329. return __io_prep_rw(req, sqe, ITER_DEST);
  330. }
  331. int io_prep_write_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  332. {
  333. return __io_prep_rw(req, sqe, ITER_SOURCE);
  334. }
  335. static int io_rw_import_reg_vec(struct io_kiocb *req,
  336. struct io_async_rw *io,
  337. int ddir, unsigned int issue_flags)
  338. {
  339. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  340. unsigned uvec_segs = rw->len;
  341. int ret;
  342. ret = io_import_reg_vec(ddir, &io->iter, req, &io->vec,
  343. uvec_segs, issue_flags);
  344. if (unlikely(ret))
  345. return ret;
  346. iov_iter_save_state(&io->iter, &io->iter_state);
  347. req->flags &= ~REQ_F_IMPORT_BUFFER;
  348. return 0;
  349. }
  350. static int io_rw_prep_reg_vec(struct io_kiocb *req)
  351. {
  352. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  353. struct io_async_rw *io = req->async_data;
  354. const struct iovec __user *uvec;
  355. uvec = u64_to_user_ptr(rw->addr);
  356. return io_prep_reg_iovec(req, &io->vec, uvec, rw->len);
  357. }
  358. int io_prep_readv_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  359. {
  360. int ret;
  361. ret = __io_prep_rw(req, sqe, ITER_DEST);
  362. if (unlikely(ret))
  363. return ret;
  364. return io_rw_prep_reg_vec(req);
  365. }
  366. int io_prep_writev_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  367. {
  368. int ret;
  369. ret = __io_prep_rw(req, sqe, ITER_SOURCE);
  370. if (unlikely(ret))
  371. return ret;
  372. return io_rw_prep_reg_vec(req);
  373. }
  374. /*
  375. * Multishot read is prepared just like a normal read/write request, only
  376. * difference is that we set the MULTISHOT flag.
  377. */
  378. int io_read_mshot_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  379. {
  380. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  381. int ret;
  382. /* must be used with provided buffers */
  383. if (!(req->flags & REQ_F_BUFFER_SELECT))
  384. return -EINVAL;
  385. ret = __io_prep_rw(req, sqe, ITER_DEST);
  386. if (unlikely(ret))
  387. return ret;
  388. if (rw->addr || rw->len)
  389. return -EINVAL;
  390. req->flags |= REQ_F_APOLL_MULTISHOT;
  391. return 0;
  392. }
  393. void io_readv_writev_cleanup(struct io_kiocb *req)
  394. {
  395. struct io_async_rw *rw = req->async_data;
  396. lockdep_assert_held(&req->ctx->uring_lock);
  397. io_vec_free(&rw->vec);
  398. io_rw_recycle(req, 0);
  399. }
  400. static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req)
  401. {
  402. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  403. if (rw->kiocb.ki_pos != -1)
  404. return &rw->kiocb.ki_pos;
  405. if (!(req->file->f_mode & FMODE_STREAM)) {
  406. req->flags |= REQ_F_CUR_POS;
  407. rw->kiocb.ki_pos = req->file->f_pos;
  408. return &rw->kiocb.ki_pos;
  409. }
  410. rw->kiocb.ki_pos = 0;
  411. return NULL;
  412. }
  413. static bool io_rw_should_reissue(struct io_kiocb *req)
  414. {
  415. #ifdef CONFIG_BLOCK
  416. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  417. umode_t mode = file_inode(req->file)->i_mode;
  418. struct io_async_rw *io = req->async_data;
  419. struct io_ring_ctx *ctx = req->ctx;
  420. if (!S_ISBLK(mode) && !S_ISREG(mode))
  421. return false;
  422. if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
  423. !(ctx->flags & IORING_SETUP_IOPOLL)))
  424. return false;
  425. /*
  426. * If ref is dying, we might be running poll reap from the exit work.
  427. * Don't attempt to reissue from that path, just let it fail with
  428. * -EAGAIN.
  429. */
  430. if (percpu_ref_is_dying(&ctx->refs))
  431. return false;
  432. io_meta_restore(io, &rw->kiocb);
  433. iov_iter_restore(&io->iter, &io->iter_state);
  434. return true;
  435. #else
  436. return false;
  437. #endif
  438. }
  439. static void io_req_end_write(struct io_kiocb *req)
  440. {
  441. if (req->flags & REQ_F_ISREG) {
  442. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  443. kiocb_end_write(&rw->kiocb);
  444. }
  445. }
  446. /*
  447. * Trigger the notifications after having done some IO, and finish the write
  448. * accounting, if any.
  449. */
  450. static void io_req_io_end(struct io_kiocb *req)
  451. {
  452. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  453. if (rw->kiocb.ki_flags & IOCB_WRITE) {
  454. io_req_end_write(req);
  455. fsnotify_modify(req->file);
  456. } else {
  457. fsnotify_access(req->file);
  458. }
  459. }
  460. static void __io_complete_rw_common(struct io_kiocb *req, long res)
  461. {
  462. if (res == req->cqe.res)
  463. return;
  464. if ((res == -EOPNOTSUPP || res == -EAGAIN) && io_rw_should_reissue(req)) {
  465. req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE;
  466. } else {
  467. req_set_fail(req);
  468. req->cqe.res = res;
  469. }
  470. }
  471. static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
  472. {
  473. struct io_async_rw *io = req->async_data;
  474. /* add previously done IO, if any */
  475. if (req_has_async_data(req) && io->bytes_done > 0) {
  476. if (res < 0)
  477. res = io->bytes_done;
  478. else
  479. res += io->bytes_done;
  480. }
  481. return res;
  482. }
  483. void io_req_rw_complete(struct io_tw_req tw_req, io_tw_token_t tw)
  484. {
  485. struct io_kiocb *req = tw_req.req;
  486. io_req_io_end(req);
  487. if (req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING))
  488. req->cqe.flags |= io_put_kbuf(req, req->cqe.res, NULL);
  489. io_req_rw_cleanup(req, 0);
  490. io_req_task_complete(tw_req, tw);
  491. }
  492. static void io_complete_rw(struct kiocb *kiocb, long res)
  493. {
  494. struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
  495. struct io_kiocb *req = cmd_to_io_kiocb(rw);
  496. __io_complete_rw_common(req, res);
  497. io_req_set_res(req, io_fixup_rw_res(req, res), 0);
  498. req->io_task_work.func = io_req_rw_complete;
  499. __io_req_task_work_add(req, IOU_F_TWQ_LAZY_WAKE);
  500. }
  501. static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
  502. {
  503. struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
  504. struct io_kiocb *req = cmd_to_io_kiocb(rw);
  505. if (kiocb->ki_flags & IOCB_WRITE)
  506. io_req_end_write(req);
  507. if (unlikely(res != req->cqe.res)) {
  508. if (res == -EAGAIN && io_rw_should_reissue(req))
  509. req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE;
  510. else
  511. req->cqe.res = res;
  512. }
  513. /* order with io_iopoll_complete() checking ->iopoll_completed */
  514. smp_store_release(&req->iopoll_completed, 1);
  515. }
  516. static inline void io_rw_done(struct io_kiocb *req, ssize_t ret)
  517. {
  518. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  519. /* IO was queued async, completion will happen later */
  520. if (ret == -EIOCBQUEUED)
  521. return;
  522. /* transform internal restart error codes */
  523. if (unlikely(ret < 0)) {
  524. switch (ret) {
  525. case -ERESTARTSYS:
  526. case -ERESTARTNOINTR:
  527. case -ERESTARTNOHAND:
  528. case -ERESTART_RESTARTBLOCK:
  529. /*
  530. * We can't just restart the syscall, since previously
  531. * submitted sqes may already be in progress. Just fail
  532. * this IO with EINTR.
  533. */
  534. ret = -EINTR;
  535. break;
  536. }
  537. }
  538. if (req->ctx->flags & IORING_SETUP_IOPOLL)
  539. io_complete_rw_iopoll(&rw->kiocb, ret);
  540. else
  541. io_complete_rw(&rw->kiocb, ret);
  542. }
  543. static int kiocb_done(struct io_kiocb *req, ssize_t ret,
  544. struct io_br_sel *sel, unsigned int issue_flags)
  545. {
  546. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  547. unsigned final_ret = io_fixup_rw_res(req, ret);
  548. if (ret >= 0 && req->flags & REQ_F_CUR_POS)
  549. req->file->f_pos = rw->kiocb.ki_pos;
  550. if (ret >= 0 && !(req->ctx->flags & IORING_SETUP_IOPOLL)) {
  551. u32 cflags = 0;
  552. __io_complete_rw_common(req, ret);
  553. /*
  554. * Safe to call io_end from here as we're inline
  555. * from the submission path.
  556. */
  557. io_req_io_end(req);
  558. if (sel)
  559. cflags = io_put_kbuf(req, ret, sel->buf_list);
  560. io_req_set_res(req, final_ret, cflags);
  561. io_req_rw_cleanup(req, issue_flags);
  562. return IOU_COMPLETE;
  563. } else {
  564. io_rw_done(req, ret);
  565. }
  566. return IOU_ISSUE_SKIP_COMPLETE;
  567. }
  568. static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
  569. {
  570. return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
  571. }
  572. /*
  573. * For files that don't have ->read_iter() and ->write_iter(), handle them
  574. * by looping over ->read() or ->write() manually.
  575. */
  576. static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
  577. {
  578. struct io_kiocb *req = cmd_to_io_kiocb(rw);
  579. struct kiocb *kiocb = &rw->kiocb;
  580. struct file *file = kiocb->ki_filp;
  581. ssize_t ret = 0;
  582. loff_t *ppos;
  583. /*
  584. * Don't support polled IO through this interface, and we can't
  585. * support non-blocking either. For the latter, this just causes
  586. * the kiocb to be handled from an async context.
  587. */
  588. if (kiocb->ki_flags & IOCB_HIPRI)
  589. return -EOPNOTSUPP;
  590. if ((kiocb->ki_flags & IOCB_NOWAIT) &&
  591. !(kiocb->ki_filp->f_flags & O_NONBLOCK))
  592. return -EAGAIN;
  593. if ((req->flags & REQ_F_BUF_NODE) &&
  594. (req->buf_node->buf->flags & IO_REGBUF_F_KBUF))
  595. return -EFAULT;
  596. ppos = io_kiocb_ppos(kiocb);
  597. while (iov_iter_count(iter)) {
  598. void __user *addr;
  599. size_t len;
  600. ssize_t nr;
  601. if (iter_is_ubuf(iter)) {
  602. addr = iter->ubuf + iter->iov_offset;
  603. len = iov_iter_count(iter);
  604. } else if (!iov_iter_is_bvec(iter)) {
  605. addr = iter_iov_addr(iter);
  606. len = iter_iov_len(iter);
  607. } else {
  608. addr = u64_to_user_ptr(rw->addr);
  609. len = rw->len;
  610. }
  611. if (ddir == READ)
  612. nr = file->f_op->read(file, addr, len, ppos);
  613. else
  614. nr = file->f_op->write(file, addr, len, ppos);
  615. if (nr < 0) {
  616. if (!ret)
  617. ret = nr;
  618. break;
  619. }
  620. ret += nr;
  621. if (!iov_iter_is_bvec(iter)) {
  622. iov_iter_advance(iter, nr);
  623. } else {
  624. rw->addr += nr;
  625. rw->len -= nr;
  626. if (!rw->len)
  627. break;
  628. }
  629. if (nr != len)
  630. break;
  631. }
  632. return ret;
  633. }
  634. /*
  635. * This is our waitqueue callback handler, registered through __folio_lock_async()
  636. * when we initially tried to do the IO with the iocb armed our waitqueue.
  637. * This gets called when the page is unlocked, and we generally expect that to
  638. * happen when the page IO is completed and the page is now uptodate. This will
  639. * queue a task_work based retry of the operation, attempting to copy the data
  640. * again. If the latter fails because the page was NOT uptodate, then we will
  641. * do a thread based blocking retry of the operation. That's the unexpected
  642. * slow path.
  643. */
  644. static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
  645. int sync, void *arg)
  646. {
  647. struct wait_page_queue *wpq;
  648. struct io_kiocb *req = wait->private;
  649. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  650. struct wait_page_key *key = arg;
  651. wpq = container_of(wait, struct wait_page_queue, wait);
  652. if (!wake_page_match(wpq, key))
  653. return 0;
  654. rw->kiocb.ki_flags &= ~IOCB_WAITQ;
  655. list_del_init(&wait->entry);
  656. io_req_task_queue(req);
  657. return 1;
  658. }
  659. /*
  660. * This controls whether a given IO request should be armed for async page
  661. * based retry. If we return false here, the request is handed to the async
  662. * worker threads for retry. If we're doing buffered reads on a regular file,
  663. * we prepare a private wait_page_queue entry and retry the operation. This
  664. * will either succeed because the page is now uptodate and unlocked, or it
  665. * will register a callback when the page is unlocked at IO completion. Through
  666. * that callback, io_uring uses task_work to setup a retry of the operation.
  667. * That retry will attempt the buffered read again. The retry will generally
  668. * succeed, or in rare cases where it fails, we then fall back to using the
  669. * async worker threads for a blocking retry.
  670. */
  671. static bool io_rw_should_retry(struct io_kiocb *req)
  672. {
  673. struct io_async_rw *io = req->async_data;
  674. struct wait_page_queue *wait = &io->wpq;
  675. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  676. struct kiocb *kiocb = &rw->kiocb;
  677. /*
  678. * Never retry for NOWAIT or a request with metadata, we just complete
  679. * with -EAGAIN.
  680. */
  681. if (req->flags & (REQ_F_NOWAIT | REQ_F_HAS_METADATA))
  682. return false;
  683. /* Only for buffered IO */
  684. if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
  685. return false;
  686. /*
  687. * just use poll if we can, and don't attempt if the fs doesn't
  688. * support callback based unlocks
  689. */
  690. if (io_file_can_poll(req) ||
  691. !(req->file->f_op->fop_flags & FOP_BUFFER_RASYNC))
  692. return false;
  693. wait->wait.func = io_async_buf_func;
  694. wait->wait.private = req;
  695. wait->wait.flags = 0;
  696. INIT_LIST_HEAD(&wait->wait.entry);
  697. kiocb->ki_flags |= IOCB_WAITQ;
  698. kiocb->ki_flags &= ~IOCB_NOWAIT;
  699. kiocb->ki_waitq = wait;
  700. return true;
  701. }
  702. static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter)
  703. {
  704. struct file *file = rw->kiocb.ki_filp;
  705. if (likely(file->f_op->read_iter))
  706. return file->f_op->read_iter(&rw->kiocb, iter);
  707. else if (file->f_op->read)
  708. return loop_rw_iter(READ, rw, iter);
  709. else
  710. return -EINVAL;
  711. }
  712. static bool need_complete_io(struct io_kiocb *req)
  713. {
  714. return req->flags & REQ_F_ISREG ||
  715. S_ISBLK(file_inode(req->file)->i_mode);
  716. }
  717. static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)
  718. {
  719. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  720. struct kiocb *kiocb = &rw->kiocb;
  721. struct io_ring_ctx *ctx = req->ctx;
  722. struct file *file = req->file;
  723. int ret;
  724. if (unlikely(!(file->f_mode & mode)))
  725. return -EBADF;
  726. if (!(req->flags & REQ_F_FIXED_FILE))
  727. req->flags |= io_file_get_flags(file);
  728. kiocb->ki_flags = file->f_iocb_flags;
  729. ret = kiocb_set_rw_flags(kiocb, rw->flags, rw_type);
  730. if (unlikely(ret))
  731. return ret;
  732. /*
  733. * If the file is marked O_NONBLOCK, still allow retry for it if it
  734. * supports async. Otherwise it's impossible to use O_NONBLOCK files
  735. * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
  736. */
  737. if (kiocb->ki_flags & IOCB_NOWAIT ||
  738. ((file->f_flags & O_NONBLOCK && !(req->flags & REQ_F_SUPPORT_NOWAIT))))
  739. req->flags |= REQ_F_NOWAIT;
  740. if (ctx->flags & IORING_SETUP_IOPOLL) {
  741. if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
  742. return -EOPNOTSUPP;
  743. kiocb->private = NULL;
  744. kiocb->ki_flags |= IOCB_HIPRI;
  745. req->iopoll_completed = 0;
  746. if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) {
  747. /* make sure every req only blocks once*/
  748. req->flags &= ~REQ_F_IOPOLL_STATE;
  749. req->iopoll_start = ktime_get_ns();
  750. }
  751. } else {
  752. if (kiocb->ki_flags & IOCB_HIPRI)
  753. return -EINVAL;
  754. }
  755. if (req->flags & REQ_F_HAS_METADATA) {
  756. struct io_async_rw *io = req->async_data;
  757. if (!(file->f_mode & FMODE_HAS_METADATA))
  758. return -EINVAL;
  759. /*
  760. * We have a union of meta fields with wpq used for buffered-io
  761. * in io_async_rw, so fail it here.
  762. */
  763. if (!(req->file->f_flags & O_DIRECT))
  764. return -EOPNOTSUPP;
  765. kiocb->ki_flags |= IOCB_HAS_METADATA;
  766. kiocb->private = &io->meta;
  767. }
  768. return 0;
  769. }
  770. static int __io_read(struct io_kiocb *req, struct io_br_sel *sel,
  771. unsigned int issue_flags)
  772. {
  773. bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
  774. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  775. struct io_async_rw *io = req->async_data;
  776. struct kiocb *kiocb = &rw->kiocb;
  777. ssize_t ret;
  778. loff_t *ppos;
  779. if (req->flags & REQ_F_IMPORT_BUFFER) {
  780. ret = io_rw_import_reg_vec(req, io, ITER_DEST, issue_flags);
  781. if (unlikely(ret))
  782. return ret;
  783. } else if (io_do_buffer_select(req)) {
  784. ret = io_import_rw_buffer(ITER_DEST, req, io, sel, issue_flags);
  785. if (unlikely(ret < 0))
  786. return ret;
  787. }
  788. ret = io_rw_init_file(req, FMODE_READ, READ);
  789. if (unlikely(ret))
  790. return ret;
  791. req->cqe.res = iov_iter_count(&io->iter);
  792. if (force_nonblock) {
  793. /* If the file doesn't support async, just async punt */
  794. if (unlikely(!io_file_supports_nowait(req, EPOLLIN)))
  795. return -EAGAIN;
  796. kiocb->ki_flags |= IOCB_NOWAIT;
  797. } else {
  798. /* Ensure we clear previously set non-block flag */
  799. kiocb->ki_flags &= ~IOCB_NOWAIT;
  800. }
  801. ppos = io_kiocb_update_pos(req);
  802. ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
  803. if (unlikely(ret))
  804. return ret;
  805. ret = io_iter_do_read(rw, &io->iter);
  806. /*
  807. * Some file systems like to return -EOPNOTSUPP for an IOCB_NOWAIT
  808. * issue, even though they should be returning -EAGAIN. To be safe,
  809. * retry from blocking context for either.
  810. */
  811. if (ret == -EOPNOTSUPP && force_nonblock)
  812. ret = -EAGAIN;
  813. if (ret == -EAGAIN) {
  814. /* If we can poll, just do that. */
  815. if (io_file_can_poll(req))
  816. return -EAGAIN;
  817. /* IOPOLL retry should happen for io-wq threads */
  818. if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
  819. goto done;
  820. /* no retry on NONBLOCK nor RWF_NOWAIT */
  821. if (req->flags & REQ_F_NOWAIT)
  822. goto done;
  823. ret = 0;
  824. } else if (ret == -EIOCBQUEUED) {
  825. return IOU_ISSUE_SKIP_COMPLETE;
  826. } else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
  827. (req->flags & REQ_F_NOWAIT) || !need_complete_io(req) ||
  828. (issue_flags & IO_URING_F_MULTISHOT)) {
  829. /* read all, failed, already did sync or don't want to retry */
  830. goto done;
  831. }
  832. /*
  833. * Don't depend on the iter state matching what was consumed, or being
  834. * untouched in case of error. Restore it and we'll advance it
  835. * manually if we need to.
  836. */
  837. iov_iter_restore(&io->iter, &io->iter_state);
  838. io_meta_restore(io, kiocb);
  839. do {
  840. /*
  841. * We end up here because of a partial read, either from
  842. * above or inside this loop. Advance the iter by the bytes
  843. * that were consumed.
  844. */
  845. iov_iter_advance(&io->iter, ret);
  846. if (!iov_iter_count(&io->iter))
  847. break;
  848. io->bytes_done += ret;
  849. iov_iter_save_state(&io->iter, &io->iter_state);
  850. /* if we can retry, do so with the callbacks armed */
  851. if (!io_rw_should_retry(req)) {
  852. kiocb->ki_flags &= ~IOCB_WAITQ;
  853. return -EAGAIN;
  854. }
  855. req->cqe.res = iov_iter_count(&io->iter);
  856. /*
  857. * Now retry read with the IOCB_WAITQ parts set in the iocb. If
  858. * we get -EIOCBQUEUED, then we'll get a notification when the
  859. * desired page gets unlocked. We can also get a partial read
  860. * here, and if we do, then just retry at the new offset.
  861. */
  862. ret = io_iter_do_read(rw, &io->iter);
  863. if (ret == -EIOCBQUEUED)
  864. return IOU_ISSUE_SKIP_COMPLETE;
  865. /* we got some bytes, but not all. retry. */
  866. kiocb->ki_flags &= ~IOCB_WAITQ;
  867. iov_iter_restore(&io->iter, &io->iter_state);
  868. } while (ret > 0);
  869. done:
  870. /* it's faster to check here than delegate to kfree */
  871. return ret;
  872. }
  873. int io_read(struct io_kiocb *req, unsigned int issue_flags)
  874. {
  875. struct io_br_sel sel = { };
  876. int ret;
  877. ret = __io_read(req, &sel, issue_flags);
  878. if (ret >= 0)
  879. return kiocb_done(req, ret, &sel, issue_flags);
  880. if (req->flags & REQ_F_BUFFERS_COMMIT)
  881. io_kbuf_recycle(req, sel.buf_list, issue_flags);
  882. return ret;
  883. }
  884. int io_read_mshot(struct io_kiocb *req, unsigned int issue_flags)
  885. {
  886. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  887. struct io_br_sel sel = { };
  888. unsigned int cflags = 0;
  889. int ret;
  890. /*
  891. * Multishot MUST be used on a pollable file
  892. */
  893. if (!io_file_can_poll(req))
  894. return -EBADFD;
  895. /* make it sync, multishot doesn't support async execution */
  896. rw->kiocb.ki_complete = NULL;
  897. ret = __io_read(req, &sel, issue_flags);
  898. /*
  899. * If we get -EAGAIN, recycle our buffer and just let normal poll
  900. * handling arm it.
  901. */
  902. if (ret == -EAGAIN) {
  903. /*
  904. * Reset rw->len to 0 again to avoid clamping future mshot
  905. * reads, in case the buffer size varies.
  906. */
  907. if (io_kbuf_recycle(req, sel.buf_list, issue_flags))
  908. rw->len = 0;
  909. return IOU_RETRY;
  910. } else if (ret <= 0) {
  911. io_kbuf_recycle(req, sel.buf_list, issue_flags);
  912. if (ret < 0)
  913. req_set_fail(req);
  914. } else if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
  915. cflags = io_put_kbuf(req, ret, sel.buf_list);
  916. } else {
  917. /*
  918. * Any successful return value will keep the multishot read
  919. * armed, if it's still set. Put our buffer and post a CQE. If
  920. * we fail to post a CQE, or multishot is no longer set, then
  921. * jump to the termination path. This request is then done.
  922. */
  923. cflags = io_put_kbuf(req, ret, sel.buf_list);
  924. rw->len = 0; /* similarly to above, reset len to 0 */
  925. if (io_req_post_cqe(req, ret, cflags | IORING_CQE_F_MORE)) {
  926. if (issue_flags & IO_URING_F_MULTISHOT)
  927. /*
  928. * Force retry, as we might have more data to
  929. * be read and otherwise it won't get retried
  930. * until (if ever) another poll is triggered.
  931. */
  932. io_poll_multishot_retry(req);
  933. return IOU_RETRY;
  934. }
  935. }
  936. /*
  937. * Either an error, or we've hit overflow posting the CQE. For any
  938. * multishot request, hitting overflow will terminate it.
  939. */
  940. io_req_set_res(req, ret, cflags);
  941. io_req_rw_cleanup(req, issue_flags);
  942. return IOU_COMPLETE;
  943. }
  944. static bool io_kiocb_start_write(struct io_kiocb *req, struct kiocb *kiocb)
  945. {
  946. struct inode *inode;
  947. bool ret;
  948. if (!(req->flags & REQ_F_ISREG))
  949. return true;
  950. if (!(kiocb->ki_flags & IOCB_NOWAIT)) {
  951. kiocb_start_write(kiocb);
  952. return true;
  953. }
  954. inode = file_inode(kiocb->ki_filp);
  955. ret = sb_start_write_trylock(inode->i_sb);
  956. if (ret)
  957. __sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
  958. return ret;
  959. }
  960. int io_write(struct io_kiocb *req, unsigned int issue_flags)
  961. {
  962. bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
  963. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  964. struct io_async_rw *io = req->async_data;
  965. struct kiocb *kiocb = &rw->kiocb;
  966. ssize_t ret, ret2;
  967. loff_t *ppos;
  968. if (req->flags & REQ_F_IMPORT_BUFFER) {
  969. ret = io_rw_import_reg_vec(req, io, ITER_SOURCE, issue_flags);
  970. if (unlikely(ret))
  971. return ret;
  972. }
  973. ret = io_rw_init_file(req, FMODE_WRITE, WRITE);
  974. if (unlikely(ret))
  975. return ret;
  976. req->cqe.res = iov_iter_count(&io->iter);
  977. if (force_nonblock) {
  978. /* If the file doesn't support async, just async punt */
  979. if (unlikely(!io_file_supports_nowait(req, EPOLLOUT)))
  980. goto ret_eagain;
  981. /* Check if we can support NOWAIT. */
  982. if (!(kiocb->ki_flags & IOCB_DIRECT) &&
  983. !(req->file->f_op->fop_flags & FOP_BUFFER_WASYNC) &&
  984. (req->flags & REQ_F_ISREG))
  985. goto ret_eagain;
  986. kiocb->ki_flags |= IOCB_NOWAIT;
  987. } else {
  988. /* Ensure we clear previously set non-block flag */
  989. kiocb->ki_flags &= ~IOCB_NOWAIT;
  990. }
  991. ppos = io_kiocb_update_pos(req);
  992. ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
  993. if (unlikely(ret))
  994. return ret;
  995. if (unlikely(!io_kiocb_start_write(req, kiocb)))
  996. return -EAGAIN;
  997. kiocb->ki_flags |= IOCB_WRITE;
  998. if (likely(req->file->f_op->write_iter))
  999. ret2 = req->file->f_op->write_iter(kiocb, &io->iter);
  1000. else if (req->file->f_op->write)
  1001. ret2 = loop_rw_iter(WRITE, rw, &io->iter);
  1002. else
  1003. ret2 = -EINVAL;
  1004. /*
  1005. * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
  1006. * retry them without IOCB_NOWAIT.
  1007. */
  1008. if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
  1009. ret2 = -EAGAIN;
  1010. /* no retry on NONBLOCK nor RWF_NOWAIT */
  1011. if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
  1012. goto done;
  1013. if (!force_nonblock || ret2 != -EAGAIN) {
  1014. /* IOPOLL retry should happen for io-wq threads */
  1015. if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
  1016. goto ret_eagain;
  1017. if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
  1018. trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
  1019. req->cqe.res, ret2);
  1020. /* This is a partial write. The file pos has already been
  1021. * updated, setup the async struct to complete the request
  1022. * in the worker. Also update bytes_done to account for
  1023. * the bytes already written.
  1024. */
  1025. iov_iter_save_state(&io->iter, &io->iter_state);
  1026. io->bytes_done += ret2;
  1027. if (kiocb->ki_flags & IOCB_WRITE)
  1028. io_req_end_write(req);
  1029. return -EAGAIN;
  1030. }
  1031. done:
  1032. return kiocb_done(req, ret2, NULL, issue_flags);
  1033. } else {
  1034. ret_eagain:
  1035. iov_iter_restore(&io->iter, &io->iter_state);
  1036. io_meta_restore(io, kiocb);
  1037. if (kiocb->ki_flags & IOCB_WRITE)
  1038. io_req_end_write(req);
  1039. return -EAGAIN;
  1040. }
  1041. }
  1042. int io_read_fixed(struct io_kiocb *req, unsigned int issue_flags)
  1043. {
  1044. int ret;
  1045. ret = io_init_rw_fixed(req, issue_flags, ITER_DEST);
  1046. if (unlikely(ret))
  1047. return ret;
  1048. return io_read(req, issue_flags);
  1049. }
  1050. int io_write_fixed(struct io_kiocb *req, unsigned int issue_flags)
  1051. {
  1052. int ret;
  1053. ret = io_init_rw_fixed(req, issue_flags, ITER_SOURCE);
  1054. if (unlikely(ret))
  1055. return ret;
  1056. return io_write(req, issue_flags);
  1057. }
  1058. void io_rw_fail(struct io_kiocb *req)
  1059. {
  1060. int res;
  1061. res = io_fixup_rw_res(req, req->cqe.res);
  1062. io_req_set_res(req, res, req->cqe.flags);
  1063. }
  1064. static int io_uring_classic_poll(struct io_kiocb *req, struct io_comp_batch *iob,
  1065. unsigned int poll_flags)
  1066. {
  1067. struct file *file = req->file;
  1068. if (io_is_uring_cmd(req)) {
  1069. struct io_uring_cmd *ioucmd;
  1070. ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
  1071. return file->f_op->uring_cmd_iopoll(ioucmd, iob, poll_flags);
  1072. } else {
  1073. struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
  1074. return file->f_op->iopoll(&rw->kiocb, iob, poll_flags);
  1075. }
  1076. }
  1077. static u64 io_hybrid_iopoll_delay(struct io_ring_ctx *ctx, struct io_kiocb *req)
  1078. {
  1079. struct hrtimer_sleeper timer;
  1080. enum hrtimer_mode mode;
  1081. ktime_t kt;
  1082. u64 sleep_time;
  1083. if (req->flags & REQ_F_IOPOLL_STATE)
  1084. return 0;
  1085. if (ctx->hybrid_poll_time == LLONG_MAX)
  1086. return 0;
  1087. /* Using half the running time to do schedule */
  1088. sleep_time = ctx->hybrid_poll_time / 2;
  1089. kt = ktime_set(0, sleep_time);
  1090. req->flags |= REQ_F_IOPOLL_STATE;
  1091. mode = HRTIMER_MODE_REL;
  1092. hrtimer_setup_sleeper_on_stack(&timer, CLOCK_MONOTONIC, mode);
  1093. hrtimer_set_expires(&timer.timer, kt);
  1094. set_current_state(TASK_INTERRUPTIBLE);
  1095. hrtimer_sleeper_start_expires(&timer, mode);
  1096. if (timer.task)
  1097. io_schedule();
  1098. hrtimer_cancel(&timer.timer);
  1099. __set_current_state(TASK_RUNNING);
  1100. destroy_hrtimer_on_stack(&timer.timer);
  1101. return sleep_time;
  1102. }
  1103. static int io_uring_hybrid_poll(struct io_kiocb *req,
  1104. struct io_comp_batch *iob, unsigned int poll_flags)
  1105. {
  1106. struct io_ring_ctx *ctx = req->ctx;
  1107. u64 runtime, sleep_time, iopoll_start;
  1108. int ret;
  1109. iopoll_start = READ_ONCE(req->iopoll_start);
  1110. sleep_time = io_hybrid_iopoll_delay(ctx, req);
  1111. ret = io_uring_classic_poll(req, iob, poll_flags);
  1112. runtime = ktime_get_ns() - iopoll_start - sleep_time;
  1113. /*
  1114. * Use minimum sleep time if we're polling devices with different
  1115. * latencies. We could get more completions from the faster ones.
  1116. */
  1117. if (ctx->hybrid_poll_time > runtime)
  1118. ctx->hybrid_poll_time = runtime;
  1119. return ret;
  1120. }
  1121. int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
  1122. {
  1123. unsigned int poll_flags = 0;
  1124. DEFINE_IO_COMP_BATCH(iob);
  1125. struct io_kiocb *req, *tmp;
  1126. int nr_events = 0;
  1127. /*
  1128. * Store the polling io_ring_ctx so drivers can detect if they're
  1129. * completing a request in the same ring context that's polling.
  1130. */
  1131. iob.poll_ctx = ctx;
  1132. /*
  1133. * Only spin for completions if we don't have multiple devices hanging
  1134. * off our complete list.
  1135. */
  1136. if (ctx->poll_multi_queue || force_nonspin)
  1137. poll_flags |= BLK_POLL_ONESHOT;
  1138. list_for_each_entry(req, &ctx->iopoll_list, iopoll_node) {
  1139. int ret;
  1140. /*
  1141. * Move completed and retryable entries to our local lists.
  1142. * If we find a request that requires polling, break out
  1143. * and complete those lists first, if we have entries there.
  1144. */
  1145. if (READ_ONCE(req->iopoll_completed))
  1146. break;
  1147. if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL)
  1148. ret = io_uring_hybrid_poll(req, &iob, poll_flags);
  1149. else
  1150. ret = io_uring_classic_poll(req, &iob, poll_flags);
  1151. if (unlikely(ret < 0))
  1152. return ret;
  1153. else if (ret)
  1154. poll_flags |= BLK_POLL_ONESHOT;
  1155. /* iopoll may have completed current req */
  1156. if (!rq_list_empty(&iob.req_list) ||
  1157. READ_ONCE(req->iopoll_completed))
  1158. break;
  1159. }
  1160. if (!rq_list_empty(&iob.req_list))
  1161. iob.complete(&iob);
  1162. list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, iopoll_node) {
  1163. /* order with io_complete_rw_iopoll(), e.g. ->result updates */
  1164. if (!smp_load_acquire(&req->iopoll_completed))
  1165. continue;
  1166. list_del(&req->iopoll_node);
  1167. wq_list_add_tail(&req->comp_list, &ctx->submit_state.compl_reqs);
  1168. nr_events++;
  1169. req->cqe.flags = io_put_kbuf(req, req->cqe.res, NULL);
  1170. if (!io_is_uring_cmd(req))
  1171. io_req_rw_cleanup(req, 0);
  1172. }
  1173. if (nr_events)
  1174. __io_submit_flush_completions(ctx);
  1175. return nr_events;
  1176. }
  1177. void io_rw_cache_free(const void *entry)
  1178. {
  1179. struct io_async_rw *rw = (struct io_async_rw *) entry;
  1180. io_vec_free(&rw->vec);
  1181. kfree(rw);
  1182. }