Skip to content

Commit 4879610

Browse files
niklas88hcahca
authored andcommitted
s390/pci: Fix leak of struct zpci_dev when zpci_add_device() fails
Prior to commit 0467cdd ("s390/pci: Sort PCI functions prior to creating virtual busses") the IOMMU was initialized and the device was registered as part of zpci_create_device() with the struct zpci_dev freed if either resulted in an error. With that commit this was moved into a separate function called zpci_add_device(). While this new function logs when adding failed, it expects the caller not to use and to free the struct zpci_dev on error. This difference between it and zpci_create_device() was missed while changing the callers and the incompletely initialized struct zpci_dev may get used in zpci_scan_configured_device in the error path. This then leads to a crash due to the device not being registered with the zbus. It was also not freed in this case. Fix this by handling the error return of zpci_add_device(). Since in this case the zdev was not added to the zpci_list it can simply be discarded and freed. Also make this more explicit by moving the kref_init() into zpci_add_device() and document that zpci_zdev_get()/zpci_zdev_put() must be used after adding. Cc: [email protected] Fixes: 0467cdd ("s390/pci: Sort PCI functions prior to creating virtual busses") Reviewed-by: Gerd Bayer <[email protected]> Reviewed-by: Matthew Rosato <[email protected]> Signed-off-by: Niklas Schnelle <[email protected]> Signed-off-by: Heiko Carstens <[email protected]>
1 parent adb44a4 commit 4879610

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

arch/s390/pci/pci.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,9 @@ int zpci_hot_reset_device(struct zpci_dev *zdev)
776776
* @fh: Current Function Handle of the device to be created
777777
* @state: Initial state after creation either Standby or Configured
778778
*
779-
* Creates a new zpci device and adds it to its, possibly newly created, zbus
780-
* as well as zpci_list.
779+
* Allocates a new struct zpci_dev and queries the platform for its details.
780+
* If successful the device can subsequently be added to the zPCI subsystem
781+
* using zpci_add_device().
781782
*
782783
* Returns: the zdev on success or an error pointer otherwise
783784
*/
@@ -800,7 +801,6 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
800801
goto error;
801802
zdev->state = state;
802803

803-
kref_init(&zdev->kref);
804804
mutex_init(&zdev->state_lock);
805805
mutex_init(&zdev->fmb_lock);
806806
mutex_init(&zdev->kzdev_lock);
@@ -813,6 +813,17 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
813813
return ERR_PTR(rc);
814814
}
815815

816+
/**
817+
* zpci_add_device() - Add a previously created zPCI device to the zPCI subsystem
818+
* @zdev: The zPCI device to be added
819+
*
820+
* A struct zpci_dev is added to the zPCI subsystem and to a virtual PCI bus creating
821+
* a new one as necessary. A hotplug slot is created and events start to be handled.
822+
* If successful from this point on zpci_zdev_get() and zpci_zdev_put() must be used.
823+
* If adding the struct zpci_dev fails the device was not added and should be freed.
824+
*
825+
* Return: 0 on success, or an error code otherwise
826+
*/
816827
int zpci_add_device(struct zpci_dev *zdev)
817828
{
818829
int rc;
@@ -826,6 +837,7 @@ int zpci_add_device(struct zpci_dev *zdev)
826837
if (rc)
827838
goto error_destroy_iommu;
828839

840+
kref_init(&zdev->kref);
829841
spin_lock(&zpci_list_lock);
830842
list_add_tail(&zdev->entry, &zpci_list);
831843
spin_unlock(&zpci_list_lock);
@@ -1118,7 +1130,8 @@ static void zpci_add_devices(struct list_head *scan_list)
11181130
list_sort(NULL, scan_list, &zpci_cmp_rid);
11191131
list_for_each_entry_safe(zdev, tmp, scan_list, entry) {
11201132
list_del_init(&zdev->entry);
1121-
zpci_add_device(zdev);
1133+
if (zpci_add_device(zdev))
1134+
kfree(zdev);
11221135
}
11231136
}
11241137

arch/s390/pci/pci_event.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
340340
zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
341341
if (IS_ERR(zdev))
342342
break;
343-
zpci_add_device(zdev);
343+
if (zpci_add_device(zdev)) {
344+
kfree(zdev);
345+
break;
346+
}
344347
} else {
345348
/* the configuration request may be stale */
346349
if (zdev->state != ZPCI_FN_STATE_STANDBY)
@@ -354,7 +357,10 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
354357
zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
355358
if (IS_ERR(zdev))
356359
break;
357-
zpci_add_device(zdev);
360+
if (zpci_add_device(zdev)) {
361+
kfree(zdev);
362+
break;
363+
}
358364
} else {
359365
zpci_update_fh(zdev, ccdf->fh);
360366
}

0 commit comments

Comments
 (0)