diff --git a/src/context/context.rs b/src/context/context.rs index 5926a16f84..8e7c3b8c21 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -10,7 +10,7 @@ use core::{ cmp::Ordering, mem, }; -use spin::{Mutex, RwLock}; +use spin::RwLock; use crate::arch::{interrupt::InterruptStack, paging::PAGE_SIZE}; use crate::common::unique::Unique; @@ -233,7 +233,7 @@ pub struct Context { /// User Thread local storage pub tls: Option, /// User grants - pub grants: Arc>, + pub grants: Arc>, /// The name of the context pub name: Arc>>, /// The current working directory @@ -291,7 +291,7 @@ impl Context { stack: None, sigstack: None, tls: None, - grants: Arc::new(Mutex::new(UserGrants::default())), + grants: Arc::new(RwLock::new(UserGrants::default())), name: Arc::new(RwLock::new(String::new().into_boxed_str())), cwd: Arc::new(RwLock::new(Vec::new())), files: Arc::new(RwLock::new(Vec::new())), diff --git a/src/scheme/memory.rs b/src/scheme/memory.rs index fe8810ee35..f0502a1478 100644 --- a/src/scheme/memory.rs +++ b/src/scheme/memory.rs @@ -40,7 +40,7 @@ impl Scheme for MemoryScheme { let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; let context = context_lock.read(); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); let region = grants.find_free_at(VirtualAddress::new(map.address), map.size, map.flags)?.round(); diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs index 26e3ae0514..3002f5558d 100644 --- a/src/scheme/sys/context.rs +++ b/src/scheme/sys/context.rs @@ -90,7 +90,7 @@ pub fn resource() -> Result> { if let Some(ref sigstack) = context.sigstack { memory += sigstack.size(); } - for grant in context.grants.lock().iter() { + for grant in context.grants.read().iter() { if grant.is_owned() { memory += grant.size(); } diff --git a/src/scheme/user.rs b/src/scheme/user.rs index 024a3ff577..5029e07388 100644 --- a/src/scheme/user.rs +++ b/src/scheme/user.rs @@ -126,7 +126,7 @@ impl UserInner { let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_table()) }; let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET))); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); let from_address = round_down_pages(address); let offset = address - from_address; @@ -157,7 +157,7 @@ impl UserInner { let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_table()) }; let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET))); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); if let Some(region) = grants.contains(VirtualAddress::new(address)).map(Region::from) { grants.take(®ion).unwrap().unmap_inactive(&mut new_table, &mut temporary_page); diff --git a/src/syscall/driver.rs b/src/syscall/driver.rs index 93963ffdf2..eb64f36362 100644 --- a/src/syscall/driver.rs +++ b/src/syscall/driver.rs @@ -74,7 +74,7 @@ pub fn inner_physmap(physical_address: usize, size: usize, flags: PhysmapFlags) let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; let context = context_lock.read(); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); let from_address = (physical_address/4096) * 4096; let offset = physical_address - from_address; @@ -128,7 +128,7 @@ pub fn inner_physunmap(virtual_address: usize) -> Result { let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; let context = context_lock.read(); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); if let Some(region) = grants.contains(VirtualAddress::new(virtual_address)).map(Region::from) { grants.take(®ion).unwrap().unmap(); diff --git a/src/syscall/fs.rs b/src/syscall/fs.rs index dd19ea42a0..bd4684ad3f 100644 --- a/src/syscall/fs.rs +++ b/src/syscall/fs.rs @@ -455,7 +455,7 @@ pub fn funmap_old(virtual_address: usize) -> Result { let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; let context = context_lock.read(); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); if let Some(region) = grants.contains(VirtualAddress::new(virtual_address)).map(Region::from) { let mut grant = grants.take(®ion).unwrap(); @@ -503,7 +503,7 @@ pub fn funmap(virtual_address: usize, length: usize) -> Result { let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; let context = context_lock.read(); - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); let conflicting: Vec = grants.conflicts(requested).map(Region::from).collect(); diff --git a/src/syscall/process.rs b/src/syscall/process.rs index ec6cd977ed..4e4b54cf6d 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -8,7 +8,7 @@ use alloc::{ use core::alloc::{GlobalAlloc, Layout}; use core::ops::DerefMut; use core::{intrinsics, mem}; -use spin::{RwLock, Mutex}; +use spin::RwLock; use crate::context::file::FileDescriptor; use crate::context::{ContextId, WaitpidKey}; @@ -224,11 +224,11 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result { grants = Arc::clone(&context.grants); } else { let mut grants_set = UserGrants::default(); - for grant in context.grants.lock().iter() { + for grant in context.grants.read().iter() { let start = VirtualAddress::new(grant.start_address().data() + crate::USER_TMP_GRANT_OFFSET - crate::USER_GRANT_OFFSET); grants_set.insert(grant.secret_clone(start)); } - grants = Arc::new(Mutex::new(grants_set)); + grants = Arc::new(RwLock::new(grants_set)); } if flags.contains(CLONE_VM) { @@ -275,7 +275,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result { // If not cloning virtual memory, use fmap to re-obtain every grant where possible if !flags.contains(CLONE_VM) { - let mut grants = grants.lock(); + let mut grants = grants.write(); let mut to_remove = BTreeSet::new(); @@ -395,7 +395,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result { context.image = image; // Copy grant mapping - if ! grants.lock().is_empty() { + if ! grants.read().is_empty() { let frame = active_table.p4()[crate::USER_GRANT_PML4].pointed_frame().expect("user grants not mapped"); let flags = active_table.p4()[crate::USER_GRANT_PML4].flags(); active_table.with(&mut new_table, &mut temporary_page, |mapper| { @@ -441,7 +441,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result { // Move grants { - let mut grants = grants.lock(); + let mut grants = grants.write(); let old_grants = mem::replace(&mut *grants, UserGrants::default()); for mut grant in old_grants.inner.into_iter() { @@ -558,7 +558,7 @@ fn empty(context: &mut context::Context, reaping: bool) { drop(context.tls.take()); } - let mut grants = context.grants.lock(); + let mut grants = context.grants.write(); if Arc::strong_count(&context.grants) == 1 { let grants = mem::replace(&mut *grants, UserGrants::default()); for grant in grants.inner.into_iter() {