rust: add functions and traits for lossless integer conversions
Subsystems (7)
Alexandre Courbot
maintainercover 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.
It is way overdue since its RFC [1]; recent dicussions [2] that have suggested that it might become useful have triggered this posting.
This series is based on rust-next.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/all/[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]
To: Alexandre Courbot <[email protected]>
To: Yury Norov <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Daniel Almeida <[email protected]>
To: Tamir Duberstein <[email protected]>
To: Onur Özkan <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
Cc: John Hubbard <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Timur Tabi <[email protected]>
Cc: Eliot Courtney <[email protected]>
Cc: Zhi Wang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [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 | 10 +- drivers/gpu/nova-core/falcon/fsp.rs | 2 +- drivers/gpu/nova-core/fb.rs | 2 +- drivers/gpu/nova-core/fb/hal/gb100.rs | 6 +- 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 | 2 +- drivers/gpu/nova-core/firmware/riscv.rs | 6 +- drivers/gpu/nova-core/fsp.rs | 2 +- drivers/gpu/nova-core/gsp.rs | 2 +- drivers/gpu/nova-core/gsp/cmdq.rs | 4 +- drivers/gpu/nova-core/gsp/fw.rs | 8 +- 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 | 3 + rust/kernel/num/casts.rs | 248 +++++++++++++++++++++ 19 files changed, 286 insertions(+), 246 deletions(-)
---
base-commit: 3dab139d4795f688e4f243e40c7474df00d329d9
change-id: 20251104-as_casts-6a8882ac0192
Best regards,
Signature · 1 line
Alexandre Courbot <[email protected]>
Alexandre Courbot
maintainerPATCH 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 need to 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.
--- rust/kernel/num.rs | 3 + rust/kernel/num/casts.rs | 248 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+)
rust/kernel/num.rs +3 -0
@@ -5,7 +5,10 @@use core::ops;pub mod bounded; +pub mod casts; +pub use bounded::*; +pub use casts::*;/// Designates unsigned primitive types. pub enum Unsigned {}
rust/kernel/num/casts.rs new file +248 -0
248 lines not on this page · raw message
Signature · 1 line
2.55.0
Gary Guo
reviewerQuoted 11 lines · Alexandre Courbot
The core library's
Fromimplementations do not cover conversions that are not portable or future-proof. For instance, even though it is safe today,From<usize>is not implemented foru64because 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 need to rely on the
askeyword and carry the risk of silently losing data.
Quoted 5 lines
Thus, introduce a new module
num::caststhat provides safe const functions performing more conversions allowed by the build target, as well asFromSafeCastandIntoSafeCasttraits that are just extensions ofFromandIntoto conversions that are known to be lossless.
I'm okay with having u32 -> usize casts and usize -> u64 casts, but having usize -> u32 and u64 -> usize casts like this series do is worrying as it can be misused from drivers that are supposed to be supporting multiple platforms or core subsystems.
Pointer-size dependent drivers that need these casts should define them locally.
Best,
Gary
Quoted 8 lines
Suggested-by Danilo Krummrich <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/num.rs | 3 + rust/kernel/num/casts.rs | 248 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+)
Quoted 7 lines · Gary Guo
I'm okay with having u32 -> usize casts and usize -> u64 casts, but having usize -> u32 and u64 -> usize casts like this series do is worrying as it can be misused from drivers that are supposed to be supporting multiple platforms or core subsystems.
Pointer-size dependent drivers that need these casts should define them locally.
Ideally we would define it once, but nevertheless make it explicit enough (somehow) that this makes one not build in certain configs, so that we get no repetition and no confusion.
Something like splitting the non-portable ones into a different trait/mod/method name/... so that it is very clear that it makes you (i.e. the user) user pointer-size dependent just by e.g. greping for it.
Cheers,
Miguel
Danilo Krummrich
maintainerQuoted 18 lines · Gary Guo
Quoted 11 lines · Alexandre Courbot
The core library's
Fromimplementations do not cover conversions that are not portable or future-proof. For instance, even though it is safe today,From<usize>is not implemented foru64because 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 need to rely on the
askeyword and carry the risk of silently losing data.Quoted 5 lines
Thus, introduce a new module
num::caststhat provides safe const functions performing more conversions allowed by the build target, as well asFromSafeCastandIntoSafeCasttraits that are just extensions ofFromandIntoto conversions that are known to be lossless.
Quoted 6 lines
I'm okay with having u32 -> usize casts and usize -> u64 casts, but having usize -> u32 and u64 -> usize casts like this series do is worrying as it can be misused from drivers that are supposed to be supporting multiple platforms or core subsystems.
Pointer-size dependent drivers that need these casts should define them locally.
I also raised this concern in the context of [1]. On the other hand, it is not as if this can "randomly" break with exotic or niche kernel configs; 32bit vs. 64bit compile tests are well covered.
So, as long as we are explicit about it, I think it should be fine. Maybe we can export the non-portable ones with a different name?
Alexandre Courbot
maintainerPATCH 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 | 10 +- drivers/gpu/nova-core/falcon/fsp.rs | 2 +- drivers/gpu/nova-core/fb.rs | 2 +- drivers/gpu/nova-core/fb/hal/gb100.rs | 6 +- 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 | 2 +- drivers/gpu/nova-core/firmware/riscv.rs | 6 +- drivers/gpu/nova-core/fsp.rs | 2 +- drivers/gpu/nova-core/gsp.rs | 2 +- drivers/gpu/nova-core/gsp/cmdq.rs | 4 +- drivers/gpu/nova-core/gsp/fw.rs | 8 +- 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, 35 insertions(+), 246 deletions(-)
drivers/gpu/nova-core/falcon.rs +5 -5
@@ -23,6 +23,10 @@ }, Io, }, + num::{ + 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;
drivers/gpu/nova-core/falcon/fsp.rs +1 -1
@@ -16,6 +16,7 @@ }, Io, // }, + num, prelude::*, time::Delta, };
@@ -28,7 +29,6 @@ PFalcon2Base, PFalconBase, // }, - num, regs, // };
drivers/gpu/nova-core/fb.rs +1 -1
@@ -10,6 +10,7 @@ dma::CoherentHandle, fmt, io::Io, + num::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 +4 -2
@@ -11,7 +11,10 @@ }, Io, // }, - num::Bounded, + num::{ + usize_into_u32, + Bounded, // + }, prelude::*, ptr::{ const_align_up,
@@ -23,7 +26,6 @@use crate ::{ driver::Bar0 , fb::hal::FbHal , - num::usize_into_u32, regs,// };
drivers/gpu/nova-core/firmware.rs +4 -4
@@ -10,6 +10,10 @@use kernel::{ device, firmware, + num::{ +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::{ +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::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::FromSafeCast, prelude::*, ptr::{Alignable ,
@@ -51,7 +52,6 @@ FIRMWARE_VERSION, // }, gpu::Chipset, - num::FromSafeCast, regs, };
drivers/gpu/nova-core/firmware/gsp.rs +1 -1
@@ -8,6 +8,7 @@ DataDirection, DmaAddress, // }, + num::FromSafeCast, prelude::*, scatterlist::{Owned ,
@@ -25,7 +26,6 @@ Chipset, // }, gsp::GSP_PAGE_SIZE, - num::FromSafeCast, };/// GSP firmware with 3-level radix page tables for the GSP bootloader.
drivers/gpu/nova-core/firmware/riscv.rs +2 -4
@@ -7,14 +7,12 @@ device, dma::Coherent, firmware::Firmware, + num::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 +1 -1
@@ -11,6 +11,7 @@ device, dma::Coherent, io::poll::read_poll_timeout, + num, prelude::*, ptr::{Alignable ,
@@ -42,7 +43,6 @@ NvdmHeader, NvdmType, // }, - num, regs, // };
drivers/gpu/nova-core/gsp.rs +1 -1
@@ -11,6 +11,7 @@ CoherentBox, DmaAddress, // }, + num, pci, prelude::*, transmute::{
@@ -36,7 +37,6 @@ GspArgumentsPadded, LibosMemoryRegionInitArgument, // }, - num, };pub (crate )const GSP_PAGE_SHIFT :usize =12 ;
drivers/gpu/nova-core/gsp/cmdq.rs +2 -2
@@ -16,6 +16,7 @@ Io, // }, new_mutex, + num, 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, // };
drivers/gpu/nova-core/gsp/fw.rs +4 -4
@@ -11,6 +11,10 @@use kernel::{ dma::Coherent , + num::{ + self, +FromSafeCast ,// + }, prelude::*, ptr::{Alignable ,
@@ -38,10 +42,6 @@ cmdq::Cmdq, // GSP_PAGE_SIZE, }, - num::{ - self, -FromSafeCast ,// - }, };// TODO: Replace with `IoView` projections once available.
drivers/gpu/nova-core/gsp/sequencer.rs +1 -1
@@ -10,6 +10,7 @@ poll::read_poll_timeout, Io, // }, + num::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::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