From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2046.outbound.protection.outlook.com [40.107.101.46]) (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 A98822D7BF; Mon, 2 Jun 2025 13:06:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.46 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748869582; cv=fail; b=DOQvElERcCLrpzIYhlEJU/girkSvxYZKLD2yZy2h83de7a4bUs+D37A2mOSp8wNZknsGgyLfNx0ZUjzJQqY1DwGlvfByiwyVmLMl5X2o3665758TVAJiu/Fr01OVRKZ6rYDJiNpM+8LWPop4HqfBS3/ysl3XrFczLV8Z0RXsuis= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748869582; c=relaxed/simple; bh=2WQn0y2rZ+SdCwUQ5XF7JLOcqNtla8AQAcRFLpKHOGQ=; h=Content-Type:Date:Message-Id:Cc:Subject:From:To:References: In-Reply-To:MIME-Version; b=uSDi1mdCAdTMbGBR15RErVpPwvwvw+B1G40w3kaqliu76W233RMrvorPND15+kWc1AQm1ge+QrNWdo511Ma8qO6a4r6KJFqE7ORS9ilBvociJuQHMfFCAegIqDPCfje/Wa5ppN+ink3NWSPmKOYl+0PlOFaCL3QWfv1q+ct56pE= 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=bkNOyjuF; arc=fail smtp.client-ip=40.107.101.46 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="bkNOyjuF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wf/iOxOCDVwjiQLsajOmtzpiWdeH+swbRmuNqBMLp3dTTEkMsJ06LnvDbNn78uGL7lMPHWjRhg4yCmU0VJDZI///n7mBc3c6uBfIEIKC1WDO8Z5nwfvWFm8WhDrAFv9WHF4WnlLQ7BXp3+O6QNOWGzA3M9m4fLRAy5ZNTBXj1Lsm1Cu6Bt8F0EtZZmXCvkMXsBg9u/L2Gh67TrC6DtCo4gFzDRj2sGORDLpAUYQLHbwGy8jdlGeOKirQ9drR0PUidpjfypLgS9f5lU3pbNgP8vebtIww55lM5fEHZsB6PCjYpJAxRgRqwNL3nLAGVifXATzDRwllIPzZOBIf18T/Tg== 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=Rn8hokviY+4Lwy/I5e7uW6cKdFvZGhZ+wuewoYBB2zQ=; b=GUxNnR+3PsPnTaeBD7+e2adMSzBNtKAu37LqAUtcq236avGHTtSXCEe9NCr8tLpOUnqTrIuarc68BRuJ9Xyw7Bi/Spzl67/h/MR5t7PWxYleqvQG+qfTJg6QjFA31+JV/lDIaeLccOU1lpqF/FnLLirQIgytkw63zjlF7XlbTwcdMt8bCB/Vf3zNc28XQJAOZ4Isk4/+BUTf0d5xWQHgjytykdph8UeIQyVX4N+f3x4aGXVapk9Fg1HHvKnAaeMMbCALH1/F6dK780D91Xmeh/9ZU4eW7IVGYv4VSfhbQRKXnuqtbbg5lRebHnCsV/gEMx5Gwmj5Z39izU7CMD1w6g== 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=Rn8hokviY+4Lwy/I5e7uW6cKdFvZGhZ+wuewoYBB2zQ=; b=bkNOyjuFVJVdtsRuVOj8Ko07A/yF9JVjHzkilZ7xmVxON5R8xNHDCsZv7nmSItUa8y0bXSG3v300xiSFOcTpDbdQv7qzU6SMdQCPITuGQrrWN2w8zvx18PB2iAR1YnbcePF4NTVGUtkOTkCcxJ26iBCDjrrjDrTXIilELtiNEbgdy4e8CcXmLZlGXpwf55ImzgtIHdQbyVB5E9mnPcgfmdteKQd4Yx6nlJvieXZCHG0MUm30qyjEX2+NfFBc/vGrI310TME0TwgMvHg/TX+v/zBNE8vdnDMmebZq1B8c9zDBmJ7LYn2WNAFavHFidev3EBB+LRBlQgI4ptM1LYSzsA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) by IA0PPF1D04084C7.namprd12.prod.outlook.com (2603:10b6:20f:fc04::bca) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8655.33; Mon, 2 Jun 2025 13:06:18 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::6e37:569f:82ee:3f99%4]) with mapi id 15.20.8769.037; Mon, 2 Jun 2025 13:06:17 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 02 Jun 2025 22:05:53 +0900 Message-Id: Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Valentin Obst" , "open list" , "Marek Szyprowski" , "Robin Murphy" , , , "open list:DMA MAPPING HELPERS" , "Petr Tesarik" , "Andrew Morton" , "Herbert Xu" , "Sui Jingfeng" , "Randy Dunlap" , "Michael Kelley" Subject: Re: [PATCH v4 3/3] rust: dma: add as_slice/write functions for CoherentAllocation From: "Alexandre Courbot" To: "Abdiel Janulgue" , X-Mailer: aerc 0.20.1-0-g2ecb8770224a References: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> <20250602085444.1925053-4-abdiel.janulgue@gmail.com> In-Reply-To: <20250602085444.1925053-4-abdiel.janulgue@gmail.com> X-ClientProxiedBy: TYCP286CA0315.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:38b::9) To CH2PR12MB3990.namprd12.prod.outlook.com (2603:10b6:610:28::18) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB3990:EE_|IA0PPF1D04084C7:EE_ X-MS-Office365-Filtering-Correlation-Id: 7bb1046a-9e84-41c1-b6f5-08dda1d64318 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|366016|1800799024|376014|7416014|7053199007; X-Microsoft-Antispam-Message-Info: =?utf-8?B?NFNCZjc4Z0Nyd1hpaHlvM2gwYWo1MUhSSGczbjlkQ3lnNERaUlZWZ2srQ2Vr?= =?utf-8?B?R1BNbVNYR1VnREJmeTAwWmhaT0F5TUlQZlNGNjZzTzhVQ3Azc1NvSXlYN2Uz?= =?utf-8?B?WUxtYkVGYmdKNWtiYUhPUjJxcnNuR0pWWWJseURlVUdBdUs3U01zaFk3OHhX?= =?utf-8?B?QVlKSnBXeUFI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:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(10070799003)(366016)(1800799024)(376014)(7416014)(7053199007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?OVplRTF4SjM3elpveCt2UWVjT2EvQ0FtUlpKUS91OWxuU3AzWGZOdS8vajdE?= =?utf-8?B?cTdsUjUwTjAvM1pkZG1oczBNalB2cUIvMUxsTjhJQ0RlalFXak5VT1VVdmJP?= =?utf-8?B?OExCTzk3aEdNMllLUFFlY3lKcmNVYndkZnFLMzV1bVpSb2M1b3lac09XU2g1?= =?utf-8?B?MDNSYXNDQm9UMnFSL1lrNHNLdmpXWEpMYkNqeFdDV1l2MHJNY1BhNzhsZXpx?= =?utf-8?B?VW5XOUF6VFozeExYc0lMMWoxMEI2eDF4aGRaQTdQL2tDc1hFZnhXNVlsdVZH?= =?utf-8?B?UzJJMkpsT3lCOXk2aGRnSnRkZWZUei9VZlBOSVMzVVEvVy9VaThhQStPUHdw?= =?utf-8?B?TmE0Y2RjTWl4WERlSDMrR25qa0VudWNmMnFhRFQxQ2JkRUJaRkNiL2E0K3Bl?= =?utf-8?B?SzFzcHR1eHJtcFJQbEJTS1BObHl1TTkybUhkMmttVitYK0RYN25ibXZBMS8y?= =?utf-8?B?Yk1va2NBbW44SFZUVFd2TW9zWGFWT2IxS3JicXlpSUV6ZDVnckdGcGxzaW45?= =?utf-8?B?QmpEbUlrL0N2YnRrMk0zeUQ2VUkrY1NyRC80SVhJVU1WdzlBQUVobHI5WWda?= =?utf-8?B?dWxHYXEzTlFYenFrSCtQZksya2pYZE5Gc0dGWi9pZlVSeHpZcmNEYWo0RGFr?= =?utf-8?B?TUVUdFFMR2x1ZnVjU2NzaThSSWovMGpaY3huUTVBY2U5dExlZ2VJWXNPakhJ?= =?utf-8?B?N21UL2htV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f-8?B?Wkg2UVhsVkt4WDIxd2Z1Y0tVOUI3R3doNktUamtrbVFidjVJRjV1M0YxQURr?= =?utf-8?B?YVVmSGRtRGRHN1JDbjVEcy9RcnFpNW4yN2dxSHNENm5EaVZ1emlYQnNLdklw?= =?utf-8?B?aTViT3ZjTHg1RmgrRVUwMHF0ekVIeVp1S0lvNzhjY1M2VTZlbHIvRDR6ZzlN?= =?utf-8?B?Nm52SkZpay9TYWQ3VVBsK3BRNGRJdU1uRjdoL2psTTlpdi9xTGloWnpyUnpK?= =?utf-8?B?enpucm9YUURvb25oUUs1OVlzZW9TTEdOSkVDQW9mL3MzYWRXTHp3N09UQ21a?= =?utf-8?B?VTdWS1JMR1ZnMk1ET1A5WC9PdGlYRXE2MFJTaUY3c3RVZ2RSeFRxT00yUGJn?= =?utf-8?B?MkpoRlFwNUI5THh0bER1UllhL0RqWU9JU3FSblRqdWFjZVEwTmtSQVhrMzk2?= =?utf-8?B?T0h5amZhRURYNStoSGJIS3FkZStyRjJrYXA5cXRTZjVsa25vZWMreXJ2dWZC?= =?utf-8?B?OTBIckU0TkZ3Ylc0dDRUcHcwL0g0U0ZnbFdBbjhQcEhUdmt1b3VjbXJham9U?= =?utf-8?Q?ELSDKzDW9Rgba+tWdjS8NE2Bj+hqTpujVAPYFEZ927VI?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7bb1046a-9e84-41c1-b6f5-08dda1d64318 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2025 13:06:17.8935 (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: JAVH2hu8nmXRbnMVG8vTNNPLNz2sX70PcM3Jq0u3QGaYJXdFFtO0EpL+kLopvSgHvCLOSGAcB9Eh8Ww0iisRiw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PPF1D04084C7 On Mon Jun 2, 2025 at 5:53 PM JST, Abdiel Janulgue wrote: > Add unsafe accessors for the region for reading or writing large > blocks of data. > > Reviewed-by: Andreas Hindborg > Signed-off-by: Abdiel Janulgue A couple remaining nits/questions below, but FWIW: Reviewed-by: Alexandre Courbot > --- > rust/kernel/dma.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 86 insertions(+) > > diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs > index 5a690e5f1e66..b486f63c1d3a 100644 > --- a/rust/kernel/dma.rs > +++ b/rust/kernel/dma.rs > @@ -218,6 +218,92 @@ pub fn dma_handle(&self) -> bindings::dma_addr_t { > self.dma_handle > } > =20 > + /// Common helper to validate a range applied from the allocated reg= ion in the CPU's virtual > + /// address space. > + fn validate_range(&self, offset: usize, count: usize) -> Result > + { > + if offset.checked_add(count).ok_or(EOVERFLOW)? > self.count { > + return Err(EINVAL); > + } > + Ok(()) > + } > + > + /// Returns the data from the region starting from `offset` as a sli= ce. > + /// `offset` and `count` are in units of `T`, not the number of byte= s. > + /// > + /// For ringbuffer type of r/w access or use-cases where the pointer= to the live data is needed, > + /// [`CoherentAllocation::start_ptr`] or [`CoherentAllocation::start= _ptr_mut`] could be used instead. > + /// > + /// # Safety > + /// > + /// * Callers must ensure that the device does not read/write to/fro= m 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, offset: usize, count: usize) -> Result= <&[T]> { > + self.validate_range(offset, count)?; > + // SAFETY: > + // - The pointer is valid due to type invariant on `CoherentAllo= cation`, > + // we've just checked that the range and index is within bound= s. The immutability of the > + // data is also guaranteed by the safety requirements of the f= unction. > + // - `offset + count` can't overflow since it is smaller than `s= elf.count` and we've checked > + // that `self.count` won't overflow early in the constructor. > + Ok(unsafe { core::slice::from_raw_parts(self.cpu_addr.add(offset= ), count) }) > + } > + > + /// Performs the same functionality as [`CoherentAllocation::as_slic= e`], except that a mutable > + /// slice is returned. > + /// > + /// # Safety > + /// > + /// * Callers must ensure that the device does not read/write to/fro= m memory while the returned > + /// slice is live. > + /// * Callers must ensure that this call does not race with a read o= r write to the same region > + /// while the returned slice is live. > + pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Re= sult<&mut [T]> { > + self.validate_range(offset, count)?; > + // SAFETY: > + // - The pointer is valid due to type invariant on `CoherentAllo= cation`, > + // we've just checked that the range and index is within bound= s. The immutability of the > + // data is also guaranteed by the safety requirements of the f= unction. > + // - `offset + count` can't overflow since it is smaller than `s= elf.count` and we've checked > + // that `self.count` won't overflow early in the constructor. > + Ok(unsafe { core::slice::from_raw_parts_mut(self.cpu_addr.add(of= fset), count) }) > + } > + > + /// Writes data to the region starting from `offset`. `offset` is in= units of `T`, not the > + /// number of bytes. Reading this sentence it occured to me that `offset` may be ambiguous here, as in my mind it rings as being in bytes unit. How about using `index` throughout the file? > + /// > + /// # Safety > + /// > + /// * Callers must ensure that the device does not read/write to/fro= m memory while the returned > + /// slice is live. > + /// * Callers must ensure that this call does not race with a read o= r write to the same region > + /// that overlaps with this write. > + /// > + /// # Examples > + /// > + /// ``` > + /// # fn test(alloc: &mut kernel::dma::CoherentAllocation) -> Re= sult { > + /// let somedata: [u8; 4] =3D [0xf; 4]; > + /// let buf: &[u8] =3D &somedata; > + /// // SAFETY: No hw operation on the device and no other r/w access= to the region at this point. > + /// unsafe { alloc.write(buf, 0)?; } > + /// # Ok::<(), Error>(()) } > + /// ``` > + pub unsafe fn write(&self, src: &[T], offset: usize) -> Result { Can this function be written by leveraging `as_slice_mut` and `clone_from_slice`? But doing so might require `T` to implement Clone, so maybe not a good idea (OTOH, aren't types implementing `AsBytes` implicitly Cloneable?)