Re: [PATCH v2 3/4] rust: add basic serial device bus abstractions

From: Danilo Krummrich

Date: Fri Mar 06 2026 - 15:40:40 EST


On Fri Mar 6, 2026 at 8:35 PM CET, Markus Probst wrote:
> + extern "C" fn receive_buf_callback(
> + sdev: *mut bindings::serdev_device,
> + buf: *const u8,
> + length: usize,
> + ) -> usize {
> + // SAFETY: The serial device bus only ever calls the receive buf callback with a valid
> + // pointer to a `struct serdev_device`.
> + //
> + // INVARIANT: `sdev` is valid for the duration of `receive_buf_callback()`.
> + let sdev = unsafe { &*sdev.cast::<Device<device::CoreInternal>>() };
> +
> + // SAFETY: `receive_buf_callback` is only ever called after a successful call to
> + // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
> + // and stored a `Pin<KBox<T>>`.
> + let data = unsafe { sdev.as_ref().drvdata_borrow::<T>() };
> +
> + // SAFETY:
> + // - The serial device bus only ever calls the receive buf callback with a valid pointer to
> + // a `struct serdev_device`.
> + // - `receive_buf_callback` is only ever called after a successful call to
> + // `probe_callback`, hence it's guaranteed that `sdev.private_data` is a pointer
> + // to a valid `PrivateData`.
> + let private_data = unsafe { &*(*sdev.as_raw()).private_data.cast::<PrivateData>() };
> +
> + private_data.probe_complete.complete_all();

Will do a full review pass later on, but one quick question in advance:

What is this used for? It is completed here and in probe(), but I don't see it ever
being used to actually wait.

> +
> + // SAFETY: No one has exclusive access to `private_data.error`.
> + if unsafe { *private_data.error.get() } {
> + return length;
> + }
> +
> + // SAFETY: `buf` is guaranteed to be non-null and has the size of `length`.
> + let buf = unsafe { core::slice::from_raw_parts(buf, length) };
> +
> + T::receive(sdev, data, buf)
> + }
> +}