Skip to contentSkip to conversation

pinctrl: stm32: fix the unit of the hwspinlock timeout

PATCH Superseded Ju Nan 2 participants 4 messages Aug 5 – Aug 29

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ju Nan

author
#0 lore raw

PATCH 1/1 pinctrl: stm32: fix the unit of the hwspinlock timeout

HWSPNLCK_TIMEOUT is passed to hwspin_lock_timeout_in_atomic(), whose timeout argument is in milliseconds, not microseconds:

  atomic_delay += HWSPINLOCK_RETRY_DELAY_US;
  if (atomic_delay > to * 1000)
          return -ETIMEDOUT;

So the driver asks for a 1 second timeout where the comment next to the macro says it wants 1 millisecond.

The hwspinlock core documents this explicitly:

If the mode is HWLOCK_IN_ATOMIC (called from an atomic context) the timeout is handled with busy-waiting delays, hence shall not exceed few msecs.

Pass the value the comment always described. The core retries every HWSPINLOCK_RETRY_DELAY_US (100 us), so the semaphore is still polled ten times before giving up, which is far longer than any plausible hold time on the coprocessor side. A timeout is reported with dev_err() and fails the pin configuration or the interrupt allocation, so shortening it degrades gracefully.

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +1 -1
index 6a99708a5..dbc9143ec 100644
@@ -87,7 +87,7 @@
 #define gpio_range_to_bank(chip) \
 		container_of(chip, struct stm32_gpio_bank, range)
 
-#define HWSPNLCK_TIMEOUT	1000 /* usec */
+#define HWSPNLCK_TIMEOUT	1 /* msec */
 
 static const char * const stm32_gpio_functions[] = {
 	"gpio", "af0", "af1",
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 16 lines · Ju Nan

HWSPNLCK_TIMEOUT is passed to hwspin_lock_timeout_in_atomic(), whose timeout argument is in milliseconds, not microseconds:

  atomic_delay += HWSPINLOCK_RETRY_DELAY_US;
  if (atomic_delay > to * 1000)
          return -ETIMEDOUT;

So the driver asks for a 1 second timeout where the comment next to the macro says it wants 1 millisecond.

The hwspinlock core documents this explicitly:

  If the mode is HWLOCK_IN_ATOMIC (called from an atomic context) the timeout is handled with busy-waiting delays, hence shall not exceed few msecs.

Quoted 8 lines

Pass the value the comment always described. The core retries every HWSPINLOCK_RETRY_DELAY_US (100 us), so the semaphore is still polled ten times before giving up, which is far longer than any plausible hold time on the coprocessor side. A timeout is reported with dev_err() and fails the pin configuration or the interrupt allocation, so shortening it degrades gracefully.

Please add

Fixes 290a9f937e5a ("pinctrl: stm32: use the hwspin_lock_timeout_in_atomic() API")
Quoted 12 lines

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +0 -0
index 6a99708a5..dbc9143ec 100644
@@ -87,7 +87,7 @@
  #define gpio_range_to_bank(chip) \
                 container_of(chip, struct stm32_gpio_bank, range)

drivers/pinctrl/stm32/pinctrl-stm32.c line 90

Quoted 2 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +1 -1
-#define HWSPNLCK_TIMEOUT       1000 /* usec */
+#define HWSPNLCK_TIMEOUT       1 /* msec */

Please change the macro name as HWSPNLCK_TIMEOUT_MS to keep track of the time units

Quoted 3 lines


 static const char * const stm32_gpio_functions[] = { "gpio", "af0", "af1",

Thanks!
Antonio

author
#2 lore raw

PATCH v2 1/1 pinctrl: stm32: fix the unit of the hwspinlock timeout

HWSPNLCK_TIMEOUT is passed to hwspin_lock_timeout_in_atomic(), whose timeout argument is in milliseconds, not microseconds:

  atomic_delay += HWSPINLOCK_RETRY_DELAY_US;
  if (atomic_delay > to * 1000)
          return -ETIMEDOUT;

So the driver asks for a 1 second timeout where the comment next to the macro says it wants 1 millisecond. All seven call sites spin on the hardware semaphore with udelay(), from sections that hold bank->lock or irqmux_lock, so on a non-PREEMPT_RT kernel this can keep interrupts disabled for up to one second while waiting for the coprocessor.

The hwspinlock core documents this explicitly:

If the mode is HWLOCK_IN_ATOMIC (called from an atomic context) the timeout is handled with busy-waiting delays, hence shall not exceed few msecs.

Pass the value the comment always described. The core retries every HWSPINLOCK_RETRY_DELAY_US (100 us), so the semaphore is still polled ten times before giving up, which is far longer than any plausible hold time on the coprocessor side. A timeout is reported with dev_err() and fails the pin configuration or the interrupt allocation, so shortening it degrades gracefully.

Fixes 290a9f937e5a ("pinctrl: stm32: use the hwspin_lock_timeout_in_atomic() API")

---
changelog:
v2:
- Add the "Fixes" tag
- Redefine HWSPNLCK_TIMEOUT as HWSPNLCK_TIMEOUT_MS

v1: https://lore.kernel.org/all/[email protected]/

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +8 -8
index d97057ec2..0d888d30f 100644
@@ -87,7 +87,7 @@
 #define gpio_range_to_bank(chip) \
 		container_of(chip, struct stm32_gpio_bank, range)
 
-#define HWSPNLCK_TIMEOUT	1000 /* usec */
+#define HWSPNLCK_TIMEOUT_MS	1
 
 static const char * const stm32_gpio_functions[] = {
 	"gpio", "af0", "af1",
@@ -633,7 +633,7 @@ static int stm32_gpio_domain_alloc(struct irq_domain *d,
 
 	if (pctl->hwlock) {
 		ret = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);
 		if (ret) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			pctl->irqmux_map &= ~BIT(hwirq);
@@ -943,7 +943,7 @@ static int stm32_pmx_set_mode(struct stm32_gpio_bank *bank,
 
 	if (pctl->hwlock) {
 		err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
@@ -1086,7 +1086,7 @@ static int stm32_pconf_set_driving(struct stm32_gpio_bank *bank,
 
 	if (pctl->hwlock) {
 		err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
@@ -1132,7 +1132,7 @@ static int stm32_pconf_set_speed(struct stm32_gpio_bank *bank,
 
 	if (pctl->hwlock) {
 		err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
@@ -1178,7 +1178,7 @@ static int stm32_pconf_set_bias(struct stm32_gpio_bank *bank,
 
 	if (pctl->hwlock) {
 		err = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
@@ -1239,7 +1239,7 @@ static int stm32_pconf_set_advcfgr(struct stm32_gpio_bank *bank, int offset, u32
 	spin_lock_irqsave(&bank->lock, flags);
 
 	if (pctl->hwlock) {
-		err = hwspin_lock_timeout_in_atomic(pctl->hwlock, HWSPNLCK_TIMEOUT);
+		err = hwspin_lock_timeout_in_atomic(pctl->hwlock, HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
@@ -1317,7 +1317,7 @@ stm32_pconf_set_skew_delay(struct stm32_gpio_bank *bank, int offset, u32 delay,
 	spin_lock_irqsave(&bank->lock, flags);
 
 	if (pctl->hwlock) {
-		err = hwspin_lock_timeout_in_atomic(pctl->hwlock, HWSPNLCK_TIMEOUT);
+		err = hwspin_lock_timeout_in_atomic(pctl->hwlock, HWSPNLCK_TIMEOUT_MS);
 		if (err) {
 			dev_err(pctl->dev, "Can't get hwspinlock\n");
 			goto unlock;
Signature · 1 line
2.55.0
author
#3 lore raw
Quoted 15 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +1 -1
index d97057ec2..0d888d30f 100644
@@ -87,7 +87,7 @@
 #define gpio_range_to_bank(chip) \
 		container_of(chip, struct stm32_gpio_bank, range)
 
-#define HWSPNLCK_TIMEOUT	1000 /* usec */
+#define HWSPNLCK_TIMEOUT_MS	1
 
 static const char * const stm32_gpio_functions[] = {
 	"gpio", "af0", "af1",

drivers/pinctrl/stm32/pinctrl-stm32.c static int stm32_gpio_domain_alloc(struct irq_domain *d, · line 636

Quoted 5 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +1 -1
@@ -633,7 +633,7 @@ static int stm32_gpio_domain_alloc(struct irq_domain *d,
 
 	if (pctl->hwlock) {
 		ret = hwspin_lock_timeout_in_atomic(pctl->hwlock,
-						    HWSPNLCK_TIMEOUT);
+						    HWSPNLCK_TIMEOUT_MS);

Please ignore this patch for now, since it has a dependency issue with this one:
https://lore.kernel.org/lkml/[email protected]/

Thanks Antonio, I will think more about what you said.

Regards,
Ju Nan