[PATCH v1 2/2] nvme-pci: handle dma_opt_mapping_size() returning 0
From: Ionut Nechita (Wind River)
Date: Mon Mar 16 2026 - 16:41:30 EST
From: Ionut Nechita <ionut.nechita@xxxxxxxxxxxxx>
After the previous commit, dma_opt_mapping_size() returns 0 when no DMA
backend provides an optimal mapping size hint (e.g. IOMMU in passthrough
mode with no ops->opt_mapping_size callback).
The NVMe PCI driver used min_t(u32, NVME_MAX_BYTES >> SECTOR_SHIFT,
dma_opt_mapping_size() >> 9) to cap max_hw_sectors. With a 0 return
value this would set max_hw_sectors to 0, which is invalid.
Guard the min_t so that max_hw_sectors is only capped when
dma_opt_mapping_size() provides a real hint. When it returns 0, fall
back to the existing NVME_MAX_BYTES >> SECTOR_SHIFT default.
Fixes: 3710e2b056cb ("nvme-pci: clamp max_hw_sectors based on DMA optimized limitation")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ionut Nechita <ionut.nechita@xxxxxxxxxxxxx>
---
drivers/nvme/host/pci.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index b78ba239c8ea8..dc148fb6eff28 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -3640,6 +3640,7 @@ static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
{
unsigned long quirks = id->driver_data;
int node = dev_to_node(&pdev->dev);
+ size_t dma_opt;
struct nvme_dev *dev;
struct quirk_entry *qentry;
int ret = -ENOMEM;
@@ -3691,12 +3692,16 @@ static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
dma_set_max_seg_size(&pdev->dev, 0xffffffff);
/*
- * Limit the max command size to prevent iod->sg allocations going
- * over a single page.
+ * Limit the max command size to prevent iod->sg allocations
+ * going over a single page. Only apply the DMA optimal mapping
+ * size limit when the DMA layer actually provides one (non-zero
+ * return from dma_opt_mapping_size()).
*/
- dev->ctrl.max_hw_sectors = min_t(u32,
- NVME_MAX_BYTES >> SECTOR_SHIFT,
- dma_opt_mapping_size(&pdev->dev) >> 9);
+ dev->ctrl.max_hw_sectors = NVME_MAX_BYTES >> SECTOR_SHIFT;
+ dma_opt = dma_opt_mapping_size(&pdev->dev);
+ if (dma_opt)
+ dev->ctrl.max_hw_sectors =
+ min_t(u32, dev->ctrl.max_hw_sectors, dma_opt >> 9);
dev->ctrl.max_segments = NVME_MAX_SEGS;
dev->ctrl.max_integrity_segments = 1;
return dev;
--
2.53.0