From 4e54e0a54acedf69e0198acdb9b2bc0f4347488d Mon Sep 17 00:00:00 2001 From: James Braid Date: Wed, 18 Nov 2026 22:30:00 +0000 Subject: [PATCH] jitv2: stop retrying MADV_COLLAPSE once it fails for lack of memory The compile thread calls madvise(MADV_COLLAPSE) on each fully sealed 2 MiB of the code arena, to put sealed code on hugepages and cut iTLB misses. The arena is PROT_NONE, then RW per commit, then RX per sealed page, so fault-time hugepages cannot form across those mappings and this call is the only thing that can give the arena any. On a host with fragmented free memory every call fails, slowly. On a 224-CPU shared host (93 percent of compaction attempts failing host-wide), an iris-bench run --quick --cpu r10000 made 6 collapse calls under strace -T. They took 0.004 to 1.17 s each and all returned ENOMEM, so each stalled the compile thread and collapsed nothing. A profile of an O2 PROM boot window on that host showed 46 percent of samples in the kernel's compaction spinlock; I could not reproduce that figure on a quieter day, where compaction was about 0.6 percent of samples. After the first ENOMEM, EAGAIN or EBUSY, stop calling it for that arena. A host where collapse works keeps the hugepages; a fragmented one pays for at most one failed attempt. No speed claim beyond that. Fixes: 81cd8712d ("soa bitmap to aos flag for compile/valid/deny bits ...") --- src/cpu/jitv2/paged_memory.rs | 43 ++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/cpu/jitv2/paged_memory.rs b/src/cpu/jitv2/paged_memory.rs index 8ce0bec43..9e2ed0867 100644 --- a/src/cpu/jitv2/paged_memory.rs +++ b/src/cpu/jitv2/paged_memory.rs @@ -152,13 +152,16 @@ fn madvise_hugepage(_ptr: *mut u8, _len: usize) {} /// workloads, not "this code just got hot, please collapse it immediately." /// Best-effort: a kernel without `MADV_COLLAPSE` support (pre-6.1) returns /// `ENOSYS`/`EINVAL`, silently ignored — no version probe needed, the -/// syscall's own failure path is the probe. +/// syscall's own failure path is the probe. Returns the errno, 0 on success. #[cfg(target_os = "linux")] -fn madvise_collapse(ptr: *mut u8, len: usize) { - let _ = unsafe { libc::madvise(ptr as *mut libc::c_void, len, libc::MADV_COLLAPSE) }; +fn madvise_collapse(ptr: *mut u8, len: usize) -> i32 { + if unsafe { libc::madvise(ptr as *mut libc::c_void, len, libc::MADV_COLLAPSE) } == 0 { + return 0; + } + std::io::Error::last_os_error().raw_os_error().unwrap_or(libc::EINVAL) } #[cfg(not(target_os = "linux"))] -fn madvise_collapse(_ptr: *mut u8, _len: usize) {} +fn madvise_collapse(_ptr: *mut u8, _len: usize) -> i32 { 0 } /// Dev diagnostic (`j2 hugepages`): sum the `AnonHugePages:` field of every /// `/proc/self/smaps` VMA entry that falls (even partially) within @@ -476,6 +479,9 @@ pub struct SharedArena { /// Monotonic: `try_seal_ready` is the only writer, advancing it by whole /// hugepage-sized steps as `sealed_up_to` clears each boundary. collapsed_up_to: usize, + /// Set once `MADV_COLLAPSE` fails in a way that will keep failing; it is + /// never retried, because each failed attempt stalls the compile thread. + collapse_gave_up: bool, /// How many `JITModule`s are still using this arena. /// /// The compile pool puts N independent modules on ONE arena (see @@ -570,6 +576,7 @@ impl SharedArena { sealed_up_to: 0, seal_queue: VecDeque::new(), collapsed_up_to: 0, + collapse_gave_up: false, // The arena starts with no module attached; each provider handle // registers itself in `new_shared`/`new_with_size`. users: 0, @@ -893,11 +900,19 @@ impl SharedArena { /// code). #[cfg(target_os = "linux")] fn advance_hugepage_collapse_watermark(&mut self) { + self.advance_hugepage_collapse_watermark_with(madvise_collapse); + } + + #[cfg(target_os = "linux")] + fn advance_hugepage_collapse_watermark_with(&mut self, collapse: impl FnOnce(*mut u8, usize) -> i32) { let target = (self.sealed_up_to / HUGE_PAGE_SIZE) * HUGE_PAGE_SIZE; // round DOWN — only whole, fully-sealed hugepages - if target > self.collapsed_up_to { + if target > self.collapsed_up_to && !self.collapse_gave_up { let len = target - self.collapsed_up_to; let region_ptr = unsafe { self.ptr.add(self.collapsed_up_to) }; - madvise_collapse(region_ptr, len); + // No memory to collapse into, or the kernel is busy: stop trying. + if matches!(collapse(region_ptr, len), libc::ENOMEM | libc::EAGAIN | libc::EBUSY) { + self.collapse_gave_up = true; + } self.collapsed_up_to = target; } } @@ -1418,6 +1433,22 @@ mod tests { assert_eq!(arena.inner.lock().collapsed_up_to, 0, "a single small sealed range must not advance the watermark -- it's nowhere near a full hugepage"); } + #[test] + #[cfg(target_os = "linux")] + fn collapse_is_not_retried_after_enomem() { + let (arena, _state) = new_arena(HUGE_PAGE_SIZE * 3); + let mut inner = arena.inner.lock(); + let mut calls = 0; + for sealed in [HUGE_PAGE_SIZE, HUGE_PAGE_SIZE * 2] { + inner.sealed_up_to = sealed; + inner.advance_hugepage_collapse_watermark_with(|_, _| { + calls += 1; + libc::ENOMEM + }); + } + assert_eq!(calls, 1, "a collapse that failed with ENOMEM must not be attempted again"); + } + #[test] fn out_of_order_finalize_blocks_until_contiguous() { // Two allocations from two different handles, B finalized before A: