Skip to content

Commit f12140f

Browse files
Danilo Krummrichgregkh
authored andcommitted
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/[email protected]/ [1] Fixes: e7e2296 ("rust: usb: add basic USB abstractions") Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c584a1c commit f12140f

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

rust/kernel/usb.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,13 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Interface<Ctx> {
340340
}
341341
}
342342

343-
impl<Ctx: device::DeviceContext> AsRef<Device<Ctx>> for Interface<Ctx> {
344-
fn as_ref(&self) -> &Device<Ctx> {
345-
// SAFETY: `self.as_raw()` is valid by the type invariants. For a valid interface,
346-
// the helper should always return a valid USB device pointer.
343+
impl<Ctx: device::DeviceContext> AsRef<Device> for Interface<Ctx> {
344+
fn as_ref(&self) -> &Device {
345+
// SAFETY: `self.as_raw()` is valid by the type invariants.
347346
let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
348347

349-
// SAFETY: The helper returns a valid interface pointer that shares the
350-
// same `DeviceContext`.
348+
// SAFETY: For a valid `struct usb_interface` pointer, the above call to
349+
// `interface_to_usbdev()` guarantees to return a valid pointer to a `struct usb_device`.
351350
unsafe { &*(usb_dev.cast()) }
352351
}
353352
}

0 commit comments

Comments
 (0)