Skip to content

Commit

Permalink
[Libos] Fix bug in closing socket files
Browse files Browse the repository at this point in the history
  • Loading branch information
ClawSeven committed Apr 3, 2024
1 parent 9de3456 commit 3fc5b8d
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/libos/crates/io-uring-callback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ impl IoUring {
/// to 0. If the user does not want to the method to busy polling, set
/// `polling_retries` to 0.
pub fn poll_completions(&self, min_complete: usize, polling_retries: usize) -> usize {
// let cq = self.ring.completion();
let mut cq = unsafe { self.ring.completion_shared() }; // Safety: Only polling thread is using the completion queue
let mut nr_complete = 0;
loop {
Expand All @@ -439,7 +438,6 @@ impl IoUring {
if token_key != IoUring::CANCEL_TOKEN_KEY {
let io_token = {
let token_idx = token_key as usize;
// let mut token_table = self.token_table.lock().unwrap();
let mut token_table = self.token_table.lock();
token_table.remove(token_idx)
};
Expand Down
2 changes: 2 additions & 0 deletions src/libos/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub extern "C" fn occlum_ecall_init(

vm::init_user_space();

crate::io_uring::MULTITON.poll_completions();

// Init boot up time stamp here.
time::init();

Expand Down
2 changes: 1 addition & 1 deletion src/libos/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait File: Debug + Sync + Send + Any {

/// Return the host fd, if the file is backed by an underlying host file.
fn host_fd(&self) -> Option<&HostFd> {
return None;
None
}

/// Update the ready events of a host file.
Expand Down
15 changes: 13 additions & 2 deletions src/libos/src/io_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,19 @@ impl UringSet {
pub fn poll_completions(&self) {
let mut guard = self.urings.lock();

for (uring, state) in guard.iter_mut() {
state.enable_poll(uring.clone().into())
for _ in 0..URING_LIMIT {
let uring: KeyableArc<IoUring> = Arc::new(
Builder::new()
.setup_sqpoll(500 /* ms */)
.build(256)
.unwrap(),
)
.into();
let mut state = UringState::default();
state.enable_poll(uring.clone().into());

guard.insert(uring.clone(), state);
self.running_uring_num.fetch_add(1, Ordering::Relaxed);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libos/src/net/socket/uring/file_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl File for SocketFile {
}

fn update_host_events(&self, ready: &IoEvents, mask: &IoEvents, trigger_notifier: bool) {
panic!()
unreachable!()
}

fn as_any(&self) -> &dyn core::any::Any {
Expand Down
6 changes: 6 additions & 0 deletions src/libos/src/net/socket/uring/socket_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ impl SocketFile {
}
}

impl Drop for SocketFile {
fn drop(&mut self) {
self.close();
}
}

mod impls {
use super::*;
use io_uring_callback::IoUring;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ impl<A: Addr + 'static, R: Runtime> ConnectedStream<A, R> {
// No complete request to wake. Just cancel the send requests.
let io_uring = self.common.io_uring();
let inner = self.sender.inner.lock();
// let inner = self.sender.inner.lock().unwrap();
if let Some(io_handle) = &inner.io_handle {
unsafe { io_uring.cancel(io_handle) };
// Loop again to wait the cancel request to complete
Expand Down
6 changes: 5 additions & 1 deletion src/libos/src/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::prelude::*;
const SOMAXCONN: u32 = 4096;
const SOCONN_DEFAULT: u32 = 16;

pub const ENABLE_URING: bool = false;
pub const ENABLE_URING: bool = true;

#[repr(C)]
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -264,6 +264,10 @@ pub fn do_setsockopt(
"setsockopt: fd: {}, level: {}, optname: {}, optval: {:?}, optlen: {:?}",
fd, level, optname, optval, optlen
);
if optval as usize != 0 && optlen == 0 {
return_errno!(EINVAL, "the optlen size is 0");
}

let file_ref = current!().file(fd as FileDesc)?;
let optval = from_user::make_slice(optval as *const u8, optlen as usize)?;

Expand Down
1 change: 1 addition & 0 deletions src/libos/src/process/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
};
use crate::events::HostEventFd;
use crate::fs::{EventCreationFlags, EventFile};
use crate::net::socket::uring::UringSocketType;
use crate::net::THREAD_NOTIFIERS;
use crate::prelude::*;
use crate::signal::{SigQueues, SigSet, SigStack};
Expand Down
6 changes: 3 additions & 3 deletions src/libos/src/util/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<T> Mutex<T> {
pub fn lock(&self) -> MutexGuard<'_, T> {
if self
.lock
.compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{
self.lock_contended()
Expand All @@ -62,7 +62,7 @@ impl<T> Mutex<T> {
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
if self
.lock
.compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
{
Some(MutexGuard { inner: self })
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<T> Mutex<T> {
if state == 0 {
match self
.lock
.compare_exchange_weak(0, 1, Ordering::Acquire, Ordering::Relaxed)
.compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
{
Ok(_) => return, // Locked!
Err(s) => state = s,
Expand Down

0 comments on commit 3fc5b8d

Please sign in to comment.