From nobody Wed Feb 11 07:48:19 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 B9DCB202F8E for ; Tue, 22 Apr 2025 09:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315560; cv=none; b=OhFZyN3M0VpqB6QGlj/U81UnT9rdYNSf1pXw26HcrGa4QK7alQOIrYtui4+gpw+92owDcUP/ozBFg+5sGQjC0nDWceiehi1SPB0lUFCDwrjJo4Yq2y8etzptXsx9psw3yKyThd6tkmT5a6ik8CodVooB3sTq7+34iduqg5YvP5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315560; c=relaxed/simple; bh=r/AIeXn4zZXcrGMzPV/Q2i0OF1Q5m8H0R/fE8eZv6Bk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=W9+pOJHtuUlIR4yekpQ3UVrQOGaq4/btZpN/MuTw/KRTfGBgVu4H+dDmDoLOb3mPth59FIHMl4/6plPUBlYF+uWrkodtef6ola29dhWBGaKXTvlIRUg409ttc9hDEgSZ6fh0bvqGDGrYPimq2fsorJaYyV7Jf897205WobRZ8eU= 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=J/BOk7jf; arc=none smtp.client-ip=209.85.128.73 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="J/BOk7jf" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43e9a3d2977so36956165e9.1 for ; Tue, 22 Apr 2025 02:52:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315557; x=1745920357; 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=MKNE245in3PIS3GuY7V2mOKGSA2rzg5/+glyiQQpl2k=; b=J/BOk7jfgzClxDACUcfRpH23yqXtrleUitGEy8J8sLoT94JIL994hTn1jYwa9YDD/H HgiGUHg3O0a7DLGEpktft6gUKbDvVOrtd3lpFvfeZ64du2eSMKmxYhvIEGx/bMZ0FRX4 AsN4JB3eEMwlyCEWg/zdUrtkbImbDrXdOHZoqbXR5axVkZ7dRtcx6lm9CbpJPRvmb7LT vsDm/v2KHzU58Dugk57nw3zlO+OB4sP47RTq6B/cVedkjsXfqJESQpBKyXsiGr5slcJ7 WPHtjHidyhOA2MpV5W4qumL608WatlmEFzMVku180m5nRXYdZMN6wCV0/6ukOaIJATxi uJrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315557; x=1745920357; 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=MKNE245in3PIS3GuY7V2mOKGSA2rzg5/+glyiQQpl2k=; b=JMQL5ChA+pg1ulymKDXpBxXfqFV0PbQVT2s+CzoDhSXl30L2wBL+CEp+ytqMiB8rq/ 1bY/ARIgPfnWbPLYD0Ts80LuRkDttyscYkkyIQ12hA8ze6C4yqXdY7uY4KFNiBFpBhVL 8169YbDSvISk00wR6rhAt24apbyu2hZUWIxcguWQiLd3qy3zqhEe+AxpNnWl3dreEzL4 gzxtjca1fEQ+gCgfHshtAk7ppeiM6etWpvypCimjiwdNaAK9UChx0j6UC8X5k0ZrXtpd hwc23/c9Mwr8TO5Wiy9lk6LiWoHAN76gzIas4TcdWgBG/EBw2SNmxMkuVVzvNWM4c+iC 4bAg== X-Forwarded-Encrypted: i=1; AJvYcCUCK96a240PxYKbn5Kywfczc3t3gqcoiObmF+dTqGi/6x/wZ4rcJZW66vHL5LSbjQUc9uz/iI5tl1pBbxE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzvji3dFicB5NHMJICISPbqT78tWJrGXBOlBugVXDmbsLHWFJLq 21DsW9wxVayRpLalAyGpKa4+QK+BOPAOoAcvh8t7WvSwoeQpHNcLUfNlAeWOJ1qlv1f9s4EYuL5 3DybdI4wjz7SF8g== X-Google-Smtp-Source: AGHT+IEOvPPwDs26K+TVBf3QaYIWrPHfsDsCWs9nC56jmnEiIk5mdJSQ4vTL4uHRTMDIN7Qzb1NmINrUM8IWHig= X-Received: from wmqe15.prod.google.com ([2002:a05:600c:4e4f:b0:43d:1f28:b8bf]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f8f:b0:43d:738:4a9 with SMTP id 5b1f17b1804b1-4406ac10ddcmr113156465e9.27.1745315557236; Tue, 22 Apr 2025 02:52:37 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:16 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@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=r/AIeXn4zZXcrGMzPV/Q2i0OF1Q5m8H0R/fE8eZv6Bk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bf8gjPFVfFWw2aVpXym4NSNHFohVgwUhLD/ U8fjnXKX0CJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm3wAKCRAEWL7uWMY5 RqOhEACxUGJNC7vVTyh4bmreZsz1j86f9xlUu6mPiFu7HtCToRZyqlAgDmeJ8KGnTcAu1d9AloX PEjN4o/M/XkMICYIgfWpbmAgRxV8BWb1c3PL9y8RuX1TmFF+uJIWbjW2sbFGVkbwSWo0z82nhk0 S1rcLLsokVlO1Zz+WWQ9I94T5M110/kB+iq24sMTZItiayoY+Bmm6IEtXx3FLPSpgO7KEGC1gwh DmyrCD/kaH1KoCUas/YTgii6slLoiTLLw2FNo/2z7J+jygSGeDLdXQGCof8W3jP2ED8EHh51ykc g7PSoUCn6dgtcf9oacNV+JFhuHF0j4m/jSKhmOh77UFcmcgwj2lSMbJm2XLnkj9eICA25iRcEjf Ans/fE520O/CnOTIQX9/wo6Cw1EJ5YltvQmt8usB/bEtDJ36U1Jm61/IUW0cwDdZ9c5VEZB04zj rpn2TtVfqYSFSGIJ+MdG/LJHY9BHG4qOFRMNINkJvPhZHJQgooX95eko30LX/GKINkHG4ddfozL r2afFE1sv8uDTY+seg3yo4nmvauCymNgKpxlvOG2m7nVBg63hqU1Tn5Th3+22teIEzVSnJpyMe1 7Jn0OU281oZu5sgvT100UyumXWq7l6JLfB3/YIGccQ1UqtPfVfiSfTmtH9CTlOozpHmD6Gn8m1t fN9BCRAf5xQ8ZGw== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-1-deff5eea568a@google.com> Subject: [PATCH v3 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 5798e2c890a2140a12303706578ffa2a85423167..412a2fe3ce79a0681175bf358e8= e0f628e77e875 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -413,6 +413,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.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 0194A20B20A for ; Tue, 22 Apr 2025 09:52:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315562; cv=none; b=aRq+CGTVQEXvbJpA2+GPZSZGOD4xM8F/3Zh2K+Kg3ynubr+F3FjKF7gSIKTTgmd4XDqwwT02QTNQ6jZ3lPOYWcSBrlxfUligBlYcD2vogQdPSGV1Fqp7bJN/FbdZrmTGk7+Pamk2WIjc8wwRqSzjqXs3A7MahbtsV9UtHmfuzr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315562; c=relaxed/simple; bh=3tfYucmdrXplIUc4VSHw+JRcor66PuJ268AQJ8tOuVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pwSx4WChgXZsvVda5tesBmlzs4399PcrAb9V92//bsaZX0Zec9+L+OZJ34hvXFEU7AVBYSelbHpgMQnCLDIlZcb9sC0muhJNJg/ogbLqG7z0QctFC5QjV414UnqvCx2uTWn0Ep9ChsX3a84Ic7pnE/wU34sxEI2saXTIx5T+2/s= 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=uWs/aenl; arc=none smtp.client-ip=209.85.128.73 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="uWs/aenl" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43cfda30a3cso26560495e9.3 for ; Tue, 22 Apr 2025 02:52:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315559; x=1745920359; 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=ciqTuORwuoEpSdVkNzMFiat/RoMWqsQLwZDvTI4Aq+E=; b=uWs/aenllz+y/zO6yb65dch0fxAWB5Za6bN5LdBWKgn3NCge+V3ZxDbvbMNJCWcbDd YtwSVhtoxJF3A0AjRMoN1dQfyNXtctIuVZ7ho/3m25gwD9EaNmPPpdLN5ewWRqk99OD8 sIyCbRbFFyYyUYABppeykod0yKrXYz7hwrYQV7FSEwwcIYcPOswygdlJJAB7Jj5a5WD/ l09jQe4MYB+1o99oCCZcBg6X2HHgp1M42BdjZOCkhbBaPl87giXzqmJ4jk85KdYl2KSP zlIuVeme7TF76wIYUZ0gzswO7k3ki3iYdoMkALq+UZwvVKYyqvl3RT86T86qg6GBfSbM ylYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315559; x=1745920359; 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=ciqTuORwuoEpSdVkNzMFiat/RoMWqsQLwZDvTI4Aq+E=; b=AuVXREwWlfSKHC4EkUPF7NHb/r8Uw3K7moRggr0LFGAk5JkL4tOYLIJ6hgC7behGXV 4Z+sxr+RxYIYjSaubP417yo8GcgL81I+iZVFUlDhO0JrJ4hwfbnKXLADd0xERyVc5msv livmjn/3DCCEEnorHyyyBughgmok3OltAMtUqxlyfdoOCGBXRG2ijS+rGwh/6nJhNMFf krMmv3RF815nIsZTFuzpy03lGwW2te64/81+T12FxJlAIP/dr7louO0CeDdz7g1OQbuh YUJTseqTvMUE5ZxDIs3u081JvKcETouzoVeaSXQBpGp+lOXJX1mkZed3jdEBeQEj5gv9 LFTw== X-Forwarded-Encrypted: i=1; AJvYcCVFuS1w5C6SnPUJY+vpX56zRe07Ba2TnDWgXwfal8A3CVLVc4hGuClop7ZNXSEiuFo1vs/6wpyZKDYVd7E=@vger.kernel.org X-Gm-Message-State: AOJu0YywErx1jLuz6pR34JXdiFykhYce3OhNxqT83Zu6VZ6HiqZZk1wj oBGVFPNa8AtNQ4iesd0FcXSAV3qBBelkDUQ7SXbcyiKg4WQiBrG/DzMQS4/EHRbKXj8DLTx0W0M rEgCt3tBXXfr32Q== X-Google-Smtp-Source: AGHT+IHuxz729QwBf3SD7YyBTYw4Q0wkAdfK2nCJfDLoh0jtArfMd7mAM0Ni8PdsVg1ubU3K1FB0voA3BlI67pU= X-Received: from wmbhg23.prod.google.com ([2002:a05:600c:5397:b0:43d:44cf:11f8]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3c87:b0:43d:ea:51d2 with SMTP id 5b1f17b1804b1-4406aba2c6cmr138720545e9.14.1745315559470; Tue, 22 Apr 2025 02:52:39 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:17 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=1873; i=aliceryhl@google.com; h=from:subject:message-id; bh=3tfYucmdrXplIUc4VSHw+JRcor66PuJ268AQJ8tOuVo=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bffPl/slSxUpzd0wNgPhj4xxtJEPm1Wm/zU q5dJ6NJjKWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm3wAKCRAEWL7uWMY5 RmmYD/0bypuWyiDN9HHeI0cxu3nrZoy/Lv53uCNMAo1Fl5eIKT/y/Q2dahESfTC2L/yF4kvun2Y G1spyLAUSAcAuot4RTn/CB5AWtwcBUxLVuCNFKSLtXG1LWqT7cwQqnFvOzHKxkgHmZkUyXMNtNz 61yPX9qV8eiZICiZV63HInfZ3jR0rX/fm4Qqz3WhTLibf1AVyHCJvEH/7ZFTxVzZvWRSUCBCytY KIvxxeVz8MCuUpZWvYylJIjj+jveYdC3d6qkbxfMnDYip1lRzZ1qUqAT+AdGPJ92Za9z6UxU4IE 0DgemFV3ghTTYmYt+WO1GQdMfMpDZiAtizZsfMOWhvaNaOvjKbzXZwOIXnqcE1aIjxKFaTu0JPJ 0zH4nDdn0GsBdNyCGEppKZJwbNraCl8PHHqUbHz9Uvvi9B3YMGxHGhxoZ3WlV/XDHekL340vrRW go0vWFQQddC2371LpQv2PNILrew0JF8aprM76/R60E+NG/NUV+j/QH6kx7TxY6PW+AZ/KjeN6wF cWecG42rhI+w5cGyuqpF38HXMi1BMaFcFCIDIuvGaPcdDb+J+N2b2Sjiq9lPIo9AY0a9Gpi2To+ i0LeaY43ZVy9w6Vs8uwwZtJNU/Dgfbfw/aH2YuLR9dLXJBK0AQAuE22OwnvwXvWKGQ/C7cJpc1I /xYpN/9KQDzzK6Q== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-2-deff5eea568a@google.com> Subject: [PATCH v3 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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 412a2fe3ce79a0681175bf358e8e0f628e77e875..ebca0cfd31c67f3ce13c4825d70= 39e34bb54f4d4 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -320,6 +320,37 @@ 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 { + if self.is_empty() { + return None; + } + + let removed: *mut T =3D { + // SAFETY: We just checked that the length is at least one. + let slice =3D unsafe { self.dec_len(1) }; + // SAFETY: The argument to `dec_len` was 1 so this returns a s= lice of length 1. + unsafe { slice.get_unchecked_mut(0) } + }; + + // SAFETY: The guarantees of `dec_len` allow us to take ownership = of this value. + Some(unsafe { removed.read() }) + } + /// Creates a new [`Vec`] instance with at least the given capacity. /// /// # Examples --=20 2.49.0.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 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 4FE6720C03F for ; Tue, 22 Apr 2025 09:52:43 +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=1745315565; cv=none; b=VhUijBQ8OO/OSDoGHCoSoV6BTrT2lWtnoXxTlL0nmnkCvr8MEyDPwIHGApb6cv5I0a8L5q0a7NIQQrN1tpspH73+Xg0fkxI150xKOjc4a5kDAO8aKczLrx2mSD2msGDlVzJkJjvWps/i9FsfMRgnWyyjlIfbn9ExuaHgrt310oU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315565; c=relaxed/simple; bh=Svvb6G9RffI3Cl0nzUci32sxhHJsb8RTcvqHt6zeN7A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bwDFMEm6karHrhHTIX2EqsnXKKqwchK8yjrvWDKKzglPe0qs10VIiXLp4AlgZz1ABmhYthONMTdt7MYJG8hhJUrDz+shHy+SAFbp/vxb/YPJGsObqC8r3nds8UYSljxwBp2sm8xB9iwzLSYg1b9DDRimXLFD9rB+vPxyqqm7npo= 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=U+RIQXYL; 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="U+RIQXYL" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-39135d31ca4so2055371f8f.1 for ; Tue, 22 Apr 2025 02:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315561; x=1745920361; 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=W8CL1yXQxtA+pJZQvwCMlCRVvSsYDtyq7QmVaWssZTI=; b=U+RIQXYLXnzlWEr+eBUzz9jNZnhISDqrMtfYRrb4ohFf7ER9AqLcH8JUUQLMk5nKw8 ZMD6Gp3W8tFexagHsy7fku5dWZhLqm1CwXRQrB5bQtRx8MP5iwi/7da2WTqg4nIvB/uP mXO4zfZuMMbCXtLUbByQVUJZ8MbE3HmEEfH/8MOijVrQe0S70MSqrAuPRgPMvRnR3fGE wsxhQE+z5MQH39G47uipXHhcn5NM86S6QxmYPQ18zI+ybMTeY8ocrSmmJ+62DP4w35AD y97lN/frjEXHBMi7hGDY75irB+rmttn2xPlIi2eJ/A/0A0Y8QzyjT4vvAn0Vthf8w4pb OUYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315561; x=1745920361; 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=W8CL1yXQxtA+pJZQvwCMlCRVvSsYDtyq7QmVaWssZTI=; b=RabtPnC07rH9G1hj8AnCqMWMRnIW2kQML4XslaQOR0JqpAGbfjnf1Xtp7OC7I9ucIN ZCDWVCQ62/JnTtXVjBhMrlG3hXqIhxQY0anwjBX7XavEFhTRdxWSGI/jgJSc3HaomYM1 hitEdoPAZjmsOorkqN+q3/UBY3Fcy0RHucS0P56M6vC3g7o4ER530CLSuJxKLb+wDpxE GMXgY3SQ8oTHurFEn5hMTtYTmeJjD8KqL2soy1/OEHqDJgVOlHQPPm8/DiDckqTzYeLL FBxdoYbwxci2CJrUUK0O1iDrhqByImzuasUSZ/D/j8TZxwQTZ8LiA3Xb+S+YB6ghuLOa X5tw== X-Forwarded-Encrypted: i=1; AJvYcCX154Te3fpxhVF1fuaAXUVBE/MhizOFYFcOul0SNX+J33X5SnvmlVPMWHgAX1/A+o7Ahs24IpZO4XUcX3k=@vger.kernel.org X-Gm-Message-State: AOJu0Yy32PjkTdNOSZUO8dBEgMJln6To8iVCH4Xx+v++hVozHNQrqljP PXcdfo4sHHS32F47S5s4qvepGNo0Xyuq/uyCjQZDS2RXlg5hiOfaj3y09K8LglqhXgFO9uhI85m VCCuvxHVW/HAasg== X-Google-Smtp-Source: AGHT+IFRbE7AmbHyodmXbb8GFc9/L/JqiwoJ4fe/wsfpmnT9qV+y8d/ZjbqvIcLCZBxx8Vq/Fe32tK7Itj4ll04= X-Received: from wrsg12.prod.google.com ([2002:a5d:46cc:0:b0:39a:cd99:1be0]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:400f:b0:39c:1f02:44d8 with SMTP id ffacd0b85a97d-39efbd5a27fmr11349108f8f.4.1745315561655; Tue, 22 Apr 2025 02:52:41 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:18 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3164; i=aliceryhl@google.com; h=from:subject:message-id; bh=Svvb6G9RffI3Cl0nzUci32sxhHJsb8RTcvqHt6zeN7A=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bfqUvKvbba5UFmw6yfUXkD5n17ruk/4uKwb +O/ziGnYveJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm3wAKCRAEWL7uWMY5 RmoFEAC2L84f89HGHLg0Lz1dyzkaJPXPEYHxX4wOHFdpmeni3UVoieE6QvDncQdVdY7fxZHvshT GgO0XLLXzcKrx8LlVXXH/t05lPz7zDgWKrr+Ly06G1lakTDdzCfYnKMc6Lpl9Z5RCNVRG6lq0My PeAJUz71y0Gkg5J0WONTZegW0vxyNudyP6qpkQDs8TXu/JQdp4Z8QMJv/H5oJ8k5fYWfeJgN5Bw r5y79cJ/JIPC4yndeQqEFhu75Hb01ey+mzuPclEQc98Kl54UojIxXAyUXlxJMbbFsEzv22I1yAP Cnli7Cu0AQDZCkcBDD6d4LVlOEfB/c+ETKEGlDYlA216+Bt2/T2zwSkE6qpvb7RWMqrXVJqnXc2 PILw+JkrNssRUeyjIDM5FKQdId+fzc1GXsODJvbUf2J8BRBrdimGbSdscHa2pB0qhOHbGbFSPCS GY5IF4HoEm2S8Z3OMf3X0ilLUUp5hve78b5oHLcFyjjdM0LWuzFUWisoKz4ZsFFkqlo9K5r5vkI A1RQ/ibIRTaWY86WuHeBhkyvEjlfULiKxcX7YywJWhc0OBa1MnedqpXdHt+cn+zJMXU9avSg6AW NUGvCMsEXGOlOfVt3X1c+qofNgg1mt8A20Rnzq3aAEn9Ip9kpWuJv9jXfaq9GNsxU9/LbtL4Uy/ hjCO1xwKZeiHgrA== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-3-deff5eea568a@google.com> Subject: [PATCH v3 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 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 implementation is moved to a push_within_capacity_unchecked method. This is preferred over having push() call push_within_capacity() followed by an unwrap_unchecked() for simpler unsafe. Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng --- rust/kernel/alloc/kvec.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index ebca0cfd31c67f3ce13c4825d7039e34bb54f4d4..a005a295262cb1e8b7c118125ff= a07ae252e257c 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -307,17 +307,52 @@ pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUn= init] { /// ``` pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { self.reserve(1, flags)?; + // SAFETY: The call to `reserve` was successful, so the capacity i= s at least one greater + // than the length. + unsafe { self.push_within_capacity_unchecked(v) }; + Ok(()) + } + + /// Appends an element to the back of the [`Vec`] instance without rea= llocating. + /// + /// 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() < self.capacity() { + // SAFETY: The length is less than the capacity. + unsafe { self.push_within_capacity_unchecked(v) }; + Ok(()) + } else { + Err(v) + } + } =20 + /// Appends an element to the back of the [`Vec`] instance without rea= llocating. + /// + /// # Safety + /// + /// The length must be less than the capacity. + pub unsafe fn push_within_capacity_unchecked(&mut self, v: T) { let spare =3D self.spare_capacity_mut(); =20 // SAFETY: The call to `reserve` was successful so the spare capac= ity is at least 1. unsafe { spare.get_unchecked_mut(0) }.write(v); =20 // 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. + // by 1. We also know that the new length is <=3D capacity because= the caller guarantees that + // the length is less than the capacity at the beginning of this f= unction. unsafe { self.inc_len(1) }; - Ok(()) } =20 /// Removes the last element from a vector and returns it, or `None` i= f it is empty. --=20 2.49.0.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 26ED620E018 for ; Tue, 22 Apr 2025 09:52:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315567; cv=none; b=cKg2yKV1js1SbPFIFOrlis+vY3Ts0fRmkJIR+pa0P3AQLb1ekKecFe8eYLKdghYsafJAhepWA0KyRAdeuKEUdFpY1MlcNr5+Px71WOgO7lGJ6S0EGczzWkbt6B+o3XFW0JVnKZr24o1Xc7SRldkCkD24WqOmBbA9heC5NUMgEG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315567; c=relaxed/simple; bh=8ZUC3gqd3uqi13g9pBjPEPnu/V1zgisqYYMN8IZqCUQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gLHnIErd9/N9Sg+w+ANf+Vezd+wJUf5XGXdg1MYOV8Szp85t8XqF97qPMltitGuWZRBBCb9DMHAuYE03sWvk90CpMqFqKnpVgTTA1acm9YvugJq/9SInzv2t6ylrvKCVJBSMYm4H11YGumqBpW4fIbnqSZ0NbPvLTwKaGab2Etk= 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=Ss5WTxRM; arc=none smtp.client-ip=209.85.128.73 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="Ss5WTxRM" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d0a037f97so26648405e9.2 for ; Tue, 22 Apr 2025 02:52:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315564; x=1745920364; 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=0SJu7Tdk7cfiE89XShkLgjmmcC+lkao0tEu4y6mvgi0=; b=Ss5WTxRM+gm2xXKNszSsEP8PJUbUN4cJrCp78/M0DFeR6z0LL86geOQMUxMUl/Q7fy sBSLoY4zpOx2YdqnIoGsjyjQvW9KsGR7jZ3K6hJtEb1kIBgwQDmW/c4/geLgVtd7cSMM +0kiaz6ZUYg132HZA8p7S6Qux1/rL2QFKaXZHv5O0zeqMlu2zp7KuljOnS+cQe2+//ap YRU3kZ3qZt6XYZ1otIDxfYMBTd5HAXhhDn3lwCpEEUoLF9uPtlVUHR9fCOsBWCS6Jx9y 9n+OGR6WHXXdJqOD1zDoHTxWbSe0fgPVhoF1CLpnr2uufPpRTSs6owEZ+8QGDzZprLBz ooGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315564; x=1745920364; 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=0SJu7Tdk7cfiE89XShkLgjmmcC+lkao0tEu4y6mvgi0=; b=e/pkycIyTllCwmMgjhoYeX6K8OiA04aq/PCauMfBWhTxG730kK9Z01VMvLTCv8pwG4 fPWQGxIHdTjw5pgRlQLdQNvcLSTv/pVA2/UH7edf5CSkaQy54HsV6WBN3GW04ZCS4x2p 9jNVUrZpWZnePdF9/TE5MvImCdWfyUmAp+fdVCabbrnHq6Y7oc1Ahzhs+03Irxmfrylp a+qn1NwYcq8YjI5aZFpf2WIjt0RuX5fMcQXFFLz8F69XcsnrKx6lEaMu5LYnPXLRAqhD T/PFYPi02Tt1HQnX5QV3kBksm2QWx/YakLPJksFs5Lal1mzEsr9b/5rTe17EXQ2YVLQA lnKg== X-Forwarded-Encrypted: i=1; AJvYcCX8Cv7JPBtb3j2p73VVCs+tUDIthjxeStLxJ1vIfAq1+NgMcckVcG2cIgDtVTdPw3b8esnLkHDwCOl69sA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5U7Dk0S6IE5c4CJAOLQ7r8tkbRL5em7byJ3BZlXze+xyaThl8 Us4HIPUoiE7IwBuCJth/SIgFLCEQlDy3PJbBHuQlWvBKjLHYQzOtYtXWkKwiapzR1Uix186zmDo rB25PFvGV4mDJLQ== X-Google-Smtp-Source: AGHT+IG8W+nUL5hyzHTDe2B9WWJOa3ITmAj7UJ1292j7J5WdiwVt7gfqUfJOJaiuqBKywNJpbK45nwF+fc4Z7dc= X-Received: from wmhu12.prod.google.com ([2002:a05:600c:a36c:b0:43d:847a:8ccc]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:cc4:b0:43b:d0fe:b8ac with SMTP id 5b1f17b1804b1-4406ac0fcd2mr143221025e9.30.1745315563935; Tue, 22 Apr 2025 02:52:43 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:19 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@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=8ZUC3gqd3uqi13g9pBjPEPnu/V1zgisqYYMN8IZqCUQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bgJuzKXnvdoDlLRCw8v5UFwwu/GItjknRS1 BIqHMeciLyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm4AAKCRAEWL7uWMY5 RobQEACIr6+P52rGsnxOYwogdzlcq5zYTAbzx/Vl9ibzzTkdm1PpVUJ/KJI0Icp596xBk+4Z4Z9 5/pyvQeBaT1MJcLLbjXLHN2mcOTXdZc8ZyHLLzZv8lzw1ftdnC9xUJbiGMdOnwzVF0v4b736ZW7 q0PnQ3WEVZKQTgdpfB3zMzgImObxRnyg0Y5zk5iuhB/MqupWb+1WDYo7ZTPPwCpzFQxWuLTbbIk RCyodJ4h7/wwuUMozriIAH8aHWPUsE59MfpYX3Di6EV8qs7fW4XbzW7OpitKOUc/vXa6QaPACGL U9m7TRxhUaM6gKdu+1SkFmhbG/aNQ+gZxiqGxa+6VbzVGKZxBkHWH42OhuONky6O1sNTH541DKo Qnu/KghMvHIa2cqjVUvWZ3P5lIRb7uUR7ENBH8vf0GvzgOvyhIA/iD3EfAzpT7IC3UPsmPfEOwO lkS6pREpXGWaPGIWmRMYDIxL2WFXS6I+aZdMKtJ/sQBhSh/BJgemhZH1QUutkpEVOqGqewrFzeI vmlHI44iDD1Eny5e/tQZSMYEeg67i9NC84DevdRCkl8hqg8hoKg3tveMcIlQXNIo3MpQYJ5MpMo B1vOAgmmXaCePm3pC2TG68G38x0vVt4Nb/eYyT8YaP20zBjHdUZKsh8f3zOrtk89SjBgt72Fwq/ vbPeCklS0ZBos+A== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-4-deff5eea568a@google.com> Subject: [PATCH v3 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 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. Signed-off-by: Alice Ryhl --- rust/kernel/alloc/kvec.rs | 59 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 59 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index a005a295262cb1e8b7c118125ffa07ae252e257c..4a29ca6e7dedc3e93a58830938f= 3a51619c270ed 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -583,6 +583,30 @@ pub fn truncate(&mut self, len: usize) { 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 length is not greater than the length. + let elems =3D unsafe { self.dec_len(len) }; + // 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: elems.iter_mut(), + } + } } =20 impl Vec { @@ -1070,3 +1094,38 @@ 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 T` returned by the iterator references a `T` that the iter= ator may take ownership +/// of. +pub struct DrainAll<'vec, T> { + elements: slice::IterMut<'vec, T>, +} + +impl<'vec, T> Iterator for DrainAll<'vec, T> { + type Item =3D T; + + fn next(&mut self) -> Option { + let elem: *mut T =3D self.elements.next()?; + // SAFETY: By the type invariants, we may take ownership of this v= alue. + Some(unsafe { elem.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::() { + let iter =3D core::mem::take(&mut self.elements); + let ptr: *mut [T] =3D iter.into_slice(); + // SAFETY: By the type invariants, we own these values so we m= ay destroy them. + unsafe { ptr::drop_in_place(ptr) }; + } + } +} --=20 2.49.0.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 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 4DB152036F3 for ; Tue, 22 Apr 2025 09:52:48 +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=1745315570; cv=none; b=i9aOfGiiszzPnGoCfJOIkTqcd4ZbrSRg1KKpeGjzdiDsuqMgvvvjRjNlrrAVGhuH+7sZ2fD5FxbmuQkpu/mmYIgPNNw8+bFv9stmg5EclhI4PzjosFiruZSxvbTgujnH0ci384PTfC7RVWfgun6niBUNUpbhln6bj2IVHN/naaU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315570; c=relaxed/simple; bh=mb+O7XPiHthdR+y4O3MYaMfVR53TvV7CjFTDaLnXoqM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GK0ezPNDbyXw0aR12on3mvCUlUgdV/N3zsR/e/6CFqvNoiw21wsgBPLj8Uokl0WB3XJDxrXKgZYlsLoKNhgbgateHD6nY72sJq0+UlYEN8y91NdYDreHgUR3hZdcKgfPTBVed1fK3fW/xPbqOxUhBg7T9oV9Qy+FE5dxpwuabDQ= 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=MA1LtTSG; 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="MA1LtTSG" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3912d5f6689so2746045f8f.1 for ; Tue, 22 Apr 2025 02:52:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315567; x=1745920367; 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=2DCVJ0DtabWjawqQbg/BlO9LZ4XfJJVnHx9onycFg7g=; b=MA1LtTSGtODIqERyuw3dNB6U6GbaYBdP/32sq/LZ5Fn8mYv0JdDNCvCR6q1HugAsma nFefogcXbT+V/kFqJpAeW7I0XCTMDEer+GFp5kE5RYxQHWZR9YREmT1iU6DhdXuSFP3Q U/OqW9+eJ+D+QxT1YEclwLKP4GOeacVzYn1m0U+FF5MdXZ1SVaiwhGUi5OJBSF8CTgb1 dIrI9uu4A42U2/f75vYtyRm3Qtj+MvyU1wSriRzetdEYjIqIw2lloCrjhLIprkfdphrc oIUvtGLWIw2k2uOmTtCARpj4On+eptLVTHZRhPLribKH3q2FvVVUN4EVALpvkDHOHlH1 HR5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315567; x=1745920367; 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=2DCVJ0DtabWjawqQbg/BlO9LZ4XfJJVnHx9onycFg7g=; b=anIerrx+JpqM68ZtQ/unPH7oGva+SY+m2WyOoO2s8KIFFNXbdkFHoUQhl1W89agfvB kZIioO8/Hp6GsxjwVaI9OQ69rYn1ruTnwhZxRXMeBZZ6FBhWeJB88JKD6b+rfZz2cise 95rEF8SeXDTuedP9gJi4bnaGnRTP2HVxb35tzhMxy5UPVwR6WM4ICNfuexuuHFmisQ25 mJUHiP4+ZFjYc4Ppsrg0GLT78sy7xXlWGOLBpB6C/ZkCFtrKIGQogDIOujWn6D8S5DMZ x8QRr8sfuJvNzfQXtCGTgCjcs95M9gbr7TzDnVB69zlavJoienK1gS2CkXF1Ynb74uNn NYsA== X-Forwarded-Encrypted: i=1; AJvYcCVnltaiXzmBk3b6BrbQ6aVg59krtMFy71xcvhC9aDWAYQ86hmb54QseVwLzTvolGiFkIwztGBzKJhpLxpw=@vger.kernel.org X-Gm-Message-State: AOJu0YwGjRbHiaDLSYHIMkDTvuGWfH7EM0dIL0iC70cAF3GoaZJ7lHuv vLeyOklV9AxFkSCRiWqtK57wM1BN9CiiaHYeStnOP+YHE85I3+4PoY3kxiKqMoZQX4t/b3Ynpbu 1cU1sV68/Z72Lzw== X-Google-Smtp-Source: AGHT+IEjIs1PP1IgSTDnsDg2V0e4IMM0C96a3E5gUuieuIiK+z1Yx08h5EmsDcyABmJfcB1dZ3l6N/RlKRYDPYY= X-Received: from wmbfs18.prod.google.com ([2002:a05:600c:3f92:b0:43c:fd99:1b7e]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:188b:b0:39a:e71d:ee34 with SMTP id ffacd0b85a97d-39efbad2b2emr9931349f8f.37.1745315566643; Tue, 22 Apr 2025 02:52:46 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:20 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3177; i=aliceryhl@google.com; h=from:subject:message-id; bh=mb+O7XPiHthdR+y4O3MYaMfVR53TvV7CjFTDaLnXoqM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bgzL2YXfnjsb+0grk8LTEfvaNgSyI9SVyF0 M3uPkRGogaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm4AAKCRAEWL7uWMY5 RlCcEACPjIjx9RDJ7vcD8aDcHdH+cnxFQBPXi1fTrPvF1JcMsKLVcE6ZQsN9ceI0QdlUlZ8iPt9 a2IYIFDvB+AEBCNRtfgiA1X11ecn8/425kj74k0jXjbhoiwY16VCdAuisZSAHoJxfmdkHzWzXKz Tu8gDGfNehpTvFCoY2rq+qJY4N0QyCt99bziUndoj3FYysy4DthA2t15OAWwFg2QO+2rOxno8gY EyfxfCsSiqyV0xC/qkUT0Ct24d0Y5rCTISRV74VQfPgEM3Jq92O7GATpFtSt1gtfwlCXMemkkTK V+dHgtcKP+HjXiSrkO0zJSVyxvXOK+ixsXWD4icGA4/ZEw9gCkzHrLH0LNUhx2RPnlLrkZdc2Jn LUURUMPXoXjqsnZonpAmL6r0SKHn+bmzjZzkq7tr5UidyjUg2T7lUBJE8lXvw+EAFubER/AU/Q7 YZAcFIAG3ZSChkFdcKmSFlwikvVI75EPVG3j5RmZKoEi5FOOYAV5R3+UrjxHndksq+DBtx7jzTr xeOrr++aF4i534kqOYKuRPNANZKZ57i0m3WZ+Xbxz2I0EMWruKwp4D55mVKFKHWUcbhdSUL6XtL FnqT5aH4ek0FPBWys1IO4kfxF0b4nIvGrnPSBnpFAGA0/fBQTh+/6oovLjHj4eCh/LmooE3Ut6g kh2Np/Qp5iGp1VA== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-5-deff5eea568a@google.com> Subject: [PATCH v3 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!"); } Unfortunately this can't be a kunit test because it uses the upstream Rust implementation of Vec::retain as a comparison, which we can't call from a kunit test. 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 4a29ca6e7dedc3e93a58830938f3a51619c270ed..2f894eac02212d15d902fe6702d= 6155f3128997c 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -607,6 +607,28 @@ pub fn drain_all(&mut self) -> DrainAll<'_, T> { elements: elems.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.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 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 74D65214228 for ; Tue, 22 Apr 2025 09:52:50 +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=1745315572; cv=none; b=S5mctCQNyOc0URUR9nM1qMLSNRtumlfFwxdD1xbK8HKKFRTGBT9aa1wA/wY92xxLq3RKXWtI7wynC9Xj4U+++b77j/w8NszzK3D3rF6KvffObweWAsELYWpocTgPzPYtsnzozYtUAPYS1u/Bpsn//8kwDM38RJ0dA/sdfq3Y33k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315572; c=relaxed/simple; bh=GWW1hjokZB4jc7H3QwDWktdHZ8wac6bK/vXMX0acnE8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PIAAG4Mb5jDfEBFYm4NdkXOy1W03ACQiQzSqdLNlQLffZd5a6QSSYyC6ZTzOa8KHez5twMtASkRgM1oY4O00jqBnnO++GPE2eHYjt+wowbLQAxYJvLyNS4e0op/8Q8/GUUgBB/HV20zDlcHIJNsdban89ThL5UAM5Rj2QLiY4BA= 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=t4NaX2hk; 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="t4NaX2hk" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-39c1b1c0969so2861972f8f.1 for ; Tue, 22 Apr 2025 02:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315569; x=1745920369; 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=n1b7J+UNfonWdLeszOqonYjrmWvS3LamWs4ixsdchvY=; b=t4NaX2hksZsE2vCgNOyE2CplJ+1KoQ9L8VBpbpwhgQfi/MQ4dpma8veDqRBJwyTBJj DfUm6Brhv27eETa5GWLZmWeqU9szlYULSi0RwI6ctLqbQlZaMPIRkTVUfO+PUn+UU+By UxMr1OxG3VgocoT6xmrmCEmBXbd69euP66b1bnmQ/sEfw/59lVSRX+GCCtKqtcffrZKN ruG1nPFUEekLFFL+emdmeyAmCSJNcEG7mRRXCoGJG+iHZLocES0AVrTSNQOpBsOpdhQi 1mrWeItDdzF44+aASLkTm0pnIfTIvjYnHXjhXEwVSC55u0HoYIJfBKFXs//k1OEHS4mk yHRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315569; x=1745920369; 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=n1b7J+UNfonWdLeszOqonYjrmWvS3LamWs4ixsdchvY=; b=Ymk8/iXZpwO8rLnwHxPKGTBeEF3AJcN70QBoWQ5Zdx/VHF0CEojsQuO5We5BNZE/FH UzyxK/hglLFuXZ7MB8dLD1MlyfPObpfwp7YuifnTNdX3G0ewM997m8l//c0STHOxkCx1 D/ESHZgUeltZysdQKjrmKeCH6jy/OUZ6bLQNCPL097a3mP+kfR41CeZKCLOodzzejPYn 30Eo7spwFWg61OHuQbc3ypFSIzOdkYvpLzwXVwfGWtPMgAeZAjjfUK4vaJkAHsfC+XLV aS+hSmBPdZ1BUgnv3b1lYpeHEDGBkOt/CeTd18Eaxh4CVSlWb1patmjcpjAgi6bLUzAh 4Z9g== X-Forwarded-Encrypted: i=1; AJvYcCXEl09VxSj+Btm2N9FnDfpsKiLJPe3ZsfrsUEpfchBXH7WBvFsFsoxTsvnbTKj+RqP3/msH+d+E8dcN9tw=@vger.kernel.org X-Gm-Message-State: AOJu0YwUO43+i969S9wRcyw8x2rYHOiDzXyljEblgPJoiyBEFZ7FdLgq X3Wsl6Opqi62la9/iOZgzzNEDO8IU7r0/UOq1drixPUB8YO4izzk7kyEqdV5oJTIwoW3P+zsg8Z kUznT3A2exP3beQ== X-Google-Smtp-Source: AGHT+IH+cVV9twzU1vHTbSct4nZOgZGmFSdJykADxGQWVUDv4tibjvkEOH1AsMUFz6pZR7bdKz+sIniR61DT3yk= X-Received: from wmbfo18.prod.google.com ([2002:a05:600c:6912:b0:43d:522a:45e8]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:40de:b0:39e:e438:8e32 with SMTP id ffacd0b85a97d-39efbb09208mr10899224f8f.55.1745315569002; Tue, 22 Apr 2025 02:52:49 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:21 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2030; i=aliceryhl@google.com; h=from:subject:message-id; bh=GWW1hjokZB4jc7H3QwDWktdHZ8wac6bK/vXMX0acnE8=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bgmj5mzsyklfjuCQ+MElH2I0ssL9Murx59B 0pcUIgBWoeJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm4AAKCRAEWL7uWMY5 Rj/UEACM0Wb6Q5fhB3bzfKaeO8kiKdbbrZnecj0/MDszzWQ9m8CIWhg/jrtNZ2aQOeaA9sOHDNu pXubzPjWoOcGJkc7iUPwxmsKBR8Hr2WFboblw7S2FLxH3+9MTCA8VDZnK6nJUz2HymrHJD71Tw4 FW5NdnKStp3dNezeDIqXyYrL2vlBN/tgqwhllmUMU7EnFLNxg+DFbeUv7MVqN8Rv1suaQlgjqaq EZNpWcD0/LgzBWnBoJlV/EdUGWYpkVJ0OsfnHdrXQM13/Lo3Dv/20gRhwyP+YWhmys3YosJHgjg HsjRL5kCwnf75pquh9MZK4kXHOcp5+6J/+CjRmVQ80+deCLc296cXoUp4deE5ddJ0TvPjVz5tbL 5cQPxPaf2npNTehftZmbiJ2Q45n4Oxp+DHGQj6KvatWoL6rMaFtmyMpk20bow1uJ9ZVLDLuhzyX KDxyRwneSlcaITfFauYRDvZvFEMKQA36FYwB9FLcN7/qQvo4uQgzPzWv1+bONDH9F0M/M6qcDDo 0tTDR0AH/EdWIZ5SAL3/jk6NqHMoLStsmJYabdPPrMBkpH7COmhezygm7mFnikgqiOJZKXoOSFa O237mA0LVuJKJzWj/IXTYBOCy0VLinghXokee6+suCUPN2f5vZG4hH2p/mBjfPQx1mhs6KyQ2fu uVQOEjWJGeIrE1A== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-6-deff5eea568a@google.com> Subject: [PATCH v3 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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 2f894eac02212d15d902fe6702d6155f3128997c..2f28fda793e13841b59e83f3468= 1e71ac815aff2 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -386,6 +386,37 @@ pub fn pop(&mut self) -> Option { Some(unsafe { removed.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: usize) -> 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: Since the above access did not panic, the length is at = least one. + unsafe { self.dec_len(1) }; + + // SAFETY: We checked that `i` is in-bounds. + let p =3D unsafe { self.as_mut_ptr().add(i) }; + + // INVARIANT: This restores the Vec invariants by moving the valid= values into the region + // that is required to hold valid values. + // 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) }; + + value + } + /// Creates a new [`Vec`] instance with at least the given capacity. /// /// # Examples --=20 2.49.0.805.g082f7c87e0-goog From nobody Wed Feb 11 07:48:19 2026 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 4EE52214A9B for ; Tue, 22 Apr 2025 09:52:53 +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=1745315576; cv=none; b=uSGKyokjunS8KvLcKVPUzJFnpIOrTA2WPVDp/wvyITE1jBvk9erBC3yrYUI73WAS1XYetCUEFiLg9mof5DDkosBf5vqZRZRckYdrGedJFrFhLSiJKvdLAigSavfdggUUM234kieY3TTpXgg8+Y50uM7SY2qjEI1mu/CjaK4INGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745315576; c=relaxed/simple; bh=KAoN/I1B9bjZf9yqrwjjTVg4XFhQinQf/p7dHQd7M6I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=J+gv+Bg97uRYqf9SuzGUhBzrUzTeinpRzsOAd61Tx1G5z6ASJx2QRL0Md1xmq/0vcYFAnvWSx69vGoUthq5Mc59SNEIl3ou5GhOImFV5/DqsSpgcInSIT+8dHVFKvHcb28DH6wvt0DHclnAaAVHGEymonXe6jxAsT1hsWnpzIkc= 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=Z4r2FX/4; 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="Z4r2FX/4" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-391492acb59so2291458f8f.3 for ; Tue, 22 Apr 2025 02:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745315571; x=1745920371; 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=HKQLzqoWG+gLW7RSIJ9nPWEIDBNLgsckjy8Twrs/QCY=; b=Z4r2FX/4FZlIbgq/UpjIN1noeVta/fXs6gUJqRj/Hyv0WvVWO114sq9QXLPqCrhOZm 1vsv2G98VDq4WovgMOVuwVGI+S3flf5UX/o6bCWxiAF5iN5+QsUWh8YBtO2y07z3zVY3 rCitTgO9cX/+4/qjwf4x2Rp+UzQUal4Yg0L/DnhCFyNHrd/T6H464HvSVaQ9zq63VYtP xrWbvgDg0OTnG2zRLHYWlNw+Kyj4jUrrhTaCaeNCkSDBpXf1g1Srs3qhpzn6kTpiDRPy b7zApMGPNCF3nQ5KK+AMzkna6Yi4gP5EVm6t9fg2lpG01N6pkL0egSMRl2UmdB4AYEs7 rIhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745315571; x=1745920371; 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=HKQLzqoWG+gLW7RSIJ9nPWEIDBNLgsckjy8Twrs/QCY=; b=taC5hvW23GsyfIBOdZopmuzZmEPzSTxPGIP0vrJvWvNP1BCsfPSAAq48Ir3d7req2q P/xlKOHsODehcLx4e5JfB/E6p4im3U9W630P6Mn67ZPdp1RgcDOhiPAvV9TVUnd1xnAa lXVgWW9A3GjtW+WPG+BC/jB18c1eI3CsxBPvVdFwNRsCFBXZfrJRcpY9QFjd04HTZ0ZX tk/34x1uMLwvgLTSREsBQQtZaVy9+FGGyGSEX6ClRwcMBCTxBsOsooLUiAeTImtQFZ+t BfiAoQ5IRjXcFbFFLxBqdMgCj9BAOVcd8SH5BOKFm+bN8kllN6z+ssuI/sBMGEb+8TJi dlUQ== X-Forwarded-Encrypted: i=1; AJvYcCXa/4uUsxwABGGfEW3/+ZKCiB53RoP4b53j/OHHYkDk4IozjZyCeupHHCa6+pc+kJS/h7M/ZhhDPfdqaKc=@vger.kernel.org X-Gm-Message-State: AOJu0YyAhXh5F+mF/KSclcYpJpLmK2B6cC9BhTIXjdAuPgs6MnY9d0Bx xdIZdqBsHSiK1v0uKYY2vXUebWttRReHCZIx+gnRDYRS5o0mpi8zis5nyYh3uQmzphViTxdbzwj cU5e8aX1EtfNLmg== X-Google-Smtp-Source: AGHT+IETKvRlhl6S7D4rhT2//lenejgCFrwc+s7AWMxUPkZSc8kEy7dq6FlkwpzrlC+tj63W1tyRhUWyh2QVE5A= X-Received: from wrpx7.prod.google.com ([2002:adf:f647:0:b0:39a:bf11:dab0]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2403:b0:39b:f12c:385e with SMTP id ffacd0b85a97d-39efba460f4mr11318079f8f.20.1745315571701; Tue, 22 Apr 2025 02:52:51 -0700 (PDT) Date: Tue, 22 Apr 2025 09:52:22 +0000 In-Reply-To: <20250422-vec-methods-v3-0-deff5eea568a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250422-vec-methods-v3-0-deff5eea568a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2513; i=aliceryhl@google.com; h=from:subject:message-id; bh=KAoN/I1B9bjZf9yqrwjjTVg4XFhQinQf/p7dHQd7M6I=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoB2bgQFTDW6gduDB24/nGr91rXw4Ewq8GZMpj1 TzIpatOEumJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaAdm4AAKCRAEWL7uWMY5 RrUuD/9A/S/Mp/5agMCAikeUZWTvoBaF9+18NvkiRTIbKjj1Jj7PBuHnkEyn6sKV0fkRjtMcDM4 CRzFQhs7OM//DFERlSkjdyD4vO8ljqHqjqFMWwnCL25KYzqvrPAG4nTLi9SgrHgEAbqB4rEaPt/ qJcuFkQvEocGFWYcz6UK73YA7903o28HCXiJ9jup0inWoUgdvHhbI+WwcGRwZJmYqJreJofQuhf ne3BwQs2E0xPcbXsdrdDTdzGc4AmuksU1ixwP0Im8kO51kJmg9QUwCktq17/7iYEKQtQFflEMfI pcetvKnSgOOryPW0rFveq/tfFPX4NG/towOh3HU5tpX0Qayht364XXJdJKy74ved3XJtw8jppyr A+ssVjm0GBYgzTSoon7le4EQ1uInYLO8Q+zfTWgvS2ZoYvmm70PgQT90rOMS7EvlATxftNZnsrv 9jv9njRe9IARXIa+iq5e3m6JJqarICQqyNGw9hNv3Ksp9MGOXKHn7Q/n3PDkDGHUlIvV1r8uyO/ nEBkEaObMTCU6X9GS8tHr8X4K7qOA6njkuCvJDR+o7iDinmkAO3Wumr0lP7cxX8SljtzPBLdj4h g9w8GjhvG3ms2tSUeUcV2UL0o2FNiDOTgn32Xkj8LB5FU9ddYHm6DA0z6DXgsCn0AMPEaHjA/4A zN7zNFtTre0R1Jg== X-Mailer: b4 0.14.2 Message-ID: <20250422-vec-methods-v3-7-deff5eea568a@google.com> Subject: [PATCH v3 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 | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 2f28fda793e13841b59e83f34681e71ac815aff2..d96de01a7ab3c808f5aded067ad= 6f1e3ba8029ce 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -355,6 +355,45 @@ pub unsafe fn push_within_capacity_unchecked(&mut self= , v: T) { unsafe { self.inc_len(1) }; } =20 + /// Inserts an element at the given index in the [`Vec`] instance. + /// + /// Fails if the vector does not have capacity for the new element. Pa= nics if the index is out + /// of bounds. + /// + /// # 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 insert_within_capacity(&mut self, index: usize, element: T) -> = Result<(), T> { + let len =3D self.len(); + assert!(index <=3D len); + + if len >=3D self.capacity() { + return Err(element); + } + + // SAFETY: This is in bounds since `index <=3D len < capacity`. + let p =3D unsafe { self.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 { self.inc_len(1) }; + Ok(()) + } + /// Removes the last element from a vector and returns it, or `None` i= f it is empty. /// /// # Examples --=20 2.49.0.805.g082f7c87e0-goog