Skip to contentSkip to conversation

rust: add bindings and API for bitmap.h and bitops.h.

PATCH Superseded Burak Emir 3 participants 4 messages Mar 3, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Burak Emir

author
#0 lore raw

PATCH v2 1/1 rust: add bindings and API for bitmap.h and bitops.h.

Adds a Rust bitmap API and necessary bitmap and bitops bindings. These are 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 an abstraction to underlying bitmap and bitops operations. For now, we only include methods that are necessary for reimplementing dbitmap.h. It is straightforward to add more methods later, as needed. We offer a safe API through bounds checks which panic if violated.

We introduce bindings for the non-atomic variants __set_bit and __clear_bit, and use the _find_* variants instead of the find_* wrappers. The C optimizations enabled by the wrappers does not carry over to Rust.

This series uses 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.

---
This is v2 of a patch introducing Rust bitmap API [v1]. Thanks for all the helpful comments!

Changes v1 --> v2:
- Rebased on Yury's v2 patch [1] and Viresh's v2 patch [2]
- 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)

Question to Yury: we could remove bitmap_copy helper and instead do the memcpy in Rust. Should we? If so, should we expose a helper for bitmap_size or duplicate the nbits-to-aligned-nlongs logic?

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[v1]: https://lore.kernel.org/all/[email protected]/

Thanks,
Burak

---
 MAINTAINERS                     |   8 ++
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/helpers.c          |   2 +
 rust/kernel/bitmap.rs           | 191 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 7 files changed, 224 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +8 -0
index 6d6e55d8593b..8f42fb1f24c6 100644
@@ -4032,12 +4032,15 @@ 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
 
 BITMAP API [RUST]
 M:	Viresh Kumar <[email protected]> (cpumask)
+M:	Alice Ryhl <[email protected]> (bitmap)
 R:	Yury Norov <[email protected]>
 S:	Maintained
+F:	rust/kernel/bitmap.rs
 F:	rust/kernel/cpumask.rs
 
 BITOPS API
@@ -4054,6 +4057,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/bindings/bindings_helper.h +1 -0
index 673b1daa9a58..50416c1a3de9 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 +8 -0
new file mode 100644
index 000000000000..4fa4e4f76110
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitmap.h>
+
+void rust_helper_bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits)
+{
+	bitmap_copy(dst, src, nbits);
+}
rust/helpers/bitops.c new file +13 -0
new file mode 100644
index 000000000000..986dafb45184
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitops.h>
+
+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 +2 -0
index de2341cfd917..541d8cb30195 100644
@@ -7,6 +7,8 @@
  * Sorted alphabetically.
  */
 
+#include "bitmap.c"
+#include "bitops.c"
 #include "blk.c"
 #include "bug.c"
 #include "build_assert.c"
rust/kernel/bitmap.rs new file +191 -0

191 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index efbd7be98dab..be06ffc47473 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.48.1.711.g2feabab25a-goog
maintainer
#1 lore raw
Quoted 114 lines · Burak Emir

Adds a Rust bitmap API and necessary bitmap and bitops bindings. These are 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 an abstraction to underlying bitmap and bitops operations. For now, we only include methods that are necessary for reimplementing dbitmap.h. It is straightforward to add more methods later, as needed. We offer a safe API through bounds checks which panic if violated.

We introduce bindings for the non-atomic variants __set_bit and __clear_bit, and use the _find_* variants instead of the find_* wrappers. The C optimizations enabled by the wrappers does not carry over to Rust.

This series uses 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.

---
This is v2 of a patch introducing Rust bitmap API [v1]. Thanks for all the helpful comments!

Changes v1 --> v2:
- Rebased on Yury's v2 patch [1] and Viresh's v2 patch [2]
- 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)

Question to Yury: we could remove bitmap_copy helper and instead do the memcpy in Rust. Should we? If so, should we expose a helper for bitmap_size or duplicate the nbits-to-aligned-nlongs logic?

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[v1]: https://lore.kernel.org/all/[email protected]/

Thanks,
Burak

---
 MAINTAINERS                     |   8 ++
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/helpers.c          |   2 +
 rust/kernel/bitmap.rs           | 191 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 7 files changed, 224 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +8 -0
index 6d6e55d8593b..8f42fb1f24c6 100644
@@ -4032,12 +4032,15 @@ 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
 
 BITMAP API [RUST]
 M:     Viresh Kumar <[email protected]> (cpumask)
+M:     Alice Ryhl <[email protected]> (bitmap)
 R:     Yury Norov <[email protected]>
 S:     Maintained
+F:     rust/kernel/bitmap.rs
 F:     rust/kernel/cpumask.rs
 
 BITOPS API
@@ -4054,6 +4057,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/bindings/bindings_helper.h +1 -0
index 673b1daa9a58..50416c1a3de9 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 +0 -0
new file mode 100644
index 000000000000..4fa4e4f76110

rust/helpers/bitmap.c line 8

Quoted 8 lines
rust/helpers/bitmap.c new file +8 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitmap.h>
+
+void rust_helper_bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits)
+{
+       bitmap_copy(dst, src, nbits);
+}

I was about to say that this could just be a memcpy, but ...

Quoted 6 lines
rust/helpers/bitmap.c new file +6 -0
+    /// Copy up to `nbits` bits from this bitmap into another.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `nbits` is too large for this bitmap or destination.
+    #[inline]

rust/helpers/bitmap.c

Quoted 10 lines
rust/helpers/bitmap.c new file +10 -0
+    pub fn bitmap_copy(&self, dst: &mut Bitmap, nbits: usize) {
+        if self.nbits < nbits {
+            panic_not_in_bounds_le("nbits", self.nbits, nbits);
+        }
+        if dst.nbits < nbits {
+            panic_not_in_bounds_le("nbits", dst.nbits, nbits);
+        }
+        // SAFETY: nbits == 0 is supported and access to `self` and `dst` is within bounds.
+        unsafe { bindings::bitmap_copy(dst.as_mut_ptr(), self.ptr.as_ptr(), nbits as u32) };
+    }

... then I realized that we're probably not using it correctly. I would expect this to modify the first nbits bits in dst, leaving any remaining bits unmodified. However, if nbits is not divisible by BITS_PER_LONG it might modify some bits it shouldn't.

That said, Binder needs this only in the case where the sizes are equal. Perhaps we could rename this to copy_from_bitmap with this signature:
fn copy_from_bitmap(&mut self, src: Bitmap)

and have the function require that the sizes are equal. This mirrors the standard library function copy_from_slice that takes two slices that must have the exact same size and copies the contents.

Alice

maintainer
#2 lore raw
Quoted 149 lines · Alice Ryhl
Quoted 114 lines · Burak Emir

Adds a Rust bitmap API and necessary bitmap and bitops bindings. These are 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 an abstraction to underlying bitmap and bitops operations. For now, we only include methods that are necessary for reimplementing dbitmap.h. It is straightforward to add more methods later, as needed. We offer a safe API through bounds checks which panic if violated.

We introduce bindings for the non-atomic variants __set_bit and __clear_bit, and use the _find_* variants instead of the find_* wrappers. The C optimizations enabled by the wrappers does not carry over to Rust.

This series uses 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.

---
This is v2 of a patch introducing Rust bitmap API [v1]. Thanks for all the helpful comments!

Changes v1 --> v2:
- Rebased on Yury's v2 patch [1] and Viresh's v2 patch [2]
- 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)

Question to Yury: we could remove bitmap_copy helper and instead do the memcpy in Rust. Should we? If so, should we expose a helper for bitmap_size or duplicate the nbits-to-aligned-nlongs logic?

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[v1]: https://lore.kernel.org/all/[email protected]/

Thanks,
Burak

---
 MAINTAINERS                     |   8 ++
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/helpers.c          |   2 +
 rust/kernel/bitmap.rs           | 191 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 7 files changed, 224 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +8 -0
index 6d6e55d8593b..8f42fb1f24c6 100644
@@ -4032,12 +4032,15 @@ 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
 
 BITMAP API [RUST]
 M:     Viresh Kumar <[email protected]> (cpumask)
+M:     Alice Ryhl <[email protected]> (bitmap)
 R:     Yury Norov <[email protected]>
 S:     Maintained
+F:     rust/kernel/bitmap.rs
 F:     rust/kernel/cpumask.rs
 
 BITOPS API
@@ -4054,6 +4057,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/bindings/bindings_helper.h +1 -0
index 673b1daa9a58..50416c1a3de9 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 +0 -0
new file mode 100644
index 000000000000..4fa4e4f76110

rust/helpers/bitmap.c line 8

Quoted 8 lines
rust/helpers/bitmap.c new file +8 -0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitmap.h>
+
+void rust_helper_bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits)
+{
+       bitmap_copy(dst, src, nbits);
+}

I was about to say that this could just be a memcpy, but ...

Quoted 6 lines
rust/helpers/bitmap.c new file +6 -0
+    /// Copy up to `nbits` bits from this bitmap into another.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `nbits` is too large for this bitmap or destination.
+    #[inline]

rust/helpers/bitmap.c

Quoted 10 lines
rust/helpers/bitmap.c new file +10 -0
+    pub fn bitmap_copy(&self, dst: &mut Bitmap, nbits: usize) {
+        if self.nbits < nbits {
+            panic_not_in_bounds_le("nbits", self.nbits, nbits);
+        }
+        if dst.nbits < nbits {
+            panic_not_in_bounds_le("nbits", dst.nbits, nbits);
+        }
+        // SAFETY: nbits == 0 is supported and access to `self` and `dst` is within bounds.
+        unsafe { bindings::bitmap_copy(dst.as_mut_ptr(), self.ptr.as_ptr(), nbits as u32) };
+    }

... then I realized that we're probably not using it correctly. I would expect this to modify the first nbits bits in dst, leaving any remaining bits unmodified. However, if nbits is not divisible by BITS_PER_LONG it might modify some bits it shouldn't.

Quoted 4 lines

That said, Binder needs this only in the case where the sizes are equal. Perhaps we could rename this to copy_from_bitmap with this signature:
fn copy_from_bitmap(&mut self, src: Bitmap)

Sorry I meant src: &Bitmap here.

Also, we could rewrite it to just call memcpy rather than go through bitmap_copy. Though that requires us to have a Rust version of bitmap_size, which I think it makes sense to avoid using a Rust helper for.

We could reimplement it by first computing the number of longs and then computing the number of bytes

const fn bitmap_size(nbits: usize) -> usize {
    nbits.div_ceil(c_ulong::BITS) * size_of::<c_ulong>()
}

Thoughts?

Alice

maintainer
#3 lore raw
Quoted 29 lines · Alice Ryhl
Quoted 29 lines

rust/helpers/bitmap.c line 8

Quoted 4 lines
rust/helpers/bitmap.c new file +4 -0
+void rust_helper_bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits)
+{
+       bitmap_copy(dst, src, nbits);
+}

I was about to say that this could just be a memcpy, but ...

Quoted 6 lines
rust/kernel/bitmap.rs new file +6 -0
+    /// Copy up to `nbits` bits from this bitmap into another.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `nbits` is too large for this bitmap or destination.
+    #[inline]

rust/kernel/bitmap.rs line 139

Quoted 10 lines
rust/kernel/bitmap.rs new file +10 -0
+    pub fn bitmap_copy(&self, dst: &mut Bitmap, nbits: usize) {
+        if self.nbits < nbits {
+            panic_not_in_bounds_le("nbits", self.nbits, nbits);
+        }
+        if dst.nbits < nbits {
+            panic_not_in_bounds_le("nbits", dst.nbits, nbits);
+        }
+        // SAFETY: nbits == 0 is supported and access to `self` and `dst` is within bounds.
+        unsafe { bindings::bitmap_copy(dst.as_mut_ptr(), self.ptr.as_ptr(), nbits as u32) };
+    }

... then I realized that we're probably not using it correctly. I would expect this to modify the first nbits bits in dst, leaving any remaining bits unmodified. However, if nbits is not divisible by BITS_PER_LONG it might modify some bits it shouldn't.

Quoted 6 lines
Quoted 4 lines

That said, Binder needs this only in the case where the sizes are equal. Perhaps we could rename this to copy_from_bitmap with this signature:
fn copy_from_bitmap(&mut self, src: Bitmap)

Sorry I meant src: &Bitmap here.

Yes, you're right. bitmap_copy() copies the whole bitmap. So if your Bitmap already has size, you don't need to pass it explicitly.

Quoted 4 lines

Also, we could rewrite it to just call memcpy rather than go through bitmap_copy. Though that requires us to have a Rust version of bitmap_size, which I think it makes sense to avoid using a Rust helper for.

No, you couldn't. I don't want rust bindings to diverge from the main kernel. So the rule is simple: if inline wrapper exists only for the small_const_nbits() optimization - go ahead and use the outlined underscored version. If the wrapper does something else - no matter what - it should be wrapped.

Quoted 10 lines

We could reimplement it by first computing the number of longs and then computing the number of bytes

const fn bitmap_size(nbits: usize) -> usize {
    nbits.div_ceil(c_ulong::BITS) * size_of::<c_ulong>()
}

Thoughts?

Alice