mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Laine Taffin Altman <alexanderaltman@me.com>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: "Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	stable@vger.kernel.org, rust-for-linux@vger.kernel.org,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] rust: init: remove impl Zeroable for Infallible
Date: Mon, 18 Mar 2024 20:17:07 -0700	[thread overview]
Message-ID: <93FD9491-7E2D-4324-8443-0884B7CFC6EF@me.com> (raw)
In-Reply-To: <Zfh5DYkxNAm-mY_9@boqun-archlinux>

On Mar 18, 2024, at 10:25 AM, Boqun Feng <boqun.feng@gmail.com> wrote:
> On Wed, Mar 13, 2024 at 11:09:37PM +0000, Benno Lossin wrote:
>> From: Laine Taffin Altman <alexanderaltman@me.com>
>> 
>> It is not enough for a type to be a ZST to guarantee that zeroed memory
>> is a valid value for it; it must also be inhabited. Creating a value of
>> an uninhabited type, ZST or no, is immediate UB.
>> Thus remove the implementation of `Zeroable` for `Infallible`, since
>> that type is not inhabited.
>> 
>> Cc: stable@vger.kernel.org
>> Fixes: 38cde0bd7b67 ("rust: init: add `Zeroable` trait and `init::zeroed` function")
>> Closes: https://github.com/Rust-for-Linux/pinned-init/pull/13
>> Signed-off-by: Laine Taffin Altman <alexanderaltman@me.com>
>> Signed-off-by: Benno Lossin <benno.lossin@proton.me>
> 
> I think either in the commit log or in the code comment, there better be
> a link or explanation on "(un)inhabited type". The rest looks good to
> me.

Would the following be okay for that purpose?

A type is inhabited if at least one valid value of that type exists; a
type is uninhabited if no valid values of that type exist.  The terms
"inhabited" and "uninhabited" in this sense originate in type theory,
a branch of mathematics.

In Rust, producing an invalid value of any type is immediate undefined
behavior (UB); this includes via zeroing memory.  Therefore, since an
uninhabited type has no valid values, producing any values at all for
it is UB.

The Rust standard library type `core::convert::Infallible` is
uninhabited, by virtue of having been declared as an enum with no
cases, which always produces uninhabited types in Rust.  Thus, remove
the implementation of `Zeroable` for `Infallible`, thereby avoiding
the UB.

Thanks,
Laine

> 
> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
> 
> Regards,
> Boqun
> 
>> ---
>> rust/kernel/init.rs | 4 ++--
>> 1 file changed, 2 insertions(+), 2 deletions(-)
>> 
>> diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
>> index 424257284d16..538e03cfc84a 100644
>> --- a/rust/kernel/init.rs
>> +++ b/rust/kernel/init.rs
>> @@ -1292,8 +1292,8 @@ macro_rules! impl_zeroable {
>>     i8, i16, i32, i64, i128, isize,
>>     f32, f64,
>> 
>> -    // SAFETY: These are ZSTs, there is nothing to zero.
>> -    {<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, Infallible, (),
>> +    // SAFETY: These are inhabited ZSTs, there is nothing to zero and a valid value exists.
>> +    {<T: ?Sized>} PhantomData<T>, core::marker::PhantomPinned, (),
>> 
>>     // SAFETY: Type is allowed to take any value, including all zeros.
>>     {<T>} MaybeUninit<T>,
>> 
>> base-commit: 768409cff6cc89fe1194da880537a09857b6e4db
>> -- 
>> 2.42.0
>> 
>> 
>> 


  reply	other threads:[~2024-03-19  3:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 23:09 Benno Lossin
2024-03-14  9:14 ` Alice Ryhl
2024-03-18 17:25 ` Boqun Feng
2024-03-19  3:17   ` Laine Taffin Altman [this message]
2024-03-19  4:39     ` Boqun Feng
2024-03-19  5:28       ` Laine Taffin Altman
2024-03-19 10:34         ` Benno Lossin
2024-03-19 11:24           ` Miguel Ojeda
2024-03-21  4:53           ` Laine Taffin Altman
2024-03-21  9:07             ` Miguel Ojeda
2024-03-30 12:03             ` Benno Lossin
2024-03-30 16:36               ` Laine Taffin Altman
2024-03-30 16:43                 ` Benno Lossin

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=93FD9491-7E2D-4324-8443-0884B7CFC6EF@me.com \
    --to=alexanderaltman@me.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    --cc=yakoyoku@gmail.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®