Skip to contentSkip to conversation

regulator: freeze delayed hardware access during suspend

PATCH In review Jiale Yao 4 participants 9 messages Oct 16 – Oct 27

Subsystems (2)
lore
Share & tools
lore

Jiale Yao

author
#0 lore raw

cover regulator: freeze delayed hardware access during suspend

Deferred regulator disable and IRQ recovery callbacks can perform hardware access after a caller-provided delay. Their current workqueues remain active while devices are being suspended, allowing the callbacks to reach a regulator after its backing bus has been suspended.

Move deferred disable to the freezable power-efficient workqueue. Move IRQ helper callbacks to a freezable workqueue as well, while retaining the high-priority property for users that request it. Each patch is independent and can be applied separately.

Jiale Yao (2):
regulator: core: freeze deferred disable work during suspend
regulator: irq: freeze deferred hardware access during suspend

 drivers/regulator/core.c        |  3 ++-
 drivers/regulator/irq_helpers.c | 38 +++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 regulator: core: freeze deferred disable work during suspend

regulator_disable_deferred() schedules hardware access after a caller-provided delay. The power-efficient system workqueue remains active during system suspend, so deferred regulator operations can reach a bus after its controller has been suspended.

Use the freezable power-efficient workqueue to preserve the existing power policy while deferring regulator access until devices have resumed.

---
 drivers/regulator/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/regulator/core.c +2 -1
index 6a4008f387b5..317f02696e47 100644
@@ -3465,7 +3465,8 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
 
 	regulator_lock(rdev);
 	regulator->deferred_disables++;
-	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
+	mod_delayed_work(system_freezable_power_efficient_wq,
+			 &rdev->disable_work,
 			 msecs_to_jiffies(ms));
 	regulator_unlock(rdev);
 
Signature · 1 line
2.34.1
#2 lore raw

Re:[PATCH 1/2] regulator: core: freeze deferred disable work during suspend

Quoted 8 lines · Jiale Yao

regulator_disable_deferred() schedules hardware access after a caller-provided delay. The power-efficient system workqueue remains active during system suspend, so deferred regulator operations can reach a bus after its controller has been suspended.

Use the freezable power-efficient workqueue to preserve the existing power policy while deferring regulator access until devices have resumed.

Fixes da07ecd93b19 ("regulator: Implement deferred disable support")
Quoted 21 lines

---
drivers/regulator/core.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/regulator/core.c +2 -1
index 6a4008f387b5..317f02696e47 100644
@@ -3465,7 +3465,8 @@ int regulator_disable_deferred(struct regulator *regulator, int ms)
 
 	regulator_lock(rdev);
 	regulator->deferred_disables++;
-	mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
+	mod_delayed_work(system_freezable_power_efficient_wq,
+			 &rdev->disable_work,
 			 msecs_to_jiffies(ms));
 	regulator_unlock(rdev);
 
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH 1/2] regulator: core: freeze deferred disable work during suspend

In reply to Jiale Yao
Quoted 7 lines · Jiale Yao

regulator_disable_deferred() schedules hardware access after a caller-provided delay. The power-efficient system workqueue remains active during system suspend, so deferred regulator operations can reach a bus after its controller has been suspended.

Use the freezable power-efficient workqueue to preserve the existing power policy while deferring regulator access until devices have resumed.

This is going to have a fun interaction with the only current user of deferred disables, DAPM. If a DAPM supply that uses deferred disables is active at system suspend then it will disable it using a deferred disable but since the workqueue is frozen quite early on in suspend this will simply result in the disable not happening until after the suspend has completed which isn't the desired outcome.

Since if we're suspending it's reasonable to assume that the supply won't be bounced on again quickly, or that if it is we can reasonably pay the cost of full ramp up, we should probably arrange for the disable to be processed immeditely if we're in suspend. In addition if there are already pending disables it'd make sense to flush them rather than letting the workqueue freeze, for similar reasons.

#4 lore raw

Re:Re: [PATCH 1/2] regulator: core: freeze deferred disable work during suspend

Quoted 17 lines · Mark Brown
Quoted 7 lines · Jiale Yao

regulator_disable_deferred() schedules hardware access after a caller-provided delay. The power-efficient system workqueue remains active during system suspend, so deferred regulator operations can reach a bus after its controller has been suspended.

Use the freezable power-efficient workqueue to preserve the existing power policy while deferring regulator access until devices have resumed.

This is going to have a fun interaction with the only current user of deferred disables, DAPM. If a DAPM supply that uses deferred disables is active at system suspend then it will disable it using a deferred disable but since the workqueue is frozen quite early on in suspend this will simply result in the disable not happening until after the suspend has completed which isn't the desired outcome.

Quoted 6 lines

Since if we're suspending it's reasonable to assume that the supply won't be bounced on again quickly, or that if it is we can reasonably pay the cost of full ramp up, we should probably arrange for the disable to be processed immeditely if we're in suspend. In addition if there are already pending disables it'd make sense to flush them rather than letting the workqueue freeze, for similar reasons.

Thanks for your review, this is updated version,
https://lore.kernel.org/all/[email protected]/

author
#5 lore raw

PATCH 2/2 regulator: irq: freeze deferred hardware access during suspend

In reply to Jiale Yao

The regulator IRQ helper delays status checks and recovery callbacks on non-freezable workqueues. Those callbacks may access regulator hardware after the backing bus has been suspended.

Use system_freezable_wq for normal-priority callbacks. Allocate a freezable high-priority workqueue when requested so the high_prio contract is preserved while hardware access is frozen across suspend.

---
 drivers/regulator/irq_helpers.c | 38 +++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)
drivers/regulator/irq_helpers.c +22 -16
index 5b3cfac28667..cdbd170d2b15 100644
@@ -15,6 +15,7 @@
 #include <linux/regmap.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/workqueue.h>
 #include <linux/regulator/driver.h>
 
 #include "internal.h"
@@ -27,6 +28,7 @@ struct regulator_irq {
 	int irq;
 	int retry_cnt;
 	struct delayed_work isr_work;
+	struct workqueue_struct *wq;
 };
 
 /*
@@ -145,12 +147,7 @@ static void regulator_notifier_isr_work(struct work_struct *work)
 	return;
 
 reschedule:
-	if (!d->high_prio)
-		mod_delayed_work(system_dfl_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
-	else
-		mod_delayed_work(system_highpri_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
+	mod_delayed_work(h->wq, &h->isr_work, msecs_to_jiffies(tmo));
 }
 
 static irqreturn_t regulator_notifier_isr(int irq, void *data)
@@ -248,13 +245,8 @@ static irqreturn_t regulator_notifier_isr(int irq, void *data)
 	}
 
 	if (d->irq_off_ms) {
-		if (!d->high_prio)
-			schedule_delayed_work(&h->isr_work,
-					      msecs_to_jiffies(d->irq_off_ms));
-		else
-			mod_delayed_work(system_highpri_wq,
-					 &h->isr_work,
-					 msecs_to_jiffies(d->irq_off_ms));
+		mod_delayed_work(h->wq, &h->isr_work,
+				 msecs_to_jiffies(d->irq_off_ms));
 	}
 
 	return IRQ_HANDLED;
@@ -363,13 +355,25 @@ void *regulator_irq_helper(struct device *dev,
 
 	init_rdev_errors(h);
 
-	if (h->desc.irq_off_ms)
+	if (h->desc.irq_off_ms) {
+		if (h->desc.high_prio) {
+			h->wq = alloc_workqueue("regulator_irq_highpri",
+						WQ_FREEZABLE | WQ_HIGHPRI, 0);
+			if (!h->wq)
+				return ERR_PTR(-ENOMEM);
+		} else {
+			h->wq = system_freezable_wq;
+		}
+
 		INIT_DELAYED_WORK(&h->isr_work, regulator_notifier_isr_work);
+	}
 
 	ret = request_threaded_irq(h->irq, NULL, regulator_notifier_isr,
 				   IRQF_ONESHOT | irq_flags, h->desc.name, h);
 	if (ret) {
 		dev_err(dev, "Failed to request IRQ %d\n", irq);
+		if (h->desc.irq_off_ms && h->desc.high_prio)
+			destroy_workqueue(h->wq);
 
 		return ERR_PTR(ret);
 	}
@@ -393,8 +397,11 @@ void regulator_irq_helper_cancel(void **handle)
 		struct regulator_irq *h = *handle;
 
 		free_irq(h->irq, h);
-		if (h->desc.irq_off_ms)
+		if (h->desc.irq_off_ms) {
 			cancel_delayed_work_sync(&h->isr_work);
+			if (h->desc.high_prio)
+				destroy_workqueue(h->wq);
+		}
 
 		h = NULL;
 	}
@@ -441,4 +448,3 @@ int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
 	return 0;
 }
 EXPORT_SYMBOL_GPL(regulator_irq_map_event_simple);
-
Signature · 1 line
2.34.1
#6 lore raw

Re:[PATCH 2/2] regulator: irq: freeze deferred hardware access during suspend

Quoted 8 lines · Jiale Yao

The regulator IRQ helper delays status checks and recovery callbacks on non-freezable workqueues. Those callbacks may access regulator hardware after the backing bus has been suspended.

Use system_freezable_wq for normal-priority callbacks. Allocate a freezable high-priority workqueue when requested so the high_prio contract is preserved while hardware access is frozen across suspend.

Fixes 7111c6d1b31b ("regulator: IRQ based event/error notification helpers")
Quoted 102 lines

---
drivers/regulator/irq_helpers.c | 38 +++++++++++++++++++--------------

1 file changed, 22 insertions(+), 16 deletions(-)
drivers/regulator/irq_helpers.c +2 -0
index 5b3cfac28667..cdbd170d2b15 100644
@@ -15,6 +15,7 @@
 #include <linux/regmap.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/workqueue.h>
 #include <linux/regulator/driver.h>
 
 #include "internal.h"
@@ -27,6 +28,7 @@ struct regulator_irq {
 	int irq;
 	int retry_cnt;
 	struct delayed_work isr_work;
+	struct workqueue_struct *wq;
 };
 
 /*
@@ -145,12 +147,7 @@ static void regulator_notifier_isr_work(struct work_struct *work)
 	return;
 

reschedule:

drivers/regulator/irq_helpers.c +1 -6
-	if (!d->high_prio)
-		mod_delayed_work(system_dfl_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
-	else
-		mod_delayed_work(system_highpri_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
+	mod_delayed_work(h->wq, &h->isr_work, msecs_to_jiffies(tmo));

}

static irqreturn_t regulator_notifier_isr(int irq, void *data)

drivers/regulator/irq_helpers.c +19 -10
@@ -248,13 +245,8 @@ static irqreturn_t regulator_notifier_isr(int irq, void *data)
 	}
 
 	if (d->irq_off_ms) {
-		if (!d->high_prio)
-			schedule_delayed_work(&h->isr_work,
-					      msecs_to_jiffies(d->irq_off_ms));
-		else
-			mod_delayed_work(system_highpri_wq,
-					 &h->isr_work,
-					 msecs_to_jiffies(d->irq_off_ms));
+		mod_delayed_work(h->wq, &h->isr_work,
+				 msecs_to_jiffies(d->irq_off_ms));
 	}
 
 	return IRQ_HANDLED;
@@ -363,13 +355,25 @@ void *regulator_irq_helper(struct device *dev,
 
 	init_rdev_errors(h);
 
-	if (h->desc.irq_off_ms)
+	if (h->desc.irq_off_ms) {
+		if (h->desc.high_prio) {
+			h->wq = alloc_workqueue("regulator_irq_highpri",
+						WQ_FREEZABLE | WQ_HIGHPRI, 0);
+			if (!h->wq)
+				return ERR_PTR(-ENOMEM);
+		} else {
+			h->wq = system_freezable_wq;
+		}
+
 		INIT_DELAYED_WORK(&h->isr_work, regulator_notifier_isr_work);
+	}
 
 	ret = request_threaded_irq(h->irq, NULL, regulator_notifier_isr,
 				   IRQF_ONESHOT | irq_flags, h->desc.name, h);
 	if (ret) {
 		dev_err(dev, "Failed to request IRQ %d\n", irq);
+		if (h->desc.irq_off_ms && h->desc.high_prio)
+			destroy_workqueue(h->wq);
 
 		return ERR_PTR(ret);
 	}
@@ -393,8 +397,11 @@ void regulator_irq_helper_cancel(void **handle)
 		struct regulator_irq *h = *handle;
 
 		free_irq(h->irq, h);
-		if (h->desc.irq_off_ms)
+		if (h->desc.irq_off_ms) {
 			cancel_delayed_work_sync(&h->isr_work);
+			if (h->desc.high_prio)
+				destroy_workqueue(h->wq);
+		}
 
 		h = NULL;
 	}
@@ -441,4 +448,3 @@ int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
 	return 0;
 }
 EXPORT_SYMBOL_GPL(regulator_irq_map_event_simple);
-
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] regulator: irq: freeze deferred hardware access during suspend

In reply to Jiale Yao
Quoted 3 lines · Jiale Yao

The regulator IRQ helper delays status checks and recovery callbacks on non-freezable workqueues. Those callbacks may access regulator hardware after the backing bus has been suspended.

I wonder if there has been a real system where the problem has been seen or if this is purely a theoretical problem? But yes, I think this can happen.

Quoted 4 lines

Use system_freezable_wq for normal-priority callbacks. Allocate a freezable high-priority workqueue when requested so the high_prio contract is preserved while hardware access is frozen across suspend.

Quoted 1 line

I don't really know the internals of workqueues well enough to say if there are any potential side-effects. Hence my review is a bit of superficial. With this remark:

Quoted 25 lines
---
  drivers/regulator/irq_helpers.c | 38 +++++++++++++++++++--------------
  1 file changed, 22 insertions(+), 16 deletions(-)
drivers/regulator/irq_helpers.c +2 -0
index 5b3cfac28667..cdbd170d2b15 100644
@@ -15,6 +15,7 @@
  #include <linux/regmap.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
+#include <linux/workqueue.h>
  #include <linux/regulator/driver.h>
 
  #include "internal.h"
@@ -27,6 +28,7 @@ struct regulator_irq {
  	int irq;
  	int retry_cnt;
  	struct delayed_work isr_work;
+	struct workqueue_struct *wq;
  };
 
  /*

drivers/regulator/irq_helpers.c static void regulator_notifier_isr_work(struct work_struct *work) · line 150

Quoted 10 lines
drivers/regulator/irq_helpers.c +1 -6
@@ -145,12 +147,7 @@ static void regulator_notifier_isr_work(struct work_struct *work)
  	return;
 
  reschedule:
-	if (!d->high_prio)
-		mod_delayed_work(system_dfl_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
-	else
-		mod_delayed_work(system_highpri_wq, &h->isr_work,
-				 msecs_to_jiffies(tmo));
+	mod_delayed_work(h->wq, &h->isr_work, msecs_to_jiffies(tmo));

I like the fact that this change removes the if (!d->high_prio) from the ISR (below) and this worker, and does the queue selection at the initialization time. I suppose this could be done even if workqueue types weren't changed. Thanks for this :)

Quoted 64 lines
drivers/regulator/irq_helpers.c +19 -10
  }
 
  static irqreturn_t regulator_notifier_isr(int irq, void *data)
@@ -248,13 +245,8 @@ static irqreturn_t regulator_notifier_isr(int irq, void *data)
  	}
 
  	if (d->irq_off_ms) {
-		if (!d->high_prio)
-			schedule_delayed_work(&h->isr_work,
-					      msecs_to_jiffies(d->irq_off_ms));
-		else
-			mod_delayed_work(system_highpri_wq,
-					 &h->isr_work,
-					 msecs_to_jiffies(d->irq_off_ms));
+		mod_delayed_work(h->wq, &h->isr_work,
+				 msecs_to_jiffies(d->irq_off_ms));
  	}
 
  	return IRQ_HANDLED;
@@ -363,13 +355,25 @@ void *regulator_irq_helper(struct device *dev,
 
  	init_rdev_errors(h);
 
-	if (h->desc.irq_off_ms)
+	if (h->desc.irq_off_ms) {
+		if (h->desc.high_prio) {
+			h->wq = alloc_workqueue("regulator_irq_highpri",
+						WQ_FREEZABLE | WQ_HIGHPRI, 0);
+			if (!h->wq)
+				return ERR_PTR(-ENOMEM);
+		} else {
+			h->wq = system_freezable_wq;
+		}
+
  		INIT_DELAYED_WORK(&h->isr_work, regulator_notifier_isr_work);
+	}
 
  	ret = request_threaded_irq(h->irq, NULL, regulator_notifier_isr,
  				   IRQF_ONESHOT | irq_flags, h->desc.name, h);
  	if (ret) {
  		dev_err(dev, "Failed to request IRQ %d\n", irq);
+		if (h->desc.irq_off_ms && h->desc.high_prio)
+			destroy_workqueue(h->wq);
 
  		return ERR_PTR(ret);
  	}
@@ -393,8 +397,11 @@ void regulator_irq_helper_cancel(void **handle)
  		struct regulator_irq *h = *handle;
 
  		free_irq(h->irq, h);
-		if (h->desc.irq_off_ms)
+		if (h->desc.irq_off_ms) {
  			cancel_delayed_work_sync(&h->isr_work);
+			if (h->desc.high_prio)
+				destroy_workqueue(h->wq);
+		}
 
  		h = NULL;
  	}
@@ -441,4 +448,3 @@ int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
  	return 0;
  }
  EXPORT_SYMBOL_GPL(regulator_irq_map_event_simple);
-
Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
maintainer
#8 lore raw

Re: (subset) [PATCH 0/2] regulator: freeze delayed hardware access during suspend

In reply to Jiale Yao
Quoted 8 lines · Jiale Yao

regulator: freeze delayed hardware access during suspend

Deferred regulator disable and IRQ recovery callbacks can perform hardware access after a caller-provided delay. Their current workqueues remain active while devices are being suspended, allowing the callbacks to reach a regulator after its backing bus has been suspended.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-7.4

Thanks!

[2/2] regulator: irq: freeze deferred hardware access during suspend

      https://git.kernel.org/broonie/regulator/c/8a6d73e316f9

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark