Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

dax,ext2: replace the XIP page fault handler with the DAX page fault handler

Instead of calling aops->get_xip_mem from the fault handler, the
filesystem passes a get_block_t that is used to find the appropriate
blocks.

This requires that all architectures implement copy_user_page(). At the
time of writing, mips and arm do not. Patches exist and are in progress.

[akpm@linux-foundation.org: remap_file_pages went away]
Signed-off-by: Matthew Wilcox <matthew.r.wilcox@intel.com>
Reviewed-by: Jan Kara <jack@suse.cz>
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: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Russell King <rmk@arm.linux.org.uk>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Matthew Wilcox and committed by
Linus Torvalds
f7ca90b1 289c6aed

+276 -209
+241
fs/dax.c
··· 19 19 #include <linux/buffer_head.h> 20 20 #include <linux/fs.h> 21 21 #include <linux/genhd.h> 22 + #include <linux/highmem.h> 23 + #include <linux/memcontrol.h> 24 + #include <linux/mm.h> 22 25 #include <linux/mutex.h> 23 26 #include <linux/sched.h> 24 27 #include <linux/uio.h> 28 + #include <linux/vmstat.h> 25 29 26 30 int dax_clear_blocks(struct inode *inode, sector_t block, long size) 27 31 { ··· 225 221 return retval; 226 222 } 227 223 EXPORT_SYMBOL_GPL(dax_do_io); 224 + 225 + /* 226 + * The user has performed a load from a hole in the file. Allocating 227 + * a new page in the file would cause excessive storage usage for 228 + * workloads with sparse files. We allocate a page cache page instead. 229 + * We'll kick it out of the page cache if it's ever written to, 230 + * otherwise it will simply fall out of the page cache under memory 231 + * pressure without ever having been dirtied. 232 + */ 233 + static int dax_load_hole(struct address_space *mapping, struct page *page, 234 + struct vm_fault *vmf) 235 + { 236 + unsigned long size; 237 + struct inode *inode = mapping->host; 238 + if (!page) 239 + page = find_or_create_page(mapping, vmf->pgoff, 240 + GFP_KERNEL | __GFP_ZERO); 241 + if (!page) 242 + return VM_FAULT_OOM; 243 + /* Recheck i_size under page lock to avoid truncate race */ 244 + size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; 245 + if (vmf->pgoff >= size) { 246 + unlock_page(page); 247 + page_cache_release(page); 248 + return VM_FAULT_SIGBUS; 249 + } 250 + 251 + vmf->page = page; 252 + return VM_FAULT_LOCKED; 253 + } 254 + 255 + static int copy_user_bh(struct page *to, struct buffer_head *bh, 256 + unsigned blkbits, unsigned long vaddr) 257 + { 258 + void *vfrom, *vto; 259 + if (dax_get_addr(bh, &vfrom, blkbits) < 0) 260 + return -EIO; 261 + vto = kmap_atomic(to); 262 + copy_user_page(vto, vfrom, vaddr, to); 263 + kunmap_atomic(vto); 264 + return 0; 265 + } 266 + 267 + static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh, 268 + struct vm_area_struct *vma, struct vm_fault *vmf) 269 + { 270 + struct address_space *mapping = inode->i_mapping; 271 + sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9); 272 + unsigned long vaddr = (unsigned long)vmf->virtual_address; 273 + void *addr; 274 + unsigned long pfn; 275 + pgoff_t size; 276 + int error; 277 + 278 + i_mmap_lock_read(mapping); 279 + 280 + /* 281 + * Check truncate didn't happen while we were allocating a block. 282 + * If it did, this block may or may not be still allocated to the 283 + * file. We can't tell the filesystem to free it because we can't 284 + * take i_mutex here. In the worst case, the file still has blocks 285 + * allocated past the end of the file. 286 + */ 287 + size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; 288 + if (unlikely(vmf->pgoff >= size)) { 289 + error = -EIO; 290 + goto out; 291 + } 292 + 293 + error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size); 294 + if (error < 0) 295 + goto out; 296 + if (error < PAGE_SIZE) { 297 + error = -EIO; 298 + goto out; 299 + } 300 + 301 + if (buffer_unwritten(bh) || buffer_new(bh)) 302 + clear_page(addr); 303 + 304 + error = vm_insert_mixed(vma, vaddr, pfn); 305 + 306 + out: 307 + i_mmap_unlock_read(mapping); 308 + 309 + if (bh->b_end_io) 310 + bh->b_end_io(bh, 1); 311 + 312 + return error; 313 + } 314 + 315 + static int do_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, 316 + get_block_t get_block) 317 + { 318 + struct file *file = vma->vm_file; 319 + struct address_space *mapping = file->f_mapping; 320 + struct inode *inode = mapping->host; 321 + struct page *page; 322 + struct buffer_head bh; 323 + unsigned long vaddr = (unsigned long)vmf->virtual_address; 324 + unsigned blkbits = inode->i_blkbits; 325 + sector_t block; 326 + pgoff_t size; 327 + int error; 328 + int major = 0; 329 + 330 + size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; 331 + if (vmf->pgoff >= size) 332 + return VM_FAULT_SIGBUS; 333 + 334 + memset(&bh, 0, sizeof(bh)); 335 + block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits); 336 + bh.b_size = PAGE_SIZE; 337 + 338 + repeat: 339 + page = find_get_page(mapping, vmf->pgoff); 340 + if (page) { 341 + if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) { 342 + page_cache_release(page); 343 + return VM_FAULT_RETRY; 344 + } 345 + if (unlikely(page->mapping != mapping)) { 346 + unlock_page(page); 347 + page_cache_release(page); 348 + goto repeat; 349 + } 350 + size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; 351 + if (unlikely(vmf->pgoff >= size)) { 352 + /* 353 + * We have a struct page covering a hole in the file 354 + * from a read fault and we've raced with a truncate 355 + */ 356 + error = -EIO; 357 + goto unlock_page; 358 + } 359 + } 360 + 361 + error = get_block(inode, block, &bh, 0); 362 + if (!error && (bh.b_size < PAGE_SIZE)) 363 + error = -EIO; /* fs corruption? */ 364 + if (error) 365 + goto unlock_page; 366 + 367 + if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) { 368 + if (vmf->flags & FAULT_FLAG_WRITE) { 369 + error = get_block(inode, block, &bh, 1); 370 + count_vm_event(PGMAJFAULT); 371 + mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 372 + major = VM_FAULT_MAJOR; 373 + if (!error && (bh.b_size < PAGE_SIZE)) 374 + error = -EIO; 375 + if (error) 376 + goto unlock_page; 377 + } else { 378 + return dax_load_hole(mapping, page, vmf); 379 + } 380 + } 381 + 382 + if (vmf->cow_page) { 383 + struct page *new_page = vmf->cow_page; 384 + if (buffer_written(&bh)) 385 + error = copy_user_bh(new_page, &bh, blkbits, vaddr); 386 + else 387 + clear_user_highpage(new_page, vaddr); 388 + if (error) 389 + goto unlock_page; 390 + vmf->page = page; 391 + if (!page) { 392 + i_mmap_lock_read(mapping); 393 + /* Check we didn't race with truncate */ 394 + size = (i_size_read(inode) + PAGE_SIZE - 1) >> 395 + PAGE_SHIFT; 396 + if (vmf->pgoff >= size) { 397 + i_mmap_unlock_read(mapping); 398 + error = -EIO; 399 + goto out; 400 + } 401 + } 402 + return VM_FAULT_LOCKED; 403 + } 404 + 405 + /* Check we didn't race with a read fault installing a new page */ 406 + if (!page && major) 407 + page = find_lock_page(mapping, vmf->pgoff); 408 + 409 + if (page) { 410 + unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT, 411 + PAGE_CACHE_SIZE, 0); 412 + delete_from_page_cache(page); 413 + unlock_page(page); 414 + page_cache_release(page); 415 + } 416 + 417 + error = dax_insert_mapping(inode, &bh, vma, vmf); 418 + 419 + out: 420 + if (error == -ENOMEM) 421 + return VM_FAULT_OOM | major; 422 + /* -EBUSY is fine, somebody else faulted on the same PTE */ 423 + if ((error < 0) && (error != -EBUSY)) 424 + return VM_FAULT_SIGBUS | major; 425 + return VM_FAULT_NOPAGE | major; 426 + 427 + unlock_page: 428 + if (page) { 429 + unlock_page(page); 430 + page_cache_release(page); 431 + } 432 + goto out; 433 + } 434 + 435 + /** 436 + * dax_fault - handle a page fault on a DAX file 437 + * @vma: The virtual memory area where the fault occurred 438 + * @vmf: The description of the fault 439 + * @get_block: The filesystem method used to translate file offsets to blocks 440 + * 441 + * When a page fault occurs, filesystems may call this helper in their 442 + * fault handler for DAX files. 443 + */ 444 + int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, 445 + get_block_t get_block) 446 + { 447 + int result; 448 + struct super_block *sb = file_inode(vma->vm_file)->i_sb; 449 + 450 + if (vmf->flags & FAULT_FLAG_WRITE) { 451 + sb_start_pagefault(sb); 452 + file_update_time(vma->vm_file); 453 + } 454 + result = do_dax_fault(vma, vmf, get_block); 455 + if (vmf->flags & FAULT_FLAG_WRITE) 456 + sb_end_pagefault(sb); 457 + 458 + return result; 459 + } 460 + EXPORT_SYMBOL_GPL(dax_fault);
+32 -2
fs/ext2/file.c
··· 25 25 #include "xattr.h" 26 26 #include "acl.h" 27 27 28 + #ifdef CONFIG_EXT2_FS_XIP 29 + static int ext2_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 30 + { 31 + return dax_fault(vma, vmf, ext2_get_block); 32 + } 33 + 34 + static int ext2_dax_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 35 + { 36 + return dax_mkwrite(vma, vmf, ext2_get_block); 37 + } 38 + 39 + static const struct vm_operations_struct ext2_dax_vm_ops = { 40 + .fault = ext2_dax_fault, 41 + .page_mkwrite = ext2_dax_mkwrite, 42 + }; 43 + 44 + static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma) 45 + { 46 + if (!IS_DAX(file_inode(file))) 47 + return generic_file_mmap(file, vma); 48 + 49 + file_accessed(file); 50 + vma->vm_ops = &ext2_dax_vm_ops; 51 + vma->vm_flags |= VM_MIXEDMAP; 52 + return 0; 53 + } 54 + #else 55 + #define ext2_file_mmap generic_file_mmap 56 + #endif 57 + 28 58 /* 29 59 * Called when filp is released. This happens when all file descriptors 30 60 * for a single struct file are closed. Note that different open() calls ··· 100 70 #ifdef CONFIG_COMPAT 101 71 .compat_ioctl = ext2_compat_ioctl, 102 72 #endif 103 - .mmap = generic_file_mmap, 73 + .mmap = ext2_file_mmap, 104 74 .open = dquot_file_open, 105 75 .release = ext2_release_file, 106 76 .fsync = ext2_fsync, ··· 119 89 #ifdef CONFIG_COMPAT 120 90 .compat_ioctl = ext2_compat_ioctl, 121 91 #endif 122 - .mmap = xip_file_mmap, 92 + .mmap = ext2_file_mmap, 123 93 .open = dquot_file_open, 124 94 .release = ext2_release_file, 125 95 .fsync = ext2_fsync,
+3 -1
include/linux/fs.h
··· 51 51 struct seq_file; 52 52 struct workqueue_struct; 53 53 struct iov_iter; 54 + struct vm_fault; 54 55 55 56 extern void __init inode_init(void); 56 57 extern void __init inode_init_early(void); ··· 2591 2590 ssize_t dax_do_io(int rw, struct kiocb *, struct inode *, struct iov_iter *, 2592 2591 loff_t, get_block_t, dio_iodone_t, int flags); 2593 2592 int dax_clear_blocks(struct inode *, sector_t block, long size); 2593 + int dax_fault(struct vm_area_struct *, struct vm_fault *, get_block_t); 2594 + #define dax_mkwrite(vma, vmf, gb) dax_fault(vma, vmf, gb) 2594 2595 2595 2596 #ifdef CONFIG_FS_XIP 2596 - extern int xip_file_mmap(struct file * file, struct vm_area_struct * vma); 2597 2597 extern int xip_truncate_page(struct address_space *mapping, loff_t from); 2598 2598 #else 2599 2599 static inline int xip_truncate_page(struct address_space *mapping, loff_t from)
-206
mm/filemap_xip.c
··· 23 23 #include <asm/io.h> 24 24 25 25 /* 26 - * We do use our own empty page to avoid interference with other users 27 - * of ZERO_PAGE(), such as /dev/zero 28 - */ 29 - static DEFINE_MUTEX(xip_sparse_mutex); 30 - static seqcount_t xip_sparse_seq = SEQCNT_ZERO(xip_sparse_seq); 31 - static struct page *__xip_sparse_page; 32 - 33 - /* called under xip_sparse_mutex */ 34 - static struct page *xip_sparse_page(void) 35 - { 36 - if (!__xip_sparse_page) { 37 - struct page *page = alloc_page(GFP_HIGHUSER | __GFP_ZERO); 38 - 39 - if (page) 40 - __xip_sparse_page = page; 41 - } 42 - return __xip_sparse_page; 43 - } 44 - 45 - /* 46 - * __xip_unmap is invoked from xip_unmap and xip_write 47 - * 48 - * This function walks all vmas of the address_space and unmaps the 49 - * __xip_sparse_page when found at pgoff. 50 - */ 51 - static void __xip_unmap(struct address_space * mapping, unsigned long pgoff) 52 - { 53 - struct vm_area_struct *vma; 54 - struct page *page; 55 - unsigned count; 56 - int locked = 0; 57 - 58 - count = read_seqcount_begin(&xip_sparse_seq); 59 - 60 - page = __xip_sparse_page; 61 - if (!page) 62 - return; 63 - 64 - retry: 65 - i_mmap_lock_read(mapping); 66 - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { 67 - pte_t *pte, pteval; 68 - spinlock_t *ptl; 69 - struct mm_struct *mm = vma->vm_mm; 70 - unsigned long address = vma->vm_start + 71 - ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 72 - 73 - BUG_ON(address < vma->vm_start || address >= vma->vm_end); 74 - pte = page_check_address(page, mm, address, &ptl, 1); 75 - if (pte) { 76 - /* Nuke the page table entry. */ 77 - flush_cache_page(vma, address, pte_pfn(*pte)); 78 - pteval = ptep_clear_flush(vma, address, pte); 79 - page_remove_rmap(page); 80 - dec_mm_counter(mm, MM_FILEPAGES); 81 - BUG_ON(pte_dirty(pteval)); 82 - pte_unmap_unlock(pte, ptl); 83 - /* must invalidate_page _before_ freeing the page */ 84 - mmu_notifier_invalidate_page(mm, address); 85 - page_cache_release(page); 86 - } 87 - } 88 - i_mmap_unlock_read(mapping); 89 - 90 - if (locked) { 91 - mutex_unlock(&xip_sparse_mutex); 92 - } else if (read_seqcount_retry(&xip_sparse_seq, count)) { 93 - mutex_lock(&xip_sparse_mutex); 94 - locked = 1; 95 - goto retry; 96 - } 97 - } 98 - 99 - /* 100 - * xip_fault() is invoked via the vma operations vector for a 101 - * mapped memory region to read in file data during a page fault. 102 - * 103 - * This function is derived from filemap_fault, but used for execute in place 104 - */ 105 - static int xip_file_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 106 - { 107 - struct file *file = vma->vm_file; 108 - struct address_space *mapping = file->f_mapping; 109 - struct inode *inode = mapping->host; 110 - pgoff_t size; 111 - void *xip_mem; 112 - unsigned long xip_pfn; 113 - struct page *page; 114 - int error; 115 - 116 - /* XXX: are VM_FAULT_ codes OK? */ 117 - again: 118 - size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 119 - if (vmf->pgoff >= size) 120 - return VM_FAULT_SIGBUS; 121 - 122 - error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0, 123 - &xip_mem, &xip_pfn); 124 - if (likely(!error)) 125 - goto found; 126 - if (error != -ENODATA) 127 - return VM_FAULT_OOM; 128 - 129 - /* sparse block */ 130 - if ((vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) && 131 - (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) && 132 - (!(mapping->host->i_sb->s_flags & MS_RDONLY))) { 133 - int err; 134 - 135 - /* maybe shared writable, allocate new block */ 136 - mutex_lock(&xip_sparse_mutex); 137 - error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 1, 138 - &xip_mem, &xip_pfn); 139 - mutex_unlock(&xip_sparse_mutex); 140 - if (error) 141 - return VM_FAULT_SIGBUS; 142 - /* unmap sparse mappings at pgoff from all other vmas */ 143 - __xip_unmap(mapping, vmf->pgoff); 144 - 145 - found: 146 - /* 147 - * We must recheck i_size under i_mmap_rwsem to prevent races 148 - * with truncation 149 - */ 150 - i_mmap_lock_read(mapping); 151 - size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 152 - PAGE_CACHE_SHIFT; 153 - if (unlikely(vmf->pgoff >= size)) { 154 - i_mmap_unlock_read(mapping); 155 - return VM_FAULT_SIGBUS; 156 - } 157 - err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, 158 - xip_pfn); 159 - i_mmap_unlock_read(mapping); 160 - if (err == -ENOMEM) 161 - return VM_FAULT_OOM; 162 - /* 163 - * err == -EBUSY is fine, we've raced against another thread 164 - * that faulted-in the same page 165 - */ 166 - if (err != -EBUSY) 167 - BUG_ON(err); 168 - return VM_FAULT_NOPAGE; 169 - } else { 170 - int err, ret = VM_FAULT_OOM; 171 - 172 - mutex_lock(&xip_sparse_mutex); 173 - write_seqcount_begin(&xip_sparse_seq); 174 - error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0, 175 - &xip_mem, &xip_pfn); 176 - if (unlikely(!error)) { 177 - write_seqcount_end(&xip_sparse_seq); 178 - mutex_unlock(&xip_sparse_mutex); 179 - goto again; 180 - } 181 - if (error != -ENODATA) 182 - goto out; 183 - 184 - /* 185 - * We must recheck i_size under i_mmap_rwsem to prevent races 186 - * with truncation 187 - */ 188 - i_mmap_lock_read(mapping); 189 - size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 190 - PAGE_CACHE_SHIFT; 191 - if (unlikely(vmf->pgoff >= size)) { 192 - ret = VM_FAULT_SIGBUS; 193 - goto unlock; 194 - } 195 - /* not shared and writable, use xip_sparse_page() */ 196 - page = xip_sparse_page(); 197 - if (!page) 198 - goto unlock; 199 - err = vm_insert_page(vma, (unsigned long)vmf->virtual_address, 200 - page); 201 - if (err == -ENOMEM) 202 - goto unlock; 203 - 204 - ret = VM_FAULT_NOPAGE; 205 - unlock: 206 - i_mmap_unlock_read(mapping); 207 - out: 208 - write_seqcount_end(&xip_sparse_seq); 209 - mutex_unlock(&xip_sparse_mutex); 210 - 211 - return ret; 212 - } 213 - } 214 - 215 - static const struct vm_operations_struct xip_file_vm_ops = { 216 - .fault = xip_file_fault, 217 - .page_mkwrite = filemap_page_mkwrite, 218 - }; 219 - 220 - int xip_file_mmap(struct file * file, struct vm_area_struct * vma) 221 - { 222 - BUG_ON(!file->f_mapping->a_ops->get_xip_mem); 223 - 224 - file_accessed(file); 225 - vma->vm_ops = &xip_file_vm_ops; 226 - vma->vm_flags |= VM_MIXEDMAP; 227 - return 0; 228 - } 229 - EXPORT_SYMBOL_GPL(xip_file_mmap); 230 - 231 - /* 232 26 * truncate a page used for execute in place 233 27 * functionality is analog to block_truncate_page but does use get_xip_mem 234 28 * to get the page instead of page cache