From: Brendan Shephard <bshephar@bne-home.net>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: dakr@kernel.org, acourbot@nvidia.com, airlied@gmail.com,
aliceryhl@google.com, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: Return Option from page_align and ensure no usize overflow
Date: Fri, 28 Nov 2025 00:21:02 +1000 [thread overview]
Message-ID: <aSheTh-T1oroAUHR@fedora> (raw)
In-Reply-To: <56446175-58B0-407B-9FC8-97DCE1969673@collabora.com>
On Thu, Nov 27, 2025 at 10:44:16AM -0300, Daniel Almeida wrote:
>
>
> > On 27 Nov 2025, at 10:07, Brendan Shephard <bshephar@bne-home.net> wrote:
> >
> > Changed page_align() to return Option<usize> which allows for validation
>
> nit: imperative voice here
>
Yeah, my bad. I'll fix that in v2.
> > of the provided addr value. This ensures that any value that is provided
> > within one PAGE_SIZE of usize::MAX will not panic and instead page_align
> > will return None.
> >
> > Callers of page_align() should raise a EINVAL when they receive None
> > from page_align().
> >
> > Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
> > ---
> > rust/kernel/page.rs | 13 ++++++-------
> > 1 file changed, 6 insertions(+), 7 deletions(-)
> >
> > diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> > index 432fc0297d4a..b78473b67003 100644
> > --- a/rust/kernel/page.rs
> > +++ b/rust/kernel/page.rs
> > @@ -26,13 +26,12 @@
> > 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`].
>
> Can you document the return value? I’d find it weird that this returns Option otherwise.
>
Sounds like a good plan. I'll document both return Options in v2. None
if the resulting value would overflow a usize::MAX, or Some usize that
is page aligned.
> > -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 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
> >
>
> With the change above:
>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
>
next prev parent reply other threads:[~2025-11-27 14:21 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 [this message]
2025-11-27 14:40 ` [PATCH v2] " Brendan Shephard
2025-11-27 16:24 ` 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=aSheTh-T1oroAUHR@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).