From: Daniel Almeida <daniel.almeida@collabora.com>
To: Jerome Brunet <jbrunet@baylibre.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Michal Wilczynski" <m.wilczynski@samsung.com>,
"Drew Fustini" <fustini@kernel.org>,
"Guo Ren" <guoren@kernel.org>, "Fu Wei" <wefu@redhat.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
"Stephen Boyd" <sboyd@kernel.org>,
"Brian Masney" <bmasney+clk@redhat.com>,
"Jerome Brunet" <jbrunet+clk@baylibre.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org,
linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org,
rust-for-linux@vger.kernel.org,
"Boris Brezillon" <boris.brezillon@collabora.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Maurice Hieronymus" <mhi@mailbox.org>
Subject: Re: [PATCH v6 3/7] rust: clk: use the type-state pattern
Date: Mon, 23 Nov 2026 13:33:47 -0300 [thread overview]
Message-ID: <4FF31142-6624-4A90-9BE2-F654D896EF22@collabora.com> (raw)
In-Reply-To: <179519333138.3795820.61438010297111723.b4-review@b4>
Hi Jerome,
> On 20 Nov 2026, at 13:48, Jerome Brunet <jbrunet@baylibre.com> wrote:
>
>> clk.prepare();
>> clk.enable();
>> clk.enable();
>
> This particular sequence is indeed allowed in C. I don't get why it should not
> be ? (in general and/or in rust)
>
> If you dis-allow this in rust, the 2 sides drift apart, don't they ?
> I mean they start behaving differently. I suppose this is not something
> desirable ?
The problem with the current API is that it does not enforce a proper
order for the calls, i.e.: prepare() -> enable() -> disable() ->
unprepare().
For example, getting a clk and repeatedly disabling it is allowed:
while true {
clk.disable();
}
There is also a problem of balancing the refcounts. As you can see from
the example above, it is trivial to have a decrement without a
corresponding increment, or the other way around.
What this series aims to do is use Rust's type system to address the two
points above by enforcing a legal transition between states using a
small FSM via this "typestate" technique. As a bonus, this also solves
the balancing problem, because each state owns the appropriate count.
With this series, the broken example above doesn’t even compile anymore.
Note that taking more than one count is still possible. Patch 4 adds
guards for that: each enable_guard() does a clk_enable(), and the
matching clk_disable() happens when that guard goes away. So the
sequence you quoted can still be written. What you can no longer do is
call disable() without having called enable() first, or forget to
release the prepare/enabled counts you acquired.
Notice that this doesn't, in and of itself, preclude the usage of the
old API. Someone can add a new state that allows the old behavior, as a
Clk<Unknown> or some other name, but if so, they should provide a user,
and we should _really_ ensure that their usecase can't be adequately
described by the safer API above.
So no, I do not think it is a deviation from C, more like a safer
veneer, because Rust makes it much easier for us to provide one.
>
>>
>> And nothing gets undone on drop().
>>
>> c) It does not let a user express the state of the clk through the
>> type system. For example, there is currently no way to encode that a Clk is
>> enabled via the type system alone.
>>
>> Following the Regulator abstraction, switch this abstraction to use the
>> type-state pattern instead. It solves both a) and b) by establishing a
>> number of states and the valid ways to transition between them. It also
>> automatically undoes any call to clk_get(), clk_prepare() and clk_enable()
>> as applicable on drop(), so users do not have to do anything special
>> before Clk goes out of scope.
>
> So the point the is automatically clean-up (as in disable()) clock when they
> go out of scope ? In C, the equivalent would be calling clk_put() with a non
> NULL enable/prepare count. We do complain loudly when this happen. I don't
> people should encouraged to not actively care for this.
>
> --
> Jerome
Actually, the point would be to forbid this using the type system. By
using the new types, the abstraction knows which counts it must
release before calling clk_put.
IOW, a Clk<Enabled> knows, on drop, that it must release one count of
enable, and one count of prepare before clk_put is called. Clk<Prepared>
knows it must release one count of prepare before clk_put is called, and
Clk<Unprepared> can simply call clk_put directly. The type states make
the situation you described unrepresentable in Rust.
This is not new on the C side either: devm_clk_get_enabled() already
runs clk_disable_unprepare() and clk_put() on its own when the device
is unbound. Here the same happens when the value goes away.
— Daniel
next prev parent reply other threads:[~2026-11-23 16:35 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 17:07 [PATCH v6 0/7] Clk improvements Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 1/7] rust: clk: use 'kernel vertical style' for imports Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-10-13 17:07 ` [PATCH v6 2/7] rust: clk: replace `OptionalClk` with `Clk::get_optional()` Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-10-13 17:07 ` [PATCH v6 3/7] rust: clk: use the type-state pattern Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-11-20 16:48 ` Jerome Brunet
2026-11-23 16:33 ` Daniel Almeida [this message]
2026-10-13 17:07 ` [PATCH v6 4/7] rust: clk: add guards for scoped prepare and enable Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-10-13 17:07 ` [PATCH v6 5/7] rust: clk: add devres-managed clks Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-11-18 14:24 ` Gary Guo
2026-11-18 14:47 ` Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 6/7] rust: clk: add `ExclusiveClk` Daniel Almeida
2026-10-13 17:14 ` Daniel Almeida
2026-11-18 13:47 ` Alice Ryhl
2026-10-13 17:07 ` [PATCH v6 7/7] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Daniel Almeida
2026-11-11 16:54 ` [PATCH v6 0/7] Clk improvements Daniel Almeida
2026-11-20 16:48 ` Jerome Brunet
2026-11-20 21:31 ` Miguel Ojeda
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=4FF31142-6624-4A90-9BE2-F654D896EF22@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=bmasney+clk@redhat.com \
--cc=boqun@kernel.org \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=fustini@kernel.org \
--cc=gary@garyguo.net \
--cc=guoren@kernel.org \
--cc=jbrunet+clk@baylibre.com \
--cc=jbrunet@baylibre.com \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lossin@kernel.org \
--cc=m.wilczynski@samsung.com \
--cc=mhi@mailbox.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ukleinek@kernel.org \
--cc=viresh.kumar@linaro.org \
--cc=wefu@redhat.com \
--cc=work@onurozkan.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
all inboxes | Powered by JetHome®