Skip to content

Commit 9637a5e

Browse files
dhowellsLinus Torvalds
authored andcommitted
[PATCH] add page_mkwrite() vm_operations method
Add a new VMA operation to notify a filesystem or other driver about the MMU generating a fault because userspace attempted to write to a page mapped through a read-only PTE. This facility permits the filesystem or driver to: (*) Implement storage allocation/reservation on attempted write, and so to deal with problems such as ENOSPC more gracefully (perhaps by generating SIGBUS). (*) Delay making the page writable until the contents have been written to a backing cache. This is useful for NFS/AFS when using FS-Cache/CacheFS. It permits the filesystem to have some guarantee about the state of the cache. (*) Account and limit number of dirty pages. This is one piece of the puzzle needed to make shared writable mapping work safely in FUSE. Needed by cachefs (Or is it cachefiles? Or fscache? <head spins>). At least four other groups have stated an interest in it or a desire to use the functionality it provides: FUSE, OCFS2, NTFS and JFFS2. Also, things like EXT3 really ought to use it to deal with the case of shared-writable mmap encountering ENOSPC before we permit the page to be dirtied. From: Peter Zijlstra <a.p.zijlstra@chello.nl> get_user_pages(.write=1, .force=1) can generate COW hits on read-only shared mappings, this patch traps those as mkpage_write candidates and fails to handle them the old way. Signed-off-by: David Howells <dhowells@redhat.com> Cc: Miklos Szeredi <miklos@szeredi.hu> Cc: Joel Becker <Joel.Becker@oracle.com> Cc: Mark Fasheh <mark.fasheh@oracle.com> Cc: Anton Altaparmakov <aia21@cantab.net> Cc: David Woodhouse <dwmw2@infradead.org> Cc: Hugh Dickins <hugh@veritas.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent bd96b9e commit 9637a5e

4 files changed

Lines changed: 99 additions & 28 deletions

File tree

‎include/linux/mm.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ struct vm_operations_struct {
199199
void (*close)(struct vm_area_struct * area);
200200
struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
201201
int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
202+
203+
/* notification that a previously read-only page is about to become
204+
* writable, if an error is returned it will cause a SIGBUS */
205+
int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
202206
#ifdef CONFIG_NUMA
203207
int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
204208
struct mempolicy *(*get_policy)(struct vm_area_struct *vma,

‎mm/memory.c‎

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,25 +1457,60 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
14571457
{
14581458
struct page *old_page, *new_page;
14591459
pte_t entry;
1460-
int ret = VM_FAULT_MINOR;
1460+
int reuse, ret = VM_FAULT_MINOR;
14611461

14621462
old_page = vm_normal_page(vma, address, orig_pte);
14631463
if (!old_page)
14641464
goto gotten;
14651465

1466-
if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
1467-
int reuse = can_share_swap_page(old_page);
1468-
unlock_page(old_page);
1469-
if (reuse) {
1470-
flush_cache_page(vma, address, pte_pfn(orig_pte));
1471-
entry = pte_mkyoung(orig_pte);
1472-
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1473-
ptep_set_access_flags(vma, address, page_table, entry, 1);
1474-
update_mmu_cache(vma, address, entry);
1475-
lazy_mmu_prot_update(entry);
1476-
ret |= VM_FAULT_WRITE;
1477-
goto unlock;
1466+
if (unlikely((vma->vm_flags & (VM_SHARED|VM_WRITE)) ==
1467+
(VM_SHARED|VM_WRITE))) {
1468+
if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
1469+
/*
1470+
* Notify the address space that the page is about to
1471+
* become writable so that it can prohibit this or wait
1472+
* for the page to get into an appropriate state.
1473+
*
1474+
* We do this without the lock held, so that it can
1475+
* sleep if it needs to.
1476+
*/
1477+
page_cache_get(old_page);
1478+
pte_unmap_unlock(page_table, ptl);
1479+
1480+
if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
1481+
goto unwritable_page;
1482+
1483+
page_cache_release(old_page);
1484+
1485+
/*
1486+
* Since we dropped the lock we need to revalidate
1487+
* the PTE as someone else may have changed it. If
1488+
* they did, we just return, as we can count on the
1489+
* MMU to tell us if they didn't also make it writable.
1490+
*/
1491+
page_table = pte_offset_map_lock(mm, pmd, address,
1492+
&ptl);
1493+
if (!pte_same(*page_table, orig_pte))
1494+
goto unlock;
14781495
}
1496+
1497+
reuse = 1;
1498+
} else if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
1499+
reuse = can_share_swap_page(old_page);
1500+
unlock_page(old_page);
1501+
} else {
1502+
reuse = 0;
1503+
}
1504+
1505+
if (reuse) {
1506+
flush_cache_page(vma, address, pte_pfn(orig_pte));
1507+
entry = pte_mkyoung(orig_pte);
1508+
entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1509+
ptep_set_access_flags(vma, address, page_table, entry, 1);
1510+
update_mmu_cache(vma, address, entry);
1511+
lazy_mmu_prot_update(entry);
1512+
ret |= VM_FAULT_WRITE;
1513+
goto unlock;
14791514
}
14801515

14811516
/*
@@ -1535,6 +1570,10 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
15351570
if (old_page)
15361571
page_cache_release(old_page);
15371572
return VM_FAULT_OOM;
1573+
1574+
unwritable_page:
1575+
page_cache_release(old_page);
1576+
return VM_FAULT_SIGBUS;
15381577
}
15391578

15401579
/*
@@ -2083,18 +2122,31 @@ static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
20832122
/*
20842123
* Should we do an early C-O-W break?
20852124
*/
2086-
if (write_access && !(vma->vm_flags & VM_SHARED)) {
2087-
struct page *page;
2125+
if (write_access) {
2126+
if (!(vma->vm_flags & VM_SHARED)) {
2127+
struct page *page;
20882128

2089-
if (unlikely(anon_vma_prepare(vma)))
2090-
goto oom;
2091-
page = alloc_page_vma(GFP_HIGHUSER, vma, address);
2092-
if (!page)
2093-
goto oom;
2094-
copy_user_highpage(page, new_page, address);
2095-
page_cache_release(new_page);
2096-
new_page = page;
2097-
anon = 1;
2129+
if (unlikely(anon_vma_prepare(vma)))
2130+
goto oom;
2131+
page = alloc_page_vma(GFP_HIGHUSER, vma, address);
2132+
if (!page)
2133+
goto oom;
2134+
copy_user_highpage(page, new_page, address);
2135+
page_cache_release(new_page);
2136+
new_page = page;
2137+
anon = 1;
2138+
2139+
} else {
2140+
/* if the page will be shareable, see if the backing
2141+
* address space wants to know that the page is about
2142+
* to become writable */
2143+
if (vma->vm_ops->page_mkwrite &&
2144+
vma->vm_ops->page_mkwrite(vma, new_page) < 0
2145+
) {
2146+
page_cache_release(new_page);
2147+
return VM_FAULT_SIGBUS;
2148+
}
2149+
}
20982150
}
20992151

21002152
page_table = pte_offset_map_lock(mm, pmd, address, &ptl);

‎mm/mmap.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,8 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
10651065
vma->vm_start = addr;
10661066
vma->vm_end = addr + len;
10671067
vma->vm_flags = vm_flags;
1068-
vma->vm_page_prot = protection_map[vm_flags & 0x0f];
1068+
vma->vm_page_prot = protection_map[vm_flags &
1069+
(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
10691070
vma->vm_pgoff = pgoff;
10701071

10711072
if (file) {
@@ -1089,6 +1090,12 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
10891090
goto free_vma;
10901091
}
10911092

1093+
/* Don't make the VMA automatically writable if it's shared, but the
1094+
* backer wishes to know when pages are first written to */
1095+
if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1096+
vma->vm_page_prot =
1097+
protection_map[vm_flags & (VM_READ|VM_WRITE|VM_EXEC)];
1098+
10921099
/* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
10931100
* shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
10941101
* that memory reservation must be checked; but that reservation
@@ -1921,7 +1928,8 @@ unsigned long do_brk(unsigned long addr, unsigned long len)
19211928
vma->vm_end = addr + len;
19221929
vma->vm_pgoff = pgoff;
19231930
vma->vm_flags = flags;
1924-
vma->vm_page_prot = protection_map[flags & 0x0f];
1931+
vma->vm_page_prot = protection_map[flags &
1932+
(VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
19251933
vma_link(mm, vma, prev, rb_link, rb_parent);
19261934
out:
19271935
mm->total_vm += len >> PAGE_SHIFT;

‎mm/mprotect.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
123123
unsigned long oldflags = vma->vm_flags;
124124
long nrpages = (end - start) >> PAGE_SHIFT;
125125
unsigned long charged = 0;
126+
unsigned int mask;
126127
pgprot_t newprot;
127128
pgoff_t pgoff;
128129
int error;
@@ -149,8 +150,6 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
149150
}
150151
}
151152

152-
newprot = protection_map[newflags & 0xf];
153-
154153
/*
155154
* First try to merge with previous and/or next vma.
156155
*/
@@ -177,6 +176,14 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
177176
}
178177

179178
success:
179+
/* Don't make the VMA automatically writable if it's shared, but the
180+
* backer wishes to know when pages are first written to */
181+
mask = VM_READ|VM_WRITE|VM_EXEC|VM_SHARED;
182+
if (vma->vm_ops && vma->vm_ops->page_mkwrite)
183+
mask &= ~VM_SHARED;
184+
185+
newprot = protection_map[newflags & mask];
186+
180187
/*
181188
* vm_flags and vm_page_prot are protected by the mmap_sem
182189
* held in write mode.

0 commit comments

Comments
 (0)