On Wed, Apr 16, 2025 at 01:46:06PM -0600, Uday Shankar wrote: > We now allow multiple tasks to operate on I/Os belonging to the same > queue concurrently. This means that any writes to ublk_queue in the I/O > path are potential sources of data races. Try to prevent these by > marking ublk_queue pointers as const when handling COMMIT_AND_FETCH. > Move the logic for this command into its own function > ublk_commit_and_fetch. Also open code ublk_commit_completion in > ublk_commit_and_fetch to reduce the number of parameters/avoid a > redundant lookup. > > Suggested-by: Ming Lei <ming.lei@xxxxxxxxxx> > Signed-off-by: Uday Shankar <ushankar@xxxxxxxxxxxxxxx> > Reviewed-by: Caleb Sander Mateos <csander@xxxxxxxxxxxxxxx> > --- > drivers/block/ublk_drv.c | 91 +++++++++++++++++++++++------------------------- > 1 file changed, 43 insertions(+), 48 deletions(-) > > diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c > index 4e4863d873e6593ad94c72cbf971f792df5795ae..b44cbcbcc9d1735c398dc9ac7e93f4c8736b9201 100644 > --- a/drivers/block/ublk_drv.c > +++ b/drivers/block/ublk_drv.c > @@ -1518,30 +1518,6 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma) > return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); > } > > -static void ublk_commit_completion(struct ublk_device *ub, > - const struct ublksrv_io_cmd *ub_cmd) > -{ > - u32 qid = ub_cmd->q_id, tag = ub_cmd->tag; > - struct ublk_queue *ubq = ublk_get_queue(ub, qid); > - struct ublk_io *io = &ubq->ios[tag]; > - struct request *req; > - > - /* now this cmd slot is owned by nbd driver */ > - io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV; > - io->res = ub_cmd->result; > - > - /* find the io request and complete */ > - req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag); > - if (WARN_ON_ONCE(unlikely(!req))) > - return; > - > - if (req_op(req) == REQ_OP_ZONE_APPEND) > - req->__sector = ub_cmd->zone_append_lba; > - > - if (likely(!blk_should_fake_timeout(req->q))) > - ublk_put_req_ref(ubq, req); > -} > - > /* > * Called from io task context via cancel fn, meantime quiesce ublk > * blk-mq queue, so we are called exclusively with blk-mq and io task > @@ -1918,6 +1894,45 @@ static int ublk_unregister_io_buf(struct io_uring_cmd *cmd, > return io_buffer_unregister_bvec(cmd, index, issue_flags); > } > > +static int ublk_commit_and_fetch(const struct ublk_queue *ubq, > + struct ublk_io *io, struct io_uring_cmd *cmd, > + const struct ublksrv_io_cmd *ub_cmd, > + struct request *req) request argument can be removed by getting it inside this function directly, and it is helpful to switch to retrieve & store it from 'ublk_io' later. Otherwise, looks nice cleanup. thanks, Ming