1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:17:44 +00:00

Kernel: Move block condition evaluation out of the Scheduler

This makes the Scheduler a lot leaner by not having to evaluate
block conditions every time it is invoked. Instead evaluate them as
the states change, and unblock threads at that point.

This also implements some more waitid/waitpid/wait features and
behavior. For example, WUNTRACED and WNOWAIT are now supported. And
wait will now not return EINTR when SIGCHLD is delivered at the
same time.
This commit is contained in:
Tom 2020-11-29 16:05:27 -07:00 committed by Andreas Kling
parent 6a620562cc
commit 046d6855f5
53 changed files with 2027 additions and 930 deletions

View file

@ -568,9 +568,6 @@ int Process::sys$execve(Userspace<const Syscall::SC_execve_params*> user_params)
if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
return -E2BIG;
if (wait_for_tracer_at_next_execve())
Thread::current()->send_urgent_signal_to_self(SIGSTOP);
String path;
{
auto path_arg = get_syscall_path_argument(params.path);

View file

@ -111,10 +111,8 @@ KResult Process::do_killself(int signal)
return KSuccess;
auto current_thread = Thread::current();
if (!current_thread->should_ignore_signal(signal)) {
if (!current_thread->should_ignore_signal(signal))
current_thread->send_signal(signal, this);
(void)current_thread->block<Thread::SemiPermanentBlocker>(nullptr, Thread::SemiPermanentBlocker::Reason::Signal);
}
return KSuccess;
}

View file

@ -50,10 +50,12 @@ ssize_t Process::sys$read(int fd, Userspace<u8*> buffer, ssize_t size)
return -EISDIR;
if (description->is_blocking()) {
if (!description->can_read()) {
if (Thread::current()->block<Thread::ReadBlocker>(nullptr, *description).was_interrupted())
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
if (Thread::current()->block<Thread::ReadBlocker>(nullptr, *description, unblock_flags).was_interrupted())
return -EINTR;
if (!description->can_read())
if (!((u32)unblock_flags & (u32)Thread::FileBlocker::BlockFlags::Read))
return -EAGAIN;
// TODO: handle exceptions in unblock_flags
}
}
auto user_buffer = UserOrKernelBuffer::for_user_buffer(buffer, size);

View file

@ -68,66 +68,77 @@ int Process::sys$select(const Syscall::SC_select_params* user_params)
current_thread->update_signal_mask(previous_signal_mask);
});
Thread::SelectBlocker::FDVector rfds;
Thread::SelectBlocker::FDVector wfds;
Thread::SelectBlocker::FDVector efds;
fd_set fds_read, fds_write, fds_except;
if (params.readfds && !copy_from_user(&fds_read, params.readfds))
return -EFAULT;
if (params.writefds && !copy_from_user(&fds_write, params.writefds))
return -EFAULT;
if (params.exceptfds && !copy_from_user(&fds_except, params.exceptfds))
return -EFAULT;
auto transfer_fds = [&](auto* fds_unsafe, auto& vector) -> int {
vector.clear_with_capacity();
if (!fds_unsafe)
return 0;
fd_set fds;
if (!copy_from_user(&fds, fds_unsafe))
return -EFAULT;
for (int fd = 0; fd < params.nfds; ++fd) {
if (FD_ISSET(fd, &fds)) {
if (!file_description(fd)) {
dbg() << "sys$select: Bad fd number " << fd;
return -EBADF;
}
vector.append(fd);
}
Thread::SelectBlocker::FDVector fds_info;
Vector<int> fds;
for (int fd = 0; fd < params.nfds; fd++) {
u32 block_flags = (u32)Thread::FileBlocker::BlockFlags::None;
if (params.readfds && FD_ISSET(fd, &fds_read))
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
if (params.writefds && FD_ISSET(fd, &fds_write))
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
if (params.exceptfds && FD_ISSET(fd, &fds_except))
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Exception;
if (block_flags == (u32)Thread::FileBlocker::BlockFlags::None)
continue;
auto description = file_description(fd);
if (!description) {
dbg() << "sys$select: Bad fd number " << fd;
return -EBADF;
}
return 0;
};
if (int error = transfer_fds(params.writefds, wfds))
return error;
if (int error = transfer_fds(params.readfds, rfds))
return error;
if (int error = transfer_fds(params.exceptfds, efds))
return error;
#if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
dbg() << "selecting on (read:" << rfds.size() << ", write:" << wfds.size() << "), timeout=" << params.timeout;
#endif
if (timeout.should_block()) {
if (current_thread->block<Thread::SelectBlocker>(timeout, rfds, wfds, efds).was_interrupted())
return -EINTR;
fds_info.append({ description.release_nonnull(), (Thread::FileBlocker::BlockFlags)block_flags });
fds.append(fd);
}
int marked_fd_count = 0;
auto mark_fds = [&](auto* fds_unsafe, auto& vector, auto should_mark) {
if (!fds_unsafe)
return 0;
fd_set fds;
FD_ZERO(&fds);
for (int fd : vector) {
if (auto description = file_description(fd); description && should_mark(*description)) {
FD_SET(fd, &fds);
++marked_fd_count;
}
}
if (!copy_to_user(fds_unsafe, &fds))
return -EFAULT;
return 0;
};
if (int error = mark_fds(params.readfds, rfds, [](auto& description) { return description.can_read(); }))
return error;
if (int error = mark_fds(params.writefds, wfds, [](auto& description) { return description.can_write(); }))
return error;
// FIXME: We should also mark exceptfds as appropriate.
#if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
dbg() << "selecting on " << fds_info.size() << " fds, timeout=" << params.timeout;
#endif
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted()) {
dbg() << "select was interrupted";
return -EINTR;
}
if (params.readfds)
FD_ZERO(&fds_read);
if (params.writefds)
FD_ZERO(&fds_write);
if (params.exceptfds)
FD_ZERO(&fds_except);
int marked_fd_count = 0;
for (size_t i = 0; i < fds_info.size(); i++) {
auto& fd_entry = fds_info[i];
if (fd_entry.unblocked_flags == Thread::FileBlocker::BlockFlags::None)
continue;
if (params.readfds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Read)) {
FD_SET(fds[i], &fds_read);
marked_fd_count++;
}
if (params.writefds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Write)) {
FD_SET(fds[i], &fds_write);
marked_fd_count++;
}
if (params.exceptfds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Exception)) {
FD_SET(fds[i], &fds_except);
marked_fd_count++;
}
}
if (params.readfds && !copy_to_user(params.readfds, &fds_read))
return -EFAULT;
if (params.writefds && !copy_to_user(params.writefds, &fds_write))
return -EFAULT;
if (params.exceptfds && !copy_to_user(params.exceptfds, &fds_except))
return -EFAULT;
return marked_fd_count;
}
@ -165,15 +176,22 @@ int Process::sys$poll(Userspace<const Syscall::SC_poll_params*> user_params)
return -EFAULT;
}
Thread::SelectBlocker::FDVector rfds;
Thread::SelectBlocker::FDVector wfds;
for (unsigned i = 0; i < params.nfds; ++i) {
Thread::SelectBlocker::FDVector fds_info;
for (size_t i = 0; i < params.nfds; i++) {
auto& pfd = fds_copy[i];
auto description = file_description(pfd.fd);
if (!description) {
dbg() << "sys$poll: Bad fd number " << pfd.fd;
return -EBADF;
}
u32 block_flags = (u32)Thread::FileBlocker::BlockFlags::Exception; // always want POLLERR, POLLHUP, POLLNVAL
if (pfd.events & POLLIN)
rfds.append(pfd.fd);
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
if (pfd.events & POLLOUT)
wfds.append(pfd.fd);
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
if (pfd.events & POLLPRI)
block_flags |= (u32)Thread::FileBlocker::BlockFlags::ReadPriority;
fds_info.append({ description.release_nonnull(), (Thread::FileBlocker::BlockFlags)block_flags });
}
auto current_thread = Thread::current();
@ -187,31 +205,45 @@ int Process::sys$poll(Userspace<const Syscall::SC_poll_params*> user_params)
});
#if defined(DEBUG_IO) || defined(DEBUG_POLL_SELECT)
dbg() << "polling on (read:" << rfds.size() << ", write:" << wfds.size() << ")";
dbg() << "polling on " << fds_info.size() << " fds, timeout=" << params.timeout;
#endif
if (timeout.should_block()) {
if (current_thread->block<Thread::SelectBlocker>(timeout, rfds, wfds, Thread::SelectBlocker::FDVector()).was_interrupted())
return -EINTR;
}
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted())
return -EINTR;
int fds_with_revents = 0;
for (unsigned i = 0; i < params.nfds; ++i) {
auto& pfd = fds_copy[i];
auto description = file_description(pfd.fd);
if (!description) {
pfd.revents = POLLNVAL;
} else {
pfd.revents = 0;
if (pfd.events & POLLIN && description->can_read())
pfd.revents |= POLLIN;
if (pfd.events & POLLOUT && description->can_write())
pfd.revents |= POLLOUT;
auto& fds_entry = fds_info[i];
if (pfd.revents)
++fds_with_revents;
pfd.revents = 0;
if (fds_entry.unblocked_flags == Thread::FileBlocker::BlockFlags::None)
continue;
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Exception) {
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::ReadHangUp)
pfd.revents |= POLLRDHUP;
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::WriteError)
pfd.revents |= POLLERR;
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::WriteHangUp)
pfd.revents |= POLLNVAL;
} else {
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Read) {
ASSERT(pfd.events & POLLIN);
pfd.revents |= POLLIN;
}
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::ReadPriority) {
ASSERT(pfd.events & POLLPRI);
pfd.revents |= POLLPRI;
}
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Write) {
ASSERT(pfd.events & POLLOUT);
pfd.revents |= POLLOUT;
}
}
if (pfd.revents)
fds_with_revents++;
}
if (params.nfds > 0 && !copy_to_user(&params.fds[0], &fds_copy[0], params.nfds * sizeof(pollfd)))

View file

@ -112,7 +112,8 @@ int Process::sys$accept(int accepting_socket_fd, Userspace<sockaddr*> user_addre
if (!socket.can_accept()) {
if (accepting_socket_description->is_blocking()) {
if (Thread::current()->block<Thread::AcceptBlocker>(nullptr, *accepting_socket_description).was_interrupted())
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
if (Thread::current()->block<Thread::AcceptBlocker>(nullptr, *accepting_socket_description, unblock_flags).was_interrupted())
return -EINTR;
} else {
return -EAGAIN;

View file

@ -32,74 +32,20 @@ namespace Kernel {
KResultOr<siginfo_t> Process::do_waitid(idtype_t idtype, int id, int options)
{
if (idtype == P_PID) {
ScopedSpinLock lock(g_processes_lock);
if (idtype == P_PID && !Process::from_pid(id))
return KResult(-ECHILD);
// FIXME: Race: After 'lock' releases, the 'id' process might vanish.
// If that is not a problem, why check for it?
// If it is a problem, let's fix it! (Eventually.)
}
ProcessID waitee_pid { 0 };
// FIXME: WaitBlocker should support idtype/id specs directly.
if (idtype == P_ALL) {
waitee_pid = -1;
} else if (idtype == P_PID) {
waitee_pid = id;
} else {
// FIXME: Implement other PID specs.
switch (idtype) {
case P_ALL:
case P_PID:
case P_PGID:
break;
default:
return KResult(-EINVAL);
}
if (Thread::current()->block<Thread::WaitBlocker>(nullptr, options, waitee_pid).was_interrupted())
KResultOr<siginfo_t> result = KResult(KSuccess);
if (Thread::current()->block<Thread::WaitBlocker>(nullptr, options, idtype, id, result).was_interrupted())
return KResult(-EINTR);
ScopedSpinLock lock(g_processes_lock);
// NOTE: If waitee was -1, m_waitee_pid will have been filled in by the scheduler.
auto waitee_process = Process::from_pid(waitee_pid);
if (!waitee_process)
return KResult(-ECHILD);
ASSERT(waitee_process);
if (waitee_process->is_dead()) {
return reap(*waitee_process);
} else {
// FIXME: PID/TID BUG
// Make sure to hold the scheduler lock so that we operate on a consistent state
ScopedSpinLock scheduler_lock(g_scheduler_lock);
auto waitee_thread = Thread::from_tid(waitee_pid.value());
if (!waitee_thread)
return KResult(-ECHILD);
ASSERT((options & WNOHANG) || waitee_thread->state() == Thread::State::Stopped);
siginfo_t siginfo;
memset(&siginfo, 0, sizeof(siginfo));
siginfo.si_signo = SIGCHLD;
siginfo.si_pid = waitee_process->pid().value();
siginfo.si_uid = waitee_process->uid();
switch (waitee_thread->state()) {
case Thread::State::Stopped:
siginfo.si_code = CLD_STOPPED;
break;
case Thread::State::Running:
case Thread::State::Runnable:
case Thread::State::Blocked:
case Thread::State::Dying:
case Thread::State::Dead:
case Thread::State::Queued:
siginfo.si_code = CLD_CONTINUED;
break;
default:
ASSERT_NOT_REACHED();
break;
}
siginfo.si_status = waitee_thread->m_stop_signal;
return siginfo;
}
ASSERT(!result.is_error() || (options & WNOHANG) || result.error() != KSuccess);
return result;
}
pid_t Process::sys$waitid(Userspace<const Syscall::SC_waitid_params*> user_params)

View file

@ -96,10 +96,12 @@ ssize_t Process::do_write(FileDescription& description, const UserOrKernelBuffer
ASSERT(total_nwritten > 0);
return total_nwritten;
}
if (Thread::current()->block<Thread::WriteBlocker>(nullptr, description).was_interrupted()) {
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
if (Thread::current()->block<Thread::WriteBlocker>(nullptr, description, unblock_flags).was_interrupted()) {
if (total_nwritten == 0)
return -EINTR;
}
// TODO: handle exceptions in unblock_flags
}
auto nwritten_or_error = description.write(data.offset(total_nwritten), data_size - total_nwritten);
if (nwritten_or_error.is_error()) {