Skip to contentSkip to conversation

Auxiliary bus Rust abstractions

PATCH Superseded Danilo Krummrich 2 participants 7 messages Mar 12, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

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 [1] (taken through the nova tree for v6.15); a branch containing the patches, including the dependency can be found in [2].

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary

Danilo Krummrich (4):
rust: types: add Opaque::zeroed
rust: auxiliary: add auxiliary driver abstractions
rust: auxiliary: add auxiliary registration
samples: rust: add Rust auxiliary driver sample

 MAINTAINERS                           |   2 +
 rust/bindings/bindings_helper.h       |   1 +
 rust/helpers/auxiliary.c              |  23 ++
 rust/helpers/helpers.c                |   1 +
 rust/kernel/auxiliary.rs              | 311 ++++++++++++++++++++++++++
 rust/kernel/lib.rs                    |   2 +
 rust/kernel/types.rs                  |   8 +
 samples/rust/Kconfig                  |  10 +
 samples/rust/Makefile                 |   1 +
 samples/rust/rust_driver_auxiliary.rs |  99 ++++++++
 10 files changed, 458 insertions(+)
 create mode 100644 rust/helpers/auxiliary.c
 create mode 100644 rust/kernel/auxiliary.rs
 create mode 100644 samples/rust/rust_driver_auxiliary.rs

base-commit: b28786b190d1ae2df5e6a5181ad78c6f226ea3e1

Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 1/4 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
index 2bbaab83b9d6..f5e7fed903a7 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/4 rust: auxiliary: add auxiliary driver abstractions

In reply to Danilo Krummrich

Implement 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        | 233 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 6 files changed, 261 insertions(+)
 create mode 100644 rust/helpers/auxiliary.c
 create mode 100644 rust/kernel/auxiliary.rs
MAINTAINERS +1 -0
index f5c7022937a7..0a737b28cdfa 100644
@@ -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
index f46cf3bb7069..83026b6e53b2 100644
@@ -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
new file mode 100644
index 000000000000..0db3860d774e
@@ -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
index 0640b7e115be..d744af85e3b2 100644
@@ -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 +233 -0

233 lines not on this page · raw message

rust/kernel/lib.rs +2 -0
index 398242f92a96..d9a2ca9d1f20 100644
@@ -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
author maintainer
#3 lore raw

PATCH 3/4 rust: auxiliary: add auxiliary registration

In reply to Danilo Krummrich

Implement the auxiliary::Registration type, which provides an API to create and register new auxiliary devices in the system.

---
 rust/kernel/auxiliary.rs | 80 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)
rust/kernel/auxiliary.rs +79 -1
index 3bdd690990aa..275dffc64a02 100644
@@ -14,7 +14,7 @@
     types::{ARef, ForeignOwnable, Opaque},
     ThisModule,
 };
-use core::ptr::addr_of_mut;
+use core::ptr::{addr_of_mut, NonNull};
 
 /// An adapter for the registration of auxiliary drivers.
 pub struct Adapter<T: Driver>(T);
@@ -224,6 +224,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 AsRef<device::Device> for Device {
@@ -231,3 +241,71 @@ fn as_ref(&self) -> &device::Device {
         &self.0
     }
 }
+
+/// 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()) };
+    }
+}
Signature · 1 line
2.48.1
author maintainer
#4 lore raw

PATCH 4/4 samples: rust: add Rust auxiliary driver sample

In reply to Danilo Krummrich

Add 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                  | 10 +++
 samples/rust/Makefile                 |  1 +
 samples/rust/rust_driver_auxiliary.rs | 99 +++++++++++++++++++++++++++
 4 files changed, 111 insertions(+)
 create mode 100644 samples/rust/rust_driver_auxiliary.rs
MAINTAINERS +1 -0
index 0a737b28cdfa..6ec20e4f715d 100644
@@ -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 +10 -0
index 3b6eae84b297..6679d1f1fd48 100644
@@ -71,6 +71,16 @@ config SAMPLE_RUST_DRIVER_FAUX
 
 	  If unsure, say N.
 
+config SAMPLE_RUST_DRIVER_AUXILIARY
+	tristate "Auxiliary Driver"
+	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
index 0dbc6d90f1ef..30a8809d0a15 100644
@@ -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 +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.48.1
maintainer
#5 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

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.

Nice!

Quoted 5 lines

This patch series depends on [1] (taken through the nova tree for v6.15); a branch containing the patches, including the dependency can be found in [2].

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary

Do you want this to wait until that lands in 6.15-rc1 or do you want to take this now through the nova tree? Either is fine with me.

thanks,

greg k-h

author maintainer
#6 lore raw
Quoted 12 lines · Greg KH
Quoted 8 lines · Danilo Krummrich

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.

Nice!

Quoted 8 lines
Quoted 5 lines

This patch series depends on [1] (taken through the nova tree for v6.15); a branch containing the patches, including the dependency can be found in [2].

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary

Do you want this to wait until that lands in 6.15-rc1 or do you want to take this now through the nova tree? Either is fine with me.

Thanks! Let's wait for things to land in Linus' tree and give people some time to review.

I will also send a v2 soon, fixing up a soundness issue I'm also working on a fix for platform and PCI.