From 622a4fb3cd84377b5b40cebd9ddf3975ece31324 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Fri, 19 Jun 2026 17:33:46 +0700 Subject: [PATCH] Solve context leak when switching --- src/context/switch.rs | 3 +++ src/syscall/process.rs | 9 +-------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/context/switch.rs b/src/context/switch.rs index 61d860fac1..b94a02b713 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -351,6 +351,9 @@ pub fn switch(token: &mut CleanLockToken) -> SwitchResult { .being_sigkilled .set(next_context.being_sigkilled); + // Anything implement Drop must be manually dropped now + drop(prev_context_lock); + unsafe { percpu.new_addrsp_guard.set(addr_space_guard); arch::switch_to(prev_context, next_context); diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 8f9a737604..123fe81767 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -45,8 +45,6 @@ pub fn exit_this_context(excp: Option, token: &mut CleanLock // Files must be closed while context is valid so that messages can be passed close_files.force_close_all(token); if let Some(addrspace) = addrspace_opt { - // TODO: addrspace utable should be dropped immediately but it's not the case. - // the utable leaves us with 8 memory pages (32K) leak per context if let Ok(addrspace) = Arc::try_unwrap(addrspace) { addrspace.into_drop(token); } @@ -55,12 +53,6 @@ pub fn exit_this_context(excp: Option, token: &mut CleanLock let owner = { let mut guard = context_lock.write(token.token()); guard.status = context::Status::Dead { excp }; - // TODO: context should be dropped immediately but it's not the case. - // we drop kstack to prevent 32 memory pages (128K) leaking - // TODO: can't drop the kstack here immediately, as this very function runs on that stack. - // until the Arc leaks can be found, consider using a global "garbage collection - // queue" - // drop(guard.kstack.take()); guard.owner_proc_id }; @@ -80,6 +72,7 @@ pub fn exit_this_context(excp: Option, token: &mut CleanLock } } } + drop(close_files); context::switch(token); unreachable!(); }