kbuf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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/namei.h>
  9. #include <linux/poll.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/io_uring.h>
  12. #include <uapi/linux/io_uring.h>
  13. #include "io_uring.h"
  14. #include "opdef.h"
  15. #include "kbuf.h"
  16. #include "memmap.h"
  17. /* BIDs are addressed by a 16-bit field in a CQE */
  18. #define MAX_BIDS_PER_BGID (1 << 16)
  19. struct kmem_cache *io_buf_cachep;
  20. struct io_provide_buf {
  21. struct file *file;
  22. __u64 addr;
  23. __u32 len;
  24. __u32 bgid;
  25. __u32 nbufs;
  26. __u16 bid;
  27. };
  28. static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
  29. unsigned int bgid)
  30. {
  31. lockdep_assert_held(&ctx->uring_lock);
  32. return xa_load(&ctx->io_bl_xa, bgid);
  33. }
  34. static int io_buffer_add_list(struct io_ring_ctx *ctx,
  35. struct io_buffer_list *bl, unsigned int bgid)
  36. {
  37. /*
  38. * Store buffer group ID and finally mark the list as visible.
  39. * The normal lookup doesn't care about the visibility as we're
  40. * always under the ->uring_lock, but the RCU lookup from mmap does.
  41. */
  42. bl->bgid = bgid;
  43. atomic_set(&bl->refs, 1);
  44. return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
  45. }
  46. bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
  47. {
  48. struct io_ring_ctx *ctx = req->ctx;
  49. struct io_buffer_list *bl;
  50. struct io_buffer *buf;
  51. io_ring_submit_lock(ctx, issue_flags);
  52. buf = req->kbuf;
  53. bl = io_buffer_get_list(ctx, buf->bgid);
  54. list_add(&buf->list, &bl->buf_list);
  55. req->flags &= ~REQ_F_BUFFER_SELECTED;
  56. req->buf_index = buf->bgid;
  57. io_ring_submit_unlock(ctx, issue_flags);
  58. return true;
  59. }
  60. void __io_put_kbuf(struct io_kiocb *req, int len, unsigned issue_flags)
  61. {
  62. /*
  63. * We can add this buffer back to two lists:
  64. *
  65. * 1) The io_buffers_cache list. This one is protected by the
  66. * ctx->uring_lock. If we already hold this lock, add back to this
  67. * list as we can grab it from issue as well.
  68. * 2) The io_buffers_comp list. This one is protected by the
  69. * ctx->completion_lock.
  70. *
  71. * We migrate buffers from the comp_list to the issue cache list
  72. * when we need one.
  73. */
  74. if (issue_flags & IO_URING_F_UNLOCKED) {
  75. struct io_ring_ctx *ctx = req->ctx;
  76. spin_lock(&ctx->completion_lock);
  77. __io_put_kbuf_list(req, len, &ctx->io_buffers_comp);
  78. spin_unlock(&ctx->completion_lock);
  79. } else {
  80. lockdep_assert_held(&req->ctx->uring_lock);
  81. __io_put_kbuf_list(req, len, &req->ctx->io_buffers_cache);
  82. }
  83. }
  84. static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
  85. struct io_buffer_list *bl)
  86. {
  87. if (!list_empty(&bl->buf_list)) {
  88. struct io_buffer *kbuf;
  89. kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
  90. list_del(&kbuf->list);
  91. if (*len == 0 || *len > kbuf->len)
  92. *len = kbuf->len;
  93. if (list_empty(&bl->buf_list))
  94. req->flags |= REQ_F_BL_EMPTY;
  95. req->flags |= REQ_F_BUFFER_SELECTED;
  96. req->kbuf = kbuf;
  97. req->buf_index = kbuf->bid;
  98. return u64_to_user_ptr(kbuf->addr);
  99. }
  100. return NULL;
  101. }
  102. static int io_provided_buffers_select(struct io_kiocb *req, size_t *len,
  103. struct io_buffer_list *bl,
  104. struct iovec *iov)
  105. {
  106. void __user *buf;
  107. buf = io_provided_buffer_select(req, len, bl);
  108. if (unlikely(!buf))
  109. return -ENOBUFS;
  110. iov[0].iov_base = buf;
  111. iov[0].iov_len = *len;
  112. return 1;
  113. }
  114. static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
  115. struct io_buffer_list *bl,
  116. unsigned int issue_flags)
  117. {
  118. struct io_uring_buf_ring *br = bl->buf_ring;
  119. __u16 tail, head = bl->head;
  120. struct io_uring_buf *buf;
  121. void __user *ret;
  122. tail = smp_load_acquire(&br->tail);
  123. if (unlikely(tail == head))
  124. return NULL;
  125. if (head + 1 == tail)
  126. req->flags |= REQ_F_BL_EMPTY;
  127. buf = io_ring_head_to_buf(br, head, bl->mask);
  128. if (*len == 0 || *len > buf->len)
  129. *len = buf->len;
  130. req->flags |= REQ_F_BUFFER_RING | REQ_F_BUFFERS_COMMIT;
  131. req->buf_list = bl;
  132. req->buf_index = buf->bid;
  133. ret = u64_to_user_ptr(buf->addr);
  134. if (issue_flags & IO_URING_F_UNLOCKED || !io_file_can_poll(req)) {
  135. /*
  136. * If we came in unlocked, we have no choice but to consume the
  137. * buffer here, otherwise nothing ensures that the buffer won't
  138. * get used by others. This does mean it'll be pinned until the
  139. * IO completes, coming in unlocked means we're being called from
  140. * io-wq context and there may be further retries in async hybrid
  141. * mode. For the locked case, the caller must call commit when
  142. * the transfer completes (or if we get -EAGAIN and must poll of
  143. * retry).
  144. */
  145. io_kbuf_commit(req, bl, *len, 1);
  146. req->buf_list = NULL;
  147. }
  148. return ret;
  149. }
  150. void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
  151. unsigned int issue_flags)
  152. {
  153. struct io_ring_ctx *ctx = req->ctx;
  154. struct io_buffer_list *bl;
  155. void __user *ret = NULL;
  156. io_ring_submit_lock(req->ctx, issue_flags);
  157. bl = io_buffer_get_list(ctx, req->buf_index);
  158. if (likely(bl)) {
  159. if (bl->flags & IOBL_BUF_RING)
  160. ret = io_ring_buffer_select(req, len, bl, issue_flags);
  161. else
  162. ret = io_provided_buffer_select(req, len, bl);
  163. }
  164. io_ring_submit_unlock(req->ctx, issue_flags);
  165. return ret;
  166. }
  167. /* cap it at a reasonable 256, will be one page even for 4K */
  168. #define PEEK_MAX_IMPORT 256
  169. static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
  170. struct io_buffer_list *bl)
  171. {
  172. struct io_uring_buf_ring *br = bl->buf_ring;
  173. struct iovec *iov = arg->iovs;
  174. int nr_iovs = arg->nr_iovs;
  175. __u16 nr_avail, tail, head;
  176. struct io_uring_buf *buf;
  177. tail = smp_load_acquire(&br->tail);
  178. head = bl->head;
  179. nr_avail = min_t(__u16, tail - head, UIO_MAXIOV);
  180. if (unlikely(!nr_avail))
  181. return -ENOBUFS;
  182. buf = io_ring_head_to_buf(br, head, bl->mask);
  183. if (arg->max_len) {
  184. u32 len = READ_ONCE(buf->len);
  185. if (unlikely(!len))
  186. return -ENOBUFS;
  187. /*
  188. * Limit incremental buffers to 1 segment. No point trying
  189. * to peek ahead and map more than we need, when the buffers
  190. * themselves should be large when setup with
  191. * IOU_PBUF_RING_INC.
  192. */
  193. if (bl->flags & IOBL_INC) {
  194. nr_avail = 1;
  195. } else {
  196. size_t needed;
  197. needed = (arg->max_len + len - 1) / len;
  198. needed = min_not_zero(needed, (size_t) PEEK_MAX_IMPORT);
  199. if (nr_avail > needed)
  200. nr_avail = needed;
  201. }
  202. }
  203. /*
  204. * only alloc a bigger array if we know we have data to map, eg not
  205. * a speculative peek operation.
  206. */
  207. if (arg->mode & KBUF_MODE_EXPAND && nr_avail > nr_iovs && arg->max_len) {
  208. iov = kmalloc_array(nr_avail, sizeof(struct iovec), GFP_KERNEL);
  209. if (unlikely(!iov))
  210. return -ENOMEM;
  211. if (arg->mode & KBUF_MODE_FREE)
  212. kfree(arg->iovs);
  213. arg->iovs = iov;
  214. nr_iovs = nr_avail;
  215. } else if (nr_avail < nr_iovs) {
  216. nr_iovs = nr_avail;
  217. }
  218. /* set it to max, if not set, so we can use it unconditionally */
  219. if (!arg->max_len)
  220. arg->max_len = INT_MAX;
  221. req->buf_index = buf->bid;
  222. do {
  223. u32 len = buf->len;
  224. /* truncate end piece, if needed, for non partial buffers */
  225. if (len > arg->max_len) {
  226. len = arg->max_len;
  227. if (!(bl->flags & IOBL_INC)) {
  228. arg->partial_map = 1;
  229. if (iov != arg->iovs)
  230. break;
  231. buf->len = len;
  232. }
  233. }
  234. iov->iov_base = u64_to_user_ptr(buf->addr);
  235. iov->iov_len = len;
  236. iov++;
  237. arg->out_len += len;
  238. arg->max_len -= len;
  239. if (!arg->max_len)
  240. break;
  241. buf = io_ring_head_to_buf(br, ++head, bl->mask);
  242. } while (--nr_iovs);
  243. if (head == tail)
  244. req->flags |= REQ_F_BL_EMPTY;
  245. req->flags |= REQ_F_BUFFER_RING;
  246. req->buf_list = bl;
  247. return iov - arg->iovs;
  248. }
  249. int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
  250. unsigned int issue_flags)
  251. {
  252. struct io_ring_ctx *ctx = req->ctx;
  253. struct io_buffer_list *bl;
  254. int ret = -ENOENT;
  255. io_ring_submit_lock(ctx, issue_flags);
  256. bl = io_buffer_get_list(ctx, req->buf_index);
  257. if (unlikely(!bl))
  258. goto out_unlock;
  259. if (bl->flags & IOBL_BUF_RING) {
  260. ret = io_ring_buffers_peek(req, arg, bl);
  261. /*
  262. * Don't recycle these buffers if we need to go through poll.
  263. * Nobody else can use them anyway, and holding on to provided
  264. * buffers for a send/write operation would happen on the app
  265. * side anyway with normal buffers. Besides, we already
  266. * committed them, they cannot be put back in the queue.
  267. */
  268. if (ret > 0) {
  269. req->flags |= REQ_F_BUFFERS_COMMIT | REQ_F_BL_NO_RECYCLE;
  270. io_kbuf_commit(req, bl, arg->out_len, ret);
  271. }
  272. } else {
  273. ret = io_provided_buffers_select(req, &arg->out_len, bl, arg->iovs);
  274. }
  275. out_unlock:
  276. io_ring_submit_unlock(ctx, issue_flags);
  277. return ret;
  278. }
  279. int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg)
  280. {
  281. struct io_ring_ctx *ctx = req->ctx;
  282. struct io_buffer_list *bl;
  283. int ret;
  284. lockdep_assert_held(&ctx->uring_lock);
  285. bl = io_buffer_get_list(ctx, req->buf_index);
  286. if (unlikely(!bl))
  287. return -ENOENT;
  288. if (bl->flags & IOBL_BUF_RING) {
  289. ret = io_ring_buffers_peek(req, arg, bl);
  290. if (ret > 0)
  291. req->flags |= REQ_F_BUFFERS_COMMIT;
  292. return ret;
  293. }
  294. /* don't support multiple buffer selections for legacy */
  295. return io_provided_buffers_select(req, &arg->max_len, bl, arg->iovs);
  296. }
  297. static int __io_remove_buffers(struct io_ring_ctx *ctx,
  298. struct io_buffer_list *bl, unsigned nbufs)
  299. {
  300. unsigned i = 0;
  301. /* shouldn't happen */
  302. if (!nbufs)
  303. return 0;
  304. if (bl->flags & IOBL_BUF_RING) {
  305. i = bl->buf_ring->tail - bl->head;
  306. if (bl->buf_nr_pages) {
  307. int j;
  308. if (!(bl->flags & IOBL_MMAP)) {
  309. for (j = 0; j < bl->buf_nr_pages; j++)
  310. unpin_user_page(bl->buf_pages[j]);
  311. }
  312. io_pages_unmap(bl->buf_ring, &bl->buf_pages,
  313. &bl->buf_nr_pages, bl->flags & IOBL_MMAP);
  314. bl->flags &= ~IOBL_MMAP;
  315. }
  316. /* make sure it's seen as empty */
  317. INIT_LIST_HEAD(&bl->buf_list);
  318. bl->flags &= ~IOBL_BUF_RING;
  319. return i;
  320. }
  321. /* protects io_buffers_cache */
  322. lockdep_assert_held(&ctx->uring_lock);
  323. while (!list_empty(&bl->buf_list)) {
  324. struct io_buffer *nxt;
  325. nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
  326. list_move(&nxt->list, &ctx->io_buffers_cache);
  327. if (++i == nbufs)
  328. return i;
  329. cond_resched();
  330. }
  331. return i;
  332. }
  333. void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
  334. {
  335. if (atomic_dec_and_test(&bl->refs)) {
  336. __io_remove_buffers(ctx, bl, -1U);
  337. kfree_rcu(bl, rcu);
  338. }
  339. }
  340. void io_destroy_buffers(struct io_ring_ctx *ctx)
  341. {
  342. struct io_buffer_list *bl;
  343. struct list_head *item, *tmp;
  344. struct io_buffer *buf;
  345. unsigned long index;
  346. xa_for_each(&ctx->io_bl_xa, index, bl) {
  347. xa_erase(&ctx->io_bl_xa, bl->bgid);
  348. io_put_bl(ctx, bl);
  349. }
  350. /*
  351. * Move deferred locked entries to cache before pruning
  352. */
  353. spin_lock(&ctx->completion_lock);
  354. if (!list_empty(&ctx->io_buffers_comp))
  355. list_splice_init(&ctx->io_buffers_comp, &ctx->io_buffers_cache);
  356. spin_unlock(&ctx->completion_lock);
  357. list_for_each_safe(item, tmp, &ctx->io_buffers_cache) {
  358. buf = list_entry(item, struct io_buffer, list);
  359. kmem_cache_free(io_buf_cachep, buf);
  360. }
  361. }
  362. static void io_destroy_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
  363. {
  364. xa_erase(&ctx->io_bl_xa, bl->bgid);
  365. io_put_bl(ctx, bl);
  366. }
  367. int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  368. {
  369. struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
  370. u64 tmp;
  371. if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
  372. sqe->splice_fd_in)
  373. return -EINVAL;
  374. tmp = READ_ONCE(sqe->fd);
  375. if (!tmp || tmp > MAX_BIDS_PER_BGID)
  376. return -EINVAL;
  377. memset(p, 0, sizeof(*p));
  378. p->nbufs = tmp;
  379. p->bgid = READ_ONCE(sqe->buf_group);
  380. return 0;
  381. }
  382. int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
  383. {
  384. struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
  385. struct io_ring_ctx *ctx = req->ctx;
  386. struct io_buffer_list *bl;
  387. int ret = 0;
  388. io_ring_submit_lock(ctx, issue_flags);
  389. ret = -ENOENT;
  390. bl = io_buffer_get_list(ctx, p->bgid);
  391. if (bl) {
  392. ret = -EINVAL;
  393. /* can't use provide/remove buffers command on mapped buffers */
  394. if (!(bl->flags & IOBL_BUF_RING))
  395. ret = __io_remove_buffers(ctx, bl, p->nbufs);
  396. }
  397. io_ring_submit_unlock(ctx, issue_flags);
  398. if (ret < 0)
  399. req_set_fail(req);
  400. io_req_set_res(req, ret, 0);
  401. return IOU_OK;
  402. }
  403. int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  404. {
  405. unsigned long size, tmp_check;
  406. struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
  407. u64 tmp;
  408. if (sqe->rw_flags || sqe->splice_fd_in)
  409. return -EINVAL;
  410. tmp = READ_ONCE(sqe->fd);
  411. if (!tmp || tmp > MAX_BIDS_PER_BGID)
  412. return -E2BIG;
  413. p->nbufs = tmp;
  414. p->addr = READ_ONCE(sqe->addr);
  415. p->len = READ_ONCE(sqe->len);
  416. if (!p->len)
  417. return -EINVAL;
  418. if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
  419. &size))
  420. return -EOVERFLOW;
  421. if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
  422. return -EOVERFLOW;
  423. size = (unsigned long)p->len * p->nbufs;
  424. if (!access_ok(u64_to_user_ptr(p->addr), size))
  425. return -EFAULT;
  426. p->bgid = READ_ONCE(sqe->buf_group);
  427. tmp = READ_ONCE(sqe->off);
  428. if (tmp > USHRT_MAX)
  429. return -E2BIG;
  430. if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
  431. return -EINVAL;
  432. p->bid = tmp;
  433. return 0;
  434. }
  435. #define IO_BUFFER_ALLOC_BATCH 64
  436. static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
  437. {
  438. struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
  439. int allocated;
  440. /*
  441. * Completions that don't happen inline (eg not under uring_lock) will
  442. * add to ->io_buffers_comp. If we don't have any free buffers, check
  443. * the completion list and splice those entries first.
  444. */
  445. if (!list_empty_careful(&ctx->io_buffers_comp)) {
  446. spin_lock(&ctx->completion_lock);
  447. if (!list_empty(&ctx->io_buffers_comp)) {
  448. list_splice_init(&ctx->io_buffers_comp,
  449. &ctx->io_buffers_cache);
  450. spin_unlock(&ctx->completion_lock);
  451. return 0;
  452. }
  453. spin_unlock(&ctx->completion_lock);
  454. }
  455. /*
  456. * No free buffers and no completion entries either. Allocate a new
  457. * batch of buffer entries and add those to our freelist.
  458. */
  459. allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
  460. ARRAY_SIZE(bufs), (void **) bufs);
  461. if (unlikely(!allocated)) {
  462. /*
  463. * Bulk alloc is all-or-nothing. If we fail to get a batch,
  464. * retry single alloc to be on the safe side.
  465. */
  466. bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
  467. if (!bufs[0])
  468. return -ENOMEM;
  469. allocated = 1;
  470. }
  471. while (allocated)
  472. list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
  473. return 0;
  474. }
  475. static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
  476. struct io_buffer_list *bl)
  477. {
  478. struct io_buffer *buf;
  479. u64 addr = pbuf->addr;
  480. int i, bid = pbuf->bid;
  481. for (i = 0; i < pbuf->nbufs; i++) {
  482. if (list_empty(&ctx->io_buffers_cache) &&
  483. io_refill_buffer_cache(ctx))
  484. break;
  485. buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
  486. list);
  487. list_move_tail(&buf->list, &bl->buf_list);
  488. buf->addr = addr;
  489. buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
  490. buf->bid = bid;
  491. buf->bgid = pbuf->bgid;
  492. addr += pbuf->len;
  493. bid++;
  494. cond_resched();
  495. }
  496. return i ? 0 : -ENOMEM;
  497. }
  498. int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
  499. {
  500. struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
  501. struct io_ring_ctx *ctx = req->ctx;
  502. struct io_buffer_list *bl;
  503. int ret = 0;
  504. io_ring_submit_lock(ctx, issue_flags);
  505. bl = io_buffer_get_list(ctx, p->bgid);
  506. if (unlikely(!bl)) {
  507. bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
  508. if (!bl) {
  509. ret = -ENOMEM;
  510. goto err;
  511. }
  512. INIT_LIST_HEAD(&bl->buf_list);
  513. ret = io_buffer_add_list(ctx, bl, p->bgid);
  514. if (ret) {
  515. /*
  516. * Doesn't need rcu free as it was never visible, but
  517. * let's keep it consistent throughout.
  518. */
  519. kfree_rcu(bl, rcu);
  520. goto err;
  521. }
  522. }
  523. /* can't add buffers via this command for a mapped buffer ring */
  524. if (bl->flags & IOBL_BUF_RING) {
  525. ret = -EINVAL;
  526. goto err;
  527. }
  528. ret = io_add_buffers(ctx, p, bl);
  529. err:
  530. io_ring_submit_unlock(ctx, issue_flags);
  531. if (ret < 0)
  532. req_set_fail(req);
  533. io_req_set_res(req, ret, 0);
  534. return IOU_OK;
  535. }
  536. static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
  537. struct io_buffer_list *bl)
  538. {
  539. struct io_uring_buf_ring *br = NULL;
  540. struct page **pages;
  541. int nr_pages, ret;
  542. pages = io_pin_pages(reg->ring_addr,
  543. flex_array_size(br, bufs, reg->ring_entries),
  544. &nr_pages);
  545. if (IS_ERR(pages))
  546. return PTR_ERR(pages);
  547. br = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
  548. if (!br) {
  549. ret = -ENOMEM;
  550. goto error_unpin;
  551. }
  552. #ifdef SHM_COLOUR
  553. /*
  554. * On platforms that have specific aliasing requirements, SHM_COLOUR
  555. * is set and we must guarantee that the kernel and user side align
  556. * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
  557. * the application mmap's the provided ring buffer. Fail the request
  558. * if we, by chance, don't end up with aligned addresses. The app
  559. * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
  560. * this transparently.
  561. */
  562. if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1)) {
  563. ret = -EINVAL;
  564. goto error_unpin;
  565. }
  566. #endif
  567. bl->buf_pages = pages;
  568. bl->buf_nr_pages = nr_pages;
  569. bl->buf_ring = br;
  570. bl->flags |= IOBL_BUF_RING;
  571. bl->flags &= ~IOBL_MMAP;
  572. return 0;
  573. error_unpin:
  574. unpin_user_pages(pages, nr_pages);
  575. kvfree(pages);
  576. vunmap(br);
  577. return ret;
  578. }
  579. static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
  580. struct io_uring_buf_reg *reg,
  581. struct io_buffer_list *bl)
  582. {
  583. size_t ring_size;
  584. ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
  585. bl->buf_ring = io_pages_map(&bl->buf_pages, &bl->buf_nr_pages, ring_size);
  586. if (IS_ERR(bl->buf_ring)) {
  587. bl->buf_ring = NULL;
  588. return -ENOMEM;
  589. }
  590. bl->flags |= (IOBL_BUF_RING | IOBL_MMAP);
  591. return 0;
  592. }
  593. int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
  594. {
  595. struct io_uring_buf_reg reg;
  596. struct io_buffer_list *bl, *free_bl = NULL;
  597. int ret;
  598. lockdep_assert_held(&ctx->uring_lock);
  599. if (copy_from_user(&reg, arg, sizeof(reg)))
  600. return -EFAULT;
  601. if (reg.resv[0] || reg.resv[1] || reg.resv[2])
  602. return -EINVAL;
  603. if (reg.flags & ~(IOU_PBUF_RING_MMAP | IOU_PBUF_RING_INC))
  604. return -EINVAL;
  605. if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
  606. if (!reg.ring_addr)
  607. return -EFAULT;
  608. if (reg.ring_addr & ~PAGE_MASK)
  609. return -EINVAL;
  610. } else {
  611. if (reg.ring_addr)
  612. return -EINVAL;
  613. }
  614. if (!is_power_of_2(reg.ring_entries))
  615. return -EINVAL;
  616. /* cannot disambiguate full vs empty due to head/tail size */
  617. if (reg.ring_entries >= 65536)
  618. return -EINVAL;
  619. bl = io_buffer_get_list(ctx, reg.bgid);
  620. if (bl) {
  621. /* if mapped buffer ring OR classic exists, don't allow */
  622. if (bl->flags & IOBL_BUF_RING || !list_empty(&bl->buf_list))
  623. return -EEXIST;
  624. io_destroy_bl(ctx, bl);
  625. }
  626. free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
  627. if (!bl)
  628. return -ENOMEM;
  629. if (!(reg.flags & IOU_PBUF_RING_MMAP))
  630. ret = io_pin_pbuf_ring(&reg, bl);
  631. else
  632. ret = io_alloc_pbuf_ring(ctx, &reg, bl);
  633. if (!ret) {
  634. bl->nr_entries = reg.ring_entries;
  635. bl->mask = reg.ring_entries - 1;
  636. if (reg.flags & IOU_PBUF_RING_INC)
  637. bl->flags |= IOBL_INC;
  638. io_buffer_add_list(ctx, bl, reg.bgid);
  639. return 0;
  640. }
  641. kfree_rcu(free_bl, rcu);
  642. return ret;
  643. }
  644. int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
  645. {
  646. struct io_uring_buf_reg reg;
  647. struct io_buffer_list *bl;
  648. lockdep_assert_held(&ctx->uring_lock);
  649. if (copy_from_user(&reg, arg, sizeof(reg)))
  650. return -EFAULT;
  651. if (reg.resv[0] || reg.resv[1] || reg.resv[2])
  652. return -EINVAL;
  653. if (reg.flags)
  654. return -EINVAL;
  655. bl = io_buffer_get_list(ctx, reg.bgid);
  656. if (!bl)
  657. return -ENOENT;
  658. if (!(bl->flags & IOBL_BUF_RING))
  659. return -EINVAL;
  660. xa_erase(&ctx->io_bl_xa, bl->bgid);
  661. io_put_bl(ctx, bl);
  662. return 0;
  663. }
  664. int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
  665. {
  666. struct io_uring_buf_status buf_status;
  667. struct io_buffer_list *bl;
  668. int i;
  669. if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
  670. return -EFAULT;
  671. for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
  672. if (buf_status.resv[i])
  673. return -EINVAL;
  674. bl = io_buffer_get_list(ctx, buf_status.buf_group);
  675. if (!bl)
  676. return -ENOENT;
  677. if (!(bl->flags & IOBL_BUF_RING))
  678. return -EINVAL;
  679. buf_status.head = bl->head;
  680. if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
  681. return -EFAULT;
  682. return 0;
  683. }
  684. struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
  685. unsigned long bgid)
  686. {
  687. struct io_buffer_list *bl;
  688. bool ret;
  689. /*
  690. * We have to be a bit careful here - we're inside mmap and cannot grab
  691. * the uring_lock. This means the buffer_list could be simultaneously
  692. * going away, if someone is trying to be sneaky. Look it up under rcu
  693. * so we know it's not going away, and attempt to grab a reference to
  694. * it. If the ref is already zero, then fail the mapping. If successful,
  695. * the caller will call io_put_bl() to drop the the reference at at the
  696. * end. This may then safely free the buffer_list (and drop the pages)
  697. * at that point, vm_insert_pages() would've already grabbed the
  698. * necessary vma references.
  699. */
  700. rcu_read_lock();
  701. bl = xa_load(&ctx->io_bl_xa, bgid);
  702. /* must be a mmap'able buffer ring and have pages */
  703. ret = false;
  704. if (bl && bl->flags & IOBL_MMAP)
  705. ret = atomic_inc_not_zero(&bl->refs);
  706. rcu_read_unlock();
  707. if (ret)
  708. return bl;
  709. return ERR_PTR(-EINVAL);
  710. }
  711. int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma)
  712. {
  713. struct io_ring_ctx *ctx = file->private_data;
  714. loff_t pgoff = vma->vm_pgoff << PAGE_SHIFT;
  715. struct io_buffer_list *bl;
  716. int bgid, ret;
  717. bgid = (pgoff & ~IORING_OFF_MMAP_MASK) >> IORING_OFF_PBUF_SHIFT;
  718. bl = io_pbuf_get_bl(ctx, bgid);
  719. if (IS_ERR(bl))
  720. return PTR_ERR(bl);
  721. ret = io_uring_mmap_pages(ctx, vma, bl->buf_pages, bl->buf_nr_pages);
  722. io_put_bl(ctx, bl);
  723. return ret;
  724. }