2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/atomic.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
20 #include <linux/dax.h>
22 #include <linux/genhd.h>
23 #include <linux/highmem.h>
24 #include <linux/memcontrol.h>
26 #include <linux/mutex.h>
27 #include <linux/pagevec.h>
28 #include <linux/pmem.h>
29 #include <linux/sched.h>
30 #include <linux/uio.h>
31 #include <linux/vmstat.h>
32 #include <linux/pfn_t.h>
33 #include <linux/sizes.h>
34 #include <linux/mmu_notifier.h>
35 #include <linux/iomap.h>
38 /* We choose 4096 entries - same as per-zone page wait tables */
39 #define DAX_WAIT_TABLE_BITS 12
40 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
42 static wait_queue_head_t wait_table
[DAX_WAIT_TABLE_ENTRIES
];
44 static int __init
init_dax_wait_table(void)
48 for (i
= 0; i
< DAX_WAIT_TABLE_ENTRIES
; i
++)
49 init_waitqueue_head(wait_table
+ i
);
52 fs_initcall(init_dax_wait_table
);
54 static long dax_map_atomic(struct block_device
*bdev
, struct blk_dax_ctl
*dax
)
56 struct request_queue
*q
= bdev
->bd_queue
;
59 dax
->addr
= ERR_PTR(-EIO
);
60 if (blk_queue_enter(q
, true) != 0)
63 rc
= bdev_direct_access(bdev
, dax
);
65 dax
->addr
= ERR_PTR(rc
);
72 static void dax_unmap_atomic(struct block_device
*bdev
,
73 const struct blk_dax_ctl
*dax
)
75 if (IS_ERR(dax
->addr
))
77 blk_queue_exit(bdev
->bd_queue
);
80 static int dax_is_pmd_entry(void *entry
)
82 return (unsigned long)entry
& RADIX_DAX_PMD
;
85 static int dax_is_pte_entry(void *entry
)
87 return !((unsigned long)entry
& RADIX_DAX_PMD
);
90 static int dax_is_zero_entry(void *entry
)
92 return (unsigned long)entry
& RADIX_DAX_HZP
;
95 static int dax_is_empty_entry(void *entry
)
97 return (unsigned long)entry
& RADIX_DAX_EMPTY
;
100 struct page
*read_dax_sector(struct block_device
*bdev
, sector_t n
)
102 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
103 struct blk_dax_ctl dax
= {
105 .sector
= n
& ~((((int) PAGE_SIZE
) / 512) - 1),
110 return ERR_PTR(-ENOMEM
);
112 rc
= dax_map_atomic(bdev
, &dax
);
115 memcpy_from_pmem(page_address(page
), dax
.addr
, PAGE_SIZE
);
116 dax_unmap_atomic(bdev
, &dax
);
121 * DAX radix tree locking
123 struct exceptional_entry_key
{
124 struct address_space
*mapping
;
128 struct wait_exceptional_entry_queue
{
130 struct exceptional_entry_key key
;
133 static wait_queue_head_t
*dax_entry_waitqueue(struct address_space
*mapping
,
134 pgoff_t index
, void *entry
, struct exceptional_entry_key
*key
)
139 * If 'entry' is a PMD, align the 'index' that we use for the wait
140 * queue to the start of that PMD. This ensures that all offsets in
141 * the range covered by the PMD map to the same bit lock.
143 if (dax_is_pmd_entry(entry
))
144 index
&= ~((1UL << (PMD_SHIFT
- PAGE_SHIFT
)) - 1);
146 key
->mapping
= mapping
;
147 key
->entry_start
= index
;
149 hash
= hash_long((unsigned long)mapping
^ index
, DAX_WAIT_TABLE_BITS
);
150 return wait_table
+ hash
;
153 static int wake_exceptional_entry_func(wait_queue_t
*wait
, unsigned int mode
,
154 int sync
, void *keyp
)
156 struct exceptional_entry_key
*key
= keyp
;
157 struct wait_exceptional_entry_queue
*ewait
=
158 container_of(wait
, struct wait_exceptional_entry_queue
, wait
);
160 if (key
->mapping
!= ewait
->key
.mapping
||
161 key
->entry_start
!= ewait
->key
.entry_start
)
163 return autoremove_wake_function(wait
, mode
, sync
, NULL
);
167 * Check whether the given slot is locked. The function must be called with
168 * mapping->tree_lock held
170 static inline int slot_locked(struct address_space
*mapping
, void **slot
)
172 unsigned long entry
= (unsigned long)
173 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
174 return entry
& RADIX_DAX_ENTRY_LOCK
;
178 * Mark the given slot is locked. The function must be called with
179 * mapping->tree_lock held
181 static inline void *lock_slot(struct address_space
*mapping
, void **slot
)
183 unsigned long entry
= (unsigned long)
184 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
186 entry
|= RADIX_DAX_ENTRY_LOCK
;
187 radix_tree_replace_slot(&mapping
->page_tree
, slot
, (void *)entry
);
188 return (void *)entry
;
192 * Mark the given slot is unlocked. The function must be called with
193 * mapping->tree_lock held
195 static inline void *unlock_slot(struct address_space
*mapping
, void **slot
)
197 unsigned long entry
= (unsigned long)
198 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
200 entry
&= ~(unsigned long)RADIX_DAX_ENTRY_LOCK
;
201 radix_tree_replace_slot(&mapping
->page_tree
, slot
, (void *)entry
);
202 return (void *)entry
;
206 * Lookup entry in radix tree, wait for it to become unlocked if it is
207 * exceptional entry and return it. The caller must call
208 * put_unlocked_mapping_entry() when he decided not to lock the entry or
209 * put_locked_mapping_entry() when he locked the entry and now wants to
212 * The function must be called with mapping->tree_lock held.
214 static void *get_unlocked_mapping_entry(struct address_space
*mapping
,
215 pgoff_t index
, void ***slotp
)
218 struct wait_exceptional_entry_queue ewait
;
219 wait_queue_head_t
*wq
;
221 init_wait(&ewait
.wait
);
222 ewait
.wait
.func
= wake_exceptional_entry_func
;
225 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
,
227 if (!entry
|| !radix_tree_exceptional_entry(entry
) ||
228 !slot_locked(mapping
, slot
)) {
234 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &ewait
.key
);
235 prepare_to_wait_exclusive(wq
, &ewait
.wait
,
236 TASK_UNINTERRUPTIBLE
);
237 spin_unlock_irq(&mapping
->tree_lock
);
239 finish_wait(wq
, &ewait
.wait
);
240 spin_lock_irq(&mapping
->tree_lock
);
244 static void dax_unlock_mapping_entry(struct address_space
*mapping
,
249 spin_lock_irq(&mapping
->tree_lock
);
250 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
, &slot
);
251 if (WARN_ON_ONCE(!entry
|| !radix_tree_exceptional_entry(entry
) ||
252 !slot_locked(mapping
, slot
))) {
253 spin_unlock_irq(&mapping
->tree_lock
);
256 unlock_slot(mapping
, slot
);
257 spin_unlock_irq(&mapping
->tree_lock
);
258 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
261 static void put_locked_mapping_entry(struct address_space
*mapping
,
262 pgoff_t index
, void *entry
)
264 if (!radix_tree_exceptional_entry(entry
)) {
268 dax_unlock_mapping_entry(mapping
, index
);
273 * Called when we are done with radix tree entry we looked up via
274 * get_unlocked_mapping_entry() and which we didn't lock in the end.
276 static void put_unlocked_mapping_entry(struct address_space
*mapping
,
277 pgoff_t index
, void *entry
)
279 if (!radix_tree_exceptional_entry(entry
))
282 /* We have to wake up next waiter for the radix tree entry lock */
283 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
287 * Find radix tree entry at given index. If it points to a page, return with
288 * the page locked. If it points to the exceptional entry, return with the
289 * radix tree entry locked. If the radix tree doesn't contain given index,
290 * create empty exceptional entry for the index and return with it locked.
292 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
293 * either return that locked entry or will return an error. This error will
294 * happen if there are any 4k entries (either zero pages or DAX entries)
295 * within the 2MiB range that we are requesting.
297 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
298 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
299 * insertion will fail if it finds any 4k entries already in the tree, and a
300 * 4k insertion will cause an existing 2MiB entry to be unmapped and
301 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
302 * well as 2MiB empty entries.
304 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
305 * real storage backing them. We will leave these real 2MiB DAX entries in
306 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
308 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
309 * persistent memory the benefit is doubtful. We can add that later if we can
312 static void *grab_mapping_entry(struct address_space
*mapping
, pgoff_t index
,
313 unsigned long size_flag
)
315 bool pmd_downgrade
= false; /* splitting 2MiB entry into 4k entries? */
319 spin_lock_irq(&mapping
->tree_lock
);
320 entry
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
323 if (size_flag
& RADIX_DAX_PMD
) {
324 if (!radix_tree_exceptional_entry(entry
) ||
325 dax_is_pte_entry(entry
)) {
326 put_unlocked_mapping_entry(mapping
, index
,
328 entry
= ERR_PTR(-EEXIST
);
331 } else { /* trying to grab a PTE entry */
332 if (radix_tree_exceptional_entry(entry
) &&
333 dax_is_pmd_entry(entry
) &&
334 (dax_is_zero_entry(entry
) ||
335 dax_is_empty_entry(entry
))) {
336 pmd_downgrade
= true;
341 /* No entry for given index? Make sure radix tree is big enough. */
342 if (!entry
|| pmd_downgrade
) {
347 * Make sure 'entry' remains valid while we drop
348 * mapping->tree_lock.
350 entry
= lock_slot(mapping
, slot
);
353 spin_unlock_irq(&mapping
->tree_lock
);
355 * Besides huge zero pages the only other thing that gets
356 * downgraded are empty entries which don't need to be
359 if (pmd_downgrade
&& dax_is_zero_entry(entry
))
360 unmap_mapping_range(mapping
,
361 (index
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
363 err
= radix_tree_preload(
364 mapping_gfp_mask(mapping
) & ~__GFP_HIGHMEM
);
367 put_locked_mapping_entry(mapping
, index
, entry
);
370 spin_lock_irq(&mapping
->tree_lock
);
373 radix_tree_delete(&mapping
->page_tree
, index
);
374 mapping
->nrexceptional
--;
375 dax_wake_mapping_entry_waiter(mapping
, index
, entry
,
379 entry
= dax_radix_locked_entry(0, size_flag
| RADIX_DAX_EMPTY
);
381 err
= __radix_tree_insert(&mapping
->page_tree
, index
,
382 dax_radix_order(entry
), entry
);
383 radix_tree_preload_end();
385 spin_unlock_irq(&mapping
->tree_lock
);
387 * Someone already created the entry? This is a
388 * normal failure when inserting PMDs in a range
389 * that already contains PTEs. In that case we want
390 * to return -EEXIST immediately.
392 if (err
== -EEXIST
&& !(size_flag
& RADIX_DAX_PMD
))
395 * Our insertion of a DAX PMD entry failed, most
396 * likely because it collided with a PTE sized entry
397 * at a different index in the PMD range. We haven't
398 * inserted anything into the radix tree and have no
403 /* Good, we have inserted empty locked entry into the tree. */
404 mapping
->nrexceptional
++;
405 spin_unlock_irq(&mapping
->tree_lock
);
408 /* Normal page in radix tree? */
409 if (!radix_tree_exceptional_entry(entry
)) {
410 struct page
*page
= entry
;
413 spin_unlock_irq(&mapping
->tree_lock
);
415 /* Page got truncated? Retry... */
416 if (unlikely(page
->mapping
!= mapping
)) {
423 entry
= lock_slot(mapping
, slot
);
425 spin_unlock_irq(&mapping
->tree_lock
);
430 * We do not necessarily hold the mapping->tree_lock when we call this
431 * function so it is possible that 'entry' is no longer a valid item in the
432 * radix tree. This is okay because all we really need to do is to find the
433 * correct waitqueue where tasks might be waiting for that old 'entry' and
436 void dax_wake_mapping_entry_waiter(struct address_space
*mapping
,
437 pgoff_t index
, void *entry
, bool wake_all
)
439 struct exceptional_entry_key key
;
440 wait_queue_head_t
*wq
;
442 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &key
);
445 * Checking for locked entry and prepare_to_wait_exclusive() happens
446 * under mapping->tree_lock, ditto for entry handling in our callers.
447 * So at this point all tasks that could have seen our entry locked
448 * must be in the waitqueue and the following check will see them.
450 if (waitqueue_active(wq
))
451 __wake_up(wq
, TASK_NORMAL
, wake_all
? 0 : 1, &key
);
454 static int __dax_invalidate_mapping_entry(struct address_space
*mapping
,
455 pgoff_t index
, bool trunc
)
459 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
461 spin_lock_irq(&mapping
->tree_lock
);
462 entry
= get_unlocked_mapping_entry(mapping
, index
, NULL
);
463 if (!entry
|| !radix_tree_exceptional_entry(entry
))
466 (radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_DIRTY
) ||
467 radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
)))
469 radix_tree_delete(page_tree
, index
);
470 mapping
->nrexceptional
--;
473 put_unlocked_mapping_entry(mapping
, index
, entry
);
474 spin_unlock_irq(&mapping
->tree_lock
);
478 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
479 * entry to get unlocked before deleting it.
481 int dax_delete_mapping_entry(struct address_space
*mapping
, pgoff_t index
)
483 int ret
= __dax_invalidate_mapping_entry(mapping
, index
, true);
486 * This gets called from truncate / punch_hole path. As such, the caller
487 * must hold locks protecting against concurrent modifications of the
488 * radix tree (usually fs-private i_mmap_sem for writing). Since the
489 * caller has seen exceptional entry for this index, we better find it
490 * at that index as well...
497 * Invalidate exceptional DAX entry if easily possible. This handles DAX
498 * entries for invalidate_inode_pages() so we evict the entry only if we can
499 * do so without blocking.
501 int dax_invalidate_mapping_entry(struct address_space
*mapping
, pgoff_t index
)
505 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
507 spin_lock_irq(&mapping
->tree_lock
);
508 entry
= __radix_tree_lookup(page_tree
, index
, NULL
, &slot
);
509 if (!entry
|| !radix_tree_exceptional_entry(entry
) ||
510 slot_locked(mapping
, slot
))
512 if (radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_DIRTY
) ||
513 radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
))
515 radix_tree_delete(page_tree
, index
);
516 mapping
->nrexceptional
--;
519 spin_unlock_irq(&mapping
->tree_lock
);
521 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, true);
526 * Invalidate exceptional DAX entry if it is clean.
528 int dax_invalidate_mapping_entry_sync(struct address_space
*mapping
,
531 return __dax_invalidate_mapping_entry(mapping
, index
, false);
535 * The user has performed a load from a hole in the file. Allocating
536 * a new page in the file would cause excessive storage usage for
537 * workloads with sparse files. We allocate a page cache page instead.
538 * We'll kick it out of the page cache if it's ever written to,
539 * otherwise it will simply fall out of the page cache under memory
540 * pressure without ever having been dirtied.
542 static int dax_load_hole(struct address_space
*mapping
, void **entry
,
543 struct vm_fault
*vmf
)
548 /* Hole page already exists? Return it... */
549 if (!radix_tree_exceptional_entry(*entry
)) {
554 /* This will replace locked radix tree entry with a hole page */
555 page
= find_or_create_page(mapping
, vmf
->pgoff
,
556 vmf
->gfp_mask
| __GFP_ZERO
);
561 ret
= finish_fault(vmf
);
565 /* Grab reference for PTE that is now referencing the page */
567 return VM_FAULT_NOPAGE
;
572 static int copy_user_dax(struct block_device
*bdev
, sector_t sector
, size_t size
,
573 struct page
*to
, unsigned long vaddr
)
575 struct blk_dax_ctl dax
= {
581 if (dax_map_atomic(bdev
, &dax
) < 0)
582 return PTR_ERR(dax
.addr
);
583 vto
= kmap_atomic(to
);
584 copy_user_page(vto
, (void __force
*)dax
.addr
, vaddr
, to
);
586 dax_unmap_atomic(bdev
, &dax
);
591 * By this point grab_mapping_entry() has ensured that we have a locked entry
592 * of the appropriate size so we don't have to worry about downgrading PMDs to
593 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
594 * already in the tree, we will skip the insertion and just dirty the PMD as
597 static void *dax_insert_mapping_entry(struct address_space
*mapping
,
598 struct vm_fault
*vmf
,
599 void *entry
, sector_t sector
,
602 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
604 bool hole_fill
= false;
606 pgoff_t index
= vmf
->pgoff
;
608 if (vmf
->flags
& FAULT_FLAG_WRITE
)
609 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
611 /* Replacing hole page with block mapping? */
612 if (!radix_tree_exceptional_entry(entry
)) {
615 * Unmap the page now before we remove it from page cache below.
616 * The page is locked so it cannot be faulted in again.
618 unmap_mapping_range(mapping
, vmf
->pgoff
<< PAGE_SHIFT
,
620 error
= radix_tree_preload(vmf
->gfp_mask
& ~__GFP_HIGHMEM
);
622 return ERR_PTR(error
);
623 } else if (dax_is_zero_entry(entry
) && !(flags
& RADIX_DAX_HZP
)) {
624 /* replacing huge zero page with PMD block mapping */
625 unmap_mapping_range(mapping
,
626 (vmf
->pgoff
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
629 spin_lock_irq(&mapping
->tree_lock
);
630 new_entry
= dax_radix_locked_entry(sector
, flags
);
633 __delete_from_page_cache(entry
, NULL
);
634 /* Drop pagecache reference */
636 error
= __radix_tree_insert(page_tree
, index
,
637 dax_radix_order(new_entry
), new_entry
);
639 new_entry
= ERR_PTR(error
);
642 mapping
->nrexceptional
++;
643 } else if (dax_is_zero_entry(entry
) || dax_is_empty_entry(entry
)) {
645 * Only swap our new entry into the radix tree if the current
646 * entry is a zero page or an empty entry. If a normal PTE or
647 * PMD entry is already in the tree, we leave it alone. This
648 * means that if we are trying to insert a PTE and the
649 * existing entry is a PMD, we will just leave the PMD in the
650 * tree and dirty it if necessary.
652 struct radix_tree_node
*node
;
656 ret
= __radix_tree_lookup(page_tree
, index
, &node
, &slot
);
657 WARN_ON_ONCE(ret
!= entry
);
658 __radix_tree_replace(page_tree
, node
, slot
,
659 new_entry
, NULL
, NULL
);
661 if (vmf
->flags
& FAULT_FLAG_WRITE
)
662 radix_tree_tag_set(page_tree
, index
, PAGECACHE_TAG_DIRTY
);
664 spin_unlock_irq(&mapping
->tree_lock
);
666 radix_tree_preload_end();
668 * We don't need hole page anymore, it has been replaced with
669 * locked radix tree entry now.
671 if (mapping
->a_ops
->freepage
)
672 mapping
->a_ops
->freepage(entry
);
679 static inline unsigned long
680 pgoff_address(pgoff_t pgoff
, struct vm_area_struct
*vma
)
682 unsigned long address
;
684 address
= vma
->vm_start
+ ((pgoff
- vma
->vm_pgoff
) << PAGE_SHIFT
);
685 VM_BUG_ON_VMA(address
< vma
->vm_start
|| address
>= vma
->vm_end
, vma
);
689 /* Walk all mappings of a given index of a file and writeprotect them */
690 static void dax_mapping_entry_mkclean(struct address_space
*mapping
,
691 pgoff_t index
, unsigned long pfn
)
693 struct vm_area_struct
*vma
;
694 pte_t pte
, *ptep
= NULL
;
699 i_mmap_lock_read(mapping
);
700 vma_interval_tree_foreach(vma
, &mapping
->i_mmap
, index
, index
) {
701 unsigned long address
;
705 if (!(vma
->vm_flags
& VM_SHARED
))
708 address
= pgoff_address(index
, vma
);
710 if (follow_pte_pmd(vma
->vm_mm
, address
, &ptep
, &pmdp
, &ptl
))
714 #ifdef CONFIG_FS_DAX_PMD
717 if (pfn
!= pmd_pfn(*pmdp
))
719 if (!pmd_dirty(*pmdp
) && !pmd_write(*pmdp
))
722 flush_cache_page(vma
, address
, pfn
);
723 pmd
= pmdp_huge_clear_flush(vma
, address
, pmdp
);
724 pmd
= pmd_wrprotect(pmd
);
725 pmd
= pmd_mkclean(pmd
);
726 set_pmd_at(vma
->vm_mm
, address
, pmdp
, pmd
);
732 if (pfn
!= pte_pfn(*ptep
))
734 if (!pte_dirty(*ptep
) && !pte_write(*ptep
))
737 flush_cache_page(vma
, address
, pfn
);
738 pte
= ptep_clear_flush(vma
, address
, ptep
);
739 pte
= pte_wrprotect(pte
);
740 pte
= pte_mkclean(pte
);
741 set_pte_at(vma
->vm_mm
, address
, ptep
, pte
);
744 pte_unmap_unlock(ptep
, ptl
);
748 mmu_notifier_invalidate_page(vma
->vm_mm
, address
);
750 i_mmap_unlock_read(mapping
);
753 static int dax_writeback_one(struct block_device
*bdev
,
754 struct address_space
*mapping
, pgoff_t index
, void *entry
)
756 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
757 struct blk_dax_ctl dax
;
758 void *entry2
, **slot
;
762 * A page got tagged dirty in DAX mapping? Something is seriously
765 if (WARN_ON(!radix_tree_exceptional_entry(entry
)))
768 spin_lock_irq(&mapping
->tree_lock
);
769 entry2
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
770 /* Entry got punched out / reallocated? */
771 if (!entry2
|| !radix_tree_exceptional_entry(entry2
))
774 * Entry got reallocated elsewhere? No need to writeback. We have to
775 * compare sectors as we must not bail out due to difference in lockbit
778 if (dax_radix_sector(entry2
) != dax_radix_sector(entry
))
780 if (WARN_ON_ONCE(dax_is_empty_entry(entry
) ||
781 dax_is_zero_entry(entry
))) {
786 /* Another fsync thread may have already written back this entry */
787 if (!radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
))
789 /* Lock the entry to serialize with page faults */
790 entry
= lock_slot(mapping
, slot
);
792 * We can clear the tag now but we have to be careful so that concurrent
793 * dax_writeback_one() calls for the same index cannot finish before we
794 * actually flush the caches. This is achieved as the calls will look
795 * at the entry only under tree_lock and once they do that they will
796 * see the entry locked and wait for it to unlock.
798 radix_tree_tag_clear(page_tree
, index
, PAGECACHE_TAG_TOWRITE
);
799 spin_unlock_irq(&mapping
->tree_lock
);
802 * Even if dax_writeback_mapping_range() was given a wbc->range_start
803 * in the middle of a PMD, the 'index' we are given will be aligned to
804 * the start index of the PMD, as will the sector we pull from
805 * 'entry'. This allows us to flush for PMD_SIZE and not have to
806 * worry about partial PMD writebacks.
808 dax
.sector
= dax_radix_sector(entry
);
809 dax
.size
= PAGE_SIZE
<< dax_radix_order(entry
);
812 * We cannot hold tree_lock while calling dax_map_atomic() because it
813 * eventually calls cond_resched().
815 ret
= dax_map_atomic(bdev
, &dax
);
817 put_locked_mapping_entry(mapping
, index
, entry
);
821 if (WARN_ON_ONCE(ret
< dax
.size
)) {
826 dax_mapping_entry_mkclean(mapping
, index
, pfn_t_to_pfn(dax
.pfn
));
827 wb_cache_pmem(dax
.addr
, dax
.size
);
829 * After we have flushed the cache, we can clear the dirty tag. There
830 * cannot be new dirty data in the pfn after the flush has completed as
831 * the pfn mappings are writeprotected and fault waits for mapping
834 spin_lock_irq(&mapping
->tree_lock
);
835 radix_tree_tag_clear(page_tree
, index
, PAGECACHE_TAG_DIRTY
);
836 spin_unlock_irq(&mapping
->tree_lock
);
838 dax_unmap_atomic(bdev
, &dax
);
839 put_locked_mapping_entry(mapping
, index
, entry
);
843 put_unlocked_mapping_entry(mapping
, index
, entry2
);
844 spin_unlock_irq(&mapping
->tree_lock
);
849 * Flush the mapping to the persistent domain within the byte range of [start,
850 * end]. This is required by data integrity operations to ensure file data is
851 * on persistent storage prior to completion of the operation.
853 int dax_writeback_mapping_range(struct address_space
*mapping
,
854 struct block_device
*bdev
, struct writeback_control
*wbc
)
856 struct inode
*inode
= mapping
->host
;
857 pgoff_t start_index
, end_index
;
858 pgoff_t indices
[PAGEVEC_SIZE
];
863 if (WARN_ON_ONCE(inode
->i_blkbits
!= PAGE_SHIFT
))
866 if (!mapping
->nrexceptional
|| wbc
->sync_mode
!= WB_SYNC_ALL
)
869 start_index
= wbc
->range_start
>> PAGE_SHIFT
;
870 end_index
= wbc
->range_end
>> PAGE_SHIFT
;
872 tag_pages_for_writeback(mapping
, start_index
, end_index
);
874 pagevec_init(&pvec
, 0);
876 pvec
.nr
= find_get_entries_tag(mapping
, start_index
,
877 PAGECACHE_TAG_TOWRITE
, PAGEVEC_SIZE
,
878 pvec
.pages
, indices
);
883 for (i
= 0; i
< pvec
.nr
; i
++) {
884 if (indices
[i
] > end_index
) {
889 ret
= dax_writeback_one(bdev
, mapping
, indices
[i
],
897 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range
);
899 static int dax_insert_mapping(struct address_space
*mapping
,
900 struct block_device
*bdev
, sector_t sector
, size_t size
,
901 void **entryp
, struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
903 unsigned long vaddr
= vmf
->address
;
904 struct blk_dax_ctl dax
= {
909 void *entry
= *entryp
;
911 if (dax_map_atomic(bdev
, &dax
) < 0)
912 return PTR_ERR(dax
.addr
);
913 dax_unmap_atomic(bdev
, &dax
);
915 ret
= dax_insert_mapping_entry(mapping
, vmf
, entry
, dax
.sector
, 0);
920 return vm_insert_mixed(vma
, vaddr
, dax
.pfn
);
924 * dax_pfn_mkwrite - handle first write to DAX page
925 * @vma: The virtual memory area where the fault occurred
926 * @vmf: The description of the fault
928 int dax_pfn_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
930 struct file
*file
= vma
->vm_file
;
931 struct address_space
*mapping
= file
->f_mapping
;
933 pgoff_t index
= vmf
->pgoff
;
935 spin_lock_irq(&mapping
->tree_lock
);
936 entry
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
937 if (!entry
|| !radix_tree_exceptional_entry(entry
)) {
939 put_unlocked_mapping_entry(mapping
, index
, entry
);
940 spin_unlock_irq(&mapping
->tree_lock
);
941 return VM_FAULT_NOPAGE
;
943 radix_tree_tag_set(&mapping
->page_tree
, index
, PAGECACHE_TAG_DIRTY
);
944 entry
= lock_slot(mapping
, slot
);
945 spin_unlock_irq(&mapping
->tree_lock
);
947 * If we race with somebody updating the PTE and finish_mkwrite_fault()
948 * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry
949 * the fault in either case.
951 finish_mkwrite_fault(vmf
);
952 put_locked_mapping_entry(mapping
, index
, entry
);
953 return VM_FAULT_NOPAGE
;
955 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite
);
957 static bool dax_range_is_aligned(struct block_device
*bdev
,
958 unsigned int offset
, unsigned int length
)
960 unsigned short sector_size
= bdev_logical_block_size(bdev
);
962 if (!IS_ALIGNED(offset
, sector_size
))
964 if (!IS_ALIGNED(length
, sector_size
))
970 int __dax_zero_page_range(struct block_device
*bdev
, sector_t sector
,
971 unsigned int offset
, unsigned int length
)
973 struct blk_dax_ctl dax
= {
978 if (dax_range_is_aligned(bdev
, offset
, length
)) {
979 sector_t start_sector
= dax
.sector
+ (offset
>> 9);
981 return blkdev_issue_zeroout(bdev
, start_sector
,
982 length
>> 9, GFP_NOFS
, true);
984 if (dax_map_atomic(bdev
, &dax
) < 0)
985 return PTR_ERR(dax
.addr
);
986 clear_pmem(dax
.addr
+ offset
, length
);
987 dax_unmap_atomic(bdev
, &dax
);
991 EXPORT_SYMBOL_GPL(__dax_zero_page_range
);
993 static sector_t
dax_iomap_sector(struct iomap
*iomap
, loff_t pos
)
995 return iomap
->blkno
+ (((pos
& PAGE_MASK
) - iomap
->offset
) >> 9);
999 dax_iomap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
1000 struct iomap
*iomap
)
1002 struct iov_iter
*iter
= data
;
1003 loff_t end
= pos
+ length
, done
= 0;
1006 if (iov_iter_rw(iter
) == READ
) {
1007 end
= min(end
, i_size_read(inode
));
1011 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
1012 return iov_iter_zero(min(length
, end
- pos
), iter
);
1015 if (WARN_ON_ONCE(iomap
->type
!= IOMAP_MAPPED
))
1019 * Write can allocate block for an area which has a hole page mapped
1020 * into page tables. We have to tear down these mappings so that data
1021 * written by write(2) is visible in mmap.
1023 if ((iomap
->flags
& IOMAP_F_NEW
) && inode
->i_mapping
->nrpages
) {
1024 invalidate_inode_pages2_range(inode
->i_mapping
,
1026 (end
- 1) >> PAGE_SHIFT
);
1030 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1031 struct blk_dax_ctl dax
= { 0 };
1034 if (fatal_signal_pending(current
)) {
1039 dax
.sector
= dax_iomap_sector(iomap
, pos
);
1040 dax
.size
= (length
+ offset
+ PAGE_SIZE
- 1) & PAGE_MASK
;
1041 map_len
= dax_map_atomic(iomap
->bdev
, &dax
);
1049 if (map_len
> end
- pos
)
1050 map_len
= end
- pos
;
1052 if (iov_iter_rw(iter
) == WRITE
)
1053 map_len
= copy_from_iter_pmem(dax
.addr
, map_len
, iter
);
1055 map_len
= copy_to_iter(dax
.addr
, map_len
, iter
);
1056 dax_unmap_atomic(iomap
->bdev
, &dax
);
1058 ret
= map_len
? map_len
: -EFAULT
;
1067 return done
? done
: ret
;
1071 * dax_iomap_rw - Perform I/O to a DAX file
1072 * @iocb: The control block for this I/O
1073 * @iter: The addresses to do I/O from or to
1074 * @ops: iomap ops passed from the file system
1076 * This function performs read and write operations to directly mapped
1077 * persistent memory. The callers needs to take care of read/write exclusion
1078 * and evicting any page cache pages in the region under I/O.
1081 dax_iomap_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
1082 struct iomap_ops
*ops
)
1084 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1085 struct inode
*inode
= mapping
->host
;
1086 loff_t pos
= iocb
->ki_pos
, ret
= 0, done
= 0;
1089 if (iov_iter_rw(iter
) == WRITE
)
1090 flags
|= IOMAP_WRITE
;
1092 while (iov_iter_count(iter
)) {
1093 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
), flags
, ops
,
1094 iter
, dax_iomap_actor
);
1101 iocb
->ki_pos
+= done
;
1102 return done
? done
: ret
;
1104 EXPORT_SYMBOL_GPL(dax_iomap_rw
);
1106 static int dax_fault_return(int error
)
1109 return VM_FAULT_NOPAGE
;
1110 if (error
== -ENOMEM
)
1111 return VM_FAULT_OOM
;
1112 return VM_FAULT_SIGBUS
;
1116 * dax_iomap_fault - handle a page fault on a DAX file
1117 * @vma: The virtual memory area where the fault occurred
1118 * @vmf: The description of the fault
1119 * @ops: iomap ops passed from the file system
1121 * When a page fault occurs, filesystems may call this helper in their fault
1122 * or mkwrite handler for DAX files. Assumes the caller has done all the
1123 * necessary locking for the page fault to proceed successfully.
1125 int dax_iomap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
1126 struct iomap_ops
*ops
)
1128 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1129 struct inode
*inode
= mapping
->host
;
1130 unsigned long vaddr
= vmf
->address
;
1131 loff_t pos
= (loff_t
)vmf
->pgoff
<< PAGE_SHIFT
;
1133 struct iomap iomap
= { 0 };
1134 unsigned flags
= IOMAP_FAULT
;
1135 int error
, major
= 0;
1140 * Check whether offset isn't beyond end of file now. Caller is supposed
1141 * to hold locks serializing us with truncate / punch hole so this is
1144 if (pos
>= i_size_read(inode
))
1145 return VM_FAULT_SIGBUS
;
1147 if ((vmf
->flags
& FAULT_FLAG_WRITE
) && !vmf
->cow_page
)
1148 flags
|= IOMAP_WRITE
;
1151 * Note that we don't bother to use iomap_apply here: DAX required
1152 * the file system block size to be equal the page size, which means
1153 * that we never have to deal with more than a single extent here.
1155 error
= ops
->iomap_begin(inode
, pos
, PAGE_SIZE
, flags
, &iomap
);
1157 return dax_fault_return(error
);
1158 if (WARN_ON_ONCE(iomap
.offset
+ iomap
.length
< pos
+ PAGE_SIZE
)) {
1159 vmf_ret
= dax_fault_return(-EIO
); /* fs corruption? */
1163 entry
= grab_mapping_entry(mapping
, vmf
->pgoff
, 0);
1164 if (IS_ERR(entry
)) {
1165 vmf_ret
= dax_fault_return(PTR_ERR(entry
));
1169 sector
= dax_iomap_sector(&iomap
, pos
);
1171 if (vmf
->cow_page
) {
1172 switch (iomap
.type
) {
1174 case IOMAP_UNWRITTEN
:
1175 clear_user_highpage(vmf
->cow_page
, vaddr
);
1178 error
= copy_user_dax(iomap
.bdev
, sector
, PAGE_SIZE
,
1179 vmf
->cow_page
, vaddr
);
1188 goto error_unlock_entry
;
1190 __SetPageUptodate(vmf
->cow_page
);
1191 vmf_ret
= finish_fault(vmf
);
1193 vmf_ret
= VM_FAULT_DONE_COW
;
1197 switch (iomap
.type
) {
1199 if (iomap
.flags
& IOMAP_F_NEW
) {
1200 count_vm_event(PGMAJFAULT
);
1201 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1202 major
= VM_FAULT_MAJOR
;
1204 error
= dax_insert_mapping(mapping
, iomap
.bdev
, sector
,
1205 PAGE_SIZE
, &entry
, vma
, vmf
);
1206 /* -EBUSY is fine, somebody else faulted on the same PTE */
1207 if (error
== -EBUSY
)
1210 case IOMAP_UNWRITTEN
:
1212 if (!(vmf
->flags
& FAULT_FLAG_WRITE
)) {
1213 vmf_ret
= dax_load_hole(mapping
, &entry
, vmf
);
1224 vmf_ret
= dax_fault_return(error
) | major
;
1226 put_locked_mapping_entry(mapping
, vmf
->pgoff
, entry
);
1228 if (ops
->iomap_end
) {
1229 int copied
= PAGE_SIZE
;
1231 if (vmf_ret
& VM_FAULT_ERROR
)
1234 * The fault is done by now and there's no way back (other
1235 * thread may be already happily using PTE we have installed).
1236 * Just ignore error from ->iomap_end since we cannot do much
1239 ops
->iomap_end(inode
, pos
, PAGE_SIZE
, copied
, flags
, &iomap
);
1243 EXPORT_SYMBOL_GPL(dax_iomap_fault
);
1245 #ifdef CONFIG_FS_DAX_PMD
1247 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1248 * more often than one might expect in the below functions.
1250 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1252 static int dax_pmd_insert_mapping(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1253 struct vm_fault
*vmf
, unsigned long address
,
1254 struct iomap
*iomap
, loff_t pos
, bool write
, void **entryp
)
1256 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1257 struct block_device
*bdev
= iomap
->bdev
;
1258 struct blk_dax_ctl dax
= {
1259 .sector
= dax_iomap_sector(iomap
, pos
),
1262 long length
= dax_map_atomic(bdev
, &dax
);
1265 if (length
< 0) /* dax_map_atomic() failed */
1266 return VM_FAULT_FALLBACK
;
1267 if (length
< PMD_SIZE
)
1268 goto unmap_fallback
;
1269 if (pfn_t_to_pfn(dax
.pfn
) & PG_PMD_COLOUR
)
1270 goto unmap_fallback
;
1271 if (!pfn_t_devmap(dax
.pfn
))
1272 goto unmap_fallback
;
1274 dax_unmap_atomic(bdev
, &dax
);
1276 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, dax
.sector
,
1279 return VM_FAULT_FALLBACK
;
1282 return vmf_insert_pfn_pmd(vma
, address
, pmd
, dax
.pfn
, write
);
1285 dax_unmap_atomic(bdev
, &dax
);
1286 return VM_FAULT_FALLBACK
;
1289 static int dax_pmd_load_hole(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1290 struct vm_fault
*vmf
, unsigned long address
,
1291 struct iomap
*iomap
, void **entryp
)
1293 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1294 unsigned long pmd_addr
= address
& PMD_MASK
;
1295 struct page
*zero_page
;
1300 zero_page
= mm_get_huge_zero_page(vma
->vm_mm
);
1302 if (unlikely(!zero_page
))
1303 return VM_FAULT_FALLBACK
;
1305 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, 0,
1306 RADIX_DAX_PMD
| RADIX_DAX_HZP
);
1308 return VM_FAULT_FALLBACK
;
1311 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1312 if (!pmd_none(*pmd
)) {
1314 return VM_FAULT_FALLBACK
;
1317 pmd_entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
1318 pmd_entry
= pmd_mkhuge(pmd_entry
);
1319 set_pmd_at(vma
->vm_mm
, pmd_addr
, pmd
, pmd_entry
);
1321 return VM_FAULT_NOPAGE
;
1324 int dax_iomap_pmd_fault(struct vm_area_struct
*vma
, unsigned long address
,
1325 pmd_t
*pmd
, unsigned int flags
, struct iomap_ops
*ops
)
1327 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1328 unsigned long pmd_addr
= address
& PMD_MASK
;
1329 bool write
= flags
& FAULT_FLAG_WRITE
;
1330 unsigned int iomap_flags
= (write
? IOMAP_WRITE
: 0) | IOMAP_FAULT
;
1331 struct inode
*inode
= mapping
->host
;
1332 int result
= VM_FAULT_FALLBACK
;
1333 struct iomap iomap
= { 0 };
1334 pgoff_t max_pgoff
, pgoff
;
1335 struct vm_fault vmf
;
1340 /* Fall back to PTEs if we're going to COW */
1341 if (write
&& !(vma
->vm_flags
& VM_SHARED
))
1344 /* If the PMD would extend outside the VMA */
1345 if (pmd_addr
< vma
->vm_start
)
1347 if ((pmd_addr
+ PMD_SIZE
) > vma
->vm_end
)
1351 * Check whether offset isn't beyond end of file now. Caller is
1352 * supposed to hold locks serializing us with truncate / punch hole so
1353 * this is a reliable test.
1355 pgoff
= linear_page_index(vma
, pmd_addr
);
1356 max_pgoff
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
1358 if (pgoff
> max_pgoff
)
1359 return VM_FAULT_SIGBUS
;
1361 /* If the PMD would extend beyond the file size */
1362 if ((pgoff
| PG_PMD_COLOUR
) > max_pgoff
)
1366 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1367 * setting up a mapping, so really we're using iomap_begin() as a way
1368 * to look up our filesystem block.
1370 pos
= (loff_t
)pgoff
<< PAGE_SHIFT
;
1371 error
= ops
->iomap_begin(inode
, pos
, PMD_SIZE
, iomap_flags
, &iomap
);
1375 if (iomap
.offset
+ iomap
.length
< pos
+ PMD_SIZE
)
1379 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1380 * PMD or a HZP entry. If it can't (because a 4k page is already in
1381 * the tree, for instance), it will return -EEXIST and we just fall
1382 * back to 4k entries.
1384 entry
= grab_mapping_entry(mapping
, pgoff
, RADIX_DAX_PMD
);
1390 vmf
.gfp_mask
= mapping_gfp_mask(mapping
) | __GFP_IO
;
1392 switch (iomap
.type
) {
1394 result
= dax_pmd_insert_mapping(vma
, pmd
, &vmf
, address
,
1395 &iomap
, pos
, write
, &entry
);
1397 case IOMAP_UNWRITTEN
:
1399 if (WARN_ON_ONCE(write
))
1401 result
= dax_pmd_load_hole(vma
, pmd
, &vmf
, address
, &iomap
,
1410 put_locked_mapping_entry(mapping
, pgoff
, entry
);
1412 if (ops
->iomap_end
) {
1413 int copied
= PMD_SIZE
;
1415 if (result
== VM_FAULT_FALLBACK
)
1418 * The fault is done by now and there's no way back (other
1419 * thread may be already happily using PMD we have installed).
1420 * Just ignore error from ->iomap_end since we cannot do much
1423 ops
->iomap_end(inode
, pos
, PMD_SIZE
, copied
, iomap_flags
,
1427 if (result
== VM_FAULT_FALLBACK
) {
1428 split_huge_pmd(vma
, pmd
, address
);
1429 count_vm_event(THP_FAULT_FALLBACK
);
1433 EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault
);
1434 #endif /* CONFIG_FS_DAX_PMD */