Rust for Linux List
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register!
@ 2026-11-30  8:13 Boming Kong
  2026-11-30  8:13 ` [RFC PATCH 1/2] rust: bitfield: added optional overlap argument for bitfield! Boming Kong
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Boming Kong @ 2026-11-30  8:13 UTC (permalink / raw)
  To: rust-for-linux
  Cc: Boming Kong, Alexandre Courbot, Yury Norov, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Onur Özkan, driver-core

This RFC proposes adding optional compile-time overlap checking for
bitfield! and register! to reject overlapping fields.

Currently, register! does not check whether user-defined bitfield
overlap. Accidental overlaps may lead to unintended modification of
other fields and introduce driver bugs. For example:

register! {
    base: Region<0x1000>;
    pub CONFIG(u32) @ 0x100 {
        7:0 mode;
        11:4 index;
    }
}

Modifying either field may unintentionally affect the other, as they
share bits 7:4.

I propose adding optional overlap checking to bitfield! and enabling
it by default in register!, while keeping it disabled by default in
bitfield!. Users can explicitly enable or disable the check like:


// This code will not compile
register! {
    base: Region<0x1000>;
    pub CONFIG(u32, overlap=false) @ 0x100 {
        7:0 mode;
        11:4 index;
    }
}

The validation would reuse the existing field masks and be performed
entirely at compile time, introducing no runtime overhead.

Although some hardware registers intentionally provide multiple
interpretations of the same register, these could be represented
using register aliases. Mixing different interpretations within a
single register definition may make the code more error-prone.

Do you think this is a useful feature to add?

Prior discussion:
Overlapping fields were previously discussed and confirmed to be an
intentional feature [1]. This proposal preserves that functionality
while allowing accidental overlaps to be detected if user request for
overlapping check.

[1] https://lkml.iu.edu/hypermail/linux/kernel/2510.0/05029.html


Boming Kong (2):
  added optional overlap argument for bitfield!
  implemented overlap flag inside register!

 rust/kernel/bitfield.rs    | 26 +++++++++++++++++++++++---
 rust/macros/io/register.rs | 24 +++++++++++++++++-------
 2 files changed, 40 insertions(+), 10 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-02  9:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 1/2] rust: bitfield: added optional overlap argument for bitfield! Boming Kong
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
2026-12-02  9:44       ` Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox