s390/pci: fix zpci_zdev_put() on reserve
cover s390/pci: fix zpci_zdev_put() on reserve
Hi Greg, Hi Sasha,
Please take this backport of the upstream commit a46044a92add ("s390/pci: fix zpci_zdev_put() on reserve") for the v5.10 stable series. Unlike the v5.14 series we do *NOT* include commit 023cc3cb1e4b ("s390/pci: cleanup resources only if necessary") either as a separate commit subsumed. We don't yet have the zdev->has_resource attribute that was added as part of v5.13 and do not need it for this fix.
Thanks,
Niklas
Niklas Schnelle (1):
s390/pci: fix zpci_zdev_put() on reserve
arch/s390/include/asm/pci.h | 3 ++ arch/s390/pci/pci.c | 45 ++++++++++++++++++++++++++---- arch/s390/pci/pci_event.c | 4 +-- drivers/pci/hotplug/s390_pci_hpc.c | 9 +----- 4 files changed, 46 insertions(+), 15 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/1 s390/pci: fix zpci_zdev_put() on reserve
commit a46044a92add6a400f4dada7b943b30221f7cc80 upstream.
Since commit 2a671f77ee49 ("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.
--- arch/s390/include/asm/pci.h | 3 ++ arch/s390/pci/pci.c | 45 ++++++++++++++++++++++++++---- arch/s390/pci/pci_event.c | 4 +-- drivers/pci/hotplug/s390_pci_hpc.c | 9 +----- 4 files changed, 46 insertions(+), 15 deletions(-)
arch/s390/include/asm/pci.h +3 -0
@@ -205,6 +205,9 @@ int zpci_create_device(u32 fid, u32 fh, enum zpci_state state);void zpci_remove_device (struct zpci_dev *zdev,bool set_error);int zpci_enable_device (struct zpci_dev *);int zpci_disable_device (struct zpci_dev *); +void zpci_device_reserved (struct zpci_dev *zdev); +bool zpci_is_device_configured (struct zpci_dev *zdev); +int zpci_register_ioat (struct zpci_dev *,u8 ,u64 ,u64 ,u64 );int zpci_unregister_ioat (struct zpci_dev *,u8 );void zpci_remove_reserved_devices (void );
arch/s390/pci/pci.c +40 -5
69 lines not on this page · raw message
arch/s390/pci/pci_event.c +2 -2
@@ -146,7 +146,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf) zdev->state =ZPCI_FN_STATE_STANDBY ;if (!clp_get_state (ccdf->fid, &state) && state ==ZPCI_FN_STATE_RESERVED ) { -zpci_zdev_put (zdev); +zpci_device_reserved (zdev); }break ;case 0x0306 :/* 0x308 or 0x302 for multiple devices */
@@ -156,7 +156,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)case 0x0308 :/* Standby -> Reserved */ if (!zdev)break ; -zpci_zdev_put (zdev); +zpci_device_reserved (zdev);break ;default :break ;
drivers/pci/hotplug/s390_pci_hpc.c +1 -8
@@ -109,14 +109,7 @@ static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value) struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev, hotplug_slot); -switch (zdev->state) { -case ZPCI_FN_STATE_STANDBY : - *value =0 ; -break ; -default : - *value =1 ; -break ; - } + *value =zpci_is_device_configured (zdev) ?1 :0 ;return 0 ; }
Signature · 1 line
2.25.1
Greg KH
maintainerQuoted 30 lines · Niklas Schnelle
commit
a46044a92add6a400f4dada7b943b30221f7cc80upstream.Since commit
2a671f77ee49("s390/pci: fix use after free ofzpci_dev") the reference count of azpci_devis incremented betweenpcibios_add_device()andpcibios_release_device()which was supposed to prevent thezpci_devfrom being freed while the common PCI code has access to it. It was missed however that the handling of zPCI availability events assumed that oncezpci_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 thezpci_devwill not be considered for further events by removing it from thezpci_list.This also means that the device actually stays in the
ZPCI_FN_STATE_RESERVEDstate 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 inzpci_release_device()to make sure holders of the reference do see valid data until the release.Fixes 2a671f77ee49 ("s390/pci: fix use after free of zpci_dev")Signed-off-by Niklas Schnelle <[email protected]>Signed-off-by Vasily Gorbik <[email protected]>
Quoted 6 lines
--- arch/s390/include/asm/pci.h | 3 ++ arch/s390/pci/pci.c | 45 ++++++++++++++++++++++++++---- arch/s390/pci/pci_event.c | 4 +-- drivers/pci/hotplug/s390_pci_hpc.c | 9 +----- 4 files changed, 46 insertions(+), 15 deletions(-)
Does not apply:
Applying patch s390-pci-fix-zpci_zdev_put-on-reserve.patch patching file arch/s390/include/asm/pci.h
patching file arch/s390/pci/pci.c
Hunk #3 FAILED at 835.
Hunk #4 succeeded at 843 (offset 1 line).
1 out of 4 hunks FAILED -- rejects in file arch/s390/pci/pci.c patching file arch/s390/pci/pci_event.c
patching file drivers/pci/hotplug/s390_pci_hpc.c
What did you make this against?
Ah, that's due to another patch we have in the queue right now. I'll go fix this up by hand, thanks!
greg k-h
Quoted 50 lines · Greg KH
Quoted 30 lines · Niklas Schnelle
commit
a46044a92add6a400f4dada7b943b30221f7cc80upstream.Since commit
2a671f77ee49("s390/pci: fix use after free ofzpci_dev") the reference count of azpci_devis incremented betweenpcibios_add_device()andpcibios_release_device()which was supposed to prevent thezpci_devfrom being freed while the common PCI code has access to it. It was missed however that the handling of zPCI availability events assumed that oncezpci_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 thezpci_devwill not be considered for further events by removing it from thezpci_list.This also means that the device actually stays in the
ZPCI_FN_STATE_RESERVEDstate 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 inzpci_release_device()to make sure holders of the reference do see valid data until the release.Fixes 2a671f77ee49 ("s390/pci: fix use after free of zpci_dev")Signed-off-by Niklas Schnelle <[email protected]>Signed-off-by Vasily Gorbik <[email protected]>Quoted 6 lines
--- arch/s390/include/asm/pci.h | 3 ++ arch/s390/pci/pci.c | 45 ++++++++++++++++++++++++++---- arch/s390/pci/pci_event.c | 4 +-- drivers/pci/hotplug/s390_pci_hpc.c | 9 +----- 4 files changed, 46 insertions(+), 15 deletions(-)Does not apply:
Applying patch s390-pci-fix-zpci_zdev_put-on-reserve.patch patching file
arch/s390/include/asm/pci.h
patching filearch/s390/pci/pci.c
Hunk #3 FAILED at 835.
Hunk #4 succeeded at 843 (offset 1 line).
1 out of 4 hunks FAILED -- rejects in filearch/s390/pci/pci.cpatching filearch/s390/pci/pci_event.c
patching filedrivers/pci/hotplug/s390_pci_hpc.c
Quoted 6 lines
What did you make this against?
Ah, that's due to another patch we have in the queue right now. I'll go fix this up by hand, thanks!
greg k-h
Yes it's due to "s390/pci: cleanup resources only if necessary" you need to drop that and keep this one.