Skip to content

Commit cc07162

Browse files
bonzinikevmw
authored andcommitted
block: introduce max_hw_iov for use in scsi-generic
Linux limits the size of iovecs to 1024 (UIO_MAXIOV in the kernel sources, IOV_MAX in POSIX). Because of this, on some host adapters requests with many iovecs are rejected with -EINVAL by the io_submit() or readv()/writev() system calls. In fact, the same limit applies to SG_IO as well. To fix both the EINVAL and the possible performance issues from using fewer iovecs than allowed by Linux (some HBAs have max_segments as low as 128), introduce a separate entry in BlockLimits to hold the max_segments value from sysfs. This new limit is used only for SG_IO and clamped to bs->bl.max_iov anyway, just like max_hw_transfer is clamped to bs->bl.max_transfer. Reported-by: Halil Pasic <[email protected]> Cc: Hanna Reitz <[email protected]> Cc: Kevin Wolf <[email protected]> Cc: [email protected] Cc: [email protected] Fixes: 1847346 ("file-posix: try BLKSECTGET on block devices too, do not round to power of 2", 2021-06-25) Signed-off-by: Paolo Bonzini <[email protected]> Message-Id: <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent d318fc2 commit cc07162

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-2
lines changed

block/block-backend.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,12 @@ uint32_t blk_get_max_transfer(BlockBackend *blk)
19861986
return ROUND_DOWN(max, blk_get_request_alignment(blk));
19871987
}
19881988

1989+
int blk_get_max_hw_iov(BlockBackend *blk)
1990+
{
1991+
return MIN_NON_ZERO(blk->root->bs->bl.max_hw_iov,
1992+
blk->root->bs->bl.max_iov);
1993+
}
1994+
19891995
int blk_get_max_iov(BlockBackend *blk)
19901996
{
19911997
return blk->root->bs->bl.max_iov;

block/file-posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
12731273

12741274
ret = hdev_get_max_segments(s->fd, &st);
12751275
if (ret > 0) {
1276-
bs->bl.max_iov = ret;
1276+
bs->bl.max_hw_iov = ret;
12771277
}
12781278
}
12791279
}

block/io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
136136
dst->min_mem_alignment = MAX(dst->min_mem_alignment,
137137
src->min_mem_alignment);
138138
dst->max_iov = MIN_NON_ZERO(dst->max_iov, src->max_iov);
139+
dst->max_hw_iov = MIN_NON_ZERO(dst->max_hw_iov, src->max_hw_iov);
139140
}
140141

141142
typedef struct BdrvRefreshLimitsState {

hw/scsi/scsi-generic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static int scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s, int len)
180180
page = r->req.cmd.buf[2];
181181
if (page == 0xb0) {
182182
uint64_t max_transfer = blk_get_max_hw_transfer(s->conf.blk);
183-
uint32_t max_iov = blk_get_max_iov(s->conf.blk);
183+
uint32_t max_iov = blk_get_max_hw_iov(s->conf.blk);
184184

185185
assert(max_transfer);
186186
max_transfer = MIN_NON_ZERO(max_transfer, max_iov * qemu_real_host_page_size)

include/block/block_int.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,13 @@ typedef struct BlockLimits {
718718
*/
719719
uint64_t max_hw_transfer;
720720

721+
/* Maximal number of scatter/gather elements allowed by the hardware.
722+
* Applies whenever transfers to the device bypass the kernel I/O
723+
* scheduler, for example with SG_IO. If larger than max_iov
724+
* or if zero, blk_get_max_hw_iov will fall back to max_iov.
725+
*/
726+
int max_hw_iov;
727+
721728
/* memory alignment, in bytes so that no bounce buffer is needed */
722729
size_t min_mem_alignment;
723730

include/sysemu/block-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ uint32_t blk_get_request_alignment(BlockBackend *blk);
211211
uint32_t blk_get_max_transfer(BlockBackend *blk);
212212
uint64_t blk_get_max_hw_transfer(BlockBackend *blk);
213213
int blk_get_max_iov(BlockBackend *blk);
214+
int blk_get_max_hw_iov(BlockBackend *blk);
214215
void blk_set_guest_block_size(BlockBackend *blk, int align);
215216
void *blk_try_blockalign(BlockBackend *blk, size_t size);
216217
void *blk_blockalign(BlockBackend *blk, size_t size);

0 commit comments

Comments
 (0)