From fa31e2235c20731f7ea547521689915baa070129 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 27 Sep 2021 13:39:10 +0800 Subject: [PATCH] kernel: allow freeing unbacked virtual memory Memory is on-demand allocated, meaning it's entirely possible to allocate and free memory without ever touching physical pages. Previously, when we attempted to free memory, it would skip over pages that were never backed by real memory. With this patch, we continue to free the virtual page even if there is no associated physical page. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 3ed511015..6cca3a19f 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -530,8 +530,14 @@ impl MemoryManager { /// * MemoryInUse - The specified page is already mapped pub fn unmap_page(&mut self, virt: *mut usize) -> Result { let pid = crate::arch::process::current_pid(); - let phys = crate::arch::mem::virt_to_phys(virt as usize)?; - self.release_page(phys as *mut usize, pid)?; + + // If the virtual address has an assigned physical address, release that + // address from this process. + if let Ok(phys) = crate::arch::mem::virt_to_phys(virt as usize) { + self.release_page(phys as *mut usize, pid)?; + }; + + // Free the virtual address. crate::arch::mem::unmap_page_inner(self, virt as usize) }