Skip to contentSkip to conversation

iommu/vt-d: fix pci device refcount leak in prq_event_thread()

PATCH Superseded Yang Yingliang 2 participants 6 messages Nov 19, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/vt-d: fix pci device refcount leak in prq_event_thread()

As comment of pci_get_domain_bus_and_slot() says, it returns a pci device with refcount increment, when finish using it, the caller must decrement the reference count by calling pci_dev_put(). So call pci_dev_put() after using the 'pdev' to avoid refcount leak.

Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")
---
 drivers/iommu/intel/svm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/intel/svm.c +1 -0
index 7d08eb034f2d..47c07556955e 100644
@@ -751,6 +751,7 @@ static irqreturn_t prq_event_thread(int irq, void *d)
 		trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
 				 req->priv_data[0], req->priv_data[1],
 				 iommu->prq_seq_number++);
+		pci_dev_put(pdev);
 prq_advance:
 		head = (head + sizeof(*req)) & PRQ_RING_MASK;
 	}
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi Yingliang,

Quoted 17 lines · Yang Yingliang

As comment of pci_get_domain_bus_and_slot() says, it returns a pci device with refcount increment, when finish using it, the caller must decrement the reference count by calling pci_dev_put(). So call pci_dev_put() after using the 'pdev' to avoid refcount leak.

Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")
---
  drivers/iommu/intel/svm.c | 1 +
  1 file changed, 1 insertion(+)
drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..47c07556955e 100644

drivers/iommu/intel/svm.c static irqreturn_t prq_event_thread(int irq, void *d) · line 757

Quoted 7 lines
drivers/iommu/intel/svm.c +1 -0
@@ -751,6 +751,7 @@ static irqreturn_t prq_event_thread(int irq, void *d)
  		trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
  				 req->priv_data[0], req->priv_data[1],
  				 iommu->prq_seq_number++);
+		pci_dev_put(pdev);
  prq_advance:
  		head = (head + sizeof(*req)) & PRQ_RING_MASK;
  	}

Thanks for the fix.Furthermore, if the returned @pdev is empty, we do not need to trace this fault, nor do we need to put the device. Hence, how about below changes?

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..2991f9cccf5f 100644
@@ -745,12 +745,17 @@ static irqreturn_t prq_event_thread(int irq, void *d)
                 * If prq is to be handled outside iommu driver via 

receiver of
* the fault notifiers, we skip the page response here. */
- if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req))
- handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
+ if (!pdev)
+ goto prq_advance;

- trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
- req->priv_data[0], req->priv_data[1],
- iommu->prq_seq_number++);
+ if (intel_svm_prq_report(iommu, &pdev->dev, req))
+ handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
+ else
+ trace_prq_report(iommu, &pdev->dev,

+                                        req->qw_0, req->qw_1,
+                                        req->priv_data[0], req->priv_data[1],
+                                        iommu->prq_seq_number++);
+               pci_dev_put(pdev);
 prq_advance:
                head = (head + sizeof(*req)) & PRQ_RING_MASK;
        }

Best regards,
baolu

maintainer
#2 lore raw
Quoted 38 lines · Baolu Lu

Hi Yingliang,

Quoted 21 lines · Yang Yingliang

As comment of pci_get_domain_bus_and_slot() says, it returns a pci device with refcount increment, when finish using it, the caller must decrement the reference count by calling pci_dev_put(). So call pci_dev_put() after using the 'pdev' to avoid refcount leak.

Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")

---
  drivers/iommu/intel/svm.c | 1 +

  1 file changed, 1 insertion(+)
drivers/iommu/intel/svm.c +1 -0
index 7d08eb034f2d..47c07556955e 100644
@@ -751,6 +751,7 @@ static irqreturn_t prq_event_thread(int irq, void *d)
           trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
                    req->priv_data[0], req->priv_data[1],
                    iommu->prq_seq_number++);
+        pci_dev_put(pdev);
Quoted 3 lines

  prq_advance:
          head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Thanks for the fix.Furthermore, if the returned @pdev is empty, we do not need to trace this fault, nor do we need to put the device. Hence, how about below changes?

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..2991f9cccf5f 100644
@@ -745,12 +745,17 @@ static irqreturn_t prq_event_thread(int irq, void *d)
                  * If prq is to be handled outside iommu driver via 
Quoted 7 lines

receiver of
                 * the fault notifiers, we skip the page response here. */
-               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req)) -                       handle_bad_prq_event(iommu, req, QI_RESP_INVALID); +               if (!pdev)
+                       goto prq_advance;

Here it should be

						goto bad_req;

Sorry about it!

Quoted 16 lines

-               trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1, -                                req->priv_data[0], req->priv_data[1], -                                iommu->prq_seq_number++);
+               if (intel_svm_prq_report(iommu, &pdev->dev, req)) +                       handle_bad_prq_event(iommu, req, QI_RESP_INVALID); +               else
+                       trace_prq_report(iommu, &pdev->dev,
+                                        req->qw_0, req->qw_1, +                                        req->priv_data[0], req->priv_data[1],
+                                        iommu->prq_seq_number++); +               pci_dev_put(pdev);
 prq_advance:
                head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Best regards,
baolu

#3 lore raw

Hi,

Quoted 53 lines · Baolu Lu
Quoted 38 lines · Baolu Lu

Hi Yingliang,

Quoted 22 lines · Yang Yingliang

As comment of pci_get_domain_bus_and_slot() says, it returns a pci device with refcount increment, when finish using it, the caller must decrement the reference count by calling pci_dev_put(). So call pci_dev_put() after using the 'pdev' to avoid refcount leak.

Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")

---
  drivers/iommu/intel/svm.c | 1 +

  1 file changed, 1 insertion(+)
drivers/iommu/intel/svm.c +1 -0
index 7d08eb034f2d..47c07556955e 100644
@@ -751,6 +751,7 @@ static irqreturn_t prq_event_thread(int irq, 
 void *d)
           trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
                    req->priv_data[0], req->priv_data[1],
                    iommu->prq_seq_number++);
+        pci_dev_put(pdev);
Quoted 3 lines

  prq_advance:
          head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Thanks for the fix.Furthermore, if the returned @pdev is empty, we do not need to trace this fault, nor do we need to put the device. Hence, how about below changes?

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..2991f9cccf5f 100644

drivers/iommu/intel/svm.c static irqreturn_t prq_event_thread(int irq, · line 752

Quoted 10 lines
drivers/iommu/intel/svm.c +2 -2
@@ -745,12 +745,17 @@ static irqreturn_t prq_event_thread(int irq, 
 void *d)
                   * If prq is to be handled outside iommu driver via receiver of
                   * the fault notifiers, we skip the page response here.
                   */
-               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, 
 req))
-                       handle_bad_prq_event(iommu, req, 
 QI_RESP_INVALID);
+               if (!pdev)
+                       goto prq_advance;

Here it should be
                        goto bad_req;

Quoted 1 line

Sorry about it!

May be this looks more clear:

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..35e44f79b7ac 100644
@@ -745,12 +745,16 @@ static irqreturn_t prq_event_thread(int irq, void *d)

                 * If prq is to be handled outside iommu driver via receiver of
                 * the fault notifiers, we skip the page response here. */

-               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req))
+               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req)) {

                        handle_bad_prq_event(iommu, req, QI_RESP_INVALID); +                       goto put_dev;
+               }

                trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1, req->priv_data[0], req->priv_data[1], iommu->prq_seq_number++); +put_dev:
+               pci_dev_put(pdev);
 prq_advance:
                head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Thanks,
Yang

Quoted 24 lines
Quoted 11 lines
drivers/iommu/intel/svm.c +2 -3
-               trace_prq_report(iommu, &pdev->dev, req->qw_0, 
 req->qw_1,
-                                req->priv_data[0], req->priv_data[1],
-                                iommu->prq_seq_number++);
+               if (intel_svm_prq_report(iommu, &pdev->dev, req))
+                       handle_bad_prq_event(iommu, req, 

QI_RESP_INVALID);
+               else
+                       trace_prq_report(iommu, &pdev->dev,

Quoted 8 lines

+                                        req->qw_0, req->qw_1, +                                        req->priv_data[0], req->priv_data[1],
+ iommu->prq_seq_number++);
+               pci_dev_put(pdev);
  prq_advance:
                 head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Best regards,
baolu

.

maintainer
#4 lore raw
Quoted 11 lines · Yang Yingliang

May be this looks more clear:

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..35e44f79b7ac 100644
@@ -745,12 +745,16 @@ static irqreturn_t prq_event_thread(int irq, void *d)
                  * If prq is to be handled outside iommu driver via 

receiver of
                 * the fault notifiers, we skip the page response here. */

Quoted 6 lines
-               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, req))
+               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, 

req)) {
                        handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
+                       goto put_dev;
+               }

	else
		trace_prq_report(...)

looks more readable.

Quoted 9 lines

                trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1, req->priv_data[0], req->priv_data[1], iommu->prq_seq_number++);
+put_dev:
+               pci_dev_put(pdev);
 prq_advance:
                head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Best regards,
baolu

#5 lore raw
Quoted 23 lines · Baolu Lu
Quoted 8 lines · Yang Yingliang

May be this looks more clear:

drivers/iommu/intel/svm.c +0 -0
index 7d08eb034f2d..35e44f79b7ac 100644
@@ -745,12 +745,16 @@ static irqreturn_t prq_event_thread(int irq, 
 void *d)

drivers/iommu/intel/svm.c static irqreturn_t prq_event_thread(int irq, · line 754

Quoted 10 lines
drivers/iommu/intel/svm.c +3 -1
                   * If prq is to be handled outside iommu driver via receiver of
                   * the fault notifiers, we skip the page response here.
                   */
-               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, 
 req))
+               if (!pdev || intel_svm_prq_report(iommu, &pdev->dev, 
 req)) {
                          handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
+                       goto put_dev;
+               }

    else
        trace_prq_report(...)

Quoted 1 line

looks more readable.

Well, as I know, in kernel, it always handle error case like this:
if (err) {
    // error path
    goto/return
}
...
// success path

And prq_event_thread() other error handlings use the 'goto' style too.

Thanks,
Yang

Quoted 15 lines
Quoted 10 lines
drivers/iommu/intel/svm.c +2 -0
                  trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
                                   req->priv_data[0], req->priv_data[1],
                                   iommu->prq_seq_number++);
+put_dev:
+               pci_dev_put(pdev);

  prq_advance:
                 head = (head + sizeof(*req)) & PRQ_RING_MASK; }

Best regards,
baolu

.