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