Skip to content

Commit

Permalink
epoll: keep strong reference while blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Dec 28, 2024
1 parent 660b1a2 commit 8663c1c
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/shims/unix/linux_like/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ struct Epoll {
// it.
ready_list: Rc<ReadyList>,
/// A list of thread ids blocked on this epoll instance.
thread_id: RefCell<Vec<ThreadId>>,
blocked_tid: RefCell<Vec<ThreadId>>,
}

impl VisitProvenance for Epoll {
fn visit_provenance(&self, _visit: &mut VisitWith<'_>) {
// No provenance anywhere in this type.
}
}

/// EpollEventInstance contains information that will be returned by epoll_wait.
Expand Down Expand Up @@ -454,24 +460,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let Some(epfd) = this.machine.fds.get(epfd_value) else {
return this.set_last_error_and_return(LibcError("EBADF"), dest);
};
let epoll_file_description = epfd
.downcast::<Epoll>()
.ok_or_else(|| err_unsup_format!("non-epoll FD passed to `epoll_wait`"))?;
// Create a weak ref of epfd and pass it to callback so we will make sure that epfd
// is not close after the thread unblocks.
let weak_epfd = FileDescriptionRef::downgrade(&epoll_file_description);
let Some(epfd) = epfd.downcast::<Epoll>() else {
return this.set_last_error_and_return(LibcError("EBADF"), dest);
};

// We just need to know if the ready list is empty and borrow the thread_ids out.
// The whole logic is wrapped inside a block so we don't need to manually drop epfd later.
let ready_list_empty;
let mut thread_ids;
{
ready_list_empty = epoll_file_description.ready_list.mapping.borrow().is_empty();
thread_ids = epoll_file_description.thread_id.borrow_mut();
}
let ready_list_empty = epfd.ready_list.mapping.borrow().is_empty();
if timeout == 0 || !ready_list_empty {
// If the ready list is not empty, or the timeout is 0, we can return immediately.
return_ready_list(epfd_value, weak_epfd, dest, &event, this)?;
return_ready_list(&epfd, dest, &event, this)?;
} else {
// Blocking
let timeout = match timeout {
Expand All @@ -486,30 +483,30 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
);
}
};
thread_ids.push(this.active_thread());
// Record this thread as blocked.
epfd.blocked_tid.borrow_mut().push(this.active_thread());
// And block it.
let dest = dest.clone();
// We keep a strong ref to the underlying `Epoll` to make sure it sticks around.
// This means there'll be a leak if we never wake up, but that anyway would imply
// a thread is permanently blocked so this is fine.
this.block_thread(
BlockReason::Epoll,
timeout,
callback!(
@capture<'tcx> {
epfd_value: i32,
weak_epfd: WeakFileDescriptionRef<Epoll>,
epfd: FileDescriptionRef<Epoll>,
dest: MPlaceTy<'tcx>,
event: MPlaceTy<'tcx>,
}
@unblock = |this| {
return_ready_list(epfd_value, weak_epfd, &dest, &event, this)?;
return_ready_list(&epfd, &dest, &event, this)?;
interp_ok(())
}
@timeout = |this| {
// No notification after blocking timeout.
let Some(epfd) = weak_epfd.upgrade() else {
throw_unsup_format!("epoll FD {epfd_value} got closed while blocking.")
};
// Remove the current active thread_id from the blocked thread_id list.
epfd
.thread_id.borrow_mut()
.blocked_tid.borrow_mut()
.retain(|&id| id != this.active_thread());
this.write_int(0, &dest)?;
interp_ok(())
Expand Down Expand Up @@ -554,7 +551,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// holds a strong ref to epoll_interest.
let epfd = epoll_interest.borrow().weak_epfd.upgrade().unwrap();
// FIXME: We can randomly pick a thread to unblock.
if let Some(thread_id) = epfd.thread_id.borrow_mut().pop() {
if let Some(thread_id) = epfd.blocked_tid.borrow_mut().pop() {
waiter.push(thread_id);
};
}
Expand Down Expand Up @@ -625,16 +622,11 @@ fn check_and_update_one_event_interest<'tcx>(
/// Stores the ready list of the `epfd` epoll instance into `events` (which must be an array),
/// and the number of returned events into `dest`.
fn return_ready_list<'tcx>(
epfd_value: i32,
weak_epfd: WeakFileDescriptionRef<Epoll>,
epfd: &FileDescriptionRef<Epoll>,
dest: &MPlaceTy<'tcx>,
events: &MPlaceTy<'tcx>,
ecx: &mut MiriInterpCx<'tcx>,
) -> InterpResult<'tcx> {
let Some(epfd) = weak_epfd.upgrade() else {
throw_unsup_format!("epoll FD {epfd_value} got closed while blocking.")
};

let ready_list = epfd.get_ready_list();

let mut ready_list = ready_list.mapping.borrow_mut();
Expand Down

0 comments on commit 8663c1c

Please sign in to comment.