Re: [PATCH v2 1/3] rust: extract `bitfield!` macro from `register!`

From: Alexandre Courbot

Date: Fri May 01 2026 - 02:08:34 EST


On Thu Apr 16, 2026 at 10:35 AM JST, Yury Norov wrote:
> On Thu, Apr 09, 2026 at 11:58:47PM +0900, Alexandre Courbot wrote:
>> Extract the bitfield-defining part of the `register!` macro into an
>> independent macro used to define bitfield types with bounds-checked
>> accessors.
>>
>> Each field is represented as a `Bounded` of the appropriate bit width,
>> ensuring field values are never silently truncated.
>>
>> Fields can optionally be converted to/from custom types, either fallibly
>> or infallibly.
>>
>> Appropriate documentation is also added, and a MAINTAINERS entry created
>> for the new module.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>> ---
>> MAINTAINERS | 8 +
>> rust/kernel/bitfield.rs | 491 +++++++++++++++++++++++++++++++++++++++++++++
>> rust/kernel/io/register.rs | 246 +----------------------
>> rust/kernel/lib.rs | 1 +
>> 4 files changed, 502 insertions(+), 244 deletions(-)
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index b01791963e25..77f2617ade5d 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -23186,6 +23186,14 @@ F: scripts/*rust*
>> F: tools/testing/selftests/rust/
>> K: \b(?i:rust)\b
>>
>> +RUST [BITFIELD]
>> +M: Alexandre Courbot <acourbot@xxxxxxxxxx>
>> +M: Joel Fernandes <joelagnelf@xxxxxxxxxx>
>> +R: Yury Norov <yury.norov@xxxxxxxxx>
>> +L: rust-for-linux@xxxxxxxxxxxxxxx
>> +S: Maintained
>> +F: rust/kernel/bitfield.rs
>> +
>> RUST [ALLOC]
>> M: Danilo Krummrich <dakr@xxxxxxxxxx>
>> R: Lorenzo Stoakes <ljs@xxxxxxxxxx>
>> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
>> new file mode 100644
>> index 000000000000..f5948eec8a76
>> --- /dev/null
>> +++ b/rust/kernel/bitfield.rs
>> @@ -0,0 +1,491 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +//! Support for defining bitfields as Rust structures.
>> +//!
>> +//! The [`bitfield!`](kernel::bitfield!) macro declares integer types that are split into distinct
>> +//! bit fields of arbitrary length. Each field is typed using [`Bounded`](kernel::num::Bounded) to
>> +//! ensure values are properly validated and to avoid implicit data loss.
>> +//!
>> +//! # Example
>> +//!
>> +//! ```rust
>> +//! use kernel::bitfield;
>> +//! use kernel::num::Bounded;
>> +//!
>> +//! bitfield! {
>> +//! pub struct Rgb(u16) {
>> +//! 15:11 blue;
>> +//! 10:5 green;
>> +//! 4:0 red;
>
> Hi Alex,
>
> Can you please describe those implied naming limitations we've
> discussed on the previous round, like with_blue, BLUE_SHIFT etc.
> in a separate top comment?

Yes, that was missing indeed. Added for v3.