From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Drew Fustini" <fustini@kernel.org>,
"Guo Ren" <guoren@kernel.org>, "Fu Wei" <wefu@redhat.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
"Michael Turquette" <mturquette@baylibre.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Miguel Ojeda" <ojeda@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>,
"Michal Wilczynski" <m.wilczynski@samsung.com>,
"Boqun Feng" <boqun@kernel.org>,
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 <mhi@mailbox.org>
Subject: Re: [PATCH v5 2/4] rust: clk: implement Clone for Clk<T>
Date: Sun, 02 Aug 2026 19:32:41 +0100 [thread overview]
Message-ID: <DKEO8W6FXG2Z.1YKNFY9CYZZN4@garyguo.net> (raw)
In-Reply-To: <DKE1Z4WT4K89.3QZI5FUEB37U0@nvidia.com>
On Sun Aug 2, 2026 at 2:05 AM BST, Alexandre Courbot wrote:
> On Mon Jul 6, 2026 at 11:37 PM JST, Daniel Almeida wrote:
>> The type-state pattern makes state transitions consume the Clk value,
>> which means a single Clk cannot be shared between users that need to
>> hold the clock in different states,
>
> Sharing a clock can be done by acquiring it several times though - this
> actually seems cleaner to me when concurrent setting of a given clock is
> needed within the same driver.
>
>> nor can a driver keep a long-lived
>> Clk<Prepared> around while temporarily enabling it for scoped sections.
>
> Something like an `EnabledGuard` for a `Clk<Prepared>` should be a good
> fit for this? If you make it generic over `Deref<Target =
> Clk<Prepared>>` you could even make it work with `&Clk<Prepared>` (for
> short-lived enables) and `Arc<Clk<Prepared>>` (for enable periods longer
> than a code block).
This seems to be exactly matching my issue with the current API design:
https://lore.kernel.org/rust-for-linux/DKEO7DXQ8604.1CB09QFM2EO68@garyguo.net/
i.e. the type state is used for two things.
Best,
Gary
>
>>
>> Implement Clone for Clk<T>: each clone is an independent view of the
>> same underlying clock, in the same state, owning its own
>> clk_prepare()/clk_enable() counts, e.g.:
>>
>> let enabled_clk = prepared_clk.clone().enable()?;
>>
>> // Do stuff that requires the clock to be enabled.
>>
>> // enabled_clk goes out of scope and releases the counts it
>> // owns; the clock remains prepared through prepared_clk.
>
> Looks indeed like something an `EnabledGuard` would cover.
>
>>
>> Since struct clk is not refcounted on the C side, share it between
>> clones by wrapping the pointer in an Arc'd RawClk, whose drop
>> implementation calls clk_put() exactly once, when the last clone goes
>> out of scope. This costs one small allocation per clk_get(), which was
>> deemed negligible when compared against the pre-existing indirections
>> in the clk framework.
>>
>> Cloning a prepared (or enabled) Clk calls clk_prepare() (and
>> clk_enable()) on the underlying clock. These calls cannot fail here:
>> the value being cloned already holds a count of each, so the C side
>> only increments the respective counts. This is what makes an infallible
>> Clone implementation possible.
>
> Another point to consider is that `clk_prepare` (and thus `clone()`, as
> you mentioned in the doccomment of the patch) can sleep. An
> `EnabledGuard`, otoh, would only need to call `clk_enable` and thus
> wouldn't sleep, making it usable in atomic context.
>
>>
>> The DISABLE_ON_DROP and UNPREPARE_ON_DROP constants are renamed to
>> ENABLED and PREPARED respectively, since they now describe the state
>
> Let's give them their final name in patch 1 then, since it's also
> accurate (and arguably better) even before this patch.
>
> I am not quite familiar with the client code that will make use of this;
> a pointer would be welcome. But I strongly suspect that multiple calls
> to `get` and an `EnabledGuard` can cover most (if not all) use-cases.
> Introducing an `Arc` here forces a storage decision upon users while
> hiding it.
>
> It sets us up for some dilemmas in the future: for instance, Maurice's
> series exposing `clk_rate_exclusive_get` turns a `Clk` into an
> `ExclusiveClk`, but its potential clones are still around as regular
> `Clk`s. Moreover, `ExclusiveClk` derefs to `Clk<Enabled>`, which can be
> cloned as per this patch; so is it really exclusive?
>
> So I'd like to keep discussing this design a bit more, with more
> visibility over user code. Maybe it should be moved to the tail of the
> series, or even extracted into its own series so as to not block the
> other patches.
next prev parent reply other threads:[~2026-08-02 18:32 UTC|newest]
Thread overview: 23+ 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 ` [PATCH v5 1/4] rust: clk: use the type-state pattern Daniel Almeida
2026-08-01 14:33 ` Alexandre Courbot
2026-08-02 18:30 ` Gary Guo
2026-08-03 4:00 ` Alexandre Courbot
2026-08-03 12:29 ` Gary Guo
2026-08-06 13:54 ` Alexandre Courbot
2026-07-06 14:37 ` [PATCH v5 2/4] rust: clk: implement Clone for Clk<T> Daniel Almeida
2026-08-02 1:05 ` Alexandre Courbot
2026-08-02 18:32 ` Gary Guo [this message]
2026-07-06 14:37 ` [PATCH v5 3/4] rust: clk: add devres-managed clks Daniel Almeida
2026-08-01 11:22 ` Onur Özkan
2026-08-02 5:24 ` Alexandre Courbot
2026-08-02 18:34 ` Gary Guo
2026-08-03 3:57 ` Alexandre Courbot
2026-08-03 12:32 ` Gary Guo
2026-07-06 14:37 ` [PATCH v5 4/4] rust: clk: use 'kernel vertical style' for imports Daniel Almeida
2026-08-01 17:44 ` Onur Özkan
2026-08-02 5:36 ` Alexandre Courbot
2026-07-31 18:37 ` [PATCH v5 0/4] Clk improvements Maurice Hieronymus
2026-07-31 21:32 ` Daniel Almeida
2026-08-03 13:06 ` Brian Masney
2026-08-03 13:24 ` 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=DKEO8W6FXG2Z.1YKNFY9CYZZN4@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=fustini@kernel.org \
--cc=guoren@kernel.org \
--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=maarten.lankhorst@linux.intel.com \
--cc=mhi@mailbox.org \
--cc=mripard@kernel.org \
--cc=mturquette@baylibre.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=tzimmermann@suse.de \
--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;
as well as URLs for NNTP newsgroup(s).