From: Yury Norov <ynorov@nvidia.com>
To: Alexandre Courbot <acourbot@nvidia.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"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>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: bitfield: add conversions to Bounded of matching width
Date: Fri, 27 Nov 2026 15:36:29 -0500 [thread overview]
Message-ID: <awnpzRnd7EF1PC_x@yury> (raw)
In-Reply-To: <20261127-bitfield-conversions-v1-1-2f0b127c0857@nvidia.com>
On Fri, Nov 27, 2026 at 09:05:40PM +0900, Alexandre Courbot wrote:
> `From`/`Into` implementations already exist between a bitfield type and
> its storage type, but these are not enough to support nested bitfields,
> since every field is a `Bounded`. This resulted in some ad-hoc
> implementations to enable this kind of declaration:
>
> bitfield! {
> pub struct Version(u8) {
> 7:4 major;
> 3:0 minor;
> }
> }
>
> bitfield! {
> pub struct Id(u16) {
> 15:8 vendor;
> 7:0 version => Version;
> }
> }
>
> Generate these conversions automatically through the `bitfield!` macro,
> as these tend to be useful.
>
> Tyr had such ad-hoc implementations, so remove them to avoid conflicts.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
> ---
> drivers/gpu/drm/tyr/regs.rs | 30 +++++------------
> rust/kernel/bitfield.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 89 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
> index 69c901afb28f..0392869adc5a 100644
> --- a/drivers/gpu/drm/tyr/regs.rs
> +++ b/drivers/gpu/drm/tyr/regs.rs
> @@ -1150,20 +1150,6 @@ struct MMU_MEMATTR_STAGE1(u8) {
> }
> }
>
> - impl TryFrom<Bounded<u64, 8>> for MMU_MEMATTR_STAGE1 {
> - type Error = Error;
> -
> - fn try_from(val: Bounded<u64, 8>) -> Result<Self, Self::Error> {
> - Ok(Self::from_raw(val.get() as u8))
> - }
> - }
> -
> - impl From<MMU_MEMATTR_STAGE1> for Bounded<u64, 8> {
> - fn from(val: MMU_MEMATTR_STAGE1) -> Self {
> - Bounded::try_new(u64::from(val.into_raw())).unwrap()
> - }
> - }
> -
> register! {
> base: TyrRegisters;
>
> @@ -1172,14 +1158,14 @@ fn from(val: MMU_MEMATTR_STAGE1) -> Self {
> /// Each address space can configure up to 8 different memory attribute profiles.
> /// Each attribute profile follows the MMU_MEMATTR_STAGE1 layout.
> pub(crate) MEMATTR(u64)[MAX_AS, stride = STRIDE] @ 0x2408 {
> - 7:0 attribute0 ?=> MMU_MEMATTR_STAGE1;
> - 15:8 attribute1 ?=> MMU_MEMATTR_STAGE1;
> - 23:16 attribute2 ?=> MMU_MEMATTR_STAGE1;
> - 31:24 attribute3 ?=> MMU_MEMATTR_STAGE1;
> - 39:32 attribute4 ?=> MMU_MEMATTR_STAGE1;
> - 47:40 attribute5 ?=> MMU_MEMATTR_STAGE1;
> - 55:48 attribute6 ?=> MMU_MEMATTR_STAGE1;
> - 63:56 attribute7 ?=> MMU_MEMATTR_STAGE1;
> + 7:0 attribute0 => MMU_MEMATTR_STAGE1;
> + 15:8 attribute1 => MMU_MEMATTR_STAGE1;
> + 23:16 attribute2 => MMU_MEMATTR_STAGE1;
> + 31:24 attribute3 => MMU_MEMATTR_STAGE1;
> + 39:32 attribute4 => MMU_MEMATTR_STAGE1;
> + 47:40 attribute5 => MMU_MEMATTR_STAGE1;
> + 55:48 attribute6 => MMU_MEMATTR_STAGE1;
> + 63:56 attribute7 => MMU_MEMATTR_STAGE1;
> }
>
> // MEMATTR is a logical 64-bit register, but it is laid out in hardware as two
> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index 8d7d78175336..f53001a41731 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -78,7 +78,8 @@
> //! every value assigned to a field. This ensures that data is never accidentally truncated.
> //!
> //! The macro generates the bitfield type, [`From`] and [`Into`] implementations for its storage
> -//! type, as well as [`Debug`] and [`Zeroable`](pin_init::Zeroable) implementations.
> +//! type and for any [`Bounded`] with the same number of bits as its storage type, as well as
> +//! [`Debug`] and [`Zeroable`](pin_init::Zeroable) implementations.
> //!
> //! For each field, it also generates:
> //!
> @@ -249,6 +250,37 @@
> //! assert_eq!(Config::from(0b11).mode(), Err(3));
> //! ```
> //!
> +//! ## Nested bitfields
> +//!
> +//! A bitfield type can be converted from and into any [`Bounded`] with exactly as many bits as
> +//! its storage type. Thus, it can be used with `=>` as the type of a field of the same width in
> +//! another bitfield:
> +//!
> +//! ```rust
> +//! use kernel::bitfield;
> +//!
> +//! bitfield! {
> +//! pub struct Version(u8) {
> +//! 7:4 major;
> +//! 3:0 minor;
> +//! }
> +//! }
> +//!
> +//! bitfield! {
> +//! pub struct Id(u16) {
> +//! 15:8 vendor;
> +//! 7:0 version => Version;
> +//! }
> +//! }
> +//!
> +//! let id = Id::from_raw(0x10_42);
> +//! assert_eq!(id.version().major(), 0x4);
> +//! assert_eq!(id.version().minor(), 0x2);
> +//!
> +//! let id = id.with_version(Version::zeroed().with_const_major::<0x1>());
> +//! assert_eq!(id.into_raw(), 0x10_10);
> +//! ```
> +//!
> //! # Bits outside of declared fields
> //!
> //! Bits of the storage type that are not part of any declared field are preserved by the setter
> @@ -346,6 +378,31 @@ fn from(val: $storage) -> $name {
> }
> }
>
> + // Conversions from/to any `Bounded` with the exact same width as `$storage`, to allow
> + // infallible nested bitfields.
> +
> + impl<T> ::core::convert::From<$crate::num::Bounded<T, { <$storage>::BITS }>> for $name
> + where
> + T: $crate::num::Integer,
> + $storage: ::core::convert::From<$crate::num::Bounded<T, { <$storage>::BITS }>>,
> + {
> + #[inline(always)]
> + fn from(val: $crate::num::Bounded<T, { <$storage>::BITS }>) -> $name {
> + Self::from_raw(val.into())
> + }
> + }
> +
> + impl<T> ::core::convert::From<$name> for $crate::num::Bounded<T, { <$storage>::BITS }>
> + where
> + T: $crate::num::Integer,
> + Self: ::core::convert::From<$storage>,
> + {
> + #[inline(always)]
> + fn from(val: $name) -> Self {
> + val.into_raw().into()
> + }
> + }
> +
> // SAFETY: `$name` is transparent over `$storage` and `$storage` has no interior mutability.
> unsafe impl $crate::mem::AsRepr for $name {
> // Normalize `$storage` to the canonical repr type in case it is signed.
> @@ -653,6 +710,14 @@ struct TestU8(u8) {
> }
> }
>
> + bitfield! {
> + struct TestNested(u32) {
> + 31:16 field_31_16 => TestU16;
> + 15:8 field_15_8;
> + 7:0 field_7_0 => TestU8;
> + }
> + }
> +
> // Single and multi-bit fields basic access.
> #[test]
> fn test_basic_access() {
> @@ -770,6 +835,21 @@ fn test_fallible_conversion() {
> assert_eq!(TestU64::from_raw(raw).field_15_12(), Err(0x7));
> }
>
> + // `=>` conversion from/to another bitfield with the same width as the field.
> + #[test]
> + fn test_nested_bitfield() {
> + let val = TestNested::from_raw(0x42AB_00AF);
> + assert_eq!(val.field_31_16(), TestU16::from_raw(0x42AB));
> + assert_eq!(val.field_31_16().field_15_8(), 0x42);
> + assert_eq!(val.field_7_0(), TestU8::from_raw(0xAF));
> + assert_eq!(val.field_7_0().field_7_4(), 0xA);
> +
> + let val = val
> + .with_field_31_16(TestU16::zeroed().with_const_field_3_1::<0x5>())
> + .with_field_7_0(TestU8::zeroed().with_field_0(true));
> + assert_eq!(val.into_raw(), 0x000A_0001);
> + }
> +
> // Test that setting an overlapping field affects the overlapped one as expected.
> #[test]
> fn test_overlapping_fields() {
>
> ---
> base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
> change-id: 20261127-bitfield-conversions-e5a6b02f0ac4
>
> Best regards,
> --
> Alexandre Courbot <acourbot@nvidia.com>
next prev parent reply other threads:[~2026-11-27 20:36 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 12:05 [PATCH] rust: bitfield: add conversions to Bounded of matching width Alexandre Courbot
2026-11-27 17:15 ` Alice Ryhl
2026-11-27 20:05 ` Gary Guo
2026-11-27 20:36 ` Yury Norov [this message]
2026-12-01 21:18 ` Miguel Ojeda
2026-12-02 0:21 ` Alexandre Courbot
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=awnpzRnd7EF1PC_x@yury \
--to=ynorov@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.