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