rust: add `Alignment` type
Subsystems (7)
Alexandre Courbot
maintainerv3 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]>
Alexandre Courbot
maintainerPATCH 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
@@ -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
Alexandre Courbot
maintainerPATCH v3 2/2 gpu: nova-core: use Alignment for alignment-related operations
In reply to Alexandre CourbotMake 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
@@ -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
@@ -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
@@ -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