Skip to content

Commit 914e0dc

Browse files
Ming Leiaxboe
authored andcommitted
ublk: run auto buf unregisgering in same io_ring_ctx with registering
UBLK_F_AUTO_BUF_REG requires that the buffer registered automatically is unregistered in same `io_ring_ctx`, so check it explicitly. Document this requirement for UBLK_F_AUTO_BUF_REG. Drop WARN_ON_ONCE() which is triggered from userspace code path. Fixes: 99c1e4e ("ublk: register buffer to local io_uring with provided buf index via UBLK_F_AUTO_BUF_REG") Reported-by: Caleb Sander Mateos <[email protected]> Reviewed-by: Caleb Sander Mateos <[email protected]> Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 3a91f28 commit 914e0dc

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

drivers/block/ublk_drv.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct ublk_rq_data {
8484

8585
/* for auto-unregister buffer in case of UBLK_F_AUTO_BUF_REG */
8686
u16 buf_index;
87+
void *buf_ctx_handle;
8788
};
8889

8990
struct ublk_uring_cmd_pdu {
@@ -1211,6 +1212,8 @@ static bool ublk_auto_buf_reg(struct request *req, struct ublk_io *io,
12111212
}
12121213
/* one extra reference is dropped by ublk_io_release */
12131214
refcount_set(&data->ref, 2);
1215+
1216+
data->buf_ctx_handle = io_uring_cmd_ctx_handle(io->cmd);
12141217
/* store buffer index in request payload */
12151218
data->buf_index = pdu->buf.index;
12161219
io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
@@ -2111,12 +2114,22 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
21112114
if (ublk_support_auto_buf_reg(ubq)) {
21122115
int ret;
21132116

2117+
/*
2118+
* `UBLK_F_AUTO_BUF_REG` only works iff `UBLK_IO_FETCH_REQ`
2119+
* and `UBLK_IO_COMMIT_AND_FETCH_REQ` are issued from same
2120+
* `io_ring_ctx`.
2121+
*
2122+
* If this uring_cmd's io_ring_ctx isn't same with the
2123+
* one for registering the buffer, it is ublk server's
2124+
* responsibility for unregistering the buffer, otherwise
2125+
* this ublk request gets stuck.
2126+
*/
21142127
if (io->flags & UBLK_IO_FLAG_AUTO_BUF_REG) {
21152128
struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
21162129

2117-
WARN_ON_ONCE(io_buffer_unregister_bvec(cmd,
2118-
data->buf_index,
2119-
issue_flags));
2130+
if (data->buf_ctx_handle == io_uring_cmd_ctx_handle(cmd))
2131+
io_buffer_unregister_bvec(cmd, data->buf_index,
2132+
issue_flags);
21202133
io->flags &= ~UBLK_IO_FLAG_AUTO_BUF_REG;
21212134
}
21222135

include/uapi/linux/ublk_cmd.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@
226226
*
227227
* For using this feature:
228228
*
229-
* - ublk server has to create sparse buffer table
229+
* - ublk server has to create sparse buffer table on the same `io_ring_ctx`
230+
* for issuing `UBLK_IO_FETCH_REQ` and `UBLK_IO_COMMIT_AND_FETCH_REQ`.
231+
* If uring_cmd isn't issued on same `io_ring_ctx`, it is ublk server's
232+
* responsibility to unregister the buffer by issuing `IO_UNREGISTER_IO_BUF`
233+
* manually, otherwise this ublk request won't complete.
230234
*
231235
* - ublk server passes auto buf register data via uring_cmd's sqe->addr,
232236
* `struct ublk_auto_buf_reg` is populated from sqe->addr, please see

0 commit comments

Comments
 (0)