rust: add functions and traits for lossless integer conversions
Subsystems (7)
Alexandre Courbot
maintainerv2 cover rust: add functions and traits for lossless integer conversions
This series introduces a copy of the lossless integer conversion functions/traits of nova-core into the kernel crate, and makes nova-core use them.
This revision addresses the feedback received on v1 by making architecture-dependent conversions available via a dedicated arch sub-module to make it more obvious that they are not portable.
This series is based on rust-next.
---
Changes in v2:
- Move architecture-dependent conversions into arch sub-module, and use dedicated trait names.
- Use use crate:: instead of use kernel:: for kernel imports.
- Replace use of build_assert! with const_assert! (Sashiko).
- #[inline] all the functions (Sashiko).
- Remove stray #[allow(unused)].
- Group kernel imports properly (Sashiko).
- Do not reexport the contents of casts in num.
- Link to v1: https://patch.msgid.link/[email protected]
Changes in v1:
- Update to latest version in nova-core.
- Make nova-core use the kernel crate implementation and remove its own.
- Link to RFC: https://lore.kernel.org/r/[email protected]
---
Alexandre Courbot (2):
rust: add functions and traits for lossless integer conversions
gpu: nova-core: use kernel lossless integer conversion module
drivers/gpu/nova-core/falcon.rs | 12 +- drivers/gpu/nova-core/falcon/fsp.rs | 4 +- drivers/gpu/nova-core/fb.rs | 2 +- drivers/gpu/nova-core/fb/hal/gb100.rs | 11 +- drivers/gpu/nova-core/firmware.rs | 8 +- drivers/gpu/nova-core/firmware/booter.rs | 10 +- drivers/gpu/nova-core/firmware/fwsec.rs | 2 +- drivers/gpu/nova-core/firmware/fwsec/bootloader.rs | 2 +- drivers/gpu/nova-core/firmware/gsp.rs | 7 +- drivers/gpu/nova-core/firmware/riscv.rs | 6 +- drivers/gpu/nova-core/fsp.rs | 4 +- drivers/gpu/nova-core/gsp.rs | 4 +- drivers/gpu/nova-core/gsp/cmdq.rs | 24 +- drivers/gpu/nova-core/gsp/fw.rs | 42 +-- drivers/gpu/nova-core/gsp/sequencer.rs | 2 +- drivers/gpu/nova-core/num.rs | 211 --------------- drivers/gpu/nova-core/vbios.rs | 2 +- rust/kernel/num.rs | 2 + rust/kernel/num/casts.rs | 298 +++++++++++++++++++++ 19 files changed, 373 insertions(+), 280 deletions(-)
---
base-commit: dc01dfb37b34beeefcfe1c3055364d41a4070c7e
change-id: 20251104-as_casts-6a8882ac0192
Best regards,
Signature · 1 line
Alexandre Courbot <[email protected]>
Alexandre Courbot
maintainerPATCH v2 1/2 rust: add functions and traits for lossless integer conversions
The core library's From implementations do not cover conversions that are not portable or future-proof. For instance, even though it is safe today, From<usize> is not implemented for u64 because of the possibility of supporting larger-than-64bit architectures in the future.
However, the kernel supports a narrower set of architectures, with a considerable amount of code that is architecture-specific. This makes it helpful and desirable to provide more infallible conversions, lest we rely on the as keyword and carry the risk of silently losing data.
Thus, introduce a new module num::casts that provides safe const functions performing more conversions allowed by the build target, as well as FromSafeCast and IntoSafeCast traits that are just extensions of From and Into to conversions that are known to be lossless.
Some conversions are architecture-specific: for instance, converting a u64 to a usize is only lossless on 64-bit platforms. These conversions are made available via a dedicated arch sub-module.
--- rust/kernel/num.rs | 2 + rust/kernel/num/casts.rs | 298 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 300 insertions(+)
rust/kernel/num.rs +2 -0
@@ -5,6 +5,8 @@use core::ops;pub mod bounded; +pub mod casts; +pub use bounded::*;/// Designates unsigned primitive types.
rust/kernel/num/casts.rs new file +298 -0
298 lines not on this page · raw message
Signature · 1 line
2.55.0
Alexandre Courbot
maintainerPATCH v2 2/2 gpu: nova-core: use kernel lossless integer conversion module
In reply to Alexandre CourbotThe kernel crate now features a copy of our lossless integer conversion routines. Switch to the kernel version and remove our own.
--- drivers/gpu/nova-core/falcon.rs | 12 +- drivers/gpu/nova-core/falcon/fsp.rs | 4 +- drivers/gpu/nova-core/fb.rs | 2 +- drivers/gpu/nova-core/fb/hal/gb100.rs | 11 +- drivers/gpu/nova-core/firmware.rs | 8 +- drivers/gpu/nova-core/firmware/booter.rs | 10 +- drivers/gpu/nova-core/firmware/fwsec.rs | 2 +- drivers/gpu/nova-core/firmware/fwsec/bootloader.rs | 2 +- drivers/gpu/nova-core/firmware/gsp.rs | 7 +- drivers/gpu/nova-core/firmware/riscv.rs | 6 +- drivers/gpu/nova-core/fsp.rs | 4 +- drivers/gpu/nova-core/gsp.rs | 4 +- drivers/gpu/nova-core/gsp/cmdq.rs | 24 +-- drivers/gpu/nova-core/gsp/fw.rs | 42 ++-- drivers/gpu/nova-core/gsp/sequencer.rs | 2 +- drivers/gpu/nova-core/num.rs | 211 --------------------- drivers/gpu/nova-core/vbios.rs | 2 +- 17 files changed, 73 insertions(+), 280 deletions(-)
drivers/gpu/nova-core/falcon.rs +6 -6
@@ -23,6 +23,10 @@ }, Io, }, + num::casts::{ + self, + FromSafeCast, // + }, prelude::*, sync::aref::ARef, time::Delta,
@@ -33,11 +37,7 @@ driver::Bar0, falcon::hal::LoadMethod, gpu::Chipset, - num::{ - self, - FromSafeCast, // - }, - regs, + regs, // };pub (crate )mod fsp;
@@ -518,7 +518,7 @@ fn dma_wr( target_mem: FalconMem, load_offsets: FalconDmaLoadTarget, ) -> Result { - const DMA_LEN: u32 = num::usize_into_u32::<{ MEM_BLOCK_ALIGNMENT }>(); + const DMA_LEN: u32 = casts::usize_into_u32::<{ MEM_BLOCK_ALIGNMENT }>();// For IMEM, we want to use the start offset as a virtual address tag for each page, since // code addresses in the firmware (and the boot vector) are virtual.
drivers/gpu/nova-core/falcon/fsp.rs +2 -2
@@ -16,6 +16,7 @@ }, Io, // }, + num::casts, prelude::*, time::Delta, };
@@ -28,7 +29,6 @@ PFalcon2Base, PFalconBase, // }, - num, regs, // };
@@ -155,7 +155,7 @@ pub(crate) fn recv_msg(&mut self, bar: Bar0<'_>) -> Result<KVec<u8>> { Delta::from_millis(10), Delta::from_millis(FSP_MSG_TIMEOUT_MS), ) - .map (num::u32_as_usize)?; + .map (casts::u32_as_usize)?;let mut buffer =KVec ::<u8 >::new (); buffer.resize (msg_size,0 ,GFP_KERNEL )?;
drivers/gpu/nova-core/fb.rs +1 -1
@@ -10,6 +10,7 @@ dma::CoherentHandle, fmt, io::Io, + num::casts::FromSafeCast, prelude::*, ptr::{Alignable ,
@@ -23,7 +24,6 @@ firmware::gsp::GspFirmware, gpu::Chipset, gsp, - num::FromSafeCast, regs, // };
drivers/gpu/nova-core/fb/hal/gb100.rs +7 -4
@@ -11,7 +11,10 @@ }, Io, // }, - num::Bounded, + num::{ + casts, + Bounded, // + }, prelude::*, ptr::{ const_align_up,
@@ -23,7 +26,6 @@use crate ::{ driver::Bar0 , fb::hal::FbHal , - num::usize_into_u32, regs,// };
@@ -79,8 +81,9 @@ fn write_sysmem_flush_page_gb100(bar: Bar0<'_>, addr: Bounded<u64, 52>) { }pub (super )const fn pmu_reserved_size_gb100 () ->u32 { -usize_into_u32 ::<{const_align_up (SZ_8M +SZ_16M +SZ_4K ,Alignment ::new ::<SZ_128K >()).unwrap () }>( - ) + casts::usize_into_u32 ::< + {const_align_up (SZ_8M +SZ_16M +SZ_4K ,Alignment ::new ::<SZ_128K >()).unwrap () }, + >() }impl FbHal for Gb100 {
drivers/gpu/nova-core/firmware.rs +4 -4
@@ -10,6 +10,10 @@use kernel::{ device, firmware, + num::casts::{ +FromSafeCast , +IntoSafeCast ,// + }, prelude::*, str::CString , transmute::FromBytes ,//
@@ -21,10 +25,6 @@ FalconFirmware, // }, gpu, - num::{ -FromSafeCast , -IntoSafeCast ,// - }, };pub (crate )mod booter;
drivers/gpu/nova-core/firmware/booter.rs +5 -5
@@ -10,6 +10,10 @@use kernel::{ device, dma::Coherent , + num::casts::{ +FromSafeCast , +IntoSafeCast ,// + }, prelude::*, transmute::FromBytes ,// };
@@ -31,11 +35,7 @@ Signed, Unsigned, // }, - gpu::Chipset, - num::{ -FromSafeCast , -IntoSafeCast ,// - }, + gpu::Chipset, // };/// Local convenience function to return a copy of `S` by reinterpreting the bytes starting at
drivers/gpu/nova-core/firmware/fwsec.rs +1 -1
@@ -19,6 +19,7 @@ self, Device, // }, + num::casts::FromSafeCast, prelude::*, transmute::{AsBytes ,
@@ -43,7 +44,6 @@ Signed, Unsigned, // }, - num::FromSafeCast, vbios::Vbios, };
drivers/gpu/nova-core/firmware/fwsec/bootloader.rs +1 -1
@@ -17,6 +17,7 @@ register::WithBase, // Io, }, + num::casts::FromSafeCast, prelude::*, ptr::{Alignable ,
@@ -51,7 +52,6 @@ FIRMWARE_VERSION, // }, gpu::Chipset, - num::FromSafeCast, regs, };
drivers/gpu/nova-core/firmware/gsp.rs +5 -2
@@ -8,6 +8,10 @@ DataDirection, DmaAddress, // }, + num::casts::{ + arch::FromSafeCastArch, + FromSafeCast, // + }, prelude::*, scatterlist::{Owned ,
@@ -25,7 +29,6 @@ Chipset, // }, gsp::GSP_PAGE_SIZE, - num::FromSafeCast, };/// GSP firmware with 3-level radix page tables for the GSP bootloader.
@@ -175,7 +178,7 @@ pub(crate) fn radix3_dma_handle(&self) -> DmaAddress {fn map_into_lvl (sg_table: &SGTable <Owned <VVec <u8 >>>,mut dst:VVec <u8 >) ->Result <VVec <u8 >> {for sg_entryin sg_table.iter () {// Number of pages we need to map. -let num_pages = usize::from_safe_cast (sg_entry.dma_len ()).div_ceil (GSP_PAGE_SIZE ); +let num_pages = usize::from_safe_cast_arch (sg_entry.dma_len ()).div_ceil (GSP_PAGE_SIZE );for iin 0 ..num_pages {let entry = sg_entry.dma_address ()
drivers/gpu/nova-core/firmware/riscv.rs +2 -4
@@ -7,14 +7,12 @@ device, dma::Coherent, firmware::Firmware, + num::casts::FromSafeCast, prelude::*, transmute::FromBytes, // }; -use crate ::{ - firmware::BinFirmware , - num::FromSafeCast ,// -}; +use crate ::firmware::BinFirmware ;/// Descriptor for microcode running on a RISC-V core. #[repr( C )]
drivers/gpu/nova-core/fsp.rs +2 -2
@@ -11,6 +11,7 @@ device, dma::Coherent, io::poll::read_poll_timeout, + num::casts, prelude::*, ptr::{Alignable ,
@@ -42,7 +43,6 @@ NvdmHeader, NvdmType, // }, - num, regs, // };
@@ -128,7 +128,7 @@ fn new<'a>( };let version = hal::fsp_hal (args.chipset).ok_or (ENOTSUPP )?.cot_version (); -let size = num::usize_into_u16 ::<{ core::mem::size_of ::<NvdmPayloadCot >() }>(); +let size = casts::usize_into_u16 ::<{ core::mem::size_of ::<NvdmPayloadCot >() }>(); Ok(init!(Self { mctp_header: MctpHeader::single_packet(),
drivers/gpu/nova-core/gsp.rs +2 -2
@@ -11,6 +11,7 @@ CoherentBox, DmaAddress, // }, + num::casts, pci, prelude::*, transmute::{
@@ -36,7 +37,6 @@ GspArgumentsPadded, LibosMemoryRegionInitArgument, // }, - num, };pub (crate )const GSP_PAGE_SHIFT :usize =12 ;
@@ -61,7 +61,7 @@ impl<const NUM_PAGES: usize> PteArray<NUM_PAGES> {// TODO: Replace with `IoView` projection once available. fn entry (start:DmaAddress , index:usize ) ->Result <u64 > { start - .checked_add (num::usize_as_u64 (index) <<GSP_PAGE_SHIFT ) + .checked_add (casts::usize_as_u64 (index) <<GSP_PAGE_SHIFT ) .ok_or (EOVERFLOW ) } }
drivers/gpu/nova-core/gsp/cmdq.rs +12 -12
@@ -16,6 +16,7 @@ Io, // }, new_mutex, + num::casts, prelude::*, ptr, sync::{
@@ -26,7 +27,7 @@ transmute::{AsBytes ,FromBytes ,// - }, + }, // };use continuation::{
@@ -50,7 +51,6 @@ GSP_PAGE_SHIFT, GSP_PAGE_SIZE, // }, - num, regs, sbuffer::SBufferIter, // };
@@ -154,7 +154,7 @@ fn read(#[repr( C , align( 0x1000 ))] #[derive( Debug )] struct MsgqData { - data: [[u8 ;GSP_PAGE_SIZE ]; num::u32_as_usize (MSGQ_NUM_PAGES )], + data: [[u8 ;GSP_PAGE_SIZE ]; casts::u32_as_usize (MSGQ_NUM_PAGES )], }// Annoyingly we are forced to use a literal to specify the alignment of
@@ -229,8 +229,8 @@ unsafe impl FromBytes for GspMem {}impl DmaGspMem {/// Allocate a new instance and map it for `dev`. fn new (dev: &device::Device <device::Bound >) ->Result <Self > { -const MSGQ_SIZE :u32 = num::usize_into_u32 ::<{size_of ::<Msgq >() }>(); -const RX_HDR_OFF :u32 = num::usize_into_u32 ::<{ mem::offset_of!(Msgq , rx) }>(); +const MSGQ_SIZE :u32 = casts::usize_into_u32 ::<{size_of ::<Msgq >() }>(); +const RX_HDR_OFF :u32 = casts::usize_into_u32 ::<{ mem::offset_of!(Msgq , rx) }>();let gsp_mem =Coherent ::<GspMem >::zeroed (dev,GFP_KERNEL )?;
@@ -291,10 +291,10 @@ fn new(dev: &device::Device<device::Bound>) -> Result<Self> {unsafe { ( core::slice::from_raw_parts_mut ( - data.add (num::u32_as_usize (tx)), - num::u32_as_usize (tail_end - tx), + data.add (casts::u32_as_usize (tx)), + casts::u32_as_usize (tail_end - tx), ), - core::slice::from_raw_parts_mut (data, num::u32_as_usize (wrap_end)), + core::slice::from_raw_parts_mut (data, casts::u32_as_usize (wrap_end)), ) } }
@@ -309,7 +309,7 @@ fn driver_write_area_size(&self) -> usize {// `cpu_write_ptr`. The minimum value case is where `rx == 0` and `tx == MSGQ_NUM_PAGES - // 1`, which gives `0 + MSGQ_NUM_PAGES - (MSGQ_NUM_PAGES - 1) - 1 == 0`. let slots = (rx +MSGQ_NUM_PAGES - tx -1 ) %MSGQ_NUM_PAGES ; - num::u32_as_usize (slots) *GSP_PAGE_SIZE + casts::u32_as_usize (slots) *GSP_PAGE_SIZE }/// Returns the region of the GSP message queue that the driver is currently allowed to read
@@ -345,10 +345,10 @@ fn driver_write_area_size(&self) -> usize {unsafe { ( core::slice::from_raw_parts ( - data.add (num::u32_as_usize (rx)), - num::u32_as_usize (tail_end - rx), + data.add (casts::u32_as_usize (rx)), + casts::u32_as_usize (tail_end - rx), ), - core::slice::from_raw_parts (data, num::u32_as_usize (wrap_end)), + core::slice::from_raw_parts (data, casts::u32_as_usize (wrap_end)), ) } }
drivers/gpu/nova-core/gsp/fw.rs +21 -21
109 lines not on this page · raw message
drivers/gpu/nova-core/gsp/sequencer.rs +1 -1
@@ -10,6 +10,7 @@ poll::read_poll_timeout, Io, // }, + num::casts::FromSafeCast, prelude::*, time::{ delay::fsleep,
@@ -32,7 +33,6 @@ }, fw, }, - num::FromSafeCast, sbuffer::SBufferIter, };
drivers/gpu/nova-core/num.rs +0 -211
217 lines not on this page · raw message
drivers/gpu/nova-core/vbios.rs +1 -1
@@ -5,6 +5,7 @@use kernel::{ device, io::Io , + num::casts::FromSafeCast , prelude::*, ptr::{Alignable ,
@@ -26,7 +27,6 @@ FalconUCodeDescV2, FalconUCodeDescV3, // }, - num::FromSafeCast, };/// BIOS Image Type from PCI Data Structure code_type field.
Signature · 1 line
2.55.0
Quoted 3 lines · Alexandre Courbot
Alexandre Courbot (2):
rust: add functions and traits for lossless integer conversions
gpu: nova-core: use kernel lossless integer conversion module
Quoted 2 lines · Alexandre Courbot
rust: add functions and traits for lossless integer conversions
Applied (just this one) to rust-next yesterday (next-20260813) -- thanks everyone!
[ Added a few more intra-doc links. - Miguel ]
The second would have a bunch of conflicts when merging in linux-next -- if you really want it this cycle, please let me know.
Cheers,
Miguel