From 8fcd375bd9f76b71f1575ea9c69defaae913c02a Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 13 Feb 2021 13:06:13 -0700 Subject: [PATCH] Switch Context::grants to RwLock --- src/context/context.rs | 6 +++--- src/scheme/memory.rs | 2 +- src/scheme/sys/context.rs | 2 +- src/scheme/user.rs | 4 ++-- src/syscall/driver.rs | 4 ++-- src/syscall/fs.rs | 4 ++-- src/syscall/process.rs | 14 +++++++------- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/context/context.rs b/src/context/context.rs index 5926a16..8e7c3b8 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 5fc4e55..9ee45c1 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 26e3ae0..3002f55 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 c28b8a4..a0e3046 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_utable()) }; 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_utable()) }; 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 f6e39b6..045b3ed 100644 --- a/src/syscall/driver.rs +++ b/src/syscall/driver.rs @@ -80,7 +80,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; @@ -135,7 +135,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 dd19ea4..bd4684a 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 94d3766..d91d049 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}; @@ -237,11 +237,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) { @@ -288,7 +288,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(); @@ -420,7 +420,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_utable.p4()[crate::USER_GRANT_PML4].pointed_frame().expect("user grants not mapped"); let flags = active_utable.p4()[crate::USER_GRANT_PML4].flags(); active_utable.with(&mut new_utable, &mut temporary_upage, |mapper| { @@ -466,7 +466,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() { @@ -606,7 +606,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() {