Skip to content

Commit

Permalink
mm/validate: use usize instead of isize to index into the bitmap
Browse files Browse the repository at this point in the history
The offsets are always positive from the start of the bitmap, so use
an unsigned type.

Signed-off-by: Carlos López <carlos.lopez@suse.com>
  • Loading branch information
00xc committed Oct 17, 2023
1 parent 765b758 commit 94567fa
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/mm/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,19 @@ impl ValidBitmap {
}

#[inline(always)]
fn index(&self, paddr: PhysAddr) -> (isize, usize) {
fn index(&self, paddr: PhysAddr) -> (usize, usize) {
let page_offset = (paddr - self.pbase) / PAGE_SIZE;
let index: isize = (page_offset / 64).try_into().unwrap();
let bit: usize = page_offset % 64;
let index = page_offset / 64;
let bit = page_offset % 64;

(index, bit)
}

fn clear_all(&mut self) {
let (mut i, bit) = self.index(self.pend);
let (mut index, bit) = self.index(self.pend);
if bit != 0 {
i += 1;
index += 1;
}
let index: usize = i.try_into().unwrap();

unsafe {
ptr::write_bytes(self.bitmap, 0, index);
Expand All @@ -159,7 +158,7 @@ impl ValidBitmap {
}

unsafe {
ptr::copy_nonoverlapping(self.bitmap, new_bitmap, count as usize);
ptr::copy_nonoverlapping(self.bitmap, new_bitmap, count);
}
self.bitmap = new_bitmap;
}
Expand All @@ -179,9 +178,9 @@ impl ValidBitmap {
assert!(self.check_addr(paddr));

unsafe {
let mut val: u64 = ptr::read(self.bitmap.offset(index));
let mut val: u64 = ptr::read(self.bitmap.add(index));
val |= 1u64 << bit;
ptr::write(self.bitmap.offset(index), val);
ptr::write(self.bitmap.add(index), val);
}
}

Expand All @@ -196,9 +195,9 @@ impl ValidBitmap {
assert!(self.check_addr(paddr));

unsafe {
let mut val: u64 = ptr::read(self.bitmap.offset(index));
let mut val: u64 = ptr::read(self.bitmap.add(index));
val &= !(1u64 << bit);
ptr::write(self.bitmap.offset(index), val);
ptr::write(self.bitmap.add(index), val);
}
}

Expand All @@ -207,15 +206,15 @@ impl ValidBitmap {
return;
}

const NR_INDEX: isize = (PAGE_SIZE_2M / (PAGE_SIZE * 64)) as isize;
const NR_INDEX: usize = PAGE_SIZE_2M / (PAGE_SIZE * 64);
let (index, _) = self.index(paddr);

assert!(paddr.is_aligned(PAGE_SIZE_2M));
assert!(self.check_addr(paddr));

for i in 0..NR_INDEX {
unsafe {
ptr::write(self.bitmap.offset(index + i), val);
ptr::write(self.bitmap.add(index + i), val);
}
}
}
Expand All @@ -228,10 +227,10 @@ impl ValidBitmap {
self.set_2m(paddr, 0u64);
}

fn modify_bitmap_word(&mut self, index: isize, mask: u64, new_val: u64) {
let val = unsafe { ptr::read(self.bitmap.offset(index)) };
fn modify_bitmap_word(&mut self, index: usize, mask: u64, new_val: u64) {
let val = unsafe { ptr::read(self.bitmap.add(index)) };
let val = (val & !mask) | (new_val & mask);
unsafe { ptr::write(self.bitmap.offset(index), val) };
unsafe { ptr::write(self.bitmap.add(index), val) };
}

fn set_range(&mut self, paddr_begin: PhysAddr, paddr_end: PhysAddr, new_val: bool) {
Expand All @@ -251,7 +250,7 @@ impl ValidBitmap {
self.modify_bitmap_word(index_head, mask_head, new_val);

for index in (index_head + 1)..index_tail {
unsafe { ptr::write(self.bitmap.offset(index), new_val) };
unsafe { ptr::write(self.bitmap.add(index), new_val) };
}

if bit_tail_end != 0 {
Expand Down Expand Up @@ -284,7 +283,7 @@ impl ValidBitmap {

unsafe {
let mask: u64 = 1u64 << bit;
let val: u64 = ptr::read(self.bitmap.offset(index));
let val: u64 = ptr::read(self.bitmap.add(index));

(val & mask) == mask
}
Expand Down

0 comments on commit 94567fa

Please sign in to comment.