[PATCH v5 04/11] rust: uaccess: add UserSliceWriter::write_truncated()
Alistair Popple <[email protected]>
| Newsgroups | dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
Add a helper that writes as much of an AsBytes value as fits in the remaining userspace buffer and returns the number of bytes copied. This avoids requiring callers of versioned UAPIs to convert values to byte slices and truncate them manually. Signed-off-by: Alistair Popple <[email protected]> Suggested-by: Danilo Krummrich <[email protected]> --- rust/kernel/uaccess.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 5f6c4d7a1a51..c63b91942631 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -624,6 +624,20 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result { self.length -= len; Ok(()) } + + /// Writes as much of the provided value as fits in the remaining buffer. + /// + /// Copies `min(size_of::<T>(), self.len())` bytes to userspace. Returns the number of bytes + /// actually written. This is useful for versioned structs where an older userspace may provide + /// a smaller buffer than the current kernel struct. + /// + /// Fails with [`EFAULT`] if the write happens on a bad address. This call may modify the + /// associated userspace slice even if it returns an error. + pub fn write_truncated<T: AsBytes>(&mut self, value: &T) -> Result<usize> { + let len = self.length.min(size_of::<T>()); + self.write_slice(&value.as_bytes()[..len])?; + Ok(len) + } } /// Reads a nul-terminated string into `dst` and returns the length. -- 2.54.0