Skip to contentSkip to conversation

qla2xxx: Fix memory leak in error path

PATCH In review Anirban Chakraborty 2 participants 5 messages Jan 19, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qla2xxx: Fix memory leak in error path

The following patch fixes memory leak in the error path of scsi host allocation failure.
Please review and apply.

Thanks,
Anirban

drivers/scsi/qla2xxx/qla_os.c +2 -0
index df6e6a2..2cf1491 100644
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_queues(ha);
 		goto probe_hw_failed;
 	}
 
#1 lore raw
Quoted 15 lines · Anirban Chakraborty

The following patch fixes memory leak in the error path of scsi host allocation failure.
Please review and apply.

Thanks,
Anirban

drivers/scsi/qla2xxx/qla_os.c +0 -0
index df6e6a2..2cf1491 100644

drivers/scsi/qla2xxx/qla_os.c qla2x00_probe_one(struct pci_dev *pdev, const struct · line 1895

Quoted 8 lines
drivers/scsi/qla2xxx/qla_os.c +2 -0
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct 
 pci_device_id *id)
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_queues(ha);
 		goto probe_hw_failed;
 	}

This patch is not sufficient, because req and rsp are not freed and at this point ha->req_q_map and ha->rsp_q_map are not allocated yet, so qla2x00_free_queues is not needed.

Following is my patch.
Comments?

drivers/scsi/qla2xxx/qla_os.c +7 -1
diff -Nrup linux-2.6.29-rc2.org/drivers/scsi/qla2xxx/qla_os.c linux-2.6.29-rc2.qla2x/drivers/scsi/qla2xxx/qla_os.c
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, 
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_hw_failed;
 	}
 
@@ -1916,14 +1918,18 @@ qla2x00_probe_one(struct pci_dev *pdev, 
 
 	/* Set up the irqs */
 	ret = qla2x00_request_irqs(ha, rsp);
-	if (ret)
+	if (ret) {
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_failed;
+	}
 
 	/* Alloc arrays of request and response ring ptrs */
 	if (!qla2x00_alloc_queues(ha)) {
 		qla_printk(KERN_WARNING, ha,
 		"[ERROR] Failed to allocate memory for queue"
 		" pointers\n");
+
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_failed;
 	}
 	ha->rsp_q_map[0] = rsp;
#2 lore raw
Quoted 62 lines · Hisashi Hifumi
Quoted 21 lines · Anirban Chakraborty

The following patch fixes memory leak in the error path of scsi host allocation failure.
Please review and apply.

Thanks,
Anirban

drivers/scsi/qla2xxx/qla_os.c +0 -0
index df6e6a2..2cf1491 100644
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, const  

struct
pci_device_id *id)

		    "[ERROR] Failed to allocate memory for scsi_host\n");

		ret = -ENOMEM;
drivers/scsi/qla2xxx/qla_os.c +2 -0
+		qla2x00_mem_free(ha);
+		qla2x00_free_queues(ha);
Quoted 3 lines
		goto probe_hw_failed;
	}

This patch is not sufficient, because req and rsp are not freed and at this point ha->req_q_map and ha->rsp_q_map are not allocated yet, so qla2x00_free_queues is not needed.

Following is my patch.
Comments?

diff -Nrup linux-2.6.29-rc2.org/drivers/scsi/qla2xxx/qla_os.c linux-2.6.29-rc2.qla2x/drivers/scsi/qla2xxx/qla_os.c
--- linux-2.6.29-rc2.org/drivers/scsi/qla2xxx/qla_os.c 2009-01-19
11:46:19.000000000 +0900
+++ linux-2.6.29-rc2.qla2x/drivers/scsi/qla2xxx/qla_os.c 2009-01-19
16:00:42.000000000 +0900

drivers/scsi/qla2xxx/qla_os.c +5 -1
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev,
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_hw_failed;
 	}
 
@@ -1916,14 +1918,18 @@ qla2x00_probe_one(struct pci_dev *pdev,
 
 	/* Set up the irqs */
 	ret = qla2x00_request_irqs(ha, rsp);
-	if (ret)
+	if (ret) {
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_failed;
+	}
 

drivers/scsi/qla2xxx/qla_os.c qla2x00_probe_one(struct pci_dev *pdev, · line 1935

Quoted 10 lines
drivers/scsi/qla2xxx/qla_os.c +2 -0
 	/* Alloc arrays of request and response ring ptrs */
 	if (!qla2x00_alloc_queues(ha)) {
 		qla_printk(KERN_WARNING, ha,
 		"[ERROR] Failed to allocate memory for queue"
 		" pointers\n");
+
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_failed;
 	}
 	ha->rsp_q_map[0] = rsp;

There is a hole here. We need to set to ha->max_queues = 0 after calling qla2x00_free_que as otherwise the call to qla2x00_free_device-->qla2x00_free_queues will try to access ha->req-q_map and ha->rsp_q_map arrays which was not allocated at that point of time, as you mentioned. I will resend the combined patch. -Anirban

#3 lore raw
In reply to Anirban Chakraborty

Here is the updated patch.
Thanks,
Anirban

drivers/scsi/qla2xxx/qla_os.c +8 -3
index df6e6a2..cbf377f 100644
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_hw_failed;
 	}
 
@@ -1917,14 +1919,13 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 	/* Set up the irqs */
 	ret = qla2x00_request_irqs(ha, rsp);
 	if (ret)
-		goto probe_failed;
-
+		goto probe_init_failed;
 	/* Alloc arrays of request and response ring ptrs */
 	if (!qla2x00_alloc_queues(ha)) {
 		qla_printk(KERN_WARNING, ha,
 		"[ERROR] Failed to allocate memory for queue"
 		" pointers\n");
-		goto probe_failed;
+		goto probe_init_failed;
 	}
 	ha->rsp_q_map[0] = rsp;
 	ha->req_q_map[0] = req;
@@ -1997,6 +1998,10 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	return 0;
 
+probe_init_failed:
+	qla2x00_free_que(ha, req, rsp);
+	ha->max_queues = 0;
+
 probe_failed:
 	qla2x00_free_device(base_vha);
 
Reviewed-by
#4 lore raw
Quoted 48 lines · Anirban Chakraborty

Here is the updated patch.
Thanks,
Anirban

drivers/scsi/qla2xxx/qla_os.c +4 -3
index df6e6a2..cbf377f 100644
@@ -1888,6 +1888,8 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct 
 pci_device_id *id)
 		    "[ERROR] Failed to allocate memory for scsi_host\n");
 
 		ret = -ENOMEM;
+		qla2x00_mem_free(ha);
+		qla2x00_free_que(ha, req, rsp);
 		goto probe_hw_failed;
 	}
@@ -1917,14 +1919,13 @@ qla2x00_probe_one(struct pci_dev *pdev, const 
 struct pci_device_id *id)
 	/* Set up the irqs */
 	ret = qla2x00_request_irqs(ha, rsp);
 	if (ret)
-		goto probe_failed;
-
+		goto probe_init_failed;
 	/* Alloc arrays of request and response ring ptrs */
 	if (!qla2x00_alloc_queues(ha)) {
 		qla_printk(KERN_WARNING, ha,
 		"[ERROR] Failed to allocate memory for queue"
 		" pointers\n");
-		goto probe_failed;
+		goto probe_init_failed;
 	}
 	ha->rsp_q_map[0] = rsp;
	ha->req_q_map[0] = req;
drivers/scsi/qla2xxx/qla_os.c +4 -0
@@ -1997,6 +1998,10 @@ qla2x00_probe_one(struct pci_dev *pdev, const struct 
 pci_device_id *id)
 
 	return 0;
 
+probe_init_failed:
+	qla2x00_free_que(ha, req, rsp);
+	ha->max_queues = 0;
+

probe_failed:

Quoted 2 lines
	qla2x00_free_device(base_vha);

It looks like good, Please add

if you wish.
Thanks.