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 <[email protected]> --- 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(®_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
