iommu/vt-d: fix pci device refcount leak in prq_event_thread()
Subsystems (2)
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.
--- drivers/iommu/intel/svm.c | 1 + 1 file changed, 1 insertion(+)
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 ; }
Signature · 1 line
2.25.1
Baolu Lu
maintainerHi 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 callingpci_dev_put(). So callpci_dev_put()after using the 'pdev' to avoid refcount leak.Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")Signed-off-by Yang Yingliang <[email protected]>--- drivers/iommu/intel/svm.c | 1 + 1 file changed, 1 insertion(+)drivers/iommu/intel/svm.c +0
-0
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
@@ -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
Baolu Lu
maintainerQuoted 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 callingpci_dev_put(). So callpci_dev_put()after using the 'pdev' to avoid refcount leak.Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")Signed-off-by Yang Yingliang <[email protected]>---
drivers/iommu/intel/svm.c| 1 +1 file changed, 1 insertion(+)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);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@@ -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)
+ gotoprq_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
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 callingpci_dev_put(). So callpci_dev_put()after using the 'pdev' to avoid refcount leak.Fixes 06f4b8d09dba ("iommu/vt-d: Remove unnecessary SVA data accesses in page fault path")Signed-off-by Yang Yingliang <[email protected]>---
drivers/iommu/intel/svm.c| 1 +1 file changed, 1 insertion(+)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);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
-0drivers/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
gotobad_req;
Quoted 1 line
Sorry about it!
May be this looks more clear:
drivers/iommu/intel/svm.c +0 -0
@@ -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.
Baolu Lu
maintainerQuoted 11 lines · Yang Yingliang
May be this looks more clear:
drivers/iommu/intel/svm.c +0
-0@@ -745,12 +745,16 @@ static irqreturn_t prq_event_thread(int irq, void *d) * If prq is to be handled outside iommu driver viareceiver 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);
+ gotoput_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
Quoted 23 lines · Baolu Lu
Quoted 8 lines · Yang Yingliang
May be this looks more clear:
drivers/iommu/intel/svm.c +0
-0@@ -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
-0trace_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.