From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm2-f13.google.com (mail-wm2-f13.google.com [74.125.225.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1ACDC3BADAA for ; Sun, 27 Sep 2026 10:45:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790505936; cv=none; b=uXfhesYgcCamg2dFi+IlWofjdxg7QMhN/Lg5BKFvDNAc1NxBOSpexua0EmStB8LUiOGPGLTHzszKtTbd7K9+GCJ88OqzpBjS+mwwW+o5oHzwfCI5w9KCxosVX6Ic6tM+fzwK0q6/3g6+dBXnpSX54Sn/yxxz3Q+DO3DnN/nYHUU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790505936; c=relaxed/simple; bh=D+PPLTDoy1BMZdBIKZnJBleicfxyVmZ6wM/9g5CxMsY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pGZkgHj+BRxNIoFeerrUtkpFEOPrzgDkf1v4Th2OdnUcbCOozR7BtfszoqZ4dSYobu24VsjimYiIn7Rh+sw2XiZYI95V2QEINJiD/0WppSgdOCzd/zab8vsMusjc5AQbeeeQJDXbLwUjSqTAxUvMISc9E/MPQs9aPiCQKJG6zd0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G+5R8jWk; arc=none smtp.client-ip=74.125.225.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G+5R8jWk" Received: by mail-wm2-f13.google.com with SMTP id 5b1f17b1804b1-49e6c0fce17so10666015e9.1 for ; Sun, 27 Sep 2026 03:45:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790505933; x=1791110733; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=pxv+Z2Smk33JrKxjz4dLy9oowfBjQSsd5GaEq0kknSw=; b=G+5R8jWk5EFRvhYyIzzX1TTnZohX79XxWUan/UqSJnm2i/xH7/noEIcKpz38Bu8Gn2 lxEHZGsDvKGyCv+mgAKAE7i8Hv0W2TitaM+XpdrHIgj23fEE1sdOMyb8+8uVMfmhNCtZ k718NXF/wRALtroJVrdp3BH/pxGlxn0OjurHJiKWcmXC0ImpUEYZFD8y5WzFZMwmZgo2 XaPxGayVNe69JqCc/P0Cu2VKFWY9FB37kin6gmLyChogkcKjWJnE7NjAFLinoUMeT1b/ O1e5dBn8TRSAnCMva39S9Luzj2E6ochriqmel3bT5kttZIdSa2VeRz2GIajLctumLmvl 3GSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790505933; x=1791110733; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=pxv+Z2Smk33JrKxjz4dLy9oowfBjQSsd5GaEq0kknSw=; b=msCVyN8pXyy68NtJyX4YFPKLBZtJyQNXx0Fpd1oCLelihN615kyAX6MQ+BU528BDqh 6faTvnoOkmHMSy7KBx4Mfn5IF9xk8C8Ulq6uNBb5fgMwvjFu4QvMSIoayZTuEFrnMGTK VtVokDfVoVBm/HlS7CsFanhxPPhWSU1E3LJ1unkAgFOY1rbiaiOfe4YA7GFDlOtBmNpK lODlTA6j2BzEnu7DgX0zJzF5rNAvvtletw+A6GtYAh23urBVaWQjf5ncpVE/c6Wkn4yN 5Q2fBs2keMIsBS++v/gkaLtqbyuTek3jfrzUMSeqSnrlrNOMHW5XQaBU626fqTsaLFud aIuQ== X-Forwarded-Encrypted: i=1; AKwUvBxfGU/1vjTIrToUYE04lqejQJvN2JBpChFLw9l0A6sk4lWpmD5N6fV4KbDSDHi8ukTQ4wwlzyBM9l0JBrlC2g==@vger.kernel.org X-Gm-Message-State: AFuF++ksb8fIQlFi+6ggHMzZgC0VEga37goJIjXpUuEDIQnUR/SD+MtC z72gu6GbmwbBix6pydT1nl0xBL5Wq2ys0qh2IqtFdkaVXymrHnSiOn6o X-Gm-Gg: AYBFou2151t2eEwFVJkIMmjQVhJkmhh17SVO4mVLT982WjWzUX+9EcZgi4kWM4BGFZF T9X5dVUlEN7BiuY4hBC8kY5UFdHrOudfdEvmgDtKsA6/tGt/bbWpctHZWWbJVVUkmcFKDX6fO7/ vowmiyIMMZMtFs03v6W9yMQNxG0QVfiEagHvvli0QPOGGQTHT1retxtpNchpKAz6l1TOzy2S8ru 9CLDWyVNudqw5kjJJ8D40/eIH0KwsmDLxMCnOVibPWDNQnd2kPVTkNVTGoxayZeVu7SO1ueZB2+ 8tGLWyf5XjHKrfNafCV+YF6oOg8VZG0bf12wX1XH/FcJj/b/N1oaL5RpnlxFbrNpHvsJsRsoTAr VTHlKyKRGAWlxve6EfIU2zZ1/lSWAv1cSM0PF7h/6Ol0hKhHxt8p7uiApy6SOWJRSk4KJ0VNjhN UzoDNwxVZbLIVf2g7Bsxw+B2HbY/mB1ZnJAyKBnzZ2v+bvPh/VFw9uybpMieQLbF8sWEgxRQt41 y/lUPKfD2pIGLqegp8nJjWX1yJ+7axwy285ai2f7xptvJaW5x8WkYmZljbn9CBoF/8= X-Received: by 2002:a05:600c:c48f:b0:499:bf8c:cfd1 with SMTP id 5b1f17b1804b1-49fe66bdfdbmr191208665e9.2.1790505932955; Sun, 27 Sep 2026 03:45:32 -0700 (PDT) Received: from moosama76.. ([102.44.245.127]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4a001915fc2sm46005195e9.14.2026.09.27.03.45.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2026 03:45:32 -0700 (PDT) From: Mohamed Osama To: 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: [PATCH 1/4] rust: mem: add DropGuard Date: Sun, 27 Sep 2026 13:45:25 +0300 Message-ID: <20260927104528.103881-2-mohamed.osama189110@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260927104528.103881-1-mohamed.osama189110@gmail.com> References: <20260927104528.103881-1-mohamed.osama189110@gmail.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Vendor DropGuard from the Rust standard library and re-export it from kernel::mem. 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. The kernel version removes unstable const-trait machinery from the upstream implementation and adds #[inline] where appropriate. Suggested-by: Gary Guo Signed-off-by: Mohamed Osama --- rust/kernel/mem.rs | 2 + rust/kernel/std_vendor.rs | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/rust/kernel/mem.rs b/rust/kernel/mem.rs index f2d4cdf87d00..7f8ddd96f907 100644 --- a/rust/kernel/mem.rs +++ b/rust/kernel/mem.rs @@ -4,6 +4,8 @@ use crate::prelude::*; +pub use crate::std_vendor::DropGuard; + /// Transmute between two types. /// /// Use this instead of [`core::mem::transmute`] when it is known that sizes are identical but this diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs index abbab5050cc5..21a26b4eec71 100644 --- a/rust/kernel/std_vendor.rs +++ b/rust/kernel/std_vendor.rs @@ -7,6 +7,9 @@ //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, //! see . +use core::mem::ManuallyDrop; +use core::ops::{Deref, DerefMut}; + /// [`std::dbg`], but using [`pr_info`] instead of [`eprintln`]. /// /// Prints and returns the value of a given expression for quick and dirty @@ -162,3 +165,87 @@ macro_rules! dbg { ($($crate::dbg!($val)),+,) }; } + +/// 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 +where + F: FnOnce(T), +{ + inner: ManuallyDrop, + f: ManuallyDrop, +} + +impl DropGuard +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 Deref for DropGuard +where + F: FnOnce(T), +{ + type Target = T; + + #[inline] + fn deref(&self) -> &T { + &self.inner + } +} + +impl DerefMut for DropGuard +where + F: FnOnce(T), +{ + #[inline] + fn deref_mut(&mut self) -> &mut T { + &mut self.inner + } +} + +impl Drop for DropGuard +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); + } +} -- 2.43.0