Skip to content

Commit d065bd8

Browse files
walken-googletorvalds
authored andcommitted
mm: retry page fault when blocking on disk transfer
This change reduces mmap_sem hold times that are caused by waiting for disk transfers when accessing file mapped VMAs. It introduces the VM_FAULT_ALLOW_RETRY flag, which indicates that the call site wants mmap_sem to be released if blocking on a pending disk transfer. In that case, filemap_fault() returns the VM_FAULT_RETRY status bit and do_page_fault() will then re-acquire mmap_sem and retry the page fault. It is expected that the retry will hit the same page which will now be cached, and thus it will complete with a low mmap_sem hold time. Tests: - microbenchmark: thread A mmaps a large file and does random read accesses to the mmaped area - achieves about 55 iterations/s. Thread B does mmap/munmap in a loop at a separate location - achieves 55 iterations/s before, 15000 iterations/s after. - We are seeing related effects in some applications in house, which show significant performance regressions when running without this change. [akpm@linux-foundation.org: fix warning & crash] Signed-off-by: Michel Lespinasse <walken@google.com> Acked-by: Rik van Riel <riel@redhat.com> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Cc: Nick Piggin <nickpiggin@yahoo.com.au> Reviewed-by: Wu Fengguang <fengguang.wu@intel.com> Cc: Ying Han <yinghan@google.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Ingo Molnar <mingo@elte.hu> Cc: Thomas Gleixner <tglx@linutronix.de> Acked-by: "H. Peter Anvin" <hpa@zytor.com> Cc: <linux-arch@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b522c94 commit d065bd8

5 files changed

Lines changed: 64 additions & 15 deletions

File tree

‎arch/x86/mm/fault.c‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,10 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
956956
struct task_struct *tsk;
957957
unsigned long address;
958958
struct mm_struct *mm;
959-
int write;
960959
int fault;
960+
int write = error_code & PF_WRITE;
961+
unsigned int flags = FAULT_FLAG_ALLOW_RETRY |
962+
(write ? FAULT_FLAG_WRITE : 0);
961963

962964
tsk = current;
963965
mm = tsk->mm;
@@ -1068,6 +1070,7 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
10681070
bad_area_nosemaphore(regs, error_code, address);
10691071
return;
10701072
}
1073+
retry:
10711074
down_read(&mm->mmap_sem);
10721075
} else {
10731076
/*
@@ -1111,8 +1114,6 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
11111114
* we can handle it..
11121115
*/
11131116
good_area:
1114-
write = error_code & PF_WRITE;
1115-
11161117
if (unlikely(access_error(error_code, write, vma))) {
11171118
bad_area_access_error(regs, error_code, address);
11181119
return;
@@ -1123,21 +1124,34 @@ do_page_fault(struct pt_regs *regs, unsigned long error_code)
11231124
* make sure we exit gracefully rather than endlessly redo
11241125
* the fault:
11251126
*/
1126-
fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
1127+
fault = handle_mm_fault(mm, vma, address, flags);
11271128

11281129
if (unlikely(fault & VM_FAULT_ERROR)) {
11291130
mm_fault_error(regs, error_code, address, fault);
11301131
return;
11311132
}
11321133

1133-
if (fault & VM_FAULT_MAJOR) {
1134-
tsk->maj_flt++;
1135-
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
1136-
regs, address);
1137-
} else {
1138-
tsk->min_flt++;
1139-
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
1140-
regs, address);
1134+
/*
1135+
* Major/minor page fault accounting is only done on the
1136+
* initial attempt. If we go through a retry, it is extremely
1137+
* likely that the page will be found in page cache at that point.
1138+
*/
1139+
if (flags & FAULT_FLAG_ALLOW_RETRY) {
1140+
if (fault & VM_FAULT_MAJOR) {
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+
}
11411155
}
11421156

11431157
check_v8086_mode(regs, address, tsk);

‎include/linux/mm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ extern pgprot_t protection_map[16];
144144
#define FAULT_FLAG_WRITE 0x01 /* Fault was a write access */
145145
#define FAULT_FLAG_NONLINEAR 0x02 /* Fault was via a nonlinear mapping */
146146
#define FAULT_FLAG_MKWRITE 0x04 /* Fault was mkwrite of existing pte */
147+
#define FAULT_FLAG_ALLOW_RETRY 0x08 /* Retry fault if blocking */
147148

148149
/*
149150
* 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)
723724

724725
#define VM_FAULT_NOPAGE 0x0100 /* ->fault installed the pte, not return page */
725726
#define VM_FAULT_LOCKED 0x0200 /* ->fault locked the returned page */
727+
#define VM_FAULT_RETRY 0x0400 /* ->fault blocked, must retry */
726728

727729
#define VM_FAULT_HWPOISON_LARGE_MASK 0xf000 /* encodes hpage index for large hwpoison */
728730

‎include/linux/pagemap.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
299299
extern void __lock_page(struct page *page);
300300
extern int __lock_page_killable(struct page *page);
301301
extern void __lock_page_nosync(struct page *page);
302+
extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
303+
unsigned int flags);
302304
extern void unlock_page(struct page *page);
303305

304306
static inline void __set_page_locked(struct page *page)
@@ -350,6 +352,17 @@ static inline void lock_page_nosync(struct page *page)
350352
__lock_page_nosync(page);
351353
}
352354

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+
*/
359+
static 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+
353366
/*
354367
* This is exported only for wait_on_page_locked/wait_on_page_writeback.
355368
* Never use this directly!

‎mm/filemap.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,19 @@ void __lock_page_nosync(struct page *page)
612612
TASK_UNINTERRUPTIBLE);
613613
}
614614

615+
int __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+
615628
/**
616629
* find_get_page - find and get a page reference
617630
* @mapping: the address_space to search
@@ -1550,7 +1563,8 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
15501563
goto no_cached_page;
15511564
}
15521565

1553-
lock_page(page);
1566+
if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags))
1567+
return ret | VM_FAULT_RETRY;
15541568

15551569
/* Did it get truncated? */
15561570
if (unlikely(page->mapping != mapping)) {

‎mm/memory.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,7 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
26272627
struct page *page, *swapcache = NULL;
26282628
swp_entry_t entry;
26292629
pte_t pte;
2630+
int locked;
26302631
struct mem_cgroup *ptr = NULL;
26312632
int exclusive = 0;
26322633
int ret = 0;
@@ -2677,8 +2678,12 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
26772678
goto out_release;
26782679
}
26792680

2680-
lock_page(page);
2681+
locked = lock_page_or_retry(page, mm, flags);
26812682
delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
2683+
if (!locked) {
2684+
ret |= VM_FAULT_RETRY;
2685+
goto out_release;
2686+
}
26822687

26832688
/*
26842689
* 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,
29272932
vmf.page = NULL;
29282933

29292934
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)))
29312937
return ret;
29322938

29332939
if (unlikely(PageHWPoison(vmf.page))) {

0 commit comments

Comments
 (0)