Skip to content

Commit 0a9beaf

Browse files
Ming Leiaxboe
authored andcommitted
ublk: refactor auto buffer register in ublk_dispatch_req()
Refactor auto buffer register code and prepare for supporting batch IO feature, and the main motivation is to put 'ublk_io' operation code together, so that per-io lock can be applied for the code block. The key changes are: - Rename ublk_auto_buf_reg() as ublk_do_auto_buf_reg() - Introduce an enum `auto_buf_reg_res` to represent the result of the buffer registration attempt (FAIL, FALLBACK, OK). - Split the existing `ublk_do_auto_buf_reg` function into two: - `__ublk_do_auto_buf_reg`: Performs the actual buffer registration and returns the `auto_buf_reg_res` status. - `ublk_do_auto_buf_reg`: A wrapper that calls the internal function and handles the I/O preparation based on the result. - Introduce `ublk_prep_auto_buf_reg_io` to encapsulate the logic for preparing the I/O for completion after buffer registration. - Pass the `tag` directly to `ublk_auto_buf_reg_fallback` to avoid recalculating it. This refactoring makes the control flow clearer and isolates the different stages of the auto buffer registration process. Reviewed-by: Caleb Sander Mateos <csander@purestorage.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 8d61ece commit 0a9beaf

1 file changed

Lines changed: 43 additions & 21 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,47 +1168,65 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,
11681168
}
11691169

11701170
static void
1171-
ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, struct ublk_io *io)
1171+
ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, unsigned tag)
11721172
{
1173-
unsigned tag = io - ubq->ios;
11741173
struct ublksrv_io_desc *iod = ublk_get_iod(ubq, tag);
11751174

11761175
iod->op_flags |= UBLK_IO_F_NEED_REG_BUF;
11771176
}
11781177

1179-
static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
1180-
struct ublk_io *io, struct io_uring_cmd *cmd,
1181-
unsigned int issue_flags)
1178+
enum auto_buf_reg_res {
1179+
AUTO_BUF_REG_FAIL,
1180+
AUTO_BUF_REG_FALLBACK,
1181+
AUTO_BUF_REG_OK,
1182+
};
1183+
1184+
static void ublk_prep_auto_buf_reg_io(const struct ublk_queue *ubq,
1185+
struct request *req, struct ublk_io *io,
1186+
struct io_uring_cmd *cmd,
1187+
enum auto_buf_reg_res res)
1188+
{
1189+
if (res == AUTO_BUF_REG_OK) {
1190+
io->task_registered_buffers = 1;
1191+
io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
1192+
io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
1193+
}
1194+
ublk_init_req_ref(ubq, io);
1195+
__ublk_prep_compl_io_cmd(io, req);
1196+
}
1197+
1198+
static enum auto_buf_reg_res
1199+
__ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
1200+
struct ublk_io *io, struct io_uring_cmd *cmd,
1201+
unsigned int issue_flags)
11821202
{
11831203
int ret;
11841204

11851205
ret = io_buffer_register_bvec(cmd, req, ublk_io_release,
11861206
io->buf.auto_reg.index, issue_flags);
11871207
if (ret) {
11881208
if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
1189-
ublk_auto_buf_reg_fallback(ubq, io);
1190-
return true;
1209+
ublk_auto_buf_reg_fallback(ubq, req->tag);
1210+
return AUTO_BUF_REG_FALLBACK;
11911211
}
11921212
blk_mq_end_request(req, BLK_STS_IOERR);
1193-
return false;
1213+
return AUTO_BUF_REG_FAIL;
11941214
}
11951215

1196-
io->task_registered_buffers = 1;
1197-
io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
1198-
io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
1199-
return true;
1216+
return AUTO_BUF_REG_OK;
12001217
}
12011218

1202-
static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq,
1203-
struct request *req, struct ublk_io *io,
1204-
struct io_uring_cmd *cmd,
1205-
unsigned int issue_flags)
1219+
static void ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
1220+
struct ublk_io *io, struct io_uring_cmd *cmd,
1221+
unsigned int issue_flags)
12061222
{
1207-
ublk_init_req_ref(ubq, io);
1208-
if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
1209-
return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags);
1223+
enum auto_buf_reg_res res = __ublk_do_auto_buf_reg(ubq, req, io, cmd,
1224+
issue_flags);
12101225

1211-
return true;
1226+
if (res != AUTO_BUF_REG_FAIL) {
1227+
ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res);
1228+
io_uring_cmd_done(cmd, UBLK_IO_RES_OK, issue_flags);
1229+
}
12121230
}
12131231

12141232
static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
@@ -1281,8 +1299,12 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
12811299
if (!ublk_start_io(ubq, req, io))
12821300
return;
12831301

1284-
if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags))
1302+
if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) {
1303+
ublk_do_auto_buf_reg(ubq, req, io, io->cmd, issue_flags);
1304+
} else {
1305+
ublk_init_req_ref(ubq, io);
12851306
ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
1307+
}
12861308
}
12871309

12881310
static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,

0 commit comments

Comments
 (0)