From 0edfc07939456c52fab73f57ce575ae0d91e5216 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 23 Mar 2019 23:50:30 +0800 Subject: [PATCH] Allow page fault handler to bypass process lock and fix thread pool wakeup for exited process --- crate/thread/src/thread_pool.rs | 9 ++++++++- kernel/src/arch/x86_64/driver/mod.rs | 1 - kernel/src/arch/x86_64/driver/rtc_cmos.rs | 1 - kernel/src/drivers/net/ixgbe.rs | 3 --- kernel/src/drivers/net/virtio_net.rs | 2 -- kernel/src/memory.rs | 8 ++++++-- kernel/src/net/structs.rs | 3 --- kernel/src/net/test.rs | 1 - kernel/src/process/mod.rs | 8 ++++++++ kernel/src/process/structs.rs | 1 - kernel/src/syscall/mem.rs | 3 ++- kernel/src/syscall/mod.rs | 4 ++++ 12 files changed, 28 insertions(+), 16 deletions(-) diff --git a/crate/thread/src/thread_pool.rs b/crate/thread/src/thread_pool.rs index a951d98b..33306c8e 100644 --- a/crate/thread/src/thread_pool.rs +++ b/crate/thread/src/thread_pool.rs @@ -190,7 +190,14 @@ impl ThreadPool { } pub fn wakeup(&self, tid: Tid) { - self.set_status(tid, Status::Ready); + let mut proc_lock = self.threads[tid].lock(); + if let Some(mut proc) = proc_lock.as_mut() { + trace!("thread {} {:?} -> {:?}", tid, proc.status, status); + if let Status::Sleeping = proc.status { + proc.status = Status::Ready; + self.scheduler.push(tid); + } + } } pub fn exit(&self, tid: Tid, code: ExitCode) { diff --git a/kernel/src/arch/x86_64/driver/mod.rs b/kernel/src/arch/x86_64/driver/mod.rs index de21fe54..d370c5fd 100644 --- a/kernel/src/arch/x86_64/driver/mod.rs +++ b/kernel/src/arch/x86_64/driver/mod.rs @@ -1,5 +1,4 @@ use once::*; -use crate::arch::interrupt::{consts, enable_irq}; pub mod vga; pub mod serial; diff --git a/kernel/src/arch/x86_64/driver/rtc_cmos.rs b/kernel/src/arch/x86_64/driver/rtc_cmos.rs index f5d0c750..58645d00 100644 --- a/kernel/src/arch/x86_64/driver/rtc_cmos.rs +++ b/kernel/src/arch/x86_64/driver/rtc_cmos.rs @@ -1,6 +1,5 @@ //! Driver for x86 CMOS RTC clock use crate::arch::interrupt; -use log::*; use x86_64::instructions::port::Port; const CMOS_ADDR: u16 = 0x70; diff --git a/kernel/src/drivers/net/ixgbe.rs b/kernel/src/drivers/net/ixgbe.rs index 032e9b80..284f4576 100644 --- a/kernel/src/drivers/net/ixgbe.rs +++ b/kernel/src/drivers/net/ixgbe.rs @@ -11,19 +11,16 @@ use rcore_memory::paging::PageTable; use rcore_memory::PAGE_SIZE; use smoltcp::iface::*; use smoltcp::phy::{self, Checksum, DeviceCapabilities}; -use smoltcp::socket::*; use smoltcp::time::Instant; use smoltcp::wire::EthernetAddress; use smoltcp::wire::*; use smoltcp::Result; -use volatile::Volatile; use crate::memory::active_table; use crate::net::SOCKETS; use crate::sync::FlagsGuard; use crate::sync::SpinNoIrqLock as Mutex; use crate::sync::{MutexGuard, SpinNoIrq}; -use crate::HEAP_ALLOCATOR; use super::super::{provider::Provider, DeviceType, Driver, DRIVERS, NET_DRIVERS, SOCKET_ACTIVITY}; diff --git a/kernel/src/drivers/net/virtio_net.rs b/kernel/src/drivers/net/virtio_net.rs index 7b151b93..397bd5d7 100644 --- a/kernel/src/drivers/net/virtio_net.rs +++ b/kernel/src/drivers/net/virtio_net.rs @@ -12,7 +12,6 @@ use log::*; use rcore_memory::paging::PageTable; use rcore_memory::PAGE_SIZE; use smoltcp::phy::{self, DeviceCapabilities}; -use smoltcp::socket::SocketSet; use smoltcp::time::Instant; use smoltcp::wire::{EthernetAddress, Ipv4Address}; use smoltcp::Result; @@ -20,7 +19,6 @@ use volatile::{ReadOnly, Volatile}; use crate::memory::active_table; use crate::sync::SpinNoIrqLock as Mutex; -use crate::sync::{MutexGuard, SpinNoIrq}; use crate::HEAP_ALLOCATOR; use super::super::bus::virtio_mmio::*; diff --git a/kernel/src/memory.rs b/kernel/src/memory.rs index 1adf4e22..78adf0a5 100644 --- a/kernel/src/memory.rs +++ b/kernel/src/memory.rs @@ -4,7 +4,7 @@ use crate::consts::MEMORY_OFFSET; use super::HEAP_ALLOCATOR; use rcore_memory::*; pub use rcore_memory::memory_set::{MemoryArea, MemoryAttr, handler::*}; -use crate::process::{process}; +use crate::process::process_unsafe; use crate::sync::SpinNoIrqLock; use lazy_static::*; use log::*; @@ -102,7 +102,11 @@ impl Drop for KernelStack { #[cfg(not(feature = "no_mmu"))] pub fn handle_page_fault(addr: usize) -> bool { debug!("page fault @ {:#x}", addr); - process().vm.handle_page_fault(addr) + + // This is safe as long as page fault never happens in page fault handler + unsafe { + process_unsafe().vm.handle_page_fault(addr) + } } pub fn init_heap() { diff --git a/kernel/src/net/structs.rs b/kernel/src/net/structs.rs index bc883619..d24abe4b 100644 --- a/kernel/src/net/structs.rs +++ b/kernel/src/net/structs.rs @@ -1,9 +1,6 @@ use alloc::sync::Arc; -use core::fmt; - use crate::arch::rand; use crate::drivers::{NET_DRIVERS, SOCKET_ACTIVITY}; -use crate::process::structs::Process; use crate::sync::SpinNoIrqLock as Mutex; use crate::syscall::*; diff --git a/kernel/src/net/test.rs b/kernel/src/net/test.rs index ee40d1db..9e0633a3 100644 --- a/kernel/src/net/test.rs +++ b/kernel/src/net/test.rs @@ -1,4 +1,3 @@ -use crate::drivers::Driver; use crate::drivers::NET_DRIVERS; use crate::net::SOCKETS; use crate::thread; diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index 08b88726..64348dc7 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -32,6 +32,14 @@ pub fn process() -> MutexGuard<'static, Process> { current_thread().proc.lock() } +/// Get current process, ignoring its lock +/// Only use this when necessary +pub unsafe fn process_unsafe() -> MutexGuard<'static, Process> { + let thread = current_thread(); + thread.proc.force_unlock(); + thread.proc.lock() +} + /// Get current thread /// /// FIXME: It's obviously unsafe to get &mut ! diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index 9998bddd..2463beb9 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -6,7 +6,6 @@ use spin::{Mutex, RwLock}; use xmas_elf::{ElfFile, header, program::{Flags, Type}}; use rcore_memory::PAGE_SIZE; use rcore_thread::Tid; -use rcore_fs::vfs::FileType; use core::str; use crate::arch::interrupt::{Context, TrapFrame}; diff --git a/kernel/src/syscall/mem.rs b/kernel/src/syscall/mem.rs index 457131f1..8a78b053 100644 --- a/kernel/src/syscall/mem.rs +++ b/kernel/src/syscall/mem.rs @@ -66,7 +66,8 @@ pub fn sys_mmap( let file = proc.get_file(fd)?; let read_len = file.read_at(offset, data)?; if read_len != data.len() { - data[read_len..].iter_mut().map(|x| *x = 0); + // use count() to consume the iterator + data[read_len..].iter_mut().map(|x| *x = 0).count(); } return Ok(addr); } diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index ad8deb58..a7bc453b 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -184,6 +184,10 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { warn!("mount is unimplemented"); Err(SysError::EACCES) } + SYS_UMOUNT2 => { + warn!("umount2 is unimplemented"); + Err(SysError::EACCES) + } SYS_REBOOT => sys_reboot(args[0] as u32, args[1] as u32, args[2] as u32, args[3] as *const u8), SYS_GETTID => sys_gettid(), SYS_FUTEX => sys_futex(args[0], args[1] as u32, args[2] as i32, args[3] as *const TimeSpec),