Skip to content
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
80 changes: 55 additions & 25 deletions rs/moq-relay/tests/integration/runtime_uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,45 +1287,25 @@ async fn uring_sessions_log_their_close_under_a_connection_span() {

/// A ring refused at worker startup reaches a `Ready` waiter with the actionable error.
#[tokio::test]
async fn uring_startup_reports_the_memlock_limit() {
// Resource limits belong to the process, so isolate even under libtest.
if !test_env::Env::default().set("MOQ_TEST_MEMLOCK", "0").isolate() {
async fn uring_startup_reports_a_refused_ring() {
// The filter binds the whole process, so isolate even under libtest.
if !test_env::Env::default().set("MOQ_TEST_REFUSE_RING", "1").isolate() {
return;
}
let _ = rind_rustls::default_provider().unwrap().install_default();
if !supported() {
return;
}
// CAP_IPC_LOCK, which root holds, exempts io_uring from the limit.
let status = std::fs::read_to_string("/proc/self/status").expect("process status");
let effective = status
.lines()
.find_map(|line| line.strip_prefix("CapEff:"))
.expect("effective capabilities");
let effective = u64::from_str_radix(effective.trim(), 16).expect("capability mask");
const CAP_IPC_LOCK: u32 = 14;
if effective & (1 << CAP_IPC_LOCK) != 0 {
eprintln!("skipping the memlock test: CAP_IPC_LOCK lifts the limit");
return;
}
let dir = tempfile::tempdir().expect("tempdir");
let (cert, key) = certificate(dir.path());
let relay = Relay::load(uring_config(&cert, &key))
.await
.expect("load relay")
.with_signals(false);
let mut limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
// SAFETY: valid out-pointer in an isolated test process.
assert_eq!(unsafe { libc::getrlimit(libc::RLIMIT_MEMLOCK, &mut limit) }, 0);
limit.rlim_cur = 0;
// SAFETY: valid limit in an isolated test process.
assert_eq!(unsafe { libc::setrlimit(libc::RLIMIT_MEMLOCK, &limit) }, 0);
refuse_rings();
let ready = relay.ready();
let running = tokio::spawn(relay.run());
let error = ready.wait().await.expect_err("a worker needs locked memory");
let error = ready.wait().await.expect_err("a worker needs a ring");
let message = format!("{error:#}");
for expected in [
"io_uring worker 0 failed to start",
Expand All @@ -1340,3 +1320,53 @@ async fn uring_startup_reports_the_memlock_limit() {
.expect("join relay")
.expect_err("run fails with the waiter");
}

/// Fails every later `io_uring_setup` in this process with `ENOMEM`, the way a
/// spent memory cgroup or, on Linux 6.14 up to the 7.3 fix, a spent
/// `RLIMIT_MEMLOCK` refuses a ring. Which one a kernel charges varies, so the
/// test injects the refusal instead of exhausting either.
fn refuse_rings() {
use libc::{BPF_ABS, BPF_JEQ, BPF_JMP, BPF_K, BPF_LD, BPF_RET, BPF_W, sock_filter};
let op = |code: u32, k: u32| sock_filter {
code: code as u16,
jt: 0,
jf: 0,
k,
};
// Matching the syscall number alone suffices: this test only issues native
// syscalls, and the filter injects a fault rather than enforcing a policy.
let filter = [
// seccomp_data.nr
op(BPF_LD | BPF_W | BPF_ABS, 0),
sock_filter {
code: (BPF_JMP | BPF_JEQ | BPF_K) as u16,
jt: 0,
jf: 1,
k: libc::SYS_io_uring_setup as u32,
},
op(BPF_RET | BPF_K, libc::SECCOMP_RET_ERRNO | libc::ENOMEM as u32),
op(BPF_RET | BPF_K, libc::SECCOMP_RET_ALLOW),
];
let program = libc::sock_fprog {
len: filter.len() as u16,
filter: filter.as_ptr().cast_mut(),
};
// SAFETY: plain flag; without it an unprivileged process may not install a filter.
assert_eq!(unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) }, 0);
// SAFETY: `program` points at `filter`, both live across the call, which copies them.
// TSYNC applies it to the threads already running too, not only the ones spawned later.
let installed = unsafe {
libc::syscall(
libc::SYS_seccomp,
libc::SECCOMP_SET_MODE_FILTER,
libc::SECCOMP_FILTER_FLAG_TSYNC,
&program,
)
};
assert_eq!(
installed,
0,
"install the seccomp filter: {}",
std::io::Error::last_os_error()
);
}
7 changes: 4 additions & 3 deletions rs/moq-uring/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ impl Error {
/// A failed ring setup or registration, naming the locked-memory limit when
/// that is what ran out.
///
/// The kernel charges every ring and provided-buffer ring to the user's
/// `RLIMIT_MEMLOCK`, shared with every other io_uring that user runs, so a
/// bare `ENOMEM` points at the wrong culprit.
/// Linux 6.14 up to the 7.3 fix (backported to stable) charges every ring
/// and provided-buffer ring to the user's `RLIMIT_MEMLOCK`, shared with
/// every other io_uring that user runs, so a bare `ENOMEM` points at the
/// wrong culprit. Other kernels charge them to the memory cgroup.
pub(crate) fn ring(err: std::io::Error) -> Self {
if err.raw_os_error() != Some(libc::ENOMEM) {
return Self::Io(err);
Expand Down
6 changes: 3 additions & 3 deletions rs/moq-uring/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const SQ_ENTRIES: u32 = 128;
/// allocation-per-CQE slow path and it ends any armed multishot receive, so
/// the CQ is sized to keep it out of steady state.
///
/// No larger: the ring is charged to `RLIMIT_MEMLOCK` at 16 bytes per entry,
/// most of each worker's footprint, and that budget is shared by every
/// io_uring the user runs.
/// No larger: the ring costs 16 bytes per entry, most of each worker's
/// footprint, and Linux 6.14 up to the 7.3 fix charges it to `RLIMIT_MEMLOCK`,
/// a budget shared by every io_uring the user runs.
const PRODUCTION_CQ_ENTRIES: u32 = 2048;
#[cfg(not(test))]
const CQ_ENTRIES: u32 = PRODUCTION_CQ_ENTRIES;
Expand Down