All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] rust: io: document const-offset requirement for infallible accessors
@ 2026-07-16 14:25 Gary Guo
  2026-07-16 14:25 ` [PATCH 2/2] rust: drm: fix non-const `read8` in unit test Gary Guo
  0 siblings, 1 reply; 2+ messages in thread
From: Gary Guo @ 2026-07-16 14:25 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: driver-core, rust-for-linux, linux-kernel

From: Gary Guo <gary@garyguo.net>

Due to the usage of `build_assert!` for address validity checking, these
accessors want constant offsets. Non-constant offsets can work but it
depend on compiler optimization levels, so it should be avoided.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/io.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 95f46bb75f9e..a38c20ba3d23 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -721,6 +721,8 @@ fn try_write64(self, value: u64, offset: usize) -> Result
     }
 
     /// Infallible 8-bit read with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn read8(self, offset: usize) -> u8
     where
@@ -731,6 +733,8 @@ fn read8(self, offset: usize) -> u8
     }
 
     /// Infallible 16-bit read with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn read16(self, offset: usize) -> u16
     where
@@ -741,6 +745,8 @@ fn read16(self, offset: usize) -> u16
     }
 
     /// Infallible 32-bit read with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn read32(self, offset: usize) -> u32
     where
@@ -751,6 +757,8 @@ fn read32(self, offset: usize) -> u32
     }
 
     /// Infallible 64-bit read with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn read64(self, offset: usize) -> u64
     where
@@ -761,6 +769,8 @@ fn read64(self, offset: usize) -> u64
     }
 
     /// Infallible 8-bit write with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn write8(self, value: u8, offset: usize)
     where
@@ -771,6 +781,8 @@ fn write8(self, value: u8, offset: usize)
     }
 
     /// Infallible 16-bit write with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn write16(self, value: u16, offset: usize)
     where
@@ -781,6 +793,8 @@ fn write16(self, value: u16, offset: usize)
     }
 
     /// Infallible 32-bit write with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn write32(self, value: u32, offset: usize)
     where
@@ -791,6 +805,8 @@ fn write32(self, value: u32, offset: usize)
     }
 
     /// Infallible 64-bit write with compile-time bounds check.
+    ///
+    /// `offset` should be constant.
     #[inline(always)]
     fn write64(self, value: u64, offset: usize)
     where

base-commit: b8809969e1d7a591e0f49dd464a5d04b3cf02ab1
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] rust: drm: fix non-const `read8` in unit test
  2026-07-16 14:25 [PATCH 1/2] rust: io: document const-offset requirement for infallible accessors Gary Guo
@ 2026-07-16 14:25 ` Gary Guo
  0 siblings, 0 replies; 2+ messages in thread
From: Gary Guo @ 2026-07-16 14:25 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, David Airlie, Simona Vetter
  Cc: driver-core, rust-for-linux, linux-kernel, dri-devel

From: Gary Guo <gary@garyguo.net>

With CONFIG_CC_OPTIMIZE_FOR_SIZE, the address validity check in non-const
`read8` invocaction is not optimized away, leading to build failure.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/drm/gem/shmem.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 580e0808d6c0..a687d46d170d 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -688,7 +688,7 @@ fn vmap_io() -> Result {
         // Ensure the ordering in memory is correct
         let expected = 0xFEDCBA98_u32.to_ne_bytes().into_iter();
         for (offset, expected) in (0x20..=0x23).zip(expected) {
-            assert_eq!(vmap.read8(offset), expected);
+            assert_eq!(vmap.try_read8(offset).unwrap(), expected);
         }
 
         Ok(())
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-16 14:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-16 14:25 [PATCH 1/2] rust: io: document const-offset requirement for infallible accessors Gary Guo
2026-07-16 14:25 ` [PATCH 2/2] rust: drm: fix non-const `read8` in unit test Gary Guo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.