io_uring_cmd_is_kmbuf_ring() returns true if there is a kernel-managed buffer ring at the specified buffer group. This is a preparatory patch for upcoming fuse kernel-managed buffer support, which needs to ensure the buffer ring registered by the server is a kernel-managed buffer ring. Signed-off-by: Joanne Koong --- include/linux/io_uring/cmd.h | 9 +++++++++ io_uring/kbuf.c | 19 +++++++++++++++++++ io_uring/kbuf.h | 2 ++ io_uring/uring_cmd.c | 9 +++++++++ 4 files changed, 39 insertions(+) diff --git a/include/linux/io_uring/cmd.h b/include/linux/io_uring/cmd.h index 2988592e045c..a94f1dbc89c7 100644 --- a/include/linux/io_uring/cmd.h +++ b/include/linux/io_uring/cmd.h @@ -99,6 +99,9 @@ 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); + +int io_uring_cmd_is_kmbuf_ring(struct io_uring_cmd *ioucmd, + unsigned int buf_group, unsigned int issue_flags); #else static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, @@ -176,6 +179,12 @@ static inline int io_uring_cmd_kmbuffer_recycle(struct io_uring_cmd *cmd, { return -EOPNOTSUPP; } +static inline int io_uring_cmd_is_kmbuf_ring(struct io_uring_cmd *ioucmd, + unsigned int buf_group, + 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 f494d896c17e..b16f6a6aa872 100644 --- a/io_uring/kbuf.c +++ b/io_uring/kbuf.c @@ -960,3 +960,22 @@ int io_register_kmbuf_ring(struct io_ring_ctx *ctx, void __user *arg) return 0; } + +bool io_is_kmbuf_ring(struct io_kiocb *req, unsigned int buf_group, + unsigned int issue_flags) +{ + struct io_ring_ctx *ctx = req->ctx; + struct io_buffer_list *bl; + bool is_kmbuf_ring = false; + + io_ring_submit_lock(ctx, issue_flags); + + bl = io_buffer_get_list(ctx, buf_group); + if (likely(bl) && (bl->flags & IOBL_KERNEL_MANAGED)) { + WARN_ON_ONCE(!(bl->flags & IOBL_BUF_RING)); + is_kmbuf_ring = true; + } + + io_ring_submit_unlock(ctx, issue_flags); + return is_kmbuf_ring; +} diff --git a/io_uring/kbuf.h b/io_uring/kbuf.h index 4d8b7491628e..68c4c78fbb44 100644 --- a/io_uring/kbuf.h +++ b/io_uring/kbuf.h @@ -149,4 +149,6 @@ int io_kbuf_ring_unpin(struct io_kiocb *req, unsigned buf_group, int io_kmbuf_recycle(struct io_kiocb *req, unsigned int bgid, u64 addr, unsigned int len, unsigned int bid, unsigned int issue_flags); +bool io_is_kmbuf_ring(struct io_kiocb *req, unsigned int buf_group, + unsigned int issue_flags); #endif diff --git a/io_uring/uring_cmd.c b/io_uring/uring_cmd.c index ee95d1102505..4534710252da 100644 --- a/io_uring/uring_cmd.c +++ b/io_uring/uring_cmd.c @@ -448,3 +448,12 @@ int io_uring_cmd_kmbuffer_recycle(struct io_uring_cmd *ioucmd, return io_kmbuf_recycle(req, buf_group, addr, len, bid, issue_flags); } EXPORT_SYMBOL_GPL(io_uring_cmd_kmbuffer_recycle); + +int io_uring_cmd_is_kmbuf_ring(struct io_uring_cmd *ioucmd, + unsigned int buf_group, unsigned int issue_flags) +{ + struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); + + return io_is_kmbuf_ring(req, buf_group, issue_flags); +} +EXPORT_SYMBOL_GPL(io_uring_cmd_is_kmbuf_ring); -- 2.47.3