Skip to contentSkip to conversation

ASPM: Use msleep instead of cpu_relax during link retraining

PATCH Superseded Andrew Patterson 4 participants 6 messages Dec 22, 2008 – Jan 5, 2009 PCI

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASPM: Use msleep instead of cpu_relax during link retraining

ASPM: Use msleep instead of cpu_relax during link retraining

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Introduce define LINK_RETRAIN_TIMEOUT instead of hard-coding timeout in pcie_aspm_configure_common_clock.

---

drivers/pci/pcie/aspm.c +9 -4
index e361c7d..c00fd65 100644
@@ -17,6 +17,7 @@
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/jiffies.h>
+#include <linux/delay.h>
 #include <linux/pci-aspm.h>
 #include "../pci.h"
 
@@ -70,6 +71,8 @@ static const char *policy_str[] = {
 	[POLICY_POWERSAVE] = "powersave"
 };
 
+#define LINK_RETRAIN_TIMEOUT HZ
+
 static int policy_to_aspm_state(struct pci_dev *pdev)
 {
 	struct pcie_link_state *link_state = pdev->link_state;
@@ -217,16 +220,18 @@ static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
 	pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
 
 	/* Wait for link training end */
-	/* break out after waiting for 1 second */
+	/* break out after waiting for timeout */
 	start_jiffies = jiffies;
-	while ((jiffies - start_jiffies) < HZ) {
+	for (;;) {
 		pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
 		if (!(reg16 & PCI_EXP_LNKSTA_LT))
 			break;
-		cpu_relax();
+		if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT)
+			break;
+		msleep(1);
 	}
 	/* training failed -> recover */
-	if ((jiffies - start_jiffies) >= HZ) {
+	if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT) {
 		dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
 			    " common clock\n");
 		i = 0;
#1 lore raw
Quoted 5 lines · Andrew Patterson

ASPM: Use msleep instead of cpu_relax during link retraining

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Author clearly wanted to do a busy loop... why do you think 10msec delay here is acceptable?

Quoted 19 lines

Introduce define LINK_RETRAIN_TIMEOUT instead of hard-coding timeout in pcie_aspm_configure_common_clock.

drivers/pci/pcie/aspm.c +3 -1
@@ -70,6 +71,8 @@ static const char *policy_str[] = {
 	[POLICY_POWERSAVE] = "powersave"
 };
 
+#define LINK_RETRAIN_TIMEOUT HZ
+
 static int policy_to_aspm_state(struct pci_dev *pdev)
 {
 	struct pcie_link_state *link_state = pdev->link_state;
@@ -217,16 +220,18 @@ static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
 	pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
 
 	/* Wait for link training end */
-	/* break out after waiting for 1 second */
+	/* break out after waiting for timeout */

drivers/pci/pcie/aspm.c static void pcie_aspm_configure_common_clock(struct pci_dev *pdev) · line 231

Quoted 10 lines
drivers/pci/pcie/aspm.c +4 -2
 	start_jiffies = jiffies;
-	while ((jiffies - start_jiffies) < HZ) {
+	for (;;) {
 		pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
 		if (!(reg16 & PCI_EXP_LNKSTA_LT))
 			break;
-		cpu_relax();
+		if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT)
+			break;
+		msleep(1);

Is this safe w.r.t. jiffie wraparounds?

drivers/pci/pcie/aspm.c static void pcie_aspm_configure_common_clock(struct pci_dev *pdev) · line 237

Quoted 7 lines
drivers/pci/pcie/aspm.c +1 -1
 	}
 	/* training failed -> recover */
-	if ((jiffies - start_jiffies) >= HZ) {
+	if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT) {
 		dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
 			    " common clock\n");
 		i = 0;

AFAICT this can trigger false positives. !reg16 test succeeds and then jiffies tick.

...it could happen before but you make it way more probable...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#2 lore raw
Quoted 9 lines · Pavel Machek
Quoted 5 lines · Andrew Patterson

ASPM: Use msleep instead of cpu_relax during link retraining

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Author clearly wanted to do a busy loop... why do you think 10msec delay here is acceptable?

10ms? I see a 1ms sleep.

Quoted 18 lines
Quoted 6 lines
drivers/pci/pcie/aspm.c +1 -1
@@ -217,16 +220,18 @@ static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
 	pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
 
 	/* Wait for link training end */
-	/* break out after waiting for 1 second */
+	/* break out after waiting for timeout */

drivers/pci/pcie/aspm.c static void pcie_aspm_configure_common_clock(struct pci_dev *pdev) · line 231

Quoted 10 lines
drivers/pci/pcie/aspm.c +4 -2
 	start_jiffies = jiffies;
-	while ((jiffies - start_jiffies) < HZ) {
+	for (;;) {
 		pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
 		if (!(reg16 & PCI_EXP_LNKSTA_LT))
 			break;
-		cpu_relax();
+		if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT)
+			break;
+		msleep(1);

Is this safe w.r.t. jiffie wraparounds?

Definitely needs to be time_before/time_after.

Quoted 11 lines

drivers/pci/pcie/aspm.c static void pcie_aspm_configure_common_clock(struct pci_dev *pdev) · line 237

Quoted 7 lines
drivers/pci/pcie/aspm.c +1 -1
 	}
 	/* training failed -> recover */
-	if ((jiffies - start_jiffies) >= HZ) {
+	if ((jiffies - start_jiffies) >= LINK_RETRAIN_TIMEOUT) {
 		dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
 			    " common clock\n");
 		i = 0;

AFAICT this can trigger false positives. !reg16 test succeeds and then jiffies tick.

Quoted 1 line

...it could happen before but you make it way more probable...

No, because the test moved.

I came up with this loop (off the top of my head):

<willy> for (;;) {
<willy> pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
<willy> if (!(reg16 & PCI_EXP_LNKSTA_LT))
<willy> break;
<willy> if ((jiffies - start_jiffies) >= HZ)
<willy> break;
<willy> msleep(1);
<willy> }

Andrew has mostly followed that ... improving it to LINK_RETRAIN_TIMEOUT instead of HZ.

Yes, the subsequent test should be of reg16 instead of jiffies.

And we should be using time_before/after instead of the explicit comparison.

Signature · 4 lines
Matthew Wilcox				Intel Open Source Technology Centre
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours.  We can't possibly take such
a retrograde step."
#3 lore raw
Quoted 4 lines · Matthew Wilcox
Quoted 3 lines · Pavel Machek
Quoted 2 lines · Andrew Patterson

ASPM: Use msleep instead of cpu_relax during link retraining

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Author clearly wanted to do a busy loop... why do you think 10msec delay here is acceptable?

10ms? I see a 1ms sleep.

Yes... IIRC msleep will sleep for up-to 1/HZ on non-highres systems.

Quoted 1 line

Yes, the subsequent test should be of reg16 instead of jiffies.

Thanks.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#4 lore raw
Quoted 9 lines · Pavel Machek
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Pavel Machek
Quoted 5 lines · Andrew Patterson

ASPM: Use msleep instead of cpu_relax during link retraining

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Author clearly wanted to do a busy loop... why do you think 10msec delay here is acceptable?

10ms? I see a 1ms sleep.

Yes... IIRC msleep will sleep for up-to 1/HZ on non-highres systems.

Quoted 1 line

Yes, the subsequent test should be of reg16 instead of jiffies.

Andrew, care to send an updated patch which includes fixes for the issues caught by Pavel & Matthew?

Thanks,

Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
#5 lore raw
Quoted 13 lines · Jesse Barnes
Quoted 12 lines · Pavel Machek
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Pavel Machek
Quoted 5 lines · Andrew Patterson

ASPM: Use msleep instead of cpu_relax during link retraining

The cpu_relax() function can be a noop on certain architectures like IA-64 when CPU threads are disabled, so use msleep instead during link retraining busy/wait loop.

Author clearly wanted to do a busy loop... why do you think 10msec delay here is acceptable?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

10ms? I see a 1ms sleep.

Yes... IIRC msleep will sleep for up-to 1/HZ on non-highres systems.

Quoted 1 line

Yes, the subsequent test should be of reg16 instead of jiffies.

Andrew, care to send an updated patch which includes fixes for the issues caught by Pavel & Matthew?

Will do.

Andrew

Quoted 1 line

Thanks,