Skip to contentSkip to conversation

rust: adds Bitmap API, ID pool and bindings

PATCH Superseded Burak Emir 3 participants 10 messages Mar 27, 2025 – Apr 23, 2025

Subsystems (4)

v6 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Burak Emir

author
#0 lore raw

v6 cover rust: adds Bitmap API, ID pool and bindings

This series adds a Rust bitmap API for porting the approach from commit 15d9da3f818c ("binder: use bitmap for faster descriptor lookup") to Rust. The functionality in dbitmap.h makes use of bitmap and bitops.

The Rust bitmap API provides a safe abstraction to underlying bitmap and bitops operations. For now, only includes method necessary for dbitmap.h, more can be added later. We perform bounds checks for hardening, violations are programmer errors that result in panics.

We include set_bit_atomic and clear_bit_atomic operations. One has to avoid races with non-atomic operations, which is ensure by the Rust type system: either callers have shared references &bitmap in which case the mutations are atomic operations. Or there is a exclusive reference &mut bitmap, in which case there is no concurrent access.

This version includes an optimization to represent the bitmap inline, as suggested by Yury.

We introduce a Rust API that would replace (dbitmap.h) in file id_pool.rs. This data structure is tightly coupled to the bitmap API. Includes an example of usage that requires releasing a spinlock, as expected in Binder driver.

This is v6 of a patch introducing Rust bitmap API [v5]. Thanks for all the helpful comments, this series has improved significantly as a result of your work.

Not adding separate unit tests: the Rust unit test infrastructure is very new, and there does not seem to be benchmarking support for Rust tests yet. Are the # Examples tests enough? Alternatively, can we add more test cases to those until the unit test infrastructure is in place?

Changes v5 --> v6:
- Added SAFETY comment for atomic operations.
- Added missing volatile to bitops set_bit and clear_bit bindings.
- Fixed condition on nbits to be <= i32::MAX, update SAFETY comments.
- Readability improvements.
- Updated doc comments wording and indentation.

Changes v4 --> v5: (suggested by Yury and Alice)
- rebased on next-20250318
- split MAINTAINERS changes
- no dependencies on [1] and [2] anymore - Viresh,
please do add a separate section if you want to maintain cpumask.rs separately.
- imports atomic and non-atomic variants, introduces a naming convention set_bit and set_bit_atomic on the Rust side.
- changed naming and comments. Keeping new.
- change dynamic_id_pool to id_pool
- represent bitmap inline when possible
- add some more tests
- add myself to M: line for the Rust abstractions

Changes v3 --> v4:
- Rebased on Viresh's v3 [2].
- split into multiple patches, separate Rust and bindings. (Yury)
- adds dynamic_id_pool.rs to show the Binder use case. (Yury)
- include example usage that requires release of spinlock (Alice)
- changed bounds checks to assert!, shorter (Yury)
- fix param names in binding helpers. (Miguel)
- proper rustdoc formatting, and use examples as kunit tests. (Miguel)
- reduce number of Bitmap methods, and simplify API through use Option<usize> to handle the "not found" case.
- make Bitmap pointer accessors private, so Rust Bitmap API provides an actual abstraction boundary (Tamir)
- we still return AllocError in Bitmap::new in case client code asks for a size that is too large. Intentionally different from other bounds checks because it is not about access but allocation, and we expect that client code need never handle AllocError and nbits > u32::MAX situations differently.

Changes v2 --> v3:
- change bitmap_copy to copy_from_bitmap_and_extend which zeroes out extra bits. This enables dbitmap shrink and grow use cases while offering a consistent and understandable Rust API for other uses (Alice)

Changes v1 --> v2:
- Rebased on Yury's v2 [1] and Viresh's v3 [2] changes related to bitmap.
- Removed import of bindings::*, keeping only prefix (Miguel)
- Renamed panic methods to make more explicit (Miguel)
- use markdown in doc comments and added example/kunit test (Miguel)
- Added maintainer section for BITOPS API BINDINGS [RUST] (Yury)
- Added M: entry for bitmap.rs which goes to Alice (Viresh, Alice)
- Changed calls from find_* to _find_*, removed helpers (Yury)
- Use non-atomic __set_bit and __clear_bit from Bitmap Rust API (Yury)

Link [1] https://lore.kernel.org/all/[email protected]/
Link [2] https://lore.kernel.org/rust-for-linux/[email protected]/
Link [v5]: https://lore.kernel.org/lkml/[email protected]/

Burak Emir (4):
rust: add bindings for bitmap.h
rust: add bindings for bitops.h
rust: add bitmap API.
rust: add dynamic ID pool abstraction for bitmap

 MAINTAINERS                     |  14 ++
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   9 +
 rust/helpers/bitops.c           |  23 +++
 rust/helpers/helpers.c          |   2 +
 rust/kernel/bitmap.rs           | 306 ++++++++++++++++++++++++++++++++
 rust/kernel/id_pool.rs          | 201 +++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 8 files changed, 558 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/kernel/bitmap.rs
 create mode 100644 rust/kernel/id_pool.rs
Signature · 1 line
2.49.0.395.g12beb8f557-goog
author
#1 lore raw

PATCH v6 1/4 rust: add bindings for bitmap.h

Makes the bitmap_copy_and_extend inline function available to Rust. Adds F: to existing MAINTAINERS section BITMAP API BINDINGS [RUST].

---
 MAINTAINERS                     | 1 +
 rust/bindings/bindings_helper.h | 1 +
 rust/helpers/bitmap.c           | 9 +++++++++
 rust/helpers/helpers.c          | 1 +
 4 files changed, 12 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
MAINTAINERS +1 -0
index 1cd25139cc58..fcc56f7d7f16 100644
@@ -4031,6 +4031,7 @@ F:	tools/lib/find_bit.c
 BITMAP API BINDINGS [RUST]
 M:	Yury Norov <[email protected]>
 S:	Maintained
+F:	rust/helpers/bitmap.c
 F:	rust/helpers/cpumask.c
 
 BITOPS API
rust/bindings/bindings_helper.h +1 -0
index 2396ca1cf8fb..1d96fb915917 100644
@@ -7,6 +7,7 @@
  */
 
 #include <kunit/test.h>
+#include <linux/bitmap.h>
 #include <linux/blk-mq.h>
 #include <linux/blk_types.h>
 #include <linux/blkdev.h>
rust/helpers/bitmap.c new file +9 -0
new file mode 100644
index 000000000000..a50e2f082e47
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitmap.h>
+
+void rust_helper_bitmap_copy_and_extend(unsigned long *to, const unsigned long *from,
+		unsigned int count, unsigned int size)
+{
+	bitmap_copy_and_extend(to, from, count, size);
+}
rust/helpers/helpers.c +1 -0
index e1c21eba9b15..d4a60f1d6cc4 100644
@@ -7,6 +7,7 @@
  * Sorted alphabetically.
  */
 
+#include "bitmap.c"
 #include "blk.c"
 #include "bug.c"
 #include "build_assert.c"
Signature · 1 line
2.49.0.395.g12beb8f557-goog
author
#2 lore raw

PATCH v6 2/4 rust: add bindings for bitops.h

In reply to Burak Emir

Makes atomic set_bit and clear_bit inline functions as well as the non-atomic variants __set_bit and __clear_bit available to Rust. Adds a new MAINTAINERS section BITOPS API BINDINGS [RUST].

---
 MAINTAINERS            |  5 +++++
 rust/helpers/bitops.c  | 23 +++++++++++++++++++++++
 rust/helpers/helpers.c |  1 +
 3 files changed, 29 insertions(+)
 create mode 100644 rust/helpers/bitops.c
MAINTAINERS +5 -0
index fcc56f7d7f16..11bc11945838 100644
@@ -4048,6 +4048,11 @@ F:	include/linux/bitops.h
 F:	lib/test_bitops.c
 F:	tools/*/bitops*
 
+BITOPS API BINDINGS [RUST]
+M:	Yury Norov <[email protected]>
+S:	Maintained
+F:	rust/helpers/bitops.c
+
 BLINKM RGB LED DRIVER
 M:	Jan-Simon Moeller <[email protected]>
 S:	Maintained
rust/helpers/bitops.c new file +23 -0
new file mode 100644
index 000000000000..1fe9e3b23a39
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitops.h>
+
+void rust_helper___set_bit(unsigned int nr, unsigned long *addr)
+{
+	__set_bit(nr, addr);
+}
+
+void rust_helper___clear_bit(unsigned int nr, unsigned long *addr)
+{
+	__clear_bit(nr, addr);
+}
+
+void rust_helper_set_bit(unsigned int nr, volatile unsigned long *addr)
+{
+	set_bit(nr, addr);
+}
+
+void rust_helper_clear_bit(unsigned int nr, volatile unsigned long *addr)
+{
+	clear_bit(nr, addr);
+}
rust/helpers/helpers.c +1 -0
index d4a60f1d6cc4..0c25cc86a52a 100644
@@ -8,6 +8,7 @@
  */
 
 #include "bitmap.c"
+#include "bitops.c"
 #include "blk.c"
 #include "bug.c"
 #include "build_assert.c"
Signature · 1 line
2.49.0.395.g12beb8f557-goog
author
#3 lore raw

PATCH v6 3/4 rust: add bitmap API.

In reply to Burak Emir

Provides an abstraction for C bitmap API and bitops operations. Includes enough to implement a Binder data structure that was introduced in commit 15d9da3f818c ("binder: use bitmap for faster descriptor lookup"), namely drivers/android/dbitmap.h.

The implementation is optimized to represent the bitmap inline if it would take the space of a pointer. This saves allocations. We offer a safe API through bounds checks which panic if violated.

Atomic variants set_bit_atomic and clear_bit_atomic are provided. For these, absence of data races is ensured by the Rust type system:
all non-atomic operations require a &mut reference which amounts to exclusive access.

We use the usize type for sizes and indices into the bitmap, because Rust generally always uses that type for indices and lengths and it will be more convenient if the API accepts that type. This means that we need to perform some casts to/from u32 and usize, since the C headers use unsigned int instead of size_t/unsigned long for these numbers in some places.

Adds new MAINTAINERS section BITMAP API [RUST].

---
 MAINTAINERS           |   7 +
 rust/kernel/bitmap.rs | 306 ++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs    |   1 +
 3 files changed, 314 insertions(+)
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +7 -0
index 11bc11945838..efb0d367dea2 100644
@@ -4034,6 +4034,13 @@ S:	Maintained
 F:	rust/helpers/bitmap.c
 F:	rust/helpers/cpumask.c
 
+BITMAP API [RUST]
+M:	Alice Ryhl <[email protected]>
+M:	Burak Emir <[email protected]>
+R:	Yury Norov <[email protected]>
+S:	Maintained
+F:	rust/kernel/bitmap.rs
+
 BITOPS API
 M:	Yury Norov <[email protected]>
 R:	Rasmus Villemoes <[email protected]>
rust/kernel/bitmap.rs new file +306 -0

306 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index 7697c60b2d1a..c82b23236056 100644
@@ -36,6 +36,7 @@
 pub use ffi;
 
 pub mod alloc;
+pub mod bitmap;
 #[cfg(CONFIG_BLOCK)]
 pub mod block;
 #[doc(hidden)]
Signature · 1 line
2.49.0.395.g12beb8f557-goog
author
#6 lore raw

PATCH v6 4/4 rust: add dynamic ID pool abstraction for bitmap

In reply to Burak Emir

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.

---
 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
MAINTAINERS +1 -0
index efb0d367dea2..13bc1c695858 100644
@@ -4040,6 +4040,7 @@ M:	Burak Emir <[email protected]>
 R:	Yury Norov <[email protected]>
 S:	Maintained
 F:	rust/kernel/bitmap.rs
+F:	rust/kernel/id_pool.rs
 
 BITOPS API
 M:	Yury Norov <[email protected]>
rust/kernel/id_pool.rs new file +201 -0

201 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index c82b23236056..fc19d97f7b9b 100644
@@ -51,6 +51,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;
Signature · 1 line
2.49.0.395.g12beb8f557-goog
author
#4 lore raw

Re: [PATCH v6 3/4] rust: add bitmap API.

In reply to Burak Emir
Quoted 188 lines · Burak Emir

Provides an abstraction for C bitmap API and bitops operations. Includes enough to implement a Binder data structure that was introduced in commit 15d9da3f818c ("binder: use bitmap for faster descriptor lookup"), namely drivers/android/dbitmap.h.

The implementation is optimized to represent the bitmap inline if it would take the space of a pointer. This saves allocations. We offer a safe API through bounds checks which panic if violated.

Atomic variants set_bit_atomic and clear_bit_atomic are provided. For these, absence of data races is ensured by the Rust type system:
all non-atomic operations require a &mut reference which amounts to exclusive access.

We use the usize type for sizes and indices into the bitmap, because Rust generally always uses that type for indices and lengths and it will be more convenient if the API accepts that type. This means that we need to perform some casts to/from u32 and usize, since the C headers use unsigned int instead of size_t/unsigned long for these numbers in some places.

Adds new MAINTAINERS section BITMAP API [RUST].

---
 MAINTAINERS           |   7 +
 rust/kernel/bitmap.rs | 306 ++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs    |   1 +
 3 files changed, 314 insertions(+)
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +7 -0
index 11bc11945838..efb0d367dea2 100644
@@ -4034,6 +4034,13 @@ S:       Maintained
 F:     rust/helpers/bitmap.c
 F:     rust/helpers/cpumask.c
 
+BITMAP API [RUST]
+M:     Alice Ryhl <[email protected]>
+M:     Burak Emir <[email protected]>
+R:     Yury Norov <[email protected]>
+S:     Maintained
+F:     rust/kernel/bitmap.rs
+
 BITOPS API
 M:     Yury Norov <[email protected]>
 R:     Rasmus Villemoes <[email protected]>
rust/kernel/bitmap.rs new file +130 -0
new file mode 100644
index 000000000000..2622af3af1ec
@@ -0,0 +1,306 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// Copyright (C) 2025 Google LLC.
+
+//! Rust API for bitmap.
+//!
+//! C headers: [`include/linux/bitmap.h`](srctree/include/linux/bitmap.h).
+
+use crate::alloc::{AllocError, Flags};
+use crate::bindings;
+use core::ptr::NonNull;
+
+/// Holds either a pointer to array of `unsigned long` or a small bitmap.
+#[repr(C)]
+union BitmapRepr {
+    bitmap: usize,
+    ptr: NonNull<usize>,
+}
+
+/// Represents a bitmap.
+///
+/// Wraps underlying C bitmap API.
+///
+/// # Examples
+///
+/// Basic usage
+///
+/// ```
+/// use kernel::alloc::flags::GFP_KERNEL;
+/// use kernel::bitmap::Bitmap;
+///
+/// let mut b = Bitmap::new(16, GFP_KERNEL)?;
+///
+/// assert_eq!(16, b.len());
+/// for i in 0..16 {
+///     if i % 4 == 0 {
+///       b.set_bit(i);
+///     }
+/// }
+/// assert_eq!(Some(1), b.next_zero_bit(0));
+/// assert_eq!(Some(5), b.next_zero_bit(5));
+/// assert_eq!(Some(12), b.last_bit());
+/// # Ok::<(), Error>(())
+/// ```
+///
+/// Requesting too large values results in [`AllocError`]
+///
+/// ```
+/// use kernel::alloc::flags::GFP_KERNEL;
+/// use kernel::bitmap::Bitmap;
+///
+/// assert!(Bitmap::new(1 << 31, GFP_KERNEL).is_err());
+/// ```
+///
+/// # Invariants
+///
+/// * `nbits` is `<= i32::MAX` and never changes.
+/// * if `nbits <= bindings::BITS_PER_LONG`, then `repr` is a bitmap.
+/// * otherwise, `repr` holds a non-null pointer that was obtained from a
+///   successful call to `bitmap_zalloc` and holds the address of an initialized
+///   array of `unsigned long` that is large enough to hold `nbits` bits.
+pub struct Bitmap {
+    /// Representation of bitmap.
+    repr: BitmapRepr,
+    /// Length of this bitmap. Must be `<= i32::MAX`.
+    nbits: usize,
+}
+
+impl Drop for Bitmap {
+    fn drop(&mut self) {
+        if self.nbits <= bindings::BITS_PER_LONG as _ {
+            return;
+        }
+        // SAFETY: `self.ptr` was returned by the C `bitmap_zalloc`.
+        //
+        // INVARIANT: there is no other use of the `self.ptr` after this
+        // call and the value is being dropped so the broken invariant is
+        // not observable on function exit.
+        unsafe { bindings::bitmap_free(self.as_mut_ptr()) };
+    }
+}
+
+impl Bitmap {
+    /// Constructs a new [`Bitmap`].
+    ///
+    /// Fails with [`AllocError`] when the [`Bitmap`] could not be allocated. This
+    /// includes the case when `nbits` is greater than `i32::MAX`.
+    #[inline]
+    pub fn new(nbits: usize, flags: Flags) -> Result<Self, AllocError> {
+        if nbits <= bindings::BITS_PER_LONG as _ {
+            return Ok(Bitmap {
+                repr: BitmapRepr { bitmap: 0 },
+                nbits,
+            });
+        }
+        if nbits > i32::MAX.try_into().unwrap() {
+            return Err(AllocError);
+        }
+        let nbits_u32 = u32::try_from(nbits).unwrap();
+        // SAFETY: `bindings::BITS_PER_LONG < nbits` and `nbits <= i32::MAX`.
+        let ptr = unsafe { bindings::bitmap_zalloc(nbits_u32, flags.as_raw()) };
+        let ptr = NonNull::new(ptr).ok_or(AllocError)?;
+        // INVARIANT: `ptr` returned by C `bitmap_zalloc` and `nbits` checked.
+        return Ok(Bitmap {
+            repr: BitmapRepr { ptr },
+            nbits,
+        });
+    }
+
+    /// Returns length of this [`Bitmap`].
+    #[inline]
+    pub fn len(&self) -> usize {
+        self.nbits
+    }
+
+    /// Returns a mutable raw pointer to the backing [`Bitmap`].
+    #[inline]
+    fn as_mut_ptr(&mut self) -> *mut usize {
+        if self.nbits <= bindings::BITS_PER_LONG as _ {
+            // SAFETY: Bitmap is represented inline.
+            unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }
+        } else {
+            // SAFETY: Bitmap is represented as array of `unsigned long`.
+            unsafe { self.repr.ptr.as_mut() }
+        }
+    }
+
+    /// Returns a raw pointer to the backing [`Bitmap`].
+    #[inline]
+    fn as_ptr(&self) -> *const usize {

rust/kernel/bitmap.rs line 140

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+        if self.nbits <= bindings::BITS_PER_LONG as _ {
+            // SAFETY: Bitmap is represented inline.
+            unsafe { core::ptr::addr_of!(self.repr.bitmap) }
+        } else {
+            // SAFETY: Bitmap is represented as array of `unsigned long`.
+            unsafe { self.repr.ptr.as_ptr() }
+        }
+    }
+
+    /// Set bit with index `index`.

I missed this, will change to /// Set index bit,

Quoted 7 lines
rust/kernel/bitmap.rs new file +7 -0
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]
+    pub fn set_bit(&mut self, index: usize) {
+        assert!(

rust/kernel/bitmap.rs line 157

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: Bit `index` is within bounds.
+        unsafe { bindings::__set_bit(index as u32, self.as_mut_ptr()) };
+    }
+
+    /// Set bit with index `index`, atomically.

dto, will change to /// Set index bit, atomically.

rust/kernel/bitmap.rs line 159

Quoted 2 lines
rust/kernel/bitmap.rs new file +2 -0
+    ///
+    /// WARNING: this is a relaxed atomic operation (no implied memory barriers).

Is this the kind of warning you had in mind?

Quoted 5 lines
rust/kernel/bitmap.rs new file +5 -0
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]

rust/kernel/bitmap.rs line 174

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+    pub fn set_bit_atomic(&self, index: usize) {
+        assert!(
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: `index` is within bounds and there cannot be any data races
+        // because all non-atomic operations require exclusive access through
+        // a &mut reference.

I have considered marking set_bit_atomic as unsafe, but then come around to think that it is actually safe.

I'd appreciate a review of the reasoning by my fellow Rust-for-Linux folks.

What must be ensured is absence of data race, e.g. that an atomic op does not happen concurrently with a conflicting non-synchronized, non-atomic op.
Do I need to worry about non-atomic accesses in the same thread (temporarily reborrowing a &mut to & in the same thread is a possibility)?

Quoted 147 lines
rust/kernel/bitmap.rs new file +132 -0
+        unsafe { bindings::set_bit(index as u32, self.as_ptr() as *mut usize) };
+    }
+
+    /// Clear `index` bit.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]
+    pub fn clear_bit(&mut self, index: usize) {
+        assert!(
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: `index` is within bounds.
+        unsafe { bindings::__clear_bit(index as u32, self.as_mut_ptr()) };
+    }
+
+    /// Clear `index` bit, atomically.
+    ///
+    /// WARNING: this is a relaxed atomic operation (no implied memory barriers).
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]
+    pub fn clear_bit_atomic(&self, index: usize) {
+        assert!(
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: `index` is within bounds and there cannot be any data races
+        // because all non-atomic operations require exclusive access through
+        // a &mut reference.
+        unsafe { bindings::clear_bit(index as u32, self.as_ptr() as *mut usize) };
+    }
+
+    /// Copy `src` into this [`Bitmap`] and set any remaining bits to zero.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::alloc::{AllocError, flags::GFP_KERNEL};
+    /// use kernel::bitmap::Bitmap;
+    ///
+    /// let mut long_bitmap = Bitmap::new(256, GFP_KERNEL)?;
+    //
+    /// assert_eq!(None, long_bitmap.last_bit());
+    //
+    /// let mut short_bitmap = Bitmap::new(16, GFP_KERNEL)?;
+    //
+    /// short_bitmap.set_bit(7);
+    /// long_bitmap.copy_and_extend(&short_bitmap);
+    /// assert_eq!(Some(7), long_bitmap.last_bit());
+    ///
+    /// long_bitmap.clear_bit(7);
+    /// assert_eq!(None, long_bitmap.last_bit());
+    ///
+    /// # Ok::<(), AllocError>(())
+    /// ```
+    #[inline]
+    pub fn copy_and_extend(&mut self, src: &Bitmap) {
+        let len = core::cmp::min(src.nbits, self.nbits);
+        // SAFETY: access to `self` and `src` is within bounds.
+        unsafe {
+            bindings::bitmap_copy_and_extend(
+                self.as_mut_ptr(),
+                src.as_ptr(),
+                len as u32,
+                self.nbits as u32,
+            )
+        };
+    }
+
+    /// Finds last set bit.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::alloc::{AllocError, flags::GFP_KERNEL};
+    /// use kernel::bitmap::Bitmap;
+    ///
+    /// let bitmap = Bitmap::new(64, GFP_KERNEL)?;
+    ///
+    /// match bitmap.last_bit() {
+    ///     Some(idx) => {
+    ///         pr_info!("The last bit has index {idx}.\n");
+    ///     }
+    ///     None => {
+    ///         pr_info!("All bits in this bitmap are 0.\n");
+    ///     }
+    /// }
+    /// # Ok::<(), AllocError>(())
+    /// ```
+    #[inline]
+    pub fn last_bit(&self) -> Option<usize> {
+        // SAFETY: access is within bounds.
+        let index = unsafe { bindings::_find_last_bit(self.as_ptr(), self.nbits) };
+        if index == self.nbits {
+            None
+        } else {
+            Some(index)
+        }
+    }
+
+    /// Finds next zero bit, starting from `start`.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]
+    pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
+        assert!(
+            start < self.nbits,
+            "`start` must be < {}, was {}",
+            self.nbits,
+            start
+        );
+
+        // SAFETY: access is within bounds.
+        let index = unsafe { bindings::_find_next_zero_bit(self.as_ptr(), self.nbits, start) };
+        if index == self.nbits {
+            None
+        } else {
+            Some(index)
+        }
+    }
+}
rust/kernel/lib.rs +1 -1
index 7697c60b2d1a..c82b23236056 100644
@@ -36,6 +36,7 @@
 pub use ffi;
 
 pub mod alloc;
+pub mod bitmap;
 #[cfg(CONFIG_BLOCK)]
 pub mod block;
 #[doc(hidden)]
--

2.49.0.395.g12beb8f557-goog

maintainer
#7 lore raw
In reply to Burak Emir
Quoted 8 lines · Burak Emir

This series adds a Rust bitmap API for porting the approach from commit 15d9da3f818c ("binder: use bitmap for faster descriptor lookup") to Rust. The functionality in dbitmap.h makes use of bitmap and bitops.

The Rust bitmap API provides a safe abstraction to underlying bitmap and bitops operations. For now, only includes method necessary for dbitmap.h, more can be added later. We perform bounds checks for hardening, violations are programmer errors that result in panics.

So, I hung around and found the bit_set crate. Rust implements BitSet, BitVec, and it seems it has functionality that you bind here.

I didn't find any discussions related to the bit_set in kernel context. Is it possible to use it in kernel? If not, can you mention that in commit message? If yes, I think you should consider to use internal language tools.

Quoted 14 lines

We include set_bit_atomic and clear_bit_atomic operations. One has to avoid races with non-atomic operations, which is ensure by the Rust type system: either callers have shared references &bitmap in which case the mutations are atomic operations. Or there is a exclusive reference &mut bitmap, in which case there is no concurrent access.

This version includes an optimization to represent the bitmap inline, as suggested by Yury.

We introduce a Rust API that would replace (dbitmap.h) in file id_pool.rs. This data structure is tightly coupled to the bitmap API. Includes an example of usage that requires releasing a spinlock, as expected in Binder driver.

Quoted 7 lines

This is v6 of a patch introducing Rust bitmap API [v5]. Thanks for all the helpful comments, this series has improved significantly as a result of your work.

Not adding separate unit tests: the Rust unit test infrastructure is very new, and there does not seem to be benchmarking support for Rust tests yet.

I don't understand this.

Benchmarking is a very simple procedure - as simple as surrounding blocks of tested code with ktime_get(). And I see that Alice even implemented the Ktime class last year.

Quoted 3 lines

Are the # Examples tests enough?
Alternatively, can we add more test cases to those until the unit test infrastructure is in place?

I encourage you to implement the tests as normal kernel tests - in source files that may be enabled in config. I can't insist on that, and will not block the series because of lack of benchmarks and tests written in a traditional way.

But to me, scattered wrongly formatted commented-out in-place way of writing tests is something fundamentally wrong. Not mentioning that it bloats source files, making them harder to read.

Thanks,
Yury

maintainer
#5 lore raw

Re: [PATCH v6 3/4] rust: add bitmap API.

In reply to Burak Emir
Quoted 24 lines · Burak Emir
Quoted 2 lines · Burak Emir

+ /// Set bit with index index.

I missed this, will change to /// Set index bit,

Quoted 7 lines
rust/kernel/bitmap.rs new file +7 -0
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]
+    pub fn set_bit(&mut self, index: usize) {
+        assert!(

rust/kernel/bitmap.rs line 157

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: Bit `index` is within bounds.
+        unsafe { bindings::__set_bit(index as u32, self.as_mut_ptr()) };
+    }
+
+    /// Set bit with index `index`, atomically.
Quoted 6 lines

dto, will change to /// Set index bit, atomically.

Review

Quoted 2 lines
(quoted diff) +2 -0
+    ///
+    /// WARNING: this is a relaxed atomic operation (no implied memory barriers).

Is this the kind of warning you had in mind?

The __set_bit() in C and set_bit() in rust is a non-atomic function. Relaxed atomic API has a different meaning. Please add something like the following on top of 'pub fn set_bit()' implementation:

    /// ATTENTION: Contrary to C, the rust set_bit() method is non-atomic.
    /// This mismatches kernel naming convention and corresponds to the C
    /// function __set_bit(). For atomicity, use the set_bit_atomic() method.
Quoted 19 lines
Quoted 5 lines
rust/kernel/bitmap.rs new file +5 -0
+    ///
+    /// # Panics
+    ///
+    /// Panics if `index` is greater than or equal to `self.nbits`.
+    #[inline]

rust/kernel/bitmap.rs line 174

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+    pub fn set_bit_atomic(&self, index: usize) {
+        assert!(
+            index < self.nbits,
+            "Bit `index` must be < {}, was {}",
+            self.nbits,
+            index
+        );
+        // SAFETY: `index` is within bounds and there cannot be any data races
+        // because all non-atomic operations require exclusive access through
+        // a &mut reference.

I have considered marking set_bit_atomic as unsafe, but then come around to think that it is actually safe.

Quoted 8 lines

I'd appreciate a review of the reasoning by my fellow Rust-for-Linux folks.

What must be ensured is absence of data race, e.g. that an atomic op does not happen concurrently with a conflicting non-synchronized, non-atomic op.
Do I need to worry about non-atomic accesses in the same thread (temporarily reborrowing a &mut to & in the same thread is a possibility)?

To me - no. Atomicity only works if everyone follow the same rules. If someone accessed some data without grabbing a lock on it, and ended up corrupting the kernel, it's not a problem of spinlock API.

Thanks,
Yury

#8 lore raw
In reply to Yury Norov
Quoted 3 lines · Yury Norov

I didn't find any discussions related to the bit_set in kernel context. Is it possible to use it in kernel? If not, can you mention that in commit

In principle, from a very quick look, yes, it supports no_std and should be very easy to vendor since they don't have dependencies (i.e. bit_set and bit_vec).

Quoted 2 lines

message? If yes, I think you should consider to use internal language tools.

Hmm... if by "internal language tools" you mean a normal library (like that one you mention), then yeah, they can be considered.

In general, we have been prudent about using third-party libraries so far, but it is possible if it is the right choice -- please see:

https://rust-for-linux.com/third-party-crates#suitability-of-a-crate

So if everyone agrees that or another library would be the best fit than mimicking or using the C side (performance-wise, maintainability-wise, etc.), then I am happy to integrate it.

Quoted 8 lines

I encourage you to implement the tests as normal kernel tests - in source files that may be enabled in config. I can't insist on that, and will not block the series because of lack of benchmarks and tests written in a traditional way.

But to me, scattered wrongly formatted commented-out in-place way of writing tests is something fundamentally wrong. Not mentioning that it bloats source files, making them harder to read.

I don't think documentation makes things harder to read; quite the opposite -- if the docs are good.

But, yeah, if it is an actual test that is not suitable as an example, then it should be a separate test (whether as a #[test] one, if that works already for this use case, or as a sample module otherwise).

Thanks!

Cheers,
Miguel

author
#9 lore raw
Quoted 16 lines · Miguel Ojeda
Quoted 3 lines · Yury Norov

I didn't find any discussions related to the bit_set in kernel context. Is it possible to use it in kernel? If not, can you mention that in commit

In principle, from a very quick look, yes, it supports no_std and should be very easy to vendor since they don't have dependencies (i.e. bit_set and bit_vec).

Quoted 2 lines

message? If yes, I think you should consider to use internal language tools.

Hmm... if by "internal language tools" you mean a normal library (like that one you mention), then yeah, they can be considered.

Quoted 9 lines

In general, we have been prudent about using third-party libraries so far, but it is possible if it is the right choice -- please see:

https://rust-for-linux.com/third-party-crates#suitability-of-a-crate

So if everyone agrees that or another library would be the best fit than mimicking or using the C side (performance-wise, maintainability-wise, etc.), then I am happy to integrate it.

IMHO, we should strive to avoid parallel implementations of basic data structures.

This little patch series was started to replace a self-contained C code with Rust implementation. It may not representative for all Rust code, but building an abstraction over C code will make porting a lot easier to write and review.

Now, one can argue that Rust abstractions could use alternative implementations instead of binding the C code underneath. This opens the door to subtle differences in performance or correctness (e.g. the case with atomics and inner mutability).

Quoted 9 lines
Quoted 8 lines

I encourage you to implement the tests as normal kernel tests - in source files that may be enabled in config. I can't insist on that, and will not block the series because of lack of benchmarks and tests written in a traditional way.

But to me, scattered wrongly formatted commented-out in-place way of writing tests is something fundamentally wrong. Not mentioning that it bloats source files, making them harder to read.

Quoted 7 lines

I don't think documentation makes things harder to read; quite the opposite -- if the docs are good.

But, yeah, if it is an actual test that is not suitable as an example, then it should be a separate test (whether as a #[test] one, if that works already for this use case, or as a sample module otherwise).

Now that I figured out how to do it, I'll add the separate kunit test that exercise code paths which are not helpful in documentation.

Thanks,
Burak