Skip to content

Commit 09854ba

Browse files
committed
mm: do_wp_page() simplification
How about we just make sure we're the only possible valid user fo the page before we bother to reuse it? Simplify, simplify, simplify. And get rid of the nasty serialization on the page lock at the same time. [peterx: add subject prefix] Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Peter Xu <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent bcf8768 commit 09854ba

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

mm/memory.c

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,50 +2924,25 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
29242924
* not dirty accountable.
29252925
*/
29262926
if (PageAnon(vmf->page)) {
2927-
int total_map_swapcount;
2928-
if (PageKsm(vmf->page) && (PageSwapCache(vmf->page) ||
2929-
page_count(vmf->page) != 1))
2927+
struct page *page = vmf->page;
2928+
2929+
/* PageKsm() doesn't necessarily raise the page refcount */
2930+
if (PageKsm(page) || page_count(page) != 1)
2931+
goto copy;
2932+
if (!trylock_page(page))
2933+
goto copy;
2934+
if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) {
2935+
unlock_page(page);
29302936
goto copy;
2931-
if (!trylock_page(vmf->page)) {
2932-
get_page(vmf->page);
2933-
pte_unmap_unlock(vmf->pte, vmf->ptl);
2934-
lock_page(vmf->page);
2935-
vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
2936-
vmf->address, &vmf->ptl);
2937-
if (!pte_same(*vmf->pte, vmf->orig_pte)) {
2938-
update_mmu_tlb(vma, vmf->address, vmf->pte);
2939-
unlock_page(vmf->page);
2940-
pte_unmap_unlock(vmf->pte, vmf->ptl);
2941-
put_page(vmf->page);
2942-
return 0;
2943-
}
2944-
put_page(vmf->page);
2945-
}
2946-
if (PageKsm(vmf->page)) {
2947-
bool reused = reuse_ksm_page(vmf->page, vmf->vma,
2948-
vmf->address);
2949-
unlock_page(vmf->page);
2950-
if (!reused)
2951-
goto copy;
2952-
wp_page_reuse(vmf);
2953-
return VM_FAULT_WRITE;
2954-
}
2955-
if (reuse_swap_page(vmf->page, &total_map_swapcount)) {
2956-
if (total_map_swapcount == 1) {
2957-
/*
2958-
* The page is all ours. Move it to
2959-
* our anon_vma so the rmap code will
2960-
* not search our parent or siblings.
2961-
* Protected against the rmap code by
2962-
* the page lock.
2963-
*/
2964-
page_move_anon_rmap(vmf->page, vma);
2965-
}
2966-
unlock_page(vmf->page);
2967-
wp_page_reuse(vmf);
2968-
return VM_FAULT_WRITE;
29692937
}
2970-
unlock_page(vmf->page);
2938+
/*
2939+
* Ok, we've got the only map reference, and the only
2940+
* page count reference, and the page is locked,
2941+
* it's dark out, and we're wearing sunglasses. Hit it.
2942+
*/
2943+
wp_page_reuse(vmf);
2944+
unlock_page(page);
2945+
return VM_FAULT_WRITE;
29712946
} else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
29722947
(VM_WRITE|VM_SHARED))) {
29732948
return wp_page_shared(vmf);

0 commit comments

Comments
 (0)