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/sched.h>
29 #include <linux/sched/signal.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 #define CREATE_TRACE_POINTS
39 #include <trace/events/fs_dax.h>
41 /* We choose 4096 entries - same as per-zone page wait tables */
42 #define DAX_WAIT_TABLE_BITS 12
43 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
45 /* The 'colour' (ie low bits) within a PMD of a page offset. */
46 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
47 #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
49 static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
51 static int __init init_dax_wait_table(void)
55 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
56 init_waitqueue_head(wait_table + i);
59 fs_initcall(init_dax_wait_table);
62 * We use lowest available bit in exceptional entry for locking, one bit for
63 * the entry size (PMD) and two more to tell us if the entry is a zero page or
64 * an empty entry that is just used for locking. In total four special bits.
66 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
67 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
70 #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 4)
71 #define RADIX_DAX_ENTRY_LOCK (1 << RADIX_TREE_EXCEPTIONAL_SHIFT)
72 #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
73 #define RADIX_DAX_ZERO_PAGE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
74 #define RADIX_DAX_EMPTY (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 3))
76 static unsigned long dax_radix_sector(void *entry)
78 return (unsigned long)entry >> RADIX_DAX_SHIFT;
81 static void *dax_radix_locked_entry(sector_t sector, unsigned long flags)
83 return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags |
84 ((unsigned long)sector << RADIX_DAX_SHIFT) |
85 RADIX_DAX_ENTRY_LOCK);
88 static unsigned int dax_radix_order(void *entry)
90 if ((unsigned long)entry & RADIX_DAX_PMD)
91 return PMD_SHIFT - PAGE_SHIFT;
95 static int dax_is_pmd_entry(void *entry)
97 return (unsigned long)entry & RADIX_DAX_PMD;
100 static int dax_is_pte_entry(void *entry)
102 return !((unsigned long)entry & RADIX_DAX_PMD);
105 static int dax_is_zero_entry(void *entry)
107 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
110 static int dax_is_empty_entry(void *entry)
112 return (unsigned long)entry & RADIX_DAX_EMPTY;
116 * DAX radix tree locking
118 struct exceptional_entry_key {
119 struct address_space *mapping;
123 struct wait_exceptional_entry_queue {
124 wait_queue_entry_t wait;
125 struct exceptional_entry_key key;
128 static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
129 pgoff_t index, void *entry, struct exceptional_entry_key *key)
134 * If 'entry' is a PMD, align the 'index' that we use for the wait
135 * queue to the start of that PMD. This ensures that all offsets in
136 * the range covered by the PMD map to the same bit lock.
138 if (dax_is_pmd_entry(entry))
139 index &= ~PG_PMD_COLOUR;
141 key->mapping = mapping;
142 key->entry_start = index;
144 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
145 return wait_table + hash;
148 static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
149 int sync, void *keyp)
151 struct exceptional_entry_key *key = keyp;
152 struct wait_exceptional_entry_queue *ewait =
153 container_of(wait, struct wait_exceptional_entry_queue, wait);
155 if (key->mapping != ewait->key.mapping ||
156 key->entry_start != ewait->key.entry_start)
158 return autoremove_wake_function(wait, mode, sync, NULL);
162 * We do not necessarily hold the mapping->tree_lock when we call this
163 * function so it is possible that 'entry' is no longer a valid item in the
164 * radix tree. This is okay because all we really need to do is to find the
165 * correct waitqueue where tasks might be waiting for that old 'entry' and
168 static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
169 pgoff_t index, void *entry, bool wake_all)
171 struct exceptional_entry_key key;
172 wait_queue_head_t *wq;
174 wq = dax_entry_waitqueue(mapping, index, entry, &key);
177 * Checking for locked entry and prepare_to_wait_exclusive() happens
178 * under mapping->tree_lock, ditto for entry handling in our callers.
179 * So at this point all tasks that could have seen our entry locked
180 * must be in the waitqueue and the following check will see them.
182 if (waitqueue_active(wq))
183 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
187 * Check whether the given slot is locked. The function must be called with
188 * mapping->tree_lock held
190 static inline int slot_locked(struct address_space *mapping, void **slot)
192 unsigned long entry = (unsigned long)
193 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
194 return entry & RADIX_DAX_ENTRY_LOCK;
198 * Mark the given slot is locked. The function must be called with
199 * mapping->tree_lock held
201 static inline void *lock_slot(struct address_space *mapping, void **slot)
203 unsigned long entry = (unsigned long)
204 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
206 entry |= RADIX_DAX_ENTRY_LOCK;
207 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
208 return (void *)entry;
212 * Mark the given slot is unlocked. The function must be called with
213 * mapping->tree_lock held
215 static inline void *unlock_slot(struct address_space *mapping, void **slot)
217 unsigned long entry = (unsigned long)
218 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
220 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
221 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
222 return (void *)entry;
226 * Lookup entry in radix tree, wait for it to become unlocked if it is
227 * exceptional entry and return it. The caller must call
228 * put_unlocked_mapping_entry() when he decided not to lock the entry or
229 * put_locked_mapping_entry() when he locked the entry and now wants to
232 * The function must be called with mapping->tree_lock held.
234 static void *get_unlocked_mapping_entry(struct address_space *mapping,
235 pgoff_t index, void ***slotp)
238 struct wait_exceptional_entry_queue ewait;
239 wait_queue_head_t *wq;
241 init_wait(&ewait.wait);
242 ewait.wait.func = wake_exceptional_entry_func;
245 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
248 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
249 !slot_locked(mapping, slot)) {
255 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
256 prepare_to_wait_exclusive(wq, &ewait.wait,
257 TASK_UNINTERRUPTIBLE);
258 spin_unlock_irq(&mapping->tree_lock);
260 finish_wait(wq, &ewait.wait);
261 spin_lock_irq(&mapping->tree_lock);
265 static void dax_unlock_mapping_entry(struct address_space *mapping,
270 spin_lock_irq(&mapping->tree_lock);
271 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
272 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
273 !slot_locked(mapping, slot))) {
274 spin_unlock_irq(&mapping->tree_lock);
277 unlock_slot(mapping, slot);
278 spin_unlock_irq(&mapping->tree_lock);
279 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
282 static void put_locked_mapping_entry(struct address_space *mapping,
285 dax_unlock_mapping_entry(mapping, index);
289 * Called when we are done with radix tree entry we looked up via
290 * get_unlocked_mapping_entry() and which we didn't lock in the end.
292 static void put_unlocked_mapping_entry(struct address_space *mapping,
293 pgoff_t index, void *entry)
298 /* We have to wake up next waiter for the radix tree entry lock */
299 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
303 * Find radix tree entry at given index. If it points to an exceptional entry,
304 * return it with the radix tree entry locked. If the radix tree doesn't
305 * contain given index, create an empty exceptional entry for the index and
306 * return with it locked.
308 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
309 * either return that locked entry or will return an error. This error will
310 * happen if there are any 4k entries within the 2MiB range that we are
313 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
314 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
315 * insertion will fail if it finds any 4k entries already in the tree, and a
316 * 4k insertion will cause an existing 2MiB entry to be unmapped and
317 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
318 * well as 2MiB empty entries.
320 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
321 * real storage backing them. We will leave these real 2MiB DAX entries in
322 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
324 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
325 * persistent memory the benefit is doubtful. We can add that later if we can
328 static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
329 unsigned long size_flag)
331 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
335 spin_lock_irq(&mapping->tree_lock);
336 entry = get_unlocked_mapping_entry(mapping, index, &slot);
338 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
339 entry = ERR_PTR(-EIO);
344 if (size_flag & RADIX_DAX_PMD) {
345 if (dax_is_pte_entry(entry)) {
346 put_unlocked_mapping_entry(mapping, index,
348 entry = ERR_PTR(-EEXIST);
351 } else { /* trying to grab a PTE entry */
352 if (dax_is_pmd_entry(entry) &&
353 (dax_is_zero_entry(entry) ||
354 dax_is_empty_entry(entry))) {
355 pmd_downgrade = true;
360 /* No entry for given index? Make sure radix tree is big enough. */
361 if (!entry || pmd_downgrade) {
366 * Make sure 'entry' remains valid while we drop
367 * mapping->tree_lock.
369 entry = lock_slot(mapping, slot);
372 spin_unlock_irq(&mapping->tree_lock);
374 * Besides huge zero pages the only other thing that gets
375 * downgraded are empty entries which don't need to be
378 if (pmd_downgrade && dax_is_zero_entry(entry))
379 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
382 err = radix_tree_preload(
383 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
386 put_locked_mapping_entry(mapping, index);
389 spin_lock_irq(&mapping->tree_lock);
393 * We needed to drop the page_tree lock while calling
394 * radix_tree_preload() and we didn't have an entry to
395 * lock. See if another thread inserted an entry at
396 * our index during this time.
398 entry = __radix_tree_lookup(&mapping->page_tree, index,
401 radix_tree_preload_end();
402 spin_unlock_irq(&mapping->tree_lock);
408 radix_tree_delete(&mapping->page_tree, index);
409 mapping->nrexceptional--;
410 dax_wake_mapping_entry_waiter(mapping, index, entry,
414 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
416 err = __radix_tree_insert(&mapping->page_tree, index,
417 dax_radix_order(entry), entry);
418 radix_tree_preload_end();
420 spin_unlock_irq(&mapping->tree_lock);
422 * Our insertion of a DAX entry failed, most likely
423 * because we were inserting a PMD entry and it
424 * collided with a PTE sized entry at a different
425 * index in the PMD range. We haven't inserted
426 * anything into the radix tree and have no waiters to
431 /* Good, we have inserted empty locked entry into the tree. */
432 mapping->nrexceptional++;
433 spin_unlock_irq(&mapping->tree_lock);
436 entry = lock_slot(mapping, slot);
438 spin_unlock_irq(&mapping->tree_lock);
442 static int __dax_invalidate_mapping_entry(struct address_space *mapping,
443 pgoff_t index, bool trunc)
447 struct radix_tree_root *page_tree = &mapping->page_tree;
449 spin_lock_irq(&mapping->tree_lock);
450 entry = get_unlocked_mapping_entry(mapping, index, NULL);
451 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
454 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
455 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
457 radix_tree_delete(page_tree, index);
458 mapping->nrexceptional--;
461 put_unlocked_mapping_entry(mapping, index, entry);
462 spin_unlock_irq(&mapping->tree_lock);
466 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
467 * entry to get unlocked before deleting it.
469 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
471 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
474 * This gets called from truncate / punch_hole path. As such, the caller
475 * must hold locks protecting against concurrent modifications of the
476 * radix tree (usually fs-private i_mmap_sem for writing). Since the
477 * caller has seen exceptional entry for this index, we better find it
478 * at that index as well...
485 * Invalidate exceptional DAX entry if it is clean.
487 int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
490 return __dax_invalidate_mapping_entry(mapping, index, false);
493 static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
494 sector_t sector, size_t size, struct page *to,
503 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
507 id = dax_read_lock();
508 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
513 vto = kmap_atomic(to);
514 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
521 * By this point grab_mapping_entry() has ensured that we have a locked entry
522 * of the appropriate size so we don't have to worry about downgrading PMDs to
523 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
524 * already in the tree, we will skip the insertion and just dirty the PMD as
527 static void *dax_insert_mapping_entry(struct address_space *mapping,
528 struct vm_fault *vmf,
529 void *entry, sector_t sector,
530 unsigned long flags, bool dirty)
532 struct radix_tree_root *page_tree = &mapping->page_tree;
534 pgoff_t index = vmf->pgoff;
537 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
539 if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
540 /* we are replacing a zero page with block mapping */
541 if (dax_is_pmd_entry(entry))
542 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
545 unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
548 spin_lock_irq(&mapping->tree_lock);
549 new_entry = dax_radix_locked_entry(sector, flags);
551 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
553 * Only swap our new entry into the radix tree if the current
554 * entry is a zero page or an empty entry. If a normal PTE or
555 * PMD entry is already in the tree, we leave it alone. This
556 * means that if we are trying to insert a PTE and the
557 * existing entry is a PMD, we will just leave the PMD in the
558 * tree and dirty it if necessary.
560 struct radix_tree_node *node;
564 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
565 WARN_ON_ONCE(ret != entry);
566 __radix_tree_replace(page_tree, node, slot,
572 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
574 spin_unlock_irq(&mapping->tree_lock);
578 static inline unsigned long
579 pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
581 unsigned long address;
583 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
584 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
588 /* Walk all mappings of a given index of a file and writeprotect them */
589 static void dax_mapping_entry_mkclean(struct address_space *mapping,
590 pgoff_t index, unsigned long pfn)
592 struct vm_area_struct *vma;
593 pte_t pte, *ptep = NULL;
597 i_mmap_lock_read(mapping);
598 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
599 unsigned long address, start, end;
603 if (!(vma->vm_flags & VM_SHARED))
606 address = pgoff_address(index, vma);
609 * Note because we provide start/end to follow_pte_pmd it will
610 * call mmu_notifier_invalidate_range_start() on our behalf
611 * before taking any lock.
613 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
617 * No need to call mmu_notifier_invalidate_range() as we are
618 * downgrading page table protection not changing it to point
621 * See Documentation/vm/mmu_notifier.txt
624 #ifdef CONFIG_FS_DAX_PMD
627 if (pfn != pmd_pfn(*pmdp))
629 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
632 flush_cache_page(vma, address, pfn);
633 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
634 pmd = pmd_wrprotect(pmd);
635 pmd = pmd_mkclean(pmd);
636 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
641 if (pfn != pte_pfn(*ptep))
643 if (!pte_dirty(*ptep) && !pte_write(*ptep))
646 flush_cache_page(vma, address, pfn);
647 pte = ptep_clear_flush(vma, address, ptep);
648 pte = pte_wrprotect(pte);
649 pte = pte_mkclean(pte);
650 set_pte_at(vma->vm_mm, address, ptep, pte);
652 pte_unmap_unlock(ptep, ptl);
655 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
657 i_mmap_unlock_read(mapping);
660 static int dax_writeback_one(struct block_device *bdev,
661 struct dax_device *dax_dev, struct address_space *mapping,
662 pgoff_t index, void *entry)
664 struct radix_tree_root *page_tree = &mapping->page_tree;
665 void *entry2, **slot, *kaddr;
673 * A page got tagged dirty in DAX mapping? Something is seriously
676 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
679 spin_lock_irq(&mapping->tree_lock);
680 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
681 /* Entry got punched out / reallocated? */
682 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
685 * Entry got reallocated elsewhere? No need to writeback. We have to
686 * compare sectors as we must not bail out due to difference in lockbit
689 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
691 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
692 dax_is_zero_entry(entry))) {
697 /* Another fsync thread may have already written back this entry */
698 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
700 /* Lock the entry to serialize with page faults */
701 entry = lock_slot(mapping, slot);
703 * We can clear the tag now but we have to be careful so that concurrent
704 * dax_writeback_one() calls for the same index cannot finish before we
705 * actually flush the caches. This is achieved as the calls will look
706 * at the entry only under tree_lock and once they do that they will
707 * see the entry locked and wait for it to unlock.
709 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
710 spin_unlock_irq(&mapping->tree_lock);
713 * Even if dax_writeback_mapping_range() was given a wbc->range_start
714 * in the middle of a PMD, the 'index' we are given will be aligned to
715 * the start index of the PMD, as will the sector we pull from
716 * 'entry'. This allows us to flush for PMD_SIZE and not have to
717 * worry about partial PMD writebacks.
719 sector = dax_radix_sector(entry);
720 size = PAGE_SIZE << dax_radix_order(entry);
722 id = dax_read_lock();
723 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
728 * dax_direct_access() may sleep, so cannot hold tree_lock over
731 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
735 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
740 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
741 dax_flush(dax_dev, kaddr, size);
743 * After we have flushed the cache, we can clear the dirty tag. There
744 * cannot be new dirty data in the pfn after the flush has completed as
745 * the pfn mappings are writeprotected and fault waits for mapping
748 spin_lock_irq(&mapping->tree_lock);
749 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
750 spin_unlock_irq(&mapping->tree_lock);
751 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
754 put_locked_mapping_entry(mapping, index);
758 put_unlocked_mapping_entry(mapping, index, entry2);
759 spin_unlock_irq(&mapping->tree_lock);
764 * Flush the mapping to the persistent domain within the byte range of [start,
765 * end]. This is required by data integrity operations to ensure file data is
766 * on persistent storage prior to completion of the operation.
768 int dax_writeback_mapping_range(struct address_space *mapping,
769 struct block_device *bdev, struct writeback_control *wbc)
771 struct inode *inode = mapping->host;
772 pgoff_t start_index, end_index;
773 pgoff_t indices[PAGEVEC_SIZE];
774 struct dax_device *dax_dev;
779 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
782 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
785 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
789 start_index = wbc->range_start >> PAGE_SHIFT;
790 end_index = wbc->range_end >> PAGE_SHIFT;
792 trace_dax_writeback_range(inode, start_index, end_index);
794 tag_pages_for_writeback(mapping, start_index, end_index);
798 pvec.nr = find_get_entries_tag(mapping, start_index,
799 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
800 pvec.pages, indices);
805 for (i = 0; i < pvec.nr; i++) {
806 if (indices[i] > end_index) {
811 ret = dax_writeback_one(bdev, dax_dev, mapping,
812 indices[i], pvec.pages[i]);
814 mapping_set_error(mapping, ret);
818 start_index = indices[pvec.nr - 1] + 1;
822 trace_dax_writeback_range_done(inode, start_index, end_index);
823 return (ret < 0 ? ret : 0);
825 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
827 static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
829 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
832 static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
835 const sector_t sector = dax_iomap_sector(iomap, pos);
841 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
844 id = dax_read_lock();
845 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
852 if (PFN_PHYS(length) < size)
854 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
856 /* For larger pages we need devmap */
857 if (length > 1 && !pfn_t_devmap(*pfnp))
866 * The user has performed a load from a hole in the file. Allocating a new
867 * page in the file would cause excessive storage usage for workloads with
868 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
869 * If this page is ever written to we will re-fault and change the mapping to
870 * point to real DAX storage instead.
872 static int dax_load_hole(struct address_space *mapping, void *entry,
873 struct vm_fault *vmf)
875 struct inode *inode = mapping->host;
876 unsigned long vaddr = vmf->address;
877 int ret = VM_FAULT_NOPAGE;
878 struct page *zero_page;
881 zero_page = ZERO_PAGE(0);
882 if (unlikely(!zero_page)) {
887 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
888 RADIX_DAX_ZERO_PAGE, false);
889 if (IS_ERR(entry2)) {
890 ret = VM_FAULT_SIGBUS;
894 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
896 trace_dax_load_hole(inode, vmf, ret);
900 static bool dax_range_is_aligned(struct block_device *bdev,
901 unsigned int offset, unsigned int length)
903 unsigned short sector_size = bdev_logical_block_size(bdev);
905 if (!IS_ALIGNED(offset, sector_size))
907 if (!IS_ALIGNED(length, sector_size))
913 int __dax_zero_page_range(struct block_device *bdev,
914 struct dax_device *dax_dev, sector_t sector,
915 unsigned int offset, unsigned int size)
917 if (dax_range_is_aligned(bdev, offset, size)) {
918 sector_t start_sector = sector + (offset >> 9);
920 return blkdev_issue_zeroout(bdev, start_sector,
921 size >> 9, GFP_NOFS, 0);
928 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
932 id = dax_read_lock();
933 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
939 memset(kaddr + offset, 0, size);
940 dax_flush(dax_dev, kaddr + offset, size);
945 EXPORT_SYMBOL_GPL(__dax_zero_page_range);
948 dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
951 struct block_device *bdev = iomap->bdev;
952 struct dax_device *dax_dev = iomap->dax_dev;
953 struct iov_iter *iter = data;
954 loff_t end = pos + length, done = 0;
958 if (iov_iter_rw(iter) == READ) {
959 end = min(end, i_size_read(inode));
963 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
964 return iov_iter_zero(min(length, end - pos), iter);
967 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
971 * Write can allocate block for an area which has a hole page mapped
972 * into page tables. We have to tear down these mappings so that data
973 * written by write(2) is visible in mmap.
975 if (iomap->flags & IOMAP_F_NEW) {
976 invalidate_inode_pages2_range(inode->i_mapping,
978 (end - 1) >> PAGE_SHIFT);
981 id = dax_read_lock();
983 unsigned offset = pos & (PAGE_SIZE - 1);
984 const size_t size = ALIGN(length + offset, PAGE_SIZE);
985 const sector_t sector = dax_iomap_sector(iomap, pos);
991 if (fatal_signal_pending(current)) {
996 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1000 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
1007 map_len = PFN_PHYS(map_len);
1010 if (map_len > end - pos)
1011 map_len = end - pos;
1014 * The userspace address for the memory copy has already been
1015 * validated via access_ok() in either vfs_read() or
1016 * vfs_write(), depending on which operation we are doing.
1018 if (iov_iter_rw(iter) == WRITE)
1019 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1022 map_len = copy_to_iter(kaddr, map_len, iter);
1024 ret = map_len ? map_len : -EFAULT;
1032 dax_read_unlock(id);
1034 return done ? done : ret;
1038 * dax_iomap_rw - Perform I/O to a DAX file
1039 * @iocb: The control block for this I/O
1040 * @iter: The addresses to do I/O from or to
1041 * @ops: iomap ops passed from the file system
1043 * This function performs read and write operations to directly mapped
1044 * persistent memory. The callers needs to take care of read/write exclusion
1045 * and evicting any page cache pages in the region under I/O.
1048 dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
1049 const struct iomap_ops *ops)
1051 struct address_space *mapping = iocb->ki_filp->f_mapping;
1052 struct inode *inode = mapping->host;
1053 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1056 if (iov_iter_rw(iter) == WRITE) {
1057 lockdep_assert_held_exclusive(&inode->i_rwsem);
1058 flags |= IOMAP_WRITE;
1060 lockdep_assert_held(&inode->i_rwsem);
1063 while (iov_iter_count(iter)) {
1064 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
1065 iter, dax_iomap_actor);
1072 iocb->ki_pos += done;
1073 return done ? done : ret;
1075 EXPORT_SYMBOL_GPL(dax_iomap_rw);
1077 static int dax_fault_return(int error)
1080 return VM_FAULT_NOPAGE;
1081 if (error == -ENOMEM)
1082 return VM_FAULT_OOM;
1083 return VM_FAULT_SIGBUS;
1087 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1088 * flushed on write-faults (non-cow), but not read-faults.
1090 static bool dax_fault_is_synchronous(unsigned long flags,
1091 struct vm_area_struct *vma, struct iomap *iomap)
1093 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1094 && (iomap->flags & IOMAP_F_DIRTY);
1097 static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
1098 int *iomap_errp, const struct iomap_ops *ops)
1100 struct vm_area_struct *vma = vmf->vma;
1101 struct address_space *mapping = vma->vm_file->f_mapping;
1102 struct inode *inode = mapping->host;
1103 unsigned long vaddr = vmf->address;
1104 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1105 struct iomap iomap = { 0 };
1106 unsigned flags = IOMAP_FAULT;
1107 int error, major = 0;
1108 bool write = vmf->flags & FAULT_FLAG_WRITE;
1114 trace_dax_pte_fault(inode, vmf, vmf_ret);
1116 * Check whether offset isn't beyond end of file now. Caller is supposed
1117 * to hold locks serializing us with truncate / punch hole so this is
1120 if (pos >= i_size_read(inode)) {
1121 vmf_ret = VM_FAULT_SIGBUS;
1125 if (write && !vmf->cow_page)
1126 flags |= IOMAP_WRITE;
1128 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1129 if (IS_ERR(entry)) {
1130 vmf_ret = dax_fault_return(PTR_ERR(entry));
1135 * It is possible, particularly with mixed reads & writes to private
1136 * mappings, that we have raced with a PMD fault that overlaps with
1137 * the PTE we need to set up. If so just return and the fault will be
1140 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1141 vmf_ret = VM_FAULT_NOPAGE;
1146 * Note that we don't bother to use iomap_apply here: DAX required
1147 * the file system block size to be equal the page size, which means
1148 * that we never have to deal with more than a single extent here.
1150 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1152 *iomap_errp = error;
1154 vmf_ret = dax_fault_return(error);
1157 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1158 error = -EIO; /* fs corruption? */
1159 goto error_finish_iomap;
1162 if (vmf->cow_page) {
1163 sector_t sector = dax_iomap_sector(&iomap, pos);
1165 switch (iomap.type) {
1167 case IOMAP_UNWRITTEN:
1168 clear_user_highpage(vmf->cow_page, vaddr);
1171 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1172 sector, PAGE_SIZE, vmf->cow_page, vaddr);
1181 goto error_finish_iomap;
1183 __SetPageUptodate(vmf->cow_page);
1184 vmf_ret = finish_fault(vmf);
1186 vmf_ret = VM_FAULT_DONE_COW;
1190 sync = dax_fault_is_synchronous(flags, vma, &iomap);
1192 switch (iomap.type) {
1194 if (iomap.flags & IOMAP_F_NEW) {
1195 count_vm_event(PGMAJFAULT);
1196 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
1197 major = VM_FAULT_MAJOR;
1199 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1201 goto error_finish_iomap;
1203 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1204 dax_iomap_sector(&iomap, pos),
1206 if (IS_ERR(entry)) {
1207 error = PTR_ERR(entry);
1208 goto error_finish_iomap;
1212 * If we are doing synchronous page fault and inode needs fsync,
1213 * we can insert PTE into page tables only after that happens.
1214 * Skip insertion for now and return the pfn so that caller can
1215 * insert it after fsync is done.
1218 if (WARN_ON_ONCE(!pfnp)) {
1220 goto error_finish_iomap;
1223 vmf_ret = VM_FAULT_NEEDDSYNC | major;
1226 trace_dax_insert_mapping(inode, vmf, entry);
1228 error = vm_insert_mixed_mkwrite(vma, vaddr, pfn);
1230 error = vm_insert_mixed(vma, vaddr, pfn);
1232 /* -EBUSY is fine, somebody else faulted on the same PTE */
1233 if (error == -EBUSY)
1236 case IOMAP_UNWRITTEN:
1239 vmf_ret = dax_load_hole(mapping, entry, vmf);
1250 vmf_ret = dax_fault_return(error) | major;
1252 if (ops->iomap_end) {
1253 int copied = PAGE_SIZE;
1255 if (vmf_ret & VM_FAULT_ERROR)
1258 * The fault is done by now and there's no way back (other
1259 * thread may be already happily using PTE we have installed).
1260 * Just ignore error from ->iomap_end since we cannot do much
1263 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
1266 put_locked_mapping_entry(mapping, vmf->pgoff);
1268 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
1272 #ifdef CONFIG_FS_DAX_PMD
1273 static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
1276 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1277 unsigned long pmd_addr = vmf->address & PMD_MASK;
1278 struct inode *inode = mapping->host;
1279 struct page *zero_page;
1284 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
1286 if (unlikely(!zero_page))
1289 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
1290 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false);
1294 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1295 if (!pmd_none(*(vmf->pmd))) {
1300 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
1301 pmd_entry = pmd_mkhuge(pmd_entry);
1302 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
1304 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
1305 return VM_FAULT_NOPAGE;
1308 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
1309 return VM_FAULT_FALLBACK;
1312 static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
1313 const struct iomap_ops *ops)
1315 struct vm_area_struct *vma = vmf->vma;
1316 struct address_space *mapping = vma->vm_file->f_mapping;
1317 unsigned long pmd_addr = vmf->address & PMD_MASK;
1318 bool write = vmf->flags & FAULT_FLAG_WRITE;
1320 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
1321 struct inode *inode = mapping->host;
1322 int result = VM_FAULT_FALLBACK;
1323 struct iomap iomap = { 0 };
1324 pgoff_t max_pgoff, pgoff;
1331 * Check whether offset isn't beyond end of file now. Caller is
1332 * supposed to hold locks serializing us with truncate / punch hole so
1333 * this is a reliable test.
1335 pgoff = linear_page_index(vma, pmd_addr);
1336 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1338 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
1341 * Make sure that the faulting address's PMD offset (color) matches
1342 * the PMD offset from the start of the file. This is necessary so
1343 * that a PMD range in the page table overlaps exactly with a PMD
1344 * range in the radix tree.
1346 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1347 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1350 /* Fall back to PTEs if we're going to COW */
1351 if (write && !(vma->vm_flags & VM_SHARED))
1354 /* If the PMD would extend outside the VMA */
1355 if (pmd_addr < vma->vm_start)
1357 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1360 if (pgoff >= max_pgoff) {
1361 result = VM_FAULT_SIGBUS;
1365 /* If the PMD would extend beyond the file size */
1366 if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
1370 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1371 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1372 * is already in the tree, for instance), it will return -EEXIST and
1373 * we just fall back to 4k entries.
1375 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1380 * It is possible, particularly with mixed reads & writes to private
1381 * mappings, that we have raced with a PTE fault that overlaps with
1382 * the PMD we need to set up. If so just return and the fault will be
1385 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1386 !pmd_devmap(*vmf->pmd)) {
1392 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1393 * setting up a mapping, so really we're using iomap_begin() as a way
1394 * to look up our filesystem block.
1396 pos = (loff_t)pgoff << PAGE_SHIFT;
1397 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1401 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1404 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
1406 switch (iomap.type) {
1408 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1412 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1413 dax_iomap_sector(&iomap, pos),
1414 RADIX_DAX_PMD, write && !sync);
1419 * If we are doing synchronous page fault and inode needs fsync,
1420 * we can insert PMD into page tables only after that happens.
1421 * Skip insertion for now and return the pfn so that caller can
1422 * insert it after fsync is done.
1425 if (WARN_ON_ONCE(!pfnp))
1428 result = VM_FAULT_NEEDDSYNC;
1432 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1433 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1436 case IOMAP_UNWRITTEN:
1438 if (WARN_ON_ONCE(write))
1440 result = dax_pmd_load_hole(vmf, &iomap, entry);
1448 if (ops->iomap_end) {
1449 int copied = PMD_SIZE;
1451 if (result == VM_FAULT_FALLBACK)
1454 * The fault is done by now and there's no way back (other
1455 * thread may be already happily using PMD we have installed).
1456 * Just ignore error from ->iomap_end since we cannot do much
1459 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1463 put_locked_mapping_entry(mapping, pgoff);
1465 if (result == VM_FAULT_FALLBACK) {
1466 split_huge_pmd(vma, vmf->pmd, vmf->address);
1467 count_vm_event(THP_FAULT_FALLBACK);
1470 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
1474 static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
1475 const struct iomap_ops *ops)
1477 return VM_FAULT_FALLBACK;
1479 #endif /* CONFIG_FS_DAX_PMD */
1482 * dax_iomap_fault - handle a page fault on a DAX file
1483 * @vmf: The description of the fault
1484 * @pe_size: Size of the page to fault in
1485 * @pfnp: PFN to insert for synchronous faults if fsync is required
1486 * @iomap_errp: Storage for detailed error code in case of error
1487 * @ops: Iomap ops passed from the file system
1489 * When a page fault occurs, filesystems may call this helper in
1490 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1491 * has done all the necessary locking for page fault to proceed
1494 int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1495 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
1499 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
1501 return dax_iomap_pmd_fault(vmf, pfnp, ops);
1503 return VM_FAULT_FALLBACK;
1506 EXPORT_SYMBOL_GPL(dax_iomap_fault);
1509 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1510 * @vmf: The description of the fault
1511 * @pe_size: Size of entry to be inserted
1512 * @pfn: PFN to insert
1514 * This function inserts writeable PTE or PMD entry into page tables for mmaped
1515 * DAX file. It takes care of marking corresponding radix tree entry as dirty
1518 static int dax_insert_pfn_mkwrite(struct vm_fault *vmf,
1519 enum page_entry_size pe_size,
1522 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1523 void *entry, **slot;
1524 pgoff_t index = vmf->pgoff;
1527 spin_lock_irq(&mapping->tree_lock);
1528 entry = get_unlocked_mapping_entry(mapping, index, &slot);
1529 /* Did we race with someone splitting entry or so? */
1531 (pe_size == PE_SIZE_PTE && !dax_is_pte_entry(entry)) ||
1532 (pe_size == PE_SIZE_PMD && !dax_is_pmd_entry(entry))) {
1533 put_unlocked_mapping_entry(mapping, index, entry);
1534 spin_unlock_irq(&mapping->tree_lock);
1535 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1537 return VM_FAULT_NOPAGE;
1539 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
1540 entry = lock_slot(mapping, slot);
1541 spin_unlock_irq(&mapping->tree_lock);
1544 error = vm_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
1545 vmf_ret = dax_fault_return(error);
1547 #ifdef CONFIG_FS_DAX_PMD
1549 vmf_ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
1554 vmf_ret = VM_FAULT_FALLBACK;
1556 put_locked_mapping_entry(mapping, index);
1557 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, vmf_ret);
1562 * dax_finish_sync_fault - finish synchronous page fault
1563 * @vmf: The description of the fault
1564 * @pe_size: Size of entry to be inserted
1565 * @pfn: PFN to insert
1567 * This function ensures that the file range touched by the page fault is
1568 * stored persistently on the media and handles inserting of appropriate page
1571 int dax_finish_sync_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1575 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1578 if (pe_size == PE_SIZE_PTE)
1580 else if (pe_size == PE_SIZE_PMD)
1584 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1586 return VM_FAULT_SIGBUS;
1587 return dax_insert_pfn_mkwrite(vmf, pe_size, pfn);
1589 EXPORT_SYMBOL_GPL(dax_finish_sync_fault);