From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=46.30.211.244 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796203428; b=j8E62tGB5sDZE/kJzaNjNM7FfOgytIOm4tAONNfhwZGfMYhQC4vgMu8PEqs+emrmZNPw hR7zTPsNiYt9V8qY2bANhMhs10yA9jvOs2mB50FTr6ZCcey1886By4eAh8mUiaVJtg/4p kLX0ElyxfqYGPzHHE/2ETRJn9/xkfvZwpA2wO5tJ6k76lw0t7quSRtTKODJ6k+vY5Jz4c dwh/nhP7M6B65dbWAQ8W9Vf3vlMcYoEGeIqYjBILT94E9cXv0MjUFTbiIdk+tzdDS+3na 6riW/kTGx9ATq5zAeTb5mdClFaXopq2t5qLVVBK7cM05T8t6IAu4IXgoFu0HyEpohsg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796203428; h=DMARC-Filter:DKIM-Signature:DKIM-Signature:X-HalOne-ID:Received:From: To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=efU5h+HLAxOVqAnl951np9r5zLEHoB2lR3Td8/Y71ZY=; b=GOpphZc7HMZImfQCEto3wD5CLw29s3muQCvfomvsM0+L2n8xU+KXOMyP50U/kXX/MOy/ 805ZPgRxheqzZOtsYddncCLm1w9rVz8S5FMoD03fI582gDoAxlR/3govAMtZiEKChOO1H G4VrH3TXPeSFsfEFYPplHPrYjlx5ma77ft3jSfQZXyYU1dpJfw3MsUKzvE28k8HQVHGhZ i7v0nM6fEXUkxG92VJnLFH1X10ySjvRFU3MqD27uggTOQJf6Z1Ws40/i35pW8rMb7N2wq GVlggDfiISpwtonDAm5bL8ODkVRk4+ZTFzu3zEGUWXmEk8txnb9HrpDoDB8q+M2MkcA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=konsulko.se; spf=none smtp.mailfrom=konsulko.se; dkim=pass header.d=konsulko.se header.i=@konsulko.se header.a=rsa-sha256 header.s=rsa2 header.b=dgqOdiPs; dkim=pass header.d=konsulko.se header.i=@konsulko.se header.a=ed25519-sha256 header.s=ed2 header.b=uowBEWK7; arc=none smtp.remote-ip=46.30.211.244 Received: from mailrelay-egress5.pub.mailoutpod3-cph3.one.com (mailrelay-egress5.pub.mailoutpod3-cph3.one.com [46.30.211.244]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94174606A32A for ; Wed, 2 Dec 2026 09:23:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 94174606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=konsulko.se Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=konsulko.se Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=konsulko.se header.i=@konsulko.se header.a=rsa-sha256 header.s=rsa2 header.b=dgqOdiPs; dkim=pass header.d=konsulko.se header.i=@konsulko.se header.a=ed25519-sha256 header.s=ed2 header.b=uowBEWK7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1796203425; x=1796808225; d=konsulko.se; s=rsa2; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=efU5h+HLAxOVqAnl951np9r5zLEHoB2lR3Td8/Y71ZY=; b=dgqOdiPsZvQ62X25z3y6A+Ppyrcyh0R8kfEDecl4Tv5MLXqM39l5PBwj1WgSzOyEQ+GbD19QivmC4 NUIdLjegCd1VzxjkCBk6CtG+6gLgj6ei6VkofnakPZQ2cDr3P1Fa6TFcKZ/qo/7bexIHwC0MepRsIK 66vbpUjMw3BZnIuuudt9iPoBD48TLmBPJReKL7z6SO7oR/KejTpeO4OKN5QOHwLSOHHumzrpT4h8VG 3GCWl5LqFSMH6p0KHVj0EBOZgHsOQXXhKRPsDMMSgYR7BA7t3QJk95yr5FYpJ78J+uQj28tb/GzE9n yajFeZhLROnhHDnNkr7cwbzdWJ/E4ug== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1796203425; x=1796808225; d=konsulko.se; s=ed2; h=content-transfer-encoding:mime-version:references:in-reply-to:message-id:date: subject:cc:to:from:from; bh=efU5h+HLAxOVqAnl951np9r5zLEHoB2lR3Td8/Y71ZY=; b=uowBEWK77/dd9lz+ftSW5oTtr6aUz5H30OUClPFsQ4iWijux5SNJVrRk/owaD4G2/bBm9s+PdPnOh pzAN6v7Bw== X-HalOne-ID: 10a4fa5f-ee32-11f1-ba34-ada7ea9cd1ac Received: from vitaly-vul-HP-EliteBook.. (m90-129-126-215.cust.tele2.se [90.129.126.215]) by mailrelay6.pub.mailoutpod3-cph3.one.com (Halon) with ESMTPSA id 10a4fa5f-ee32-11f1-ba34-ada7ea9cd1ac; Wed, 02 Dec 2026 09:23:44 +0000 (UTC) From: Vitaly Wool To: Andreas Hindborg , Jens Axboe , Yury Norov Cc: Burak Emir , Boqun Feng , Alice Ryhl , Miguel Ojeda , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Wool , Igor Belousov , "Claude Opus 5 . 5" Subject: [PATCH 1/2] rust: bitmap: add BitmapArray Date: Wed, 2 Dec 2026 10:23:31 +0100 Message-ID: <20261202092332.136789-2-vitaly.wool@konsulko.se> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202092332.136789-1-vitaly.wool@konsulko.se> References: <20261202092332.136789-1-vitaly.wool@konsulko.se> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A `BitmapVec` keeps up to `usize::BITS` bits inline and allocates its storage with `bitmap_zalloc()` beyond that. A bitmap embedded in a structure that is allocated anyway, with a maximum length known at compile time, then needs a second allocation and a pointer to follow, or `unsafe` code that wraps an array with `Bitmap::from_raw()`. Add `BitmapArray`, an owned bitmap of up to `WORDS` words of bits stored inline, which never allocates and dereferences to `Bitmap` like `BitmapVec`. Signed-off-by: Vitaly Wool Tested-by: Igor Belousov Assisted-by: Claude Opus 5.5 --- rust/kernel/bitmap.rs | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs index 610d565535cc..7d7bd40e2e2b 100644 --- a/rust/kernel/bitmap.rs +++ b/rust/kernel/bitmap.rs @@ -8,6 +8,7 @@ use crate::alloc::{AllocError, Flags}; use crate::bindings; +use crate::error::{code::EINVAL, Result}; #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))] use crate::pr_err; use crate::ptr::Alignment; @@ -320,6 +321,96 @@ pub fn fill_random(&mut self) { } } +/// An owned bitmap of up to `WORDS * usize::BITS` bits, stored inline. +/// +/// Unlike [`BitmapVec`], it never allocates, so it suits bitmaps whose maximum length is known at +/// compile time and that are embedded in other structures. See [`Bitmap`] for available methods. +/// +/// # Examples +/// +/// ``` +/// use kernel::bitmap::BitmapArray; +/// +/// let mut b = BitmapArray::<2>::new(100)?; +/// +/// assert_eq!(100, b.len()); +/// b.set_bit(3); +/// b.set_bit(99); +/// assert_eq!(Some(3), b.next_bit(0)); +/// assert_eq!(Some(99), b.next_bit(4)); +/// assert_eq!(Some(99), b.last_bit()); +/// +/// b.clear_all(); +/// assert_eq!(None, b.next_bit(0)); +/// +/// assert!(BitmapArray::<2>::new(129).is_err()); +/// # Ok::<(), Error>(()) +/// ``` +/// +/// # Invariants +/// +/// `nbits` is at most [`BitmapArray::CAPACITY`] and at most [`BitmapVec::MAX_LEN`]. +pub struct BitmapArray { + /// Storage of the bitmap. + words: [usize; WORDS], + /// Length of this bitmap. + nbits: usize, +} + +impl BitmapArray { + /// The maximum length of a `BitmapArray` with `WORDS` words. + pub const CAPACITY: usize = WORDS.saturating_mul(usize::BITS as usize); + + /// Constructs a new [`BitmapArray`] of `nbits` bits, all clear. + /// + /// Fails with [`EINVAL`] if `nbits` is greater than [`BitmapArray::CAPACITY`] or than + /// [`BitmapVec::MAX_LEN`]. + #[inline] + pub fn new(nbits: usize) -> Result { + if nbits > Self::CAPACITY || nbits > BitmapVec::MAX_LEN { + return Err(EINVAL); + } + // INVARIANT: `nbits` was checked above. + Ok(Self { + words: [0; WORDS], + nbits, + }) + } + + /// Returns length of this [`BitmapArray`]. + #[allow(clippy::len_without_is_empty)] + #[inline] + pub fn len(&self) -> usize { + self.nbits + } + + /// Clears all bits of this [`BitmapArray`]. + #[inline] + pub fn clear_all(&mut self) { + self.words = [0; WORDS]; + } +} + +impl core::ops::Deref for BitmapArray { + type Target = Bitmap; + + #[inline] + fn deref(&self) -> &Bitmap { + // SAFETY: `self.words` is an initialized array that holds `CAPACITY >= nbits` bits, + // `nbits <= MAX_LEN` by the type invariants, and the array lives as long as the returned + // shared borrow of `self`. + unsafe { Bitmap::from_raw(self.words.as_ptr(), self.nbits) } + } +} + +impl core::ops::DerefMut for BitmapArray { + #[inline] + fn deref_mut(&mut self) -> &mut Bitmap { + // SAFETY: As in `deref`, and the returned borrow of `self` is exclusive. + unsafe { Bitmap::from_raw_mut(self.words.as_mut_ptr(), self.nbits) } + } +} + impl Bitmap { /// Set bit with index `index`. /// -- 2.39.5