pci: clean all funcs when hot-removing multifunc device
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 pci: clean all funcs when hot-removing multifunc device
----- Original Message -----
'slot->funcs' is initialized in acpiphp_glue.c:register_slot() before hotpluging device, and only one entry(func 0) is added to it, no new entry will be added to the list when hotpluging devices to the slot. When we release the whole device, there is only one entry in the list, this causes func1~7 could not be released.
I try to add entries for all hotpluged device in enable_device(), but it doesn't work, because 'slot->funcs' is used in many place which we only need to process func 0. This patch just try to clean all funcs in disable_device().
drivers/pci/hotplug/acpiphp_glue.c:
static int disable_device(struct acpiphp_slot *slot) {
list_for_each_entry(func, &slot->funcs, sibling) {
pdev = pci_get_slot(slot->bridge->pci_bus,
PCI_DEVFN(slot->device, func->function));
..clean code.. // those code can only be executed one time(func 0)
pci_remove_bus_device(pdev);---pci_bus_add_device() is called for each func device in acpiphp_glue.c:enable_device(). pci_remove_bus_device(pdev) is only called for func 0 in acpiphp_glue.c:disable_device().
Boot up a KVM guest, hotplug a multifunc device(8 funcs), we can find it in the guest. @ ls /dev/vd*
vda vdb vdc vde vdf vdg vdh
@ lspci
00:06.0 SCSI storage controller: Red Hat, Inc Virtio block device
...
00:06.7 SCSI storage controller: Red Hat, Inc Virtio block device
But func 1~7 still exist in guest after hot-removing the multifunc device through qemu monitor.
@ lspci (00:06.0 disappeared)
00:06.1 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff)
...
00:06.7 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff)
^^^^^^^^ @ ls /dev/vd* vdb vdc vde vdf vdg vdh @ mkfs /dev/vdb INFO: task mkfs.ext2:1784 blocked for more than 120 seconds. (task hung)
Hotpluging multifunc of WinXp is fine.
--- drivers/pci/hotplug/acpiphp_glue.c | 27 ++++++++++++++++++--------- 1 files changed, 18 insertions(+), 9 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +18 -9
@@ -880,6 +880,8 @@ static int disable_device(struct acpiphp_slot *slot) {struct acpiphp_func *func;struct pci_dev *pdev; +struct pci_bus *bus = slot->bridge->pci_bus; +int i, num =1 ;/* is this slot already disabled? */ if (!(slot->flags &SLOT_ENABLED ))
@@ -893,16 +895,23 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { -pci_stop_bus_device (pdev); -if (pdev->subordinate) { -disable_bridges (pdev->subordinate); -pci_disable_device (pdev); + pdev =pci_scan_single_device (bus, +PCI_DEVFN (slot->device,0 )); +if (!pdev) + goto err_exit; +if (pdev->multifunction ==1 ) + num =8 ; +for (i=0 ; i<num; i++) { + pdev =pci_get_slot (bus,PCI_DEVFN (slot->device, i)); +if (pdev) { +pci_stop_bus_device (pdev); +if (pdev->subordinate) { +disable_bridges (pdev->subordinate); +pci_disable_device (pdev); + } +pci_remove_bus_device (pdev); +pci_dev_put (pdev); } -pci_remove_bus_device (pdev); -pci_dev_put (pdev); } }
Signature · 1 line
1.7.6.1
Bjorn Helgaas
maintainerQuoted 9 lines · Amos Kong
'
slot->funcs' is initialized in acpiphp_glue.c:register_slot() before hotpluging device, and only one entry(func 0) is added to it, no new entry will be added to the list when hotpluging devices to the slot. When we release the whole device, there is only one entry in the list, this causes func1~7 could not be released.
I try to add entries for all hotpluged device inenable_device(), but it doesn't work, because 'slot->funcs' is used in many place which we only need to process func 0. This patch just try to clean all funcs indisable_device().
...
Quoted 1 line
Hotpluging multifunc of WinXp is fine.
I'm going to ignore this patch for now. Please consider these questions, then repost it if you still want it:
I assume you mean that Linux and WinXP are both running on top of the same SeaBIOS, and hot-remove of a multifunction device works in WinXP and fails in Linux. That sounds like Linux is broken, and we should fix it. We might want to make a SeaBIOS change for other reasons, but it'd still be good to fix Linux in case there are other similar BIOSes.
Why do we need pci_scan_single_device()? The device should have been scanned already when it was added, and I would think that should have set pdev->multifunction.
Your patch needs spaces around the operators in the "for" loop.
In the changelog, it would be nice to have the URL of a bugzilla where the dmesg and DSDT are attached.
Bjorn
Quoted 59 lines
Signed-off-by Amos Kong <[email protected]>---
drivers/pci/hotplug/acpiphp_glue.c | 27 ++++++++++++++++++---------1 files changed, 18 insertions(+), 9 deletions(-)drivers/pci/hotplug/acpiphp_glue.c +18
-10@@ -880,6 +880,8 @@ static int disable_device(struct acpiphp_slot *slot) { struct acpiphp_func *func; struct pci_dev *pdev; + struct pci_bus *bus = slot->bridge->pci_bus; + int i, num = 1; /* is this slot already disabled? */ if (!(slot->flags & SLOT_ENABLED))@@ -893,16 +895,23 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge = NULL; } - pdev = pci_get_slot(slot->bridge->pci_bus, - PCI_DEVFN(slot->device, func->function)); - if (pdev) { - pci_stop_bus_device(pdev); - if (pdev->subordinate) { - disable_bridges(pdev->subordinate); - pci_disable_device(pdev); + pdev = pci_scan_single_device(bus, + PCI_DEVFN(slot->device, 0)); + if (!pdev) + goto err_exit; + if (pdev->multifunction == 1) + num = 8; + for (i=0; i<num; i++) { + pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, i)); + if (pdev) { + pci_stop_bus_device(pdev); + if (pdev->subordinate) { + disable_bridges(pdev->subordinate); + pci_disable_device(pdev); + } + pci_remove_bus_device(pdev); + pci_dev_put(pdev); } - pci_remove_bus_device(pdev); - pci_dev_put(pdev); } } --1.7.6.1
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 16 lines · Bjorn Helgaas
Quoted 9 lines · Amos Kong
'
slot->funcs' is initialized in acpiphp_glue.c:register_slot() before hotpluging device, and only one entry(func 0) is added to it, no new entry will be added to the list when hotpluging devices to the slot. When we release the whole device, there is only one entry in the list, this causes func1~7 could not be released.
I try to add entries for all hotpluged device inenable_device(), but it doesn't work, because 'slot->funcs' is used in many place which we only need to process func 0. This patch just try to clean all funcs indisable_device()....
Quoted 1 line
Hotpluging multifunc of WinXp is fine.
I'm going to ignore this patch for now. Please consider these questions, then repost it if you still want it:
Quoted 6 lines
I assume you mean that Linux and WinXP are both running on top of the same SeaBIOS, and hot-remove of a multifunction device works in WinXP and fails in Linux. That sounds like Linux is broken, and we should fix it. We might want to make a SeaBIOS change for other reasons, but it'd still be good to fix Linux in case there are other similar BIOSes.
No objection about fixing Linux.
Quoted 4 lines
Why do we need
pci_scan_single_device()? The device should have been scanned already when it was added, and I would think that should have setpdev->multifunction.
It should be pci_get_slot() instead. Note that it needs corresponding pci_dev_put().
Regards,
Kenji Kaneshige
Quoted 69 lines
Your patch needs spaces around the operators in the "for" loop.
In the changelog, it would be nice to have the URL of a bugzilla where the dmesg and DSDT are attached.
Bjorn
Quoted 59 lines
Signed-off-by Amos Kong<[email protected]>--- drivers/pci/hotplug/acpiphp_glue.c | 27 ++++++++++++++++++--------- 1 files changed, 18 insertions(+), 9 deletions(-)drivers/pci/hotplug/acpiphp_glue.c +18
-10@@ -880,6 +880,8 @@ static int disable_device(struct acpiphp_slot *slot) {struct acpiphp_func *func;struct pci_dev *pdev; +struct pci_bus *bus = slot->bridge->pci_bus; +int i, num =1 ;/* is this slot already disabled? */ if (!(slot->flags&SLOT_ENABLED ))@@ -893,16 +895,23 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { -pci_stop_bus_device (pdev); -if (pdev->subordinate) { -disable_bridges (pdev->subordinate); -pci_disable_device (pdev); + pdev =pci_scan_single_device (bus, +PCI_DEVFN (slot->device,0 )); +if (!pdev) + goto err_exit; +if (pdev->multifunction ==1 ) + num =8 ; +for (i=0 ; i<num; i++) { + pdev =pci_get_slot (bus,PCI_DEVFN (slot->device, i)); +if (pdev) { +pci_stop_bus_device (pdev); +if (pdev->subordinate) { +disable_bridges (pdev->subordinate); +pci_disable_device (pdev); + } +pci_remove_bus_device (pdev); +pci_dev_put (pdev); } -pci_remove_bus_device (pdev); -pci_dev_put (pdev); } } --1.7.6.1
--
To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v2 1/1 pci: clean all funcs when hot-removing multifunc device
In reply to a message not in the archiveRHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=737720#c8
(dmesg and DSDT were attached in bz)
Boot up a Linux VM with 8 pci block devices which
are the 8 functions in one pci slot.
| # qemu-kvm ... | -drive file=images/u0,if=none,id=drv0,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv0,id=v0,multifunction=on,addr=0x03.0 \ | .... | -drive file=images/u7,if=none,id=drv7,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv7,id=v7,multifunction=on,addr=0x03.7 \
Check devices in guest.
| vm)# ls /dev/vd* | vda vdb vdc vde vdf vdg vdh | vm)# lspci |grep block | 00:03.0 SCSI storage controller: Red Hat, Inc Virtio block device | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device |
Func1~7 still exist in guest after hot-removing the whole slot by qemu monitor cmd.
| vm)# lspci |grep block (00:03.0 disappeared) | 00:03.1 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | vm)# ls /dev/vd* (vda disappeared) | vdb vdc vde vdf vdg vdh | vm)# mkfs /dev/vdb | INFO: task mkfs.ext2:1784 blocked for more than 120 seconds.
We process pci slot as a whole device, in seabios only defines one device for a slot in ACPI DSDT table, then there is only one entry (for func#0) would be added into 'slot->funcs' list.
When we release the whole slot, only the entry in 'slot->funcs' will be cleaned, so func#1~7 could not be cleaned from system.
| drivers/pci/hotplug/acpiphp_glue.c:
| static int disable_device(struct acpiphp_slot *slot) {
| list_for_each_entry(func, &slot->funcs, sibling) {
| pdev = pci_get_slot(slot->bridge->pci_bus,
| PCI_DEVFN(slot->device, func->function));
| ..clean code.. // those code is only executed 1 time(for func#0)
| __pci_remove_bus_device(pdev);
| pci_dev_put(pdev);I try to add entries for all hotpluged device in enable_device(), but it doesn't work, because 'slot->funcs' is used in many place which we only need to process func#0. This patch just try to clean all funcs in disable_device().
Hotpluging multifunc of guests(WinXp/Win7) is fine.
---
v1 thread: http://marc.info/?t=131597601700003&r=1&w=2
Changes from v1:
- rebase patch to latest linux.git
- remove unnecessary multiplefunction check
- rename 'i' to meaningful 'fn'
- fix coding style
--- drivers/pci/hotplug/acpiphp_glue.c | 20 +++++++++++--------- 1 files changed, 11 insertions(+), 9 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +11 -9
@@ -887,6 +887,7 @@ static int disable_device(struct acpiphp_slot *slot)struct acpiphp_func *func;struct pci_dev *pdev;struct pci_bus *bus = slot->bridge->pci_bus; +int fn;/* The slot will be enabled when func 0 is added, so check func 0 before disable the slot. */
@@ -902,16 +903,17 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { -pci_stop_bus_device (pdev); -if (pdev->subordinate) { -disable_bridges (pdev->subordinate); -pci_disable_device (pdev); +for (fn =0 ; fn <8 ; fn++) { + pdev =pci_get_slot (bus,PCI_DEVFN (slot->device, fn)); +if (pdev) { +pci_stop_bus_device (pdev); +if (pdev->subordinate) { +disable_bridges (pdev->subordinate); +pci_disable_device (pdev); + } +__pci_remove_bus_device (pdev); +pci_dev_put (pdev); } -__pci_remove_bus_device (pdev); -pci_dev_put (pdev); } }
PATCH v3 1/1 pci: clean all funcs when hot-removing multifunc device
In reply to a message not in the archiveHotplug CallTrace:
int acpiphp_enable_slot(struct acpiphp_slot *slot)
\_enable_device(slot);
\_pci_bus_add_devices(bus);
# un-added new devs(all funcs in slot) will be added
list_for_each_entry(dev, &bus->devices, bus_list) {
if (dev->is_added)
continue;
pci_bus_add_device(dev);
device_add(&dev->dev);
dev->is_added = 1;'dev->is_added' is used to trace if pci dev is added to bus, all funcs in same slot would be added to bus in enable_device(slot). So we need to clean all funcs of same slot in disable_device(slot).
But hot-remove exists bug: https://bugzilla.kernel.org/show_bug.cgi?id=43219
(dmesg and DSDT were attached in bz), detail:
. Boot up a Linux VM with 8 pci block devices which are the 8 functions in one pci slot.
| # qemu-kvm ... | -drive file=images/u0,if=none,id=drv0,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv0,id=v0,multifunction=on,addr=0x03.0 \ | .... | -drive file=images/u7,if=none,id=drv7,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv7,id=v7,multifunction=on,addr=0x03.7 \
. Check devices in guest.
| vm)# ls /dev/vd* | vda vdb vdc vde vdf vdg vdh | vm)# lspci |grep block | 00:03.0 SCSI storage controller: Red Hat, Inc Virtio block device | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device |
. Func1~7 still exist in guest after hot-removing the whole slot by qemu monitor cmd.
| vm)# lspci |grep block (00:03.0 disappeared) | 00:03.1 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | vm)# ls /dev/vd* (vda disappeared) | vdb vdc vde vdf vdg vdh | vm)# mkfs /dev/vdb | INFO: task mkfs.ext2:1784 blocked for more than 120 seconds.
We process pci slot as a whole device in pciphp spec, seabios only defines one device for a slot in ACPI DSDT table.
In acpiphp_glue.c:register_slot(), only one entry is added (for func#0) into 'slot->funcs' list. When we release the whole slot, only the entry in 'slot->funcs' will be cleaned, so func#1~7 could not be cleaned from system.
| drivers/pci/hotplug/acpiphp_glue.c:
| static int disable_device(struct acpiphp_slot *slot) {
| list_for_each_entry(func, &slot->funcs, sibling) {
| pdev = pci_get_slot(slot->bridge->pci_bus,
| PCI_DEVFN(slot->device, func->function));
| ..clean code.. // those code is only executed 1 time(for func#0)
| __pci_remove_bus_device(pdev);
| pci_dev_put(pdev);Hotpluging multifunc of guests(WinXp/Win7) is fine.
---
v1 thread: http://marc.info/?t=131597601700003&r=1&w=2
Changes from v1:
- rebase patch to latest linux.git
- remove unnecessary multiplefunction check
- rename 'i' to meaningful 'fn'
- fix coding style
Changes from v2:
- update detail reason(calltrace) to commitlog
- remove hardcode 8, find funcs in pci devlist
--- drivers/pci/hotplug/acpiphp_glue.c | 9 +++++---- 1 files changed, 5 insertions(+), 4 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +5 -4
@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check
@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate);
Quoted 14 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c +4
-3@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +
The pci_bus_sem lock should be acquired when walking the bus->devices list. Otherwise it may cause invalid memory access if another thread is modifying the bus->devices list concurrently.
BTW, what's the relationship with "[PATCH v3] hotplug: add device per func in ACPI DSDT tables"? Seems they are both solving the same issue.
Quoted 8 lines
drivers/pci/hotplug/acpiphp_glue.c +0
-1pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate); --To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 30 lines · Jiang Liu
Quoted 14 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c +4
-3@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
Quoted 2 lines
BTW, what's the relationship with "[PATCH v3] hotplug: add device per func in ACPI DSDT tables"? Seems they are both solving the same issue.
That's a bios patch. It's needed if you want broken linux to work. This makes linux behave properly on the original bios.
Quoted 8 lines
Quoted 8 lines
drivers/pci/hotplug/acpiphp_glue.c +0
-1pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate); --To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 32 lines · Michael S. Tsirkin
Quoted 29 lines · Jiang Liu
Quoted 14 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c +4
-3@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
Quoted 2 lines
Quoted 2 lines
BTW, what's the relationship with "[PATCH v3] hotplug: add device per func in ACPI DSDT tables"? Seems they are both solving the same issue.
Two work need to be done when we disable a slot, cleaning configuration(in OS) and power off slot.
Currently the second part(power off) works fine, all funcs disappear from "(qemu)#info block" after hot-remove slot.
The only problem is func 1~7 are not unconfigured, so I NAKed seabios patch, and try to fix this problem in pci driver.
(btw, winxp & win7 hotplug works currently)
/**
* acpiphp_disable_slot - power off slot
* @slot: ACPI PHP slot
*/
int acpiphp_disable_slot(struct acpiphp_slot *slot)
{
mutex_lock(&slot->crit_sect);
/* unconfigure all functions */
retval = disable_device(slot);
/* power off all functions */
retval = power_off_slot(slot);
....
}Quoted 6 lines
That's a bios patch. It's needed if you want broken linux to work. This makes linux behave properly on the original bios.
Quoted 3 lines
Quoted 3 lines
drivers/pci/hotplug/acpiphp_glue.c +0
-0pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate);
Signature · 1 line
Amos.
Quoted 21 lines · Amos Kong
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jiang Liu
Quoted 17 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check @@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; }
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 7 lines
drivers/pci/hotplug/acpiphp_glue.c +4
-3- pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
pci_bus_sem lock is only request for writing &bus->devices list, right ? and this protection already exists in pci_destory_dev().
static int disable_device(struct acpiphp_slot *slot)
\_ list_for_each_entry_safe(pdev, tmp, &bus->devices, bus_list) {
\_ __pci_remove_bus_device(pdev);
\_ pci_destroy_dev(dev);static void pci_destroy_dev(struct pci_dev *dev)
{
/* Remove the device from the device lists, and prevent any further
* list accesses from this device */
down_write(&pci_bus_sem);
list_del(&dev->bus_list);
dev->bus_list.next = dev->bus_list.prev = NULL;
up_write(&pci_bus_sem);
...
}Signature · 1 line
Amos.
Quoted 33 lines · Amos Kong
Quoted 21 lines · Amos Kong
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jiang Liu
Quoted 17 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus)static int disable_device (struct acpiphp_slot *slot) {struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp;struct pci_bus *bus = slot->bridge->pci_bus;/* The slot will be enabled when func 0 is added, so check @@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; }Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 7 lines
drivers/pci/hotplug/acpiphp_glue.c +4
-3- pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +list_for_each_entry_safe (pdev, tmp, &bus->devices, bus_list) { +if (PCI_SLOT (pdev->devfn) != slot->device) +continue ; +The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
Quoted 2 lines
pci_bus_semlock is only request for writing&bus->deviceslist, right ? and this protection already exists inpci_destory_dev().
That's for writer. For reader to walk the pci_bus->devices list, you also need to acquire the reader lock by down_read(&pci_bus_sem). Please refer to pci_get_slot() for example. This especially import for native OS because there may be multiple PCI slots/devices on the bus.
Bjorn Helgaas
maintainerQuoted 23 lines · Jiang Liu
Quoted 22 lines · Amos Kong
Quoted 21 lines · Amos Kong
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jiang Liu
Quoted 17 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus) static int disable_device(struct acpiphp_slot *slot) { struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp; struct pci_bus *bus = slot->bridge->pci_bus; /* The slot will be enabled when func 0 is added, so check@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge = NULL; }
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 907
Quoted 6 lines
drivers/pci/hotplug/acpiphp_glue.c +3
-3- pdev = pci_get_slot(slot->bridge->pci_bus, - PCI_DEVFN(slot->device, func->function)); - if (pdev) { + list_for_each_entry_safe(pdev, tmp, &bus->devices, bus_list) { + if (PCI_SLOT(pdev->devfn) != slot->device) + continue;
I think the concept is good: in enable_device(), we use pci_scan_slot(), which scans all possible functions in the slot. So in disable_device() we should do something symmetric to remove all the functions.
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 1 line
drivers/pci/hotplug/acpiphp_glue.c +1
-0+The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
pci_bus_semlock is only request for writing&bus->deviceslist, right ? and this protection already exists inpci_destory_dev().That's for writer. For reader to walk the
pci_bus->deviceslist, you also need to acquire the reader lock bydown_read(&pci_bus_sem). Please refer topci_get_slot()for example. This especially import for native OS because there may be multiple PCI slots/devices on the bus.
There is a lot of existing code that walks bus->devices without holding pci_bus_sem, but most of it is boot-time code that is arguably safe (though I think things like pcibios_fixup_bus() are poorly designed and don't fit well in the hotplug-enabled world).
In this case, I do think we need to protect against updates while we're walking bus->devices. It's probably not trivial because __pci_remove_bus_device() calls pci_destroy_dev(), where we do the down_write(), so simply wrapping the whole thing with down_read() will cause a deadlock.
Kenji-san, Yinghai, do you have any input?
Bjorn
PATCH v4 1/1 pci: clean all funcs when hot-removing multifunc device
From: Amos Kong <[email protected]>
Hotplug CallTrace:
int acpiphp_enable_slot(struct acpiphp_slot *slot)
\_enable_device(slot);
\_pci_bus_add_devices(bus);
# un-added new devs(all funcs in slot) will be added
list_for_each_entry(dev, &bus->devices, bus_list) {
if (dev->is_added)
continue;
pci_bus_add_device(dev);
device_add(&dev->dev);
dev->is_added = 1;'dev->is_added' is used to trace if pci dev is added to bus, all funcs in same slot would be added to bus in enable_device(slot). So we need to clean all funcs of same slot in disable_device(slot).
But hot-remove exists bug: https://bugzilla.kernel.org/show_bug.cgi?id=43219
(dmesg and DSDT were attached in bz), detail:
. Boot up a Linux VM with 8 pci block devices which are the 8 functions in one pci slot.
| # qemu-kvm ... | -drive file=images/u0,if=none,id=drv0,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv0,id=v0,multifunction=on,addr=0x03.0 \ | .... | -drive file=images/u7,if=none,id=drv7,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv7,id=v7,multifunction=on,addr=0x03.7 \
. Check devices in guest.
| vm)# ls /dev/vd* | vda vdb vdc vde vdf vdg vdh | vm)# lspci |grep block | 00:03.0 SCSI storage controller: Red Hat, Inc Virtio block device | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device |
. Func1~7 still exist in guest after hot-removing the whole slot by qemu monitor cmd.
| vm)# lspci |grep block (00:03.0 disappeared) | 00:03.1 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | vm)# ls /dev/vd* (vda disappeared) | vdb vdc vde vdf vdg vdh | vm)# mkfs /dev/vdb | INFO: task mkfs.ext2:1784 blocked for more than 120 seconds.
We process pci slot as a whole device in pciphp spec, seabios only defines one device for a slot in ACPI DSDT table.
In acpiphp_glue.c:register_slot(), only one entry is added (for func#0) into 'slot->funcs' list. When we release the whole slot, only the entry in 'slot->funcs' will be cleaned, so func#1~7 could not be cleaned from system.
| drivers/pci/hotplug/acpiphp_glue.c:
| static int disable_device(struct acpiphp_slot *slot) {
| list_for_each_entry(func, &slot->funcs, sibling) {
| pdev = pci_get_slot(slot->bridge->pci_bus,
| PCI_DEVFN(slot->device, func->function));
| ..clean code.. // those code is only executed 1 time(for func#0)
| __pci_remove_bus_device(pdev);
| pci_dev_put(pdev);Hotpluging multifunc of guests(WinXp/Win7) is fine.
---
v1 thread: http://marc.info/?t=131597601700003&r=1&w=2
Changes from v1:
- rebase patch to latest linux.git
- remove unnecessary multiplefunction check
- rename 'i' to meaningful 'fn'
- fix coding style
Changes from v2:
- update detail reason(calltrace) to commitlog
- remove hardcode 8, find funcs in pci devlist
Changes from v3:
- use pci_bus_sem lock when walking bus->devices list
--- drivers/pci/hotplug/acpiphp_glue.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +15 -3
@@ -893,6 +893,7 @@ static int disable_device(struct acpiphp_slot *slot) pdev =pci_get_slot (bus,PCI_DEVFN (slot->device,0 ));if (!pdev) goto err_exit; +pci_dev_put (pdev);list_for_each_entry (func, &slot->funcs, sibling) {if (func->bridge) {
@@ -902,9 +903,20 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +while (1 ) { +/* pci_bus_sem is used to protect bus->devices list, +it may cause invalid memory access if threads +modify bus->devices list concurrently. */ +down_read (&pci_bus_sem); +list_for_each_entry (pdev, &bus->devices, bus_list) +if (PCI_SLOT (pdev->devfn) == slot->device) { +pci_dev_get (pdev); +break ; + } +up_read (&pci_bus_sem); + +if (PCI_SLOT (pdev->devfn) != slot->device) +break ;pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate);
Signature · 1 line
1.7.9.5
Quoted 24 lines · Bjorn Helgaas
Quoted 23 lines · Jiang Liu
Quoted 22 lines · Amos Kong
Quoted 21 lines · Amos Kong
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jiang Liu
Quoted 17 lines · Amos Kong
drivers/pci/hotplug/acpiphp_glue.c +1
-1@@ -885,7 +885,7 @@ static void disable_bridges(struct pci_bus *bus) static int disable_device(struct acpiphp_slot *slot) { struct acpiphp_func *func; -struct pci_dev *pdev; +struct pci_dev *pdev, *tmp; struct pci_bus *bus = slot->bridge->pci_bus; /* The slot will be enabled when func 0 is added, so check@@ -902,9 +902,10 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge = NULL; }
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 907
Quoted 6 lines
drivers/pci/hotplug/acpiphp_glue.c +3
-3- pdev = pci_get_slot(slot->bridge->pci_bus, - PCI_DEVFN(slot->device, func->function)); - if (pdev) { + list_for_each_entry_safe(pdev, tmp, &bus->devices, bus_list) { + if (PCI_SLOT(pdev->devfn) != slot->device) + continue;I think the concept is good: in
enable_device(), we usepci_scan_slot(), which scans all possible functions in the slot. So indisable_device()we should do something symmetric to remove all the functions.
Right!
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
drivers/pci/hotplug/acpiphp_glue.c static int disable_device(struct acpiphp_slot *slot) · line 908
Quoted 1 line
drivers/pci/hotplug/acpiphp_glue.c +1
-0+The
pci_bus_semlock should be acquired when walking thebus->deviceslist. Otherwise it may cause invalid memory access if another thread is modifying thebus->deviceslist concurrently.
pci_bus_semlock is only request for writing&bus->deviceslist, right ? and this protection already exists inpci_destory_dev().That's for writer. For reader to walk the
pci_bus->deviceslist, you also need to acquire the reader lock bydown_read(&pci_bus_sem). Please refer topci_get_slot()for example. This especially import for native OS because there may be multiple PCI slots/devices on the bus.
Quoted 4 lines
There is a lot of existing code that walks
bus->deviceswithout holdingpci_bus_sem, but most of it is boot-time code that is arguably safe (though I think things likepcibios_fixup_bus()are poorly designed and don't fit well in the hotplug-enabled world).
disable_remove() is not boot-time code, we might hot-remove devices when system is running.
Quoted 5 lines
In this case, I do think we need to protect against updates while we're walking
bus->devices. It's probably not trivial because__pci_remove_bus_device()callspci_destroy_dev(), where we do thedown_write(), so simply wrapping the whole thing withdown_read()will cause a deadlock.
I posted a V4 to add pci_bus_sem protection , please help to review. Thanks for Jiang Liu's guide.
Quoted 3 lines
Kenji-san, Yinghai, do you have any input?
Bjorn
PATCH v5 1/1 pci: clean all funcs when hot-removing multifunc device
In reply to Bjorn HelgaasFrom: Amos Kong <[email protected]>
Hotplug CallTrace:
int acpiphp_enable_slot(struct acpiphp_slot *slot)
\_enable_device(slot);
\_pci_bus_add_devices(bus);
# un-added new devs(all funcs in slot) will be added
list_for_each_entry(dev, &bus->devices, bus_list) {
if (dev->is_added)
continue;
pci_bus_add_device(dev);
device_add(&dev->dev);
dev->is_added = 1;'dev->is_added' is used to trace if pci dev is added to bus, all funcs in same slot would be added to bus in enable_device(slot). So we need to clean all funcs of same slot in disable_device(slot).
But hot-remove exists bug: https://bugzilla.kernel.org/show_bug.cgi?id=43219
(dmesg and DSDT were attached in bz), detail:
. Boot up a Linux VM with 8 pci block devices which are the 8 functions in one pci slot.
| # qemu-kvm ... | -drive file=images/u0,if=none,id=drv0,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv0,id=v0,multifunction=on,addr=0x03.0 \ | .... | -drive file=images/u7,if=none,id=drv7,format=qcow2,cache=none \ | -device virtio-blk-pci,drive=drv7,id=v7,multifunction=on,addr=0x03.7 \
. Check devices in guest.
| vm)# ls /dev/vd* | vda vdb vdc vde vdf vdg vdh | vm)# lspci |grep block | 00:03.0 SCSI storage controller: Red Hat, Inc Virtio block device | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device |
. Func1~7 still exist in guest after hot-removing the whole slot by qemu monitor cmd.
| vm)# lspci |grep block (00:03.0 disappeared) | 00:03.1 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | ... | 00:03.7 SCSI storage controller: Red Hat, Inc Virtio block device (rev ff) | vm)# ls /dev/vd* (vda disappeared) | vdb vdc vde vdf vdg vdh | vm)# mkfs /dev/vdb | INFO: task mkfs.ext2:1784 blocked for more than 120 seconds.
We process pci slot as a whole device in pciphp spec, seabios only defines one device for a slot in ACPI DSDT table.
In acpiphp_glue.c:register_slot(), only one entry is added (for func#0) into 'slot->funcs' list. When we release the whole slot, only the entry in 'slot->funcs' will be cleaned, so func#1~7 could not be cleaned from system.
| drivers/pci/hotplug/acpiphp_glue.c:
| static int disable_device(struct acpiphp_slot *slot) {
| list_for_each_entry(func, &slot->funcs, sibling) {
| pdev = pci_get_slot(slot->bridge->pci_bus,
| PCI_DEVFN(slot->device, func->function));
| ..clean code.. // those code is only executed 1 time(for func#0)
| __pci_remove_bus_device(pdev);
| pci_dev_put(pdev);Hotpluging multifunc of guests(WinXp/Win7) is fine.
Changes from v1:
- rebase patch to latest linux.git
- remove unnecessary multiplefunction check
- rename 'i' to meaningful 'fn'
- fix coding style
Changes from v2:
- update detail reason(calltrace) to commitlog
- remove hardcode 8, find funcs in pci devlist
Changes from v3:
- use pci_bus_sem lock when walking bus->devices list
Changes from V4:
- check null point of pdev
--- drivers/pci/hotplug/acpiphp_glue.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +15 -3
@@ -893,6 +893,7 @@ static int disable_device(struct acpiphp_slot *slot) pdev =pci_get_slot (bus,PCI_DEVFN (slot->device,0 ));if (!pdev) goto err_exit; +pci_dev_put (pdev);list_for_each_entry (func, &slot->funcs, sibling) {if (func->bridge) {
@@ -902,9 +903,20 @@ static int disable_device(struct acpiphp_slot *slot) func->bridge =NULL ; } - pdev =pci_get_slot (slot->bridge->pci_bus, -PCI_DEVFN (slot->device, func->function)); -if (pdev) { +while (1 ) { +/* pci_bus_sem is used to protect bus->devices list, +it may cause invalid memory access if threads +modify bus->devices list concurrently. */ +down_read (&pci_bus_sem); +list_for_each_entry (pdev, &bus->devices, bus_list) +if (PCI_SLOT (pdev->devfn) == slot->device) { +pci_dev_get (pdev); +break ; + } +up_read (&pci_bus_sem); + +if (!pdev ||PCI_SLOT (pdev->devfn) != slot->device) +break ;pci_stop_bus_device (pdev);if (pdev->subordinate) {disable_bridges (pdev->subordinate);
Signature · 1 line
1.7.9.5