public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Michael Turquette" <mturquette@baylibre.com>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Russell King" <linux@armlinux.org.uk>,
	linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org,
	rust-for-linux@vger.kernel.org,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>
Subject: Re: [PATCH V3 2/2] rust: Add initial clk abstractions
Date: Thu, 6 Mar 2025 13:33:42 +0100	[thread overview]
Message-ID: <Z8mWJox-0IyP1uUo@cassiopeiae> (raw)
In-Reply-To: <20250304085351.inrvjgixvxla4yn3@vireshk-i7>

On Tue, Mar 04, 2025 at 02:23:51PM +0530, Viresh Kumar wrote:
> +/// This structure represents the Rust abstraction for a C [`struct clk`].
> +///
> +/// # Invariants
> +///
> +/// A [`Clk`] instance always corresponds to a valid [`struct clk`] created by the C portion of the
> +/// kernel.
> +///
> +/// Instances of this type are reference-counted. Calling `get` ensures that the allocation remains
> +/// valid for the lifetime of the [`Clk`].
> +///
> +/// ## Example
> +///
> +/// The following example demonstrates how to obtain and configure a clock for a device.
> +///
> +/// ```
> +/// use kernel::clk::{Clk, Hertz};
> +/// use kernel::device::Device;
> +/// use kernel::error::Result;
> +///
> +/// fn configure_clk(dev: &Device) -> Result {
> +///     let clk = Clk::get(dev, "apb_clk")?;
> +///
> +///     clk.prepare_enable()?;
> +///
> +///     let expected_rate = Hertz::new(1_000_000_000);
> +///
> +///     if clk.rate() != expected_rate {
> +///         clk.set_rate(expected_rate)?;
> +///     }
> +///
> +///     clk.disable_unprepare();
> +///     Ok(())
> +/// }
> +/// ```
> +///
> +/// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html
> +#[repr(transparent)]
> +pub struct Clk(*mut bindings::clk);
> +
> +impl Clk {
> +    /// Gets `Clk` corresponding to a [`Device`] and a connection id.
> +    pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
> +        let con_id = if let Some(name) = name {
> +            name.as_ptr() as *const _
> +        } else {
> +            ptr::null()
> +        };
> +
> +        // SAFETY: It is safe to call `clk_get()` for a valid device pointer.
> +        Ok(Self(from_err_ptr(unsafe {
> +            bindings::clk_get(dev.as_raw(), con_id)
> +        })?))
> +    }
> +
> +    /// Obtain the raw `struct clk *`.
> +    #[inline]
> +    pub fn as_raw(&self) -> *mut bindings::clk {
> +        self.0
> +    }
> +
> +    /// Enable the clock.
> +    #[inline]
> +    pub fn enable(&self) -> Result {
> +        // SAFETY: It is safe to call clk APIs of the C code for a clock pointer earlier returned
> +        // by `clk_get()`.

You may want to add an invariant for this, i.e. something along the lines of
"Clk always holds either a pointer to a valid struct clk or a NULL pointer".

In this safety comment you can then say that by the type invariant of Clk
self.as_raw() is a valid argument for $func.

Not that your type invariant needs the NULL case, since OptionalClk may set Clk
to hold a NULL pointer.

I still think that a new type MaybeNull<T> would be nice to encapsulate this
invariant, but we can also wait until we get another use-case for it.

  parent reply	other threads:[~2025-03-06 12:33 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-03  9:58 [PATCH V3 0/2] rust: Add basic clock abstractions Viresh Kumar
2025-03-03  9:58 ` [PATCH V3 1/2] rust: Add clk helpers Viresh Kumar
2025-03-03 10:05   ` Alice Ryhl
2025-03-03 11:15     ` Viresh Kumar
2025-03-03  9:58 ` [PATCH V3 2/2] rust: Add initial clk abstractions Viresh Kumar
2025-03-03 10:04   ` Alice Ryhl
2025-03-03 11:32     ` Viresh Kumar
2025-03-03 10:16   ` Miguel Ojeda
2025-03-03 11:44     ` Viresh Kumar
2025-03-03 15:50       ` Miguel Ojeda
2025-03-04  8:53     ` Viresh Kumar
2025-03-04  9:37       ` Miguel Ojeda
2025-03-05 11:46         ` Viresh Kumar
2025-03-05 22:31           ` Stephen Boyd
2025-03-06  4:40             ` Viresh Kumar
2025-03-06 20:58               ` Stephen Boyd
2025-03-07  7:23                 ` Viresh Kumar
2025-03-06 12:33       ` Danilo Krummrich [this message]
2025-03-08  2:03       ` 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=Z8mWJox-0IyP1uUo@cassiopeiae \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=mturquette@baylibre.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    /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