Skip to contentSkip to conversation

PCI/ASPM: Remove locking

PATCH In review Bjorn Helgaas 3 participants 5 messages Oct 10, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover PCI/ASPM: Remove locking

From: Bjorn Helgaas <[email protected]>

In reference to this thread:
https://lore.kernel.org/r/[email protected]

This removes locking from pcie_aspm_enabled() because the reference count held by the driver should provide all the locking we need.

Bjorn Helgaas (1):
PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking

 drivers/pci/pcie/aspm.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
#1 lore raw

PATCH 1/1 PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking

From: Bjorn Helgaas <[email protected]>

The lifetime of the link_state structure (bridge->link_state) is not the same as the lifetime of "bridge" itself. The link_state is allocated by pcie_aspm_init_link_state() after children of the bridge have been enumerated, and it is deallocated by pcie_aspm_exit_link_state() after all children of the bridge (but not the bridge itself) have been removed.

Previously pcie_aspm_enabled() acquired aspm_lock to ensure that link_state was not deallocated while we're looking at it. But the fact that the caller of pcie_aspm_enabled() holds a reference to @pdev means there's always at least one child of the bridge, which means link_state can't be deallocated.

Remove the unnecessary locking in pcie_aspm_enabled().

---
 drivers/pci/pcie/aspm.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pci/pcie/aspm.c +6 -6
index 652ef23bba35..f5c7138a34aa 100644
@@ -1172,20 +1172,20 @@ module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
 /**
  * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
  * @pdev: Target device.
+ *
+ * Relies on the upstream bridge's link_state being valid.  The link_state
+ * is deallocated only when the last child of the bridge (i.e., @pdev or a
+ * sibling) is removed, and the caller should be holding a reference to
+ * @pdev, so this should be safe.
  */
 bool pcie_aspm_enabled(struct pci_dev *pdev)
 {
 	struct pci_dev *bridge = pci_upstream_bridge(pdev);
-	bool ret;
 
 	if (!bridge)
 		return false;
 
-	mutex_lock(&aspm_lock);
-	ret = bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
-	mutex_unlock(&aspm_lock);
-
-	return ret;
+	return bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
 }
 EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
 
Signature · 1 line
2.23.0.581.g78d2f28ef7-goog
#2 lore raw

Re: [PATCH 1/1] PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking

drivers/pci/pcie/aspm.c module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, · line 1188

Quoted 1 line · Bjorn Helgaas
drivers/pci/pcie/aspm.c +1 -0
+	return bridge->link_state ? !!bridge->link_state->aspm_enabled : false;

Can we unobsfucated this while we're at it?

	if (!bridge->link_state)
		return false;
	return bridge->link_state->aspm_enabled;
#3 lore raw

Re: [PATCH 1/1] PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking

Quoted 8 lines · Christoph Hellwig

drivers/pci/pcie/aspm.c module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, · line 1188

Quoted 1 line · Bjorn Helgaas
drivers/pci/pcie/aspm.c +1 -0
+	return bridge->link_state ? !!bridge->link_state->aspm_enabled : false;

Can we unobsfucated this while we're at it?

	if (!bridge->link_state)
		return false;
	return bridge->link_state->aspm_enabled;

Yep, after some of the follow-up patches from Heiner, it looks like this:

bool pcie_aspm_enabled(struct pci_dev *pdev)

  {
	  struct pcie_link_state *link = pcie_aspm_get_link(pdev);

	  if (!link)
		  return false;

	  return link->aspm_enabled;

}

Acked-by on v1 1/1
#4 lore raw

Re: [PATCH 1/1] PCI/ASPM: Remove pcie_aspm_enabled() unnecessary locking

In reply to Bjorn Helgaas
Quoted 15 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

The lifetime of the link_state structure (bridge->link_state) is not the same as the lifetime of "bridge" itself. The link_state is allocated by pcie_aspm_init_link_state() after children of the bridge have been enumerated, and it is deallocated by pcie_aspm_exit_link_state() after all children of the bridge (but not the bridge itself) have been removed.

Previously pcie_aspm_enabled() acquired aspm_lock to ensure that link_state was not deallocated while we're looking at it. But the fact that the caller of pcie_aspm_enabled() holds a reference to @pdev means there's always at least one child of the bridge, which means link_state can't be deallocated.

Quoted 3 lines

Remove the unnecessary locking in pcie_aspm_enabled().

(also for the further changes you and Christoph have been discussing).

Quoted 38 lines
---
 drivers/pci/pcie/aspm.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/pci/pcie/aspm.c +6 -7
index 652ef23bba35..f5c7138a34aa 100644
@@ -1172,20 +1172,20 @@ module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
 /**
  * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
  * @pdev: Target device.
+ *
+ * Relies on the upstream bridge's link_state being valid.  The link_state
+ * is deallocated only when the last child of the bridge (i.e., @pdev or a
+ * sibling) is removed, and the caller should be holding a reference to
+ * @pdev, so this should be safe.
  */
 bool pcie_aspm_enabled(struct pci_dev *pdev)
 {
        struct pci_dev *bridge = pci_upstream_bridge(pdev);
-       bool ret;
 
        if (!bridge)
                return false;
 
-       mutex_lock(&aspm_lock);
-       ret = bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
-       mutex_unlock(&aspm_lock);
-
-       return ret;
+       return bridge->link_state ? !!bridge->link_state->aspm_enabled : false;
 }
 EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
 
--

2.23.0.581.g78d2f28ef7-goog