public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/4] rust: alloc: add Vec shrinking methods
@ 2026-02-07 11:32 Shivam Kalra via B4 Relay
  2026-02-07 11:32 ` [PATCH v3 1/4] rust: alloc: introduce Shrinkable trait Shivam Kalra via B4 Relay
                   ` (4 more replies)
  0 siblings, 5 replies; 24+ messages in thread
From: Shivam Kalra via B4 Relay @ 2026-02-07 11:32 UTC (permalink / raw)
  To: Danilo Krummrich, Lorenzo Stoakes, Vlastimil Babka,
	Liam R. Howlett, Uladzislau Rezki, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	Carlos Llamas
  Cc: rust-for-linux, linux-kernel, Shivam Kalra

This series adds shrink_to() and shrink_to_fit() methods to Vec<T, A>
(and by extension KVVec, KVec, VVec) to allow explicit capacity
reduction for memory reclamation.

Problem:
When elements are removed from a KVVec, the allocated capacity is not
reduced. The underlying C allocators (krealloc/kvrealloc) don't shrink
memory in-place. This can cause significant memory waste in scenarios
with variable workloads.

Solution:
- Patch 1: Introduces the `Shrinkable` marker trait to identify allocators
  that can meaningfully reclaim memory when shrinking (e.g., Vmalloc but
  not Kmalloc, since Kmalloc uses fixed-size buckets).
- Patch 2: Implements shrink_to(min_capacity, flags) and
  shrink_to_fit(flags)  methods. Shrinking only occurs when
  the allocator is shrinkable AND the operation would free at least
  one page of memory.
- Patch 3: Adds KUnit tests for the new methods across different allocator
  backends (Vmalloc and KVmalloc).
- Patch 4: Uses shrink_to() in the Rust binder driver to reclaim memory
  when processes are deregistered. Shrinking is triggered only when the
  vector capacity exceeds 128 elements and less than half is used.

Testing:
- KUnit tests pass (rust_kvec test suite).
- Kernel boots successfully in QEMU with CONFIG_ANDROID_BINDER_IPC_RUST=y.

Changes since v2:
- Introduced new `Shrinkable` marker trait to distinguish allocators that
  benefit from shrinking (Vmalloc) from those that don't (Kmalloc).
- Shrinking now only occurs for vmalloc-backed buffers when at least one
  page can be freed, avoiding unnecessary work for kmalloc buffers.
  (Suggested by Danilo Krummrich)
- Split into 4 patches: Shrinkable trait introduction is now a
  separate patch to improve reviewability.
- Uses explicit alloc+copy+free since vrealloc doesn't yet support
  in-place shrinking; TODO added for future optimization when vrealloc
  gains that capability. (Discussed with Danilo Krummrich)
- Fixed minor KVVec/KVec terminology (binder uses KVVec not KVec).
- Expanded KUnit tests to cover different allocator backends and
  page-boundary shrinking behavior.

Changes since v1:
- Resend with correct threading (no code changes).
- Removed base-commit.
- Added explicit lore link to dependency in cover letter.

[1] https://lore.kernel.org/lkml/20260130205424.261700-1-shivamklr@cock.li/
[2] https://lore.kernel.org/lkml/20260131154016.270385-1-shivamklr@cock.li/

Signed-off-by: Shivam Kalra <shivamklr@cock.li>
---
Shivam Kalra (4):
      rust: alloc: introduce Shrinkable trait
      rust: kvec: implement shrink_to and shrink_to_fit for Vec
      rust: alloc: add KUnit tests for Vec shrink operations
      rust: binder: shrink all_procs when deregistering processes

 drivers/android/binder/context.rs |  10 ++
 rust/kernel/alloc/allocator.rs    |  48 +++++++
 rust/kernel/alloc/kvec.rs         | 296 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 352 insertions(+), 2 deletions(-)
---
base-commit: 3c4ae63073d84abee5d81ce46d86a94e9dae9c89
change-id: 20260207-binder-shrink-vec-v3-54045d77b0e7

Best regards,
-- 
Shivam Kalra <shivamklr@cock.li>



^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2026-02-11  9:36 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-07 11:32 [PATCH v3 0/4] rust: alloc: add Vec shrinking methods Shivam Kalra via B4 Relay
2026-02-07 11:32 ` [PATCH v3 1/4] rust: alloc: introduce Shrinkable trait Shivam Kalra via B4 Relay
2026-02-07 11:32 ` [PATCH v3 2/4] rust: kvec: implement shrink_to and shrink_to_fit for Vec Shivam Kalra via B4 Relay
2026-02-07 17:23   ` Danilo Krummrich
2026-02-08 16:11     ` Shivam Kalra
2026-02-07 11:32 ` [PATCH v3 3/4] rust: alloc: add KUnit tests for Vec shrink operations Shivam Kalra via B4 Relay
2026-02-07 11:32 ` [PATCH v3 4/4] rust: binder: shrink all_procs when deregistering processes Shivam Kalra via B4 Relay
2026-02-09 13:54   ` Alice Ryhl
2026-02-10 11:47     ` Shivam Kalra
2026-02-10 13:38 ` [PATCH v3 0/4] rust: alloc: add Vec shrinking methods Shivam Kalra
2026-02-10 13:57   ` Alice Ryhl
2026-02-10 15:05     ` Danilo Krummrich
2026-02-10 17:42       ` Shivam Kalra
2026-02-10 20:05       ` Alice Ryhl
2026-02-10 20:43         ` Danilo Krummrich
2026-02-10 20:53           ` Danilo Krummrich
2026-02-10 20:56             ` Danilo Krummrich
2026-02-10 20:58             ` Alice Ryhl
2026-02-10 21:11               ` Danilo Krummrich
2026-02-11  1:08                 ` Shivam Kalra
2026-02-11  6:51                   ` Alice Ryhl
2026-02-11  8:41                     ` Shivam Kalra
2026-02-11  8:57                       ` Danilo Krummrich
2026-02-11  9:35                         ` Shivam Kalra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox