[RFC PATCH 6/6] pci: Protect is_busmaster using the state lock

2018-08-16 Thread Benjamin Herrenschmidt
This wraps pci_set_master() and pci_clear_master() with the pci_dev
state lock. The clearing of is_busmaster in pci_disable_device()
is already covered.

This also adds a comment explaining why is_busmaster must not be
checked in pci_set_master() due to how the power management code
uses it.

Signed-off-by: Benjamin Herrenschmidt 
---
 drivers/pci/pci.c   | 9 +
 include/linux/pci.h | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 68152de2b5a0..13d988d5b2a3 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -4052,11 +4052,18 @@ void __weak pcibios_set_master(struct pci_dev *dev)
  *
  * Enables bus-mastering on the device and calls pcibios_set_master()
  * to do the needed arch specific settings.
+ *
+ * Note: This must not check dev->is_busmaster because the power management
+ *   code will call this in order to restore the config space to the
+ *   state of is_busmaster, thus is_busmaster might be set but the
+ *   config space bit cleared.
  */
 void pci_set_master(struct pci_dev *dev)
 {
+   pci_dev_state_lock(dev);
__pci_set_master(dev, true);
pcibios_set_master(dev);
+   pci_dev_state_unlock(dev);
 }
 EXPORT_SYMBOL(pci_set_master);
 
@@ -4066,7 +4073,9 @@ EXPORT_SYMBOL(pci_set_master);
  */
 void pci_clear_master(struct pci_dev *dev)
 {
+   pci_dev_state_lock(dev);
__pci_set_master(dev, false);
+   pci_dev_state_unlock(dev);
 }
 EXPORT_SYMBOL(pci_clear_master);
 
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 0d4fc22df190..a5bac5b21454 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -375,7 +375,6 @@ struct pci_dev {
unsigned intmultifunction:1;/* Multi-function device */
 
unsigned intis_added:1;
-   unsigned intis_busmaster:1; /* Is busmaster */
unsigned intno_msi:1;   /* May not use MSI */
unsigned intno_64bit_msi:1; /* May only use 32-bit MSIs */
unsigned intblock_cfg_access:1; /* Config space access blocked 
*/
@@ -450,6 +449,7 @@ struct pci_dev {
struct mutexstate_lock; /* Protect local state bits */
 
/* --- Fields below this line are protected by the state_lock mutex */
+   unsigned intis_busmaster:1; /* Is busmaster */
 };
 
 static inline void pci_dev_state_lock(struct pci_dev *dev)
-- 
2.17.1



[RFC PATCH 6/6] pci: Protect is_busmaster using the state lock

2018-08-16 Thread Benjamin Herrenschmidt
This wraps pci_set_master() and pci_clear_master() with the pci_dev
state lock. The clearing of is_busmaster in pci_disable_device()
is already covered.

This also adds a comment explaining why is_busmaster must not be
checked in pci_set_master() due to how the power management code
uses it.

Signed-off-by: Benjamin Herrenschmidt 
---
 drivers/pci/pci.c   | 9 +
 include/linux/pci.h | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 68152de2b5a0..13d988d5b2a3 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -4052,11 +4052,18 @@ void __weak pcibios_set_master(struct pci_dev *dev)
  *
  * Enables bus-mastering on the device and calls pcibios_set_master()
  * to do the needed arch specific settings.
+ *
+ * Note: This must not check dev->is_busmaster because the power management
+ *   code will call this in order to restore the config space to the
+ *   state of is_busmaster, thus is_busmaster might be set but the
+ *   config space bit cleared.
  */
 void pci_set_master(struct pci_dev *dev)
 {
+   pci_dev_state_lock(dev);
__pci_set_master(dev, true);
pcibios_set_master(dev);
+   pci_dev_state_unlock(dev);
 }
 EXPORT_SYMBOL(pci_set_master);
 
@@ -4066,7 +4073,9 @@ EXPORT_SYMBOL(pci_set_master);
  */
 void pci_clear_master(struct pci_dev *dev)
 {
+   pci_dev_state_lock(dev);
__pci_set_master(dev, false);
+   pci_dev_state_unlock(dev);
 }
 EXPORT_SYMBOL(pci_clear_master);
 
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 0d4fc22df190..a5bac5b21454 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -375,7 +375,6 @@ struct pci_dev {
unsigned intmultifunction:1;/* Multi-function device */
 
unsigned intis_added:1;
-   unsigned intis_busmaster:1; /* Is busmaster */
unsigned intno_msi:1;   /* May not use MSI */
unsigned intno_64bit_msi:1; /* May only use 32-bit MSIs */
unsigned intblock_cfg_access:1; /* Config space access blocked 
*/
@@ -450,6 +449,7 @@ struct pci_dev {
struct mutexstate_lock; /* Protect local state bits */
 
/* --- Fields below this line are protected by the state_lock mutex */
+   unsigned intis_busmaster:1; /* Is busmaster */
 };
 
 static inline void pci_dev_state_lock(struct pci_dev *dev)
-- 
2.17.1