scsi: sd: Fix optimal I/O size for devices that change reported values

Some USB bridge devices will return a default set of characteristics during
initialization. And then, once an attached drive has spun up, substitute
the actual parameters reported by the drive. According to the SCSI spec,
the device should return a UNIT ATTENTION in case any reported parameters
change. But in this case the change is made silently after a small window
where default values are reported.

Commit a83da8a4509d ("scsi: sd: Optimal I/O size should be a multiple of
physical block size") validated the reported optimal I/O size against the
physical block size to overcome problems with devices reporting nonsensical
transfer sizes. However, this validation did not account for the fact that
aforementioned devices will return default values during a brief window
during spin-up. The subsequent change in reported characteristics would
invalidate the checking that had previously been performed.

Unset a previously configured optimal I/O size should the sanity checking
fail on subsequent revalidate attempts.

Link: https://lore.kernel.org/r/33fb522e-4f61-1b76-914f-c9e6a3553c9b@gmail.com
Cc: Bryan Gurney <bgurney@redhat.com>
Cc: <stable@vger.kernel.org>
Reported-by: Bernhard Sulzer <micraft.b@gmail.com>
Tested-by: Bernhard Sulzer <micraft.b@gmail.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

Changed files
+3 -1
drivers
scsi
+3 -1
drivers/scsi/sd.c
··· 3169 3169 if (sd_validate_opt_xfer_size(sdkp, dev_max)) { 3170 3170 q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks); 3171 3171 rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks); 3172 - } else 3172 + } else { 3173 + q->limits.io_opt = 0; 3173 3174 rw_max = min_not_zero(logical_to_sectors(sdp, dev_max), 3174 3175 (sector_t)BLK_DEF_MAX_SECTORS); 3176 + } 3175 3177 3176 3178 /* Do not exceed controller limit */ 3177 3179 rw_max = min(rw_max, queue_max_hw_sectors(q));