| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::alloc::{AllocError, Flags};
|
| 10 | use crate::bindings;
|
| 11 | #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
|
| 12 | use crate::pr_err;
|
| 13 | use core::ptr::NonNull;
|
| 14 |
|
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #[cfg_attr(CONFIG_64BIT, repr(align(8)))]
|
| 21 | #[cfg_attr(not(CONFIG_64BIT), repr(align(4)))]
|
| 22 | pub struct Bitmap {
|
| 23 | data: [()],
|
| 24 | }
|
| 25 |
|
| 26 | impl Bitmap {
|
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | pub unsafe fn from_raw<'a>(ptr: *const usize, nbits: usize) -> &'a Bitmap {
|
| 36 | let data: *const [()] = core::ptr::slice_from_raw_parts(ptr.cast(), nbits);
|
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | unsafe { &*(data as *const Bitmap) }
|
| 50 | }
|
| 51 |
|
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | pub unsafe fn from_raw_mut<'a>(ptr: *mut usize, nbits: usize) -> &'a mut Bitmap {
|
| 61 | let data: *mut [()] = core::ptr::slice_from_raw_parts_mut(ptr.cast(), nbits);
|
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | unsafe { &mut *(data as *mut Bitmap) }
|
| 73 | }
|
| 74 |
|
| 75 | |
| 76 | pub fn as_ptr(&self) -> *const usize {
|
| 77 | core::ptr::from_ref::<Bitmap>(self).cast::<usize>()
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | pub fn as_mut_ptr(&mut self) -> *mut usize {
|
| 82 | core::ptr::from_mut::<Bitmap>(self).cast::<usize>()
|
| 83 | }
|
| 84 |
|
| 85 | |
| 86 | #[expect(clippy::len_without_is_empty)]
|
| 87 | pub fn len(&self) -> usize {
|
| 88 | self.data.len()
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | |
| 93 | #[repr(C)]
|
| 94 | union BitmapRepr {
|
| 95 | bitmap: usize,
|
| 96 | ptr: NonNull<usize>,
|
| 97 | }
|
| 98 |
|
| 99 | macro_rules! bitmap_assert {
|
| 100 | ($cond:expr, $($arg:tt)+) => {
|
| 101 | #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
|
| 102 | assert!($cond, $($arg)*);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | macro_rules! bitmap_assert_return {
|
| 107 | ($cond:expr, $($arg:tt)+) => {
|
| 108 | #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
|
| 109 | assert!($cond, $($arg)*);
|
| 110 |
|
| 111 | #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
|
| 112 | if !($cond) {
|
| 113 | pr_err!($($arg)*);
|
| 114 | return
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | pub struct BitmapVec {
|
| 155 | |
| 156 | repr: BitmapRepr,
|
| 157 | |
| 158 | nbits: usize,
|
| 159 | }
|
| 160 |
|
| 161 | impl core::ops::Deref for BitmapVec {
|
| 162 | type Target = Bitmap;
|
| 163 |
|
| 164 | fn deref(&self) -> &Bitmap {
|
| 165 | let ptr = if self.nbits <= BitmapVec::MAX_INLINE_LEN {
|
| 166 | |
| 167 | #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
|
| 168 | unsafe {
|
| 169 | core::ptr::addr_of!(self.repr.bitmap)
|
| 170 | }
|
| 171 | } else {
|
| 172 | |
| 173 | unsafe { self.repr.ptr.as_ptr() }
|
| 174 | };
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | unsafe { Bitmap::from_raw(ptr, self.nbits) }
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | impl core::ops::DerefMut for BitmapVec {
|
| 183 | fn deref_mut(&mut self) -> &mut Bitmap {
|
| 184 | let ptr = if self.nbits <= BitmapVec::MAX_INLINE_LEN {
|
| 185 | |
| 186 | #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
|
| 187 | unsafe {
|
| 188 | core::ptr::addr_of_mut!(self.repr.bitmap)
|
| 189 | }
|
| 190 | } else {
|
| 191 | |
| 192 | unsafe { self.repr.ptr.as_ptr() }
|
| 193 | };
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | unsafe { Bitmap::from_raw_mut(ptr, self.nbits) }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | |
| 202 | |
| 203 | |
| 204 | unsafe impl Send for BitmapVec {}
|
| 205 |
|
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | unsafe impl Sync for BitmapVec {}
|
| 211 |
|
| 212 | impl Drop for BitmapVec {
|
| 213 | fn drop(&mut self) {
|
| 214 | if self.nbits <= BitmapVec::MAX_INLINE_LEN {
|
| 215 | return;
|
| 216 | }
|
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | unsafe { bindings::bitmap_free(self.repr.ptr.as_ptr()) };
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | impl BitmapVec {
|
| 227 | |
| 228 | pub const MAX_LEN: usize = i32::MAX as usize;
|
| 229 |
|
| 230 | |
| 231 | pub const MAX_INLINE_LEN: usize = usize::BITS as usize;
|
| 232 |
|
| 233 | |
| 234 | #[inline]
|
| 235 | pub fn new_inline() -> Self {
|
| 236 | |
| 237 | BitmapVec {
|
| 238 | repr: BitmapRepr { bitmap: 0 },
|
| 239 | nbits: BitmapVec::MAX_INLINE_LEN,
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | #[inline]
|
| 248 | pub fn new(nbits: usize, flags: Flags) -> Result<Self, AllocError> {
|
| 249 | if nbits <= BitmapVec::MAX_INLINE_LEN {
|
| 250 | return Ok(BitmapVec {
|
| 251 | repr: BitmapRepr { bitmap: 0 },
|
| 252 | nbits,
|
| 253 | });
|
| 254 | }
|
| 255 | if nbits > Self::MAX_LEN {
|
| 256 | return Err(AllocError);
|
| 257 | }
|
| 258 | let nbits_u32 = u32::try_from(nbits).unwrap();
|
| 259 | |
| 260 | let ptr = unsafe { bindings::bitmap_zalloc(nbits_u32, flags.as_raw()) };
|
| 261 | let ptr = NonNull::new(ptr).ok_or(AllocError)?;
|
| 262 | |
| 263 | Ok(BitmapVec {
|
| 264 | repr: BitmapRepr { ptr },
|
| 265 | nbits,
|
| 266 | })
|
| 267 | }
|
| 268 |
|
| 269 | |
| 270 | #[allow(clippy::len_without_is_empty)]
|
| 271 | #[inline]
|
| 272 | pub fn len(&self) -> usize {
|
| 273 | self.nbits
|
| 274 | }
|
| 275 |
|
| 276 | |
| 277 | #[cfg(CONFIG_FIND_BIT_BENCHMARK_RUST)]
|
| 278 | pub fn fill_random(&mut self) {
|
| 279 | |
| 280 | |
| 281 | unsafe {
|
| 282 | bindings::get_random_bytes(
|
| 283 | self.as_mut_ptr().cast::<ffi::c_void>(),
|
| 284 | usize::div_ceil(self.nbits, bindings::BITS_PER_LONG as usize)
|
| 285 | * bindings::BITS_PER_LONG as usize
|
| 286 | / 8,
|
| 287 | );
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | impl Bitmap {
|
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | #[inline]
|
| 306 | pub fn set_bit(&mut self, index: usize) {
|
| 307 | bitmap_assert_return!(
|
| 308 | index < self.len(),
|
| 309 | "Bit `index` must be < {}, was {}",
|
| 310 | self.len(),
|
| 311 | index
|
| 312 | );
|
| 313 | |
| 314 | unsafe { bindings::__set_bit(index, self.as_mut_ptr()) };
|
| 315 | }
|
| 316 |
|
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | #[inline]
|
| 332 | pub fn set_bit_atomic(&self, index: usize) {
|
| 333 | bitmap_assert_return!(
|
| 334 | index < self.len(),
|
| 335 | "Bit `index` must be < {}, was {}",
|
| 336 | self.len(),
|
| 337 | index
|
| 338 | );
|
| 339 | |
| 340 | |
| 341 | unsafe { bindings::set_bit(index, self.as_ptr().cast_mut()) };
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | #[inline]
|
| 357 | pub fn clear_bit(&mut self, index: usize) {
|
| 358 | bitmap_assert_return!(
|
| 359 | index < self.len(),
|
| 360 | "Bit `index` must be < {}, was {}",
|
| 361 | self.len(),
|
| 362 | index
|
| 363 | );
|
| 364 | |
| 365 | unsafe { bindings::__clear_bit(index, self.as_mut_ptr()) };
|
| 366 | }
|
| 367 |
|
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | #[inline]
|
| 383 | pub fn clear_bit_atomic(&self, index: usize) {
|
| 384 | bitmap_assert_return!(
|
| 385 | index < self.len(),
|
| 386 | "Bit `index` must be < {}, was {}",
|
| 387 | self.len(),
|
| 388 | index
|
| 389 | );
|
| 390 | |
| 391 | |
| 392 | unsafe { bindings::clear_bit(index, self.as_ptr().cast_mut()) };
|
| 393 | }
|
| 394 |
|
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | #[inline]
|
| 416 | pub fn copy_and_extend(&mut self, src: &Bitmap) {
|
| 417 | let len = core::cmp::min(src.len(), self.len());
|
| 418 | |
| 419 | unsafe {
|
| 420 | bindings::bitmap_copy_and_extend(
|
| 421 | self.as_mut_ptr(),
|
| 422 | src.as_ptr(),
|
| 423 | len as u32,
|
| 424 | self.len() as u32,
|
| 425 | )
|
| 426 | };
|
| 427 | }
|
| 428 |
|
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | #[inline]
|
| 450 | pub fn last_bit(&self) -> Option<usize> {
|
| 451 | |
| 452 | let index = unsafe { bindings::_find_last_bit(self.as_ptr(), self.len()) };
|
| 453 | if index >= self.len() {
|
| 454 | None
|
| 455 | } else {
|
| 456 | Some(index)
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | |
| 462 | |
| 463 | #[inline]
|
| 464 | pub fn next_bit(&self, start: usize) -> Option<usize> {
|
| 465 | bitmap_assert!(
|
| 466 | start < self.len(),
|
| 467 | "`start` must be < {} was {}",
|
| 468 | self.len(),
|
| 469 | start
|
| 470 | );
|
| 471 | |
| 472 | |
| 473 | let index = unsafe { bindings::_find_next_bit(self.as_ptr(), self.len(), start) };
|
| 474 | if index >= self.len() {
|
| 475 | None
|
| 476 | } else {
|
| 477 | Some(index)
|
| 478 | }
|
| 479 | }
|
| 480 |
|
| 481 | |
| 482 | |
| 483 | #[inline]
|
| 484 | pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
|
| 485 | bitmap_assert!(
|
| 486 | start < self.len(),
|
| 487 | "`start` must be < {} was {}",
|
| 488 | self.len(),
|
| 489 | start
|
| 490 | );
|
| 491 | |
| 492 | |
| 493 | let index = unsafe { bindings::_find_next_zero_bit(self.as_ptr(), self.len(), start) };
|
| 494 | if index >= self.len() {
|
| 495 | None
|
| 496 | } else {
|
| 497 | Some(index)
|
| 498 | }
|
| 499 | }
|
| 500 | }
|
| 501 |
|
| 502 | use macros::kunit_tests;
|
| 503 |
|
| 504 | #[kunit_tests(rust_kernel_bitmap)]
|
| 505 | mod tests {
|
| 506 | use super::*;
|
| 507 | use kernel::alloc::flags::GFP_KERNEL;
|
| 508 |
|
| 509 | #[test]
|
| 510 | fn bitmap_borrow() {
|
| 511 | let fake_bitmap: [usize; 2] = [0, 0];
|
| 512 | let fake_bitmap_len = 2 * usize::BITS as usize;
|
| 513 | |
| 514 | let b = unsafe { Bitmap::from_raw(fake_bitmap.as_ptr(), fake_bitmap_len) };
|
| 515 | assert_eq!(fake_bitmap_len, b.len());
|
| 516 | assert_eq!(None, b.next_bit(0));
|
| 517 | }
|
| 518 |
|
| 519 | #[test]
|
| 520 | fn bitmap_copy() {
|
| 521 | let fake_bitmap: usize = 0xFF;
|
| 522 | |
| 523 | let b = unsafe { Bitmap::from_raw(core::ptr::addr_of!(fake_bitmap), 8) };
|
| 524 | assert_eq!(8, b.len());
|
| 525 | assert_eq!(None, b.next_zero_bit(0));
|
| 526 | }
|
| 527 |
|
| 528 | #[test]
|
| 529 | fn bitmap_vec_new() -> Result<(), AllocError> {
|
| 530 | let b = BitmapVec::new(0, GFP_KERNEL)?;
|
| 531 | assert_eq!(0, b.len());
|
| 532 |
|
| 533 | let b = BitmapVec::new(3, GFP_KERNEL)?;
|
| 534 | assert_eq!(3, b.len());
|
| 535 |
|
| 536 | let b = BitmapVec::new(1024, GFP_KERNEL)?;
|
| 537 | assert_eq!(1024, b.len());
|
| 538 |
|
| 539 | |
| 540 | let res = BitmapVec::new(1 << 31, GFP_KERNEL);
|
| 541 | assert!(res.is_err());
|
| 542 | Ok(())
|
| 543 | }
|
| 544 |
|
| 545 | #[test]
|
| 546 | fn bitmap_set_clear_find() -> Result<(), AllocError> {
|
| 547 | let mut b = BitmapVec::new(128, GFP_KERNEL)?;
|
| 548 |
|
| 549 | |
| 550 | assert_eq!(None, b.next_bit(0));
|
| 551 | assert_eq!(Some(0), b.next_zero_bit(0));
|
| 552 | assert_eq!(None, b.last_bit());
|
| 553 |
|
| 554 | b.set_bit(17);
|
| 555 |
|
| 556 | assert_eq!(Some(17), b.next_bit(0));
|
| 557 | assert_eq!(Some(17), b.next_bit(17));
|
| 558 | assert_eq!(None, b.next_bit(18));
|
| 559 | assert_eq!(Some(17), b.last_bit());
|
| 560 |
|
| 561 | b.set_bit(107);
|
| 562 |
|
| 563 | assert_eq!(Some(17), b.next_bit(0));
|
| 564 | assert_eq!(Some(17), b.next_bit(17));
|
| 565 | assert_eq!(Some(107), b.next_bit(18));
|
| 566 | assert_eq!(Some(107), b.last_bit());
|
| 567 |
|
| 568 | b.clear_bit(17);
|
| 569 |
|
| 570 | assert_eq!(Some(107), b.next_bit(0));
|
| 571 | assert_eq!(Some(107), b.last_bit());
|
| 572 | Ok(())
|
| 573 | }
|
| 574 |
|
| 575 | #[test]
|
| 576 | fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
|
| 577 | |
| 578 | |
| 579 | #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
|
| 580 | {
|
| 581 | let mut b = BitmapVec::new(128, GFP_KERNEL)?;
|
| 582 | b.set_bit(2048);
|
| 583 | b.set_bit_atomic(2048);
|
| 584 | b.clear_bit(2048);
|
| 585 | b.clear_bit_atomic(2048);
|
| 586 | assert_eq!(None, b.next_bit(2048));
|
| 587 | assert_eq!(None, b.next_zero_bit(2048));
|
| 588 | assert_eq!(None, b.last_bit());
|
| 589 | }
|
| 590 | Ok(())
|
| 591 | }
|
| 592 |
|
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 |
|
| 603 | #[test]
|
| 604 | fn bitmap_copy_and_extend() -> Result<(), AllocError> {
|
| 605 | let mut long_bitmap = BitmapVec::new(256, GFP_KERNEL)?;
|
| 606 |
|
| 607 | long_bitmap.set_bit(3);
|
| 608 | long_bitmap.set_bit(200);
|
| 609 |
|
| 610 | let mut short_bitmap = BitmapVec::new(32, GFP_KERNEL)?;
|
| 611 |
|
| 612 | short_bitmap.set_bit(17);
|
| 613 |
|
| 614 | long_bitmap.copy_and_extend(&short_bitmap);
|
| 615 |
|
| 616 | |
| 617 | assert_eq!(Some(17), long_bitmap.next_bit(0));
|
| 618 | assert_eq!(Some(17), long_bitmap.last_bit());
|
| 619 | Ok(())
|
| 620 | }
|
| 621 | }
|