From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CWXP265CU010.outbound.protection.outlook.com (mail-ukwestazon11022114.outbound.protection.outlook.com [52.101.101.114]) (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 CBC593321A2; Mon, 2 Mar 2026 20:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.101.114 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772482494; cv=fail; b=TPLZNF+sZDg8CLTDEwHcii6kv9bUdqcnJB4tzg4nPKLpfqHdfm0k3cs1aKY5Zi5eGsz1J/Av6hSs+vNQPqDJs786Je20w67LnmNWOumXz7xbXTihOR84zlubofVBhhUPsC0iE4NhDsatdtyPY8bsv9OrbqAzueCVv6qipUHv+UE= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772482494; c=relaxed/simple; bh=v9Dm2YvzHeL+zpEypnpmEEvw6GJw/gihCuaT7vhxxHA=; h=Content-Type:Date:Message-Id:Cc:Subject:From:To:References: In-Reply-To:MIME-Version; b=KRUcC0U5cQQPhG2dn3dU+b9D30wwymEMES6LH/4kobMe7i/1cqCYekgIJDu6TEa1OluhpViyjFc0qUzYf25La8TBDQThzR0vmzwwx/XodjQTsOVsmWlE0XCwz9QOKMLLxFxyJbkfFvFnfPI2FVrm1h7+JWHW0Qxtg0DZ0kv1QAw= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=garyguo.net; spf=pass smtp.mailfrom=garyguo.net; dkim=pass (1024-bit key) header.d=garyguo.net header.i=@garyguo.net header.b=eYDKJMXJ; arc=fail smtp.client-ip=52.101.101.114 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=garyguo.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=garyguo.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=garyguo.net header.i=@garyguo.net header.b="eYDKJMXJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=q3dJ/KTLBHxOaZFczWfW18zB61Z3IDH+vc/UexbPkWY2yyuBhxoNjI8Fnj7imix38nqZ/upwUfNcB3fSwVTub9MfaOZrbgiKUO+sA1UsqVPCbLtLQWL/p6dtB7M64jDIlQFLaJvA4sKuE7qrl14MP9TalnIM4GNG8YqOM9w4kDHmAAvNBNzDD3W1cYlhOqooSwwrjSsheJxq+l4erABeNlSONc/2/qPdtRzhyUfrmC71dxbT8O2ej0kD6dDwamQyUg/F2D/b5du/9WiweDHjvK/SRNY1nd2osdETWqZOwRT4USpn+Q7MQP0uFH44j4f7Dkebt4n0PSuyHDGHP9stSA== 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=OTjMY5MgDvkqiSUbAJWtcHlN8YWDcDQT3SX3fXrAjEo=; b=prQA6kghAKJ+4KGirjkH/VL2f69drowr1jFMMpY4LTHNWz/qjVB0k0zYvWkR0yQTN2VC+k/bjhca6jXOXs6WX5stCs671+4ts+0gFTXzs1/PS38WEUR7f3mF5/5RbjpxzO0pZj1/8eakathhNbUWnKtCxbKIobxp4YrxoNn1K161c4leYeo4RczAIEig3rNdY+YntEsh77dWNVxMF7gfI6YXt/+ZYfnoSXeKCRmklVvn8GD1PB2mEfQqIT57A1mWRvKo/Alax9Y0Wyj6kXwRDV/GTSx5DuvMVEhq2dBKs2IgunCKMN0m/t2WQiDdAKKVA9QKF6iykuTsNEg714R09A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OTjMY5MgDvkqiSUbAJWtcHlN8YWDcDQT3SX3fXrAjEo=; b=eYDKJMXJ308WYDBhI2Z7FDrw3FzeQfnEbhHefIpfpeNYzVU49GLhoHb0EI0Kl1IoeGCgreU3sG1grLHsnCqCAw1/bi5qTBOparY+ETy1WIuhTeJO5Rrku8Oe/nlNY4rj9GRz8G9nBPWeOnbWb70D1Mp1WA/x58Ndu4VaLfRxLro= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:488::16) by CWXP265MB3366.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:e5::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9654.21; Mon, 2 Mar 2026 20:14:49 +0000 Received: from LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM ([fe80::1c3:ceba:21b4:9986]) by LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM ([fe80::1c3:ceba:21b4:9986%5]) with mapi id 15.20.9654.020; Mon, 2 Mar 2026 20:14:49 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 02 Mar 2026 20:14:48 +0000 Message-Id: Cc: , "Aditya Rajan" , , Subject: Re: [PATCH v3 1/2] rust: add projection infrastructure From: "Gary Guo" To: "Benno Lossin" , "Gary Guo" , "Miguel Ojeda" , "Boqun Feng" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , "Nathan Chancellor" , "Nicolas Schier" X-Mailer: aerc 0.21.0 References: <20260302130223.134058-1-gary@kernel.org> <20260302130223.134058-2-gary@kernel.org> In-Reply-To: X-ClientProxiedBy: LO4P123CA0247.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a7::18) To LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:488::16) 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: LOVP265MB8871:EE_|CWXP265MB3366:EE_ X-MS-Office365-Filtering-Correlation-Id: cd2093ca-d3f9-4840-2f88-08de78985b36 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|10070799003|366016|376014|7416014|921020; X-Microsoft-Antispam-Message-Info: eG/KZIq3DXt9N07t3K2uTQNXpXOTBeayy8jqhkLamPRCtq0mrHYy+VUsj1U7UfYPTcaU3Bd6Ld9MiRWJmP/fnS1jo1ELD+sKN57Y1UX3NzcccjYq4sLgYp0k4qzpeEscjEMLffUuiXwvNukkZT4rFCva6U8HnYqx5hJmr4DTc7j+WfbnfiJAfBW8ROaLOlH9Cx3bkLTSHh9DkAiBM5Fa2w+tXEDUgHgvpk1lS0VxJMloD0rLOzK6Lf6Tkw36kjBlslqQ4oXp+0jrgxzJ4SI1FLzA+M7QRShamKeLYi8jDa1szG7iFsclrYthIiEHqVVqDhiPaHUjbU5NngFhcaBDZL0Lwn4YCw+e8EXj+ip/2Wz8xJK3V5mZ8yPfmpFB7RTOiqnKRPSpJz2yom5mZ+NzW5KUNbEGNzfdJMr5dRf+So41MT+uBUxQw6+YdnqjWmzSJ1ojZcANqwmSulQNzBSZMdYPplRjQHAtiDffed3RwdgZntyeDWfL7M01X2+LG+fzV4azvUxBqkA2AJBOMx0LmGjiyrh4bNr6asL90BFbIV8MHBTglNmXumZu5KSel72NMGeMatc3yFjNpdsWgiFpDy5SrDTbJTzmk4olD2PLO2o82q2SxNZc46QBj/ADha+CNOl0JvfVmEyfC3EAb0LkKsddCFvfWCvBCmKwhffU57bRhji0G/4tQb43ng1ZeU+dxrPeV/Z8MwESgSPcAjJ0Eso/vmbaSO2hEZPLZori5V8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(10070799003)(366016)(376014)(7416014)(921020);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?MVFucUd5N2RnVFU1RzNYeVE3Q01ZVDdBak5rYytlSE1UNkw2V2c0NGZQZWRS?= =?utf-8?B?Y0w2NkhiMWcvS3A2N0JOWUE4RlZuK25Xb2Qya1JXK2x2dmRjNE5RaDE4WU5G?= =?utf-8?B?RkI0Y1lKK2J3NDNsTE1haCtmZ1Q4RjlOc0M4OXJHUldoVjNWd3Q3Mm54T1Ey?= =?utf-8?B?VFhORFJTVXorM2NsZ2JGbmZCUXVFRXR0WlZRNUg0dUpPYnJCMVVOaWVjd1Rp?= =?utf-8?B?WThreFZlZlM1U0tiOHdabjAxeEFNaGhUS2d3VWRoM1JNUTYwd1ZKOW5HVUxC?= =?utf-8?B?bWJKVUNWTHBUOU1Tc2dBNXJzUm5UT1JWQ2pQTDV6S2prK2xOMTI1UUlNZ0V3?= =?utf-8?B?M0duOU5ZOUphNVU0WlRwWHJuZG9xRWYyNFlvVXdoUDJwZnJsWDFiUHhNOElZ?= =?utf-8?B?YlRhaGNueHRtNUNlbGlYcnBvWWJjTDV1b0M5YXpLYWtHYnhZc1IxeHJhL2FC?= =?utf-8?B?QXdSaERrWWJYNlpHY21TazVvVHp3V2pUdWthZGRGeTRVRmhQNXAvVzA3ZFFl?= =?utf-8?B?V3JYRGwrcDF6R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: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: cd2093ca-d3f9-4840-2f88-08de78985b36 X-MS-Exchange-CrossTenant-AuthSource: LOVP265MB8871.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2026 20:14:49.4708 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GYcXspxQKcKDV9r7tMUZwd2h6yHgsZE6dQsVaNZpXXopE2UYKevPMuc5xHssyxYmpI2IaUVDyt5R2FYjFUwstA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CWXP265MB3366 On Mon Mar 2, 2026 at 6:49 PM GMT, Benno Lossin wrote: > On Mon Mar 2, 2026 at 3:49 PM CET, Gary Guo wrote: >> On Mon Mar 2, 2026 at 2:38 PM GMT, Benno Lossin wrote: >>> On Mon Mar 2, 2026 at 2:02 PM CET, Gary Guo wrote: >>>> +/// A helper trait to perform index projection. >>>> +/// >>>> +/// This is similar to `core::slice::SliceIndex`, but operate on raw = pointers safely and fallibly. >>>> +/// >>>> +/// # Safety >>>> +/// >>>> +/// `get` must return a pointer in bounds of the provided pointer. >>> >>> This only makes sense when the provided pointer already points at an >>> allocation. But since the functions of this trait aren't `unsafe`, it >>> must be sound to pass `ptr::null` to them. >> >> The "in bounds" here is the conceptual bounds of the pointer. So, for a = pointer >> with size `x`, the address of the returned pointer lies between `ptr .. = ptr + >> x`. > > Okay, I haven't really seen that as a concept. Also, what is the size of > an invalid pointer? It's `size_of::()` for sized types, and `size_of::() * slice.len()` f= or a raw slice pointer. The projection semantics is same regardless whether it's valid or not. The = I/O projection work will rely on this, as many I/O impls will act on pointers t= hat are not "valid" in Rust sense because they refer to a different address spa= ce. But they're still legit pointers with proper meaning. > >>> I first thought that we might be able to just use `mem::size_of_val_raw= ` >>> [1] to give an upper and lower bound on the address of the returned >>> pointer, but that is unsafe and cannot be called with an arbitrary >>> pointer. Interestingly, `ptr::metadata` [2] can be called safely & with >>> any pointer; I would expect them to be very similar (except of course >>> for extern types). >>> >>> [1]: https://doc.rust-lang.org/std/mem/fn.size_of_val_raw.html >>> [2]: https://doc.rust-lang.org/std/ptr/fn.metadata.html >> >> I have a `KnownSize` trait for this in my I/O projection series that is >> implemented for `T: Sized` and `[T]`, and it returns the size when given= a raw >> pointer. >> >>> >>> A pretty expensive solution would be to add a sealed trait `Indexable` >>> that we implement for all things that `T` is allowed to be; and then we >>> provide a safe function in that trait to query the maximum offset the >>> `get` function is allowed to make. >>> >>> Alternatively, we could use something like this: >>> >>> The implementation of `get` must: >>> - return a pointer obtained by offsetting the input pointer. >>> - ensure that when the input pointer points at a valid value of typ= e >>> `T`, the offset must not be greater than [`mem::size_of_val_raw`] >>> of the input pointer. >> >> Given that I'm not introducing `KnownSize` trait in this patch, this is = why I >> haven't used this kind of wording. Perhaps I can just bring `KnownSize` = in early >> and use it first for documentation purpose only? > > That sounds great. > >>> Or something simpler that says "if the input pointer is valid, then >>> `get` must return a valid output pointer"? >> >> Hmm, wouldn't this give impression that "you can do whatever you want if= the >> input pointer is not valid"? > > Yes that's true, but why is that a problem? A impl that returns an arbitrary pointer when given a null pointer is not v= alid. I/O projection will use the ability to project on null pointers, too. An ex= ample is PCI config space code, which will project using null pointer as starting pointer. The "bounds" projected pointer must still be with in `0..KnownSize::size(pt= r)`. > >>>> +#[diagnostic::on_unimplemented(message =3D "`{Self}` cannot be used t= o index `{T}`")] >>>> +#[doc(hidden)] >>>> +pub unsafe trait ProjectIndex: Sized { >>>> + type Output: ?Sized; >>>> + >>>> + /// Returns an index-projected pointer, if in bounds. >>>> + fn get(self, slice: *mut T) -> Option<*mut Self::Output>; >>> >>> How about we name this `try_index` instead of the general `get`? >> >> I'm following the name on `SliceIndex`: >> https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html. > > Hmm, the methods in that trait are marked as unstable under > `slice_index_methods`, which doesn't have a tracking issue, so are > perma-unstable? I'll suggest the rename upstream as well. I mean, they're named after the `slice::get` method [1] and `Index::index` method [2], so I don't really see the naming issue here. [1]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.get [2]: https://doc.rust-lang.org/stable/std/primitive.slice.html#method.index= -24 Best, Gary > > Cheers, > Benno