Rust for Linux List
 help / color / mirror / Atom feed
From: Boming Kong <michaelkongboming@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: "Boming Kong" <michaelkongboming@gmail.com>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	driver-core@lists.linux.dev
Subject: [RFC PATCH 1/2] rust: bitfield: added optional overlap argument for bitfield!
Date: Mon, 30 Nov 2026 03:13:57 -0500	[thread overview]
Message-ID: <20261130081359.158313-2-michaelkongboming@gmail.com> (raw)
In-Reply-To: <20261130081359.158313-1-michaelkongboming@gmail.com>

Implement bit range overlap check inside bitfield!, set default to
true to stay the same as current implementation

Signed-off-by: Boming Kong <michaelkongboming@gmail.com>
---
 rust/kernel/bitfield.rs | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
index 8d7d781753..9d9852a7fe 100644
--- a/rust/kernel/bitfield.rs
+++ b/rust/kernel/bitfield.rs
@@ -292,12 +292,12 @@
 macro_rules! bitfield {
     // Entry point defining the bitfield struct, its implementations and its field accessors.
     (
-        $(#[$attr:meta])* $vis:vis struct $name:ident($storage:ty) { $($fields:tt)* }
+        $(#[$attr:meta])* $vis:vis struct $name:ident($storage:ty $(, overlap = $overlap:literal)?) { $($fields:tt)* }
     ) => {
         $crate::bitfield!(@core
             $(#[$attr])* $vis $name $storage
         );
-        $crate::bitfield!(@fields $vis $name $storage { $($fields)* });
+        $crate::bitfield!(@fields $vis $name $storage $(, overlap = $overlap)? { $($fields)* });
     };
 
     // All rules below are helpers.
@@ -358,7 +358,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 {
+    (@fields $vis:vis $name:ident $storage:ty $(, overlap = $overlap:literal)? {
         $($(#[doc = $doc:expr])* $hi:literal:$lo:literal $field:ident
             $(?=> $try_into_type:ty)?
             $(=> $into_type:ty)?
@@ -379,6 +379,7 @@ impl $name {
         }
 
         $crate::bitfield!(@debug $name { $($field;)* });
+        $crate::bitfield!(@lohi_chk $name $storage $(, overlap = $overlap)? { $($field;)* });
     };
 
     // Private field accessors working with the exact `Bounded` type for the field.
@@ -554,6 +555,25 @@ fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result {
             }
         }
     };
+
+    (@lohi_chk $name:ident $storage:ty { $($field:ident;)* }) => {};
+    (@lohi_chk $name:ident $storage:ty, overlap = $overlap:literal { $($field:ident;)* }) => {
+        ::kernel::macros::paste!(
+        const _: () = {
+            let overlap: bool = $overlap;
+            if !overlap {
+                let masks: &[$storage] = &[$($name::[<$field:upper _MASK>]),*];
+                let mut used: $storage = 0;
+                let mut i = 0;
+                while i < masks.len() {
+                    ::core::assert!(used & masks[i] == 0, "bitfields overlap");
+                    used |= masks[i];
+                    i += 1;
+                }
+            }
+        };
+        );
+    };
 }
 
 #[cfg(CONFIG_RUST_BITFIELD_KUNIT_TEST)]
-- 
2.56.1


  reply	other threads:[~2026-11-30  8:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:13 [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register! Boming Kong
2026-11-30  8:13 ` Boming Kong [this message]
2026-11-30  8:13 ` [RFC PATCH 2/2] rust: io: register: implemented overlap flag inside register! Boming Kong
2026-11-30 10:34 ` [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register! Miguel Ojeda
2026-11-30 17:31   ` Boming Kong
2026-12-02  6:23   ` Alexandre Courbot
2026-12-02  7:33     ` Boming Kong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130081359.158313-2-michaelkongboming@gmail.com \
    --to=michaelkongboming@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox