aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/mm/fault.c38
-rw-r--r--include/linux/mm.h2
-rw-r--r--include/linux/pagemap.h13
-rw-r--r--mm/filemap.c16
-rw-r--r--mm/memory.c10
5 files changed, 64 insertions, 15 deletions
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index 852b319edbdc..9b2345c9e0c3 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -956,8 +956,10 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
956 struct task_struct *tsk; 956 struct task_struct *tsk;
957 unsigned long address; 957 unsigned long address;
958 struct mm_struct *mm; 958 struct mm_struct *mm;
959 int write;
960 int fault; 959 int fault;
960 int write = error_code & PF_WRITE;
961 unsigned int flags = FAULT_FLAG_ALLOW_RETRY |
962 (write ? FAULT_FLAG_WRITE : 0);
961 963
962 tsk = current; 964 tsk = current;
963 mm = tsk->mm; 965 mm = tsk->mm;
@@ -1068,6 +1070,7 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
1068 bad_area_nosemaphore(regs, error_code, address); 1070 bad_area_nosemaphore(regs, error_code, address);
1069 return; 1071 return;
1070 } 1072 }
1073retry:
1071 down_read(&mm->mmap_sem); 1074 down_read(&mm->mmap_sem);
1072 } else { 1075 } else {
1073 /* 1076 /*
@@ -1111,8 +1114,6 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
1111 * we can handle it.. 1114 * we can handle it..
1112 */ 1115 */
1113good_area: 1116good_area:
1114 write = error_code & PF_WRITE;
1115
1116 if (unlikely(access_error(error_code, write, vma))) { 1117 if (unlikely(access_error(error_code, write, vma))) {
1117 bad_area_access_error(regs, error_code, address); 1118 bad_area_access_error(regs, error_code, address);
1118 return; 1119 return;
@@ -1123,21 +1124,34 @@ good_area:
1123 * make sure we exit gracefully rather than endlessly redo 1124 * make sure we exit gracefully rather than endlessly redo
1124 * the fault: 1125 * the fault:
1125 */ 1126 */
1126 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0); 1127 fault = handle_mm_fault(mm, vma, address, flags);
1127 1128
1128 if (unlikely(fault & VM_FAULT_ERROR)) { 1129 if (unlikely(fault & VM_FAULT_ERROR)) {
1129 mm_fault_error(regs, error_code, address, fault); 1130 mm_fault_error(regs, error_code, address, fault);
1130 return; 1131 return;
1131 } 1132 }
1132 1133
1133 if (fault & VM_FAULT_MAJOR) { 1134 /*
1134 tsk->maj_flt++; 1135 * Major/minor page fault accounting is only done on the
1135 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, 1136 * initial attempt. If we go through a retry, it is extremely
1136 regs, address); 1137 * likely that the page will be found in page cache at that point.
1137 } else { 1138 */
1138 tsk->min_flt++; 1139 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1139 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, 1140 if (fault & VM_FAULT_MAJOR) {
1140 regs, address); 1141 tsk->maj_flt++;
1142 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
1143 regs, address);
1144 } else {
1145 tsk->min_flt++;
1146 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
1147 regs, address);
1148 }
1149 if (fault & VM_FAULT_RETRY) {
1150 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1151 * of starvation. */
1152 flags &= ~FAULT_FLAG_ALLOW_RETRY;
1153 goto retry;
1154 }
1141 } 1155 }
1142 1156
1143 check_v8086_mode(regs, address, tsk); 1157 check_v8086_mode(regs, address, tsk);
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 2862009f9573..3bf46655b50a 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -144,6 +144,7 @@ extern pgprot_t protection_map[16];
144#define FAULT_FLAG_WRITE 0x01 /* Fault was a write access */ 144#define FAULT_FLAG_WRITE 0x01 /* Fault was a write access */
145#define FAULT_FLAG_NONLINEAR 0x02 /* Fault was via a nonlinear mapping */ 145#define FAULT_FLAG_NONLINEAR 0x02 /* Fault was via a nonlinear mapping */
146#define FAULT_FLAG_MKWRITE 0x04 /* Fault was mkwrite of existing pte */ 146#define FAULT_FLAG_MKWRITE 0x04 /* Fault was mkwrite of existing pte */
147#define FAULT_FLAG_ALLOW_RETRY 0x08 /* Retry fault if blocking */
147 148
148/* 149/*
149 * This interface is used by x86 PAT code to identify a pfn mapping that is 150 * This interface is used by x86 PAT code to identify a pfn mapping that is
@@ -723,6 +724,7 @@ static inline int page_mapped(struct page *page)
723 724
724#define VM_FAULT_NOPAGE 0x0100 /* ->fault installed the pte, not return page */ 725#define VM_FAULT_NOPAGE 0x0100 /* ->fault installed the pte, not return page */
725#define VM_FAULT_LOCKED 0x0200 /* ->fault locked the returned page */ 726#define VM_FAULT_LOCKED 0x0200 /* ->fault locked the returned page */
727#define VM_FAULT_RETRY 0x0400 /* ->fault blocked, must retry */
726 728
727#define VM_FAULT_HWPOISON_LARGE_MASK 0xf000 /* encodes hpage index for large hwpoison */ 729#define VM_FAULT_HWPOISON_LARGE_MASK 0xf000 /* encodes hpage index for large hwpoison */
728 730
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index e12cdc6d79ee..2d1ffe3cf1ee 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -299,6 +299,8 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
299extern void __lock_page(struct page *page); 299extern void __lock_page(struct page *page);
300extern int __lock_page_killable(struct page *page); 300extern int __lock_page_killable(struct page *page);
301extern void __lock_page_nosync(struct page *page); 301extern void __lock_page_nosync(struct page *page);
302extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
303 unsigned int flags);
302extern void unlock_page(struct page *page); 304extern void unlock_page(struct page *page);
303 305
304static inline void __set_page_locked(struct page *page) 306static inline void __set_page_locked(struct page *page)
@@ -351,6 +353,17 @@ static inline void lock_page_nosync(struct page *page)
351} 353}
352 354
353/* 355/*
356 * lock_page_or_retry - Lock the page, unless this would block and the
357 * caller indicated that it can handle a retry.
358 */
359static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
360 unsigned int flags)
361{
362 might_sleep();
363 return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
364}
365
366/*
354 * This is exported only for wait_on_page_locked/wait_on_page_writeback. 367 * This is exported only for wait_on_page_locked/wait_on_page_writeback.
355 * Never use this directly! 368 * Never use this directly!
356 */ 369 */
diff --git a/mm/filemap.c b/mm/filemap.c
index 8ed709a83eb7..33f81252a744 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -612,6 +612,19 @@ void __lock_page_nosync(struct page *page)
612 TASK_UNINTERRUPTIBLE); 612 TASK_UNINTERRUPTIBLE);
613} 613}
614 614
615int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
616 unsigned int flags)
617{
618 if (!(flags & FAULT_FLAG_ALLOW_RETRY)) {
619 __lock_page(page);
620 return 1;
621 } else {
622 up_read(&mm->mmap_sem);
623 wait_on_page_locked(page);
624 return 0;
625 }
626}
627
615/** 628/**
616 * find_get_page - find and get a page reference 629 * find_get_page - find and get a page reference
617 * @mapping: the address_space to search 630 * @mapping: the address_space to search
@@ -1550,7 +1563,8 @@ retry_find:
1550 goto no_cached_page; 1563 goto no_cached_page;
1551 } 1564 }
1552 1565
1553 lock_page(page); 1566 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags))
1567 return ret | VM_FAULT_RETRY;
1554 1568
1555 /* Did it get truncated? */ 1569 /* Did it get truncated? */
1556 if (unlikely(page->mapping != mapping)) { 1570 if (unlikely(page->mapping != mapping)) {
diff --git a/mm/memory.c b/mm/memory.c
index 92cc54e94137..714c4438d887 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2627,6 +2627,7 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
2627 struct page *page, *swapcache = NULL; 2627 struct page *page, *swapcache = NULL;
2628 swp_entry_t entry; 2628 swp_entry_t entry;
2629 pte_t pte; 2629 pte_t pte;
2630 int locked;
2630 struct mem_cgroup *ptr = NULL; 2631 struct mem_cgroup *ptr = NULL;
2631 int exclusive = 0; 2632 int exclusive = 0;
2632 int ret = 0; 2633 int ret = 0;
@@ -2677,8 +2678,12 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
2677 goto out_release; 2678 goto out_release;
2678 } 2679 }
2679 2680
2680 lock_page(page); 2681 locked = lock_page_or_retry(page, mm, flags);
2681 delayacct_clear_flag(DELAYACCT_PF_SWAPIN); 2682 delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2683 if (!locked) {
2684 ret |= VM_FAULT_RETRY;
2685 goto out_release;
2686 }
2682 2687
2683 /* 2688 /*
2684 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not 2689 * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
@@ -2927,7 +2932,8 @@ static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
2927 vmf.page = NULL; 2932 vmf.page = NULL;
2928 2933
2929 ret = vma->vm_ops->fault(vma, &vmf); 2934 ret = vma->vm_ops->fault(vma, &vmf);
2930 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) 2935 if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
2936 VM_FAULT_RETRY)))
2931 return ret; 2937 return ret;
2932 2938
2933 if (unlikely(PageHWPoison(vmf.page))) { 2939 if (unlikely(PageHWPoison(vmf.page))) {