summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2025-09-25 20:59:26 +0200
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2025-09-26 08:09:08 +0200
commitf12140f21acba1499e55cc0220d7c1fe518de369 (patch)
treee10d14d767726830d7d77155b8f3e334de9fe21f /rust/kernel
parentc584a1c7c8a192c13637bc51c7b63a9f15fe6474 (diff)
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. Link: https://lore.kernel.org/all/0ff2a825-1115-426a-a6f9-df544cd0c5fc@rowland.harvard.edu/ [1] Fixes: e7e2296b0ecf ("rust: usb: add basic USB abstractions") Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250925190400.144699-1-dakr@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/usb.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index 8899e7520b58..955fd93b6f52 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -340,14 +340,13 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Interface<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()) }
}
}