From nobody Sat Feb 7 21:08:12 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011021.outbound.protection.outlook.com [52.101.62.21]) (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 C2BD031AAA7; Fri, 30 Jan 2026 08:35:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.21 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769762142; cv=fail; b=d00lAzuS0en0n2Y/EhF1tahXsEm03cILzKBkwrrtQ5QOYdgsglhYBFdWfMwj6pHKMeqzEQ8P33XYJrDyvyVui8gSbcjEdoUp6b6ASm5T6fGW3AH+Q3bGdmxXrLi9F/8qV0wtaPgYXZOexqhAOMjTUpfBrS4J7w8O/nRyOdInnWo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769762142; c=relaxed/simple; bh=EANHL0hsrvYBkgQaKdLzeh2Vf47DzJGIUFrikyVV1Bg=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=A4/eFAdmMxJg4pI7rts+FfnP6dqGNF4JixLaJvoE+grikyYbU/PdwUT5ClsF9Yka8UQeiO5st3+DmtrYC9e7b9CiR+OPz0ia+25HK4NX6/8TWGc+ehqPug5jqnZs95xLY/6wiu2KpuAogm9oUzjlAlESVPfhlF+RENB4VtSE8xo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=dUljGZ44; arc=fail smtp.client-ip=52.101.62.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="dUljGZ44" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hCb9tYBZLYgZfKfUhXhSOUw445tjS7hFXZqbkSdDcJDu2piiu5CLfqPBDtH5Wo+6sgfQfDWXVbcO2ZcWv6CGOue/bQtD4aaMkYBWRLmNM6KcxgSkmzGzGIY0RhM69p+k/HvRUhFbr+8u1Zrsy606uU0YNLpW721zgXt/T0ukh7NXaIGe0ZPn/CiGA2kctENUH+bXMw1ypp5E9s6bAB1bRimf0iEcAsGe0R18hmx8IRJjL8sddjYF6vq6M0UtUV2CvX7Pp1sh7f7dAnXgGBSIwmE5EsvQtUrfZeJpmWQdg3oyQVfGjr+C1DCtJugbUdazG40+2UmxxFgC6HJbiAJsUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9y9ODM4+ldeghrT49aub4SQ57dU9CY/QpzXDKno35mE=; b=XAZEdAJA8/+gCwiBNlVyHsPkeuKxMndd4tzi7EOPgxUk9/dEN7UP996XFVI7W+VjP+gntDaGw2Tooam1x7xtzcfslT4ASBkXWUTKpvFfNjrf4S5jZCzKQ3WY5DpUPc83iRflhbtgdKgbVq+f+9uZeFah5lGYVr6SOc1IbKTzNlUu4iN0DmJ5NEU4JTtlLGBlmK3RCePv35f4QMVYiK8lorMyTwqRrwy8iaWvuEr/pawBcpLhL6wZ3NdHEYb1gXbkOjFGZyhC/JLpPD07WA7CHI+oNeFscfFWvCcb2sW8vYeebt95YE8ZUkSkx5ttXGB/PyI7o8Z3eKjC2VJjAxEdGg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9y9ODM4+ldeghrT49aub4SQ57dU9CY/QpzXDKno35mE=; b=dUljGZ44A+iD3N3b0a+2eqX/5wRVRfTlnMYMtCdsMEXQ1HLBHT6ZQeAA8cJ1UbThOwjDaeEAkQ0TQ2rGpBztg+SBdfezq60CjAMU4Zfm3o+8Hr/TIVq4NPxe8R0o3i33hV0bxYHl42CYmWgP+AGzF8x6kqvshMNA6iMtVEDep5tQBLa5ufeelyvV/8np+0D1x6RnVCSNv6wvmruLjgzBetffDhGdDItCCu49qpr7dOQUFcaMzxh6O+5HnIyij0/YscvioE3xoi8JF+a+1XU29XLBZfZMvdbaePWOSbhnRCtJoeiP3TqD4GBtwn6qyCMDWKbK7vq2ry0jCaxL7AIX9Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB2353.namprd12.prod.outlook.com (2603:10b6:207:4c::31) by DS0PR12MB9400.namprd12.prod.outlook.com (2603:10b6:8:1b6::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9564.11; Fri, 30 Jan 2026 08:35:35 +0000 Received: from BL0PR12MB2353.namprd12.prod.outlook.com ([fe80::99b:dcff:8d6d:78e0]) by BL0PR12MB2353.namprd12.prod.outlook.com ([fe80::99b:dcff:8d6d:78e0%6]) with mapi id 15.20.9542.010; Fri, 30 Jan 2026 08:35:35 +0000 From: Eliot Courtney Date: Fri, 30 Jan 2026 17:34:06 +0900 Subject: [PATCH 3/9] rust: dma: add CoherentArray for compile-time sized allocations Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260130-coherent-array-v1-3-bcd672dacc70@nvidia.com> References: <20260130-coherent-array-v1-0-bcd672dacc70@nvidia.com> In-Reply-To: <20260130-coherent-array-v1-0-bcd672dacc70@nvidia.com> To: Danilo Krummrich , Alexandre Courbot , Alice Ryhl , David Airlie , Simona Vetter , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Andreas Hindborg , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Trevor Gross Cc: nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org, Eliot Courtney X-Mailer: b4 0.14.3 X-ClientProxiedBy: BL1PR13CA0174.namprd13.prod.outlook.com (2603:10b6:208:2bd::29) To BL0PR12MB2353.namprd12.prod.outlook.com (2603:10b6:207:4c::31) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL0PR12MB2353:EE_|DS0PR12MB9400:EE_ X-MS-Office365-Filtering-Correlation-Id: 77b79c9b-89d6-45ce-0b26-08de5fda8981 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014|7416014|10070799003|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?eWVXUG84ZGxrWStNWW1CZzNVNlpZMVZFREYyNWZtdjVxM05hRTllV0dkNTlV?= =?utf-8?B?SEE0c3owUGZnZ3lLSFdzZmZsZ0dxWEhaWlJrYW91K2JyREx4UEFweERxUHdo?= =?utf-8?B?UzBZcWxNWk1MQjFOSzZ1VGFrekNhU2FaZFlXY044TDlRdStPdUlmdkZnYUls?= =?utf-8?B?VE1xYlBiUitCYU5wU2RFL0djbU5SRHNWeExlYnVCYWNqWTR2YlJGY00ybzZQ?= =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NM3FocjFkSUJ4RERZSnJGNG1JMC9IcXJ6?= =?utf-8?B?WlIrc3pnbHN0N2c0cmdJNzJkVjVnS2M0a0JXOWpRWERNMC9zVko5VUJhcWJE?= =?utf-8?B?TU5zTUZjZjVuMy9kb2FLN1JRaVcyMUR1aWdmWEtvU2ZCVWhXQUhFUmhVUEpi?= =?utf-8?B?QUE9PQ==?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB2353.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(7416014)(10070799003)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?TDZxOFVubGs4YTI5L2ZPazZuVzNldkdkRkc5YnNTeDNZQVFNdkVKaDY5K0dl?= =?utf-8?B?YTl4SE9HcGZoeWdtSlNnTDUrYjNzZlptWkltMkdRUWNXMklMSTh5YTRyNjZZ?= =?utf-8?B?TVYwMXY4aW9GUVRkV090ZnA3WjB0S0p3d2Zwak1DSnkyUFBOZXV1RUlPQkRi?= =?utf-8?B?KzdpTitlajZGYlQrbEJ1cnROODhVTThrT0J5ZlRHckRnbkRMZDkrT0ErcDFL?= =?utf-8?B?R2U4MEhFUTZ0OGErRHhoeXU5bWM2dGFTMzFYTWYyYm05VFJnYlNlOTh2dUlW?= =?utf-8?B?d2tLLzBEZXN5SzZmdXpoL2JWSnM3QVljeWRQN1ozQS9TbkNVWWxwd3RhWW1Y?= =?utf-8?B?eG1jNVgxcFltRlBRM1VSOGZ2RWs2SnRtdzhncTJzR0tjaFhYOU43YmcrT2Ey?= =?utf-8?B?K1Y0NWVLWUplZTYxQ2x5bWtzWFkxam1sV3dYT01sSnZqZ0tBMW5NWVViOEhX?= =?utf-8?B?K1RFUnYvODVtSmtlK0pVRlBmeUZIMisyRVIvZEVMVGdsNTVOZS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ltamVidjFaNktaZVVjZ0lBSDFkVlk3?= =?utf-8?Q?/GhpzDjxoMuoiRB6aUqsvHUDlsbwwV5ci8mbVdt/KN2p9?= X-MS-Exchange-AntiSpam-MessageData-1: 5n8ituzASDtPAg== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 77b79c9b-89d6-45ce-0b26-08de5fda8981 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB2353.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jan 2026 08:35:35.0093 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Dnb4kOo/arHVayXugeHeS7bM+xeRjDuqR50cHltJdIYE3DVA093WapWibECD9ymK7X3JMd2dW/buB55oPzIGTg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9400 Add a CoherentArray type alias which takes the size parameter directly, without using the StaticSize marker type. This makes it a bit nicer to use. Signed-off-by: Eliot Courtney --- drivers/gpu/nova-core/dma.rs | 8 +-- rust/kernel/dma.rs | 127 +++++++++++++++++++++++++++++++++++++++= +++- samples/rust/rust_dma.rs | 8 +-- 3 files changed, 132 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/nova-core/dma.rs b/drivers/gpu/nova-core/dma.rs index f77754f12f02..c217cdb14223 100644 --- a/drivers/gpu/nova-core/dma.rs +++ b/drivers/gpu/nova-core/dma.rs @@ -9,13 +9,13 @@ =20 use kernel::{ device, - dma::CoherentAllocation, + dma::CoherentSlice, page::PAGE_SIZE, prelude::*, // }; =20 pub(crate) struct DmaObject { - dma: CoherentAllocation, + dma: CoherentSlice, } =20 impl DmaObject { @@ -24,7 +24,7 @@ pub(crate) fn new(dev: &device::Device, le= n: usize) -> Result, data: &[u8]) -> Res } =20 impl Deref for DmaObject { - type Target =3D CoherentAllocation; + type Target =3D CoherentSlice; =20 fn deref(&self) -> &Self::Target { &self.dma diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 6e6d91a9cd62..43ed0dfdbc08 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -194,12 +194,12 @@ pub const fn value(&self) -> u64 { /// /// ``` /// # use kernel::device::{Bound, Device}; -/// use kernel::dma::{attrs::*, CoherentAllocation}; +/// use kernel::dma::{attrs::*, CoherentArray}; /// /// # fn test(dev: &Device) -> Result { /// let attribs =3D DMA_ATTR_FORCE_CONTIGUOUS | DMA_ATTR_NO_WARN; -/// let c: CoherentAllocation =3D -/// CoherentAllocation::alloc_attrs(dev, 4, GFP_KERNEL, attribs)?; +/// let c: CoherentArray =3D +/// CoherentArray::alloc_attrs(dev, GFP_KERNEL, attribs)?; /// # Ok::<(), Error>(()) } /// ``` #[derive(Clone, Copy, PartialEq)] @@ -414,6 +414,9 @@ pub struct CoherentAllocation =3D CoherentAllocation; =20 +/// A coherent DMA allocation for an array of `N` elements. +pub type CoherentArray =3D CoherentAllocation>; + impl CoherentAllocation { /// Returns the number of elements `T` in this allocation. /// @@ -692,6 +695,124 @@ pub fn alloc_coherent( } } =20 +impl CoherentArray { + /// Allocates a region of `size_of:: * N` of coherent memory. + /// + /// # Examples + /// + /// ``` + /// # use kernel::device::{Bound, Device}; + /// use kernel::dma::{attrs::*, CoherentArray}; + /// + /// # fn test(dev: &Device) -> Result { + /// let c: CoherentArray =3D + /// CoherentArray::alloc_attrs(dev, GFP_KERNEL, DMA_ATTR_NO_WARN)?; + /// # Ok::<(), Error>(()) } + /// ``` + pub fn alloc_attrs( + dev: &device::Device, + gfp_flags: kernel::alloc::Flags, + dma_attrs: Attrs, + ) -> Result { + Self::alloc_impl(dev, N, gfp_flags, dma_attrs) + } + + /// Performs the same functionality as [`CoherentArray::alloc_attrs`],= except the + /// `dma_attrs` is 0 by default. + pub fn alloc_coherent( + dev: &device::Device, + gfp_flags: kernel::alloc::Flags, + ) -> Result { + Self::alloc_attrs(dev, gfp_flags, Attrs(0)) + } + + /// Returns a DMA handle starting at `OFFSET` (in units of `T`) which = may be given to the + /// device as the DMA address base of the region. + pub fn dma_handle_with_offset(&self) -> DmaAddres= s { + build_assert!(OFFSET < N, "Offset is out of bounds for the allocat= ion."); + + // INVARIANT: The type invariant of `Self` guarantees that `size_o= f:: * N` fits + // into a `usize`, and `OFFSET` is inferior to `N`. + self.dma_handle + (OFFSET * core::mem::size_of::()) as DmaAddre= ss + } + + /// Returns the data from the region starting from `OFFSET` as a slice. + /// `OFFSET` and `COUNT` are in units of `T`, not the number of bytes. + /// + /// For ringbuffer type of r/w access or use-cases where the pointer t= o the live data is needed, + /// [`CoherentAllocation::start_ptr`] or [`CoherentAllocation::start_p= tr_mut`] could be used + /// instead. + /// + /// # Safety + /// + /// * Callers must ensure that the device does not read/write to/from = memory while the returned + /// slice is live. + /// * Callers must ensure that this call does not race with a write to= the same region while + /// the returned slice is live. + pub unsafe fn as_slice(&self)= -> &[T] { + build_assert!( + OFFSET + COUNT <=3D N, + "Range is out of bounds for the allocation." + ); + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion`, + // we've just checked that the range and index is within bounds.= The immutability of the + // data is also guaranteed by the safety requirements of the fun= ction. + // - `OFFSET + COUNT` can't overflow since it is smaller than `N` = and we've checked + // that `N` won't overflow early in the constructor. + unsafe { core::slice::from_raw_parts(self.start_ptr().add(OFFSET),= COUNT) } + } + + /// Performs the same functionality as [`CoherentArray::as_slice`], ex= cept that a mutable + /// slice is returned. + /// + /// # Safety + /// + /// * Callers must ensure that the device does not read/write to/from = memory while the returned + /// slice is live. + /// * Callers must ensure that this call does not race with a read or = write to the same region + /// while the returned slice is live. + pub unsafe fn as_slice_mut(&m= ut self) -> &mut [T] { + build_assert!( + OFFSET + COUNT <=3D N, + "Range is out of bounds for the allocation." + ); + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion`, + // we've just checked that the range and index is within bounds.= The immutability of the + // data is also guaranteed by the safety requirements of the fun= ction. + // - `OFFSET + COUNT` can't overflow since it is smaller than `N` = and we've checked + // that `N` won't overflow early in the constructor. + unsafe { core::slice::from_raw_parts_mut(self.start_ptr_mut().add(= OFFSET), COUNT) } + } + + /// Writes data to the region starting from `OFFSET`. `OFFSET` is in u= nits of `T`, not the + /// number of bytes. + /// + /// # Safety + /// + /// * Callers must ensure that this call does not race with a read or = write to the same region + /// that overlaps with this write. + pub unsafe fn write(&mut self,= src: &[T; SIZE]) { + build_assert!( + OFFSET + SIZE <=3D N, + "Range is out of bounds for the allocation." + ); + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion` + // and we've just checked that the range and index is within bou= nds. + // - `OFFSET + SIZE` can't overflow since it is smaller than `N` a= nd we've checked + // that `N` won't overflow early in the constructor. + unsafe { + core::ptr::copy_nonoverlapping( + src.as_ptr(), + self.start_ptr_mut().add(OFFSET), + src.len(), + ) + }; + } +} + /// Note that the device configured to do DMA must be halted before this o= bject is dropped. impl Drop for CoherentAlloca= tion { fn drop(&mut self) { diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs index 7a87048575df..97711a99ac8b 100644 --- a/samples/rust/rust_dma.rs +++ b/samples/rust/rust_dma.rs @@ -6,7 +6,7 @@ =20 use kernel::{ device::Core, - dma::{CoherentAllocation, DataDirection, Device, DmaMask}, + dma::{CoherentSlice, DataDirection, Device, DmaMask}, page, pci, prelude::*, scatterlist::{Owned, SGTable}, @@ -16,7 +16,7 @@ #[pin_data(PinnedDrop)] struct DmaSampleDriver { pdev: ARef, - ca: CoherentAllocation, + ca: CoherentSlice, #[pin] sgt: SGTable>>, } @@ -64,8 +64,8 @@ fn probe(pdev: &pci::Device, _info: &Self::IdInfo) = -> impl PinInit =3D - CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VAL= UES.len(), GFP_KERNEL)?; + let ca: CoherentSlice =3D + CoherentSlice::alloc_coherent(pdev.as_ref(), TEST_VALUES.l= en(), GFP_KERNEL)?; =20 for (i, value) in TEST_VALUES.into_iter().enumerate() { kernel::try_dma_write!(ca[i] =3D MyStruct::new(value.0, va= lue.1))?; --=20 2.52.0