From: Brendan Shephard <bshephar@bne-home.net>
To: Alexandre Courbot <acourbot@nvidia.com>
Cc: dakr@kernel.org, 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:18:34 +1000 [thread overview]
Message-ID: <aShdutWrmU2q7gUT@fedora> (raw)
In-Reply-To: <DEJIRMFOO880.1V1IA1QDH9WQ0@nvidia.com>
On Thu, Nov 27, 2025 at 10:42:31PM +0900, Alexandre Courbot wrote:
> On Thu Nov 27, 2025 at 10:07 PM JST, Brendan Shephard wrote:
> > Changed page_align() to return Option<usize> which allows for validation
>
> Please use imperative style as per the patch submission guidelines:
> "Change page_align() ...".
>
> https://docs.kernel.org/process/submitting-patches.html
>
Eh, my bad. Yeah, 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().
>
> Callers will return the error that is relevant to them. :) This
> sentence is not needed.
>
Ack, I'll remove that part.
> >
> > 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`].
> > -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`]
>
> You can doclink to [`None`].
>
> Nit: dot at end of sentence missing.
>
Will fix this in v2
> > +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
>
> A more idiomatic way to do this:
>
> addr.checked_add(PAGE_SIZE - 1).map(|sum| sum & PAGE_MASK)
>
>
That would be nice, but alas since this is a const fn, we can't use map here.
I assume that was done for optimisation to ensure this could be referenced
at compile time. So I'm not sure we want to remove that optimisation here.
next prev parent reply other threads:[~2025-11-27 14:18 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 [this message]
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 ` [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=aShdutWrmU2q7gUT@fedora \
--to=bshephar@bne-home.net \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=dakr@kernel.org \
--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).