Issue
I am looking for a way to insert some artificial delays when the operating system access the Swap Space. I am working on the latest version of kernel (Version 6.0.6 right now), I have found some locations in the kernel open source code that it handles page faults (do_page_fault()
), but I am not sure if it is the right place to insert the delay?
I tried do_page_fault
part in arch/x86/fault.c
(I am using the ubuntu 22.04 for my testing). But no success. When making and updating kernel, sometimes it will insert so many delays and sometime it don't.
Solution
There are two types of page faults: Major and Minor.
What you are looking for is recognized as Major Fault. There is a file called memory.c
. in the function vm_fault_t do_swap_page(struct vm_fault *vmf)
you can find the implementations of it, see the following code: (linux -> mm -> memory.c)
if (!folio) {
/*
* Back out if somebody else faulted in this pte
* while we released the pte lock.
*/
vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
vmf->address, &vmf->ptl);
if (likely(pte_same(*vmf->pte, vmf->orig_pte)))
ret = VM_FAULT_OOM;
goto unlock;
}
/* Had to read the page from swap area: Major fault */
ret = VM_FAULT_MAJOR;
count_vm_event(PGMAJFAULT);
count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Answered By - Amirhossein Sefati Answer Checked By - Gilberto Lyons (WPSolving Admin)