mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "John Hubbard" <jhubbard@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>
Cc: "Joel Fernandes" <joelagnelf@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Shashank Sharma" <shashanks@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, "David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"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>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 2/3] rust: ptr: add Alignment::from_u64() for DeviceSize constants
Date: Tue, 24 Mar 2026 14:19:26 +0000	[thread overview]
Message-ID: <DHB2TMCY99BO.1PTMCOYPCY634@garyguo.net> (raw)
In-Reply-To: <20260312031507.216709-3-jhubbard@nvidia.com>

On Thu Mar 12, 2026 at 3:15 AM GMT, John Hubbard wrote:
> Alignment::new() takes a const usize, which means callers that work
> with DeviceSize constants still need to import the usize SZ_*
> variants. Add from_u64() so callers can write
> Alignment::from_u64(u64::SZ_128K) and stay entirely in the
> DeviceSize world.
>
> Both asserts evaluate at compile time in const context, so there is
> no runtime cost.
>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
> ---
>  rust/kernel/ptr.rs | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
>
> diff --git a/rust/kernel/ptr.rs b/rust/kernel/ptr.rs
> index 5b6a382637fe..b06f6b404a46 100644
> --- a/rust/kernel/ptr.rs
> +++ b/rust/kernel/ptr.rs
> @@ -76,6 +76,41 @@ pub const fn new_checked(align: usize) -> Option<Self> {
>          }
>      }
>  
> +    /// Creates an [`Alignment`] from a [`u64`] value.
> +    ///
> +    /// This is useful when the alignment comes from a [`DeviceSize`] constant
> +    /// rather than a [`usize`] literal.
> +    ///
> +    /// A build error is triggered if `align` is not a power of two, or if it
> +    /// exceeds [`usize::MAX`].
> +    ///
> +    /// [`DeviceSize`]: crate::sizes::DeviceSize
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::ptr::Alignment;
> +    /// use kernel::sizes::DeviceSize;
> +    ///
> +    /// let v = Alignment::from_u64(u64::SZ_128K);
> +    /// assert_eq!(v.as_usize(), 0x0002_0000);
> +    /// ```
> +    #[inline(always)]
> +    pub const fn from_u64(align: u64) -> Self {
> +        assert!(
> +            align.is_power_of_two(),
> +            "Provided alignment is not a power of two."
> +        );

This should be build_assert! to match the doc.

Best,
Gary

> +        assert!(
> +            align <= usize::MAX as u64,
> +            "Provided alignment exceeds usize::MAX."
> +        );
> +
> +        // INVARIANT: `align` is a power of two.
> +        // SAFETY: `align` is a power of two, fits in usize, and thus non-zero.
> +        Self(unsafe { NonZero::new_unchecked(align as usize) })
> +    }
> +
>      /// Returns the alignment of `T`.
>      ///
>      /// This is equivalent to [`align_of`], but with the return value provided as an [`Alignment`].


  parent reply	other threads:[~2026-03-24 14:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-12  3:15 [PATCH v2 0/3] rust, nova-core: add DeviceSize trait for SZ_* constants John Hubbard
2026-03-12  3:15 ` [PATCH v2 1/3] rust: sizes: add DeviceSize trait for device address space constants John Hubbard
2026-03-24 14:32   ` Alexandre Courbot
2026-03-12  3:15 ` [PATCH v2 2/3] rust: ptr: add Alignment::from_u64() for DeviceSize constants John Hubbard
2026-03-12  6:16   ` Miguel Ojeda
2026-03-12  6:23     ` John Hubbard
2026-03-24 14:19   ` Gary Guo [this message]
2026-03-12  3:15 ` [PATCH v2 3/3] gpu: nova-core: use DeviceSize trait for u64 size constants John Hubbard
2026-03-24 14:06   ` Alexandre Courbot

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=DHB2TMCY99BO.1PTMCOYPCY634@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=ecourtney@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=shashanks@nvidia.com \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=zhiw@nvidia.com \
    /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

all inboxes | Powered by JetHome®