drop the mmap_sem when doing IO in the fault path
Subsystems (33)
cover drop the mmap_sem when doing IO in the fault path
Now that we have proper isolation in place with cgroups2 we have started going through and fixing the various priority inversions. Most are all gone now, but this one is sort of weird since it's not necessarily a priority inversion that happens within the kernel, but rather because of something userspace does.
We have giant applications that we want to protect, and parts of these giant applications do things like watch the system state to determine how healthy the box is for load balancing and such. This involves running 'ps' or other such utilities. These utilities will often walk /proc/<pid>/whatever, and these files can sometimes need to down_read(&task->mmap_sem). Not usually a big deal, but we noticed when we are stress testing that sometimes our protected application has latency spikes trying to get the mmap_sem for tasks that are in lower priority cgroups.
This is because any down_write() on a semaphore essentially turns it into a mutex, so even if we currently have it held for reading, any new readers will not be allowed on to keep from starving the writer. This is fine, except a lower priority task could be stuck doing IO because it has been throttled to the point that its IO is taking much longer than normal. But because a higher priority group depends on this completing it is now stuck behind lower priority work.
In order to avoid this particular priority inversion we want to use the existing retry mechanism to stop from holding the mmap_sem at all if we are going to do IO. This already exists in the read case sort of, but needed to be extended for more than just grabbing the page lock. With io.latency we throttle at submit_bio() time, so the readahead stuff can block and even page_cache_read can block, so all these paths need to have the mmap_sem dropped.
The other big thing is ->page_mkwrite. btrfs is particularly shitty here because we have to reserve space for the dirty page, which can be a very expensive operation. We use the same retry method as the read path, and simply cache the page and verify the page is still setup properly the next pass through ->page_mkwrite().
I've tested these patches with xfstests and there are no regressions. Let me know what you think. Thanks,
Josef
PATCH 1/8 mm: push vm_fault into the page fault handlers
In preparation for caching pages during filemap faults we need to push the struct vm_fault up a level into the arch page fault handlers, since they are the ones responsible for retrying if we unlock the mmap_sem.
--- arch/alpha/mm/fault.c | 4 ++- arch/arc/mm/fault.c | 2 ++ arch/arm/mm/fault.c | 18 ++++++++----- arch/arm64/mm/fault.c | 18 +++++++------ arch/hexagon/mm/vm_fault.c | 4 ++- arch/ia64/mm/fault.c | 4 ++- arch/m68k/mm/fault.c | 5 ++-- arch/microblaze/mm/fault.c | 4 ++- arch/mips/mm/fault.c | 4 ++- arch/nds32/mm/fault.c | 5 ++-- arch/nios2/mm/fault.c | 4 ++- arch/openrisc/mm/fault.c | 5 ++-- arch/parisc/mm/fault.c | 5 ++-- arch/powerpc/mm/copro_fault.c | 4 ++- arch/powerpc/mm/fault.c | 4 ++- arch/riscv/mm/fault.c | 2 ++ arch/s390/mm/fault.c | 4 ++- arch/sh/mm/fault.c | 4 ++- arch/sparc/mm/fault_32.c | 6 ++++- arch/sparc/mm/fault_64.c | 2 ++ arch/um/kernel/trap.c | 4 ++- arch/unicore32/mm/fault.c | 17 +++++++----- arch/x86/mm/fault.c | 4 ++- arch/xtensa/mm/fault.c | 4 ++- drivers/iommu/amd_iommu_v2.c | 4 ++- drivers/iommu/intel-svm.c | 6 +++-- include/linux/mm.h | 16 +++++++++--- mm/gup.c | 8 ++++-- mm/hmm.c | 4 ++- mm/ksm.c | 10 ++++--- mm/memory.c | 61 +++++++++++++++++++++---------------------- 31 files changed, 157 insertions(+), 89 deletions(-)
arch/alpha/mm/fault.c +3 -1
@@ -84,6 +84,7 @@ asmlinkage void do_page_fault(unsigned long address, unsigned long mmcsr, long cause, struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct * vma;struct mm_struct *mm = current->mm;const struct exception_table_entry *fixup;
@@ -148,7 +149,8 @@ do_page_fault(unsigned long address, unsigned long mmcsr,/* If for any reason at all we couldn't handle the fault, make sure we exit gracefully rather than endlessly redo the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmfs, vma, flags, address); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/arc/mm/fault.c +2 -0
@@ -63,6 +63,7 @@ noinline static int handle_kernel_vaddr_fault(unsigned long address)void do_page_fault (unsigned long address,struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct *vma =NULL ;struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;
@@ -141,6 +142,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)* make sure we exit gracefully rather than endlessly redo * the fault. */ +vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (vma, address, flags);/* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
arch/arm/mm/fault.c +11 -7
@@ -225,17 +225,17 @@ static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) }static vm_fault_t __kprobes -__do_page_fault (struct mm_struct *mm,unsigned long addr,unsigned int fsr, -unsigned int flags,struct task_struct *tsk) +__do_page_fault (struct mm_struct *mm,struct vm_fault *vm,unsigned int fsr, +struct task_struct *tsk) {struct vm_area_struct *vma;vm_fault_t fault; - vma =find_vma (mm, addr); + vma =find_vma (mm, vmf->address); fault =VM_FAULT_BADMAP ;if (unlikely (!vma)) goto out; -if (unlikely (vma->vm_start > addr)) +if (unlikely (vma->vm_start > vmf->address)) goto check_stack;/*
@@ -248,12 +248,14 @@ __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, goto out; } -return handle_mm_fault (vma, addr &PAGE_MASK , flags); + vmf->vma = vma; +return handle_mm_fault (vmf); check_stack:/* Don't allow expansion below FIRST_USER_ADDRESS */ if (vma->vm_flags &VM_GROWSDOWN && - addr >=FIRST_USER_ADDRESS && !expand_stack (vma, addr)) + vmf->address >=FIRST_USER_ADDRESS && + !expand_stack (vma, vmf->address)) goto good_area; out:return fault;
@@ -262,6 +264,7 @@ __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,static int __kprobes do_page_fault (unsigned long addr,unsigned int fsr,struct pt_regs *regs) { + struct vm_fault = {};struct task_struct *tsk;struct mm_struct *mm;int sig, code;
@@ -314,7 +317,8 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)#endif } - fault =__do_page_fault (mm, addr, fsr, flags, tsk); +vm_fault_init (&vmf,NULL , addr, flags); + fault =__do_page_fault (mm, &vmf, fsr, tsk);/* If we need to retry but a fatal signal is pending, handle the * signal first. We do not need to release the mmap_sem because
arch/arm64/mm/fault.c +10 -8
@@ -379,18 +379,17 @@ static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *re#define VM_FAULT_BADMAP 0x010000#define VM_FAULT_BADACCESS 0x020000 -static vm_fault_t __do_page_fault (struct mm_struct *mm,unsigned long addr, -unsigned int mm_flags,unsigned long vm_flags, -struct task_struct *tsk) +static vm_fault_t __do_page_fault (struct mm_struct *mm,struct vm_fault *vmf, +unsigned long vm_flags,struct task_struct *tsk) {struct vm_area_struct *vma;vm_fault_t fault; - vma =find_vma (mm, addr); + vma =find_vma (mm, vmf->address); fault =VM_FAULT_BADMAP ;if (unlikely (!vma)) goto out; -if (unlikely (vma->vm_start > addr)) +if (unlikely (vma->vm_start > vmf->address)) goto check_stack;/*
@@ -407,10 +406,11 @@ static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr, goto out; } -return handle_mm_fault (vma, addr &PAGE_MASK , mm_flags); + vmf->vma = vma; +return handle_mm_fault (vmf); check_stack: -if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, addr)) +if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, vmf->address)) goto good_area; out:return fault;
@@ -424,6 +424,7 @@ static bool is_el0_instruction_abort(unsigned int esr)static int __kprobes do_page_fault (unsigned long addr,unsigned int esr,struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;struct siginfo si;
@@ -493,7 +494,8 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,#endif } - fault =__do_page_fault (mm, addr, mm_flags, vm_flags, tsk); +vm_fault_init (&vmf,NULL , addr, mm_flags); + fault =__do_page_fault (mm, vmf, vm_flags, tsk); major |= fault &VM_FAULT_MAJOR ;if (fault &VM_FAULT_RETRY ) {
arch/hexagon/mm/vm_fault.c +3 -1
@@ -48,6 +48,7 @@*/ void do_page_fault (unsigned long address,long cause,struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct *vma;struct mm_struct *mm = current->mm;int si_signo;
@@ -102,7 +103,8 @@ void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)break ; } - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/ia64/mm/fault.c +3 -1
@@ -82,6 +82,7 @@ mapped_kernel_page_is_present (unsigned long address)void __kprobes ia64_do_page_fault (unsigned long address,unsigned long isr,struct pt_regs *regs) { +struct vm_fault vmf = {};int signal =SIGSEGV , code =SEGV_MAPERR ;struct vm_area_struct *vma, *prev_vma;struct mm_struct *mm = current->mm;
@@ -161,7 +162,8 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re* sure we exit gracefully rather than endlessly redo the * fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/m68k/mm/fault.c +3 -2
@@ -68,6 +68,7 @@ int send_fault_sig(struct pt_regs *regs)int do_page_fault (struct pt_regs *regs,unsigned long address,unsigned long error_code) { +struct vm_fault vmf = {};struct mm_struct *mm = current->mm;struct vm_area_struct * vma;vm_fault_t fault;
@@ -134,8 +135,8 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* make sure we exit gracefully rather than endlessly redo * the fault. */ - - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);pr_debug ("handle_mm_fault returns %x\n" , fault);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))
arch/microblaze/mm/fault.c +3 -1
@@ -86,6 +86,7 @@ void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)void do_page_fault (struct pt_regs *regs,unsigned long address,unsigned long error_code) { +struct vm_fault vmf = {};struct vm_area_struct *vma;struct mm_struct *mm = current->mm;int code =SEGV_MAPERR ;
@@ -215,7 +216,8 @@ void do_page_fault(struct pt_regs *regs, unsigned long address,* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/mips/mm/fault.c +3 -1
@@ -38,6 +38,7 @@ int show_unhandled_signals = 1;static void __kprobes __do_page_fault (struct pt_regs *regs,unsigned long write,unsigned long address) { +struct vm_fault vmf = {};struct vm_area_struct * vma =NULL ;struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;
@@ -152,7 +153,8 @@ static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/nds32/mm/fault.c +3 -2
@@ -69,6 +69,7 @@ void show_pte(struct mm_struct *mm, unsigned long addr)void do_page_fault (unsigned long entry,unsigned long addr,unsigned int error_code,struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;struct vm_area_struct *vma;
@@ -203,8 +204,8 @@ void do_page_fault(unsigned long entry, unsigned long addr,* make sure we exit gracefully rather than endlessly redo * the fault. */ - - fault =handle_mm_fault (vma, addr, flags); +vm_fault_init (&vmf, vma, addr, flags); + fault =handle_mm_fault (&vmf);/* * If we need to retry but a fatal signal is pending, handle the
arch/nios2/mm/fault.c +3 -1
@@ -43,6 +43,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,unsigned long address) { +struct vm_fault vmf = {};struct vm_area_struct *vma =NULL ;struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;
@@ -132,7 +133,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/openrisc/mm/fault.c +3 -2
@@ -49,6 +49,7 @@ extern void die(char *, struct pt_regs *, long); asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,unsigned long vector,int write_acc) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;struct vm_area_struct *vma;
@@ -162,8 +163,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,* make sure we exit gracefully rather than endlessly redo * the fault. */ - - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/parisc/mm/fault.c +3 -2
@@ -258,6 +258,7 @@ show_signal_msg(struct pt_regs *regs, unsigned long code,void do_page_fault (struct pt_regs *regs,unsigned long code,unsigned long address) { +struct vm_fault vmf = {};struct vm_area_struct *vma, *prev_vma;struct task_struct *tsk;struct mm_struct *mm;
@@ -300,8 +301,8 @@ void do_page_fault(struct pt_regs *regs, unsigned long code,* sure we exit gracefully rather than endlessly redo the * fault. */ - - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
arch/powerpc/mm/copro_fault.c +3 -1
@@ -36,6 +36,7 @@int copro_handle_mm_fault (struct mm_struct *mm,unsigned long ea,unsigned long dsisr,vm_fault_t *flt) { +struct vm_fault vmf = {};struct vm_area_struct *vma;unsigned long is_write;int ret;
@@ -77,7 +78,8 @@ int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, } ret =0 ; - *flt =handle_mm_fault (vma, ea, is_write ?FAULT_FLAG_WRITE :0 ); +vm_fault_init (&vmf, vma, ea, is_write ?FAULT_FLAG_WRITE :0 ); + *flt =handle_mm_fault (&vmf);if (unlikely (*flt &VM_FAULT_ERROR )) {if (*flt &VM_FAULT_OOM ) { ret = -ENOMEM ;
arch/powerpc/mm/fault.c +3 -1
@@ -409,6 +409,7 @@ static void sanity_check_fault(bool is_write, unsigned long error_code) { }static int __do_page_fault (struct pt_regs *regs,unsigned long address,unsigned long error_code) { +struct vm_fault vmf = {};struct vm_area_struct * vma;struct mm_struct *mm = current->mm;unsigned int flags =FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;
@@ -538,7 +539,8 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);#ifdef CONFIG_PPC_MEM_KEYS /*
arch/riscv/mm/fault.c +2 -0
@@ -36,6 +36,7 @@*/ asmlinkage void do_page_fault(struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct vm_area_struct *vma;struct mm_struct *mm;
@@ -120,6 +121,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs)* make sure we exit gracefully rather than endlessly redo * the fault. */ +vm_fault_init (&vmf, vma, addr, flags); fault =handle_mm_fault (vma, addr, flags);/*
arch/s390/mm/fault.c +3 -1
@@ -404,6 +404,7 @@ static noinline void do_fault_error(struct pt_regs *regs, int access,*/ static inline vm_fault_t do_exception (struct pt_regs *regs,int access) { +struct vm_fault vmf = {};struct gmap *gmap;struct task_struct *tsk;struct mm_struct *mm;
@@ -499,7 +500,8 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access)* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);/* No reason to continue if interrupted by SIGKILL. */ if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { fault =VM_FAULT_SIGNAL ;
arch/sh/mm/fault.c +3 -1
@@ -392,6 +392,7 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address) { + stuct vm_fault vmf = {};unsigned long vec;struct task_struct *tsk;struct mm_struct *mm;
@@ -481,7 +482,8 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if (unlikely (fault & (VM_FAULT_RETRY |VM_FAULT_ERROR )))if (mm_fault_error (regs, error_code, address, fault))
arch/sparc/mm/fault_32.c +5 -1
@@ -160,6 +160,7 @@ static noinline void do_fault_siginfo(int code, int sig, struct pt_regs *regs, asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,unsigned long address) { +struct vm_fault vmf = {};struct vm_area_struct *vma;struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;
@@ -235,6 +236,7 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,* make sure we exit gracefully rather than endlessly redo * the fault. */ +vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (vma, address, flags);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))
@@ -377,6 +379,7 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,/* This always deals with user addresses. */ static void force_user_fault (unsigned long address,int write) { +struct vm_fault vmf = {};struct vm_area_struct *vma;struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;
@@ -405,7 +408,8 @@ static void force_user_fault(unsigned long address, int write)if (!(vma->vm_flags & (VM_READ |VM_EXEC ))) goto bad_area; } -switch (handle_mm_fault (vma, address, flags)) { +vm_fault_init (&vmf, vma, address, flags); +switch (handle_mm_fault (&vmf)) {case VM_FAULT_SIGBUS :case VM_FAULT_OOM : goto do_sigbus;
arch/sparc/mm/fault_64.c +2 -0
@@ -274,6 +274,7 @@ static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs) asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs) { +struct vm_fault vmf = {};enum ctx_state prev_state =exception_enter ();struct mm_struct *mm = current->mm;struct vm_area_struct *vma;
@@ -433,6 +434,7 @@ asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs) goto bad_area; } +vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (vma, address, flags);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))
arch/um/kernel/trap.c +3 -1
@@ -25,6 +25,7 @@int handle_page_fault (unsigned long address,unsigned long ip,int is_write,int is_user,int *code_out) { +struct vm_fault vmf = {};struct mm_struct *mm = current->mm;struct vm_area_struct *vma;pgd_t *pgd;
@@ -74,7 +75,8 @@ int handle_page_fault(unsigned long address, unsigned long ip, do { vm_fault_t fault; - fault = handle_mm_fault(vma, address, flags); + vm_fault_init(&vmf, vma, address, flags); + fault = handle_mm_fault(&vmf); if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) goto out_nosemaphore;
arch/unicore32/mm/fault.c +10 -7
@@ -168,17 +168,17 @@ static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)return vma->vm_flags & mask ? false : true; } -static vm_fault_t __do_pf (struct mm_struct *mm,unsigned long addr, -unsigned int fsr,unsigned int flags,struct task_struct *tsk) +static vm_fault_t __do_pf (struct mm_struct *mm,struct vm_fault *vmf, +unsigned int fsr,struct task_struct *tsk) {struct vm_area_struct *vma;vm_fault_t fault; - vma =find_vma (mm, addr); + vma =find_vma (mm, vmf->address); fault =VM_FAULT_BADMAP ;if (unlikely (!vma)) goto out; -if (unlikely (vma->vm_start > addr)) +if (unlikely (vma->vm_start > vmf->address)) goto check_stack;/*
@@ -195,11 +195,12 @@ static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,* If for any reason at all we couldn't handle the fault, make * sure we exit gracefully rather than endlessly redo the fault. */ - fault =handle_mm_fault (vma, addr &PAGE_MASK , flags); + vmf->vma = vma; + fault =handle_mm_fault (vmf);return fault; check_stack: -if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, addr)) +if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, vmf->address)) goto good_area; out:return fault;
@@ -207,6 +208,7 @@ static vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,static int do_pf (unsigned long addr,unsigned int fsr,struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;int sig, code;
@@ -253,7 +255,8 @@ static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)#endif } - fault =__do_pf (mm, addr, fsr, flags, tsk); +vm_fault_init (&vmf,NULL , addr, flags); + fault =__do_pf (mm, &vmf, fsr, tsk);/* If we need to retry but a fatal signal is pending, handle the * signal first. We do not need to release the mmap_sem because
arch/x86/mm/fault.c +3 -1
@@ -1211,6 +1211,7 @@ static noinline void __do_page_fault(struct pt_regs *regs, unsigned long error_code, unsigned long address) { + struct vm_fault vmf = {};struct vm_area_struct *vma;struct task_struct *tsk;struct mm_struct *mm;
@@ -1392,7 +1393,8 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,* fault, so we read the pkey beforehand. */ pkey =vma_pkey (vma); - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf); major |= fault &VM_FAULT_MAJOR ;/*
arch/xtensa/mm/fault.c +3 -1
@@ -35,6 +35,7 @@ void bad_page_fault(struct pt_regs*, unsigned long, int);void do_page_fault (struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct * vma;struct mm_struct *mm = current->mm;unsigned int exccause = regs->exccause;
@@ -108,7 +109,8 @@ void do_page_fault(struct pt_regs *regs)* make sure we exit gracefully rather than endlessly redo * the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + fault =handle_mm_fault (&vmf);if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current))return ;
drivers/iommu/amd_iommu_v2.c +3 -1
@@ -506,6 +506,7 @@ static bool access_error(struct vm_area_struct *vma, struct fault *fault)static void do_fault (struct work_struct *work) { +struct vm_fault vmf = {}; struct fault *fault = container_of(work, struct fault, work);struct vm_area_struct *vma;vm_fault_t ret =VM_FAULT_ERROR ;
@@ -532,7 +533,8 @@ static void do_fault(struct work_struct *work)if (access_error (vma, fault)) goto out; - ret =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmf, vma, address, flags); + ret =handle_mm_fault (&vmf); out:up_read (&mm->mmap_sem);
drivers/iommu/intel-svm.c +4 -2
@@ -567,6 +567,7 @@ static bool is_canonical_address(u64 addr)static irqreturn_t prq_event_thread (int irq,void *d) { +struct vm_fault vmf = {};struct intel_iommu *iommu = d;struct intel_svm *svm =NULL ;int head, tail, handled =0 ;
@@ -636,8 +637,9 @@ static irqreturn_t prq_event_thread(int irq, void *d)if (access_error (vma, req)) goto invalid; - ret =handle_mm_fault (vma, address, - req->wr_req ?FAULT_FLAG_WRITE :0 ); +vm_fault_init (&vmf, vma, address, + req->wr_req ?FAULT_FLAG_WRITE :0 ); + ret =handle_mm_fault (&vmf);if (ret &VM_FAULT_ERROR ) goto invalid;
include/linux/mm.h +12 -4
@@ -378,6 +378,16 @@ struct vm_fault {*/ }; +static inline void vm_fault_init (struct vm_fault *vmf, +struct vm_area_struct *vma, +unsigned long address, +unsigned int flags) +{ + vmf->vma = vma; + vmf->address = address; + vmf->flags = flags; +} +/* page entry size for vm->huge_fault() */ enum page_entry_size {PE_SIZE_PTE =0 ,
@@ -1403,8 +1413,7 @@ int generic_error_remove_page(struct address_space *mapping, struct page *page);int invalidate_inode_page (struct page *page);#ifdef CONFIG_MMU -extern vm_fault_t handle_mm_fault (struct vm_area_struct *vma, -unsigned long address,unsigned int flags); +extern vm_fault_t handle_mm_fault (struct vm_fault *vmf);extern int fixup_user_fault (struct task_struct *tsk,struct mm_struct *mm,unsigned long address,unsigned int fault_flags,bool *unlocked);
@@ -1413,8 +1422,7 @@ void unmap_mapping_pages(struct address_space *mapping,void unmap_mapping_range (struct address_space *mapping,loff_t const holebegin,loff_t const holelen,int even_cows);#else -static inline vm_fault_t handle_mm_fault (struct vm_area_struct *vma, -unsigned long address,unsigned int flags) +static inline vm_fault_t handle_mm_fault (struct vm_fault *vmf) {/* should never happen if there's no MMU */ BUG ();
mm/gup.c +6 -2
@@ -496,6 +496,7 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,static int faultin_page (struct task_struct *tsk,struct vm_area_struct *vma,unsigned long address,unsigned int *flags,int *nonblocking) { +struct vm_fault vmf = {};unsigned int fault_flags =0 ;vm_fault_t ret;
@@ -515,7 +516,8 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma, fault_flags |=FAULT_FLAG_TRIED ; } - ret =handle_mm_fault (vma, address, fault_flags); +vm_fault_init (&vmf, vma, address, fault_flags); + ret =handle_mm_fault (&vmf);if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret, *flags);
@@ -817,6 +819,7 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, unsigned long address, unsigned int fault_flags, bool *unlocked) { + struct vm_fault vmf = {};struct vm_area_struct *vma;vm_fault_t ret, major =0 ;
@@ -831,7 +834,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,if (!vma_permits_fault (vma, fault_flags))return -EFAULT ; - ret =handle_mm_fault (vma, address, fault_flags); +vm_fault_init (&vmf, vma, address, fault_flags); + ret =handle_mm_fault (&vmf); major |= ret &VM_FAULT_MAJOR ;if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret,0 );
mm/hmm.c +3 -1
@@ -298,6 +298,7 @@ struct hmm_vma_walk {static int hmm_vma_do_fault (struct mm_walk *walk,unsigned long addr,bool write_fault,uint64_t *pfn) { +struct vm_fault vmf = {};unsigned int flags =FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_REMOTE ;struct hmm_vma_walk *hmm_vma_walk = walk->private;struct hmm_range *range = hmm_vma_walk->range;
@@ -306,7 +307,8 @@ static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, flags |= hmm_vma_walk->block ?0 :FAULT_FLAG_ALLOW_RETRY ; flags |= write_fault ?FAULT_FLAG_WRITE :0 ; - ret =handle_mm_fault (vma, addr, flags); +vm_fault_init (&vmf, vma, addr, flags); + ret =handle_mm_fault (&vmf);if (ret &VM_FAULT_RETRY )return -EBUSY ;if (ret &VM_FAULT_ERROR ) {
mm/ksm.c +6 -4
@@ -478,10 +478,12 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE);if (IS_ERR_OR_NULL (page))break ; -if (PageKsm (page)) - ret =handle_mm_fault (vma, addr, -FAULT_FLAG_WRITE |FAULT_FLAG_REMOTE ); -else +if (PageKsm (page)) { +struct vm_fault vmf = {}; +vm_fault_init (&vmf, vma, addr, +FAULT_FLAG_WRITE |FAULT_FLAG_REMOTE ); + ret =handle_mm_fault (&vmf); + }else ret =VM_FAULT_WRITE ;put_page (page); }while (!(ret & (VM_FAULT_WRITE |VM_FAULT_SIGBUS |VM_FAULT_SIGSEGV |VM_FAULT_OOM )));
mm/memory.c +30 -31
134 lines not on this page · raw message
Signature · 1 line
2.14.3
PATCH 2/8 mm: drop mmap_sem for page cache read IO submission
In reply to Josef BacikFrom: Johannes Weiner <[email protected]>
Reads can take a long time, and if anybody needs to take a write lock on the mmap_sem it'll block any subsequent readers to the mmap_sem while the read is outstanding, which could cause long delays. Instead drop the mmap_sem if we do any reads at all.
--- mm/filemap.c | 119 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 90 insertions(+), 29 deletions(-)
mm/filemap.c +90 -29
@@ -2366,6 +2366,18 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)EXPORT_SYMBOL (generic_file_read_iter);#ifdef CONFIG_MMU +static struct file *maybe_unlock_mmap_for_io (struct vm_area_struct *vma,int flags) +{ +if ((flags & (FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT )) ==FAULT_FLAG_ALLOW_RETRY ) { +struct file *file; + + file =get_file (vma->vm_file); +up_read (&vma->vm_mm->mmap_sem); +return file; + } +return NULL ; +} +/** * page_cache_read - adds requested page to the page cache if not already there * @file: file to read
@@ -2405,23 +2417,28 @@ static int page_cache_read(struct file *file, pgoff_t offset, gfp_t gfp_mask)* Synchronous readahead happens when we don't even find * a page in the page cache at all. */ -static void do_sync_mmap_readahead (struct vm_area_struct *vma, -struct file_ra_state *ra, -struct file *file, -pgoff_t offset) +static int do_sync_mmap_readahead (struct vm_area_struct *vma, +struct file_ra_state *ra, +struct file *file, +pgoff_t offset, +int flags) {struct address_space *mapping = file->f_mapping; +struct file *fpin;/* If we don't want any read-ahead, don't bother */ if (vma->vm_flags &VM_RAND_READ ) -return ; +return 0 ;if (!ra->ra_pages) -return ; +return 0 ;if (vma->vm_flags &VM_SEQ_READ ) { + fpin =maybe_unlock_mmap_for_io (vma, flags);page_cache_sync_readahead (mapping, ra, file, offset, ra->ra_pages); -return ; +if (fpin) +fput (fpin); +return fpin ? -EAGAIN :0 ; }/* Avoid banging the cache line if not needed */
@@ -2433,7 +2450,9 @@ static void do_sync_mmap_readahead(struct vm_area_struct *vma,* stop bothering with read-ahead. It will only hurt. */ if (ra->mmap_miss >MMAP_LOTSAMISS ) -return ; +return 0 ; + + fpin =maybe_unlock_mmap_for_io (vma, flags);/* * mmap read-around
@@ -2442,28 +2461,40 @@ static void do_sync_mmap_readahead(struct vm_area_struct *vma, ra->size = ra->ra_pages; ra->async_size = ra->ra_pages /4 ;ra_submit (ra, mapping, file); + +if (fpin) +fput (fpin); + +return fpin ? -EAGAIN :0 ; }/* * Asynchronous readahead happens when we find the page and PG_readahead, * so we want to possibly extend the readahead further.. */ -static void do_async_mmap_readahead (struct vm_area_struct *vma, -struct file_ra_state *ra, -struct file *file, -struct page *page, -pgoff_t offset) +static int do_async_mmap_readahead (struct vm_area_struct *vma, +struct file_ra_state *ra, +struct file *file, +struct page *page, +pgoff_t offset, +int flags) {struct address_space *mapping = file->f_mapping; +struct file *fpin;/* If we don't want any read-ahead, don't bother */ if (vma->vm_flags &VM_RAND_READ ) -return ; +return 0 ;if (ra->mmap_miss >0 ) ra->mmap_miss--; -if (PageReadahead (page)) -page_cache_async_readahead (mapping, ra, file, - page, offset, ra->ra_pages); +if (!PageReadahead (page)) +return 0 ; + fpin =maybe_unlock_mmap_for_io (vma, flags); +page_cache_async_readahead (mapping, ra, file, + page, offset, ra->ra_pages); +if (fpin) +fput (fpin); +return fpin ? -EAGAIN :0 ; }/**
@@ -2479,10 +2510,8 @@ static void do_async_mmap_readahead(struct vm_area_struct *vma,* * vma->vm_mm->mmap_sem must be held on entry. * -* If our return value has VM_FAULT_RETRY set, it's because -* lock_page_or_retry() returned 0. -* The mmap_sem has usually been released in this case. -* See __lock_page_or_retry() for the exception. +* If our return value has VM_FAULT_RETRY set, the mmap_sem has +* usually been released. * * If our return value does not have VM_FAULT_RETRY set, the mmap_sem * has not been released.
@@ -2492,11 +2521,13 @@ static void do_async_mmap_readahead(struct vm_area_struct *vma,vm_fault_t filemap_fault (struct vm_fault *vmf) {int error; +struct mm_struct *mm = vmf->vma->vm_mm;struct file *file = vmf->vma->vm_file;struct address_space *mapping = file->f_mapping;struct file_ra_state *ra = &file->f_ra;struct inode *inode = mapping->host;pgoff_t offset = vmf->pgoff; +int flags = vmf->flags;pgoff_t max_off;struct page *page;vm_fault_t ret =0 ;
@@ -2509,27 +2540,44 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)* Do we have something in the page cache already? */ page =find_get_page (mapping, offset); -if (likely (page) && !(vmf->flags &FAULT_FLAG_TRIED )) { +if (likely (page) && !(flags &FAULT_FLAG_TRIED )) {/* * We found the page, so try async readahead before * waiting for the lock. */ -do_async_mmap_readahead (vmf->vma, ra, file, page, offset); + error =do_async_mmap_readahead (vmf->vma, ra, file, page, offset, vmf->flags); +if (error == -EAGAIN ) + goto out_retry_wait; }else if (!page) {/* No page in the page cache at all */ -do_sync_mmap_readahead (vmf->vma, ra, file, offset); -count_vm_event (PGMAJFAULT ); -count_memcg_event_mm (vmf->vma->vm_mm,PGMAJFAULT ); ret =VM_FAULT_MAJOR ; +count_vm_event (PGMAJFAULT ); +count_memcg_event_mm (mm,PGMAJFAULT ); + error =do_sync_mmap_readahead (vmf->vma, ra, file, offset, vmf->flags); +if (error == -EAGAIN ) + goto out_retry_wait; retry_find: page =find_get_page (mapping, offset);if (!page) goto no_cached_page; } -if (!lock_page_or_retry (page, vmf->vma->vm_mm, vmf->flags)) { -put_page (page); -return ret |VM_FAULT_RETRY ; +if (!trylock_page (page)) { +if (flags &FAULT_FLAG_ALLOW_RETRY ) { +if (flags &FAULT_FLAG_RETRY_NOWAIT ) + goto out_retry; +up_read (&mm->mmap_sem); + goto out_retry_wait; + } +if (flags &FAULT_FLAG_KILLABLE ) { +int ret =__lock_page_killable (page); + +if (ret) { +up_read (&mm->mmap_sem); + goto out_retry; + } + }else +__lock_page (page); }/* Did it get truncated? */
@@ -2607,6 +2655,19 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)/* Things didn't work out. Return zero to tell the mm layer so. */ shrink_readahead_size_eio (file, ra);return VM_FAULT_SIGBUS ; + +out_retry_wait: +if (page) { +if (flags &FAULT_FLAG_KILLABLE ) +wait_on_page_locked_killable (page); +else +wait_on_page_locked (page); + } + +out_retry: +if (page) +put_page (page); +return ret |VM_FAULT_RETRY ; }EXPORT_SYMBOL (filemap_fault);
Signature · 1 line
2.14.3
PATCH 3/8 mm: clean up swapcache lookup and creation function names
In reply to Josef BacikFrom: Johannes Weiner <[email protected]>
__read_swap_cache_async() has a misleading name. All it does is look up or create a page in swapcache; it doesn't initiate any IO.
The swapcache has many parallels to the page cache, and shares naming schemes with it elsewhere. Analogous to the cache lookup and creation API, rename __read_swap_cache_async() find_or_create_swap_cache() and lookup_swap_cache() to find_swap_cache().
--- include/linux/swap.h | 14 ++++++++------ mm/memory.c | 2 +- mm/shmem.c | 2 +- mm/swap_state.c | 43 ++++++++++++++++++++++--------------------- mm/zswap.c | 8 ++++---- 5 files changed, 36 insertions(+), 33 deletions(-)
include/linux/swap.h +8 -6
@@ -412,15 +412,17 @@ extern void __delete_from_swap_cache(struct page *);extern void delete_from_swap_cache (struct page *);extern void free_page_and_swap_cache (struct page *);extern void free_pages_and_swap_cache (struct page **,int ); -extern struct page *lookup_swap_cache (swp_entry_t entry, -struct vm_area_struct *vma, -unsigned long addr); +extern struct page *find_swap_cache (swp_entry_t entry, +struct vm_area_struct *vma, +unsigned long addr); +extern struct page *find_or_create_swap_cache (swp_entry_t entry, +gfp_t gfp_mask, +struct vm_area_struct *vma, +unsigned long addr, +bool *created);extern struct page *read_swap_cache_async (swp_entry_t ,gfp_t ,struct vm_area_struct *vma,unsigned long addr,bool do_poll); -extern struct page *__read_swap_cache_async (swp_entry_t ,gfp_t , -struct vm_area_struct *vma,unsigned long addr, -bool *new_page_allocated);extern struct page *swap_cluster_readahead (swp_entry_t entry,gfp_t flag,struct vm_fault *vmf); extern struct page *swapin_readahead(swp_entry_t entry, gfp_t flag,
mm/memory.c +1 -1
@@ -2935,7 +2935,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)delayacct_set_flag (DELAYACCT_PF_SWAPIN ); - page =lookup_swap_cache (entry, vma, vmf->address); + page =find_swap_cache (entry, vma, vmf->address); swapcache = page;if (!page) {
mm/shmem.c +1 -1
@@ -1679,7 +1679,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,if (swap.val) {/* Look it up and read it in.. */ - page =lookup_swap_cache (swap,NULL ,0 ); + page =find_swap_cache (swap,NULL ,0 );if (!page) {/* Or update major stats only when swapin succeeds?? */ if (fault_type) {
mm/swap_state.c +22 -21
107 lines not on this page · raw message
mm/zswap.c +4 -4
@@ -823,11 +823,11 @@ enum zswap_get_swap_ret {static int zswap_get_swap_cache_page (swp_entry_t entry,struct page **retpage) { -bool page_was_allocated; +bool created; - *retpage =__read_swap_cache_async (entry,GFP_KERNEL , -NULL ,0 , &page_was_allocated); -if (page_was_allocated) + *retpage =find_or_create_swap_cache (entry,GFP_KERNEL , +NULL ,0 , &created); +if (created)return ZSWAP_SWAPCACHE_NEW ;if (!*retpage)return ZSWAP_SWAPCACHE_FAIL ;
Signature · 1 line
2.14.3
PATCH 4/8 mm: drop mmap_sem for swap read IO submission
In reply to Josef BacikFrom: Johannes Weiner <[email protected]>
We don't need to hold the mmap_sem while we're doing the IO, simply drop it and retry appropriately.
--- mm/page_io.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
mm/page_io.c +14 -0
@@ -365,6 +365,20 @@ int swap_readpage(struct page *page, bool synchronous) goto out; } +/* +* XXX: +* +* Propagate mm->mmap_sem into this function. Then: +* +* get_file(sis->swap_file) +* up_read(mm->mmap_sem) +* submit io request +* fput +* +* After mmap_sem is dropped, sis is no longer valid. Go +* through swap_file->blah->bdev. +*/ +if (sis->flags &SWP_FILE ) {struct file *swap_file = sis->swap_file;struct address_space *mapping = swap_file->f_mapping;
Signature · 1 line
2.14.3
PATCH 5/8 mm: drop the mmap_sem in all read fault cases
In reply to Josef BacikJohannes' patches didn't quite cover all of the IO cases that we need to drop the mmap_sem for, this patch covers the rest of them.
--- mm/filemap.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
mm/filemap.c +11 -0
@@ -2523,6 +2523,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)int error;struct mm_struct *mm = vmf->vma->vm_mm;struct file *file = vmf->vma->vm_file; +struct file *fpin =NULL ;struct address_space *mapping = file->f_mapping;struct file_ra_state *ra = &file->f_ra;struct inode *inode = mapping->host;
@@ -2610,11 +2611,15 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)return ret |VM_FAULT_LOCKED ; no_cached_page: + fpin =maybe_unlock_mmap_for_io (vmf->vma, vmf->flags); +/* * We're only likely to ever get here if MADV_RANDOM is in * effect. */ error =page_cache_read (file, offset, vmf->gfp_mask); +if (fpin) + goto out_retry;/* * The page we want has now been added to the page cache.
@@ -2634,6 +2639,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)return VM_FAULT_SIGBUS ; page_not_uptodate: + fpin =maybe_unlock_mmap_for_io (vmf->vma, vmf->flags); +/* * Umm, take care of errors if the page isn't up-to-date. * Try to re-read it _once_. We do this synchronously,
@@ -2647,6 +2654,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)if (!PageUptodate (page)) error = -EIO ; } +if (fpin) + goto out_retry;put_page (page);if (!error || error ==AOP_TRUNCATED_PAGE )
@@ -2665,6 +2674,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) } out_retry: +if (fpin) +fput (fpin);if (page)put_page (page);return ret |VM_FAULT_RETRY ;
Signature · 1 line
2.14.3
PATCH 6/8 mm: keep the page we read for the next loop
In reply to Josef BacikIf we drop the mmap_sem we need to redo the vma lookup and then re-lookup the page. This is kind of a waste since we've already done the work, and we could even possibly evict the page, causing a refault. Instead just hold a reference to the page and save it in our vm_fault. The next time we go through filemap_fault we'll grab our page, verify that it's the one we want and carry on.
--- arch/alpha/mm/fault.c | 7 +++++-- arch/arc/mm/fault.c | 6 +++++- arch/arm/mm/fault.c | 2 ++ arch/arm64/mm/fault.c | 2 ++ arch/hexagon/mm/vm_fault.c | 6 +++++- arch/ia64/mm/fault.c | 6 +++++- arch/m68k/mm/fault.c | 6 +++++- arch/microblaze/mm/fault.c | 6 +++++- arch/mips/mm/fault.c | 6 +++++- arch/nds32/mm/fault.c | 3 +++ arch/nios2/mm/fault.c | 6 +++++- arch/openrisc/mm/fault.c | 6 +++++- arch/parisc/mm/fault.c | 6 +++++- arch/powerpc/mm/copro_fault.c | 3 ++- arch/powerpc/mm/fault.c | 3 +++ arch/riscv/mm/fault.c | 6 +++++- arch/s390/mm/fault.c | 1 + arch/sh/mm/fault.c | 8 ++++++-- arch/sparc/mm/fault_32.c | 8 +++++++- arch/sparc/mm/fault_64.c | 6 +++++- arch/um/kernel/trap.c | 6 +++++- arch/unicore32/mm/fault.c | 5 ++++- arch/x86/mm/fault.c | 2 ++ arch/xtensa/mm/fault.c | 6 +++++- drivers/iommu/amd_iommu_v2.c | 1 + drivers/iommu/intel-svm.c | 1 + include/linux/mm.h | 14 ++++++++++++++ mm/filemap.c | 31 ++++++++++++++++++++++++++++--- mm/gup.c | 3 +++ mm/hmm.c | 1 + mm/ksm.c | 1 + 31 files changed, 151 insertions(+), 23 deletions(-)
arch/alpha/mm/fault.c +5 -2
@@ -152,10 +152,13 @@ do_page_fault(unsigned long address, unsigned long mmcsr,vm_fault_init (&vmfs, vma, flags, address); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -181,7 +184,7 @@ do_page_fault(unsigned long address, unsigned long mmcsr, goto retry; } } - +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/arc/mm/fault.c +5 -1
@@ -149,8 +149,10 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)if (unlikely (fatal_signal_pending (current))) {if ((fault &VM_FAULT_ERROR ) && !(fault &VM_FAULT_RETRY ))up_read (&mm->mmap_sem); -if (user_mode (regs)) +if (user_mode (regs)) { +vm_fault_cleanup (&vmf);return ; + } }perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);
@@ -176,10 +178,12 @@ void do_page_fault(unsigned long address, struct pt_regs *regs) }/* Fault Handled Gracefully */ +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ; } +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
arch/arm/mm/fault.c +2 -0
@@ -325,6 +325,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)* it would already be released in __lock_page_or_retry in * mm/filemap.c. */ if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);if (!user_mode (regs)) goto no_context;return 0 ;
@@ -356,6 +357,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);/*
arch/arm64/mm/fault.c +2 -0
@@ -506,6 +506,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,* in __lock_page_or_retry in mm/filemap.c. */ if (fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);if (!user_mode (regs)) goto no_context;return 0 ;
@@ -521,6 +522,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr, goto retry; } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);/*
arch/hexagon/mm/vm_fault.c +5 -1
@@ -106,8 +106,10 @@ void do_page_fault(unsigned long address, long cause, struct pt_regs *regs)vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }/* The most common case -- we are done. */ if (likely (!(fault &VM_FAULT_ERROR ))) {
@@ -123,10 +125,12 @@ void do_page_fault(unsigned long address, long cause, struct pt_regs *regs) } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ; } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);/* Handle copyin/out exception cases */
arch/ia64/mm/fault.c +5 -1
@@ -165,8 +165,10 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *revm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) {/*
@@ -174,6 +176,7 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re* to us that made us unable to handle the page fault * gracefully. */ +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) { goto out_of_memory; }else if (fault &VM_FAULT_SIGSEGV ) {
@@ -203,6 +206,7 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/m68k/mm/fault.c +5 -1
@@ -139,10 +139,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, fault =handle_mm_fault (&vmf);pr_debug ("handle_mm_fault returns %x\n" , fault); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return 0 ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -178,6 +181,7 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return 0 ;
arch/microblaze/mm/fault.c +5 -1
@@ -219,10 +219,13 @@ void do_page_fault(struct pt_regs *regs, unsigned long address,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -251,6 +254,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long address, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);/*
arch/mips/mm/fault.c +5 -1
@@ -156,11 +156,14 @@ static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -193,6 +196,7 @@ static void __kprobes __do_page_fault(struct pt_regs *regs, unsigned long write, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/nds32/mm/fault.c +3 -0
@@ -213,12 +213,14 @@ void do_page_fault(unsigned long entry, unsigned long addr,* would already be released in __lock_page_or_retry in mm/filemap.c. */ if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);if (!user_mode (regs)) goto no_context;return ; }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGBUS )
@@ -249,6 +251,7 @@ void do_page_fault(unsigned long entry, unsigned long addr, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/nios2/mm/fault.c +5 -1
@@ -136,10 +136,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -175,6 +178,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/openrisc/mm/fault.c +5 -1
@@ -166,10 +166,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -198,6 +201,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/parisc/mm/fault.c +5 -1
@@ -304,8 +304,10 @@ void do_page_fault(struct pt_regs *regs, unsigned long code,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) {/*
@@ -313,6 +315,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long code,* other thing happened to us that made us unable to * handle the page fault gracefully. */ +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -339,6 +342,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, goto retry; } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/powerpc/mm/copro_fault.c +2 -1
@@ -81,6 +81,7 @@ int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,vm_fault_init (&vmf, vma, ea, is_write ?FAULT_FLAG_WRITE :0 ); *flt =handle_mm_fault (&vmf);if (unlikely (*flt &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (*flt &VM_FAULT_OOM ) { ret = -ENOMEM ; goto out_unlock;
@@ -95,7 +96,7 @@ int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, current->maj_flt++; else current->min_flt++; - +vm_fault_cleanup (&vmf); out_unlock:up_read (&mm->mmap_sem);return ret;
arch/powerpc/mm/fault.c +3 -0
@@ -552,6 +552,7 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,int pkey =vma_pkey (vma); +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return bad_key_fault_exception (regs, address, pkey); }
@@ -580,9 +581,11 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,* User mode? Just return to handle the fatal exception otherwise * return to bad_page_fault */ +vm_fault_cleanup (&vmf);return is_user ?0 :SIGBUS ; } +vm_fault_cleanup (&vmf);up_read (¤t->mm->mmap_sem);if (unlikely (fault &VM_FAULT_ERROR ))
arch/riscv/mm/fault.c +5 -1
@@ -129,10 +129,13 @@ asmlinkage void do_page_fault(struct pt_regs *regs)* signal first. We do not need to release the mmap_sem because it * would already be released in __lock_page_or_retry in mm/filemap.c. */ -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (tsk)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (tsk)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGBUS )
@@ -172,6 +175,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs) } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
arch/s390/mm/fault.c +1 -0
@@ -561,6 +561,7 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access) out_up:up_read (&mm->mmap_sem); out: +vm_fault_cleanup (&vmf);return fault; }
arch/sh/mm/fault.c +6 -2
@@ -485,9 +485,12 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if (unlikely (fault & (VM_FAULT_RETRY |VM_FAULT_ERROR ))) -if (mm_fault_error (regs, error_code, address, fault)) +if (unlikely (fault & (VM_FAULT_RETRY |VM_FAULT_ERROR ))) { +if (mm_fault_error (regs, error_code, address, fault)) { +vm_fault_cleanup (&vmf);return ; + } + }if (flags &FAULT_FLAG_ALLOW_RETRY ) {if (fault &VM_FAULT_MAJOR ) {
@@ -512,5 +515,6 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem); }
arch/sparc/mm/fault_32.c +7 -1
@@ -239,10 +239,13 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (vma, address, flags); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -275,6 +278,7 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write, } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ;
@@ -412,8 +416,10 @@ static void force_user_fault(unsigned long address, int write)switch (handle_mm_fault (&vmf)) {case VM_FAULT_SIGBUS :case VM_FAULT_OOM : +vm_fault_cleanup (&vmf); goto do_sigbus; } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);return ; bad_area:
arch/sparc/mm/fault_64.c +5 -1
@@ -437,10 +437,13 @@ asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (vma, address, flags); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf); goto exit_exception; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -472,6 +475,7 @@ asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs) goto retry; } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem); mm_rss =get_mm_rss (mm);
arch/um/kernel/trap.c +5 -1
@@ -78,10 +78,13 @@ int handle_page_fault(unsigned long address, unsigned long ip,vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf); goto out_nosemaphore; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) { goto out_of_memory; }else if (fault &VM_FAULT_SIGSEGV ) {
@@ -109,6 +112,7 @@ int handle_page_fault(unsigned long address, unsigned long ip, pud =pud_offset (pgd, address); pmd =pmd_offset (pud, address); pte =pte_offset_kernel (pmd, address); +vm_fault_cleanup (&vmf); }while (!pte_present (*pte)); err =0 ;/*
arch/unicore32/mm/fault.c +4 -1
@@ -262,8 +262,10 @@ static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)* signal first. We do not need to release the mmap_sem because * it would already be released in __lock_page_or_retry in * mm/filemap.c. */ -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return 0 ; + }if (!(fault &VM_FAULT_ERROR ) && (flags &FAULT_FLAG_ALLOW_RETRY )) {if (fault &VM_FAULT_MAJOR )
@@ -278,6 +280,7 @@ static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs) } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);/*
arch/x86/mm/fault.c +2 -0
@@ -1410,6 +1410,7 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,if (!fatal_signal_pending (tsk)) goto retry; } +vm_fault_cleanup (&vmf);/* User mode? Just return to handle the fatal exception */ if (flags &FAULT_FLAG_USER )
@@ -1420,6 +1421,7 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,return ; } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);if (unlikely (fault &VM_FAULT_ERROR )) {mm_fault_error (regs, error_code, address, &pkey, fault);
arch/xtensa/mm/fault.c +5 -1
@@ -112,10 +112,13 @@ void do_page_fault(struct pt_regs *regs)vm_fault_init (&vmf, vma, address, flags); fault =handle_mm_fault (&vmf); -if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) +if ((fault &VM_FAULT_RETRY ) &&fatal_signal_pending (current)) { +vm_fault_cleanup (&vmf);return ; + }if (unlikely (fault &VM_FAULT_ERROR )) { +vm_fault_cleanup (&vmf);if (fault &VM_FAULT_OOM ) goto out_of_memory;else if (fault &VM_FAULT_SIGSEGV )
@@ -142,6 +145,7 @@ void do_page_fault(struct pt_regs *regs) } } +vm_fault_cleanup (&vmf);up_read (&mm->mmap_sem);perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);if (flags &VM_FAULT_MAJOR )
drivers/iommu/amd_iommu_v2.c +1 -0
@@ -535,6 +535,7 @@ static void do_fault(struct work_struct *work)vm_fault_init (&vmf, vma, address, flags); ret =handle_mm_fault (&vmf); +vm_fault_cleanup (&vmf); out:up_read (&mm->mmap_sem);
drivers/iommu/intel-svm.c +1 -0
@@ -640,6 +640,7 @@ static irqreturn_t prq_event_thread(int irq, void *d)vm_fault_init (&vmf, vma, address, req->wr_req ?FAULT_FLAG_WRITE :0 ); ret =handle_mm_fault (&vmf); +vm_fault_cleanup (&vmf);if (ret &VM_FAULT_ERROR ) goto invalid;
include/linux/mm.h +14 -0
@@ -360,6 +360,12 @@ struct vm_fault {* is set (which is also implied by * VM_FAULT_ERROR). */ +struct page *cached_page;/* ->fault handlers that return +* VM_FAULT_RETRY can store their +* previous page here to be reused the +* next time we loop through the fault +* handler for faster lookup. +*/ /* These three entries are valid only while holding ptl lock */ pte_t *pte;/* Pointer to pte entry matching * the 'address'. NULL if the page
@@ -953,6 +959,14 @@ static inline void put_page(struct page *page)__put_page (page); } +static inline void vm_fault_cleanup (struct vm_fault *vmf) +{ +if (vmf->cached_page) { +put_page (vmf->cached_page); + vmf->cached_page =NULL ; + } +} +#if defined(CONFIG_SPARSEMEM ) && !defined(CONFIG_SPARSEMEM_VMEMMAP )#define SECTION_IN_PAGE_FLAGS #endif
mm/filemap.c +28 -3
@@ -2530,13 +2530,38 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)pgoff_t offset = vmf->pgoff;int flags = vmf->flags;pgoff_t max_off; -struct page *page; +struct page *page =NULL ; +struct page *cached_page = vmf->cached_page;vm_fault_t ret =0 ; max_off =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE );if (unlikely (offset >= max_off))return VM_FAULT_SIGBUS ; +/* +* We may have read in the page already and have a page from an earlier +* loop. If so we need to see if this page is still valid, and if not +* do the whole dance over again. +*/ +if (cached_page) { +if (flags &FAULT_FLAG_KILLABLE ) { + error =lock_page_killable (cached_page); +if (error) { +up_read (&mm->mmap_sem); + goto out_retry; + } + }else +lock_page (cached_page); + vmf->cached_page =NULL ; +if (cached_page->mapping == mapping && + cached_page->index == offset) { + page = cached_page; + goto have_cached_page; + } +unlock_page (cached_page); +put_page (cached_page); + } +/* * Do we have something in the page cache already? */
@@ -2587,8 +2612,8 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)put_page (page); goto retry_find; } +have_cached_page:VM_BUG_ON_PAGE (page->index != offset, page); -/* * We have a locked page in the page cache, now we need to check * that it's up-to-date. If not, it is going to be due to an error.
@@ -2677,7 +2702,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)if (fpin)fput (fpin);if (page) -put_page (page); + vmf->cached_page = page;return ret |VM_FAULT_RETRY ; }EXPORT_SYMBOL (filemap_fault);
mm/gup.c +3 -0
@@ -518,6 +518,7 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,vm_fault_init (&vmf, vma, address, fault_flags); ret =handle_mm_fault (&vmf); +vm_fault_cleanup (&vmf);if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret, *flags);
@@ -840,6 +841,7 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,if (ret &VM_FAULT_ERROR ) {int err =vm_fault_to_errno (ret,0 ); +vm_fault_cleanup (&vmf);if (err)return err;BUG ();
@@ -854,6 +856,7 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, goto retry; } } +vm_fault_cleanup (&vmf);if (tsk) {if (major)
mm/hmm.c +1 -0
@@ -309,6 +309,7 @@ static int hmm_vma_do_fault(struct mm_walk *walk, unsigned long addr, flags |= write_fault ?FAULT_FLAG_WRITE :0 ;vm_fault_init (&vmf, vma, addr, flags); ret =handle_mm_fault (&vmf); +vm_fault_cleanup (&vmf);if (ret &VM_FAULT_RETRY )return -EBUSY ;if (ret &VM_FAULT_ERROR ) {
mm/ksm.c +1 -0
@@ -483,6 +483,7 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr)vm_fault_init (&vmf, vma, addr,FAULT_FLAG_WRITE |FAULT_FLAG_REMOTE ); ret =handle_mm_fault (&vmf); +vm_fault_cleanup (&vmf); }else ret =VM_FAULT_WRITE ;put_page (page);
Signature · 1 line
2.14.3
PATCH 7/8 mm: add a flag to indicate we used a cached page
In reply to Josef BacikThis is preparation for dropping the mmap_sem in page_mkwrite. We need to know if we used our cached page so we can be sure it is the page we already did the page_mkwrite stuff on so we don't have to redo all of that work.
--- include/linux/mm.h | 6 +++++- mm/filemap.c | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/mm.h +5 -1
@@ -318,6 +318,9 @@ extern pgprot_t protection_map[16];#define FAULT_FLAG_USER 0x40/* The fault originated in userspace */ #define FAULT_FLAG_REMOTE 0x80/* faulting for non current tsk/mm */ #define FAULT_FLAG_INSTRUCTION 0x100/* The fault was during an instruction fetch */ +#define FAULT_FLAG_USED_CACHED 0x200/* Our vmf->page was from a previous +* loop through the fault handler. +*/ #define FAULT_FLAG_TRACE \ { FAULT_FLAG_WRITE, "WRITE" }, \
@@ -328,7 +331,8 @@ extern pgprot_t protection_map[16]; { FAULT_FLAG_TRIED, "TRIED" }, \ { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ - { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" } + { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ + { FAULT_FLAG_USED_CACHED, "USED_CACHED" }/* * vm_fault is filled by the the pagefault handler and passed to the vma's
mm/filemap.c +4 -1
@@ -2556,6 +2556,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)if (cached_page->mapping == mapping && cached_page->index == offset) { page = cached_page; + vmf->flags |=FAULT_FLAG_USED_CACHED ; goto have_cached_page; }unlock_page (cached_page);
@@ -2618,8 +2619,10 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)* We have a locked page in the page cache, now we need to check * that it's up-to-date. If not, it is going to be due to an error. */ -if (unlikely (!PageUptodate (page))) +if (unlikely (!PageUptodate (page))) { + vmf->flags &= ~(FAULT_FLAG_USED_CACHED ); goto page_not_uptodate; + }/* * Found the page and have a reference on it.
Signature · 1 line
2.14.3
PATCH 8/8 btrfs: drop mmap_sem in mkwrite for btrfs
In reply to Josef Bacik->page_mkwrite is extremely expensive in btrfs. We have to reserve space, which can take 6 lifetimes, and we could possibly have to wait on writeback on the page, another several lifetimes. To avoid this simply drop the mmap_sem if we didn't have the cached page and do all of our work and return the appropriate retry error. If we have the cached page we know we did all the right things to set this page up and we can just carry on.
--- fs/btrfs/inode.c | 40 ++++++++++++++++++++++++++++++++++++++-- include/linux/mm.h | 14 ++++++++++++++ mm/filemap.c | 3 ++- 3 files changed, 54 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +38 -2
@@ -8809,7 +8809,9 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,vm_fault_t btrfs_page_mkwrite (struct vm_fault *vmf) {struct page *page = vmf->page; -struct inode *inode =file_inode (vmf->vma->vm_file); +struct file *file = vmf->vma->vm_file, *fpin; +struct mm_struct *mm = vmf->vma->vm_mm; +struct inode *inode =file_inode (file);struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);struct extent_io_tree *io_tree = &BTRFS_I (inode)->io_tree;struct btrfs_ordered_extent *ordered;
@@ -8828,6 +8830,29 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) reserved_space =PAGE_SIZE ; +/* +* We have our cached page from a previous mkwrite, check it to make +* sure it's still dirty and our file size matches when we ran mkwrite +* the last time. If everything is OK then return VM_FAULT_LOCKED, +* otherwise do the mkwrite again. +*/ +if (vmf->flags &FAULT_FLAG_USED_CACHED ) { +lock_page (page); +if (vmf->cached_size ==i_size_read (inode) && +PageDirty (page)) +return VM_FAULT_LOCKED ; +unlock_page (page); + } + +/* +* mkwrite is extremely expensive, and we are holding the mmap_sem +* during this, which means we can starve out anybody trying to +* down_write(mmap_sem) for a long while, especially if we throw cgroups +* into the mix. So just drop the mmap_sem and do all of our work, +* we'll loop back through and verify everything is ok the next time and +* hopefully avoid doing the work twice. +*/ + fpin =maybe_unlock_mmap_for_io (vmf->vma, vmf->flags);sb_start_pagefault (inode->i_sb); page_start =page_offset (page); page_end = page_start +PAGE_SIZE -1 ;
@@ -8844,7 +8869,7 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) ret2 =btrfs_delalloc_reserve_space (inode, &data_reserved, page_start, reserved_space);if (!ret2) { - ret2 =file_update_time (vmf->vma->vm_file); + ret2 =file_update_time (file); reserved =1 ; }if (ret2) {
@@ -8943,6 +8968,13 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)btrfs_delalloc_release_extents (BTRFS_I (inode),PAGE_SIZE , true);sb_end_pagefault (inode->i_sb);extent_changeset_free (data_reserved); +if (fpin) { +unlock_page (page); +fput (fpin); + vmf->cached_size = size; +down_read (&mm->mmap_sem); +return VM_FAULT_RETRY ; + }return VM_FAULT_LOCKED ; }
@@ -8955,6 +8987,10 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf) out_noreserve:sb_end_pagefault (inode->i_sb);extent_changeset_free (data_reserved); +if (fpin) { +fput (fpin); +down_read (&mm->mmap_sem); + }return ret; }
include/linux/mm.h +14 -0
@@ -370,6 +370,13 @@ struct vm_fault {* next time we loop through the fault * handler for faster lookup. */ +loff_t cached_size;/* ->page_mkwrite handlers may drop +* the mmap_sem to avoid starvation, in +* which case they need to save the +* i_size in order to verify the cached +* page we're using the next loop +* through hasn't changed under us. +*/ /* These three entries are valid only while holding ptl lock */ pte_t *pte;/* Pointer to pte entry matching * the 'address'. NULL if the page
@@ -1435,6 +1442,8 @@ extern vm_fault_t handle_mm_fault(struct vm_fault *vmf);extern int fixup_user_fault (struct task_struct *tsk,struct mm_struct *mm,unsigned long address,unsigned int fault_flags,bool *unlocked); +extern struct file *maybe_unlock_mmap_for_io (struct vm_area_struct *vma, +int flags);void unmap_mapping_pages (struct address_space *mapping,pgoff_t start,pgoff_t nr,bool even_cows); void unmap_mapping_range(struct address_space *mapping,
@@ -1454,6 +1463,11 @@ static inline int fixup_user_fault(struct task_struct *tsk,BUG ();return -EFAULT ; } +stiatc inline struct file *maybe_unlock_mmap_for_io(struct vm_area_struct *vma, +int flags) +{ +return NULL ; +}static inline void unmap_mapping_pages (struct address_space *mapping,pgoff_t start,pgoff_t nr,bool even_cows) { } static inline void unmap_mapping_range(struct address_space *mapping,
mm/filemap.c +2 -1
@@ -2366,7 +2366,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)EXPORT_SYMBOL (generic_file_read_iter);#ifdef CONFIG_MMU -static struct file *maybe_unlock_mmap_for_io (struct vm_area_struct *vma,int flags) +struct file *maybe_unlock_mmap_for_io (struct vm_area_struct *vma,int flags) {if ((flags & (FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_RETRY_NOWAIT )) ==FAULT_FLAG_ALLOW_RETRY ) {struct file *file;
@@ -2377,6 +2377,7 @@ static struct file *maybe_unlock_mmap_for_io(struct vm_area_struct *vma, int fla }return NULL ; } +EXPORT_SYMBOL_GPL (maybe_unlock_mmap_for_io);/** * page_cache_read - adds requested page to the page cache if not already there
Signature · 1 line
2.14.3
Re: [PATCH 1/8] mm: push vm_fault into the page fault handlers
In reply to Josef BacikQuoted 52 lines · Josef Bacik
In preparation for caching pages during filemap faults we need to push the struct
vm_faultup a level into the arch page fault handlers, since they are the ones responsible for retrying if we unlock themmap_sem.Signed-off-by Josef Bacik <[email protected]>--- arch/alpha/mm/fault.c | 4 ++- arch/arc/mm/fault.c | 2 ++ arch/arm/mm/fault.c | 18 ++++++++----- arch/arm64/mm/fault.c | 18 +++++++------ arch/hexagon/mm/vm_fault.c | 4 ++- arch/ia64/mm/fault.c | 4 ++- arch/m68k/mm/fault.c | 5 ++-- arch/microblaze/mm/fault.c | 4 ++- arch/mips/mm/fault.c | 4 ++- arch/nds32/mm/fault.c | 5 ++-- arch/nios2/mm/fault.c | 4 ++- arch/openrisc/mm/fault.c | 5 ++-- arch/parisc/mm/fault.c | 5 ++-- arch/powerpc/mm/copro_fault.c | 4 ++- arch/powerpc/mm/fault.c | 4 ++- arch/riscv/mm/fault.c | 2 ++ arch/s390/mm/fault.c | 4 ++- arch/sh/mm/fault.c | 4 ++- arch/sparc/mm/fault_32.c | 6 ++++- arch/sparc/mm/fault_64.c | 2 ++ arch/um/kernel/trap.c | 4 ++- arch/unicore32/mm/fault.c | 17 +++++++----- arch/x86/mm/fault.c | 4 ++- arch/xtensa/mm/fault.c | 4 ++- drivers/iommu/amd_iommu_v2.c | 4 ++- drivers/iommu/intel-svm.c | 6 +++-- include/linux/mm.h | 16 +++++++++--- mm/gup.c | 8 ++++-- mm/hmm.c | 4 ++- mm/ksm.c | 10 ++++--- mm/memory.c | 61 +++++++++++++++++++++---------------------- 31 files changed, 157 insertions(+), 89 deletions(-)arch/alpha/mm/fault.c +1
-0@@ -84,6 +84,7 @@ asmlinkage void do_page_fault(unsigned long address, unsigned long mmcsr, long cause, struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct * vma;struct mm_struct *mm = current->mm;const struct exception_table_entry *fixup;
arch/alpha/mm/fault.c do_page_fault(unsigned long address, unsigned long mmcsr, · line 153
Quoted 6 lines
arch/alpha/mm/fault.c +2
-1@@ -148,7 +149,8 @@ do_page_fault(unsigned long address, unsigned long mmcsr,/* If for any reason at all we couldn't handle the fault, make sure we exit gracefully rather than endlessly redo the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmfs, vma, flags, address); + fault =handle_mm_fault (&vmf);
Doesn't compile.
arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) · line 228
Quoted 9 lines
arch/arm/mm/fault.c +1
-2@@ -225,17 +225,17 @@ static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) } static vm_fault_t __kprobes -__do_page_fault (struct mm_struct *mm,unsigned long addr,unsigned int fsr, -unsigned int flags,struct task_struct *tsk) +__do_page_fault(struct mm_struct *mm, struct vm_fault *vm, unsigned int fsr,
vm_fault is *vm....
arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) · line 234
Quoted 7 lines
arch/arm/mm/fault.c +2
-1+ struct task_struct *tsk) {struct vm_area_struct *vma;vm_fault_t fault; - vma =find_vma (mm, addr); + vma =find_vma (mm, vmf->address);
So this doesn't compile.
Quoted 16 lines
arch/arm/mm/fault.c +2
-1check_stack: -if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, addr)) +if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, vmf->address)) goto good_area; out:return fault;@@ -424,6 +424,7 @@ static bool is_el0_instruction_abort(unsigned int esr)static int __kprobes do_page_fault (unsigned long addr,unsigned int esr,struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;struct siginfo si;
arch/arm/mm/fault.c static int __kprobes do_page_fault(unsigned long addr, unsigned int esr, · line 498
Quoted 6 lines
arch/arm/mm/fault.c +2
-1@@ -493,7 +494,8 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,#endif } - fault =__do_page_fault (mm, addr, mm_flags, vm_flags, tsk); +vm_fault_init (&vmf,NULL , addr, mm_flags); + fault =__do_page_fault (mm, vmf, vm_flags, tsk);
I'm betting this doesn't compile, either.
/me stops looking.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 8/8] btrfs: drop mmap_sem in mkwrite for btrfs
In reply to Josef Bacikinclude/linux/mm.h static inline int fixup_user_fault(struct task_struct *tsk, · line 1470
Quoted 9 lines · Josef Bacik
include/linux/mm.h +5
-0@@ -1454,6 +1463,11 @@ static inline int fixup_user_fault(struct task_struct *tsk,BUG ();return -EFAULT ; } +stiatc inline struct file *maybe_unlock_mmap_for_io(struct vm_area_struct *vma, +int flags) +{ +return NULL ; +}
This doesn't compile either.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 1/8] mm: push vm_fault into the page fault handlers
In reply to Dave Chinner(Only have my iPhone, sorry)
Hey it compiled on x86 and passed xfstests, good enough for an RFC. I thought kbuild testbot did these and since it hadn’t emailed me yet I assumed I didn’t break anything (a bad assumption I know). I’ll go through more thoroughly, but it for sure compiles on x86. Thanks,
Josef
Quoted 126 lines · Dave Chinner
Quoted 29 lines · Josef Bacik
In preparation for caching pages during filemap faults we need to push the struct
vm_faultup a level into the arch page fault handlers, since they are the ones responsible for retrying if we unlock themmap_sem.Signed-off-by Josef Bacik <[email protected]>--- arch/alpha/mm/fault.c | 4 ++- arch/arc/mm/fault.c | 2 ++ arch/arm/mm/fault.c | 18 ++++++++----- arch/arm64/mm/fault.c | 18 +++++++------ arch/hexagon/mm/vm_fault.c | 4 ++- arch/ia64/mm/fault.c | 4 ++- arch/m68k/mm/fault.c | 5 ++-- arch/microblaze/mm/fault.c | 4 ++- arch/mips/mm/fault.c | 4 ++- arch/nds32/mm/fault.c | 5 ++-- arch/nios2/mm/fault.c | 4 ++- arch/openrisc/mm/fault.c | 5 ++-- arch/parisc/mm/fault.c | 5 ++-- arch/powerpc/mm/copro_fault.c | 4 ++- arch/powerpc/mm/fault.c | 4 ++- arch/riscv/mm/fault.c | 2 ++ arch/s390/mm/fault.c | 4 ++- arch/sh/mm/fault.c | 4 ++- arch/sparc/mm/fault_32.c | 6 ++++- arch/sparc/mm/fault_64.c | 2 ++ arch/um/kernel/trap.c | 4 ++- arch/unicore32/mm/fault.c | 17 +++++++----- arch/x86/mm/fault.c | 4 ++-Quoted 8 lines
arch/xtensa/mm/fault.c | 4 ++- drivers/iommu/amd_iommu_v2.c | 4 ++- drivers/iommu/intel-svm.c | 6 +++-- include/linux/mm.h | 16 +++++++++--- mm/gup.c | 8 ++++-- mm/hmm.c | 4 ++- mm/ksm.c | 10 ++++--- mm/memory.c | 61+++++++++++++++++++++----------------------
arch/alpha/mm/fault.c do_page_fault(unsigned long address, unsigned long
Quoted 15 lines
31 files changed, 157 insertions(+), 89 deletions(-)arch/alpha/mm/fault.c +1
-0@@ -84,6 +84,7 @@ asmlinkage void do_page_fault(unsigned long address, unsigned long mmcsr, long cause, struct pt_regs *regs) { +struct vm_fault vmf = {};struct vm_area_struct * vma;struct mm_struct *mm = current->mm;const struct exception_table_entry *fixup;@@ -148,7 +149,8 @@ do_page_fault(unsigned long address, unsigned longmmcsr,
arch/alpha/mm/fault.c do_page_fault(unsigned long address, unsigned long mmcsr, · line 153
Quoted 6 lines
arch/alpha/mm/fault.c +2
-1/* If for any reason at all we couldn't handle the fault, make sure we exit gracefully rather than endlessly redo the fault. */ - fault =handle_mm_fault (vma, address, flags); +vm_fault_init (&vmfs, vma, flags, address); + fault =handle_mm_fault (&vmf);Doesn't compile.
arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr,
Quoted 3 lines
arch/arm/mm/fault.c +0
-0@@ -225,17 +225,17 @@ static inline bool access_error(unsigned int fsr,struct
vm_area_struct*vma)arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
Quoted 4 lines
arch/arm/mm/fault.c +0
-1} static vm_fault_t __kprobes -__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned intfsr,
arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, · line 228
Quoted 2 lines
arch/arm/mm/fault.c +1
-1- unsigned int flags, struct task_struct *tsk) +__do_page_fault(struct mm_struct *mm, struct vm_fault *vm, unsigned intfsr,
vm_faultis *vm....arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) · line 234
Quoted 7 lines
arch/arm/mm/fault.c +2
-1+ struct task_struct *tsk) {struct vm_area_struct *vma;vm_fault_t fault; - vma =find_vma (mm, addr); + vma =find_vma (mm, vmf->address);So this doesn't compile.
arch/arm/mm/fault.c static inline bool access_error(unsigned int fsr, · line 236
Quoted 4 lines
arch/arm/mm/fault.c +1
-1check_stack: -if (vma->vm_flags &VM_GROWSDOWN && !expand_stack (vma, addr)) + if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma,
vmf->address))arch/arm/mm/fault.c static bool is_el0_instruction_abort(unsigned int
Quoted 4 lines
arch/arm/mm/fault.c +0
-0goto good_area; out:return fault;@@ -424,6 +424,7 @@ static bool is_el0_instruction_abort(unsigned intesr)
arch/arm/mm/fault.c static int __kprobes do_page_fault(unsigned long
Quoted 8 lines
arch/arm/mm/fault.c +1
-0static int __kprobes do_page_fault (unsigned long addr,unsigned int esr,struct pt_regs *regs) { +struct vm_fault vmf = {};struct task_struct *tsk;struct mm_struct *mm;struct siginfo si;@@ -493,7 +494,8 @@ static int __kprobes do_page_fault(unsigned longaddr, unsigned int esr,
arch/arm/mm/fault.c static int __kprobes do_page_fault(unsigned long · line 498
Quoted 6 lines
arch/arm/mm/fault.c +2
-1#endif } - fault =__do_page_fault (mm, addr, mm_flags, vm_flags, tsk); +vm_fault_init (&vmf,NULL , addr, mm_flags); + fault =__do_page_fault (mm, vmf, vm_flags, tsk);I'm betting this doesn't compile, either.
/me stops looking.
Cheers,
Dave.
--
Dave Chinner
[email protected]