mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/2] drm/tyr: safely write GpuInfo to userspace
@ 2026-09-26  0:26 Deborah Brouwer
  2026-09-26  0:26 ` [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive Deborah Brouwer
  2026-09-26  0:26 ` [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
  0 siblings, 2 replies; 7+ messages in thread
From: Deborah Brouwer @ 2026-09-26  0:26 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer

This series makes zerocopy and zerocopy_derive available to the UAPI crate
so that zerocopy derive attributes can be added to generated UAPI
bindings. It derives the zerocopy traits specifically for struct
drm_panthor_gpu_info, allowing Tyr to safely copy this struct byte-for-
byte into userspace memory.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
Changes in v3:
- split the commit into two to keep Tyr changes separate
- Link to v2: https://lore.kernel.org/r/20260924-b4-gpu_info_intobytes-v2-1-1b05e33df8a4@collabora.com

Changes in v2:
- use zerocopy_derive::most_traits
- Link to v1: https://lore.kernel.org/r/20260923-b4-gpu_info_intobytes-v1-1-bb2173f91e11@collabora.com

---
Deborah Brouwer (2):
      rust: uapi: add zerocopy/zerocopy_derive
      drm/tyr: safely write GpuInfo to userspace

 drivers/gpu/drm/tyr/file.rs |  2 +-
 drivers/gpu/drm/tyr/gpu.rs  | 12 ------------
 rust/Makefile               | 12 +++++++++---
 rust/bindgen_parameters     |  3 +++
 4 files changed, 13 insertions(+), 16 deletions(-)
---
base-commit: 896ed083362758b33c49a1b5e5a3423c5814d87e
change-id: 20260923-b4-gpu_info_intobytes-c2d45f2935c8

Best regards,
-- 
Deborah Brouwer <deborah.brouwer@collabora.com>


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

* [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive
  2026-09-26  0:26 [PATCH v3 0/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
@ 2026-09-26  0:26 ` Deborah Brouwer
  2026-09-26 15:50   ` Gary Guo
  2026-09-26  0:26 ` [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
  1 sibling, 1 reply; 7+ messages in thread
From: Deborah Brouwer @ 2026-09-26  0:26 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer

Make the zerocopy and zerocopy_derive crates available for use when
building the UAPI crate and its tests. Then attempt to derive most of
the zerocopy traits for struct drm_panthor_gpu_info when generating its
binding. This will be used by the Tyr driver that needs the traits
IntoBytes and Immutable to safely copy the struct byte-for-byte to
userspace.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 rust/Makefile           | 12 +++++++++---
 rust/bindgen_parameters |  3 +++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index da1a7409d984..bf02a1efd6f3 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -365,8 +365,11 @@ rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_ini
 rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
 	+$(call if_changed,rustc_test_library)
 
-rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
-rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
+rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init \
+	--extern zerocopy=$(objtree)/$(obj)/test/libzerocopy.rlib \
+	--extern zerocopy_derive=$(objtree)/$(obj)/test/$(libzerocopy_derive_name)
+rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init \
+	rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
 	+$(call if_changed,rustc_test_library)
 
 quiet_cmd_rustdoc_test = RUSTDOC T $<
@@ -782,11 +785,14 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \
     $(obj)/bindings/bindings_helpers_generated.rs FORCE
 	+$(call if_changed_rule,rustc_library)
 
-$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
+$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init \
+	--extern zerocopy --extern zerocopy_derive
 $(obj)/uapi.o: private skip_gendwarfksyms = 1
 $(obj)/uapi.o: $(src)/uapi/lib.rs \
     $(obj)/ffi.o \
     $(obj)/pin_init.o \
+    $(obj)/zerocopy.o \
+    $(obj)/$(libzerocopy_derive_name) \
     $(obj)/uapi/uapi_generated.rs FORCE
 	+$(call if_changed_rule,rustc_library)
 
diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
index 8402b0c93545..332bc8d6a9f7 100644
--- a/rust/bindgen_parameters
+++ b/rust/bindgen_parameters
@@ -71,3 +71,6 @@
 # Structs should implement `Zeroable` when all of their fields do.
 --with-derive-custom-struct .*=MaybeZeroable
 --with-derive-custom-union .*=MaybeZeroable
+
+# `drm_panthor_gpu_info` is copied byte-for-byte to userspace.
+--with-derive-custom-struct '^drm_panthor_gpu_info$'=zerocopy_derive::most_traits

-- 
2.55.0


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

* [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace
  2026-09-26  0:26 [PATCH v3 0/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
  2026-09-26  0:26 ` [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive Deborah Brouwer
@ 2026-09-26  0:26 ` Deborah Brouwer
  2026-09-26  2:44   ` Daniel Almeida
  2026-09-26 16:48   ` Danilo Krummrich
  1 sibling, 2 replies; 7+ messages in thread
From: Deborah Brouwer @ 2026-09-26  0:26 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer

Tyr creates the struct drm_panthor_gpu_info from the Panthor UAPI
bindings. It initializes this struct by querying the gpu and then writes
the struct into userspace memory faithfully byte-by-byte.

Currently, the struct drm_panthor_gpu_info does not have any implicit
padding, but if implicit padding were added in the future Rust does not
guarantee that it would be initialized. Then when Tyr writes the struct
back to userspace it could expose uninitialized kernel memory.

Tyr implements the unsafe trait AsBytes for GpuInfo whereby the developer
guarantees that struct drm_panthor_gpu_info does not include implicit
padding, pointers, or interior mutability. This allows Tyr to safely copy
the struct into userspace memory. However, relying on the unsafe trait
AsBytes is fragile because if the Panthor UAPI changes, the SAFETY
guarantees may no longer be accurate.

Instead, use the derive macro `zerocopy_derive::most_traits` for struct
drm_panthor_gpu_info. This macro will attempt to implement the zerocopy
traits for struct drm_panthor_gpu_info. If the Panthor UAPI changes so
that the traits IntoBytes or Immutable can no longer be implemented, Tyr
will no longer compile.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 drivers/gpu/drm/tyr/file.rs |  2 +-
 drivers/gpu/drm/tyr/gpu.rs  | 12 ------------
 2 files changed, 1 insertion(+), 13 deletions(-)

diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs
index 9f60a90d4948..ceadde976502 100644
--- a/drivers/gpu/drm/tyr/file.rs
+++ b/drivers/gpu/drm/tyr/file.rs
@@ -54,7 +54,7 @@ pub(crate) fn dev_query(
                     )
                     .writer();
 
-                    writer.write(&reg_data.gpu_info)?;
+                    writer.write_slice(reg_data.gpu_info.as_bytes())?;
 
                     Ok(0)
                 }
diff --git a/drivers/gpu/drm/tyr/gpu.rs b/drivers/gpu/drm/tyr/gpu.rs
index e1b04a0c5159..fde5bd0f87a1 100644
--- a/drivers/gpu/drm/tyr/gpu.rs
+++ b/drivers/gpu/drm/tyr/gpu.rs
@@ -16,7 +16,6 @@
     },
     prelude::*,
     time::Delta,
-    transmute::AsBytes,
     uapi, //
 };
 
@@ -134,17 +133,6 @@ fn deref_mut(&mut self) -> &mut Self::Target {
     }
 }
 
-// SAFETY: `GpuInfo`'s invariant guarantees that it is the same type that is
-// already exposed to userspace by the C driver. This implies that it fulfills
-// the requirements for `AsBytes`.
-//
-// This means:
-//
-// - No implicit padding,
-// - No kernel pointers,
-// - No interior mutability.
-unsafe impl AsBytes for GpuInfo {}
-
 struct GpuModels {
     name: &'static str,
     arch_major: u32,

-- 
2.55.0


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

* Re: [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace
  2026-09-26  0:26 ` [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
@ 2026-09-26  2:44   ` Daniel Almeida
  2026-09-26 16:48   ` Danilo Krummrich
  1 sibling, 0 replies; 7+ messages in thread
From: Daniel Almeida @ 2026-09-26  2:44 UTC (permalink / raw)
  To: Deborah Brouwer
  Cc: Danilo Krummrich, Alice Ryhl, David Airlie, Simona Vetter,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, dri-devel, rust-for-linux,
	linux-kernel

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>

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

* Re: [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive
  2026-09-26  0:26 ` [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive Deborah Brouwer
@ 2026-09-26 15:50   ` Gary Guo
  2026-09-26 16:26     ` Danilo Krummrich
  0 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2026-09-26 15:50 UTC (permalink / raw)
  To: Deborah Brouwer, Danilo Krummrich, Alice Ryhl, Daniel Almeida,
	David Airlie, Simona Vetter, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: dri-devel, rust-for-linux, linux-kernel

On Sat Sep 26, 2026 at 1:26 AM BST, Deborah Brouwer wrote:
> Make the zerocopy and zerocopy_derive crates available for use when
> building the UAPI crate and its tests. Then attempt to derive most of
> the zerocopy traits for struct drm_panthor_gpu_info when generating its
> binding. This will be used by the Tyr driver that needs the traits
> IntoBytes and Immutable to safely copy the struct byte-for-byte to
> userspace.
>
> Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
> ---
>  rust/Makefile           | 12 +++++++++---
>  rust/bindgen_parameters |  3 +++
>  2 files changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/rust/Makefile b/rust/Makefile
> index da1a7409d984..bf02a1efd6f3 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -365,8 +365,11 @@ rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_ini
>  rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
>  	+$(call if_changed,rustc_test_library)
>  
> -rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
> -rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
> +rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init \
> +	--extern zerocopy=$(objtree)/$(obj)/test/libzerocopy.rlib \
> +	--extern zerocopy_derive=$(objtree)/$(obj)/test/$(libzerocopy_derive_name)
> +rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init \
> +	rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
>  	+$(call if_changed,rustc_test_library)
>  
>  quiet_cmd_rustdoc_test = RUSTDOC T $<
> @@ -782,11 +785,14 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \
>      $(obj)/bindings/bindings_helpers_generated.rs FORCE
>  	+$(call if_changed_rule,rustc_library)
>  
> -$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
> +$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init \
> +	--extern zerocopy --extern zerocopy_derive
>  $(obj)/uapi.o: private skip_gendwarfksyms = 1
>  $(obj)/uapi.o: $(src)/uapi/lib.rs \
>      $(obj)/ffi.o \
>      $(obj)/pin_init.o \
> +    $(obj)/zerocopy.o \
> +    $(obj)/$(libzerocopy_derive_name) \

Please add these to `bindings` crate as well to avoid have it diverging with
`uapi`.

>      $(obj)/uapi/uapi_generated.rs FORCE
>  	+$(call if_changed_rule,rustc_library)
>  
> diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
> index 8402b0c93545..332bc8d6a9f7 100644
> --- a/rust/bindgen_parameters
> +++ b/rust/bindgen_parameters
> @@ -71,3 +71,6 @@
>  # Structs should implement `Zeroable` when all of their fields do.
>  --with-derive-custom-struct .*=MaybeZeroable
>  --with-derive-custom-union .*=MaybeZeroable
> +
> +# `drm_panthor_gpu_info` is copied byte-for-byte to userspace.
> +--with-derive-custom-struct '^drm_panthor_gpu_info$'=zerocopy_derive::most_traits

This is Tyr specific change and should be in patch 2.

Best,
Gary



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

* Re: [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive
  2026-09-26 15:50   ` Gary Guo
@ 2026-09-26 16:26     ` Danilo Krummrich
  0 siblings, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2026-09-26 16:26 UTC (permalink / raw)
  To: Gary Guo
  Cc: Deborah Brouwer, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter, Miguel Ojeda, Boqun Feng, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, dri-devel, rust-for-linux,
	linux-kernel

On Sat Sep 26, 2026 at 5:50 PM CEST, Gary Guo wrote:
> On Sat Sep 26, 2026 at 1:26 AM BST, Deborah Brouwer wrote:
>> @@ -71,3 +71,6 @@
>>  # Structs should implement `Zeroable` when all of their fields do.
>>  --with-derive-custom-struct .*=MaybeZeroable
>>  --with-derive-custom-union .*=MaybeZeroable
>> +
>> +# `drm_panthor_gpu_info` is copied byte-for-byte to userspace.
>> +--with-derive-custom-struct '^drm_panthor_gpu_info$'=zerocopy_derive::most_traits
>
> This is Tyr specific change and should be in patch 2.

Why list all of them separately? Can't we apply this to all uAPI structs?

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

* Re: [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace
  2026-09-26  0:26 ` [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
  2026-09-26  2:44   ` Daniel Almeida
@ 2026-09-26 16:48   ` Danilo Krummrich
  1 sibling, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2026-09-26 16:48 UTC (permalink / raw)
  To: Deborah Brouwer
  Cc: Alice Ryhl, Daniel Almeida, David Airlie, Simona Vetter,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, dri-devel, rust-for-linux,
	linux-kernel

On Sat Sep 26, 2026 at 2:26 AM CEST, Deborah Brouwer wrote:
> @@ -54,7 +54,7 @@ pub(crate) fn dev_query(
>                      )
>                      .writer();
>  
> -                    writer.write(&reg_data.gpu_info)?;
> +                    writer.write_slice(reg_data.gpu_info.as_bytes())?;

I think we eventually want write() to require T: IntoBytes + Immutable, so I'm
not sure there is much value going back and forth with this.

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

end of thread, other threads:[~2026-09-26 16:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-26  0:26 [PATCH v3 0/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
2026-09-26  0:26 ` [PATCH v3 1/2] rust: uapi: add zerocopy/zerocopy_derive Deborah Brouwer
2026-09-26 15:50   ` Gary Guo
2026-09-26 16:26     ` Danilo Krummrich
2026-09-26  0:26 ` [PATCH v3 2/2] drm/tyr: safely write GpuInfo to userspace Deborah Brouwer
2026-09-26  2:44   ` Daniel Almeida
2026-09-26 16:48   ` Danilo Krummrich

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®