Skip to contentSkip to conversation

PCI: Use a local mutex instead of pci_bus_sem to avoid deadlock

PATCH Superseded Yijing Wang 5 participants 19 messages Jun 11, 2015 – Jul 17, 2015 1 message not in the archive PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Use a local mutex instead of pci_bus_sem to avoid deadlock

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()

	pciehp_configure_device()
		pci_bus_add_devices()
			device_attach(dev)
				device_lock(dev) //acquire device mutex successfully
			...
			pciehp_probe(dev)
				__pci_hp_register()
					pci_create_slot()
						down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +6 -5
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
 					struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
 	struct pci_slot *slot;
 	/*
-	 * We already hold pci_bus_sem so don't worry
+	 * We already hold pci_slot_mutex so don't worry
 	 */
 	list_for_each_entry(slot, &parent->slots, list)
 		if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
 	int err = 0;
 	char *slot_name = NULL;
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 
 	if (slot_nr == -1)
 		goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
 	kfree(slot_name);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 	return slot;
 err:
 	kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
 	dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
 		slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 	kobject_put(&slot->kobj);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 
Signature · 1 line
1.7.1
#1 lore raw

[email protected] is not reachable now.

So add CC: [email protected]

Quoted 87 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()

	pciehp_configure_device()
		pci_bus_add_devices()
			device_attach(dev)
				device_lock(dev) //acquire device mutex successfully
			...
			pciehp_probe(dev)
				__pci_hp_register()
					pci_create_slot()
						down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +6 -5
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
 					struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
 	struct pci_slot *slot;
 	/*
-	 * We already hold pci_bus_sem so don't worry
+	 * We already hold pci_slot_mutex so don't worry
 	 */
 	list_for_each_entry(slot, &parent->slots, list)
 		if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
 	int err = 0;
 	char *slot_name = NULL;
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 
 	if (slot_nr == -1)
 		goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
 	kfree(slot_name);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 	return slot;
 err:
 	kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
 	dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
 		slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 	kobject_put(&slot->kobj);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 
Signature · 2 lines
Thanks!
Yijing
#2 lore raw

[+Guenter]

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

Thanks,

Rajat

Quoted 98 lines · Yijing Wang

[email protected] is not reachable now.

So add CC: [email protected]

Quoted 77 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()
pciehp_configure_device()
pci_bus_add_devices()
device_attach(dev)
device_lock(dev) //acquire device mutex successfully
...
pciehp_probe(dev)
__pci_hp_register()
pci_create_slot()
down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +4 -3
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                      struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
      struct pci_slot *slot;
      /*
-      * We already hold pci_bus_sem so don't worry
+      * We already hold pci_slot_mutex so don't worry
       */
      list_for_each_entry(slot, &parent->slots, list)
              if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
      int err = 0;
      char *slot_name = NULL;
 
-     down_write(&pci_bus_sem);
+     mutex_lock(&pci_slot_mutex);
 
      if (slot_nr == -1)
              goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
      kfree(slot_name);
-     up_write(&pci_bus_sem);
+     mutex_unlock(&pci_slot_mutex);
      return slot;
 err:
      kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
      dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 341

Quoted 10 lines
drivers/pci/slot.c +2 -2
              slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-     down_write(&pci_bus_sem);
+     mutex_lock(&pci_slot_mutex);
      kobject_put(&slot->kobj);
-     up_write(&pci_bus_sem);
+     mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 

--
Thanks!
Yijing

#3 lore raw

Actually forgot to add Guenter. Also +rthirumal.

Quoted 111 lines · Rajat Jain

[+Guenter]

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test/use this?

Thanks,

Rajat

Quoted 98 lines · Yijing Wang

[email protected] is not reachable now.

So add CC: [email protected]

Quoted 77 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()
pciehp_configure_device()
pci_bus_add_devices()
device_attach(dev)
device_lock(dev) //acquire device mutex successfully
...
pciehp_probe(dev)
__pci_hp_register()
pci_create_slot()
down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +4 -3
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                      struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
      struct pci_slot *slot;
      /*
-      * We already hold pci_bus_sem so don't worry
+      * We already hold pci_slot_mutex so don't worry
       */
      list_for_each_entry(slot, &parent->slots, list)
              if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
      int err = 0;
      char *slot_name = NULL;
 
-     down_write(&pci_bus_sem);
+     mutex_lock(&pci_slot_mutex);
 
      if (slot_nr == -1)
              goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
      kfree(slot_name);
-     up_write(&pci_bus_sem);
+     mutex_unlock(&pci_slot_mutex);
      return slot;
 err:
      kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
      dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 341

Quoted 10 lines
drivers/pci/slot.c +2 -2
              slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-     down_write(&pci_bus_sem);
+     mutex_lock(&pci_slot_mutex);
      kobject_put(&slot->kobj);
-     up_write(&pci_bus_sem);
+     mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 

--
Thanks!
Yijing

#5 lore raw
In reply to a message not in the archive
Quoted 7 lines · Rajat Jain

[+Guenter]

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

I'll dig up the patch and test it.

Guenter

#6 lore raw
Quoted 9 lines · Guenter Roeck
Quoted 7 lines · Rajat Jain

[+Guenter]

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

I'll dig up the patch and test it.

Rajat, Guenter, thanks for your help.

Thanks!
Yijing.

Quoted 4 lines

Guenter

Signature · 2 lines
Thanks!
Yijing
#7 lore raw
In reply to Guenter Roeck
Quoted 9 lines · Guenter Roeck
Quoted 7 lines · Rajat Jain

[+Guenter]

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

I'll dig up the patch and test it.

Hi Guenter, any update ?

Quoted 4 lines

Guenter

Signature · 2 lines
Thanks!
Yijing
Tested-by
#8 lore raw
Quoted 4 lines · Yijing Wang
Quoted 3 lines · Guenter Roeck
Quoted 2 lines · Rajat Jain

[+Guenter]

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

I'll dig up the patch and test it.

Hi Guenter, any update ?

I merged your patch into our images. I have not seen a single failure since then.

At the same time, we tried to reproduce the problem Rajat had reported originally. Unfortunately we have not been able to reproduce it. I don't know if that is bad luck, if we did something wrong (most likely), or if something else changed in the infrastructure since Rajat did his tests.

Either case, I would appreciate if this patch could find its way upstream. I don't feel comfortable to enable AER without it. Feel free to add

Thanks,
Guenter

#9 lore raw
Quoted 22 lines · Guenter Roeck
Quoted 4 lines · Yijing Wang
Quoted 3 lines · Guenter Roeck
Quoted 2 lines · Rajat Jain

[+Guenter]

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines

Looks good to me. Unfortunately, I do not have the hardware anymore to test it :-(

Guenter: Do you have it, or know some body who has and wants to test this?

I'll dig up the patch and test it.

Hi Guenter, any update ?

I merged your patch into our images. I have not seen a single failure since then.

At the same time, we tried to reproduce the problem Rajat had reported originally. Unfortunately we have not been able to reproduce it. I don't know if that is bad luck, if we did something wrong (most likely), or if something else changed in the infrastructure since Rajat did his tests.

Quoted 4 lines

Either case, I would appreciate if this patch could find its way upstream. I don't feel comfortable to enable AER without it. Feel free to add

OK, thanks for your help test.

Thanks!
Yijing.

Quoted 5 lines

Thanks,
Guenter

Signature · 2 lines
Thanks!
Yijing
maintainer
#10 lore raw
In reply to Yijing Wang

[+cc Guenter, Rafael]

Quoted 26 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()

	pciehp_configure_device()
		pci_bus_add_devices()
			device_attach(dev)
				device_lock(dev) //acquire device mutex successfully
			...
			pciehp_probe(dev)
				__pci_hp_register()
					pci_create_slot()
						down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Quoted 3 lines

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

I see there's definitely a problem here, and using a new mutex instead of pci_bus_sem certainly avoids the deadlock.

I'm trying to convince myself that it is safe. I think we need to protect:

- search of bus->slots list in get_slot()
- addition to bus->slots list in pci_create_slot()
- search of bus->devices list in pci_create_slot()
- search of bus->devices list in pci_slot_release()
- deletion from bus->slots list in pci_slot_release()

Most other maintenance of these lists is protected by pci_bus_sem, so using a different mutex here seems like a problem.

If I'm mistaken, please correct me and explain why this patch is safe.

Quoted 59 lines
---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +6 -5
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
 					struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
 	struct pci_slot *slot;
 	/*
-	 * We already hold pci_bus_sem so don't worry
+	 * We already hold pci_slot_mutex so don't worry
 	 */
 	list_for_each_entry(slot, &parent->slots, list)
 		if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
 	int err = 0;
 	char *slot_name = NULL;
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 
 	if (slot_nr == -1)
 		goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
 	kfree(slot_name);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 	return slot;
 err:
 	kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
 	dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
 		slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 	kobject_put(&slot->kobj);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 
Signature · 1 line
1.7.1
#11 lore raw
Quoted 44 lines · Bjorn Helgaas

[+cc Guenter, Rafael]

Quoted 26 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()

	pciehp_configure_device()
		pci_bus_add_devices()
			device_attach(dev)
				device_lock(dev) //acquire device mutex successfully
			...
			pciehp_probe(dev)
				__pci_hp_register()
					pci_create_slot()
						down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Quoted 3 lines

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

I see there's definitely a problem here, and using a new mutex instead of pci_bus_sem certainly avoids the deadlock.

I'm trying to convince myself that it is safe. I think we need to protect:

- search of bus->slots list in get_slot()
- addition to bus->slots list in pci_create_slot()
- search of bus->devices list in pci_create_slot()
- search of bus->devices list in pci_slot_release()
- deletion from bus->slots list in pci_slot_release()

Quoted 4 lines

Most other maintenance of these lists is protected by pci_bus_sem, so using a different mutex here seems like a problem.

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +10 -5
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
        list_del(&slot->list);
 
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 

Thanks!
Yijing.

Quoted 63 lines
Quoted 60 lines
---
 drivers/pci/slot.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/pci/slot.c +6 -5
index 396c200..feb08de 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
 					struct attribute *attr, char *buf)
@@ -195,7 +196,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
 	struct pci_slot *slot;
 	/*
-	 * We already hold pci_bus_sem so don't worry
+	 * We already hold pci_slot_mutex so don't worry
 	 */
 	list_for_each_entry(slot, &parent->slots, list)
 		if (slot->number == slot_nr) {
@@ -253,7 +254,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
 	int err = 0;
 	char *slot_name = NULL;
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 
 	if (slot_nr == -1)
 		goto placeholder;
@@ -310,7 +311,7 @@ placeholder:
 
 out:
 	kfree(slot_name);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 	return slot;
 err:
 	kfree(slot);
@@ -332,9 +333,9 @@ void pci_destroy_slot(struct pci_slot *slot)
 	dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
 		slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-	down_write(&pci_bus_sem);
+	mutex_lock(&pci_slot_mutex);
 	kobject_put(&slot->kobj);
-	up_write(&pci_bus_sem);
+	mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
 
Signature · 1 line
1.7.1

.

Signature · 2 lines
Thanks!
Yijing
maintainer
#12 lore raw
Quoted 72 lines · Yijing Wang
Quoted 44 lines · Bjorn Helgaas

[+cc Guenter, Rafael]

Quoted 26 lines · Yijing Wang

Rajat Jain reported a deadlock when a hierarchical hot plug thread and aer recovery thread both run.
https://lkml.org/lkml/2015/3/11/861

thread 1:
pciehp_enable_slot()

	pciehp_configure_device()
		pci_bus_add_devices()
			device_attach(dev)
				device_lock(dev) //acquire device mutex successfully
			...
			pciehp_probe(dev)
				__pci_hp_register()
					pci_create_slot()
						down_write(pci_bus_sem) //deadlock here

thread 2:
aer_isr_one_error()
aer_process_err_device()
do_recovery()
broadcast_error_message()
pci_walk_bus()
down_read(&pci_bus_sem) //acquire pci_bus_sem successfully
report_error_detected(dev)
device_lock(dev) // deadlock here

Quoted 3 lines

Now we use pci_bus_sem to protect pci_slot creation and destroy, it's unnecessary. We could introduce a new local mutex instead of pci_bus_sem to avoid the deadlock.

I see there's definitely a problem here, and using a new mutex instead of pci_bus_sem certainly avoids the deadlock.

I'm trying to convince myself that it is safe. I think we need to protect:

- search of bus->slots list in get_slot()
- addition to bus->slots list in pci_create_slot()
- search of bus->devices list in pci_create_slot()
- search of bus->devices list in pci_slot_release()
- deletion from bus->slots list in pci_slot_release()

Quoted 4 lines

Most other maintenance of these lists is protected by pci_bus_sem, so using a different mutex here seems like a problem.

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +1 -0
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)

drivers/pci/slot.c static void pci_slot_release(struct kobject *kobj) · line 116

Quoted 10 lines
drivers/pci/slot.c +2 -0
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
        list_del(&slot->list);

This list_del() updates the bus->slots list.

Quoted 49 lines
drivers/pci/slot.c +7 -5
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);
#13 lore raw
Quoted 33 lines
Quoted 33 lines
Quoted 1 line

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +3 -0
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
Quoted 3 lines

drivers/pci/slot.c static void pci_slot_release(struct kobject *kobj) · line 116

Quoted 1 line
drivers/pci/slot.c +0 -0
        list_del(&slot->list);

This list_del() updates the bus->slots list.

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

Thanks!
Yijing.

Quoted 53 lines
Quoted 40 lines
drivers/pci/slot.c +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 10 lines
drivers/pci/slot.c +2 -2
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

Signature · 2 lines
Thanks!
Yijing
maintainer
#14 lore raw
Quoted 33 lines · Yijing Wang
Quoted 33 lines
Quoted 33 lines
Quoted 1 line

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +3 -0
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
Quoted 5 lines
Quoted 3 lines

drivers/pci/slot.c static void pci_slot_release(struct kobject *kobj) · line 116

Quoted 1 line
drivers/pci/slot.c +0 -0
        list_del(&slot->list);

This list_del() updates the bus->slots list.

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

That doesn't protect anybody else who might be traversing the bus->slots list while we're deleting this entry.

Quoted 58 lines
Quoted 42 lines
Quoted 42 lines
drivers/pci/slot.c +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
Quoted 10 lines

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 7 lines
drivers/pci/slot.c +2 -2
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

--
Thanks!
Yijing

#15 lore raw
Quoted 34 lines · Bjorn Helgaas
Quoted 33 lines · Yijing Wang
Quoted 33 lines
Quoted 33 lines
Quoted 1 line

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +3 -0
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

drivers/pci/slot.c static void pci_slot_release(struct kobject *kobj) · line 116

Quoted 1 line
drivers/pci/slot.c +0 -0
        list_del(&slot->list);

This list_del() updates the bus->slots list.

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

That doesn't protect anybody else who might be traversing the bus->slots list while we're deleting this entry.

Hi Bjorn, sorry, I don't understand your point, before this patch, we use pci_bus_sem to protect the whole pci_slot_release(), in which, we would traverse the bus->devices list and update the bus->slots list. And now what we did is introduce a new pci_slot_mutex to protect the bus->slots here, and use down_read(pci_bus_sem) instead of down_write(pci_bus_sem).
Could you explain it a little more ?

Thanks!
Yijing.

Quoted 62 lines
Quoted 55 lines
Quoted 43 lines
Quoted 43 lines
drivers/pci/slot.c +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
Quoted 10 lines

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 7 lines
drivers/pci/slot.c +2 -2
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

Quoted 4 lines

--
Thanks!
Yijing

.

Signature · 2 lines
Thanks!
Yijing
maintainer
#16 lore raw
Quoted 39 lines · Yijing Wang
Quoted 38 lines · Bjorn Helgaas
Quoted 37 lines · Yijing Wang
Quoted 36 lines
Quoted 24 lines
Quoted 1 line

If I'm mistaken, please correct me and explain why this patch is safe.

Hi Bjorn, I think pci_bus_sem here was introduced to protect the bus->slots list, because it
use down_write() here, for bus->devices list, we only traverse it, won't add/remove it, for the latter, down_read() is enough. When I posted this patch, I thought we should protect the bus when we start to register a slot,
something like a big lock at outermost routine to tell others not to touch its children devices, use pci_bus_sem to protect hotplug cases is not a good idea, and actually in PCI code, we have found several deadlock caused by the pci_bus_sem.

But for this patch, I know what you worried, what about add a down_read(&pci_bus_sem) to avoid to introduce a regression ?

drivers/pci/slot.c +1 -0
index 396c200..a9079d9 100644
@@ -14,6 +14,7 @@
 
 struct kset *pci_slots_kset;
 EXPORT_SYMBOL_GPL(pci_slots_kset);
+static DEFINE_MUTEX(pci_slot_mutex);
 
 static ssize_t pci_slot_attr_show(struct kobject *kobj,
                                        struct attribute *attr, char *buf)

drivers/pci/slot.c static void pci_slot_release(struct kobject *kobj) · line 116

Quoted 10 lines
drivers/pci/slot.c +2 -0
@@ -106,9 +107,11 @@ static void pci_slot_release(struct kobject *kobj)
        dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
                slot->number, pci_slot_name(slot));
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &slot->bus->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = NULL;
+       up_read(&pci_bus_sem);
 
        list_del(&slot->list);

This list_del() updates the bus->slots list.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

That doesn't protect anybody else who might be traversing the bus->slots list while we're deleting this entry.

Hi Bjorn, sorry, I don't understand your point, before this patch, we use pci_bus_sem to protect the whole pci_slot_release(), in which, we would traverse the bus->devices list and update the bus->slots list. And now what we did is introduce a new pci_slot_mutex to protect the bus->slots here, and use down_read(pci_bus_sem) instead of down_write(pci_bus_sem).

pci_setup_device() does this:

        list_for_each_entry(slot, &dev->bus->slots, list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = slot;

What keeps that code from running at the same time pci_slot_release() is removing something from the bus->slots list?

It looks to me like the loop in pci_setup_device() is unsafe to begin with. But the obvious thing to do would be to add down_read(&pci_bus_sem) there, and then you'd need a down_write() in pci_slot_release(), so you're back where we started.

Quoted 67 lines
Quoted 54 lines
Quoted 54 lines
Quoted 52 lines
Quoted 39 lines
drivers/pci/slot.c +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);

drivers/pci/slot.c void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 10 lines
drivers/pci/slot.c +2 -2
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

Quoted 7 lines
Quoted 4 lines

--
Thanks!
Yijing

.

--
Thanks!
Yijing

#17 lore raw
Quoted 17 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This list_del() updates the bus->slots list.

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

That doesn't protect anybody else who might be traversing the bus->slots list while we're deleting this entry.

Hi Bjorn, sorry, I don't understand your point, before this patch, we use pci_bus_sem to protect the whole pci_slot_release(), in which, we would traverse the bus->devices list and update the bus->slots list. And now what we did is introduce a new pci_slot_mutex to protect the bus->slots here, and use down_read(pci_bus_sem) instead of down_write(pci_bus_sem).

pci_setup_device() does this:

        list_for_each_entry(slot, &dev->bus->slots, list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = slot;
Quoted 7 lines

What keeps that code from running at the same time pci_slot_release() is removing something from the bus->slots list?

It looks to me like the loop in pci_setup_device() is unsafe to begin with. But the obvious thing to do would be to add down_read(&pci_bus_sem) there, and then you'd need a down_write() in pci_slot_release(), so you're back where we started.

I got it, I missed the bus->slots list traverse in pci scan code,
What about moving the bus->slots loop code from pci_setup_device() to drivers/pci/slot.c, and add a pci_slot_mutex to protect it ? I think we should avoid to use pci_bus_sem to protect bus->slots list.

Something like this:

drivers/pci/probe.c +1 -4
index cefd636..6f00273 100644
@@ -1149,10 +1149,7 @@ int pci_setup_device(struct pci_dev *dev)
        dev->error_state = pci_channel_io_normal;
        set_pcie_port_type(dev);
 
-       list_for_each_entry(slot, &dev->bus->slots, list)
-               if (PCI_SLOT(dev->devfn) == slot->number)
-                       dev->slot = slot;
-
+       pci_dev_assign_slot(dev);
        /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
           set this higher, assuming the system even supports it.  */
        dev->dma_mask = 0xffffffff;
drivers/pci/slot.c +9 -0
index a9079d9..cf259e7 100644
@@ -99,6 +99,15 @@ static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
        return bus_speed_read(slot->bus->cur_bus_speed, buf);
 }
 
+void pci_dev_assign_slot(struct pci_dev *dev)
+{
+       mutex_lock(&pci_slot_mutex);
+       list_for_each_entry(slot, &dev->bus->slots, list)
+               if (PCI_SLOT(dev->devfn) == slot->number)
+                       dev->slot = slot;
+       mutex_unlock(&pci_slot_mutex);
+}
+
 static void pci_slot_release(struct kobject *kobj)
 {
        struct pci_dev *dev;
Quoted 71 lines
Quoted 60 lines
Quoted 60 lines
Quoted 59 lines
Quoted 43 lines
Quoted 43 lines
(quoted diff) +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
Quoted 10 lines

Review void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 7 lines
(quoted diff) +2 -2
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

--
Thanks!
Yijing

Quoted 8 lines
Quoted 2 lines

.

--
Thanks!
Yijing

.

Signature · 2 lines
Thanks!
Yijing
maintainer
#18 lore raw
Quoted 20 lines · Yijing Wang
Quoted 20 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

This list_del() updates the bus->slots list.

It's safe here, because we have locked the pci_slot_mutex in pci_destroy_slot(), which is the only caller of pci_slot_release().

That doesn't protect anybody else who might be traversing the bus->slots list while we're deleting this entry.

Hi Bjorn, sorry, I don't understand your point, before this patch, we use pci_bus_sem to protect the whole pci_slot_release(), in which, we would traverse the bus->devices list and update the bus->slots list. And now what we did is introduce a new pci_slot_mutex to protect the bus->slots here, and use down_read(pci_bus_sem) instead of down_write(pci_bus_sem).

pci_setup_device() does this:

        list_for_each_entry(slot, &dev->bus->slots, list)
                if (PCI_SLOT(dev->devfn) == slot->number)
                        dev->slot = slot;

What keeps that code from running at the same time pci_slot_release() is removing something from the bus->slots list?

Quoted 8 lines
Quoted 4 lines

It looks to me like the loop in pci_setup_device() is unsafe to begin with. But the obvious thing to do would be to add down_read(&pci_bus_sem) there, and then you'd need a down_write() in pci_slot_release(), so you're back where we started.

I got it, I missed the bus->slots list traverse in pci scan code,
What about moving the bus->slots loop code from pci_setup_device() to drivers/pci/slot.c, and add a pci_slot_mutex to protect it ? I think we should avoid to use pci_bus_sem to protect bus->slots list.

Seems better, although it's starting to feel a bit ad hoc. We would need to write down this locking rule somewhere, e.g., near the struct pci_bus declaration.

Quoted 116 lines

Something like this:

drivers/pci/probe.c +1 -4
index cefd636..6f00273 100644
@@ -1149,10 +1149,7 @@ int pci_setup_device(struct pci_dev *dev)
        dev->error_state = pci_channel_io_normal;
        set_pcie_port_type(dev);
 
-       list_for_each_entry(slot, &dev->bus->slots, list)
-               if (PCI_SLOT(dev->devfn) == slot->number)
-                       dev->slot = slot;
-
+       pci_dev_assign_slot(dev);
        /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
           set this higher, assuming the system even supports it.  */
        dev->dma_mask = 0xffffffff;
drivers/pci/slot.c +9 -0
index a9079d9..cf259e7 100644
@@ -99,6 +99,15 @@ static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
        return bus_speed_read(slot->bus->cur_bus_speed, buf);
 }
 
+void pci_dev_assign_slot(struct pci_dev *dev)
+{
+       mutex_lock(&pci_slot_mutex);
+       list_for_each_entry(slot, &dev->bus->slots, list)
+               if (PCI_SLOT(dev->devfn) == slot->number)
+                       dev->slot = slot;
+       mutex_unlock(&pci_slot_mutex);
+}
+
 static void pci_slot_release(struct kobject *kobj)
 {
        struct pci_dev *dev;
Quoted 66 lines
Quoted 66 lines
Quoted 64 lines
Quoted 57 lines
Quoted 45 lines
Quoted 45 lines
(quoted diff) +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
Quoted 10 lines

Review void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 7 lines
(quoted diff) +2 -2
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

Quoted 4 lines

--
Thanks!
Yijing

.

Quoted 7 lines
Quoted 4 lines

--
Thanks!
Yijing

.

--
Thanks!
Yijing

#19 lore raw
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

It looks to me like the loop in pci_setup_device() is unsafe to begin with. But the obvious thing to do would be to add down_read(&pci_bus_sem) there, and then you'd need a down_write() in pci_slot_release(), so you're back where we started.

Quoted 7 lines
Quoted 3 lines

I got it, I missed the bus->slots list traverse in pci scan code,
What about moving the bus->slots loop code from pci_setup_device() to drivers/pci/slot.c, and add a pci_slot_mutex to protect it ? I think we should avoid to use pci_bus_sem to protect bus->slots list.

Seems better, although it's starting to feel a bit ad hoc. We would need to write down this locking rule somewhere, e.g., near the struct pci_bus declaration.

OK, I will post a updated patch.

Thanks!
Yijing.

Quoted 120 lines
Quoted 109 lines

Something like this:

drivers/pci/probe.c +1 -4
index cefd636..6f00273 100644
@@ -1149,10 +1149,7 @@ int pci_setup_device(struct pci_dev *dev)
        dev->error_state = pci_channel_io_normal;
        set_pcie_port_type(dev);
 
-       list_for_each_entry(slot, &dev->bus->slots, list)
-               if (PCI_SLOT(dev->devfn) == slot->number)
-                       dev->slot = slot;
-
+       pci_dev_assign_slot(dev);
        /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
           set this higher, assuming the system even supports it.  */
        dev->dma_mask = 0xffffffff;
drivers/pci/slot.c +9 -0
index a9079d9..cf259e7 100644
@@ -99,6 +99,15 @@ static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
        return bus_speed_read(slot->bus->cur_bus_speed, buf);
 }
 
+void pci_dev_assign_slot(struct pci_dev *dev)
+{
+       mutex_lock(&pci_slot_mutex);
+       list_for_each_entry(slot, &dev->bus->slots, list)
+               if (PCI_SLOT(dev->devfn) == slot->number)
+                       dev->slot = slot;
+       mutex_unlock(&pci_slot_mutex);
+}
+
 static void pci_slot_release(struct kobject *kobj)
 {
        struct pci_dev *dev;
Quoted 71 lines
Quoted 70 lines
Quoted 57 lines
Quoted 57 lines
Quoted 55 lines
Quoted 42 lines
(quoted diff) +5 -3
@@ -195,7 +198,7 @@ static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
 {
        struct pci_slot *slot;
        /*
-        * We already hold pci_bus_sem so don't worry
+        * We already hold pci_slot_mutex so don't worry
         */
        list_for_each_entry(slot, &parent->slots, list)
                if (slot->number == slot_nr) {
@@ -253,7 +256,7 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
        int err = 0;
        char *slot_name = NULL;
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
 
        if (slot_nr == -1)
                goto placeholder;
@@ -301,16 +304,18 @@ placeholder:
        INIT_LIST_HEAD(&slot->list);
        list_add(&slot->list, &parent->slots);
 
+       down_read(&pci_bus_sem);
        list_for_each_entry(dev, &parent->devices, bus_list)
                if (PCI_SLOT(dev->devfn) == slot_nr)
                        dev->slot = slot;
+       up_read(&pci_bus_sem);
 
        dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
                slot_nr, pci_slot_name(slot));
 
 out:
        kfree(slot_name);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
        return slot;
 err:
        kfree(slot);

Review void pci_destroy_slot(struct pci_slot *slot) · line 344

Quoted 10 lines
(quoted diff) +2 -2
@@ -332,9 +337,9 @@ void pci_destroy_slot(struct pci_slot *slot)
        dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
                slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
 
-       down_write(&pci_bus_sem);
+       mutex_lock(&pci_slot_mutex);
        kobject_put(&slot->kobj);
-       up_write(&pci_bus_sem);
+       mutex_unlock(&pci_slot_mutex);
 }
 EXPORT_SYMBOL_GPL(pci_destroy_slot);

.

Quoted 7 lines
Quoted 4 lines

--
Thanks!
Yijing

.

--
Thanks!
Yijing

Quoted 8 lines
Quoted 2 lines

.

--
Thanks!
Yijing

.

Signature · 2 lines
Thanks!
Yijing