Add an interface for buffers to be recycled back into a kernel-managed buffer ring. This is a preparatory patch for fuse over io-uring. Signed-off-by: Joanne Koong --- include/linux/io_uring/cmd.h | 13 +++++++++++ io_uring/kbuf.c | 42 ++++++++++++++++++++++++++++++++++++ io_uring/kbuf.h | 3 +++ io_uring/uring_cmd.c | 11 ++++++++++ 4 files changed, 69 insertions(+) diff --git a/include/linux/io_uring/cmd.h b/include/linux/io_uring/cmd.h index 424f071f42e5..7169a2a9a744 100644 --- a/include/linux/io_uring/cmd.h +++ b/include/linux/io_uring/cmd.h @@ -88,6 +88,11 @@ int io_uring_cmd_buf_ring_pin(struct io_uring_cmd *ioucmd, unsigned buf_group, unsigned issue_flags, struct io_buffer_list **bl); int io_uring_cmd_buf_ring_unpin(struct io_uring_cmd *ioucmd, unsigned buf_group, unsigned issue_flags); + +int io_uring_cmd_kmbuffer_recycle(struct io_uring_cmd *cmd, + unsigned int buf_group, u64 addr, + unsigned int len, unsigned int bid, + unsigned int issue_flags); #else static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, @@ -143,6 +148,14 @@ static inline int io_uring_cmd_buf_ring_unpin(struct io_uring_cmd *ioucmd, { return -EOPNOTSUPP; } +static inline int io_uring_cmd_kmbuffer_recycle(struct io_uring_cmd *cmd, + unsigned int buf_group, + u64 addr, unsigned int len, + unsigned int bid, + unsigned int issue_flags) +{ + return -EOPNOTSUPP; +} #endif static inline struct io_uring_cmd *io_uring_cmd_from_tw(struct io_tw_req tw_req) diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c index 49dc75f24432..f494d896c17e 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -101,6 +101,48 @@ void io_kbuf_drop_legacy(struct io_kiocb *req) req->kbuf = NULL; } +int io_kmbuf_recycle(struct io_kiocb *req, unsigned int bgid, u64 addr, + unsigned int len, unsigned int bid, + unsigned int issue_flags) +{ + struct io_ring_ctx *ctx = req->ctx; + struct io_uring_buf_ring *br; + struct io_uring_buf *buf; + struct io_buffer_list *bl; + int ret = -EINVAL; + + if (WARN_ON_ONCE(req->flags & REQ_F_BUFFERS_COMMIT)) + return ret; + + io_ring_submit_lock(ctx, issue_flags); + + bl = io_buffer_get_list(ctx, bgid); + + if (WARN_ON_ONCE(!(bl->flags & IOBL_BUF_RING)) || + WARN_ON_ONCE(!(bl->flags & IOBL_KERNEL_MANAGED))) + goto done; + + br = bl->buf_ring; + + if (WARN_ON_ONCE((br->tail - bl->head) >= bl->nr_entries)) + goto done; + + buf = &br->bufs[(br->tail) & bl->mask]; + + buf->addr = addr; + buf->len = len; + buf->bid = bid; + + req->flags &= ~REQ_F_BUFFER_RING; + + br->tail++; + ret = 0; + +done: + io_ring_submit_unlock(ctx, issue_flags); + return ret; +} + bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags) { struct io_ring_ctx *ctx = req->ctx; diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index c4368f35cf11..4d8b7491628e 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -146,4 +146,7 @@ int io_kbuf_ring_pin(struct io_kiocb *req, unsigned buf_group, unsigned issue_flags, struct io_buffer_list **bl); int io_kbuf_ring_unpin(struct io_kiocb *req, unsigned buf_group, unsigned issue_flags); +int io_kmbuf_recycle(struct io_kiocb *req, unsigned int bgid, u64 addr, + unsigned int len, unsigned int bid, + unsigned int issue_flags); #endif diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index 8ac79ead4158..b6b675010bfd 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -416,3 +416,14 @@ int io_uring_cmd_buf_ring_unpin(struct io_uring_cmd *ioucmd, unsigned buf_group, return io_kbuf_ring_unpin(req, buf_group, issue_flags); } EXPORT_SYMBOL_GPL(io_uring_cmd_buf_ring_unpin); + +int io_uring_cmd_kmbuffer_recycle(struct io_uring_cmd *ioucmd, + unsigned int buf_group, u64 addr, + unsigned int len, unsigned int bid, + unsigned int issue_flags) +{ + struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); + + return io_kmbuf_recycle(req, buf_group, addr, len, bid, issue_flags); +} +EXPORT_SYMBOL_GPL(io_uring_cmd_kmbuffer_recycle); -- 2.47.3