Auxiliary bus Rust abstractions
Subsystems (3)
Danilo Krummrich
maintainerv3 cover Auxiliary bus Rust abstractions
This series adds Rust abstractions for the auxiliary bus.
It implements the required abstractions to write auxiliary drivers and create auxiliary device registrations. A driver sample illustrates how the corresponding abstractions work.
The auxiliary abstractions are required for the Nova driver project, in order to connect nova-core with the nova-drm driver.
This patch series depends on patches queued for v6.15-rc1 from different trees (driver-core, nova), plus the patches in [1]; a branch containing the patches, including the dependencies can be found in [2].
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
Changes in v3:
- export auxiliary_bus_type
- implement impl TryFrom<&Device> for &auxiliary::Device
- implement Send + Sync for Registration and Device
Changes in v2:
- implement bus device soundness improvements from [2]
Danilo Krummrich (5):
rust: types: add Opaque::zeroed
driver: core: auxiliary: export auxiliary_bus_type
rust: auxiliary: add auxiliary device / driver abstractions
rust: auxiliary: add auxiliary registration
samples: rust: add Rust auxiliary driver sample
MAINTAINERS | 2 + drivers/base/auxiliary.c | 3 +- include/linux/auxiliary_bus.h | 2 + rust/bindings/bindings_helper.h | 1 + rust/helpers/auxiliary.c | 23 ++ rust/helpers/helpers.c | 1 + rust/kernel/auxiliary.rs | 383 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/types.rs | 8 + samples/rust/Kconfig | 12 + samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 110 ++++++++ 12 files changed, 547 insertions(+), 1 deletion(-) create mode 100644 rust/helpers/auxiliary.c create mode 100644 rust/kernel/auxiliary.rs create mode 100644 samples/rust/rust_driver_auxiliary.rs
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v3 1/5 rust: types: add `Opaque::zeroed`
Analogous to Opaque::uninit add Opaque::zeroed, which sets the corresponding memory to zero. In contrast to Opaque::uninit, the corresponding value, depending on its type, may be initialized.
--- rust/kernel/types.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/types.rs +8 -0
@@ -326,6 +326,14 @@ pub const fn uninit() -> Self { } } +/// Creates a new zeroed opaque value. +pub const fn zeroed () ->Self { +Self { + value:UnsafeCell ::new (MaybeUninit ::zeroed ()), + _pin:PhantomPinned , + } + } +/// Create an opaque pin-initializer from the given pin-initializer. pub fn pin_init (slot:impl PinInit <T >) ->impl PinInit <Self > {Self ::ffi_init (|ptr: *mut T | {
Signature · 1 line
2.48.1
Re: [PATCH v3 1/5] rust: types: add `Opaque::zeroed`
Quoted 6 lines · Danilo Krummrich
Analogous to
Opaque::uninitaddOpaque::zeroed, which sets the corresponding memory to zero. In contrast toOpaque::uninit, the corresponding value, depending on its type, may be initialized.Signed-off-by Danilo Krummrich <[email protected]>
Danilo Krummrich
maintainerPATCH v3 2/5 driver: core: auxiliary: export auxiliary_bus_type
In reply to Danilo KrummrichIn Rust we can safely derive a struct auxiliary_device pointer from a generic struct device pointer by checking
dev->bus == auxiliary_bus_type
hence, export auxiliary_bus_type.
--- drivers/base/auxiliary.c | 3 ++- include/linux/auxiliary_bus.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/base/auxiliary.c +2 -1
@@ -244,7 +244,7 @@ static void auxiliary_bus_shutdown(struct device *dev) auxdrv->shutdown (auxdev); } -static const struct bus_type auxiliary_bus_type = { +const struct bus_type auxiliary_bus_type = { .name ="auxiliary" , .probe = auxiliary_bus_probe, .remove = auxiliary_bus_remove,
@@ -253,6 +253,7 @@ static const struct bus_type auxiliary_bus_type = { .uevent = auxiliary_uevent, .pm = &auxiliary_dev_pm_ops, }; +EXPORT_SYMBOL_GPL (auxiliary_bus_type);/** * auxiliary_device_init - check auxiliary_device and initialize
include/linux/auxiliary_bus.h +2 -0
@@ -197,6 +197,8 @@ struct auxiliary_driver {const struct auxiliary_device_id *id_table; }; +extern const struct bus_type auxiliary_bus_type; +static inline void *auxiliary_get_drvdata (struct auxiliary_device *auxdev) {return dev_get_drvdata (&auxdev->dev);
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v3 3/5 rust: auxiliary: add auxiliary device / driver abstractions
In reply to Danilo KrummrichImplement the basic auxiliary abstractions required to implement a driver matching an auxiliary device.
The design and implementation is analogous to PCI and platform and is based on the generic device / driver abstractions.
--- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/helpers/auxiliary.c | 23 +++ rust/helpers/helpers.c | 1 + rust/kernel/auxiliary.rs | 299 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 327 insertions(+) create mode 100644 rust/helpers/auxiliary.c create mode 100644 rust/kernel/auxiliary.rs
MAINTAINERS +1 -0
@@ -7114,6 +7114,7 @@ F: include/linux/kobj* F: include/linux/property.h F: include/linux/sysfs.h F: lib/kobj* +F: rust/kernel/auxiliary.rs F: rust/kernel/device.rs F: rust/kernel/device_id.rs F: rust/kernel/devres.rs
rust/bindings/bindings_helper.h +1 -0
@@ -7,6 +7,7 @@*/ #include <kunit/test.h> +#include <linux/auxiliary_bus.h> #include <linux/blk-mq.h> #include <linux/blk_types.h> #include <linux/blkdev.h>
rust/helpers/auxiliary.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/auxiliary_bus.h> + +void rust_helper_auxiliary_set_drvdata (struct auxiliary_device *adev,void *data) +{ +auxiliary_set_drvdata (adev, data); +} + +void *rust_helper_auxiliary_get_drvdata (struct auxiliary_device *adev) +{ +return auxiliary_get_drvdata (adev); +} + +void rust_helper_auxiliary_device_uninit (struct auxiliary_device *adev) +{ +return auxiliary_device_uninit (adev); +} + +void rust_helper_auxiliary_device_delete (struct auxiliary_device *adev) +{ +return auxiliary_device_delete (adev); +}
rust/helpers/helpers.c +1 -0
@@ -7,6 +7,7 @@* Sorted alphabetically. */ +#include "auxiliary.c" #include "blk.c" #include "bug.c" #include "build_assert.c"
rust/kernel/auxiliary.rs new file +299 -0
299 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -36,6 +36,8 @@pub use ffi;pub mod alloc; +#[cfg( CONFIG_AUXILIARY_BUS )] +pub mod auxiliary;#[cfg( CONFIG_BLOCK )] pub mod block;#[doc(hidden)]
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v3 4/5 rust: auxiliary: add auxiliary registration
In reply to Danilo KrummrichImplement the auxiliary::Registration type, which provides an API to create and register new auxiliary devices in the system.
--- rust/kernel/auxiliary.rs | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
rust/kernel/auxiliary.rs +84 -0
@@ -220,6 +220,16 @@ pub fn id(&self) -> u32 {// `struct auxiliary_device`. unsafe { (*self.as_raw ()).id } } + +extern "C" fn release (dev: *mut bindings::device ) { +// SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device` +// embedded in `struct auxiliary_device`. +let adev =unsafe {container_of! (dev, bindings::auxiliary_device, dev) }.cast_mut (); + +// SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via +// `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`. +let _ =unsafe {KBox ::<Opaque <bindings::auxiliary_device >>::from_raw (adev.cast ()) }; + } }impl Deref for Device <device::Core > {
@@ -297,3 +307,77 @@ unsafe impl Send for Device {}// SAFETY: `Device` can be shared among threads because all methods of `Device` // (i.e. `Device<Normal>) are thread safe. unsafe impl Sync for Device {} + +/// The registration of an auxiliary device. +/// +/// This type represents the registration of a [`struct auxiliary_device`]. When an instance of this +/// type is dropped, its respective auxiliary device will be unregistered from the system. +/// +/// # Invariants +/// +/// `self.0` always holds a valid pointer to an initialized and registered +/// [`struct auxiliary_device`]. +pub struct Registration (NonNull <bindings::auxiliary_device >); + +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) }; + +return Err (Error ::from_errno (ret)); + } + +// SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated successfully. +// +// INVARIANT: The device will remain registered until `auxiliary_device_delete()` is called, +// which happens in `Self::drop()`. +Ok (Self (unsafe {NonNull ::new_unchecked (adev) })) + } +} + +impl Drop for Registration { +fn drop (&mut self) { +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_delete (self.0 .as_ptr ()) }; + +// This drops the reference we acquired through `auxiliary_device_init()`. +// +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_uninit (self.0 .as_ptr ()) }; + } +} + +// SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread. +unsafe impl Send for Registration {} + +// SAFETY: `Registration` does not expose any methods or fields that need synchronization. +unsafe impl Sync for Registration {}
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v3 5/5 samples: rust: add Rust auxiliary driver sample
In reply to Danilo KrummrichAdd a sample Rust auxiliary driver based on a PCI driver for QEMU's "pci-testdev" device.
The PCI driver only registers an auxiliary device, in order to make the corresponding auxiliary driver probe.
--- MAINTAINERS | 1 + samples/rust/Kconfig | 12 +++ samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 110 ++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 samples/rust/rust_driver_auxiliary.rs
MAINTAINERS +1 -0
@@ -7121,6 +7121,7 @@ F: rust/kernel/devres.rs F: rust/kernel/driver.rs F: rust/kernel/faux.rs F: rust/kernel/platform.rs +F: samples/rust/rust_driver_auxiliary.rs F: samples/rust/rust_driver_platform.rs F: samples/rust/rust_driver_faux.rs
samples/rust/Kconfig +12 -0
@@ -71,6 +71,18 @@ config SAMPLE_RUST_DRIVER_FAUX If unsure, say N. +config SAMPLE_RUST_DRIVER_AUXILIARY + tristate "Auxiliary Driver" + depends on AUXILIARY_BUS + depends on PCI + help + This option builds the Rust auxiliary driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_auxiliary. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help
samples/rust/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o rust_print-y := rust_print_main.o rust_print_events.o
samples/rust/rust_driver_auxiliary.rs new file +110 -0
110 lines not on this page · raw message
Signature · 1 line
2.48.1