linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: page: Simplify overflow check using checked_add()
@ 2025-12-23 10:06 Kari Argillander
  2025-12-23 11:57 ` Gary Guo
  0 siblings, 1 reply; 5+ messages in thread
From: Kari Argillander @ 2025-12-23 10:06 UTC (permalink / raw)
  To: Alice Ryhl, Dirk Behme, Alexandre Courbot, Lorenzo Stoakes,
	Liam R. Howlett, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich
  Cc: linux-mm, rust-for-linux, linux-kernel, 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.

Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
---
Changes in v2:
- Added MSVR todo (Dirk Behme)
- Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com
---
 rust/kernel/page.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 432fc0297d4a..cd2af7e4c357 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -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,
-- 
Kari Argillander <kari.argillander@gmail.com>



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

* Re: [PATCH v2] rust: page: Simplify overflow check using checked_add()
  2025-12-23 10:06 [PATCH v2] rust: page: Simplify overflow check using checked_add() Kari Argillander
@ 2025-12-23 11:57 ` Gary Guo
  2025-12-23 12:29   ` Dirk Behme
  0 siblings, 1 reply; 5+ messages in thread
From: Gary Guo @ 2025-12-23 11:57 UTC (permalink / raw)
  To: Kari Argillander
  Cc: Alice Ryhl, Dirk Behme, Alexandre Courbot, Lorenzo Stoakes,
	Liam R. Howlett, Miguel Ojeda, Boqun Feng, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	linux-mm, rust-for-linux, linux-kernel

On Tue, 23 Dec 2025 12:06:17 +0200
Kari Argillander <kari.argillander@gmail.com> wrote:

> 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.
> 
> Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
> ---
> Changes in v2:
> - Added MSVR todo (Dirk Behme)
> - Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com
> ---
>  rust/kernel/page.rs | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 432fc0297d4a..cd2af7e4c357 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -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.

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!

Reviewed-by: Gary Guo <gary@garyguo.net>

Best,
Gary

> +        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,



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

* Re: [PATCH v2] rust: page: Simplify overflow check using checked_add()
  2025-12-23 11:57 ` Gary Guo
@ 2025-12-23 12:29   ` Dirk Behme
  2025-12-23 12:50     ` Gary Guo
  2025-12-23 12:50     ` Kari Argillander
  0 siblings, 2 replies; 5+ messages in thread
From: Dirk Behme @ 2025-12-23 12:29 UTC (permalink / raw)
  To: Gary Guo, Kari Argillander
  Cc: Alice Ryhl, Dirk Behme, Alexandre Courbot, Lorenzo Stoakes,
	Liam R. Howlett, Miguel Ojeda, Boqun Feng, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	linux-mm, rust-for-linux, linux-kernel

On 23.12.25 12:57, Gary Guo wrote:
> On Tue, 23 Dec 2025 12:06:17 +0200
> Kari Argillander <kari.argillander@gmail.com> wrote:
> 
>> 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.
>>
>> Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
>> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
>> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
>> ---
>> Changes in v2:
>> - Added MSVR todo (Dirk Behme)
>> - Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com
>> ---
>>  rust/kernel/page.rs | 19 +++++++++----------
>>  1 file changed, 9 insertions(+), 10 deletions(-)
>>
>> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
>> index 432fc0297d4a..cd2af7e4c357 100644
>> --- a/rust/kernel/page.rs
>> +++ b/rust/kernel/page.rs
>> @@ -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.
> 
> 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


> Reviewed-by: Gary Guo <gary@garyguo.net>
> 
> Best,
> Gary
> 
>> +        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,
> 
> 



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

* Re: [PATCH v2] rust: page: Simplify overflow check using checked_add()
  2025-12-23 12:29   ` Dirk Behme
@ 2025-12-23 12:50     ` Gary Guo
  2025-12-23 12:50     ` Kari Argillander
  1 sibling, 0 replies; 5+ messages in thread
From: Gary Guo @ 2025-12-23 12:50 UTC (permalink / raw)
  To: Dirk Behme
  Cc: Kari Argillander, Alice Ryhl, Dirk Behme, Alexandre Courbot,
	Lorenzo Stoakes, Liam R. Howlett, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, linux-mm, rust-for-linux,
	linux-kernel

On Tue, 23 Dec 2025 13:29:10 +0100
Dirk Behme <dirk.behme@gmail.com> wrote:

> On 23.12.25 12:57, Gary Guo wrote:
> > On Tue, 23 Dec 2025 12:06:17 +0200
> > Kari Argillander <kari.argillander@gmail.com> wrote:
> >   
> >> 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.
> >>
> >> Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
> >> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
> >> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
> >> ---
> >> Changes in v2:
> >> - Added MSVR todo (Dirk Behme)
> >> - Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com
> >> ---
> >>  rust/kernel/page.rs | 19 +++++++++----------
> >>  1 file changed, 9 insertions(+), 10 deletions(-)
> >>
> >> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> >> index 432fc0297d4a..cd2af7e4c357 100644
> >> --- a/rust/kernel/page.rs
> >> +++ b/rust/kernel/page.rs
> >> @@ -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.  
> > 
> > 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.

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

> 
> Cheers
> 
> Dirk
> 
> 
> > Reviewed-by: Gary Guo <gary@garyguo.net>
> > 
> > Best,
> > Gary
> >   
> >> +        if off.checked_add(len).map_or(true, |end| end > PAGE_SIZE) {
> >> +            return Err(EINVAL);
> >>          }


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

* Re: [PATCH v2] rust: page: Simplify overflow check using checked_add()
  2025-12-23 12:29   ` Dirk Behme
  2025-12-23 12:50     ` Gary Guo
@ 2025-12-23 12:50     ` Kari Argillander
  1 sibling, 0 replies; 5+ messages in thread
From: Kari Argillander @ 2025-12-23 12:50 UTC (permalink / raw)
  To: Dirk Behme
  Cc: Gary Guo, Alice Ryhl, Dirk Behme, Alexandre Courbot,
	Lorenzo Stoakes, Liam R. Howlett, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, linux-mm, rust-for-linux,
	linux-kernel

On 23.12.2025 14.29 +0200 Dirk Behme (dirk.behme@gmail.com) wrote:
>
> On 23.12.25 12:57, Gary Guo wrote:
> > On Tue, 23 Dec 2025 12:06:17 +0200
> > Kari Argillander <kari.argillander@gmail.com> wrote:
> >
> >> 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.
> >>
> >> Reviewed-by: Dirk Behme <dirk.behme@de.bosch.com>
> >> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
> >> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
> >> ---
> >> Changes in v2:
> >> - Added MSVR todo (Dirk Behme)
> >> - Link to v1: https://lore.kernel.org/r/20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com
> >> ---
> >>  rust/kernel/page.rs | 19 +++++++++----------
> >>  1 file changed, 9 insertions(+), 10 deletions(-)
> >>
> >> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> >> index 432fc0297d4a..cd2af7e4c357 100644
> >> --- a/rust/kernel/page.rs
> >> +++ b/rust/kernel/page.rs
> >> @@ -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.
> >
> > 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?

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.

> Cheers
>
> Dirk
>
>
> > Reviewed-by: Gary Guo <gary@garyguo.net>
> >
> > Best,
> > Gary
> >
> >> +        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,
> >
> >
>


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

end of thread, other threads:[~2025-12-23 12:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-23 10:06 [PATCH v2] rust: page: Simplify overflow check using checked_add() Kari Argillander
2025-12-23 11:57 ` Gary Guo
2025-12-23 12:29   ` Dirk Behme
2025-12-23 12:50     ` Gary Guo
2025-12-23 12:50     ` Kari Argillander

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox