ANDROID: mm: allow limited speculative page faulting in do_swap_page()

Speculative page handling was disabled in do_swap_page() because it was
unsafe to call migration_entry_wait(). Another calls which are not safe
without taking mmap_lock are ksm_might_need_to_copy() because it relies
on the VMA being stable and readahead. However if we avoid these cases,
the rest seems to be safe. Relax the check to avoid only these unsafe
cases and allow speculation otherwise.

Bug: 322762567
Change-Id: Ic1fda0a5549088d5f37004dbacf3193116a5f868
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
This commit is contained in:
Suren Baghdasaryan
2024-02-08 13:45:31 -08:00
parent d87717c9e0
commit 8014b372fb

View File

@@ -3720,16 +3720,26 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
void *shadow = NULL;
if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
/* ksm_might_need_to_copy() needs a stable VMA, spf can't be used */
#ifdef CONFIG_KSM
pte_unmap(vmf->pte);
count_vm_spf_event(SPF_ABORT_SWAP);
return VM_FAULT_RETRY;
#endif
}
if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte))
if (!pte_unmap_same(vma->vm_mm, vmf->pmd, vmf->pte, vmf->orig_pte)) {
if (vmf->flags & FAULT_FLAG_SPECULATIVE)
ret = VM_FAULT_RETRY;
goto out;
}
entry = pte_to_swp_entry(vmf->orig_pte);
if (unlikely(non_swap_entry(entry))) {
if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
ret = VM_FAULT_RETRY;
goto out;
}
if (is_migration_entry(entry)) {
migration_entry_wait(vma->vm_mm, vmf->pmd,
vmf->address);
@@ -3787,6 +3797,17 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
swap_readpage(page, true);
set_page_private(page, 0);
}
} else if (vmf->flags & FAULT_FLAG_SPECULATIVE) {
/*
* Don't try readahead during a speculative page fault
* as the VMA's boundaries may change in our back.
* If the page is not in the swap cache and synchronous
* read is disabled, fall back to the regular page fault
* mechanism.
*/
delayacct_clear_flag(current, DELAYACCT_PF_SWAPIN);
ret = VM_FAULT_RETRY;
goto out;
} else {
page = swapin_readahead(entry,
GFP_HIGHUSER_MOVABLE | __GFP_CMA,