From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CY7PR03CU001.outbound.protection.outlook.com (mail-westcentralusazon11010010.outbound.protection.outlook.com [40.93.198.10]) (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 BD7AA378D7B; Thu, 30 Apr 2026 11:24:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.198.10 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777548256; cv=fail; b=qOJN9Wf/cjAjc2UzC26Or4oasEA5jNoSol+XtA0iKznVxXri3oSRV/e1pyHgxfbNh45dphlPYW4bHUBlRG4Yi3DcQCV7s71nlNk6v4NcYgvl5kfud1AJ12oiaa2OA3eK0asQhYx9ZorOymkZHcA3/XcCGtO0ojVaRviq7gH1Mo8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777548256; c=relaxed/simple; bh=cLT769gC4lIXuvy4uMiU3xaZZHThOmJioLXekRizLZw=; h=Content-Type:Date:Message-Id:Cc:Subject:From:To:References: In-Reply-To:MIME-Version; b=KOVuQ3rtaz8X5S/OsWpy8EvrzAH4RpaTL2FrvlgYJLgBp2AO6arbMNh7398Jw0mZ5CI8Ye1ROI5mcis2pKWA7gnx+zUcllyCpLHKLEVUqEL/ODt+bG1P+OeRnMV47zbbwpd3xOo/Suf5sabmOrM212GNCaTaWbyFPXRV9F1KPgA= 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=L+ARXhWJ; arc=fail smtp.client-ip=40.93.198.10 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="L+ARXhWJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ux2pjP+qzRO/+K4dSJefrofMx/0yVWf2nC9Ij4jMY3j5dKWCl+14LSAmgefZp/MlbtJJAQql3EfC7scJOBBgbrdw+17wV4jZDtzcywKpBlvYpanoOhUOT/wZoKQPpZIQNJddPzvmiXfqTRFNYXEXYuuuNg9grcNLgw41lQjSDgBFHFSd5T2wauQBRwqpCwIJazd6ikcFPvYKZKQBCF0M9TdXFhomOmEkKtqYd02Q3D4GoZbdRR+qYrPEXIIPJZk/SGrvRYKsHKDzESVY1E75PB2uvTcRGSKu2dcCCc+gTobNAHKIhClD5MrMYxGURsyHxL4xE8m1GyZx5L3stoiTiQ== 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=3Pf+SXqQqxLRjLO0A5gQ0qhj2W1j4Q8GR35yJ6qqop8=; b=qqhy1MgWcxqXtLcxstGkJeLKdd5Zwa0P1TrE5WxygebSE13661SUlCCR3MyULTdZ/zDBmos0xH+kUKAzLSHZPhiHlKk78cgPHS1qj7wTsAuiy0PAdJvLHnXoZdTKSUWyVPxZXyNlA8EZ4O8VcQHi4x5nVksOg6CXnL2DrOUIpXb3VUQMYSSIuHtAUiuaGG2Kprq3rsESS8FG0eZudH8HHtc9z/v4DBE5v/IxKfUbc3eLYcPKpZ93oS/1MVthQKxXMtnSNsD9Ddfc20pFXxIH61s8wzAFaGfSGxD8xSbegbENQgAiTUazYw1K+1wen98jWWVg3L4lIn7SS8BF9REONA== 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=3Pf+SXqQqxLRjLO0A5gQ0qhj2W1j4Q8GR35yJ6qqop8=; b=L+ARXhWJJbQWiH/0uyax1YYmvg2Axe5JRPP+URkE+eWZNYMUu2iEA1hXFWeftYFUPGSLn6CIEDjBIyvUXdy+tEqYSPCfZSFXa61+Phb+xGQurMdZjCjHSS9mzAjZbfEEDILl2wqyOh2rj/zf7O9SQPMCrlHPq0QiO0SnOGMrEaDiNFkiM8xIPndjExFcOFNNv6w5t2oV68Cy3OPF0jtzROpoZ6EeudCaO3gRDzfSzTvKqrfzv2QPE8Ih37Ifvr9mKaESztSMYhatAA2aJbfMc9FPbhJOawGy4PUCqgS7CHctCSoKo8FvS4SjIGcrdgRhvv6tiVczdQoC0qAN1zq7rQ== 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 DM4PR12MB6469.namprd12.prod.outlook.com (2603:10b6:8:b6::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9870.21; Thu, 30 Apr 2026 11:24:09 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989%4]) with mapi id 15.20.9870.013; Thu, 30 Apr 2026 11:24:03 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 30 Apr 2026 20:23:59 +0900 Message-Id: Cc: "Danilo Krummrich" , "Abdiel Janulgue" , "Daniel Almeida" , "Robin Murphy" , "Andreas Hindborg" , "Miguel Ojeda" , "Boqun Feng" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "David Airlie" , "Simona Vetter" , , , , , Subject: Re: [PATCH 1/5] rust: ptr: add panicking index projection variant From: "Alexandre Courbot" To: "Gary Guo" References: <20260415-projection-syntax-rework-v1-0-450723cb3727@garyguo.net> <20260415-projection-syntax-rework-v1-1-450723cb3727@garyguo.net> In-Reply-To: X-ClientProxiedBy: OSTP286CA0038.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:224::11) 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_|DM4PR12MB6469:EE_ X-MS-Office365-Filtering-Correlation-Id: 643abb0f-48f2-4e57-d47e-08dea6aafb69 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|10070799003|7416014|376014|1800799024|22082099003|18002099003|56012099003; X-Microsoft-Antispam-Message-Info: xiGG0JrlXi6QxyN7nOqJWlEkqhCpP2hRc/GKiE+CW4IX7JOcgJbUc2Jvcp7q3K/vvFazG/XnAmDJX1JTMSpmDNHSrnQJLLUz79bC0UISdIpKJ2A8va4O6IxoO8ff7oYh8U/T7JNCPmaIPE4z7ksKPyqGGztpaQn0//z+zgzh9xC95zTgdbvhqAhM2/6CvFGdYgi5zT3AzjoW9k75e6N5IdQUh74aBYIHq5FPzN3MGYb32Q2V3YS+l9LvwyZs7Jh7Xa7RllogpVzhAhGI1reVIPodXZmXnkRzYzYFP1/D2YvrFmsPStA10cpGyo3a47XZ9PMk8jnwYUF3PPNK8E4ufEFnyeBtt6/fn/pMyX3rmUhjaH3V03npf9VEz9HxYHW2IssPcviAB2LjtSc3J9Wkeb3bYvFJ8EYGMgkiy2ExDdK1jcGgISv1dxmYUh70ytmHYXkfVLFC99tf2QaDa8ajdXxmNPteRNT9NTvXu2t8T7FSyczt9qnV1udOa2NN39kxCynVe/W/DDB7UNTIXkRHUzoDxDSR4FUYzrnwKO4dggdoa8lQ0TsvLlI0MhtveLiIPsyztyii1QXVSsPwy1Rz7+3gP0k1jouJl5+oc4xF7UVogqjeCqW0ROmzGvYXUklhQMH3qXTw2t5UWq/fcRE2+XvoNmXxxIvtlv05RoWqs5ER2tHhw3wJ6pRn61E1x62fl3/d6YGHnKXD2VcOefNozjznLye40TSwcH7Q37MR1Cg= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB3990.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(10070799003)(7416014)(376014)(1800799024)(22082099003)(18002099003)(56012099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?MUhEM0JWU3Bwcys5c3I4bDV0ME12RVc4bVhWdXU3TGNtMXJmTVlXSzVCcGlu?= =?utf-8?B?RXRlUWJiZUdFNmhMbExDQ0k5VDBhc3haVTJUWkt5R204bFZMRjZWcUFRdnVx?= =?utf-8?B?aUFEaW53M0JEMmEyYTIvK1U1eGdGVlBnRTk4UEZEazgxNHRRWElkQVN0d09U?= =?utf-8?B?MkxtUFFta2xhblIrOEVJU01xZUhhUEZmdDZ6RHNrcE1ZTlFQdnhoalQ2N0JU?= =?utf-8?B?M2E0aS8yaUpacGNmdE9kL3NvRXUxMm9Hb0pEYnFMZXBRZEd6YWJ5SjNTMFlp?= =?utf-8?B?bzlqaEpIdFM1SjNTZVE5Ni9Ua3hjYmYwTUxEck1ERnVLK201NWVvZjRMVHd2?= =?utf-8?B?YnJwMW95NXNDZERB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=?utf-8?B?R2dqbC9lclR0VW5yL1ZFTDhnK0JyckNFZ0twUHc2dFZ4MjBvdDJ1MGRyLzhp?= =?utf-8?B?OUU3NWo0blM4ckZWQkVVMHZ1clNGRzB6TjI2NFhXSHVCNDVURWQ1TlljTkFy?= =?utf-8?B?aFoxTCtRbEVueEJEcm5PZWY3UDFReURoZ0wvTUpaTDF0KzNqVlZ1V1V3WGto?= =?utf-8?Q?DFQG1375wSKYurQESoUWe/uERZazm6rluNeZWDDpju8iT?= X-MS-Exchange-AntiSpam-MessageData-1: RLTYMNQ2JkIihw== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 643abb0f-48f2-4e57-d47e-08dea6aafb69 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Apr 2026 11:24:02.9573 (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: DudyOw9Vj4LBgWkTSfPRrYQLL0kzOFnvdNfJq/jP9n/wZ1qKVfWrr9vXVSh94Kd1oamlmsJhrGxy2Q4d4tqw6g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6469 On Wed Apr 29, 2026 at 8:29 PM JST, Gary Guo wrote: > On Wed Apr 29, 2026 at 12:22 PM BST, Alexandre Courbot wrote: >> On Thu Apr 16, 2026 at 4:57 AM JST, Gary Guo wrote: >>> There have been a few cases where the programmer knows that the indices= are >>> in bounds but compiler cannot deduce that. This is also >>> compiler-version-dependent, so using build indexing here can be >>> problematic. On the other hand, it is also not ideal to use the fallibl= e >>> variant, as it adds error handling path that is never hit. >>> >>> Add a new panicking index projection for this scenario. Like all panick= ing >>> operations, this should be used carefully only in cases where the user >>> knows the index is going to be in bounds, and panicking would indicate >>> something is catastrophically wrong. >>> >>> To signify this, require users to explicitly denote the type of index b= eing >>> used. The existing two types of index projections also gain the keyword= ed >>> version, which will be the recommended way going forward. >>> >>> The keyworded syntax also paves the way of perhaps adding more flavors = in >>> the future, e.g. `unsafe` index projection. However, unless the code is >>> extremely performance sensitive and bounds checking cannot be tolerated= , >>> panicking variant is safer and should be preferred, so it will be left = to >>> future when demand arises. >> >> Review nit (no need to respin for this): this patch would probably be >> easier to review if the renaming of `index` to `build_index` was split >> into its own patch. It would reduce the diff, while also removing the >> burden of having to keep in mind that `index` means a different thing >> depending on whether the line is removed or added. I've found it a bit >> difficult to keep track of this. > > This'll need a respin anyway (to merge with the IO view series) for patch > logistics reason, so I'll make that change. > >> >>> >>> Signed-off-by: Gary Guo >>> --- >>> rust/kernel/dma.rs | 3 ++ >>> rust/kernel/ptr/projection.rs | 98 +++++++++++++++++++++++++++++++++++= -------- >>> 2 files changed, 84 insertions(+), 17 deletions(-) >>> >>> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs >>> index 4995ee5dc689..3e4d44749aaf 100644 >>> --- a/rust/kernel/dma.rs >>> +++ b/rust/kernel/dma.rs >>> @@ -1207,6 +1207,9 @@ macro_rules! dma_write { >>> (@parse [$dma:expr] [$($proj:tt)*] [.$field:tt $($rest:tt)*]) =3D>= { >>> $crate::dma_write!(@parse [$dma] [$($proj)* .$field] [$($rest)= *]) >>> }; >>> + (@parse [$dma:expr] [$($proj:tt)*] [[$flavor:ident: $index:expr] $= ($rest:tt)*]) =3D> { >>> + $crate::dma_write!(@parse [$dma] [$($proj)* [$flavor: $index]]= [$($rest)*]) >>> + }; >>> (@parse [$dma:expr] [$($proj:tt)*] [[$index:expr]? $($rest:tt)*]) = =3D> { >>> $crate::dma_write!(@parse [$dma] [$($proj)* [$index]?] [$($res= t)*]) >>> }; >>> diff --git a/rust/kernel/ptr/projection.rs b/rust/kernel/ptr/projection= .rs >>> index 140ea8e21617..845811795393 100644 >>> --- a/rust/kernel/ptr/projection.rs >>> +++ b/rust/kernel/ptr/projection.rs >>> @@ -26,14 +26,14 @@ fn from(_: OutOfBound) -> Self { >>> /// >>> /// # Safety >>> /// >>> -/// The implementation of `index` and `get` (if [`Some`] is returned) = must ensure that, if provided >>> -/// input pointer `slice` and returned pointer `output`, then: >>> +/// The implementation of `index`, `build_index` and `get` (if [`Some`= ] is returned) must ensure >>> +/// that, if provided input pointer `slice` and returned pointer `outp= ut`, then: >>> /// - `output` has the same provenance as `slice`; >>> /// - `output.byte_offset_from(slice)` is between 0 to >>> /// `KnownSize::size(slice) - KnownSize::size(output)`. >>> /// >>> -/// This means that if the input pointer is valid, then pointer return= ed by `get` or `index` is >>> -/// also valid. >>> +/// This means that if the input pointer is valid, then pointer return= ed by `get`, `index` or >>> +/// `build_index` is also valid. >>> #[diagnostic::on_unimplemented(message =3D "`{Self}` cannot be used to= index `{T}`")] >>> #[doc(hidden)] >>> pub unsafe trait ProjectIndex: Sized { >>> @@ -42,9 +42,12 @@ pub unsafe trait ProjectIndex: Sized { >>> /// Returns an index-projected pointer, if in bounds. >>> fn get(self, slice: *mut T) -> Option<*mut Self::Output>; >>> =20 >>> + /// Returns an index-projected pointer; panic if out of bounds. >>> + fn index(self, slice: *mut T) -> *mut Self::Output; >> >> This looks like this could have a default implementation: >> >> fn index(self, slice: *mut T) -> *mut Self::Output { >> Self::get(self, slice).unwrap() >> } >> >> I'm sure there is a good reason for now having this though, so at least >> for my education: why not? :) > > It could, but the implementation is going to be dead code. Would the generated code from this default implementation be worse than the specialized ones you wrote? I'd have expected the compiler to optimize things in such a way that they would be equivalent, only with less code. Haven't looked at it though.