Skip to content

Commit a236346

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: take io_opt and io_min into account for max_sectors
The soft max_sectors limit is normally capped by the hardware limits and an arbitrary upper limit enforced by the kernel, but can be modified by the user. A few drivers want to increase this limit (nbd, rbd) or adjust it up or down based on hardware capabilities (sd). Change blk_validate_limits to default max_sectors to the optimal I/O size, or upgrade it to the preferred minimal I/O size if that is larger than the kernel default if no optimal I/O size is provided based on the logic in the SD driver. This keeps the existing kernel default for drivers that do not provide an io_opt or very big io_min value, but picks a much more useful default for those who provide these hints, and allows to remove the hacks to set the user max_sectors limit in nbd, rbd and sd. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Reviewed-by: Damien Le Moal <[email protected]> Acked-by: Ilya Dryomov <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent a00d4bf commit a236346

File tree

4 files changed

+13
-26
lines changed

4 files changed

+13
-26
lines changed

block/blk-settings.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ static int blk_validate_limits(struct queue_limits *lim)
153153
if (lim->max_user_sectors < PAGE_SIZE / SECTOR_SIZE)
154154
return -EINVAL;
155155
lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors);
156+
} else if (lim->io_opt) {
157+
lim->max_sectors =
158+
min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT);
159+
} else if (lim->io_min &&
160+
lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) {
161+
lim->max_sectors =
162+
min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT);
156163
} else {
157164
lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP);
158165
}

drivers/block/nbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
18081808
{
18091809
struct queue_limits lim = {
18101810
.max_hw_sectors = 65536,
1811-
.max_user_sectors = 256,
1811+
.io_opt = 256 << SECTOR_SHIFT,
18121812
.max_segments = USHRT_MAX,
18131813
.max_segment_size = UINT_MAX,
18141814
};

drivers/block/rbd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4954,7 +4954,6 @@ static int rbd_init_disk(struct rbd_device *rbd_dev)
49544954
rbd_dev->layout.object_size * rbd_dev->layout.stripe_count;
49554955
struct queue_limits lim = {
49564956
.max_hw_sectors = objset_bytes >> SECTOR_SHIFT,
4957-
.max_user_sectors = objset_bytes >> SECTOR_SHIFT,
49584957
.io_opt = objset_bytes,
49594958
.io_min = rbd_dev->opts->alloc_size,
49604959
.max_segments = USHRT_MAX,

drivers/scsi/sd.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,7 +3593,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
35933593
struct request_queue *q = sdkp->disk->queue;
35943594
sector_t old_capacity = sdkp->capacity;
35953595
unsigned char *buffer;
3596-
unsigned int dev_max, rw_max;
3596+
unsigned int dev_max;
35973597

35983598
SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
35993599
"sd_revalidate_disk\n"));
@@ -3675,34 +3675,15 @@ static int sd_revalidate_disk(struct gendisk *disk)
36753675
else
36763676
blk_queue_io_min(sdkp->disk->queue, 0);
36773677

3678-
if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
3679-
q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3680-
rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
3681-
} else {
3682-
q->limits.io_opt = 0;
3683-
rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
3684-
(sector_t)BLK_DEF_MAX_SECTORS_CAP);
3685-
}
3686-
36873678
/*
36883679
* Limit default to SCSI host optimal sector limit if set. There may be
36893680
* an impact on performance for when the size of a request exceeds this
36903681
* host limit.
36913682
*/
3692-
rw_max = min_not_zero(rw_max, sdp->host->opt_sectors);
3693-
3694-
/* Do not exceed controller limit */
3695-
rw_max = min(rw_max, queue_max_hw_sectors(q));
3696-
3697-
/*
3698-
* Only update max_sectors if previously unset or if the current value
3699-
* exceeds the capabilities of the hardware.
3700-
*/
3701-
if (sdkp->first_scan ||
3702-
q->limits.max_sectors > q->limits.max_dev_sectors ||
3703-
q->limits.max_sectors > q->limits.max_hw_sectors) {
3704-
q->limits.max_sectors = rw_max;
3705-
q->limits.max_user_sectors = rw_max;
3683+
q->limits.io_opt = sdp->host->opt_sectors << SECTOR_SHIFT;
3684+
if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
3685+
q->limits.io_opt = min_not_zero(q->limits.io_opt,
3686+
logical_to_bytes(sdp, sdkp->opt_xfer_blocks));
37063687
}
37073688

37083689
sdkp->first_scan = 0;

0 commit comments

Comments
 (0)