rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Brendan Shephard <bshephar@bne-home.net>
To: dakr@kernel.org, acourbot@nvidia.com, airlied@gmail.com,
	aliceryhl@google.com, daniel.almeida@collabora.com
Cc: rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2] rust: Return Option from page_align and ensure no usize overflow
Date: Fri, 28 Nov 2025 00:40:53 +1000	[thread overview]
Message-ID: <aShi9fAhQzRzWj9X@fedora> (raw)
In-Reply-To: <aShM9CAyRPe2gYpK@fedora>

Changes in v2:
  - Reworded commit message to follow the imperative form.
  - Expanded the documentation to explain the `Some` and `None` return cases.
  - Added a period at the end of the documentation comment.

Change `page_align()` to return `Option<usize>` to allow validation
of the provided `addr` value. This ensures that any value that is
within one `PAGE_SIZE` of `usize::MAX` will not panic, and instead
returns `None` to indicate overflow.

Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
---
 rust/kernel/page.rs | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 432fc0297d4a..d5cc340c0689 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -26,13 +26,13 @@
 pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
 
 /// Round up the given number to the next multiple of [`PAGE_SIZE`].
-///
-/// It is incorrect to pass an address where the next multiple of [`PAGE_SIZE`] doesn't fit in a
-/// [`usize`].
-pub const fn page_align(addr: usize) -> usize {
-    // Parentheses around `PAGE_SIZE - 1` to avoid triggering overflow sanitizers in the wrong
-    // cases.
-    (addr + (PAGE_SIZE - 1)) & PAGE_MASK
+/// Return Some [`usize`] that is page aligned. Or None in cases where the next multiple of
+/// [`PAGE_SIZE`] would overflow a [`usize`].
+pub const fn page_align(addr: usize) -> Option<usize> {
+    if let Some(sum) = addr.checked_add(PAGE_SIZE - 1) {
+        return Some(sum & PAGE_MASK);
+    }
+    None
 }
 
 /// Representation of a non-owning reference to a [`Page`].
-- 
2.51.1

  parent reply	other threads:[~2025-11-27 14:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27 13:07 [PATCH] rust: Return Option from page_align and ensure no usize overflow Brendan Shephard
2025-11-27 13:42 ` Alexandre Courbot
2025-11-27 14:18   ` Brendan Shephard
2025-11-28  0:28     ` Alexandre Courbot
2025-11-28  2:12       ` Miguel Ojeda
2025-11-28  5:29         ` Brendan Shephard
2025-11-28  6:51           ` Alexandre Courbot
2025-11-28  9:09             ` Alice Ryhl
2025-11-28 13:34               ` Alexandre Courbot
2025-11-27 13:44 ` Daniel Almeida
2025-11-27 14:21   ` Brendan Shephard
2025-11-27 14:40 ` Brendan Shephard [this message]
2025-11-27 16:24   ` [PATCH v2] " Miguel Ojeda
2025-11-28  3:35     ` Brendan Shephard

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=aShi9fAhQzRzWj9X@fedora \
    --to=bshephar@bne-home.net \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).