From nobody Fri Dec 19 16:08:49 2025 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 0157E28A1E3 for ; Mon, 19 May 2025 16:17: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=1747671462; cv=none; b=trRcoWEaSaF59Ko0opteniGoNpxh8E/wAgszC5uNS7l2GEtxozs2g1i98eXDmVBGPrOkSxhTbUl83ElywHutmpHk9MQKu7YdxPcPWu/zPbm3hrpLJaEfNj637mQ8vCVEJGYYaWQBZ4UUueauFwL9eiy9slsRVY6e4slihI9BJrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747671462; c=relaxed/simple; bh=rHUxzB2Bwi+2jlKp4urPGDKcCylqhDAV9PHNICkE1AM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ebUKIRXbmJsu/t3DBfJ5CS3JxTrTu2jSnOz6r5Raci648zKO0JcJErWI8BEz3UIG86RD2u5wrXUltfgQ00MS5qlj5+B3/ovB5IZoK6UIUBADaQpBd1N9WQfXfHHXKidJDC1z5eBafLdkDboKytpKmExHwItiMVEOW69Uh+JQu8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bqe.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QL1YDF5F; 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--bqe.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QL1YDF5F" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d734da1a3so25195425e9.0 for ; Mon, 19 May 2025 09:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747671459; x=1748276259; 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=zRmZrCz4QpMiECwxwWyXnnAcIyfbi3oIrpsYNgBmqlI=; b=QL1YDF5FRrVyQG5Mqn5MfgOfwsLL+ZL01joP1us5ZT8X7nMCzh+JCNji/cYMSUhu1a XqMOs4RSeuxqkASbtkVFIAqyDzzSqZ4trkHIfmJ+KSGN80jsYZPxkUouoq3osUGfIb8k C3ZX5S2UXrtoP6JzlzNsUGJp7WlLtaw9v08i2wfTgm8+gr+D2L/f0eIyBQ2aVfxkpb/L xIRfh4Yltpnhc9k/QRLbukvTDSS3IEBSXQn/8urA0zuqY0WsIjPvB4wuu41ppipvRPpc PyDLQULIHtDrM9DaxHBO+x7zCa0xkAIfNUjoQWY6mETBQLT7vRWqG5+ccpA1CUfb7MJD Oy0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747671459; x=1748276259; 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=zRmZrCz4QpMiECwxwWyXnnAcIyfbi3oIrpsYNgBmqlI=; b=GEr/ejoh2xoY1Ui2Sw4+FYjwcjtOPq2NJ8IBCNZ0ynuQZPYxvhW7hkENNzGaYPtEAD Wbo/rUwXez/WDFuyRsnbhCYPSjW55DwawMLOOsZi01tVyyI+YuSXDETc4+yhCOTParGd u3x2jjaLxgVRiaMAMgjfnlu8lN27HeKgCGNNKE+df3rMp4w9mwr1Tnr4O/0gdrMbbE0z GiizXwwbh17CCG4A0itV22nfjHw1+wa6Xv5TaJE6x0dZcNP+0vwztH7zrT+HohPzoqys oQ82bQuVlSTroX1aXBMfGbymdnOnLmOI9YUodWCpHL1XAjny1sVok6X70Fwo+Iukfgdp mXzw== X-Forwarded-Encrypted: i=1; AJvYcCUyEsLUaId0TSt6GCfWKbXCsLYVShEyFhUFYpChRA/ylp9aUTdn52UOoWHu/9+pAA7EeKkYailZfZ+6Vjw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+lNOmbQLaANCk2aIKDnDoPai8TLHnIONn2jkGz2AEfKKhA6Y3 MqYSu4C8SmxjmFhtuQfhDP78wO5nY/AVo7DgNfBZ+9TPzebJOsVOvJ6kkEeJ/TNGGp1RxA== X-Google-Smtp-Source: AGHT+IFyBZQy4NfRKtT3XwWN6zobbmN0oZUd8mI/AD9F82y7coNWR9BSlEwIZv+kecL0Ld+zemnMy6w= X-Received: from wmdd3.prod.google.com ([2002:a05:600c:a203:b0:445:1cd2:5e5f]) (user=bqe job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a46:b0:442:dc6f:2f11 with SMTP id 5b1f17b1804b1-442ff032533mr80126215e9.25.1747671459299; Mon, 19 May 2025 09:17:39 -0700 (PDT) Date: Mon, 19 May 2025 16:17:05 +0000 In-Reply-To: <20250519161712.2609395-1-bqe@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250519161712.2609395-1-bqe@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519161712.2609395-6-bqe@google.com> Subject: [PATCH v8 5/5] rust: add dynamic ID pool abstraction for bitmap From: Burak Emir To: Yury Norov , Kees Cook Cc: Burak Emir , Rasmus Villemoes , Viresh Kumar , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , "Gustavo A . R . Silva" , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This is a port of the Binder data structure introduced in commit 15d9da3f818c ("binder: use bitmap for faster descriptor lookup") to Rust. Like drivers/android/dbitmap.h, the ID pool abstraction lets clients acquire and release IDs. The implementation uses a bitmap to know what IDs are in use, and gives clients fine-grained control over the time of allocation. This fine-grained control is needed in the Android Binder. We provide an example that release a spinlock for allocation and unit tests (rustdoc examples). The implementation is not aware that the underlying Bitmap abstraction handles lengths below BITS_PER_LONG without allocation. Suggested-by: Alice Ryhl Suggested-by: Yury Norov Signed-off-by: Burak Emir --- MAINTAINERS | 1 + rust/kernel/id_pool.rs | 201 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 3 files changed, 203 insertions(+) create mode 100644 rust/kernel/id_pool.rs diff --git a/MAINTAINERS b/MAINTAINERS index 943d85ed1876..bc95d98f266b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4134,6 +4134,7 @@ R: Yury Norov S: Maintained F: lib/find_bit_benchmark_rust.rs F: rust/kernel/bitmap.rs +F: rust/kernel/id_pool.rs =20 BITOPS API M: Yury Norov diff --git a/rust/kernel/id_pool.rs b/rust/kernel/id_pool.rs new file mode 100644 index 000000000000..8f07526bb580 --- /dev/null +++ b/rust/kernel/id_pool.rs @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Google LLC. + +//! Rust API for an ID pool backed by a `Bitmap`. + +use crate::alloc::{AllocError, Flags}; +use crate::bitmap::Bitmap; + +/// Represents a dynamic ID pool backed by a `Bitmap`. +/// +/// Clients acquire and release IDs from zero bits in a bitmap. +/// +/// The ID pool can grow or shrink as needed. It has been designed +/// to support the scenario where users need to control the time +/// of allocation of a new backing bitmap, which may require release +/// of locks. +/// These operations then, are verified to determine if the grow or +/// shrink is sill valid. +/// +/// # Examples +/// +/// Basic usage +/// +/// ``` +/// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; +/// use kernel::id_pool::IdPool; +/// +/// let mut pool =3D IdPool::new(64, GFP_KERNEL)?; +/// for i in 0..64 { +/// assert_eq!(i, pool.acquire_next_id(i).ok_or(ENOSPC)?); +/// } +/// +/// pool.release_id(23); +/// assert_eq!(23, pool.acquire_next_id(0).ok_or(ENOSPC)?); +/// +/// assert_eq!(None, pool.acquire_next_id(0)); // time to realloc. +/// let resizer =3D pool.grow_alloc().alloc(GFP_KERNEL)?; +/// pool.grow(resizer); +/// +/// assert_eq!(pool.acquire_next_id(0), Some(64)); +/// # Ok::<(), Error>(()) +/// ``` +/// +/// Releasing spinlock to grow the pool +/// +/// ```no_run +/// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; +/// use kernel::sync::{new_spinlock, SpinLock}; +/// use kernel::id_pool::IdPool; +/// +/// fn get_id_maybe_alloc(guarded_pool: &SpinLock) -> Result { +/// let mut pool =3D guarded_pool.lock(); +/// loop { +/// match pool.acquire_next_id(0) { +/// Some(index) =3D> return Ok(index), +/// None =3D> { +/// let alloc_request =3D pool.grow_alloc(); +/// drop(pool); +/// let resizer =3D alloc_request.alloc(GFP_KERNEL)?; +/// pool =3D guarded_pool.lock(); +/// pool.grow(resizer) +/// } +/// } +/// } +/// } +/// ``` +pub struct IdPool { + map: Bitmap, +} + +/// Returned when the `IdPool` should change size. +pub struct AllocRequest { + nbits: usize, +} + +/// Contains an allocated `Bitmap` for resizing `IdPool`. +pub struct PoolResizer { + new: Bitmap, +} + +impl AllocRequest { + /// Allocates a new `Bitmap` for `IdPool`. + pub fn alloc(&self, flags: Flags) -> Result { + let new =3D Bitmap::new(self.nbits, flags)?; + Ok(PoolResizer { new }) + } +} + +impl IdPool { + /// Constructs a new `[IdPool]`. + #[inline] + pub fn new(nbits: usize, flags: Flags) -> Result { + let map =3D Bitmap::new(nbits, flags)?; + Ok(Self { map }) + } + + /// Returns how many IDs this pool can currently have. + #[inline] + pub fn len(&self) -> usize { + self.map.len() + } + + /// Returns an [`AllocRequest`] if the [`IdPool`] can be shrunk, [`Non= e`] otherwise. + /// + /// # Examples + /// + /// ``` + /// use kernel::alloc::{AllocError, flags::GFP_KERNEL}; + /// use kernel::id_pool::{AllocRequest, IdPool}; + /// + /// let mut pool =3D IdPool::new(1024, GFP_KERNEL)?; + /// let alloc_request =3D pool.shrink_alloc().ok_or(AllocError)?; + /// let resizer =3D alloc_request.alloc(GFP_KERNEL)?; + /// pool.shrink(resizer); + /// assert_eq!(pool.len(), kernel::bindings::BITS_PER_LONG as usize); + /// # Ok::<(), AllocError>(()) + /// ``` + #[inline] + pub fn shrink_alloc(&self) -> Option { + let len =3D self.map.len(); + if len <=3D bindings::BITS_PER_LONG as usize { + return None; + } + /* + * Determine if the bitmap can shrink based on the position of + * its last set bit. If the bit is within the first quarter of + * the bitmap then shrinking is possible. In this case, the + * bitmap should shrink to half its current size. + */ + match self.map.last_bit() { + Some(bit) =3D> { + if bit < (len >> 2) { + Some(AllocRequest { nbits: len >> 1 }) + } else { + None + } + } + None =3D> Some(AllocRequest { + nbits: bindings::BITS_PER_LONG as usize, + }), + } + } + + /// Shrinks pool by using a new `Bitmap`, if still possible. + #[inline] + pub fn shrink(&mut self, mut resizer: PoolResizer) { + // Verify that shrinking is still possible. The `resizer` + // bitmap might have been allocated without locks, so this call + // could now be outdated. In this case, drop `resizer` and move on. + if let Some(AllocRequest { nbits }) =3D self.shrink_alloc() { + if nbits <=3D resizer.new.len() { + resizer.new.copy_and_extend(&self.map); + self.map =3D resizer.new; + return; + } + } + } + + /// Returns an `AllocRequest` for growing this `IdPool`. + #[inline] + pub fn grow_alloc(&self) -> AllocRequest { + AllocRequest { + nbits: self.map.len() << 1, + } + } + + /// Grows pool by using a new `Bitmap`, if still necessary. + #[inline] + pub fn grow(&mut self, mut resizer: PoolResizer) { + // `resizer` bitmap might have been allocated without locks, + // so this call could now be outdated. In this case, drop + // `resizer` and move on. + if resizer.new.len() <=3D self.map.len() { + return; + } + + resizer.new.copy_and_extend(&self.map); + self.map =3D resizer.new; + } + + /// Acquires a new ID by finding and setting the next zero bit in the + /// bitmap. Upon success, returns its index. Otherwise, returns `None` + /// to indicate that a `grow_alloc` is needed. + #[inline] + pub fn acquire_next_id(&mut self, offset: usize) -> Option { + match self.map.next_zero_bit(offset) { + res @ Some(nr) =3D> { + self.map.set_bit(nr); + res + } + None =3D> None, + } + } + + /// Releases an ID. + #[inline] + pub fn release_id(&mut self, id: usize) { + self.map.clear_bit(id); + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 8c4161cd82ac..d7def807900a 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -54,6 +54,7 @@ #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod fs; +pub mod id_pool; pub mod init; pub mod io; pub mod ioctl; --=20 2.49.0.1101.gccaa498523-goog