Skip to content

Commit ca369d5

Browse files
block/sd: Fix device-imposed transfer length limits
Commit 4f258a4 ("sd: Fix maximum I/O size for BLOCK_PC requests") had the unfortunate side-effect of removing an implicit clamp to BLK_DEF_MAX_SECTORS for REQ_TYPE_FS requests in the block layer code. This caused problems for some SMR drives. Debugging this issue revealed a few problems with the existing infrastructure since the block layer didn't know how to deal with device-imposed limits, only limits set by the I/O controller. - Introduce a new queue limit, max_dev_sectors, which is used by the ULD to signal the maximum sectors for a REQ_TYPE_FS request. - Ensure that max_dev_sectors is correctly stacked and taken into account when overriding max_sectors through sysfs. - Rework sd_read_block_limits() so it saves the max_xfer and opt_xfer values for later processing. - In sd_revalidate() set the queue's max_dev_sectors based on the MAXIMUM TRANSFER LENGTH value in the Block Limits VPD. If this value is not reported, fall back to a cap based on the CDB TRANSFER LENGTH field size. - In sd_revalidate(), use OPTIMAL TRANSFER LENGTH from the Block Limits VPD--if reported and sane--to signal the preferred device transfer size for FS requests. Otherwise use BLK_DEF_MAX_SECTORS. - blk_limits_max_hw_sectors() is no longer used and can be removed. Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=93581 Reviewed-by: Christoph Hellwig <hch@lst.de> Tested-by: sweeneygj@gmx.com Tested-by: Arzeets <anatol.pomozov@gmail.com> Tested-by: David Eisner <david.eisner@oriel.oxon.org> Tested-by: Mario Kicherer <dev@kicherer.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent f7f9f26 commit ca369d5

5 files changed

Lines changed: 51 additions & 37 deletions

File tree

block/blk-settings.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ void blk_set_default_limits(struct queue_limits *lim)
9191
lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
9292
lim->virt_boundary_mask = 0;
9393
lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
94-
lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
94+
lim->max_sectors = lim->max_dev_sectors = lim->max_hw_sectors =
95+
BLK_SAFE_MAX_SECTORS;
9596
lim->chunk_sectors = 0;
9697
lim->max_write_same_sectors = 0;
9798
lim->max_discard_sectors = 0;
@@ -127,6 +128,7 @@ void blk_set_stacking_limits(struct queue_limits *lim)
127128
lim->max_hw_sectors = UINT_MAX;
128129
lim->max_segment_size = UINT_MAX;
129130
lim->max_sectors = UINT_MAX;
131+
lim->max_dev_sectors = UINT_MAX;
130132
lim->max_write_same_sectors = UINT_MAX;
131133
}
132134
EXPORT_SYMBOL(blk_set_stacking_limits);
@@ -214,8 +216,8 @@ void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
214216
EXPORT_SYMBOL(blk_queue_bounce_limit);
215217

216218
/**
217-
* blk_limits_max_hw_sectors - set hard and soft limit of max sectors for request
218-
* @limits: the queue limits
219+
* blk_queue_max_hw_sectors - set max sectors for a request for this queue
220+
* @q: the request queue for the device
219221
* @max_hw_sectors: max hardware sectors in the usual 512b unit
220222
*
221223
* Description:
@@ -224,36 +226,29 @@ EXPORT_SYMBOL(blk_queue_bounce_limit);
224226
* the device driver based upon the capabilities of the I/O
225227
* controller.
226228
*
229+
* max_dev_sectors is a hard limit imposed by the storage device for
230+
* READ/WRITE requests. It is set by the disk driver.
231+
*
227232
* max_sectors is a soft limit imposed by the block layer for
228233
* filesystem type requests. This value can be overridden on a
229234
* per-device basis in /sys/block/<device>/queue/max_sectors_kb.
230235
* The soft limit can not exceed max_hw_sectors.
231236
**/
232-
void blk_limits_max_hw_sectors(struct queue_limits *limits, unsigned int max_hw_sectors)
237+
void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
233238
{
239+
struct queue_limits *limits = &q->limits;
240+
unsigned int max_sectors;
241+
234242
if ((max_hw_sectors << 9) < PAGE_CACHE_SIZE) {
235243
max_hw_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
236244
printk(KERN_INFO "%s: set to minimum %d\n",
237245
__func__, max_hw_sectors);
238246
}
239247

240248
limits->max_hw_sectors = max_hw_sectors;
241-
limits->max_sectors = min_t(unsigned int, max_hw_sectors,
242-
BLK_DEF_MAX_SECTORS);
243-
}
244-
EXPORT_SYMBOL(blk_limits_max_hw_sectors);
245-
246-
/**
247-
* blk_queue_max_hw_sectors - set max sectors for a request for this queue
248-
* @q: the request queue for the device
249-
* @max_hw_sectors: max hardware sectors in the usual 512b unit
250-
*
251-
* Description:
252-
* See description for blk_limits_max_hw_sectors().
253-
**/
254-
void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
255-
{
256-
blk_limits_max_hw_sectors(&q->limits, max_hw_sectors);
249+
max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
250+
max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
251+
limits->max_sectors = max_sectors;
257252
}
258253
EXPORT_SYMBOL(blk_queue_max_hw_sectors);
259254

@@ -527,6 +522,7 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
527522

528523
t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
529524
t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
525+
t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
530526
t->max_write_same_sectors = min(t->max_write_same_sectors,
531527
b->max_write_same_sectors);
532528
t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);

block/blk-sysfs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
205205
if (ret < 0)
206206
return ret;
207207

208+
max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
209+
q->limits.max_dev_sectors >> 1);
210+
208211
if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
209212
return -EINVAL;
210213

drivers/scsi/sd.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,11 +2238,8 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
22382238
}
22392239
}
22402240

2241-
if (sdkp->capacity > 0xffffffff) {
2241+
if (sdkp->capacity > 0xffffffff)
22422242
sdp->use_16_for_rw = 1;
2243-
sdkp->max_xfer_blocks = SD_MAX_XFER_BLOCKS;
2244-
} else
2245-
sdkp->max_xfer_blocks = SD_DEF_XFER_BLOCKS;
22462243

22472244
/* Rescale capacity to 512-byte units */
22482245
if (sector_size == 4096)
@@ -2559,22 +2556,18 @@ static void sd_read_block_limits(struct scsi_disk *sdkp)
25592556
{
25602557
unsigned int sector_sz = sdkp->device->sector_size;
25612558
const int vpd_len = 64;
2562-
u32 max_xfer_length;
25632559
unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
25642560

25652561
if (!buffer ||
25662562
/* Block Limits VPD */
25672563
scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
25682564
goto out;
25692565

2570-
max_xfer_length = get_unaligned_be32(&buffer[8]);
2571-
if (max_xfer_length)
2572-
sdkp->max_xfer_blocks = max_xfer_length;
2573-
25742566
blk_queue_io_min(sdkp->disk->queue,
25752567
get_unaligned_be16(&buffer[6]) * sector_sz);
2576-
blk_queue_io_opt(sdkp->disk->queue,
2577-
get_unaligned_be32(&buffer[12]) * sector_sz);
2568+
2569+
sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
2570+
sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
25782571

25792572
if (buffer[3] == 0x3c) {
25802573
unsigned int lba_count, desc_count;
@@ -2723,6 +2716,11 @@ static int sd_try_extended_inquiry(struct scsi_device *sdp)
27232716
return 0;
27242717
}
27252718

2719+
static inline u32 logical_to_sectors(struct scsi_device *sdev, u32 blocks)
2720+
{
2721+
return blocks << (ilog2(sdev->sector_size) - 9);
2722+
}
2723+
27262724
/**
27272725
* sd_revalidate_disk - called the first time a new disk is seen,
27282726
* performs disk spin up, read_capacity, etc.
@@ -2732,8 +2730,9 @@ static int sd_revalidate_disk(struct gendisk *disk)
27322730
{
27332731
struct scsi_disk *sdkp = scsi_disk(disk);
27342732
struct scsi_device *sdp = sdkp->device;
2733+
struct request_queue *q = sdkp->disk->queue;
27352734
unsigned char *buffer;
2736-
unsigned int max_xfer;
2735+
unsigned int dev_max, rw_max;
27372736

27382737
SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
27392738
"sd_revalidate_disk\n"));
@@ -2781,11 +2780,26 @@ static int sd_revalidate_disk(struct gendisk *disk)
27812780
*/
27822781
sd_set_flush_flag(sdkp);
27832782

2784-
max_xfer = sdkp->max_xfer_blocks;
2785-
max_xfer <<= ilog2(sdp->sector_size) - 9;
2783+
/* Initial block count limit based on CDB TRANSFER LENGTH field size. */
2784+
dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
2785+
2786+
/* Some devices report a maximum block count for READ/WRITE requests. */
2787+
dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
2788+
q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
2789+
2790+
/*
2791+
* Use the device's preferred I/O size for reads and writes
2792+
* unless the reported value is unreasonably large (or garbage).
2793+
*/
2794+
if (sdkp->opt_xfer_blocks && sdkp->opt_xfer_blocks <= dev_max &&
2795+
sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS)
2796+
rw_max = q->limits.io_opt =
2797+
logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
2798+
else
2799+
rw_max = BLK_DEF_MAX_SECTORS;
27862800

2787-
sdkp->disk->queue->limits.max_sectors =
2788-
min_not_zero(queue_max_hw_sectors(sdkp->disk->queue), max_xfer);
2801+
/* Combine with controller limits */
2802+
q->limits.max_sectors = min(rw_max, queue_max_hw_sectors(q));
27892803

27902804
set_capacity(disk, sdkp->capacity);
27912805
sd_config_write_same(sdkp);

drivers/scsi/sd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct scsi_disk {
6767
atomic_t openers;
6868
sector_t capacity; /* size in 512-byte sectors */
6969
u32 max_xfer_blocks;
70+
u32 opt_xfer_blocks;
7071
u32 max_ws_blocks;
7172
u32 max_unmap_blocks;
7273
u32 unmap_granularity;

include/linux/blkdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ struct queue_limits {
253253
unsigned long virt_boundary_mask;
254254

255255
unsigned int max_hw_sectors;
256+
unsigned int max_dev_sectors;
256257
unsigned int chunk_sectors;
257258
unsigned int max_sectors;
258259
unsigned int max_segment_size;
@@ -948,7 +949,6 @@ extern struct request_queue *blk_init_allocated_queue(struct request_queue *,
948949
extern void blk_cleanup_queue(struct request_queue *);
949950
extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
950951
extern void blk_queue_bounce_limit(struct request_queue *, u64);
951-
extern void blk_limits_max_hw_sectors(struct queue_limits *, unsigned int);
952952
extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
953953
extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
954954
extern void blk_queue_max_segments(struct request_queue *, unsigned short);

0 commit comments

Comments
 (0)