Linux Audio

Check our new training course

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