On Thu, Nov 26, 2026 at 01:04:32PM -0300, Daniel Almeida wrote:
> +    /// Deasserts the reset line and moves the [`SharedResetControl`] to the 
> [`Deasserted`] state.
> +    ///
> +    /// Equivalent to the kernel's [`reset_control_deassert()`].
> +    ///
> +    /// [`reset_control_deassert()`]:
> +    /// 
> https://docs.kernel.org/driver-api/reset.html#c.reset_control_deassert
> +    #[inline]
> +    pub fn deassert(self) -> Result<SharedResetControl<'a, Deasserted>, 
> Error<Self>> {
> +        // INVARIANT: The reference that `self` owns moves to the value that 
> this returns.
> +        let reset = ManuallyDrop::new(self);
> +
> +        // SAFETY: By the type invariants, `reset.inner` is a pointer that a 
> C getter returned, and
> +        // `reset` owns its reference.
> +        to_result(unsafe { bindings::reset_control_deassert(reset.inner) })
> +            .map(|()| SharedResetControl {
> +                inner: reset.inner,
> +                _phantom: PhantomData,
> +            })
> +            .map_err(|error| Error {
> +                error,
> +                reset: ManuallyDrop::into_inner(reset),
> +            })
> +    }

The C side here interacts badly with the error path of
`SharedResetControl::deassert()`.

In `drivers/reset/core.c`, `reset_control_deassert()` does:

    if (rstc->shared) {
        if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
            return -EINVAL;

        if (atomic_inc_return(&rstc->deassert_count) != 1)
            return 0;
    }
    ...
    return rcdev->ops->deassert(rcdev, rstc->id);

Unlike `reset_control_reset()`, `reset_control_deassert()` does not
decrement `rstc->deassert_count` when `rcdev->ops->deassert()` returns
an error.

Because `SharedResetControl::deassert()` returns `reset` in the
`Unknown` state (`ASSERT_ON_DROP == false`) on error:

- If the caller drops `reset`, `deassert_count` stays leaked at 1, so
  future shared users will see `deassert_count != 0` and skip
  `ops->deassert()`.

- If the caller retries `reset.deassert()`, `atomic_inc_return()` returns
  2 (`!= 1`), so `reset_control_deassert()` returns 0 without calling
  `ops->deassert()`, leaving the line asserted while Rust treats the
  control as `Deasserted`.

I wonder if this is a bug on the C side we should fix in core.c?

Alice

Reply via email to