From nobody Sat Feb 7 20:41:09 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011051.outbound.protection.outlook.com [52.101.62.51]) (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 4036F3271FD; Fri, 30 Jan 2026 08:35:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.51 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769762138; cv=fail; b=IU54KR1Ni0fV4t1mm/Zi17QEwe/K8ApT9DJaXyHGnlccHQEfE6HKuSk1Zgeiqo5a9eju6Bk9sXu8AnG4cq+owQmiW5cyOxZ73Gn7MaHDSgO8TQcRJLudgRSGfzznV87F2piHleAFc/+xbOrksSe8lai//KeROwwX5fDSwL6kl5g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769762138; c=relaxed/simple; bh=QMbjE2U+lzgW5rFO3dsmt55VETLhk5PdN8u8R+EY/qg=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=B2KApAXDH6jj2hQHeaZbaITKK1xrVE5TiPmzwS2GJWnWWtWvXILnq18GTy9zP3+YUmiw1npAvx9IQ3LiAUlqmeVXErSIOMXmq2fgZ+V4C2S+/8439JnobgSpoosgKgPJe6yVzfzs+wl+uDzGaAGHhiz5rXx0v1Mu6sqXZVRKrPA= 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=X7zJqADw; arc=fail smtp.client-ip=52.101.62.51 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="X7zJqADw" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r0rBbkQ2vO10ZuxdbxCrDDwX6tcKGdmn/IAmHvcDH26mK2A/CJdRjSsD2UNM2Goduz/NOptRvPrbIFyZreG5xj2uX5vZoryrfOJhQG1dNo+pWRwZhYc/tO313MA67uoK4cG3lyQWKbBhCNkrqStGXFP7VUjysE3zwtBbxcdSOuPn47yr04amOcU3qeDqONDUdrwv1sVpHGpnAttfUTuP/2kToofeWG3QIR4SLt+oc8Wpq7yw/bNA/weeGICu34eSoeHfLU1TQk3IzSdB4kpzkPCkWbH57AtXE7OGTgcoW3nhXkbrUR4E4PCf8PamkAIvS8uG/RcUSPsVeMnLRNBRsg== 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=0PVSLl84T6R4bkcOBRJEdC3f6ytQAkcWCVq/xY9WxCY=; b=bEgAE3ZdF7wX2nUUhYBZDABwyrH2XME8ZQVGWTajwkp4ExGb/++o6BIFMnDHC+FywFwpRtCH14ShNepCMHh8p1Gf4Ev76J7HyfrXaMKok8lnvAY58vh+kBksd0DlvyKwn4OOgQdbDF1xZvUXRThGX8DM53ZoeXq8gZt/2zvxaR5nS4uX5rV19tz8pCKGhDo3CQdktglMLOuZoD5Prkzh+I2KwO704kmOgdGs8CVF8iWikJUc7UrtWImJDtr0oiqstG6/j0qh8/L4Mk0NztopPY+oPBs63im/SnHrpsmAidwjnCD5/+oSpQphX0j6SSroAQNnzMUhmyNSMc15aFZZoA== 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=0PVSLl84T6R4bkcOBRJEdC3f6ytQAkcWCVq/xY9WxCY=; b=X7zJqADw9TZ1vnO9pec+CqaJZfzB/nWifZ5EuMtLKzdKt9yPlgXQtGM5g+crAF5wWTj/1m4aL9tNMdraFLN39wg3YLPFlKwW78El6VawNvtW57Q0X+wdL8jOKhbIHXVJk4xpZ+V6Su+h7JX6B3CsWJDQZn78ONJpCOacPTkui8NhRaWHPreBqqULs6Ia/bxjmraqKFWTc3Ivr3VE/vtqvZoZn/5Lbqe2b3srCR9g9B0ZI4RxnyXYXA7dYcZxTV6ifO3Vy4LKGUV619kh/Sk7f2vM7GKb0Usl7W42EGUqBQRNtfN9PuESA25T4xpkAnpkJBa8njqz0/hLrFug49jPWQ== 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:31 +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:31 +0000 From: Eliot Courtney Date: Fri, 30 Jan 2026 17:34:05 +0900 Subject: [PATCH 2/9] rust: dma: parameterize CoherentAllocation with AllocationSize Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260130-coherent-array-v1-2-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: BL1PR13CA0410.namprd13.prod.outlook.com (2603:10b6:208:2c2::25) 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: fb1bc477-8c91-4b88-7470-08de5fda877e 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?Q28rMHU4NHRqdDA0SVlxN0xlU01KUVVvTVVMaFJXS2N3THRqSVFQNWhjcVk5?= =?utf-8?B?dnpPUVNnRzJ6TmVlMUI2SU8xeVhHNHZDbkpEL05yWjNFdWtXZVhRMGZOMUtS?= =?utf-8?B?ek5xR1AvSG5rTVI5VWVrOWZTNHBqQ2g4WFVDdit2Y0xVQXBBMXh3UGZnL0tZ?= =?utf-8?B?bGdwVHpmTUwrdG9OcmFtc25ZMXFlL1FLamRtbklqaW02TDZUV1VZUWdPbnkr?= =?utf-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qTUZueUpiWGdMSlNQak5tc1R1UmZx?= =?utf-8?B?UHlKb3gzdXF6Nng4OUxUeC9HWlZ1Vm96M2grWE5pdUNFMTJweUJ6c2ZBM2Jr?= =?utf-8?B?bTgwdmxob1d2c0gxekg5UkZ2ZS9HajkxOFc1U25jcHFmVFZtNmMwbldRVTV1?= =?utf-8?B?MXc9PQ==?= 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?THZtZzl6Kzhua3AxbXpTS2lLRFVMZ3NxWlRQZUJYSVBoNTdOQ3lYaUw5eVlv?= =?utf-8?B?ZHZRSlRLZ3JmZFVSZFV2VnFYaGxleDY2Nnl6c3dzWVpkRzV1a2lodWx5bExI?= =?utf-8?B?cmVSOUY0UTY5L0I1S0xmOTFOTnZkUHhwbHpXc3dFNXhHOG4xVWJWSUVKTXNz?= =?utf-8?B?WkNCdklhYWFGcU9ZSzR4K0wwUXVxLzZaeGxtK0g1UW10eFRsVWRLTE8wWnhh?= =?utf-8?B?eGVRRGNwWDZDOWJPSVZhZGR2SVJpbFQ0VHlyQnpXdFEzbWVBTE1ERDFXb1Zm?= =?utf-8?B?V00wcklLU09sNE4vRmF4bUgvaFVXMG9tRHltRE9pLy9GVmwzTHdJQ0gzYjY2?= =?utf-8?B?STdnc3d4Z1E5MUQrcTBpRHg5bERCVk5BZU9JMzM2S3BsUVljd0M0K0g1ZGlN?= =?utf-8?B?VCtQVlFreHoweDZ4ZUNQMy8vb0ZoMFFXRmFaNjRyeFRFd0ZSYVVJSkJiSzBt?= =?utf-8?B?QTRaNVhoRlRHb2taZ1BxQVlHeFNWSlRNQXJIUjVnTno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-8?B?R0JzTWhHcHNYZjBEekRKMWZWMWFMSXlYcGNjdENraXIrMTUzZ1QxaU5PclFL?= =?utf-8?Q?EJa9KT9GdhIbHeeEdZsn/iPjHpa2TX8LAaKi3iqj0DmWk?= X-MS-Exchange-AntiSpam-MessageData-1: Xz40MZ/xyFuUMw== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fb1bc477-8c91-4b88-7470-08de5fda877e 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:31.6513 (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: QIRXtNaF0R7/GcnAQQaObxgGHugWhcidZyChaHbQ2LIwJqcDrhxJQr4EW+XaIaeFecjYEJcVHYWHo6vCCOIHwQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB9400 Parameterize CoherentAllocation with AllocationSize. This lets it carry information about whether it knows its size at compile time. This follows a similar design to Device and DeviceContext. This is useful to be able to read/write without having to handle a Result, and to move indexing errors from runtime to build time. Signed-off-by: Eliot Courtney --- rust/kernel/dma.rs | 185 +++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 117 insertions(+), 68 deletions(-) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 02321d5f3f06..6e6d91a9cd62 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -12,7 +12,7 @@ sync::aref::ARef, transmute::{AsBytes, FromBytes}, }; -use core::ptr::NonNull; +use core::{marker::PhantomData, ptr::NonNull}; =20 /// DMA address type. /// @@ -344,6 +344,29 @@ fn from(direction: DataDirection) -> Self { } } =20 +/// Marker trait for the size parameter of a [`CoherentAllocation`]. +/// +/// [`AllocationSize`] is a marker trait for the size parameter of a [`Coh= erentAllocation`]. +/// +/// The specific types of size are `RuntimeSize` and `StaticSize`. +pub trait AllocationSize: private::Sealed {} + +/// Marker type for a [`CoherentAllocation`] with a runtime-determined siz= e. +pub struct RuntimeSize; + +/// Marker type for a [`CoherentAllocation`] with a compile-time-known siz= e of `N` elements. +pub struct StaticSize; + +mod private { + pub trait Sealed {} + + impl Sealed for super::RuntimeSize {} + impl Sealed for super::StaticSize {} +} + +impl AllocationSize for RuntimeSize {} +impl AllocationSize for StaticSize {} + /// An abstraction of the `dma_alloc_coherent` API. /// /// This is an abstraction around the `dma_alloc_coherent` API which is us= ed to allocate and map @@ -361,6 +384,12 @@ fn from(direction: DataDirection) -> Self { /// region. /// - The size in bytes of the allocation is equal to `size_of:: * coun= t`. /// - `size_of:: * count` fits into a `usize`. +/// - If parameterized by `StaticSize`, then `count =3D=3D N`. +/// +/// # Allocation size +/// +/// [`CoherentAllocation`] is generic over an [`AllocationSize`], which le= ts it record a compile +/// time known size (in number of elements of `T`). // TODO // // DMA allocations potentially carry device resources (e.g.IOMMU mappings)= , hence for soundness @@ -373,79 +402,19 @@ fn from(direction: DataDirection) -> Self { // // Hence, find a way to revoke the device resources of a `CoherentAllocati= on`, but not the // entire `CoherentAllocation` including the allocated memory itself. -pub struct CoherentAllocation { +pub struct CoherentAllocation { dev: ARef, dma_handle: DmaAddress, count: usize, cpu_addr: NonNull, dma_attrs: Attrs, + _size: PhantomData, } =20 -impl CoherentAllocation { - /// Allocates a region of `size_of:: * count` of coherent memory. - /// - /// # Examples - /// - /// ``` - /// # use kernel::device::{Bound, Device}; - /// use kernel::dma::{attrs::*, CoherentAllocation}; - /// - /// # fn test(dev: &Device) -> Result { - /// let c: CoherentAllocation =3D - /// CoherentAllocation::alloc_attrs(dev, 4, GFP_KERNEL, DMA_ATTR_N= O_WARN)?; - /// # Ok::<(), Error>(()) } - /// ``` - pub fn alloc_attrs( - dev: &device::Device, - count: usize, - gfp_flags: kernel::alloc::Flags, - dma_attrs: Attrs, - ) -> Result> { - build_assert!( - core::mem::size_of::() > 0, - "It doesn't make sense for the allocated type to be a ZST" - ); - - let size =3D count - .checked_mul(core::mem::size_of::()) - .ok_or(EOVERFLOW)?; - let mut dma_handle =3D 0; - // SAFETY: Device pointer is guaranteed as valid by the type invar= iant on `Device`. - let addr =3D unsafe { - bindings::dma_alloc_attrs( - dev.as_raw(), - size, - &mut dma_handle, - gfp_flags.as_raw(), - dma_attrs.as_raw(), - ) - }; - let addr =3D NonNull::new(addr).ok_or(ENOMEM)?; - // INVARIANT: - // - We just successfully allocated a coherent region which is acc= essible for - // `count` elements, hence the cpu address is valid. We also hol= d a refcounted reference - // to the device. - // - The allocated `size` is equal to `size_of:: * count`. - // - The allocated `size` fits into a `usize`. - Ok(Self { - dev: dev.into(), - dma_handle, - count, - cpu_addr: addr.cast(), - dma_attrs, - }) - } - - /// Performs the same functionality as [`CoherentAllocation::alloc_att= rs`], except the - /// `dma_attrs` is 0 by default. - pub fn alloc_coherent( - dev: &device::Device, - count: usize, - gfp_flags: kernel::alloc::Flags, - ) -> Result> { - CoherentAllocation::alloc_attrs(dev, count, gfp_flags, Attrs(0)) - } +/// A coherent DMA allocation with a runtime-determined size. +pub type CoherentSlice =3D CoherentAllocation; =20 +impl CoherentAllocation { /// Returns the number of elements `T` in this allocation. /// /// Note that this is not the size of the allocation in bytes, which i= s provided by @@ -644,10 +613,87 @@ pub unsafe fn field_write(&self, field: *= mut F, val: F) { // the UB caused by racing between two kernel functions nor do the= y provide atomicity. unsafe { field.write_volatile(val) } } + + // Allocates a region of `size_of:: * count` of coherent memory. + fn alloc_impl( + dev: &device::Device, + count: usize, + gfp_flags: kernel::alloc::Flags, + dma_attrs: Attrs, + ) -> Result { + build_assert!( + core::mem::size_of::() > 0, + "It doesn't make sense for the allocated type to be a ZST" + ); + + let size =3D count + .checked_mul(core::mem::size_of::()) + .ok_or(EOVERFLOW)?; + let mut dma_handle =3D 0; + // SAFETY: Device pointer is guaranteed as valid by the type invar= iant on `Device`. + let addr =3D unsafe { + bindings::dma_alloc_attrs( + dev.as_raw(), + size, + &mut dma_handle, + gfp_flags.as_raw(), + dma_attrs.as_raw(), + ) + }; + let addr =3D NonNull::new(addr).ok_or(ENOMEM)?; + // INVARIANT: + // - We just successfully allocated a coherent region which is acc= essible for + // `count` elements, hence the cpu address is valid. We also hol= d a refcounted reference + // to the device. + // - The allocated `size` is equal to `size_of:: * count`. + // - The allocated `size` fits into a `usize`. + Ok(Self { + dev: dev.into(), + dma_handle, + count, + cpu_addr: addr.cast(), + dma_attrs, + _size: PhantomData, + }) + } +} + +impl CoherentSlice { + /// Allocates a region of `size_of:: * count` of coherent memory. + /// + /// # Examples + /// + /// ``` + /// # use kernel::device::{Bound, Device}; + /// use kernel::dma::{attrs::*, CoherentSlice}; + /// + /// # fn test(dev: &Device) -> Result { + /// let c: CoherentSlice =3D + /// CoherentSlice::alloc_attrs(dev, 4, GFP_KERNEL, DMA_ATTR_NO_WAR= N)?; + /// # Ok::<(), Error>(()) } + /// ``` + pub fn alloc_attrs( + dev: &device::Device, + count: usize, + gfp_flags: kernel::alloc::Flags, + dma_attrs: Attrs, + ) -> Result { + Self::alloc_impl(dev, count, gfp_flags, dma_attrs) + } + + /// Performs the same functionality as [`CoherentSlice::alloc_attrs`],= except the + /// `dma_attrs` is 0 by default. + pub fn alloc_coherent( + dev: &device::Device, + count: usize, + gfp_flags: kernel::alloc::Flags, + ) -> Result { + Self::alloc_attrs(dev, count, gfp_flags, Attrs(0)) + } } =20 /// Note that the device configured to do DMA must be halted before this o= bject is dropped. -impl Drop for CoherentAllocation { +impl Drop for CoherentAlloca= tion { fn drop(&mut self) { let size =3D self.count * core::mem::size_of::(); // SAFETY: Device pointer is guaranteed as valid by the type invar= iant on `Device`. @@ -667,7 +713,10 @@ fn drop(&mut self) { =20 // SAFETY: It is safe to send a `CoherentAllocation` to another thread if = `T` // can be sent to another thread. -unsafe impl Send for CoherentAllocation = {} +unsafe impl Send + for CoherentAllocation +{ +} =20 /// Reads a field of an item from an allocated region of structs. /// --=20 2.52.0