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/sched/signal.h>
31 #include <linux/uio.h>
32 #include <linux/vmstat.h>
33 #include <linux/pfn_t.h>
34 #include <linux/sizes.h>
35 #include <linux/mmu_notifier.h>
36 #include <linux/iomap.h>
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/fs_dax.h>
42 /* We choose 4096 entries - same as per-zone page wait tables */
43 #define DAX_WAIT_TABLE_BITS 12
44 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
46 static wait_queue_head_t wait_table
[DAX_WAIT_TABLE_ENTRIES
];
48 static int __init
init_dax_wait_table(void)
52 for (i
= 0; i
< DAX_WAIT_TABLE_ENTRIES
; i
++)
53 init_waitqueue_head(wait_table
+ i
);
56 fs_initcall(init_dax_wait_table
);
58 static long dax_map_atomic(struct block_device
*bdev
, struct blk_dax_ctl
*dax
)
60 struct request_queue
*q
= bdev
->bd_queue
;
63 dax
->addr
= ERR_PTR(-EIO
);
64 if (blk_queue_enter(q
, true) != 0)
67 rc
= bdev_direct_access(bdev
, dax
);
69 dax
->addr
= ERR_PTR(rc
);
76 static void dax_unmap_atomic(struct block_device
*bdev
,
77 const struct blk_dax_ctl
*dax
)
79 if (IS_ERR(dax
->addr
))
81 blk_queue_exit(bdev
->bd_queue
);
84 static int dax_is_pmd_entry(void *entry
)
86 return (unsigned long)entry
& RADIX_DAX_PMD
;
89 static int dax_is_pte_entry(void *entry
)
91 return !((unsigned long)entry
& RADIX_DAX_PMD
);
94 static int dax_is_zero_entry(void *entry
)
96 return (unsigned long)entry
& RADIX_DAX_HZP
;
99 static int dax_is_empty_entry(void *entry
)
101 return (unsigned long)entry
& RADIX_DAX_EMPTY
;
104 struct page
*read_dax_sector(struct block_device
*bdev
, sector_t n
)
106 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
107 struct blk_dax_ctl dax
= {
109 .sector
= n
& ~((((int) PAGE_SIZE
) / 512) - 1),
114 return ERR_PTR(-ENOMEM
);
116 rc
= dax_map_atomic(bdev
, &dax
);
119 memcpy_from_pmem(page_address(page
), dax
.addr
, PAGE_SIZE
);
120 dax_unmap_atomic(bdev
, &dax
);
125 * DAX radix tree locking
127 struct exceptional_entry_key
{
128 struct address_space
*mapping
;
132 struct wait_exceptional_entry_queue
{
134 struct exceptional_entry_key key
;
137 static wait_queue_head_t
*dax_entry_waitqueue(struct address_space
*mapping
,
138 pgoff_t index
, void *entry
, struct exceptional_entry_key
*key
)
143 * If 'entry' is a PMD, align the 'index' that we use for the wait
144 * queue to the start of that PMD. This ensures that all offsets in
145 * the range covered by the PMD map to the same bit lock.
147 if (dax_is_pmd_entry(entry
))
148 index
&= ~((1UL << (PMD_SHIFT
- PAGE_SHIFT
)) - 1);
150 key
->mapping
= mapping
;
151 key
->entry_start
= index
;
153 hash
= hash_long((unsigned long)mapping
^ index
, DAX_WAIT_TABLE_BITS
);
154 return wait_table
+ hash
;
157 static int wake_exceptional_entry_func(wait_queue_t
*wait
, unsigned int mode
,
158 int sync
, void *keyp
)
160 struct exceptional_entry_key
*key
= keyp
;
161 struct wait_exceptional_entry_queue
*ewait
=
162 container_of(wait
, struct wait_exceptional_entry_queue
, wait
);
164 if (key
->mapping
!= ewait
->key
.mapping
||
165 key
->entry_start
!= ewait
->key
.entry_start
)
167 return autoremove_wake_function(wait
, mode
, sync
, NULL
);
171 * Check whether the given slot is locked. The function must be called with
172 * mapping->tree_lock held
174 static inline int slot_locked(struct address_space
*mapping
, void **slot
)
176 unsigned long entry
= (unsigned long)
177 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
178 return entry
& RADIX_DAX_ENTRY_LOCK
;
182 * Mark the given slot is locked. The function must be called with
183 * mapping->tree_lock held
185 static inline void *lock_slot(struct address_space
*mapping
, void **slot
)
187 unsigned long entry
= (unsigned long)
188 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
190 entry
|= RADIX_DAX_ENTRY_LOCK
;
191 radix_tree_replace_slot(&mapping
->page_tree
, slot
, (void *)entry
);
192 return (void *)entry
;
196 * Mark the given slot is unlocked. The function must be called with
197 * mapping->tree_lock held
199 static inline void *unlock_slot(struct address_space
*mapping
, void **slot
)
201 unsigned long entry
= (unsigned long)
202 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
204 entry
&= ~(unsigned long)RADIX_DAX_ENTRY_LOCK
;
205 radix_tree_replace_slot(&mapping
->page_tree
, slot
, (void *)entry
);
206 return (void *)entry
;
210 * Lookup entry in radix tree, wait for it to become unlocked if it is
211 * exceptional entry and return it. The caller must call
212 * put_unlocked_mapping_entry() when he decided not to lock the entry or
213 * put_locked_mapping_entry() when he locked the entry and now wants to
216 * The function must be called with mapping->tree_lock held.
218 static void *get_unlocked_mapping_entry(struct address_space
*mapping
,
219 pgoff_t index
, void ***slotp
)
222 struct wait_exceptional_entry_queue ewait
;
223 wait_queue_head_t
*wq
;
225 init_wait(&ewait
.wait
);
226 ewait
.wait
.func
= wake_exceptional_entry_func
;
229 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
,
231 if (!entry
|| !radix_tree_exceptional_entry(entry
) ||
232 !slot_locked(mapping
, slot
)) {
238 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &ewait
.key
);
239 prepare_to_wait_exclusive(wq
, &ewait
.wait
,
240 TASK_UNINTERRUPTIBLE
);
241 spin_unlock_irq(&mapping
->tree_lock
);
243 finish_wait(wq
, &ewait
.wait
);
244 spin_lock_irq(&mapping
->tree_lock
);
248 static void dax_unlock_mapping_entry(struct address_space
*mapping
,
253 spin_lock_irq(&mapping
->tree_lock
);
254 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
, &slot
);
255 if (WARN_ON_ONCE(!entry
|| !radix_tree_exceptional_entry(entry
) ||
256 !slot_locked(mapping
, slot
))) {
257 spin_unlock_irq(&mapping
->tree_lock
);
260 unlock_slot(mapping
, slot
);
261 spin_unlock_irq(&mapping
->tree_lock
);
262 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
265 static void put_locked_mapping_entry(struct address_space
*mapping
,
266 pgoff_t index
, void *entry
)
268 if (!radix_tree_exceptional_entry(entry
)) {
272 dax_unlock_mapping_entry(mapping
, index
);
277 * Called when we are done with radix tree entry we looked up via
278 * get_unlocked_mapping_entry() and which we didn't lock in the end.
280 static void put_unlocked_mapping_entry(struct address_space
*mapping
,
281 pgoff_t index
, void *entry
)
283 if (!radix_tree_exceptional_entry(entry
))
286 /* We have to wake up next waiter for the radix tree entry lock */
287 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
291 * Find radix tree entry at given index. If it points to a page, return with
292 * the page locked. If it points to the exceptional entry, return with the
293 * radix tree entry locked. If the radix tree doesn't contain given index,
294 * create empty exceptional entry for the index and return with it locked.
296 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
297 * either return that locked entry or will return an error. This error will
298 * happen if there are any 4k entries (either zero pages or DAX entries)
299 * within the 2MiB range that we are requesting.
301 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
302 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
303 * insertion will fail if it finds any 4k entries already in the tree, and a
304 * 4k insertion will cause an existing 2MiB entry to be unmapped and
305 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
306 * well as 2MiB empty entries.
308 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
309 * real storage backing them. We will leave these real 2MiB DAX entries in
310 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
312 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
313 * persistent memory the benefit is doubtful. We can add that later if we can
316 static void *grab_mapping_entry(struct address_space
*mapping
, pgoff_t index
,
317 unsigned long size_flag
)
319 bool pmd_downgrade
= false; /* splitting 2MiB entry into 4k entries? */
323 spin_lock_irq(&mapping
->tree_lock
);
324 entry
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
327 if (size_flag
& RADIX_DAX_PMD
) {
328 if (!radix_tree_exceptional_entry(entry
) ||
329 dax_is_pte_entry(entry
)) {
330 put_unlocked_mapping_entry(mapping
, index
,
332 entry
= ERR_PTR(-EEXIST
);
335 } else { /* trying to grab a PTE entry */
336 if (radix_tree_exceptional_entry(entry
) &&
337 dax_is_pmd_entry(entry
) &&
338 (dax_is_zero_entry(entry
) ||
339 dax_is_empty_entry(entry
))) {
340 pmd_downgrade
= true;
345 /* No entry for given index? Make sure radix tree is big enough. */
346 if (!entry
|| pmd_downgrade
) {
351 * Make sure 'entry' remains valid while we drop
352 * mapping->tree_lock.
354 entry
= lock_slot(mapping
, slot
);
357 spin_unlock_irq(&mapping
->tree_lock
);
359 * Besides huge zero pages the only other thing that gets
360 * downgraded are empty entries which don't need to be
363 if (pmd_downgrade
&& dax_is_zero_entry(entry
))
364 unmap_mapping_range(mapping
,
365 (index
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
367 err
= radix_tree_preload(
368 mapping_gfp_mask(mapping
) & ~__GFP_HIGHMEM
);
371 put_locked_mapping_entry(mapping
, index
, entry
);
374 spin_lock_irq(&mapping
->tree_lock
);
378 * We needed to drop the page_tree lock while calling
379 * radix_tree_preload() and we didn't have an entry to
380 * lock. See if another thread inserted an entry at
381 * our index during this time.
383 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
,
386 radix_tree_preload_end();
387 spin_unlock_irq(&mapping
->tree_lock
);
393 radix_tree_delete(&mapping
->page_tree
, index
);
394 mapping
->nrexceptional
--;
395 dax_wake_mapping_entry_waiter(mapping
, index
, entry
,
399 entry
= dax_radix_locked_entry(0, size_flag
| RADIX_DAX_EMPTY
);
401 err
= __radix_tree_insert(&mapping
->page_tree
, index
,
402 dax_radix_order(entry
), entry
);
403 radix_tree_preload_end();
405 spin_unlock_irq(&mapping
->tree_lock
);
407 * Our insertion of a DAX entry failed, most likely
408 * because we were inserting a PMD entry and it
409 * collided with a PTE sized entry at a different
410 * index in the PMD range. We haven't inserted
411 * anything into the radix tree and have no waiters to
416 /* Good, we have inserted empty locked entry into the tree. */
417 mapping
->nrexceptional
++;
418 spin_unlock_irq(&mapping
->tree_lock
);
421 /* Normal page in radix tree? */
422 if (!radix_tree_exceptional_entry(entry
)) {
423 struct page
*page
= entry
;
426 spin_unlock_irq(&mapping
->tree_lock
);
428 /* Page got truncated? Retry... */
429 if (unlikely(page
->mapping
!= mapping
)) {
436 entry
= lock_slot(mapping
, slot
);
438 spin_unlock_irq(&mapping
->tree_lock
);
443 * We do not necessarily hold the mapping->tree_lock when we call this
444 * function so it is possible that 'entry' is no longer a valid item in the
445 * radix tree. This is okay because all we really need to do is to find the
446 * correct waitqueue where tasks might be waiting for that old 'entry' and
449 void dax_wake_mapping_entry_waiter(struct address_space
*mapping
,
450 pgoff_t index
, void *entry
, bool wake_all
)
452 struct exceptional_entry_key key
;
453 wait_queue_head_t
*wq
;
455 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &key
);
458 * Checking for locked entry and prepare_to_wait_exclusive() happens
459 * under mapping->tree_lock, ditto for entry handling in our callers.
460 * So at this point all tasks that could have seen our entry locked
461 * must be in the waitqueue and the following check will see them.
463 if (waitqueue_active(wq
))
464 __wake_up(wq
, TASK_NORMAL
, wake_all
? 0 : 1, &key
);
467 static int __dax_invalidate_mapping_entry(struct address_space
*mapping
,
468 pgoff_t index
, bool trunc
)
472 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
474 spin_lock_irq(&mapping
->tree_lock
);
475 entry
= get_unlocked_mapping_entry(mapping
, index
, NULL
);
476 if (!entry
|| !radix_tree_exceptional_entry(entry
))
479 (radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_DIRTY
) ||
480 radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
)))
482 radix_tree_delete(page_tree
, index
);
483 mapping
->nrexceptional
--;
486 put_unlocked_mapping_entry(mapping
, index
, entry
);
487 spin_unlock_irq(&mapping
->tree_lock
);
491 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
492 * entry to get unlocked before deleting it.
494 int dax_delete_mapping_entry(struct address_space
*mapping
, pgoff_t index
)
496 int ret
= __dax_invalidate_mapping_entry(mapping
, index
, true);
499 * This gets called from truncate / punch_hole path. As such, the caller
500 * must hold locks protecting against concurrent modifications of the
501 * radix tree (usually fs-private i_mmap_sem for writing). Since the
502 * caller has seen exceptional entry for this index, we better find it
503 * at that index as well...
510 * Invalidate exceptional DAX entry if it is clean.
512 int dax_invalidate_mapping_entry_sync(struct address_space
*mapping
,
515 return __dax_invalidate_mapping_entry(mapping
, index
, false);
519 * The user has performed a load from a hole in the file. Allocating
520 * a new page in the file would cause excessive storage usage for
521 * workloads with sparse files. We allocate a page cache page instead.
522 * We'll kick it out of the page cache if it's ever written to,
523 * otherwise it will simply fall out of the page cache under memory
524 * pressure without ever having been dirtied.
526 static int dax_load_hole(struct address_space
*mapping
, void **entry
,
527 struct vm_fault
*vmf
)
532 /* Hole page already exists? Return it... */
533 if (!radix_tree_exceptional_entry(*entry
)) {
538 /* This will replace locked radix tree entry with a hole page */
539 page
= find_or_create_page(mapping
, vmf
->pgoff
,
540 vmf
->gfp_mask
| __GFP_ZERO
);
545 ret
= finish_fault(vmf
);
549 /* Grab reference for PTE that is now referencing the page */
551 return VM_FAULT_NOPAGE
;
556 static int copy_user_dax(struct block_device
*bdev
, sector_t sector
, size_t size
,
557 struct page
*to
, unsigned long vaddr
)
559 struct blk_dax_ctl dax
= {
565 if (dax_map_atomic(bdev
, &dax
) < 0)
566 return PTR_ERR(dax
.addr
);
567 vto
= kmap_atomic(to
);
568 copy_user_page(vto
, (void __force
*)dax
.addr
, vaddr
, to
);
570 dax_unmap_atomic(bdev
, &dax
);
575 * By this point grab_mapping_entry() has ensured that we have a locked entry
576 * of the appropriate size so we don't have to worry about downgrading PMDs to
577 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
578 * already in the tree, we will skip the insertion and just dirty the PMD as
581 static void *dax_insert_mapping_entry(struct address_space
*mapping
,
582 struct vm_fault
*vmf
,
583 void *entry
, sector_t sector
,
586 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
588 bool hole_fill
= false;
590 pgoff_t index
= vmf
->pgoff
;
592 if (vmf
->flags
& FAULT_FLAG_WRITE
)
593 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
595 /* Replacing hole page with block mapping? */
596 if (!radix_tree_exceptional_entry(entry
)) {
599 * Unmap the page now before we remove it from page cache below.
600 * The page is locked so it cannot be faulted in again.
602 unmap_mapping_range(mapping
, vmf
->pgoff
<< PAGE_SHIFT
,
604 error
= radix_tree_preload(vmf
->gfp_mask
& ~__GFP_HIGHMEM
);
606 return ERR_PTR(error
);
607 } else if (dax_is_zero_entry(entry
) && !(flags
& RADIX_DAX_HZP
)) {
608 /* replacing huge zero page with PMD block mapping */
609 unmap_mapping_range(mapping
,
610 (vmf
->pgoff
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
613 spin_lock_irq(&mapping
->tree_lock
);
614 new_entry
= dax_radix_locked_entry(sector
, flags
);
617 __delete_from_page_cache(entry
, NULL
);
618 /* Drop pagecache reference */
620 error
= __radix_tree_insert(page_tree
, index
,
621 dax_radix_order(new_entry
), new_entry
);
623 new_entry
= ERR_PTR(error
);
626 mapping
->nrexceptional
++;
627 } else if (dax_is_zero_entry(entry
) || dax_is_empty_entry(entry
)) {
629 * Only swap our new entry into the radix tree if the current
630 * entry is a zero page or an empty entry. If a normal PTE or
631 * PMD entry is already in the tree, we leave it alone. This
632 * means that if we are trying to insert a PTE and the
633 * existing entry is a PMD, we will just leave the PMD in the
634 * tree and dirty it if necessary.
636 struct radix_tree_node
*node
;
640 ret
= __radix_tree_lookup(page_tree
, index
, &node
, &slot
);
641 WARN_ON_ONCE(ret
!= entry
);
642 __radix_tree_replace(page_tree
, node
, slot
,
643 new_entry
, NULL
, NULL
);
645 if (vmf
->flags
& FAULT_FLAG_WRITE
)
646 radix_tree_tag_set(page_tree
, index
, PAGECACHE_TAG_DIRTY
);
648 spin_unlock_irq(&mapping
->tree_lock
);
650 radix_tree_preload_end();
652 * We don't need hole page anymore, it has been replaced with
653 * locked radix tree entry now.
655 if (mapping
->a_ops
->freepage
)
656 mapping
->a_ops
->freepage(entry
);
663 static inline unsigned long
664 pgoff_address(pgoff_t pgoff
, struct vm_area_struct
*vma
)
666 unsigned long address
;
668 address
= vma
->vm_start
+ ((pgoff
- vma
->vm_pgoff
) << PAGE_SHIFT
);
669 VM_BUG_ON_VMA(address
< vma
->vm_start
|| address
>= vma
->vm_end
, vma
);
673 /* Walk all mappings of a given index of a file and writeprotect them */
674 static void dax_mapping_entry_mkclean(struct address_space
*mapping
,
675 pgoff_t index
, unsigned long pfn
)
677 struct vm_area_struct
*vma
;
678 pte_t pte
, *ptep
= NULL
;
683 i_mmap_lock_read(mapping
);
684 vma_interval_tree_foreach(vma
, &mapping
->i_mmap
, index
, index
) {
685 unsigned long address
;
689 if (!(vma
->vm_flags
& VM_SHARED
))
692 address
= pgoff_address(index
, vma
);
694 if (follow_pte_pmd(vma
->vm_mm
, address
, &ptep
, &pmdp
, &ptl
))
698 #ifdef CONFIG_FS_DAX_PMD
701 if (pfn
!= pmd_pfn(*pmdp
))
703 if (!pmd_dirty(*pmdp
) && !pmd_write(*pmdp
))
706 flush_cache_page(vma
, address
, pfn
);
707 pmd
= pmdp_huge_clear_flush(vma
, address
, pmdp
);
708 pmd
= pmd_wrprotect(pmd
);
709 pmd
= pmd_mkclean(pmd
);
710 set_pmd_at(vma
->vm_mm
, address
, pmdp
, pmd
);
716 if (pfn
!= pte_pfn(*ptep
))
718 if (!pte_dirty(*ptep
) && !pte_write(*ptep
))
721 flush_cache_page(vma
, address
, pfn
);
722 pte
= ptep_clear_flush(vma
, address
, ptep
);
723 pte
= pte_wrprotect(pte
);
724 pte
= pte_mkclean(pte
);
725 set_pte_at(vma
->vm_mm
, address
, ptep
, pte
);
728 pte_unmap_unlock(ptep
, ptl
);
732 mmu_notifier_invalidate_page(vma
->vm_mm
, address
);
734 i_mmap_unlock_read(mapping
);
737 static int dax_writeback_one(struct block_device
*bdev
,
738 struct address_space
*mapping
, pgoff_t index
, void *entry
)
740 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
741 struct blk_dax_ctl dax
;
742 void *entry2
, **slot
;
746 * A page got tagged dirty in DAX mapping? Something is seriously
749 if (WARN_ON(!radix_tree_exceptional_entry(entry
)))
752 spin_lock_irq(&mapping
->tree_lock
);
753 entry2
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
754 /* Entry got punched out / reallocated? */
755 if (!entry2
|| !radix_tree_exceptional_entry(entry2
))
758 * Entry got reallocated elsewhere? No need to writeback. We have to
759 * compare sectors as we must not bail out due to difference in lockbit
762 if (dax_radix_sector(entry2
) != dax_radix_sector(entry
))
764 if (WARN_ON_ONCE(dax_is_empty_entry(entry
) ||
765 dax_is_zero_entry(entry
))) {
770 /* Another fsync thread may have already written back this entry */
771 if (!radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
))
773 /* Lock the entry to serialize with page faults */
774 entry
= lock_slot(mapping
, slot
);
776 * We can clear the tag now but we have to be careful so that concurrent
777 * dax_writeback_one() calls for the same index cannot finish before we
778 * actually flush the caches. This is achieved as the calls will look
779 * at the entry only under tree_lock and once they do that they will
780 * see the entry locked and wait for it to unlock.
782 radix_tree_tag_clear(page_tree
, index
, PAGECACHE_TAG_TOWRITE
);
783 spin_unlock_irq(&mapping
->tree_lock
);
786 * Even if dax_writeback_mapping_range() was given a wbc->range_start
787 * in the middle of a PMD, the 'index' we are given will be aligned to
788 * the start index of the PMD, as will the sector we pull from
789 * 'entry'. This allows us to flush for PMD_SIZE and not have to
790 * worry about partial PMD writebacks.
792 dax
.sector
= dax_radix_sector(entry
);
793 dax
.size
= PAGE_SIZE
<< dax_radix_order(entry
);
796 * We cannot hold tree_lock while calling dax_map_atomic() because it
797 * eventually calls cond_resched().
799 ret
= dax_map_atomic(bdev
, &dax
);
801 put_locked_mapping_entry(mapping
, index
, entry
);
805 if (WARN_ON_ONCE(ret
< dax
.size
)) {
810 dax_mapping_entry_mkclean(mapping
, index
, pfn_t_to_pfn(dax
.pfn
));
811 wb_cache_pmem(dax
.addr
, dax
.size
);
813 * After we have flushed the cache, we can clear the dirty tag. There
814 * cannot be new dirty data in the pfn after the flush has completed as
815 * the pfn mappings are writeprotected and fault waits for mapping
818 spin_lock_irq(&mapping
->tree_lock
);
819 radix_tree_tag_clear(page_tree
, index
, PAGECACHE_TAG_DIRTY
);
820 spin_unlock_irq(&mapping
->tree_lock
);
822 dax_unmap_atomic(bdev
, &dax
);
823 put_locked_mapping_entry(mapping
, index
, entry
);
827 put_unlocked_mapping_entry(mapping
, index
, entry2
);
828 spin_unlock_irq(&mapping
->tree_lock
);
833 * Flush the mapping to the persistent domain within the byte range of [start,
834 * end]. This is required by data integrity operations to ensure file data is
835 * on persistent storage prior to completion of the operation.
837 int dax_writeback_mapping_range(struct address_space
*mapping
,
838 struct block_device
*bdev
, struct writeback_control
*wbc
)
840 struct inode
*inode
= mapping
->host
;
841 pgoff_t start_index
, end_index
;
842 pgoff_t indices
[PAGEVEC_SIZE
];
847 if (WARN_ON_ONCE(inode
->i_blkbits
!= PAGE_SHIFT
))
850 if (!mapping
->nrexceptional
|| wbc
->sync_mode
!= WB_SYNC_ALL
)
853 start_index
= wbc
->range_start
>> PAGE_SHIFT
;
854 end_index
= wbc
->range_end
>> PAGE_SHIFT
;
856 tag_pages_for_writeback(mapping
, start_index
, end_index
);
858 pagevec_init(&pvec
, 0);
860 pvec
.nr
= find_get_entries_tag(mapping
, start_index
,
861 PAGECACHE_TAG_TOWRITE
, PAGEVEC_SIZE
,
862 pvec
.pages
, indices
);
867 for (i
= 0; i
< pvec
.nr
; i
++) {
868 if (indices
[i
] > end_index
) {
873 ret
= dax_writeback_one(bdev
, mapping
, indices
[i
],
878 start_index
= indices
[pvec
.nr
- 1] + 1;
882 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range
);
884 static int dax_insert_mapping(struct address_space
*mapping
,
885 struct block_device
*bdev
, sector_t sector
, size_t size
,
886 void **entryp
, struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
888 unsigned long vaddr
= vmf
->address
;
889 struct blk_dax_ctl dax
= {
894 void *entry
= *entryp
;
896 if (dax_map_atomic(bdev
, &dax
) < 0)
897 return PTR_ERR(dax
.addr
);
898 dax_unmap_atomic(bdev
, &dax
);
900 ret
= dax_insert_mapping_entry(mapping
, vmf
, entry
, dax
.sector
, 0);
905 return vm_insert_mixed(vma
, vaddr
, dax
.pfn
);
909 * dax_pfn_mkwrite - handle first write to DAX page
910 * @vmf: The description of the fault
912 int dax_pfn_mkwrite(struct vm_fault
*vmf
)
914 struct file
*file
= vmf
->vma
->vm_file
;
915 struct address_space
*mapping
= file
->f_mapping
;
917 pgoff_t index
= vmf
->pgoff
;
919 spin_lock_irq(&mapping
->tree_lock
);
920 entry
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
921 if (!entry
|| !radix_tree_exceptional_entry(entry
)) {
923 put_unlocked_mapping_entry(mapping
, index
, entry
);
924 spin_unlock_irq(&mapping
->tree_lock
);
925 return VM_FAULT_NOPAGE
;
927 radix_tree_tag_set(&mapping
->page_tree
, index
, PAGECACHE_TAG_DIRTY
);
928 entry
= lock_slot(mapping
, slot
);
929 spin_unlock_irq(&mapping
->tree_lock
);
931 * If we race with somebody updating the PTE and finish_mkwrite_fault()
932 * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry
933 * the fault in either case.
935 finish_mkwrite_fault(vmf
);
936 put_locked_mapping_entry(mapping
, index
, entry
);
937 return VM_FAULT_NOPAGE
;
939 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite
);
941 static bool dax_range_is_aligned(struct block_device
*bdev
,
942 unsigned int offset
, unsigned int length
)
944 unsigned short sector_size
= bdev_logical_block_size(bdev
);
946 if (!IS_ALIGNED(offset
, sector_size
))
948 if (!IS_ALIGNED(length
, sector_size
))
954 int __dax_zero_page_range(struct block_device
*bdev
, sector_t sector
,
955 unsigned int offset
, unsigned int length
)
957 struct blk_dax_ctl dax
= {
962 if (dax_range_is_aligned(bdev
, offset
, length
)) {
963 sector_t start_sector
= dax
.sector
+ (offset
>> 9);
965 return blkdev_issue_zeroout(bdev
, start_sector
,
966 length
>> 9, GFP_NOFS
, true);
968 if (dax_map_atomic(bdev
, &dax
) < 0)
969 return PTR_ERR(dax
.addr
);
970 clear_pmem(dax
.addr
+ offset
, length
);
971 dax_unmap_atomic(bdev
, &dax
);
975 EXPORT_SYMBOL_GPL(__dax_zero_page_range
);
977 static sector_t
dax_iomap_sector(struct iomap
*iomap
, loff_t pos
)
979 return iomap
->blkno
+ (((pos
& PAGE_MASK
) - iomap
->offset
) >> 9);
983 dax_iomap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
986 struct iov_iter
*iter
= data
;
987 loff_t end
= pos
+ length
, done
= 0;
990 if (iov_iter_rw(iter
) == READ
) {
991 end
= min(end
, i_size_read(inode
));
995 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
996 return iov_iter_zero(min(length
, end
- pos
), iter
);
999 if (WARN_ON_ONCE(iomap
->type
!= IOMAP_MAPPED
))
1003 * Write can allocate block for an area which has a hole page mapped
1004 * into page tables. We have to tear down these mappings so that data
1005 * written by write(2) is visible in mmap.
1007 if (iomap
->flags
& IOMAP_F_NEW
) {
1008 invalidate_inode_pages2_range(inode
->i_mapping
,
1010 (end
- 1) >> PAGE_SHIFT
);
1014 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1015 struct blk_dax_ctl dax
= { 0 };
1018 if (fatal_signal_pending(current
)) {
1023 dax
.sector
= dax_iomap_sector(iomap
, pos
);
1024 dax
.size
= (length
+ offset
+ PAGE_SIZE
- 1) & PAGE_MASK
;
1025 map_len
= dax_map_atomic(iomap
->bdev
, &dax
);
1033 if (map_len
> end
- pos
)
1034 map_len
= end
- pos
;
1036 if (iov_iter_rw(iter
) == WRITE
)
1037 map_len
= copy_from_iter_pmem(dax
.addr
, map_len
, iter
);
1039 map_len
= copy_to_iter(dax
.addr
, map_len
, iter
);
1040 dax_unmap_atomic(iomap
->bdev
, &dax
);
1042 ret
= map_len
? map_len
: -EFAULT
;
1051 return done
? done
: ret
;
1055 * dax_iomap_rw - Perform I/O to a DAX file
1056 * @iocb: The control block for this I/O
1057 * @iter: The addresses to do I/O from or to
1058 * @ops: iomap ops passed from the file system
1060 * This function performs read and write operations to directly mapped
1061 * persistent memory. The callers needs to take care of read/write exclusion
1062 * and evicting any page cache pages in the region under I/O.
1065 dax_iomap_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
1066 const struct iomap_ops
*ops
)
1068 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1069 struct inode
*inode
= mapping
->host
;
1070 loff_t pos
= iocb
->ki_pos
, ret
= 0, done
= 0;
1073 if (iov_iter_rw(iter
) == WRITE
) {
1074 lockdep_assert_held_exclusive(&inode
->i_rwsem
);
1075 flags
|= IOMAP_WRITE
;
1077 lockdep_assert_held(&inode
->i_rwsem
);
1080 while (iov_iter_count(iter
)) {
1081 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
), flags
, ops
,
1082 iter
, dax_iomap_actor
);
1089 iocb
->ki_pos
+= done
;
1090 return done
? done
: ret
;
1092 EXPORT_SYMBOL_GPL(dax_iomap_rw
);
1094 static int dax_fault_return(int error
)
1097 return VM_FAULT_NOPAGE
;
1098 if (error
== -ENOMEM
)
1099 return VM_FAULT_OOM
;
1100 return VM_FAULT_SIGBUS
;
1103 static int dax_iomap_pte_fault(struct vm_fault
*vmf
,
1104 const struct iomap_ops
*ops
)
1106 struct address_space
*mapping
= vmf
->vma
->vm_file
->f_mapping
;
1107 struct inode
*inode
= mapping
->host
;
1108 unsigned long vaddr
= vmf
->address
;
1109 loff_t pos
= (loff_t
)vmf
->pgoff
<< PAGE_SHIFT
;
1111 struct iomap iomap
= { 0 };
1112 unsigned flags
= IOMAP_FAULT
;
1113 int error
, major
= 0;
1118 * Check whether offset isn't beyond end of file now. Caller is supposed
1119 * to hold locks serializing us with truncate / punch hole so this is
1122 if (pos
>= i_size_read(inode
))
1123 return VM_FAULT_SIGBUS
;
1125 if ((vmf
->flags
& FAULT_FLAG_WRITE
) && !vmf
->cow_page
)
1126 flags
|= IOMAP_WRITE
;
1128 entry
= grab_mapping_entry(mapping
, vmf
->pgoff
, 0);
1130 return dax_fault_return(PTR_ERR(entry
));
1133 * It is possible, particularly with mixed reads & writes to private
1134 * mappings, that we have raced with a PMD fault that overlaps with
1135 * the PTE we need to set up. If so just return and the fault will be
1138 if (pmd_trans_huge(*vmf
->pmd
) || pmd_devmap(*vmf
->pmd
)) {
1139 vmf_ret
= VM_FAULT_NOPAGE
;
1144 * Note that we don't bother to use iomap_apply here: DAX required
1145 * the file system block size to be equal the page size, which means
1146 * that we never have to deal with more than a single extent here.
1148 error
= ops
->iomap_begin(inode
, pos
, PAGE_SIZE
, flags
, &iomap
);
1150 vmf_ret
= dax_fault_return(error
);
1153 if (WARN_ON_ONCE(iomap
.offset
+ iomap
.length
< pos
+ PAGE_SIZE
)) {
1154 error
= -EIO
; /* fs corruption? */
1155 goto error_finish_iomap
;
1158 sector
= dax_iomap_sector(&iomap
, pos
);
1160 if (vmf
->cow_page
) {
1161 switch (iomap
.type
) {
1163 case IOMAP_UNWRITTEN
:
1164 clear_user_highpage(vmf
->cow_page
, vaddr
);
1167 error
= copy_user_dax(iomap
.bdev
, sector
, PAGE_SIZE
,
1168 vmf
->cow_page
, vaddr
);
1177 goto error_finish_iomap
;
1179 __SetPageUptodate(vmf
->cow_page
);
1180 vmf_ret
= finish_fault(vmf
);
1182 vmf_ret
= VM_FAULT_DONE_COW
;
1186 switch (iomap
.type
) {
1188 if (iomap
.flags
& IOMAP_F_NEW
) {
1189 count_vm_event(PGMAJFAULT
);
1190 mem_cgroup_count_vm_event(vmf
->vma
->vm_mm
, PGMAJFAULT
);
1191 major
= VM_FAULT_MAJOR
;
1193 error
= dax_insert_mapping(mapping
, iomap
.bdev
, sector
,
1194 PAGE_SIZE
, &entry
, vmf
->vma
, vmf
);
1195 /* -EBUSY is fine, somebody else faulted on the same PTE */
1196 if (error
== -EBUSY
)
1199 case IOMAP_UNWRITTEN
:
1201 if (!(vmf
->flags
& FAULT_FLAG_WRITE
)) {
1202 vmf_ret
= dax_load_hole(mapping
, &entry
, vmf
);
1213 vmf_ret
= dax_fault_return(error
) | major
;
1215 if (ops
->iomap_end
) {
1216 int copied
= PAGE_SIZE
;
1218 if (vmf_ret
& VM_FAULT_ERROR
)
1221 * The fault is done by now and there's no way back (other
1222 * thread may be already happily using PTE we have installed).
1223 * Just ignore error from ->iomap_end since we cannot do much
1226 ops
->iomap_end(inode
, pos
, PAGE_SIZE
, copied
, flags
, &iomap
);
1229 put_locked_mapping_entry(mapping
, vmf
->pgoff
, entry
);
1233 #ifdef CONFIG_FS_DAX_PMD
1235 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1236 * more often than one might expect in the below functions.
1238 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1240 static int dax_pmd_insert_mapping(struct vm_fault
*vmf
, struct iomap
*iomap
,
1241 loff_t pos
, void **entryp
)
1243 struct address_space
*mapping
= vmf
->vma
->vm_file
->f_mapping
;
1244 struct block_device
*bdev
= iomap
->bdev
;
1245 struct inode
*inode
= mapping
->host
;
1246 struct blk_dax_ctl dax
= {
1247 .sector
= dax_iomap_sector(iomap
, pos
),
1250 long length
= dax_map_atomic(bdev
, &dax
);
1253 if (length
< 0) /* dax_map_atomic() failed */
1255 if (length
< PMD_SIZE
)
1256 goto unmap_fallback
;
1257 if (pfn_t_to_pfn(dax
.pfn
) & PG_PMD_COLOUR
)
1258 goto unmap_fallback
;
1259 if (!pfn_t_devmap(dax
.pfn
))
1260 goto unmap_fallback
;
1262 dax_unmap_atomic(bdev
, &dax
);
1264 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, dax
.sector
,
1270 trace_dax_pmd_insert_mapping(inode
, vmf
, length
, dax
.pfn
, ret
);
1271 return vmf_insert_pfn_pmd(vmf
->vma
, vmf
->address
, vmf
->pmd
,
1272 dax
.pfn
, vmf
->flags
& FAULT_FLAG_WRITE
);
1275 dax_unmap_atomic(bdev
, &dax
);
1277 trace_dax_pmd_insert_mapping_fallback(inode
, vmf
, length
,
1279 return VM_FAULT_FALLBACK
;
1282 static int dax_pmd_load_hole(struct vm_fault
*vmf
, struct iomap
*iomap
,
1285 struct address_space
*mapping
= vmf
->vma
->vm_file
->f_mapping
;
1286 unsigned long pmd_addr
= vmf
->address
& PMD_MASK
;
1287 struct inode
*inode
= mapping
->host
;
1288 struct page
*zero_page
;
1293 zero_page
= mm_get_huge_zero_page(vmf
->vma
->vm_mm
);
1295 if (unlikely(!zero_page
))
1298 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, 0,
1299 RADIX_DAX_PMD
| RADIX_DAX_HZP
);
1304 ptl
= pmd_lock(vmf
->vma
->vm_mm
, vmf
->pmd
);
1305 if (!pmd_none(*(vmf
->pmd
))) {
1310 pmd_entry
= mk_pmd(zero_page
, vmf
->vma
->vm_page_prot
);
1311 pmd_entry
= pmd_mkhuge(pmd_entry
);
1312 set_pmd_at(vmf
->vma
->vm_mm
, pmd_addr
, vmf
->pmd
, pmd_entry
);
1314 trace_dax_pmd_load_hole(inode
, vmf
, zero_page
, ret
);
1315 return VM_FAULT_NOPAGE
;
1318 trace_dax_pmd_load_hole_fallback(inode
, vmf
, zero_page
, ret
);
1319 return VM_FAULT_FALLBACK
;
1322 static int dax_iomap_pmd_fault(struct vm_fault
*vmf
,
1323 const struct iomap_ops
*ops
)
1325 struct vm_area_struct
*vma
= vmf
->vma
;
1326 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1327 unsigned long pmd_addr
= vmf
->address
& PMD_MASK
;
1328 bool write
= vmf
->flags
& FAULT_FLAG_WRITE
;
1329 unsigned int iomap_flags
= (write
? IOMAP_WRITE
: 0) | IOMAP_FAULT
;
1330 struct inode
*inode
= mapping
->host
;
1331 int result
= VM_FAULT_FALLBACK
;
1332 struct iomap iomap
= { 0 };
1333 pgoff_t max_pgoff
, pgoff
;
1339 * Check whether offset isn't beyond end of file now. Caller is
1340 * supposed to hold locks serializing us with truncate / punch hole so
1341 * this is a reliable test.
1343 pgoff
= linear_page_index(vma
, pmd_addr
);
1344 max_pgoff
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
1346 trace_dax_pmd_fault(inode
, vmf
, max_pgoff
, 0);
1348 /* Fall back to PTEs if we're going to COW */
1349 if (write
&& !(vma
->vm_flags
& VM_SHARED
))
1352 /* If the PMD would extend outside the VMA */
1353 if (pmd_addr
< vma
->vm_start
)
1355 if ((pmd_addr
+ PMD_SIZE
) > vma
->vm_end
)
1358 if (pgoff
> max_pgoff
) {
1359 result
= VM_FAULT_SIGBUS
;
1363 /* If the PMD would extend beyond the file size */
1364 if ((pgoff
| PG_PMD_COLOUR
) > max_pgoff
)
1368 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1369 * PMD or a HZP entry. If it can't (because a 4k page is already in
1370 * the tree, for instance), it will return -EEXIST and we just fall
1371 * back to 4k entries.
1373 entry
= grab_mapping_entry(mapping
, pgoff
, RADIX_DAX_PMD
);
1378 * It is possible, particularly with mixed reads & writes to private
1379 * mappings, that we have raced with a PTE fault that overlaps with
1380 * the PMD we need to set up. If so just return and the fault will be
1383 if (!pmd_none(*vmf
->pmd
) && !pmd_trans_huge(*vmf
->pmd
) &&
1384 !pmd_devmap(*vmf
->pmd
)) {
1390 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1391 * setting up a mapping, so really we're using iomap_begin() as a way
1392 * to look up our filesystem block.
1394 pos
= (loff_t
)pgoff
<< PAGE_SHIFT
;
1395 error
= ops
->iomap_begin(inode
, pos
, PMD_SIZE
, iomap_flags
, &iomap
);
1399 if (iomap
.offset
+ iomap
.length
< pos
+ PMD_SIZE
)
1402 switch (iomap
.type
) {
1404 result
= dax_pmd_insert_mapping(vmf
, &iomap
, pos
, &entry
);
1406 case IOMAP_UNWRITTEN
:
1408 if (WARN_ON_ONCE(write
))
1410 result
= dax_pmd_load_hole(vmf
, &iomap
, &entry
);
1418 if (ops
->iomap_end
) {
1419 int copied
= PMD_SIZE
;
1421 if (result
== VM_FAULT_FALLBACK
)
1424 * The fault is done by now and there's no way back (other
1425 * thread may be already happily using PMD we have installed).
1426 * Just ignore error from ->iomap_end since we cannot do much
1429 ops
->iomap_end(inode
, pos
, PMD_SIZE
, copied
, iomap_flags
,
1433 put_locked_mapping_entry(mapping
, pgoff
, entry
);
1435 if (result
== VM_FAULT_FALLBACK
) {
1436 split_huge_pmd(vma
, vmf
->pmd
, vmf
->address
);
1437 count_vm_event(THP_FAULT_FALLBACK
);
1440 trace_dax_pmd_fault_done(inode
, vmf
, max_pgoff
, result
);
1444 static int dax_iomap_pmd_fault(struct vm_fault
*vmf
,
1445 const struct iomap_ops
*ops
)
1447 return VM_FAULT_FALLBACK
;
1449 #endif /* CONFIG_FS_DAX_PMD */
1452 * dax_iomap_fault - handle a page fault on a DAX file
1453 * @vmf: The description of the fault
1454 * @ops: iomap ops passed from the file system
1456 * When a page fault occurs, filesystems may call this helper in
1457 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1458 * has done all the necessary locking for page fault to proceed
1461 int dax_iomap_fault(struct vm_fault
*vmf
, enum page_entry_size pe_size
,
1462 const struct iomap_ops
*ops
)
1466 return dax_iomap_pte_fault(vmf
, ops
);
1468 return dax_iomap_pmd_fault(vmf
, ops
);
1470 return VM_FAULT_FALLBACK
;
1473 EXPORT_SYMBOL_GPL(dax_iomap_fault
);