From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Kari Argillander" <kari.argillander@gmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: <linux-mm@kvack.org>, <rust-for-linux@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] rust: page: Simplify overflow check using checked_add()
Date: Tue, 23 Dec 2025 16:12:30 +0900 [thread overview]
Message-ID: <DF5ER5QH96IJ.3TVQDT1YQ39VA@nvidia.com> (raw)
In-Reply-To: <20251219-rust-page-check-v1-1-df2e52fa3bd5@gmail.com>
On Sat Dec 20, 2025 at 6:29 AM JST, Kari Argillander 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).
>
> Option::is_none_or() would be nicer, but it requires Rust 1.82; the
> kernel currently targets 1.78.
>
> No functional change intended.
>
> Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
> ---
> rust/kernel/page.rs | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 432fc0297d4a..a07e6d256860 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -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) {
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:
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
prev parent reply other threads:[~2025-12-23 7:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-19 21:29 Kari Argillander
2025-12-20 8:41 ` Dirk Behme
2025-12-20 12:53 ` Kari Argillander
2025-12-23 7:12 ` Alexandre Courbot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DF5ER5QH96IJ.3TVQDT1YQ39VA@nvidia.com \
--to=acourbot@nvidia.com \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=kari.argillander@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox