RFC rust: Add compile-time overlap checking for bitfield! and register!
Get series with b4
b4 am [email protected]Versions newest first
- v1 RFC Latest2 patches 7 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | rust: bitfield: added optional overlap argument for bitfield!rust/ | +23 | 0 replies | |
| 2/2 | rust: io: register: implemented overlap flag inside register!rust/ | +17 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
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