Skip to contentSkip to conversation

rust: io: require proper alignment in the safety contracts of `IoCapable`

PATCH In review Alexandre Courbot 2 participants 3 messages Jun 8 – Jun 9

Subsystems (3)
lore
Share & tools
lore

Alexandre Courbot

author reviewer
#0 lore raw

PATCH 1/1 rust: io: require proper alignment in the safety contracts of `IoCapable`

The addresses passed to io_read and io_write must be properly aligned, but the safety contract only mentions a valid range as a requirement.

Add alignment to the requirements. The existing call sites already obtain the address through io_addr() or io_addr_assert(), which both check alignment, so no code changes are needed to them.

---
I noticed this when reviewing the GEM SHMEM series; it is probably better to explicitly mention this requirement.

---
 rust/kernel/io.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/io.rs +4 -2
index fcc7678fd9e3..854932d1cb72 100644
@@ -166,14 +166,16 @@ pub trait IoCapable<T> {
     ///
     /// # Safety
     ///
-    /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - `address` must be properly aligned for `T`.
     unsafe fn io_read(&self, address: usize) -> T;
 
     /// Performs an I/O write of `value` at `address`.
     ///
     /// # Safety
     ///
-    /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - `address` must be properly aligned for `T`.
     unsafe fn io_write(&self, value: T, address: usize);
 }
 

---
base-commit: 46def663dd34da36464ba059f7cfeacf29d98e5e
change-id: 20260607-iocapable-align-fa8bf1ac7208

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
reviewer
#1 lore raw
Quoted 9 lines · Alexandre Courbot

The addresses passed to io_read and io_write must be properly aligned, but the safety contract only mentions a valid range as a requirement.

Add alignment to the requirements. The existing call sites already obtain the address through io_addr() or io_addr_assert(), which both check alignment, so no code changes are needed to them.

There is
https://lore.kernel.org/rust-for-linux/[email protected]/.

Although this patch will be gone from v3, as IoCapable is not going to be unsafe anymore.

Best,
Gary

Quoted 38 lines

---
I noticed this when reviewing the GEM SHMEM series; it is probably better to explicitly mention this requirement.

---
 rust/kernel/io.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/io.rs +4 -3
index fcc7678fd9e3..854932d1cb72 100644
@@ -166,14 +166,16 @@ pub trait IoCapable<T> {
     ///
     /// # Safety
     ///
-    /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - `address` must be properly aligned for `T`.
     unsafe fn io_read(&self, address: usize) -> T;
 
     /// Performs an I/O write of `value` at `address`.
     ///
     /// # Safety
     ///
-    /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
+    /// - `address` must be properly aligned for `T`.
     unsafe fn io_write(&self, value: T, address: usize);
 }
 
 
---

base-commit: 46def663dd34da36464ba059f7cfeacf29d98e5e
change-id: 20260607-iocapable-align-fa8bf1ac7208

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author reviewer
#2 lore raw
Quoted 9 lines · Gary Guo
Quoted 8 lines · Alexandre Courbot

The addresses passed to io_read and io_write must be properly aligned, but the safety contract only mentions a valid range as a requirement.

Add alignment to the requirements. The existing call sites already obtain the address through io_addr() or io_addr_assert(), which both check alignment, so no code changes are needed to them.

Quoted 7 lines
Quoted 1 line

There is
https://lore.kernel.org/rust-for-linux/[email protected]/.

Although this patch will be gone from v3, as IoCapable is not going to be unsafe anymore.

Ah, perfect! Dropping this patch then.