* [PATCH v3] rust: alloc: use `spare_capacity_mut` to reduce unsafe
@ 2025-03-18 14:52 Tamir Duberstein
2025-04-07 15:04 ` Danilo Krummrich
0 siblings, 1 reply; 2+ messages in thread
From: Tamir Duberstein @ 2025-03-18 14:52 UTC (permalink / raw)
To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross
Cc: rust-for-linux, linux-kernel, Tamir Duberstein
Use `spare_capacity_mut` in the implementation of `push` to reduce the
use of `unsafe`. Both methods were added in commit 2aac4cd7dae3 ("rust:
alloc: implement kernel `Vec` type").
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
Changes in v3:
- Clarify unsafe operation by factoring out safe call. (Benno Lossin)
- Link to v2: https://lore.kernel.org/r/20250317-vec-push-use-spare-v2-1-5dc6583f732c@gmail.com
Changes in v2:
- Use `slice::get_unchecked_mut` to ensure we avoid a bounds check.
(Benno Lossin)
- Link to v1: https://lore.kernel.org/r/20250317-vec-push-use-spare-v1-1-7e025ef4ae14@gmail.com
---
rust/kernel/alloc/kvec.rs | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ae9d072741ce..f5b1f201a80a 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -285,15 +285,10 @@ pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
self.reserve(1, flags)?;
- // SAFETY:
- // - `self.len` is smaller than `self.capacity` and hence, the resulting pointer is
- // guaranteed to be part of the same allocated object.
- // - `self.len` can not overflow `isize`.
- let ptr = unsafe { self.as_mut_ptr().add(self.len) };
+ let spare = self.spare_capacity_mut();
- // SAFETY:
- // - `ptr` is properly aligned and valid for writes.
- unsafe { core::ptr::write(ptr, v) };
+ // SAFETY: The call to `reserve` was successful so the spare capacity is at least 1.
+ unsafe { spare.get_unchecked_mut(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
---
base-commit: cf25bc61f8aecad9b0c45fe32697e35ea4b13378
change-id: 20250317-vec-push-use-spare-27484fd016a9
Best regards,
--
Tamir Duberstein <tamird@gmail.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v3] rust: alloc: use `spare_capacity_mut` to reduce unsafe
2025-03-18 14:52 [PATCH v3] rust: alloc: use `spare_capacity_mut` to reduce unsafe Tamir Duberstein
@ 2025-04-07 15:04 ` Danilo Krummrich
0 siblings, 0 replies; 2+ messages in thread
From: Danilo Krummrich @ 2025-04-07 15:04 UTC (permalink / raw)
To: Tamir Duberstein
Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, rust-for-linux, linux-kernel
On 3/18/25 3:52 PM, Tamir Duberstein wrote:
> Use `spare_capacity_mut` in the implementation of `push` to reduce the
> use of `unsafe`. Both methods were added in commit 2aac4cd7dae3 ("rust:
> alloc: implement kernel `Vec` type").
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Benno Lossin <benno.lossin@proton.me>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
Applied to rust/alloc-next, thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-04-07 15:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-18 14:52 [PATCH v3] rust: alloc: use `spare_capacity_mut` to reduce unsafe Tamir Duberstein
2025-04-07 15:04 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®