From 66abf389ec70c2ea3a42bd270342041dd8a75d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Tue, 17 Oct 2023 13:36:00 +0200 Subject: [PATCH 1/3] mm/validate: remove pub from methods on private struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove useless pub identifiers on methods on the private ValidBitmap struct. Signed-off-by: Carlos López --- src/mm/validate.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/mm/validate.rs b/src/mm/validate.rs index cdb8d5746..b24a04886 100644 --- a/src/mm/validate.rs +++ b/src/mm/validate.rs @@ -101,26 +101,26 @@ struct ValidBitmap { } impl ValidBitmap { - pub const fn new() -> Self { + const fn new() -> Self { ValidBitmap { region: MemoryRegion::from_addresses(PhysAddr::null(), PhysAddr::null()), bitmap: ptr::null_mut(), } } - pub fn set_region(&mut self, region: MemoryRegion) { + fn set_region(&mut self, region: MemoryRegion) { self.region = region; } - pub fn set_bitmap(&mut self, bitmap: *mut u64) { + fn set_bitmap(&mut self, bitmap: *mut u64) { self.bitmap = bitmap; } - pub fn check_addr(&self, paddr: PhysAddr) -> bool { + fn check_addr(&self, paddr: PhysAddr) -> bool { self.region.contains(paddr) } - pub fn bitmap_addr(&self) -> PhysAddr { + fn bitmap_addr(&self) -> PhysAddr { assert!(!self.bitmap.is_null()); virt_to_phys(VirtAddr::from(self.bitmap)) } @@ -134,7 +134,7 @@ impl ValidBitmap { (index, bit) } - pub fn clear_all(&mut self) { + fn clear_all(&mut self) { let (mut i, bit) = self.index(self.region.end()); if bit != 0 { i += 1; @@ -146,11 +146,11 @@ impl ValidBitmap { } } - pub fn alloc_order(&self) -> usize { + fn alloc_order(&self) -> usize { bitmap_alloc_order(self.region) } - pub fn migrate(&mut self, new_bitmap: *mut u64) { + fn migrate(&mut self, new_bitmap: *mut u64) { let (count, _) = self.index(self.region.end()); unsafe { @@ -163,7 +163,7 @@ impl ValidBitmap { !self.bitmap.is_null() } - pub fn set_valid_4k(&mut self, paddr: PhysAddr) { + fn set_valid_4k(&mut self, paddr: PhysAddr) { if !self.initialized() { return; } @@ -180,7 +180,7 @@ impl ValidBitmap { } } - pub fn clear_valid_4k(&mut self, paddr: PhysAddr) { + fn clear_valid_4k(&mut self, paddr: PhysAddr) { if !self.initialized() { return; } @@ -215,11 +215,11 @@ impl ValidBitmap { } } - pub fn set_valid_2m(&mut self, paddr: PhysAddr) { + fn set_valid_2m(&mut self, paddr: PhysAddr) { self.set_2m(paddr, !0u64); } - pub fn clear_valid_2m(&mut self, paddr: PhysAddr) { + fn clear_valid_2m(&mut self, paddr: PhysAddr) { self.set_2m(paddr, 0u64); } @@ -260,15 +260,15 @@ impl ValidBitmap { } } - pub fn set_valid_range(&mut self, paddr_begin: PhysAddr, paddr_end: PhysAddr) { + fn set_valid_range(&mut self, paddr_begin: PhysAddr, paddr_end: PhysAddr) { self.set_range(paddr_begin, paddr_end, true); } - pub fn clear_valid_range(&mut self, paddr_begin: PhysAddr, paddr_end: PhysAddr) { + fn clear_valid_range(&mut self, paddr_begin: PhysAddr, paddr_end: PhysAddr) { self.set_range(paddr_begin, paddr_end, false); } - pub fn is_valid_4k(&self, paddr: PhysAddr) -> bool { + fn is_valid_4k(&self, paddr: PhysAddr) -> bool { if !self.initialized() { return false; } From 2b0162d6aa16f3a6d13544e34e3b8a26767b4deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Tue, 17 Oct 2023 14:27:44 +0200 Subject: [PATCH 2/3] mm/validate: migrate all pages in bitmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the number of pages in the bitmap was not a multiple of 64, the remainder would not be copied over to the new bitmap during migration, which could lead to tracking validated pages as invalid. Signed-off-by: Carlos López --- src/mm/validate.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mm/validate.rs b/src/mm/validate.rs index b24a04886..979b6592b 100644 --- a/src/mm/validate.rs +++ b/src/mm/validate.rs @@ -135,14 +135,9 @@ impl ValidBitmap { } fn clear_all(&mut self) { - let (mut i, bit) = self.index(self.region.end()); - if bit != 0 { - i += 1; - } - let index: usize = i.try_into().unwrap(); - + let len = self.bitmap_len(); unsafe { - ptr::write_bytes(self.bitmap, 0, index); + ptr::write_bytes(self.bitmap, 0, len); } } @@ -150,11 +145,16 @@ impl ValidBitmap { bitmap_alloc_order(self.region) } - fn migrate(&mut self, new_bitmap: *mut u64) { - let (count, _) = self.index(self.region.end()); + /// The number of u64's in the bitmap + fn bitmap_len(&self) -> usize { + let num_pages = self.region.len() / PAGE_SIZE; + num_pages.div_ceil(u64::BITS as usize) + } + fn migrate(&mut self, new_bitmap: *mut u64) { + let count = self.bitmap_len(); unsafe { - ptr::copy_nonoverlapping(self.bitmap, new_bitmap, count as usize); + ptr::copy_nonoverlapping(self.bitmap, new_bitmap, count); } self.bitmap = new_bitmap; } From 6ba7274f6b6301cddd40f982870cf7eb8edd455c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Tue, 17 Oct 2023 14:35:44 +0200 Subject: [PATCH 3/3] mm/validate: use usize instead of isize to index into the bitmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The offsets are always positive from the start of the bitmap, so use an unsigned type. Signed-off-by: Carlos López --- src/mm/validate.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/mm/validate.rs b/src/mm/validate.rs index 979b6592b..8644c1d03 100644 --- a/src/mm/validate.rs +++ b/src/mm/validate.rs @@ -126,10 +126,10 @@ impl ValidBitmap { } #[inline(always)] - fn index(&self, paddr: PhysAddr) -> (isize, usize) { + fn index(&self, paddr: PhysAddr) -> (usize, usize) { let page_offset = (paddr - self.region.start()) / 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) } @@ -174,9 +174,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); } } @@ -191,9 +191,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); } } @@ -202,7 +202,7 @@ 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)); @@ -210,7 +210,7 @@ impl ValidBitmap { for i in 0..NR_INDEX { unsafe { - ptr::write(self.bitmap.offset(index + i), val); + ptr::write(self.bitmap.add(index + i), val); } } } @@ -223,10 +223,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) { @@ -246,7 +246,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 { @@ -279,7 +279,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 }