From nobody Wed Dec 17 08:51:13 2025 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) (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 657111DDC30; Tue, 18 Mar 2025 20:14:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328863; cv=none; b=Qg1y3t5lo59kwLHONZ8FnhOP6RCgeNIxAUslGAUhE1CTdmHka/zh62ML4QO6LEg+fQAu75ZQv+vHAa6+SRCIwidxejPUoTZUnd1fcJLgk7qdQOdTcF32i2cI8vxYnynQqY2dqJKKDCPPW4lyNtwlfz5GjXR1pHCzCG+cAnXIgdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328863; c=relaxed/simple; bh=u81mPTwzISAqjuJ8U83p3mV3+NpkdOo2hR6Sa918HLY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IgkGzNv9BPcDuoCcKlbmzWq2/E9laPmw7y3NBQxvLtJbhbb5rqA7AsfY9upArhnRVqyprCrbCSkdmUlRlk4ZJkMsToyJ2UBdFbM1apfPg8QKRbFcMekaeP2YF4bC3fncbuoL6NfqlHN0Yt6XkuIRH/pmpzjE4/eF9hZhUGOrqFk= 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=HjHSLfcQ; arc=none smtp.client-ip=209.85.219.51 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="HjHSLfcQ" Received: by mail-qv1-f51.google.com with SMTP id 6a1803df08f44-6e89a2501a0so58120266d6.1; Tue, 18 Mar 2025 13:14:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742328860; x=1742933660; 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=i6DXPq4ezx60cnU9aZXHs1sMKK1LCNiHRchniD7m90I=; b=HjHSLfcQWPHkufS0SI8jgx1M8nL2IBncnrezKugItTNqFBLONsXa74bHVEkEKeNfQo 6VpabtLRyvknnG8zsIbj+1AKoXgm6RccvPs6JCSGTNjfSKOXS6XK/BtKxiNstV6t2Sad tqSBD+TA0nfGtPJeJvsUXj/WBQg0utJQesNdEuUCaJ1QAyY5JxEjC44sooiv+0Ufj53N najKKbdc+e4iDcDPFP6d0jnQ4x0V5x9SftpZwhe+iSil7bRWEDktpWSy+xSvn2rpahry vUy6gmZ22KP8wgXyzUU+orm9Dzl4KHyaBN/76R5QqUPO4wNru7ehvU3MPkSVJjoUXM39 6JLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742328860; x=1742933660; 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=i6DXPq4ezx60cnU9aZXHs1sMKK1LCNiHRchniD7m90I=; b=cmRblilgMJy/vHVRAtzXYdBObefULbPFJkKx4uTD8E4uNdGm6onujTwZ+Ja0l58LQN GZ2HXAcDDenRyGVbZ1TTSUOYd3xZ3CXK6+RSu/B8nD3qj24p7iWBOI+04s7rltKZpqEk s52raDk9BQiA7LR+aTP6BZthlvy3+qKbh4IVTuVQB51BOF8qHp1ZiAPYvo4t/H6FbS2g FnbD9Ny2KVKSHUNXeSOArhW1zHoFbFCAv/lkEjhlUJn3nJkeecOpyaUwtG8dLbWkmBHn imBQXY+1BW+d8ewGVm0XuEvoezSrM1jaMF1svKwH24UlY/BrJ3WS060NyKKHh3pJbI55 6GPA== X-Forwarded-Encrypted: i=1; AJvYcCWxHHP8juy43zuid0a1j8PQlkxUFNVAgnfrSAKS8deCxHfqCKoc52qGPeYAfVf/dEroCO3loPxY67+96/o=@vger.kernel.org X-Gm-Message-State: AOJu0YzbeYwZWLM4VLF75azS5706tiTeKW+FowqhQ1YymFbN1m2w6iEb iCfdKCgbyP4aZ0/4cSg20r3RJpjxRnqWCUH9QIQbEjsm2w7UxMZV X-Gm-Gg: ASbGnctqkka9Z4shMXr750xDWq5Q0L6xvg0wsWPqw5hborKAq8amO4vFdCPb8kaDdqH ypWZ7FEXDUKQRkpjWQO/PsnjYTQlu3wtL3t7xGFLwG/vOhiilL0xOSbFdOlmEwVgk2qpaNRG87w OxGoOrK44ptQ+233/HTe8dUIb6MOAL6GTyf/UbLZnukhnQzOCe2g1Ilj349rlkHrqPrRHyEoAJF m55wqVS7q9SDYYxfCq3zx2++2pmp8l/s9w5/wIZhkqSsV1g0gpuTF/DedoUGVAsI1KwUq/S3oM3 7MgAdSDOmLz0BlxaZtJJoLIOR3Iou3e7hj7vKUQHmdYCxG+yClNww22U0G8EPO12ma6u X-Google-Smtp-Source: AGHT+IEQ2QGIxkeIvd3Szhkc5NsPCnpFpBoYbiua+k8oizbuLHSDGlKUYAOT7qKsDfdfNfoHPoLH9g== X-Received: by 2002:a05:6214:238e:b0:6e8:f905:aff0 with SMTP id 6a1803df08f44-6eb293dd244mr2379256d6.35.1742328860184; Tue, 18 Mar 2025 13:14:20 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2620:10d:c091:600::1:e903]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade209335sm71301046d6.22.2025.03.18.13.14.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Mar 2025 13:14:19 -0700 (PDT) From: Tamir Duberstein Date: Tue, 18 Mar 2025 16:13:53 -0400 Subject: [PATCH v2 1/4] rust: alloc: add Vec::len() <= Vec::capacity invariant 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: <20250318-vec-set-len-v2-1-293d55f82d18@gmail.com> References: <20250318-vec-set-len-v2-0-293d55f82d18@gmail.com> In-Reply-To: <20250318-vec-set-len-v2-0-293d55f82d18@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 Document the invariant that the vector's length is always less than or equal to its capacity. This is already implied by these other invariants: - `self.len` always represents the exact number of elements stored in the vector. - `self.layout` represents the absolute number of elements that can be stored within the vector without re-allocation. but it doesn't hurt to spell it out. Note that the language references `self.capacity` rather than `self.layout.len` as the latter is zero for a vector of ZSTs. Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kvec.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 18bcc59f0b38..ce58ee66c99b 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -90,6 +90,8 @@ macro_rules! kvec { /// without re-allocation. For ZSTs `self.layout`'s capacity is zero. Ho= wever, it is legal for the /// backing buffer to be larger than `layout`. /// +/// - `self.len()` is always less than or equal to `self.capacity()`. +/// /// - The `Allocator` type `A` of the vector is the exact same `Allocator`= type the backing buffer /// was allocated with (and must be freed with). pub struct Vec { @@ -259,8 +261,8 @@ pub const fn new() -> Self { /// Returns a slice of `MaybeUninit` for the remaining spare capaci= ty of the vector. pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit] { // SAFETY: - // - `self.len` is smaller than `self.capacity` and hence, the res= ulting pointer is - // guaranteed to be part of the same allocated object. + // - `self.len` is smaller than `self.capacity` by the type invari= ant and hence, the + // resulting pointer is guaranteed to be part of the same alloca= ted object. // - `self.len` can not overflow `isize`. let ptr =3D unsafe { self.as_mut_ptr().add(self.len) } as *mut May= beUninit; =20 @@ -286,8 +288,8 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<()= , AllocError> { self.reserve(1, flags)?; =20 // SAFETY: - // - `self.len` is smaller than `self.capacity` and hence, the res= ulting pointer is - // guaranteed to be part of the same allocated object. + // - `self.len` is smaller than `self.capacity` by the type invari= ant and hence, the + // resulting pointer is guaranteed to be part of the same alloca= ted object. // - `self.len` can not overflow `isize`. let ptr =3D unsafe { self.as_mut_ptr().add(self.len) }; =20 @@ -793,12 +795,13 @@ pub fn collect(self, flags: Flags) -> Vec { unsafe { ptr::copy(ptr, buf.as_ptr(), len) }; ptr =3D buf.as_ptr(); =20 - // SAFETY: `len` is guaranteed to be smaller than `self.layout= .len()`. + // SAFETY: `len` is guaranteed to be smaller than `self.layout= .len()` by the type + // invariant. let layout =3D unsafe { ArrayLayout::::new_unchecked(len) }; =20 - // SAFETY: `buf` points to the start of the backing buffer and= `len` is guaranteed to be - // smaller than `cap`. Depending on `alloc` this operation may= shrink the buffer or leaves - // it as it is. + // SAFETY: `buf` points to the start of the backing buffer and= `len` is guaranteed by + // the type invariant to be smaller than `cap`. Depending on `= alloc` this operation may + // shrink the buffer or leaves it as it is. ptr =3D match unsafe { A::realloc(Some(buf.cast()), layout.into(), old_layout.int= o(), flags) } { --=20 2.48.1 From nobody Wed Dec 17 08:51:13 2025 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (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 81316213E92; Tue, 18 Mar 2025 20:14:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328865; cv=none; b=DLRQcgqszQ8C/ka5b7JikZzE2PN37ofFCT9L1OewfhCrn7VqQooCoKgXRra/n7su3XloMmtZt1rZPUpga05S24sFopkT68NDKoeNvbZbgPyWCCsu4QIlAZmY1QNB+1cFjQIMjON1yutwoYRyhZzET9so/hftEo7QgTl1yyIB6RE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328865; c=relaxed/simple; bh=7+0rzdIb3E5uC+bBhbWSE1kHLKPgCfbyaxKsl765tN4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QLnHEFPo7HWCaoA9R4dh/WWwb7azT4008GfThcxjT15VOiZfBb+8/TCRROxrTGNId0vlwnGWFmroztEXLLP/kh09DNCRyv7KDIGykHKGYN0xUke7Ll3Mo3DzJ5pOYRK9QvZr8oTHhvaruyJRYJaCYyuE2NpQnfadcdCXOvMx4ls= 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=Q+v8vXZu; arc=none smtp.client-ip=209.85.219.50 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="Q+v8vXZu" Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6e8f8657f29so47998386d6.3; Tue, 18 Mar 2025 13:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742328862; x=1742933662; 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=3Ui0Js7vP/HnV0ydOiZt9XbGjxD0E4LWZMWUPUUTT4M=; b=Q+v8vXZuntbk+zqYH9vcxefNe4ku8G8F27LeSyQnyPF2BNHwii/dJXqmKcbBPUPTIZ 1P3n/9km7TsvcBiQCALf4kGBbzzilzG8uibbsdgny8AjUqr2Zq7s8zp7ggvPLw3VtFPV Qm14izBaw8e6iO4+GYe1PyCN1qlnNB14Bq9Kd2iu3u+BvI3RglmSOkOoxtVtBqslwba8 4FnCHpvVQ4TU1vmBNqz3gT2yBYRQbRjzuc+inaWR0qsTeUvrxW2sUMfVqTGK1xtg2+WQ GUJZYV6DN1QpUsVPsJ1thahyDF0tbcVEZXU+LfZMl/5obv8h+4Li2skVcZacrmGJpN6e Q/rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742328862; x=1742933662; 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=3Ui0Js7vP/HnV0ydOiZt9XbGjxD0E4LWZMWUPUUTT4M=; b=vNUG3E+XGrBfNhzKTPS5/M3n2UET9FXxa/bx4hnyetgtmiPU5Vig3l9Vp2tPjicUgS 9wsgS8hu2Y34khjf48xR1LPosCpSm1t8qWEhNv6hbw0M74Jp0FbO1rwZe8BaqL5tiJ96 DD3Ndi0a4PKAKjM96mSNJR8MOPchMDXB1lBW2nK9VJp0ac638pPu/np1rYY3IXg0Lz68 8a2Uv0ayJKZgH1tQkGFUhgJd7xZAPoFCtsN2opoHteiLW0Q47BVGEprsunCuXvEvtqG2 LEc7WA+AC/w00oFGNiSsan4ZLrvqG6swyuGPeEEZ+FT+7mEse9/S8WGYDDfpBvW7YFZ6 wlPQ== X-Forwarded-Encrypted: i=1; AJvYcCVwKL2zZjCd78/EcXtlo4jyPacBmuBcUHak03/HQmElhHgMdawAvSbhm2MnUddMgxxCXZgQU2eTBhY+KnQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxR/vL05da600x6U5bRpqd+h1qMGXP2dm+meXfegIZVcquOKLWs uTfpB+s4R+0pSydcJEGZYyISfqQgJIfgpoyO+AXoFTaV4EOWZhg2 X-Gm-Gg: ASbGncvhzOOT71MFnvCufiG9/gSvjXPiryoRtrJUee0twYXYYRodWbB+cjUjTfzEZoC DzM1yrQ0SvUC7moeGcT/Byk9RNzrrhVIOTeqbFmudzeQXV4jR+pLoIIK+08lshAFxYowds1QX37 OQ6+0URdzIe0c7QMXxxwl1CgwzSBSZFU3NX4T7FR6n0Bbxssyp2YBN61L7IFVruaxBLk36+Fn/B q2z/3o3XXGzu68kmwBgpwYZ4goFMuN4k+87rivpMqkwruztqX+1//gwR51Mxaq0x2Tfbh7/9dH5 CYhv4hLoIOFg2X6fVaJ3GGM8xbBUXkPwgfwQO9MIlVxheWs91LIaAVMDFijQWBRAAH7c X-Google-Smtp-Source: AGHT+IH3owlftZQk1ycNvpWBiE0nuK9EkqNgNzbk0uPgTfoU1HhTlRK3YIlb66/5OuxO+c94pRsULw== X-Received: by 2002:a05:6214:27c9:b0:6ea:d69c:a22e with SMTP id 6a1803df08f44-6eb2940f29bmr2364306d6.30.1742328862311; Tue, 18 Mar 2025 13:14:22 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2620:10d:c091:600::1:e903]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade209335sm71301046d6.22.2025.03.18.13.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Mar 2025 13:14:20 -0700 (PDT) From: Tamir Duberstein Date: Tue, 18 Mar 2025 16:13:54 -0400 Subject: [PATCH v2 2/4] 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: <20250318-vec-set-len-v2-2-293d55f82d18@gmail.com> References: <20250318-vec-set-len-v2-0-293d55f82d18@gmail.com> In-Reply-To: <20250318-vec-set-len-v2-0-293d55f82d18@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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index ce58ee66c99b..97cc5ab11e2a 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -198,6 +198,25 @@ pub unsafe fn set_len(&mut self, new_len: usize) { self.len =3D new_len; } =20 + /// Decreases `self.len` by `count`. + /// + /// Returns a mutable slice to the elements forgotten by the vector. I= t is the caller's + /// responsibility to drop these elements if necessary. + /// + /// # 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()); + // INVARIANT: We relinquish ownership of the elements within the r= ange `[self.len - count, + // self.len)`, hence the updated value of `set.len` represents the= exact number of elements + // stored within `self`. + self.len -=3D count; + // SAFETY: The memory after `self.len()` is guaranteed to contain = `count` 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 From nobody Wed Dec 17 08:51:13 2025 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 C8E5A21422A; Tue, 18 Mar 2025 20:14:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328866; cv=none; b=sl4dPq6v2sOiftFOFkeIdyKfvyVzxQ7hgHcAr2evaoEmq5jLXqs1m8Yh4LGTgNZvxMOF+2SldS8pwd1WIpnnStbj8BdAuOJsnpcHndAHgHXqD1yz+hY4uipFdw4k0G8biDqlK9P2S7AJjf+cZzRGnpuiV1dVAR76Y1Xc/pH5YVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328866; c=relaxed/simple; bh=5j8T8fLbwD/lYmKw3WtoojFHPXun1QjbrGOlCZ1Nwsk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ujpxcdW6obfxZABgzrZSLk2J858s4ibVE7Bf3jBTSJfpLN38Dw0XJXAMqR/DGmb60cNtXxPHEPlmtrYL0zHdbWUKA/JsHtmfGhWtsXT3txaPsrttYUiqZphOfIGa3Q/9NTJDVkjP/2VhW7Iyq127oAke3aMiaMQqw6+K3ib4Aos= 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=jyy9uLYk; arc=none smtp.client-ip=209.85.219.42 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="jyy9uLYk" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6e904f53151so48165466d6.3; Tue, 18 Mar 2025 13:14:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742328864; x=1742933664; 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=Eu5YGVPzxTPmx7RxIoIVUx7/ZemGARjV+dgoadiAvn4=; b=jyy9uLYkqOFw97+mVUx1TtnD1DgJBiTPFVvaUu09tFNw/Oiy2Miobs7X9Mzyz4k1Zr dwTIrNCM9flqgblOx5PtoC6aJcugaec4dgy9ktpvoPO5W9K2J3YV4koNTqkx/2nS+slB bew2VABi+UQESJ9ECMXMdxWNxqrgfsOoonFHJhu+UArL06ODQOdCI7qai82cZLpfzk70 c8Y7ZAA2sqM0WgOX8cc5dBUdgJkTC0bRFvg7OLQtmdzf5X+KKI1AsmEg5kW6Ds5E9Z04 Y4Xy1AGlRzUS4jxT7JVJwRy4/GNlOlDC6j1fwfY22c3Yr4C0Qoquy+dbX1idtJn333ZL Kxvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742328864; x=1742933664; 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=Eu5YGVPzxTPmx7RxIoIVUx7/ZemGARjV+dgoadiAvn4=; b=tJPiHk8m4Z4mTTjyOJkuLAx9wJzpkFfA/bnj4fgaIbpAl0deFCsyss3gdOTGBvaYwj KyTAJhoaDlGHZb8JN4HlbyHjyAaS7QZmyIEKx2bQwYkm2z96uPAxo/5Z35d/Zetwmo2u qJfyD3pQN0LJM+VZg/OC9idt6ABeGLmQ8sWbAA8I/S9slXAeS/oCun/JbVi5TvubZ+wh xU489LN15secb5usVey8FiO3lq+JQMHBx8RnLO2BEf8G9BE8YrcVEaVlgRwO+2vHWLld zZHVjam4t4Q+UF9YOYYnKWa4CgWTzvalajjdn+YqPHSp7ErzrR1LQRcaS5pqcg/PDVCm jitg== X-Forwarded-Encrypted: i=1; AJvYcCUPqzhOvcErb/3OSZZkb+f3r7AEElFIUePC9hgU8iVT97hRGapBPhgEsfBMBT6oNVdW5NQrIlyGLcn3Fiw=@vger.kernel.org X-Gm-Message-State: AOJu0Yzg+uaQyybgaEkxgEa4PzjW04HkHlHkZ4CFH7wbZk2lak3WkzMO 4dglAXGx0vyCAVaRTPfCu2m8m6NuM2JeaOJqP+QzDlLoCgubHzWN X-Gm-Gg: ASbGnctOiIyOwP7H7I19i1oFurV4o9aVugf2+Q0e40cGlOvu2AyT4lS1k7oGz8Df4an YVJOJVv13KWqGMIsm8AqqxT413o+LDQiUN0m/ga0xBcJST7Vf4wGu+SVe9p5wvx/nMN6GP9XA8M Lkhih5FvsSVaPG7+gLE5xLcMbzIofenqhF4FFi1QGQZV6E4bzokJ4QCxFe8ipLFgLRH78RsA+s0 b5h0ynGIqhSIHPCZwDnO0/CMke43qAXERqJIVWM3Y+GNt1PhXSLgRDhP42gnEFASA/d+1dyAZYZ 0LIPOM3NklX1m4zaoPga5WRuiUJ9XVnUm5poTGo3dlnUaPhwSKLE5hqrKd6SRJRC+RFQ X-Google-Smtp-Source: AGHT+IF5HMBrmpCRvADAPabd+SHnZF9gFvMFpSy+kXHGIuXkWZrA76UwPEqMrAbNFAMIrTmOdwLK7w== X-Received: by 2002:a05:6214:d46:b0:6ea:d604:9e5b with SMTP id 6a1803df08f44-6eb29279e79mr2548976d6.2.1742328863565; Tue, 18 Mar 2025 13:14:23 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2620:10d:c091:600::1:e903]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade209335sm71301046d6.22.2025.03.18.13.14.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Mar 2025 13:14:22 -0700 (PDT) From: Tamir Duberstein Date: Tue, 18 Mar 2025 16:13:55 -0400 Subject: [PATCH v2 3/4] rust: alloc: refactor `Vec::truncate` using `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: <20250318-vec-set-len-v2-3-293d55f82d18@gmail.com> References: <20250318-vec-set-len-v2-0-293d55f82d18@gmail.com> In-Reply-To: <20250318-vec-set-len-v2-0-293d55f82d18@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 Use `checked_sub` to satisfy the safety requirements of `dec_len` and replace nearly the whole body of `truncate` with a call to `dec_len`. Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kvec.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 97cc5ab11e2a..6f4dc89ef7f8 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -489,25 +489,18 @@ pub fn reserve(&mut self, additional: usize, flags: F= lags) -> Result<(), AllocEr /// # Ok::<(), Error>(()) /// ``` pub fn truncate(&mut self, len: usize) { - if len >=3D self.len() { - return; + match self.len().checked_sub(len) { + None =3D> {} + Some(count) =3D> { + // SAFETY: `count` is `self.len() - len` so it is guarante= ed to be less than or + // equal to `self.len()`. + let tail =3D unsafe { self.dec_len(count) }; + + // SAFETY: the contract of `dec_len` guarantees that the e= lements in `tail` are + // valid elements whose ownership has been transferred to = the caller. + unsafe { ptr::drop_in_place(ptr) }; + } } - - let drop_range =3D len..self.len(); - - // SAFETY: `drop_range` is a subrange of `[0, len)` by the bounds = check above. - let ptr: *mut [T] =3D unsafe { self.get_unchecked_mut(drop_range) = }; - - // SAFETY: - // - this will always shrink the vector because of the above bound= s check - // - [`new_len`, `self.len`) will be dropped through the call to `= drop_in_place` below - unsafe { self.set_len(len) }; - - // SAFETY: - // - the dropped values are valid `T`s by the type invariant - // - we are allowed to invalidate [`new_len`, `old_len`) because w= e just changed the - // len, therefore we have exclusive access to [`new_len`, `old_l= en`) - unsafe { ptr::drop_in_place(ptr) }; } } =20 --=20 2.48.1 From nobody Wed Dec 17 08:51:13 2025 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 866902144CF; Tue, 18 Mar 2025 20:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328868; cv=none; b=UFnTUSi7jLiIDa7i1FpEw/PAyiAn1gXlC5UboSSHf34MFXjuE0U85XarsU3UApEJ8rTPmbMu41S3I3T1HpyUMuZ6rhj4v++NSbrr7XLDOEjRPvKwPDhtFgQB+UX6BzgdFjgxkXIHT5Hf7OJ9I/uyTBE5lbzyYE2JSNGCR2LpVcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742328868; c=relaxed/simple; bh=MRiEmw8syRgJRG/Ds75HKnk3JF4c0ISBvoTApwOqhZM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BZMtdGg7ty7puspPZoVW2sKmpEVVj1ERSt6KJIXCJyiQWVbggsdzUNb9Sd8DW9uzfue1UMVQWolSTLCn365GkfO/6mlwBrj/trE+aYEtbuXGhTXTbOA9gbWqw1O2a/va0vt5F21i9DS1uVKYfHn84RNtn/OmUhmNC4Zri5HV/4M= 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=lC+mtRXN; arc=none smtp.client-ip=209.85.219.42 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="lC+mtRXN" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6e8f43c1fa0so61169606d6.3; Tue, 18 Mar 2025 13:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742328865; x=1742933665; 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=zt31eb5/eW+3di5uGkFBbBmKY9ayfpvlJ8VgZ2239PU=; b=lC+mtRXNgXv44z0HILoFNd9lrgorg1RnW9LM7/fq1pjSZwG4cefMR5tqzyT3XpLguq B+PHThJ0bz/0wT4wCFKUKKzKuwEejZSwIh8a1xfGi6Zd6E0tHh4c5dq1DtrcxzEWq37k b7kvHhYuxbSRjmlVQTm8gDd746/D/nMGEioc0rMz3MG3eKW+SVhtI0o/HAqlUuABhhYt emRjppxpyuexcCjd/eRjsNf1QzjfcdjKzDqBJ4InJDIo5bk24vFIiSyBWm2lgSwmMSrQ JedD+up0if5cI+jNiOBx4HRxGRTpSuFT5nwgMNYYscpNptGSMeKIIXamFFfVTR6k1PVn QJhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742328865; x=1742933665; 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=zt31eb5/eW+3di5uGkFBbBmKY9ayfpvlJ8VgZ2239PU=; b=hja985+AGLkIqOiXO9esKKTvi/iBz/LLrIjEZ8Tm5QhoOZvx5oaZrTw8GSgAqMfy/3 krE4rDfnElKAigvDUbRSY498+EQsZ0MpKtP8Z4zI0Y3/SKnIS9hv1VpErtPpYYWUv3Qx eVyBlZmKtv9sLkC//4D9+NVP+BRJM/Ti4UNi9Pknw9lQULamxBrfQoKk4x5AOuNfWO9Z ghehPpQCTa0bMB1gSxf7dWNbWVRTUGa3Ixte6C33QdnHAmouG3FYeG+yEz5is20y2oAt oxK2WGqdXabpTvkEQi+OyFKdYtxX5wQT4Zwju6oiBlPtYxHXonkD4xhUa0dv6UpLnerm z+qA== X-Forwarded-Encrypted: i=1; AJvYcCVib9Ue8KLsE0SLd49DhjbI0GoU9CLXFsGaAAJ4Qo6K3GgNxYcIBt0a/cEAl0vJ4JeF8qoY8WPU/GMr0Mw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3Eidv0Ufs57CY2agAGhQ3PaF2oOG6v11A7y6m8XC4aEMdfQQG Ix3rpxdCEjBNYk92W7wamWq4wM++OvIhgR9krm4EdASJLFCAa+eDJbyB4KdzEco= X-Gm-Gg: ASbGncsB20IftOzgcCtZV5vZkpLJNkKvRy84iNSvBw3N2DbZwfNyH61w8T0yWlWM01b BcLO9nnmpicGhJTnPc/gz++ln7w3o+yMe7wFNiNJfh1CJAhd1eNnMKqkdka1v0XKG4T8K1BEhj1 XqPDHxgJSlC1+/0+T5bRJGhUBt2nBIj1HNCvmDeGraF7pvwOJKrQxDHEJTdzD8DikzaPmut/4OR zSqXHLNZTGs6Im/K2J2JQsO53VN346uBif5LibaVgqY9v1tHMXTwWnnD1cjWY0lyOkQ7jUzKLYY z3i0ork2oRNiPUl6Cxv8LywF4BkSBWUBWSnQjx4lbwpM8t1NR0KSuzxLLbXyLD3+Q3ws X-Google-Smtp-Source: AGHT+IEFVr+H4BqSEz6nAdfcWvKGKQWv8IjR3NqxQIf4XDtCbUUlTc1Euv+KVJst2zT/rT2ofKpVKw== X-Received: by 2002:a05:6214:2587:b0:6e8:fa7a:14ab with SMTP id 6a1803df08f44-6eb29284340mr2666136d6.6.1742328865309; Tue, 18 Mar 2025 13:14:25 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([2620:10d:c091:600::1:e903]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6eade209335sm71301046d6.22.2025.03.18.13.14.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Mar 2025 13:14:24 -0700 (PDT) From: Tamir Duberstein Date: Tue, 18 Mar 2025 16:13:56 -0400 Subject: [PATCH v2 4/4] 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: <20250318-vec-set-len-v2-4-293d55f82d18@gmail.com> References: <20250318-vec-set-len-v2-0-293d55f82d18@gmail.com> In-Reply-To: <20250318-vec-set-len-v2-0-293d55f82d18@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. Note that the usage in `CString::try_from_fmt` remains correct as the receiver is known to have `len =3D=3D 0`. Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kvec.rs | 20 ++++++++++---------- rust/kernel/str.rs | 2 +- rust/kernel/uaccess.rs | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 6f4dc89ef7f8..977f6b2e899f 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -185,17 +185,17 @@ 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. + /// - `additional` must be less than or equal to `self.capacity - self= .len`. + /// - 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) { + // Guaranteed by the type invariant to never underflow. + debug_assert!(additional <=3D self.capacity() - self.len()); + self.len +=3D additional; } =20 /// Decreases `self.len` by `count`. @@ -319,7 +319,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 @@ -525,7 +525,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(()) } @@ -556,7 +556,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