Skip to content

RFC rust: Add compile-time overlap checking for bitfield! and register!

Boming Kong driver-core rust-for-linux 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest2 patches 7 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2rust: bitfield: added optional overlap argument for bitfield!rust/kernel/bitfield.rs+23 -30 replies
2/2rust: io: register: implemented overlap flag inside register!rust/macros/io/register.rs+17 -70 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register!

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(-)
Signature · 1 line
2.56.1