Skip to contentSkip to conversation

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

PATCH Superseded Burak Emir 5 participants 6 messages Feb 27, 2025 – Feb 28, 2025

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Burak Emir

author
#0 lore raw

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

Adds a bitmap and bitops bindings and bitmap Rust API. 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 bitmaps 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.

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.

Adds F: entries to MAINTAINERS.

---
This series adds a Rust abstraction for bitmap, and binding helpers for inline methods of bitmap.h bitops.h.

It depends on [1] and [2] which add bitmap helpers, MAINTAINERS entries and an abstraction that is part of the bitmaps Rust API.

Question for Yury: What would you like us to do for the MAINTAINERS file? For now I just added the new files as F: entries.

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

 MAINTAINERS                     |   4 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/find.c             |  15 +++
 rust/helpers/helpers.c          |   3 +
 rust/kernel/bitmap.rs           | 182 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 8 files changed, 227 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/helpers/find.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +4 -0
index 6d6e55d8593b..359f09e8e2c0 100644
@@ -4033,12 +4033,16 @@ BITMAP API BINDINGS [RUST]
 M:	Yury Norov <[email protected]>
 S:	Maintained
 F:	rust/helpers/cpumask.c
+F:	rust/helpers/find.c
+F:	rust/helpers/bitmap.c
+F:	rust/helpers/bitops.c
 
 BITMAP API [RUST]
 M:	Viresh Kumar <[email protected]> (cpumask)
 R:	Yury Norov <[email protected]>
 S:	Maintained
 F:	rust/kernel/cpumask.rs
+F:	rust/kernel/bitmap.rs
 
 BITOPS API
 M:	Yury Norov <[email protected]>
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..191ef0341fd5
@@ -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/find.c new file +15 -0
new file mode 100644
index 000000000000..3841d3f0330f
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/find.h>
+
+unsigned long rust_helper_find_last_bit(const unsigned long *addr, unsigned long size)
+{
+	return find_last_bit(addr, size);
+}
+
+
+unsigned long rust_helper_find_next_zero_bit(const unsigned long *addr, unsigned long size,
+				 unsigned long offset)
+{
+	return find_next_zero_bit(addr, size, offset);
+}
rust/helpers/helpers.c +3 -0
index de2341cfd917..86ebbc97a57d 100644
@@ -7,6 +7,8 @@
  * Sorted alphabetically.
  */
 
+#include "bitmap.c"
+#include "bitops.c"
 #include "blk.c"
 #include "bug.c"
 #include "build_assert.c"
@@ -15,6 +17,7 @@
 #include "cred.c"
 #include "device.c"
 #include "err.c"
+#include "find.c"
 #include "fs.c"
 #include "io.c"
 #include "jump_label.c"
rust/kernel/bitmap.rs new file +182 -0

182 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.658.g4767266eb4-goog
#1 lore raw

MAINTAINERS BITMAP API BINDINGS [RUST] · line 4040

Quoted 5 lines · Burak Emir

Question for Yury: What would you like us to do for the MAINTAINERS file? For now I just added the new files as F: entries.

MAINTAINERS +0 -0
 BITMAP API [RUST]

I would suggest adding this too.

+ M: Burak Emir <[email protected]> (bitmap)

Quoted 5 lines

M: Viresh Kumar <[email protected]> (cpumask)
R: Yury Norov <[email protected]>
S: Maintained
F: rust/kernel/cpumask.rs
+F: rust/kernel/bitmap.rs

Signature · 1 line
viresh
#3 lore raw
In reply to Burak Emir

rust/kernel/bitmap.rs line 12

Quoted 4 lines · Burak Emir
rust/kernel/bitmap.rs new file +3 -0
+use crate::bindings::{
+    bitmap_copy, bitmap_free, bitmap_zalloc, clear_bit, find_last_bit, find_next_zero_bit, set_bit,
+};

I think it may be best to avoid importing bindings::* things, so that it is clear when they happen and so that they are easier to grep for.

I guess some of these may benefit from being inline when small constants are involved, similar to what the C docs mention -- do we want to have equivalents to the C "generic" ones that could be inlined well?

Quoted 1 line

+/// * ptr is obtained from a successful call to bitmap_zalloc and

Please use Markdown wherever possible, e.g. bitmap_zalloc.

rust/kernel/bitmap.rs line 39

Quoted 4 lines
rust/kernel/bitmap.rs new file +4 -0
+#[cold]
+fn not_in_bounds_lt(arg: &'static str, len: usize, val: usize) -> ! {
+    panic!("{arg} must be less than length {len}, was {val}");
+}

Should these be more explicit? e.g. panic_if_not_in_bounds_lt?

Do we want to only have infallible versions?

rust/kernel/bitmap.rs line 47

Quoted 2 lines
rust/kernel/bitmap.rs new file +2 -0
+impl Bitmap {
+    /// Constructs a new Bitmap.

Please use intra-doc links where possible (and Markdown), e.g. [Bitmap].

rust/kernel/bitmap.rs line 50

Quoted 2 lines
rust/kernel/bitmap.rs new file +2 -0
+    /// Fails with AllocError if `nbits` is greater than or equal to 2^32,
+    /// or when the bitmap could not be allocated.

Some examples would be nice to add, which double as KUnit tests.

Thanks!

Cheers,
Miguel

maintainer
#4 lore raw
In reply to Burak Emir
Quoted 54 lines · Burak Emir

Adds a bitmap and bitops bindings and bitmap Rust API. 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 bitmaps 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.

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.

Adds F: entries to MAINTAINERS.

---
This series adds a Rust abstraction for bitmap, and binding helpers for inline methods of bitmap.h bitops.h.

It depends on [1] and [2] which add bitmap helpers, MAINTAINERS entries and an abstraction that is part of the bitmaps Rust API.

Question for Yury: What would you like us to do for the MAINTAINERS file? For now I just added the new files as F: entries.

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

 MAINTAINERS                     |   4 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/find.c             |  15 +++
 rust/helpers/helpers.c          |   3 +
 rust/kernel/bitmap.rs           | 182 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 8 files changed, 227 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/helpers/find.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +0 -0
index 6d6e55d8593b..359f09e8e2c0 100644

MAINTAINERS BITMAP API BINDINGS [RUST] · line 4038

Quoted 6 lines
MAINTAINERS +3 -0
@@ -4033,12 +4033,16 @@ BITMAP API BINDINGS [RUST]
 M:	Yury Norov <[email protected]>
 S:	Maintained
 F:	rust/helpers/cpumask.c
+F:	rust/helpers/find.c
+F:	rust/helpers/bitmap.c
+F:	rust/helpers/bitops.c

bitops.c is part of BITOPS API, not the BITMAP one. I think we need a new record for it?

Quoted 45 lines
MAINTAINERS +1 -0
 BITMAP API [RUST]
 M:	Viresh Kumar <[email protected]> (cpumask)
 R:	Yury Norov <[email protected]>
 S:	Maintained
 F:	rust/kernel/cpumask.rs
+F:	rust/kernel/bitmap.rs
 
 BITOPS API
 M:	Yury Norov <[email protected]>
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 +3 -0
new file mode 100644
index 000000000000..191ef0341fd5
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitops.h>

rust/helpers/bitops.c line 13

Quoted 10 lines
rust/helpers/bitops.c new file +10 -0
+
+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);
+}

So you mention that you're rewriting dbitmap, and I took a brief look at drivers/android/dbitmap.h.

What I can say is that at least dbitmap_acquire_next_zero_bit() abuses the set_bit() API. It should use non-atomic __set_bit(). If you're going to re-write it, can you review the existing code and make sure you're using the right API in a right way?

Quoted 11 lines
rust/helpers/find.c new file +5 -0
new file mode 100644
index 000000000000..3841d3f0330f
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/find.h>
+
+unsigned long rust_helper_find_last_bit(const unsigned long *addr, unsigned long size)

rust/helpers/find.c line 15

Quoted 10 lines
rust/helpers/find.c new file +10 -0
+{
+	return find_last_bit(addr, size);
+}
+
+
+unsigned long rust_helper_find_next_zero_bit(const unsigned long *addr, unsigned long size,
+				 unsigned long offset)
+{
+	return find_next_zero_bit(addr, size, offset);
+}

For those two, the find_*_bit() are the wrappers around _find_*_bit() in lib/find_bit.c, with a few exceptions. The reason for having the wrappers is an optimization that improves code generation for small bitmaps.

In your case, when you wrap a macro, the optimization becomes impossible, and the macro is unneeded.

From technical perspective, the underscored _find_*_bit() functions should be accessible by rust directly. Can you confirm that? If so, maybe just use them and avoid bloating?

Thanks,
Yury

maintainer
#2 lore raw
In reply to Viresh Kumar
Quoted 11 lines · Viresh Kumar

MAINTAINERS BITMAP API BINDINGS [RUST] · line 4040

Quoted 5 lines · Burak Emir

Question for Yury: What would you like us to do for the MAINTAINERS file? For now I just added the new files as F: entries.

MAINTAINERS +0 -0
 BITMAP API [RUST]

I would suggest adding this too.

+ M: Burak Emir <[email protected]> (bitmap)

If a new maintainer is needed, it should probably be me. Burak is helping me out with some Binder changes, but I will be the owner of the Binder code.

Alice

maintainer
#5 lore raw
In reply to Yury Norov
Quoted 125 lines · Yury Norov
Quoted 54 lines · Burak Emir

Adds a bitmap and bitops bindings and bitmap Rust API. 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 bitmaps 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.

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.

Adds F: entries to MAINTAINERS.

---
This series adds a Rust abstraction for bitmap, and binding helpers for inline methods of bitmap.h bitops.h.

It depends on [1] and [2] which add bitmap helpers, MAINTAINERS entries and an abstraction that is part of the bitmaps Rust API.

Question for Yury: What would you like us to do for the MAINTAINERS file? For now I just added the new files as F: entries.

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

 MAINTAINERS                     |   4 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/bitmap.c           |   8 ++
 rust/helpers/bitops.c           |  13 +++
 rust/helpers/find.c             |  15 +++
 rust/helpers/helpers.c          |   3 +
 rust/kernel/bitmap.rs           | 182 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 8 files changed, 227 insertions(+)
 create mode 100644 rust/helpers/bitmap.c
 create mode 100644 rust/helpers/bitops.c
 create mode 100644 rust/helpers/find.c
 create mode 100644 rust/kernel/bitmap.rs
MAINTAINERS +0 -0
index 6d6e55d8593b..359f09e8e2c0 100644

MAINTAINERS BITMAP API BINDINGS [RUST] · line 4038

Quoted 6 lines
MAINTAINERS +3 -0
@@ -4033,12 +4033,16 @@ BITMAP API BINDINGS [RUST]
 M:   Yury Norov <[email protected]>
 S:   Maintained
 F:   rust/helpers/cpumask.c
+F:   rust/helpers/find.c
+F:   rust/helpers/bitmap.c
+F:   rust/helpers/bitops.c

bitops.c is part of BITOPS API, not the BITMAP one. I think we need a new record for it?

Quoted 45 lines
MAINTAINERS +1 -0
 BITMAP API [RUST]
 M:   Viresh Kumar <[email protected]> (cpumask)
 R:   Yury Norov <[email protected]>
 S:   Maintained
 F:   rust/kernel/cpumask.rs
+F:   rust/kernel/bitmap.rs
 
 BITOPS API
 M:   Yury Norov <[email protected]>
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 +3 -0
new file mode 100644
index 000000000000..191ef0341fd5
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bitops.h>

rust/helpers/bitops.c line 13

Quoted 10 lines
rust/helpers/bitops.c new file +10 -0
+
+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);
+}

So you mention that you're rewriting dbitmap, and I took a brief look at drivers/android/dbitmap.h.

Quoted 4 lines

What I can say is that at least dbitmap_acquire_next_zero_bit() abuses the set_bit() API. It should use non-atomic __set_bit(). If you're going to re-write it, can you review the existing code and make sure you're using the right API in a right way?

Huh, I did not realize the method was atomic. I had assumed it wasn't from how Binder is using it.

Quoted 27 lines
Quoted 11 lines
rust/helpers/find.c new file +5 -0
new file mode 100644
index 000000000000..3841d3f0330f
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/find.h>
+
+unsigned long rust_helper_find_last_bit(const unsigned long *addr, unsigned long size)

rust/helpers/find.c line 15

Quoted 10 lines
rust/helpers/find.c new file +10 -0
+{
+     return find_last_bit(addr, size);
+}
+
+
+unsigned long rust_helper_find_next_zero_bit(const unsigned long *addr, unsigned long size,
+                              unsigned long offset)
+{
+     return find_next_zero_bit(addr, size, offset);
+}

For those two, the find_*_bit() are the wrappers around _find_*_bit() in lib/find_bit.c, with a few exceptions. The reason for having the wrappers is an optimization that improves code generation for small bitmaps.

Quoted 6 lines

In your case, when you wrap a macro, the optimization becomes impossible, and the macro is unneeded.

From technical perspective, the underscored _find_*_bit() functions should be accessible by rust directly. Can you confirm that? If so, maybe just use them and avoid bloating?

That sounds likely.

Alice