rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rust: transmute: implement FromBytes and AsBytes for ()
@ 2025-12-08  9:54 Alexandre Courbot
  2025-12-08  9:54 ` [PATCH v2 1/2] " Alexandre Courbot
  2025-12-08  9:54 ` [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message Alexandre Courbot
  0 siblings, 2 replies; 7+ messages in thread
From: Alexandre Courbot @ 2025-12-08  9:54 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel, Alexandre Courbot

This is going to be useful in Nova's GSP message handling, as some
messages are empty and we need to explicitly use an empty structure for
them.

If accepted, I would like to merge it through `drm-rust-next` so Nova
code can start using this feature quickly.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Changes in v2:
- Make use of new feature in Nova.
- Link to v1: https://patch.msgid.link/20251208-transmute_unit-v1-1-680c7386b5d9@nvidia.com

---
Alexandre Courbot (2):
      rust: transmute: implement FromBytes and AsBytes for ()
      gpu: nova-core: gsp: use () as message type for GspInitDone message

 drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
 rust/kernel/transmute.rs              | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
---
base-commit: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251208-transmute_unit-78ab58ba9e6e

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
  2025-12-08  9:54 [PATCH v2 0/2] rust: transmute: implement FromBytes and AsBytes for () Alexandre Courbot
@ 2025-12-08  9:54 ` Alexandre Courbot
  2025-12-08 11:00   ` Alistair Popple
  2025-12-08 13:27   ` Gary Guo
  2025-12-08  9:54 ` [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message Alexandre Courbot
  1 sibling, 2 replies; 7+ messages in thread
From: Alexandre Courbot @ 2025-12-08  9:54 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel, Alexandre Courbot

This is useful when using types that may or may not be empty in generic
code relying on these traits. It is also safe because technically a
no-op.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/transmute.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index be5dbf3829e2..8d78c81e3749 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
 }
 
 impl_frombytes! {
+    // SAFETY: This type is empty and thus does not consume any data.
+    (),
+
     // SAFETY: All bit patterns are acceptable values of the types below.
     u8, u16, u32, u64, usize,
     i8, i16, i32, i64, isize,
@@ -230,6 +233,9 @@ macro_rules! impl_asbytes {
 }
 
 impl_asbytes! {
+    // SAFETY: This type is empty and thus returns an empty slice.
+    (),
+
     // SAFETY: Instances of the following types have no uninitialized portions.
     u8, u16, u32, u64, usize,
     i8, i16, i32, i64, isize,

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message
  2025-12-08  9:54 [PATCH v2 0/2] rust: transmute: implement FromBytes and AsBytes for () Alexandre Courbot
  2025-12-08  9:54 ` [PATCH v2 1/2] " Alexandre Courbot
@ 2025-12-08  9:54 ` Alexandre Courbot
  2025-12-08 10:56   ` Alistair Popple
  1 sibling, 1 reply; 7+ messages in thread
From: Alexandre Courbot @ 2025-12-08  9:54 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich
  Cc: John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel, Alexandre Courbot

`GspInitDone` has no payload whatsoever, so the unit type `()` is the
correct way to represent its message content. We can use it now that
`()` implements `FromBytes`.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs
index 0425c65b5d6f..2050771f9b53 100644
--- a/drivers/gpu/nova-core/gsp/commands.rs
+++ b/drivers/gpu/nova-core/gsp/commands.rs
@@ -142,7 +142,7 @@ fn init_variable_payload(
 }
 
 /// Message type for GSP initialization done notification.
-struct GspInitDone {}
+struct GspInitDone;
 
 // SAFETY: `GspInitDone` is a zero-sized type with no bytes, therefore it
 // trivially has no uninitialized bytes.
@@ -151,13 +151,13 @@ unsafe impl FromBytes for GspInitDone {}
 impl MessageFromGsp for GspInitDone {
     const FUNCTION: MsgFunction = MsgFunction::GspInitDone;
     type InitError = Infallible;
-    type Message = GspInitDone;
+    type Message = ();
 
     fn read(
         _msg: &Self::Message,
         _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>,
     ) -> Result<Self, Self::InitError> {
-        Ok(GspInitDone {})
+        Ok(GspInitDone)
     }
 }
 

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message
  2025-12-08  9:54 ` [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message Alexandre Courbot
@ 2025-12-08 10:56   ` Alistair Popple
  0 siblings, 0 replies; 7+ messages in thread
From: Alistair Popple @ 2025-12-08 10:56 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, John Hubbard, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel

On 2025-12-08 at 20:54 +1100, Alexandre Courbot <acourbot@nvidia.com> wrote...
> `GspInitDone` has no payload whatsoever, so the unit type `()` is the
> correct way to represent its message content. We can use it now that
> `()` implements `FromBytes`.

Much nicer, thanks.

Reviewed-by: Alistair Popple <apopple@nvidia.com>

> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  drivers/gpu/nova-core/gsp/commands.rs | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs
> index 0425c65b5d6f..2050771f9b53 100644
> --- a/drivers/gpu/nova-core/gsp/commands.rs
> +++ b/drivers/gpu/nova-core/gsp/commands.rs
> @@ -142,7 +142,7 @@ fn init_variable_payload(
>  }
>  
>  /// Message type for GSP initialization done notification.
> -struct GspInitDone {}
> +struct GspInitDone;
>  
>  // SAFETY: `GspInitDone` is a zero-sized type with no bytes, therefore it
>  // trivially has no uninitialized bytes.
> @@ -151,13 +151,13 @@ unsafe impl FromBytes for GspInitDone {}
>  impl MessageFromGsp for GspInitDone {
>      const FUNCTION: MsgFunction = MsgFunction::GspInitDone;
>      type InitError = Infallible;
> -    type Message = GspInitDone;
> +    type Message = ();
>  
>      fn read(
>          _msg: &Self::Message,
>          _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>,
>      ) -> Result<Self, Self::InitError> {
> -        Ok(GspInitDone {})
> +        Ok(GspInitDone)
>      }
>  }
>  
> 
> -- 
> 2.52.0
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
  2025-12-08  9:54 ` [PATCH v2 1/2] " Alexandre Courbot
@ 2025-12-08 11:00   ` Alistair Popple
  2025-12-08 13:27   ` Gary Guo
  1 sibling, 0 replies; 7+ messages in thread
From: Alistair Popple @ 2025-12-08 11:00 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, John Hubbard, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel

On 2025-12-08 at 20:54 +1100, Alexandre Courbot <acourbot@nvidia.com> wrote...
> This is useful when using types that may or may not be empty in generic
> code relying on these traits. It is also safe because technically a
> no-op.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/transmute.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..8d78c81e3749 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
>  }
>  
>  impl_frombytes! {
> +    // SAFETY: This type is empty and thus does not consume any data.
> +    (),
> +
>      // SAFETY: All bit patterns are acceptable values of the types below.
>      u8, u16, u32, u64, usize,
>      i8, i16, i32, i64, isize,
> @@ -230,6 +233,9 @@ macro_rules! impl_asbytes {
>  }
>  
>  impl_asbytes! {
> +    // SAFETY: This type is empty and thus returns an empty slice.

I would have thought the safety comment should have referenced that it cannot
contain any uninitialized data. OTOH that's probably obvious so:

Reviewed-by: Alistair Popple <apopple@nvidia.com>

> +    (),
> +
>      // SAFETY: Instances of the following types have no uninitialized portions.
>      u8, u16, u32, u64, usize,
>      i8, i16, i32, i64, isize,
> 
> -- 
> 2.52.0
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
  2025-12-08  9:54 ` [PATCH v2 1/2] " Alexandre Courbot
  2025-12-08 11:00   ` Alistair Popple
@ 2025-12-08 13:27   ` Gary Guo
  2025-12-09  2:39     ` Alexandre Courbot
  1 sibling, 1 reply; 7+ messages in thread
From: Gary Guo @ 2025-12-08 13:27 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel

On Mon, 08 Dec 2025 18:54:40 +0900
Alexandre Courbot <acourbot@nvidia.com> wrote:

> This is useful when using types that may or may not be empty in generic
> code relying on these traits. It is also safe because technically a
> no-op.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/transmute.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
> index be5dbf3829e2..8d78c81e3749 100644
> --- a/rust/kernel/transmute.rs
> +++ b/rust/kernel/transmute.rs
> @@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
>  }
>  
>  impl_frombytes! {
> +    // SAFETY: This type is empty and thus does not consume any data.
> +    (),

I'd avoid use the word "empty" as it has the meaning of uninhabited
types (never type) in type theory. ZSTs that are inhabited are unit
types (or singletons).


Perhaps better to have a justification that applies to all inhabited
ZSTs that don't have special semantics. Something like this?

	// SAFETY: Inhabited ZSTs only have one possible bit pattern.
	(),
	{<T>} PhantomData<T>,

Best,
Gary
	
> +
>      // SAFETY: All bit patterns are acceptable values of the types below.
>      u8, u16, u32, u64, usize,
>      i8, i16, i32, i64, isize,
> @@ -230,6 +233,9 @@ macro_rules! impl_asbytes {
>  }
>  
>  impl_asbytes! {
> +    // SAFETY: This type is empty and thus returns an empty slice.
> +    (),
> +
>      // SAFETY: Instances of the following types have no uninitialized portions.
>      u8, u16, u32, u64, usize,
>      i8, i16, i32, i64, isize,
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] rust: transmute: implement FromBytes and AsBytes for ()
  2025-12-08 13:27   ` Gary Guo
@ 2025-12-09  2:39     ` Alexandre Courbot
  0 siblings, 0 replies; 7+ messages in thread
From: Alexandre Courbot @ 2025-12-09  2:39 UTC (permalink / raw)
  To: Gary Guo, Alexandre Courbot
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	Edwin Peer, rust-for-linux, linux-kernel

On Mon Dec 8, 2025 at 10:27 PM JST, Gary Guo wrote:
> On Mon, 08 Dec 2025 18:54:40 +0900
> Alexandre Courbot <acourbot@nvidia.com> wrote:
>
>> This is useful when using types that may or may not be empty in generic
>> code relying on these traits. It is also safe because technically a
>> no-op.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/transmute.rs | 6 ++++++
>>  1 file changed, 6 insertions(+)
>> 
>> diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
>> index be5dbf3829e2..8d78c81e3749 100644
>> --- a/rust/kernel/transmute.rs
>> +++ b/rust/kernel/transmute.rs
>> @@ -170,6 +170,9 @@ macro_rules! impl_frombytes {
>>  }
>>  
>>  impl_frombytes! {
>> +    // SAFETY: This type is empty and thus does not consume any data.
>> +    (),
>
> I'd avoid use the word "empty" as it has the meaning of uninhabited
> types (never type) in type theory. ZSTs that are inhabited are unit
> types (or singletons).
>
>
> Perhaps better to have a justification that applies to all inhabited
> ZSTs that don't have special semantics. Something like this?
>
> 	// SAFETY: Inhabited ZSTs only have one possible bit pattern.
> 	(),
> 	{<T>} PhantomData<T>,

Sounds good - I'll use your SAFETY statement as-is. Adding `PhantomData`
is also a great idea.

Thanks for the review!

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-12-09  2:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-08  9:54 [PATCH v2 0/2] rust: transmute: implement FromBytes and AsBytes for () Alexandre Courbot
2025-12-08  9:54 ` [PATCH v2 1/2] " Alexandre Courbot
2025-12-08 11:00   ` Alistair Popple
2025-12-08 13:27   ` Gary Guo
2025-12-09  2:39     ` Alexandre Courbot
2025-12-08  9:54 ` [PATCH v2 2/2] gpu: nova-core: gsp: use () as message type for GspInitDone message Alexandre Courbot
2025-12-08 10:56   ` Alistair Popple

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).