Skip to contentSkip to conversation

scsi: hisi_sas: drop free_irq of devm_request_irq allocated irq

PATCH Superseded Yang Yingliang 2 participants 4 messages May 18, 2021 – May 19, 2021

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

PATCH 1/1 scsi: hisi_sas: drop free_irq of devm_request_irq allocated irq

irq allocated with devm_request_irq should not be freed using free_irq, because doing so causes a dangling pointer, and a subsequent double free.

---
 drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +3 -3
index 499c770d405c..684f762bcfb3 100644
@@ -4811,9 +4811,9 @@ hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, struct hisi_hba *hisi_hba)
 {
 	int i;
 
-	free_irq(pci_irq_vector(pdev, 1), hisi_hba);
-	free_irq(pci_irq_vector(pdev, 2), hisi_hba);
-	free_irq(pci_irq_vector(pdev, 11), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 1), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 2), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 11), hisi_hba);
 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
 		int nr = hisi_sas_intr_conv ? 16 : 16 + i;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 18 lines · Yang Yingliang

irq allocated with devm_request_irq should not be freed using free_irq, because doing so causes a dangling pointer, and a subsequent double free.

---
  drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +0 -0
index 499c770d405c..684f762bcfb3 100644
@@ -4811,9 +4811,9 @@ hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, struct hisi_hba *hisi_hba)
  {
  	int i;

drivers/scsi/hisi_sas/hisi_sas_v3_hw.c hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, struct hisi_hba *hisi_hba) · line 4819

Quoted 10 lines
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +3 -3
 
-	free_irq(pci_irq_vector(pdev, 1), hisi_hba);
-	free_irq(pci_irq_vector(pdev, 2), hisi_hba);
-	free_irq(pci_irq_vector(pdev, 11), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 1), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 2), hisi_hba);
+	devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 11), hisi_hba);
  	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
  		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  		int nr = hisi_sas_intr_conv ? 16 : 16 + i;

Does the free_irq(pci_irq_vector(pdev, nr, cq)) call also need to change (not shown)?

Having said that, why have these at all if we use devm_request_irq()? devm_irq_release() calls free_irq().

Thanks,
John

#2 lore raw
Quoted 23 lines · John Garry
Quoted 22 lines · Yang Yingliang

irq allocated with devm_request_irq should not be freed using free_irq, because doing so causes a dangling pointer, and a subsequent double free.

---
  drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +1 -2
index 499c770d405c..684f762bcfb3 100644
@@ -4811,9 +4811,9 @@ hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, 
 struct hisi_hba *hisi_hba)
   {
       int i;
   -    free_irq(pci_irq_vector(pdev, 1), hisi_hba);
-    free_irq(pci_irq_vector(pdev, 2), hisi_hba);
-    free_irq(pci_irq_vector(pdev, 11), hisi_hba);
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 1), hisi_hba);
Quoted 8 lines
Quoted 6 lines
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +2 -0
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 2), hisi_hba);
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 11), hisi_hba);
       for (i = 0; i < hisi_hba->cq_nvecs; i++) {
           struct hisi_sas_cq *cq = &hisi_hba->cq[i];

          int nr = hisi_sas_intr_conv ? 16 : 16 + i;

Does the free_irq(pci_irq_vector(pdev, nr, cq)) call also need to change (not shown)?

Yes, I missed that, it should be changed too.

Quoted 2 lines

Having said that, why have these at all if we use devm_request_irq()? devm_irq_release() calls free_irq().

I keep the original logic here, only avoid double free.

Quoted 5 lines

Thanks,
John

.

#3 lore raw
Quoted 33 lines · Yang Yingliang
Quoted 23 lines · John Garry
Quoted 22 lines · Yang Yingliang

irq allocated with devm_request_irq should not be freed using free_irq, because doing so causes a dangling pointer, and a subsequent double free.

---
  drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +1 -2
index 499c770d405c..684f762bcfb3 100644
@@ -4811,9 +4811,9 @@ hisi_sas_v3_destroy_irqs(struct pci_dev *pdev, 
 struct hisi_hba *hisi_hba)
   {
       int i;
   -    free_irq(pci_irq_vector(pdev, 1), hisi_hba);
-    free_irq(pci_irq_vector(pdev, 2), hisi_hba);
-    free_irq(pci_irq_vector(pdev, 11), hisi_hba);
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 1), hisi_hba);
Quoted 8 lines
Quoted 6 lines
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +2 -0
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 2), hisi_hba);
+    devm_free_irq(&pdev->dev, pci_irq_vector(pdev, 11), hisi_hba);
       for (i = 0; i < hisi_hba->cq_nvecs; i++) {
           struct hisi_sas_cq *cq = &hisi_hba->cq[i];

          int nr = hisi_sas_intr_conv ? 16 : 16 + i;

Does the free_irq(pci_irq_vector(pdev, nr, cq)) call also need to change (not shown)?

Quoted 1 line

Yes, I missed that, it should be changed too.

So I think that we need this addition:
devm_free_irq(&pdev->dev, pci_irq_vector(pdev, nr), cq);

Quoted 3 lines
Quoted 2 lines

Having said that, why have these at all if we use devm_request_irq()? devm_irq_release() calls free_irq().

I keep the original logic here, only avoid double free.

Kasan doesn't complain. Anyway, I think we can't rely on device-managed method (for calling free_irq()) as it conflicts with pci free vectors call. I thought that someone was developed a device-managed version of that (pci_alloc_irq_vectors()).

Anyway, please proceed with your change, but with the suggested addition.

Thanks