Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jishnu Prakash" <jishnu.prakash@oss.qualcomm.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 4/4] regulator: qcom-rpmh: Add debounce delay before disabling regulator
Date: Tue, 27 Oct 2026 12:58:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-186593@kernel.org> (raw)
In-Reply-To: <20261027-regulator-off-on-delay-v5-4-f9c3b128c6d0@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] regulator: qcom-rpmh: race between teardown and rpmh_regulator_enable()
- [Medium] regulator: qcom-rpmh: vreg->enabled left false when rpmh_delayed_off_work() fails
- [Medium] regulator: qcom-rpmh: premature parent supply disable during debounce_delay
--

commit 5538c8b7a418742a3928d43327d4d04c4bf72849
Author: Jishnu Prakash <jishnu.prakash@oss.qualcomm.com>

regulator: qcom-rpmh: Add debounce delay before disabling regulator

This commit adds support for the qcom,regulator-off-debounce-delay-us property
to defer RPMh regulator disable requests via a cancelable delayed work item. It
also tracks off_on_delay locally when debounce_delay is configured so the minimum
off time is measured from when the disable request reaches RPMh.

> diff --git a/drivers/regulator/qcom-rpmh-regulator.c b/drivers/regulator/qcom-rpmh-regulator.c
> index 25c14de3cd8b5..28060bcfe48f4 100644
> --- a/drivers/regulator/qcom-rpmh-regulator.c
> +++ b/drivers/regulator/qcom-rpmh-regulator.c

[ ... ]

> @@ -313,19 +354,76 @@ static int rpmh_regulator_set_enable_state(struct regulator_dev *rdev,
>  
>  	ret = rpmh_regulator_send_request(vreg, &cmd, enable);
>  	if (!ret)
> -		vreg->enabled = enable;
> +		WRITE_ONCE(vreg->enabled, enable);
>  
>  	return ret;
>  }
>  
> +static void rpmh_delayed_off_work(struct work_struct *work)
> +{
> +	struct rpmh_vreg *vreg = container_of(work,
> +					struct rpmh_vreg, delayed_off_work.work);
> +
> +	/*
> +	 * No locking needed. cancel_delayed_work_sync() already guarantees
> +	 * this work has either not started or has fully finished by the
> +	 * time enable()/teardown proceed to touch shared state.
> +	 */
> +	if (!_rpmh_regulator_set_enable_state(vreg, READ_ONCE(vreg->rdev), false))
> +		vreg->last_off = ktime_get_boottime();
> +}

[Severity: Medium]
What happens to vreg->enabled if _rpmh_regulator_set_enable_state() fails in
rpmh_delayed_off_work()?

When rpmh_regulator_disable() queues vreg->delayed_off_work, it sets
vreg->enabled to false before the RPMh request is sent. When
rpmh_delayed_off_work() later runs:

rpmh_delayed_off_work()
  _rpmh_regulator_set_enable_state(vreg, READ_ONCE(vreg->rdev), false)
    rpmh_regulator_send_request(vreg, &cmd, false)

If rpmh_write_async() fails with -ENOMEM or rpmh_write() fails with
-ETIMEDOUT (when vreg->always_wait_for_ack is set), vreg->last_off is not
updated, but vreg->enabled remains false even though the hardware rail is
still enabled and vreg->delayed_off_work is no longer pending.

Could this leave vreg->enabled out of sync with the hardware state, causing
rpmh_regulator_is_enabled() to report false for an enabled rail and causing
regulator_late_cleanup() or subsequent voltage changes to treat the active
rail as disabled?

> +
>  static int rpmh_regulator_enable(struct regulator_dev *rdev)
>  {
> -	return rpmh_regulator_set_enable_state(rdev, true);
> +	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
> +	s64 remaining;
> +
> +	/* See struct rpmh_vreg's @lock for why no lock is needed here. */
> +	if (cancel_delayed_work_sync(&vreg->delayed_off_work)) {
> +		/* Queued disable never reached RPMh, so undo disable()'s flip. */
> +		WRITE_ONCE(vreg->enabled, true);
> +		return 0;
> +	}
> +
> +	if (vreg->off_on_delay) {
> +		remaining = ktime_us_delta(ktime_add_us(vreg->last_off,
> +							vreg->off_on_delay),
> +							ktime_get_boottime());
> +		if (remaining > 0)
> +			fsleep(remaining);
> +	}
> +
> +	return _rpmh_regulator_set_enable_state(vreg, rdev, true);
>  }
>  
>  static int rpmh_regulator_disable(struct regulator_dev *rdev)
>  {
> -	return rpmh_regulator_set_enable_state(rdev, false);
> +	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
> +
> +	guard(mutex)(&vreg->lock);
> +
> +	/*
> +	 * vreg->rdev is also read by delayed_off_work without this lock
> +	 * held, so READ_ONCE()/WRITE_ONCE() are used instead of locking
> +	 * there too.
> +	 */
> +	WRITE_ONCE(vreg->rdev, rdev);
> +
> +	if (vreg->debounce_delay) {
> +		/*
> +		 * Flip the logical state now so is_enabled() does not keep
> +		 * reporting "enabled" while the real RPMh disable is still
> +		 * queued. Without this, a quick re-enable would see the stale
> +		 * state and skip calling back into enable(), letting this
> +		 * queued disable run regardless.
> +		 */
> +		WRITE_ONCE(vreg->enabled, false);
> +		queue_delayed_work(system_percpu_wq, &vreg->delayed_off_work,
> +				   usecs_to_jiffies(vreg->debounce_delay));
> +		return 0;
> +	}
> +
> +	return _rpmh_regulator_set_enable_state(vreg, rdev, false);
>  }

[Severity: Medium]
Can returning 0 immediately from rpmh_regulator_disable() while the child
regulator is still enabled in RPMh cause a parent supply rail to be disabled
prematurely?

In rpmh_regulator_init_vreg(), vreg->rdesc.supply_name is populated from
rpmh_data->supply_name (such as a parent SMPS or BOB rail supplying an LDO).
When the last consumer disables the child regulator, _regulator_disable() in
drivers/regulator/core.c runs:

_regulator_disable()
  _regulator_do_disable(rdev)
    rpmh_regulator_disable(rdev) -> queues delayed_off_work and returns 0
  _regulator_handle_consumer_disable(rdev)
  if (ret == 0 && rdev->use_count == 0 && rdev->supply)
    _regulator_disable(rdev->supply);

Because rpmh_regulator_disable() returns 0 before vreg->delayed_off_work runs,
the regulator core immediately drops consumer load and disables rdev->supply
while the child regulator remains enabled in hardware for vreg->debounce_delay
microseconds. If a consumer quickly re-enables the child regulator within
vreg->debounce_delay, rdev->supply has already been power-cycled.

> @@ -481,6 +579,36 @@ static const struct regulator_ops rpmh_regulator_xob_ops = {
>  	.is_enabled		= rpmh_regulator_is_enabled,
>  };
>  
> +static void rpmh_regulator_cancel_delayed_off_work(void *data)
> +{
> +	struct rpmh_vreg *vreg = data;
> +	bool pending;
> +
> +	/*
> +	 * Mark the feature torn down so a disable() racing with this
> +	 * teardown cannot queue delayed_off_work after the queue has
> +	 * already been cleared.
> +	 */
> +	scoped_guard(mutex, &vreg->lock)
> +		vreg->debounce_delay = 0;
> +
> +	/*
> +	 * Lock is not held across this cancel_delayed_work_sync(): a running
> +	 * instance may be waiting on an RPMh ACK, and the lock would needlessly
> +	 * block disable() for that duration.
> +	 */
> +	pending = cancel_delayed_work_sync(&vreg->delayed_off_work);
> +
> +	/*
> +	 * If a disable() was queued, run it to ensure regulator hardware
> +	 * state is synced correctly with software state.
> +	 */
> +	scoped_guard(mutex, &vreg->lock) {
> +		if (pending)
> +			_rpmh_regulator_set_enable_state(vreg, vreg->rdev, false);
> +	}
> +}

[Severity: High]
Can rpmh_regulator_cancel_delayed_off_work() race with rpmh_regulator_enable()
and disable a regulator that a consumer just enabled?

In rpmh_regulator_init_vreg(), devm_add_action_or_reset() registers
rpmh_regulator_cancel_delayed_off_work() after devm_regulator_register():

	rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
	if (IS_ERR(rdev)) {
		...
	}

	if (vreg->debounce_delay) {
		ret = devm_add_action_or_reset(dev, rpmh_regulator_cancel_delayed_off_work,
					       vreg);

Because devm actions run in reverse registration order during teardown (or if
a later regulator node fails in rpmh_regulator_probe()),
rpmh_regulator_cancel_delayed_off_work() runs before regulator_unregister()
while rdev is still registered and accessible to consumers.

If a prior rpmh_regulator_disable() queued vreg->delayed_off_work, the
following interleaving can occur:

CPU0 (teardown)                         CPU1 (regulator_enable)
rpmh_regulator_cancel_delayed_off_work()
  vreg->debounce_delay = 0;
  pending = cancel_delayed_work_sync()
  (returns true)
                                        rpmh_regulator_enable()
                                          cancel_delayed_work_sync()
                                          (returns false, already cancelled)
                                          _rpmh_regulator_set_enable_state(..., true)
                                            vreg->enabled = true;
  scoped_guard(mutex, &vreg->lock)
    if (pending)
      _rpmh_regulator_set_enable_state(..., false)
        vreg->enabled = false;

Because rpmh_regulator_enable() does not acquire vreg->lock and
rpmh_regulator_cancel_delayed_off_work() does not hold rdev->mutex, CPU0's
delayed disable overwrites CPU1's enable in both RPMh and vreg->enabled.

Also, when _rpmh_regulator_set_enable_state(vreg, ..., false) runs from
rpmh_regulator_cancel_delayed_off_work() (or from rpmh_regulator_disable()
after vreg->debounce_delay is cleared to 0), vreg->last_off is not updated
even though init_data->constraints.off_on_delay was cleared to 0 in
rpmh_regulator_init_vreg(), so a subsequent rpmh_regulator_enable() would not
enforce vreg->off_on_delay.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261027-regulator-off-on-delay-v5-0-f9c3b128c6d0@oss.qualcomm.com?part=4

  reply	other threads:[~2026-10-27 12:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 12:34 [PATCH v5 0/4] regulator: qcom-rpmh: Add support for off-on-delay and debounce-delay Jishnu Prakash
2026-10-27 12:34 ` [PATCH v5 1/4] regulator: dt-bindings: Add 'regulator-off-on-delay-us' property Jishnu Prakash
2026-10-27 12:34 ` [PATCH v5 2/4] regulator: core: Parse regulator-off-on-delay-us generically Jishnu Prakash
2026-10-27 12:34 ` [PATCH v5 3/4] regulator: dt-bindings: qcom,rpmh-regulator: Add debounce delay property Jishnu Prakash
2026-10-27 12:34 ` [PATCH v5 4/4] regulator: qcom-rpmh: Add debounce delay before disabling regulator Jishnu Prakash
2026-10-27 12:58   ` sashiko-bot [this message]
2026-11-13 22:36   ` Mark Brown
2026-11-24  8:07     ` Jishnu Prakash

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=sashiko-outbox-186593@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jishnu.prakash@oss.qualcomm.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox