Skip to content

Commit be8fcd4

Browse files
ionutnechita-wrmartinkpetersen
authored andcommitted
scsi: sas: Skip opt_sectors when DMA reports no real optimization hint
sas_host_setup() unconditionally sets shost->opt_sectors from dma_opt_mapping_size(). When the IOMMU is disabled or in passthrough mode and no DMA ops provide an opt_mapping_size callback, dma_opt_mapping_size() returns min(dma_max_mapping_size(), SIZE_MAX) which equals dma_max_mapping_size() — a hard upper bound, not an optimization hint. On a Dell PowerEdge R750 with mpt3sas (Broadcom SAS3816, FW 33.15.00.00) and intel_iommu=off the following values are observed: dma_opt_mapping_size() = dma_max_mapping_size() (no real hint) shost->max_sectors = 32767 opt_sectors = min(32767, huge >> 9) = 32767 optimal_io_size = 32767 << 9 = 16776704 → round_down(16776704, 4096) = 16773120 The SAS disk (SAMSUNG MZILT800HBHQ0D3) does not report an Optimal Transfer Length in VPD page B0, so sdkp->opt_xfer_blocks remains 0. sd_revalidate_disk() then uses min_not_zero(0, opt_sectors) = opt_sectors, propagating the bogus value into the block device's optimal_io_size (visible as OPT-IO = 16773120 in lsblk --topology). mkfs.xfs picks up optimal_io_size and minimum_io_size and computes: swidth = 16773120 / 4096 = 4095 sunit = 8192 / 4096 = 2 Since 4095 % 2 != 0, XFS rejects the geometry: SB stripe unit sanity check failed This makes it impossible to create XFS filesystems (e.g. for /var/lib/docker) during system bootstrap. Fix this by introducing a sas_dma_setup_opt_sectors() helper that sets opt_sectors only when dma_opt_mapping_size() is strictly less than dma_max_mapping_size(), indicating a genuine DMA optimization constraint. The helper computes min(opt_sectors, max_sectors) first, then rounds down to a power of two so that filesystem geometry calculations always produce clean results. When the two DMA values are equal, no backend provided a real hint, so opt_sectors stays at 0 ("no preference"). [mkp: implemented hch's suggestion] Fixes: 4cbfca5 ("scsi: scsi_transport_sas: cap shost opt_sectors according to DMA optimal limit") Cc: stable@vger.kernel.org Reviewed-by: John Garry <john.g.garry@oracle.com> Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://patch.msgid.link/20260519135238.373784-2-ionut.nechita@windriver.com Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 8c292e8 commit be8fcd4

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

drivers/scsi/scsi_transport_sas.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/module.h>
2828
#include <linux/jiffies.h>
2929
#include <linux/err.h>
30+
#include <linux/log2.h>
3031
#include <linux/slab.h>
3132
#include <linux/string.h>
3233
#include <linux/blkdev.h>
@@ -220,12 +221,45 @@ static int sas_bsg_initialize(struct Scsi_Host *shost, struct sas_rphy *rphy)
220221
* SAS host attributes
221222
*/
222223

224+
/*
225+
* Set shost->opt_sectors from the DMA optimal mapping size, but only
226+
* when dma_opt_mapping_size() is strictly less than dma_max_mapping_size(),
227+
* indicating a genuine optimization hint from an IOMMU or DMA backend.
228+
* When the two are equal (e.g. IOMMU disabled / passthrough), no real
229+
* hint exists, so leave opt_sectors at 0 to avoid bogus optimal_io_size
230+
* values that break filesystem geometry (e.g. mkfs.xfs stripe alignment).
231+
*/
232+
static void sas_dma_setup_opt_sectors(struct Scsi_Host *shost)
233+
{
234+
struct device *dma_dev = shost->dma_dev;
235+
size_t opt = dma_opt_mapping_size(dma_dev);
236+
size_t max = dma_max_mapping_size(dma_dev);
237+
unsigned int opt_sectors;
238+
239+
/* opt >= max means no real hint was provided by the DMA layer */
240+
if (opt >= max)
241+
return;
242+
243+
/* Clamp to max_sectors to avoid overflow in sector arithmetic */
244+
opt_sectors = min_t(unsigned int, opt >> SECTOR_SHIFT,
245+
shost->max_sectors);
246+
247+
/* Guard against zero before rounddown_pow_of_two() */
248+
if (!opt_sectors)
249+
return;
250+
251+
/*
252+
* Round down to power-of-two so filesystem geometry calculations
253+
* (e.g. XFS stripe width/unit) always produce clean divisors.
254+
*/
255+
shost->opt_sectors = rounddown_pow_of_two(opt_sectors);
256+
}
257+
223258
static int sas_host_setup(struct transport_container *tc, struct device *dev,
224259
struct device *cdev)
225260
{
226261
struct Scsi_Host *shost = dev_to_shost(dev);
227262
struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
228-
struct device *dma_dev = shost->dma_dev;
229263

230264
INIT_LIST_HEAD(&sas_host->rphy_list);
231265
mutex_init(&sas_host->lock);
@@ -237,10 +271,7 @@ static int sas_host_setup(struct transport_container *tc, struct device *dev,
237271
dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
238272
shost->host_no);
239273

240-
if (dma_dev->dma_mask) {
241-
shost->opt_sectors = min_t(unsigned int, shost->max_sectors,
242-
dma_opt_mapping_size(dma_dev) >> SECTOR_SHIFT);
243-
}
274+
sas_dma_setup_opt_sectors(shost);
244275

245276
return 0;
246277
}

0 commit comments

Comments
 (0)