Make mprotect&munmap return fast when len is zero

This commit is contained in:
Jianfeng Jiang
2024-08-16 07:38:56 +00:00
committed by Tate, Hongliang Tian
parent 212440dbb1
commit aa77747f94
2 changed files with 19 additions and 1 deletions

View File

@ -12,7 +12,17 @@ pub fn sys_mprotect(addr: Vaddr, len: usize, perms: u64, ctx: &Context) -> Resul
addr, len, vm_perms
);
let root_vmar = ctx.process.root_vmar();
debug_assert!(addr % PAGE_SIZE == 0);
// According to linux behavior,
// <https://elixir.bootlin.com/linux/v6.0.9/source/mm/mprotect.c#L681>,
// the addr is checked even if len is 0.
if addr % PAGE_SIZE != 0 {
return_errno_with_message!(Errno::EINVAL, "the start address should be page aligned");
}
if len == 0 {
return Ok(SyscallReturn::Return(0));
}
let len = len.align_up(PAGE_SIZE);
let range = addr..(addr + len);
root_vmar.protect(vm_perms, range)?;

View File

@ -7,6 +7,14 @@ use crate::prelude::*;
pub fn sys_munmap(addr: Vaddr, len: usize, ctx: &Context) -> Result<SyscallReturn> {
debug!("addr = 0x{:x}, len = {}", addr, len);
if addr % PAGE_SIZE != 0 {
return_errno_with_message!(Errno::EINVAL, "munmap addr must be page-aligned");
}
if len == 0 {
return_errno_with_message!(Errno::EINVAL, "munmap len cannot be zero");
}
let root_vmar = ctx.process.root_vmar();
let len = len.align_up(PAGE_SIZE);
debug!("unmap range = 0x{:x} - 0x{:x}", addr, addr + len);