staticint io_buffer_add_list(struct io_ring_ctx *ctx, struct io_buffer_list *bl, unsignedint bgid)
{ /* * Store buffer group ID and finally mark the list as visible. * The normal lookup doesn't care about the visibility as we're * always under the ->uring_lock, but lookups from mmap do.
*/
bl->bgid = bgid;
guard(mutex)(&ctx->mmap_lock); return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
}
if (issue_flags & IO_URING_F_UNLOCKED || !io_file_can_poll(req)) { /* * If we came in unlocked, we have no choice but to consume the * buffer here, otherwise nothing ensures that the buffer won't * get used by others. This does mean it'll be pinned until the * IO completes, coming in unlocked means we're being called from * io-wq context and there may be further retries in async hybrid * mode. For the locked case, the caller must call commit when * the transfer completes (or if we get -EAGAIN and must poll of * retry).
*/
io_kbuf_commit(req, bl, *len, 1);
req->buf_list = NULL;
} return ret;
}
tail = smp_load_acquire(&br->tail);
head = bl->head;
nr_avail = min_t(__u16, tail - head, UIO_MAXIOV); if (unlikely(!nr_avail)) return -ENOBUFS;
buf = io_ring_head_to_buf(br, head, bl->mask); if (arg->max_len) {
u32 len = READ_ONCE(buf->len);
size_t needed;
if (unlikely(!len)) return -ENOBUFS;
needed = (arg->max_len + len - 1) / len;
needed = min_not_zero(needed, (size_t) PEEK_MAX_IMPORT); if (nr_avail > needed)
nr_avail = needed;
}
/* * only alloc a bigger array if we know we have data to map, eg not * a speculative peek operation.
*/ if (arg->mode & KBUF_MODE_EXPAND && nr_avail > nr_iovs && arg->max_len) {
iov = kmalloc_array(nr_avail, sizeof(struct iovec), GFP_KERNEL); if (unlikely(!iov)) return -ENOMEM; if (arg->mode & KBUF_MODE_FREE)
kfree(arg->iovs);
arg->iovs = iov;
nr_iovs = nr_avail;
} elseif (nr_avail < nr_iovs) {
nr_iovs = nr_avail;
}
/* set it to max, if not set, so we can use it unconditionally */ if (!arg->max_len)
arg->max_len = INT_MAX;
req->buf_index = buf->bid; do {
u32 len = READ_ONCE(buf->len);
/* truncate end piece, if needed, for non partial buffers */ if (len > arg->max_len) {
len = arg->max_len; if (!(bl->flags & IOBL_INC)) {
arg->partial_map = 1; if (iov != arg->iovs) break;
buf->len = len;
}
}
int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg, unsignedint issue_flags)
{ struct io_ring_ctx *ctx = req->ctx; struct io_buffer_list *bl; int ret = -ENOENT;
io_ring_submit_lock(ctx, issue_flags);
bl = io_buffer_get_list(ctx, arg->buf_group); if (unlikely(!bl)) goto out_unlock;
if (bl->flags & IOBL_BUF_RING) {
ret = io_ring_buffers_peek(req, arg, bl); /* * Don't recycle these buffers if we need to go through poll. * Nobody else can use them anyway, and holding on to provided * buffers for a send/write operation would happen on the app * side anyway with normal buffers. Besides, we already * committed them, they cannot be put back in the queue.
*/ if (ret > 0) {
req->flags |= REQ_F_BUFFERS_COMMIT | REQ_F_BL_NO_RECYCLE;
io_kbuf_commit(req, bl, arg->out_len, ret);
}
} else {
ret = io_provided_buffers_select(req, &arg->out_len, bl, arg->iovs);
}
out_unlock:
io_ring_submit_unlock(ctx, issue_flags); return ret;
}
int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg)
{ struct io_ring_ctx *ctx = req->ctx; struct io_buffer_list *bl; int ret;
lockdep_assert_held(&ctx->uring_lock);
bl = io_buffer_get_list(ctx, arg->buf_group); if (unlikely(!bl)) return -ENOENT;
if (bl->flags & IOBL_BUF_RING) {
ret = io_ring_buffers_peek(req, arg, bl); if (ret > 0)
req->flags |= REQ_F_BUFFERS_COMMIT; return ret;
}
/* don't support multiple buffer selections for legacy */ return io_provided_buffers_select(req, &arg->max_len, bl, arg->iovs);
}
staticinlinebool __io_put_kbuf_ring(struct io_kiocb *req, int len, int nr)
{ struct io_buffer_list *bl = req->buf_list; bool ret = true;
if (bl)
ret = io_kbuf_commit(req, bl, len, nr);
req->flags &= ~REQ_F_BUFFER_RING; return ret;
}
unsignedint __io_put_kbufs(struct io_kiocb *req, int len, int nbufs)
{ unsignedint ret;
ret = IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
if (unlikely(!(req->flags & REQ_F_BUFFER_RING))) {
io_kbuf_drop_legacy(req); return ret;
}
if (!__io_put_kbuf_ring(req, len, nbufs))
ret |= IORING_CQE_F_BUF_MORE; return ret;
}
staticint io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, struct io_buffer_list *bl)
{ struct io_buffer *buf;
u64 addr = pbuf->addr; int ret = -ENOMEM, i, bid = pbuf->bid;
for (i = 0; i < pbuf->nbufs; i++) { /* * Nonsensical to have more than sizeof(bid) buffers in a * buffer list, as the application then has no way of knowing * which duplicate bid refers to what buffer.
*/ if (bl->nbufs == USHRT_MAX) {
ret = -EOVERFLOW; break;
}
buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); if (!buf) break;
if (copy_from_user(®, arg, sizeof(reg))) return -EFAULT; if (!mem_is_zero(reg.resv, sizeof(reg.resv))) return -EINVAL; if (reg.flags & ~(IOU_PBUF_RING_MMAP | IOU_PBUF_RING_INC)) return -EINVAL; if (!is_power_of_2(reg.ring_entries)) return -EINVAL; /* cannot disambiguate full vs empty due to head/tail size */ if (reg.ring_entries >= 65536) return -EINVAL;
bl = io_buffer_get_list(ctx, reg.bgid); if (bl) { /* if mapped buffer ring OR classic exists, don't allow */ if (bl->flags & IOBL_BUF_RING || !list_empty(&bl->buf_list)) return -EEXIST;
io_destroy_bl(ctx, bl);
}
bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT); if (!bl) return -ENOMEM;
#ifdef SHM_COLOUR /* * On platforms that have specific aliasing requirements, SHM_COLOUR * is set and we must guarantee that the kernel and user side align * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and * the application mmap's the provided ring buffer. Fail the request * if we, by chance, don't end up with aligned addresses. The app * should use IOU_PBUF_RING_MMAP instead, and liburing will handle * this transparently.
*/ if (!(reg.flags & IOU_PBUF_RING_MMAP) &&
((reg.ring_addr | (unsignedlong)br) & (SHM_COLOUR - 1))) {
ret = -EINVAL; goto fail;
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.