From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 32C7F1F150E for ; Fri, 21 Mar 2025 12:10:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559028; cv=none; b=pfS248DXHftm4yqTC1E22lgBrhtg6lcGSh7yU+t74fS1EnXAMbfaNURP9fIvkEXA9jtqt4atUXFPH0YmSzk0aJvED9V5wEOdCTt4YVYbsTWxoA1vAyjOhAkDYaV8iRciW/67/sri3wvnbilh0Zr8zSVqk5HRbd/pHZYXBw7SnEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559028; c=relaxed/simple; bh=HshBdY9tyPHXYCABit0wg9iizu5qxvE0QHr6aVv8M2U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Rt2RpJ67LL0wf5UL45Kl7JWw6O04j7ENK8yhyE8NQCGvM4PQMk6M4vFrpmZnW5Fg35c2bwPfiIa5CvxJfCCTYI9874rA7Nzd8w+kugIw+ylLPnn5Wz4O+IjSY23TMe1zAIu1tuqNO8bui9zMLJeO+xtvQ5IISgBpmkYlkDKbm9k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W4Qk1exX; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W4Qk1exX" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43d01024089so13929655e9.1 for ; Fri, 21 Mar 2025 05:10:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559024; x=1743163824; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RzX66093NdWDWJT+/2WCtfxfnRGCxGreLydWXedi3Vc=; b=W4Qk1exX76iZqx3l/U0VrFChNJC0cNwGPM7VV9Lo4G1hMEiIaBvQa9FMWJigTAQvCc hhluYM/nZ0+Ha+volTLs1akaIq5OlxX+t19Bwj7ktdRKv3DbFsYZNRPh5s+Cd5c1UVku wa93+9ifT/w9+8bgeieG5Xwcxkq9QjnpSzX+JPt9WOB/9/iQwVHYUReXKiOA67qThaKr Nfn/rN2RvfI5ad9fZgQvsSE3a20Z/NuwtJXVOQQGnB62oEpt+wqWjCYWlrckf9mEOFkQ qTTI12K7rSiEIOu+Ehhe56Dw7RsxQn5nmGW489y7xXawR7+WOCvvERQY/hpAZY9Z9n/I nbPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559024; x=1743163824; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RzX66093NdWDWJT+/2WCtfxfnRGCxGreLydWXedi3Vc=; b=Bt2lHRvTHHsdpRf7YrH5XNbMVOuIHiUEOSWWzbWlJD8Bajwbip15IZrp60Gafd1hPC 30i3H8HWAioiewrOFkrJ/P4Yj1cPzeNYTgoqpmJG+LbD9HGFVD3QJS5s0So+KEGUOw88 b7wEpGdkqsmHRdq3oAW8UTZXFFhw30vKEnL05+OyGcBqrodGwqzip+e9cXmeWXaZ5rZS QQbud7SijOMTKrUC4Msc5RwpP0kSS5RoXjRbjiv9Vj2rQsqvfdM1rxa6/Mpar0Gu9vWa S2gEvBEX6oXcRB0WHq0XYQ9wQWumb0sMQTAnz6vbDTpIqXbuqHiy72ScPTJcAhwOR0zt o9gg== X-Forwarded-Encrypted: i=1; AJvYcCVsrB/ACugWKAuj75jRWLBnzC+Vh65IAOT4RFL0isGbzlw9vvHzkozuurzJXmovRmqUnGB1aJPHJHtOeJU=@vger.kernel.org X-Gm-Message-State: AOJu0YzDR/DvsC4M3olKTavIlmjHYUKcSiQJCzhWvYdp6+JbvVLhE+06 w1up7NUZPMDUXCEkFIv8kIgZdsc2CzgGUyCe7iehZOKFUJyEYFGPdDA53KD3vdlBVtLFUBAXsDA +E7wNXbRTRW0rgg== X-Google-Smtp-Source: AGHT+IErPZK/k8We6HLKh56+Dy5TRrufv83EZvupyE3hnbr/ycIpO069ol3CrSRNZZquyh8P4yiRGfehrBSyACo= X-Received: from wmgg14.prod.google.com ([2002:a05:600d:e:b0:43c:f7c3:c16e]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:83c7:b0:43c:fc0c:7f33 with SMTP id 5b1f17b1804b1-43d509e4a78mr30493065e9.2.1742559024697; Fri, 21 Mar 2025 05:10:24 -0700 (PDT) Date: Fri, 21 Mar 2025 12:09:56 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1339; i=aliceryhl@google.com; h=from:subject:message-id; bh=HshBdY9tyPHXYCABit0wg9iizu5qxvE0QHr6aVv8M2U=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcmiJ7Tf2GburnJFnABJO3jD7esdEjzlSvBZ Hhni4owZEmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XJgAKCRAEWL7uWMY5 Ri9BD/42sQd6lHZxui9NajNxPApmfoRB/8zYhbcwQVPtfAsWTcoMGWW3iGfsywI7LDYSqEo04Xo qbDeXKQXhOvjfw9Z3c1Wr0ipLN1HI5ExTqApQo2NDFoM0sZqClerI7HVJ2F3H80edJDWfAaF58g az+XUfMlnM/SxX+c+sOi9ykJW/r3FIS7XkWu0Lqkmp/3FdKsLxa343rzKLLg4Iu3m0EePcFq+2F ZpV5S7sD9tz23uLT8sHnL2WaIjOAR4ONdi02ltvljwpJ8WNC+HEcgpjNuuQJGUj0DIxeJQnKnAh KD7GtyL0iUwK7RCJwh6+OvLoj3ae7pG6IO2tVdFyIc5ZuR65Ux0pNpK8cO+eKTjiwQfRvYiVRM+ W3eISMpU3yzEkjzLfpj9g/T1hZHPRt/zAIQsNFIVNpC2OoQ19+lKMzI1YD+nfzBjSlTlcGt2nP5 Gz/SRnnWr3hX1XVV+qJxTJ6PrAWBpmsvQFQyycjIBt15Ij4z0cJTN9NuTz0SD5wkm4R+T74kbl8 w+i29+oEP5Q0SdaMXs0gZlFywFO6qB096bzWEkKnxBUT/dsiIoFs7KkZk9QEphskVQSPEv9i/2V /0wxJTpftfprvjS2e7Y5opkMj4hD8Zfs/aqPFl71lxlsFF6p98Gi/fj5OU0KCdgrhNhbAHwyq+5 6xWWR0dMZuvrOLQ== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-1-6d9c8a4634cb@google.com> Subject: [PATCH v2 1/7] rust: alloc: add Vec::clear From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Benno Lossin , Tamir Duberstein Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Our custom Vec type is missing the stdlib method `clear`, thus add it. It will be used in the miscdevice sample. Reviewed-by: Benno Lossin Reviewed-by: Tamir Duberstein Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index eb6d40a1bf8ba45126bd47f1dd4a7b7ef86112c4..95e752ed27395fce72d372976b7= 4fb1b0e957194 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -395,6 +395,26 @@ pub fn into_raw_parts(self) -> (*mut T, usize, usize) { (ptr, len, capacity) } =20 + /// Clears the vector, removing all values. + /// + /// Note that this method has no effect on the allocated capacity + /// of the vector. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D kernel::kvec![1, 2, 3]?; + /// + /// v.clear(); + /// + /// assert!(v.is_empty()); + /// # Ok::<(), Error>(()) + /// ``` + #[inline] + pub fn clear(&mut self) { + self.truncate(0); + } + /// Ensures that the capacity exceeds the length by at least `addition= al` elements. /// /// # Examples --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 547EF226CE4 for ; Fri, 21 Mar 2025 12:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559030; cv=none; b=f5CPfkBykbcsyq1Zm5WHad1RiWN5e/8r310amXkNfC2HOWiepJgEXKUr+BaKvW3ezf3vIBTMvLkBmXFUEYGZWmfYxuHMYQXRqi9eC77p7K37wx8jDa+HU1ky5EAtTunYtpFtBB+SC0lwhpHOVXjPxC+6hjV0bGctX9C6Hvi9OzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559030; c=relaxed/simple; bh=TulV6My3X/Pv7lQZ0jbzI/s+3T91OC+LBGsc12t3AUM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bdAK7y3KxSi+oNLVrm2N2+C5PmDQG93Drz+7gp3UJm19JPANTv/5xu/5tF8kxCbKkng2pN7IEZF1eGNhuVG7vv0c51s5WAeCQnkZFqFS+QYw3eqlzFT1zuh72f9I1knahFYMs+2OegcIS38TIpHxIVs0EiTLEce4q74fXOh8saE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cPgKZ/yE; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cPgKZ/yE" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-39141ffa913so976610f8f.2 for ; Fri, 21 Mar 2025 05:10:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559026; x=1743163826; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mQmdkift3Su0uIdxp4srCVh0Efk/lmcsN2cxpYCwzDI=; b=cPgKZ/yEL+fYdDaJgLLFj+a+5exQh2DIrspEzHjQt2tOtE2yRJ0q4bhhwGG/D+jKYE uIuRTfB53QIvCvGVlSpxjZ8Aim0UKw7VvWnrPTbSTbbgMBFE4jA8PAiSIpkJNisOKU4D Lzj8k6QQbGW8jnQsd7BDE1JT2yXIE/e4E+xOBXrzwl60jNiCnKVMyKAYJWmHxYOJne0m QAzzmHeJ9w9KI/YR046QbRV7924A/5Va6Tm0t4y8kZfGZMiHqlE6L4J1r1eXYzlNpLsV mdG0eNmkZSX6JFPvtiSnZxICWovKCsjFbOdDdzKs3g0bPPDYth2PFvUS+J0wAABcd6Mb XS5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559026; x=1743163826; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mQmdkift3Su0uIdxp4srCVh0Efk/lmcsN2cxpYCwzDI=; b=AN9NYh4rfSF42SHRp+6z/MeDLfShr1ahENpUc0vcfa+IDmwSw7SapRSZcTlhM6Zs1e S+nICm2sS6RrqOCu2z3/cvTeqs1MFiO6M0o5fftZXAcYhzT3DZAGl/06VykIgXsA+Cg/ GlXau59TlOusHLcFo/v0/jb7OHZYbFjhj623jNopUh1Z/HhD5vDcMaTojKceAKg/iylE ofcOKGyMLdTtmtmsnCu1Fhdr7g2S6pc1Vs87DNtxDhjD2beaOtuyMkQYVAafuN/7bc7D hEiSQ5GTUNPK9/T6GMi/X3G0hAB2BCdKfkwVfcFrfMiDTGq5zKm5S+CPETg2W7xcGIWJ cM6A== X-Forwarded-Encrypted: i=1; AJvYcCUiEyGQTAQLoOGq2qGkgfJO1ERW3wLG6LKLhLlAQFBxu2QkawKzgQpqX+RYA7LQKiZA4Ml2nqtfwYeDITU=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0oEYgrprV7lGRofCTjklH6qjDnmL7TKK3X7f/hwr+JzBw3uBc //NOTqyuOd7YVKtktG2fYyELcrv7GgmhMHdjGm/QmlwrugiGCV8ng4qWjVKWl5k0vmpPpVYTKWx UGiZBArQK0FFPAA== X-Google-Smtp-Source: AGHT+IH8Dfv+WZcG9sSAF+pG+9WzdD7a2AlCaDbBDGJDmMjPJZo+A4V8E4Z1Q47z5HJ0WzxWWDOCbn14ESFYpmM= X-Received: from wmbfl10.prod.google.com ([2002:a05:600c:b8a:b0:43c:f122:1874]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2cd:b0:391:412b:e22b with SMTP id ffacd0b85a97d-3997f903a95mr3381007f8f.18.1742559026780; Fri, 21 Mar 2025 05:10:26 -0700 (PDT) Date: Fri, 21 Mar 2025 12:09:57 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1934; i=aliceryhl@google.com; h=from:subject:message-id; bh=TulV6My3X/Pv7lQZ0jbzI/s+3T91OC+LBGsc12t3AUM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcmhoW3khohXPFzA6QonRmk0NHq6nP8ON96Q kADkcTEEZWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XJgAKCRAEWL7uWMY5 RtpSD/9+sKJQlgNeJ9Yp5YwX7uyovPbbm1lkOL8QzsWVlezWA8jAxhtPtrhySh6VSLkTd+4DQbt g2NspHynxWyiV+O8KMNRjccfXs/dA3XDIJPQ0cjPjfSM2MfsHi36xiaWXf2uljpaArxkVp44SI6 eWS2dBoSi9m+P8084KZ+yQQ/HOxa1GpLjM+PiAJttvJXGNSKHDBIn7aRUB6T8mRVpeWo7cu0ZCp XQsYIlVks9/IENzEbY4qfxYAXlSudkTZYMt2nc6SS5K+qtTAJFm8F+/iB+KZ2RC26ojhoxP5kqW I/dBiWCVIxo6aUlkl4CWjx0ehgu8lWUs/GsXhyMWqcl3sSeImVYowwtVI/t5b/vlSdELcSBb1Ma s7UfU0TCy2f2SQk7SJpWTzu9+HzqulXefre8HnaF3EmOlk57Zc2nw0d/UF0AC2F+DDdAAeRnRup a5Nxl6VsfU+0jvP2GrSl8yML002eiTssCYEFX5UgTnEh+mAWoUmTTEbvt3UgiMyJXMO3Dx9t9EF 4FnUWAvMQSXFT5CkJXDOwoxjuEBPsLmZthLtUoh1FTV+5pjlMJ1XeqIyEludXxjEgcnM2GJQtA+ cTSepSzyyb2EEKmVsraebFeoQhw6hHSiSR4nBPHFo/1poMsa6aIHTMVBjCKA+ZPZhl3l06lTGx1 jupHjO3Ezqb7Hfw== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-2-6d9c8a4634cb@google.com> Subject: [PATCH v2 2/7] rust: alloc: add Vec::pop From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This introduces a basic method that our custom Vec is missing. I expect that it will be used in many places, but at the time of writing, Rust Binder has six calls to Vec::pop. Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 95e752ed27395fce72d372976b74fb1b0e957194..e42c5c40c992c796409505da709= 75f970524fc24 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -302,6 +302,35 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<(= ), AllocError> { Ok(()) } =20 + /// Removes the last element from a vector and returns it, or `None` i= f it is empty. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D KVec::new(); + /// v.push(1, GFP_KERNEL)?; + /// v.push(2, GFP_KERNEL)?; + /// assert_eq!(&v, &[1, 2]); + /// + /// assert_eq!(v.pop(), Some(2)); + /// assert_eq!(v.pop(), Some(1)); + /// assert_eq!(v.pop(), None); + /// # Ok::<(), Error>(()) + /// ``` + pub fn pop(&mut self) -> Option { + let len_sub_1 =3D self.len.checked_sub(1)?; + + // SAFETY: If the first `len` elements are valid, then the first `= len - 1` elements are + // valid. + self.set_len(len_sub_1); + + // This invalidates a value in this vector's allocation, but the V= ec invariants do not + // require it to be valid because `self.len <=3D len_sub_1`. + // SAFETY: Since `len_sub_1` is less than the value `self.len` had= at the beginning of + // `pop`, this index holds a valid value. + Some(unsafe { self.as_mut_ptr().add(len_sub_1).read() }) + } + /// Creates a new [`Vec`] instance with at least the given capacity. /// /// # Examples --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 5DED4227EBD for ; Fri, 21 Mar 2025 12:10:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559032; cv=none; b=l4RwTVALQ8bb4urFBg58fIO+m3eRZF5hXvwT7NzZ5uLTh6P2qCj5dswellB7maAqmXBRpP80fWGoGImPHKWPWPi+3PYynhABAmCjA1X+8v8i1GwOlZmuXgHJgRkGCDbP1gtErmetknlklDzhyEes69U88HzTQwE6EWketWo9tRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559032; c=relaxed/simple; bh=CenZwVOD+GrhyGqXr7eT4mqrBBg+m3/fOxMffAwc6YE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cm5VZrW+dqb0OluQ2WXIwWWksAkjBxUiLVNh3X5WZ/OMU8MhDOmr6p0nJry28xipP5mjURM+6qHbhwP8YCM04PQMMMVffjLGz54xIhwf/wokcrKIaCb01eAW2iatbMrTJcDmXw6nRvafBkDyeOu1UubVQuSVd6ZNI1qdix14ylU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gfJAZYv+; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gfJAZYv+" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4394c489babso8504605e9.1 for ; Fri, 21 Mar 2025 05:10:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559028; x=1743163828; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=8p9XCS80u4SekshtxVRT2AVnFTXfb9fXAjsyjCLRaEc=; b=gfJAZYv+MnsfkYXV1rggzulbF8oUiZ6A4iZ3BJr2qz/+2Td5UveGOe7PqeUI3DtYZw dfkn8zPXrDds/W/UDD2NS6kmUQY0xP8E1orrn9vsGemqA0eMY+RXdB0QibVM0z5HE3/H GiyDC1MR+O4KNoXahz0MMVTIQdYSjmzPq0V8Lz0kwslmJAdiwuY32qJ/5LqeMposcl/e 6dlj8X/pNVTFXMH/rgVpYLViH8/G8m0TCxgahq2eKf+UbGMvyDEOzdHzxzLRkUtLf+a5 SYpC7jwASOkdb3ShAityRq4Y6gOgDx6gmw3pYpwDU+PfA20DPMjrtbHMc/AFSh74VhEa Ctww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559028; x=1743163828; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8p9XCS80u4SekshtxVRT2AVnFTXfb9fXAjsyjCLRaEc=; b=L8cfI+wfj3uxZozW4i7mji9OdVu3hSjXlLwTvt5zOVIHKg0pB8qG+/tQ9N1Xe2lmYM ezYKj2IVI8UI9Hfcm/2UKO70RRGQ8HZ3lifktPD3HmUrsmvshf4JYVlgtoyQ8WxPKzAs FO38PT0cpYvyV95jfr3MQxJ8k4cH1pyvr+iKaXFVJkS1VNbcxX9s4lGTazqOL7RB0I6A E6lkSCO2VXOB7e9rHeDmgCq4OxiBpd0mY8cqP0jMPMCu7eOYis0QmFJvMt8+AhQdG2GS NIpL++kmDMPHslpTmVXinBWGqXLD9+GJ7K074SEi/u4qPrXW/xpEPE+ohHQuQ7hEckvI Mn4Q== X-Forwarded-Encrypted: i=1; AJvYcCXLaWVFXlEV3V305HMjj82HoappOcJxtI0gECN5x2uKuCFekV68OrbuUC8TKdv/Z55iyvPI0oR9CCZMmq0=@vger.kernel.org X-Gm-Message-State: AOJu0YxYQbDNBJbS9CpQu0/n6FycQDNyXk36tIjAWHjjXTdxTVx+MBms g35hgsAnpPokOYXQPpZ1J25YKuYDyrI6PykfIvVwR6BtPQrHXgBBAooHosI7gU6nCFLZM6t87aa 03zBaEttHctkzLA== X-Google-Smtp-Source: AGHT+IE0JzqCo4hT9QhFZy6HGmQmE5baL1LKwOCenfRXCBC0aZnS95NxOjoqopt/ytWRBePpcJcn2995SwMi5dQ= X-Received: from wmbbe9.prod.google.com ([2002:a05:600c:1e89:b0:43d:1c63:a630]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:ac7:b0:43d:abd:ad0e with SMTP id 5b1f17b1804b1-43d52a8fa06mr14331465e9.18.1742559028756; Fri, 21 Mar 2025 05:10:28 -0700 (PDT) Date: Fri, 21 Mar 2025 12:09:58 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1965; i=aliceryhl@google.com; h=from:subject:message-id; bh=CenZwVOD+GrhyGqXr7eT4mqrBBg+m3/fOxMffAwc6YE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcnRUiLDyS+/y9aPym1UGxnWYuFowsPTBhvo eunLeIjeqCJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XJwAKCRAEWL7uWMY5 RuMrD/48ZEWJt7SecaUIQ6dxxdUbGuIINRB33Cj7n9VFJ0LeTrdZwvaKHenGsqt0DUe0ISCphQ9 NxRzT3Og/mJkCXL+V76PUg7g82LwLG4HqI+KcyS47ytPTozohC/jnT0k0aNzpyEDVV9G+MzOE2q woYoD8+NGDVlbb/BqP1jSGT+vMoCXCkaWRJn2yCbKd49GEqpYpI/1hRpRwhvC6PcaeS5+7IpcZ8 yG036eNtsM72oJTCUAo28fEffERRcXp5yqwq/zn/XSsUPXETtRFsJgIMwDFEQBF2xT9qHzN2sAM oJit/chN4q19jYRBylKecR79zYABtDFZO/qCzrbYM0c+5U4bll9TIFbQ8zut9jQMiNxEMYEgNgG 7r808ZAetZ/6jrRqqGq6iUeXlv9a/Mzzrvnj1nzgAReWK//J7RQxKsyJRxlIIj7hFXy5cGm1aa+ rP0qJoIqM9EF+fB7lBbJ6UiQLA/JVzZV2Xpp/XYtOvz/AfiTYIUWLEsI53Ozgoo6lhMlidDf9/V E7rsKXbeC8SAOcLF6KoFqqsg0/itVkNT1i976l0Kc+rUTPvNz13QuenjDK42MAl/8oLIbNb0NQV +mKm9+IuO3kaBGmmezdYbaAIg91j9QP2Y2U5GOPOcdeCZ2FJFN/g3pzDMX6fDZPyLCOQu1whnaM r384qMqhlA7u49A== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-3-6d9c8a4634cb@google.com> Subject: [PATCH v2 3/7] rust: alloc: add Vec::push_within_capacity From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This introduces a new method called `push_within_capacity` for appending to a vector without attempting to allocate if the capacity is full. Rust Binder will use this in various places to safely push to a vector while holding a spinlock. The existing Vec::push method is reimplemented in terms of the new method. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index e42c5c40c992c796409505da70975f970524fc24..c9de619466eed1590fda039a291= 207100729a96a 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -284,6 +284,31 @@ pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUni= nit] { /// ``` pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { self.reserve(1, flags)?; + let err =3D self.push_within_capacity(v); + // SAFETY: The call to `reserve` was successful, so `push_within_c= apacity` cannot fail. + unsafe { err.unwrap_unchecked() }; + Ok(()) + } + + /// Appends an element to the back of the [`Vec`] instance. + /// + /// Fails if the vector does not have capacity for the new element. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D KVec::with_capacity(10, GFP_KERNEL); + /// for i in 0..10 { + /// v.push_within_capacity(i).unwrap(); + /// } + /// + /// assert!(v.push_within_capacity(10).is_err()); + /// # Ok::<(), Error>(()) + /// ``` + pub fn push_within_capacity(&mut self, v: T) -> Result<(), T> { + if self.len() >=3D self.capacity() { + return Err(v); + } =20 // SAFETY: // - `self.len` is smaller than `self.capacity` and hence, the res= ulting pointer is --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 80B1F22A4FA for ; Fri, 21 Mar 2025 12:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559034; cv=none; b=nW4fzCTEq3+ZswJjvx45us6ioLdzlUo2C60rm0xOHj4o6allIv1Ud2LOt0NR5z4lnYit4YHwoBpI9tzUwdPmWLdxgtY+EiofbEfMRHiz0VP600wWVQ4XHEA5ahRVzCXd3QHl9dZYhVjc1lpKu5ifdKsEqSFOIC7p0iY7PBODOAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559034; c=relaxed/simple; bh=6lzCJFSUMWX1AlTPPuLqtPzwOcTIE+6x81G2SG9eZx4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iCb/7rWTwLT6qkFpTpJtXPCkUoqikeBMPFJTp3M4+D/CtnCiO3unrTg+LoUXb8Pfxs9DKp2wCRoOpTHLwf7LgRrE2/Pu3hich0U00oTizD7EgBIQv18WbyK914V6urogHZEfiyKkkChF9aSLpcgtAf3CbwMChv5Pm6cI3yqSb8g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=leiY3BpT; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="leiY3BpT" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43935e09897so12755055e9.1 for ; Fri, 21 Mar 2025 05:10:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559031; x=1743163831; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NBztbKfuuo+ytlc4YEnjoWWJ1PxUABYTHC+WTNo/Y1w=; b=leiY3BpTbgul1dW1kKoHMq6HSPwfWd2c8mcVwUCh3iGYyXNKp+FJjwPF8ztN3gZbxU YljRAdpRonfPKQkid8/SY7AhgIUhYJWPQZxJFBDrKt1kheNl79pW4aKiGOTtP21PqwXw wWNnX4B87nDcWVXO3KXzmh/B7WAnJHGAbFwdVmQl+ONTLJ549Qg3tNdwFDZablpviXYW ONgjNZfHQcsgPrzQdPgUz/ZVQG/fIKPiUknvInyW8mgVZTil3I4x9kPqDPjqFbzeigjY IzML4MYGm5qIdKUJA55PRPQcNb/KlZeiv9MZ33br4tfFR2HkQ8Wxb/rqVZzRXR7lFc8G YJGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559031; x=1743163831; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NBztbKfuuo+ytlc4YEnjoWWJ1PxUABYTHC+WTNo/Y1w=; b=CMvLTMuqk9R0dDVR23fSVD6HP5VTpNtmbb3cAVOpMCBOn0s+bIzn+V8BKmIvNwE0CF uqkRE0iiA8ulpD2TDM+nT48/nZ51zyUm/XNR0YCLsKRLl22Vl9BqxCoo7NlKC6p4rOR0 ekp+c1h3fi3UMqFPdD+rBJD+4RwKk+KQZII0X8g5EqJMBcEelFKW5qBE+f+wbQMlNqoD GLQw4sgvE0D6lXBnrMo1AViaRHvWXTs9Eu/ScNg1xKTLqFRj8PTLA8iXe5LYU1TaxAv+ EHlYzd1SmpDHSOsONGPaQO3xV54SMkNWm+NnJnMiFMB+KKm5zv+EemIdzBzZyp172Cjk keSw== X-Forwarded-Encrypted: i=1; AJvYcCWkb+och4NI//8sHAOvft7784p4Y9kSBcAptOhpU+eY9vk1h3+ENCm0Cj2KpGXMv1GhdRcmVDwnxyeIR4s=@vger.kernel.org X-Gm-Message-State: AOJu0YxGy2tla4Rs2bEoKlIoAuoIHT5PyYke3+7bEljzhlV1q2u2u4m7 q2RVzf3A60lS7ZU9TiFvQg75IkMcAgiM9opm+sp/Il+frBtPdCK6jvBAo5nC2NFBo0ZObIiTt4A 1m7SzUf1AsWKNVg== X-Google-Smtp-Source: AGHT+IF65edUlhZbjCNGt6uO6OKPFramMmkd/YsaLLbYtg4M9yl1T3kNo+qR6bBWCuygeAsNqnIVt9V9KVGreYo= X-Received: from wmsp31.prod.google.com ([2002:a05:600c:1d9f:b0:43d:4ba5:b5d6]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5021:b0:43c:ea1a:720c with SMTP id 5b1f17b1804b1-43d509f6307mr30136625e9.18.1742559030874; Fri, 21 Mar 2025 05:10:30 -0700 (PDT) Date: Fri, 21 Mar 2025 12:09:59 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2989; i=aliceryhl@google.com; h=from:subject:message-id; bh=6lzCJFSUMWX1AlTPPuLqtPzwOcTIE+6x81G2SG9eZx4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcoXPTuvzLjM3mlQwcw5ajNQRpmPUqneFwoJ NpzBaM5TAaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XKAAKCRAEWL7uWMY5 RiC9D/9zTp2bu0cXiRsjGd0CNOjEPoLfPzpemTPJGzXVqh4p2WRoh0FgrO9TEES+xXODz45qfy8 VpT7/7mb6ofpz8+dZREU3iUvhs+LCO6n0dWTY9UypmCKxm5h7HKpYyq+dOFKazr+qokDVtE8Syt HtInTIlIUQaiyl+lbJRPIQ4VGdQftZRtLPQYAq4zZ4bd/qVa9kh2VeGlOp5fj8iLKfGHDi4+lzg Yf00KoUVwJiiwnBW0cO94jfBp6MwOH8Fagr74RhlUBUuQ8VaBwfxd+T5AaYMnHHT+gCVvbi//hu Um7D/7h3IRPtZ63TawMCr7ABTqVja5RsmeBPVoW9rU719uwRUdV0H3smrHbyrXYC7PxPSmjdsCk 7whcVGpF8Er2Lj6UDEQNNp4/iW9tNVQzO+mbJVlAmaQwlMqJVrD9yn/gvPHfpuQw1ue/m3Ku85d klWITTz6pvC1DINStsyOoN64yECXLwnGsNjP76rdSoQmT6XQnbe7KE4wJKN4A5e5ea8qaTRIm4d 0UfFgT/gd1hkyMMwXmoHyALkkf8sE5Z79vmhdBP755hBmo8uB2CsCaO4C9eIQa0nYrxU52V3Vvm /z2hGlZijhSQ/Bpagfhc7rk1PdarOhF0sq774ChlYg/RLbgSO0Xbbr4ZjTJE2nl8AV1fv/a2r9v NncP4pCJ9YX4M9A== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-4-6d9c8a4634cb@google.com> Subject: [PATCH v2 4/7] rust: alloc: add Vec::drain_all From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This is like the stdlib method drain, except that it's hard-coded to use the entire vector's range. Rust Binder uses it in the range allocator to take ownership of everything in a vector in a case where reusing the vector is desirable. Implementing `DrainAll` in terms of `slice::IterMut` lets us reuse some nice optimizations in core for the case where T is a ZST. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 57 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index c9de619466eed1590fda039a291207100729a96a..d16360c3156b9bb4fc57cddcfd0= 76adb70abd3c9 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -562,6 +562,30 @@ pub fn truncate(&mut self, len: usize) { // len, therefore we have exclusive access to [`new_len`, `old_l= en`) unsafe { ptr::drop_in_place(ptr) }; } + + /// Takes ownership of all items in this vector without consuming the = allocation. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D kernel::kvec![0, 1, 2, 3]?; + /// + /// for (i, j) in v.drain_all().enumerate() { + /// assert_eq!(i, j); + /// } + /// + /// assert!(v.capacity() >=3D 4); + /// ``` + pub fn drain_all(&mut self) -> DrainAll<'_, T> { + let len =3D self.len(); + // SAFETY: The first 0 elements are valid. + self.set_len(0); + // INVARIANT: The first `len` elements of the spare capacity are v= alid values, and as we + // just set the length to zero, we may transfer ownership to the `= DrainAll` object. + DrainAll { + elements: self.spare_capacity_mut()[..len].iter_mut(), + } + } } =20 impl Vec { @@ -1047,3 +1071,36 @@ fn into_iter(self) -> Self::IntoIter { } } } + +/// An iterator that owns all items in a vector, but does not own its allo= cation. +/// +/// # Invariants +/// +/// Every `&mut MaybeUninit` returned by the iterator contains a valid = `T` owned by this +/// `DrainAll`. +pub struct DrainAll<'vec, T> { + elements: slice::IterMut<'vec, MaybeUninit>, +} + +impl<'vec, T> Iterator for DrainAll<'vec, T> { + type Item =3D T; + + fn next(&mut self) -> Option { + let elem =3D self.elements.next()?; + // SAFETY: By the type invariants, we may take ownership of the va= lue in this + // `MaybeUninit`. + Some(unsafe { elem.assume_init_read() }) + } + + fn size_hint(&self) -> (usize, Option) { + self.elements.size_hint() + } +} + +impl<'vec, T> Drop for DrainAll<'vec, T> { + fn drop(&mut self) { + if core::mem::needs_drop::() { + while self.next().is_some() {} + } + } +} --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 A385F22ACFA for ; Fri, 21 Mar 2025 12:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559036; cv=none; b=Og4GAG0xy5jnw1xO8wP/YdKosa8sAeOQvVdjJViwmoXlyaFiqwe0XazwCIXh0asRNQJasLMbPCKwkqlhmf1XAQmxhY6QA1FSs5QvrqORora7mrfka67+cLL+/Zq7IZaiOoue074swHfQOGon8iM/iWEzLhQtW/UKf90S/kJfJAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559036; c=relaxed/simple; bh=hADnINVDNiY5Q5VJ37thqPwwLyvHazWkF4CZ6mUuFfI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oIhAigWy0q4+Oy04eyU+5lG0yDu8qpqTcekv9wvPaiYGonCqodl7Eti9pgjTQCV2pA6JclThk+y62nhDkr5+KjelXySik5uXqNOoHDRzEKAlC5mrhLfOA8nn9RZ7YIkwSUREvqv3LltofvdVIV7ItHe3Qm82GKnafn1xoWrkQ+Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wEI9knt6; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wEI9knt6" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43cec217977so12198585e9.0 for ; Fri, 21 Mar 2025 05:10:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559033; x=1743163833; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=m/gG0yWCnHb2fPyeO8ul+ISu228ga/KBnWNJmMzlw2k=; b=wEI9knt6t5ypzQQIFRLFtbA3GfZMmTIx/Ums9PDa6cbbfAeAvW5vpdwvNKAOffGICs 3fFXtatf4YHzIOZOMxMWvnpLhHrhRYwrAu+b8IuYJDEpvA91ib5w/DxIBMmSvwfJM9zT MkJj7W28XqWY+UXgSAUhEl+TF4ZespVqlDsIeQwAVLPJSFV9akmsAEzv2fqc0Z+FokmL tD0Es4UjXu2R0qYPD6G05qpqsJAViMCGdNYKby7o+Uz5pHugUiHWz+mYVTE7DtxdjjA1 7ZmLsG9+lJ2pxrBn1V+nDZgcPsBvzwWwNbwN7i8e3sc5QwT+51FM2xlwwBFMhRvpmXlR xgZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559033; x=1743163833; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=m/gG0yWCnHb2fPyeO8ul+ISu228ga/KBnWNJmMzlw2k=; b=anuIMOXIMfAZACNVZO7JDGy19beRD+RVNgWmiPZrFH5mHfuVp89tSiWK71J406FfHf QSkgxqy0QenX2hOhlhOyzGn49i+njq+FHvrxZVFhMNg30hAC2lE+Mt/c5Y33yU1LUCkz NsFIqjtiRzEbx0zq56E+3UpmMR5DxQz4k3Y3UtSU9KKwkd0R9ON+wCHnpXOhAUB6/eKr /McKY+ZOjlYeLubqoLrclGWb5Ybn/HrAk1KQC883wEQKBnigRzSL2pdH3beVEH543XTV ZDchsV1+Y6gfUcThHmE3G0j3hUvqp6A5+N85OTu45vhXGV2WCCluLed4serCq26ZfWmK HnQQ== X-Forwarded-Encrypted: i=1; AJvYcCVEQWEjVFEB2eKsVRXqxZayXOQzlvsacpDXCtdk8vMVLwYuTa0IqZfV3CqpT4jcQx88e6Pac2RwL22dLOU=@vger.kernel.org X-Gm-Message-State: AOJu0YyTZvzFNmmpmEjtqWfOO9iaavgmS7nbIz1bz344yrALY/S2JQ1Q hlReJ7Z5ocTwRaLiZF/kvEmywXwMjhdBav1RzzdUkU1h1O2yqhCIrk4UD3/OzCpLCZwEbAyJExz 1vU82anhziUR9uw== X-Google-Smtp-Source: AGHT+IHASaQPvbkiZRRrsAAJjN6hUNe+sQmf56CNTZ+8mw9tQbEFetUBGO3Z65CQRzZ2NQLtxKQjhg1dhv6RtZg= X-Received: from wmqa14.prod.google.com ([2002:a05:600c:348e:b0:43c:ebbe:4bce]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d8f:b0:43d:bb9:ad00 with SMTP id 5b1f17b1804b1-43d509f5650mr28824525e9.15.1742559032986; Fri, 21 Mar 2025 05:10:32 -0700 (PDT) Date: Fri, 21 Mar 2025 12:10:00 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3038; i=aliceryhl@google.com; h=from:subject:message-id; bh=hADnINVDNiY5Q5VJ37thqPwwLyvHazWkF4CZ6mUuFfI=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcoFlnD/akIF8WDUZTaYNYUbSOvCYtzimgXI 4q4bz9fnVGJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XKAAKCRAEWL7uWMY5 Rg4pD/wO2FhIXrVpiDNLmIt9cPLJvSdHY8rI/2AEeZGrHnxylYPaMQn31Bi/qIrwcgIsSwGTRFn okkhkgdd5sBfm+6i/2o8VBI1w8U5xjH7fzhFPyw5EFkjR79fesAxq2Yyhb5e1HUl5LI1Hjwcaxt 4hdIEpnUlWXP1XWGcEtU4/0uIYIQ7KZqY40t8MtJp0yo1ze7IGZElgufEiNwOT/MR9qtOLzbxYW dNjyrdO1AuNDkRCb3LltUOl0vvsFfaI3i4YqYXbIR6JvMBW/pW4m+T9HNsvt6JdDErl9/FUa5B8 4L0odGz0GKe4WV6zLx4kXBX6ivyoqjyPSYCIZZ/5F/oN+EN8Q/BEgjmYBXM1HmvDC3BgoT6P5T6 GbvA1MMlQGLw0ERLKqLyroel36uEOZZghlCCzNlIkHxr/skWc6eCcNwbvyAjF5xcCwkNRNNUOl5 ihWf4VujOcucZSevYFuOvwWiURYHah+SP0ObhKGgL08J9mkYDsm7Fc07rh6E0noE56XYOPiBN3B omezUSh4/BVPGK8E4Xld6ZRDwEWizN26X5Yx/rcQ0oXr48y9aSckvN+5LxlyMQB5E64C/PA7KmL eEbD18eZqOiQ7Gy1NGn04r4oTwAh/clUZTVlvPvKSHUwYggj/4iEmaEJTjtc0plXAqIyx+ectvb LvTlJiT/vVMMToA== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-5-6d9c8a4634cb@google.com> Subject: [PATCH v2 5/7] rust: alloc: add Vec::retain From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds a common Vec method called `retain` that removes all elements that don't match a certain condition. Rust Binder uses it to find all processes that match a given pid. The stdlib retain method takes &T rather than &mut T and has a separate retain_mut for the &mut T case. However, this is considered an API mistake that can't be fixed now due to backwards compatibility. There's no reason for us to repeat that mistake. To verify the correctness of this implementation, you may run the following program in userspace: fn retain(vec: &mut Vec, f: impl Fn(&T) -> bool) { let mut num_kept =3D 0; let mut next_to_check =3D 0; while let Some(to_check) =3D vec.get_mut(next_to_check) { if f(to_check) { vec.swap(num_kept, next_to_check); num_kept +=3D 1; } next_to_check +=3D 1; } vec.truncate(num_kept); } fn verify(c: &[bool]) { let mut vec1: Vec =3D (0..c.len()).collect(); let mut vec2: Vec =3D (0..c.len()).collect(); vec1.retain(|i| c[*i]); retain(&mut vec2, |i| c[*i]); assert_eq!(vec1, vec2); } // Used to loop through all 2^n bit vectors. fn add(value: &mut [bool]) -> bool { let mut carry =3D true; for v in value { let new_v =3D carry !=3D *v; carry =3D carry && *v; *v =3D new_v; } carry } fn main() { for len in 0..10 { let mut retain =3D vec![false; len]; while !add(&mut retain) { verify(&retain); } } println!("ok!"); } Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index d16360c3156b9bb4fc57cddcfd076adb70abd3c9..866369406ea95e68adea3668285= 52e76e451e24f 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -586,6 +586,28 @@ pub fn drain_all(&mut self) -> DrainAll<'_, T> { elements: self.spare_capacity_mut()[..len].iter_mut(), } } + + /// Removes all elements that don't match the provided closure. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D kernel::kvec![1, 2, 3, 4]?; + /// v.retain(|i| i % 2 =3D=3D 0); + /// assert_eq!(v, [2, 4]); + /// ``` + pub fn retain(&mut self, mut f: impl FnMut(&mut T) -> bool) { + let mut num_kept =3D 0; + let mut next_to_check =3D 0; + while let Some(to_check) =3D self.get_mut(next_to_check) { + if f(to_check) { + self.swap(num_kept, next_to_check); + num_kept +=3D 1; + } + next_to_check +=3D 1; + } + self.truncate(num_kept); + } } =20 impl Vec { --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 C973022B5A8 for ; Fri, 21 Mar 2025 12:10:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559038; cv=none; b=rTvg/st94OZdAgPdEYLglI9AUUsNQgydU1M1u7xooQthZm+yVXcMpPK5451BJiHiliZDFQy8p75JIGIWAfLWu7FnlY9IsMoKC8xuZj4BBQFIz0araKILKBICSBbWc5sB/mfK1ehSNt1NhM3LRMfEtUE0X+jxkiE3CY/zgl7eOzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559038; c=relaxed/simple; bh=3oJRcVdI0f2kepdQ6XQpwMxZNqeOsXeu5B+vRxaad7o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aqYxJt1qBAVv8vCsmLVZfPnZFEaawRfdM6anpS7fxzbM9oFZYaNJa4/ECZPVlU3WMCT1lYQBJHiAiWgz4bLSMySMrfTUT6sD2eI/yxyk6q7Md2rpAWK+c0CNtk6cp0ijBkPAk27vp1bYO0csjCuSTpqiGAakultIHnVRu9qF9xg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Wm3UCpH+; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Wm3UCpH+" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-391459c0395so941079f8f.2 for ; Fri, 21 Mar 2025 05:10:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559035; x=1743163835; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4ouy1jhMCEvTEa8eqKCvY5GbK3OhWh50uVrS/szpeMA=; b=Wm3UCpH+tbnbG28e/SPsRPGOp4pOs0b63MffiXDC83Fjnylco0FMglbXKL/iWXLwiz HLgawuEoAI6VpYj2XM2e6J5Jijv6yB7DLOOwkxy+F2wt/mBWvw70IDyRnbaQderuXtrM 3O1RqMvNAGT/k/QOGfCTsp/krYga1Qfd5vXaaw7nAsCNfoQGSDLSnIfsdivALOTQsMRn 7yPUE7uOg0DBF4Ao42fXildwTIC0PAa0dmBqbLQhDInhCokXk5BBAoUi5klWRcxU8BI7 NgLg3Kn0UpSwn1RvSpjlUCA8ghGxSf2MSNU60IcIVhu8nPa6HMUawhO630kH/bnv3UC6 E+1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559035; x=1743163835; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4ouy1jhMCEvTEa8eqKCvY5GbK3OhWh50uVrS/szpeMA=; b=pyjnZf6I9yj2VR9D3Z2TTXgCImFqFELFY6jsq/qA55vmpV6HbOg8f8qbYg6jXfEutu Sflhcnb+eSl1K8YMVpsjkrZJ7OCqHbtJONQHxfgwzrzlcv4SYW0dSRfvIyLOgFk8P/K7 EPwtr+49xnNkh9HhpBxXvOfaDTU+Y4DLAd23iDOr6/DD7B25mTqoKctiRfBS+RWa0iYB 025eDsFE1LFFvAhIguGo2VoHMqYLmfyjSkShzlP7gKmhMBK64Wbmcgpas5z/fKRan2oo zDnjNEBrBw5kSiwtyLAnhsXyRifQpRlziUcP1NZcCdIWSQz1P9gAcVH4IdOpvPC1jjLz 4+Fg== X-Forwarded-Encrypted: i=1; AJvYcCUg9PzBQFHbk7/NPXv22fzX6dHVKybrR+hRrFAi/3KkEi7ODaGJ17zPmjIN5FxOEEZHNbboqqrnf64FHfI=@vger.kernel.org X-Gm-Message-State: AOJu0YyaGEO5KwPtTO/AmMQc0GY9mAqOpxcmJdaoFHHMIexW3eHLBR7b t2LY42b1SeMkpNZB36dxytocIWzp3fgv7T7ABX7IxZFD1qihFbkVz33miw387vlF6CgJdAatSsm +8qQZXj1xAds2Hw== X-Google-Smtp-Source: AGHT+IFfkEvFvZCRq2yLE0H25Zi0Vl92qMU4ckTbkdDV45pE7Gz8I7/U9otzQj1IcHMd9eaM7Yw9eh8l9lcm4T4= X-Received: from wrbeh2.prod.google.com ([2002:a05:6000:4102:b0:399:7520:4538]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:156b:b0:391:3173:4fa2 with SMTP id ffacd0b85a97d-3997f90087emr2770824f8f.5.1742559035065; Fri, 21 Mar 2025 05:10:35 -0700 (PDT) Date: Fri, 21 Mar 2025 12:10:01 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2035; i=aliceryhl@google.com; h=from:subject:message-id; bh=3oJRcVdI0f2kepdQ6XQpwMxZNqeOsXeu5B+vRxaad7o=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcpMwUpKhnEtgIC8iBlUga4gWy7cNLTEDy9Z OvolKJUy9WJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XKQAKCRAEWL7uWMY5 RvLRD/0QxMH8jzWbipTt48i98CYPdCN6V72hLZVOpyXnEb9uPNF7lyyFlGe+DfpAUsZOhNcubbz caoeYr+PfBMWib8N8YiM/go8qaJjZbXVg2kQALtfozhjben/46ayX8VjHMQ/UDcnk/CsHfbi0CL bBRIO2r8XzUS0DLbCIuxf7TMQ07l1SCJgvboiADcnFGjkWWr+08RaRssIs3QaLx79VVqllmi7IT ZsVW24CXjdtpOjZG3T4md3bFi/sTElZELP0ffBHs39GlKeFeGmf54rO9A77Ctnoy6sovSMpR5oQ /ulp5Vzy9eUejmNL5ClNLpJIVAaGtKmEfNpRe57rlvA5sOY/6mH+Fe7W34yQN8Qzc17t9cNIk7T unPNyaJckgRKfL5P0cVhXFnmW6RyAJZ4GWG3sA9rsRdw0MGdrQ3+cqFpABN2xkW2RB/Z1IjoczO 1pYdqRxFrZjmMHsDqE08At94PROGbI+6icVpvU2A6EabxmeQsTDCc8BiHJ/DuTFCITs9gxhvZl5 5udvrDTrdtbfQRg5vXSDJm0Y1zcQOdFrTI2vFy8kHw8gQmVOu/hsvlmG+NK2IBF4v+QPgUXSqAt 4S+FgBORo2Ael0pw2EeV+KuXO6cTjZvnCW92C0K93JnnbJcJNhFdtmkys4aWu1X1prO0Qa9GkIM lq2yYfLXLf2W45w== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-6-6d9c8a4634cb@google.com> Subject: [PATCH v2 6/7] rust: alloc: add Vec::remove From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This is needed by Rust Binder in the range allocator, and by upcoming GPU drivers during firmware initialization. Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 866369406ea95e68adea366828552e76e451e24f..f7f7f9c650f8167ad6f53b0d83e= 328203445aa1f 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -356,6 +356,34 @@ pub fn pop(&mut self) -> Option { Some(unsafe { self.as_mut_ptr().add(len_sub_1).read() }) } =20 + /// Removes the element at the given index. + /// + /// # Examples + /// + /// ``` + /// let mut v =3D kernel::kvec![1, 2, 3]?; + /// assert_eq!(v.remove(1), 2); + /// assert_eq!(v, [1, 3]); + /// # Ok::<(), Error>(()) + /// ``` + pub fn remove(&mut self, i: idx) -> T { + // INVARIANT: This breaks the invariants by invalidating the value= at index `i`, but we + // restore the invariants below. + // SAFETY: Since `&self[i]` did not result in a panic, the value a= t index `i` is valid. + let value =3D unsafe { ptr::read(&self[i]) }; + + // SAFETY: We checked that `i` is in-bounds. + let p =3D unsafe { self.as_mut_ptr().add(i) }; + // INVARIANT: The invariants are still broken, but now the invalid= value is the last + // element of the vector. + // SAFETY: `p.add(1).add(self.len - i - 1)` is `i+1+len-i-1 =3D=3D= len` elements after the + // beginning of the vector, so this is in-bounds of the vector. + unsafe { ptr::copy(p.add(1), p, self.len - i - 1) }; + // INVARIANT: This restores the invariants since the invalid eleme= nt no longer needs to be + // valid. + self.len =3D self.len - 1; + } + /// Creates a new [`Vec`] instance with at least the given capacity. /// /// # Examples --=20 2.49.0.395.g12beb8f557-goog From nobody Wed Dec 17 10:19:46 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 216B822C35C for ; Fri, 21 Mar 2025 12:10:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559045; cv=none; b=IdRwKglLJDRBxkYoUaMsVeQmn/+9Qo5Y1cVHPUugSS/o03SDTHsT0YT0z10Kq+g4r6F9Fp0LCsjL/CGiNetT31eTjRRouuKW6nEQmbXHR2Lo3hLGuLrDnNgPWa5cGTvmYts7/T4hJzKtvg4KQ8X5d1P/thMCymYDq6iJqINUoGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742559045; c=relaxed/simple; bh=op8FOXsG8eKuiuXoYyinweSNuAafqCPjsxLJhA3CuUA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kAYxg52SiwSaSVH6LeSTOBCq1JT9nB0V1Xx4sv1vJMiL4Y0DKzvCXhbsxJcy9rGe8jGYS4S/EANBeFJOG4/VD/pNVMIXjfVnVBoYRsnk+9VKdBUQiV0b5gvTHnnwSfyImmscaittdsGQtKyg60wxsTqUxZJgEvBHdNsz3VYq0w8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PazOPxuL; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PazOPxuL" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43d51bd9b41so5664145e9.3 for ; Fri, 21 Mar 2025 05:10:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742559042; x=1743163842; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=65EXsBa9VCEM2Xc9qqeAJo/ccGDFW5pMORs8L2NFvxE=; b=PazOPxuLh5I4meT1Qb4aUQipXQyjRKmbUUx47on2b3Q2/Pf2j1rUEmmxAX3c6mzd/h A7XugF8czcuFzo8hb3sndusEQDBUe18cPBYD/pMdfhvxSPGMdW/DnckDLCGOQhuk03GR NTLqpJ31j74hsOqPYMAFRyHMWLQ16AKNhuCGZrL8bo5y6/9hmPHd5Twi8aHYfvNqDyVN hk8U0ri1pLmZXoMUBEEXxre4hWHy6JQ9Yf2QCopjpkb6fSLOmp+j7C9DgtV6EAjHyA3l FcWl0785LzaRUgkI5z9TpyLW71IvaSv7B3hy+Ca95xVnRfU23xotf3uoCs+gQqZOVLWr HAjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742559042; x=1743163842; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=65EXsBa9VCEM2Xc9qqeAJo/ccGDFW5pMORs8L2NFvxE=; b=mrLXYQcroXUJCkhkIwniP/eQCFM7iBZuRi+l6f7wTYVqA+x9LAzxBjlJYbQsfDB3MO +p1sKnqwGRPRGR3G+sWhNnJxQbhraWuE64V5+EcJ2fo9NpkQYP+trV2RaNmS0scvWnWR sHwT97dsmni457jGRzd6ywHmo5XWm8ax7m1ALZZUkdrNAbTwvbv7DP+Hc82WZslT/M5+ peBsA8EsTMYLT9QxLsOx6lTL0A+EinVAIRtuYvdq9XUVgB3F7iomOzuD6TXS5I95fqIv X8OtzTlL+QRTHmQojsSssaicsr/9n72Jmwg/2NQWAKPcLHFvgzoP506Oi79GyVI4AKL2 mGjQ== X-Forwarded-Encrypted: i=1; AJvYcCUITTvzIHL9BwlrEGEInABk7sq+Nv7yKxXqsJ0kDlIH3KR6X48qsDL0I1xkPmi7xx9DUkSJ/gfrq5/hO2U=@vger.kernel.org X-Gm-Message-State: AOJu0YxzAwcTA0Bv8pBX++p3tyGQIjPbHNhgGe2ZZt4mwcJOyK+pWu32 cP6ePy4IAfIml8qLvnq16L/DV2mnOWik+wS+k1/xrOjEIuOuFnBU1CLYSBlVD4mNRTkEc2phIVz 7J2n1zIf1B1Dlnw== X-Google-Smtp-Source: AGHT+IFOO2Wy+xG5LzSEhSafYmkXsCDVc1uW2ONz8f494sMu+TmkfaiuuJVB8qhS6G8b2lOHhyxpCsyngE6rBVI= X-Received: from wmbhc9.prod.google.com ([2002:a05:600c:8709:b0:43d:1ef1:b314]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:468a:b0:43c:ef55:f1e8 with SMTP id 5b1f17b1804b1-43d509f4983mr25528385e9.13.1742559037417; Fri, 21 Mar 2025 05:10:37 -0700 (PDT) Date: Fri, 21 Mar 2025 12:10:02 +0000 In-Reply-To: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250321-vec-methods-v2-0-6d9c8a4634cb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1923; i=aliceryhl@google.com; h=from:subject:message-id; bh=op8FOXsG8eKuiuXoYyinweSNuAafqCPjsxLJhA3CuUA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBn3VcqVJ3vDPsIBvOO+bI8RrCdXtJ8eKSCKt2ZG 1FdyCgakL6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ91XKgAKCRAEWL7uWMY5 RqJYD/9Ofti/igTSpvAQmd2uQ3JHgyZhw12A/8M4zfIZKjmDKBjUMIYVWQ2+Ag6hY3qeuupROoW ki/qGeewtku66mr5Jt3Juka3HlZTOKw1fgf0i5XdHJNZDhYa2JcoCNYarO2XM1vJi9z3HwyzO2U JzF0ocNfDS0WdxoP2/VUBGY1qd4M43cVmiZ51ikYZte2NAKw671Nlz/dNJ34moDuSKkMdfjRevy SEfQdhYPHV0GxGoLudURQ90+S8Dx1cQzflup66zMatT+eKEd5gDNWdbMjqhgf+46Ex7DtQfKLyF BGxSYSy9rUhYejv4FZh9Nt2cV1S1jjcnMFCZT27JlcGY2t1lPDr+uuRGh0Wv4X1TIpUUQ6q30U0 TKjkWNHsF5TFF16j8QqS6bcSLMN8teBXTkrn9/oW+1mrP9Sl6MRf+2Mcl4Rq0cBX64qB66L3hWi bh1ko1YxypBenbPu5CQ/K3FITQrF8v1MKff2hv5zrQEcuf4cb6QQtkb3FRIgKmOFSQHRtx20Rqo bnPfY1/3y66MGULXuOA/00/oQpMjlZWMu8kSQ7kROa5ueQ+L0UWKXU9QlRhqXZaRP/1nobASv+0 PQ9k74QrNLAKtNJ/G0wFJgHqAcvuNTuqg872hXCKibOpTOQtGlDwbQ5ou/oSaj1YPWk2d9v8Ez3 2W+zQLS11op734g== X-Mailer: b4 0.14.2 Message-ID: <20250321-vec-methods-v2-7-6d9c8a4634cb@google.com> Subject: [PATCH v2 7/7] rust: alloc: add Vec::insert_within_capacity From: Alice Ryhl To: Danilo Krummrich Cc: Matthew Maurer , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds a variant of Vec::insert that does not allocate memory. This makes it safe to use this function while holding a spinlock. Rust Binder uses it for the range allocator fast path. Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index f7f7f9c650f8167ad6f53b0d83e328203445aa1f..38ffd0cf2af6e375f8d4fc2f9af= e9295b29e7db0 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -327,6 +327,27 @@ pub fn push_within_capacity(&mut self, v: T) -> Result= <(), T> { Ok(()) } =20 + pub fn insert_within_capacity(&mut self, index: usize, element: T) -> = Result<(), T> { + assert!(index <=3D self.len()); + + if self.len() >=3D self.capacity() { + return Err(element); + } + + // SAFETY: This is in bounds since `index <=3D len < capacity`. + let p =3D unsafe { vec.as_mut_ptr().add(index) }; + // INVARIANT: This breaks the Vec invariants by making `index` con= tain an invalid element, + // but we restore the invariants below. + // SAFETY: Both the src and dst ranges end no later than one eleme= nt after the length. + // Since the length is less than the capacity, both ranges are in = bounds of the allocation. + unsafe { ptr::copy(p, p.add(1), len - index) }; + // INVARIANT: This restores the Vec invariants. + // SAFETY: The pointer is in-bounds of the allocation. + unsafe { ptr::write(p, element) }; + // SAFETY: Index `len` contains a valid element due to the above c= opy and write. + unsafe { vec.set_len(len + 1) }; + } + /// Removes the last element from a vector and returns it, or `None` i= f it is empty. /// /// # Examples --=20 2.49.0.395.g12beb8f557-goog