attempt to fix race conditions is dirty/flush loop in swapper

this ultimately failed, we're going to try a different approach
This commit is contained in:
bunnie 2025-05-17 16:27:17 +08:00
parent 979ce7a589
commit 1a5a0a1388

View file

@ -1138,6 +1138,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
} }
_ => { _ => {
// if it happens to be swapped out, don't error on this // if it happens to be swapped out, don't error on this
println!("MarkDirty: page does not exist in memory {:x}(v)", page);
() ()
} }
} }
@ -1147,40 +1148,57 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
}) })
} }
SwapAbi::Sync => { SwapAbi::Sync => {
if pid.get() != xous_kernel::SWAPPER_PID {
return Err(xous_kernel::Error::AccessDenied);
}
// This will only affect the current PID's mapped pages; it also does not de-allocate // This will only affect the current PID's mapped pages; it also does not de-allocate
// the pages from RAM, it merely syncs to disk and clears the dirty bit. // the pages from RAM, it merely syncs to disk and clears the dirty bit.
let vaddr_start = a1; let vaddr_start = a1;
let len_to_sync = a2; let len_to_sync = a2;
// Any process can request a sync of dirty pages to FLASH let target_pid_usize = a3;
MemoryManager::with_mut(|mm| { let target_tid = crate::arch::process::current_tid();
let vaddr_start_aligned = vaddr_start & !(PAGE_SIZE - 1);
let vaddr_end_aligned = use crate::services::SystemServices;
((vaddr_start_aligned + (vaddr_start - vaddr_start_aligned) + len_to_sync) SystemServices::with(|system_services| {
+ (PAGE_SIZE - 1)) let target_pid = PID::new(target_pid_usize as u8).unwrap();
& !(PAGE_SIZE - 1); // get a handle to the target memory space
for vpage in (vaddr_start_aligned..vaddr_end_aligned).step_by(PAGE_SIZE) { let target_map = system_services.get_process(target_pid).unwrap().mapping;
match crate::arch::mem::virt_to_phys(vpage) {
Ok(paddr) => { let swapper_pid = PID::new(xous_kernel::SWAPPER_PID).unwrap();
if mm.rpt_is_dirty(paddr) { let swapper_map = system_services.get_process(swapper_pid).unwrap().mapping;
#[cfg(feature = "debug-swap-verbose")] // Any process can request a sync of dirty pages to FLASH
println!( MemoryManager::with_mut(|mm| {
"*********** Flushing dirty page: {:x}v {:x}p", let vaddr_start_aligned = vaddr_start & !(PAGE_SIZE - 1);
vpage, paddr let vaddr_end_aligned =
); ((vaddr_start_aligned + (vaddr_start - vaddr_start_aligned) + len_to_sync)
Swap::with_mut(|swap| swap.flush_page_syscall(vpage, paddr)) + (PAGE_SIZE - 1))
} else { & !(PAGE_SIZE - 1);
#[cfg(feature = "debug-swap-verbose")] for vpage in (vaddr_start_aligned..vaddr_end_aligned).step_by(PAGE_SIZE) {
println!("********** {:x}v is clean", vpage); // swap to target memory space
target_map.activate().unwrap();
let paddr = match crate::arch::mem::virt_to_phys(vpage) {
Ok(paddr) => paddr,
_ => {
// keep this hint around because this may be indicative
// of an error.
println!("*********** Sync encountered unmapped page: {:x}v", vpage);
continue;
} }
} };
_ => { swapper_map.activate().unwrap();
// keep this hint around because this may be indicative if mm.rpt_is_dirty(paddr) {
// of an error. #[cfg(feature = "debug-swap-verbose")]
println!("*********** Sync encountered unmapped page: {:x}v", vpage); println!("*********** Flushing dirty page: {:x}v {:x}p", vpage, paddr);
() Swap::with_mut(|swap| {
swap.flush_page_syscall(vpage, paddr, target_pid, target_tid)
})
} else {
#[cfg(feature = "debug-swap-verbose")]
println!("********** {:x}v is clean", vpage);
} }
} }
} });
Ok(xous_kernel::Result::Ok) Ok(xous_kernel::Result::Ok)
}) })
} }