Skip to content

Commit 188907c

Browse files
niklas88gregkh
authored andcommitted
s390/pci: fix zpci_zdev_put() on reserve
commit a46044a upstream. Since commit 2a671f7 ("s390/pci: fix use after free of zpci_dev") the reference count of a zpci_dev is incremented between pcibios_add_device() and pcibios_release_device() which was supposed to prevent the zpci_dev from being freed while the common PCI code has access to it. It was missed however that the handling of zPCI availability events assumed that once zpci_zdev_put() was called no later availability event would still see the device. With the previously mentioned commit however this assumption no longer holds and we must make sure that we only drop the initial long-lived reference the zPCI subsystem holds exactly once. Do so by introducing a zpci_device_reserved() function that handles when a device is reserved. Here we make sure the zpci_dev will not be considered for further events by removing it from the zpci_list. This also means that the device actually stays in the ZPCI_FN_STATE_RESERVED state between the time we know it has been reserved and the final reference going away. We thus need to consider it a real state instead of just a conceptual state after the removal. The final cleanup of PCI resources, removal from zbus, and destruction of the IOMMU stays in zpci_release_device() to make sure holders of the reference do see valid data until the release. Fixes: 2a671f7 ("s390/pci: fix use after free of zpci_dev") Cc: stable@vger.kernel.org Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f18b90e commit 188907c

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎arch/s390/include/asm/pci.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ int zpci_create_device(u32 fid, u32 fh, enum zpci_state state);
205205
void zpci_remove_device(struct zpci_dev *zdev, bool set_error);
206206
int zpci_enable_device(struct zpci_dev *);
207207
int zpci_disable_device(struct zpci_dev *);
208+
void zpci_device_reserved(struct zpci_dev *zdev);
209+
bool zpci_is_device_configured(struct zpci_dev *zdev);
210+
208211
int zpci_register_ioat(struct zpci_dev *, u8, u64, u64, u64);
209212
int zpci_unregister_ioat(struct zpci_dev *, u8);
210213
void zpci_remove_reserved_devices(void);

‎arch/s390/pci/pci.c‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void zpci_remove_reserved_devices(void)
9292
spin_unlock(&zpci_list_lock);
9393

9494
list_for_each_entry_safe(zdev, tmp, &remove, entry)
95-
zpci_zdev_put(zdev);
95+
zpci_device_reserved(zdev);
9696
}
9797

9898
int pci_domain_nr(struct pci_bus *bus)
@@ -787,6 +787,39 @@ int zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
787787
return rc;
788788
}
789789

790+
bool zpci_is_device_configured(struct zpci_dev *zdev)
791+
{
792+
enum zpci_state state = zdev->state;
793+
794+
return state != ZPCI_FN_STATE_RESERVED &&
795+
state != ZPCI_FN_STATE_STANDBY;
796+
}
797+
798+
/**
799+
* zpci_device_reserved() - Mark device as resverved
800+
* @zdev: the zpci_dev that was reserved
801+
*
802+
* Handle the case that a given zPCI function was reserved by another system.
803+
* After a call to this function the zpci_dev can not be found via
804+
* get_zdev_by_fid() anymore but may still be accessible via existing
805+
* references though it will not be functional anymore.
806+
*/
807+
void zpci_device_reserved(struct zpci_dev *zdev)
808+
{
809+
if (zdev->has_hp_slot)
810+
zpci_exit_slot(zdev);
811+
/*
812+
* Remove device from zpci_list as it is going away. This also
813+
* makes sure we ignore subsequent zPCI events for this device.
814+
*/
815+
spin_lock(&zpci_list_lock);
816+
list_del(&zdev->entry);
817+
spin_unlock(&zpci_list_lock);
818+
zdev->state = ZPCI_FN_STATE_RESERVED;
819+
zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
820+
zpci_zdev_put(zdev);
821+
}
822+
790823
void zpci_release_device(struct kref *kref)
791824
{
792825
struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
@@ -802,17 +835,19 @@ void zpci_release_device(struct kref *kref)
802835
case ZPCI_FN_STATE_STANDBY:
803836
if (zdev->has_hp_slot)
804837
zpci_exit_slot(zdev);
838+
spin_lock(&zpci_list_lock);
839+
list_del(&zdev->entry);
840+
spin_unlock(&zpci_list_lock);
841+
zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
842+
fallthrough;
843+
case ZPCI_FN_STATE_RESERVED:
805844
zpci_cleanup_bus_resources(zdev);
806845
zpci_bus_device_unregister(zdev);
807846
zpci_destroy_iommu(zdev);
808847
fallthrough;
809848
default:
810849
break;
811850
}
812-
813-
spin_lock(&zpci_list_lock);
814-
list_del(&zdev->entry);
815-
spin_unlock(&zpci_list_lock);
816851
zpci_dbg(3, "rem fid:%x\n", zdev->fid);
817852
kfree(zdev);
818853
}

‎arch/s390/pci/pci_event.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
146146
zdev->state = ZPCI_FN_STATE_STANDBY;
147147
if (!clp_get_state(ccdf->fid, &state) &&
148148
state == ZPCI_FN_STATE_RESERVED) {
149-
zpci_zdev_put(zdev);
149+
zpci_device_reserved(zdev);
150150
}
151151
break;
152152
case 0x0306: /* 0x308 or 0x302 for multiple devices */
@@ -156,7 +156,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
156156
case 0x0308: /* Standby -> Reserved */
157157
if (!zdev)
158158
break;
159-
zpci_zdev_put(zdev);
159+
zpci_device_reserved(zdev);
160160
break;
161161
default:
162162
break;

‎drivers/pci/hotplug/s390_pci_hpc.c‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,7 @@ static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
109109
struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
110110
hotplug_slot);
111111

112-
switch (zdev->state) {
113-
case ZPCI_FN_STATE_STANDBY:
114-
*value = 0;
115-
break;
116-
default:
117-
*value = 1;
118-
break;
119-
}
112+
*value = zpci_is_device_configured(zdev) ? 1 : 0;
120113
return 0;
121114
}
122115

0 commit comments

Comments
 (0)