aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorMatthew Wilcox <matthew.r.wilcox@intel.com>2015-02-16 18:58:46 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-16 20:56:02 -0500
commit283307c7607de2a06d3bfae4cfbf5a566d457090 (patch)
tree297291594a08da91938d91849ae21e8edc388658 /mm
parentb4770fe5270ed4c67a53d5a9791aba63f8141e2a (diff)
mm: fix XIP fault vs truncate race
DAX is a replacement for the variation of XIP currently supported by the ext2 filesystem. We have three different things in the tree called 'XIP', and the new focus is on access to data rather than executables, so a name change was in order. DAX stands for Direct Access. The X is for eXciting. The new focus on data access has resulted in more careful attention to races that exist in the current XIP code, but are not hit by the use-case that it was designed for. XIP's architecture worked fine for ext2, but DAX is architected to work with modern filsystems such as ext4 and XFS. DAX is not intended for use with btrfs; the value that btrfs adds relies on manipulating data and writing data to different locations, while DAX's value is for write-in-place and keeping the kernel from touching the data. DAX was developed in order to support NV-DIMMs, but it's become clear that its usefuless extends beyond NV-DIMMs and there are several potential customers including the tracing machinery. Other people want to place the kernel log in an area of memory, as long as they have a BIOS that does not clear DRAM on reboot. Patch 1 is a bug fix, probably worth including in 3.18. Patches 2 & 3 are infrastructure for DAX. Patches 4-8 replace the XIP code with its DAX equivalents, transforming ext2 to use the DAX code as we go. Note that patch 10 is the Documentation patch. Patches 9-15 clean up after the XIP code, removing the infrastructure that is no longer needed and renaming various XIP things to DAX. Most of these patches were added after Jan found things he didn't like in an earlier version of the ext4 patch ... that had been copied from ext2. So ext2 i being transformed to do things the same way that ext4 will later. The ability to mount ext2 filesystems with the 'xip' option is retained, although the 'dax' option is now preferred. Patch 16 adds some DAX infrastructure to support ext4. Patch 17 adds DAX support to ext4. It is broadly similar to ext2's DAX support, but it is more efficient than ext4's due to its support for unwritten extents. Patch 18 is another cleanup patch renaming XIP to DAX. My thanks to Mathieu Desnoyers for his reviews of the v11 patchset. Most of the changes below were based on his feedback. This patch (of 18): Pagecache faults recheck i_size after taking the page lock to ensure that the fault didn't race against a truncate. We don't have a page to lock in the XIP case, so use i_mmap_lock_read() instead. It is locked in the truncate path in unmap_mapping_range() after updating i_size. So while we hold it in the fault path, we are guaranteed that either i_size has already been updated in the truncate path, or that the truncate will subsequently call zap_page_range_single() and so remove the mapping we have just inserted. There is a window of time in which i_size has been reduced and the thread has a mapping to a page which will be removed from the file, but this is harmless as the page will not be allocated to a different purpose before the thread's access to it is revoked. [akpm@linux-foundation.org: switch to i_mmap_lock_read(), add comment in unmap_single_vma()] Signed-off-by: Matthew Wilcox <matthew.r.wilcox@intel.com> Reviewed-by: Jan Kara <jack@suse.cz> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andreas Dilger <andreas.dilger@intel.com> Cc: Boaz Harrosh <boaz@plexistor.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Dave Chinner <david@fromorbit.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Ross Zwisler <ross.zwisler@linux.intel.com> Cc: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/filemap_xip.c30
-rw-r--r--mm/memory.c1
2 files changed, 29 insertions, 2 deletions
diff --git a/mm/filemap_xip.c b/mm/filemap_xip.c
index c175f9f25210..59e1c5585748 100644
--- a/mm/filemap_xip.c
+++ b/mm/filemap_xip.c
@@ -256,8 +256,20 @@ again:
256 __xip_unmap(mapping, vmf->pgoff); 256 __xip_unmap(mapping, vmf->pgoff);
257 257
258found: 258found:
259 /*
260 * We must recheck i_size under i_mmap_rwsem to prevent races
261 * with truncation
262 */
263 i_mmap_lock_read(mapping);
264 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
265 PAGE_CACHE_SHIFT;
266 if (unlikely(vmf->pgoff >= size)) {
267 i_mmap_unlock_read(mapping);
268 return VM_FAULT_SIGBUS;
269 }
259 err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, 270 err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
260 xip_pfn); 271 xip_pfn);
272 i_mmap_unlock_read(mapping);
261 if (err == -ENOMEM) 273 if (err == -ENOMEM)
262 return VM_FAULT_OOM; 274 return VM_FAULT_OOM;
263 /* 275 /*
@@ -281,16 +293,30 @@ found:
281 } 293 }
282 if (error != -ENODATA) 294 if (error != -ENODATA)
283 goto out; 295 goto out;
296
297 /*
298 * We must recheck i_size under i_mmap_rwsem to prevent races
299 * with truncation
300 */
301 i_mmap_lock_read(mapping);
302 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
303 PAGE_CACHE_SHIFT;
304 if (unlikely(vmf->pgoff >= size)) {
305 ret = VM_FAULT_SIGBUS;
306 goto unlock;
307 }
284 /* not shared and writable, use xip_sparse_page() */ 308 /* not shared and writable, use xip_sparse_page() */
285 page = xip_sparse_page(); 309 page = xip_sparse_page();
286 if (!page) 310 if (!page)
287 goto out; 311 goto unlock;
288 err = vm_insert_page(vma, (unsigned long)vmf->virtual_address, 312 err = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
289 page); 313 page);
290 if (err == -ENOMEM) 314 if (err == -ENOMEM)
291 goto out; 315 goto unlock;
292 316
293 ret = VM_FAULT_NOPAGE; 317 ret = VM_FAULT_NOPAGE;
318unlock:
319 i_mmap_unlock_read(mapping);
294out: 320out:
295 write_seqcount_end(&xip_sparse_seq); 321 write_seqcount_end(&xip_sparse_seq);
296 mutex_unlock(&xip_sparse_mutex); 322 mutex_unlock(&xip_sparse_mutex);
diff --git a/mm/memory.c b/mm/memory.c
index 99275325f303..1b04e13b9993 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2329,6 +2329,7 @@ void unmap_mapping_range(struct address_space *mapping,
2329 details.last_index = ULONG_MAX; 2329 details.last_index = ULONG_MAX;
2330 2330
2331 2331
2332 /* DAX uses i_mmap_lock to serialise file truncate vs page fault */
2332 i_mmap_lock_write(mapping); 2333 i_mmap_lock_write(mapping);
2333 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap))) 2334 if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
2334 unmap_mapping_range_tree(&mapping->i_mmap, &details); 2335 unmap_mapping_range_tree(&mapping->i_mmap, &details);