rw.c 31 KB

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