Skip to content

Commit 9a95f3c

Browse files
Paul Cassellatorvalds
authored andcommitted
mm: describe mmap_sem rules for __lock_page_or_retry() and callers
Add a comment describing the circumstances in which __lock_page_or_retry() will or will not release the mmap_sem when returning 0. Add comments to lock_page_or_retry()'s callers (filemap_fault(), do_swap_page()) noting the impact on VM_FAULT_RETRY returns. Add comments on up the call tree, particularly replacing the false "We return with mmap_sem still held" comments. Signed-off-by: Paul Cassella <cassella@cray.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4ffeaf3 commit 9a95f3c

6 files changed

Lines changed: 82 additions & 8 deletions

File tree

‎arch/x86/mm/fault.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,8 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,
12181218
/*
12191219
* If for any reason at all we couldn't handle the fault,
12201220
* make sure we exit gracefully rather than endlessly redo
1221-
* the fault:
1221+
* the fault. Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1222+
* we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
12221223
*/
12231224
fault = handle_mm_fault(mm, vma, address, flags);
12241225

‎include/linux/pagemap.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ static inline int lock_page_killable(struct page *page)
484484
/*
485485
* lock_page_or_retry - Lock the page, unless this would block and the
486486
* caller indicated that it can handle a retry.
487+
*
488+
* Return value and mmap_sem implications depend on flags; see
489+
* __lock_page_or_retry().
487490
*/
488491
static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
489492
unsigned int flags)

‎mm/filemap.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,17 @@ int __lock_page_killable(struct page *page)
808808
}
809809
EXPORT_SYMBOL_GPL(__lock_page_killable);
810810

811+
/*
812+
* Return values:
813+
* 1 - page is locked; mmap_sem is still held.
814+
* 0 - page is not locked.
815+
* mmap_sem has been released (up_read()), unless flags had both
816+
* FAULT_FLAG_ALLOW_RETRY and FAULT_FLAG_RETRY_NOWAIT set, in
817+
* which case mmap_sem is still held.
818+
*
819+
* If neither ALLOW_RETRY nor KILLABLE are set, will always return 1
820+
* with the page locked and the mmap_sem unperturbed.
821+
*/
811822
int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
812823
unsigned int flags)
813824
{
@@ -1827,6 +1838,18 @@ static void do_async_mmap_readahead(struct vm_area_struct *vma,
18271838
* The goto's are kind of ugly, but this streamlines the normal case of having
18281839
* it in the page cache, and handles the special cases reasonably without
18291840
* having a lot of duplicated code.
1841+
*
1842+
* vma->vm_mm->mmap_sem must be held on entry.
1843+
*
1844+
* If our return value has VM_FAULT_RETRY set, it's because
1845+
* lock_page_or_retry() returned 0.
1846+
* The mmap_sem has usually been released in this case.
1847+
* See __lock_page_or_retry() for the exception.
1848+
*
1849+
* If our return value does not have VM_FAULT_RETRY set, the mmap_sem
1850+
* has not been released.
1851+
*
1852+
* We never return with VM_FAULT_RETRY and a bit from VM_FAULT_ERROR set.
18301853
*/
18311854
int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
18321855
{

‎mm/gup.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,
258258
return ret;
259259
}
260260

261+
/*
262+
* mmap_sem must be held on entry. If @nonblocking != NULL and
263+
* *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
264+
* If it is, *@nonblocking will be set to 0 and -EBUSY returned.
265+
*/
261266
static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
262267
unsigned long address, unsigned int *flags, int *nonblocking)
263268
{
@@ -373,7 +378,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
373378
* with a put_page() call when it is finished with. vmas will only
374379
* remain valid while mmap_sem is held.
375380
*
376-
* Must be called with mmap_sem held for read or write.
381+
* Must be called with mmap_sem held. It may be released. See below.
377382
*
378383
* __get_user_pages walks a process's page tables and takes a reference to
379384
* each struct page that each user address corresponds to at a given
@@ -396,7 +401,14 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
396401
*
397402
* If @nonblocking != NULL, __get_user_pages will not wait for disk IO
398403
* or mmap_sem contention, and if waiting is needed to pin all pages,
399-
* *@nonblocking will be set to 0.
404+
* *@nonblocking will be set to 0. Further, if @gup_flags does not
405+
* include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
406+
* this case.
407+
*
408+
* A caller using such a combination of @nonblocking and @gup_flags
409+
* must therefore hold the mmap_sem for reading only, and recognize
410+
* when it's been released. Otherwise, it must be held for either
411+
* reading or writing and will not be released.
400412
*
401413
* In most cases, get_user_pages or get_user_pages_fast should be used
402414
* instead of __get_user_pages. __get_user_pages should be used only if
@@ -528,7 +540,7 @@ EXPORT_SYMBOL(__get_user_pages);
528540
* such architectures, gup() will not be enough to make a subsequent access
529541
* succeed.
530542
*
531-
* This should be called with the mm_sem held for read.
543+
* This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
532544
*/
533545
int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
534546
unsigned long address, unsigned int fault_flags)

‎mm/memory.c‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,7 +2399,10 @@ EXPORT_SYMBOL(unmap_mapping_range);
23992399
/*
24002400
* We enter with non-exclusive mmap_sem (to exclude vma changes,
24012401
* but allow concurrent faults), and pte mapped but not yet locked.
2402-
* We return with mmap_sem still held, but pte unmapped and unlocked.
2402+
* We return with pte unmapped and unlocked.
2403+
*
2404+
* We return with the mmap_sem locked or unlocked in the same cases
2405+
* as does filemap_fault().
24032406
*/
24042407
static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
24052408
unsigned long address, pte_t *page_table, pmd_t *pmd,
@@ -2688,6 +2691,11 @@ static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
26882691
return VM_FAULT_OOM;
26892692
}
26902693

2694+
/*
2695+
* The mmap_sem must have been held on entry, and may have been
2696+
* released depending on flags and vma->vm_ops->fault() return value.
2697+
* See filemap_fault() and __lock_page_retry().
2698+
*/
26912699
static int __do_fault(struct vm_area_struct *vma, unsigned long address,
26922700
pgoff_t pgoff, unsigned int flags, struct page **page)
26932701
{
@@ -3016,6 +3024,12 @@ static int do_shared_fault(struct mm_struct *mm, struct vm_area_struct *vma,
30163024
return ret;
30173025
}
30183026

3027+
/*
3028+
* We enter with non-exclusive mmap_sem (to exclude vma changes,
3029+
* but allow concurrent faults).
3030+
* The mmap_sem may have been released depending on flags and our
3031+
* return value. See filemap_fault() and __lock_page_or_retry().
3032+
*/
30193033
static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
30203034
unsigned long address, pte_t *page_table, pmd_t *pmd,
30213035
unsigned int flags, pte_t orig_pte)
@@ -3040,7 +3054,9 @@ static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
30403054
*
30413055
* We enter with non-exclusive mmap_sem (to exclude vma changes,
30423056
* but allow concurrent faults), and pte mapped but not yet locked.
3043-
* We return with mmap_sem still held, but pte unmapped and unlocked.
3057+
* We return with pte unmapped and unlocked.
3058+
* The mmap_sem may have been released depending on flags and our
3059+
* return value. See filemap_fault() and __lock_page_or_retry().
30443060
*/
30453061
static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
30463062
unsigned long address, pte_t *page_table, pmd_t *pmd,
@@ -3172,7 +3188,10 @@ static int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
31723188
*
31733189
* We enter with non-exclusive mmap_sem (to exclude vma changes,
31743190
* but allow concurrent faults), and pte mapped but not yet locked.
3175-
* We return with mmap_sem still held, but pte unmapped and unlocked.
3191+
* We return with pte unmapped and unlocked.
3192+
*
3193+
* The mmap_sem may have been released depending on flags and our
3194+
* return value. See filemap_fault() and __lock_page_or_retry().
31763195
*/
31773196
static int handle_pte_fault(struct mm_struct *mm,
31783197
struct vm_area_struct *vma, unsigned long address,
@@ -3232,6 +3251,9 @@ static int handle_pte_fault(struct mm_struct *mm,
32323251

32333252
/*
32343253
* By the time we get here, we already hold the mm semaphore
3254+
*
3255+
* The mmap_sem may have been released depending on flags and our
3256+
* return value. See filemap_fault() and __lock_page_or_retry().
32353257
*/
32363258
static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
32373259
unsigned long address, unsigned int flags)
@@ -3313,6 +3335,12 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
33133335
return handle_pte_fault(mm, vma, address, pte, pmd, flags);
33143336
}
33153337

3338+
/*
3339+
* By the time we get here, we already hold the mm semaphore
3340+
*
3341+
* The mmap_sem may have been released depending on flags and our
3342+
* return value. See filemap_fault() and __lock_page_or_retry().
3343+
*/
33163344
int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
33173345
unsigned long address, unsigned int flags)
33183346
{

‎mm/mlock.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,19 @@ unsigned int munlock_vma_page(struct page *page)
210210
* @vma: target vma
211211
* @start: start address
212212
* @end: end address
213+
* @nonblocking:
213214
*
214215
* This takes care of making the pages present too.
215216
*
216217
* return 0 on success, negative error code on error.
217218
*
218-
* vma->vm_mm->mmap_sem must be held for at least read.
219+
* vma->vm_mm->mmap_sem must be held.
220+
*
221+
* If @nonblocking is NULL, it may be held for read or write and will
222+
* be unperturbed.
223+
*
224+
* If @nonblocking is non-NULL, it must held for read only and may be
225+
* released. If it's released, *@nonblocking will be set to 0.
219226
*/
220227
long __mlock_vma_pages_range(struct vm_area_struct *vma,
221228
unsigned long start, unsigned long end, int *nonblocking)

0 commit comments

Comments
 (0)