Fix drop_table not dropping L1 tables and make it drop allocated memory

This commit is contained in:
pjht 2024-08-17 09:35:43 -05:00
parent 4d908f1335
commit f258f4ed71
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 61 additions and 39 deletions

View File

@ -287,15 +287,9 @@ impl Tasking {
if let Some(current_pid) = *self.current_pid.read() {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let current_process = &processes[current_pid];
let bytes_used = current_process.address_space.as_ref().map_or_else(
|| ACTIVE_SPACE.lock().get_bytes_allocated(),
|space| space.get_bytes_allocated(),
);
let buf_bytes_used = current_process.buf_allocated.load(Ordering::Relaxed);
println!(
"[TASKING] PID {current_pid} exiting, used {} ({}) + {} ({}) of kernel buffers, this is being leaked.",
SizeFormatter::new(bytes_used, BINARY),
bytes_used / 4096,
"[TASKING] PID {current_pid} exiting, used {} ({}) of kernel buffers, this is being leaked.",
SizeFormatter::new(buf_bytes_used, BINARY),
buf_bytes_used / 4096,
);
@ -432,8 +426,7 @@ impl Tasking {
}
pub fn print_stats(&self) {
let mut total =
KERNEL_SPACE.lock().get_bytes_allocated();
let mut total = KERNEL_SPACE.lock().get_bytes_allocated();
println!(
"[TASKING] Kernel used {}",
SizeFormatter::new(KERNEL_SPACE.lock().get_bytes_allocated(), BINARY)
@ -446,7 +439,12 @@ impl Tasking {
let buf_bytes_used = process.buf_allocated.load(Ordering::Relaxed);
total += bytes_used;
total += buf_bytes_used;
println!("[TASKING] PID {} used {} + {} of kernel buffers", i, SizeFormatter::new(bytes_used, BINARY), SizeFormatter::new(buf_bytes_used, BINARY));
println!(
"[TASKING] PID {} used {} + {} of kernel buffers",
i,
SizeFormatter::new(bytes_used, BINARY),
SizeFormatter::new(buf_bytes_used, BINARY)
);
}
println!("[TASKING] Total used {} ({})", SizeFormatter::new(total, BINARY), total / 4096);
}

View File

@ -480,7 +480,9 @@ impl AddressSpace {
PageTableFlags::USER_ACCESSIBLE
} else {
PageTableFlags::empty()
},
}
| PageTableFlags::BIT_9, // Bit 9 is used to indicate the underlying
// frame should be freed on process exit
PageTableFlags::PRESENT
| PageTableFlags::WRITABLE
| PageTableFlags::USER_ACCESSIBLE,
@ -623,7 +625,11 @@ impl AddressSpace {
.map_to_with_table_flags(
page,
frame,
flags | PageTableFlags::PRESENT | PageTableFlags::WRITABLE,
flags
| PageTableFlags::PRESENT
| PageTableFlags::WRITABLE
| PageTableFlags::BIT_9, // Bit 9 is used to indicate the underlying
// frame should be freed on process exit
PageTableFlags::PRESENT
| PageTableFlags::WRITABLE
| PageTableFlags::USER_ACCESSIBLE,
@ -703,7 +709,8 @@ impl Drop for AddressSpace {
}
fn drop_table(table: &PageTable, level: u8) {
if level > 2 {
match level.cmp(&1) {
core::cmp::Ordering::Greater => {
for (i, entry) in table.iter().enumerate() {
if level == 4 && i >= 256 {
continue;
@ -724,7 +731,7 @@ fn drop_table(table: &PageTable, level: u8) {
)]
#[expect(
clippy::arithmetic_side_effects,
reason = "level is at minimum 3 here, thus this can't underflow"
reason = "level is at minimum 2 here, thus this can't underflow"
)]
drop_table(
unsafe { PhysFrame::from_start_address(entry.addr()).unwrap().as_virt_ref() },
@ -732,6 +739,23 @@ fn drop_table(table: &PageTable, level: u8) {
);
}
}
},
core::cmp::Ordering::Equal => {
for entry in table.iter() {
if entry.flags().contains(PageTableFlags::PRESENT)
&& entry.flags().contains(PageTableFlags::BIT_9)
{
//#[expect(
// clippy::unwrap_used,
// reason = "PhysFrame requires its argument to be 4k aligned, which is guaranteed since page tables must be 4k aligned"
//)]
unsafe {
PHYSICAL_MEMORY.lock().deallocate_frame(entry.frame().unwrap());
};
}
}
},
core::cmp::Ordering::Less => unreachable!(),
}
#[expect(
clippy::unwrap_used,