[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250930144537.3559207-8-joelagnelf@nvidia.com>
Date: Tue, 30 Sep 2025 10:45:35 -0400
From: Joel Fernandes <joelagnelf@...dia.com>
To: linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org,
dri-devel@...ts.freedesktop.org,
dakr@...nel.org,
acourbot@...dia.com
Cc: Alistair Popple <apopple@...dia.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
bjorn3_gh@...tonmail.com,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>,
Thomas Zimmermann <tzimmermann@...e.de>,
John Hubbard <jhubbard@...dia.com>,
Joel Fernandes <joelagnelf@...dia.com>,
Timur Tabi <ttabi@...dia.com>,
joel@...lfernandes.org,
Elle Rhumsaa <elle@...thered-steel.dev>,
Yury Norov <yury.norov@...il.com>,
Daniel Almeida <daniel.almeida@...labora.com>,
Andrea Righi <arighi@...dia.com>,
nouveau@...ts.freedesktop.org
Subject: [PATCH v5 7/9] rust: bitfield: Use 'as' operator for setter type conversion
The bitfield macro's setter accesors currently uses the From trait for
type conversion, which is overly restrictive and prevents use cases such
as narrowing conversions (e.g., u8 struct storage size does not work
with 'as u32' for the field size).
Replace 'from' with 'as' in the setter implementation to support this.
An example of such a bitfield struct is:
bitfield! {
struct TestWideFields: u8 {
3:0 nibble as u32;
7:4 high_nibble as u32;
7:0 full as u64;
}
}
Note that there is already no requirement to have the total size of all
the 'as <type>' fields to be <= the struct's storage width. For example,
it is already possible to have a u8 sized struct, with two 'as u8'
fields. So the struct's width is already independent of the total width
of the 'as uXX' instances.
Link: https://lore.kernel.org/all/aMIqGBoNaJ7rUrYQ@yury/
Suggested-by: Yury Norov <yury.norov@...il.com>
Signed-off-by: Joel Fernandes <joelagnelf@...dia.com>
---
rust/kernel/bitfield.rs | 37 ++++++++++++++++++++++++++++++++++---
1 file changed, 34 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
index 9a20bcd2eb60..a74e6d45ecd3 100644
--- a/rust/kernel/bitfield.rs
+++ b/rust/kernel/bitfield.rs
@@ -107,8 +107,6 @@ pub const fn into_raw(self) -> T {
/// - Raw accessor: `raw()` - returns the underlying raw value
/// - Field accessors: `mode()`, `state()`, etc.
/// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining with builder pattern).
-/// Note that the compiler will error out if the size of the setter's arg exceeds the
-/// struct's storage size.
/// - Debug and Default implementations.
///
/// Note: Field accessors and setters inherit the same visibility as the struct itself.
@@ -360,7 +358,9 @@ impl $name {
$vis fn [<set_ $field>](mut self, value: $to_type) -> Self {
const MASK: $storage = $name::[<$field:upper _MASK>];
const SHIFT: u32 = $name::[<$field:upper _SHIFT>];
- let val = (<$storage>::from(value) << SHIFT) & MASK;
+ // Here we are potentially narrowing value from a wider bit value
+ // to a narrower bit value. So we have to use `as` instead of `::from()`.
+ let val = ((value as $storage) << SHIFT) & MASK;
let new_val = (self.raw() & !MASK) | val;
self.0 = ::kernel::bitfield::BitfieldInternalStorage::from_raw(new_val);
@@ -505,6 +505,15 @@ struct TestStatusRegister(u8) {
}
}
+ // For testing wide field types on narrow storage
+ bitfield! {
+ struct TestWideFields(u8) {
+ 3:0 nibble as u32;
+ 7:4 high_nibble as u32;
+ 7:0 full as u64;
+ }
+ }
+
#[test]
fn test_single_bits() {
let mut pte = TestPageTableEntry::default();
@@ -722,4 +731,26 @@ fn test_u8_bitfield() {
assert_eq!(status4.reserved(), 0xF);
assert_eq!(status4.full_byte(), 0xFF);
}
+
+ #[test]
+ fn test_wide_field_types() {
+ let mut wf = TestWideFields::default();
+
+ wf = wf.set_nibble(0x0000000F_u32);
+ assert_eq!(wf.nibble(), 0x0000000F_u32);
+
+ wf = wf.set_high_nibble(0x00000007_u32);
+ assert_eq!(wf.high_nibble(), 0x00000007_u32);
+
+ wf = wf.set_full(0xBE_u64);
+ assert_eq!(wf.full(), 0xBE_u64);
+ assert_eq!(wf.raw(), 0xBE_u8);
+
+ wf = TestWideFields::default()
+ .set_nibble(0x5_u32)
+ .set_high_nibble(0xA_u32);
+ assert_eq!(wf.raw(), 0xA5_u8);
+ assert_eq!(wf.nibble(), 0x5_u32);
+ assert_eq!(wf.high_nibble(), 0xA_u32);
+ }
}
--
2.34.1
Powered by blists - more mailing lists