Skip to content

Commit

Permalink
修正进程退出时未分离共享内存的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed Dec 7, 2024
1 parent 57b9eb3 commit 14bf62b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
37 changes: 1 addition & 36 deletions kernel/src/ipc/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
libs::{align::page_align_up, spinlock::SpinLock},
mm::{
allocator::page_frame::{PageFrameCount, PhysPageFrame, VirtPageFrame},
page::{page_manager_lock_irqsave, EntryFlags, PageFlushAll, PageType},
page::{page_manager_lock_irqsave, EntryFlags, PageFlushAll},
syscall::ProtFlags,
ucontext::{AddressSpace, VMA},
VirtAddr, VmFlags,
Expand Down Expand Up @@ -434,41 +434,6 @@ impl Syscall {
return Err(SystemError::EINVAL);
}

// 获取映射的物理地址
let paddr = address_write_guard
.user_mapper
.utable
.translate(vaddr)
.ok_or(SystemError::EINVAL)?
.0;

// 如果物理页的shm_id为None,代表不是共享页
let mut page_manager_guard = page_manager_lock_irqsave();
let page = page_manager_guard.get(&paddr).ok_or(SystemError::EINVAL)?;
let page_guard = page.read_irqsave();
let shm_id = if let PageType::Shm(shm_id) = page_guard.page_type() {
shm_id
} else {
return Err(SystemError::EINVAL);
};
drop(page_manager_guard);

// 获取对应共享页管理信息
let mut shm_manager_guard = shm_manager_lock();
let kernel_shm = shm_manager_guard
.get_mut(shm_id)
.ok_or(SystemError::EINVAL)?;
// 更新最后一次断开连接时间
kernel_shm.update_dtim();

// 映射计数减少
kernel_shm.decrease_count();

// 释放shm_id
if kernel_shm.map_count() == 0 && kernel_shm.mode().contains(ShmFlags::SHM_DEST) {
shm_manager_guard.free_id(shm_id);
}

// 取消映射
let flusher: PageFlushAll<MMArch> = PageFlushAll::new();
vma.unmap(&mut address_write_guard.user_mapper.utable, flusher);
Expand Down
26 changes: 25 additions & 1 deletion kernel/src/mm/ucontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
arch::{mm::PageMapper, CurrentIrqArch, MMArch},
exception::InterruptArch,
filesystem::vfs::file::File,
ipc::shm::{shm_manager_lock, ShmFlags},
libs::{
align::page_align_up,
rwlock::RwLock,
Expand Down Expand Up @@ -1151,12 +1152,35 @@ impl LockedVMA {

pub fn unmap(&self, mapper: &mut PageMapper, mut flusher: impl Flusher<MMArch>) {
// todo: 如果当前vma与文件相关,完善文件相关的逻辑

let mut guard = self.lock_irqsave();

// 获取物理页的anon_vma的守卫
let mut page_manager_guard: SpinLockGuard<'_, crate::mm::page::PageManager> =
page_manager_lock_irqsave();

// 获取映射的物理地址
if let Some((paddr, _flags)) = mapper.translate(guard.region().start()) {
// 如果是共享页,执行释放操作
let page = page_manager_guard.get(&paddr).unwrap();
let page_guard = page.read_irqsave();
if let PageType::Shm(shm_id) = page_guard.page_type() {
let mut shm_manager_guard = shm_manager_lock();
if let Some(kernel_shm) = shm_manager_guard.get_mut(shm_id) {
// 更新最后一次断开连接时间
kernel_shm.update_dtim();

// 映射计数减少
kernel_shm.decrease_count();

// 释放shm_id
if kernel_shm.map_count() == 0 && kernel_shm.mode().contains(ShmFlags::SHM_DEST)
{
shm_manager_guard.free_id(shm_id);
}
}
}
}

for page in guard.region.pages() {
if mapper.translate(page.virt_address()).is_none() {
continue;
Expand Down

0 comments on commit 14bf62b

Please sign in to comment.