From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2082.outbound.protection.outlook.com [40.107.94.82]) (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 E5B3D4A1E; Tue, 8 Apr 2025 03:08:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.82 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744081737; cv=fail; b=LQP8B0VI9IAy9W+EkGhQLRtFRBldA6iyF4T7xkNmJH9c2TuFbdreuqHTu/+tWQoOTKpzEWtls7tc5mYtOu66gqF/QStCO8BDzFZ7HCGCq4HrwfTrfsj3b6mToXjvppTHvqQ5trn+N1dNq4P42/8MbVAnxDbOE09/ZsaU73WmCFE= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744081737; c=relaxed/simple; bh=9epIaHHQK8o6xLQqKwNqVSnzZZoZ860wB9KRjeQFiyE=; h=Content-Type:Date:Message-Id:To:Cc:Subject:From:References: In-Reply-To:MIME-Version; b=CjpElIJWuYGNwWmzIvTAw5iPikGqM4yKAZnWxa3zevIA+wFOTSXkw3yc6lLK82xkN6DYW06stvRC5B6D4lM3xoKsMFNCikkCjQ/Kmrjoey0dYiZDe8BlCpWNHJMtNY81C5tOTOLHezmSZlvx0GOWU9l6CEILAugQG2PGptC1VZs= 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=MonFW/qQ; arc=fail smtp.client-ip=40.107.94.82 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="MonFW/qQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HvjcNenitswRaTjfCaWHQ8kfrDQy6hImPMo53XINtTY+FrCxzH/lHeqE42jLHjcyqsMkEApQjJiz15fbc8H/R1ZdeaZ7+H7cizidokVttYv5wjzDfBY0cxw5IGLuiB2UDTwznxie6m/Ak9sOgR8yzisjuIOpHuRcMgTuSvj7WxkWhQHXGVInOqW/CUNbWh2N/4Px+lqIPzE0bJNXVbxqLvBvovrz6i0ycNfwR7mlS3DeADlzzoeyBluTAOntlPARRMXW1DJ3O+7ZLFy6wONQJVSscngmMUDlAcOeYZYyaFmT2MQnFDCMl3ZjGQHdD4w85oZFH5CFbqPCMmDkBsrxMQ== 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=+ZKKTRRF54mKXbKEV/KV8MFAD/i8fH+HycaO6HC1MSg=; b=SyhAJQJ8fiuxyoh3jmnurhRimn3Xr8P2coR1qP6kUkhfes5PXypvDT0MhlFYvVOIGbhCRxjCW7aVYeTytWW3uuFqxO9q5sbctUHH1TI5URnMEiyi0dXTZSirJ0f5yUyrTyVnAkjMSEhoe0PJGmmVeKIWBwhghq+uMDMzXyes2PjYLJDn3KPyDqc/pYJCr8YLt+HnFu3Pv4gu/pEIoPjDBWA7fVgs24c7xsylWodFVI1MPCFEuxIywOCYq0FiMJGgPA67TmwPAGsrGB75bczX+R5BeXxRclVFz10OCBIi1Vhn66qoMtPo1ncC0eIDfH+Yzct2mJHO9gj2qqBVhdgx2w== 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=+ZKKTRRF54mKXbKEV/KV8MFAD/i8fH+HycaO6HC1MSg=; b=MonFW/qQIgOaFpoasjakN+nM0TsklE1e7Bfq4/urXY/mYQmHc8+ddOT3ehnRxbnjyxGZcQZrSkI8pJ+9knPUTzASg5xE2mj3LVjpLrKK2c2H4fVrjhRAR6yAagvAu5nJCVkncONOHC2/Bpvnl8gRmpl8WhXZnaNbHdqHWWDyczOQMBy7vIdw77yZlqiXh7jG46WJUSMLcimvSwZoMYon1xou8S4oWgxAA29l1bif5u+/n79b5ZD/uBTUU4jnie5mxReQwgsy9qkPZ+iUBemRumEcJKgwBiADztf7g/9M5eqyxKFIYilGInUpJQ6P3ZxSLgkTCxNxhaR0u9ltw4WPtw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB3997.namprd12.prod.outlook.com (2603:10b6:208:161::11) by CYXPR12MB9443.namprd12.prod.outlook.com (2603:10b6:930:db::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8583.42; Tue, 8 Apr 2025 03:08:53 +0000 Received: from MN2PR12MB3997.namprd12.prod.outlook.com ([fe80::d161:329:fdd3:e316]) by MN2PR12MB3997.namprd12.prod.outlook.com ([fe80::d161:329:fdd3:e316%6]) with mapi id 15.20.8606.029; Tue, 8 Apr 2025 03:08:53 +0000 Content-Type: text/plain; charset=UTF-8 Date: Tue, 08 Apr 2025 12:08:49 +0900 Message-Id: To: "Abdiel Janulgue" , , Cc: "Danilo Krummrich" , "Daniel Almeida" , "Robin Murphy" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "open list:DMA MAPPING HELPERS DEVICE DRIVER API [RUST]" , "Marek Szyprowski" , "open list:DMA MAPPING HELPERS" , "open list" Subject: Re: [PATCH 3/3] rust: dma: add as_slice/write functions for CoherentAllocation From: "Alexandre Courbot" Content-Transfer-Encoding: quoted-printable X-Mailer: aerc 0.20.1-0-g2ecb8770224a References: <20250326201230.3193329-1-abdiel.janulgue@gmail.com> <20250326201230.3193329-4-abdiel.janulgue@gmail.com> In-Reply-To: <20250326201230.3193329-4-abdiel.janulgue@gmail.com> X-ClientProxiedBy: OS7PR01CA0191.jpnprd01.prod.outlook.com (2603:1096:604:250::10) To MN2PR12MB3997.namprd12.prod.outlook.com (2603:10b6:208:161::11) 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: MN2PR12MB3997:EE_|CYXPR12MB9443:EE_ X-MS-Office365-Filtering-Correlation-Id: 795f83b3-2eee-4c8f-bc83-08dd764ab13f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|10070799003|7416014|366016|7053199007; X-Microsoft-Antispam-Message-Info: =?utf-8?B?YnBTZkdSa3g0dndrTmpZSkxqVkU3M3VTalVIZ2dHODZNUjBOLzhjUjdpdU1V?= =?utf-8?B?ZThCSXlSZE40TEx3cEp5SVI0TUczcjdHNXpnZ2YzVDBKUVdqUlhrSUxGaFVK?= =?utf-8?B?L3FRTlZmMG9DU0wxWW1wNTUvYVpkSjFsMUhmZURuTFNlcFVnMlVzbThYYkdE?= =?utf-8?B?eGFuOUg3UGZQT0VibnVvb09aM2NVRjRtNytDcUkrVTUyNzhSQWgxbzFzRUVW?= =?utf-8?B?dW5pcTliL1NCODE1MXFD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B?aStzNnY2Sk15RENYa2hVc1psZ0FLN3pUeVM4U3VmcmRFN2hYSUc1b1p0Wm9Y?= =?utf-8?B?RmxCZjlvUjVpbjdwaEJYb2x5ZFR4aEM4VmxHc092V0lWUkorNG5JOXVPNXBv?= =?utf-8?B?V3ArQnI0MHpnSENKTERBU2JNNDg5bDNpRHdkSG1md1NKN25uWlVycEc5R2dW?= =?utf-8?B?Ni9DS0svdk1Xa3pNejdkZlBLNFdScFZ1UFh6THZkSGVuSXg2YlNubnZjSVo0?= =?utf-8?B?d1VxdmRlUFN1TzZPMDF2QmR5cWZmaG1ZQzQrVWZYUlFTa09EU0xKNWRwSTli?= =?utf-8?B?WmdlSEt4aEVGUjd1WVhGZDMrK2RQbHVxQkZVZURvdjhFeU53U1BVQlZKMmpP?= =?utf-8?B?enFKSmNRbWpJajVKN0FNSWNNaVBwSHNUa1Q5bWNyd2taeWNJWmYrZE9MVmFp?= =?utf-8?B?RGlIUTVXUGhra1lHQUFCTUxOWUVRb1FRcTRoQ0tvdFIzbklvQVRNdDd2blpx?= =?utf-8?B?N1hZL2ZXOXNlaTNhWFVHRlhKRGQzK3J2TG9RNUgrU2czVFZ3TmtRODhwY1lS?= =?utf-8?B?ems0U0Z3WHFhcERKcy81MjV0MHVYMHd3UE10YmhObHltdmErdTNyaHBvRnNH?= =?utf-8?B?SE5LSUxaMmR2dmpOMnRmbEl5eGVlUkRGYXFlYWFhZ1NwTTdhcmdndUtPSHdP?= =?utf-8?B?ZU9RSnZlbzhnOWdYalhjUjF6bzlSVVdZOE5ZT2sraGhEaHovcTRJTGk3NGRG?= =?utf-8?Q?1jbUzLnwJ/H7C?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN2PR12MB3997.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(10070799003)(7416014)(366016)(7053199007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiS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HajliVmtpSERyQ01G?= =?utf-8?B?cE5BTUFGcHIxWDROaWNlczNHR0l5Z1R6Z3F6ZEVncTU4NndEZ1djQ3NodDg3?= =?utf-8?B?SHFCek8xaisxSE4zN1Q0RWNPQ0U0d25HYlJ0Z0EvcFY4QUp5OG1zKzhkM2ZE?= =?utf-8?B?U0xUMVREQWlDWm9Xa3lxSHZBVUU0bExhSkl6bmYwRGphZzgzaUFxbzFQUGcw?= =?utf-8?B?NnNFN1N4RHhhd1AzYTEzeDlxU0RkMHZnK2p6N2xCZW95S0JXTThtbGY4aHc2?= =?utf-8?B?Q1VHYzBYbE9DbUNwOHptb1pZNnlnd0FHamFOK2RRcGRYZjRBV0FXUzZUZzlo?= =?utf-8?B?U1kvemFxRmpqb2pPeVpEOGRuTmw0TGltNWNyYWVLcnhucDJIZE50SGpJaFNp?= =?utf-8?B?dThxaEVXcHJkaTF0a2lsbXlBa0o0emVETERQZG5GcG9WekFlcjlYc2kyZXhM?= =?utf-8?Q?cnymGL/Ra9+R2YETrH6HE5b7cSF+8I5y02y5GDI3QdaA?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 795f83b3-2eee-4c8f-bc83-08dd764ab13f X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB3997.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2025 03:08:53.2953 (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: fxIOGyOAJplypZb/HPmGPjAjG9mjXQBd0o55BtdUuwZuT6lxt7Ef4vJq0e7GGDSIlpAAfco4g7Alxd2BOtomDg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYXPR12MB9443 Hi Abdiel, On Thu Mar 27, 2025 at 5:11 AM 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 > --- > rust/kernel/dma.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 87 insertions(+) > > diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs > index 24a6f10370c4..24025ec602ff 100644 > --- a/rust/kernel/dma.rs > +++ b/rust/kernel/dma.rs > @@ -218,6 +218,93 @@ pub fn dma_handle(&self) -> bindings::dma_addr_t { > self.dma_handle > } > =20 > + /// 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. > + /// > + /// Due to the safety requirements of slice, the caller should consi= der that the region could > + /// be modified by the device at anytime. For ringbuffer type of r/w= access or use-cases where > + /// the pointer to the live data is needed, `start_ptr()` or `start_= ptr_mut()` could be > + /// used instead. > + /// > + /// # Safety > + /// > + /// * Callers must ensure that no hardware operations that involve t= he buffer are currently > + /// taking place while the returned slice is live. > + /// * Callers must ensure that this call does not race with a write = to the same region while > + /// while the returned slice is live. > + pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result= <&[T]> { > + let end =3D offset.checked_add(count).ok_or(EOVERFLOW)?; > + if end >=3D self.count { IIUC this should be `if end > self.count`. > + return Err(EINVAL); > + } > + // SAFETY: > + // - The pointer is valid due to type invariant on `CoherentAllo= cation`, > + // we've just checked that the range and index is within bounds.= The immutability of the > + // of data is also guaranteed by the safety requirements of the = function. > + // - `offset` can't overflow since it is smaller than `self.coun= t` 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 no hardware operations that involve t= he buffer are currently > + /// taking place 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]> { > + let end =3D offset.checked_add(count).ok_or(EOVERFLOW)?; > + if end >=3D self.count { Ditto. > + return Err(EINVAL); > + } > + // SAFETY: > + // - The pointer is valid due to type invariant on `CoherentAllo= cation`, > + // we've just checked that the range and index is within bounds.= The immutability of the > + // of data is also guaranteed by the safety requirements of the = function. > + // - `offset` can't overflow since it is smaller than `self.coun= t` 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. > + /// > + /// # Safety > + /// > + /// * Callers must ensure that no hardware operations that involve t= he buffer overlaps with > + /// this write. > + /// * 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 { > + let end =3D offset.checked_add(src.len()).ok_or(EOVERFLOW)?; > + if end >=3D self.count { Ditto. > + return Err(EINVAL); > + } > + // SAFETY: > + // - The pointer is valid due to type invariant on `CoherentAllo= cation` > + // and we've just checked that the range and index is within bou= nds. > + // - `offset` can't overflow since it is smaller than `self.coun= t` and we've checked > + // that `self.count` won't overflow early in the constructor. > + unsafe { > + core::ptr::copy_nonoverlapping(src.as_ptr(), self.cpu_addr.a= dd(offset), src.len()) > + }; How about leveraging as_slice_mut() so this unsafe block can be removed? Cheers, Alex.