Re: [PATCH v3] scsi: sas: skip opt_sectors when DMA reports no real optimization hint

From: Damien Le Moal

Date: Wed Mar 18 2026 - 23:28:47 EST


On 3/19/26 05:05, Ionut Nechita (Wind River) wrote:
> +static unsigned int sas_dma_opt_sectors(struct device *dma_dev,
> + unsigned int max_sectors)
> +{
> + size_t opt = dma_opt_mapping_size(dma_dev);
> + unsigned int opt_sectors;
> +
> + if (opt >= dma_max_mapping_size(dma_dev))
> + return 0;

I really do not understand this one. How can the optimal DMA mapping size be
larger than the maximum possible DMA size ?
If that happens, it is a driver bug, we should WARN_ONCE and return
dma_max_mapping_size(), no ?

> +
> + opt = rounddown_pow_of_two(opt);
> + opt_sectors = opt >> SECTOR_SHIFT;

if opt is super large, can this overflow the 32-bits opt_sectors ?

> +
> + return min(opt_sectors, max_sectors);
> +}
> +
> static int sas_host_setup(struct transport_container *tc, struct device *dev,
> struct device *cdev)
> {
> @@ -239,10 +268,9 @@ static int sas_host_setup(struct transport_container *tc, struct device *dev,
> dev_printk(KERN_ERR, dev, "fail to a bsg device %d\n",
> shost->host_no);
>
> - if (dma_dev->dma_mask) {
> - shost->opt_sectors = min_t(unsigned int, shost->max_sectors,
> - dma_opt_mapping_size(dma_dev) >> SECTOR_SHIFT);
> - }
> + if (dma_dev->dma_mask)
> + shost->opt_sectors = sas_dma_opt_sectors(dma_dev,
> + shost->max_sectors);

Splitting the line after the "=" would make this look nicer:

if (dma_dev->dma_mask)
shost->opt_sectors =
sas_dma_opt_sectors(dma_dev, shost->max_sectors);

>
> return 0;
> }


--
Damien Le Moal
Western Digital Research