aboutsummaryrefslogtreecommitdiffstats
path: root/mm/truncate.c
diff options
context:
space:
mode:
authorNick Piggin <npiggin@suse.de>2007-07-19 04:46:57 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-19 13:04:41 -0400
commitd00806b183152af6d24f46f0c33f14162ca1262a (patch)
tree36f829cf13d5410374a3f00b56ec0b1f8dc3ce3c /mm/truncate.c
parent589f1e81bde732dd0b1bc5d01b6bddd4bcb4527b (diff)
mm: fix fault vs invalidate race for linear mappings
Fix the race between invalidate_inode_pages and do_no_page. Andrea Arcangeli identified a subtle race between invalidation of pages from pagecache with userspace mappings, and do_no_page. The issue is that invalidation has to shoot down all mappings to the page, before it can be discarded from the pagecache. Between shooting down ptes to a particular page, and actually dropping the struct page from the pagecache, do_no_page from any process might fault on that page and establish a new mapping to the page just before it gets discarded from the pagecache. The most common case where such invalidation is used is in file truncation. This case was catered for by doing a sort of open-coded seqlock between the file's i_size, and its truncate_count. Truncation will decrease i_size, then increment truncate_count before unmapping userspace pages; do_no_page will read truncate_count, then find the page if it is within i_size, and then check truncate_count under the page table lock and back out and retry if it had subsequently been changed (ptl will serialise against unmapping, and ensure a potentially updated truncate_count is actually visible). Complexity and documentation issues aside, the locking protocol fails in the case where we would like to invalidate pagecache inside i_size. do_no_page can come in anytime and filemap_nopage is not aware of the invalidation in progress (as it is when it is outside i_size). The end result is that dangling (->mapping == NULL) pages that appear to be from a particular file may be mapped into userspace with nonsense data. Valid mappings to the same place will see a different page. Andrea implemented two working fixes, one using a real seqlock, another using a page->flags bit. He also proposed using the page lock in do_no_page, but that was initially considered too heavyweight. However, it is not a global or per-file lock, and the page cacheline is modified in do_no_page to increment _count and _mapcount anyway, so a further modification should not be a large performance hit. Scalability is not an issue. This patch implements this latter approach. ->nopage implementations return with the page locked if it is possible for their underlying file to be invalidated (in that case, they must set a special vm_flags bit to indicate so). do_no_page only unlocks the page after setting up the mapping completely. invalidation is excluded because it holds the page lock during invalidation of each page (and ensures that the page is not mapped while holding the lock). This also allows significant simplifications in do_no_page, because we have the page locked in the right place in the pagecache from the start. Signed-off-by: Nick Piggin <npiggin@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/truncate.c')
-rw-r--r--mm/truncate.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/mm/truncate.c b/mm/truncate.c
index f47e46d1be3b..aed85f0b707f 100644
--- a/mm/truncate.c
+++ b/mm/truncate.c
@@ -192,6 +192,11 @@ void truncate_inode_pages_range(struct address_space *mapping,
192 unlock_page(page); 192 unlock_page(page);
193 continue; 193 continue;
194 } 194 }
195 if (page_mapped(page)) {
196 unmap_mapping_range(mapping,
197 (loff_t)page_index<<PAGE_CACHE_SHIFT,
198 PAGE_CACHE_SIZE, 0);
199 }
195 truncate_complete_page(mapping, page); 200 truncate_complete_page(mapping, page);
196 unlock_page(page); 201 unlock_page(page);
197 } 202 }
@@ -229,6 +234,11 @@ void truncate_inode_pages_range(struct address_space *mapping,
229 break; 234 break;
230 lock_page(page); 235 lock_page(page);
231 wait_on_page_writeback(page); 236 wait_on_page_writeback(page);
237 if (page_mapped(page)) {
238 unmap_mapping_range(mapping,
239 (loff_t)page->index<<PAGE_CACHE_SHIFT,
240 PAGE_CACHE_SIZE, 0);
241 }
232 if (page->index > next) 242 if (page->index > next)
233 next = page->index; 243 next = page->index;
234 next++; 244 next++;
@@ -405,7 +415,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
405 break; 415 break;
406 } 416 }
407 wait_on_page_writeback(page); 417 wait_on_page_writeback(page);
408 while (page_mapped(page)) { 418 if (page_mapped(page)) {
409 if (!did_range_unmap) { 419 if (!did_range_unmap) {
410 /* 420 /*
411 * Zap the rest of the file in one hit. 421 * Zap the rest of the file in one hit.
@@ -425,6 +435,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
425 PAGE_CACHE_SIZE, 0); 435 PAGE_CACHE_SIZE, 0);
426 } 436 }
427 } 437 }
438 BUG_ON(page_mapped(page));
428 ret = do_launder_page(mapping, page); 439 ret = do_launder_page(mapping, page);
429 if (ret == 0 && !invalidate_complete_page2(mapping, page)) 440 if (ret == 0 && !invalidate_complete_page2(mapping, page))
430 ret = -EIO; 441 ret = -EIO;