Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

powerpc/powernv/npu: Fix reference leak

Since 902bdc57451c, get_pci_dev() calls pci_get_domain_bus_and_slot(). This
has the effect of incrementing the reference count of the PCI device, as
explained in drivers/pci/search.c:

* Given a PCI domain, bus, and slot/function number, the desired PCI
* device is located in the list of PCI devices. If the device is
* found, its reference count is increased and this function returns a
* pointer to its data structure. The caller must decrement the
* reference count by calling pci_dev_put(). If no device is found,
* %NULL is returned.

Nothing was done to call pci_dev_put() and the reference count of GPU and
NPU PCI devices rockets up.

A natural way to fix this would be to teach the callers about the change,
so that they call pci_dev_put() when done with the pointer. This turns
out to be quite intrusive, as it affects many paths in npu-dma.c,
pci-ioda.c and vfio_pci_nvlink2.c. Also, the issue appeared in 4.16 and
some affected code got moved around since then: it would be problematic
to backport the fix to stable releases.

All that code never cared for reference counting anyway. Call pci_dev_put()
from get_pci_dev() to revert to the previous behavior.

Fixes: 902bdc57451c ("powerpc/powernv/idoa: Remove unnecessary pcidev from pci_dn")
Cc: stable@vger.kernel.org # v4.16
Signed-off-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Greg Kurz and committed by
Michael Ellerman
02c5f539 c806a6fd

+14 -1
+14 -1
arch/powerpc/platforms/powernv/npu-dma.c
··· 31 31 static struct pci_dev *get_pci_dev(struct device_node *dn) 32 32 { 33 33 struct pci_dn *pdn = PCI_DN(dn); 34 + struct pci_dev *pdev; 34 35 35 - return pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus), 36 + pdev = pci_get_domain_bus_and_slot(pci_domain_nr(pdn->phb->bus), 36 37 pdn->busno, pdn->devfn); 38 + 39 + /* 40 + * pci_get_domain_bus_and_slot() increased the reference count of 41 + * the PCI device, but callers don't need that actually as the PE 42 + * already holds a reference to the device. Since callers aren't 43 + * aware of the reference count change, call pci_dev_put() now to 44 + * avoid leaks. 45 + */ 46 + if (pdev) 47 + pci_dev_put(pdev); 48 + 49 + return pdev; 37 50 } 38 51 39 52 /* Given a NPU device get the associated PCI device. */