fix page table racing by adding a global mutex

master
WangRunji 6 years ago
parent ba8d08d733
commit fbd2aef916

@ -16,7 +16,7 @@ use super::HEAP_ALLOCATOR;
pub use crate::arch::paging::*; pub use crate::arch::paging::*;
use crate::consts::{KERNEL_OFFSET, MEMORY_OFFSET}; use crate::consts::{KERNEL_OFFSET, MEMORY_OFFSET};
use crate::process::current_thread; use crate::process::current_thread;
use crate::sync::SpinNoIrqLock; use crate::sync::{SpinNoIrqLock, MutexGuard, SpinNoIrq};
use alloc::boxed::Box; use alloc::boxed::Box;
use bitmap_allocator::BitAlloc; use bitmap_allocator::BitAlloc;
use buddy_system_allocator::Heap; use buddy_system_allocator::Heap;
@ -52,24 +52,19 @@ pub type FrameAlloc = bitmap_allocator::BitAlloc4K;
lazy_static! { lazy_static! {
pub static ref FRAME_ALLOCATOR: SpinNoIrqLock<FrameAlloc> = pub static ref FRAME_ALLOCATOR: SpinNoIrqLock<FrameAlloc> =
SpinNoIrqLock::new(FrameAlloc::default()); SpinNoIrqLock::new(FrameAlloc::default());
pub static ref ACTIVE_TABLE: SpinNoIrqLock<ActivePageTable> =
SpinNoIrqLock::new(unsafe { ActivePageTable::new() });
} }
/// The only way to get active page table /// The only way to get current active page table safely
/// ///
/// ## CHANGE LOG /// NOTE:
/// /// Current implementation of recursive page table has a problem that
/// In the past, this function returns a `MutexGuard` of a global /// will cause race condition to the initial page table.
/// `Mutex<ActiveTable>` object, which means only one CPU core /// So we have to add a global mutex to avoid the racing.
/// can access its active table at a time. /// This will be removed after replacing recursive mapping by linear mapping.
/// pub fn active_table() -> MutexGuard<'static, ActivePageTable, SpinNoIrq> {
/// But given that a page table is ** process local **, and being active ACTIVE_TABLE.lock()
/// when and only when a thread of the process is running.
/// The ownership of this page table is in the `MemorySet` object.
/// So it's safe to access the active table inside `MemorySet`.
/// But the shared parts is readonly, e.g. all pages mapped in
/// `InactivePageTable::map_kernel()`.
pub fn active_table() -> ActivePageTable {
unsafe { ActivePageTable::new() }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]

Loading…
Cancel
Save