The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: Vitaly Wool <vitaly.wool@konsulko.se>
Cc: "Andreas Hindborg" <a.hindborg@kernel.org>,
	"Jens Axboe" <axboe@kernel.dk>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Burak Emir" <burak.emir@gmail.com>,
	"Boqun Feng" <boqun@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, "Igor Belousov" <igor.b@beldev.am>,
	"Claude Opus 5 . 5" <noreply@anthropic.com>
Subject: Re: [PATCH 1/2] rust: bitmap: add BitmapArray
Date: Wed, 2 Dec 2026 10:02:07 -0500	[thread overview]
Message-ID: <axAy76kn7CqAU8vQ@yury> (raw)
In-Reply-To: <20261202092332.136789-2-vitaly.wool@konsulko.se>

On Wed, Dec 02, 2026 at 10:23:31AM +0100, Vitaly Wool wrote:
> 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<WORDS>`, an owned bitmap of up to `WORDS` words of
> bits stored inline, which never allocates and dereferences to `Bitmap`
> like `BitmapVec`.

BitmapArray is a tautology. Bitmap is already an "array of bits". So
unless you're talking about 2d bitmaps, the name is wrong
 
> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
> Tested-by: Igor Belousov <igor.b@beldev.am>
> Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
> ---
>  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

You need it to embed in a structure? Why don't you show how it works
in the example? Maybe even add pahole output to convince me that you
did it right...

> +/// ```
> +/// use kernel::bitmap::BitmapArray;
> +///
> +/// let mut b = BitmapArray::<2>::new(100)?;

This is simply wrong. On my 32-bit box this wouldn't work, for sure.
Check how DECLARE_BITMAP() works in C. It doesn't take the number of
words. The same holds for every other bitmap function. That's on
purpose, because one of the main goals of the API is to decouple the
data structure from machine specifics.

Just look at the implementation and see how much work has been done
to keep bitmaps working smoothly across the different word lengths.
Sorry, but strong NAK.

But not a NAK for the idea of embedded long bitmaps. I would first
experiment with something like Bitmap<100>.

This should look like;

        struct Device {
            my_bitmap: Bitmap<100>,
        }
        
And from pahole perspective, should be the full analogue of 

         struct device {
            unsigned long my_bitmap[2];
         };

on your 64-bit machine.

Thanks,
Yury

> +/// 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<const WORDS: usize> {
> +    /// Storage of the bitmap.
> +    words: [usize; WORDS],
> +    /// Length of this bitmap.
> +    nbits: usize,
> +}
> +
> +impl<const WORDS: usize> BitmapArray<WORDS> {
> +    /// 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<Self> {
> +        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<const WORDS: usize> core::ops::Deref for BitmapArray<WORDS> {
> +    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<const WORDS: usize> core::ops::DerefMut for BitmapArray<WORDS> {
> +    #[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

  reply	other threads:[~2026-12-02 15:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:23 [PATCH 0/2] rnull: support 64K pages Vitaly Wool
2026-12-02  9:23 ` [PATCH 1/2] rust: bitmap: add BitmapArray Vitaly Wool
2026-12-02 15:02   ` Yury Norov [this message]
2026-12-02 18:05     ` Re: [PATCH 1/2] rnull: support 64K pages Vitaly Wool
2026-12-02 18:59       ` Yury Norov
2026-12-02 14:13 ` [PATCH 0/2] " Yury Norov
2026-12-02 17:24   ` Vitaly Wool
2026-12-02 18:32     ` Andreas Hindborg
2026-12-02 18:46     ` Gary Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axAy76kn7CqAU8vQ@yury \
    --to=ynorov@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=axboe@kernel.dk \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=burak.emir@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=igor.b@beldev.am \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=noreply@anthropic.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=vitaly.wool@konsulko.se \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox