lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DG8WJPYVA0H1.1PO95BAW0TK3Y@kernel.org>
Date: Sat, 07 Feb 2026 18:23:05 +0100
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Shivam Kalra via B4 Relay" <devnull+shivamklr.cock.li@...nel.org>
Cc: <shivamklr@...k.li>, "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>,
 <rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v3 2/4] rust: kvec: implement shrink_to and
 shrink_to_fit for Vec

On Sat Feb 7, 2026 at 12:32 PM CET, Shivam Kalra via B4 Relay wrote:
> +impl<T, A: Shrinkable> Vec<T, A> {

I don't think we should have a Shrinkable trait with is_shrinkable(). This is a
decision taken by the backing Allocator's realloc() function already.

Instead, shrink_to() should be a normal method of Vec<A, T> and just call
A::realloc().

For the temporary workaround we can have a temporary ShrinkQuirk trait that has
methods that take the same arguments as shrink_to().

In Vec::shrink_to() we can then hook in before calling A::realloc() and apply
the quirk.

	fn shrink_to() {
	    if self.shrink_needs_quirk() {
	        return self.shrink_quirk();
	    }

	    // Allocator backend decides.
	    A::realloc();
	}

> +    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);

This is the specific heuristic we use for the Vmalloc shrink workaround
(including when for KVmalloc is_vmalloc_addr() is true) and it doesn't belong
into the common code path.

But this goes away anyways with the above changes.

> +        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)?;

You can just use VVec<u32>.

> +    /// 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

Powered by Openwall GNU/*/Linux Powered by OpenVZ