From nobody Tue Dec 2 00:25:29 2025 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.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 D1AE623372C for ; Tue, 25 Nov 2025 14:00:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764079204; cv=none; b=oPNMHDPkPElfXwFlE3lyNKHVRCwrKTzitw5A8jocDOgzkZZnukPVexwHXXMsSpAHwAt4MpA3c0KiVcCNy4u5r5kPRThyD0t3Y4qFWVGIj4C6+N8tAT/Cb40ti4YWRdeyuaRBcmnRn7NTA7KGzjnBWFa2NRwxG4/W/RNbdEGcKuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764079204; c=relaxed/simple; bh=H5sQlozQgdTviWcXpdsUawBLLbxV7JUv936dYiLlNyk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RAW17S/GDOx6p8GjBoJopi4ZCSdmFEgoGqtipvllNw51UpTPPIGGSdPig/ecXTazsJxtYdeja4lqT9Rfg4kbstsJpB4rJ+xkMMCqHSpgF0PUM9hkBSFANimhloXM+0JQQ77EIIVki81PYiAfp7284LpxNRTpYvgV/JZDACu5cr8= 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=dn/3CME0; arc=none smtp.client-ip=209.85.218.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="dn/3CME0" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b726a3c3214so522028066b.2 for ; Tue, 25 Nov 2025 06:00:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764079201; x=1764684001; 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=FGyrSjyLmW1aQxrJ0voWMyUBxAPU9xZJRGnfMgNjx+E=; b=dn/3CME0LnyXCiG5ssdDq7Qnb35Tog0pIgYO3306sioF+sF6SVym8/GDcfTIUrpgfZ 5qMHAHJ3FBNheFiYvSWzrrIwIhcUm/1V7rn3ZolZeBQM3BfZa0bq28i3R1H/29hhDYWK xL5Oa3HKYFwuROpJYBAPO3GqBl3iTg7JriMly9sRio3Aq8Huj5wlvksftK2x8vPO/7Cs sSjO9GgnKdOV/gI97BvGowMR3ntGfXMM6Cms/lVTYMZ+Tdiw666S7+30HlSP9lcnUjmq muXVvfph9Y3ndCHimTzqrvWP+F1Ize7kzhm/xvCvE70aaZo0O6YLpEP+Mgu3ebFo5C56 45Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764079201; x=1764684001; 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=FGyrSjyLmW1aQxrJ0voWMyUBxAPU9xZJRGnfMgNjx+E=; b=oCb/4DKYTQkmCLicoM6CCIoGTKBaM1chXV6ApTk/FiBZscM/EoBU8eVqeHhbSadu+o LlvtIelV/on86zRTF0hW9nhjLZwJ7h4rO8NzJ4ZqzPhMrqDzMEFu7OBEDG0tcFORp4pX zf2yvxSvV4tMjDFq8s5kEuArqCzepNGXHvVpQf1GcaVUylPIeAZslUAEXP8q8uwYpYF4 wxQXVBWvBPzehlmb3RCYSeRbztzS+3PH7HA6InRIuMYX6Zu6741EsqTdTt7nOaTPv/7P V0ipM/J393ZQsy/NL/DVDb+4HLwdcnuxsg1KFWrZkZkEIjqyIRuzzmnRj9xJro6WNG5a 0p4Q== X-Forwarded-Encrypted: i=1; AJvYcCVLU/LnOX8TIkVpJg6nryDljz3epaGWSddZOD/kMGCKBQDohnPv/5/ziKAqMC4NbsGsPk1usr3jObTW960=@vger.kernel.org X-Gm-Message-State: AOJu0YzQKHx/CPpW+9utRjsxpnqC1Bg75ev0lfggz1gs/HadllvAw8P+ bomv8XKdIwtl9QWR4n7mJtk3CjgPFdaI7KzlAgP/diALgQXBOadcLga+peelYdIbCEzV5Z3NgzM KIQZjPTxDYFwJauPYJg== X-Google-Smtp-Source: AGHT+IEEAp8zbzW+oweQ62B2PauNM+6chBML5rrFj3kEC2Ce+RRKkoXiSkljEfXTQ11HO5U11MJwjzYkdTLISkc= X-Received: from ejcrm13.prod.google.com ([2002:a17:907:6b0d:b0:b73:7130:3880]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:72c6:b0:b73:8759:62f6 with SMTP id a640c23a62f3a-b7671a52051mr1832541666b.60.1764079201065; Tue, 25 Nov 2025 06:00:01 -0800 (PST) Date: Tue, 25 Nov 2025 13:59:41 +0000 In-Reply-To: <20251125-binder-bitmap-v6-0-dedaf1d05a98@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251125-binder-bitmap-v6-0-dedaf1d05a98@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4864; i=aliceryhl@google.com; h=from:subject:message-id; bh=H5sQlozQgdTviWcXpdsUawBLLbxV7JUv936dYiLlNyk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBpJbZYbcchNTH2JmYGWm49+hAseDKMgJBR+0DJh V8ZgkJLLCuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaSW2WAAKCRAEWL7uWMY5 RlwdD/4skWmtrLw53OsM6+KwIO1lo04b0MiJ2bN7l79IRUMlg8smzHpbVMxs5+/A9+8xqeTIDCW FcwOCWyit/4grQp+Ro8m0SQUYDdnRkZPbseaMobFgk3wikwy27fEpggFYhFCpbnAbyRu+yRMzuA PDlI/oKkm0i9kpJay78qpMRI6Mx1KZKSKqsDZx2LvJsms4sGijVDyN/k/HOqGuPXDi7K1sqgTdp FnhMDody9uEFiqJAnZDyEYfHJFHtUgFEH1RBwmj/WrpZx+JMRr7FJEZivDGzoKJvKBXPDNITRmk 6pimRRxCNtx6Z7nkI8VuiqEU9Hd3lU0Lkm2SO2L09/KMvPE9fvziovCxpb9IUqQJLU6a+o+R9uL 0udXYqc9sd5TwuScDiUReeBLwt14cjC4we5rqWdX7QstHZfWK9hvjT8yILNCW+qjO2w1CgYj4dE HHsstY0zFxu1J6KPsLwdkWfTZI1J3VLiNq4o/AZQ73fNQDd+re/FUdete13sv40tGYZep2zNA9T rfCD16asrIr11qumv6rcu3J8XkRFwGpv3FQ51x4sJvB7JHciJlIgFCCIbEGoIAA40N2oQLDzhII KSBqROZqnZVf+JH5I6Le9r9B+M3UUupMue4UuOxgYIkFH+rCQcTpxyYKN9f90IkMAdfeGDej8Eq usIOVn4mBARy7Ww== X-Mailer: b4 0.14.2 Message-ID: <20251125-binder-bitmap-v6-5-dedaf1d05a98@google.com> Subject: [PATCH v6 5/6] rust: id_pool: do not immediately acquire new ids From: Alice Ryhl To: Greg Kroah-Hartman , Yury Norov Cc: "=?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , Burak Emir , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , 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 When Rust Binder assigns a new ID, it performs various fallible operations before it "commits" to actually using the new ID. To support this pattern, change acquire_next_id() so that it does not immediately call set_bit(), but instead returns an object that may be used to call set_bit() later. The UnusedId type holds a exclusive reference to the IdPool, so it's guaranteed that nobody else can call find_unused_id() while the UnusedId object is live. Reviewed-by: Burak Emir Reviewed-by: Danilo Krummrich Signed-off-by: Alice Ryhl --- rust/kernel/id_pool.rs | 75 ++++++++++++++++++++++++++++++++++++++++------= ---- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/rust/kernel/id_pool.rs b/rust/kernel/id_pool.rs index 1adec2c6fdb0b80515f9b64c67218efa864ce580..a4c37d6a0971699d1ea42facc68= aa3dd2b44a0ca 100644 --- a/rust/kernel/id_pool.rs +++ b/rust/kernel/id_pool.rs @@ -23,8 +23,8 @@ /// Basic usage /// /// ``` -/// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; -/// use kernel::id_pool::IdPool; +/// use kernel::alloc::AllocError; +/// use kernel::id_pool::{IdPool, UnusedId}; /// /// let mut pool =3D IdPool::with_capacity(64, GFP_KERNEL)?; /// for i in 0..64 { @@ -32,13 +32,13 @@ /// } /// /// pool.release_id(23); -/// assert_eq!(23, pool.acquire_next_id(0).ok_or(ENOSPC)?); +/// assert_eq!(23, pool.find_unused_id(0).ok_or(ENOSPC)?.acquire()); /// -/// assert_eq!(None, pool.acquire_next_id(0)); // time to realloc. +/// assert!(pool.find_unused_id(0).is_none()); // time to realloc. /// let resizer =3D pool.grow_request().ok_or(ENOSPC)?.realloc(GFP_KERNEL)= ?; /// pool.grow(resizer); /// -/// assert_eq!(pool.acquire_next_id(0), Some(64)); +/// assert_eq!(pool.find_unused_id(0).ok_or(ENOSPC)?.acquire(), 64); /// # Ok::<(), Error>(()) /// ``` /// @@ -52,8 +52,8 @@ /// fn get_id_maybe_realloc(guarded_pool: &SpinLock) -> Result { /// let mut pool =3D guarded_pool.lock(); /// loop { -/// match pool.acquire_next_id(0) { -/// Some(index) =3D> return Ok(index), +/// match pool.find_unused_id(0) { +/// Some(index) =3D> return Ok(index.acquire()), /// None =3D> { /// let alloc_request =3D pool.grow_request(); /// drop(pool); @@ -221,18 +221,18 @@ pub fn grow(&mut self, mut resizer: PoolResizer) { self.map =3D resizer.new; } =20 - /// Acquires a new ID by finding and setting the next zero bit in the - /// bitmap. + /// Finds an unused ID in the bitmap. /// /// Upon success, returns its index. Otherwise, returns [`None`] /// to indicate that a [`Self::grow_request`] is needed. #[inline] - pub fn acquire_next_id(&mut self, offset: usize) -> Option { - let next_zero_bit =3D self.map.next_zero_bit(offset); - if let Some(nr) =3D next_zero_bit { - self.map.set_bit(nr); - } - next_zero_bit + #[must_use] + pub fn find_unused_id(&mut self, offset: usize) -> Option= > { + // INVARIANT: `next_zero_bit()` returns None or an integer less th= an `map.len()` + Some(UnusedId { + id: self.map.next_zero_bit(offset)?, + pool: self, + }) } =20 /// Releases an ID. @@ -242,6 +242,51 @@ pub fn release_id(&mut self, id: usize) { } } =20 +/// Represents an unused id in an [`IdPool`]. +/// +/// # Invariants +/// +/// The value of `id` is less than `pool.map.len()`. +pub struct UnusedId<'pool> { + id: usize, + pool: &'pool mut IdPool, +} + +impl<'pool> UnusedId<'pool> { + /// Get the unused id as an usize. + /// + /// Be aware that the id has not yet been acquired in the pool. The + /// [`acquire`] method must be called to prevent others from taking th= e id. + /// + /// [`acquire`]: UnusedId::acquire() + #[inline] + #[must_use] + pub fn as_usize(&self) -> usize { + self.id + } + + /// Get the unused id as an u32. + /// + /// Be aware that the id has not yet been acquired in the pool. The + /// [`acquire`] method must be called to prevent others from taking th= e id. + /// + /// [`acquire`]: UnusedId::acquire() + #[inline] + #[must_use] + pub fn as_u32(&self) -> u32 { + // CAST: By the type invariants: + // `self.id < pool.map.len() <=3D BitmapVec::MAX_LEN =3D i32::MAX`. + self.id as u32 + } + + /// Acquire the unused id. + #[inline] + pub fn acquire(self) -> usize { + self.pool.map.set_bit(self.id); + self.id + } +} + impl Default for IdPool { #[inline] fn default() -> Self { --=20 2.52.0.460.gd25c4c69ec-goog