Skip to contentSkip to conversation

rust: add `Alignment` type

PATCH Superseded Alexandre Courbot 1 participant 3 messages Aug 12, 2025

Subsystems (7)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v3 cover rust: add `Alignment` type

Thanks to the feedback received on the last revision, I feel like we are converging.

The ACP for ffs/fls equivalents having been accepted [1], I am dropping any work related to them from this patchset which is now exclusively focused on alignment features.

The first patch makes two additions:

- Bring an equivalent of the nightly upstream Alignment type [2] with an identical public interface,
- Add a new Alignable extension trait that provides a way to align values up or down according to an Alignment, and implement it on unsigned integer types.

The second patch makes use of these in the Nova driver.

[3] is recommended as a dependency, a small cleanup preventing an unwrap upon applying this series.

[1] https://github.com/rust-lang/libs-team/issues/631
[2] https://doc.rust-lang.org/std/ptr/struct.Alignment.html
[3] https://lore.kernel.org/rust-for-linux/[email protected]/

---
Changes in v3:
- Move align_down and align_up into their own Alignable trait. (thanks Miguel!)
- Fix Alignment::mask implementation to match upstream's.
- Add missing SAFETY: comments.
- Improve grammar on a few hard-to-parse comments.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Remove last_checked_bit (use Rust's checked_ilog2 instead).
- Port Rust nightly Alignment type and extend it with align_down and align_up methods.
- Link to v1: https://lore.kernel.org/r/[email protected]

Changes since split from the nova-core series:
- Rename fls to last_set_bit,
- Generate per-type doctests,
- Add invariants section to PowerOfTwo.
- Do not use reference to self in PowerOfTwo methods since it implements Copy,
- Use #[derive] where possible instead of implementing traits manually,
- Remove Deref and Borrow implementations.

---
Alexandre Courbot (2):
rust: add Alignment type
gpu: nova-core: use Alignment for alignment-related operations

 Documentation/gpu/nova/core/todo.rst |   1 -
 drivers/gpu/nova-core/fb.rs          |   6 +-
 drivers/gpu/nova-core/vbios.rs       |   4 +-
 rust/kernel/lib.rs                   |   1 +
 rust/kernel/ptr.rs                   | 218 +++++++++++++++++++++++++++++++++++
 5 files changed, 224 insertions(+), 6 deletions(-)

---
base-commit: 062b3e4a1f880f104a8d4b90b767788786aa7b78
change-id: 20250620-num-9420281c02c7
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 2439f5f9b560ee4867716f0018b5326dcd72cda3

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/2 rust: add `Alignment` type

Alignment operations are very common in the kernel. Since they are always performed using a power-of-two value, enforcing this invariant through a dedicated type leads to fewer bugs and can improve the generated code.

Introduce the Alignment type, inspired by the nightly Rust type of the same name and providing the same interface, and a new Alignable trait allowing unsigned integers to be aligned up or down.

---
 rust/kernel/lib.rs |   1 +
 rust/kernel/ptr.rs | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 219 insertions(+)
rust/kernel/lib.rs +1 -0
index ed53169e795c0badf548025a57f946fa18bc73e3..65586b8bd3f7ddef5f398b12796716fc192c507e 100644
@@ -110,6 +110,7 @@
 pub mod platform;
 pub mod prelude;
 pub mod print;
+pub mod ptr;
 pub mod rbtree;
 pub mod regulator;
 pub mod revocable;
rust/kernel/ptr.rs new file +218 -0

218 lines not on this page · raw message

Signature · 1 line
2.50.1
author maintainer
#2 lore raw

PATCH v3 2/2 gpu: nova-core: use Alignment for alignment-related operations

In reply to Alexandre Courbot

Make use of the newly-available Alignment type and remove the corresponding TODO item.

---
 Documentation/gpu/nova/core/todo.rst | 1 -
 drivers/gpu/nova-core/fb.rs          | 6 +++---
 drivers/gpu/nova-core/vbios.rs       | 4 ++--
 3 files changed, 5 insertions(+), 6 deletions(-)
Documentation/gpu/nova/core/todo.rst +0 -1
index 894a1e9c3741a43ad4eb76d24a9486862999874e..8fdb5bced3460a3971699df79ffa2c69f84b2735 100644
@@ -147,7 +147,6 @@ Numerical operations [NUMM]
 Nova uses integer operations that are not part of the standard library (or not
 implemented in an optimized way for the kernel). These include:
 
-- Aligning up and down to a power of two,
 - The "Find Last Set Bit" (`fls` function of the C part of the kernel)
   operation.
 
drivers/gpu/nova-core/fb.rs +3 -3
index 4a702525fff4f394b75fcf54145ba78e34a1a539..344cb5d2d919c456212d60d7210667de8041f812 100644
@@ -3,6 +3,7 @@
 use core::ops::Range;
 
 use kernel::prelude::*;
+use kernel::ptr::{Alignable, Alignment};
 use kernel::sizes::*;
 use kernel::types::ARef;
 use kernel::{dev_warn, device};
@@ -130,10 +131,9 @@ pub(crate) fn new(chipset: Chipset, bar: &Bar0) -> Result<Self> {
         };
 
         let frts = {
-            const FRTS_DOWN_ALIGN: u64 = SZ_128K as u64;
+            const FRTS_DOWN_ALIGN: Alignment = Alignment::new(SZ_128K);
             const FRTS_SIZE: u64 = SZ_1M as u64;
-            // TODO[NUMM]: replace with `align_down` once it lands.
-            let frts_base = (vga_workspace.start & !(FRTS_DOWN_ALIGN - 1)) - FRTS_SIZE;
+            let frts_base = vga_workspace.start.align_down(FRTS_DOWN_ALIGN) - FRTS_SIZE;
 
             frts_base..frts_base + FRTS_SIZE
         };
drivers/gpu/nova-core/vbios.rs +2 -2
index 5b5d9f38cbb3a6b1c374c1e0eee2509eb8d5660c..680d4d961593d7cb88bdd58c6c1dbf77b51b6d7f 100644
@@ -10,6 +10,7 @@
 use kernel::error::Result;
 use kernel::pci;
 use kernel::prelude::*;
+use kernel::ptr::{Alignable, Alignment};
 
 /// The offset of the VBIOS ROM in the BAR0 space.
 const ROM_OFFSET: usize = 0x300000;
@@ -177,8 +178,7 @@ fn next(&mut self) -> Option<Self::Item> {
 
         // Advance to next image (aligned to 512 bytes).
         self.current_offset += image_size;
-        // TODO[NUMM]: replace with `align_up` once it lands.
-        self.current_offset = self.current_offset.next_multiple_of(512);
+        self.current_offset = self.current_offset.align_up(Alignment::new(512))?;
 
         Some(Ok(full_image))
     }
Signature · 1 line
2.50.1