rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
To: Brendan Shephard <bshephar@bne-home.net>
Cc: aliceryhl@google.com, dakr@kernel.org, acourbot@nvidia.com,
	 daniel.almeida@collabora.com, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v4] rust: Return Option from page_align and ensure no usize overflow
Date: Sun, 30 Nov 2025 13:01:58 +0100	[thread overview]
Message-ID: <CANiq72mLPvB_6Ow3bW5-V4-km=RyA59chQ1g1x9qUt2P-zZweg@mail.gmail.com> (raw)
In-Reply-To: <aSpEWf8ytDn_laHh@fedora>

Hi Brendan,

This looks much better now with the examples!

On Sat, Nov 29, 2025 at 1:54 AM Brendan Shephard <bshephar@bne-home.net> wrote:
>
> +/// Returns a page aligned [`usize`] in cases where the value can be aligned. Otherwise, returns `None`
> +/// if the aligned size will overflow a [`usize`].

[`None`]

> +/// # Examples

Newline before `# Examples` header.

> +/// Assuming a `PAGE_SIZE` of 4096 (0x1000):

Can we assume that? i.e. these tests run as KUnit tests and we support
architectures that allow for other sizes, so we may need to guard
these with a `cfg` or `if` (which may be hidden), or perhaps better,
you could compute the values based on `PAGE_SIZE`, e.g. something
like:

    assert_eq!(page_align(PAGE_SIZE + 1), Some(2 * PAGE_SIZE));

It would be nice if you can confirm the tests fail (and then work
again) running them in a kernel config with a different page size:

    https://docs.kernel.org/rust/testing.html#the-kunit-tests

> +/// // The check asserts that None is returned when a value is requested within one PAGE_SIZE of
> +/// // usize::MAX.

`None`
`PAGE_SIZE`
`usize::MAX`

> +/// let overflow_addr = usize::MAX - (PAGE_SIZE / 2);
> +/// assert_eq!(page_align(overflow_addr), None);

Is there a reason for that particular address? If not, perhaps just
using `usize::MAX` (the maximum value) would be simpler. Or perhaps
the edge case (the first value that returns `None`) would be better.
Or even better, both.

Thanks!

Cheers,
Miguel

  reply	other threads:[~2025-11-30 12:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-29  0:54 [PATCH v4] rust: Return Option from page_align and ensure no usize overflow Brendan Shephard
2025-11-30 12:01 ` Miguel Ojeda [this message]
2025-11-30 22:22   ` 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='CANiq72mLPvB_6Ow3bW5-V4-km=RyA59chQ1g1x9qUt2P-zZweg@mail.gmail.com' \
    --to=miguel.ojeda.sandonis@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bshephar@bne-home.net \
    --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).