Skip to content

Commit 1e8c386

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

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

rust/helpers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ unsigned long rust_helper_copy_to_user(void __user *to, const void *from, unsign
2222
return copy_to_user(to, from, n);
2323
}
2424

25+
unsigned long rust_helper_clear_user(void __user *to, unsigned long n)
26+
{
27+
return clear_user(to, n);
28+
}
29+
2530
void rust_helper_spin_lock_init(spinlock_t *lock, const char *name,
2631
struct lock_class_key *key)
2732
{

rust/kernel/user_ptr.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ extern "C" {
2020
from: *const c_types::c_void,
2121
n: c_types::c_ulong,
2222
) -> c_types::c_ulong;
23+
24+
fn rust_helper_clear_user(to: *mut c_types::c_void, n: c_types::c_ulong) -> c_types::c_ulong;
2325
}
2426

2527
/// Specifies that a type is safely readable from byte slices.
@@ -242,6 +244,27 @@ impl UserSlicePtrWriter {
242244
self.len() == 0
243245
}
244246

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

0 commit comments

Comments
 (0)