提交 c122383d 编写于 作者: N Niklas Schnelle 提交者: Vasily Gorbik

s390/pci: improve zpci_dev reference counting

Currently zpci_dev uses kref based reference counting but only accounts
for one original reference plus one reference from an added pci_dev to
its underlying zpci_dev. Counting just the original reference worked
until the pci_dev reference was added in commit 2a671f77 ("s390/pci:
fix use after free of zpci_dev") because once a zpci_dev goes away, i.e.
enters the reserved state, it would immediately get released. However
with the pci_dev reference this is no longer the case and the zpci_dev
may still appear in multiple availability events indicating that it was
reserved. This was solved by detecting when the zpci_dev is already on
its way out but still hanging around. This has however shown some light
on how unusual our zpci_dev reference counting is.

Improve upon this by modelling zpci_dev reference counting on pci_dev.
Analogous to pci_get_slot() increment the reference count in
get_zdev_by_fid(). Thus all users of get_zdev_by_fid() must drop the
reference once they are done with the zpci_dev.

Similar to pci_scan_single_device(), zpci_create_device() returns the
device with an initial count of 1 and the device added to the zpci_list
(analogous to the PCI bus' device_list). In turn users of
zpci_create_device() must only drop the reference once the device is
gone from the point of view of the zPCI subsystem, it might still be
referenced by the common PCI subsystem though.
Reviewed-by: NMatthew Rosato <mjrosato@linux.ibm.com>
Signed-off-by: NNiklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: NVasily Gorbik <gor@linux.ibm.com>
上级 7277b421
...@@ -69,6 +69,7 @@ struct zpci_dev *get_zdev_by_fid(u32 fid) ...@@ -69,6 +69,7 @@ struct zpci_dev *get_zdev_by_fid(u32 fid)
list_for_each_entry(tmp, &zpci_list, entry) { list_for_each_entry(tmp, &zpci_list, entry) {
if (tmp->fid == fid) { if (tmp->fid == fid) {
zdev = tmp; zdev = tmp;
zpci_zdev_get(zdev);
break; break;
} }
} }
......
...@@ -19,7 +19,8 @@ void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error); ...@@ -19,7 +19,8 @@ void zpci_bus_remove_device(struct zpci_dev *zdev, bool set_error);
void zpci_release_device(struct kref *kref); void zpci_release_device(struct kref *kref);
static inline void zpci_zdev_put(struct zpci_dev *zdev) static inline void zpci_zdev_put(struct zpci_dev *zdev)
{ {
kref_put(&zdev->kref, zpci_release_device); if (zdev)
kref_put(&zdev->kref, zpci_release_device);
} }
static inline void zpci_zdev_get(struct zpci_dev *zdev) static inline void zpci_zdev_get(struct zpci_dev *zdev)
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#include <asm/clp.h> #include <asm/clp.h>
#include <uapi/asm/clp.h> #include <uapi/asm/clp.h>
#include "pci_bus.h"
bool zpci_unique_uid; bool zpci_unique_uid;
void update_uid_checking(bool new) void update_uid_checking(bool new)
...@@ -404,8 +406,11 @@ static void __clp_add(struct clp_fh_list_entry *entry, void *data) ...@@ -404,8 +406,11 @@ static void __clp_add(struct clp_fh_list_entry *entry, void *data)
return; return;
zdev = get_zdev_by_fid(entry->fid); zdev = get_zdev_by_fid(entry->fid);
if (!zdev) if (zdev) {
zpci_create_device(entry->fid, entry->fh, entry->config_state); zpci_zdev_put(zdev);
return;
}
zpci_create_device(entry->fid, entry->fh, entry->config_state);
} }
int clp_scan_pci_devices(void) int clp_scan_pci_devices(void)
......
...@@ -269,7 +269,7 @@ static void __zpci_event_error(struct zpci_ccdf_err *ccdf) ...@@ -269,7 +269,7 @@ static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid); pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
if (!pdev) if (!pdev)
return; goto no_pdev;
switch (ccdf->pec) { switch (ccdf->pec) {
case 0x003a: /* Service Action or Error Recovery Successful */ case 0x003a: /* Service Action or Error Recovery Successful */
...@@ -286,6 +286,8 @@ static void __zpci_event_error(struct zpci_ccdf_err *ccdf) ...@@ -286,6 +286,8 @@ static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
break; break;
} }
pci_dev_put(pdev); pci_dev_put(pdev);
no_pdev:
zpci_zdev_put(zdev);
} }
void zpci_event_error(void *data) void zpci_event_error(void *data)
...@@ -314,6 +316,7 @@ static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh) ...@@ -314,6 +316,7 @@ static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh)
static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf) static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
{ {
struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid); struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
bool existing_zdev = !!zdev;
enum zpci_state state; enum zpci_state state;
zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n", zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n",
...@@ -378,6 +381,8 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf) ...@@ -378,6 +381,8 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
default: default:
break; break;
} }
if (existing_zdev)
zpci_zdev_put(zdev);
} }
void zpci_event_availability(void *data) void zpci_event_availability(void *data)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册