Skip to content

Commit

Permalink
ocxl: fix pci device refcount leak when calling get_function_0()
Browse files Browse the repository at this point in the history
[ Upstream commit 27158c7 ]

get_function_0() calls pci_get_domain_bus_and_slot(), as comment
says, it returns a pci device with refcount increment, so after
using it, pci_dev_put() needs be called.

Get the device reference when get_function_0() is not called, so
pci_dev_put() can be called in the error path and callers
unconditionally. And add comment above get_dvsec_vendor0() to tell
callers to call pci_dev_put().

Fixes: 87db757 ("ocxl: control via sysfs whether the FPGA is reloaded on a link reset")
Suggested-by: Andrew Donnellan <ajd@linux.ibm.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Acked-by: Andrew Donnellan <ajd@linux.ibm.com>
Link: https://lore.kernel.org/r/20221121154339.4088935-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Yang Yingliang authored and gregkh committed Dec 31, 2022
1 parent 3299983 commit 37a13b2
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion drivers/misc/ocxl/config.c
Expand Up @@ -204,6 +204,18 @@ static int read_dvsec_vendor(struct pci_dev *dev)
return 0;
}

/**
* get_dvsec_vendor0() - Find a related PCI device (function 0)
* @dev: PCI device to match
* @dev0: The PCI device (function 0) found
* @out_pos: The position of PCI device (function 0)
*
* Returns 0 on success, negative on failure.
*
* NOTE: If it's successful, the reference of dev0 is increased,
* so after using it, the callers must call pci_dev_put() to give
* up the reference.
*/
static int get_dvsec_vendor0(struct pci_dev *dev, struct pci_dev **dev0,
int *out_pos)
{
Expand All @@ -213,10 +225,14 @@ static int get_dvsec_vendor0(struct pci_dev *dev, struct pci_dev **dev0,
dev = get_function_0(dev);
if (!dev)
return -1;
} else {
dev = pci_dev_get(dev);
}
pos = find_dvsec(dev, OCXL_DVSEC_VENDOR_ID);
if (!pos)
if (!pos) {
pci_dev_put(dev);
return -1;
}
*dev0 = dev;
*out_pos = pos;
return 0;
Expand All @@ -233,6 +249,7 @@ int ocxl_config_get_reset_reload(struct pci_dev *dev, int *val)

pci_read_config_dword(dev0, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
&reset_reload);
pci_dev_put(dev0);
*val = !!(reset_reload & BIT(0));
return 0;
}
Expand All @@ -254,6 +271,7 @@ int ocxl_config_set_reset_reload(struct pci_dev *dev, int val)
reset_reload &= ~BIT(0);
pci_write_config_dword(dev0, pos + OCXL_DVSEC_VENDOR_RESET_RELOAD,
reset_reload);
pci_dev_put(dev0);
return 0;
}

Expand Down

0 comments on commit 37a13b2

Please sign in to comment.