Re: [PATCH 5/5] rust: device: Add a stub abstraction for devices

From: Gary Guo
Date: Mon Mar 13 2023 - 13:52:44 EST


On Fri, 24 Feb 2023 19:53:17 +0900
Asahi Lina <lina@xxxxxxxxxxxxx> wrote:

> From: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
>
> Add a Device type which represents an owned reference to a generic
> struct device. This minimal implementation just handles reference
> counting and allows the user to get the device name.
>
> Lina: Rewrote commit message, dropped the Amba bits, and squashed in
> simple changes to the core Device code from latter commits in
> rust-for-linux/rust. Also include the rust_helper_dev_get_drvdata
> helper which will be needed by consumers later on anyway.
>
> Co-developed-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
> Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
> Signed-off-by: Wedson Almeida Filho <wedsonaf@xxxxxxxxx>
> Signed-off-by: Asahi Lina <lina@xxxxxxxxxxxxx>
> ---
> rust/helpers.c | 13 +++++++++
> rust/kernel/device.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 88 insertions(+), 1 deletion(-)
>
> diff --git a/rust/helpers.c b/rust/helpers.c
> index 04b9be46e887..54954fd80c77 100644
> --- a/rust/helpers.c
> +++ b/rust/helpers.c
> @@ -20,6 +20,7 @@
>
> #include <linux/bug.h>
> #include <linux/build_bug.h>
> +#include <linux/device.h>
> #include <linux/err.h>
> #include <linux/refcount.h>
>
> @@ -65,6 +66,18 @@ long rust_helper_PTR_ERR(__force const void *ptr)
> }
> EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
>
> +void *rust_helper_dev_get_drvdata(struct device *dev)
> +{
> + return dev_get_drvdata(dev);
> +}
> +EXPORT_SYMBOL_GPL(rust_helper_dev_get_drvdata);
> +
> +const char *rust_helper_dev_name(const struct device *dev)
> +{
> + return dev_name(dev);
> +}
> +EXPORT_SYMBOL_GPL(rust_helper_dev_name);
> +
> /*
> * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
> * as the Rust `usize` type, so we can use it in contexts where Rust
> 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>);

?

Best,
Gary