From patchwork Mon Mar 31 20:27:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14034099 X-Patchwork-Delegate: bhelgaas@google.com 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 2C62821B9D1; Mon, 31 Mar 2025 20:28:53 +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=1743452934; cv=none; b=oZz1k8sr2Ly4/u258cCEP8FODTFF63bKxraB9TMEnwmrK/HjMyXJqfgogaK4Fp95WwdzSu70Tb9Hf2wooHzhOmrpDug+sV4dGDTfWfcVcIFho61SWlu4RxiCiI71+uW1nQoFOldPl0YNqZWesVKkoPTquP+gAhkS1JGIhxv1VLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452934; c=relaxed/simple; bh=YtAeePVBbSHdITuUrszzto4t2A2eShloNRdKMMuxwuU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JA9WAxSzdziqVA7S58PGUADWviGkW5sjR3oxJJbaiBw9SK1mwnsVj9dwfRAaSn4dk3rs1HvkDOUnV18AUZkbGy+SNTskZjPFsBLfUzfXqrzaa02I1m95RO2pikGVkF3AXVPynPo0w4ieimtMnBgFcq8toKTWys0/mYfBnM0KnDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AmIje9AY; 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="AmIje9AY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E58CEC4CEE5; Mon, 31 Mar 2025 20:28:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452933; bh=YtAeePVBbSHdITuUrszzto4t2A2eShloNRdKMMuxwuU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AmIje9AYESzfS4539CMNdY4OTlfZ3CgR9MThTKrMIsb3g00LVmBOD60EM79vzpfnD 9rbmRbMx5+pvJ2u9Xf6whbfCior//uiQ44IRMhIrxPEQZQSNBZs1/zwQNjuV8+t3F9 BOYSAoP09cKXgNzECVdgS5O/8DaIb4Ml24UGaFvcB1zR8i433CsAV4bmkkdKkD0Q5C QdGK3UkJT7LwQQxX4Fq2PG/VGaOUs3QQWf/B/7ZRV5LZRM2KUyDN+G8Y0cPOf+llFJ 9DoHmNt3+MDxANmZJPQPEtAr0NvY1LRusxJ59iNIPGpceZjF5O+J+Xy2l3Jc34x397 uXMY3cuXFA8yg== From: Danilo Krummrich To: bhelgaas@google.com, 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 1/9] rust: device: implement impl_device_context_deref! Date: Mon, 31 Mar 2025 22:27:54 +0200 Message-ID: <20250331202805.338468-2-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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. Signed-off-by: Danilo Krummrich --- rust/kernel/device.rs | 29 +++++++++++++++++++++++++++++ rust/kernel/pci.rs | 14 +------------- rust/kernel/platform.rs | 15 +-------------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 21b343a1dc4d..e3ab8980270e 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -235,6 +235,35 @@ impl Sealed for super::Normal {} impl DeviceContext for Core {} impl DeviceContext for Normal {} +#[doc(hidden)] +#[macro_export] +macro_rules! __impl_device_context_deref { + ($src:ty, $dst:ty, $device:tt) => { + impl core::ops::Deref for $device<$src> { + type Target = $device<$dst>; + + fn deref(&self) -> &Self::Target { + let ptr: *const Self = self; + + // CAST: `Device` types are transparent to each other. + 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. +#[macro_export] +macro_rules! impl_device_context_deref { + ($device:tt) => { + kernel::__impl_device_context_deref!($crate::device::Core, $crate::device::Normal, $device); + }; +} + #[doc(hidden)] #[macro_export] macro_rules! dev_printk { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index c97d6d470b28..0e735409bfc4 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -422,19 +422,7 @@ 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 } - } -} +kernel::impl_device_context_deref!(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..9268e1edca9b 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,7 @@ 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 } - } -} +kernel::impl_device_context_deref!(Device); impl From<&Device> for ARef { fn from(dev: &Device) -> Self { From patchwork Mon Mar 31 20:27:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 14034100 X-Patchwork-Delegate: bhelgaas@google.com 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 E01D521B9DC; Mon, 31 Mar 2025 20:28:57 +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=1743452938; cv=none; b=oDyEQo7PjvRPohdgPuGxTTBu2hxDjB8IjQwJYJT/SU8LY0YRerunjC8vVEry+dZYDDprtdRiwnPPniTrc3U38A8detyazlhZQkbzce1kHU5ReL/5iLmVGuIvyn098MVUBuAk0T7SQ9JCId+24O7B+b7cVMFn8A0278DG1k7V+n0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452938; c=relaxed/simple; bh=E8LvxAEPSE3C1ltgikB5ignndFPecSrN9rb0pMCJ7ZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ndIl/Bro2ysQboNH+F1sO4FPa+vJvNfjfkQoQTJzSStU9e/nltCj0mIAfHKUewMc3P6aD6MdihY7bQGslEPIA3OvAp6YLnCrbizvfCNEEzLlU7LO6102NRPFoi50wfQk6WTWE8PyehOpJ3DifC2TAnpL6iFUq/lUcbCGoV5B0rM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pEJGvFVW; 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="pEJGvFVW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F4E4C4CEED; Mon, 31 Mar 2025 20:28:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452937; bh=E8LvxAEPSE3C1ltgikB5ignndFPecSrN9rb0pMCJ7ZQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pEJGvFVWkUIYq7gvPiP2/39DUdzWlVXPB65bfUy8K9T1eG5OiKu9fcYN+EHpT2YQJ ZXksRN/D48IWVTWsiOrxMposIWC2kucl8aLMmKl4tbLhSXeTA0N5H18iucmVEGnXR0 so6QvsRg2tjQA9KSDotB4bEniU4rl1k8VwLG8gHJCtzND7HzNZNeoi0gg1P/mv3hv5 qdB6/wI6gGeSTuMiiU1VvhGGxtXQhBkkIe2GgpXsWRmo0uS8mJBv4GpAOgc8iIDIKO pzzQbNbwe/3/dAtDr/D9YW7Y0gC8wHVssOxpTViTgG9PdOkeW4Hb1UA/0Ru3VdUexq BrGugvfe3ltgw== From: Danilo Krummrich To: bhelgaas@google.com, 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 2/9] rust: device: implement impl_device_context_into_aref! Date: Mon, 31 Mar 2025 22:27:55 +0200 Message-ID: <20250331202805.338468-3-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- 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 e3ab8980270e..68652ba62b02 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -264,6 +264,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 0e735409bfc4..e235aa23c63a 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -423,12 +423,7 @@ pub fn set_master(&self) { } kernel::impl_device_context_deref!(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 9268e1edca9b..9133490ea4c9 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, }; @@ -190,12 +190,7 @@ fn as_raw(&self) -> *mut bindings::platform_device { } kernel::impl_device_context_deref!(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 Mon Mar 31 20:27: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: 14034101 X-Patchwork-Delegate: bhelgaas@google.com 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 5EA2D21A45F; Mon, 31 Mar 2025 20:29:02 +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=1743452942; cv=none; b=h1yT8AeCQQK793X88vVzZR+xjxEuy1hs/lVKyAGiasANgMKTD+AovfBSXu0cF54NP1DIFhiskPPg+xll0JWJPW0iZxsamYfpvMsbqrohlCnUQsjEVAdZfKH2h/9OWfUMPHeatCYukBxi57upAan5E9bWI+ckkyyVCw1iVftlC4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452942; c=relaxed/simple; bh=nxP79RjMxaLT6mWO28gxcx1n2NZPhyKfYT/HHlAu4b8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a1qBP67nB74laHoM6aK3sJQom0tkHhqJ6mFbhIoXUTMmC3bdGAN7b6sXOFC1qJIEX+eafnF51vQa2gnX/ei1YAEZoGhtFL1Xzqklrdx5eCN1TCMqLbdxEZiGWEZ4a7goGVBQF1WYkn5LBcG+T6rXi7qeH7meUAYIvGYwoftW2Qw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sIXeGIBW; 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="sIXeGIBW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F1B7C4CEE5; Mon, 31 Mar 2025 20:28:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452941; bh=nxP79RjMxaLT6mWO28gxcx1n2NZPhyKfYT/HHlAu4b8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sIXeGIBWSrDXxazYUOOjVsRzWz3VGPhwJLQw7+9DK9AOt6UZaUdbbV2ea2ODgvR6P FMzLwf7QolokWK/ELoXLcdA4edBlriOv+LaKqvotiBQ23CSVvj4Jzmpr99z4tDOACR nLdPyw2VLZSLGd87rwDN1c5pWg2AaTrrj8vokjkkvuLUi/jqyT8GVT63arPmOWw5v6 76wV1SIRCp3WgGJnBgPDVrJ4ubY7SToxV1kGq/TNLdlSNwi4Aitw9Lio2dzFuihv1h ofUnoHQDhdwfBJ2A3IPyC6vNr+o0nak2P6Ut94tU1znN4arveZxQM22yRLpH+CaNbg nGNtExxR4Muzw== From: Danilo Krummrich To: bhelgaas@google.com, 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 3/9] rust: device: implement device context for Device Date: Mon, 31 Mar 2025 22:27:56 +0200 Message-ID: <20250331202805.338468-4-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- rust/kernel/device.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 68652ba62b02..2d98e650376e 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,9 @@ pub fn property_present(&self, name: &CStr) -> bool { } } +kernel::impl_device_context_deref!(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 Mon Mar 31 20:27: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: 14034102 X-Patchwork-Delegate: bhelgaas@google.com 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 23F7121ABC3; Mon, 31 Mar 2025 20:29:06 +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=1743452946; cv=none; b=RUzKwySiess9uG+EvuSrTVgGob+IqXzQsxSHkR+NFGCF6WVYcI+pExMfz2u/C6NVbx4wT94VI5L8yD0MHQR7SbL8ECS7WpZlgNV1SZDN/vpLtwZExscvQ30vtVqp5uOiYVWYwW0U68WSeoy/ZWTt63h4LjLwLmY5vQZsl77nmcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452946; c=relaxed/simple; bh=YooJygfybgW+FdQ7/bu7ky5o5E8yt0T77/D8/f7ELV0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=brv05/WJWR46XdDQ/17epwfrxXH+l0x0GQaSgE7+72+1owCGHXO+2Klh+0dsSTOMMMdZC5++JM10XVVQonr59zo/a4D0F612NvTyztdZXUvr51pug/gl2oAMKw+AStztKHy//Zne2gF4GGuLHBEDCpLxt6F/AahLXESued/mptA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rDyGRwDk; 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="rDyGRwDk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52732C4CEE3; Mon, 31 Mar 2025 20:29:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452946; bh=YooJygfybgW+FdQ7/bu7ky5o5E8yt0T77/D8/f7ELV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rDyGRwDkQhN+FSpeaw1RF3LFiqdVfDSWQTScgiG8S4SRucrG/8qZO4VfNLLcfj6P5 I5JM71+2Moh0kTm9oO2H9Gh5JJnXCtN85I+CIDLxD2t/PI4aZqcEADbgCnq9PgUk5l i39TnQWTlymfWlrEIbiay6sPU5noQH6juh+jshg9zbF+9DKWKG/eZc3M6x46TDDPa4 HTw9AdPyW8utJnckeyNPcaPtSE+TKbuD9VIhXgdxebgk/m8M5OH13z4npOl8G56Fuh 3zzMgLM/2CSJ52llRAvGrSAcGc5g7dNhC6NTstW8f6B+wg6+51mfmT5DfiW7nC/71r vBlZr4gmuu3GA== From: Danilo Krummrich To: bhelgaas@google.com, 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 4/9] rust: platform: preserve device context in AsRef Date: Mon, 31 Mar 2025 22:27:57 +0200 Message-ID: <20250331202805.338468-5-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- 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 9133490ea4c9..fa07f895af3a 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() } @@ -205,8 +205,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 Mon Mar 31 20:27: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: 14034103 X-Patchwork-Delegate: bhelgaas@google.com 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 A16DC21B199; Mon, 31 Mar 2025 20:29:10 +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=1743452951; cv=none; b=Rt9srwQUClEidyHITRCwz4lx3mBM+pr6pRXVMFAOBOqvdXsveS7gZu5SBarTsqPPDwwqJM/yzMFSn9J4hG6VvdH0U/re9qwVLvIySNh8IlnKHnDptZJVOsFl5e5pZ+KpNNI5E4yurXiwpDYoPFFkcbKNgOQQFnHKpfNFXZvm9+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452951; c=relaxed/simple; bh=YxHRs5a6HHHdMxiYXlHaorHuDEWaBr0Vhep1wlSBUhw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SVskWtbgyf9JlbmWjODaasijU7uMJQBpXbYGD0O0ULALcvrLFnCNN6tbCV9AtLYhC3wtZF4fuRlFHMo68LdTqm/yMIKXkCfHBTIWly+OqUIMbBhGR/6ULm2YyNsDHAJWAx9aL9hVL37A57hNe/M7wjNV73hkOiDhZyhaAgAuGpc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T4zSVxqs; 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="T4zSVxqs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7204FC4CEE3; Mon, 31 Mar 2025 20:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452950; bh=YxHRs5a6HHHdMxiYXlHaorHuDEWaBr0Vhep1wlSBUhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4zSVxqsVW9dEyoK7om3DWhnKEDZFbLfanpUkkPKKbbzRg18ur3S07RpgPqSCOeTT BbamUZzpWcYj94Zw+h+oiNpNg4uG2/iq7ROS3LZRgYsx8WEyzSIbLZFi4AYVC7LloL QFgSvI/oJRGSglsxkgcxHBI0QWi8KNQ2KYaH+BtQolmwFWI5iZ6Q+kw3kvcCcfEKw0 XJKA2PUqVv1ZxVStVclslMZlEjM0nc6QxHJViZ39IAZnPrzGptSayImcmddky2Js2b s7STfAH4LRrgLwnF1VVd+98NTbYOXimpD/TwPhFQLfxGOBppYOuYi2SaQoLzZDAaTM 4CSYexckOuaeQ== From: Danilo Krummrich To: bhelgaas@google.com, 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 5/9] rust: pci: preserve device context in AsRef Date: Mon, 31 Mar 2025 22:27:58 +0200 Message-ID: <20250331202805.338468-6-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- 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 e235aa23c63a..de140cce13cf 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`. @@ -438,8 +440,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 Mon Mar 31 20:27: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: 14034104 X-Patchwork-Delegate: bhelgaas@google.com 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 DC39521ADC5; Mon, 31 Mar 2025 20:29: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=1743452957; cv=none; b=jMEAun1p9smZn8zr3U5zT7ig3dghFsyCgzjLt6AgwQ/M7yHeS7uguwy30e7eSxAj6MRdq+GNPPrjOxXldnQvOVjHofUlUOMR2xdfykQdJKJDrNK+CHftFG+MIEyAjdTT1dWQxtq5gPH5pYjFQ5RnSA4uYtvMQcm+CB3PHIGjPvs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452957; c=relaxed/simple; bh=4LBQYOuKWUMJKxTctedQtQnbNZFRSegEXeOdIXP3acY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KOi4/4V+ah91pRmv09JkMyFHRpsIvpU9Xeh3crFSJ0kvUqyYQLTgZQv9Ofr+oDv0saoyIIj9PiVH31wj6U198WyuXHG81p2y5Umcsm4fvUQeXiFyx3LVEBvDXDUYiJOJ58Nj22wntOzPbHgFzxYvI591QX4u4xxzPOEYk2bD2ZU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PnSZSNZF; 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="PnSZSNZF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FAE9C4CEE5; Mon, 31 Mar 2025 20:29:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452954; bh=4LBQYOuKWUMJKxTctedQtQnbNZFRSegEXeOdIXP3acY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PnSZSNZFiPLA+JW4CNZQW+GSBuNbsGx57oQDJaYQQWb4AX+fHsMPh37/lTp7YJoIW fbDl9cD9HxY4LFAvn2W2Cz+NXwWjjYxF9EgdX3cmUyTEitNDx4AowIcmYjTHN8sVdc cUwp3P49l/bIgKpOBu4+ARPSKX2JA0txJLx3zvoho9ecCXkyZACppYEjTqnA7Jl6NL N4Y9UO0Z4nf1wjp6GL8NhDZjsXQRbQIm4AR6SUbzS3Zqeq8eHJzYg6WpaR/1Oj1jnD NnQGU0q6EDofYUNSGwXWSsR+kKJ/8VazrM3736PSxGrvBaV1CAahQI/IrDZh7a9lxD phZ+su4qUzeYg== From: Danilo Krummrich To: bhelgaas@google.com, 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 6/9] rust: device: implement Bound device context Date: Mon, 31 Mar 2025 22:27:59 +0200 Message-ID: <20250331202805.338468-7-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- rust/kernel/device.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 2d98e650376e..a7da1519439d 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -230,13 +230,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 {} @@ -265,7 +271,12 @@ fn deref(&self) -> &Self::Target { #[macro_export] macro_rules! impl_device_context_deref { ($device:tt) => { - kernel::__impl_device_context_deref!($crate::device::Core, $crate::device::Normal, $device); + kernel::__impl_device_context_deref!($crate::device::Core, $crate::device::Bound, $device); + kernel::__impl_device_context_deref!( + $crate::device::Bound, + $crate::device::Normal, + $device + ); }; } @@ -287,6 +298,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 Mon Mar 31 20:28: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: 14034105 X-Patchwork-Delegate: bhelgaas@google.com 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 E041121D5B4; Mon, 31 Mar 2025 20:29: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=1743452959; cv=none; b=EgCzJbqCjhHWvexp/lj/FXwwfU8G7tkMngDGgQOG49bHd5HqIWL8ROaR9mMGWGDJlEuIHLgB0xht7Cr+mptRGmqKlF0Gu3TClX9YyhbOlWy3Bs8jK4n9InwOnlaAxulwaGhhGoOk0fYbRRHfC7bZXf6dGqc44rTNmNr/9AndLXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452959; c=relaxed/simple; bh=jj5/lxEZE+tngZSVTxtZjkJcf2L2JtaOvEVjvyRamcg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dX1JNGEd/w0CB8CwUFfvF7xIov5rAyCUGjW/ncqVvpm8TQZYebh6sTzPwlXXBsQZ+h8Pthz6fnDRdyag6EVlx2Vux8IPx9PNis7KqfjFk6qvtq7OFGR2g0ZJuB7dBo/+fcfKx8LXH20cO9Bod+JOkaLcEYuWbKOYU2/t3r9MhuE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cf7OrCSN; 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="Cf7OrCSN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACD51C4CEE3; Mon, 31 Mar 2025 20:29:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452958; bh=jj5/lxEZE+tngZSVTxtZjkJcf2L2JtaOvEVjvyRamcg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cf7OrCSNGRbmlU5cpIPJ/M46rf/5H22gXE01GUizW+bsNdMq8TcLRj2o+VlH9eHwD 29vsy6IfbyQGzTvjIypH/HtONpTBktsuiv1z7iehuQbHYBr14P3U2iOFTubjRaCliH KrUEq6eo8z1y7DsojIzZizXY3LdbcKw8Z0rm1y0fKE5DWQQwYXFRMU1ZRJlunsVwg5 nTnWpN/w2n2e/WbamKVBU68UqUcHY1msKh6Omhs6QRNyD0CzDi1aFdQ/aILuNLAbI3 KPYv4WddZf+p6lribO+t281a03J34u7u1vtBCspc+Zqp3Dnd8v72DaPSbyzWyB5JgJ BWcrm3bFhA97w== From: Danilo Krummrich To: bhelgaas@google.com, 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 7/9] rust: pci: move iomap_region() to impl Device Date: Mon, 31 Mar 2025 22:28:00 +0200 Message-ID: <20250331202805.338468-8-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 --- rust/kernel/pci.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index de140cce13cf..b93bf57d2b54 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 Mon Mar 31 20:28: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: 14034106 X-Patchwork-Delegate: bhelgaas@google.com 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 9F07121B9FD; Mon, 31 Mar 2025 20:29: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=1743452962; cv=none; b=k+w79UWDDkM7gMsmi9ZFrQ9mpXWfrTMXUkFJ/tVzdFfKiCFhky62tFMe/QoAzupIdie+G+qWi+VuCrbUF7aBB8NmNGL8FLKbbhEZdOQGTKRyX+5dbf63W0svIm4j8cauEJ/qQ621Fy+RoWWGyD9q4chm97gGFn7MQ34ccky1Jqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452962; c=relaxed/simple; bh=pQKXbW5g5QATkdun2XLx2iYsIxArlMlNIpZWyKcpnYU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o06Nq72GqOyzdE4tHH2360p2NKucg7xul2KQ00y97zsAnu5u4F4PyYNSMZiqYrZwiAP3GJyMe5ZpfB0fvps3CXfIA6B8ZqEF4IumhVSjoN1Mc+u8nXb4Ca3YDrLa0UDLWU9Mz+KIW7kx9bWm2LN2GCjVodiAUyWzdUvgn4gzxQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uWrylcPR; 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="uWrylcPR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA36EC4CEE5; Mon, 31 Mar 2025 20:29:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452962; bh=pQKXbW5g5QATkdun2XLx2iYsIxArlMlNIpZWyKcpnYU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uWrylcPR0S0pc/kaUMIHKIS7PWHM4WTLu8Wmf+0LHhnwyGxnzCQAc55fMaq5N2K9j hy4PlwZLv2qMDCR/xEzwdu4FVzzwtIciCsGSMrnFFgdkxWHFFSS0Np+7IougQCNU/w bjZ2mWJGZEJ6bSBXkqmG9xc7JiNTFnj5SKLKSRLiqhDT6rjb9NRAfD9lj8q+9WG21q B/SdYRPIZ7W8W9Ag4rBt4N+EixJTDYKwQcgd37HRZTHV0yUi86mjCCIVAuXKVtaHvZ W0/LbGmnXsBNjZ1OISO40ny3GXbaDjQld4wnoDRe1S9Qz1mh+C5jQJVNevGGge9ZN9 rH06KokRxRaIA== From: Danilo Krummrich To: bhelgaas@google.com, 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 8/9] rust: devres: require a bound device Date: Mon, 31 Mar 2025 22:28:01 +0200 Message-ID: <20250331202805.338468-9-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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 Mon Mar 31 20:28: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: 14034107 X-Patchwork-Delegate: bhelgaas@google.com 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 64AEC21A45A; Mon, 31 Mar 2025 20:29: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=1743452967; cv=none; b=Kc4rGvK3bnfmL/RM9OBqUIjRDXHsjgbzm1pcMt9l8qbDP6zTT/O/qAC6GGsFut+6qThYEwvlJAQ4WG6UCBlnaDRObMGBQLK/dlAV71ISrzWTtKYdWWZP2BdcTYsEIsH/Jki1nbl3/hX2oNXn3osU6QkvVSke6OsPK0brAbdxwTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743452967; c=relaxed/simple; bh=qrHamV6jaiuFnZizd0nUtJBmUrT9qGDrGOwrWzjYKZ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tlTeb0q1yHEM3djhEyLyPNYKpMj6hiPAxw73vazrlaFHrCY0V6dMJLRGChja0EhirAhugqrgkuKNJP7EbXyCmbi7QnijWG4fAy18TU7zUyfn2Ark5foWVILp9S61RI/YmPqOvRfqqzdvM7oVQVlRdE32003RvoL3evKcSbAZlmI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V/+2mW+y; 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="V/+2mW+y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E978BC4CEE3; Mon, 31 Mar 2025 20:29:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743452966; bh=qrHamV6jaiuFnZizd0nUtJBmUrT9qGDrGOwrWzjYKZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V/+2mW+yHhqtKcbzaLf8i5CXTcfYLBL+PTIhxRRZGHzb4BOGFJ9aqglEbzspXYtjI HrAvqS/s6I5Iqj2zTEq3wuaaRmTFtZptBTsteO+QeKLsfqGSacXfMOKZbb3i+LqDFK MF0/mkSYcQJo7Aapn7w7Oe+t6Bv4Xe01mWXTTNxNyZDvfYygsfuAAcJAUpsFDdSTbS RWyDkCZUDMyCSDnt6b7ltOtHX3oaFQ2kD88ect/O+rgnypaniODdkrFV926IDlIupX dIT9NH/T1rLqugqDRTfi3ZEBF3z6m3G5xh/sFEilLfOWIca/UsGAdP4PAYbZkIWW6p BTraSG5Ob5/7g== From: Danilo Krummrich To: bhelgaas@google.com, 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 9/9] rust: dma: require a bound device Date: Mon, 31 Mar 2025 22:28:02 +0200 Message-ID: <20250331202805.338468-10-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250331202805.338468-1-dakr@kernel.org> References: <20250331202805.338468-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@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> {