Skip to content

Commit d012bcf

Browse files
authored
Fixes needed by io_uring. (#873)
* Fixes needed by io_uring. - Have the `io_uring` module export more types used in opcode entries. - Define a conversion from `SocketAddr` to `SocketAddrAny`. - Add comments to the `fadvise` implementation. * Pin regex to 1.9 in Rust 1.63 builds. * Make more `Epoll` types available to io_uring too. This enables most of the epoll module without needing "alloc". It doesn't enable `epoll_wait`, because that needs `Vec`, but this does potentially open up a path to having a non-allocating opetion for waiting on an epoll.
1 parent 5e5c046 commit d012bcf

File tree

12 files changed

+86
-21
lines changed

12 files changed

+86
-21
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
if: matrix.rust == '1.63'
5353
run: |
5454
cargo update --package=dashmap --precise 5.4.0
55+
cargo update --package=regex --precise=1.9.0
5556
5657
- run: >
5758
rustup target add
@@ -481,6 +482,7 @@ jobs:
481482
if: matrix.rust == '1.63'
482483
run: |
483484
cargo update --package=dashmap --precise 5.4.0
485+
cargo update --package=regex --precise=1.9.0
484486
485487
- run: |
486488
cargo test --verbose --features=all-apis --release --workspace -- --nocapture

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ event = []
135135
fs = []
136136

137137
# Enable `rustix::io_uring::*` (on platforms that support it).
138-
io_uring = ["fs", "net", "linux-raw-sys/io_uring"]
138+
io_uring = ["event", "fs", "net", "linux-raw-sys/io_uring"]
139139

140140
# Enable `rustix::mount::*`.
141141
mount = []

src/backend/libc/event/epoll.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@
7474
//! ```
7575
7676
use crate::backend::c;
77-
use crate::backend::conv::{ret, ret_owned_fd, ret_u32};
77+
#[cfg(feature = "alloc")]
78+
use crate::backend::conv::ret_u32;
79+
use crate::backend::conv::{ret, ret_owned_fd};
7880
use crate::fd::{AsFd, AsRawFd, OwnedFd};
7981
use crate::io;
8082
use crate::utils::as_mut_ptr;
83+
#[cfg(feature = "alloc")]
8184
use alloc::vec::Vec;
8285
use bitflags::bitflags;
8386
use core::ffi::c_void;
@@ -257,6 +260,8 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
257260
///
258261
/// For each event of interest, an element is written to `events`. On
259262
/// success, this returns the number of written elements.
263+
#[cfg(feature = "alloc")]
264+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
260265
pub fn wait(epoll: impl AsFd, event_list: &mut EventVec, timeout: c::c_int) -> io::Result<()> {
261266
// SAFETY: We're calling `epoll_wait` via FFI and we know how it
262267
// behaves.
@@ -395,10 +400,12 @@ struct SixtyFourBitPointer {
395400
}
396401

397402
/// A vector of `Event`s, plus context for interpreting them.
403+
#[cfg(feature = "alloc")]
398404
pub struct EventVec {
399405
events: Vec<Event>,
400406
}
401407

408+
#[cfg(feature = "alloc")]
402409
impl EventVec {
403410
/// Constructs an `EventVec` from raw pointer, length, and capacity.
404411
///
@@ -473,6 +480,7 @@ impl EventVec {
473480
}
474481
}
475482

483+
#[cfg(feature = "alloc")]
476484
impl<'a> IntoIterator for &'a EventVec {
477485
type IntoIter = Iter<'a>;
478486
type Item = Event;

src/backend/libc/event/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub(crate) mod types;
55
#[cfg_attr(windows, path = "windows_syscalls.rs")]
66
pub(crate) mod syscalls;
77

8-
#[cfg(all(feature = "alloc", linux_kernel))]
8+
#[cfg(linux_kernel)]
99
pub mod epoll;

src/backend/linux_raw/conv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl<'a, Num: ArgNumber> From<crate::event::EventfdFlags> for ArgReg<'a, Num> {
581581
}
582582
}
583583

584-
#[cfg(all(feature = "alloc", feature = "event"))]
584+
#[cfg(feature = "event")]
585585
impl<'a, Num: ArgNumber> From<crate::event::epoll::CreateFlags> for ArgReg<'a, Num> {
586586
#[inline]
587587
fn from(flags: crate::event::epoll::CreateFlags) -> Self {

src/backend/linux_raw/event/epoll.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ use crate::backend::c;
7979
use crate::backend::event::syscalls;
8080
use crate::fd::{AsFd, AsRawFd, OwnedFd};
8181
use crate::io;
82+
#[cfg(feature = "alloc")]
8283
use alloc::vec::Vec;
8384
use bitflags::bitflags;
8485
use core::ffi::c_void;
@@ -242,6 +243,8 @@ pub fn delete(epoll: impl AsFd, source: impl AsFd) -> io::Result<()> {
242243
///
243244
/// For each event of interest, an element is written to `events`. On
244245
/// success, this returns the number of written elements.
246+
#[cfg(feature = "alloc")]
247+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
245248
#[inline]
246249
pub fn wait(epoll: impl AsFd, event_list: &mut EventVec, timeout: c::c_int) -> io::Result<()> {
247250
// SAFETY: We're calling `epoll_wait` via FFI and we know how it
@@ -371,10 +374,12 @@ struct SixtyFourBitPointer {
371374
}
372375

373376
/// A vector of `Event`s, plus context for interpreting them.
377+
#[cfg(feature = "alloc")]
374378
pub struct EventVec {
375379
events: Vec<Event>,
376380
}
377381

382+
#[cfg(feature = "alloc")]
378383
impl EventVec {
379384
/// Constructs an `EventVec` from raw pointer, length, and capacity.
380385
///
@@ -449,6 +454,7 @@ impl EventVec {
449454
}
450455
}
451456

457+
#[cfg(feature = "alloc")]
452458
impl<'a> IntoIterator for &'a EventVec {
453459
type IntoIter = Iter<'a>;
454460
type Item = Event;

src/backend/linux_raw/event/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(feature = "alloc")]
21
pub mod epoll;
32
pub(crate) mod poll_fd;
43
pub(crate) mod syscalls;

src/backend/linux_raw/event/syscalls.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
#![allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
77

88
use crate::backend::c;
9-
use crate::backend::conv::{c_int, c_uint, ret_owned_fd, ret_usize, slice_mut};
10-
use crate::event::{EventfdFlags, PollFd};
11-
use crate::fd::OwnedFd;
12-
use crate::io;
139
#[cfg(feature = "alloc")]
14-
use {
15-
crate::backend::conv::{by_ref, pass_usize, raw_fd, ret, zero},
16-
crate::event::epoll,
17-
crate::fd::BorrowedFd,
18-
linux_raw_sys::general::{EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD},
10+
use crate::backend::conv::pass_usize;
11+
use crate::backend::conv::{
12+
by_ref, c_int, c_uint, raw_fd, ret, ret_owned_fd, ret_usize, slice_mut, zero,
1913
};
14+
use crate::event::{epoll, EventfdFlags, PollFd};
15+
use crate::fd::{BorrowedFd, OwnedFd};
16+
use crate::io;
17+
use linux_raw_sys::general::{EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD};
2018
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
2119
use {
2220
crate::backend::conv::{opt_ref, size_of},
@@ -52,13 +50,11 @@ pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: c::c_int) -> io::Result<usiz
5250
}
5351
}
5452

55-
#[cfg(feature = "alloc")]
5653
#[inline]
5754
pub(crate) fn epoll_create(flags: epoll::CreateFlags) -> io::Result<OwnedFd> {
5855
unsafe { ret_owned_fd(syscall_readonly!(__NR_epoll_create1, flags)) }
5956
}
6057

61-
#[cfg(feature = "alloc")]
6258
#[inline]
6359
pub(crate) unsafe fn epoll_add(
6460
epfd: BorrowedFd<'_>,
@@ -74,7 +70,6 @@ pub(crate) unsafe fn epoll_add(
7470
))
7571
}
7672

77-
#[cfg(feature = "alloc")]
7873
#[inline]
7974
pub(crate) unsafe fn epoll_mod(
8075
epfd: BorrowedFd<'_>,
@@ -90,7 +85,6 @@ pub(crate) unsafe fn epoll_mod(
9085
))
9186
}
9287

93-
#[cfg(feature = "alloc")]
9488
#[inline]
9589
pub(crate) unsafe fn epoll_del(epfd: BorrowedFd<'_>, fd: c::c_int) -> io::Result<()> {
9690
ret(syscall_readonly!(

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ pub(crate) fn fadvise(fd: BorrowedFd<'_>, pos: u64, len: u64, advice: Advice) ->
378378
lo(len)
379379
))
380380
}
381+
381382
// On mips, the arguments are not reordered, and padding is inserted
382383
// instead to ensure alignment.
383384
#[cfg(any(target_arch = "mips", target_arch = "mips32r6"))]
@@ -393,6 +394,9 @@ pub(crate) fn fadvise(fd: BorrowedFd<'_>, pos: u64, len: u64, advice: Advice) ->
393394
advice
394395
))
395396
}
397+
398+
// For all other 32-bit architectures, use `fadvise64_64` so that we get a
399+
// 64-bit length.
396400
#[cfg(all(
397401
target_pointer_width = "32",
398402
not(any(
@@ -413,6 +417,8 @@ pub(crate) fn fadvise(fd: BorrowedFd<'_>, pos: u64, len: u64, advice: Advice) ->
413417
advice
414418
))
415419
}
420+
421+
// On 64-bit architectures, use `fadvise64` which is sufficient.
416422
#[cfg(target_pointer_width = "64")]
417423
unsafe {
418424
ret(syscall_readonly!(

src/event/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod poll;
1313
#[cfg(solarish)]
1414
pub mod port;
1515

16-
#[cfg(all(feature = "alloc", linux_kernel))]
16+
#[cfg(linux_kernel)]
1717
pub use crate::backend::event::epoll;
1818
#[cfg(any(
1919
linux_kernel,

src/io_uring.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,47 @@ use core::ptr::{null_mut, write_bytes};
3232
use linux_raw_sys::net;
3333

3434
// Export types used in io_uring APIs.
35-
pub use crate::fs::{Advice, AtFlags, OFlags, RenameFlags, ResolveFlags, Statx};
35+
pub use crate::event::epoll::{
36+
Event as EpollEvent, EventData as EpollEventData, EventFlags as EpollEventFlags,
37+
};
38+
pub use crate::fs::{Advice, AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, Statx, StatxFlags};
39+
pub use crate::io::ReadWriteFlags;
3640
pub use crate::net::{RecvFlags, SendFlags, SocketFlags};
3741
pub use crate::timespec::Timespec;
42+
pub use linux_raw_sys::general::sigset_t;
43+
44+
pub use net::{__kernel_sockaddr_storage as sockaddr_storage, msghdr, sockaddr, socklen_t};
45+
46+
// Declare the `c_char` type for use with entries that take pointers
47+
// to C strings. Define it as unsigned or signed according to the platform
48+
// so that we match what Rust's `CStr` uses.
49+
//
50+
// When we can update to linux-raw-sys 0.5, we can remove this, as its
51+
// `c_char` type will declare this.
52+
/// The C `char` type.
53+
#[cfg(any(
54+
target_arch = "aarch64",
55+
target_arch = "arm",
56+
target_arch = "msp430",
57+
target_arch = "powerpc",
58+
target_arch = "powerpc64",
59+
target_arch = "riscv32",
60+
target_arch = "riscv64",
61+
target_arch = "s390x",
62+
))]
63+
#[allow(non_camel_case_types)]
64+
pub type c_char = u8;
65+
/// The C `char` type.
66+
#[cfg(any(
67+
target_arch = "mips",
68+
target_arch = "mips64",
69+
target_arch = "sparc64",
70+
target_arch = "x86",
71+
target_arch = "x86_64",
72+
target_arch = "xtensa",
73+
))]
74+
#[allow(non_camel_case_types)]
75+
pub type c_char = i8;
3876

3977
mod sys {
4078
pub(super) use linux_raw_sys::io_uring::*;
@@ -1393,6 +1431,8 @@ impl Default for register_or_sqe_op_or_sqe_flags_union {
13931431
fn io_uring_layouts() {
13941432
use sys as c;
13951433

1434+
assert_eq_size!(io_uring_ptr, u64);
1435+
13961436
check_renamed_type!(off_or_addr2_union, io_uring_sqe__bindgen_ty_1);
13971437
check_renamed_type!(addr_or_splice_off_in_union, io_uring_sqe__bindgen_ty_2);
13981438
check_renamed_type!(addr3_or_cmd_union, io_uring_sqe__bindgen_ty_6);

src/net/socket_addr_any.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#[cfg(unix)]
1313
use crate::net::SocketAddrUnix;
14-
use crate::net::{AddressFamily, SocketAddrV4, SocketAddrV6};
14+
use crate::net::{AddressFamily, SocketAddr, SocketAddrV4, SocketAddrV6};
1515
use crate::{backend, io};
1616
#[cfg(feature = "std")]
1717
use core::fmt;
@@ -32,6 +32,16 @@ pub enum SocketAddrAny {
3232
Unix(SocketAddrUnix),
3333
}
3434

35+
impl From<SocketAddr> for SocketAddrAny {
36+
#[inline]
37+
fn from(from: SocketAddr) -> Self {
38+
match from {
39+
SocketAddr::V4(v4) => Self::V4(v4),
40+
SocketAddr::V6(v6) => Self::V6(v6),
41+
}
42+
}
43+
}
44+
3545
impl From<SocketAddrV4> for SocketAddrAny {
3646
#[inline]
3747
fn from(from: SocketAddrV4) -> Self {

0 commit comments

Comments
 (0)