From: "Gary Guo" <gary@garyguo.net>
To: "Mohamed Osama" <mohamed.osama189110@gmail.com>, <ojeda@kernel.org>
Cc: <boqun@kernel.org>, <gary@garyguo.net>,
<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
<a.hindborg@kernel.org>, <aliceryhl@google.com>,
<tmgross@umich.edu>, <dakr@kernel.org>,
<daniel.almeida@collabora.com>, <tamird@kernel.org>,
<acourbot@nvidia.com>, <work@onurozkan.dev>, <longman@redhat.com>,
<lyude@redhat.com>, <linux-block@vger.kernel.org>,
<linux-serial@vger.kernel.org>, <rust-for-linux@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v3 2/5] rust: mem: add DropGuard
Date: Sun, 27 Sep 2026 00:53:15 +0100 [thread overview]
Message-ID: <DLPNIATK93BE.MP4DZTVSAEPN@garyguo.net> (raw)
In-Reply-To: <20260926173315.56772-3-mohamed.osama189110@gmail.com>
On Sat Sep 26, 2026 at 6:33 PM BST, Mohamed Osama wrote:
> Add DropGuard to the Rust kernel memory module.
>
> DropGuard runs a FnOnce callback when the guard is dropped and provides
> dismiss() to take ownership of the wrapped value without running the
> cleanup callback.
>
> This follows the upstream core::mem::DropGuard API, which is being
> stabilized in Rust.
>
> Keep ScopeGuard for existing users rather than changing its API in this
> patch. Migrate the existing users that can use DropGuard to establish
> the kernel API alongside the upstream Rust API.
If you're vendoring code from standard library, please put them in std_vendor
and have it re-exported from kernel::mem please.
Also, if the code is taken directly from std with only minor changes (e.g.
removing const trait stuff and add `#[inline]`), then I don't think we need the
kunit tests.
Best,
Gary
>
> Signed-off-by: Mohamed Osama <mohamed.osama189110@gmail.com>
> ---
> rust/kernel/mem.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 121 insertions(+)
>
> diff --git a/rust/kernel/mem.rs b/rust/kernel/mem.rs
> index f2d4cdf87d00..e7a84d15f3b3 100644
> --- a/rust/kernel/mem.rs
> +++ b/rust/kernel/mem.rs
> @@ -4,6 +4,93 @@
>
> use crate::prelude::*;
>
> +use core::mem::ManuallyDrop;
> +use core::ops::{Deref, DerefMut};
> +
> +/// Wraps a value and runs a closure when dropped.
> +///
> +/// This is useful for running cleanup code when leaving a scope.
> +///
> +/// The [`DropGuard::dismiss`] function can be used to take ownership of the wrapped
> +/// value without running the cleanup function.
> +#[doc(alias = "ScopeGuard")]
> +#[doc(alias = "defer")]
> +pub struct DropGuard<T, F>
> +where
> + F: FnOnce(T),
> +{
> + inner: ManuallyDrop<T>,
> + f: ManuallyDrop<F>,
> +}
> +
> +impl<T, F> DropGuard<T, F>
> +where
> + F: FnOnce(T),
> +{
> + /// Creates a new `DropGuard`.
> + #[inline]
> + #[must_use]
> + pub fn new(inner: T, f: F) -> Self {
> + Self {
> + inner: ManuallyDrop::new(inner),
> + f: ManuallyDrop::new(f),
> + }
> + }
> +
> + /// Consumes the `DropGuard`, returning the wrapped value without
> + /// running the cleanup function.
> + #[inline]
> + pub fn dismiss(guard: Self) -> T {
> + let mut guard = ManuallyDrop::new(guard);
> +
> + // SAFETY: We have taken ownership of the guard and prevent its destructor from running.
> + let value = unsafe { ManuallyDrop::take(&mut guard.inner) };
> +
> + // SAFETY: We have taken ownership of the guard.
> + unsafe { ManuallyDrop::drop(&mut guard.f) };
> +
> + value
> + }
> +}
> +
> +impl<T, F> Deref for DropGuard<T, F>
> +where
> + F: FnOnce(T),
> +{
> + type Target = T;
> +
> + #[inline]
> + fn deref(&self) -> &T {
> + &self.inner
> + }
> +}
> +
> +impl<T, F> DerefMut for DropGuard<T, F>
> +where
> + F: FnOnce(T),
> +{
> + #[inline]
> + fn deref_mut(&mut self) -> &mut T {
> + &mut self.inner
> + }
> +}
> +
> +impl<T, F> Drop for DropGuard<T, F>
> +where
> + F: FnOnce(T),
> +{
> + #[inline]
> + fn drop(&mut self) {
> + // 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);
> + }
> +}
> +
> /// Transmute between two types.
> ///
> /// Use this instead of [`core::mem::transmute`] when it is known that sizes are identical but this
> @@ -232,3 +319,37 @@ unsafe impl AsReprMut for $signed {}
> // `usize` is not normalized to particular integer for portability.
> usize isize,
> }
> +
> +#[cfg(CONFIG_RUST_DROP_GUARD_KUNIT_TEST)]
> +#[macros::kunit_tests(rust_drop_guard)]
> +mod tests {
> + use super::*;
> +
> + #[test]
> + fn test_drop_runs_cleanup() {
> + let mut cleaned = false;
> +
> + {
> + let _guard = DropGuard::new(42, |value| {
> + assert_eq!(value, 42);
> + cleaned = true;
> + });
> + }
> +
> + assert!(cleaned);
> + }
> +
> + #[test]
> + fn test_dismiss_returns_value_without_cleanup() {
> + let mut cleaned = false;
> +
> + let guard = DropGuard::new(42, |_| {
> + cleaned = true;
> + });
> +
> + let value = DropGuard::dismiss(guard);
> +
> + assert_eq!(value, 42);
> + assert!(!cleaned);
> + }
> +}
next prev parent reply other threads:[~2026-09-26 23:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-26 17:33 [PATCH v3 0/5] rust: " Mohamed Osama
2026-09-26 17:33 ` [PATCH v3 1/5] rust: add DropGuard KUnit test configuration Mohamed Osama
2026-09-26 17:33 ` [PATCH v3 2/5] rust: mem: add DropGuard Mohamed Osama
2026-09-26 23:53 ` Gary Guo [this message]
2026-09-26 17:33 ` [PATCH v3 3/5] rust: block: gen_disk: use DropGuard Mohamed Osama
2026-09-26 17:33 ` [PATCH v3 4/5] rust: serdev: " Mohamed Osama
2026-09-26 17:33 ` [PATCH v3 5/5] rust: sync: lock: " Mohamed Osama
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=DLPNIATK93BE.MP4DZTVSAEPN@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mohamed.osama189110@gmail.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.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
all inboxes | Powered by JetHome®