From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D09C83009D4; Wed, 3 Jun 2026 01:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780449034; cv=none; b=eym7BmLoIImGoEIuT+QKDxun6Zl2YoCKOCvGy8UfWQKbtmOyifuDUNuz+5/aBBQnRgZpCuoWmdHAc9kx1xnNWdEqLOERVwblRswdDAWy3zExfYUddmPTHCp+fIXWzzTXehM8++pzLAX7YRdFdohxYNLkCj3KLLn9WyaU2gU/ilY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780449034; c=relaxed/simple; bh=i4XYcmDtnqt8WTR+fYIp8lD6BYSRNbq4l4LKDNpZzTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uGgdrw03kIOy0SQ8ugty4A199fzvitDqtgun57CBRaJ0vSM5cm/4y+PgT3vXXce21xJym7cgvlPcF+PNkLRKWsTZpmqsIeXkTEueqB7FXPAIzU8/2BsXHvfB9QnvZ+i+Cx/bqiof4AyNft8R7T8K/GS+i/fBRPUgrcjNRZjhikU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MxRCovIc; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MxRCovIc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B415E1F00898; Wed, 3 Jun 2026 01:10:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780449032; bh=wwWFb/VYif3ZZHAOc0E3QU4RogxZ39CKNKOixWvfwK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MxRCovIcwAtWWFXC7oCfKa6sOblVR61MKYnY0VBrzGB6jcsAoTq9qpbnocKsJo5zd XO/gz3x0Zq3V9PzhLfeftL3eRdB8BVpg4nAj6SEK+exKx6YRKY9y5nOLfOBTq0I4cK Om7f3NP5b885eWJrCg0LRSZjRG1I1mOWHCr9pAXo62hY6zvwVnAs/R46xCy5V4nskb veLqMke0wRxpLWPQpPkyyP/s//yhTjusqX3rQ6DPAR018d6FY25tDXydGB+RKmVi2k d6xc0mt2ljnGG8N4if+vr1sP17xGBlch3KiNBtQ6qrP0YpC66eHN2UCA8/npfgUZFN 9dK/7UIT1OqiA== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, acourbot@nvidia.com, ecourtney@nvidia.com, m.wilczynski@samsung.com, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, daniel.almeida@collabora.com, bhelgaas@google.com, kwilczynski@kernel.org Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-pwm@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH v2 1/7] rust: types: rename ForLt to CovariantForLt Date: Wed, 3 Jun 2026 03:10:12 +0200 Message-ID: <20260603011020.2073650-2-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260603011020.2073650-1-dakr@kernel.org> References: <20260603011020.2073650-1-dakr@kernel.org> 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 Rename ForLt to CovariantForLt to prepare for the introduction of a new ForLt base trait that does not require covariance. The existing ForLt trait requires covariance, which enables the safe cast_ref() method. This rename preserves the same semantics under a more precise name, making room for a weaker ForLt trait in a subsequent commit. No functional change. Signed-off-by: Danilo Krummrich --- drivers/gpu/nova-core/driver.rs | 4 +- rust/kernel/auxiliary.rs | 23 +++++----- rust/kernel/types.rs | 2 +- rust/kernel/types/for_lt.rs | 61 ++++++++++++++------------- rust/macros/for_lt.rs | 4 +- rust/macros/lib.rs | 11 +++-- samples/rust/rust_driver_auxiliary.rs | 8 ++-- 7 files changed, 57 insertions(+), 56 deletions(-) diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs index ade73da68be5..dce8b3b8dc6f 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -15,7 +15,7 @@ Atomic, Relaxed, // }, - types::ForLt, + types::CovariantForLt, }; use crate::gpu::Gpu; @@ -29,7 +29,7 @@ pub(crate) struct NovaCore<'bound> { pub(crate) gpu: Gpu<'bound>, bar: pci::Bar<'bound, BAR0_SIZE>, #[allow(clippy::type_complexity)] - _reg: auxiliary::Registration<'bound, ForLt!(())>, + _reg: auxiliary::Registration<'bound, CovariantForLt!(())>, } pub(crate) struct NovaCoreDriver; diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs index c42928d5a239..40a0af74a8e5 100644 --- a/rust/kernel/auxiliary.rs +++ b/rust/kernel/auxiliary.rs @@ -20,7 +20,7 @@ }, prelude::*, types::{ - ForLt, + CovariantForLt, ForeignOwnable, Opaque, // }, @@ -272,16 +272,16 @@ pub fn parent(&self) -> &device::Device { /// Returns a pinned reference to the registration data set by the registering (parent) driver. /// - /// `F` is the [`ForLt`](trait@ForLt) encoding of the data type. The returned + /// `F` is the [`CovariantForLt`](trait@CovariantForLt) encoding of the data type. The returned /// reference has its lifetime shortened from `'static` to `&self`'s borrow lifetime via - /// [`ForLt::cast_ref`]. + /// [`CovariantForLt::cast_ref`]. /// /// Returns [`EINVAL`] if `F` does not match the type used by the parent driver when calling /// [`Registration::new()`]. /// /// Returns [`ENOENT`] if no registration data has been set, e.g. when the device was /// registered by a C driver. - pub fn registration_data(&self) -> Result>> { + pub fn registration_data(&self) -> Result>> { // SAFETY: By the type invariant, `self.as_raw()` is a valid `struct auxiliary_device`. let ptr = unsafe { (*self.as_raw()).registration_data_rust }; if ptr.is_null() { @@ -399,8 +399,9 @@ struct RegistrationData { /// This type represents the registration of a [`struct auxiliary_device`]. When its parent device /// is unbound, the corresponding auxiliary device will be unregistered from the system. /// -/// The type parameter `F` is a [`ForLt`](trait@ForLt) encoding of the registration -/// data type. For non-lifetime-parameterized types, use [`ForLt!(T)`](macro@ForLt). +/// The type parameter `F` is a [`CovariantForLt`](trait@CovariantForLt) encoding of the +/// registration data type. For non-lifetime-parameterized types, use +/// [`CovariantForLt!(T)`](macro@CovariantForLt). /// The data can be accessed by the auxiliary driver through [`Device::registration_data()`]. /// /// # Invariants @@ -408,12 +409,12 @@ struct RegistrationData { /// `self.adev` always holds a valid pointer to an initialized and registered /// [`struct auxiliary_device`] whose `registration_data_rust` field points to a /// valid `Pin>>>`. -pub struct Registration<'a, F: ForLt + 'static> { +pub struct Registration<'a, F: CovariantForLt + 'static> { adev: NonNull, _phantom: PhantomData>, } -impl<'a, F: ForLt> Registration<'a, F> +impl<'a, F: CovariantForLt> Registration<'a, F> where for<'b> F::Of<'b>: Send + Sync, { @@ -525,7 +526,7 @@ pub fn new( } } -impl Drop for Registration<'_, F> { +impl Drop for Registration<'_, F> { fn drop(&mut self) { // SAFETY: By the type invariant of `Self`, `self.adev.as_ptr()` is a valid registered // `struct auxiliary_device`. @@ -547,7 +548,7 @@ fn drop(&mut self) { } // SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread. -unsafe impl Send for Registration<'_, F> where for<'a> F::Of<'a>: Send {} +unsafe impl Send for Registration<'_, F> where for<'a> F::Of<'a>: Send {} // SAFETY: `Registration` does not expose any methods or fields that need synchronization. -unsafe impl Sync for Registration<'_, F> where for<'a> F::Of<'a>: Send {} +unsafe impl Sync for Registration<'_, F> where for<'a> F::Of<'a>: Send {} diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ac316fd7b538..cbe6907042d3 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -13,7 +13,7 @@ #[doc(hidden)] pub mod for_lt; -pub use for_lt::ForLt; +pub use for_lt::CovariantForLt; /// Used to transfer ownership to and from foreign (non-Rust) languages. /// diff --git a/rust/kernel/types/for_lt.rs b/rust/kernel/types/for_lt.rs index d44323c28e8d..ef510ab6c092 100644 --- a/rust/kernel/types/for_lt.rs +++ b/rust/kernel/types/for_lt.rs @@ -1,8 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT -//! Provide implementation and test of the `ForLt` trait and macro. +//! Provide implementation and test of the `CovariantForLt` trait and macro. //! -//! This module is hidden and user should just use `ForLt!` directly. +//! This module is hidden and user should just use `CovariantForLt!` directly. use core::marker::PhantomData; @@ -15,38 +15,39 @@ /// /// # Macro /// -/// It is not recommended to implement this trait directly. `ForLt!` macro is provided to obtain a -/// type that implements this trait. +/// It is not recommended to implement this trait directly. `CovariantForLt!` macro is provided to +/// obtain a type that implements this trait. /// /// The full syntax is /// /// ``` -/// # use kernel::types::ForLt; -/// # fn expect_lt() {} +/// # use kernel::types::CovariantForLt; +/// # fn expect_lt() {} /// # struct TypeThatUse<'a>(&'a ()); /// # expect_lt::< -/// ForLt!(for<'a> TypeThatUse<'a>) +/// CovariantForLt!(for<'a> TypeThatUse<'a>) /// # >(); /// ``` /// -/// which gives a type so that ` TypeThatUse<'a>) as ForLt>::Of<'b>` +/// which gives a type so that +/// ` TypeThatUse<'a>) as CovariantForLt>::Of<'b>` /// is `TypeThatUse<'b>`. /// /// You may also use a short-hand syntax which works similar to lifetime elision. /// The macro also accepts types that do not involve a lifetime at all. /// /// ``` -/// # use kernel::types::ForLt; -/// # fn expect_lt() {} +/// # use kernel::types::CovariantForLt; +/// # fn expect_lt() {} /// # struct TypeThatUse<'a>(&'a ()); /// # expect_lt::< -/// ForLt!(TypeThatUse<'_>) // Equivalent to `ForLt!(for<'a> TypeThatUse<'a>)`. +/// CovariantForLt!(TypeThatUse<'_>) // Equivalent to `CovariantForLt!(for<'a> TypeThatUse<'a>)`. /// # >(); /// # expect_lt::< -/// ForLt!(&u32) // Equivalent to `ForLt!(for<'a> &'a u32)`. +/// CovariantForLt!(&u32) // Equivalent to `CovariantForLt!(for<'a> &'a u32)`. /// # >(); /// # expect_lt::< -/// ForLt!(u32) // Equivalent to `ForLt!(for<'a> u32)`. +/// CovariantForLt!(u32) // Equivalent to `CovariantForLt!(for<'a> u32)`. /// # >(); /// ``` /// @@ -55,10 +56,10 @@ /// it. /// /// ```ignore,compile_fail -/// # use kernel::types::ForLt; -/// # fn expect_lt() {} +/// # use kernel::types::CovariantForLt; +/// # fn expect_lt() {} /// # expect_lt::< -/// ForLt!(fn(&u32)) // Contravariant, will fail compilation. +/// CovariantForLt!(fn(&u32)) // Contravariant, will fail compilation. /// # >(); /// ``` /// @@ -67,23 +68,23 @@ /// the generic parameter but is in a separate item. /// /// ``` -/// # use kernel::types::ForLt; -/// fn expect_lt() {} +/// # use kernel::types::CovariantForLt; +/// fn expect_lt() {} /// # #[allow(clippy::unnecessary_safety_comment, reason = "false positive")] /// fn generic_fn() { /// // Syntactically proven by the macro -/// expect_lt::(); +/// expect_lt::(); /// // Syntactically proven by the macro -/// expect_lt::)>(); +/// expect_lt::)>(); /// // Cannot be syntactically proven, need to check covariance of `KBox` -/// // expect_lt::)>(); +/// // expect_lt::)>(); /// } /// ``` /// /// # Safety /// /// `Self::Of<'a>` must be covariant over the lifetime `'a`. -pub unsafe trait ForLt { +pub unsafe trait CovariantForLt { /// The type parameterized by the lifetime. type Of<'a>: 'a; @@ -94,11 +95,11 @@ fn cast_ref<'r, 'short: 'r, 'long: 'short>(long: &'r Self::Of<'long>) -> &'r Sel unsafe { core::mem::transmute(long) } } } -pub use macros::ForLt; +pub use macros::CovariantForLt; -/// This is intended to be an "unsafe-to-refer-to" type. +/// Helper type for the `CovariantForLt!` macro. /// -/// Must only be used by the `ForLt!` macro. +/// Must only be used by the `CovariantForLt!` macro. /// /// `T` is the magic `dyn for<'a> WithLt<'a, TypeThatUse<'a>>` generated by macro. /// @@ -107,16 +108,16 @@ fn cast_ref<'r, 'short: 'r, 'long: 'short>(long: &'r Self::Of<'long>) -> &'r Sel /// `N` is to provide the macro a place to emit arbitrary items, in case it needs to prove /// additional properties. #[doc(hidden)] -pub struct UnsafeForLtImpl(PhantomData<(WF, T)>); +pub struct CovariantForLtImpl(PhantomData<(WF, T)>); -// This is a helper trait for implementation `ForLt` to be able to use HRTB. +// This is a helper trait for implementation `CovariantForLt` to be able to use HRTB. #[doc(hidden)] pub trait WithLt<'a> { type Of: 'a; } -// SAFETY: In `ForLt!` macro, a covariance proof is generated when naming `UnsafeForLtImpl` -// and it will fail to evaluate if the type is not covariant. -unsafe impl WithLt<'a>, WF> ForLt for UnsafeForLtImpl { +// SAFETY: In `CovariantForLt!` macro, a covariance proof is generated when naming +// `CovariantForLtImpl` and it will fail to evaluate if the type is not covariant. +unsafe impl WithLt<'a>, WF> CovariantForLt for CovariantForLtImpl { type Of<'a> = >::Of; } diff --git a/rust/macros/for_lt.rs b/rust/macros/for_lt.rs index 364d4113cd10..3cb094d00548 100644 --- a/rust/macros/for_lt.rs +++ b/rust/macros/for_lt.rs @@ -176,7 +176,7 @@ fn prove(&mut self, ty: &'a Type) { } } -pub(crate) fn for_lt(input: HigherRankedType) -> TokenStream { +pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream { let (ty, lifetime) = match input { HigherRankedType::Explicit { lifetime, ty, .. } => (ty, lifetime), HigherRankedType::Implicit { ty } => { @@ -235,7 +235,7 @@ fn #cov_proof_name<'__short, '__long: '__short>( ); quote!( - ::kernel::types::for_lt::UnsafeForLtImpl::< + ::kernel::types::for_lt::CovariantForLtImpl::< dyn for<#lifetime> ::kernel::types::for_lt::WithLt<#lifetime, Of = #ty>, #ty_static, { diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index 4a48fabbc268..2167cb270928 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -491,14 +491,13 @@ pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream { .into() } -/// Obtain a type that implements [`ForLt`] for the given higher-ranked type. +/// Obtain a type that implements [`CovariantForLt`] for the given higher-ranked type. /// -/// Please refer to the documentation of the [`ForLt`] trait. +/// Please refer to the documentation of the [`CovariantForLt`] trait. /// -/// [`ForLt`]: trait.ForLt.html +/// [`CovariantForLt`]: trait.CovariantForLt.html #[proc_macro] -// The macro shares the name with the trait. #[allow(non_snake_case)] -pub fn ForLt(input: TokenStream) -> TokenStream { - for_lt::for_lt(parse_macro_input!(input)).into() +pub fn CovariantForLt(input: TokenStream) -> TokenStream { + for_lt::covariant_for_lt(parse_macro_input!(input)).into() } diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs index 2c1351040e45..92ee6a6d348e 100644 --- a/samples/rust/rust_driver_auxiliary.rs +++ b/samples/rust/rust_driver_auxiliary.rs @@ -13,7 +13,7 @@ driver, pci, prelude::*, - types::ForLt, + types::CovariantForLt, InPlaceModule, // }; @@ -60,8 +60,8 @@ struct Data<'bound> { #[allow(clippy::type_complexity)] struct ParentData<'bound> { - _reg0: auxiliary::Registration<'bound, ForLt!(Data<'_>)>, - _reg1: auxiliary::Registration<'bound, ForLt!(Data<'_>)>, + _reg0: auxiliary::Registration<'bound, CovariantForLt!(Data<'_>)>, + _reg1: auxiliary::Registration<'bound, CovariantForLt!(Data<'_>)>, } kernel::pci_device_table!( @@ -115,7 +115,7 @@ fn probe<'bound>( impl ParentDriver { fn connect(adev: &auxiliary::Device) -> Result { - let data = adev.registration_data::)>()?; + let data = adev.registration_data::)>()?; let pdev = data.parent; dev_info!( -- 2.54.0