mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "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>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, <linux-kernel@vger.kernel.org>,
	<rust-for-linux@vger.kernel.org>, <nova-gpu@lists.linux.dev>,
	<dri-devel@lists.freedesktop.org>
Subject: Re: [PATCH 1/2] rust: add functions and traits for lossless integer conversions
Date: Mon, 27 Jul 2026 11:28:04 +0100	[thread overview]
Message-ID: <DK9A6KGK5JQD.3U23MBEMSAXBK@garyguo.net> (raw)
In-Reply-To: <20260727-as_casts-v1-1-6ea704ff25d8@nvidia.com>

On Mon Jul 27, 2026 at 11:15 AM BST, Alexandre Courbot wrote:
> The core library's `From` implementations do not cover conversions that
> are not portable or future-proof. For instance, even though it is safe
> today, `From<usize>` is not implemented for `u64` because of the
> possibility of supporting larger-than-64bit architectures in the future.
>
> However, the kernel supports a narrower set of architectures, with a
> considerable amount of code that is architecture-specific. This makes it
> helpful and desirable to provide more infallible conversions, lest we
> need to rely on the `as` keyword and carry the risk of silently losing
> data.
>
> Thus, introduce a new module `num::casts` that provides safe const
> functions performing more conversions allowed by the build target, as
> well as `FromSafeCast` and `IntoSafeCast` traits that are just
> extensions of `From` and `Into` to conversions that are known to be
> lossless.

I'm okay with having u32 -> usize casts and usize -> u64 casts, but having
usize -> u32 and u64 -> usize casts like this series do is worrying as it can be
misused from drivers that are supposed to be supporting multiple platforms or
core subsystems.

Pointer-size dependent drivers that need these casts should define them locally.

Best,
Gary

>
> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> Link: https://lore.kernel.org/rust-for-linux/DDK4KADWJHMG.1FUPL3SDR26XF@kernel.org/
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/num.rs       |   3 +
>  rust/kernel/num/casts.rs | 248 +++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 251 insertions(+)

  reply	other threads:[~2026-07-27 10:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-27 10:15 [PATCH 0/2] " Alexandre Courbot
2026-07-27 10:15 ` [PATCH 1/2] " Alexandre Courbot
2026-07-27 10:28   ` Gary Guo [this message]
2026-07-27 11:10     ` Miguel Ojeda
2026-07-27 11:19     ` Danilo Krummrich
2026-07-27 10:15 ` [PATCH 2/2] gpu: nova-core: use kernel lossless integer conversion module 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=DK9A6KGK5JQD.3U23MBEMSAXBK@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ecourtney@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.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®