Skip to content

Commit

Permalink
mmap locking API: use coccinelle to convert mmap_sem rwsem call sites
Browse files Browse the repository at this point in the history
This change converts the existing mmap_sem rwsem calls to use the new mmap
locking API instead.

The change is generated using coccinelle with the following rule:

// spatch --sp-file mmap_lock_api.cocci --in-place --include-headers --dir .

@@
expression mm;
@@
(
-init_rwsem
+mmap_init_lock
|
-down_write
+mmap_write_lock
|
-down_write_killable
+mmap_write_lock_killable
|
-down_write_trylock
+mmap_write_trylock
|
-up_write
+mmap_write_unlock
|
-downgrade_write
+mmap_write_downgrade
|
-down_read
+mmap_read_lock
|
-down_read_killable
+mmap_read_lock_killable
|
-down_read_trylock
+mmap_read_trylock
|
-up_read
+mmap_read_unlock
)
-(&mm->mmap_sem)
+(mm)

Signed-off-by: Michel Lespinasse <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Reviewed-by: Daniel Jordan <[email protected]>
Reviewed-by: Laurent Dufour <[email protected]>
Reviewed-by: Vlastimil Babka <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
Cc: Jerome Glisse <[email protected]>
Cc: John Hubbard <[email protected]>
Cc: Liam Howlett <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Ying Han <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
walken-google authored and torvalds committed Jun 9, 2020
1 parent 0adf65f commit d8ed45c
Show file tree
Hide file tree
Showing 148 changed files with 645 additions and 645 deletions.
4 changes: 2 additions & 2 deletions arch/alpha/kernel/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,12 +957,12 @@ do_entUnaUser(void __user * va, unsigned long opcode,
si_code = SEGV_ACCERR;
else {
struct mm_struct *mm = current->mm;
down_read(&mm->mmap_sem);
mmap_read_lock(mm);
if (find_vma(mm, (unsigned long)va))
si_code = SEGV_ACCERR;
else
si_code = SEGV_MAPERR;
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);
}
send_sig_fault(SIGSEGV, si_code, va, 0, current);
return;
Expand Down
10 changes: 5 additions & 5 deletions arch/alpha/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ do_page_fault(unsigned long address, unsigned long mmcsr,
if (user_mode(regs))
flags |= FAULT_FLAG_USER;
retry:
down_read(&mm->mmap_sem);
mmap_read_lock(mm);
vma = find_vma(mm, address);
if (!vma)
goto bad_area;
Expand Down Expand Up @@ -180,14 +180,14 @@ do_page_fault(unsigned long address, unsigned long mmcsr,
}
}

up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

return;

/* Something tried to access memory that isn't in our memory map.
Fix it, but check if it's kernel or user first. */
bad_area:
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

if (user_mode(regs))
goto do_sigsegv;
Expand All @@ -211,14 +211,14 @@ do_page_fault(unsigned long address, unsigned long mmcsr,
/* We ran out of memory, or some other thing happened to us that
made us unable to handle the page fault gracefully. */
out_of_memory:
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);
if (!user_mode(regs))
goto no_context;
pagefault_out_of_memory();
return;

do_sigbus:
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);
/* Send a sigbus, regardless of whether we were in kernel
or user mode. */
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address, 0);
Expand Down
4 changes: 2 additions & 2 deletions arch/arc/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)
if (unlikely(ret != -EFAULT))
goto fail;

down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
FAULT_FLAG_WRITE, NULL);
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);

if (likely(!ret))
goto again;
Expand Down
4 changes: 2 additions & 2 deletions arch/arc/kernel/troubleshoot.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void show_faulting_vma(unsigned long address)
/* can't use print_vma_addr() yet as it doesn't check for
* non-inclusive vma
*/
down_read(&active_mm->mmap_sem);
mmap_read_lock(active_mm);
vma = find_vma(active_mm, address);

/* check against the find_vma( ) behaviour which returns the next VMA
Expand All @@ -111,7 +111,7 @@ static void show_faulting_vma(unsigned long address)
} else
pr_info(" @No matching VMA found\n");

up_read(&active_mm->mmap_sem);
mmap_read_unlock(active_mm);
}

static void show_ecr_verbose(struct pt_regs *regs)
Expand Down
4 changes: 2 additions & 2 deletions arch/arc/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
flags |= FAULT_FLAG_WRITE;

retry:
down_read(&mm->mmap_sem);
mmap_read_lock(mm);

vma = find_vma(mm, address);
if (!vma)
Expand Down Expand Up @@ -150,7 +150,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
}

bad_area:
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

/*
* Major/minor page fault accounting
Expand Down
4 changes: 2 additions & 2 deletions arch/arm/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
npages = 1; /* for sigpage */
npages += vdso_total_pages;

if (down_write_killable(&mm->mmap_sem))
if (mmap_write_lock_killable(mm))
return -EINTR;
hint = sigpage_addr(mm, npages);
addr = get_unmapped_area(NULL, hint, npages << PAGE_SHIFT, 0, 0);
Expand All @@ -458,7 +458,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
arm_install_vdso(mm, addr + PAGE_SIZE);

up_fail:
up_write(&mm->mmap_sem);
mmap_write_unlock(mm);
return ret;
}
#endif
4 changes: 2 additions & 2 deletions arch/arm/kernel/swp_emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ static void set_segfault(struct pt_regs *regs, unsigned long addr)
{
int si_code;

down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
if (find_vma(current->mm, addr) == NULL)
si_code = SEGV_MAPERR;
else
si_code = SEGV_ACCERR;
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);

pr_debug("SWP{B} emulation: access caused memory abort!\n");
arm_notify_die("Illegal memory access", regs,
Expand Down
16 changes: 8 additions & 8 deletions arch/arm/lib/uaccess_with_memcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
atomic = faulthandler_disabled();

if (!atomic)
down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
while (n) {
pte_t *pte;
spinlock_t *ptl;
int tocopy;

while (!pin_page_for_write(to, &pte, &ptl)) {
if (!atomic)
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);
if (__put_user(0, (char __user *)to))
goto out;
if (!atomic)
down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
}

tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
Expand All @@ -133,7 +133,7 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
spin_unlock(ptl);
}
if (!atomic)
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);

out:
return n;
Expand Down Expand Up @@ -170,17 +170,17 @@ __clear_user_memset(void __user *addr, unsigned long n)
return 0;
}

down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
while (n) {
pte_t *pte;
spinlock_t *ptl;
int tocopy;

while (!pin_page_for_write(addr, &pte, &ptl)) {
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);
if (__put_user(0, (char __user *)addr))
goto out;
down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
}

tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
Expand All @@ -198,7 +198,7 @@ __clear_user_memset(void __user *addr, unsigned long n)
else
spin_unlock(ptl);
}
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);

out:
return n;
Expand Down
6 changes: 3 additions & 3 deletions arch/arm/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
* validly references user space from well defined areas of the code,
* we can bug out early if this is from code which shouldn't.
*/
if (!down_read_trylock(&mm->mmap_sem)) {
if (!mmap_read_trylock(mm)) {
if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
goto no_context;
retry:
down_read(&mm->mmap_sem);
mmap_read_lock(mm);
} else {
/*
* The above down_read_trylock() might have succeeded in
Expand Down Expand Up @@ -325,7 +325,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
}
}

up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

/*
* Handle the "normal" case first - VM_FAULT_MAJOR
Expand Down
4 changes: 2 additions & 2 deletions arch/arm64/kernel/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,12 @@ void arm64_notify_segfault(unsigned long addr)
{
int code;

down_read(&current->mm->mmap_sem);
mmap_read_lock(current->mm);
if (find_vma(current->mm, addr) == NULL)
code = SEGV_MAPERR;
else
code = SEGV_ACCERR;
up_read(&current->mm->mmap_sem);
mmap_read_unlock(current->mm);

force_signal_inject(SIGSEGV, code, addr);
}
Expand Down
8 changes: 4 additions & 4 deletions arch/arm64/kernel/vdso.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
struct mm_struct *mm = current->mm;
int ret;

if (down_write_killable(&mm->mmap_sem))
if (mmap_write_lock_killable(mm))
return -EINTR;

ret = aarch32_kuser_helpers_setup(mm);
Expand All @@ -357,7 +357,7 @@ int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
#endif /* CONFIG_COMPAT_VDSO */

out:
up_write(&mm->mmap_sem);
mmap_write_unlock(mm);
return ret;
}
#endif /* CONFIG_COMPAT */
Expand Down Expand Up @@ -398,15 +398,15 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
struct mm_struct *mm = current->mm;
int ret;

if (down_write_killable(&mm->mmap_sem))
if (mmap_write_lock_killable(mm))
return -EINTR;

ret = __setup_additional_pages(VDSO_ABI_AA64,
mm,
bprm,
uses_interp);

up_write(&mm->mmap_sem);
mmap_write_unlock(mm);

return ret;
}
8 changes: 4 additions & 4 deletions arch/arm64/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,11 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
* validly references user space from well defined areas of the code,
* we can bug out early if this is from code which shouldn't.
*/
if (!down_read_trylock(&mm->mmap_sem)) {
if (!mmap_read_trylock(mm)) {
if (!user_mode(regs) && !search_exception_tables(regs->pc))
goto no_context;
retry:
down_read(&mm->mmap_sem);
mmap_read_lock(mm);
} else {
/*
* The above down_read_trylock() might have succeeded in which
Expand All @@ -510,7 +510,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
might_sleep();
#ifdef CONFIG_DEBUG_VM
if (!user_mode(regs) && !search_exception_tables(regs->pc)) {
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);
goto no_context;
}
#endif
Expand All @@ -532,7 +532,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
goto retry;
}
}
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

/*
* Handle the "normal" (no error) case first.
Expand Down
4 changes: 2 additions & 2 deletions arch/csky/kernel/vdso.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
unsigned long addr;
struct mm_struct *mm = current->mm;

down_write(&mm->mmap_sem);
mmap_write_lock(mm);

addr = get_unmapped_area(NULL, STACK_TOP, PAGE_SIZE, 0, 0);
if (IS_ERR_VALUE(addr)) {
Expand All @@ -70,7 +70,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
mm->context.vdso = (void *)addr;

up_fail:
up_write(&mm->mmap_sem);
mmap_write_unlock(mm);
return ret;
}

Expand Down
8 changes: 4 additions & 4 deletions arch/csky/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
if (in_atomic() || !mm)
goto bad_area_nosemaphore;

down_read(&mm->mmap_sem);
mmap_read_lock(mm);
vma = find_vma(mm, address);
if (!vma)
goto bad_area;
Expand Down Expand Up @@ -170,15 +170,15 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
address);
}

up_read(&mm->mmap_sem);
mmap_read_unlock(mm);
return;

/*
* Something tried to access memory that isn't in our memory map..
* Fix it, but check if it's kernel or user first..
*/
bad_area:
up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

bad_area_nosemaphore:
/* User mode accesses just cause a SIGSEGV */
Expand Down Expand Up @@ -217,7 +217,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
do_sigbus:
tsk->thread.trap_no = (regs->sr >> 16) & 0xff;

up_read(&mm->mmap_sem);
mmap_read_unlock(mm);

/* Kernel mode? Handle exceptions or die */
if (!user_mode(regs))
Expand Down
4 changes: 2 additions & 2 deletions arch/hexagon/kernel/vdso.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
unsigned long vdso_base;
struct mm_struct *mm = current->mm;

if (down_write_killable(&mm->mmap_sem))
if (mmap_write_lock_killable(mm))
return -EINTR;

/* Try to get it loaded right near ld.so/glibc. */
Expand All @@ -76,7 +76,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
mm->context.vdso = (void *)vdso_base;

up_fail:
up_write(&mm->mmap_sem);
mmap_write_unlock(mm);
return ret;
}

Expand Down
Loading

0 comments on commit d8ed45c

Please sign in to comment.