]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.19.31/scsi-sd-optimal-i-o-size-should-be-a-multiple-of-physical-block-size.patch
Linux 4.19.31
[thirdparty/kernel/stable-queue.git] / releases / 4.19.31 / scsi-sd-optimal-i-o-size-should-be-a-multiple-of-physical-block-size.patch
1 From a83da8a4509d3ebfe03bb7fffce022e4d5d4764f Mon Sep 17 00:00:00 2001
2 From: "Martin K. Petersen" <martin.petersen@oracle.com>
3 Date: Tue, 12 Feb 2019 16:21:05 -0500
4 Subject: scsi: sd: Optimal I/O size should be a multiple of physical block size
5
6 From: Martin K. Petersen <martin.petersen@oracle.com>
7
8 commit a83da8a4509d3ebfe03bb7fffce022e4d5d4764f upstream.
9
10 It was reported that some devices report an OPTIMAL TRANSFER LENGTH of
11 0xFFFF blocks. That looks bogus, especially for a device with a
12 4096-byte physical block size.
13
14 Ignore OPTIMAL TRANSFER LENGTH if it is not a multiple of the device's
15 reported physical block size.
16
17 To make the sanity checking conditionals more readable--and to
18 facilitate printing warnings--relocate the checking to a helper
19 function. No functional change aside from the printks.
20
21 Cc: <stable@vger.kernel.org>
22 Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=199759
23 Reported-by: Christoph Anton Mitterer <calestyo@scientia.net>
24 Reviewed-by: Christoph Hellwig <hch@lst.de>
25 Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
26 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
27
28 ---
29 drivers/scsi/sd.c | 59 +++++++++++++++++++++++++++++++++++++++++++++---------
30 1 file changed, 50 insertions(+), 9 deletions(-)
31
32 --- a/drivers/scsi/sd.c
33 +++ b/drivers/scsi/sd.c
34 @@ -3066,6 +3066,55 @@ static void sd_read_security(struct scsi
35 sdkp->security = 1;
36 }
37
38 +/*
39 + * Determine the device's preferred I/O size for reads and writes
40 + * unless the reported value is unreasonably small, large, not a
41 + * multiple of the physical block size, or simply garbage.
42 + */
43 +static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp,
44 + unsigned int dev_max)
45 +{
46 + struct scsi_device *sdp = sdkp->device;
47 + unsigned int opt_xfer_bytes =
48 + logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
49 +
50 + if (sdkp->opt_xfer_blocks > dev_max) {
51 + sd_first_printk(KERN_WARNING, sdkp,
52 + "Optimal transfer size %u logical blocks " \
53 + "> dev_max (%u logical blocks)\n",
54 + sdkp->opt_xfer_blocks, dev_max);
55 + return false;
56 + }
57 +
58 + if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) {
59 + sd_first_printk(KERN_WARNING, sdkp,
60 + "Optimal transfer size %u logical blocks " \
61 + "> sd driver limit (%u logical blocks)\n",
62 + sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS);
63 + return false;
64 + }
65 +
66 + if (opt_xfer_bytes < PAGE_SIZE) {
67 + sd_first_printk(KERN_WARNING, sdkp,
68 + "Optimal transfer size %u bytes < " \
69 + "PAGE_SIZE (%u bytes)\n",
70 + opt_xfer_bytes, (unsigned int)PAGE_SIZE);
71 + return false;
72 + }
73 +
74 + if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) {
75 + sd_first_printk(KERN_WARNING, sdkp,
76 + "Optimal transfer size %u bytes not a " \
77 + "multiple of physical block size (%u bytes)\n",
78 + opt_xfer_bytes, sdkp->physical_block_size);
79 + return false;
80 + }
81 +
82 + sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n",
83 + opt_xfer_bytes);
84 + return true;
85 +}
86 +
87 /**
88 * sd_revalidate_disk - called the first time a new disk is seen,
89 * performs disk spin up, read_capacity, etc.
90 @@ -3144,15 +3193,7 @@ static int sd_revalidate_disk(struct gen
91 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
92 q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
93
94 - /*
95 - * Determine the device's preferred I/O size for reads and writes
96 - * unless the reported value is unreasonably small, large, or
97 - * garbage.
98 - */
99 - if (sdkp->opt_xfer_blocks &&
100 - sdkp->opt_xfer_blocks <= dev_max &&
101 - sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
102 - logical_to_bytes(sdp, sdkp->opt_xfer_blocks) >= PAGE_SIZE) {
103 + if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
104 q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
105 rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
106 } else