From nobody Wed Dec 17 17:23:05 2025 Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 521821DACB8; Sun, 16 Mar 2025 22:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742164327; cv=none; b=ageVw+HxOThz4RnUvva8A3sXxPdDyT+lWQ/S4qEVtdsPHxET8AFZel00D4RGGc5MeRRJ6QCFvxerGnzwhhQVJAVSr0RlXkW9wEf+nlz9mIo0ksgDY4xcEtvCx9dH1ESUfUK9StaQnQmoC9ZMhCv0XE1KNhKaXgEopfk7Af2IEbs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742164327; c=relaxed/simple; bh=mmrKCOTP0QlHyNzH1bZH6zcPL2ywROdp+K21VV9cQL4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RLOXmza9A3T0LOyEy2aWHgHOsQv02UrjGdKDTcNfJ9YvzAY4zGIAdHmgqc4dgMgpR1jVtFYGBGV1kdhgYM+o5TnYb+muuco8qMZeyOniW2FrxcwiKYgFxgIhqygQEy1qdQU5JxCQz6Nh6flKVK3r7NuVLWjKdLpY+MEX1ogauJs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=h6mSvbhq; arc=none smtp.client-ip=209.85.219.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="h6mSvbhq" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-6eafac1f047so8017846d6.2; Sun, 16 Mar 2025 15:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742164324; x=1742769124; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=c6rkapsl1dsMuoCULlqyhf3BuA8jB33Df/+hoVs9skc=; b=h6mSvbhqR/kili2Lr/z7tjc6M/bZ8r5iuMGo5UVejgMonIV3O57EazHRKQnY8DcO5Z DC86PwKZDq9V0Gk3tuIuw2aTm8xQtvLSR9K3UyKOFdZBuPIQDg4LYcbXjvLulIERhWwA QKu9p8lJGqmK9o5LYrulO0EzjEgiXXJSon3MRufb0UZA06xAIllGwzwjulVN60hol7j+ ArRp5RuHIOyoGspPt49DxATpnfVcFSVMpaxMOdVxomVaMwKgOCeOkmiMOcxVapELLr7X qufO/qHdGSjloxr+YIw8DtXMzAkeZ8R34xUcNjfkTOPxUh81RqtZc4MyihM815DCxdg6 yVGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742164324; x=1742769124; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c6rkapsl1dsMuoCULlqyhf3BuA8jB33Df/+hoVs9skc=; b=oJvogwiPjs2UhxjYfD+z9gE8YROv6/EHKcuTeRQYF6ASKpZ5mPTFfi3uy20RZTEX3G BsGY7OHWBriYRTZu9M1wzS0Ml9heOdZNojM7R8JjkqnWG/BhEtMecPfYGynvyw3Q0pXd lsNEtDgwhmLKa1+82W+s7ST4yuugPd/Wwj/spQwLmdM5ejLQnmTU931b8vQauRRhLnhb RELQHP5fu6Njn1cUCdo4viu8mN+Sc9V5iWO6zCnt6UNVgMNpM8dWpJDCGIiXmEak/YZk zUu9XwprOiMl7a7t4zvyhGAgHF0EZqA0WEdfN3J1rzJgZ0eXtRSeKeLO71A71Sifft0/ sF/w== X-Forwarded-Encrypted: i=1; AJvYcCVwcQpeN0vLJYv1RNS4Y7xQLlND6ZDK7389oNb7GvvK+k8pqbSkFiGmN679T3jeMi52ELyMoCGCvWAj88c=@vger.kernel.org X-Gm-Message-State: AOJu0YwpfJI63S15+9WCNIqiGNSFeKwG22M9zvMVjognG/J5vMsUMpzu eI3yyqQrHPtOMsQtkC+G8L4OsTeuZ70N1vepM4tAvIkeHALKZ4oSUqcpYkuPPTo= X-Gm-Gg: ASbGncvfe4zyD+hyKopMGVQPeeyulZg5wOvN3NwHpgNjqHn+1ITRvAjiIq9VEymzAHJ V18OJhiBiPI0BWReOlIc80zfH3KtzTjs1tykNdKYPegDGWU1MwebYBYVaYCVIOz22vZRlW8Bv1g z9q+ECmE10atPYY5vNP8n0cVs2gKDWXk8wNOql4+36NvHabViRV1tiJhctg7M/eWoI4glnC4O4/ qvVzB6xd4B84UJas/TJLTKdeyMTKG0cuU+eF5jXjCSNo5FVi1CxMXFrm8LrMcAVXJLt2nDrmaS+ k13h+iu3kd+LSvi+MifpacASQQqNK907tMJbnpOfhSdWEYDTE4mdit6T2/V6k+Sbq/L+ X-Google-Smtp-Source: AGHT+IGnZJYVSuOawBG3HnXGtfS1/dTnspEKlDB34ubWSJduIX+N/JeJmvhz8cOwyT/5P0E6Bcva2w== X-Received: by 2002:ad4:5ccd:0:b0:6ea:d629:f492 with SMTP id 6a1803df08f44-6eaeaacd846mr157384816d6.29.1742164324316; Sun, 16 Mar 2025 15:32:04 -0700 (PDT) Received: from tamird-mac.local ([2600:4041:5be7:7c00:ac75:40f2:fdb1:31e5]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade3343b8sm48262146d6.69.2025.03.16.15.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Mar 2025 15:32:03 -0700 (PDT) From: Tamir Duberstein Date: Sun, 16 Mar 2025 18:32:00 -0400 Subject: [PATCH 1/2] rust: alloc: replace `Vec::set_len` with `inc_len` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250316-vec-set-len-v1-1-60f98a28723f@gmail.com> References: <20250316-vec-set-len-v1-0-60f98a28723f@gmail.com> In-Reply-To: <20250316-vec-set-len-v1-0-60f98a28723f@gmail.com> To: Danilo Krummrich , Andrew Ballance , Alice Ryhl , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev 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 } =20 - /// Forcefully sets `self.len` to `new_len`. + /// Increments `self.len` by `additional`. /// /// # 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::ca= pacity`]. + /// - All elements within the interval [`self.len`,`self.len + additio= nal`) must be initialized. #[inline] - pub unsafe fn set_len(&mut self, new_len: usize) { - debug_assert!(new_len <=3D self.capacity()); - self.len =3D new_len; + pub unsafe fn inc_len(&mut self, additional: usize) { + debug_assert!(self.len() + additional <=3D self.capacity()); + self.len +=3D additional; } =20 /// Returns a slice of the entire vector. @@ -298,7 +297,7 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<()= , AllocError> { // SAFETY: We just initialised the first spare entry, so it is saf= e to increase the length // by 1. We also know that the new length is <=3D capacity because= of the previous call to // `reserve` above. - unsafe { self.set_len(self.len() + 1) }; + unsafe { self.inc_len(1) }; Ok(()) } =20 @@ -475,7 +474,7 @@ pub fn extend_with(&mut self, n: usize, value: T, flags= : Flags) -> Result<(), Al // SAFETY: // - `self.len() + n < self.capacity()` due to the call to reserve= above, // - the loop and the line above initialized the next `n` elements. - unsafe { self.set_len(self.len() + n) }; + unsafe { self.inc_len(n) }; =20 Ok(()) } @@ -506,7 +505,7 @@ pub fn extend_from_slice(&mut self, other: &[T], flags:= Flags) -> Result<(), All // the length by the same number. // - `self.len() + other.len() <=3D self.capacity()` is guaranteed= by the preceding `reserve` // call. - unsafe { self.set_len(self.len() + other.len()) }; + unsafe { self.inc_len(other.len()) }; Ok(()) } =20 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= { =20 // SAFETY: The number of bytes that can be written to `f` is bound= ed by `size`, which is // `buf`'s capacity. The contents of the buffer have been initiali= sed by writes to `f`. - unsafe { buf.set_len(f.bytes_written()) }; + unsafe { buf.inc_len(f.bytes_written()) }; =20 // Check that there are no `NUL` bytes before the end. // SAFETY: The buffer is valid for read because `f.bytes_written()= ` is bounded by `size` diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 719b0a48ff55..0aa5455a18be 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -291,7 +291,7 @@ pub fn read_all(mut self, buf: &mut Vec, flags: Flags) -> R =20 // SAFETY: Since the call to `read_raw` was successful, so the nex= t `len` bytes of the // vector have been initialized. - unsafe { buf.set_len(buf.len() + len) }; + unsafe { buf.inc_len(len) }; Ok(()) } } --=20 2.48.1 From nobody Wed Dec 17 17:23:05 2025 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B61661EEA29; Sun, 16 Mar 2025 22:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742164330; cv=none; b=O4/0xO5gfEjG+8fR8T81SXLe0zsjFUs3IhrWr67ytyBzx37BWxhQkFRzEH10WNuWPZ9WsGfCBi9i24OwP8LNsjz8NU43aXj1NOHt7CzXos2EKrrvdTXy5LpRDTRp7TPe411vM6BO1wpr5W6ilJM8YoQ5CxLiwM3NU5GsPE3awd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742164330; c=relaxed/simple; bh=ZTJ0/WLxbcfAHux5UNNp9XEDxpZbfyMam0DKL2fBUuI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fSmLkH6QKIyjBlrPFfYMKwq+tdAhVMUvURSDbxwdjp5H6QDXLuk6qh0bKJjvVMZGkcGPWphqOKn90HAXeOUTEclK/LhkB3OvrFn2fxO8gUH0V80zLGU09nMVDVNqsSL6uikkhpBWlMAm+wNHiMJ4QqZaZRKyUlmKkhxVM0Iydm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=C2GcQZO8; arc=none smtp.client-ip=209.85.219.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="C2GcQZO8" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6e8fd49b85eso59542066d6.0; Sun, 16 Mar 2025 15:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742164327; x=1742769127; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=N7vZ5MLc4cyYefzAIVNKTaGSvbVYwxeoBh8a01cbZtE=; b=C2GcQZO8duh2o5y8LvuXkbuqgjTWOxjy83IGqzE0SHBz/NOmIL3ULWkvIu4cdMeh5b 3qjtMSt5c/WspEi6Qw+VVEuRRp1KTsgEC/Bk15vJoBUBD2SgfhIjNJnGSGku+KrXN+Op 8MFVL/xsajZVE8w3qN/ckvOguMu8wfwdcHrrW746KGoA+/50xUjx5zuvitD9oZyzagSM sUuUgMfMRH4NB42zDsVJDkVPTcQbL6Mh3NzdAcc6rXHEJIGkhLrTZK0x4wosOf53yUTA 0nrC9rz2bmkjhXBE4KY6RlmsnoqEJUCLkibpKmIMin8zCc2wttrS1QmluqCQfxeeNDZW wd/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742164327; x=1742769127; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N7vZ5MLc4cyYefzAIVNKTaGSvbVYwxeoBh8a01cbZtE=; b=QfTdpNPqgfQ7J9vtMOQ4YuziGRx2qDOSl/abJbftVY2TBZTLoke/vsw3PdxzbCH8vg GdeYLHMD0ShJRCvsqoRDZjtbFphYzxo8LKa69hIjf05PzjHr8vy85mRZZ3WRP2hpXiDK 9iV2bxj2cP3X+9qFtDZ9kgSzbgUv9jslUA1m0/C1RsMplqzC690aTQHfVPAiX/gfgPDR 6o3gEbSiyaplFno8pMsHuB42SKwTTzI6ZtXLAjGgBMEIKAAHRlXRncbQ5LuXo1jISimD js/5nBBbXMypiAVmB8FBBbXARzaexPu7pLMKMT7YVhg0KR23blnrauZKG+MdsZ9dIspc Kkdg== X-Forwarded-Encrypted: i=1; AJvYcCXV+HvCBG0N8Se5IJnS1h8GMbM/x0TC4aHfoBCGglwC6/3sXhMVaDelzdjTA1ipNx+mN3PjkrPllULQbT4=@vger.kernel.org X-Gm-Message-State: AOJu0YyQ5yB0FU765BH/wkHLFXT26Q8t6qberfRzpBxNkl24Y/i7i/Vi 7VsQcJKWabyDeJZWm3JBS7w3wTvujADizkLysNxgKOYK7J6bDaPhtnBO2B/brow= X-Gm-Gg: ASbGncuGsuZ2Dhcbh7kqQKmetpjFaMflixj3wUOT3/7brKsN1RnZeJOj8NbKOmQVimf 5QlBm/Lt93ngDGC4m2+sTq4iBmjwo3e3Fa3DYKYqduz7lewS3jxNegkLDP9WJn1ePXHXRqHr7Z7 KDAF7gh9aw4vo+Wap/nkFlJX9oyrjfJhfc1zMIxuen9ldjGDgz6DfTVB6hs7B5eZJYM9hX6OkH5 4vLhmZ9D6r+d+NyoinfqwsD+YKeLg1Snvtd9GezK7aYyYI09GcPROnYH+3w9Oj9B74SHbztZc3D 0o0OWXIXa3MTYYt25oxhE7BACaUDCSkfPWDhh1dDZTnH1kdVn11BnBx7WRPP1YqwTydO X-Google-Smtp-Source: AGHT+IFZUUJ3yeTrDlzQz+Ip6mwwZe/XuUC7UOiiW1UbAk6Xiv99F1p5HHiYTolhzD7WMapit92G0g== X-Received: by 2002:a05:6214:194b:b0:6e8:90eb:e591 with SMTP id 6a1803df08f44-6eaeaa7493dmr151123786d6.24.1742164326970; Sun, 16 Mar 2025 15:32:06 -0700 (PDT) Received: from tamird-mac.local ([2600:4041:5be7:7c00:ac75:40f2:fdb1:31e5]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade3343b8sm48262146d6.69.2025.03.16.15.32.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Mar 2025 15:32:05 -0700 (PDT) From: Tamir Duberstein Date: Sun, 16 Mar 2025 18:32:01 -0400 Subject: [PATCH 2/2] rust: alloc: add `Vec::dec_len` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250316-vec-set-len-v1-2-60f98a28723f@gmail.com> References: <20250316-vec-set-len-v1-0-60f98a28723f@gmail.com> In-Reply-To: <20250316-vec-set-len-v1-0-60f98a28723f@gmail.com> To: Danilo Krummrich , Andrew Ballance , Alice Ryhl , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev Add `Vec::dec_len` that reduces the length of the receiver. This method is intended to be used from methods that remove elements from `Vec` such as `truncate`, `pop`, `remove`, and others. This method is intentionally not `pub`. Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kvec.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index d43a1d609434..5d604e04b9a5 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -195,6 +195,21 @@ pub unsafe fn inc_len(&mut self, additional: usize) { self.len +=3D additional; } =20 + /// Decreases `self.len` by `count`. + /// + /// Returns a mutable reference to the removed elements. + /// + /// # Safety + /// + /// - `count` must be less than or equal to `self.len`. + unsafe fn dec_len(&mut self, count: usize) -> &mut [T] { + debug_assert!(count <=3D self.len()); + self.len -=3D count; + // SAFETY: The memory between `self.len` and `self.len + count` is= guaranteed to contain + // `self.len` initialized elements of type `T`. + unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().add(self.len)= , count) } + } + /// Returns a slice of the entire vector. #[inline] pub fn as_slice(&self) -> &[T] { --=20 2.48.1