Skip to content

Commit 7d2800b

Browse files
committed
Add UserSlicePtrWriter::clear.
Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 3b8575d commit 7d2800b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

rust/kernel/user_ptr.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! C header: [`include/linux/uaccess.h`](../../../../include/linux/uaccess.h)
66
7-
use crate::{c_types, error::Error, KernelResult};
7+
use crate::{bindings, c_types, error::Error, KernelResult};
88
use alloc::vec::Vec;
99
use core::mem::{size_of, MaybeUninit};
1010

@@ -242,6 +242,27 @@ impl UserSlicePtrWriter {
242242
self.len() == 0
243243
}
244244

245+
/// Writes zeroes to the user slice.
246+
pub fn clear(&mut self, mut len: usize) -> KernelResult {
247+
let mut ret = Ok(());
248+
if len > self.1 {
249+
ret = Err(Error::EFAULT);
250+
len = self.1;
251+
}
252+
253+
// SAFETY: The buffer will be validated by `clear_user`. We ensure that `len` is within
254+
// bounds in the check above.
255+
let left = unsafe { bindings::clear_user(self.0, len as _) } as usize;
256+
if left != 0 {
257+
ret = Err(Error::EFAULT);
258+
len -= left;
259+
}
260+
261+
self.0 = self.0.wrapping_add(len);
262+
self.1 -= len;
263+
ret
264+
}
265+
245266
/// Writes a byte slice to the user slice.
246267
///
247268
/// Returns `EFAULT` if the byte slice is bigger than the remaining size

0 commit comments

Comments
 (0)