From c6f074f1758e233965495f589863b75ab0e1609d Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 13 Oct 2020 10:22:00 -0700 Subject: [PATCH 1/4] mm: move final page locking out of __do_fault() helper into callers The old semantics of our __do_fault() helper was that it always locked the page unless there was an error (or unless the faulting had already handled a COW event). That turns out to be a mistake. Not all callers actually want the page locked at all, and they might as well check the same VM_FAULT_LOCKED bit that __do_fault() itself checked whether the page is already locked or not. This change only moves that final page locking out into the callers, but intentionally does not actually change any of the locking semantics: the callers will not just do that final page locking themselves instead. That means that future patches may then decide to not lock the page after all, but this is just preparation for any such future change. Signed-off-by: Linus Torvalds --- mm/memory.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index eeae590e526a..b4a7b81dcc7a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3616,11 +3616,6 @@ static vm_fault_t __do_fault(struct vm_fault *vmf) return VM_FAULT_HWPOISON; } - if (unlikely(!(ret & VM_FAULT_LOCKED))) - lock_page(vmf->page); - else - VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); - return ret; } @@ -4000,6 +3995,11 @@ static vm_fault_t do_read_fault(struct vm_fault *vmf) if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) return ret; + if (unlikely(!(ret & VM_FAULT_LOCKED))) + lock_page(vmf->page); + else + VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); + ret |= finish_fault(vmf); unlock_page(vmf->page); if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) @@ -4031,6 +4031,11 @@ static vm_fault_t do_cow_fault(struct vm_fault *vmf) if (ret & VM_FAULT_DONE_COW) return ret; + if (unlikely(!(ret & VM_FAULT_LOCKED))) + lock_page(vmf->page); + else + VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); + copy_user_highpage(vmf->cow_page, vmf->page, vmf->address, vma); __SetPageUptodate(vmf->cow_page); @@ -4054,6 +4059,11 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf) if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY))) return ret; + if (unlikely(!(ret & VM_FAULT_LOCKED))) + lock_page(vmf->page); + else + VM_BUG_ON_PAGE(!PageLocked(vmf->page), vmf->page); + /* * Check if the backing address space wants to know that the page is * about to become writable -- 2.28.0.218.gc12ef3d349