From: Dan Carpenter <dan@pereq.eu>
To: Yaohan Li <shunsheng1218@gmail.com>
Cc: Dan Carpenter <error27@gmail.com>,
Mark Brown <broonie@kernel.org>,
oe-kbuild@lists.linux.dev, oe-kbuild-all@lists.linux.dev,
lkp@intel.com
Subject: Re: [broonie-regulator:for-7.5 6/6] drivers/regulator/qcom-labibb-regulator.c:444 qcom_labibb_setup_ocp_irq() warn: 'vreg->ocp_irq' from request_threaded_irq() not released on lines: 444.
Date: Fri, 27 Nov 2026 09:30:49 +0300 [thread overview]
Message-ID: <awkjmRQl0B-ZJdpd@stanley.mountain> (raw)
In-Reply-To: <awkf4CNhSPz-lcEk@stanley.mountain>
I asked Claude to read my blog and re-write the file. It fixes
the bug from earlier and two other bugs:
- The "Short-circuit irq not found" message passed vreg->dev before it
was set, so it printed without a device. It now uses dev.
- The "error registering" message printed ret, which held the earlier
regmap_read() result, not the registration error. It now prints
PTR_ERR(vreg->rdev).
Worth checking
- The order inside qcom_labibb_stop() is deliberate: it disables the
workers before the interrupt lines. The recovery workers are the
only callers of enable_irq(). If the lines were disabled first, a
worker still running could re-enable one afterwards.
- The OCP worker is now initialized even without an OCP interrupt.
That costs nothing and removes one condition to mirror. The worker
can only be queued by the OCP handler.
regards,
dan carpenter
diff --git a/drivers/regulator/qcom-labibb-regulator.c b/drivers/regulator/qcom-labibb-regulator.c
index 49aaa31a539d..c6d066577150 100644
--- a/drivers/regulator/qcom-labibb-regulator.c
+++ b/drivers/regulator/qcom-labibb-regulator.c
@@ -96,14 +96,18 @@ struct labibb_regulator {
int ocp_irq;
int ocp_irq_count;
int fatal_count;
- bool registered;
+ bool started;
bool ocp_requested;
- bool ocp_irq_done;
- bool shutting_down;
- /* serialises the over-current request between set_ocp() and probe */
+ bool ocp_enabled;
+ /* protects started, ocp_requested and ocp_enabled */
struct mutex ocp_lock;
};
+struct labibb_priv {
+ struct labibb_regulator *vregs[2];
+ int num_vregs;
+};
+
struct labibb_regulator_data {
const char *name;
u8 type;
@@ -314,134 +318,26 @@ static irqreturn_t qcom_labibb_ocp_isr(int irq, void *chip)
}
/*
- * 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.
+ * Turn on the over-current source and its interrupt line, once the
+ * protection was requested and the regulator is started.
*/
-static int qcom_labibb_setup_ocp_irq(struct labibb_regulator *vreg)
+static int qcom_labibb_ocp_arm(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)
+ if (!vreg->ocp_requested || vreg->ocp_enabled)
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;
- return ret;
+ enable_irq(vreg->ocp_irq);
+ vreg->ocp_enabled = true;
+
+ return 0;
}
static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
@@ -462,24 +358,10 @@ static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
return -EINVAL;
/* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
- switch (vreg->type) {
- case QCOM_LAB_TYPE:
- irq_trig_low = 1;
- break;
- case QCOM_IBB_TYPE:
- irq_trig_low = 0;
- break;
- default:
- return -EINVAL;
- }
+ irq_trig_low = vreg->type == QCOM_LAB_TYPE;
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,
@@ -502,13 +384,9 @@ static int qcom_labibb_set_ocp(struct regulator_dev *rdev, int lim,
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);
+ /* During regulator_register() this waits for qcom_labibb_start() */
+ if (vreg->started)
+ ret = qcom_labibb_ocp_arm(vreg);
unlock:
mutex_unlock(&vreg->ocp_lock);
@@ -899,153 +777,231 @@ static const struct of_device_id qcom_labibb_match[] = {
};
MODULE_DEVICE_TABLE(of, qcom_labibb_match);
-static int qcom_labibb_regulator_probe(struct platform_device *pdev)
+/* Turn on the interrupts and, if it was requested, over-current protection */
+static int qcom_labibb_start(struct labibb_regulator *vreg)
+{
+ int ret;
+
+ mutex_lock(&vreg->ocp_lock);
+ ret = qcom_labibb_ocp_arm(vreg);
+ if (ret)
+ goto unlock;
+
+ vreg->started = true;
+ enable_irq(vreg->sc_irq);
+unlock:
+ mutex_unlock(&vreg->ocp_lock);
+ return ret;
+}
+
+static void qcom_labibb_stop(struct labibb_regulator *vreg)
+{
+ bool ocp_enabled;
+
+ mutex_lock(&vreg->ocp_lock);
+ vreg->started = false;
+ ocp_enabled = vreg->ocp_enabled;
+ vreg->ocp_enabled = false;
+ mutex_unlock(&vreg->ocp_lock);
+
+ /* The workers requeue themselves, so cancel_delayed_work_sync() isn't enough */
+ disable_delayed_work_sync(&vreg->sc_recovery_work);
+ disable_irq(vreg->sc_irq);
+
+ if (ocp_enabled) {
+ disable_delayed_work_sync(&vreg->ocp_recovery_work);
+ disable_irq(vreg->ocp_irq);
+ qcom_labibb_ocp_hw_disable(vreg->rdev);
+ }
+}
+
+static int qcom_labibb_setup_vreg(struct device *dev, struct regmap *regmap,
+ const struct labibb_regulator_data *reg_data,
+ struct labibb_regulator **result)
{
- struct labibb_regulator *vreg;
- struct device *dev = &pdev->dev;
struct regulator_config cfg = {};
+ struct labibb_regulator *vreg;
struct device_node *reg_node;
- const struct labibb_regulator_data *reg_data;
- struct regmap *reg_regmap;
+ unsigned long ocp_irq_flags;
+ char *sc_irq_name;
unsigned int type;
int ret;
- reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
- if (!reg_regmap) {
- dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
- return -ENODEV;
+ /* Validate if the type of regulator is indeed what's mentioned in DT */
+ ret = regmap_read(regmap, reg_data->base + REG_PERPH_TYPE, &type);
+ if (ret < 0) {
+ dev_err(dev, "Peripheral type read failed ret=%d\n", ret);
+ return -EINVAL;
}
- reg_data = device_get_match_data(&pdev->dev);
- if (!reg_data)
- return -ENODEV;
+ if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
+ WARN_ON(type != reg_data->type))
+ return -EINVAL;
- for (; reg_data->name; reg_data++) {
- char *sc_irq_name;
- int irq = 0;
+ vreg = devm_kzalloc_obj(dev, *vreg);
+ if (!vreg)
+ return -ENOMEM;
- /* Validate if the type of regulator is indeed
- * what's mentioned in DT.
- */
- ret = regmap_read(reg_regmap, reg_data->base + REG_PERPH_TYPE,
- &type);
- if (ret < 0) {
- dev_err(dev,
- "Peripheral type read failed ret=%d\n",
- ret);
- return -EINVAL;
- }
+ sc_irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s-short-circuit",
+ reg_data->name);
+ if (!sc_irq_name)
+ return -ENOMEM;
- if (WARN_ON((type != QCOM_LAB_TYPE) && (type != QCOM_IBB_TYPE)) ||
- WARN_ON(type != reg_data->type))
- return -EINVAL;
-
- vreg = devm_kzalloc_obj(&pdev->dev, *vreg);
- if (!vreg)
- return -ENOMEM;
-
- sc_irq_name = devm_kasprintf(dev, GFP_KERNEL,
- "%s-short-circuit",
- reg_data->name);
- if (!sc_irq_name)
- return -ENOMEM;
-
- reg_node = of_get_child_by_name(pdev->dev.of_node,
- reg_data->name);
- if (!reg_node)
- return -EINVAL;
-
- /* The Short Circuit interrupt is critical */
- irq = of_irq_get_byname(reg_node, "sc-err");
- if (irq <= 0) {
- if (irq == 0)
- irq = -EINVAL;
-
- of_node_put(reg_node);
- return dev_err_probe(vreg->dev, irq,
- "Short-circuit irq not found.\n");
- }
- vreg->sc_irq = irq;
+ reg_node = of_get_child_by_name(dev->of_node, reg_data->name);
+ if (!reg_node)
+ return -EINVAL;
- /* OverCurrent Protection IRQ is optional */
- irq = of_irq_get_byname(reg_node, "ocp");
- vreg->ocp_irq = irq;
- vreg->ocp_irq_count = 0;
- of_node_put(reg_node);
-
- vreg->regmap = reg_regmap;
- 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);
-
- if (vreg->ocp_irq > 0)
- INIT_DELAYED_WORK(&vreg->ocp_recovery_work,
- qcom_labibb_ocp_recovery_worker);
-
- switch (vreg->type) {
- case QCOM_LAB_TYPE:
- /* LAB Limits: 200-1600mA */
- vreg->uA_limits.uA_min = 200000;
- vreg->uA_limits.uA_step = 200000;
- vreg->uA_limits.ovr_val = LAB_CURRENT_LIMIT_OVERRIDE_EN;
- break;
- case QCOM_IBB_TYPE:
- /* IBB Limits: 0-1550mA */
- vreg->uA_limits.uA_min = 0;
- vreg->uA_limits.uA_step = 50000;
- vreg->uA_limits.ovr_val = 0; /* No override bit */
- break;
- default:
- return -EINVAL;
- }
+ /* The Short Circuit interrupt is critical */
+ vreg->sc_irq = of_irq_get_byname(reg_node, "sc-err");
+ /* OverCurrent Protection IRQ is optional */
+ vreg->ocp_irq = of_irq_get_byname(reg_node, "ocp");
+ of_node_put(reg_node);
- memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
- vreg->desc.of_match = reg_data->name;
- vreg->desc.name = reg_data->name;
+ if (vreg->sc_irq <= 0) {
+ ret = vreg->sc_irq ?: -EINVAL;
+ return dev_err_probe(dev, ret, "Short-circuit irq not found.\n");
+ }
- cfg.dev = vreg->dev;
- cfg.driver_data = vreg;
- cfg.regmap = vreg->regmap;
+ vreg->regmap = regmap;
+ 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);
+ INIT_DELAYED_WORK(&vreg->ocp_recovery_work,
+ qcom_labibb_ocp_recovery_worker);
+
+ if (vreg->type == QCOM_LAB_TYPE) {
+ /* LAB Limits: 200-1600mA */
+ vreg->uA_limits.uA_min = 200000;
+ vreg->uA_limits.uA_step = 200000;
+ vreg->uA_limits.ovr_val = LAB_CURRENT_LIMIT_OVERRIDE_EN;
+ /* IRQ polarities - LAB: trigger-low, IBB: trigger-high */
+ ocp_irq_flags = IRQF_TRIGGER_LOW;
+ } else {
+ /* IBB Limits: 0-1550mA */
+ vreg->uA_limits.uA_min = 0;
+ vreg->uA_limits.uA_step = 50000;
+ vreg->uA_limits.ovr_val = 0; /* No override bit */
+ ocp_irq_flags = IRQF_TRIGGER_HIGH;
+ }
- vreg->rdev = devm_regulator_register(vreg->dev, &vreg->desc,
- &cfg);
+ memcpy(&vreg->desc, reg_data->desc, sizeof(vreg->desc));
+ vreg->desc.of_match = reg_data->name;
+ vreg->desc.name = reg_data->name;
- if (IS_ERR(vreg->rdev)) {
- dev_err(dev, "qcom_labibb: error registering %s : %d\n",
- reg_data->name, ret);
- return PTR_ERR(vreg->rdev);
- }
+ /* The handlers use vreg->rdev, so qcom_labibb_start() enables them */
+ ret = request_threaded_irq(vreg->sc_irq, NULL, qcom_labibb_sc_isr,
+ IRQF_ONESHOT | IRQF_TRIGGER_RISING |
+ IRQF_NO_AUTOEN,
+ sc_irq_name, vreg);
+ if (ret)
+ goto destroy_mutex;
- ret = devm_request_threaded_irq(vreg->dev, vreg->sc_irq, NULL,
- qcom_labibb_sc_isr,
- IRQF_ONESHOT |
- IRQF_TRIGGER_RISING,
- sc_irq_name, vreg);
+ if (vreg->ocp_irq > 0) {
+ ret = request_threaded_irq(vreg->ocp_irq, NULL,
+ qcom_labibb_ocp_isr,
+ IRQF_ONESHOT | ocp_irq_flags |
+ IRQF_NO_AUTOEN,
+ vreg->desc.name, vreg);
if (ret)
- return ret;
+ goto free_sc_irq;
+ }
- ret = devm_add_action_or_reset(vreg->dev,
- qcom_labibb_stop_works, vreg);
- if (ret)
- return ret;
+ cfg.dev = dev;
+ cfg.driver_data = vreg;
+ cfg.regmap = vreg->regmap;
- /*
- * 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);
+ vreg->rdev = regulator_register(dev, &vreg->desc, &cfg);
+ if (IS_ERR(vreg->rdev)) {
+ ret = PTR_ERR(vreg->rdev);
+ dev_err(dev, "qcom_labibb: error registering %s : %d\n",
+ reg_data->name, ret);
+ goto free_ocp_irq;
+ }
+
+ ret = qcom_labibb_start(vreg);
+ if (ret)
+ goto unregister;
+
+ *result = vreg;
+ return 0;
+
+unregister:
+ regulator_unregister(vreg->rdev);
+free_ocp_irq:
+ if (vreg->ocp_irq > 0)
+ free_irq(vreg->ocp_irq, vreg);
+free_sc_irq:
+ free_irq(vreg->sc_irq, vreg);
+destroy_mutex:
+ mutex_destroy(&vreg->ocp_lock);
+ return ret;
+}
+
+static void qcom_labibb_cleanup_vreg(struct labibb_regulator *vreg)
+{
+ qcom_labibb_stop(vreg);
+ regulator_unregister(vreg->rdev);
+ if (vreg->ocp_irq > 0)
+ free_irq(vreg->ocp_irq, vreg);
+ free_irq(vreg->sc_irq, vreg);
+ mutex_destroy(&vreg->ocp_lock);
+}
+
+static int qcom_labibb_regulator_probe(struct platform_device *pdev)
+{
+ const struct labibb_regulator_data *reg_data;
+ struct device *dev = &pdev->dev;
+ struct labibb_priv *priv;
+ struct regmap *reg_regmap;
+ int i, ret;
+
+ reg_regmap = dev_get_regmap(pdev->dev.parent, NULL);
+ if (!reg_regmap) {
+ dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
+ return -ENODEV;
+ }
+
+ reg_data = device_get_match_data(&pdev->dev);
+ if (!reg_data)
+ return -ENODEV;
+
+ priv = devm_kzalloc_obj(dev, *priv);
+ if (!priv)
+ return -ENOMEM;
+
+ for (i = 0; reg_data[i].name; i++) {
+ if (i >= ARRAY_SIZE(priv->vregs)) {
+ ret = -EINVAL;
+ goto cleanup_vregs;
+ }
+
+ ret = qcom_labibb_setup_vreg(dev, reg_regmap, ®_data[i],
+ &priv->vregs[i]);
if (ret)
- return ret;
+ goto cleanup_vregs;
}
+ priv->num_vregs = i;
+ platform_set_drvdata(pdev, priv);
+
return 0;
+
+cleanup_vregs:
+ while (--i >= 0)
+ qcom_labibb_cleanup_vreg(priv->vregs[i]);
+ return ret;
+}
+
+static void qcom_labibb_regulator_remove(struct platform_device *pdev)
+{
+ struct labibb_priv *priv = platform_get_drvdata(pdev);
+ int i;
+
+ for (i = priv->num_vregs - 1; i >= 0; i--)
+ qcom_labibb_cleanup_vreg(priv->vregs[i]);
}
static struct platform_driver qcom_labibb_regulator_driver = {
@@ -1055,6 +1011,7 @@ static struct platform_driver qcom_labibb_regulator_driver = {
.of_match_table = qcom_labibb_match,
},
.probe = qcom_labibb_regulator_probe,
+ .remove = qcom_labibb_regulator_remove,
};
module_platform_driver(qcom_labibb_regulator_driver);
next prev parent reply other threads:[~2026-11-27 6:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 17:55 [broonie-regulator:for-7.5 6/6] drivers/regulator/qcom-labibb-regulator.c:444 qcom_labibb_setup_ocp_irq() warn: 'vreg->ocp_irq' from request_threaded_irq() not released on lines: 444 kernel test robot
2026-11-26 5:56 ` Dan Carpenter
2026-11-26 7:16 ` Yaohan Li
2026-11-27 6:14 ` Dan Carpenter
2026-11-27 6:30 ` Dan Carpenter [this message]
2026-11-27 7:56 ` Yaohan Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awkjmRQl0B-ZJdpd@stanley.mountain \
--to=dan@pereq.eu \
--cc=broonie@kernel.org \
--cc=error27@gmail.com \
--cc=lkp@intel.com \
--cc=oe-kbuild-all@lists.linux.dev \
--cc=oe-kbuild@lists.linux.dev \
--cc=shunsheng1218@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.