Skip to contentSkip to conversation

rust: page: Simplify overflow check using checked_add()

PATCH Superseded Kari Argillander 3 participants 4 messages Dec 19, 2025 – Dec 23, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: page: Simplify overflow check using checked_add()

Replace the explicit bounds comparisons with a single checked_add()-based range check. This avoids redundant comparisons, makes the overflow case explicit, and results in simpler generated code (checked with godbolt for x86).

Option::is_none_or() would be nicer, but it requires Rust 1.82; the kernel currently targets 1.78.

No functional change intended.

---
 rust/kernel/page.rs | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
rust/kernel/page.rs +8 -10
index 432fc0297d4a..a07e6d256860 100644
@@ -239,17 +239,15 @@ fn with_pointer_into_page<T>(
         len: usize,
         f: impl FnOnce(*mut u8) -> Result<T>,
     ) -> Result<T> {
-        let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE;
-
-        if bounds_ok {
-            self.with_page_mapped(move |page_addr| {
-                // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
-                // result in a pointer that is in bounds or one off the end of the page.
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) {
+            return Err(EINVAL);
         }
+
+        self.with_page_mapped(move |page_addr| {
+            // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
+            // result in a pointer that is in bounds or one off the end of the page.
+            f(unsafe { page_addr.add(off) })
+        })
     }
 
     /// Maps the page and reads from it into the given buffer.

---
base-commit: cc3aa43b44bdb43dfbac0fcb51c56594a11338a8
change-id: 20251219-rust-page-check-819ccc39c53a

Best regards,

Signature · 1 line
Kari Argillander <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 47 lines · Kari Argillander

Replace the explicit bounds comparisons with a single checked_add()-based range check. This avoids redundant comparisons, makes the overflow case explicit, and results in simpler generated code (checked with godbolt for x86).

Option::is_none_or() would be nicer, but it requires Rust 1.82; the kernel currently targets 1.78.

No functional change intended.

---
 rust/kernel/page.rs | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
rust/kernel/page.rs +8 -11
index 432fc0297d4a..a07e6d256860 100644
@@ -239,17 +239,15 @@ fn with_pointer_into_page<T>(
         len: usize,
         f: impl FnOnce(*mut u8) -> Result<T>,
     ) -> Result<T> {
-        let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE;
-
-        if bounds_ok {
-            self.with_page_mapped(move |page_addr| {
-                // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
-                // result in a pointer that is in bounds or one off the end of the page.
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) {
+            return Err(EINVAL);
         }
+
+        self.with_page_mapped(move |page_addr| {
+            // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
+            // result in a pointer that is in bounds or one off the end of the page.
+            f(unsafe { page_addr.add(off) })
+        })
     }
 
     /// Maps the page and reads from it into the given buffer.
 
---
Quoted 2 lines

base-commit: cc3aa43b44bdb43dfbac0fcb51c56594a11338a8
change-id: 20251219-rust-page-check-819ccc39c53a

Thanks, this switches to the early return style we recently discussed in [1].

I just wonder if we would like to add a comment like

// TODO: replace map_or with is_none_or once the MSRV is >= 1.82.

like we do in e.g. transmute.rs [2]?

Anyway

Thanks

Dirk

[1]
https://lore.kernel.org/rust-for-linux/CANiq72kiscT5euAUjcSzvxMzM9Hdj8aQGeUN_pVF-vHf3DhBuQ@mail.gmail.com/

[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/transmute.rs#n75

#2 lore raw
Quoted 11 lines · On 20.12.2025 10.41 Dirk Behme ([email protected]) wrote:
Quoted 3 lines · Kari Argillander

Option::is_none_or() would be nicer, but it requires Rust 1.82; the kernel currently targets 1.78.

I just wonder if we would like to add a comment like

// TODO: replace map_or with is_none_or once the MSRV is >= 1.82.

like we do in e.g. transmute.rs [2]?

Sure, I can do that. I’ll send a respin maybe next Tuesday.

Quoted 2 lines

[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/rust/kernel/transmute.rs#n75

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Kari Argillander
Quoted 24 lines · Kari Argillander

Replace the explicit bounds comparisons with a single checked_add()-based range check. This avoids redundant comparisons, makes the overflow case explicit, and results in simpler generated code (checked with godbolt for x86).

Option::is_none_or() would be nicer, but it requires Rust 1.82; the kernel currently targets 1.78.

No functional change intended.

---
 rust/kernel/page.rs | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
rust/kernel/page.rs +0 -1
index 432fc0297d4a..a07e6d256860 100644
@@ -239,17 +239,15 @@ fn with_pointer_into_page<T>(
         len: usize,
         f: impl FnOnce(*mut u8) -> Result<T>,
     ) -> Result<T> {
-        let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE;

rust/kernel/page.rs fn with_pointer_into_page<T>( · line 242

Quoted 10 lines
rust/kernel/page.rs +1 -9
-
-        if bounds_ok {
-            self.with_page_mapped(move |page_addr| {
-                // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
-                // result in a pointer that is in bounds or one off the end of the page.
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) {

I find this line a bit heavy with the two-steps check, how about something like:

    if !matches!(off.checked_add(len), Some(e) if e <= PAGE_SIZE) {

Just for your consideration - the original is still valid and an improvement over the original code, so regardless: