Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport patches to 0.38 #1220

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/backend/libc/event/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,19 @@ pub(crate) fn port_getn(
events: &mut Vec<Event>,
mut nget: u32,
) -> io::Result<()> {
// `port_getn` special-cases a max value of 0 to be a query that returns
// the number of events. We don't want to do the `set_len` in that case, so
// so bail out early if needed.
if events.capacity() == 0 {
return Ok(());
}

let timeout = timeout.map_or(null_mut(), as_mut_ptr);
unsafe {
ret(c::port_getn(
borrowed_fd(port),
events.as_mut_ptr().cast(),
events.len().try_into().unwrap(),
events.capacity().try_into().unwrap(),
&mut nget,
timeout,
))?;
Expand All @@ -333,6 +340,24 @@ pub(crate) fn port_getn(
Ok(())
}

#[cfg(solarish)]
pub(crate) fn port_getn_query(port: BorrowedFd<'_>) -> io::Result<u32> {
let mut nget: u32 = 0;

// Pass a `max` of 0 to query the number of available events.
unsafe {
ret(c::port_getn(
borrowed_fd(port),
null_mut(),
0,
&mut nget,
null_mut(),
))?;
}

Ok(nget)
}

#[cfg(solarish)]
pub(crate) fn port_send(
port: BorrowedFd<'_>,
Expand Down
22 changes: 22 additions & 0 deletions src/event/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ pub fn port_get(port: impl AsFd, timeout: Option<Duration>) -> io::Result<Event>
/// `port_getn(port, events, min_events, timeout)`—Gets multiple events from a
/// port.
///
/// This requests up to a max of `events.capacity()` events, and then resizes
/// `events` to the number of events retrieved. If `events.capacity()` is 0,
/// this does nothing and returns immediately.
///
/// To query the number of events without retrieving any, use
/// [`port_getn_query`].
///
/// # References
/// - [OpenSolaris]
/// - [illumos]
Expand Down Expand Up @@ -138,6 +145,21 @@ pub fn port_getn(
)
}

/// `port_getn(port, NULL, 0, NULL)`—Queries the number of events
/// available from a port.
///
/// To retrieve the events, use [`port_getn`].
///
/// # References
/// - [OpenSolaris]
/// - [illumos]
///
/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_getn/
/// [illumos]: https://illumos.org/man/3C/port_getn
pub fn port_getn_query(port: impl AsFd) -> io::Result<u32> {
syscalls::port_getn_query(port.as_fd())
}

/// `port_send(port, events, userdata)`—Sends an event to a port.
///
/// # References
Expand Down
44 changes: 44 additions & 0 deletions src/io_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,39 @@ pub struct io_uring_buf {
pub resv: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct buf_ring_tail_struct {
pub resv1: u64,
pub resv2: u32,
pub resv3: u16,
pub tail: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct buf_ring_bufs_struct {
pub bufs: sys::__IncompleteArrayField<io_uring_buf>,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct tail_or_bufs_struct {
pub tail: sys::__BindgenUnionField<buf_ring_tail_struct>,
pub bufs: sys::__BindgenUnionField<buf_ring_bufs_struct>,
pub union_field: [u64; 2],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct io_uring_buf_ring {
pub tail_or_bufs: tail_or_bufs_struct,
}

impl Default for ioprio_union {
#[inline]
fn default() -> Self {
Expand Down Expand Up @@ -1599,5 +1632,16 @@ mod tests {
check_struct!(io_uring_buf_reg, ring_addr, ring_entries, bgid, pad, resv);
check_struct!(io_uring_buf, addr, len, bid, resv);
check_struct!(io_uring_sync_cancel_reg, addr, fd, flags, timeout, pad);

check_renamed_type!(tail_or_bufs_struct, io_uring_buf_ring__bindgen_ty_1);
check_renamed_type!(
buf_ring_tail_struct,
io_uring_buf_ring__bindgen_ty_1__bindgen_ty_1
);
check_renamed_type!(
buf_ring_bufs_struct,
io_uring_buf_ring__bindgen_ty_1__bindgen_ty_2
);
check_struct_renamed_field!(io_uring_buf_ring, tail_or_bufs, __bindgen_anon_1);
}
}
1 change: 1 addition & 0 deletions tests/fs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod fcntl;
target_os = "emscripten",
target_os = "fuchsia",
target_os = "redox",
target_os = "solaris",
target_os = "wasi"
)))]
mod fcntl_lock;
Expand Down
73 changes: 70 additions & 3 deletions tests/io_uring/register.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::ptr;

use libc::c_void;
use rustix::fd::{AsFd, AsRawFd, BorrowedFd};
use rustix::io::Result;
use rustix::io::{Errno, Result};
use rustix::io_uring::{
io_uring_params, io_uring_register_with, io_uring_rsrc_update, io_uring_setup,
IoringFeatureFlags, IoringRegisterFlags, IoringRegisterOp,
io_uring_buf, io_uring_buf_reg, io_uring_buf_ring, io_uring_params, io_uring_register_with,
io_uring_rsrc_update, io_uring_setup, IoringFeatureFlags, IoringRegisterFlags,
IoringRegisterOp,
};
use rustix::mm::{MapFlags, ProtFlags};

fn do_register<FD>(
fd: FD,
Expand Down Expand Up @@ -87,6 +91,19 @@ where
Ok(())
}

fn register_buf_ring<FD>(fd: FD, reg: &io_uring_buf_reg) -> Result<()>
where
FD: AsFd,
{
do_register(
fd,
false,
IoringRegisterOp::RegisterPbufRing,
reg as *const io_uring_buf_reg as *const c_void,
1,
)
}

#[test]
fn test_io_uring_register_with() {
let mut params = io_uring_params::default();
Expand All @@ -104,3 +121,53 @@ fn test_io_uring_register_with() {
unregister_ring(ring_fd).unwrap();
register_result.unwrap();
}

#[test]
fn io_uring_buf_ring_can_be_registered() {
const ENTRIES: usize = 8;
const BGID: u16 = 42;

let mut params = io_uring_params::default();
let ring_fd = io_uring_setup(4, &mut params).unwrap();

// Test that the kernel version supports IORING_REGISTER_PBUF_RING. If it doesn't, the kernel
// will return EINVAL. Not setting a `ring_addr` on `io_uring_buf_reg` will return `EFAULT`.
if let Err(e) = register_buf_ring(ring_fd.as_fd(), &io_uring_buf_reg::default()) {
if e == Errno::INVAL {
// Skip the test, as the current kernel version doesn't support what we need to test.
return;
}
}

let buf_ring_size = ENTRIES * std::mem::size_of::<io_uring_buf>();

let br_ptr = unsafe {
rustix::mm::mmap_anonymous(
ptr::null_mut(),
buf_ring_size,
ProtFlags::READ | ProtFlags::WRITE,
MapFlags::PRIVATE,
)
}
.unwrap() as *mut io_uring_buf_ring;

let br = unsafe { br_ptr.as_mut() }.expect("A valid io_uring_buf_ring struct");

let reg = io_uring_buf_reg {
ring_addr: br_ptr as u64,
ring_entries: ENTRIES as u32,
bgid: BGID,
pad: 0,
resv: [0u64; 3],
};

assert_eq!(register_buf_ring(ring_fd, &reg), Ok(()));

let tail = unsafe { br.tail_or_bufs.tail.as_mut() };
tail.tail = 0;
let bufs = unsafe { br.tail_or_bufs.bufs.as_mut().bufs.as_mut_slice(ENTRIES) };

assert_eq!(bufs[0].bid, 0);
bufs[7].bid = 7;
assert_eq!(bufs[7].bid, 7);
}