Skip to contentSkip to conversation

rust: page: Simplify overflow check using checked_add()

PATCH In review Kari Argillander 3 participants 5 messages Dec 23, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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).

No functional change intended.

---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
rust/kernel/page.rs +9 -10
index 432fc0297d4a..cd2af7e4c357 100644
@@ -239,17 +239,16 @@ 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)
+        // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82.
+        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]>
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 27 lines · On Tue, 23 Dec 2025 12:06:17 +0200 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).

No functional change intended.

---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
rust/kernel/page.rs +0 -1
index 432fc0297d4a..cd2af7e4c357 100644
@@ -239,17 +239,16 @@ 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)
+        // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82.

I was about to suggest just enable the feature gate, but turns out it was only added in 1.81. That's a fast one to stabilize!

Best,
Gary

Quoted 18 lines
rust/kernel/page.rs +8 -1
+        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,

#2 lore raw
Quoted 35 lines · Gary Guo
Quoted 32 lines · On Tue, 23 Dec 2025 12:06:17 +0200 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).

No functional change intended.

---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
rust/kernel/page.rs +0 -6
index 432fc0297d4a..cd2af7e4c357 100644
@@ -239,17 +239,16 @@ 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.
Quoted 8 lines

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

Quoted 5 lines
rust/kernel/page.rs +1 -4
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82.

I was about to suggest just enable the feature gate, but turns out it was only added in 1.81. That's a fast one to stabilize!

With Alexandre's matches! proposal I was thinking that we could drop the TODO comment and we won't need to touch this file again once MSRV is >= 1.82. Opinions?

Cheers

Dirk

Quoted 25 lines

Best,
Gary

Quoted 18 lines
rust/kernel/page.rs +8 -1
+        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,

reviewer
#3 lore raw
Quoted 46 lines · On Tue, 23 Dec 2025 13:29:10 +0100 Dirk Behme <[email protected]> wrote:
Quoted 35 lines · Gary Guo
Quoted 32 lines · On Tue, 23 Dec 2025 12:06:17 +0200 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).

No functional change intended.

---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
rust/kernel/page.rs +0 -6
index 432fc0297d4a..cd2af7e4c357 100644
@@ -239,17 +239,16 @@ 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.
Quoted 8 lines

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

Quoted 5 lines
rust/kernel/page.rs +1 -4
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82.  

I was about to suggest just enable the feature gate, but turns out it was only added in 1.81. That's a fast one to stabilize!

Quoted 3 lines

With Alexandre's matches! proposal I was thinking that we could drop the TODO comment and we won't need to touch this file again once MSRV is >= 1.82. Opinions.

I personally would prefer map_or to match. The matches! approach is a bit verbose to read to me.

Keeping a TODO comment about MSRV is good IMO as they're the data sources that could justify bumping MSRV (also to remind people of cleaning up after MSRV is bumped).

Best,
Gary

Quoted 14 lines

Cheers

Dirk

Quoted 8 lines

Best,
Gary

Quoted 3 lines
rust/kernel/page.rs +2 -0
+        if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) {
+            return Err(EINVAL);
         }
#4 lore raw
In reply to Dirk Behme
Quoted 47 lines · On 23.12.2025 14.29 +0200 Dirk Behme ([email protected]) wrote:
Quoted 35 lines · Gary Guo
Quoted 32 lines · On Tue, 23 Dec 2025 12:06:17 +0200 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).

No functional change intended.

---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
rust/kernel/page.rs +0 -6
index 432fc0297d4a..cd2af7e4c357 100644
@@ -239,17 +239,16 @@ 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.
Quoted 8 lines

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

Quoted 5 lines
rust/kernel/page.rs +1 -4
-                f(unsafe { page_addr.add(off) })
-            })
-        } else {
-            Err(EINVAL)
+        // TODO: Replace `map_or` with `is_none_or` once the MSRV is >= 1.82.

I was about to suggest just enable the feature gate, but turns out it was only added in 1.81. That's a fast one to stabilize!

Quoted 3 lines

With Alexandre's matches! proposal I was thinking that we could drop the TODO comment and we won't need to touch this file again once MSRV is >= 1.82. Opinions?

Personally I like is_none_or more than matches! but then thinking again I agree that it might be better because we do not have to leave TODO here. I will respin again tomorrow.

I also checked that output for at least x86 is identical.

Quoted 31 lines

Cheers

Dirk

Quoted 25 lines

Best,
Gary

Quoted 18 lines
rust/kernel/page.rs +8 -1
+        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,