Skip to contentSkip to conversation

nvme-pci: fix dma mapping leak on data setup error

PATCH Superseded Keith Busch 2 participants 2 messages May 19 NVMe

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-pci: fix dma mapping leak on data setup error

From: Keith Busch <[email protected]>

Whether we're using PRP or SGL format, we're leaking the initial DMA mapping during iteration if we fail to allocate the tracking descriptor. Unmap it directly; we can't use the helper because it depends on the tracking descriptor being successfully allocated.

---
I apparently have a lot more systems pushing memory utilization to the limits and finding interesting issues.

 drivers/nvme/host/pci.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/nvme/host/pci.c +12 -2
index 139a10cd687f9..0dbe9ad45b548 100644
@@ -1006,8 +1006,12 @@ static blk_status_t nvme_pci_setup_data_prp(struct request *req,
 	unsigned int prp_len, i;
 	__le64 *prp_list;
 
-	if (!nvme_pci_prp_save_mapping(req, nvmeq->dev->dev, iter))
+	if (!nvme_pci_prp_save_mapping(req, nvmeq->dev->dev, iter)) {
+		dma_unmap_phys(nvmeq->dev->dev, iter->addr, iter->len,
+			       rq_dma_dir(req),
+			       iod->flags & IOD_DATA_MMIO ? DMA_ATTR_MMIO : 0);
 		return iter->status;
+	}
 
 	/*
 	 * PRP1 always points to the start of the DMA transfers.
@@ -1112,6 +1116,7 @@ static blk_status_t nvme_pci_setup_data_prp(struct request *req,
 	dev_err_once(nvmeq->dev->dev,
 		"Incorrectly formed request for payload:%d nents:%d\n",
 		blk_rq_payload_bytes(req), blk_rq_nr_phys_segments(req));
+	nvme_unmap_data(req);
 	return BLK_STS_IOERR;
 }
 
@@ -1155,8 +1160,13 @@ static blk_status_t nvme_pci_setup_data_sgl(struct request *req,
 
 	sg_list = dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC,
 			&sgl_dma);
-	if (!sg_list)
+	if (!sg_list) {
+		dma_unmap_phys(nvmeq->dev->dev, iter->addr, iter->len,
+			       rq_dma_dir(req),
+			       iod->flags & IOD_DATA_MMIO ? DMA_ATTR_MMIO : 0);
 		return BLK_STS_RESOURCE;
+	}
+
 	iod->descriptors[iod->nr_descriptors++] = sg_list;
 
 	do {
Signature · 1 line
2.53.0-Meta
maintainer
#1 lore raw
Quoted 19 lines · Keith Busch
drivers/nvme/host/pci.c +6 -1
-	if (!nvme_pci_prp_save_mapping(req, nvmeq->dev->dev, iter))
+	if (!nvme_pci_prp_save_mapping(req, nvmeq->dev->dev, iter)) {
+		dma_unmap_phys(nvmeq->dev->dev, iter->addr, iter->len,
+			       rq_dma_dir(req),
+			       iod->flags & IOD_DATA_MMIO ? DMA_ATTR_MMIO : 0);
 		return iter->status;
+	}
 
 	/*
 	 * PRP1 always points to the start of the DMA transfers.
@@ -1112,6 +1116,7 @@ static blk_status_t nvme_pci_setup_data_prp(struct request *req,
 	dev_err_once(nvmeq->dev->dev,
 		"Incorrectly formed request for payload:%d nents:%d\n",
 		blk_rq_payload_bytes(req), blk_rq_nr_phys_segments(req));
+	nvme_unmap_data(req);
 	return BLK_STS_IOERR;
 }
 

drivers/nvme/host/pci.c static blk_status_t nvme_pci_setup_data_sgl(struct request *req, · line 1168

Quoted 10 lines
drivers/nvme/host/pci.c +5 -1
@@ -1155,8 +1160,13 @@ static blk_status_t nvme_pci_setup_data_sgl(struct request *req,
 
 	sg_list = dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC,
 			&sgl_dma);
-	if (!sg_list)
+	if (!sg_list) {
+		dma_unmap_phys(nvmeq->dev->dev, iter->addr, iter->len,
+			       rq_dma_dir(req),
+			       iod->flags & IOD_DATA_MMIO ? DMA_ATTR_MMIO : 0);
 		return BLK_STS_RESOURCE;
+	}

Oh, these need to check dma_need_unmap() first. I'll send a v2.