From nobody Mon Oct 6 20:43:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D1B3223E34C; Thu, 17 Jul 2025 22:48:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752792500; cv=none; b=I3wJ2BUaLUJXOOhSRsxwBlrPJcDf5MBjeYuvka846BzrTQZmuhOMovK0x6aDIj+SMZTbrIFBiJC7AWdlUOJ2n/mSAQvGLXHnQumKsMLP2+/UOl3T9Ow02ByIpfEcLORKmOKR/Vpmqi4qC0rneDRaCVGAqnJnHPo6qK6DWu9iq4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752792500; c=relaxed/simple; bh=NSomQBe9GsxuecBlz9tHDynvoC2dGHK58amK9Fm823s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JcvYc76hterUw5F0MBRbX5D1EjcD2wM3PCziyBfE4ST5ThcFvaxG66A/9hG6i/vJy1CXBUyXl3UXQgE56jTUzf+4nu3Fxs9BrkatOAUp6tAPeJBW/OwcK5OOlAQgjkXMvpTEQ3Hbc/XOnXe76mOg60tqR8F4iJvEpqVzbhC5U1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mGHsz+tV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mGHsz+tV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82BAFC4CEEB; Thu, 17 Jul 2025 22:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752792500; bh=NSomQBe9GsxuecBlz9tHDynvoC2dGHK58amK9Fm823s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mGHsz+tVPYdLfAclOygHZ9kC16UypYZ+dhDHjQVv/mGFz1YvotwRE5miTK+xUTF77 uMkBfag8qLryEBxHTH9cHUp/CLUah/CDJ4cuLBN97KY71W8M9eJYEQqmnhxKLkDO9R YTO3dEbSD+G+hHCbBNwnDfcXgyuxa/NOWkMRVFfMUzhK3aHIZpJFoxlEM8BijKEgGq PVn20sL0BxyYhTTjBIENEflrrAvKjWXtFDqcvUc4rXDQ0cjAoyM+CerBAcGvUq/gH1 J7a4VSrqhMtjucGsq5/M945JAvdmQkeMo9pevVbx2wyjTNoOx9NmZuZXtdqh6V/KZw pxfPvKEvBOp4A== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 1/3] device: rust: documentation for DeviceContext Date: Fri, 18 Jul 2025 00:45:37 +0200 Message-ID: <20250717224806.54763-2-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250717224806.54763-1-dakr@kernel.org> References: <20250717224806.54763-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand the documentation around DeviceContext states and types, in order to provide detailed information about their purpose and relationship with each other. Signed-off-by: Danilo Krummrich --- rust/kernel/device.rs | 63 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index ca82926fd67f..d7ac56628fe5 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -311,28 +311,69 @@ unsafe impl Send for Device {} // synchronization in `struct device`. unsafe impl Sync for Device {} =20 -/// Marker trait for the context of a bus specific device. +/// Marker trait for the context or scope of a bus specific device. /// -/// Some functions of a bus specific device should only be called from a c= ertain context, i.e. bus -/// callbacks, such as `probe()`. +/// [`DeviceContext`] is a marker trait for structures representing the co= ntext of a bus specific +/// [`Device`]. /// -/// This is the marker trait for structures representing the context of a = bus specific device. +/// The specific device context types are: [`CoreInternal`], [`Core`], [`B= ound`] and [`Normal`]. +/// +/// [`DeviceContext`] types are hierarchical, which means that there is a = strict hierarchy that +/// defines which [`DeviceContext`] type can be derived from another. For = instance, any +/// [`Device`] can dereference to a [`Device`]. +/// +/// The following enunumeration illustrates the dereference hierarchy of [= `DeviceContext`] types. +/// +/// - [`CoreInternal`] =3D> [`Core`] =3D> [`Bound`] =3D> [`Normal`] +/// - [`Core`] =3D> [`Bound`] =3D> [`Normal`] +/// - [`Bound`] =3D> [`Normal`] +/// - [`Normal`] +/// +/// Bus devices can automatically implement the dereference hierarchy by u= sing +/// [`impl_device_context_deref`](kernel::impl_device_context_deref). pub trait DeviceContext: private::Sealed {} =20 -/// The [`Normal`] context is the context of a bus specific device when it= is not an argument of -/// any bus callback. +/// The [`Normal`] context is the default [`DeviceContext`] of any [`Devic= e`]. +/// +/// The normal context does not indicate any specific scope. Any `Device` is also a valid +/// [`Device`]. It is the only [`DeviceContext`] for which it is v= alid to implement +/// [`AlwaysRefCounted`](kernel::types::AlwaysRefCounted) for. pub struct Normal; =20 -/// The [`Core`] context is the context of a bus specific device when it i= s supplied as argument of -/// any of the bus callbacks, such as `probe()`. +/// The [`Core`] context is the context of a bus specific device when it a= ppears as argument of +/// any bus specific callback, such as `probe()`. +/// +/// The core context indicates that the [`Device`] reference's scope= is limited to the bus +/// callback it appears in. It is intended to be used for synchronization = purposes. Bus device +/// implementations can implement methods for [`Device`], such that = they can only be called +/// from bus callbacks. pub struct Core; =20 -/// Semantically the same as [`Core`] but reserved for internal usage of t= he corresponding bus +/// Semantically the same as [`Core`], but reserved for internal usage of = the corresponding bus /// abstraction. +/// +/// The internal core context is intended to be used in exactly the same w= ay as the [Core] context, +/// with the difference that this [`DeviceContext`] is internal to the cor= responding bus +/// abstraction. +/// +/// This context mainly exists to share generic [`Device`] infrastructure = that should only be called +/// from bus callbacks with bus abstractions, but without making them acce= ssible for drivers. pub struct CoreInternal; =20 -/// The [`Bound`] context is the context of a bus specific device referenc= e when it is guaranteed to -/// be bound for the duration of its lifetime. +/// The [`Bound`] context is the [`DeviceContext`] of a bus specific devic= e when it is guaranteed to +/// be bound to a driver. +/// +/// The bound context indicates that for the entire duration of the lifeti= me of a [`Device`] +/// reference, the [`Device`] is guaranteed to be bound to a driver. +/// +/// Some APIs, such as [`dma::CoherentAllocation`](kernel::dma::CoherentAl= location) or +/// [`Devres`](kernel::devres::Devres) rely on the [`Device`] to be bound,= which can be proven with +/// the [`Bound`] device context. +/// +/// Any abstraction that can guarantee a scope where the corresponding bus= device is bound, should +/// provide a [`Device`] reference to its users for this scope. Thi= s allows users to benefit +/// from optimizations for accessing device resources, see also +/// [Devres::access](kernel::devres::Devres::access). pub struct Bound; =20 mod private { --=20 2.50.0