Avoid flushing TLB one by one when the un-mapped range is large

This commit is contained in:
Zhang Junyang 2024-09-14 11:05:57 +08:00 committed by Tate, Hongliang Tian
parent cfd23336e5
commit 5df3c9b749
2 changed files with 11 additions and 1 deletions

2
osdk/Cargo.lock generated
View File

@ -146,7 +146,7 @@ dependencies = [
[[package]]
name = "cargo-osdk"
version = "0.8.1"
version = "0.8.2"
dependencies = [
"assert_cmd",
"clap",

View File

@ -372,12 +372,18 @@ impl CursorMut<'_, '_> {
pub fn unmap(&mut self, len: usize) {
assert!(len % super::PAGE_SIZE == 0);
let end_va = self.virt_addr() + len;
let tlb_prefer_flush_all = len > TLB_FLUSH_ALL_THRESHOLD * PAGE_SIZE;
loop {
// SAFETY: It is safe to un-map memory in the userspace.
let result = unsafe { self.pt_cursor.take_next(end_va - self.virt_addr()) };
match result {
PageTableItem::Mapped { va, page, .. } => {
if !self.need_remote_flush && tlb_prefer_flush_all {
// Only on single-CPU cases we can drop the page immediately before flushing.
drop(page);
continue;
}
self.issue_tlb_flush(TlbFlushOp::Address(va), Some(page));
}
PageTableItem::NotMapped { .. } => {
@ -389,6 +395,10 @@ impl CursorMut<'_, '_> {
}
}
if !self.need_remote_flush && tlb_prefer_flush_all {
self.issue_tlb_flush(TlbFlushOp::All, None);
}
self.dispatch_tlb_flush();
}