From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010026.outbound.protection.outlook.com [52.101.61.26]) (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 54C491DE8B3; Thu, 9 Oct 2025 12:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.26 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760013451; cv=fail; b=mrY48G70WnXLKLjARvP3u+dDrqaqVqrFL6XhvtsyEbDkxofvb4sE1Z/3cY3ZHdTRkHZU+nLRcA4/z+UUh+OvdLsdPECyUm19XmCiZZGLNpzKxCisasDMZWN6vga0pcqklphYKgIrdaKfQukiEQwsCD3Bue2kKWnsD/xOZaLridc= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760013451; c=relaxed/simple; bh=FlqfpM63lTATrwPzZ6wvhllHbQ2X2X4cWFJ1L06ykU0=; h=From:Subject:Date:Message-Id:Content-Type:To:Cc:MIME-Version; b=LyptcTRjmJ4Qq2WpD0qYErtpNQ6H26olj2mE56ZblQv5VEFtSTLQxTYQb+6STfNKaagkxDDIzTnlXfwtPxV/sROP+ZHwr+BoC7+m1RANfPBbkn6UIXzYwBMVld3E1h4Fi+pTdFldz2ejdOI9GHUq/X6wVbpam/M7vSabxdBn2EI= 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=RvjLUsdt; arc=fail smtp.client-ip=52.101.61.26 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="RvjLUsdt" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nZqXoEOSVB8HbvXNcQJwTJPNwa9+LY6jkiAqaybuARSWRQf18aKHlZEo8XGXg8sQknRABChgN99NAIQOiOKJY5AHvXTI83oWPDKSCqxwzdJBENnnaXMEKi433ymuXppnW7NmiXZ9YEmgUOP4hpGmEwe4rw2cka5CrI2nw4Mnb0OOGfoPYes5GAi4nUi40hl2asZUwnszJvT5hNyEV1H0s+nAyU4gfpIdtNGivlbpIm2+CiQ5OcbmJbsrmI+naxeUN10ye+ErkKkHLtyVz6va+iK+I1aIxdMsFiJAEj4bxfw13BpFs2Iho+ks0HiEELCi6FVHzSHSGKSSJKGx/aT0Cg== 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=sYX5cR4zQYQpuAlWZaj+ZDSMXOj5oC6uxW01PAaN8/E=; b=KjXu9WlnOFM7BxiYR6h5rqDKYv0pnYBQZXmOygHx8AFeDuSAeQXbQ1fh5NKy9VBdYh4QuYSk47gLUds/8W4QUoLecJja+DR6l1Y8jGdOH7BTPLG36N5VJfChwyngz3MLXpHpmQs23WDSliQqdFKydGxOSx4ALD6j+ZSvlcnM55DlSLhwPTvwCq+6FOWjvU4lPl9t2DpfXZz359J0Q6wI2E0ZLXkmbKKOUK70OIzGtNmtV8TFFDzgZdfyxRnD7jc50xi0EUWCF34e7xFzBbfI5hcLdS6c1MWzFMLYtH1GU9lZBuVvOCYdfth5H706ebkhtX259IRaiot0ZaOvapCDng== 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=sYX5cR4zQYQpuAlWZaj+ZDSMXOj5oC6uxW01PAaN8/E=; b=RvjLUsdt1XtYZeLL0Je4H+jBECtTBzPeD3EI15XkCL5li6VxfPZqwI/WRMs9ZglbbFcB9WHiLOZfSp1UZNLvt34pyDH53ShZI0P9SjcwqHrP4KCmYOgz/ZQboUR79u9oYLTfVfjS7oGXWMSPN547T1GuMchmxtwmhCZ90hnpOPl6F4GBoTGmMurhVIDs465HOM99FUY98Z3rxPf3GvvwPqUMtAcj5mZQwC8XgGcvdeOyAeswTwZkSH/otwk5WDbjxS9Ugw3wbLsAkO4Zt6WQEuOIf6ZtpYldL225iU44tuB9Qo36bIVRQpf7WrmNH8hWmz/9+GEYopYr9LZf0cPuEg== 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 CY8PR12MB8315.namprd12.prod.outlook.com (2603:10b6:930:7e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9203.9; Thu, 9 Oct 2025 12:37:25 +0000 Received: from CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989]) by CH2PR12MB3990.namprd12.prod.outlook.com ([fe80::7de1:4fe5:8ead:5989%6]) with mapi id 15.20.9203.009; Thu, 9 Oct 2025 12:37:25 +0000 From: Alexandre Courbot Subject: [RFC PATCH v2 0/3] rust: bounded integer types and use in register macro Date: Thu, 09 Oct 2025 21:37:07 +0900 Message-Id: <20251009-bounded_ints-v2-0-ff3d7fee3ffd@nvidia.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAHOs52gC/1XMywrCMBCF4VcpszYyCUm9rHwPKRI7UzsLE0lqU Erf3Vhw4fI/cL4ZMifhDMdmhsRFssRQw2wa6EcfbqyEaoNB4zSiVtf4DMR0kTBlpQmt29HBs2m hXh6JB3mt3LmrPUqeYnqvetHf9QeZf6hohYqoxcHt0bK3p1CExG/7eIduWZYP3avtFKkAAAA= X-Change-ID: 20251001-bounded_ints-1d0457d9ae26 To: Danilo Krummrich , Joel Fernandes , Yury Norov , Jesung Yang , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alexandre Courbot , Edwin Peer X-Mailer: b4 0.14.3 X-ClientProxiedBy: TYCP301CA0030.JPNP301.PROD.OUTLOOK.COM (2603:1096:400:381::20) 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_|CY8PR12MB8315:EE_ X-MS-Office365-Filtering-Correlation-Id: dd4de3e4-eb87-4e38-e7ed-08de0730995a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|7416014|366016|10070799003|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?B?aCtPR21HMTNWSjYwZjVma2VFTHRPZnRkZ0RsSENtcmFLSXRwNzljM09MNGNY?= =?utf-8?B?UmQxaWJEdmpXbzM5U2VlZmYzbWFvTTV2NFBqY1RKNGs0a3dvazkxZ1ZpcHVX?= =?utf-8?B?dkg3bFNUVk5VMjNqN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U5zdFI3bnIySDlUNEFM?= =?utf-8?B?MG9jT25ybW5WNThIUVZpTDRrc3licmxCV2tGZ2czTTZrbUZYRE5TRUtUbHlh?= =?utf-8?B?MlBLL1U3eW5CaEliWWNIQ2tYVzBkL3ZTN2ZqaTZsN2VzTlZlZHhmQmUvdjN3?= =?utf-8?Q?5VDQCd?= 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)(376014)(1800799024)(7416014)(366016)(10070799003)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?K1N4dyt3T1IyUllyMWFtc0J5b1lNcUV3Tnl6V1gvVUkyV1N2NmVKY2NSU1E1?= =?utf-8?B?S09SQzRTaElmNzZVWFZZN0g0RERqQUpaZmZOWHFzZXpQbTUxdTljTytodVo4?= =?utf-8?B?aTNFSUhvOWpqT2VNNWt6RUNzR09oOWFzeEg5ZjVmbVZENlIvNFk0N3hFSWxp?= =?utf-8?B?VG9xemF6Z29uQmc0d1VqYmpnMG9sb0l6RUxUenRIYWt0RWZPUVZPNm83ZzRO?= =?utf-8?B?YmdXV285SElHNUY5UTFMS2dsYmpMeVlvTVRJUFNhbTFVbXBkeUw2bUVBc05q?= =?utf-8?B?NVRkemc2L0lMUUNGcVVFWkhVT01BUStMcFdja0Y3d1BTMEtEM0h6emxSSURn?= =?utf-8?B?OVEvZVF1R2tWREJlWS9VU1hBZE9XRWNMVk0va0s3Smx3eDh2T2ZacW5rdWxl?= =?utf-8?B?TjhkT1k0T1NQWXZxSG5FVmVldzBhaGtmYk03aGpXdVk2STgwR2Z1UnhONlV2?= =?utf-8?B?S0lyUkdNYzFlb283cy9memYwVjRGUnljNE9uZXhQWFRTR0ZWM3ZjYng2aFdW?= =?utf-8?B?RitLRzVRcT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dUhl?= =?utf-8?B?V2ZHeHlJZ1JFOVh2NEt0QWtIcWl5Qm5FR1hRSHFwN0JYU2xxb2NLNnBkcytr?= =?utf-8?B?aVk2ZlZXSTRYaThZRGFlVEQzdlZ4UjRVWkozeDcrVHV6dW05SmJTZmVsYWlO?= =?utf-8?B?L1U1dFh2Sy9pZWhOUTJhaWFMcC9GdXowcEVRcHZlWTE1TUtLTW9Nb2VnS2Qv?= =?utf-8?B?L3ZGTzA4M2pONCtsUXVGSldlelp0RGRRVmVRZGFwajI5T2JKNTI1TTNSc0c5?= =?utf-8?B?ekEyUTRNV29UeGxzd3o4R2U3Q0VsZDU4SlNraXNHRkExTUtISUx4YkJ2QTd4?= =?utf-8?B?K1ZpUTl3cU5XcDYwbC92N3hxcWZHb1VDamxHMXpWVFZUeExIc1RDck9oSm9w?= =?utf-8?B?dG9ORG9jN1cvcHZnZWNXd2tyMi96Y3R6SWtYRlB4ZWFWamlmb2svVVhoNVRy?= =?utf-8?B?eGJkZ09hS0FpZm56MDJBOUk3QXkrWVByK21vOHA2RmY1dU9qSWR2OHMyUVJw?= =?utf-8?B?L3IyYTNYQ3k1T2RiaTZ4UGFwbXNsYTRlaUxPdjJ2Nk42WFQwclExT1FCUGRz?= =?utf-8?B?Q1ZVZGxGTnVFSXFxQ05sZUZxYWJLTmlwdkVsYWJWbGxFUzlrcTAxZXpxdkFM?= =?utf-8?B?RHQrMnNtTDhYci9vVTE0K2ZMZHROaEV4NkRqMlJZMittV0I3aVpMWU5QTDZ0?= =?utf-8?B?Vm4vOGlmOXJzRWk3ZWRuQjVVKy95aUpSQlgzaGIzcVJtVEJTd0RoKzhZKyt5?= =?utf-8?Q?fCbqRapiXJ1DfDgJizeu67PtmQmaqtrHdSxTW68d2k3E?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: dd4de3e4-eb87-4e38-e7ed-08de0730995a X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB3990.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Oct 2025 12:37:24.8547 (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: nJqXvXsQ9FZQ6CD9jU8kJidzRVBxstV0SBMfk7XwcxW5ZiEtHZ0JSOM68os5C5nCTBwAk2NKQiBVGgjtopec1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8315 Second revision, much more complete, of the RFC/PoC for an idea I submitted a bit earlier [1] regarding how to handle truncated bits in the register/bitfield macro field setters. Currently, the register macro allows to define fields that are shorter than the primitive type used to set them. For instance, in the following register: register!(NV_PFALCON_FALCON_DMATRFBASE1 @ PFalconBase[0x00000128] { 8:0 base as u16; }); The `base` field is 9 bits long, but is set using the following setter method: fn set_base(self, value: u16) -> Self which discards bits of `value` higher than 9 without any error or warning if some happen to be set. And so you happily start a DMA transfer from the wrong base address... While this behavior is not UB, this can also be a source of bugs. Several ideas have been submitted to not let these unnoticed, including making the register setters fallible, or panicking or printing a warning whenever extra bits are discarded. These solutions are either too risky (like panicking), or add extra code and checks on what can be a critical path as registers are commonly accessed in interrupt handlers. In pure Rust fashion, we would prefer to guarantee at compile-time, whenever possible, that no bits are discarded when setting register fields with a non-round number of bits. This PoC proposes a possible solution for that. It introduces a new `BoundedInt` type that wraps a primitive integer type and, using a const generic parameter, limits the number of bits that can actually be used within it. This is similar in spirit to `NonZero` or `Alignment` in that it provides a static guarantee that the value it contains is limited to a certain subset of the possible values of its primitive type. Instances of `BoundedInt` can be constructed infallibly when the compiler is able to guarantee that the passed value fits within the allowed bounds, or fallibly using `try_new(primitive)`. This type is then used by the register/bitfield macros to let the fields getter and (more importantly) setter methods work with the exact number of bits they can handle. For instance, the above method would become: fn set_base(self, value: impl Into>) -> Self which guarantees that no bits are ever discarded by the setter, since the type of `value` carries an invariant that only the 9 lowest bits can ever be set. It is then the responsibility of the caller to build the adequate `BoundedInt`, which very often can be done infallibly, but all the cases that require a fallible operation are cases that the caller should have checked anyway (lest you beam out the wrong memory region on your DMA transfer!). Compared to v1, this version of the RFC is much more complete. It notably provides many `From` implementations for conversions from/to bounded types that cannot fail, which removes a lot of the friction one would expect when introducing new integer types. Another side effect of this is that the bitfield definitions are considerably simplified, since their type can now be automatically inferred. This means that 8:0 base as u16; could become simply 8:0 base; And the getter/setters would work with a `BoundedInt` (provided the bitfield type is backed by a `u32`). For convenience, this PoC is based on drm-rust-next. If we decide to proceed with it, we would do it after the patchset extracting and moving the bitfield logic [2] lands, as the two would conflict heavily. Patch 1 is a pre-requisite that should be merged to nova-core, but can be considered external to this series. Patch 2 is the `BoundedInt` implementation. Patch 3 makes nova-core use `BoundedInt` for its register macro, and serves to illustrate how callers of the register field accessors are affected by the change (rather minimally imho). Feedback is welcome! If there is no pushback I will remove the RFC tag for the next revision as I think the `BoundedInt` implementation is already rather exhaustive. [1] https://lore.kernel.org/rust-for-linux/DD5D59FH4JTT.2G5WEXF3RBCQJ@nvidia.com/ [2] https://lore.kernel.org/rust-for-linux/DD68A3TZD9CV.2CL7R7K4UAICU@kernel.org/T/ Signed-off-by: Alexandre Courbot --- Changes in v2: - Thorough implementation of `BoundedInt`. - nova-core fully adapted to use `BoundedInt`. - Link to v1: https://lore.kernel.org/r/20251002-bounded_ints-v1-0-dd60f5804ea4@nvidia.com --- Alexandre Courbot (3): gpu: nova-core: register: use field type for Into implementation rust: kernel: add bounded integer types gpu: nova-core: use BoundedInt drivers/gpu/nova-core/falcon.rs | 118 ++++--- drivers/gpu/nova-core/falcon/hal/ga102.rs | 5 +- drivers/gpu/nova-core/fb/hal/ga100.rs | 3 +- drivers/gpu/nova-core/gpu.rs | 9 +- drivers/gpu/nova-core/regs.rs | 139 ++++----- drivers/gpu/nova-core/regs/macros.rs | 264 ++++++++-------- rust/kernel/lib.rs | 1 + rust/kernel/num.rs | 499 ++++++++++++++++++++++++++++++ 8 files changed, 783 insertions(+), 255 deletions(-) --- base-commit: 299eb32863e584cfff7c6b667c3e92ae7d4d2bf9 change-id: 20251001-bounded_ints-1d0457d9ae26 Best regards, -- Alexandre Courbot