From 800fcbe3bfe7f3b8210a4b6f783a32cf070536bc Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 24 Nov 2021 01:50:15 +0800 Subject: [PATCH] fix page table consistency issue -- needs more testing to see if we licked it --- services/pddb/src/backend/basis.rs | 18 +++++++++++++++--- services/pddb/src/backend/dictionary.rs | 4 ++-- services/pddb/src/backend/hw.rs | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index a87546a4f..e4be4e2d3 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -808,7 +808,7 @@ impl BasisCacheEntry { // wipe & de-allocate any small pages for index in 0..dcache.small_pool.len() { let pool_vaddr = VirtAddr::new(dcache.index as u64 * SMALL_POOL_STRIDE + SMALL_POOL_START + index as u64 * SMALL_CAPACITY as u64).unwrap(); - if let Some(pp) = self.v2p_map.remove(&pool_vaddr) { + if let Some(pp) = self.v2p_map.get_mut(&pool_vaddr) { if paranoid { let mut random = [0u8; PAGE_SIZE]; hw.trng_slice(&mut random); @@ -823,7 +823,7 @@ impl BasisCacheEntry { let key_pages = 1 + (dcache.last_disk_key_index + 1) as usize / DK_PER_VPAGE; for page in 0..key_pages { let dk_vaddr = VirtAddr::new(dcache.index as u64 * DICT_VSIZE as u64 + page as u64 * VPAGE_SIZE as u64).unwrap(); - if let Some(pp) = self.v2p_map.remove(&dk_vaddr) { + if let Some(pp) = self.v2p_map.get_mut(&dk_vaddr) { log::info!("erasing dk page 0x{:x}/0x{:x}", dk_vaddr, pp.page_number() as usize * PAGE_SIZE); let mut random = [0u8; PAGE_SIZE]; hw.trng_slice(&mut random); @@ -919,13 +919,25 @@ impl BasisCacheEntry { /// Looks for dirty entries in the page table, and flushes them to disk. pub(crate) fn pt_sync(&mut self, hw: &mut PddbOs) { self.last_sync = Some(hw.timestamp_now()); + let mut kill_list = Vec::::new(); for (&virt, phys) in self.v2p_map.iter_mut() { - if !phys.clean() { + if !phys.valid() { + // erase the entry + log::info!("deleting pte va: {:x?} pa: {:x?}", virt, phys); + kill_list.push(virt); + hw.pt_erase(phys.page_number()); + } else if !phys.clean() { log::info!("syncing dirty pte va: {:x?} pa: {:x?}", virt, phys); hw.pt_patch_mapping(virt, phys.page_number()); phys.set_clean(true); } } + // have to do this in a second phase due to interior mutability problems doing it inside the first iterator + for kill in kill_list { + if self.v2p_map.remove(&kill).is_none() { + log::warn!("went to remove PTE from v2p map but it wasn't there: {:x}", kill); + } + } } /// This will sync the named Dictionary header + dirty *key descriptors*. It does not diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 91423b590..32c56a932 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -404,7 +404,7 @@ impl DictCacheEntry { let vpage_end_offset = PageAlignedVa::from((written + offset) as u64); if (vpage_end_offset.as_u64() - kcache.start) > kcache.reserved { for vpage in (vpage_end_offset.as_u64()..kcache.start + kcache.reserved).step_by(VPAGE_SIZE) { - if let Some(pp) = v2p_map.remove(&VirtAddr::new(vpage).unwrap()) { + if let Some(pp) = v2p_map.get_mut(&VirtAddr::new(vpage).unwrap()) { hw.fast_space_free(pp); } } @@ -565,7 +565,7 @@ impl DictCacheEntry { kcache.flags.set_valid(false); // ...but we remove the virtual pages from the page pool, effectively reclaiming the physical space. for vpage in kcache.large_pool_vpages() { - if let Some(pp) = v2p_map.remove(&vpage) { + if let Some(pp) = v2p_map.get_mut(&vpage) { if paranoid { let mut noise = [0u8; PAGE_SIZE]; hw.trng_slice(&mut noise); diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index f758d24a9..95766e3cd 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -326,6 +326,12 @@ impl PddbOs { //log::info!("pte ct: {:x?}", block); self.patch_pagetable(&block, phys_page_num * aes::BLOCK_SIZE as u32); } + /// erases a page table entry by overwriting it with garbage + pub(crate) fn pt_erase(&mut self, phys_page_num: u32) { + let mut eraseblock = [0u8; aes::BLOCK_SIZE]; + self.trng_slice(&mut eraseblock); + self.patch_pagetable(&eraseblock, phys_page_num * aes::BLOCK_SIZE as u32); + } /// Searches the page table for an MBBB slot. This is currently an O(N) search but /// in practice for Precursor there are only 8 pages, so it's quite fast on average. /// This would want to be optimized or cached for a much larger filesystem. @@ -371,6 +377,7 @@ impl PddbOs { pp.set_page_number(((page_index * PAGE_SIZE / aes::BLOCK_SIZE) + index) as PhysAddr); // the state is clean because this entry is, by definition, synchronized with the disk pp.set_clean(true); + pp.set_valid(true); // handle conflicting journal versions here if let Some(prev_page) = map.get(&pte.vaddr()) { let cipher = Aes256GcmSiv::new(Key::from_slice(key)); @@ -853,6 +860,7 @@ impl PddbOs { // the log with 2x the number of operations to record MaybeUsed and then Used. ppc.set_space_state(SpaceState::Used); ppc.set_clean(false); // the allocated page is not clean, because it hasn't been written to disk + ppc.set_valid(true); // the allocated page is now valid, so it should be flushed to disk ppc.set_journal(pp.journal() + 1); // this is guaranteed not to overflow because of a check in the "if" clause above // commit the usage to the journal @@ -882,7 +890,7 @@ impl PddbOs { maybe_alloc } } - pub fn fast_space_free(&mut self, mut pp: PhysPage) { + pub fn fast_space_free(&mut self, pp: &mut PhysPage) { self.fast_space_ensure_next_log(); // update the fspace cache pp.set_space_state(SpaceState::Dirty); @@ -893,7 +901,7 @@ impl PddbOs { // commit the free'd block to the journal self.syskey_ensure(); let cipher = self.cipher_ecb.as_ref().expect("Inconsistent internal state - syskey_ensure() failed"); - let mut update = SpaceUpdate::new(self.entropy.borrow_mut().get_u64(), pp); + let mut update = SpaceUpdate::new(self.entropy.borrow_mut().get_u64(), pp.clone()); let mut block = Block::from_mut_slice(update.deref_mut()); log::trace!("block: {:x?}", block); cipher.encrypt_block(&mut block); @@ -907,6 +915,8 @@ impl PddbOs { // fspace_log_next_addr is already None because we used "take()". We'll find a free spot for the // next journal entry the next time around. } + // mark the page as invalid, so that it will be deleted on the next PT sync + pp.set_valid(false); } /// This is a "look before you leap" function that will potentially pause all system operations /// and do a deep scan for space if the required amount is not available.