From nobody Sun Feb 8 11:45:12 2026 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (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 BC7DC1F8681; Mon, 7 Apr 2025 14:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037558; cv=none; b=jlwMeot4sHe+BhWm4LYXebIoToSqyy2xfc73ojdxZaWxA4hz+gV3k4B/NQ1b2+sR2VA7jLuAjR2D/xA7nk5UOJ8muspguYVtf8HGVwITIy8s2TbxslCO5ft40LMXu4YVDbvV9849PFFcPTkPdUBL5OK853nrRiQThypb1gZ7Yig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037558; c=relaxed/simple; bh=nCyCTUmjgvM2Ewl4LpZ8GJPzP0gVuWaJXuVsOo7zTA4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d4hBsfl9NzqZ8jxBs3l8hyUmxBRkeW4hDXpmnfPZgyph1KtO65tPsuZVHEg6YdR/eScRxsffUchXoQFLOYl1/tehZU2nYCsXBy5vM/Ev7gSzz6EjJ7tLj69bV4Kasz4ZQFXSTUWJwL1dg2XthZ/mdh6WILB9NoTOdddJu3vnXoI= 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=hTkUapXy; arc=none smtp.client-ip=209.85.160.173 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="hTkUapXy" Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-476b4c9faa2so53162511cf.3; Mon, 07 Apr 2025 07:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744037555; x=1744642355; 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=l9bXsaHdigFtnZ6oLbvWvIAc6v1jQOnqbovRbFPfFaw=; b=hTkUapXykty0H2nLYQCzyk71NdRSJ9IOP9u6ZORSGFVlTm+mgaBWWRb7IlZHBM1V4I zd7FjfbWb5zyDvnR0fkGvUvIiiNpyv0vBO/21z/OL0Jcu8Hy6YTDTUnziEDDBEaDZqu5 tg4KJR9VJkMpocBUgRYzqbSI0wBIIcLQNxKbWn9+e9UeVxZR7PiB8OsJKivBEnuAJt9z eMV9vjaEZjkuKJWhpq/bmLvg1PNcKBLhEq64m2n+2Q5xidCFnJyc9g/Pt1o9fIsjPPLu j84qYKNzI2vu+E9XqmkjZnBo9M5RW38nUJ0iduY7bL7UzhtmpRue2vqVHMIjzofsqKub 7Fkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744037555; x=1744642355; 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=l9bXsaHdigFtnZ6oLbvWvIAc6v1jQOnqbovRbFPfFaw=; b=RF9NHvSehIFtAIpcufIVW45RxYrYaEZkzMBxXf1X8vhOo1dTZL5OgK1JMZ94bkLS5i gUTOUC+W2em5UzpkdbS9oH3IWcVn1QqcBQlPSj9s8tbm5RrvRmVkBkT4va7jL156f45J NHvTpX/P+bUboRh2iYoGPUkAYf9OYfJ/WpXZfGaPTixzQp+04bQSNoXjpHYIChL/zMO3 0twQtY07MhM73JF7vJ54IvI40iszcwhI8xcql8q/viCOAgHhcExIlF9qMYoUDs10OXDX rPg21QUKyb5tKoPHKwJyUIaZNMhiISJsKQJGMBce7KbKwji5FApSAmmw5yQOddc9wJBR 3kVA== X-Forwarded-Encrypted: i=1; AJvYcCUo9/FxrLiyxuB7Rm1b1dJ0g9JoY58x7hFUdq4kG72J7c03OWDgqDw6mgrFM1tUaVLy3/rEtZ1zWqhfWyE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9U74SzKBr76ahHpvkuaaJFblQ45hZKm3q+Ubs+BpBXJ5RQZa/ K32ETVhhjheX272U6Y4Pf/APlVWlw5wzPgIpQF3FU2iv02o1mEAtDt/xOA== X-Gm-Gg: ASbGnctxh/v+nC1e66gRym2wMS4u8acsARF82aiBtxyqbmxQfXUz8hJ88fM8coyikCQ GjMCACeF6E9ElsVrl89MTLwDwugyzwIAOEPrzoxQ3/6EVwvipI0orsHVDhXWb4cIVX68IeZJFIk DGX4/GknAr7b5OJ58zYaKZeiyHCXmVF25VfdGAeLRieK2iDqnRVPFl7PdX3faV5ldn/I0SzowXP RAnEXtzHnhAN3Y+J77FeGANltESsiqPaJN92hKPGl0ZEV/SbVVHI6TKaYL84fvU6QaYUOepTKa+ lgr1EqsGV0Oxw3x0lYSU/+B66uyvGIjxjCIunn/CvoTlofPJxsKNuIphu5BDajFL6cT0KdkZNFW /Q2Tv7oPS7QCSY2rd1P7a9tMZYVSETCbJQ0t0fCEXqrU0HCWiX9bRmw== X-Google-Smtp-Source: AGHT+IF7PDCkqwUh+RUKUoekJdm4DFiDxe2Kohgw9QJDLYYkhHpRMHxvJNPDxB1wY2rC8ks4/hvQMA== X-Received: by 2002:a05:622a:1349:b0:476:9483:feaf with SMTP id d75a77b69052e-4792595a4efmr175777321cf.19.1744037555514; Mon, 07 Apr 2025 07:52:35 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:d8ac]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-479364eaa28sm33065601cf.28.2025.04.07.07.52.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 07:52:34 -0700 (PDT) From: Tamir Duberstein Date: Mon, 07 Apr 2025 10:52:31 -0400 Subject: [PATCH v3 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: <20250407-vec-set-len-v3-1-c5da0d03216e@gmail.com> References: <20250407-vec-set-len-v3-0-c5da0d03216e@gmail.com> In-Reply-To: <20250407-vec-set-len-v3-0-c5da0d03216e@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. Update a safety comment touched by this patch to correctly reference `realloc` rather than `alloc` and replace "leaves" with "leave" to improve grammar. Signed-off-by: Tamir Duberstein Reviewed-by: Alice Ryhl --- 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 7ebec5c4a277..f8045b6c8976 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 { @@ -262,8 +264,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 @@ -289,8 +291,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 @@ -822,12 +824,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 `= realloc` this operation + // may shrink the buffer or leave it as it is. ptr =3D match unsafe { A::realloc(Some(buf.cast()), layout.into(), old_layout.int= o(), flags) } { --=20 2.49.0 From nobody Sun Feb 8 11:45:12 2026 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) (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 ABB6E1F8745; Mon, 7 Apr 2025 14:52:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037559; cv=none; b=NC95IBrst7QpEEtRcSlpoua8NmcgC6gCGwp29uSicWpnkBfrN42hrBSGxD5x7CjQye2QZXoVn77li4ZbSSo5HJWN3zpIhR3Vo151BlphjZR5ZQcwKcSO8if2gf5a/A1lwwY+rucEnibJ45hfIsU9wSEJxsihlld1xRspV/w8TIs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037559; c=relaxed/simple; bh=pP/vLwj1914GL9XTfvcpv/G1iSUzs4bBTRxc+t/uqv4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rhpVPYrsHA1Y6MQrpX1IeLfsLO6ytzk2z9RjU5T6Fi4cLKQ0mmB6f8/AzhCzeFO7oT5301r5JWONEdJMF4EX8rhcRelBfuyYiyhi7Rgzmb2OOpdgBYz14ALNSzkvcgzObkOgwc0LfPuYp6l3C10bT7+jCZzWzKt5SWj0nbCSyPQ= 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=lGnVxPSH; arc=none smtp.client-ip=209.85.160.177 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="lGnVxPSH" Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-47691d82bfbso91948851cf.0; Mon, 07 Apr 2025 07:52:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744037556; x=1744642356; 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=npHOa95vpIqO5FBiS+oki9p8r42Ah9xfbMYb5nq+smE=; b=lGnVxPSHQesPMIR7f1y9eULMvHnyM1TRHTmIwTCHgmZya5aB3G8d9h79yKTq1bTMFp 9SBPuTi0gnw9UnKPrIgiXcMbivrFcTnKmZo4pbZlQhCtY4gXUbP8V+4Y0eI8g94PPGOv 8DuPiwVTdg+0G8mXMTQAZ+S1y4LC9r6ruhquupBVAQ1zp6bn7PpJHK9NyELvCtPRP75+ e8V9G5vmqQCUfKN1I9zdretMoLo8l+RA4f6Z9FrQ9x2xKkkDmbnlf25K+K6iSVTDQigL AWo+ACTQxjYnVfKYgbZBnkU0VNx51DCK4rkiwEU2owL3VBpZo8xpJh976+scHT/GAHda 2n6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744037556; x=1744642356; 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=npHOa95vpIqO5FBiS+oki9p8r42Ah9xfbMYb5nq+smE=; b=GNAqRfs+Rm3bsoalDQWcCT5LRyDX/5Y10tFLT7h/oN/aoeUh/e23UaqkdZRns2A/6c 2STvuAKi6bsIAi3FXXtKB/zQM1lc+O5qecW3c39HaeAqG6k7VUddiq0MDBshYy+afFwO hIwS29LmmNjZSY0ptcNr7wc/mTB9LoUxNlJ79caIsoEb+zj/gKud8PpwNscCcdrJrzg7 +Z81iRoJhkgJ5X9KRl7ZUafQB4GcriXQPxwlbINuNkgHwtXfA53Llri10Wd5XjByJtEv IENpSkehVuBbglWJvLF4im6/Jx0ALEjRCvvPloe6dKnArNqCfPhl7xyBB04kp2aCCaJy zIZQ== X-Forwarded-Encrypted: i=1; AJvYcCV0Qy5TiAv/u8v5BtZQyag2gDud6vJiiP1VPC4NoSeHii34ZfCCXY0i0B0WCDBFcwM27Fj6huRqKCSCoaY=@vger.kernel.org X-Gm-Message-State: AOJu0YyAfVYzJoTpcGN+rDmic0gvdHKrOofvBfaZ5zvSjYkAoMeeHwRH 18fOMaLCnH0co3S2ftksOtWnnFkIBox+qYqJ6+ne+Tpq7iZzp0Fu X-Gm-Gg: ASbGncuG/ZT7AubjLT3/CGMKMdea11T3kuhAQVybN5RUappXQdhyaU9J1PquSmIcZ4Z nlfr3XLPGIc/F7wIY/Rx9A7UqjyDq2KJSaIYJpvw90SuoluTHAlh1dpSeHW6T5uifyL9EcYOf9B +X47zg2e1To8xFr5sV1ORgIXAuZ5E3LgGduQutVTME+b2BiaKmiEpGeInGdg8Ur3agZIrHNngYq z3QAXxUy3/P1GcWoIWIa3Ol5WwwIggy23NIaOiZD6IhnJOwvV4Ib9HK8t3ZLDE/Vuuyrnll6toG 9fa22yWcnTw+2CuvZPO5IiWby4fVeJILmNSsFVz+Q3h5ibEDeAy8MbnIdK45zxWR5wq3Z1elEW7 eRXhheTXH0q/HBr39wchKvgAlNouftBfgYjqLYU7xmjDRDCXnFIpuT3tG34VQwAFZ X-Google-Smtp-Source: AGHT+IHzq7r3nFIpr5rp2jmfyHRa+T8oEFiFcMfEAD9KtU9Mmk7FCWPdy36Pnu2LTtiHZo1VVdUlvw== X-Received: by 2002:a05:622a:1a1d:b0:476:7e72:3538 with SMTP id d75a77b69052e-479249c23afmr214725511cf.50.1744037556582; Mon, 07 Apr 2025 07:52:36 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:d8ac]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-479364eaa28sm33065601cf.28.2025.04.07.07.52.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 07:52:36 -0700 (PDT) From: Tamir Duberstein Date: Mon, 07 Apr 2025 10:52:32 -0400 Subject: [PATCH v3 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: <20250407-vec-set-len-v3-2-c5da0d03216e@gmail.com> References: <20250407-vec-set-len-v3-0-c5da0d03216e@gmail.com> In-Reply-To: <20250407-vec-set-len-v3-0-c5da0d03216e@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 Reviewed-by: Alice Ryhl --- 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 f8045b6c8976..844154d310f0 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -201,6 +201,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.49.0 From nobody Sun Feb 8 11:45:12 2026 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (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 BFB3D1F8BCC; Mon, 7 Apr 2025 14:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037560; cv=none; b=MnRqExr/kOOcT6g/nCADsaILFHCizhNAug7mqrF0KaIHqxPlv53D/+DT2nK2Tvnp16j963+JkJEfGVgF1ciOeh5n3HYZ81M6p08JqluTeSHxlzOUlb8KLsejBcixMBNBxcaZBkcmEe3XDyHiEJPq7Y+QHqGPVpR2WhdhP9lHzBQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037560; c=relaxed/simple; bh=dNjzd71oIUhFnetcIZ4cjovfuK4KPL5cuUztsNo6E1o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gMX6O3+UOu7s8oNxuNQmRch20y2f5D/6BSzZtAmJs8y8K70z/N86vXkvz0VzFCjialAVDBIRPo/ymNG6XK7skK/uLMqeeGc1zEPfvJ4TRoz0RAVhy8ZoR6etQ8WTkG6M5Wkw7BlATUGimwoJq03J5DY1vtYP/t5E9CmEK/Oc7IY= 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=WZ4XqF3j; arc=none smtp.client-ip=209.85.160.176 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="WZ4XqF3j" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-4766cb762b6so45170331cf.0; Mon, 07 Apr 2025 07:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744037557; x=1744642357; 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=IUy34P+DoBoMQt3OZN3Q8KWP0NyQoRSypET5AtaZZ10=; b=WZ4XqF3jWLN2uaUv6IVszJc8ILqWMwEYiOqlJ3AjL0RAm/hW8gWxpok+m8nqeANQF3 s/6UkJfoPN81FvcQR1pONmoK9dWpzT60/QTg3VullcTBxuSuGNyN9LNh0AYZpTEjdWME oMXNB3IH4KuMq90vEWYt7N/19gy5XEdLBPhCbiXFgyIYpOUJj2wyjDZ6KqIF4sS8NKg3 BigNPnNwwz1EmDGseVN9r/fVc+Hrq2pNQcUEM1cG5NRMLrcIpmMNzBBtxu3EwbYwW0yS QeHEgVKqdoYtz0FRAO3XtdJYCjNSR95vxKrS3zwmYOKfFD492jPqbKgL+qzo7yf2lF+Y l+8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744037557; x=1744642357; 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=IUy34P+DoBoMQt3OZN3Q8KWP0NyQoRSypET5AtaZZ10=; b=jipECschJSMjvVpYTRsEARQ+ofaAXDFDpri+3B0au5P6n0nFAe2iXhQrZ+ugN9nsUG Lnx8iyATvq2en0xaoN/FiG6dsNMiRw9o5Fi3SuVuizLBUWInbZgE0eifkkQcp023EC4B yg649ssJWSLoZf2IXhWm9gsk6WOy0zaEQT+NxkSDNI/GKhM6ox70shWJE+ZqUkxZNhGG AQvCf56LKzmxRFwI0jC+/5jK0Fhqha3kB0DLsMh14kXfVDbURo9fQGpZxh286D79OPbY 7h5P9xx5ZOM0wzGQq2S6MMJeqdG/HdfdLYCW4hGCkpG/4mRHxOlAW01gafsK1foNiP7S sS9Q== X-Forwarded-Encrypted: i=1; AJvYcCUX8gXjqHnWhW567bHWRAdOANa+/Uc3L00Szf5IIiuqDNaIgbdeovZHwdqRJPR0J9BYWNzWROsv/JUus+M=@vger.kernel.org X-Gm-Message-State: AOJu0YyVOC0vNcivkMMVlIYKOeJLFOlZyDaOy10lQB22PGx09cQD1Nc/ ogkjGLVgYBCZxdJtHC5clC41o4nCjWLDZd4znyOMv2KTGYElOZQs X-Gm-Gg: ASbGncsD4jxduUmOWYzQI6im7UrjKGcdiosAhXKNgvldY/JYewg5I13G3vu+6Qx7hfg Cq05qiO2aQ0VhgahZPVEBA/NIXI9vdTbDlt9iCOM9rWkdjAun0LXqV3RyZ4seUzz2wuUdzfwgTn GS+nWxdWsM0a61iJTZZ6GXW7c3MwIl89d1yefeh1dv7TjDXJLPKYxpRX+0PkhQmBkOCFUoFpzEl CURIisqqgQFG+XWdFC2i2nfyz2Y6SdUFhBpD4Rg0K+GwaaQ/2n7N3X/wsaGpxMIv0jomcnrfgDT d62obQbEu3lSEf42OdXBwB5CX+1/Hvb1orO8JQxU5uf26hzMAR4mK1VGcQtMRmspmT8HJta+T/T vuMG5m8hokFk028JLkcVNB3d3wpEC7Pg/mWpGi+Fn7D2+w8w9DjEnGNOwtm2vX31h X-Google-Smtp-Source: AGHT+IFXXuWHPAgJppz8Bflv9yauYrd0TI9kqDg45vrypllHt7rGZH7xP7HB56ZjnhWiQKN4J9hEjQ== X-Received: by 2002:a05:622a:205:b0:471:9e02:365e with SMTP id d75a77b69052e-47924c8547fmr207883451cf.8.1744037557552; Mon, 07 Apr 2025 07:52:37 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:d8ac]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-479364eaa28sm33065601cf.28.2025.04.07.07.52.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 07:52:37 -0700 (PDT) From: Tamir Duberstein Date: Mon, 07 Apr 2025 10:52:33 -0400 Subject: [PATCH v3 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: <20250407-vec-set-len-v3-3-c5da0d03216e@gmail.com> References: <20250407-vec-set-len-v3-0-c5da0d03216e@gmail.com> In-Reply-To: <20250407-vec-set-len-v3-0-c5da0d03216e@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 Reviewed-by: Alice Ryhl Reviewed-by: Andrew Ballance --- rust/kernel/alloc/kvec.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 844154d310f0..d0b46aa36169 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -493,23 +493,15 @@ 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; + if let Some(count) =3D self.len().checked_sub(len) { + // SAFETY: `count` is `self.len() - len` so it is guaranteed t= o be less than or + // equal to `self.len()`. + let ptr: *mut [T] =3D unsafe { self.dec_len(count) }; + + // SAFETY: the contract of `dec_len` guarantees that the eleme= nts in `ptr` 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: By the above bounds check, it is guaranteed that `len <= self.capacity()`. - 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.49.0 From nobody Sun Feb 8 11:45:12 2026 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (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 4EAD41EB5E4; Mon, 7 Apr 2025 14:52:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037565; cv=none; b=ttUSQbC6zex+bEZOb7CpzvnyPd7tdvVoTG+WSINHQjUR1eMylEhIQbXPgD+9Ye4Bg5HsiRQkBAUIePQaVULFYijU8ThSfuuVmq6MHuq5krgOIoqXmhczGAMhij6oeTV8fhFVeVgl022JSbsHlGXi8lnUvj4TiE1nSQMhJKTtnMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744037565; c=relaxed/simple; bh=pCyiXCf1/A8xml47smeAz/I3a4filcF1iQCDtSStSK4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Gl9jIBaOT7thJmhDOk8L6PzhEHjMwW8ZwCTYs/0qX+/5oCmWbOSgd1lQA9OLmnlNaVzi4fDvncFazUwpfNtsdxGfUufGTgv3Wym6s9NjSwQNp+xOs0Wnd/P8MYxspTfTfhmwkznJYeIUc8w6tg2C4Bh012gAVvkLeBRcgpMma1w= 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=a+f7Mlz+; arc=none smtp.client-ip=209.85.160.179 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="a+f7Mlz+" Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-4769b16d4fbso22303271cf.2; Mon, 07 Apr 2025 07:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744037559; x=1744642359; 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=WhKqwIIgX36Jf+RJwT/l1hiIh7wr6DbCAVI5VFkdzZs=; b=a+f7Mlz+XsOYrWbEMeaXRSkERHkErm7VCobX56hU1jVwh9zElbhUsv4NWFObhyovtg ojai0satVfMB+WNp4u9/okqOvJKlDVSTFmsYy/RuD+eDRqY7MJQpU035p3qzNp9QaITW eNPSc8eigCO70GKJzb1I6uQpd2BagXvWSbxTmigr/8pwMKcKUdyX54D8RWS9dTVGj7/m y98ZJKRzLnnOcYsn0eVv6h3EzF8CWA1SITbmfBPDJ1qQMPxEKpN5//QQTSdWhXUA1SOk pxpc/germw5XHlXbhz50F2/vNYrKdFoZLijLcx1LMG1DW0drk18ZjDwSibteuMSdmX10 QaaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744037559; x=1744642359; 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=WhKqwIIgX36Jf+RJwT/l1hiIh7wr6DbCAVI5VFkdzZs=; b=rg9UKiZvqz/3qANU3IhWJtRvaC7n9Ne1kfBJTfWrugyasn8Qfl7vac62SeIxdkUCa2 kserrTnEb1bMe+8YXHhK9tReOYCNEOc4UTqm4Pnv6MSmeEG05Gf1z2xuorAhg2Qatn4i MOm/T3CIiGx5UjLJ/i2kjGm1dobqxPmxzRsalwoS9SSMIIj3umU+H+lzIvKa4oi8scAB IaqdeCs/E3w2Ha0t4Gz3m8T2TGszx3OZoGx75kqM/QewFmDIBYEEfz/4Vy98zLCDBjtT jbf1rm75ihIim4uwee4x+X3PWqLBO2ncF+1IyW4janVOUdEZWE7y+sqS1wd2/E0UMp7h fdtg== X-Forwarded-Encrypted: i=1; AJvYcCUQqUTkJhbQWkcqk8BQyZd3yv7u2MMJyDdVyc5dsXTTsCy/8fQtKblJD6fNh81nrtI6IMoe1uuYR/EMb4Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyYwGanFRAD/+FmX+DKdkVPSCWPxyhbOh6HnJ6rMKQVHPqBWn0C yE4HCxI7yzdcUEosfxw3Ioydhyl3AsK84LWIhbHikaIC6SkN2Fz/ X-Gm-Gg: ASbGncu+DeF9Jfeugk4xXA/dTL7Fjliz1+9/+HsGDB33H6jlSQ5foE1VpsU6fNpUAPJ PNhtqgsSEOosHAY9pgdSyRPLiUmoZrIwC5aW7V/pt2otmF7yprvPAGQ+HZzoTLUg+KUKYw8cJSs qn2Qrt8r0bHY1OCbMhk8elFw1LQ43w+HbiAQMRMRkcDYm1iJzwNs7llzXMn3K4o5mP5zX1hu3MN 4VNp7o0ZmmPgKb3FR9hBOiPMpVY1rTcChnXUgRDsJFGw61AflptKLMzTDt54as0Tkw4hmc65LjM daSqE15uQV1x/rjedXN3Vd3Gji4vZxCgbdeC1+tsuQ0OLSt8V2JBzHids1MPiMC0H68BEmlZpRX 1Z8iGayjBF/XL1ZtquCY3TQvNu+MkEi5RoJPCGZmsDxe6iWOKWXxdpQ== X-Google-Smtp-Source: AGHT+IFCGkkdyhhrxU14wvgTU+kI8yw4ur3kW5NZIA570Chg+1dqT1fYQbVcQ6Ews4xW5qIvvCPoEQ== X-Received: by 2002:ac8:7e84:0:b0:476:add4:d2c3 with SMTP id d75a77b69052e-47925a26c13mr197177391cf.38.1744037559089; Mon, 07 Apr 2025 07:52:39 -0700 (PDT) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([2620:10d:c091:600::1:d8ac]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-479364eaa28sm33065601cf.28.2025.04.07.07.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 07:52:38 -0700 (PDT) From: Tamir Duberstein Date: Mon, 07 Apr 2025 10:52:34 -0400 Subject: [PATCH v3 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: <20250407-vec-set-len-v3-4-c5da0d03216e@gmail.com> References: <20250407-vec-set-len-v3-0-c5da0d03216e@gmail.com> In-Reply-To: <20250407-vec-set-len-v3-0-c5da0d03216e@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 Reviewed-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 25 ++++++++++++------------- rust/kernel/str.rs | 2 +- rust/kernel/uaccess.rs | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index d0b46aa36169..fdfdb77de7f1 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -185,20 +185,19 @@ 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()); - - // INVARIANT: By the safety requirements of this method `new_len` = represents the exact - // number of elements stored within `self`. - 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()); + // INVARIANT: By the safety requirements of this method this repre= sents the exact number of + // elements stored within `self`. + self.len +=3D additional; } =20 /// Decreases `self.len` by `count`. @@ -322,7 +321,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 @@ -526,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(()) } @@ -557,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 878111cb77bc..d3b0b00e05fa 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -886,7 +886,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 80a9782b1c6e..e4882f113d79 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -290,7 +290,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.49.0