All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mohamed Osama" <mohamed.osama189110@gmail.com>
Cc: gary@garyguo.net, ojeda@kernel.org, linux-serial@vger.kernel.org
Subject: Re: [PATCH v2] rust: mem: add DropGuard
Date: Wed, 16 Sep 2026 09:59:19 +0000	[thread overview]
Message-ID: <20260916095920.068BF1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260916094959.538394-1-mohamed.osama189110@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] rust: serdev: compilation error due to invalid method call on DropGuard
- [Low] rust: mem: DropGuard abstraction traits missing #[inline]
--

commit e6da4b253a1c111afae2f6a79a8b6b1616c3e342
Author: Mohamed Osama <mohamed.osama189110@gmail.com>

rust: mem: add DropGuard

Add a `DropGuard` type to the Rust kernel memory module for running
cleanup code when a scope is left. `DropGuard` runs a `FnOnce` callback 
when dropped and provides `dismiss()` to take the wrapped value without 
running the callback.

Link: https://github.com/Rust-for-Linux/linux/issues/1255

> diff --git a/rust/kernel/mem.rs b/rust/kernel/mem.rs
> index f2d4cdf87d00b..f2d1f9bc93379 100644
> --- a/rust/kernel/mem.rs
> +++ b/rust/kernel/mem.rs
[ ... ]
> +impl<T, F> Deref for DropGuard<T, F>
> +where
> +    F: FnOnce(T),
> +{
> +    type Target = T;
> +
> +    fn deref(&self) -> &T {
       ^^^^^^^^

[Severity: Low]
Does this small abstraction method need an #[inline] annotation?

> +        &self.inner
> +    }
> +}
> +
> +impl<T, F> DerefMut for DropGuard<T, F>
> +where
> +    F: FnOnce(T),
> +{
> +    fn deref_mut(&mut self) -> &mut T {
       ^^^^^^^^^^^^

[Severity: Low]
Should this method also be annotated with #[inline]?

> +        &mut self.inner
> +    }
> +}
> +
> +impl<T, F> Drop for DropGuard<T, F>
> +where
> +    F: FnOnce(T),
> +{
> +    fn drop(&mut self) {
       ^^^^^^^

[Severity: Low]
Should we add #[inline] to this method as well?

> +        // SAFETY: `DropGuard` is in the process of being dropped.
> +        let inner = unsafe { ManuallyDrop::take(&mut self.inner) };
> +
> +        // SAFETY: `DropGuard` is in the process of being dropped.
> +        let f = unsafe { ManuallyDrop::take(&mut self.f) };
> +
> +        f(inner);
> +    }
> +}
[ ... ]
> diff --git a/rust/kernel/serdev.rs b/rust/kernel/serdev.rs
> index 17ca504b7f8d6..a400d241c2ad6 100644
> --- a/rust/kernel/serdev.rs
> +++ b/rust/kernel/serdev.rs
[ ... ]
> @@ -174,7 +172,7 @@ extern "C" fn probe_callback(sdev: *mut bindings::serdev_device) -> kernel::ffi:
>              }))?;
>              // SAFETY: We just set drvdata to `PrivateData<'_, T>`.
>              let private_data = unsafe { sdev.as_ref().drvdata_borrow::<PrivateData<'_, T>>() };
> -            let private_data = ScopeGuard::new_with_data(private_data, |_| {
> +            let private_data = DropGuard::new(private_data, |_| {
>                  // SAFETY: We just set drvdata to `PrivateData<'_, T>`.
>                  drop(unsafe { sdev.as_ref().drvdata_obtain::<PrivateData<'_, T>>() });
>              });

[Severity: Low]
Does this change cause a compilation failure further down in probe_callback()?

Because private_data is now a DropGuard, it no longer has a dismiss() method
that takes self. Later in the function we have:

    result.map(|()| {
        private_data.dismiss();
        0
    })

Could we change this to use the associated function syntax instead, like
DropGuard::dismiss(private_data)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260916094959.538394-1-mohamed.osama189110@gmail.com?part=1

      reply	other threads:[~2026-09-16  9:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-16  9:49 [PATCH v2] rust: mem: add DropGuard Mohamed Osama
2026-09-16  9:59 ` sashiko-bot [this message]

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=20260916095920.068BF1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-serial@vger.kernel.org \
    --cc=mohamed.osama189110@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.