Re: [PATCH v6 3/7] rust: clk: use the type-state pattern
From: Daniel Almeida
Date: Mon Nov 23 2026 - 11:35:51 EST
Hi Jerome,
> On 20 Nov 2026, at 13:48, Jerome Brunet <jbrunet@xxxxxxxxxxxx> 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