Re: [RFC PATCH V2 2/8] rust: Extend OPP bindings for the OPP table

From: Viresh Kumar
Date: Mon Jun 10 2024 - 02:17:23 EST


On 07-06-24, 13:38, Manos Pitsidianakis wrote:
> On Fri, 07 Jun 2024 12:12, Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
> > +/// OPP search types.
> > +#[derive(Copy, Clone, Debug, Eq, PartialEq)]
> > +pub enum SearchType {
> > + /// Search for exact value.
> > + Exact,
> > + /// Search for highest value less than equal to value.
> > + Floor,
> > + /// Search for lowest value greater than equal to value.
> > + Ceil,
> > +}
>
> Seeing this enum made me think about memory layouts which are not stable in
> Rust and can change between compilations unless they have a specific `repr`.

Just to clarify, this enum is a Rust only entity. It doesn't have a C
counterpart..

> Not related to this series directly, has there been discussion about
> guaranteeing struct layouts in kernel APIs? It'd require a lot of things to
> happen to cause a problem (multiple users of an API in the kernel in
> separate compilation units maybe even compiled with different rustc
> versions).

I haven't followed the Rust discussions closely, hopefully someone
else can answer on this. But isn't repr(C) good enough to take care of
layout issues ? I must not be understanding it since you asked this :)

> > + /// Find OPP table from device.
> > + pub fn from_dev(dev: ARef<Device>) -> Result<Self> {
> > + // SAFETY: The requirements are satisfied by the existence of `Device` and its safety
> > + // requirements. Refcount of the OPP table is incremented as well.
> > + let ptr = from_err_ptr(unsafe { bindings::dev_pm_opp_get_opp_table(dev.as_raw()) })?;
> > +
> > + Ok(Self {
> > + ptr,
> > + dev: dev.clone(),
>
> Clone is not probably not needed here, right? the argument value will be
> dropped after this.

Hmm, I was expecting the build system to raise an error for such
things. Tried both rustfmtcheck and CLIPPY=1 and this isn't reported.
Anyway, fixed it now (along with few other that CLIPPY=1 reported).

> > + /// Finds OPP based on level.
> > + pub fn opp_from_level(&self, mut level: u32, stype: SearchType) -> Result<ARef<OPP>> {
> > + let rdev = self.dev.as_raw();
> > +
> > + let ptr = from_err_ptr(match stype {
> > + // SAFETY: The requirements are satisfied by the existence of `Device` and its
> > + // safety requirements. The returned ptr will be owned by the new [`OPP`] instance.
> > + SearchType::Exact => unsafe { bindings::dev_pm_opp_find_level_exact(rdev, level) },
> > +
>
> Minor style comment, the empty lines between match patterns are unusual

Yeah, since there were a lot of comments and code, I added them to
make it more readable. rustfmt doesn't raise any issues with it, so I
guess I can keep them :)

Thanks Manos.

--
viresh