mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] regulator: qcom-labibb: stop the recovery workers on teardown
@ 2026-11-18  7:23 Yaohan Li
  0 siblings, 0 replies; only message in thread
From: Yaohan Li @ 2026-11-18  7:23 UTC (permalink / raw)
  To: lgirdwood, broonie; +Cc: linux-arm-msm, linux-kernel, sumit.semwal

The driver never stops sc_recovery_work or ocp_recovery_work.  Both are
queued from the interrupt handlers and reschedule themselves from their
recovery paths, so they outlive the driver data that devres frees once
the probe or the removal is over.  A short-circuit or over-current event
followed by an unbind leaves a pending work item that runs against a
freed labibb_regulator.

A ->remove() callback would cover the unbind but not a probe that fails
after the interrupt was requested: the driver core does not call
->remove() for a failed probe, while devres still releases the
interrupts and the private data.

Register one devres action in ->probe() instead, after the short-circuit
interrupt, and let that action own every resource that has no devres
release of its own: it stops both workers and it releases the
over-current interrupt.  disable_delayed_work_sync() is used rather than
cancel_delayed_work_sync() because both workers reschedule themselves.

The over-current interrupt cannot be requested from
->set_over_current_protection(): the regulator core calls that op from
set_machine_constraints(), inside regulator_register(), so a devres node
created there is released after the regulator itself and the recovery
worker would still be runnable while vreg->rdev is gone.  That op now
only programs the interrupt registers and records the request; ->probe()
installs the interrupt once devm_regulator_register() has returned, and
a runtime first enable goes through the same helper.  Both requests stay
after the regulator node, so the teardown order becomes: stop the works,
release the interrupts, unregister the regulator.

The interrupt is requested with request_threaded_irq() rather than with
the devres helper so that the same action can release it; its name comes
from vreg->desc.name, which outlives the interrupt, while a name
allocated with devres would be released before the action runs.  This
also keeps the cleanup to a single devres action: a second one
registered from the helper would be released by devm_add_action_or_reset()
from under ocp_lock on allocation failure, and would then re-enter
itself through that lock.

The op and the probe side share a per-device mutex, and the probe
publishes the registration only after the deferred cleanup action
exists, so a runtime enable cannot request an interrupt that has no
teardown paired with it.  The hardware source is enabled only after the
handler has been installed, and turned back off when that fails, so a
retry can be attempted without leaving the source running on its own.

The teardown action publishes a shutdown flag before it drains anything:
an over-current request that runs while devres is releasing the actions
is refused instead of installing an interrupt whose cleanup action has
already gone, and the hardware source is turned off once the handlers
are stopped.  The interrupt may still be left disabled by a handler that
called disable_irq_nosync(), with no recovery worker left to re-enable
it; that depth is left alone on purpose, because free_irq() shuts the
line down regardless of it while an enable_irq() racing with a handler
that is still running would unmask a line whose thread has not
finished.

Fixes: 498ab2fdf855 ("regulator: qcom: Add labibb driver")
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Prepared against v7.4-rc1-1969-g666539eecdf4.

AI assistance: the analysis, the change and its review notes were
prepared with Claude (Anthropic) as a coding assistant.  The author
reviewed the reasoning and takes responsibility for the content.

Testing: compile tested only (arm64, gcc 11.4).  No PMI8998 hardware was
available, so no over-current or short-circuit event was run.
 drivers/regulator/qcom-labibb-regulator.c | 195 ++++++++++++++++++++--
 1 file changed, 177 insertions(+), 18 deletions(-)

diff --git a/drivers/regulator/qcom-labibb-regulator.c b/drivers/regulator/qcom-labibb-regulator.c
index 8641e69b55c3..49aaa31a539d 100644
--- a/drivers/regulator/qcom-labibb-regulator.c
+++ b/drivers/regulator/qcom-labibb-regulator.c
@@ -96,6 +96,12 @@ struct labibb_regulator {
 	int				ocp_irq;
 	int				ocp_irq_count;
 	int				fatal_count;
+	bool				registered;
+	bool				ocp_requested;
+	bool				ocp_irq_done;
+	bool				shutting_down;
+	/* serialises the over-current request between set_ocp() and probe */
+	struct mutex			ocp_lock;
 };
 
 struct labibb_regulator_data {
@@ -307,12 +313,141 @@ static irqreturn_t qcom_labibb_ocp_isr(int irq, void *chip)
 	return IRQ_HANDLED;
 }
 
+/*
+ * Stop the recovery workers and release the over-current interrupt.  A
+ * devres action is used instead of ->remove() so that a probe which fails
+ * after the resources were set up goes through the same teardown: the
+ * driver core does not call ->remove() for a failed probe, but devres
+ * still releases what was registered.
+ *
+ * This is the only cleanup entry, and the over-current interrupt is
+ * requested without devres so that it can be released here: registering a
+ * second action from a path that runs under ocp_lock would also make
+ * devm_add_action_or_reset() run this function from under that lock on
+ * allocation failure.
+ */
+static void qcom_labibb_stop_works(void *data)
+{
+	struct labibb_regulator *vreg = data;
+	int irq = 0;
+
+	/*
+	 * Announce the shutdown before anything is drained: a set_ocp()
+	 * that runs from here on must not request the over-current
+	 * interrupt while devres is releasing the action that would stop
+	 * its worker.
+	 */
+	mutex_lock(&vreg->ocp_lock);
+	vreg->shutting_down = true;
+	mutex_unlock(&vreg->ocp_lock);
+
+	/*
+	 * Both recovery workers reschedule themselves, so a plain cancel
+	 * would leave them queued again: disable_delayed_work_sync() also
+	 * blocks the requeue.
+	 */
+	disable_delayed_work_sync(&vreg->sc_recovery_work);
+	if (vreg->ocp_irq > 0) {
+		disable_delayed_work_sync(&vreg->ocp_recovery_work);
+
+		/*
+		 * The over-current handler turns the source off before it
+		 * queues the worker, but a handler that was stopped above
+		 * may have done so after the worker was drained: leave the
+		 * source off now that nobody would turn it back on.
+		 */
+		qcom_labibb_ocp_hw_disable(vreg->rdev);
+
+		mutex_lock(&vreg->ocp_lock);
+		if (vreg->ocp_irq_done) {
+			vreg->ocp_irq_done = false;
+			irq = vreg->ocp_irq;
+		}
+		mutex_unlock(&vreg->ocp_lock);
+
+		/*
+		 * A stopped handler can also have left the interrupt
+		 * disabled with disable_irq_nosync(), while the recovery
+		 * worker - the only caller of enable_irq() - is gone.  The
+		 * depth is deliberately not unwound here: free_irq() shuts
+		 * the line down regardless of it, whereas an enable_irq()
+		 * racing with a handler that is still running would unmask
+		 * a line whose thread has not finished.
+		 *
+		 * free_irq() waits for a running handler, so it is called
+		 * without ocp_lock held.
+		 */
+		if (irq)
+			free_irq(irq, vreg);
+	}
+}
+
+/*
+ * Request the over-current interrupt once the regulator is registered.
+ * Requesting it from ->set_over_current_protection() is not possible: the
+ * regulator core calls that op from set_machine_constraints(), inside
+ * regulator_register(), and a devres node created there is released after
+ * the regulator itself.  The recovery worker would then still be runnable
+ * while vreg->rdev is already gone.
+ */
+static int qcom_labibb_setup_ocp_irq(struct labibb_regulator *vreg)
+{
+	u32 irq_flags = IRQF_ONESHOT;
+	int ret;
+
+	lockdep_assert_held(&vreg->ocp_lock);
+
+	if (vreg->shutting_down)
+		return -ESHUTDOWN;
+
+	if (!vreg->ocp_requested)
+		return 0;
+
+	if (!vreg->ocp_irq_done) {
+		/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
+		switch (vreg->type) {
+		case QCOM_LAB_TYPE:
+			irq_flags |= IRQF_TRIGGER_LOW;
+			break;
+		case QCOM_IBB_TYPE:
+			irq_flags |= IRQF_TRIGGER_HIGH;
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		/*
+		 * The interrupt is owned and released by the cleanup
+		 * action, so its name has to outlive the interrupt:
+		 * devres would release a name allocated here before the
+		 * action runs, while vreg->desc.name is owned by the
+		 * driver data.
+		 */
+		ret = request_threaded_irq(vreg->ocp_irq, NULL,
+					   qcom_labibb_ocp_isr, irq_flags,
+					   vreg->desc.name, vreg);
+		if (ret)
+			return ret;
+
+		vreg->ocp_irq_done = true;
+	}
+
+	/*
+	 * Enable the hardware source only once a handler exists, and turn
+	 * it back off if that failed, so that a later attempt can retry
+	 * without leaving the source enabled on its own.
+	 */
+	ret = qcom_labibb_ocp_hw_enable(vreg->rdev);
+	if (ret)
+		qcom_labibb_ocp_hw_disable(vreg->rdev);
+
+	return ret;
+}
+
 static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
 			       int severity, bool enable)
 {
 	struct labibb_regulator *vreg = rdev_get_drvdata(rdev);
-	char *ocp_irq_name;
-	u32 irq_flags = IRQF_ONESHOT;
 	int irq_trig_low, ret;
 
 	/*
@@ -326,52 +461,58 @@ static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
 	if (vreg->ocp_irq <= 0)
 		return -EINVAL;
 
-	ocp_irq_name = devm_kasprintf(vreg->dev, GFP_KERNEL, "%s-over-current",
-				      vreg->desc.name);
-	if (!ocp_irq_name)
-		return -ENOMEM;
-
 	/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
 	switch (vreg->type) {
 	case QCOM_LAB_TYPE:
-		irq_flags |= IRQF_TRIGGER_LOW;
 		irq_trig_low = 1;
 		break;
 	case QCOM_IBB_TYPE:
-		irq_flags |= IRQF_TRIGGER_HIGH;
 		irq_trig_low = 0;
 		break;
 	default:
 		return -EINVAL;
 	}
 
+	mutex_lock(&vreg->ocp_lock);
+
+	if (vreg->shutting_down) {
+		ret = -ESHUTDOWN;
+		goto unlock;
+	}
+
 	/* Activate OCP HW level interrupt */
 	ret = regmap_update_bits(rdev->regmap,
 				 vreg->base + REG_LABIBB_INT_SET_TYPE,
 				 LABIBB_INT_VREG_OK,
 				 LABIBB_INT_VREG_TYPE_LEVEL);
 	if (ret)
-		return ret;
+		goto unlock;
 
 	/* Set OCP interrupt polarity */
 	ret = regmap_update_bits(rdev->regmap,
 				 vreg->base + REG_LABIBB_INT_POLARITY_HIGH,
 				 LABIBB_INT_VREG_OK, !irq_trig_low);
 	if (ret)
-		return ret;
+		goto unlock;
 	ret = regmap_update_bits(rdev->regmap,
 				 vreg->base + REG_LABIBB_INT_POLARITY_LOW,
 				 LABIBB_INT_VREG_OK, irq_trig_low);
 	if (ret)
-		return ret;
+		goto unlock;
 
-	ret = qcom_labibb_ocp_hw_enable(rdev);
-	if (ret)
-		return ret;
+	vreg->ocp_requested = true;
+
+	/*
+	 * During regulator_register() there is no interrupt yet and no
+	 * cleanup action to pair with it; the probe finishes the job once
+	 * the regulator and its devres nodes exist.
+	 */
+	if (vreg->registered)
+		ret = qcom_labibb_setup_ocp_irq(vreg);
 
-	return devm_request_threaded_irq(vreg->dev, vreg->ocp_irq, NULL,
-					 qcom_labibb_ocp_isr, irq_flags,
-					 ocp_irq_name, vreg);
+unlock:
+	mutex_unlock(&vreg->ocp_lock);
+	return ret;
 }
 
 /**
@@ -836,6 +977,7 @@ static int qcom_labibb_regulator_probe(struct platform_device *pdev)
 		vreg->dev = dev;
 		vreg->base = reg_data->base;
 		vreg->type = reg_data->type;
+		mutex_init(&vreg->ocp_lock);
 		INIT_DELAYED_WORK(&vreg->sc_recovery_work,
 				  qcom_labibb_sc_recovery_worker);
 
@@ -884,6 +1026,23 @@ static int qcom_labibb_regulator_probe(struct platform_device *pdev)
 						sc_irq_name, vreg);
 		if (ret)
 			return ret;
+
+		ret = devm_add_action_or_reset(vreg->dev,
+					       qcom_labibb_stop_works, vreg);
+		if (ret)
+			return ret;
+
+		/*
+		 * Publish the registration only now: a concurrent
+		 * set_ocp() must not be able to request the over-current
+		 * interrupt before the action that stops its worker exists.
+		 */
+		mutex_lock(&vreg->ocp_lock);
+		vreg->registered = true;
+		ret = qcom_labibb_setup_ocp_irq(vreg);
+		mutex_unlock(&vreg->ocp_lock);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
-- 
2.34.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18  7:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  7:23 [PATCH] regulator: qcom-labibb: stop the recovery workers on teardown Yaohan Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®