rsrc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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/nospec.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/compat.h>
  11. #include <linux/io_uring.h>
  12. #include <uapi/linux/io_uring.h>
  13. #include "io_uring.h"
  14. #include "alloc_cache.h"
  15. #include "openclose.h"
  16. #include "rsrc.h"
  17. #include "memmap.h"
  18. #include "register.h"
  19. struct io_rsrc_update {
  20. struct file *file;
  21. u64 arg;
  22. u32 nr_args;
  23. u32 offset;
  24. };
  25. static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
  26. static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
  27. struct io_mapped_ubuf **pimu,
  28. struct page **last_hpage);
  29. /* only define max */
  30. #define IORING_MAX_FIXED_FILES (1U << 20)
  31. #define IORING_MAX_REG_BUFFERS (1U << 14)
  32. static const struct io_mapped_ubuf dummy_ubuf = {
  33. /* set invalid range, so io_import_fixed() fails meeting it */
  34. .ubuf = -1UL,
  35. .len = UINT_MAX,
  36. };
  37. int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
  38. {
  39. unsigned long page_limit, cur_pages, new_pages;
  40. if (!nr_pages)
  41. return 0;
  42. /* Don't allow more pages than we can safely lock */
  43. page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  44. cur_pages = atomic_long_read(&user->locked_vm);
  45. do {
  46. new_pages = cur_pages + nr_pages;
  47. if (new_pages > page_limit)
  48. return -ENOMEM;
  49. } while (!atomic_long_try_cmpxchg(&user->locked_vm,
  50. &cur_pages, new_pages));
  51. return 0;
  52. }
  53. static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
  54. {
  55. if (ctx->user)
  56. __io_unaccount_mem(ctx->user, nr_pages);
  57. if (ctx->mm_account)
  58. atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
  59. }
  60. static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
  61. {
  62. int ret;
  63. if (ctx->user) {
  64. ret = __io_account_mem(ctx->user, nr_pages);
  65. if (ret)
  66. return ret;
  67. }
  68. if (ctx->mm_account)
  69. atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
  70. return 0;
  71. }
  72. static int io_buffer_validate(struct iovec *iov)
  73. {
  74. unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
  75. /*
  76. * Don't impose further limits on the size and buffer
  77. * constraints here, we'll -EINVAL later when IO is
  78. * submitted if they are wrong.
  79. */
  80. if (!iov->iov_base)
  81. return iov->iov_len ? -EFAULT : 0;
  82. if (!iov->iov_len)
  83. return -EFAULT;
  84. /* arbitrary limit, but we need something */
  85. if (iov->iov_len > SZ_1G)
  86. return -EFAULT;
  87. if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
  88. return -EOVERFLOW;
  89. return 0;
  90. }
  91. static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
  92. {
  93. struct io_mapped_ubuf *imu = *slot;
  94. unsigned int i;
  95. *slot = NULL;
  96. if (imu != &dummy_ubuf) {
  97. if (!refcount_dec_and_test(&imu->refs))
  98. return;
  99. for (i = 0; i < imu->nr_bvecs; i++) {
  100. struct folio *folio = page_folio(imu->bvec[i].bv_page);
  101. unpin_user_folio(folio, 1);
  102. }
  103. if (imu->acct_pages)
  104. io_unaccount_mem(ctx, imu->acct_pages);
  105. kvfree(imu);
  106. }
  107. }
  108. static void io_rsrc_put_work(struct io_rsrc_node *node)
  109. {
  110. struct io_rsrc_put *prsrc = &node->item;
  111. if (prsrc->tag)
  112. io_post_aux_cqe(node->ctx, prsrc->tag, 0, 0);
  113. switch (node->type) {
  114. case IORING_RSRC_FILE:
  115. fput(prsrc->file);
  116. break;
  117. case IORING_RSRC_BUFFER:
  118. io_rsrc_buf_put(node->ctx, prsrc);
  119. break;
  120. default:
  121. WARN_ON_ONCE(1);
  122. break;
  123. }
  124. }
  125. void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
  126. {
  127. if (!io_alloc_cache_put(&ctx->rsrc_node_cache, node))
  128. kfree(node);
  129. }
  130. void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
  131. __must_hold(&node->ctx->uring_lock)
  132. {
  133. struct io_ring_ctx *ctx = node->ctx;
  134. while (!list_empty(&ctx->rsrc_ref_list)) {
  135. node = list_first_entry(&ctx->rsrc_ref_list,
  136. struct io_rsrc_node, node);
  137. /* recycle ref nodes in order */
  138. if (node->refs)
  139. break;
  140. list_del(&node->node);
  141. if (likely(!node->empty))
  142. io_rsrc_put_work(node);
  143. io_rsrc_node_destroy(ctx, node);
  144. }
  145. if (list_empty(&ctx->rsrc_ref_list) && unlikely(ctx->rsrc_quiesce))
  146. wake_up_all(&ctx->rsrc_quiesce_wq);
  147. }
  148. struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
  149. {
  150. struct io_rsrc_node *ref_node;
  151. ref_node = io_alloc_cache_get(&ctx->rsrc_node_cache);
  152. if (!ref_node) {
  153. ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
  154. if (!ref_node)
  155. return NULL;
  156. }
  157. ref_node->ctx = ctx;
  158. ref_node->empty = 0;
  159. ref_node->refs = 1;
  160. return ref_node;
  161. }
  162. __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
  163. struct io_ring_ctx *ctx)
  164. {
  165. struct io_rsrc_node *backup;
  166. DEFINE_WAIT(we);
  167. int ret;
  168. /* As We may drop ->uring_lock, other task may have started quiesce */
  169. if (data->quiesce)
  170. return -ENXIO;
  171. backup = io_rsrc_node_alloc(ctx);
  172. if (!backup)
  173. return -ENOMEM;
  174. ctx->rsrc_node->empty = true;
  175. ctx->rsrc_node->type = -1;
  176. list_add_tail(&ctx->rsrc_node->node, &ctx->rsrc_ref_list);
  177. io_put_rsrc_node(ctx, ctx->rsrc_node);
  178. ctx->rsrc_node = backup;
  179. if (list_empty(&ctx->rsrc_ref_list))
  180. return 0;
  181. if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
  182. atomic_set(&ctx->cq_wait_nr, 1);
  183. smp_mb();
  184. }
  185. ctx->rsrc_quiesce++;
  186. data->quiesce = true;
  187. do {
  188. prepare_to_wait(&ctx->rsrc_quiesce_wq, &we, TASK_INTERRUPTIBLE);
  189. mutex_unlock(&ctx->uring_lock);
  190. ret = io_run_task_work_sig(ctx);
  191. if (ret < 0) {
  192. finish_wait(&ctx->rsrc_quiesce_wq, &we);
  193. mutex_lock(&ctx->uring_lock);
  194. if (list_empty(&ctx->rsrc_ref_list))
  195. ret = 0;
  196. break;
  197. }
  198. schedule();
  199. mutex_lock(&ctx->uring_lock);
  200. ret = 0;
  201. } while (!list_empty(&ctx->rsrc_ref_list));
  202. finish_wait(&ctx->rsrc_quiesce_wq, &we);
  203. data->quiesce = false;
  204. ctx->rsrc_quiesce--;
  205. if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
  206. atomic_set(&ctx->cq_wait_nr, 0);
  207. smp_mb();
  208. }
  209. return ret;
  210. }
  211. static void io_free_page_table(void **table, size_t size)
  212. {
  213. unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
  214. for (i = 0; i < nr_tables; i++)
  215. kfree(table[i]);
  216. kfree(table);
  217. }
  218. static void io_rsrc_data_free(struct io_rsrc_data *data)
  219. {
  220. size_t size = data->nr * sizeof(data->tags[0][0]);
  221. if (data->tags)
  222. io_free_page_table((void **)data->tags, size);
  223. kfree(data);
  224. }
  225. static __cold void **io_alloc_page_table(size_t size)
  226. {
  227. unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
  228. size_t init_size = size;
  229. void **table;
  230. table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
  231. if (!table)
  232. return NULL;
  233. for (i = 0; i < nr_tables; i++) {
  234. unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
  235. table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
  236. if (!table[i]) {
  237. io_free_page_table(table, init_size);
  238. return NULL;
  239. }
  240. size -= this_size;
  241. }
  242. return table;
  243. }
  244. __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, int type,
  245. u64 __user *utags,
  246. unsigned nr, struct io_rsrc_data **pdata)
  247. {
  248. struct io_rsrc_data *data;
  249. int ret = 0;
  250. unsigned i;
  251. data = kzalloc(sizeof(*data), GFP_KERNEL);
  252. if (!data)
  253. return -ENOMEM;
  254. data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
  255. if (!data->tags) {
  256. kfree(data);
  257. return -ENOMEM;
  258. }
  259. data->nr = nr;
  260. data->ctx = ctx;
  261. data->rsrc_type = type;
  262. if (utags) {
  263. ret = -EFAULT;
  264. for (i = 0; i < nr; i++) {
  265. u64 *tag_slot = io_get_tag_slot(data, i);
  266. if (copy_from_user(tag_slot, &utags[i],
  267. sizeof(*tag_slot)))
  268. goto fail;
  269. }
  270. }
  271. *pdata = data;
  272. return 0;
  273. fail:
  274. io_rsrc_data_free(data);
  275. return ret;
  276. }
  277. static int __io_sqe_files_update(struct io_ring_ctx *ctx,
  278. struct io_uring_rsrc_update2 *up,
  279. unsigned nr_args)
  280. {
  281. u64 __user *tags = u64_to_user_ptr(up->tags);
  282. __s32 __user *fds = u64_to_user_ptr(up->data);
  283. struct io_rsrc_data *data = ctx->file_data;
  284. struct io_fixed_file *file_slot;
  285. int fd, i, err = 0;
  286. unsigned int done;
  287. if (!ctx->file_data)
  288. return -ENXIO;
  289. if (up->offset + nr_args > ctx->nr_user_files)
  290. return -EINVAL;
  291. for (done = 0; done < nr_args; done++) {
  292. u64 tag = 0;
  293. if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
  294. copy_from_user(&fd, &fds[done], sizeof(fd))) {
  295. err = -EFAULT;
  296. break;
  297. }
  298. if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
  299. err = -EINVAL;
  300. break;
  301. }
  302. if (fd == IORING_REGISTER_FILES_SKIP)
  303. continue;
  304. i = array_index_nospec(up->offset + done, ctx->nr_user_files);
  305. file_slot = io_fixed_file_slot(&ctx->file_table, i);
  306. if (file_slot->file_ptr) {
  307. err = io_queue_rsrc_removal(data, i,
  308. io_slot_file(file_slot));
  309. if (err)
  310. break;
  311. file_slot->file_ptr = 0;
  312. io_file_bitmap_clear(&ctx->file_table, i);
  313. }
  314. if (fd != -1) {
  315. struct file *file = fget(fd);
  316. if (!file) {
  317. err = -EBADF;
  318. break;
  319. }
  320. /*
  321. * Don't allow io_uring instances to be registered.
  322. */
  323. if (io_is_uring_fops(file)) {
  324. fput(file);
  325. err = -EBADF;
  326. break;
  327. }
  328. *io_get_tag_slot(data, i) = tag;
  329. io_fixed_file_set(file_slot, file);
  330. io_file_bitmap_set(&ctx->file_table, i);
  331. }
  332. }
  333. return done ? done : err;
  334. }
  335. static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
  336. struct io_uring_rsrc_update2 *up,
  337. unsigned int nr_args)
  338. {
  339. u64 __user *tags = u64_to_user_ptr(up->tags);
  340. struct iovec fast_iov, *iov;
  341. struct page *last_hpage = NULL;
  342. struct iovec __user *uvec;
  343. u64 user_data = up->data;
  344. __u32 done;
  345. int i, err;
  346. if (!ctx->buf_data)
  347. return -ENXIO;
  348. if (up->offset + nr_args > ctx->nr_user_bufs)
  349. return -EINVAL;
  350. for (done = 0; done < nr_args; done++) {
  351. struct io_mapped_ubuf *imu;
  352. u64 tag = 0;
  353. uvec = u64_to_user_ptr(user_data);
  354. iov = iovec_from_user(uvec, 1, 1, &fast_iov, ctx->compat);
  355. if (IS_ERR(iov)) {
  356. err = PTR_ERR(iov);
  357. break;
  358. }
  359. if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
  360. err = -EFAULT;
  361. break;
  362. }
  363. err = io_buffer_validate(iov);
  364. if (err)
  365. break;
  366. if (!iov->iov_base && tag) {
  367. err = -EINVAL;
  368. break;
  369. }
  370. err = io_sqe_buffer_register(ctx, iov, &imu, &last_hpage);
  371. if (err)
  372. break;
  373. i = array_index_nospec(up->offset + done, ctx->nr_user_bufs);
  374. if (ctx->user_bufs[i] != &dummy_ubuf) {
  375. err = io_queue_rsrc_removal(ctx->buf_data, i,
  376. ctx->user_bufs[i]);
  377. if (unlikely(err)) {
  378. io_buffer_unmap(ctx, &imu);
  379. break;
  380. }
  381. ctx->user_bufs[i] = (struct io_mapped_ubuf *)&dummy_ubuf;
  382. }
  383. ctx->user_bufs[i] = imu;
  384. *io_get_tag_slot(ctx->buf_data, i) = tag;
  385. if (ctx->compat)
  386. user_data += sizeof(struct compat_iovec);
  387. else
  388. user_data += sizeof(struct iovec);
  389. }
  390. return done ? done : err;
  391. }
  392. static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
  393. struct io_uring_rsrc_update2 *up,
  394. unsigned nr_args)
  395. {
  396. __u32 tmp;
  397. lockdep_assert_held(&ctx->uring_lock);
  398. if (check_add_overflow(up->offset, nr_args, &tmp))
  399. return -EOVERFLOW;
  400. switch (type) {
  401. case IORING_RSRC_FILE:
  402. return __io_sqe_files_update(ctx, up, nr_args);
  403. case IORING_RSRC_BUFFER:
  404. return __io_sqe_buffers_update(ctx, up, nr_args);
  405. }
  406. return -EINVAL;
  407. }
  408. int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
  409. unsigned nr_args)
  410. {
  411. struct io_uring_rsrc_update2 up;
  412. if (!nr_args)
  413. return -EINVAL;
  414. memset(&up, 0, sizeof(up));
  415. if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
  416. return -EFAULT;
  417. if (up.resv || up.resv2)
  418. return -EINVAL;
  419. return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
  420. }
  421. int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
  422. unsigned size, unsigned type)
  423. {
  424. struct io_uring_rsrc_update2 up;
  425. if (size != sizeof(up))
  426. return -EINVAL;
  427. if (copy_from_user(&up, arg, sizeof(up)))
  428. return -EFAULT;
  429. if (!up.nr || up.resv || up.resv2)
  430. return -EINVAL;
  431. return __io_register_rsrc_update(ctx, type, &up, up.nr);
  432. }
  433. __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
  434. unsigned int size, unsigned int type)
  435. {
  436. struct io_uring_rsrc_register rr;
  437. /* keep it extendible */
  438. if (size != sizeof(rr))
  439. return -EINVAL;
  440. memset(&rr, 0, sizeof(rr));
  441. if (copy_from_user(&rr, arg, size))
  442. return -EFAULT;
  443. if (!rr.nr || rr.resv2)
  444. return -EINVAL;
  445. if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
  446. return -EINVAL;
  447. switch (type) {
  448. case IORING_RSRC_FILE:
  449. if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
  450. break;
  451. return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
  452. rr.nr, u64_to_user_ptr(rr.tags));
  453. case IORING_RSRC_BUFFER:
  454. if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
  455. break;
  456. return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
  457. rr.nr, u64_to_user_ptr(rr.tags));
  458. }
  459. return -EINVAL;
  460. }
  461. int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  462. {
  463. struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
  464. if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
  465. return -EINVAL;
  466. if (sqe->rw_flags || sqe->splice_fd_in)
  467. return -EINVAL;
  468. up->offset = READ_ONCE(sqe->off);
  469. up->nr_args = READ_ONCE(sqe->len);
  470. if (!up->nr_args)
  471. return -EINVAL;
  472. up->arg = READ_ONCE(sqe->addr);
  473. return 0;
  474. }
  475. static int io_files_update_with_index_alloc(struct io_kiocb *req,
  476. unsigned int issue_flags)
  477. {
  478. struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
  479. __s32 __user *fds = u64_to_user_ptr(up->arg);
  480. unsigned int done;
  481. struct file *file;
  482. int ret, fd;
  483. if (!req->ctx->file_data)
  484. return -ENXIO;
  485. for (done = 0; done < up->nr_args; done++) {
  486. if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
  487. ret = -EFAULT;
  488. break;
  489. }
  490. file = fget(fd);
  491. if (!file) {
  492. ret = -EBADF;
  493. break;
  494. }
  495. ret = io_fixed_fd_install(req, issue_flags, file,
  496. IORING_FILE_INDEX_ALLOC);
  497. if (ret < 0)
  498. break;
  499. if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
  500. __io_close_fixed(req->ctx, issue_flags, ret);
  501. ret = -EFAULT;
  502. break;
  503. }
  504. }
  505. if (done)
  506. return done;
  507. return ret;
  508. }
  509. int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
  510. {
  511. struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
  512. struct io_ring_ctx *ctx = req->ctx;
  513. struct io_uring_rsrc_update2 up2;
  514. int ret;
  515. up2.offset = up->offset;
  516. up2.data = up->arg;
  517. up2.nr = 0;
  518. up2.tags = 0;
  519. up2.resv = 0;
  520. up2.resv2 = 0;
  521. if (up->offset == IORING_FILE_INDEX_ALLOC) {
  522. ret = io_files_update_with_index_alloc(req, issue_flags);
  523. } else {
  524. io_ring_submit_lock(ctx, issue_flags);
  525. ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
  526. &up2, up->nr_args);
  527. io_ring_submit_unlock(ctx, issue_flags);
  528. }
  529. if (ret < 0)
  530. req_set_fail(req);
  531. io_req_set_res(req, ret, 0);
  532. return IOU_OK;
  533. }
  534. int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc)
  535. {
  536. struct io_ring_ctx *ctx = data->ctx;
  537. struct io_rsrc_node *node = ctx->rsrc_node;
  538. u64 *tag_slot = io_get_tag_slot(data, idx);
  539. ctx->rsrc_node = io_rsrc_node_alloc(ctx);
  540. if (unlikely(!ctx->rsrc_node)) {
  541. ctx->rsrc_node = node;
  542. return -ENOMEM;
  543. }
  544. node->item.rsrc = rsrc;
  545. node->type = data->rsrc_type;
  546. node->item.tag = *tag_slot;
  547. *tag_slot = 0;
  548. list_add_tail(&node->node, &ctx->rsrc_ref_list);
  549. io_put_rsrc_node(ctx, node);
  550. return 0;
  551. }
  552. void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
  553. {
  554. int i;
  555. for (i = 0; i < ctx->nr_user_files; i++) {
  556. struct file *file = io_file_from_index(&ctx->file_table, i);
  557. if (!file)
  558. continue;
  559. io_file_bitmap_clear(&ctx->file_table, i);
  560. fput(file);
  561. }
  562. io_free_file_tables(&ctx->file_table);
  563. io_file_table_set_alloc_range(ctx, 0, 0);
  564. io_rsrc_data_free(ctx->file_data);
  565. ctx->file_data = NULL;
  566. ctx->nr_user_files = 0;
  567. }
  568. int io_sqe_files_unregister(struct io_ring_ctx *ctx)
  569. {
  570. unsigned nr = ctx->nr_user_files;
  571. int ret;
  572. if (!ctx->file_data)
  573. return -ENXIO;
  574. /*
  575. * Quiesce may unlock ->uring_lock, and while it's not held
  576. * prevent new requests using the table.
  577. */
  578. ctx->nr_user_files = 0;
  579. ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
  580. ctx->nr_user_files = nr;
  581. if (!ret)
  582. __io_sqe_files_unregister(ctx);
  583. return ret;
  584. }
  585. int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
  586. unsigned nr_args, u64 __user *tags)
  587. {
  588. __s32 __user *fds = (__s32 __user *) arg;
  589. struct file *file;
  590. int fd, ret;
  591. unsigned i;
  592. if (ctx->file_data)
  593. return -EBUSY;
  594. if (!nr_args)
  595. return -EINVAL;
  596. if (nr_args > IORING_MAX_FIXED_FILES)
  597. return -EMFILE;
  598. if (nr_args > rlimit(RLIMIT_NOFILE))
  599. return -EMFILE;
  600. ret = io_rsrc_data_alloc(ctx, IORING_RSRC_FILE, tags, nr_args,
  601. &ctx->file_data);
  602. if (ret)
  603. return ret;
  604. if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
  605. io_rsrc_data_free(ctx->file_data);
  606. ctx->file_data = NULL;
  607. return -ENOMEM;
  608. }
  609. for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
  610. struct io_fixed_file *file_slot;
  611. if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
  612. ret = -EFAULT;
  613. goto fail;
  614. }
  615. /* allow sparse sets */
  616. if (!fds || fd == -1) {
  617. ret = -EINVAL;
  618. if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
  619. goto fail;
  620. continue;
  621. }
  622. file = fget(fd);
  623. ret = -EBADF;
  624. if (unlikely(!file))
  625. goto fail;
  626. /*
  627. * Don't allow io_uring instances to be registered.
  628. */
  629. if (io_is_uring_fops(file)) {
  630. fput(file);
  631. goto fail;
  632. }
  633. file_slot = io_fixed_file_slot(&ctx->file_table, i);
  634. io_fixed_file_set(file_slot, file);
  635. io_file_bitmap_set(&ctx->file_table, i);
  636. }
  637. /* default it to the whole table */
  638. io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
  639. return 0;
  640. fail:
  641. __io_sqe_files_unregister(ctx);
  642. return ret;
  643. }
  644. static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
  645. {
  646. io_buffer_unmap(ctx, &prsrc->buf);
  647. prsrc->buf = NULL;
  648. }
  649. void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
  650. {
  651. unsigned int i;
  652. for (i = 0; i < ctx->nr_user_bufs; i++)
  653. io_buffer_unmap(ctx, &ctx->user_bufs[i]);
  654. kfree(ctx->user_bufs);
  655. io_rsrc_data_free(ctx->buf_data);
  656. ctx->user_bufs = NULL;
  657. ctx->buf_data = NULL;
  658. ctx->nr_user_bufs = 0;
  659. }
  660. int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
  661. {
  662. unsigned nr = ctx->nr_user_bufs;
  663. int ret;
  664. if (!ctx->buf_data)
  665. return -ENXIO;
  666. /*
  667. * Quiesce may unlock ->uring_lock, and while it's not held
  668. * prevent new requests using the table.
  669. */
  670. ctx->nr_user_bufs = 0;
  671. ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
  672. ctx->nr_user_bufs = nr;
  673. if (!ret)
  674. __io_sqe_buffers_unregister(ctx);
  675. return ret;
  676. }
  677. /*
  678. * Not super efficient, but this is just a registration time. And we do cache
  679. * the last compound head, so generally we'll only do a full search if we don't
  680. * match that one.
  681. *
  682. * We check if the given compound head page has already been accounted, to
  683. * avoid double accounting it. This allows us to account the full size of the
  684. * page, not just the constituent pages of a huge page.
  685. */
  686. static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
  687. int nr_pages, struct page *hpage)
  688. {
  689. int i, j;
  690. /* check current page array */
  691. for (i = 0; i < nr_pages; i++) {
  692. if (!PageCompound(pages[i]))
  693. continue;
  694. if (compound_head(pages[i]) == hpage)
  695. return true;
  696. }
  697. /* check previously registered pages */
  698. for (i = 0; i < ctx->nr_user_bufs; i++) {
  699. struct io_mapped_ubuf *imu = ctx->user_bufs[i];
  700. for (j = 0; j < imu->nr_bvecs; j++) {
  701. if (!PageCompound(imu->bvec[j].bv_page))
  702. continue;
  703. if (compound_head(imu->bvec[j].bv_page) == hpage)
  704. return true;
  705. }
  706. }
  707. return false;
  708. }
  709. static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
  710. int nr_pages, struct io_mapped_ubuf *imu,
  711. struct page **last_hpage)
  712. {
  713. int i, ret;
  714. imu->acct_pages = 0;
  715. for (i = 0; i < nr_pages; i++) {
  716. if (!PageCompound(pages[i])) {
  717. imu->acct_pages++;
  718. } else {
  719. struct page *hpage;
  720. hpage = compound_head(pages[i]);
  721. if (hpage == *last_hpage)
  722. continue;
  723. *last_hpage = hpage;
  724. if (headpage_already_acct(ctx, pages, i, hpage))
  725. continue;
  726. imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
  727. }
  728. }
  729. if (!imu->acct_pages)
  730. return 0;
  731. ret = io_account_mem(ctx, imu->acct_pages);
  732. if (ret)
  733. imu->acct_pages = 0;
  734. return ret;
  735. }
  736. static bool io_do_coalesce_buffer(struct page ***pages, int *nr_pages,
  737. struct io_imu_folio_data *data, int nr_folios)
  738. {
  739. struct page **page_array = *pages, **new_array = NULL;
  740. int nr_pages_left = *nr_pages, i, j;
  741. /* Store head pages only*/
  742. new_array = kvmalloc_array(nr_folios, sizeof(struct page *),
  743. GFP_KERNEL);
  744. if (!new_array)
  745. return false;
  746. new_array[0] = compound_head(page_array[0]);
  747. /*
  748. * The pages are bound to the folio, it doesn't
  749. * actually unpin them but drops all but one reference,
  750. * which is usually put down by io_buffer_unmap().
  751. * Note, needs a better helper.
  752. */
  753. if (data->nr_pages_head > 1)
  754. unpin_user_pages(&page_array[1], data->nr_pages_head - 1);
  755. j = data->nr_pages_head;
  756. nr_pages_left -= data->nr_pages_head;
  757. for (i = 1; i < nr_folios; i++) {
  758. unsigned int nr_unpin;
  759. new_array[i] = page_array[j];
  760. nr_unpin = min_t(unsigned int, nr_pages_left - 1,
  761. data->nr_pages_mid - 1);
  762. if (nr_unpin)
  763. unpin_user_pages(&page_array[j+1], nr_unpin);
  764. j += data->nr_pages_mid;
  765. nr_pages_left -= data->nr_pages_mid;
  766. }
  767. kvfree(page_array);
  768. *pages = new_array;
  769. *nr_pages = nr_folios;
  770. return true;
  771. }
  772. static bool io_try_coalesce_buffer(struct page ***pages, int *nr_pages,
  773. struct io_imu_folio_data *data)
  774. {
  775. struct page **page_array = *pages;
  776. struct folio *folio = page_folio(page_array[0]);
  777. unsigned int count = 1, nr_folios = 1;
  778. int i;
  779. if (*nr_pages <= 1)
  780. return false;
  781. data->nr_pages_mid = folio_nr_pages(folio);
  782. if (data->nr_pages_mid == 1)
  783. return false;
  784. data->folio_shift = folio_shift(folio);
  785. data->first_folio_page_idx = folio_page_idx(folio, page_array[0]);
  786. /*
  787. * Check if pages are contiguous inside a folio, and all folios have
  788. * the same page count except for the head and tail.
  789. */
  790. for (i = 1; i < *nr_pages; i++) {
  791. if (page_folio(page_array[i]) == folio &&
  792. page_array[i] == page_array[i-1] + 1) {
  793. count++;
  794. continue;
  795. }
  796. if (nr_folios == 1) {
  797. if (folio_page_idx(folio, page_array[i-1]) !=
  798. data->nr_pages_mid - 1)
  799. return false;
  800. data->nr_pages_head = count;
  801. } else if (count != data->nr_pages_mid) {
  802. return false;
  803. }
  804. folio = page_folio(page_array[i]);
  805. if (folio_size(folio) != (1UL << data->folio_shift) ||
  806. folio_page_idx(folio, page_array[i]) != 0)
  807. return false;
  808. count = 1;
  809. nr_folios++;
  810. }
  811. if (nr_folios == 1)
  812. data->nr_pages_head = count;
  813. return io_do_coalesce_buffer(pages, nr_pages, data, nr_folios);
  814. }
  815. static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
  816. struct io_mapped_ubuf **pimu,
  817. struct page **last_hpage)
  818. {
  819. struct io_mapped_ubuf *imu = NULL;
  820. struct page **pages = NULL;
  821. unsigned long off;
  822. size_t size;
  823. int ret, nr_pages, i;
  824. struct io_imu_folio_data data;
  825. bool coalesced;
  826. *pimu = (struct io_mapped_ubuf *)&dummy_ubuf;
  827. if (!iov->iov_base)
  828. return 0;
  829. ret = -ENOMEM;
  830. pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
  831. &nr_pages);
  832. if (IS_ERR(pages)) {
  833. ret = PTR_ERR(pages);
  834. pages = NULL;
  835. goto done;
  836. }
  837. /* If it's huge page(s), try to coalesce them into fewer bvec entries */
  838. coalesced = io_try_coalesce_buffer(&pages, &nr_pages, &data);
  839. imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
  840. if (!imu)
  841. goto done;
  842. ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
  843. if (ret)
  844. goto done;
  845. size = iov->iov_len;
  846. /* store original address for later verification */
  847. imu->ubuf = (unsigned long) iov->iov_base;
  848. imu->len = iov->iov_len;
  849. imu->nr_bvecs = nr_pages;
  850. imu->folio_shift = PAGE_SHIFT;
  851. if (coalesced)
  852. imu->folio_shift = data.folio_shift;
  853. refcount_set(&imu->refs, 1);
  854. off = (unsigned long)iov->iov_base & ~PAGE_MASK;
  855. if (coalesced)
  856. off += data.first_folio_page_idx << PAGE_SHIFT;
  857. *pimu = imu;
  858. ret = 0;
  859. for (i = 0; i < nr_pages; i++) {
  860. size_t vec_len;
  861. vec_len = min_t(size_t, size, (1UL << imu->folio_shift) - off);
  862. bvec_set_page(&imu->bvec[i], pages[i], vec_len, off);
  863. off = 0;
  864. size -= vec_len;
  865. }
  866. done:
  867. if (ret) {
  868. kvfree(imu);
  869. if (pages) {
  870. for (i = 0; i < nr_pages; i++)
  871. unpin_user_folio(page_folio(pages[i]), 1);
  872. }
  873. }
  874. kvfree(pages);
  875. return ret;
  876. }
  877. static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
  878. {
  879. ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
  880. return ctx->user_bufs ? 0 : -ENOMEM;
  881. }
  882. int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
  883. unsigned int nr_args, u64 __user *tags)
  884. {
  885. struct page *last_hpage = NULL;
  886. struct io_rsrc_data *data;
  887. struct iovec fast_iov, *iov = &fast_iov;
  888. const struct iovec __user *uvec;
  889. int i, ret;
  890. BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
  891. if (ctx->user_bufs)
  892. return -EBUSY;
  893. if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
  894. return -EINVAL;
  895. ret = io_rsrc_data_alloc(ctx, IORING_RSRC_BUFFER, tags, nr_args, &data);
  896. if (ret)
  897. return ret;
  898. ret = io_buffers_map_alloc(ctx, nr_args);
  899. if (ret) {
  900. io_rsrc_data_free(data);
  901. return ret;
  902. }
  903. if (!arg)
  904. memset(iov, 0, sizeof(*iov));
  905. for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
  906. if (arg) {
  907. uvec = (struct iovec __user *) arg;
  908. iov = iovec_from_user(uvec, 1, 1, &fast_iov, ctx->compat);
  909. if (IS_ERR(iov)) {
  910. ret = PTR_ERR(iov);
  911. break;
  912. }
  913. ret = io_buffer_validate(iov);
  914. if (ret)
  915. break;
  916. if (ctx->compat)
  917. arg += sizeof(struct compat_iovec);
  918. else
  919. arg += sizeof(struct iovec);
  920. }
  921. if (!iov->iov_base && *io_get_tag_slot(data, i)) {
  922. ret = -EINVAL;
  923. break;
  924. }
  925. ret = io_sqe_buffer_register(ctx, iov, &ctx->user_bufs[i],
  926. &last_hpage);
  927. if (ret)
  928. break;
  929. }
  930. WARN_ON_ONCE(ctx->buf_data);
  931. ctx->buf_data = data;
  932. if (ret)
  933. __io_sqe_buffers_unregister(ctx);
  934. return ret;
  935. }
  936. int io_import_fixed(int ddir, struct iov_iter *iter,
  937. struct io_mapped_ubuf *imu,
  938. u64 buf_addr, size_t len)
  939. {
  940. u64 buf_end;
  941. size_t offset;
  942. if (WARN_ON_ONCE(!imu))
  943. return -EFAULT;
  944. if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
  945. return -EFAULT;
  946. /* not inside the mapped region */
  947. if (unlikely(buf_addr < imu->ubuf || buf_end > (imu->ubuf + imu->len)))
  948. return -EFAULT;
  949. /*
  950. * Might not be a start of buffer, set size appropriately
  951. * and advance us to the beginning.
  952. */
  953. offset = buf_addr - imu->ubuf;
  954. iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
  955. if (offset) {
  956. /*
  957. * Don't use iov_iter_advance() here, as it's really slow for
  958. * using the latter parts of a big fixed buffer - it iterates
  959. * over each segment manually. We can cheat a bit here, because
  960. * we know that:
  961. *
  962. * 1) it's a BVEC iter, we set it up
  963. * 2) all bvecs are the same in size, except potentially the
  964. * first and last bvec
  965. *
  966. * So just find our index, and adjust the iterator afterwards.
  967. * If the offset is within the first bvec (or the whole first
  968. * bvec, just use iov_iter_advance(). This makes it easier
  969. * since we can just skip the first segment, which may not
  970. * be folio_size aligned.
  971. */
  972. const struct bio_vec *bvec = imu->bvec;
  973. if (offset < bvec->bv_len) {
  974. iter->bvec = bvec;
  975. iter->count -= offset;
  976. iter->iov_offset = offset;
  977. } else {
  978. unsigned long seg_skip;
  979. /* skip first vec */
  980. offset -= bvec->bv_len;
  981. seg_skip = 1 + (offset >> imu->folio_shift);
  982. iter->bvec = bvec + seg_skip;
  983. iter->nr_segs -= seg_skip;
  984. iter->count -= bvec->bv_len + offset;
  985. iter->iov_offset = offset & ((1UL << imu->folio_shift) - 1);
  986. }
  987. }
  988. return 0;
  989. }
  990. static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx)
  991. {
  992. struct io_mapped_ubuf **user_bufs;
  993. struct io_rsrc_data *data;
  994. int i, ret, nbufs;
  995. /*
  996. * Accounting state is shared between the two rings; that only works if
  997. * both rings are accounted towards the same counters.
  998. */
  999. if (ctx->user != src_ctx->user || ctx->mm_account != src_ctx->mm_account)
  1000. return -EINVAL;
  1001. /*
  1002. * Drop our own lock here. We'll setup the data we need and reference
  1003. * the source buffers, then re-grab, check, and assign at the end.
  1004. */
  1005. mutex_unlock(&ctx->uring_lock);
  1006. mutex_lock(&src_ctx->uring_lock);
  1007. ret = -ENXIO;
  1008. nbufs = src_ctx->nr_user_bufs;
  1009. if (!nbufs)
  1010. goto out_unlock;
  1011. ret = io_rsrc_data_alloc(ctx, IORING_RSRC_BUFFER, NULL, nbufs, &data);
  1012. if (ret)
  1013. goto out_unlock;
  1014. ret = -ENOMEM;
  1015. user_bufs = kcalloc(nbufs, sizeof(*ctx->user_bufs), GFP_KERNEL);
  1016. if (!user_bufs)
  1017. goto out_free_data;
  1018. for (i = 0; i < nbufs; i++) {
  1019. struct io_mapped_ubuf *src = src_ctx->user_bufs[i];
  1020. if (src != &dummy_ubuf)
  1021. refcount_inc(&src->refs);
  1022. user_bufs[i] = src;
  1023. }
  1024. /* Have a ref on the bufs now, drop src lock and re-grab our own lock */
  1025. mutex_unlock(&src_ctx->uring_lock);
  1026. mutex_lock(&ctx->uring_lock);
  1027. if (!ctx->user_bufs) {
  1028. ctx->user_bufs = user_bufs;
  1029. ctx->buf_data = data;
  1030. ctx->nr_user_bufs = nbufs;
  1031. return 0;
  1032. }
  1033. /* someone raced setting up buffers, dump ours */
  1034. for (i = 0; i < nbufs; i++)
  1035. io_buffer_unmap(ctx, &user_bufs[i]);
  1036. io_rsrc_data_free(data);
  1037. kfree(user_bufs);
  1038. return -EBUSY;
  1039. out_free_data:
  1040. io_rsrc_data_free(data);
  1041. out_unlock:
  1042. mutex_unlock(&src_ctx->uring_lock);
  1043. mutex_lock(&ctx->uring_lock);
  1044. return ret;
  1045. }
  1046. /*
  1047. * Copy the registered buffers from the source ring whose file descriptor
  1048. * is given in the src_fd to the current ring. This is identical to registering
  1049. * the buffers with ctx, except faster as mappings already exist.
  1050. *
  1051. * Since the memory is already accounted once, don't account it again.
  1052. */
  1053. int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg)
  1054. {
  1055. struct io_uring_clone_buffers buf;
  1056. bool registered_src;
  1057. struct file *file;
  1058. int ret;
  1059. if (ctx->user_bufs || ctx->nr_user_bufs)
  1060. return -EBUSY;
  1061. if (copy_from_user(&buf, arg, sizeof(buf)))
  1062. return -EFAULT;
  1063. if (buf.flags & ~IORING_REGISTER_SRC_REGISTERED)
  1064. return -EINVAL;
  1065. if (memchr_inv(buf.pad, 0, sizeof(buf.pad)))
  1066. return -EINVAL;
  1067. registered_src = (buf.flags & IORING_REGISTER_SRC_REGISTERED) != 0;
  1068. file = io_uring_register_get_file(buf.src_fd, registered_src);
  1069. if (IS_ERR(file))
  1070. return PTR_ERR(file);
  1071. ret = io_clone_buffers(ctx, file->private_data);
  1072. if (!registered_src)
  1073. fput(file);
  1074. return ret;
  1075. }