public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Tamir Duberstein <tamird@gmail.com>, Alice Ryhl <aliceryhl@google.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
	"Andrew Ballance" <andrewjballance@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] rust: alloc: replace `Vec::set_len` with `inc_len`
Date: Mon, 17 Mar 2025 14:46:30 +0000	[thread overview]
Message-ID: <D8IMFM8UTOV6.1EYIRMPNAZ7I7@proton.me> (raw)
In-Reply-To: <CAJ-ks9nSNweaSCVAruo-zWxMAFKcRxcJRScV06NRNBoeEZggfA@mail.gmail.com>

On Mon Mar 17, 2025 at 12:25 PM CET, Tamir Duberstein wrote:
> On Mon, Mar 17, 2025 at 6:48 AM Alice Ryhl <aliceryhl@google.com> wrote:
>>
>> On Mon, Mar 17, 2025 at 09:58:35AM +0000, Benno Lossin wrote:
>> > On Sun Mar 16, 2025 at 11:32 PM CET, Tamir Duberstein wrote:
>> > > Rename `set_len` to `inc_len` and simplify its safety contract.
>> > > ---
>> > >  rust/kernel/alloc/kvec.rs | 19 +++++++++----------
>> > >  rust/kernel/str.rs        |  2 +-
>> > >  rust/kernel/uaccess.rs    |  2 +-
>> > >  3 files changed, 11 insertions(+), 12 deletions(-)
>> > >
>> > > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
>> > > index ae9d072741ce..d43a1d609434 100644
>> > > --- a/rust/kernel/alloc/kvec.rs
>> > > +++ b/rust/kernel/alloc/kvec.rs
>> > > @@ -183,17 +183,16 @@ pub fn len(&self) -> usize {
>> > >          self.len
>> > >      }
>> > >
>> > > -    /// Forcefully sets `self.len` to `new_len`.
>> > > +    /// Increments `self.len` by `additional`.
>> >
>> > I would keep the "Forcefully".
>
> Why? Is it possible to set it any other way?

Yeah when `truncate` and `resize` land. It conveys that this is a
low-level operation.

>> > >      ///
>> > >      /// # Safety
>> > >      ///
>> > > -    /// - `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 + additional` must be less than or equal to [`Self::capacity`].
>> > > +    /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
>> > >      #[inline]
>> > > -    pub unsafe fn set_len(&mut self, new_len: usize) {
>> > > -        debug_assert!(new_len <= self.capacity());
>> > > -        self.len = new_len;
>> > > +    pub unsafe fn inc_len(&mut self, additional: usize) {
>> > > +        debug_assert!(self.len() + additional <= self.capacity());
>> >
>> > What if this overflows? Do we always have overflow debugging on when
>> > debug assertions are enabled? If yes, then this is fine.
>>
>> I don't think we do.
>
> Rearranged as
>
>         debug_assert!(additional <= self.capacity() - self.len());

LGTM

> It should be impossible for this to underflow because capacity must be
>>= len. Interestingly this isn't a documented invariant, but it is
> relied upon by `spare_capacity_mut`.

Oh yeah that should be an invariant. Feel free to open an issue or send
a patch.

>> > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> > > index 28e2201604d6..005713839e9e 100644
>> > > --- a/rust/kernel/str.rs
>> > > +++ b/rust/kernel/str.rs
>> > > @@ -840,7 +840,7 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
>> > >
>> > >          // SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is
>> > >          // `buf`'s capacity. The contents of the buffer have been initialised by writes to `f`.
>> > > -        unsafe { buf.set_len(f.bytes_written()) };
>> > > +        unsafe { buf.inc_len(f.bytes_written()) };
>> >
>> > This change seems wrong unless the code was wrong to begin with.
>> >
>> > Otherwise the change looks good.
>>
>> The buffer has length zero as it was just created with:
>>
>> let mut buf = KVec::with_capacity(size, GFP_KERNEL)?;

Ahh, I didn't look at the context. Makes sense.

> Indeed. Added to the commit message.

Thanks.

---
Cheers,
Benno


  reply	other threads:[~2025-03-17 14:46 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-16 22:31 [PATCH 0/2] rust: alloc: split `Vec::set_len` into `Vec::{inc,dec}_len` Tamir Duberstein
2025-03-16 22:32 ` [PATCH 1/2] rust: alloc: replace `Vec::set_len` with `inc_len` Tamir Duberstein
2025-03-17  9:58   ` Benno Lossin
2025-03-17 10:23     ` Miguel Ojeda
2025-03-17 14:43       ` Benno Lossin
2025-03-17 10:48     ` Alice Ryhl
2025-03-17 11:25       ` Tamir Duberstein
2025-03-17 14:46         ` Benno Lossin [this message]
2025-03-17 15:01           ` Tamir Duberstein
2025-03-17 10:50   ` Alice Ryhl
2025-03-17 11:16     ` Danilo Krummrich
2025-03-17 11:25     ` Tamir Duberstein
2025-03-16 22:32 ` [PATCH 2/2] rust: alloc: add `Vec::dec_len` Tamir Duberstein
2025-03-16 22:35   ` Tamir Duberstein
2025-03-16 22:41   ` Danilo Krummrich
2025-03-16 22:47     ` Tamir Duberstein
2025-03-16 23:02       ` Danilo Krummrich
2025-03-16 23:27         ` Tamir Duberstein
2025-03-17 11:22           ` Danilo Krummrich
2025-03-17 11:34             ` Tamir Duberstein
2025-03-17 10:04   ` Benno Lossin
2025-03-17 11:34     ` Tamir Duberstein
2025-03-17 11:47       ` Alice Ryhl
2025-03-17 12:59         ` Alice Ryhl
2025-03-17 13:53           ` Tamir Duberstein
2025-03-18  9:30             ` Alice Ryhl
2025-03-18 14:12               ` Tamir Duberstein
2025-03-18 14:44                 ` Alice Ryhl
2025-03-18 18:28                   ` Tamir Duberstein
2025-03-18 18:46                     ` Danilo Krummrich
2025-03-18 18:53                       ` Tamir Duberstein
2025-03-18 19:26                         ` Danilo Krummrich
2025-03-18 20:05                           ` Tamir Duberstein
2025-03-18 20:13                             ` Tamir Duberstein
2025-03-18 20:15                               ` Danilo Krummrich
2025-03-17 14:42           ` Benno Lossin
2025-03-17 14:44             ` Tamir Duberstein
2025-03-17 16:16               ` Danilo Krummrich
2025-03-17 16:21                 ` Tamir Duberstein
2025-03-17 14:39       ` Benno Lossin
2025-03-17 15:37         ` Tamir Duberstein
2025-03-17 15:57           ` Miguel Ojeda
2025-03-17 17:24           ` Benno Lossin
2025-03-17 17:28             ` Tamir Duberstein
2025-03-19 21:05   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D8IMFM8UTOV6.1EYIRMPNAZ7I7@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=andrewjballance@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox