Skip to content

Commit 322720e

Browse files
Danilo Krummrichfbq
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 <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 611d72c commit 322720e

File tree

6 files changed

+19
-24
lines changed

6 files changed

+19
-24
lines changed

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

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

796795
impl CString {
@@ -803,7 +802,7 @@ impl CString {
803802
let size = f.bytes_written();
804803

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

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

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

858856
// INVARIANT: The `CStr` and `CString` types have the same invariants for
859857
// 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
types::{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
@@ -238,7 +238,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
238238
/// #[pin_data]
239239
/// struct DriverData {
240240
/// #[pin]
241-
/// queue: Mutex<Vec<Command>>,
241+
/// queue: Mutex<KVec<Command>>,
242242
/// buf: KBox<[u8; 1024 * 1024]>,
243243
/// }
244244
/// ```
@@ -247,7 +247,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
247247
/// #[pin_data(PinnedDrop)]
248248
/// struct DriverData {
249249
/// #[pin]
250-
/// queue: Mutex<Vec<Command>>,
250+
/// queue: Mutex<KVec<Command>>,
251251
/// buf: KBox<[u8; 1024 * 1024]>,
252252
/// raw_info: *mut Info,
253253
/// }
@@ -277,7 +277,7 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
277277
/// #[pin_data(PinnedDrop)]
278278
/// struct DriverData {
279279
/// #[pin]
280-
/// queue: Mutex<Vec<Command>>,
280+
/// queue: Mutex<KVec<Command>>,
281281
/// buf: KBox<[u8; 1024 * 1024]>,
282282
/// raw_info: *mut Info,
283283
/// }

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)