Re: [PATCH v2 2/2] rust: miscdevice: add base miscdevice abstraction

From: Alice Ryhl
Date: Tue Oct 01 2024 - 07:29:18 EST


On Tue, Oct 1, 2024 at 10:22 AM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> +impl<T: MiscDevice> MiscDeviceRegistration<T> {
> + /// Register a misc device.
> + pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> {
> + try_pin_init!(Self {
> + inner <- Opaque::try_ffi_init(move |slot: *mut bindings::miscdevice| {
> + // SAFETY: The initializer can write to the provided `slot`.
> + unsafe { slot.write(opts.into_raw::<T>()) };
> +
> + // SAFETY: We just wrote the misc device options to the slot. The miscdevice will
> + // get unregistered before `slot` is deallocated because the memory is pinned and
> + // the destructor of this type deallocates the memory.
> + // INVARIANT: If this returns `Ok(())`, then the `slot` will contain a registered
> + // misc device.
> + to_result(unsafe { bindings::misc_register(slot) })
> + }),
> + _t: PhantomData,
> + })
> + }

Note that right now this can only be used in the module init function
if the registration is stored in a pinned box. We need the in-place
initialization change to fix that.

Does anyone want to revive the in-place initialization patch?

Link: https://lore.kernel.org/rust-for-linux/20240328195457.225001-1-wedsonaf@xxxxxxxxx/

Alice