Skip to content

Commit 58eff8e

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: treewide: switch to the kernel Vec type
Now that we got the kernel `Vec` in place, convert all existing `Vec` users to make use of it. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-20-dakr@kernel.org [ Converted `kasan_test_rust.rs` too, as discussed. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 93e6023 commit 58eff8e

File tree

7 files changed

+20
-25
lines changed

7 files changed

+20
-25
lines changed

mm/kasan/kasan_test_rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use kernel::prelude::*;
1111
/// drop the vector, and touch it.
1212
#[no_mangle]
1313
pub extern "C" fn kasan_test_rust_uaf() -> u8 {
14-
let mut v: Vec<u8> = Vec::new();
14+
let mut v: KVec<u8> = KVec::new();
1515
for _ in 0..4096 {
1616
v.push(0x42, GFP_KERNEL).unwrap();
1717
}

rust/kernel/str.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
//! String representations.
44
5-
use crate::alloc::{flags::*, vec_ext::VecExt, AllocError};
6-
use alloc::vec::Vec;
5+
use crate::alloc::{flags::*, AllocError, KVec};
76
use core::fmt::{self, Write};
87
use core::ops::{self, Deref, DerefMut, Index};
98

@@ -791,7 +790,7 @@ impl fmt::Write for Formatter {
791790
/// assert_eq!(s.is_ok(), false);
792791
/// ```
793792
pub struct CString {
794-
buf: Vec<u8>,
793+
buf: KVec<u8>,
795794
}
796795

797796
impl CString {
@@ -804,7 +803,7 @@ impl CString {
804803
let size = f.bytes_written();
805804

806805
// Allocate a vector with the required number of bytes, and write to it.
807-
let mut buf = <Vec<_> as VecExt<_>>::with_capacity(size, GFP_KERNEL)?;
806+
let mut buf = KVec::with_capacity(size, GFP_KERNEL)?;
808807
// SAFETY: The buffer stored in `buf` is at least of size `size` and is valid for writes.
809808
let mut f = unsafe { Formatter::from_buffer(buf.as_mut_ptr(), size) };
810809
f.write_fmt(args)?;
@@ -851,10 +850,9 @@ impl<'a> TryFrom<&'a CStr> for CString {
851850
type Error = AllocError;
852851

853852
fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
854-
let mut buf = Vec::new();
853+
let mut buf = KVec::new();
855854

856-
<Vec<_> as VecExt<_>>::extend_from_slice(&mut buf, cstr.as_bytes_with_nul(), GFP_KERNEL)
857-
.map_err(|_| AllocError)?;
855+
buf.extend_from_slice(cstr.as_bytes_with_nul(), GFP_KERNEL)?;
858856

859857
// INVARIANT: The `CStr` and `CString` types have the same invariants for
860858
// the string data, and we copied it over without changes.

rust/kernel/sync/locked_by.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use core::{cell::UnsafeCell, mem::size_of, ptr};
4343
/// struct InnerDirectory {
4444
/// /// The sum of the bytes used by all files.
4545
/// bytes_used: u64,
46-
/// _files: Vec<File>,
46+
/// _files: KVec<File>,
4747
/// }
4848
///
4949
/// struct Directory {

rust/kernel/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl ForeignOwnable for () {
135135
/// # use kernel::types::ScopeGuard;
136136
/// fn example3(arg: bool) -> Result {
137137
/// let mut vec =
138-
/// ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len()));
138+
/// ScopeGuard::new_with_data(KVec::new(), |v| pr_info!("vec had {} elements\n", v.len()));
139139
///
140140
/// vec.push(10u8, GFP_KERNEL)?;
141141
/// if arg {

rust/kernel/uaccess.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{
1111
prelude::*,
1212
transmute::{AsBytes, FromBytes},
1313
};
14-
use alloc::vec::Vec;
1514
use core::ffi::{c_ulong, c_void};
1615
use core::mem::{size_of, MaybeUninit};
1716

@@ -46,15 +45,14 @@ pub type UserPtr = usize;
4645
/// every byte in the region.
4746
///
4847
/// ```no_run
49-
/// use alloc::vec::Vec;
5048
/// use core::ffi::c_void;
5149
/// use kernel::error::Result;
5250
/// use kernel::uaccess::{UserPtr, UserSlice};
5351
///
5452
/// fn bytes_add_one(uptr: UserPtr, len: usize) -> Result<()> {
5553
/// let (read, mut write) = UserSlice::new(uptr, len).reader_writer();
5654
///
57-
/// let mut buf = Vec::new();
55+
/// let mut buf = KVec::new();
5856
/// read.read_all(&mut buf, GFP_KERNEL)?;
5957
///
6058
/// for b in &mut buf {
@@ -69,7 +67,6 @@ pub type UserPtr = usize;
6967
/// Example illustrating a TOCTOU (time-of-check to time-of-use) bug.
7068
///
7169
/// ```no_run
72-
/// use alloc::vec::Vec;
7370
/// use core::ffi::c_void;
7471
/// use kernel::error::{code::EINVAL, Result};
7572
/// use kernel::uaccess::{UserPtr, UserSlice};
@@ -78,21 +75,21 @@ pub type UserPtr = usize;
7875
/// fn is_valid(uptr: UserPtr, len: usize) -> Result<bool> {
7976
/// let read = UserSlice::new(uptr, len).reader();
8077
///
81-
/// let mut buf = Vec::new();
78+
/// let mut buf = KVec::new();
8279
/// read.read_all(&mut buf, GFP_KERNEL)?;
8380
///
8481
/// todo!()
8582
/// }
8683
///
8784
/// /// Returns the bytes behind this user pointer if they are valid.
88-
/// fn get_bytes_if_valid(uptr: UserPtr, len: usize) -> Result<Vec<u8>> {
85+
/// fn get_bytes_if_valid(uptr: UserPtr, len: usize) -> Result<KVec<u8>> {
8986
/// if !is_valid(uptr, len)? {
9087
/// return Err(EINVAL);
9188
/// }
9289
///
9390
/// let read = UserSlice::new(uptr, len).reader();
9491
///
95-
/// let mut buf = Vec::new();
92+
/// let mut buf = KVec::new();
9693
/// read.read_all(&mut buf, GFP_KERNEL)?;
9794
///
9895
/// // THIS IS A BUG! The bytes could have changed since we checked them.
@@ -130,7 +127,7 @@ impl UserSlice {
130127
/// Reads the entirety of the user slice, appending it to the end of the provided buffer.
131128
///
132129
/// Fails with [`EFAULT`] if the read happens on a bad address.
133-
pub fn read_all(self, buf: &mut Vec<u8>, flags: Flags) -> Result {
130+
pub fn read_all(self, buf: &mut KVec<u8>, flags: Flags) -> Result {
134131
self.reader().read_all(buf, flags)
135132
}
136133

@@ -291,9 +288,9 @@ impl UserSliceReader {
291288
/// Reads the entirety of the user slice, appending it to the end of the provided buffer.
292289
///
293290
/// Fails with [`EFAULT`] if the read happens on a bad address.
294-
pub fn read_all(mut self, buf: &mut Vec<u8>, flags: Flags) -> Result {
291+
pub fn read_all(mut self, buf: &mut KVec<u8>, flags: Flags) -> Result {
295292
let len = self.length;
296-
VecExt::<u8>::reserve(buf, len, flags)?;
293+
buf.reserve(len, flags)?;
297294

298295
// The call to `try_reserve` was successful, so the spare capacity is at least `len` bytes
299296
// long.

rust/macros/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
242242
/// #[pin_data]
243243
/// struct DriverData {
244244
/// #[pin]
245-
/// queue: Mutex<Vec<Command>>,
245+
/// queue: Mutex<KVec<Command>>,
246246
/// buf: KBox<[u8; 1024 * 1024]>,
247247
/// }
248248
/// ```
@@ -251,7 +251,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
251251
/// #[pin_data(PinnedDrop)]
252252
/// struct DriverData {
253253
/// #[pin]
254-
/// queue: Mutex<Vec<Command>>,
254+
/// queue: Mutex<KVec<Command>>,
255255
/// buf: KBox<[u8; 1024 * 1024]>,
256256
/// raw_info: *mut Info,
257257
/// }
@@ -281,7 +281,7 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
281281
/// #[pin_data(PinnedDrop)]
282282
/// struct DriverData {
283283
/// #[pin]
284-
/// queue: Mutex<Vec<Command>>,
284+
/// queue: Mutex<KVec<Command>>,
285285
/// buf: KBox<[u8; 1024 * 1024]>,
286286
/// raw_info: *mut Info,
287287
/// }

samples/rust/rust_minimal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ module! {
1313
}
1414

1515
struct RustMinimal {
16-
numbers: Vec<i32>,
16+
numbers: KVec<i32>,
1717
}
1818

1919
impl kernel::Module for RustMinimal {
2020
fn init(_module: &'static ThisModule) -> Result<Self> {
2121
pr_info!("Rust minimal sample (init)\n");
2222
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
2323

24-
let mut numbers = Vec::new();
24+
let mut numbers = KVec::new();
2525
numbers.push(72, GFP_KERNEL)?;
2626
numbers.push(108, GFP_KERNEL)?;
2727
numbers.push(200, GFP_KERNEL)?;

0 commit comments

Comments
 (0)