linux/drivers/misc/cxl
Daniel Axtens 5324b25311 cxl: Fix unbalanced pci_dev_get in cxl_probe
[ Upstream commit 2925c2fdf1 ]

Currently the first thing we do in cxl_probe is to grab a reference
on the pci device. Later on, we call device_register on our adapter.
In our remove path, we call device_unregister, but we never call
pci_dev_put. We therefore leak the device every time we do a
reflash.

device_register/unregister is sufficient to hold the reference.
Therefore, drop the call to pci_dev_get.

Here's why this is safe.
The proposed cxl_probe(pdev) calls cxl_adapter_init:
    a) init calls cxl_adapter_alloc, which creates a struct cxl,
       conventionally called adapter. This struct contains a
       device entry, adapter->dev.

    b) init calls cxl_configure_adapter, where we set
       adapter->dev.parent = &dev->dev (here dev is the pci dev)

So at this point, the cxl adapter's device's parent is the PCI
device that I want to be refcounted properly.

    c) init calls cxl_register_adapter
       *) cxl_register_adapter calls device_register(&adapter->dev)

So now we're in device_register, where dev is the adapter device, and
we want to know if the PCI device is safe after we return.

device_register(&adapter->dev) calls device_initialize() and then
device_add().

device_add() does a get_device(). device_add() also explicitly grabs
the device's parent, and calls get_device() on it:

         parent = get_device(dev->parent);

So therefore, device_register() takes a lock on the parent PCI dev,
which is what pci_dev_get() was guarding. pci_dev_get() can therefore
be safely removed.

Fixes: f204e0b8ce ("cxl: Driver code for powernv PCIe based cards for userspace access")
Cc: stable@vger.kernel.org
Signed-off-by: Daniel Axtens <dja@axtens.net>
Acked-by: Ian Munsie <imunsie@au1.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
History 2015-10-27 09:33:08 -04:00
..
base.c cxl: Add base builtin support 2014-10-08 20:15:56 +11:00
context.c cxl: Unmap MMIO regions when detaching a context 2015-01-27 08:29:36 -08:00
cxl.h cxl: Use image state defaults for reloading FPGA 2015-03-23 21:02:54 -04:00
debugfs.c cxl: Driver code for powernv PCIe based cards for userspace access 2014-10-08 20:15:57 +11:00
fault.c cxl: Fix PSL error due to duplicate segment table entries 2014-10-28 19:52:52 +11:00
file.c cxl: Unmap MMIO regions when detaching a context 2015-01-27 08:29:36 -08:00
irq.c cxl: Add missing return statement after handling AFU errror 2015-03-23 21:02:54 -04:00
Kconfig cxl: Add driver to Kbuild and Makefiles 2014-10-08 20:15:58 +11:00
main.c cxl: Driver code for powernv PCIe based cards for userspace access 2014-10-08 20:15:57 +11:00
Makefile cxl: Add driver to Kbuild and Makefiles 2014-10-08 20:15:58 +11:00
native.c cxl: Add timeout to process element commands 2015-01-27 08:29:36 -08:00
pci.c cxl: Fix unbalanced pci_dev_get in cxl_probe 2015-10-27 09:33:08 -04:00
sysfs.c cxl: Change contexts_lock to a mutex to fix sleep while atomic bug 2015-01-27 08:29:36 -08:00