fix edge case in swapper

A particular edge case was finally reproduced & fixed. The edge
case occurs when a swap is triggered while handling an interrupt,
and an OOM happens during the context switch to/from the interrupt.

The background on this is that the hardware drivers for the swapper,
as well as much of the logical heavy lifting on e.g. what pages to
swap out, live in a userspace process known as xous-swapper.

The userspace process has to run free of interrupts - when you're
OOM'd or trying to swap, interrupts should not happen. The way to
do this in Xous is to run that code inside an interurpt context,
as interrupts are not interruptable.

Thus, the xous swapper userspace consists of two halves: a regular
userspace stub that provides some amount of control/debug over
the swapper, and a handler that gets run in a "pseudo-interrupt"
context.

Like all other interrupts, the handler "borrows" the stack of the currently
running thread - interrupt contexts don't have an explicit stack, they
just grow on top of the current stack of the current thread in the
current process at the time of the interrupt.

This is broken because when you have a swap event inside an interrupt
context, there are conditions where you might not have a stack. What
was in fact happening is that the stack pointer was being orphaned
as being the "exception stack" - which is a temporary location,
only accessible by the kernel, used for storing all the registers
going into and out of exceptions. Since no stack pointer is set up,
the moment the userspace starts to run and spill its registers to
stack, the system trips a "nested swap" event, which leads to a panic
because this is not allowed.

The fix to this is to devote two hard-wired pages, mapped in the
loader, to be the swapper's private stack area. Instead of behaving
like an interrupt and borrowing the caller's stack, the swapper instead
goes back to its hard-wired stack to do all the processing.

This should also improve the performance of swap handling in some
cases because we can now guarantee the stack is always in-memory
and doesn't need to be swapped in during swap processing.

One complication is that the "reserved memory" that the swapper
puts aside in case it *does* need to allocate extra pages during
handling can't be freed at the top of the OOM routine. Thus if
the swapper ends up using new pages, we should expect yet another
crash.

However, in practice, this doesn't seem to happen, because the
cases in which we hit needing a new page are typically related
to cases early on in boot where superpages/new page table entries
are being allocated, or if we're growing the heap that passes
data back and forth between swap and userspace. Because this call
triggers in such a specific condition, I hypothesize that actually
in practice it should generally be able to run without allocations.

One case where maybe it could hit an allocation is if the swapper
hit, for the first time, in an interrupt handler that was swapped
out, and never hit before, and the interrupt handler needed a
superpage to be allocated, thus triggering an allocation request
for a new PTE or two. I think this is probably pretty unlikely, but
if we do hit it, the fix would be to, for the purposes of targets
with "swap" enabled, to mark unmap_memory and map_memory to be
legal syscalls in interrupt contexts. These are normally illegal
because interrupts should always return - and these calls could
potentially fail or block. However, I think in this case it's
a trade-off between "we should be failing if we run out of memory"
and that failure being a potentially incorrect behavior that is guarded
against by a runtime check to discourage such calls.
This commit is contained in:
bunnie 2026-01-08 02:08:03 +08:00
parent f879068bbb
commit e27b30d818
6 changed files with 48 additions and 29 deletions

View file

@ -729,7 +729,13 @@ impl SystemServices {
let sp = if pid.get() == 1 {
EXCEPTION_STACK_TOP
} else {
arch_process.current_thread().stack_pointer()
match cb_type {
// for swap, we have to provide our own stack, because we can have a fault
// *inside* an IRQ handler, which leaves us with no stack (as the IRQ handler
// is already planning on vampiring off an existing thread's stack).
CallbackType::Swap(_) | CallbackType::SwapInIrq(_) => SWAP_STACK_TOP_VADDR,
_ => arch_process.current_thread().stack_pointer(),
}
};
// Activate the current context

View file

@ -177,6 +177,8 @@ pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) {
}
#[cfg(feature = "swap")]
{
use xous::arch::SWAP_STACK_TOP_VADDR;
// map the swap page table into PID space 2
let tt_address = cfg.processes[SWAPPER_PID as usize - 1].satp << 12;
let root = unsafe { &mut *(tt_address as *mut PageTable) };
@ -277,6 +279,18 @@ pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) {
}
}
// allocate swap private stack
for i in 0..2 {
let stack_page = cfg.alloc() as usize;
cfg.map_page(
root,
stack_page,
SWAP_STACK_TOP_VADDR - PAGE_SIZE * (i + 1),
FLG_R | FLG_W | FLG_U | FLG_VALID,
SWAPPER_PID,
);
}
// map any hardware-specific pages into the userspace swapper
crate::platform::userspace_maps(cfg);
}

View file

@ -47,6 +47,6 @@ oom-doom = []
debug-verbose = []
# mutually exclusive feature with other crates that use the app UART
# *** requires "userspace-debug" to be passed to the loader for this to work. ***
# *** requires "userspace-swap-debug" to be passed to the loader for this to work. ***
debug-print-swapper = []
default = []

View file

@ -67,10 +67,7 @@ pub enum Opcode {
Test0 = 3,
// kernel has to be compiled with "debug-proc" for these to work
// the Debug* series of opcodes are hard-coded in other crates, so don't change their discriminant.
DebugProcesses = 9,
DebugServers = 10,
DebugFree = 11,
DebugInterrupts = 12,
DebugServers = 9,
None = 255,
}

View file

@ -582,7 +582,16 @@ fn swap_handler(
reserved_mem,
)
.ok();
xous::unmap_memory(reserved_mem).expect("couldn't free memory for hard OOM handler");
match xous::unmap_memory(reserved_mem) {
Ok(_) => {}
Err(e) => {
// unmap_memory doesn't work in an IRQ context - so this errors out. Let's
// just pray we have enough free memory for the handler to do its thing?
writeln!(DebugUart {}, "Likely in IRQ context: {:?} ***************", e).ok();
// replace the pages because they weren't unmapped
ss.hard_oom_reserved_page = Some(reserved_mem);
}
};
} else {
panic!("No space was reserved for the hard OOM manager to run!");
}
@ -645,17 +654,19 @@ fn swap_handler(
)
.ok();
// Restore some reserved memory for the next hard OOM invocation.
let mut reserved = xous::map_memory(
None,
None,
PAGE_SIZE * HARD_OOM_RESERVED_PAGES,
MemoryFlags::R | MemoryFlags::W | MemoryFlags::RESERVE,
)
.expect("could't reserve space for hard OOM handler");
// *touch* the memory -- otherwise it might not actually be demand-paged
let reserved_slice: &mut [u32] = unsafe { reserved.as_slice_mut() }; // this is safe because `u32` is fully representable
reserved_slice.fill(0);
ss.hard_oom_reserved_page = Some(reserved);
if ss.hard_oom_reserved_page.is_none() {
let mut reserved = xous::map_memory(
None,
None,
PAGE_SIZE * HARD_OOM_RESERVED_PAGES,
MemoryFlags::R | MemoryFlags::W | MemoryFlags::RESERVE,
)
.expect("could't reserve space for hard OOM handler");
// *touch* the memory -- otherwise it might not actually be demand-paged
let reserved_slice: &mut [u32] = unsafe { reserved.as_slice_mut() }; // this is safe because `u32` is fully representable
reserved_slice.fill(0);
ss.hard_oom_reserved_page = Some(reserved);
}
if ss.report_full_rpt {
ss.report_full_rpt = false;
}
@ -822,21 +833,10 @@ fn main() {
.expect("couldn't Block Erase region");
}
}
Some(Opcode::DebugProcesses) => {
xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugProcesses as usize, 0, 0, 0, 0, 0, 0))
.unwrap();
}
Some(Opcode::DebugServers) => {
xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugServers as usize, 0, 0, 0, 0, 0, 0))
.unwrap();
}
Some(Opcode::DebugFree) => {
xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugFree as usize, 0, 0, 0, 0, 0, 0)).unwrap();
}
Some(Opcode::DebugInterrupts) => {
xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::DebugInterrupts as usize, 0, 0, 0, 0, 0, 0))
.unwrap();
}
#[cfg(feature = "swap-userspace-testing")]
Some(Opcode::Test0) => {
log::info!("Free mem: {}kiB", get_free_pages() * PAGE_SIZE / 1024);

View file

@ -17,6 +17,7 @@ pub const PAGE_SIZE: usize = 4096;
pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000;
pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000;
pub const THREAD_CONTEXT_AREA: usize = 0xff80_1000;
pub const USERSPACE_BUFFER: usize = 0xff90_0000;
pub const FLG_VALID: usize = 0x1;
pub const FLG_R: usize = 0x2;
@ -37,6 +38,7 @@ pub const SWAP_RPT_VADDR: usize = 0xE100_1000;
pub const SWAP_COUNT_VADDR: usize = 0xE110_0000;
pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000;
pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000;
pub const SWAP_STACK_TOP_VADDR: usize = 0xE800_0000;
pub fn map_memory_pre(
_phys: &Option<MemoryAddress>,