Skip to content

Commit

Permalink
AdressSpace: Fix physical memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
fruhland committed Feb 22, 2024
1 parent 48db0f0 commit 10993f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions os/kernel/src/memory/physical.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use alloc::format;
use alloc::string::String;
use core::cell::{Cell};
use core::cell::Cell;
use core::fmt::{Debug, Formatter};
use core::ptr;
use spin::{Mutex};
use spin::Mutex;
use spin::once::Once;
use x86_64::PhysAddr;
use x86_64::structures::paging::frame::PhysFrameRange;
Expand Down
25 changes: 14 additions & 11 deletions os/kernel/src/memory/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct VirtualMemoryArea {
typ: VmaType
}

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum VmaType {
Code, Heap, Stack
}
Expand All @@ -32,9 +32,9 @@ unsafe impl Send for AddressSpace {}
unsafe impl Sync for AddressSpace {}

pub fn create_address_space() -> Arc<AddressSpace> {
debug!("Page frame allocator before address space creation:\n{}", physical::dump());
match kernel_process() {
Some(kernel_process) => { // Create user address space
debug!("Page frame allocator before address space creation:\n{}", physical::dump());
let kernel_space = AddressSpace::from_other(&kernel_process.address_space());
Arc::new(kernel_space)
}
Expand Down Expand Up @@ -171,12 +171,12 @@ impl AddressSpace {
AddressSpace::translate_in_table(root_table, addr, depth)
}

pub fn unmap(&self, pages: PageRange) {
pub fn unmap(&self, pages: PageRange, free_physical: bool) {
let depth = self.depth;
let root_table_guard = self.root_table.read();
let root_table = unsafe { root_table_guard.as_mut().unwrap() };

AddressSpace::unmap_in_table(root_table, pages, depth);
AddressSpace::unmap_in_table(root_table, pages, depth, free_physical);
}

fn copy_table(source: &PageTable, target: &mut PageTable, level: usize) {
Expand Down Expand Up @@ -248,7 +248,7 @@ impl AddressSpace {
return total_allocated_pages;
}

fn unmap_in_table(table: &mut PageTable, mut pages: PageRange, level: usize) -> usize {
fn unmap_in_table(table: &mut PageTable, mut pages: PageRange, level: usize, free_physical: bool) -> usize {
let mut total_freed_pages: usize = 0;
let start_index = usize::from(page_table_index(pages.start.start_address(), level));

Expand All @@ -259,7 +259,7 @@ impl AddressSpace {
}

let next_level_table = unsafe { (entry.addr().as_u64() as *mut PageTable).as_mut().unwrap() };
let freed_pages = AddressSpace::unmap_in_table(next_level_table, pages, level - 1);
let freed_pages = AddressSpace::unmap_in_table(next_level_table, pages, level - 1, free_physical);
pages = PageRange { start: pages.start + freed_pages as u64, end: pages.end };
total_freed_pages += freed_pages;

Expand All @@ -283,8 +283,11 @@ impl AddressSpace {
}

if !entry.is_unused() {
let frame = PhysFrame::from_start_address(entry.addr()).unwrap();
unsafe { physical::free(PhysFrameRange { start: frame, end: frame + 1 }); }
if free_physical {
let frame = PhysFrame::from_start_address(entry.addr()).unwrap();
unsafe { physical::free(PhysFrameRange { start: frame, end: frame + 1 }); }
}

entry.set_unused();
}
}
Expand All @@ -305,10 +308,10 @@ impl AddressSpace {
let next_level_table = unsafe { (entry.addr().as_u64() as *mut PageTable).as_mut().unwrap() };
AddressSpace::drop_table(next_level_table, level - 1);
}

let table_frame = PhysFrame::from_start_address(PhysAddr::new(ptr::from_ref(table) as u64)).unwrap();
unsafe { physical::free(PhysFrameRange { start: table_frame, end: table_frame + 1 }); }
}

let table_frame = PhysFrame::from_start_address(PhysAddr::new(ptr::from_ref(table) as u64)).unwrap();
unsafe { physical::free(PhysFrameRange { start: table_frame, end: table_frame + 1 }); }
}

fn translate_in_table(table: &mut PageTable, addr: VirtAddr, level: usize) -> Option<PhysAddr> {
Expand Down
2 changes: 1 addition & 1 deletion os/kernel/src/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Process {
impl Drop for Process {
fn drop(&mut self) {
for vma in self.memory_areas.read().iter() {
self.address_space.unmap(vma.range());
self.address_space.unmap(vma.range(), true);
}
}
}
Expand Down

0 comments on commit 10993f9

Please sign in to comment.