[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260207-binder-shrink-vec-v3-v3-2-8ff388563427@cock.li>
Date: Sat, 07 Feb 2026 17:02:48 +0530
From: Shivam Kalra via B4 Relay <devnull+shivamklr.cock.li@...nel.org>
To: Danilo Krummrich <dakr@...nel.org>,
Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Vlastimil Babka <vbabka@...e.cz>,
"Liam R. Howlett" <Liam.Howlett@...cle.com>,
Uladzislau Rezki <urezki@...il.com>, Miguel Ojeda <ojeda@...nel.org>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Arve Hjønnevåg <arve@...roid.com>,
Todd Kjos <tkjos@...roid.com>, Christian Brauner <brauner@...nel.org>,
Carlos Llamas <cmllamas@...gle.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Shivam Kalra <shivamklr@...k.li>
Subject: [PATCH v3 2/4] rust: kvec: implement shrink_to and shrink_to_fit
for Vec
From: Shivam Kalra <shivamklr@...k.li>
Implement `shrink_to` and `shrink_to_fit` methods for `Vec<T, A>` where
`A` implements the `Shrinkable` trait.
`shrink_to` reduces the vector's capacity to a specified minimum, while
`shrink_to_fit` attempts to shrink capacity to match the current length.
Both methods only perform shrinking when it would be beneficial:
- The allocator must support meaningful shrinking (checked via the
`Shrinkable` trait bound and `is_shrinkable` runtime check).
- The operation must free at least one page of memory.
This prevents unnecessary allocations (where shrinking provides no
benefit) while allowing to reclaim unused memory.
The implementation uses explicit alloc+copy+free because `vrealloc`
does not yet support in-place shrinking. A TODO note marks this for
future optimization once the kernel's `vrealloc` gains that capability.
Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
Suggested-by: Danilo Krummrich <dakr@...nel.org>
Signed-off-by: Shivam Kalra <shivamklr@...k.li>
---
rust/kernel/alloc/kvec.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 109 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ac8d6f763ae81..22a327d69c061 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -3,13 +3,13 @@
//! Implementation of [`Vec`].
use super::{
- allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter},
+ allocator::{KVmalloc, Kmalloc, Shrinkable, Vmalloc, VmallocPageIter},
layout::ArrayLayout,
AllocError, Allocator, Box, Flags, NumaNode,
};
use crate::{
fmt,
- page::AsPageIter, //
+ page::{AsPageIter, PAGE_SIZE},
};
use core::{
borrow::{Borrow, BorrowMut},
@@ -735,6 +735,113 @@ pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) {
}
}
+impl<T, A: Shrinkable> Vec<T, A> {
+ /// Shrinks the capacity of the vector with a lower bound.
+ ///
+ /// The capacity will remain at least as large as both the length and the supplied value.
+ /// If the current capacity is less than the lower limit, this is a no-op.
+ ///
+ /// Shrinking only occurs if:
+ /// - The allocator supports shrinking for this allocation (see [`Shrinkable`]).
+ /// - The operation would free at least one page of memory.
+ ///
+ /// If these conditions are not met, the vector is left unchanged.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::alloc::allocator::Vmalloc;
+ ///
+ /// // Allocate enough capacity to span multiple pages.
+ /// let elements_per_page = kernel::page::PAGE_SIZE / core::mem::size_of::<u32>();
+ /// let mut v: Vec<u32, Vmalloc> = Vec::with_capacity(elements_per_page * 4, GFP_KERNEL)?;
+ /// v.push(1, GFP_KERNEL)?;
+ /// v.push(2, GFP_KERNEL)?;
+ ///
+ /// v.shrink_to(0, GFP_KERNEL)?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn shrink_to(&mut self, min_capacity: usize, flags: Flags) -> Result<(), AllocError> {
+ let target_cap = core::cmp::max(self.len(), min_capacity);
+
+ if self.capacity() <= target_cap {
+ return Ok(());
+ }
+
+ if Self::is_zst() {
+ return Ok(());
+ }
+
+ // SAFETY: `self.ptr` is valid by the type invariant.
+ if !unsafe { A::is_shrinkable(self.ptr.cast()) } {
+ return Ok(());
+ }
+
+ // Only shrink if we would free at least one page.
+ let current_size = self.capacity() * core::mem::size_of::<T>();
+ let target_size = target_cap * core::mem::size_of::<T>();
+ let current_pages = current_size.div_ceil(PAGE_SIZE);
+ let target_pages = target_size.div_ceil(PAGE_SIZE);
+
+ if current_pages <= target_pages {
+ return Ok(());
+ }
+
+ if target_cap == 0 {
+ if !self.layout.is_empty() {
+ // SAFETY: `self.ptr` was allocated with `A`, layout matches.
+ unsafe { A::free(self.ptr.cast(), self.layout.into()) };
+ }
+ self.ptr = NonNull::dangling();
+ self.layout = ArrayLayout::empty();
+ return Ok(());
+ }
+
+ // SAFETY: `target_cap <= self.capacity()` and original capacity was valid.
+ let new_layout = unsafe { ArrayLayout::<T>::new_unchecked(target_cap) };
+
+ // TODO: Once vrealloc supports in-place shrinking (mm/vmalloc.c:4316), this
+ // explicit alloc+copy+free can potentially be replaced with realloc.
+ let new_ptr = A::alloc(new_layout.into(), flags, NumaNode::NO_NODE)?;
+
+ // SAFETY: Both pointers are valid, non-overlapping, and properly aligned.
+ unsafe {
+ ptr::copy_nonoverlapping(self.as_ptr(), new_ptr.as_ptr().cast::<T>(), self.len);
+ }
+
+ // SAFETY: `self.ptr` was allocated with `A`, layout matches.
+ unsafe { A::free(self.ptr.cast(), self.layout.into()) };
+
+ // SAFETY: `new_ptr` is non-null because `A::alloc` succeeded.
+ self.ptr = unsafe { NonNull::new_unchecked(new_ptr.as_ptr().cast::<T>()) };
+ self.layout = new_layout;
+
+ Ok(())
+ }
+
+ /// Shrinks the capacity of the vector as much as possible.
+ ///
+ /// This is equivalent to calling `shrink_to(0, flags)`. See [`Vec::shrink_to`] for details.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::alloc::allocator::Vmalloc;
+ ///
+ /// let elements_per_page = kernel::page::PAGE_SIZE / core::mem::size_of::<u32>();
+ /// let mut v: Vec<u32, Vmalloc> = Vec::with_capacity(elements_per_page * 4, GFP_KERNEL)?;
+ /// v.push(1, GFP_KERNEL)?;
+ /// v.push(2, GFP_KERNEL)?;
+ /// v.push(3, GFP_KERNEL)?;
+ ///
+ /// v.shrink_to_fit(GFP_KERNEL)?;
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn shrink_to_fit(&mut self, flags: Flags) -> Result<(), AllocError> {
+ self.shrink_to(0, flags)
+ }
+}
+
impl<T: Clone, A: Allocator> Vec<T, A> {
/// Extend the vector by `n` clones of `value`.
pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
--
2.43.0
Powered by blists - more mailing lists