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