Skip to content

Commit d8ed45c

Browse files
walken-googletorvalds
authored andcommitted
mmap locking API: use coccinelle to convert mmap_sem rwsem call sites
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]>
1 parent 0adf65f commit d8ed45c

File tree

148 files changed

+645
-645
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+645
-645
lines changed

arch/alpha/kernel/traps.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,12 +957,12 @@ do_entUnaUser(void __user * va, unsigned long opcode,
957957
si_code = SEGV_ACCERR;
958958
else {
959959
struct mm_struct *mm = current->mm;
960-
down_read(&mm->mmap_sem);
960+
mmap_read_lock(mm);
961961
if (find_vma(mm, (unsigned long)va))
962962
si_code = SEGV_ACCERR;
963963
else
964964
si_code = SEGV_MAPERR;
965-
up_read(&mm->mmap_sem);
965+
mmap_read_unlock(mm);
966966
}
967967
send_sig_fault(SIGSEGV, si_code, va, 0, current);
968968
return;

arch/alpha/mm/fault.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ do_page_fault(unsigned long address, unsigned long mmcsr,
117117
if (user_mode(regs))
118118
flags |= FAULT_FLAG_USER;
119119
retry:
120-
down_read(&mm->mmap_sem);
120+
mmap_read_lock(mm);
121121
vma = find_vma(mm, address);
122122
if (!vma)
123123
goto bad_area;
@@ -180,14 +180,14 @@ do_page_fault(unsigned long address, unsigned long mmcsr,
180180
}
181181
}
182182

183-
up_read(&mm->mmap_sem);
183+
mmap_read_unlock(mm);
184184

185185
return;
186186

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

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

220220
do_sigbus:
221-
up_read(&mm->mmap_sem);
221+
mmap_read_unlock(mm);
222222
/* Send a sigbus, regardless of whether we were in kernel
223223
or user mode. */
224224
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address, 0);

arch/arc/kernel/process.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ SYSCALL_DEFINE3(arc_usr_cmpxchg, int *, uaddr, int, expected, int, new)
9090
if (unlikely(ret != -EFAULT))
9191
goto fail;
9292

93-
down_read(&current->mm->mmap_sem);
93+
mmap_read_lock(current->mm);
9494
ret = fixup_user_fault(current, current->mm, (unsigned long) uaddr,
9595
FAULT_FLAG_WRITE, NULL);
96-
up_read(&current->mm->mmap_sem);
96+
mmap_read_unlock(current->mm);
9797

9898
if (likely(!ret))
9999
goto again;

arch/arc/kernel/troubleshoot.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void show_faulting_vma(unsigned long address)
8989
/* can't use print_vma_addr() yet as it doesn't check for
9090
* non-inclusive vma
9191
*/
92-
down_read(&active_mm->mmap_sem);
92+
mmap_read_lock(active_mm);
9393
vma = find_vma(active_mm, address);
9494

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

114-
up_read(&active_mm->mmap_sem);
114+
mmap_read_unlock(active_mm);
115115
}
116116

117117
static void show_ecr_verbose(struct pt_regs *regs)

arch/arc/mm/fault.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
107107
flags |= FAULT_FLAG_WRITE;
108108

109109
retry:
110-
down_read(&mm->mmap_sem);
110+
mmap_read_lock(mm);
111111

112112
vma = find_vma(mm, address);
113113
if (!vma)
@@ -150,7 +150,7 @@ void do_page_fault(unsigned long address, struct pt_regs *regs)
150150
}
151151

152152
bad_area:
153-
up_read(&mm->mmap_sem);
153+
mmap_read_unlock(mm);
154154

155155
/*
156156
* Major/minor page fault accounting

arch/arm/kernel/process.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
431431
npages = 1; /* for sigpage */
432432
npages += vdso_total_pages;
433433

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

460460
up_fail:
461-
up_write(&mm->mmap_sem);
461+
mmap_write_unlock(mm);
462462
return ret;
463463
}
464464
#endif

arch/arm/kernel/swp_emulate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ static void set_segfault(struct pt_regs *regs, unsigned long addr)
9797
{
9898
int si_code;
9999

100-
down_read(&current->mm->mmap_sem);
100+
mmap_read_lock(current->mm);
101101
if (find_vma(current->mm, addr) == NULL)
102102
si_code = SEGV_MAPERR;
103103
else
104104
si_code = SEGV_ACCERR;
105-
up_read(&current->mm->mmap_sem);
105+
mmap_read_unlock(current->mm);
106106

107107
pr_debug("SWP{B} emulation: access caused memory abort!\n");
108108
arm_notify_die("Illegal memory access", regs,

arch/arm/lib/uaccess_with_memcpy.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
101101
atomic = faulthandler_disabled();
102102

103103
if (!atomic)
104-
down_read(&current->mm->mmap_sem);
104+
mmap_read_lock(current->mm);
105105
while (n) {
106106
pte_t *pte;
107107
spinlock_t *ptl;
108108
int tocopy;
109109

110110
while (!pin_page_for_write(to, &pte, &ptl)) {
111111
if (!atomic)
112-
up_read(&current->mm->mmap_sem);
112+
mmap_read_unlock(current->mm);
113113
if (__put_user(0, (char __user *)to))
114114
goto out;
115115
if (!atomic)
116-
down_read(&current->mm->mmap_sem);
116+
mmap_read_lock(current->mm);
117117
}
118118

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

138138
out:
139139
return n;
@@ -170,17 +170,17 @@ __clear_user_memset(void __user *addr, unsigned long n)
170170
return 0;
171171
}
172172

173-
down_read(&current->mm->mmap_sem);
173+
mmap_read_lock(current->mm);
174174
while (n) {
175175
pte_t *pte;
176176
spinlock_t *ptl;
177177
int tocopy;
178178

179179
while (!pin_page_for_write(addr, &pte, &ptl)) {
180-
up_read(&current->mm->mmap_sem);
180+
mmap_read_unlock(current->mm);
181181
if (__put_user(0, (char __user *)addr))
182182
goto out;
183-
down_read(&current->mm->mmap_sem);
183+
mmap_read_lock(current->mm);
184184
}
185185

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

203203
out:
204204
return n;

arch/arm/mm/fault.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
271271
* validly references user space from well defined areas of the code,
272272
* we can bug out early if this is from code which shouldn't.
273273
*/
274-
if (!down_read_trylock(&mm->mmap_sem)) {
274+
if (!mmap_read_trylock(mm)) {
275275
if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
276276
goto no_context;
277277
retry:
278-
down_read(&mm->mmap_sem);
278+
mmap_read_lock(mm);
279279
} else {
280280
/*
281281
* The above down_read_trylock() might have succeeded in
@@ -325,7 +325,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
325325
}
326326
}
327327

328-
up_read(&mm->mmap_sem);
328+
mmap_read_unlock(mm);
329329

330330
/*
331331
* Handle the "normal" case first - VM_FAULT_MAJOR

arch/arm64/kernel/traps.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,12 @@ void arm64_notify_segfault(unsigned long addr)
448448
{
449449
int code;
450450

451-
down_read(&current->mm->mmap_sem);
451+
mmap_read_lock(current->mm);
452452
if (find_vma(current->mm, addr) == NULL)
453453
code = SEGV_MAPERR;
454454
else
455455
code = SEGV_ACCERR;
456-
up_read(&current->mm->mmap_sem);
456+
mmap_read_unlock(current->mm);
457457

458458
force_signal_inject(SIGSEGV, code, addr);
459459
}

arch/arm64/kernel/vdso.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ int aarch32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
340340
struct mm_struct *mm = current->mm;
341341
int ret;
342342

343-
if (down_write_killable(&mm->mmap_sem))
343+
if (mmap_write_lock_killable(mm))
344344
return -EINTR;
345345

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

359359
out:
360-
up_write(&mm->mmap_sem);
360+
mmap_write_unlock(mm);
361361
return ret;
362362
}
363363
#endif /* CONFIG_COMPAT */
@@ -398,15 +398,15 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
398398
struct mm_struct *mm = current->mm;
399399
int ret;
400400

401-
if (down_write_killable(&mm->mmap_sem))
401+
if (mmap_write_lock_killable(mm))
402402
return -EINTR;
403403

404404
ret = __setup_additional_pages(VDSO_ABI_AA64,
405405
mm,
406406
bprm,
407407
uses_interp);
408408

409-
up_write(&mm->mmap_sem);
409+
mmap_write_unlock(mm);
410410

411411
return ret;
412412
}

arch/arm64/mm/fault.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,11 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
497497
* validly references user space from well defined areas of the code,
498498
* we can bug out early if this is from code which shouldn't.
499499
*/
500-
if (!down_read_trylock(&mm->mmap_sem)) {
500+
if (!mmap_read_trylock(mm)) {
501501
if (!user_mode(regs) && !search_exception_tables(regs->pc))
502502
goto no_context;
503503
retry:
504-
down_read(&mm->mmap_sem);
504+
mmap_read_lock(mm);
505505
} else {
506506
/*
507507
* The above down_read_trylock() might have succeeded in which
@@ -510,7 +510,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
510510
might_sleep();
511511
#ifdef CONFIG_DEBUG_VM
512512
if (!user_mode(regs) && !search_exception_tables(regs->pc)) {
513-
up_read(&mm->mmap_sem);
513+
mmap_read_unlock(mm);
514514
goto no_context;
515515
}
516516
#endif
@@ -532,7 +532,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
532532
goto retry;
533533
}
534534
}
535-
up_read(&mm->mmap_sem);
535+
mmap_read_unlock(mm);
536536

537537
/*
538538
* Handle the "normal" (no error) case first.

arch/csky/kernel/vdso.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
5050
unsigned long addr;
5151
struct mm_struct *mm = current->mm;
5252

53-
down_write(&mm->mmap_sem);
53+
mmap_write_lock(mm);
5454

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

7272
up_fail:
73-
up_write(&mm->mmap_sem);
73+
mmap_write_unlock(mm);
7474
return ret;
7575
}
7676

arch/csky/mm/fault.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
120120
if (in_atomic() || !mm)
121121
goto bad_area_nosemaphore;
122122

123-
down_read(&mm->mmap_sem);
123+
mmap_read_lock(mm);
124124
vma = find_vma(mm, address);
125125
if (!vma)
126126
goto bad_area;
@@ -170,15 +170,15 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
170170
address);
171171
}
172172

173-
up_read(&mm->mmap_sem);
173+
mmap_read_unlock(mm);
174174
return;
175175

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

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

220-
up_read(&mm->mmap_sem);
220+
mmap_read_unlock(mm);
221221

222222
/* Kernel mode? Handle exceptions or die */
223223
if (!user_mode(regs))

arch/hexagon/kernel/vdso.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
5252
unsigned long vdso_base;
5353
struct mm_struct *mm = current->mm;
5454

55-
if (down_write_killable(&mm->mmap_sem))
55+
if (mmap_write_lock_killable(mm))
5656
return -EINTR;
5757

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

7878
up_fail:
79-
up_write(&mm->mmap_sem);
79+
mmap_write_unlock(mm);
8080
return ret;
8181
}
8282

0 commit comments

Comments
 (0)