Skip to contentSkip to conversation

rust: io: document const-offset requirement for infallible accessors

PATCH Applied Gary Guo 3 participants 7 messages Jul 16 – Jul 21

Subsystems (5)
lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH 1/2 rust: io: document const-offset requirement for infallible accessors

From: Gary Guo <[email protected]>

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.

---
 rust/kernel/io.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
rust/kernel/io.rs +16 -0
index 95f46bb75f9e..a38c20ba3d23 100644
@@ -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

Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 2/2 rust: drm: fix non-const `read8` in unit test

From: Gary Guo <[email protected]>

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

---
 rust/kernel/drm/gem/shmem.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/drm/gem/shmem.rs +1 -1
index 580e0808d6c0..a687d46d170d 100644
@@ -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(())
Signature · 1 line
2.54.0
maintainer
#3 lore raw

Re: [PATCH 2/2] rust: drm: fix non-const `read8` in unit test

In reply to Gary Guo
Quoted 6 lines · Gary Guo

From: Gary Guo <[email protected]>

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

Fixes d055768429b3 ("rust: drm: gem: shmem: Add vmap functions")
Quoted 19 lines
---
 rust/kernel/drm/gem/shmem.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/drm/gem/shmem.rs +1 -1
index 580e0808d6c0..a687d46d170d 100644
@@ -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(())
Signature · 1 line
2.54.0
reviewer Reviewed-by on v1 1/2
#4 lore raw
In reply to Gary Guo
Quoted 7 lines · Gary Guo

From: Gary Guo <[email protected]>

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.

Do we want to say something similar for read/write?

maintainer Applied
#5 lore raw

Re: (subset) [PATCH 1/2] rust: io: document const-offset requirement for infallible accessors

In reply to Gary Guo
Quoted 1 line · Gary Guo

[PATCH 1/2] rust: io: document const-offset requirement for infallible accessors

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[2/2] rust: drm: fix non-const read8 in unit test

      commit: 22e77d81d0a9

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.

maintainer Applied
#6 lore raw

Re: (subset) [PATCH 1/2] rust: io: document const-offset requirement for infallible accessors

In reply to Gary Guo
Quoted 1 line · Gary Guo

[PATCH 1/2] rust: io: document const-offset requirement for infallible accessors

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/2] rust: io: document const-offset requirement for infallible accessors

      commit: c2d8b262057c

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.