Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Ming Lei and committed by
Jens Axboe
0a9beafa 8d61ece1

+43 -21
+43 -21
drivers/block/ublk_drv.c
··· 1168 1168 } 1169 1169 1170 1170 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) 1172 1172 { 1173 - unsigned tag = io - ubq->ios; 1174 1173 struct ublksrv_io_desc *iod = ublk_get_iod(ubq, tag); 1175 1174 1176 1175 iod->op_flags |= UBLK_IO_F_NEED_REG_BUF; 1177 1176 } 1178 1177 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) 1182 1202 { 1183 1203 int ret; 1184 1204 ··· 1206 1186 io->buf.auto_reg.index, issue_flags); 1207 1187 if (ret) { 1208 1188 if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) { 1209 - ublk_auto_buf_reg_fallback(ubq, io); 1210 - return true; 1189 + ublk_auto_buf_reg_fallback(ubq, req->tag); 1190 + return AUTO_BUF_REG_FALLBACK; 1211 1191 } 1212 1192 blk_mq_end_request(req, BLK_STS_IOERR); 1213 - return false; 1193 + return AUTO_BUF_REG_FAIL; 1214 1194 } 1215 1195 1216 - io->task_registered_buffers = 1; 1217 - io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd); 1218 - io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG; 1219 - return true; 1196 + return AUTO_BUF_REG_OK; 1220 1197 } 1221 1198 1222 - static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq, 1223 - struct request *req, struct ublk_io *io, 1224 - struct io_uring_cmd *cmd, 1225 - unsigned int issue_flags) 1199 + static void 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) 1226 1202 { 1227 - ublk_init_req_ref(ubq, io); 1228 - if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) 1229 - return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags); 1203 + enum auto_buf_reg_res res = __ublk_do_auto_buf_reg(ubq, req, io, cmd, 1204 + issue_flags); 1230 1205 1231 - return true; 1206 + if (res != AUTO_BUF_REG_FAIL) { 1207 + ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res); 1208 + io_uring_cmd_done(cmd, UBLK_IO_RES_OK, issue_flags); 1209 + } 1232 1210 } 1233 1211 1234 1212 static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req, ··· 1299 1281 if (!ublk_start_io(ubq, req, io)) 1300 1282 return; 1301 1283 1302 - if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags)) 1284 + if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) { 1285 + ublk_do_auto_buf_reg(ubq, req, io, io->cmd, issue_flags); 1286 + } else { 1287 + ublk_init_req_ref(ubq, io); 1303 1288 ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags); 1289 + } 1304 1290 } 1305 1291 1306 1292 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,