From mboxrd@z Thu Jan 1 00:00:00 1970 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 0152D2DC353 for ; Tue, 21 Oct 2025 13:33:20 +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=1761053604; cv=none; b=pGvtucUhgBdkynWEk1zMaeC9k6Fn5QyJm+OOUrNPHAP5I6eBlFsSFTdpXpcn8iKsg5+HYwqYNTLZaZy4Kt8pEVNrJKM+GRwqXuLsOh8TgPO3mzen5X4ZQlQwjtFzbCK8i1OgV/qr6qD+gZrMrKiGfeoyBiU6noSf3CSbRtjJVGk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761053604; c=relaxed/simple; bh=qytksjZdLTISpFuZRyMOiiFkZgrPPHjKD+Mj5pS8yUg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G11aMy2ymFzlVLy/JuLLDXFdl4WCO5BU6NMwUh8WckqM8xQ3R7GO7M9/JrMKM2schkX7ZIfAU25xAdXsAbQLDtPdKYYUus3TuUPtkhRn+ZIQK9gCXZ5aEx8t1wcctTTxVFPstIze1Ep5+rEXSnZ7d7fMrz8H1c6b1/BNB66vQVo= 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=Bgyzk8Qp; 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="Bgyzk8Qp" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-470fd49f185so43996755e9.2 for ; Tue, 21 Oct 2025 06:33:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761053598; x=1761658398; 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=W7fDpabke6WhS46tT+T5FNys2Yr05OPqGRGg3toBdss=; b=Bgyzk8Qp4xj47h46EXJA25pSISKghqSGZO6GhqEKpYVQEcsqBdK/Ubix3wx9iu0lE5 GgTilxXMZ1p0Pa38nL6FmNFVf/XCwyTATY9f4kMwHzTO2U2G+pV+HkXrqcA4gu0xZ0S9 Q0ycWtwynqGQxATB9OtrYMtBRuqoWc46HC7IUTNY5bb0tuY8qIB+xSD0rgpC7i2jIUc4 86Y+afobBhnD2wozWllDXWo+I+IfVoln/sCDOxoO/0sP6uGomdmjvlhgrB+PlvdGHqhn 9tUqZIos9Opz5IiCwB973Fa+UgubKPaO4/+zSgU7x040jqGL4paMTOreyo4+TwLO2HTu WOSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761053598; x=1761658398; 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=W7fDpabke6WhS46tT+T5FNys2Yr05OPqGRGg3toBdss=; b=iVlILBdDu3YbB8vd6/z8OQWkBy96YF0OzxgfvHqC/81x/ezIY6xTGkZGM6UczGAZV0 /lBbKyPqmDsH0i10j1ghX9mvHBrQttcK5qsTii6aQ5G9vnveaQ8FqXR9R456gz2IdsSA hugdYgC7gocIgTNJMapgqCCSPs+qyoqlSpxBgSJNg7mjWD00bfop2OiBX7Q9BJ5Gr5ok OouS3387H4zT0MUpB3aky26Lfn2cCdCGqaRfsL2ee8F/AOZdADZKhyAxKHBu8LJDwIZa xYNtyPOA3cylJMmIKiNV1pzPilGOUTWSt0ZRUCi1NLszaO/GWjq0Aqhi4wcl1jS8hoU/ Z3sg== X-Forwarded-Encrypted: i=1; AJvYcCVDrZANzIr2L9ut0VRhM/AkVgiXd5BaVH+/TiRGWMv1WiZOvo8O2yQ8VyxpE6mIUFLYbFzZISFOB/HXVRl3tQ==@vger.kernel.org X-Gm-Message-State: AOJu0YzT8pCQ/uA0W8RKcBXnPSDELReycHcEqwxtfCg2EAJ6rU9niIis 67fqZHDarSDVSq9UBI3dvFfBS762DAyrqdPonQC7Gj/Elv6VGB2FW3OI5+OZROJMuUE8255u6FE KjSFCjldBB5gn2mq2uA== X-Google-Smtp-Source: AGHT+IHcH4JhLZmZhIV8KyVieSRjmkESC/bOub8ErG0T1qjHYU7NkdwZHbw5zRM/BUJluwKrwaIhdK6rgh5vdFU= X-Received: from wma6.prod.google.com ([2002:a05:600c:8906:b0:45c:d828:e351]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e0b:b0:46d:27b7:e7ff with SMTP id 5b1f17b1804b1-4711791f4e1mr119704175e9.36.1761053597714; Tue, 21 Oct 2025 06:33:17 -0700 (PDT) Date: Tue, 21 Oct 2025 13:32:46 +0000 In-Reply-To: <20251021-binder-bitmap-v2-0-e652d172c62b@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251021-binder-bitmap-v2-0-e652d172c62b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2867; i=aliceryhl@google.com; h=from:subject:message-id; bh=qytksjZdLTISpFuZRyMOiiFkZgrPPHjKD+Mj5pS8yUg=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBo94uXiyeb+mK6EdxYRp+Hcgh6FeizN0nxE/D5L mEzRRFiCEyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaPeLlwAKCRAEWL7uWMY5 RnDFD/91iGIfnnaWvFEPSgghqmXHMRCCEYUUtXrbWuW+fb6NiAuTxkp7WW0mYI2qzaFIxOA7pHw DoHwg5ejs0NfdiljayEuy98e5XozbobC88QBzU0cCEp7jPBN+bzQtR2Zp5fbL16JF7+jRvANoDb fqs+SsBT0KuMgBbgEamRsvc9V0DsbRws9TNxQGSYFHHyuZw7wO4lFeqw6zxpZU/gmkMIlT2ZbSe JOHEfSvl5kWfFm9+4EoULvC995qzbpkP505x5hvXdfzLpDoQdMKqhY4UOcZelGiV9YeYSkchsaK AoDLfwbfSKudmTAeCJL5gJMutgxbb2FcI6lPhhYFiMEWiX93E1IcKBWBKoXdOqab0Al1f7iKOCf PLOTkNGXj4G4EGWl679ve40KJz7KTWOubKOGZt7/sd9qPn4s5rDRxS/dcyZ22TfGaPYo0hH5Z3M Kphh8rJzhn5dX/OBYyuRjEZZbu0KnOOt4LrFteCxsN2TQtKjYdp5/yaBci42Wh8wtqzXsFkXkwn SDemlcFVTPBysMm58t72SaKywIr2E8urVfIM2mnsXg+2lvwjO1niu8LnjZ4x6zx88QKVcBkxVPI P4OAfTqbJFItDSsmrVJ2XL7UOlHX8B9NkTKKoLhd09yp2SI6jFykcBqm9F7nsz6uHiLqutODFfl HGKYPhMQhZ1n6LQ== X-Mailer: b4 0.14.2 Message-ID: <20251021-binder-bitmap-v2-4-e652d172c62b@google.com> Subject: [PATCH v2 4/5] 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" 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. Signed-off-by: Alice Ryhl --- rust/kernel/id_pool.rs | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/rust/kernel/id_pool.rs b/rust/kernel/id_pool.rs index 126e57f34c3407cb1dab3169417f01917e172dee..053ca417e8513fc33afc84c194d7899eafac8fec 100644 --- a/rust/kernel/id_pool.rs +++ b/rust/kernel/id_pool.rs @@ -199,18 +199,16 @@ pub fn grow(&mut self, mut resizer: PoolResizer) { self.map = resizer.new; } - /// 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 = self.map.next_zero_bit(offset); - if let Some(nr) = next_zero_bit { - self.map.set_bit(nr); - } - next_zero_bit + pub fn find_unused_id(&mut self, offset: usize) -> Option> { + Some(UnusedId { + id: self.map.next_zero_bit(offset)?, + pool: self, + }) } /// Releases an ID. @@ -220,6 +218,36 @@ pub fn release_id(&mut self, id: usize) { } } +/// Represents an unused id in an [`IdPool`]. +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 the id. + 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 the id. + pub fn as_u32(&self) -> u32 { + // CAST: The maximum possible value in an IdPool is i32::MAX. + self.id as u32 + } + + /// Acquire the unused id. + pub fn acquire(self) { + self.pool.map.set_bit(self.id); + } +} + impl Default for IdPool { #[inline] fn default() -> Self { -- 2.51.0.869.ge66316f041-goog