lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z1M-2J1wtLwEhz8D@pollux>
Date: Fri, 6 Dec 2024 19:13:44 +0100
From: Danilo Krummrich <dakr@...nel.org>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: gregkh@...uxfoundation.org, rafael@...nel.org, bhelgaas@...gle.com,
	ojeda@...nel.org, alex.gaynor@...il.com, boqun.feng@...il.com,
	gary@...yguo.net, bjorn3_gh@...tonmail.com, benno.lossin@...ton.me,
	tmgross@...ch.edu, a.hindborg@...sung.com, airlied@...il.com,
	fujita.tomonori@...il.com, lina@...hilina.net, pstanner@...hat.com,
	ajanulgu@...hat.com, lyude@...hat.com, robh@...nel.org,
	daniel.almeida@...labora.com, saravanak@...gle.com,
	dirk.behme@...bosch.com, j@...nau.net, fabien.parent@...aro.org,
	chrisi.schrefl@...il.com, rust-for-linux@...r.kernel.org,
	linux-kernel@...r.kernel.org, linux-pci@...r.kernel.org,
	devicetree@...r.kernel.org,
	Wedson Almeida Filho <wedsonaf@...il.com>
Subject: Re: [PATCH v4 02/13] rust: implement generic driver registration

On Fri, Dec 06, 2024 at 02:57:19PM +0100, Alice Ryhl wrote:
> On Thu, Dec 5, 2024 at 3:16 PM Danilo Krummrich <dakr@...nel.org> wrote:
> >
> > Implement the generic `Registration` type and the `DriverOps` trait.
> >
> > The `Registration` structure is the common type that represents a driver
> > registration and is typically bound to the lifetime of a module. However,
> > it doesn't implement actual calls to the kernel's driver core to register
> > drivers itself.
> >
> > Instead the `DriverOps` trait is provided to subsystems, which have to
> > implement `DriverOps::register` and `DrvierOps::unregister`. Subsystems
> 
> typo
> 
> > have to provide an implementation for both of those methods where the
> > subsystem specific variants to register / unregister a driver have to
> > implemented.
> >
> > For instance, the PCI subsystem would call __pci_register_driver() from
> > `DriverOps::register` and pci_unregister_driver() from
> > `DrvierOps::unregister`.
> >
> > Co-developed-by: Wedson Almeida Filho <wedsonaf@...il.com>
> > Signed-off-by: Wedson Almeida Filho <wedsonaf@...il.com>
> > Signed-off-by: Danilo Krummrich <dakr@...nel.org>
> 
> [...]
> 
> > +/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
> > +/// Amba, etc.) to provide the corresponding subsystem specific implementation to register /
> > +/// unregister a driver of the particular type (`RegType`).
> > +///
> > +/// For instance, the PCI subsystem would set `RegType` to `bindings::pci_driver` and call
> > +/// `bindings::__pci_register_driver` from `RegistrationOps::register` and
> > +/// `bindings::pci_unregister_driver` from `RegistrationOps::unregister`.
> > +pub trait RegistrationOps {
> > +    /// The type that holds information about the registration. This is typically a struct defined
> > +    /// by the C portion of the kernel.
> > +    type RegType: Default;
> 
> This Default implementation doesn't seem useful. You initialize it and

I think it is -- `RegType` is always the raw bindings:: type and in
`Registration::new` in `Opaque::try_ffi_init` we call
`ptr.write(T::RegType::default())` for - since `RegType` is a raw bindings::
type - zero initialization.

> then `register` calls a C function to initialize it. Having `register`
> return an `impl PinInit` seems like it would work better here.

This would work as well, but it would effectively move the common code from
`Registration::new` to the bus specific type.

I think it's quite nice that the bus specific code does not need to care about
messing with `try_pin_init`, `Opaque::try_ffi_init`, zero initialization, etc.,
but just needs to assign the relevant fields and call register.

> 
> > +    /// Registers a driver.
> > +    ///
> > +    /// On success, `reg` must remain pinned and valid until the matching call to
> > +    /// [`RegistrationOps::unregister`].
> > +    fn register(
> > +        reg: &mut Self::RegType,
> 
> If the intent is that RegType is going to be the raw bindings:: type,
> then this isn't going to work because you're creating &mut references
> to the raw type without a Opaque wrapper in between.

True, that seems unsound. Since this is called from when the corresponding
`Opaque` wrapper is created, I think we need to fall back to a raw pointer then
and make `register` and `unregister` unsafe.

I don't think that too big of a deal though, since those two should never be
called from anywhere else than `Registration:new` or `Registration::drop`.

> 
> > +        name: &'static CStr,
> > +        module: &'static ThisModule,
> > +    ) -> Result;
> > +
> > +    /// Unregisters a driver previously registered with [`RegistrationOps::register`].
> > +    fn unregister(reg: &mut Self::RegType);
> 
> I believe this handles pinning incorrectly. You can't hand out &mut
> references to pinned values.

Same as above.

> 
> Alice

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ