rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] rust: alloc: extend safety requirements of Vec::set_len()
@ 2025-03-15 15:43 Danilo Krummrich
  2025-03-15 15:43 ` [PATCH 2/2] rust: alloc: add missing invariant in Vec::set_len() Danilo Krummrich
                   ` (3 more replies)
  0 siblings, 4 replies; 35+ messages in thread
From: Danilo Krummrich @ 2025-03-15 15:43 UTC (permalink / raw)
  To: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, benno.lossin,
	a.hindborg, aliceryhl, tmgross
  Cc: andrewjballance, rust-for-linux, Danilo Krummrich

Extend the safety requirements of set_len() to consider the case when
the new length is smaller than the old length.

Fixes: 2aac4cd7dae3 ("rust: alloc: implement kernel `Vec` type")
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/alloc/kvec.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ae9d072741ce..8540d9e2b717 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -189,7 +189,9 @@ pub fn len(&self) -> usize {
     ///
     /// - `new_len` must be less than or equal to [`Self::capacity`].
     /// - If `new_len` is greater than `self.len`, all elements within the interval
-    ///   [`self.len`,`new_len`) must be initialized.
+    ///   [`self.len`,`new_len`) must be initialized,
+    /// - if `new_len` is smaller than `self.len`, all elements within the interval
+    ///   [`new_len`, `self.len`) must either be dropped or copied and taken ownership of.
     #[inline]
     pub unsafe fn set_len(&mut self, new_len: usize) {
         debug_assert!(new_len <= self.capacity());

base-commit: 80e54e84911a923c40d7bee33a34c1b4be148d7a
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 35+ messages in thread
* Re: [PATCH 1/2] rust: alloc: extend safety requirements of Vec::set_len()
@ 2025-03-17  9:46 Benno Lossin
  0 siblings, 0 replies; 35+ messages in thread
From: Benno Lossin @ 2025-03-17  9:46 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Tamir Duberstein, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	a.hindborg, aliceryhl, tmgross, andrewjballance, rust-for-linux

On Sun Mar 16, 2025 at 7:59 PM CET, Danilo Krummrich wrote:
> On Sun, Mar 16, 2025 at 05:40:29PM +0000, Benno Lossin wrote:
>> On Sun Mar 16, 2025 at 2:46 PM CET, Danilo Krummrich wrote:
>> > On Sun, Mar 16, 2025 at 09:13:50AM -0400, Tamir Duberstein wrote:
>> >> On Sun, Mar 16, 2025 at 9:01 AM Danilo Krummrich <dakr@kernel.org> wrote:
>> >> > On Sun, Mar 16, 2025 at 08:42:43AM -0400, Tamir Duberstein wrote:
>> >> > > On Sun, Mar 16, 2025 at 8:31 AM Danilo Krummrich <dakr@kernel.org> wrote:
>> >> > >
>> >> > > > Let's go with just "must be taken ownership of" then. Unless there's subsequent
>> >> > > > feedback, I won't send a new version for this, since you both already gave your
>> >> > > > (conditional) RB for this.
>> 
>> I didn't realize the `forget` argument immediately, so I sadly have to
>> take my RB back.
>> 
>> >> > > What does it mean to take ownership, if not to run the destructor?
>> >> >
>> >> > Taking responsibility over the decision of what should happen to the value, i.e.
>> >> > forget about the value, drop it right away, keep it alive at a different memory
>> >> > location.
>> >> >
>> >> > > Given Benno's insight, I think the safety text is correct as it
>> >> > > existed before this patch.
>> >> >
>> >> > Without the addition there's no requirement for the caller to take ownership,
>> >> > but that's what we want here. Without the requirement it would be on set_len()
>> >> > to take a decision on what should happen with the value.
>> >> 
>> >> It isn't a safety requirement, right? I think it's fine to document,
>> >> but given that neglecting to run a destructor is safe, it doesn't seem
>> >> to affect safety.
>> >
>> > I don't think there's a rule that safety requirements of unsafe functions must
>> > only be used to guarantee memory safety, etc.
>> 
>> Well that's their only point. Safety documentation should only be
>> concerned with memory safety.
>
> Oops, I wanted to say "must not only be used to *directly* guarantee memory
> safety, etc.".
>
> For instance, it is also used to justify type invariants (that exist to prevent
> UB).
>
> So, what I wanted to say is that setting self.len by iteself is never directly a
> safety critical thing.

In my mental model there is no difference between "possibly immediately
UB" things and "possibly later UB" things. Both of the following
examples produce UB:

    let mut vec: KVec<u32> = KVec::new();
    unsafe { vec.set_len(1) };
    let elem = vec[0]; // UB

and

   let mut vec: KVec<u32> = KVec::new();
   let ptr = vec.as_ptr();
   let elem = unsafe { ptr.read() }; // UB

While in the first example, the UB only occurs when calling a safe
function, it is clearly the fault of the `unsafe` block before that
caused it.

>> You can still put it in the normal documentation though.
>> 
>> > But you're right, if set_len() guarantees a default of what happens to the
>> > value(s) when the caller doesn't take ownership, we could avoid the safety
>> > requirement.
>> 
>> Yeah the default is to forget the value.
>
> It's not really a defined default though, it's just what ultimately happens as
> the consequence of how Vec in general and set_len() are implemented.

What would be an alternative? I'd argue that forgetting is the only
sensible option.

> But let's define it then; what about:
>
> "[`Vec::set_len`] takes (or kepps) ownership of all elements within the range
> [0; `new_len`] and abandons ownership of all values outside of this range, if
> any."
>
> The caller may take ownership of the abandoned elements."

For some reason I don't like the wording of ownership in this case...
I'll try to come up with something better, since you're splitting the
function into `inc` and `dec` anyways (which I think is a good idea), so
I'll reply there.

---
Cheers,
Benno


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

end of thread, other threads:[~2025-04-07 12:10 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-15 15:43 [PATCH 1/2] rust: alloc: extend safety requirements of Vec::set_len() Danilo Krummrich
2025-03-15 15:43 ` [PATCH 2/2] rust: alloc: add missing invariant in Vec::set_len() Danilo Krummrich
2025-03-15 15:52   ` Danilo Krummrich
2025-03-15 17:44   ` Benno Lossin
2025-04-07 12:10   ` Danilo Krummrich
2025-03-15 16:06 ` [PATCH 1/2] rust: alloc: extend safety requirements of Vec::set_len() Tamir Duberstein
2025-03-15 17:44 ` Benno Lossin
2025-03-15 18:36   ` Danilo Krummrich
2025-03-16  0:33     ` Tamir Duberstein
2025-03-16  9:38       ` Benno Lossin
2025-03-16 12:31         ` Danilo Krummrich
2025-03-16 12:42           ` Tamir Duberstein
2025-03-16 13:01             ` Danilo Krummrich
2025-03-16 13:13               ` Tamir Duberstein
2025-03-16 13:46                 ` Danilo Krummrich
2025-03-16 17:40                   ` Benno Lossin
2025-03-16 18:59                     ` Danilo Krummrich
2025-03-16 19:09                       ` Danilo Krummrich
2025-03-16 19:30                         ` Tamir Duberstein
2025-03-16 20:54                           ` Danilo Krummrich
2025-03-16 21:10                             ` Tamir Duberstein
2025-03-16 21:17                               ` Danilo Krummrich
2025-03-16 21:20                                 ` Tamir Duberstein
2025-03-16 21:52                                   ` Tamir Duberstein
2025-03-16 21:59                                     ` Danilo Krummrich
2025-03-17  9:52                         ` Benno Lossin
2025-03-17 11:12                           ` Danilo Krummrich
2025-03-17 14:57                             ` Benno Lossin
2025-03-17 15:57                               ` Danilo Krummrich
2025-03-17 16:03                                 ` Miguel Ojeda
2025-03-17 17:33                                 ` Benno Lossin
2025-03-17 18:28                                   ` Danilo Krummrich
2025-03-16 12:08       ` Danilo Krummrich
2025-03-17 10:36 ` Alice Ryhl
  -- strict thread matches above, loose matches on Subject: below --
2025-03-17  9:46 Benno Lossin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).