Skip to content

Commit 2d0e88f

Browse files
calebsanderaxboe
authored andcommitted
io_uring/rsrc: don't use blk_rq_nr_phys_segments() as number of bvecs
io_buffer_register_bvec() currently uses blk_rq_nr_phys_segments() as the number of bvecs in the request. However, bvecs may be split into multiple segments depending on the queue limits. Thus, the number of segments may overestimate the number of bvecs. For ublk devices, the only current users of io_buffer_register_bvec(), virt_boundary_mask, seg_boundary_mask, max_segments, and max_segment_size can all be set arbitrarily by the ublk server process. Set imu->nr_bvecs based on the number of bvecs the rq_for_each_bvec() loop actually yields. However, continue using blk_rq_nr_phys_segments() as an upper bound on the number of bvecs when allocating imu to avoid needing to iterate the bvecs a second time. Link: https://lore.kernel.org/io-uring/[email protected]/ Signed-off-by: Caleb Sander Mateos <[email protected]> Fixes: 27cb27b ("io_uring: add support for kernel registered bvecs") Reviewed-by: Ming Lei <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 6a77267 commit 2d0e88f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

io_uring/rsrc.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,8 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,
943943
struct req_iterator rq_iter;
944944
struct io_mapped_ubuf *imu;
945945
struct io_rsrc_node *node;
946-
struct bio_vec bv, *bvec;
947-
u16 nr_bvecs;
946+
struct bio_vec bv;
947+
unsigned int nr_bvecs = 0;
948948
int ret = 0;
949949

950950
io_ring_submit_lock(ctx, issue_flags);
@@ -965,8 +965,11 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,
965965
goto unlock;
966966
}
967967

968-
nr_bvecs = blk_rq_nr_phys_segments(rq);
969-
imu = io_alloc_imu(ctx, nr_bvecs);
968+
/*
969+
* blk_rq_nr_phys_segments() may overestimate the number of bvecs
970+
* but avoids needing to iterate over the bvecs
971+
*/
972+
imu = io_alloc_imu(ctx, blk_rq_nr_phys_segments(rq));
970973
if (!imu) {
971974
kfree(node);
972975
ret = -ENOMEM;
@@ -977,16 +980,15 @@ int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,
977980
imu->len = blk_rq_bytes(rq);
978981
imu->acct_pages = 0;
979982
imu->folio_shift = PAGE_SHIFT;
980-
imu->nr_bvecs = nr_bvecs;
981983
refcount_set(&imu->refs, 1);
982984
imu->release = release;
983985
imu->priv = rq;
984986
imu->is_kbuf = true;
985987
imu->dir = 1 << rq_data_dir(rq);
986988

987-
bvec = imu->bvec;
988989
rq_for_each_bvec(bv, rq, rq_iter)
989-
*bvec++ = bv;
990+
imu->bvec[nr_bvecs++] = bv;
991+
imu->nr_bvecs = nr_bvecs;
990992

991993
node->buf = imu;
992994
data->nodes[index] = node;

0 commit comments

Comments
 (0)