Linux Serial subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Markus Probst" <markus.probst@posteo.de>
Cc: linux-serial@vger.kernel.org, ojeda@kernel.org, gary@garyguo.net
Subject: Re: [PATCH RESEND v6 4/4] rust: serdev: Simplify callbacks
Date: Fri, 04 Dec 2026 17:13:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-250283@kernel.org> (raw)
In-Reply-To: <20261204-rust_serdev_probe_refactor-v6-4-0e12b6e6853b@posteo.de>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] rust: serdev: missing safety comments on PrivateData drop order
  and OpenGuard
- [Low] rust: serdev: unnecessary unsafe blocks in
  PrivateData::driver_data()
--

commit 291367d86a907a67ebd9525dcf9dbb55c3c6c67c
Author: Markus Probst <markus.probst@posteo.de>

rust: serdev: Simplify callbacks

This commit initializes the driver's private data directly on PrivateData
via pin-init and introduces OpenGuard to close the serdev device on drop.

> diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs
> index 559d142ce5573..3f9165e167764 100644
> --- a/rust/kernel/serdev.rs
> +++ b/rust/kernel/serdev.rs

[ ... ]

> @@ -92,24 +88,36 @@ unsafe fn unregister(sdrv: &Opaque<Self::DriverType>) {
>      }
>  }
>  
> +struct OpenGuard<'bound> {
> +    sdev: &'bound Device<device::Bound>,
> +}
> +
> +impl Drop for OpenGuard<'_> {
> +    fn drop(&mut self) {
> +        // SAFETY:
> +        // - `self.sdev.as_raw()` is guaranteed to be a pointer to a valid
> +        //   `struct serdev_device`.
> +        // - The existence of self proves that the device is open.
> +        unsafe { bindings::serdev_device_close(self.sdev.as_raw()) };
> +    }
> +}
> +
>  #[doc(hidden)]
> -#[pin_data(PinnedDrop)]
> +#[pin_data]
>  pub struct PrivateData<'bound, T: Driver> {
> -    sdev: &'bound Device<device::Bound>,
>      #[pin]
> -    driver: UnsafeCell<MaybeUninit<T::Data<'bound>>>,
> -    open: UnsafeCell<bool>,
> +    driver: T::Data<'bound>,
> +    open: OpenGuard<'bound>,
>  }

[Severity: Low]
Should PrivateData document that driver must be declared before open for
drop-order safety?

Adapter::probe_callback() initializes open before driver in try_pin_init!(),
the reverse of PrivateData's field order.  Reordering the fields to match
would close the device before T::Data drops, causing a NULL pointer
dereference of serport->tty if T::Data's Drop calls Device<Bound> methods.

Also, OpenGuard::drop() relies on the device being open whenever an OpenGuard
exists, so OpenGuard could use an # Invariants doc section and a matching
INVARIANT comment where it is constructed in probe_callback().

>  
> -#[pinned_drop]
> -impl<T: Driver> PinnedDrop for PrivateData<'_, T> {
> -    fn drop(self: Pin<&mut Self>) {
> -        // SAFETY: We have exclusive access to `self.open`.
> -        if unsafe { *self.open.get() } {
> -            // SAFETY: `self.sdev.as_raw()` is guaranteed to be a pointer to a valid
> -            // `struct serdev_device`.
> -            unsafe { bindings::serdev_device_close(self.sdev.as_raw()) };
> -        }
> +impl<'bound, T: Driver> PrivateData<'bound, T> {
> +    #[inline]
> +    fn driver_data(self: Pin<&Self>) -> Pin<&T::Data<'bound>> {
> +        // SAFETY: We treat the result as pinned.
> +        let inner = unsafe { Pin::into_inner_unchecked(self) };
> +
> +        // SAFETY: `self.driver` is pinned.
> +        unsafe { Pin::new_unchecked(&inner.driver) }
>      }
>  }

[Severity: Low]
Can driver_data() avoid both unsafe blocks by using pin-init's generated
with_project_ref() projection?

Because PrivateData is #[pin_data] with #[pin] on driver,
self.with_project_ref(|this| this.driver) projects Pin<&Self> to
Pin<&T::Data<'bound>> safely without Pin::into_inner_unchecked() or
Pin::new_unchecked().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-rust_serdev_probe_refactor-v6-0-0e12b6e6853b@posteo.de?part=4

  reply	other threads:[~2026-12-04 17:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:58 [PATCH RESEND v6 0/4] rust: serdev: Refactor Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 1/4] tty: serdev: Add mutex lock Markus Probst
2026-12-04 17:22   ` sashiko-bot
2026-12-04 17:45     ` Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 2/4] tty: serdev: Export functions to pause receive_buf callback calls Markus Probst
2026-12-04 17:23   ` sashiko-bot
2026-12-04 16:58 ` [PATCH RESEND v6 3/4] rust: serdev: Replace `active` mutex with receive pause Markus Probst
2026-12-04 16:58 ` [PATCH RESEND v6 4/4] rust: serdev: Simplify callbacks Markus Probst
2026-12-04 17:13   ` sashiko-bot [this message]
2026-12-04 17:37     ` Markus Probst
2026-12-04 17:23 ` [PATCH RESEND v6 0/4] rust: serdev: Refactor sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250283@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-serial@vger.kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox