From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CWXP265CU010.outbound.protection.outlook.com (mail-ukwestazon11022087.outbound.protection.outlook.com [52.101.101.87]) (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 42E37392828; Tue, 18 Aug 2026 14:41:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.101.87 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787064107; cv=fail; b=elk0x8aVbwv30Zdlql92E2PvGqTIh1m6KPxgYVRs5yrPoCWQY1LwDhTHuKS84CQm4Tnj0NtxJdarLQGKG47eHn1C+DvYWZrIsFhiAgav+YDaMxa5/LwO/PN/rliUPT0Fag9JlVKpSrq3IMo7IiOIuiUDQEcjT+iVrGFlFeNX03k= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787064107; c=relaxed/simple; bh=4K68H40lAgEva0xj34IjbIZpTmsblR7AsU3Kk8L6JGU=; h=Content-Type:Date:Message-Id:Cc:Subject:From:To:References: In-Reply-To:MIME-Version; b=epkFlBzcNJMD+n2kkWHeQ2PNuKSRRqHCOO28oBSP76p5ZzJ/goavkFkN+8MHY+VTGU+HVd4o4Huja3BNS+X90+koXrSTPQNdnCgFxLBlM2DOSvQqoYTL5cZZIAP/e4/DqVZNwpdUdrqUfBLG2CsNfI4ktOXrCJpAJhBjSZjrOQU= 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=GXYE/u6Z; arc=fail smtp.client-ip=52.101.101.87 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="GXYE/u6Z" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xxLuQQ4HxBSw2PwxJdqFxKYAun91huYzVZf23b2hQuarpQH2Ox2KFHpp8sbcP/e5qxZNE+wOOn/CaNuB4nzuDHULrMIF/wH+xJQjmWXu+TW/CIfG3Sf6Rt4r4FJDDfDZ5H7VUBgBE/OoqIYCeYRwQ66rEwPN5Ynr9TlScTjpKzDpSG/nsUBu0TQNJqDFXL/w49wefiOpJUACfPcpwKsQhHjza5lK39pVRDEv77P0OoOqwA16BFy1n+huLxozn1vr2EoCdQwzBgVTpgizF0WABJ61BPWsPQqhg1xW4A9u1xTMKTkDET3WifL7LVlw43iHz3JUtkrzITJqwEkgAcf1lw== 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=LyFWeT9fmWP12YdzE16TEhHszGiKqlWXTCfNN2E5o9Y=; b=I7u6alGv+oHMdgQz4elhcPFfcDck2fSEoAHJplIuC6KitOSsaIw5UPndVq5fRAUtnn9qtAJH4c1Ws/mFnhbnIkHjX1PQid1QrUR6X7ktz8hCJ6LpC/rVrhQ1b14TWrP4Jbt2o4BaHyGHR+rjU6us9K9XIocujMbtD2/Nx7PSGFfAdAXtH6UBONQh65CYOjCH6124dfx5JGZczh+2kKlMXPNEJ2fd/6FUCtlNXv5ESQwOrU0V0Fe7akb7pnv6JtTN2uNqT6A70UEEPR5k3gj5MZQKqTR9am3gAknrJ3izxn3flijAcM3w9UvpQwKCYD5fRKgId89myfJodQYwwScwWg== 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=LyFWeT9fmWP12YdzE16TEhHszGiKqlWXTCfNN2E5o9Y=; b=GXYE/u6Z8L02F8GDrpbFNaONa6oOA/SLx37S6Jm6ie1yAzPPgsmrJ1Ew07gek6m4bI+pERk7eMenhRicO7ZMkrTcLc3bdTitIxD1hhjCQe9wQd1hFiacigZBiChbCbxPj63S7E4oX92ZeU3eQ+kYGs1Q8AHPN9NM+RgnB9WOCgc= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:4ab::19) by CW1P265MB8749.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:27a::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.339.8; Tue, 18 Aug 2026 14:41:36 +0000 Received: from LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM ([fe80::f60b:1537:68d7:4fc1]) by LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM ([fe80::f60b:1537:68d7:4fc1%4]) with mapi id 15.21.0339.007; Tue, 18 Aug 2026 14:41:36 +0000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 18 Aug 2026 15:41:35 +0100 Message-Id: Cc: "Danilo Krummrich" , "Alice Ryhl" , "Daniel Almeida" , "Miguel Ojeda" , "Boqun Feng" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Trevor Gross" , "Tamir Duberstein" , =?utf-8?q?Onur_=C3=96zkan?= , "David Airlie" , "Simona Vetter" , "Bjorn Helgaas" , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , , , , , , Subject: Re: [PATCH v2 02/16] rust: io: add `IoRepr` trait From: "Gary Guo" To: "Alexandre Courbot" , "Gary Guo" X-Mailer: aerc 0.22.0 References: <20260805-typed_register-v2-0-c3ca142220a0@garyguo.net> <20260805-typed_register-v2-2-c3ca142220a0@garyguo.net> In-Reply-To: X-ClientProxiedBy: LO4P123CA0658.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:316::7) To LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:4ab::19) Precedence: bulk X-Mailing-List: nova-gpu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LOAP265MB8560:EE_|CW1P265MB8749:EE_ X-MS-Office365-Filtering-Correlation-Id: 17d65380-66d4-4b2a-ad27-08defd36cdf6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|10070799003|366016|7416014|23010399003|10067099003|56012099006|6133799003|3023799007|5023799004|4143699003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: HpmCtaxd1omVfnWQRzq3L5f8hqP6AmAA5y4Ykm4ifsVbGnFBurNNiyRDt3mfoR2dWbnEEv/DkvUDI9wauGxuLg7wnGuypq0SC0SPwc3lK2RaBMV668Nlwm5hTf4+pXI0pVH6MjXtXPA1CIaf2TgaLz3e8h0ezA4XGewejapAxSC41kjBHs2FrgncNNpqj9pgoPIWOOTtc2h76DS/s2378dd8g/b+bC5pAgetQYzBBjUzB0crBdQsASJuHbufWhAOY2kklOKyk8AoV35+TcFmLzr4awkSqzUhZe8rLOyQBN611Dvy2loKpCQeHsay2xCh46qm6iq98BLZ4qrifh/qzvLkL+nCexhEihX90mSliDYII1Pk7iGqYfjyc9LX6SHGPjV3Un7yQ7JG69odKIWJ1S3MD7EV0Ge/TprJdCJnSFJFQmoo4Tbw1IIU3L352GNPdX8oHd+6g4RDsrbrk+Rz4oS0rv62R3lE5iL6C7HrfKaZ8EaYjij0JfAyMwtKxFoolnDAYfg2IAdouRMP4Ah9WwvBx2CGm5Ffi0heyaaLu1mqrbASaZIXn2w6tfu8OWPLEP8eTvJ81xWYCuUzBz9tmw7EaVe1pnnMYvr6fIdMpJEJzEhZY9oQ11YGG2jWsKOXgnLqbMvRrGuEz/cddlqUJSdWJY9ikZ+IpaXH3RmromQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(10070799003)(366016)(7416014)(23010399003)(10067099003)(56012099006)(6133799003)(3023799007)(5023799004)(4143699003)(18002099003)(22082099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?R1luemNPOHArRlFJczdidVoxa0RTb1hpTzJoZWVyUVJjeExwU010dFgxaHkz?= =?utf-8?B?L2FBOTlhc25lZG1mMnB3bHJ1SXcxZk54bDY5VFQ2MUFTOFE0STJLZGRPcTI4?= =?utf-8?B?MzV0aDczWXQydzRUMHBsNXNVWTJIcjlkditGSnlWUHR0TlZnc3g4S1Y0ZkNF?= =?utf-8?B?dWFPMTQzYzFZR3pKeVJyYzdrRXBpWHNIbkJTcUFBclUxNyt6VG0vUThHWHpJ?= =?utf-8?B?NlZtelpOeEY2WkpsVjdRcGFVTmk0emp3bmo2OTJLUUZtb21ZYWZBRHlmVVp6?= =?utf-8?B?MWdIa1FSVmltTk9vUDY2T2dTVG1zVExCYXRYaVozZXZaRGpmQm5DVSs2Uzlk?= =?utf-8?B?UDlrOTVrdDdBYzFvQ1BrOVpNeHJPRjRKMHhWc0xadmcvcForV0JuRGJrUXNp?= =?utf-8?B?OFJMRWJrc1NnME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M?= =?utf-8?B?aDFCK3NDOXFKS2JJa1RYekZWY3hkZlBmTDlabXJ0OGRJSnJpNm1kbmtGWUlU?= =?utf-8?B?TnBkcHl5NHRmRmZFU1c0SHZubEQyejFUaWx4K1lWUEJTcDFkTjk0Zz09?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: 17d65380-66d4-4b2a-ad27-08defd36cdf6 X-MS-Exchange-CrossTenant-AuthSource: LOAP265MB8560.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Aug 2026 14:41:35.9829 (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: 40/BU4Yi8ef2lvwVIOoCjlicVN/zMdKfBGjbp83YkNkpw6ehUPrUXrNNgP54wV6wwnXzthtS8AjsNkqVPNwhyg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CW1P265MB8749 On Mon Aug 10, 2026 at 10:30 AM BST, Alexandre Courbot wrote: > On Thu Aug 6, 2026 at 1:35 AM JST, Gary Guo wrote: >> +/// Trait indicating the underlying primitive types to be used for I/O = operations. >> +/// >> +/// Implementing trait allows arbitrary types to be used for I/O operat= ions, not just raw >> +/// primitives. >> +/// >> +/// The layout of the type and the underlying primitive must match; thi= s is enforced via const >> +/// assertions when I/O methods are used, as the type system cannot rep= resent this. >> +/// [`IoRepr::from_repr`] and [`IoRepr::into_expr`] can be overridden f= or conversions, however it >> +/// should be noted that they are only invoked on value read/write oper= ations and are not invoked >> +/// on byte operations such as [`Io::copy_read`]. >> +/// >> +/// # Examples >> +/// >> +/// ``` >> +/// # use kernel::io::*; >> +/// #[repr(transparent)] >> +/// #[derive(FromBytes, IntoBytes)] >> +/// pub struct MyNewType(u32); >> +/// >> +/// impl IoRepr for MyNewType { >> +/// type Repr =3D u32; >> +/// } >> +/// >> +/// #[repr(C)] >> +/// pub struct MyStruct { >> +/// raw: u32, >> +/// new_type: MyNewType, >> +/// } >> +/// >> +/// # fn test(mmio: Mmio<'_, MyStruct>) { >> +/// // let mmio: Mmio<'_, MyStruct>; >> +/// let val: u32 =3D io_read!(mmio, .raw); // Raw primitive read >> +/// io_write!(mmio, .raw, val); // Raw primitve write >> +/// let val: MyNewType =3D io_read!(mmio, .new_type); // Read via `IoRe= pr`. >> +/// io_write!(mmio, .new_type, val); // Write via `IoRep= r`. >> +/// # } >> +/// ``` >> +pub trait IoRepr: FromBytes + IntoBytes + Sized { >> + /// The backing I/O capable type. >> + type Repr: FromBytes + IntoBytes; >> + >> + /// Convert from [`IoRepr::Repr`] to `Self`. >> + #[inline(always)] >> + fn from_repr(repr: Self::Repr) -> Self { >> + transmute_neo(repr) >> + } >> + >> + /// Convert from `Self` to [`IoRepr::Repr`]. >> + #[inline(always)] >> + fn into_repr(this: Self) -> Self::Repr { >> + transmute_neo(this) >> + } >> +} >> + >> +macro_rules! impl_io_repr { >> + ($($ty:ty =3D> $backing:ty,)*) =3D> { >> + $(impl IoRepr for $ty { >> + type Repr =3D $backing; >> + })* >> + }; >> +} >> + >> +impl_io_repr! { >> + u8 =3D> u8, >> + u16 =3D> u16, >> + u32 =3D> u32, >> + u64 =3D> u64, >> + i8 =3D> u8, >> + i16 =3D> u16, >> + i32 =3D> u32, >> + i64 =3D> u64, >> +} > > ... and `IoRepr` and its implementations for primitive types should also > be part of `transmute` IMHO (after being renamed to e.g. `Repr`), for > there is nothing I/O exclusive to it. It just indicates that one type > can be represented by another, a property that is again useful outside > of I/O. > > That way `bitfield` gets a dependency on `transmute` rather than `io`, > which doesn't break layering. > > In order to avoid `Repr::Repr` we can also rename the associated type to > `Raw` and update the method names accordingly to `from_raw`/`into_raw` - > which would have allowed us to remove the `bitfield` methods of the same > name if they weren't needed in const context! But at least it makes > things align nicely. So I am working on this design for `kernel::mem`: pub const unsafe fn transmute_neo_unchecked(val: Src) -> Dst = { ... } pub const fn transmute_neo(val: Src) ->= Dst { ... } // Round-trip transmutable. pub unsafe trait AsRepr: Sized { /// Primitive representation of this type. type Repr; unsafe fn from_repr_unchecked(repr: Self::Repr) -> Self { ... } fn into_repr(this: Self) -> Self::Repr { ... } // Name from conceptually having this (not actually added) // fn as_repr(this: &Self) -> &Self::Repr { ... } } // Bi-directional transmutable. pub unsafe trait AsReprMut: AsRepr { fn from_repr(repr: Self::Repr) -> Self { ... } // Name from conceptually having this (not actually added) // fn as_repr_mut(this: &mut Self) -> &mut Self::Repr { ... } } This design works well with both `Atomic` and `Io`: atomic can use `T: AsRepr` while I/O can use `T: AsReprMut, IO: IoCapable<::Repr>`. One thing that I ran into is with signed/unsignedness of repr. Say for exam= ple you have #[repr(i8)] enum Foo { ... } Then it'd be more natural to have type Repr =3D i8; Similarly if user specified u8, then we would naturally want to put u8 ther= e. However, to avoid duplicating signed/unsignedness code, `Atomic` and `Io` w= ould need to pick a preferred signedness. So far, `Atomic` uses signed integers, while `Io` uses unsigned integers. Would it make sense to canonicalize everything to unsigned integers (even i= f users explicitly specify signed integer) for this trait, and convert `Atomi= c` to use unsigned types as impl (we can always cast sign back in the impl before calling C)? Best, Gary