From patchwork Sun Apr 13 17:36:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049371 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 ED1B41A0711; Sun, 13 Apr 2025 17:38:09 +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=1744565890; cv=none; b=WzxkP87hCKd53N387tzbmWJo3y34G6IMsn/1G/q0ja6VdTi+6lnM86bZThL9swvOMOxd5i83ljgPQ1T9mCB8IoBz6aifVmIF+0fj0rD4p1QVa6aCXwvrlX0YIIOH5AGvCaNOc4MrVMRzPSqIk21WXpMoRk7cgwoNTDwvqpZdB9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565890; c=relaxed/simple; bh=Lp95DzubEoanpikgjlhKpvhsatoB28z6K99cNgw02kE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=isrERo8E9Yh0yYNOSYORoncIKDAoWDbbnrY/CnbDPrB0ojc+SUSXiPVuHhhc0YWCvlTMn+BNiFQgI14gNN9NElawoyiRIZK8OxHkC4EN3UrWAOCqGxHzJmo+JN8js6Sldk+I9eD8SYWLRsuiWdRY7PBudrktEVVnDGVvGbIw29I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HSpehm4j; 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="HSpehm4j" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14F65C4CEEC; Sun, 13 Apr 2025 17:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565889; bh=Lp95DzubEoanpikgjlhKpvhsatoB28z6K99cNgw02kE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HSpehm4jXfYAIeWh1yAB4yRcxihPnRgbeqpUza2oB14Y5bLbvjjBIErCJq8cvbHFs HWGejaIjT1Xhr1AGK8UJFDCETlDztxYPC+rTcJfqAchKI8oUsObfYNyptqy7SvUdRc Toa3N9I8faIPJrio1GgkJ2LYwZuW9yCqo5m2xnZ/wubfrRuj04WL73fCAPATw1qgIZ r9izjrgP8tS1YdqXfBTcRNvPRYg+V81fQ6i7bamsJsODpRQDhSmie/2npDXQJPXgCy 8RyZIcf0kjZXRJ8r6mbFnGYpbYbeXFixcMwuPw09Z5RiC95Yrv1bZSPzAmSo9ySmN8 ztZBoXG+xWyqA== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 1/9] rust: device: implement impl_device_context_deref! Date: Sun, 13 Apr 2025 19:36:56 +0200 Message-ID: <20250413173758.12068-2-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 The Deref hierarchy for device context generics is the same for every (bus specific) device. Implement those with a generic macro to avoid duplicated boiler plate code and ensure the correct Deref hierarchy for every device implementation. Co-developed-by: Benno Lossin Signed-off-by: Benno Lossin Signed-off-by: Danilo Krummrich Reviewed-by: Christian Schrefl --- rust/kernel/device.rs | 44 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/pci.rs | 16 +++------------ rust/kernel/platform.rs | 17 +++------------- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 21b343a1dc4d..7cb6f0fc005d 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -235,6 +235,50 @@ impl Sealed for super::Normal {} impl DeviceContext for Core {} impl DeviceContext for Normal {} +/// # Safety +/// +/// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the +/// generic argument of `$device`. +#[doc(hidden)] +#[macro_export] +macro_rules! __impl_device_context_deref { + (unsafe { $device:ident, $src:ty => $dst:ty }) => { + impl ::core::ops::Deref for $device<$src> { + type Target = $device<$dst>; + + fn deref(&self) -> &Self::Target { + let ptr: *const Self = self; + + // CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the + // safety requirement of the macro. + let ptr = ptr.cast::(); + + // SAFETY: `ptr` was derived from `&self`. + unsafe { &*ptr } + } + } + }; +} + +/// Implement [`core::ops::Deref`] traits for allowed [`DeviceContext`] conversions of a (bus +/// specific) device. +/// +/// # Safety +/// +/// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the +/// generic argument of `$device`. +#[macro_export] +macro_rules! impl_device_context_deref { + (unsafe { $device:ident }) => { + // SAFETY: This macro has the exact same safety requirement as + // `__impl_device_context_deref!`. + kernel::__impl_device_context_deref!(unsafe { + $device, + $crate::device::Core => $crate::device::Normal + }); + }; +} + #[doc(hidden)] #[macro_export] macro_rules! dev_printk { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index c97d6d470b28..8474608e7a90 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -422,19 +422,9 @@ pub fn set_master(&self) { } } -impl Deref for Device { - type Target = Device; - - fn deref(&self) -> &Self::Target { - let ptr: *const Self = self; - - // CAST: `Device` is a transparent wrapper of `Opaque`. - let ptr = ptr.cast::(); - - // SAFETY: `ptr` was derived from `&self`. - unsafe { &*ptr } - } -} +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic +// argument. +kernel::impl_device_context_deref!(unsafe { Device }); impl From<&Device> for ARef { fn from(dev: &Device) -> Self { diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 4917cb34e2fe..22590bdff7bb 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -16,7 +16,6 @@ use core::{ marker::PhantomData, - ops::Deref, ptr::{addr_of_mut, NonNull}, }; @@ -190,19 +189,9 @@ fn as_raw(&self) -> *mut bindings::platform_device { } } -impl Deref for Device { - type Target = Device; - - fn deref(&self) -> &Self::Target { - let ptr: *const Self = self; - - // CAST: `Device` is a transparent wrapper of `Opaque`. - let ptr = ptr.cast::(); - - // SAFETY: `ptr` was derived from `&self`. - unsafe { &*ptr } - } -} +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic +// argument. +kernel::impl_device_context_deref!(unsafe { Device }); impl From<&Device> for ARef { fn from(dev: &Device) -> Self { From patchwork Sun Apr 13 17:36:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049372 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 7A4F91A0711; Sun, 13 Apr 2025 17:38:14 +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=1744565894; cv=none; b=c+Von5ffs+04vM7TbHG7qEhJHXMBgeU9OpHSsMx9a4VDKGHHy3nV0Zds3qvSobPU0H5rYMvZkLg9GwqFXJwD1csjsGY7sF2cqfgcAbelcSBs+vZ0rlBWEdRazPMQvHt0+n0rs9JdE/34kCFnQuH76Sc+nRH4ArjA9vH5EmvVY2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565894; c=relaxed/simple; bh=gLqLj4ZQ3X469fEAy6+OfUy9C5xFQ/xnWlLGjxKPDdM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QA+AO+5v0r3WngV/fpxIX7Z7eaejk1s+MZCaQEEs6PwoDq161k/iQW3dxQ4Gy5RNEG7snKe9fJHATXLrlMfvwbAejaGB0ldq0K4/kGP5SXyOIABprqQnOHTP3A9yIriboEnBiioJCWgMPtBzE3BxVI2qV8Xscqkv8wrugwINtGg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PqNoY+1S; 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="PqNoY+1S" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D5FCC4CEDD; Sun, 13 Apr 2025 17:38:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565894; bh=gLqLj4ZQ3X469fEAy6+OfUy9C5xFQ/xnWlLGjxKPDdM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PqNoY+1Sbb8ws1gk+lhHsYus7extZBWNS0adWIh8h2kkORY1cd9KsZHi9+chPh4ZO r8v59lAfsL1jiH583LXSzfx2+zPPbxpQyhjEBA0z6gq9s1s2Zh3XN/8SSqlxGnnNdk n0Dp4brGhoAtmZGcAOKvhwS7wjOle7ize6Vvbn5R7XYoM5w1PdnkWBA/ccgQmdK48G 1J1pO70YtahXPemE0DBX4fFnytt0ML+YfuQYIvnJvbLzNYjRArFyg/8s+Ce5KQwf4p UcwGtrFAHf2G4qWVk1UZsU38fY6Orspp7W5Wiq4hIGAeSNG+gbJ0+0hmeSp9Iu43xR FcOkY/p1/yl+g== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 2/9] rust: device: implement impl_device_context_into_aref! Date: Sun, 13 Apr 2025 19:36:57 +0200 Message-ID: <20250413173758.12068-3-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Implement a macro to implement all From conversions of a certain device to ARef. This avoids unnecessary boiler plate code for every device implementation. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/device.rs | 21 +++++++++++++++++++++ rust/kernel/pci.rs | 7 +------ rust/kernel/platform.rs | 9 ++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 7cb6f0fc005d..26e71224460b 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -279,6 +279,27 @@ macro_rules! impl_device_context_deref { }; } +#[doc(hidden)] +#[macro_export] +macro_rules! __impl_device_context_into_aref { + ($src:ty, $device:tt) => { + impl core::convert::From<&$device<$src>> for $crate::types::ARef<$device> { + fn from(dev: &$device<$src>) -> Self { + (&**dev).into() + } + } + }; +} + +/// Implement [`core::convert::From`], such that all `&Device` can be converted to an +/// `ARef`. +#[macro_export] +macro_rules! impl_device_context_into_aref { + ($device:tt) => { + kernel::__impl_device_context_into_aref!($crate::device::Core, $device); + }; +} + #[doc(hidden)] #[macro_export] macro_rules! dev_printk { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8474608e7a90..7c6ec05383e0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -425,12 +425,7 @@ pub fn set_master(&self) { // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); - -impl From<&Device> for ARef { - fn from(dev: &Device) -> Self { - (&**dev).into() - } -} +kernel::impl_device_context_into_aref!(Device); // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 22590bdff7bb..9476b717c425 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -10,7 +10,7 @@ of, prelude::*, str::CStr, - types::{ARef, ForeignOwnable, Opaque}, + types::{ForeignOwnable, Opaque}, ThisModule, }; @@ -192,12 +192,7 @@ fn as_raw(&self) -> *mut bindings::platform_device { // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic // argument. kernel::impl_device_context_deref!(unsafe { Device }); - -impl From<&Device> for ARef { - fn from(dev: &Device) -> Self { - (&**dev).into() - } -} +kernel::impl_device_context_into_aref!(Device); // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { From patchwork Sun Apr 13 17:36:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049373 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 AFD031F37D4; Sun, 13 Apr 2025 17:38:18 +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=1744565900; cv=none; b=TGe8/N65j5LGxUvDEAlc1RqG8DRUZDkLQL4zqT2bo7HrPbb9ehOabDy+YUuz+r7zoTCc8yU6nt/Qy0rG59IaM3KmeH+IzjF6wSYkKAcYKyqak7At/diFoB2VRCjrKFYVuS1kJ5gKhvTX6oX2/iGch7ItVO5KA+brrM7BM/uIE40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565900; c=relaxed/simple; bh=ZI5lPPz6yGBCSm+t0Bi5DUr3i8+J68407FIIDtD2wsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kW5xTZGyee3ZqPXwU7Z4Kzh4jwQC2OAPEq1hN9pBpaNUqNuoStmg+cP6rYgW2hANKlNJWTsa+0h3BPHP1E+0IMTPO8YRMgegwpvlf+kupktJxr6TZw9ctPvsu9Y2Yjqi7ZJzGIXgLOn7YsN1mMKF15pr30xPrISVamczN2pXfHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u6Xqpy0z; 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="u6Xqpy0z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A19C4CEE7; Sun, 13 Apr 2025 17:38:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565898; bh=ZI5lPPz6yGBCSm+t0Bi5DUr3i8+J68407FIIDtD2wsQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u6Xqpy0zLvOISYx/FpFw7Gf+A73MLZ+lPCnW91qLe+Iuj/pDnZF8IkbxVT8i9GjPU cWruFqokFVboAecNLyS2aW0XB1k00lzZji2X8GmYc4PhvJ2lPCOPgCE4Rz7oFSp4fo s7semkyUPjYu0Kj7nYpx9xokee7G2F5N+nlOou9IAYrA1dNWRcIW/rXdkkh5uHjxYi 84kMy7DqbdtIvLr/zHipZCWws10MccIdT7bbhTqxqZmCYH1Qss7L0FReFMLjgCVQBi l7Fd3mp4+7+H8e9XyHThq5+w5j/dixyFk+GrMpS41vUmn6/agTyuiAqPlUAKqjKNYz qTgyiNe9DoO0Q== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 3/9] rust: device: implement device context for Device Date: Sun, 13 Apr 2025 19:36:58 +0200 Message-ID: <20250413173758.12068-4-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Analogous to bus specific device, implement the DeviceContext generic for generic devices. This is used for APIs that work with generic devices (such as Devres) to evaluate the device' context. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/device.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 26e71224460b..487211842f77 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -9,7 +9,7 @@ str::CStr, types::{ARef, Opaque}, }; -use core::{fmt, ptr}; +use core::{fmt, marker::PhantomData, ptr}; #[cfg(CONFIG_PRINTK)] use crate::c_str; @@ -42,7 +42,7 @@ /// `bindings::device::release` is valid to be called from any thread, hence `ARef` can be /// dropped from any thread. #[repr(transparent)] -pub struct Device(Opaque); +pub struct Device(Opaque, PhantomData); impl Device { /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer. @@ -59,7 +59,9 @@ pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef { // SAFETY: By the safety requirements ptr is valid unsafe { Self::as_ref(ptr) }.into() } +} +impl Device { /// Obtain the raw `struct device *`. pub(crate) fn as_raw(&self) -> *mut bindings::device { self.0.get() @@ -189,6 +191,11 @@ pub fn property_present(&self, name: &CStr) -> bool { } } +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic +// argument. +kernel::impl_device_context_deref!(unsafe { Device }); +kernel::impl_device_context_into_aref!(Device); + // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { fn inc_ref(&self) { From patchwork Sun Apr 13 17:36:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049374 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 8B7171F3BA3; Sun, 13 Apr 2025 17:38:22 +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=1744565902; cv=none; b=hl4ptVEwpvtZaL/BqVQ3/fIW6gNNO9p+3n9BQF+eSyLs20CLSzr7PWnLJKXf3FhYmmBivgbgL5a4L4uAxxR4RBqs699Iw/n4bpJxwVINcmSel6GPqWVLT3h0Q4qVZf59cRT/VGT9s9nQYvCnXo38hBTJPrVfYtFyhHyNRZ5H/HI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565902; c=relaxed/simple; bh=EzfjRnRLuRUm/3m/Zp071bKg/YimuUsvy/AL0I0smyo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oBK/G0+K5BnAOyW1OJcPyTwIgspDJqilXrCR2p0NiMhPeCV3nF858N4oetyqOsH5R1UU6lBI+abxkwelK38iRrgA0UUlGbV+jSHeERfSUXTweD1CEKlu4AayDxe/rSDNzIvSbuv5qmS7QN4uy9ogqOtBqr2IOpZPa46l4VM2ttM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t+2FbAIW; 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="t+2FbAIW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90EE3C4CEDD; Sun, 13 Apr 2025 17:38:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565902; bh=EzfjRnRLuRUm/3m/Zp071bKg/YimuUsvy/AL0I0smyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t+2FbAIWMtf86jRA/jNlfWou4L6cPe0rx6+u8buw09YAHyjgpFx2/Dj3eeo9hRjtU qWwnj/wHPKH9slBHXK0R3gppz4t7Wmxa6pvsQPSMt5egQ/fGld3ofgfritdMaq9eGW CFjvri5aSGpWCSpwy8WgIU/j4+1tPvhPDcRqIVR+KPZul4cvplZIyQj+4YsfBaX+Mx i5Qa3+7SUNhXa8U3RdhpA4kz9OTuRmIR7B6zJbT9Y6bfuBi0QMqPKnqKJ1pHGPoEWC gA/YYibZGDPN8JLtyu2xCKyb91hVRNy7xWiuy7wwcD4KaBXUdn/DNJpnjd+wPHmMGn Q0YqpDWbMQqTA== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 4/9] rust: platform: preserve device context in AsRef Date: Sun, 13 Apr 2025 19:36:59 +0200 Message-ID: <20250413173758.12068-5-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Since device::Device has a generic over its context, preserve this device context in AsRef. For instance, when calling platform::Device the new AsRef implementation returns device::Device. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/platform.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 9476b717c425..b1c48cd95cd6 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -183,7 +183,7 @@ pub struct Device( PhantomData, ); -impl Device { +impl Device { fn as_raw(&self) -> *mut bindings::platform_device { self.0.get() } @@ -207,8 +207,8 @@ unsafe fn dec_ref(obj: NonNull) { } } -impl AsRef for Device { - fn as_ref(&self) -> &device::Device { +impl AsRef> for Device { + fn as_ref(&self) -> &device::Device { // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid // `struct platform_device`. let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) }; From patchwork Sun Apr 13 17:37:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049375 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 4E4D21F3BBA; Sun, 13 Apr 2025 17:38:26 +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=1744565907; cv=none; b=f8pvyqPBuUPi1/fm76l1/3pZneIxBVUlpeEGni6OWy5DlYwkn5n+vlk53CmP52CiowDPXl1Ih6yyWUbArhxk2+q4x/nvnlNS0MYP8mYLbF8PGUaEabuubUl66iYqxzMruQEjp0g+OZLDiANusni5wYwsZBpSp/E75K9Cddt9Y/U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565907; c=relaxed/simple; bh=ZfSAqbjxYp7micRXjG3/lmmGYU5+RMEI2eUyLbtRx9k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZsrPAo/JDtfglgIx//GOdFsWJh4+gw26KBeewpVlvGA0kDXmA3i3I/ge0cooc5bLMAns6xLpDuel53PNgY955UuCvX7/owQCSqgJY8OJOA+oORd9/80U5zrw0DVN4j9DU+sxaY8DDA+ulLHQN4FOvFeRcsxBhS12N1R6VfTloCg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Nh1MglMN; 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="Nh1MglMN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9473C4CEDD; Sun, 13 Apr 2025 17:38:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565906; bh=ZfSAqbjxYp7micRXjG3/lmmGYU5+RMEI2eUyLbtRx9k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nh1MglMNjJW0q03MiD2PsiIL4/2M+aFP/y2YWN2sMmYXzHwYbO1zDw5MEkodleBu0 +4NNJERg3uthu2dNclq/JpaIbivx9tBmIiCXkInlgyHeuIuiAP+XnElP9q1+7DeMX/ 4ZGibik5HzCIF7w7UOeJkPMZNEaesOzlYjLUyO1X7p8bz3LedYoVQWs6eTou2yEQZ7 5VNdezyW7RYsKkz0CdJqh078gWFU9ZfVnWWSJ+GEwj3n+vMTvhKT+Js/WSt6zT123v 20Asbsb8k3Gpr9KR+YujgeBpE7Pc5Bzg/kMI2U09i2M2MhkcEMQ5i2UY1SytaDRw+Y v1DtYJVP1ymHw== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 5/9] rust: pci: preserve device context in AsRef Date: Sun, 13 Apr 2025 19:37:00 +0200 Message-ID: <20250413173758.12068-6-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Since device::Device has a generic over its context, preserve this device context in AsRef. For instance, when calling pci::Device the new AsRef implementation returns device::Device. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/pci.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 7c6ec05383e0..1234b0c4a403 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -360,11 +360,13 @@ fn deref(&self) -> &Self::Target { } } -impl Device { +impl Device { fn as_raw(&self) -> *mut bindings::pci_dev { self.0.get() } +} +impl Device { /// Returns the PCI vendor ID. pub fn vendor_id(&self) -> u16 { // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. @@ -440,8 +442,8 @@ unsafe fn dec_ref(obj: NonNull) { } } -impl AsRef for Device { - fn as_ref(&self) -> &device::Device { +impl AsRef> for Device { + fn as_ref(&self) -> &device::Device { // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid // `struct pci_dev`. let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) }; From patchwork Sun Apr 13 17:37:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049376 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 2A96F1F3BBA; Sun, 13 Apr 2025 17:38:30 +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=1744565911; cv=none; b=g2Gk3farAEsSJgZUzx7P34COkRDFKaPzD33KCpd0J6w/EBQhxDijpVbb0C6sKtE0j7my03nQZfcRMThq3iFThUAK8tdIVlNUd9zbG5G8Vg66iIg+4VKpqh4iG+52WV8rwNpHmHe/tNjCMkjiMIt3gh7dZt2P2SoW7c5S/IfmJGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565911; c=relaxed/simple; bh=rK25OVJkr0b3s8LIVrUtyOi++yPYNHI2cdGfCMqMA5k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BU8Nk1o4N97mU3LAdaXKoJMGzt0SWvpQpC6Q/AJKvXVFYxadCratvKYhkQZ7+ix/5iUs7RH1grMg4++sGFawbdRTIAY97Xn8YkoL/2ToKBeMyRDZkBnPD7I8D6lMz6CMkc65KMfd4FF40tNlCNrNKBe7du/1D6GScz0/wmelJRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=msAHhgy6; 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="msAHhgy6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFFF8C4CEE7; Sun, 13 Apr 2025 17:38:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565910; bh=rK25OVJkr0b3s8LIVrUtyOi++yPYNHI2cdGfCMqMA5k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=msAHhgy6WB40bIOajhgw3qtYYamXZ5RLmXknsxaC8s7425VFc78lAX9rJZfWsjb1d DD+gDCkF6SZscet4h89ViFUUvhXVgvSry2Li030aningab+hbyCH8mPMyVAdCxS07e uNwaV27p9JVZNP0TWXrXRiIpmENApTWztLZrDJRp09P6k91N06eAzNxNuUW0m1y69A X8Pn4B4PPodR2IzRrb+dt/CU+ngtJLYvyDCUacG47nQyTvHFqKJCzJcscM1XI+xul1 JMSfZ3LGuR18GB937IRtvfsAx0AQmtoBuAQW8YlEdlY4jyK4T0U3iKZgIbVwj+SBqX Npvi8vbM9sf5g== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 6/9] rust: device: implement Bound device context Date: Sun, 13 Apr 2025 19:37:01 +0200 Message-ID: <20250413173758.12068-7-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 The Bound device context indicates that a device is bound to a driver. It must be used for APIs that require the device to be bound, such as Devres or dma::CoherentAllocation. Implement Bound and add the corresponding Deref hierarchy, as well as the corresponding ARef conversion for this device context. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/device.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 487211842f77..585a3fcfeea3 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -232,13 +232,19 @@ pub trait DeviceContext: private::Sealed {} /// any of the bus callbacks, such as `probe()`. pub struct Core; +/// The [`Bound`] context is the context of a bus specific device reference when it is guranteed to +/// be bound for the duration of its lifetime. +pub struct Bound; + mod private { pub trait Sealed {} + impl Sealed for super::Bound {} impl Sealed for super::Core {} impl Sealed for super::Normal {} } +impl DeviceContext for Bound {} impl DeviceContext for Core {} impl DeviceContext for Normal {} @@ -281,7 +287,14 @@ macro_rules! impl_device_context_deref { // `__impl_device_context_deref!`. kernel::__impl_device_context_deref!(unsafe { $device, - $crate::device::Core => $crate::device::Normal + $crate::device::Core => $crate::device::Bound + }); + + // SAFETY: This macro has the exact same safety requirement as + // `__impl_device_context_deref!`. + kernel::__impl_device_context_deref!(unsafe { + $device, + $crate::device::Bound => $crate::device::Normal }); }; } @@ -304,6 +317,7 @@ fn from(dev: &$device<$src>) -> Self { macro_rules! impl_device_context_into_aref { ($device:tt) => { kernel::__impl_device_context_into_aref!($crate::device::Core, $device); + kernel::__impl_device_context_into_aref!($crate::device::Bound, $device); }; } From patchwork Sun Apr 13 17:37:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049377 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 46E1C1F3FC0; Sun, 13 Apr 2025 17:38:34 +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=1744565915; cv=none; b=epnAJHHOFNnK+bT/LJHnnZQ16JprLetc8qVd7H1UvsYND+34WENTO2tVFZ5qqMJskUzka+fcfGTIw69moJyAj9ywSjVI3AqSvAwOahV5R1iA8lckt5yNLlfE/vdohX+mLU35mL1Epqs09weYsdGHCU/rI0dEbnPLA/OrWpZa2Os= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565915; c=relaxed/simple; bh=fkEeseR9PtCDZJ7E32DzZR1VXQCCuTAmIsu3dUC+V8I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G5PjvUgVFkqa9wQPHkB2bdCTJtjCwlgBOjoLr/gI2ajOwcSGJmRfUc15jxdUvPVKwPM+DY01w49JmJzt9vyKAuffqjJcPpIQNQSo+g5z575lxwUF80zBOiQplXS+Pb3GKNrXY6ASca7MF055JFXaPu20Trwt+muNJeV8hTAmpqk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=czTlta5D; 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="czTlta5D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 133E0C4CEDD; Sun, 13 Apr 2025 17:38:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565914; bh=fkEeseR9PtCDZJ7E32DzZR1VXQCCuTAmIsu3dUC+V8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=czTlta5DGrOUljqHgiTfzHhqbRYBnYnUVGEdplKN9m+GcYVq86rOJ7tIjXtNN+iL4 sW05mecavIWTV0TElJwCn1V/jj85hfFjb6Ez41EwsIuBVUNeYlPNgokCOdAASDZFQu 6/GOKKDgXA801XdeMOwii+pwTEBsCGW1wD4bTqd5eIfzt360MKjxffZyQbPGRRRqQ8 hXljhB8x3jPTBlARhbHqNlcn4Zvml8S9YiQ9NcrcGa3Td9tHO+r7MyoZ30gh0OpyEy GaFdq5tOAwvEAH+Iwyhi3a218pgvQAIOr/DqGeJy85S3HpSrufX9wMXC7K2nf4xqoi oBVsHBQcWqQyg== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 7/9] rust: pci: move iomap_region() to impl Device Date: Sun, 13 Apr 2025 19:37:02 +0200 Message-ID: <20250413173758.12068-8-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Require the Bound device context to be able to call iomap_region() and iomap_region_sized(). Creating I/O mapping requires the device to be bound. Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/pci.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 1234b0c4a403..3664d35b8e79 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -390,7 +390,9 @@ pub fn resource_len(&self, bar: u32) -> Result { // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`. Ok(unsafe { bindings::pci_resource_len(self.as_raw(), bar.try_into()?) }) } +} +impl Device { /// Mapps an entire PCI-BAR after performing a region-request on it. I/O operation bound checks /// can be performed on compile time for offsets (plus the requested type size) < SIZE. pub fn iomap_region_sized( From patchwork Sun Apr 13 17:37:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049378 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 172E31F3FEE; Sun, 13 Apr 2025 17:38:39 +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=1744565919; cv=none; b=aLA0cTl7ByIJrU1Bd55bKbRszXEg5dFowzhTjtiIx20ecV+dIYopZoqwtAh3vuJXfsvH+MBEfnQs2raSvAqyPIdo4KauSzXi5Lq6Oioojt8+2cyU+OMocCAA+LhYYAASkL6UN+IlA4sYh3aSa3mBjcozQYXhZHMz1PmOOU5ZvZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565919; c=relaxed/simple; bh=pQKXbW5g5QATkdun2XLx2iYsIxArlMlNIpZWyKcpnYU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=czObZT4qZKDfPgQ9zW4rAN0a9UWypWpT+dMsX/K+mnX7/cHA0GgAVd8Fbc6hOgWWShPmyuwyouamFN3sREV/E42pyaHrNIq7yc9LNgidRhfKhjEBSJRZTXyvTyjPyet+A96VpAtF1zriLGMvYT068AXBpKHojS2WcCeZKehWzKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=H8/tOA95; 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="H8/tOA95" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B3DEC4CEE7; Sun, 13 Apr 2025 17:38:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565919; bh=pQKXbW5g5QATkdun2XLx2iYsIxArlMlNIpZWyKcpnYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H8/tOA95aSQfxIogv4/Zyeqi9p2IAKsLr0Nm3f40NfQ0Mlf3idRBDinR8OHvirpOK Ie/UTThPY+e4YgmC8ciaU5+oi+fV/y4l5VZvfjfrgyqjGiMubzMo9OyPYjwRWnDA2n hDahPowRfj51IdPAxI/ZQ4KrF01AIeC4J6NN+YuRY7w8VhJ8k1+5jOEAKf4x9uiHar 1qpNtFQOUbMe+t1EUgxzgSbM2o84/BYBAjCK7U7LmqE6u9b1i2oCgSS0AcSMnclMMi 8rgPDY8JBHCAhi7AKYGXfgYYVVctmSdwQ49ihF7Km8tLFHYSRtbYUO15Diqel0JDC7 Ou2pdWP0la0wA== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 8/9] rust: devres: require a bound device Date: Sun, 13 Apr 2025 19:37:03 +0200 Message-ID: <20250413173758.12068-9-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Require the Bound device context to be able to a new Devres container. This ensures that we can't register devres callbacks for unbound devices. Signed-off-by: Danilo Krummrich --- rust/kernel/devres.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index ddb1ce4a78d9..1e58f5d22044 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -8,7 +8,7 @@ use crate::{ alloc::Flags, bindings, - device::Device, + device::{Bound, Device}, error::{Error, Result}, ffi::c_void, prelude::*, @@ -45,7 +45,7 @@ struct DevresInner { /// # Example /// /// ```no_run -/// # use kernel::{bindings, c_str, device::Device, devres::Devres, io::{Io, IoRaw}}; +/// # use kernel::{bindings, c_str, device::{Bound, Device}, devres::Devres, io::{Io, IoRaw}}; /// # use core::ops::Deref; /// /// // See also [`pci::Bar`] for a real example. @@ -83,13 +83,10 @@ struct DevresInner { /// unsafe { Io::from_raw(&self.0) } /// } /// } -/// # fn no_run() -> Result<(), Error> { -/// # // SAFETY: Invalid usage; just for the example to get an `ARef` instance. -/// # let dev = unsafe { Device::get_device(core::ptr::null_mut()) }; -/// +/// # fn no_run(dev: &Device) -> Result<(), Error> { /// // SAFETY: Invalid usage for example purposes. /// let iomem = unsafe { IoMem::<{ core::mem::size_of::() }>::new(0xBAAAAAAD)? }; -/// let devres = Devres::new(&dev, iomem, GFP_KERNEL)?; +/// let devres = Devres::new(dev, iomem, GFP_KERNEL)?; /// /// let res = devres.try_access().ok_or(ENXIO)?; /// res.write8(0x42, 0x0); @@ -99,7 +96,7 @@ struct DevresInner { pub struct Devres(Arc>); impl DevresInner { - fn new(dev: &Device, data: T, flags: Flags) -> Result>> { + fn new(dev: &Device, data: T, flags: Flags) -> Result>> { let inner = Arc::pin_init( pin_init!( DevresInner { dev: dev.into(), @@ -171,7 +168,7 @@ fn remove_action(this: &Arc) { impl Devres { /// Creates a new [`Devres`] instance of the given `data`. The `data` encapsulated within the /// returned `Devres` instance' `data` will be revoked once the device is detached. - pub fn new(dev: &Device, data: T, flags: Flags) -> Result { + pub fn new(dev: &Device, data: T, flags: Flags) -> Result { let inner = DevresInner::new(dev, data, flags)?; Ok(Devres(inner)) @@ -179,7 +176,7 @@ pub fn new(dev: &Device, data: T, flags: Flags) -> Result { /// Same as [`Devres::new`], but does not return a `Devres` instance. Instead the given `data` /// is owned by devres and will be revoked / dropped, once the device is detached. - pub fn new_foreign_owned(dev: &Device, data: T, flags: Flags) -> Result { + pub fn new_foreign_owned(dev: &Device, data: T, flags: Flags) -> Result { let _ = DevresInner::new(dev, data, flags)?; Ok(()) From patchwork Sun Apr 13 17:37:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14049379 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 9A44B1F30C0; Sun, 13 Apr 2025 17:38:43 +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=1744565923; cv=none; b=pM3zDARDs99tjUUk2J9U7TG4zmx6AegU1UCWrzCgbvLpEi6Tc3LN32Enf7fz2e0VEMgCCOQ/0BxP6kSPnB4jLT90MFtaGNkT52sM+lc0Z7ZsG6dJERcDHkjjQesB4tzj7Yw8mneTGHKeu4dLCXatZkQ98WXUFFVWrzm3+F8YOmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744565923; c=relaxed/simple; bh=qrHamV6jaiuFnZizd0nUtJBmUrT9qGDrGOwrWzjYKZ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EYeuovL330cl8LntIdqcxau3ktemOnuqZw99tFVTuI9/mB/hRclarEruTEPqs9f0wIV63jcCEgbQoWmabKVtgmJThC4+Z8VDUJ8Ua/L7xByCdi1EtPQ3vOP9EAwOYI11Epsr3tFoA3hBnZcBb67Uzl0K2c9AKpn0wTvtXolpnyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SFSym/Vd; 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="SFSym/Vd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6244DC4CEDD; Sun, 13 Apr 2025 17:38:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744565923; bh=qrHamV6jaiuFnZizd0nUtJBmUrT9qGDrGOwrWzjYKZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SFSym/VdI/ob8900eqtkABOB8d4ZkcvzWufgWOdEi0lz3GJs2JPj7j3j6TU64/f65 S/v4qrAWkFvlYv0opqQMGBM13FJ0jHA84I0n4X7TOrG1uk1FsZwWC9k7QLmRxshp+M /8VidumpMFEWeMVmzqmLdGqiyiVpb2f8z0pSLIgEgDm/iboWwupRSssj5rJQPTD5LV IHeHmB467Q8ZxGdI4ZL9WgvZIMQI4uMpTzJG/81srB+R9/wn2iZX/r6NCJGXu0jQy5 PI+EFI6fi+awMiGxpVeDrnmziWUfXkTdFec5uJwghedk+BFtsqCSBMvboDqq8uXIGF u8G7wU4GoU6QQ== From: Danilo Krummrich To: bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, abdiel.janulgue@gmail.com Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, daniel.almeida@collabora.com, robin.murphy@arm.com, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 9/9] rust: dma: require a bound device Date: Sun, 13 Apr 2025 19:37:04 +0200 Message-ID: <20250413173758.12068-10-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250413173758.12068-1-dakr@kernel.org> References: <20250413173758.12068-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 Require the Bound device context to be able to create new dma::CoherentAllocation instances. DMA memory allocations are only valid to be created for bound devices. Signed-off-by: Danilo Krummrich --- rust/kernel/dma.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 8cdc76043ee7..605e01e35715 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -6,7 +6,7 @@ use crate::{ bindings, build_assert, - device::Device, + device::{Bound, Device}, error::code::*, error::Result, transmute::{AsBytes, FromBytes}, @@ -22,10 +22,10 @@ /// # Examples /// /// ``` -/// use kernel::device::Device; +/// # use kernel::device::{Bound, Device}; /// use kernel::dma::{attrs::*, CoherentAllocation}; /// -/// # fn test(dev: &Device) -> Result { +/// # fn test(dev: &Device) -> Result { /// let attribs = DMA_ATTR_FORCE_CONTIGUOUS | DMA_ATTR_NO_WARN; /// let c: CoherentAllocation = /// CoherentAllocation::alloc_attrs(dev, 4, GFP_KERNEL, attribs)?; @@ -143,16 +143,16 @@ impl CoherentAllocation { /// # Examples /// /// ``` - /// use kernel::device::Device; + /// # use kernel::device::{Bound, Device}; /// use kernel::dma::{attrs::*, CoherentAllocation}; /// - /// # fn test(dev: &Device) -> Result { + /// # fn test(dev: &Device) -> Result { /// let c: CoherentAllocation = /// CoherentAllocation::alloc_attrs(dev, 4, GFP_KERNEL, DMA_ATTR_NO_WARN)?; /// # Ok::<(), Error>(()) } /// ``` pub fn alloc_attrs( - dev: &Device, + dev: &Device, count: usize, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, @@ -194,7 +194,7 @@ pub fn alloc_attrs( /// Performs the same functionality as [`CoherentAllocation::alloc_attrs`], except the /// `dma_attrs` is 0 by default. pub fn alloc_coherent( - dev: &Device, + dev: &Device, count: usize, gfp_flags: kernel::alloc::Flags, ) -> Result> {