rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Gary Guo <gary@garyguo.net>
Cc: "Asahi Lina" <lina@asahilina.net>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Will Deacon" <will@kernel.org>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Joerg Roedel" <joro@8bytes.org>,
	"Hector Martin" <marcan@marcan.st>,
	"Sven Peter" <sven@svenpeter.dev>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Alyssa Rosenzweig" <alyssa@rosenzweig.io>,
	"Neal Gompa" <neal@gompa.dev>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	asahi@lists.linux.dev
Subject: Re: [PATCH 5/5] rust: device: Add a stub abstraction for devices
Date: Mon, 13 Mar 2023 11:05:04 -0700	[thread overview]
Message-ID: <ZA9l0EHCRRr/myoq@boqun-archlinux> (raw)
In-Reply-To: <20230313175202.300bb1f4.gary@garyguo.net>

On Mon, Mar 13, 2023 at 05:52:02PM +0000, Gary Guo wrote:
[...]
> > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> > index 9be021e393ca..e57da622d817 100644
> > --- a/rust/kernel/device.rs
> > +++ b/rust/kernel/device.rs
> > @@ -4,7 +4,7 @@
> >  //!
> >  //! C header: [`include/linux/device.h`](../../../../include/linux/device.h)
> >  
> > -use crate::bindings;
> > +use crate::{bindings, str::CStr};
> >  
> >  /// A raw device.
> >  ///
> > @@ -20,4 +20,78 @@ use crate::bindings;
> >  pub unsafe trait RawDevice {
> >      /// Returns the raw `struct device` related to `self`.
> >      fn raw_device(&self) -> *mut bindings::device;
> > +
> > +    /// Returns the name of the device.
> > +    fn name(&self) -> &CStr {
> > +        let ptr = self.raw_device();
> > +
> > +        // SAFETY: `ptr` is valid because `self` keeps it alive.
> > +        let name = unsafe { bindings::dev_name(ptr) };
> > +
> > +        // SAFETY: The name of the device remains valid while it is alive (because the device is
> > +        // never renamed, per the safety requirement of this trait). This is guaranteed to be the
> > +        // case because the reference to `self` outlives the one of the returned `CStr` (enforced
> > +        // by the compiler because of their lifetimes).
> > +        unsafe { CStr::from_char_ptr(name) }
> > +    }
> > +}
> > +
> > +/// A ref-counted device.
> > +///
> > +/// # Invariants
> > +///
> > +/// `ptr` is valid, non-null, and has a non-zero reference count. One of the references is owned by
> > +/// `self`, and will be decremented when `self` is dropped.
> > +pub struct Device {
> > +    pub(crate) ptr: *mut bindings::device,
> > +}
> > +
> 
> Shouldn't this be
> 
> #[repr(transparent)]
> pub struct Device(Opaque<bindings::device>);
> 
> ?

I have the same feeling, for `task_struct`, we have

	#[repr(transparent)]
	pub struct Task(pub(crate) UnsafeCell<bindings::task_struct>);

and
	
	pub struct TaskRef<'a> {
	    task: &'a Task,
	    _not_send: PhantomData<*mut ()>,
	}

I wonder whether we should do the similar for `Device`, meaning `Device`
is just a tranparent representation for `struct device` and another
type (say, `DeviceRef`) is introduced as the reference type, or we can
just use `ARef`.

Regards,
Boqun



> 
> Best,
> Gary

      reply	other threads:[~2023-03-13 18:06 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 10:53 [PATCH 0/5] rust: Add io_pgtable and RTKit abstractions Asahi Lina
2023-02-24 10:53 ` [PATCH 1/5] rust: Add a Sealed trait Asahi Lina
2023-02-24 10:53 ` [PATCH 2/5] rust: device: Add a minimal RawDevice trait Asahi Lina
2023-02-24 11:23   ` Greg Kroah-Hartman
2023-02-24 13:15     ` Asahi Lina
2023-02-24 14:11       ` Greg Kroah-Hartman
2023-02-24 14:19         ` Greg Kroah-Hartman
2023-02-24 14:44           ` Asahi Lina
2023-02-24 15:25             ` Greg Kroah-Hartman
2023-02-24 15:45               ` Asahi Lina
2023-02-25 17:07             ` alyssa
2023-02-24 14:32         ` Robin Murphy
2023-02-24 14:48           ` Asahi Lina
2023-02-24 15:14             ` Robin Murphy
2023-02-24 16:23               ` Asahi Lina
2023-02-24 19:22                 ` Miguel Ojeda
2023-03-05  6:52                 ` Wedson Almeida Filho
2023-02-24 15:32           ` Greg Kroah-Hartman
2023-02-24 18:52             ` Robin Murphy
2023-02-25  7:00               ` Greg Kroah-Hartman
2023-02-24 14:43         ` Asahi Lina
2023-02-24 15:24           ` Greg Kroah-Hartman
2023-02-24 15:42             ` Asahi Lina
2023-02-24 10:53 ` [PATCH 3/5] rust: io_pgtable: Add io_pgtable abstraction Asahi Lina
2023-02-24 10:53 ` [PATCH 4/5] rust: soc: apple: rtkit: Add Apple RTKit abstraction Asahi Lina
2023-02-24 10:53 ` [PATCH 5/5] rust: device: Add a stub abstraction for devices Asahi Lina
2023-02-24 11:19   ` Greg Kroah-Hartman
2023-02-24 15:10     ` Asahi Lina
2023-02-24 15:34       ` Greg Kroah-Hartman
2023-02-24 15:51         ` Asahi Lina
2023-02-24 16:31           ` Greg Kroah-Hartman
2023-02-24 16:53             ` Asahi Lina
2023-03-05  6:39     ` Wedson Almeida Filho
2023-03-09 11:24       ` Greg Kroah-Hartman
2023-03-09 16:46         ` Wedson Almeida Filho
2023-03-09 17:11           ` Greg Kroah-Hartman
2023-03-09 19:06             ` Wedson Almeida Filho
2023-03-13 17:01               ` Gary Guo
2023-03-09 19:43             ` Miguel Ojeda
2023-03-13 17:52   ` Gary Guo
2023-03-13 18:05     ` Boqun Feng [this message]

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=ZA9l0EHCRRr/myoq@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=alyssa@rosenzweig.io \
    --cc=arnd@arndb.de \
    --cc=asahi@lists.linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joro@8bytes.org \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcan@marcan.st \
    --cc=neal@gompa.dev \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sven@svenpeter.dev \
    --cc=wedsonaf@gmail.com \
    --cc=will@kernel.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;
as well as URLs for NNTP newsgroup(s).