drm/tyr: allow mismatched buffers for dev_query
Subsystems (4)
v2 cover drm/tyr: allow mismatched buffers for dev_query
Panthor UAPI for DRM_IOCTL_TYR_DEV_QUERY allows userspace to provide a smaller or larger buffer than the kernel needs. Revise Tyr's implementation to match the expected UAPI behaviour.
---
Changes in v2:
- Added #[inline] to UserSliceWriter::clear()
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Deborah Brouwer (2):
rust: uaccess: add UserSliceWriter::clear() drm/tyr: allow userspace buffers to be mismatched
drivers/gpu/drm/tyr/file.rs | 45 +++++++++++++++++++++++++++++++++++++-------- rust/helpers/uaccess.c | 6 ++++++ rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 8 deletions(-)
---
base-commit: 5a1b7121a6c4db6c0239d3ac3fd1c2bb33a40025
change-id: 20261014-b4-dev_query_fixes-4e0ab024294a
Best regards,
Signature · 1 line
Deborah Brouwer <[email protected]>
PATCH v2 1/2 rust: uaccess: add UserSliceWriter::clear()
Add a method to zero the remaining bytes in a UserSliceWriter.
If userspace provides a buffer that is larger than necessary, provide a method to zero out the remaining UserSliceWriter buffer. This avoids userspace consuming potentially uninitialized memory and/or mistaking it for data intentionally provided by the kernel.
Use the #[inline] annotation to make this clear() method available for inlining outside of the kernel crate.
Since clear_user() is architecturally defined and may be a macro or inline function, add a rust helper to make clear_user() callable from rust.
--- rust/helpers/uaccess.c | 6 ++++++ rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
rust/helpers/uaccess.c +6 -0
@@ -27,3 +27,9 @@ unsigned long rust_helper__copy_to_user(void __user *to, const void *from, unsig return _inline_copy_to_user(to, from, n); }#endif + +__rust_helper unsigned long +rust_helper_clear_user (void __user *to,unsigned long n) +{ +return clear_user (to, n); +}
rust/kernel/uaccess.rs +24 -0
@@ -638,6 +638,30 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {pub fn write_truncated <T :AsBytes >(&mut self, value: &T ) ->Result <usize > { self.write_slice_partial (value.as_bytes (),0 ) } + +/// Zeros remaining bytes in the userspace buffer. +/// +/// Fails with [`EFAULT`] if the userspace memory cannot be cleared. +#[inline] +pub fn clear (&mut self) ->Result { +let len = self.length; + +if len ==0 { +return Ok (()); + } +// SAFETY: `clear_user` checks the userspace address and range and handles +// faults when accessing userspace memory +let res =unsafe { bindings::clear_user (self.ptr.as_mut_ptr (), len) }; + +if res !=0 { +return Err (EFAULT ); + } + + self.ptr = self.ptr.wrapping_byte_add (len); + self.length =0 ; + +Ok (()) + } }/// Reads a nul-terminated string into `dst` and returns the length.
Signature · 1 line
2.56.0
Re: [PATCH v2 1/2] rust: uaccess: add UserSliceWriter::clear()
Quoted 8 lines · Deborah Brouwer
Add a method to zero the remaining bytes in a UserSliceWriter.
If userspace provides a buffer that is larger than necessary, provide a method to zero out the remaining UserSliceWriter buffer. This avoids userspace consuming potentially uninitialized memory and/or mistaking it for data intentionally provided by the kernel.
Quoted 7 lines
Use the #[inline] annotation to make this
clear()method available for inlining outside of the kernel crate.Since
clear_user()is architecturally defined and may be a macro or inline function, add a rust helper to makeclear_user()callable from rust.Signed-off-by Deborah Brouwer <[email protected]>
PATCH v2 2/2 drm/tyr: allow userspace buffers to be mismatched
In reply to Deborah BrouwerAllow userspace to provide a buffer that is smaller or larger than the object that the kernel expects to write into it.
Set a minimum buffer size to match those in PANTHOR_UOBJ_MIN_SIZE. If userspace provides at least the minimum buffer size, then simply write until the buffer is full.
If userspace provides a larger buffer than necessary, then zero out the remaining buffer.
This ensures UAPI compatibility with Panthor.
--- drivers/gpu/drm/tyr/file.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-)
drivers/gpu/drm/tyr/file.rs +37 -8
@@ -1,11 +1,17 @@// SPDX-License-Identifier: GPL-2.0 or MIT +use core::mem::{ + offset_of, + size_of_val,// +}; +use kernel::{ drm::{ self,Registered ,// }, prelude::*, + transmute::AsBytes , uaccess::UserSlice , uapi,// };
@@ -16,6 +22,31 @@ TyrDrmRegistrationData, // }; +trait UobjMinSize { +fn min_size (&self) ->usize ; +} + +impl UobjMinSize for uapi::drm_panthor_gpu_info { +fn min_size (&self) ->usize { +offset_of! (Self , tiler_present) +size_of_val (&self.tiler_present) + } +} + +// If the user buffer is at least the minimum required size, then +// copy as much of the kernel object as will fit into the user buffer. +// Don't fail if the user buffer is smaller than the kernel object. +// If the user buffer is bigger than needed, zero out the remainder. +fn set_uobj <T :AsBytes >(usr_dst:u64 , size:u32 , min_size:usize , src_obj: &T ) ->Result { +if (sizeas usize ) < min_size { +return Err (EINVAL ); + } + +let mut writer =UserSlice ::new (UserPtr ::from_addr (usr_dstas usize ), sizeas usize ).writer (); + writer.write_truncated (src_obj)?; + writer.clear ()?; +Ok (()) +} +#[pin_data] pub (crate )struct TyrDrmFileData {}
@@ -48,14 +79,12 @@ pub(crate) fn dev_query( }else {match devquery.type_ { uapi::drm_panthor_dev_query_type_DRM_PANTHOR_DEV_QUERY_GPU_INFO => { -let mut writer =UserSlice ::new ( -UserPtr ::from_addr (devquery.pointeras usize ), - devquery.sizeas usize , - ) - .writer (); - - writer.write (®_data.gpu_info)?; - +set_uobj ( + devquery.pointer, + devquery.size, + reg_data.gpu_info.min_size (), + ®_data.gpu_info, + )?;Ok (0 ) } _ =>Err (EINVAL ),
Signature · 1 line
2.56.0
Alice Ryhl
maintainerRe: [PATCH v2 2/2] drm/tyr: allow userspace buffers to be mismatched
drivers/gpu/drm/tyr/file.rs line 41
Quoted 7 lines · Deborah Brouwer
drivers/gpu/drm/tyr/file.rs +7
-0+// If the user buffer is at least the minimum required size, then +// copy as much of the kernel object as will fit into the user buffer. +// Don't fail if the user buffer is smaller than the kernel object. +// If the user buffer is bigger than needed, zero out the remainder. +fn set_uobj <T :AsBytes >(usr_dst:u64 , size:u32 , min_size:usize , src_obj: &T ) ->Result { +if (sizeas usize ) < min_size { +return Err (EINVAL );
Use kernel::num::casts::u32_as_usize here.
drivers/gpu/drm/tyr/file.rs line 44
Quoted 3 lines
drivers/gpu/drm/tyr/file.rs +3
-0+ } + +let mut writer =UserSlice ::new (UserPtr ::from_addr (usr_dstas usize ), sizeas usize ).writer ();
Avoid casting to usize twice.
let size = kernel::num::casts::u32_as_usize(size);
Alice
Alexandre Courbot
reviewerRe: [PATCH v2 2/2] drm/tyr: allow userspace buffers to be mismatched
Quoted 11 lines · Alice Ryhl
drivers/gpu/drm/tyr/file.rs line 41
Quoted 7 lines · Deborah Brouwer
drivers/gpu/drm/tyr/file.rs +7
-0+// If the user buffer is at least the minimum required size, then +// copy as much of the kernel object as will fit into the user buffer. +// Don't fail if the user buffer is smaller than the kernel object. +// If the user buffer is bigger than needed, zero out the remainder. +fn set_uobj <T :AsBytes >(usr_dst:u64 , size:u32 , min_size:usize , src_obj: &T ) ->Result { +if (sizeas usize ) < min_size { +return Err (EINVAL );Use
kernel::num::casts::u32_as_usizehere.
Quoted 7 lines
drivers/gpu/drm/tyr/file.rs line 44
Quoted 3 lines
drivers/gpu/drm/tyr/file.rs +3
-0+ } + +let mut writer =UserSlice ::new (UserPtr ::from_addr (usr_dstas usize ), sizeas usize ).writer ();Avoid casting to usize twice.
let size = kernel::num::casts::u32_as_usize(size);
Or usize::from_safe_cast(size) after importing FromSafeCast - u32_as_usize is useful in const context, but for regular conversions the FromSafeCast trait looks slightly better.
Re: [PATCH v2 2/2] drm/tyr: allow userspace buffers to be mismatched
Quoted 16 lines · Alexandre Courbot
Quoted 15 lines · Alice Ryhl
drivers/gpu/drm/tyr/file.rs line 41
Quoted 7 lines · Deborah Brouwer
drivers/gpu/drm/tyr/file.rs +7
-0+// If the user buffer is at least the minimum required size, then +// copy as much of the kernel object as will fit into the user buffer. +// Don't fail if the user buffer is smaller than the kernel object. +// If the user buffer is bigger than needed, zero out the remainder. +fn set_uobj <T :AsBytes >(usr_dst:u64 , size:u32 , min_size:usize , src_obj: &T ) ->Result { +if (sizeas usize ) < min_size { +return Err (EINVAL );Use
kernel::num::casts::u32_as_usizehere.Quoted 3 lines
drivers/gpu/drm/tyr/file.rs +3
-0+ } + +let mut writer =UserSlice ::new (UserPtr ::from_addr (usr_dstas usize ), sizeas usize ).writer ();
Quoted 7 lines
Quoted 3 lines
Avoid casting to usize twice.
let size = kernel::num::casts::u32_as_usize(size);Or
usize::from_safe_cast(size)after importingFromSafeCast-u32_as_usizeis useful in const context, but for regular conversions theFromSafeCasttrait looks slightly better.
Yes, since we aren't using this in a const context I will switch over to usize::from_safe_cast(size) in the next version, thanks.