Commit Graph

3 Commits

Author SHA1 Message Date
Alice Ryhl
8313296331 FROMGIT: rust: alloc: add Vec::insert_within_capacity
This adds a variant of Vec::insert that does not allocate memory. This
makes it safe to use this function while holding a spinlock. Rust Binder
uses it for the range allocator fast path.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250502-vec-methods-v5-7-06d20ad9366f@google.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Bug: 414994413
(cherry picked from commit 771c5a7d9843643b035938624050e7769133b9cc
 https://github.com/Rust-for-Linux/linux.git alloc-next)
Change-Id: I5661d5ee9ff239a92c4a640e334ad6d60196ad25
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
2025-05-16 10:31:04 -07:00
Alice Ryhl
c28afde01d FROMGIT: rust: alloc: add Vec::remove
This is needed by Rust Binder in the range allocator, and by upcoming
GPU drivers during firmware initialization.

Panics in the kernel are best avoided when possible, so an error is
returned if the index is out of bounds. An error type is used rather
than just returning Option<T> to let callers handle errors with ?.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250502-vec-methods-v5-6-06d20ad9366f@google.com
[ Remove `# Panics` section; `Vec::remove() handles the error properly.`
  - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Bug: 414994413
(cherry picked from commit 294a7ecbdf0a5d65c6df1287c5d56241e9331cf2
 https://github.com/Rust-for-Linux/linux.git alloc-next)
Change-Id: I80a8925910f234cccd325614634aa8a12e85ea1c
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
2025-05-16 10:31:04 -07:00
Alice Ryhl
1a17ca097d FROMGIT: rust: alloc: add Vec::push_within_capacity
This introduces a new method called `push_within_capacity` for appending
to a vector without attempting to allocate if the capacity is full. Rust
Binder will use this in various places to safely push to a vector while
holding a spinlock.

The implementation is moved to a push_within_capacity_unchecked method.
This is preferred over having push() call push_within_capacity()
followed by an unwrap_unchecked() for simpler unsafe.

Panics in the kernel are best avoided when possible, so an error is
returned if the vector does not have sufficient capacity. An error type
is used rather than just returning Result<(),T> to make it more
convenient for callers (i.e. they can use ? or unwrap).

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250502-vec-methods-v5-3-06d20ad9366f@google.com
[ Remove public visibility from `Vec::push_within_capacity_unchecked()`.
  - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Bug: 414994413
(cherry picked from commit 9def0d0a2a1c62d7970f4ce5ad5557968c98f637
 https://github.com/Rust-for-Linux/linux.git alloc-next)
Change-Id: Ib01be654536651a8189b28fddc9f8f332ffbcbfb
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
2025-05-16 10:31:04 -07:00