From 3e6fa3c6d0c8b964234d47eb00900a0b1009ef91 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 05:29:53 -0800 Subject: [PATCH 1/2] fix(relay): test a refused io_uring ring on every kernel, not only memlock-charging ones Linux 6.14 began charging io_uring SQ/CQ and provided-buffer rings to RLIMIT_MEMLOCK, a regression that 7.3 reverted (and stable backported), so the memlock test started succeeding on fixed kernels. Inject ENOMEM into io_uring_setup with a seccomp filter instead, and stop claiming every kernel charges rings to memlock. Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/config.md | 5 +- packaging/moq-relay/moq-relay.service | 4 +- .../tests/integration/runtime_uring.rs | 80 +++++++++++++------ rs/moq-uring/README.md | 4 +- rs/moq-uring/src/error.rs | 12 +-- rs/moq-uring/src/worker.rs | 6 +- 6 files changed, 73 insertions(+), 38 deletions(-) diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index 4cef5b6ce84..4a20996c421 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -229,8 +229,9 @@ host has, would crowd onto them. Turn it on for a host the relay has to itself. `io_uring` additionally needs Linux 6.12+ and the `io-uring` cargo feature; it shares the same reloadable TLS identity and serves every configured version like the shared runtime. A setting the build or host cannot deliver refuses to -start rather than being ignored. If io\_uring workers fail to start naming -`RLIMIT_MEMLOCK`, raise it (`LimitMEMLOCK=` under systemd). +start rather than being ignored. Linux 6.14 up to the 7.3 fix (backported to +stable kernels) charges io\_uring rings to `RLIMIT_MEMLOCK`: if io\_uring workers +fail to start naming it, raise it (`LimitMEMLOCK=` under systemd). ## \[web] diff --git a/packaging/moq-relay/moq-relay.service b/packaging/moq-relay/moq-relay.service index 8af96bdf591..18fa557c751 100644 --- a/packaging/moq-relay/moq-relay.service +++ b/packaging/moq-relay/moq-relay.service @@ -17,8 +17,8 @@ RestartSec=5s RestartSteps=5 RestartMaxDelaySec=1min LimitNOFILE=1048576 -# io_uring workers charge their rings to RLIMIT_MEMLOCK (~56 KiB each), and hosts still on the old -# 64 KiB default fit one worker at most. +# Linux 6.14 up to the 7.3 fix charges io_uring rings to RLIMIT_MEMLOCK (~56 KiB per worker), and +# hosts still on the old 64 KiB default fit one worker at most. LimitMEMLOCK=infinity # Sandboxing. DynamicUser allocates a transient UID, StateDirectory creates diff --git a/rs/moq-relay/tests/integration/runtime_uring.rs b/rs/moq-relay/tests/integration/runtime_uring.rs index 90ce1571cb3..c04794743a9 100644 --- a/rs/moq-relay/tests/integration/runtime_uring.rs +++ b/rs/moq-relay/tests/integration/runtime_uring.rs @@ -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", @@ -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() + ); +} diff --git a/rs/moq-uring/README.md b/rs/moq-uring/README.md index 6aa7c96550f..16220125551 100644 --- a/rs/moq-uring/README.md +++ b/rs/moq-uring/README.md @@ -152,7 +152,9 @@ just rs test -p moq-uring --release -E 'test(benchmark_socket_queues)' --run-ign ## Parallel test runs -Locked memory is charged per user across processes, not per nextest run. +Linux 6.14 up to the 7.3 fix (backported to stable kernels) charges rings to +locked memory per user across processes, not per nextest run; other kernels +charge them to the memory cgroup. Unit tests use 128 SQ entries and 512 CQ entries instead of the production 256/2048 geometry. Integration tests still exercise production rings. The nextest group allows four ring tests per invocation. Each socket-queue diff --git a/rs/moq-uring/src/error.rs b/rs/moq-uring/src/error.rs index b4a43aa6631..abbae898063 100644 --- a/rs/moq-uring/src/error.rs +++ b/rs/moq-uring/src/error.rs @@ -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); @@ -36,8 +37,9 @@ impl Error { Self::Io(std::io::Error::new( err.kind(), format!( - "{err}: io_uring memory counts against RLIMIT_MEMLOCK ({limit}), shared by every process of this user; \ - raise it with `ulimit -l` or systemd's `LimitMEMLOCK=`" + "{err}: Linux 6.14 until the 7.3 fix charges io_uring rings to RLIMIT_MEMLOCK ({limit}), shared by every \ + process of this user, so raise it with `ulimit -l` or systemd's `LimitMEMLOCK=`; other kernels charge \ + them to the memory cgroup" ), )) } diff --git a/rs/moq-uring/src/worker.rs b/rs/moq-uring/src/worker.rs index cd9ee6355c1..4cf167c37b1 100644 --- a/rs/moq-uring/src/worker.rs +++ b/rs/moq-uring/src/worker.rs @@ -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; From e9d1825db54f4d9dabac975296cdc872e46f4623 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 05:32:10 -0800 Subject: [PATCH 2/2] fix(relay): keep the error message and docs for a follow-up PR Co-Authored-By: Claude Opus 5.5 --- doc/bin/relay/config.md | 5 ++--- packaging/moq-relay/moq-relay.service | 4 ++-- rs/moq-uring/README.md | 4 +--- rs/moq-uring/src/error.rs | 5 ++--- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index 4a20996c421..4cef5b6ce84 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -229,9 +229,8 @@ host has, would crowd onto them. Turn it on for a host the relay has to itself. `io_uring` additionally needs Linux 6.12+ and the `io-uring` cargo feature; it shares the same reloadable TLS identity and serves every configured version like the shared runtime. A setting the build or host cannot deliver refuses to -start rather than being ignored. Linux 6.14 up to the 7.3 fix (backported to -stable kernels) charges io\_uring rings to `RLIMIT_MEMLOCK`: if io\_uring workers -fail to start naming it, raise it (`LimitMEMLOCK=` under systemd). +start rather than being ignored. If io\_uring workers fail to start naming +`RLIMIT_MEMLOCK`, raise it (`LimitMEMLOCK=` under systemd). ## \[web] diff --git a/packaging/moq-relay/moq-relay.service b/packaging/moq-relay/moq-relay.service index 18fa557c751..8af96bdf591 100644 --- a/packaging/moq-relay/moq-relay.service +++ b/packaging/moq-relay/moq-relay.service @@ -17,8 +17,8 @@ RestartSec=5s RestartSteps=5 RestartMaxDelaySec=1min LimitNOFILE=1048576 -# Linux 6.14 up to the 7.3 fix charges io_uring rings to RLIMIT_MEMLOCK (~56 KiB per worker), and -# hosts still on the old 64 KiB default fit one worker at most. +# io_uring workers charge their rings to RLIMIT_MEMLOCK (~56 KiB each), and hosts still on the old +# 64 KiB default fit one worker at most. LimitMEMLOCK=infinity # Sandboxing. DynamicUser allocates a transient UID, StateDirectory creates diff --git a/rs/moq-uring/README.md b/rs/moq-uring/README.md index 16220125551..6aa7c96550f 100644 --- a/rs/moq-uring/README.md +++ b/rs/moq-uring/README.md @@ -152,9 +152,7 @@ just rs test -p moq-uring --release -E 'test(benchmark_socket_queues)' --run-ign ## Parallel test runs -Linux 6.14 up to the 7.3 fix (backported to stable kernels) charges rings to -locked memory per user across processes, not per nextest run; other kernels -charge them to the memory cgroup. +Locked memory is charged per user across processes, not per nextest run. Unit tests use 128 SQ entries and 512 CQ entries instead of the production 256/2048 geometry. Integration tests still exercise production rings. The nextest group allows four ring tests per invocation. Each socket-queue diff --git a/rs/moq-uring/src/error.rs b/rs/moq-uring/src/error.rs index abbae898063..5c48971c6c7 100644 --- a/rs/moq-uring/src/error.rs +++ b/rs/moq-uring/src/error.rs @@ -37,9 +37,8 @@ impl Error { Self::Io(std::io::Error::new( err.kind(), format!( - "{err}: Linux 6.14 until the 7.3 fix charges io_uring rings to RLIMIT_MEMLOCK ({limit}), shared by every \ - process of this user, so raise it with `ulimit -l` or systemd's `LimitMEMLOCK=`; other kernels charge \ - them to the memory cgroup" + "{err}: io_uring memory counts against RLIMIT_MEMLOCK ({limit}), shared by every process of this user; \ + raise it with `ulimit -l` or systemd's `LimitMEMLOCK=`" ), )) }