Skip to contentSkip to conversation

nvme-pci: Avoid leak if pci_p2pmem_virt_to_bus() returns null

PATCH In review Alan Mikhak 3 participants 5 messages Jun 24, 2019 – Jun 25, 2019 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-pci: Avoid leak if pci_p2pmem_virt_to_bus() returns null

Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() to free the memory it allocated using pci_alloc_p2pmem() in case pci_p2pmem_virt_to_bus() returns null.

Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() returned null which can happen if CONFIG_PCI_P2PDMA is not configured.

Signed-off-by Alan Mikhak <alan.mikhak at sifive.com>
---
 drivers/nvme/host/pci.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/host/pci.c +9 -5
index 524d6bd6d095..5dfa067f6506 100644
@@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
 
 	if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
 		nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
-		nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
-						nvmeq->sq_cmds);
-		if (nvmeq->sq_dma_addr) {
-			set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
-			return 0; 
+		if (nvmeq->sq_cmds) {
+			nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
+							nvmeq->sq_cmds);
+			if (nvmeq->sq_dma_addr) {
+				set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
+				return 0;
+			}
+
+			pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth));
 		}
 	}
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Alan Mikhak

Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() to free the memory it allocated using pci_alloc_p2pmem() in case pci_p2pmem_virt_to_bus() returns null.

Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() returned null which can happen if CONFIG_PCI_P2PDMA is not configured.

Can you

Quoted 18 lines
Signed-off-by Alan Mikhak <alan.mikhak at sifive.com>
---
 drivers/nvme/host/pci.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/host/pci.c +0 -4
index 524d6bd6d095..5dfa067f6506 100644
@@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
 
 	if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
 		nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
-		nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
-						nvmeq->sq_cmds);
-		if (nvmeq->sq_dma_addr) {
-			set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);

drivers/nvme/host/pci.c static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, · line 1467

Quoted 10 lines
drivers/nvme/host/pci.c +9 -1
-			return 0; 
+		if (nvmeq->sq_cmds) {
+			nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
+							nvmeq->sq_cmds);
+			if (nvmeq->sq_dma_addr) {
+				set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
+				return 0;
+			}
+
+			pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth));

pci_p2pmem_virt_to_bus should only fail when pci_p2pmem_virt_to_bus failed. That being said I think doing the error check on pci_alloc_p2pmem instead of relying on pci_p2pmem_virt_to_bus "passing through" the error seems odd, I'd rather check the pci_alloc_p2pmem return value directly.

#2 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Alan Mikhak

Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() to free the memory it allocated using pci_alloc_p2pmem() in case pci_p2pmem_virt_to_bus() returns null.

Quoted 6 lines
Quoted 3 lines

Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() returned null which can happen if CONFIG_PCI_P2PDMA is not configured.

Can you

I mean this patch makes sure not to call pci_free_p2pmem() if nothing was allocated by pci_alloc_p2pmem().

Quoted 26 lines
Quoted 26 lines
Signed-off-by Alan Mikhak <alan.mikhak at sifive.com>
---
 drivers/nvme/host/pci.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/host/pci.c +7 -5
index 524d6bd6d095..5dfa067f6506 100644
@@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
 
      if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
              nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
-             nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
-                                             nvmeq->sq_cmds);
-             if (nvmeq->sq_dma_addr) {
-                     set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
-                     return 0;
+             if (nvmeq->sq_cmds) {
+                     nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
+                                                     nvmeq->sq_cmds);
+                     if (nvmeq->sq_dma_addr) {
+                             set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
+                             return 0;
+                     }
Quoted 8 lines

drivers/nvme/host/pci.c static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, · line 1467

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+
+                     pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth));

pci_p2pmem_virt_to_bus should only fail when pci_p2pmem_virt_to_bus failed. That being said I think doing the error check on pci_alloc_p2pmem instead of relying on pci_p2pmem_virt_to_bus "passing through" the error seems odd, I'd rather check the pci_alloc_p2pmem return value directly.

Thanks Christoph. I could see the existing code should not leak but only after inspecting pci_p2pmem_virt_to_bus() and gen_pool_virt_to_phys(). I wondered what if these functions changed and broke the relation but that seems unlikely. Checking the return value directly may require less from the reader, if that's a good
outcome.

Alan

#3 lore raw
In reply to Alan Mikhak
Quoted 25 lines · Alan Mikhak

Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() to free the memory it allocated using pci_alloc_p2pmem() in case pci_p2pmem_virt_to_bus() returns null.

Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() returned null which can happen if CONFIG_PCI_P2PDMA is not configured.

Signed-off-by Alan Mikhak <alan.mikhak at sifive.com>
---
  drivers/nvme/host/pci.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/host/pci.c +0 -4
index 524d6bd6d095..5dfa067f6506 100644
@@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
 
  	if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
  		nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
-		nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
-						nvmeq->sq_cmds);
-		if (nvmeq->sq_dma_addr) {
-			set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);

drivers/nvme/host/pci.c static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, · line 1467

Quoted 10 lines
drivers/nvme/host/pci.c +9 -1
-			return 0;
+		if (nvmeq->sq_cmds) {
+			nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
+							nvmeq->sq_cmds);
+			if (nvmeq->sq_dma_addr) {
+				set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
+				return 0;
+			}
+
+			pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth));

Should the pointer be set to NULL here, just in case?

Quoted 4 lines
drivers/nvme/host/pci.c +0 -0
  		}
  	}
 
#4 lore raw
Quoted 33 lines · Kenneth
Quoted 29 lines · Alan Mikhak

Modify nvme_alloc_sq_cmds() to call pci_free_p2pmem() to free the memory it allocated using pci_alloc_p2pmem() in case pci_p2pmem_virt_to_bus() returns null.

Make sure not to call pci_free_p2pmem() if pci_alloc_p2pmem() returned null which can happen if CONFIG_PCI_P2PDMA is not configured.

Signed-off-by Alan Mikhak <alan.mikhak at sifive.com>
---
  drivers/nvme/host/pci.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/host/pci.c +3 -5
index 524d6bd6d095..5dfa067f6506 100644
@@ -1456,11 +1456,15 @@ static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
 
      if (qid && dev->cmb_use_sqes && (dev->cmbsz & NVME_CMBSZ_SQS)) {
              nvmeq->sq_cmds = pci_alloc_p2pmem(pdev, SQ_SIZE(depth));
-             nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
-                                             nvmeq->sq_cmds);
-             if (nvmeq->sq_dma_addr) {
-                     set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
-                     return 0;
+             if (nvmeq->sq_cmds) {
+                     nvmeq->sq_dma_addr = pci_p2pmem_virt_to_bus(pdev,
+                                                     nvmeq->sq_cmds);
Quoted 8 lines

drivers/nvme/host/pci.c static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq, · line 1467

Quoted 6 lines
drivers/nvme/host/pci.c +6 -0
+                     if (nvmeq->sq_dma_addr) {
+                             set_bit(NVMEQ_SQ_CMB, &nvmeq->flags);
+                             return 0;
+                     }
+
+                     pci_free_p2pmem(pdev, nvmeq->sq_cmds, SQ_SIZE(depth));

Should the pointer be set to NULL here, just in case?

Thanks Kenneth. The pointer gets immediately reassigned by the return value of the
code that follows. There is no intervening reference to it between the calls to pci_free_p2pmem() and dma_alloc_coherent(). It should be safe without setting it to NULL.

        nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, nvmeq->cq_size,
                                &nvmeq->sq_dma_addr, GFP_KERNEL);
Quoted 5 lines
Quoted 5 lines
drivers/nvme/host/pci.c +0 -0
              }
      }