From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.219.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796356122; b=E8rDpjZ0VC5/A159ZxjP/OPqvMnXkqQeG4HBUE4+3Kj8mY0MpjGauym1DRDiTpTkNmbF I2wLsWp2XjPn28RhYlrYET13cRvTP+wOK2s+3/d6o+b9KQ6Cv8fD90M7Kf8mPS30w/Rtc pxKeIcWXZqnxnFSaqhkr6rl3vVglSyHFtullTmkFYSkYOQRVG8360MmuwPA2syYnZNZqv 7LQsyzf1/znRF2Nxil66LB3q2yI3NTt+81nEg/t8Qkq5nHN6i4/PwsYowBS5wSmzxE0AG qIyG+gaIMjxctOkB2V49T2RqkF8xVpOzdqeUiuBPTbrusr3fttvHPXkGoWbH3DFVUmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796356122; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=DIimMg0D4x49oVslfs0fxTX6L2UwEJWWVYdGF90ZKRE=; b=E47h077MrvUCLCYABHQYD5+UNxKR6sfLeTqbug6pBbN65taBjQHTJtR0En9kmmhPfpgX 6r+miEPAXgzcl4BXwxbPDQkwkYbb30KTSEVYHzZvsbm31joJFKnNakirUw6QzoQv7NkTN Q96AS6XwAmG2C/O1y3We7g+covclUJKeWk6ClsEE/zCj2C0PXBev+WOfn1yEFiqRsrpiL 9VDEJZRSmiglxYtyiHTS7D2ZsfvLeQyTpbUixWU3ABrL/wH1PWklEn8yFAEhoxia2Jyjq SetzrNbhB5ACjP9PY7IEfJZSXqWqsGExMXvAXrSSYfnGV0rBTG88oY1BpTsVhF8yEmw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Nibvjlk5; arc=none smtp.remote-ip=209.85.219.42 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1F7A606A335 for ; Fri, 4 Dec 2026 03:48:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C1F7A606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Nibvjlk5 Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-921df18a675so2878006d6.1 for ; Thu, 03 Dec 2026 19:48:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796356121; x=1796960921; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=DIimMg0D4x49oVslfs0fxTX6L2UwEJWWVYdGF90ZKRE=; b=Nibvjlk5rX6VSb6jozdOvfQFFGLF23dqi65SJGcz/BMQ/mtP3VNB4PaXGhQ5+ocChy +sxuU2GEr2vSRb7b/DnK9aEbtqmiP9eldfbPO42Q3s3ZIl4IsmTzNIiD0IpbERTfBnip 02eap3ONZwyRt/S0ADkluOkZwUDaoE16OGEmfM5JCNHLSmfs0WH7t1OK5aKocJOdwlFU T01E9h103ZSMgrSRsUIiaO2pJUIsUA4wg2dEANmruwtSDY/8xPjpHkhmuMt6mYYPglAp gjq0KXZpObepQJ5RQjK2ZW86/QbqFf/xTTXGyxZqTnxqFzQAk6vxg84fNOUuGqAeSfIy +npA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796356121; x=1796960921; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DIimMg0D4x49oVslfs0fxTX6L2UwEJWWVYdGF90ZKRE=; b=JADZD4Hge6vvudIYHVv1aHs61vr0ZQCusOYWrAksV4UnAnF6a+V1mbDdp48sQx8MlE Y0Oivxj2O8mlxm0T5V0woeP3AgcwA/nI0gTw1eASs/IK4/D03IHq6vr60G+mHb4DTCcY Cb8AppEsHKN8gyWMGSzwY35XUquAbFsxr+wIVwClBQqb+voFGypHX7YvuoKMdwwiic6t Rl1kv+PcVzlkBTKLSAs64kkQEivs4mx+BVhu0YEUAE/TmqxUBv/BaqOFX2OKwILaGUab Yjm2xNdpzWnjlBTnmXz0eNimtXzLZIh0ssTG2mf8xHE1dUiz8xOXk0ewNbg22gl5o1ey Loyg== X-Gm-Message-State: APuU4Z8Ahphx0qWel3Wp8wTNbSV2nzaNjCxTe1MwS25nWY5xXNKSese2 1B7nQWMtd2FDLtWvEoMUSmNuUKMrSf4OmzmzLX7i0yinw1EqRhWEMoZmQQmWSHoM X-Gm-Gg: AZFdTZeqdEceFqI/b6Em+HEWkIWvJlFTW+y/wY9gJ+snBTYCk9qeJejQs02sdplArSZ of0drqE9VgYXt16tYk7gkpvjh/8H1HB+4sJG0ulC1b3ikV5s6DeX4wjpCj74w2aWSgima4no0LW PkoWMXmbk8qfgrelfB1+1DibnR6seyobpnUvnhQv/AvCpZyA73MD4XgMkDVp2aPKD7JW3sNc/Kq Eyted/Mw8vQEiIK4Tm+pAoYGvstAcnz3YnywvijtJKpKB7oZ26KsWE947rjdimgLk1tJsWfbMW1 ksulrZ4Seb/c1b05rVL9D4vdJvPiF7BBUp9OBv9d7F5PZBJoPy1seEwirGvy8k1P1GBRwK/vQGd Z6rOiEWIqcwb11DjjVKmYrAaBC8rpfBB+JYaHRY0M5KEni5cJJWZTFNkdR9EcP/dyMklhym7oHH LodmtungwHB1Ym2dZ+v+2MtHfTe1F4TRjL34ina5Sp7ZT0+kEq2m7/rkotQxvwkOOov9r+B/ClX nmPW/D9E7ZS977DS25/4ekzMR2Z5xcMJwD6kloLWlpqR8BtdO0MF81MDlnFfK9IHlWekqEQzBx6 foPY5dYE60nf5jw/pVwnZJlXzB6IKGB1b8vDKh8fWGON0rjikH9qk0+PRotQUbJ6ccmIRoZQMED M7D68m3k+W1oRSui1xfxCTT35b4GhckFqhbd63OLjrBZedA== X-Received: by 2002:a05:6214:3d9b:b0:921:3a0b:673f with SMTP id 6a1803df08f44-921e23b0874mr11363916d6.7.1796356120719; Thu, 03 Dec 2026 19:48:40 -0800 (PST) Received: from ArchDesktop.local (w-md-584f3aa9d2-143172-1.tingfiber.com. [64.98.249.166]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-921e05627a5sm12766766d6.10.2026.12.03.19.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 19:48:40 -0800 (PST) From: Boming Kong To: rust-for-linux@vger.kernel.org Cc: ojeda@kernel.org, acourbot@nvidia.com, gary@garyguo.net, Boming Kong , Danilo Krummrich , Alice Ryhl , David Airlie , Simona Vetter , Yury Norov , Boqun Feng , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Daniel Almeida , Tamir Duberstein , =?UTF-8?q?Onur=20=C3=96zkan?= , Greg Kroah-Hartman , Jakub Kicinski , Alex Deucher , Andrew Morton , Jens Axboe , nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org Subject: [PATCH v2] rust: bitfield: implement compile-time bitfield! overlap check Date: Thu, 3 Dec 2026 22:48:27 -0500 Message-ID: <20261204034830.117532-1-michaelkongboming@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This commit implemented compile-time bitfield! overlapping check to avoid unintended overlaps between fields within a bitfield! and cause subtle driver bug. This commit also introduce per-field #[allow_overlap] for user to opt-out overlap checks. Updated nova-core's MMU ver2.rs PDE and PTE definition to adapt this new function. As register! builds upon bitfield!, it inherits overlap checking without requiring changes to it's own implementation Previous discussion: https://lore.kernel.org/rust-for-linux/20261130081359.158313-1-michaelkongboming@gmail.com/ Signed-off-by: Boming Kong --- Changes in v2: - Enable overlap checking by default. - Introduce per-field #[allow_overlap] - Update affected nova-core mmu v2 definitions and existing tests. - Drop the separate register! patch, as register! now inherits overlap checking from bitfield!. Note: overlapping fields without #[allow_overlap] would fail to compile as intended, but KUnit cannot reveal this case so I verified it manually. Is this acceptable or are there any preferred ways to test this scenario? drivers/gpu/nova-core/mm/pagetable/ver2.rs | 2 ++ rust/kernel/bitfield.rs | 38 ++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/nova-core/mm/pagetable/ver2.rs b/drivers/gpu/nova-core/mm/pagetable/ver2.rs index d7169a0fcf..59f6f40c88 100644 --- a/drivers/gpu/nova-core/mm/pagetable/ver2.rs +++ b/drivers/gpu/nova-core/mm/pagetable/ver2.rs @@ -109,6 +109,7 @@ pub(in crate::mm) struct Pte(u64) { /// Atomic operations disabled. 7:7 atomic_disable; /// Frame number for system memory. + #[allow_overlap] 53:8 frame_number_sys => Pfn; /// Frame number for video memory. 32:8 frame_number_vid => Pfn; @@ -171,6 +172,7 @@ pub(in crate::mm) struct Pde(u64) { /// Disable Address Translation Services. 5:5 no_ats; /// Table frame number for system memory. + #[allow_overlap] 53:8 table_frame_sys => Pfn; /// Table frame number for video memory. 32:8 table_frame_vid => Pfn; diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs index 8d7d781753..fe67cfe9a4 100644 --- a/rust/kernel/bitfield.rs +++ b/rust/kernel/bitfield.rs @@ -359,7 +359,7 @@ unsafe impl $crate::mem::AsReprMut for $name {} // Definitions requiring knowledge of individual fields: private and public field accessors, // and `Debug` implementation. (@fields $vis:vis $name:ident $storage:ty { - $($(#[doc = $doc:expr])* $hi:literal:$lo:literal $field:ident + $($(#[$attr:ident $(= $doc:expr)?])* $hi:literal:$lo:literal $field:ident $(?=> $try_into_type:ty)? $(=> $into_type:ty)? ; @@ -371,7 +371,7 @@ impl $name { $( $crate::bitfield!(@private_field_accessors $vis $name $storage : $hi:$lo $field); $crate::bitfield!( - @public_field_accessors $(#[doc = $doc])* $vis $name $storage : $hi:$lo $field + @public_field_accessors $($(#[doc = $doc])?)* $vis $name $storage : $hi:$lo $field $(?=> $try_into_type)? $(=> $into_type)? ); @@ -379,6 +379,9 @@ impl $name { } $crate::bitfield!(@debug $name { $($field;)* }); + $crate::bitfield!(@lohi_chk $name $storage { + $($(#[$attr $(= $doc)?])* $field;)* + }); }; // Private field accessors working with the exact `Bounded` type for the field. @@ -540,6 +543,35 @@ const fn [<__with_ $field>]( ); }; + (@allow_overlap doc = $doc:expr) => { false }; + (@allow_overlap allow_overlap) => { true }; + + (@lohi_chk $name:ident $storage:ty { + $($(#[$attr:ident $(= $doc:expr)?])* $field:ident;)* + }) => { + ::kernel::macros::paste!( + const _: () = { + let masks: &[$storage] = &[ + $(if false $(|| $crate::bitfield!(@allow_overlap $attr $(= $doc)?))* { + 0 + } else { + $name::[<$field:upper _MASK>] + }),* + ]; + let err_msg: &[&str] = &[ + $(concat!(stringify!($field), " overlaps a previous field")),* + ]; + let mut used: $storage = 0; + let mut i = 0; + while i < masks.len() { + ::core::assert!(used & masks[i] == 0, "{}", err_msg[i]); + used |= masks[i]; + i += 1; + } + }; + ); + }; + // `Debug` implementation. (@debug $name:ident { $($field:ident;)* }) => { impl ::kernel::fmt::Debug for $name { @@ -637,6 +669,7 @@ struct TestU64(u64) { struct TestU16(u16) { 15:8 field_15_8; 7:4 field_7_4; // Partial overlap with `field_5_4`. + #[allow_overlap] 5:4 field_5_4 => Priority; 3:1 field_3_1; 0:0 field_0; @@ -645,6 +678,7 @@ struct TestU16(u16) { bitfield! { struct TestU8(u8) { + #[allow_overlap] 7:0 field_7_0; // Full byte overlap. 7:4 field_7_4; 3:2 field_3_2; -- 2.56.1