Re: [PATCH 2/2] rust/faux: Add missing parent argument to Registration::new()

Alice Ryhl
2025-02-26T08:39:07Z

On Tue, Feb 25, 2025 at 10:31 PM Lyude Paul <lyude@redhat.com> wrote:
>
> A little late in the review of the faux device interface, we added the
> ability to specify a parent device when creating new faux devices - but
> this never got ported over to the rust bindings. So, let's add the missing
> argument now so we don't have to convert other users later down the line.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
> rust/kernel/faux.rs | 10 ++++++++--
> samples/rust/rust_driver_faux.rs | 2 +-
> 2 files changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/rust/kernel/faux.rs b/rust/kernel/faux.rs
> index 41751403cd868..ae99ea3d114ef 100644
> --- a/rust/kernel/faux.rs
> +++ b/rust/kernel/faux.rs
> @@ -23,11 +23,17 @@
>
> impl Registration {
> /// Create and register a new faux device with the given name.
> - pub fn new(name: &CStr) -> Result<Self> {
> + pub fn new(name: &CStr, parent: Option<&device::Device>) -> Result<Self> {
> // SAFETY:
> // - `name` is copied by this function into its own storage
> // - `faux_ops` is safe to leave NULL according to the C API
> - let dev = unsafe { bindings::faux_device_create(name.as_char_ptr(), null_mut(), null()) };
> + let dev = unsafe {
> + bindings::faux_device_create(
> + name.as_char_ptr(),
> + parent.map_or(null_mut(), |p| p.as_raw()),
> + null(),

This function signature only requires that `parent` is valid for the
duration of this call to `new`, but `faux_device_create` stashes a
pointer without touching the refcount. How do you ensure that the
`parent` pointer does not become dangling?

Alice