All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Daniel Almeida" <daniel.almeida@collabora.com>
Cc: dri-devel@lists.freedesktop.org, ojeda@kernel.org
Subject: Re: [PATCH v5 3/4] rust: clk: add devres-managed clks
Date: Mon, 06 Jul 2026 14:51:17 +0000	[thread overview]
Message-ID: <20260706145118.3AE8F1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260706-clk-type-state-v5-3-67c5f326a16c@collabora.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The newly introduced Rust wrapper `devm_enable_optional_with_rate` exposes a broken error path in the underlying C framework (`devm_clk_get_optional_enabled_with_rate`) that incorrectly calls `clk_disable_unprepare` on clocks that haven't been prepared or enabled, causing unbalanced global count decrements and state corruption.
--

commit f2d6842b5aa3017f9a5274d7b455e65224dd1e79
Author: Daniel Almeida <daniel.almeida@collabora.com>

rust: clk: add devres-managed clks

The clk API allows fine-grained control, but some drivers might be
more interested in a "set and forget" API.

Expand the current API to support this. The clock will automatically be
disabled, unprepared and freed when the device is unbound from the bus
without further intervention by the driver.

> diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
> index dd5fd656271e1..b9a44f83843a3 100644
> --- a/rust/kernel/clk.rs
> +++ b/rust/kernel/clk.rs

[ ... ]

> +    /// Same as [`devm_enable_optional`], but also sets the rate.
> +    #[inline]
> +    pub fn devm_enable_optional_with_rate(
> +        dev: &Device<Bound>,
> +        name: Option<&CStr>,
> +        rate: Hertz,
> +    ) -> Result {
> +        let name = name.map_or(ptr::null(), |n| n.as_char_ptr());
> +
> +        // SAFETY: It is safe to call
> +        // [`devm_clk_get_optional_enabled_with_rate`] with a valid device
> +        // pointer.
> +        from_err_ptr(unsafe {
> +            bindings::devm_clk_get_optional_enabled_with_rate(dev.as_raw(), name, rate.as_hz())
> +        })?;

[Severity: High]
This is a pre-existing issue, but does exposing this C function to Rust
drivers introduce them to a broken error path?

Looking at the underlying C implementation:

drivers/clk/clk-devres.c:devm_clk_get_optional_enabled_with_rate() {
	...
	ret = clk_set_rate(clk, rate);
	if (ret)
		goto out_put_clk;

	ret = clk_prepare_enable(clk);
	if (ret)
		goto out_put_clk;

	return clk;

out_put_clk:
	devm_clk_put(dev, clk);
	return ERR_PTR(ret);
}

If clk_set_rate() fails (for example, due to an unsupported rate), the
error path calls devm_clk_put(). Since the devres initialization previously
registered clk_disable_unprepare() as the exit function, won't devm_clk_put()
execute it even though the clock was never actually prepared or enabled by
this consumer?

For shared clocks, couldn't this unbalanced decrement of the enable and
prepare counts forcefully turn off the underlying hardware clock while other
consumers are actively using it?

> +        Ok(())
> +    }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260706-clk-type-state-v5-0-67c5f326a16c@collabora.com?part=3

  reply	other threads:[~2026-07-06 14:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06 14:37 [PATCH v5 0/4] Clk improvements Daniel Almeida
2026-07-06 14:37 ` Daniel Almeida
2026-07-06 14:37 ` [PATCH v5 1/4] rust: clk: use the type-state pattern Daniel Almeida
2026-07-06 14:37   ` Daniel Almeida
2026-08-01 14:33   ` Alexandre Courbot
2026-08-01 14:33     ` Alexandre Courbot
2026-07-06 14:37 ` [PATCH v5 2/4] rust: clk: implement Clone for Clk<T> Daniel Almeida
2026-07-06 14:37   ` Daniel Almeida
2026-07-06 14:37 ` [PATCH v5 3/4] rust: clk: add devres-managed clks Daniel Almeida
2026-07-06 14:37   ` Daniel Almeida
2026-07-06 14:51   ` sashiko-bot [this message]
2026-08-01 11:22   ` Onur Özkan
2026-08-01 11:22     ` Onur Özkan
2026-07-06 14:37 ` [PATCH v5 4/4] rust: clk: use 'kernel vertical style' for imports Daniel Almeida
2026-07-06 14:37   ` Daniel Almeida
2026-07-31 18:37 ` [PATCH v5 0/4] Clk improvements Maurice Hieronymus
2026-07-31 18:37   ` Maurice Hieronymus
2026-07-31 21:32   ` Daniel Almeida
2026-07-31 21:32     ` Daniel Almeida

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=20260706145118.3AE8F1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ojeda@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 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.