Skip to content

Commit a83da8a

Browse files
scsi: sd: Optimal I/O size should be a multiple of physical block size
It was reported that some devices report an OPTIMAL TRANSFER LENGTH of 0xFFFF blocks. That looks bogus, especially for a device with a 4096-byte physical block size. Ignore OPTIMAL TRANSFER LENGTH if it is not a multiple of the device's reported physical block size. To make the sanity checking conditionals more readable--and to facilitate printing warnings--relocate the checking to a helper function. No functional change aside from the printks. Cc: <stable@vger.kernel.org> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=199759 Reported-by: Christoph Anton Mitterer <calestyo@scientia.net> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent d1420f2 commit a83da8a

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

drivers/scsi/sd.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3061,6 +3061,55 @@ static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)
30613061
sdkp->security = 1;
30623062
}
30633063

3064+
/*
3065+
* Determine the device's preferred I/O size for reads and writes
3066+
* unless the reported value is unreasonably small, large, not a
3067+
* multiple of the physical block size, or simply garbage.
3068+
*/
3069+
static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp,
3070+
unsigned int dev_max)
3071+
{
3072+
struct scsi_device *sdp = sdkp->device;
3073+
unsigned int opt_xfer_bytes =
3074+
logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3075+
3076+
if (sdkp->opt_xfer_blocks > dev_max) {
3077+
sd_first_printk(KERN_WARNING, sdkp,
3078+
"Optimal transfer size %u logical blocks " \
3079+
"> dev_max (%u logical blocks)\n",
3080+
sdkp->opt_xfer_blocks, dev_max);
3081+
return false;
3082+
}
3083+
3084+
if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) {
3085+
sd_first_printk(KERN_WARNING, sdkp,
3086+
"Optimal transfer size %u logical blocks " \
3087+
"> sd driver limit (%u logical blocks)\n",
3088+
sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS);
3089+
return false;
3090+
}
3091+
3092+
if (opt_xfer_bytes < PAGE_SIZE) {
3093+
sd_first_printk(KERN_WARNING, sdkp,
3094+
"Optimal transfer size %u bytes < " \
3095+
"PAGE_SIZE (%u bytes)\n",
3096+
opt_xfer_bytes, (unsigned int)PAGE_SIZE);
3097+
return false;
3098+
}
3099+
3100+
if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) {
3101+
sd_first_printk(KERN_WARNING, sdkp,
3102+
"Optimal transfer size %u bytes not a " \
3103+
"multiple of physical block size (%u bytes)\n",
3104+
opt_xfer_bytes, sdkp->physical_block_size);
3105+
return false;
3106+
}
3107+
3108+
sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n",
3109+
opt_xfer_bytes);
3110+
return true;
3111+
}
3112+
30643113
/**
30653114
* sd_revalidate_disk - called the first time a new disk is seen,
30663115
* performs disk spin up, read_capacity, etc.
@@ -3130,15 +3179,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
31303179
dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
31313180
q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
31323181

3133-
/*
3134-
* Determine the device's preferred I/O size for reads and writes
3135-
* unless the reported value is unreasonably small, large, or
3136-
* garbage.
3137-
*/
3138-
if (sdkp->opt_xfer_blocks &&
3139-
sdkp->opt_xfer_blocks <= dev_max &&
3140-
sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
3141-
logical_to_bytes(sdp, sdkp->opt_xfer_blocks) >= PAGE_SIZE) {
3182+
if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
31423183
q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
31433184
rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
31443185
} else

0 commit comments

Comments
 (0)