All of lore.kernel.org
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: gregkh@linuxfoundation.org, rafael@kernel.org,
	david.m.ertman@intel.com, ira.weiny@intel.com, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, benno.lossin@proton.me,
	a.hindborg@kernel.org, tmgross@umich.edu, airlied@gmail.com,
	acourbot@nvidia.com, jhubbard@nvidia.com,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v4 4/5] rust: auxiliary: add auxiliary registration
Date: Tue, 15 Apr 2025 14:50:02 +0200	[thread overview]
Message-ID: <Z_5V-uznqkAvrf9p@cassiopeiae> (raw)
In-Reply-To: <Z_5M5Auqj2KK-rPz@google.com>

On Tue, Apr 15, 2025 at 12:11:16PM +0000, Alice Ryhl wrote:
> On Mon, Apr 14, 2025 at 03:18:07PM +0200, Danilo Krummrich wrote:
> > +impl Registration {
> > +    /// Create and register a new auxiliary device.
> > +    pub fn new(parent: &device::Device, name: &CStr, id: u32, modname: &CStr) -> Result<Self> {
> > +        let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
> > +        let adev = boxed.get();
> > +
> > +        // SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization.
> > +        unsafe {
> > +            (*adev).dev.parent = parent.as_raw();
> > +            (*adev).dev.release = Some(Device::release);
> > +            (*adev).name = name.as_char_ptr();
> > +            (*adev).id = id;
> > +        }
> > +
> > +        // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
> > +        // which has not been initialized yet.
> > +        unsafe { bindings::auxiliary_device_init(adev) };
> > +
> > +        // Now that `adev` is initialized, leak the `Box`; the corresponding memory will be freed
> > +        // by `Device::release` when the last reference to the `struct auxiliary_device` is dropped.
> > +        let _ = KBox::into_raw(boxed);
> > +
> > +        // SAFETY:
> > +        // - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which has
> > +        //   been initialialized,
> > +        // - `modname.as_char_ptr()` is a NULL terminated string.
> > +        let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
> > +        if ret != 0 {
> > +            // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
> > +            // which has been initialialized.
> > +            unsafe { bindings::auxiliary_device_uninit(adev) };
> 
> Does this error-path actually free the box?

Yes, auxiliary_device_uninit() calls put_device() on the underlying struct
device, hence the release() callback is called at this point, which frees the
Box.

  reply	other threads:[~2025-04-15 12:50 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-14 13:18 [PATCH v4 0/5] Auxiliary bus Rust abstractions Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 1/5] rust: types: add `Opaque::zeroed` Danilo Krummrich
2025-04-15 11:59   ` Alice Ryhl
2025-04-15 12:43     ` Danilo Krummrich
2025-04-15 15:17       ` Miguel Ojeda
2025-04-14 13:18 ` [PATCH v4 2/5] rust: device: implement Device::parent() Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 3/5] rust: auxiliary: add auxiliary device / driver abstractions Danilo Krummrich
2025-04-15 12:08   ` Alice Ryhl
2025-04-15 12:45     ` Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 4/5] rust: auxiliary: add auxiliary registration Danilo Krummrich
2025-04-15 12:11   ` Alice Ryhl
2025-04-15 12:50     ` Danilo Krummrich [this message]
2025-04-16 10:38       ` Alice Ryhl
2025-04-14 13:18 ` [PATCH v4 5/5] samples: rust: add Rust auxiliary driver sample Danilo Krummrich
2025-04-15 11:38 ` [PATCH v4 0/5] Auxiliary bus Rust abstractions Greg KH
2025-04-15 12:42   ` Danilo Krummrich
2025-04-15 12:54     ` Greg KH
2025-04-19 13:01 ` Danilo Krummrich

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=Z_5V-uznqkAvrf9p@cassiopeiae \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=david.m.ertman@intel.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=ira.weiny@intel.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.