From: Danilo Krummrich <dakr@kernel.org>
To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com,
benno.lossin@proton.me, a.hindborg@samsung.com,
aliceryhl@google.com, akpm@linux-foundation.org
Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com,
boris.brezillon@collabora.com, lina@asahilina.net,
mcanal@igalia.com, zhiw@nvidia.com, cjia@nvidia.com,
jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com,
lyude@redhat.com, linux-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-mm@kvack.org,
Danilo Krummrich <dakr@kernel.org>
Subject: [PATCH v8 20/29] rust: alloc: remove `VecExt` extension
Date: Tue, 1 Oct 2024 16:59:55 +0200 [thread overview]
Message-ID: <20241001150008.183102-21-dakr@kernel.org> (raw)
In-Reply-To: <20241001150008.183102-1-dakr@kernel.org>
Now that all existing `Vec` users were moved to the kernel `Vec` type,
remove the `VecExt` extension.
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>
---
rust/kernel/alloc.rs | 1 -
rust/kernel/alloc/vec_ext.rs | 185 -----------------------------------
rust/kernel/prelude.rs | 5 +-
3 files changed, 1 insertion(+), 190 deletions(-)
delete mode 100644 rust/kernel/alloc/vec_ext.rs
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index 1d57bfe2acb1..e8955340fe62 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -7,7 +7,6 @@
pub mod kbox;
pub mod kvec;
pub mod layout;
-pub mod vec_ext;
#[cfg(any(test, testlib))]
pub mod allocator_test;
diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
deleted file mode 100644
index 1297a4be32e8..000000000000
--- a/rust/kernel/alloc/vec_ext.rs
+++ /dev/null
@@ -1,185 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-//! Extensions to [`Vec`] for fallible allocations.
-
-use super::{AllocError, Flags};
-use alloc::vec::Vec;
-
-/// Extensions to [`Vec`].
-pub trait VecExt<T>: Sized {
- /// Creates a new [`Vec`] instance with at least the given capacity.
- ///
- /// # Examples
- ///
- /// ```
- /// let v = Vec::<u32>::with_capacity(20, GFP_KERNEL)?;
- ///
- /// assert!(v.capacity() >= 20);
- /// # Ok::<(), Error>(())
- /// ```
- fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>;
-
- /// Appends an element to the back of the [`Vec`] instance.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = Vec::new();
- /// v.push(1, GFP_KERNEL)?;
- /// assert_eq!(&v, &[1]);
- ///
- /// v.push(2, GFP_KERNEL)?;
- /// assert_eq!(&v, &[1, 2]);
- /// # Ok::<(), Error>(())
- /// ```
- fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError>;
-
- /// Pushes clones of the elements of slice into the [`Vec`] instance.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = Vec::new();
- /// v.push(1, GFP_KERNEL)?;
- ///
- /// v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?;
- /// assert_eq!(&v, &[1, 20, 30, 40]);
- ///
- /// v.extend_from_slice(&[50, 60], GFP_KERNEL)?;
- /// assert_eq!(&v, &[1, 20, 30, 40, 50, 60]);
- /// # Ok::<(), Error>(())
- /// ```
- fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
- where
- T: Clone;
-
- /// Ensures that the capacity exceeds the length by at least `additional` elements.
- ///
- /// # Examples
- ///
- /// ```
- /// let mut v = Vec::new();
- /// v.push(1, GFP_KERNEL)?;
- ///
- /// v.reserve(10, GFP_KERNEL)?;
- /// let cap = v.capacity();
- /// assert!(cap >= 10);
- ///
- /// v.reserve(10, GFP_KERNEL)?;
- /// let new_cap = v.capacity();
- /// assert_eq!(new_cap, cap);
- ///
- /// # Ok::<(), Error>(())
- /// ```
- fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>;
-}
-
-impl<T> VecExt<T> for Vec<T> {
- fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
- let mut v = Vec::new();
- <Self as VecExt<_>>::reserve(&mut v, capacity, flags)?;
- Ok(v)
- }
-
- fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
- <Self as VecExt<_>>::reserve(self, 1, flags)?;
- let s = self.spare_capacity_mut();
- s[0].write(v);
-
- // SAFETY: We just initialised the first spare entry, so it is safe to increase the length
- // by 1. We also know that the new length is <= capacity because of the previous call to
- // `reserve` above.
- unsafe { self.set_len(self.len() + 1) };
- Ok(())
- }
-
- fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError>
- where
- T: Clone,
- {
- <Self as VecExt<_>>::reserve(self, other.len(), flags)?;
- for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
- slot.write(item.clone());
- }
-
- // SAFETY: We just initialised the `other.len()` spare entries, so it is safe to increase
- // the length by the same amount. We also know that the new length is <= capacity because
- // of the previous call to `reserve` above.
- unsafe { self.set_len(self.len() + other.len()) };
- Ok(())
- }
-
- #[cfg(any(test, testlib))]
- fn reserve(&mut self, additional: usize, _flags: Flags) -> Result<(), AllocError> {
- Vec::reserve(self, additional);
- Ok(())
- }
-
- #[cfg(not(any(test, testlib)))]
- fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
- let len = self.len();
- let cap = self.capacity();
-
- if cap - len >= additional {
- return Ok(());
- }
-
- if core::mem::size_of::<T>() == 0 {
- // The capacity is already `usize::MAX` for SZTs, we can't go higher.
- return Err(AllocError);
- }
-
- // We know cap is <= `isize::MAX` because `Layout::array` fails if the resulting byte size
- // is greater than `isize::MAX`. So the multiplication by two won't overflow.
- let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
- let layout = core::alloc::Layout::array::<T>(new_cap).map_err(|_| AllocError)?;
-
- let (old_ptr, len, cap) = destructure(self);
-
- // We need to make sure that `ptr` is either NULL or comes from a previous call to
- // `krealloc_aligned`. A `Vec<T>`'s `ptr` value is not guaranteed to be NULL and might be
- // dangling after being created with `Vec::new`. Instead, we can rely on `Vec<T>`'s capacity
- // to be zero if no memory has been allocated yet.
- let ptr = if cap == 0 {
- core::ptr::null_mut()
- } else {
- old_ptr
- };
-
- // SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to
- // `krealloc_aligned`. We also verified that the type is not a ZST.
- let new_ptr = unsafe { super::allocator::krealloc_aligned(ptr.cast(), layout, flags) };
- if new_ptr.is_null() {
- // SAFETY: We are just rebuilding the existing `Vec` with no changes.
- unsafe { rebuild(self, old_ptr, len, cap) };
- Err(AllocError)
- } else {
- // SAFETY: `ptr` has been reallocated with the layout for `new_cap` elements. New cap
- // is greater than `cap`, so it continues to be >= `len`.
- unsafe { rebuild(self, new_ptr.cast::<T>(), len, new_cap) };
- Ok(())
- }
- }
-}
-
-#[cfg(not(any(test, testlib)))]
-fn destructure<T>(v: &mut Vec<T>) -> (*mut T, usize, usize) {
- let mut tmp = Vec::new();
- core::mem::swap(&mut tmp, v);
- let mut tmp = core::mem::ManuallyDrop::new(tmp);
- let len = tmp.len();
- let cap = tmp.capacity();
- (tmp.as_mut_ptr(), len, cap)
-}
-
-/// Rebuilds a `Vec` from a pointer, length, and capacity.
-///
-/// # Safety
-///
-/// The same as [`Vec::from_raw_parts`].
-#[cfg(not(any(test, testlib)))]
-unsafe fn rebuild<T>(v: &mut Vec<T>, ptr: *mut T, len: usize, cap: usize) {
- // SAFETY: The safety requirements from this function satisfy those of `from_raw_parts`.
- let mut tmp = unsafe { Vec::from_raw_parts(ptr, len, cap) };
- core::mem::swap(&mut tmp, v);
-}
diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
index 80223cdaa485..07daccf6ca8e 100644
--- a/rust/kernel/prelude.rs
+++ b/rust/kernel/prelude.rs
@@ -14,10 +14,7 @@
#[doc(no_inline)]
pub use core::pin::Pin;
-pub use crate::alloc::{flags::*, vec_ext::VecExt, Box, KBox, KVBox, KVVec, KVec, VBox, VVec};
-
-#[doc(no_inline)]
-pub use alloc::vec::Vec;
+pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec};
#[doc(no_inline)]
pub use macros::{module, pin_data, pinned_drop, vtable, Zeroable};
--
2.46.1
next prev parent reply other threads:[~2024-10-01 15:02 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-01 14:59 [PATCH v8 00/29] Generic `Allocator` support for Rust Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 01/29] rust: alloc: add `Allocator` trait Danilo Krummrich
2024-10-01 18:24 ` Gary Guo
2024-10-02 14:59 ` Benno Lossin
2024-10-01 14:59 ` [PATCH v8 02/29] rust: alloc: separate `aligned_size` from `krealloc_aligned` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 03/29] rust: alloc: rename `KernelAllocator` to `Kmalloc` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 04/29] rust: alloc: implement `ReallocFunc` Danilo Krummrich
2024-10-01 16:11 ` Benno Lossin
2024-10-01 18:25 ` Gary Guo
2024-10-01 14:59 ` [PATCH v8 05/29] rust: alloc: make `allocator` module public Danilo Krummrich
2024-10-01 16:01 ` Benno Lossin
2024-10-01 18:25 ` Gary Guo
2024-10-01 14:59 ` [PATCH v8 06/29] rust: alloc: implement `Allocator` for `Kmalloc` Danilo Krummrich
2024-10-01 16:01 ` Benno Lossin
2024-10-01 18:26 ` Gary Guo
2024-10-01 14:59 ` [PATCH v8 07/29] rust: alloc: add module `allocator_test` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 08/29] rust: alloc: implement `Vmalloc` allocator Danilo Krummrich
2024-10-01 18:26 ` Gary Guo
2024-10-01 14:59 ` [PATCH v8 09/29] rust: alloc: implement `KVmalloc` allocator Danilo Krummrich
2024-10-01 18:27 ` Gary Guo
2024-10-01 14:59 ` [PATCH v8 10/29] rust: alloc: add __GFP_NOWARN to `Flags` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 11/29] rust: alloc: implement kernel `Box` Danilo Krummrich
2024-10-01 18:28 ` Gary Guo
2024-10-03 23:00 ` Miguel Ojeda
2024-10-04 12:04 ` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 12/29] rust: treewide: switch to our kernel `Box` type Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 13/29] rust: alloc: remove extension of std's `Box` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 14/29] rust: alloc: add `Box` to prelude Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 15/29] rust: alloc: introduce `ArrayLayout` Danilo Krummrich
2024-10-01 18:31 ` Gary Guo
2024-10-03 0:27 ` kernel test robot
2024-10-01 14:59 ` [PATCH v8 16/29] rust: alloc: implement kernel `Vec` type Danilo Krummrich
2024-10-01 18:39 ` Gary Guo
2024-10-02 15:02 ` Benno Lossin
2024-10-02 15:20 ` Danilo Krummrich
2024-10-02 19:04 ` Benno Lossin
2024-10-01 14:59 ` [PATCH v8 17/29] rust: alloc: implement `IntoIterator` for `Vec` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 18/29] rust: alloc: implement `collect` for `IntoIter` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 19/29] rust: treewide: switch to the kernel `Vec` type Danilo Krummrich
2024-10-01 14:59 ` Danilo Krummrich [this message]
2024-10-01 14:59 ` [PATCH v8 21/29] rust: alloc: add `Vec` to prelude Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 22/29] rust: error: use `core::alloc::LayoutError` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 23/29] rust: error: check for config `test` in `Error::name` Danilo Krummrich
2024-10-01 14:59 ` [PATCH v8 24/29] rust: alloc: implement `contains` for `Flags` Danilo Krummrich
2024-10-01 15:00 ` [PATCH v8 25/29] rust: alloc: implement `Cmalloc` in module allocator_test Danilo Krummrich
2024-10-02 14:58 ` Benno Lossin
2024-10-02 20:37 ` Gary Guo
2024-10-01 15:00 ` [PATCH v8 26/29] rust: str: test: replace `alloc::format` Danilo Krummrich
2024-10-01 15:00 ` [PATCH v8 27/29] rust: alloc: update module comment of alloc.rs Danilo Krummrich
2024-10-01 15:00 ` [PATCH v8 28/29] kbuild: rust: remove the `alloc` crate and `GlobalAlloc` Danilo Krummrich
2024-10-01 15:00 ` [PATCH v8 29/29] MAINTAINERS: add entry for the Rust `alloc` module Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241001150008.183102-21-dakr@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@samsung.com \
--cc=airlied@redhat.com \
--cc=ajanulgu@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=boris.brezillon@collabora.com \
--cc=cjia@nvidia.com \
--cc=daniel.almeida@collabora.com \
--cc=faith.ekstrand@collabora.com \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lyude@redhat.com \
--cc=mcanal@igalia.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=zhiw@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).