Skip to contentSkip to conversation

rust: usb: don't retain device context for the interface parent

PATCH New Danilo Krummrich 1 participant 2 messages Sep 25, 2025

Subsystems (2)
lore
Share & tools
lore

Danilo Krummrich

author reviewer
#0 lore raw

PATCH 1/2 rust: usb: don't retain device context for the interface parent

When deriving the parent USB device (struct usb_device) from a USB interface (struct usb_interface), do not retain the device context.

For the Bound context, as pointed out by Alan in [1], it is not guaranteed that the parent USB device is always bound when the interface is bound.

The bigger problem, however, is that we can't infer the Core context, since eventually it indicates that the device lock is held. However, there is no guarantee that if the device lock of the interface is held, also the device lock of the parent USB device is held.

Hence, fix this by not inferring any device context information; while at it, fix up the (affected) safety comments.

Fixes e7e2296b0ecf ("rust: usb: add basic USB abstractions")

---
Deriving the parent device will be needed (at least from within the abstraction), hence I kept the AsRef implementation and only dropped the inferred device context information.

Regardless, I'm not sure AsRef is really what we want here. A simple Interface::parent() method seems more appropriate to me, but this patch focuses on fixing the bug, hence I kept AsRef.

---
 rust/kernel/usb.rs | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
rust/kernel/usb.rs +5 -6
index 8899e7520b58..955fd93b6f52 100644
@@ -340,14 +340,13 @@ fn as_ref(&self) -> &device::Device<Ctx> {
     }
 }
 
-impl<Ctx: device::DeviceContext> AsRef<Device<Ctx>> for Interface<Ctx> {
-    fn as_ref(&self) -> &Device<Ctx> {
-        // SAFETY: `self.as_raw()` is valid by the type invariants. For a valid interface,
-        // the helper should always return a valid USB device pointer.
+impl<Ctx: device::DeviceContext> AsRef<Device> for Interface<Ctx> {
+    fn as_ref(&self) -> &Device {
+        // SAFETY: `self.as_raw()` is valid by the type invariants.
         let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
 
-        // SAFETY: The helper returns a valid interface pointer that shares the
-        // same `DeviceContext`.
+        // SAFETY: For a valid `struct usb_interface` pointer, the above call to
+        // `interface_to_usbdev()` guarantees to return a valid pointer to a `struct usb_device`.
         unsafe { &*(usb_dev.cast()) }
     }
 }

base-commit: c584a1c7c8a192c13637bc51c7b63a9f15fe6474

Signature · 1 line
2.51.0
author reviewer
#1 lore raw

PATCH 2/2 rust: usb: keep usb::Device private for now

The USB abstractions target to support USB interface drivers.

While internally the abstraction has to deal with the interface's parent USB device, there shouldn't be a need for users to deal with the parent USB device directly.

Functions, such as for preparing and sending USB URBs, can be implemented for the usb::Interface structure directly. Whether this internal implementation has to deal with the parent USB device can remain transparent to USB interface drivers.

Hence, keep the usb::Device structure private for now, in order to avoid confusion for users and to make it less likely to accidentally expose APIs with unnecessary indirections.

Should we start supporting USB device drivers, or need it for any other reason we do not foresee yet, it should be trivial to make it public again.

---
 rust/kernel/usb.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/usb.rs +1 -1
index 955fd93b6f52..14ddb711bab3 100644
@@ -386,7 +386,7 @@ unsafe impl Sync for Interface {}
 ///
 /// [`struct usb_device`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_device
 #[repr(transparent)]
-pub struct Device<Ctx: device::DeviceContext = device::Normal>(
+struct Device<Ctx: device::DeviceContext = device::Normal>(
     Opaque<bindings::usb_device>,
     PhantomData<Ctx>,
 );
Signature · 1 line
2.51.0