From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Daniel Almeida" <daniel.almeida@collabora.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 06/13] drm/tyr: replace lossless `as` casts with From and FromSafeCast
Date: Wed, 25 Nov 2026 19:46:57 -0300 [thread overview]
Message-ID: <20261125-b4-tyr-as-casts-v1-6-2b2f53164d86@collabora.com> (raw)
In-Reply-To: <20261125-b4-tyr-as-casts-v1-0-2b2f53164d86@collabora.com>
An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, replace the
casts from `usize` to `u64`, from `u32` to `usize` and from a
`Bounded<u64, 8>` to `u8` with conversions that cannot lose bits:
mapped as u64 -> u64::from_safe_cast(mapped)
fw_header.size as usize -> usize::from_safe_cast(fw_header.size)
val.get() as u8 -> u8::from(val)
`FromSafeCast` is only implemented where no value is lost on any
architecture, and `From` for a `Bounded` only where all its bits fit.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/fw.rs | 9 ++++++---
drivers/gpu/drm/tyr/fw/parser.rs | 11 +++++++----
drivers/gpu/drm/tyr/regs.rs | 2 +-
drivers/gpu/drm/tyr/vm.rs | 9 +++++----
4 files changed, 19 insertions(+), 12 deletions(-)
diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index aea151ff1f93..14cfe2884d5a 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -26,7 +26,10 @@
poll,
Io, //
},
- num::Bounded,
+ num::{
+ casts::FromSafeCast,
+ Bounded, //
+ },
prelude::*,
str::CString,
sync::{
@@ -238,8 +241,8 @@ pub(crate) fn new(
parsed.vm_map_flags,
)?;
- let section_start = parsed.data_range.start as usize;
- let section_end = parsed.data_range.end as usize;
+ let section_start = usize::from_safe_cast(parsed.data_range.start);
+ let section_end = usize::from_safe_cast(parsed.data_range.end);
let mut data = KVec::new();
// Ensure that the firmware slice is not out of bounds.
diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index 6ff681aa78d1..6576bc870982 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -18,7 +18,10 @@
use kernel::{
bitfield,
device::Device,
- num::Bounded,
+ num::{
+ casts::FromSafeCast,
+ Bounded, //
+ },
prelude::*,
sizes::SizeConstants, //
};
@@ -182,7 +185,7 @@ pub(super) fn new(dev: &'a Device, data: &'a [u8]) -> Self {
/// in the binary, extracting section information needed for loading.
pub(super) fn parse(&mut self) -> Result<KVec<ParsedSection>> {
let fw_header = self.parse_fw_header()?;
- let header_end = fw_header.size as usize;
+ let header_end = usize::from_safe_cast(fw_header.size);
let mut parsed_sections = KVec::new();
while self.cursor.pos() < header_end {
@@ -210,7 +213,7 @@ fn parse_fw_header(&mut self) -> Result<FirmwareHeader> {
}
};
- if fw_header.size as usize > self.cursor.len() {
+ if usize::from_safe_cast(fw_header.size) > self.cursor.len() {
dev_err!(self.cursor.dev, "Firmware image is truncated");
return Err(EINVAL);
}
@@ -323,7 +326,7 @@ fn parse_section_entry(
return Err(EINVAL);
}
- if section_hdr.data.end as usize > firmware_size {
+ if usize::from_safe_cast(section_hdr.data.end) > firmware_size {
dev_err!(
entry_cursor.dev,
"Firmware data range {:#x}..{:#x} exceeds firmware size {:#x}",
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 69c901afb28f..603a9052c439 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -1154,7 +1154,7 @@ impl TryFrom<Bounded<u64, 8>> for MMU_MEMATTR_STAGE1 {
type Error = Error;
fn try_from(val: Bounded<u64, 8>) -> Result<Self, Self::Error> {
- Ok(Self::from_raw(val.get() as u8))
+ Ok(Self::from_raw(u8::from(val)))
}
}
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 72c8593c489e..d15db120352b 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -41,6 +41,7 @@
ARM64LPAES1, //
},
new_mutex,
+ num::casts::FromSafeCast,
prelude::*,
sizes::SizeConstants,
sync::{
@@ -616,7 +617,7 @@ fn sm_step_map<'op>(
// end of the GEM buffer itself.
let gem_range_end = op.gem_offset().checked_add(op.length()).ok_or(EINVAL)?;
- if gem_range_end > op.obj().size() as u64 {
+ if gem_range_end > u64::from_safe_cast(op.obj().size()) {
dev_err!(
context.dev,
"Requested GEM range ends at {} which is beyond the GEM buffer size {}",
@@ -891,7 +892,7 @@ fn pt_map(
return Err(ENOMEM);
}
- segment_mapped += mapped as u64;
+ segment_mapped += u64::from_safe_cast(mapped);
}
Ok(segment_mapped)
@@ -937,8 +938,8 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)
return Err(EINVAL);
}
- bytes_left_to_unmap -= unmapped as u64;
- iova += unmapped as u64;
+ bytes_left_to_unmap -= u64::from_safe_cast(unmapped);
+ iova += u64::from_safe_cast(unmapped);
}
Ok(())
--
2.56.0
next prev parent reply other threads:[~2026-11-25 22:48 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
2026-11-25 22:46 ` [PATCH 01/13] drm/tyr: convert SeatInfo::slot from u8 to usize Daniel Almeida
2026-11-25 22:46 ` [PATCH 02/13] drm/tyr: convert VmAsData::va_bits from u8 to u32 Daniel Almeida
2026-11-25 22:46 ` [PATCH 03/13] drm/tyr: convert the lock region exponents " Daniel Almeida
2026-11-25 22:46 ` [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!` Daniel Almeida
2026-11-25 22:46 ` [PATCH 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants Daniel Almeida
2026-11-25 22:46 ` Daniel Almeida [this message]
2026-11-25 22:46 ` [PATCH 07/13] drm/tyr: use `cv!` for constant conversions Daniel Almeida
2026-11-25 22:46 ` [PATCH 08/13] drm/tyr: use VmMapFlags::all_bits() to validate map flags Daniel Almeida
2026-11-25 22:47 ` [PATCH 09/13] drm/tyr: justify the casts of enums to their integer value Daniel Almeida
2026-11-25 22:47 ` [PATCH 10/13] drm/tyr: add split_u64() for the 64-bit register values Daniel Almeida
2026-11-25 22:47 ` [PATCH 11/13] drm/tyr: return usize from get_pgsize() Daniel Almeida
2026-11-25 22:47 ` [PATCH 12/13] drm/tyr: justify the physical address cast in pt_map() Daniel Almeida
2026-11-25 22:47 ` [PATCH 13/13] drm/tyr: deny `clippy::as_conversions` Daniel Almeida
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125-b4-tyr-as-casts-v1-6-2b2f53164d86@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®