Skip to content

Commit b827e49

Browse files
Nick Piggintorvalds
authored andcommitted
mm: close page_mkwrite races
Change page_mkwrite to allow implementations to return with the page locked, and also change it's callers (in page fault paths) to hold the lock until the page is marked dirty. This allows the filesystem to have full control of page dirtying events coming from the VM. Rather than simply hold the page locked over the page_mkwrite call, we call page_mkwrite with the page unlocked and allow callers to return with it locked, so filesystems can avoid LOR conditions with page lock. The problem with the current scheme is this: a filesystem that wants to associate some metadata with a page as long as the page is dirty, will perform this manipulation in its ->page_mkwrite. It currently then must return with the page unlocked and may not hold any other locks (according to existing page_mkwrite convention). In this window, the VM could write out the page, clearing page-dirty. The filesystem has no good way to detect that a dirty pte is about to be attached, so it will happily write out the page, at which point, the filesystem may manipulate the metadata to reflect that the page is no longer dirty. It is not always possible to perform the required metadata manipulation in ->set_page_dirty, because that function cannot block or fail. The filesystem may need to allocate some data structure, for example. And the VM cannot mark the pte dirty before page_mkwrite, because page_mkwrite is allowed to fail, so we must not allow any window where the page could be written to if page_mkwrite does fail. This solution of holding the page locked over the 3 critical operations (page_mkwrite, setting the pte dirty, and finally setting the page dirty) closes out races nicely, preventing page cleaning for writeout being initiated in that window. This provides the filesystem with a strong synchronisation against the VM here. - Sage needs this race closed for ceph filesystem. - Trond for NFS (http://bugzilla.kernel.org/show_bug.cgi?id=12913). - I need it for fsblock. - I suspect other filesystems may need it too (eg. btrfs). - I have converted buffer.c to the new locking. Even simple block allocation under dirty pages might be susceptible to i_size changing under partial page at the end of file (we also have a buffer.c-side problem here, but it cannot be fixed properly without this patch). - Other filesystems (eg. NFS, maybe btrfs) will need to change their page_mkwrite functions themselves. [ This also moves page_mkwrite another step closer to fault, which should eventually allow page_mkwrite to be moved into ->fault, and thus avoiding a filesystem calldown and page lock/unlock cycle in __do_fault. ] [akpm@linux-foundation.org: fix derefs of NULL ->mapping] Cc: Sage Weil <sage@newdream.net> Cc: Trond Myklebust <trond.myklebust@fys.uio.no> Signed-off-by: Nick Piggin <npiggin@suse.de> Cc: Valdis Kletnieks <Valdis.Kletnieks@vt.edu> Cc: <stable@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a5fc1ab commit b827e49

3 files changed

Lines changed: 98 additions & 44 deletions

File tree

‎Documentation/filesystems/Locking‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,24 @@ locking rules:
512512
BKL mmap_sem PageLocked(page)
513513
open: no yes
514514
close: no yes
515-
fault: no yes
516-
page_mkwrite: no yes no
515+
fault: no yes can return with page locked
516+
page_mkwrite: no yes can return with page locked
517517
access: no yes
518518

519-
->page_mkwrite() is called when a previously read-only page is
520-
about to become writeable. The file system is responsible for
521-
protecting against truncate races. Once appropriate action has been
522-
taking to lock out truncate, the page range should be verified to be
523-
within i_size. The page mapping should also be checked that it is not
524-
NULL.
519+
->fault() is called when a previously not present pte is about
520+
to be faulted in. The filesystem must find and return the page associated
521+
with the passed in "pgoff" in the vm_fault structure. If it is possible that
522+
the page may be truncated and/or invalidated, then the filesystem must lock
523+
the page, then ensure it is not already truncated (the page lock will block
524+
subsequent truncate), and then return with VM_FAULT_LOCKED, and the page
525+
locked. The VM will unlock the page.
526+
527+
->page_mkwrite() is called when a previously read-only pte is
528+
about to become writeable. The filesystem again must ensure that there are
529+
no truncate/invalidate races, and then return with the page locked. If
530+
the page has been truncated, the filesystem should not look up a new page
531+
like the ->fault() handler, but simply return with VM_FAULT_NOPAGE, which
532+
will cause the VM to retry the fault.
525533

526534
->access() is called when get_user_pages() fails in
527535
acces_process_vm(), typically used to debug a process through

‎fs/buffer.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,7 +2397,8 @@ block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
23972397
if ((page->mapping != inode->i_mapping) ||
23982398
(page_offset(page) > size)) {
23992399
/* page got truncated out from underneath us */
2400-
goto out_unlock;
2400+
unlock_page(page);
2401+
goto out;
24012402
}
24022403

24032404
/* page is wholly or partially inside EOF */
@@ -2411,14 +2412,15 @@ block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
24112412
ret = block_commit_write(page, 0, end);
24122413

24132414
if (unlikely(ret)) {
2415+
unlock_page(page);
24142416
if (ret == -ENOMEM)
24152417
ret = VM_FAULT_OOM;
24162418
else /* -ENOSPC, -EIO, etc */
24172419
ret = VM_FAULT_SIGBUS;
2418-
}
2420+
} else
2421+
ret = VM_FAULT_LOCKED;
24192422

2420-
out_unlock:
2421-
unlock_page(page);
2423+
out:
24222424
return ret;
24232425
}
24242426

‎mm/memory.c‎

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,15 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
19711971
ret = tmp;
19721972
goto unwritable_page;
19731973
}
1974+
if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
1975+
lock_page(old_page);
1976+
if (!old_page->mapping) {
1977+
ret = 0; /* retry the fault */
1978+
unlock_page(old_page);
1979+
goto unwritable_page;
1980+
}
1981+
} else
1982+
VM_BUG_ON(!PageLocked(old_page));
19741983

19751984
/*
19761985
* Since we dropped the lock we need to revalidate
@@ -1980,9 +1989,11 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
19801989
*/
19811990
page_table = pte_offset_map_lock(mm, pmd, address,
19821991
&ptl);
1983-
page_cache_release(old_page);
1984-
if (!pte_same(*page_table, orig_pte))
1992+
if (!pte_same(*page_table, orig_pte)) {
1993+
unlock_page(old_page);
1994+
page_cache_release(old_page);
19851995
goto unlock;
1996+
}
19861997

19871998
page_mkwrite = 1;
19881999
}
@@ -2094,9 +2105,6 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
20942105
unlock:
20952106
pte_unmap_unlock(page_table, ptl);
20962107
if (dirty_page) {
2097-
if (vma->vm_file)
2098-
file_update_time(vma->vm_file);
2099-
21002108
/*
21012109
* Yes, Virginia, this is actually required to prevent a race
21022110
* with clear_page_dirty_for_io() from clearing the page dirty
@@ -2105,16 +2113,41 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
21052113
*
21062114
* do_no_page is protected similarly.
21072115
*/
2108-
wait_on_page_locked(dirty_page);
2109-
set_page_dirty_balance(dirty_page, page_mkwrite);
2116+
if (!page_mkwrite) {
2117+
wait_on_page_locked(dirty_page);
2118+
set_page_dirty_balance(dirty_page, page_mkwrite);
2119+
}
21102120
put_page(dirty_page);
2121+
if (page_mkwrite) {
2122+
struct address_space *mapping = dirty_page->mapping;
2123+
2124+
set_page_dirty(dirty_page);
2125+
unlock_page(dirty_page);
2126+
page_cache_release(dirty_page);
2127+
if (mapping) {
2128+
/*
2129+
* Some device drivers do not set page.mapping
2130+
* but still dirty their pages
2131+
*/
2132+
balance_dirty_pages_ratelimited(mapping);
2133+
}
2134+
}
2135+
2136+
/* file_update_time outside page_lock */
2137+
if (vma->vm_file)
2138+
file_update_time(vma->vm_file);
21112139
}
21122140
return ret;
21132141
oom_free_new:
21142142
page_cache_release(new_page);
21152143
oom:
2116-
if (old_page)
2144+
if (old_page) {
2145+
if (page_mkwrite) {
2146+
unlock_page(old_page);
2147+
page_cache_release(old_page);
2148+
}
21172149
page_cache_release(old_page);
2150+
}
21182151
return VM_FAULT_OOM;
21192152

21202153
unwritable_page:
@@ -2664,27 +2697,22 @@ static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
26642697
int tmp;
26652698

26662699
unlock_page(page);
2667-
vmf.flags |= FAULT_FLAG_MKWRITE;
2700+
vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
26682701
tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
26692702
if (unlikely(tmp &
26702703
(VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
26712704
ret = tmp;
2672-
anon = 1; /* no anon but release vmf.page */
2673-
goto out_unlocked;
2674-
}
2675-
lock_page(page);
2676-
/*
2677-
* XXX: this is not quite right (racy vs
2678-
* invalidate) to unlock and relock the page
2679-
* like this, however a better fix requires
2680-
* reworking page_mkwrite locking API, which
2681-
* is better done later.
2682-
*/
2683-
if (!page->mapping) {
2684-
ret = 0;
2685-
anon = 1; /* no anon but release vmf.page */
2686-
goto out;
2705+
goto unwritable_page;
26872706
}
2707+
if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
2708+
lock_page(page);
2709+
if (!page->mapping) {
2710+
ret = 0; /* retry the fault */
2711+
unlock_page(page);
2712+
goto unwritable_page;
2713+
}
2714+
} else
2715+
VM_BUG_ON(!PageLocked(page));
26882716
page_mkwrite = 1;
26892717
}
26902718
}
@@ -2736,19 +2764,35 @@ static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
27362764
pte_unmap_unlock(page_table, ptl);
27372765

27382766
out:
2739-
unlock_page(vmf.page);
2740-
out_unlocked:
2741-
if (anon)
2742-
page_cache_release(vmf.page);
2743-
else if (dirty_page) {
2744-
if (vma->vm_file)
2745-
file_update_time(vma->vm_file);
2767+
if (dirty_page) {
2768+
struct address_space *mapping = page->mapping;
27462769

2747-
set_page_dirty_balance(dirty_page, page_mkwrite);
2770+
if (set_page_dirty(dirty_page))
2771+
page_mkwrite = 1;
2772+
unlock_page(dirty_page);
27482773
put_page(dirty_page);
2774+
if (page_mkwrite && mapping) {
2775+
/*
2776+
* Some device drivers do not set page.mapping but still
2777+
* dirty their pages
2778+
*/
2779+
balance_dirty_pages_ratelimited(mapping);
2780+
}
2781+
2782+
/* file_update_time outside page_lock */
2783+
if (vma->vm_file)
2784+
file_update_time(vma->vm_file);
2785+
} else {
2786+
unlock_page(vmf.page);
2787+
if (anon)
2788+
page_cache_release(vmf.page);
27492789
}
27502790

27512791
return ret;
2792+
2793+
unwritable_page:
2794+
page_cache_release(page);
2795+
return ret;
27522796
}
27532797

27542798
static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,

0 commit comments

Comments
 (0)