Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alice Ryhl" <aliceryhl@google.com>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: <gregkh@linuxfoundation.org>, <rafael@kernel.org>,
	<acourbot@nvidia.com>, <david.m.ertman@intel.com>,
	<ira.weiny@intel.com>, <leon@kernel.org>, <ojeda@kernel.org>,
	<boqun@kernel.org>, <gary@garyguo.net>,
	<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
	<a.hindborg@kernel.org>, <tmgross@umich.edu>,
	<driver-core@lists.linux.dev>, <linux-kernel@vger.kernel.org>,
	<nova-gpu@lists.linux.dev>, <dri-devel@lists.freedesktop.org>,
	<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH 1/2] rust: auxiliary: add registration data to auxiliary devices
Date: Thu, 30 Apr 2026 15:31:45 +0100	[thread overview]
Message-ID: <DI6K97GKZKRG.2O9FPPA36J6RU@garyguo.net> (raw)
In-Reply-To: <afMaAfcxpDR3xIE1@google.com>

On Thu Apr 30, 2026 at 9:59 AM BST, Alice Ryhl wrote:
> On Tue, Apr 28, 2026 at 12:09:40AM +0200, Danilo Krummrich wrote:
>> Add a registration_data pointer to struct auxiliary_device, allowing the
>> registering (parent) driver to attach private data to the device at
>> registration time and retrieve it later when called back by the
>> auxiliary (child) driver.
>> 
>> By tying the data to the device's registration, Rust drivers can bind
>> the lifetime of device resources to it, since the auxiliary bus
>> guarantees that the parent driver remains bound while the auxiliary
>> device is bound.
>> 
>> On the Rust side, Registration<T> takes ownership of the data via
>> ForeignOwnable. A TypeId is stored alongside the data for runtime type
>> checking, making Device::registration_data<T>() a safe method.
>> 
>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>
> So overall I think this patch makes sense. A few comments below.
>
>> diff --git a/include/linux/auxiliary_bus.h b/include/linux/auxiliary_bus.h
>> index bc09b55e3682..4e1ad8ccbcdd 100644
>> --- a/include/linux/auxiliary_bus.h
>> +++ b/include/linux/auxiliary_bus.h
>> @@ -62,6 +62,9 @@
>>   * @sysfs.irqs: irqs xarray contains irq indices which are used by the device,
>>   * @sysfs.lock: Synchronize irq sysfs creation,
>>   * @sysfs.irq_dir_exists: whether "irqs" directory exists,
>> + * @registration_data_rust: private data owned by the registering (parent)
>> + *                          driver; valid for as long as the device is
>> + *                          registered with the driver core,
>>   *
>>   * An auxiliary_device represents a part of its parent device's functionality.
>>   * It is given a name that, combined with the registering drivers
>> @@ -148,6 +151,7 @@ struct auxiliary_device {
>>  		struct mutex lock; /* Synchronize irq sysfs creation */
>>  		bool irq_dir_exists;
>>  	} sysfs;
>> +	void *registration_data_rust;
>
> Is this really Rust-specific? Would you not want C drivers with the same
> pattern to do the same thing?
>
>> +        // SAFETY: `ptr` is non-null and was set via `into_foreign()` in `Registration::new()`;
>> +        // `RegistrationData` is `#[repr(C)]` with `type_id` at offset 0, so reading a `TypeId`
>> +        // at the start of the allocation is valid regardless of `T`.
>> +        let type_id = unsafe { ptr.cast::<TypeId>().read() };
>> +        if type_id != TypeId::of::<T>() {
>> +            return Err(EINVAL);
>> +        }
>
> Right, okay, so if you put C stuff there, we need the layout to be
> compatible with Rust type ids.
>
> Still, we could have Rust expose a couple methods to allow C code to use
> the same field with a null type id.
>
> But I guess this is all future work.
>
>> +        let data = KBox::pin_init::<Error>(
>> +            try_pin_init!(RegistrationData {
>> +                type_id: TypeId::of::<T>(),
>> +                data <- data,
>> +            }),
>> +            GFP_KERNEL,
>> +        )?;
>> +
>> +        let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
>
> Use __GFP_ZERO here instead?

On thing I wanted to implement in pin-init is a way to hint whether a type
prefers uninit or zero-init before initializing. Something like

    pub unsafe trait PinInit<T: ?Sized, E = Infallible>: Sized {
        const PREFER_ZERO_INIT: bool = false;

        unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>;

        // Extra safety requirement that `slot` is zero-filled.
        unsafe fn __pinned_init_from_zero(self, slot: *mut T) -> Result<(), E> {
            self.__pinned_init(slot)
        }
    }

Then the `pin_init` macro, if it detects that `..Zeroable::zeroed()` is used,
can generate a impl where `PREFER_ZERO_INIT` is set to `true`, `__pinned_init`
that does a memset + `__pinned_init_from_zero`.

Then we can have `KBox::pin_init` checks `PREFER_ZERO_INIT`. If true, it adds a
`__GFP_ZERO` flag and invoke the `__pinned_init_from_zero` method, other wise it
uses the `__pinned_init` method without the __GFP_ZERO flag.

I've been tempting to this for a while now, but I prioritize landing the
self-referential feature before this one.

Best,
Gary

  parent reply	other threads:[~2026-04-30 14:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-27 22:09 [PATCH 0/2] rust: auxiliary: replace drvdata() with registration data Danilo Krummrich
2026-04-27 22:09 ` [PATCH 1/2] rust: auxiliary: add registration data to auxiliary devices Danilo Krummrich
2026-04-28 10:18   ` Danilo Krummrich
2026-04-29 11:21   ` Alexandre Courbot
2026-04-29 13:58     ` Danilo Krummrich
2026-04-30  8:59   ` Alice Ryhl
2026-04-30 14:19     ` Danilo Krummrich
2026-04-30 14:31     ` Gary Guo [this message]
2026-04-30 15:08   ` Gary Guo
2026-04-30 16:05     ` Danilo Krummrich
2026-04-27 22:09 ` [PATCH 2/2] rust: driver core: remove drvdata() and driver_type Danilo Krummrich
2026-04-30  9:00   ` Alice Ryhl
2026-04-27 22:14 ` [PATCH 0/2] rust: auxiliary: replace drvdata() with registration data 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=DI6K97GKZKRG.2O9FPPA36J6RU@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=david.m.ertman@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=ira.weiny@intel.com \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox