Re: [PATCH 1/2] rust/kernel: Add platform::Device::from_raw()

Alice Ryhl
2025-01-28T14:35:36Z

On Thu, Jan 23, 2025 at 12:54 AM Lyude Paul <lyude@redhat.com> wrote:
>
> Just a convenience method to convert from a raw struct platform_device
> pointer into a platform::Device object, which we'll be using in the next
> commit.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/platform.rs | 17 ++++++++++++++++-
> 1 file changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
> index 50e6b04218132..75dc7824eccf4 100644
> --- a/rust/kernel/platform.rs
> +++ b/rust/kernel/platform.rs
> @@ -14,7 +14,7 @@
> ThisModule,
> };
>
> -use core::ptr::addr_of_mut;
> +use core::ptr::{NonNull, addr_of_mut};
>
> /// An adapter for the registration of platform drivers.
> pub struct Adapter<T: Driver>(T);
> @@ -186,6 +186,21 @@ unsafe fn from_dev(dev: ARef<device::Device>) -> Self {
> Self(dev)
> }
>
> + /// Convert a raw pointer to a `struct platform_device` into a `Device`.
> + ///
> + /// # Safety
> + ///
> + /// * `pdev` must be a valid pointer to a `bindings::platform_device`.
> + /// * The caller must be guaranteed to hold at least one reference to `pdev`.

I would probably say that the caller must *transfer ownership* of one
refcount or something like that.

Alice