Skip to contentSkip to conversation

rust: alloc: use `spare_capacity_mut` to reduce unsafe

PATCH Superseded Tamir Duberstein 2 participants 3 messages Mar 17, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: alloc: use `spare_capacity_mut` to reduce unsafe

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").

---
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/[email protected]

---
 rust/kernel/alloc/kvec.rs | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
rust/kernel/alloc/kvec.rs +2 -9
index ae9d072741ce..b91b287e0d22 100644
@@ -285,15 +285,8 @@ 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) };
-
-        // 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 { self.spare_capacity_mut().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,

Signature · 1 line
Tamir Duberstein <[email protected]>
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · 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").

One nit below, with or without that fixed:

Quoted 18 lines

---
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/[email protected]

---
 rust/kernel/alloc/kvec.rs | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
rust/kernel/alloc/kvec.rs +0 -1
index ae9d072741ce..b91b287e0d22 100644
@@ -285,15 +285,8 @@ 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:

rust/kernel/alloc/kvec.rs pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { · line 289

Quoted 10 lines
rust/kernel/alloc/kvec.rs +2 -8
-        // - `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) };
-
-        // 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 { self.spare_capacity_mut().get_unchecked_mut(0) }.write(v);

IMO it's difficult to tell which operation is the unsafe one here, so how about you factor the spare_capacity_mut call out.

---
Cheers,
Benno

Quoted 9 lines
rust/kernel/alloc/kvec.rs +0 -1
         // 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,

#2 lore raw
Quoted 30 lines · Benno Lossin
Quoted 6 lines · 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").

One nit below, with or without that fixed:

Quoted 17 lines

---
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/[email protected]

---
 rust/kernel/alloc/kvec.rs | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
rust/kernel/alloc/kvec.rs +0 -0
index ae9d072741ce..b91b287e0d22 100644
@@ -285,15 +285,8 @@ 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)?;
 
Quoted 14 lines

rust/kernel/alloc/kvec.rs pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { · line 289

Quoted 11 lines
rust/kernel/alloc/kvec.rs +2 -9
-        // 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) };
-
-        // 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 { self.spare_capacity_mut().get_unchecked_mut(0) }.write(v);

IMO it's difficult to tell which operation is the unsafe one here, so how about you factor the spare_capacity_mut call out.

👍