rust: add bindings and API for bitmap.h and bitops.h.
Subsystems (4)
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
MAINTAINERS BITMAP API BINDINGS [RUST] · line 4040
Quoted 5 lines · Burak Emir
Question for Yury: What would you like us to do for the
MAINTAINERSfile? For now I just added the new files as F: entries.MAINTAINERS +0
-0BITMAP 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
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
+/// *
ptris obtained from a successful call tobitmap_zallocand
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: &'staticstr , 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
Yury Norov
maintainerQuoted 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 indbitmap.hmakes 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.Suggested-by Alice Ryhl <[email protected]>Signed-off-by Burak Emir <[email protected]>---
This series adds a Rust abstraction for bitmap, and binding helpers for inline methods ofbitmap.hbitops.h.It depends on [1] and [2] which add bitmap helpers,
MAINTAINERSentries and an abstraction that is part of the bitmaps Rust API.Question for Yury: What would you like us to do for the
MAINTAINERSfile? 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.rsMAINTAINERS +0
-0
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
-0BITMAP 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@@ -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@@ -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@@ -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@@ -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
Alice Ryhl
maintainerQuoted 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
MAINTAINERSfile? For now I just added the new files as F: entries.MAINTAINERS +0
-0BITMAP 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
Alice Ryhl
maintainerQuoted 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 indbitmap.hmakes 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.Suggested-by Alice Ryhl <[email protected]>Signed-off-by Burak Emir <[email protected]>---
This series adds a Rust abstraction for bitmap, and binding helpers for inline methods ofbitmap.hbitops.h.It depends on [1] and [2] which add bitmap helpers,
MAINTAINERSentries and an abstraction that is part of the bitmaps Rust API.Question for Yury: What would you like us to do for the
MAINTAINERSfile? 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.rsMAINTAINERS +0
-0MAINTAINERS 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.cis part of BITOPS API, not the BITMAP one. I think we need a new record for it?Quoted 45 lines
MAINTAINERS +1
-0BITMAP 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@@ -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@@ -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@@ -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 theset_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@@ -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