io: use io_uring for all io
There should no longer be any - read - write - connect - sendmsg - recvmsg - accept calls in the codebase. Previously we were using a mix of io_uring and these calls which had some negative effects: Since we were using the old system calls, we had to set the file descriptors to non-blocking. But our io_uring code did not handle EAGAIN. This lead to programs sometimes being killed when the wayland IO was actually blocking. Now all file descriptors are set to blocking, but io_uring makes it non-blocking from our perspective. The one exception are evdev files because they are read via libinput and libinput uses the old system calls.
This commit is contained in:
parent
2db0ee8995
commit
9812a02f87
55 changed files with 900 additions and 672 deletions
67
src/io_uring/ops/accept.rs
Normal file
67
src/io_uring/ops/accept.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
use {
|
||||
crate::io_uring::{
|
||||
pending_result::PendingResult,
|
||||
sys::{io_uring_sqe, IORING_OP_ACCEPT},
|
||||
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
|
||||
},
|
||||
std::rc::Rc,
|
||||
uapi::{c, OwnedFd},
|
||||
};
|
||||
|
||||
impl IoUring {
|
||||
pub async fn accept(
|
||||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
flags: c::c_int,
|
||||
) -> Result<Rc<OwnedFd>, IoUringError> {
|
||||
self.ring.check_destroyed()?;
|
||||
let id = self.ring.id();
|
||||
let pr = self.ring.pending_results.acquire();
|
||||
{
|
||||
let mut pw = self.ring.cached_accepts.pop().unwrap_or_default();
|
||||
pw.id = id.id;
|
||||
pw.fd = fd.raw() as _;
|
||||
pw.flags = flags as _;
|
||||
pw.data = Some(Data {
|
||||
pr: pr.clone(),
|
||||
_fd: fd.clone(),
|
||||
});
|
||||
self.ring.schedule(pw);
|
||||
}
|
||||
Ok(pr.await.map(OwnedFd::new).map(Rc::new)).merge()
|
||||
}
|
||||
}
|
||||
|
||||
struct Data {
|
||||
pr: PendingResult,
|
||||
_fd: Rc<OwnedFd>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AcceptTask {
|
||||
id: u64,
|
||||
fd: i32,
|
||||
flags: u32,
|
||||
data: Option<Data>,
|
||||
}
|
||||
|
||||
unsafe impl Task for AcceptTask {
|
||||
fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn complete(mut self: Box<Self>, ring: &IoUringData, res: i32) {
|
||||
if let Some(data) = self.data.take() {
|
||||
data.pr.complete(res);
|
||||
}
|
||||
ring.cached_accepts.push(self);
|
||||
}
|
||||
|
||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||
sqe.opcode = IORING_OP_ACCEPT;
|
||||
sqe.fd = self.fd;
|
||||
sqe.u2.addr = 0;
|
||||
sqe.u1.addr2 = 0;
|
||||
sqe.u3.accept_flags = self.flags;
|
||||
}
|
||||
}
|
||||
77
src/io_uring/ops/connect.rs
Normal file
77
src/io_uring/ops/connect.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
use {
|
||||
crate::io_uring::{
|
||||
pending_result::PendingResult,
|
||||
sys::{io_uring_sqe, IORING_OP_CONNECT},
|
||||
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
|
||||
},
|
||||
std::{mem, ptr, rc::Rc},
|
||||
uapi::{c, OwnedFd, SockAddr},
|
||||
};
|
||||
|
||||
impl IoUring {
|
||||
pub async fn connect<T: SockAddr>(&self, fd: &Rc<OwnedFd>, t: &T) -> Result<(), IoUringError> {
|
||||
self.ring.check_destroyed()?;
|
||||
let id = self.ring.id();
|
||||
let pr = self.ring.pending_results.acquire();
|
||||
{
|
||||
let mut pw = self.ring.cached_connects.pop().unwrap_or_default();
|
||||
pw.id = id.id;
|
||||
pw.fd = fd.raw() as _;
|
||||
unsafe {
|
||||
ptr::copy_nonoverlapping(t, &mut pw.sockaddr as *mut _ as *mut _, 1);
|
||||
}
|
||||
pw.addrlen = mem::size_of::<T>() as _;
|
||||
pw.data = Some(Data {
|
||||
pr: pr.clone(),
|
||||
_fd: fd.clone(),
|
||||
});
|
||||
self.ring.schedule(pw);
|
||||
}
|
||||
Ok(pr.await.map(drop)).merge()
|
||||
}
|
||||
}
|
||||
|
||||
struct Data {
|
||||
pr: PendingResult,
|
||||
_fd: Rc<OwnedFd>,
|
||||
}
|
||||
|
||||
pub struct ConnectTask {
|
||||
id: u64,
|
||||
fd: i32,
|
||||
sockaddr: c::sockaddr_storage,
|
||||
addrlen: u64,
|
||||
data: Option<Data>,
|
||||
}
|
||||
|
||||
impl Default for ConnectTask {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: 0,
|
||||
fd: 0,
|
||||
sockaddr: uapi::pod_zeroed(),
|
||||
addrlen: 0,
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Task for ConnectTask {
|
||||
fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn complete(mut self: Box<Self>, ring: &IoUringData, res: i32) {
|
||||
if let Some(data) = self.data.take() {
|
||||
data.pr.complete(res);
|
||||
}
|
||||
ring.cached_connects.push(self);
|
||||
}
|
||||
|
||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||
sqe.opcode = IORING_OP_CONNECT;
|
||||
sqe.fd = self.fd;
|
||||
sqe.u2.addr = &self.sockaddr as *const _ as _;
|
||||
sqe.u1.off = self.addrlen;
|
||||
}
|
||||
}
|
||||
|
|
@ -32,6 +32,7 @@ impl IoUring {
|
|||
self.poll(fd, c::POLLIN).await.merge()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn writable(&self, fd: &Rc<OwnedFd>) -> Result<c::c_short, IoUringError> {
|
||||
self.poll(fd, c::POLLOUT).await.merge()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use {
|
||||
crate::{
|
||||
io_uring::{
|
||||
ops::TaskResult,
|
||||
pending_result::PendingResult,
|
||||
sys::{io_uring_sqe, IORING_OP_WRITE},
|
||||
IoUring, IoUringData, Task,
|
||||
sys::{io_uring_sqe, IORING_OP_READ, IORING_OP_WRITE},
|
||||
IoUring, IoUringData, IoUringError, Task, TaskResultExt,
|
||||
},
|
||||
time::Time,
|
||||
utils::buf::Buf,
|
||||
|
|
@ -14,23 +13,38 @@ use {
|
|||
};
|
||||
|
||||
impl IoUring {
|
||||
pub async fn read(&self, fd: &Rc<OwnedFd>, buf: Buf) -> Result<usize, IoUringError> {
|
||||
self.perform(fd, buf, None, IORING_OP_READ).await
|
||||
}
|
||||
|
||||
pub async fn write(
|
||||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
buf: Buf,
|
||||
timeout: Option<Time>,
|
||||
) -> TaskResult<usize> {
|
||||
) -> Result<usize, IoUringError> {
|
||||
self.perform(fd, buf, timeout, IORING_OP_WRITE).await
|
||||
}
|
||||
|
||||
async fn perform(
|
||||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
buf: Buf,
|
||||
timeout: Option<Time>,
|
||||
opcode: u8,
|
||||
) -> Result<usize, IoUringError> {
|
||||
self.ring.check_destroyed()?;
|
||||
let id = self.ring.id();
|
||||
let pr = self.ring.pending_results.acquire();
|
||||
{
|
||||
let mut pw = self.ring.cached_writes.pop().unwrap_or_default();
|
||||
let mut pw = self.ring.cached_read_writes.pop().unwrap_or_default();
|
||||
pw.opcode = opcode;
|
||||
pw.id = id.id;
|
||||
pw.has_timeout = timeout.is_some();
|
||||
pw.fd = fd.raw();
|
||||
pw.buf = buf.as_ptr() as _;
|
||||
pw.len = buf.len();
|
||||
pw.data = Some(WriteTaskData {
|
||||
pw.data = Some(ReadWriteTaskData {
|
||||
_fd: fd.clone(),
|
||||
_buf: buf,
|
||||
res: pr.clone(),
|
||||
|
|
@ -40,27 +54,28 @@ impl IoUring {
|
|||
self.schedule_timeout(time);
|
||||
}
|
||||
}
|
||||
Ok(pr.await.map(|v| v as usize))
|
||||
Ok(pr.await.map(|v| v as usize)).merge()
|
||||
}
|
||||
}
|
||||
|
||||
struct WriteTaskData {
|
||||
struct ReadWriteTaskData {
|
||||
_fd: Rc<OwnedFd>,
|
||||
_buf: Buf,
|
||||
res: PendingResult,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WriteTask {
|
||||
pub struct ReadWriteTask {
|
||||
id: u64,
|
||||
has_timeout: bool,
|
||||
fd: c::c_int,
|
||||
buf: usize,
|
||||
len: usize,
|
||||
data: Option<WriteTaskData>,
|
||||
data: Option<ReadWriteTaskData>,
|
||||
opcode: u8,
|
||||
}
|
||||
|
||||
unsafe impl Task for WriteTask {
|
||||
unsafe impl Task for ReadWriteTask {
|
||||
fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
|
@ -69,11 +84,11 @@ unsafe impl Task for WriteTask {
|
|||
if let Some(data) = self.data.take() {
|
||||
data.res.complete(res);
|
||||
}
|
||||
ring.cached_writes.push(self);
|
||||
ring.cached_read_writes.push(self);
|
||||
}
|
||||
|
||||
fn encode(&self, sqe: &mut io_uring_sqe) {
|
||||
sqe.opcode = IORING_OP_WRITE;
|
||||
sqe.opcode = self.opcode;
|
||||
sqe.fd = self.fd as _;
|
||||
sqe.u1.off = !0;
|
||||
sqe.u2.addr = self.buf as _;
|
||||
|
|
@ -16,7 +16,7 @@ impl IoUring {
|
|||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
bufs: &mut [Buf],
|
||||
fds: &mut VecDeque<OwnedFd>,
|
||||
fds: &mut VecDeque<Rc<OwnedFd>>,
|
||||
) -> Result<usize, IoUringError> {
|
||||
self.ring.check_destroyed()?;
|
||||
let id = self.ring.id();
|
||||
|
|
@ -64,7 +64,7 @@ impl IoUring {
|
|||
}
|
||||
};
|
||||
if (hdr.cmsg_level, hdr.cmsg_type) == (c::SOL_SOCKET, c::SCM_RIGHTS) {
|
||||
fds.extend(uapi::pod_iter(data).unwrap());
|
||||
fds.extend(uapi::pod_iter(data).unwrap().map(Rc::new));
|
||||
}
|
||||
}
|
||||
return_cmsg!();
|
||||
|
|
|
|||
|
|
@ -17,12 +17,22 @@ use {
|
|||
};
|
||||
|
||||
impl IoUring {
|
||||
pub async fn sendmsg(
|
||||
pub async fn sendmsg_one(
|
||||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
buf: Buf,
|
||||
fds: Vec<Rc<OwnedFd>>,
|
||||
timeout: Option<Time>,
|
||||
) -> Result<usize, IoUringError> {
|
||||
self.sendmsg(fd, &mut [buf], fds, timeout).await
|
||||
}
|
||||
|
||||
pub async fn sendmsg(
|
||||
&self,
|
||||
fd: &Rc<OwnedFd>,
|
||||
bufs: &mut [Buf],
|
||||
fds: Vec<Rc<OwnedFd>>,
|
||||
timeout: Option<Time>,
|
||||
) -> Result<usize, IoUringError> {
|
||||
self.ring.check_destroyed()?;
|
||||
let id = self.ring.id();
|
||||
|
|
@ -52,13 +62,17 @@ impl IoUring {
|
|||
}
|
||||
st.id = id.id;
|
||||
st.fd = fd.raw();
|
||||
st.iovec.iov_base = buf.as_ptr() as _;
|
||||
st.iovec.iov_len = buf.len() as _;
|
||||
st.msghdr.msg_iov = &st.iovec as *const _ as _;
|
||||
st.msghdr.msg_iovlen = 1;
|
||||
st.bufs.clear();
|
||||
st.bufs.extend(bufs.iter_mut().map(|b| b.clone()));
|
||||
st.iovecs.clear();
|
||||
st.iovecs.extend(bufs.iter().map(|b| c::iovec {
|
||||
iov_base: b.as_ptr() as _,
|
||||
iov_len: b.len(),
|
||||
}));
|
||||
st.msghdr.msg_iov = st.iovecs.as_ptr() as _;
|
||||
st.msghdr.msg_iovlen = st.iovecs.len();
|
||||
st.data = Some(SendmsgTaskData {
|
||||
_fd: fd.clone(),
|
||||
_buf: buf,
|
||||
res: pr.clone(),
|
||||
});
|
||||
st.has_timeout = timeout.is_some();
|
||||
|
|
@ -73,14 +87,14 @@ impl IoUring {
|
|||
|
||||
struct SendmsgTaskData {
|
||||
_fd: Rc<OwnedFd>,
|
||||
_buf: Buf,
|
||||
res: PendingResult,
|
||||
}
|
||||
|
||||
pub struct SendmsgTask {
|
||||
id: u64,
|
||||
iovec: c::iovec,
|
||||
iovecs: Vec<c::iovec>,
|
||||
msghdr: c::msghdr,
|
||||
bufs: Vec<Buf>,
|
||||
fd: i32,
|
||||
has_timeout: bool,
|
||||
fds: Vec<Rc<OwnedFd>>,
|
||||
|
|
@ -93,8 +107,9 @@ impl Default for SendmsgTask {
|
|||
unsafe {
|
||||
SendmsgTask {
|
||||
id: 0,
|
||||
iovec: MaybeUninit::zeroed().assume_init(),
|
||||
iovecs: vec![],
|
||||
msghdr: MaybeUninit::zeroed().assume_init(),
|
||||
bufs: vec![],
|
||||
fd: 0,
|
||||
has_timeout: false,
|
||||
fds: vec![],
|
||||
|
|
@ -112,6 +127,7 @@ unsafe impl Task for SendmsgTask {
|
|||
|
||||
fn complete(mut self: Box<Self>, ring: &IoUringData, res: i32) {
|
||||
self.fds.clear();
|
||||
self.bufs.clear();
|
||||
if let Some(data) = self.data.take() {
|
||||
data.res.complete(res);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue