]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/dax.c
dax: remove DAX code from page_cache_tree_insert()
[mirror_ubuntu-bionic-kernel.git] / fs / dax.c
CommitLineData
d475c634
MW
1/*
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>
6 *
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.
10 *
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
14 * more details.
15 */
16
17#include <linux/atomic.h>
18#include <linux/blkdev.h>
19#include <linux/buffer_head.h>
d77e92e2 20#include <linux/dax.h>
d475c634
MW
21#include <linux/fs.h>
22#include <linux/genhd.h>
f7ca90b1
MW
23#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
d475c634 26#include <linux/mutex.h>
9973c98e 27#include <linux/pagevec.h>
289c6aed 28#include <linux/sched.h>
f361bf4a 29#include <linux/sched/signal.h>
d475c634 30#include <linux/uio.h>
f7ca90b1 31#include <linux/vmstat.h>
34c0fd54 32#include <linux/pfn_t.h>
0e749e54 33#include <linux/sizes.h>
4b4bb46d 34#include <linux/mmu_notifier.h>
a254e568
CH
35#include <linux/iomap.h>
36#include "internal.h"
d475c634 37
282a8e03
RZ
38#define CREATE_TRACE_POINTS
39#include <trace/events/fs_dax.h>
40
ac401cc7
JK
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)
44
ce95ab0f 45static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
ac401cc7
JK
46
47static int __init init_dax_wait_table(void)
48{
49 int i;
50
51 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
52 init_waitqueue_head(wait_table + i);
53 return 0;
54}
55fs_initcall(init_dax_wait_table);
56
642261ac 57static int dax_is_pmd_entry(void *entry)
d1a5f2b4 58{
642261ac 59 return (unsigned long)entry & RADIX_DAX_PMD;
d1a5f2b4
DW
60}
61
642261ac 62static int dax_is_pte_entry(void *entry)
d475c634 63{
642261ac 64 return !((unsigned long)entry & RADIX_DAX_PMD);
d475c634
MW
65}
66
642261ac 67static int dax_is_zero_entry(void *entry)
d475c634 68{
91d25ba8 69 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
d475c634
MW
70}
71
642261ac 72static int dax_is_empty_entry(void *entry)
b2e0d162 73{
642261ac 74 return (unsigned long)entry & RADIX_DAX_EMPTY;
b2e0d162
DW
75}
76
ac401cc7
JK
77/*
78 * DAX radix tree locking
79 */
80struct exceptional_entry_key {
81 struct address_space *mapping;
63e95b5c 82 pgoff_t entry_start;
ac401cc7
JK
83};
84
85struct wait_exceptional_entry_queue {
ac6424b9 86 wait_queue_entry_t wait;
ac401cc7
JK
87 struct exceptional_entry_key key;
88};
89
63e95b5c
RZ
90static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
91 pgoff_t index, void *entry, struct exceptional_entry_key *key)
92{
93 unsigned long hash;
94
95 /*
96 * If 'entry' is a PMD, align the 'index' that we use for the wait
97 * queue to the start of that PMD. This ensures that all offsets in
98 * the range covered by the PMD map to the same bit lock.
99 */
642261ac 100 if (dax_is_pmd_entry(entry))
63e95b5c
RZ
101 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
102
103 key->mapping = mapping;
104 key->entry_start = index;
105
106 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
107 return wait_table + hash;
108}
109
ac6424b9 110static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
ac401cc7
JK
111 int sync, void *keyp)
112{
113 struct exceptional_entry_key *key = keyp;
114 struct wait_exceptional_entry_queue *ewait =
115 container_of(wait, struct wait_exceptional_entry_queue, wait);
116
117 if (key->mapping != ewait->key.mapping ||
63e95b5c 118 key->entry_start != ewait->key.entry_start)
ac401cc7
JK
119 return 0;
120 return autoremove_wake_function(wait, mode, sync, NULL);
121}
122
e30331ff
RZ
123/*
124 * We do not necessarily hold the mapping->tree_lock when we call this
125 * function so it is possible that 'entry' is no longer a valid item in the
126 * radix tree. This is okay because all we really need to do is to find the
127 * correct waitqueue where tasks might be waiting for that old 'entry' and
128 * wake them.
129 */
d01ad197 130static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
e30331ff
RZ
131 pgoff_t index, void *entry, bool wake_all)
132{
133 struct exceptional_entry_key key;
134 wait_queue_head_t *wq;
135
136 wq = dax_entry_waitqueue(mapping, index, entry, &key);
137
138 /*
139 * Checking for locked entry and prepare_to_wait_exclusive() happens
140 * under mapping->tree_lock, ditto for entry handling in our callers.
141 * So at this point all tasks that could have seen our entry locked
142 * must be in the waitqueue and the following check will see them.
143 */
144 if (waitqueue_active(wq))
145 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
146}
147
ac401cc7
JK
148/*
149 * Check whether the given slot is locked. The function must be called with
150 * mapping->tree_lock held
151 */
152static inline int slot_locked(struct address_space *mapping, void **slot)
153{
154 unsigned long entry = (unsigned long)
155 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
156 return entry & RADIX_DAX_ENTRY_LOCK;
157}
158
159/*
160 * Mark the given slot is locked. The function must be called with
161 * mapping->tree_lock held
162 */
163static inline void *lock_slot(struct address_space *mapping, void **slot)
164{
165 unsigned long entry = (unsigned long)
166 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
167
168 entry |= RADIX_DAX_ENTRY_LOCK;
6d75f366 169 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
170 return (void *)entry;
171}
172
173/*
174 * Mark the given slot is unlocked. The function must be called with
175 * mapping->tree_lock held
176 */
177static inline void *unlock_slot(struct address_space *mapping, void **slot)
178{
179 unsigned long entry = (unsigned long)
180 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
181
182 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
6d75f366 183 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
184 return (void *)entry;
185}
186
187/*
188 * Lookup entry in radix tree, wait for it to become unlocked if it is
189 * exceptional entry and return it. The caller must call
190 * put_unlocked_mapping_entry() when he decided not to lock the entry or
191 * put_locked_mapping_entry() when he locked the entry and now wants to
192 * unlock it.
193 *
194 * The function must be called with mapping->tree_lock held.
195 */
196static void *get_unlocked_mapping_entry(struct address_space *mapping,
197 pgoff_t index, void ***slotp)
198{
e3ad61c6 199 void *entry, **slot;
ac401cc7 200 struct wait_exceptional_entry_queue ewait;
63e95b5c 201 wait_queue_head_t *wq;
ac401cc7
JK
202
203 init_wait(&ewait.wait);
204 ewait.wait.func = wake_exceptional_entry_func;
ac401cc7
JK
205
206 for (;;) {
e3ad61c6 207 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
ac401cc7 208 &slot);
91d25ba8
RZ
209 if (!entry ||
210 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
ac401cc7
JK
211 !slot_locked(mapping, slot)) {
212 if (slotp)
213 *slotp = slot;
e3ad61c6 214 return entry;
ac401cc7 215 }
63e95b5c
RZ
216
217 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
ac401cc7
JK
218 prepare_to_wait_exclusive(wq, &ewait.wait,
219 TASK_UNINTERRUPTIBLE);
220 spin_unlock_irq(&mapping->tree_lock);
221 schedule();
222 finish_wait(wq, &ewait.wait);
223 spin_lock_irq(&mapping->tree_lock);
224 }
225}
226
b1aa812b
JK
227static void dax_unlock_mapping_entry(struct address_space *mapping,
228 pgoff_t index)
229{
230 void *entry, **slot;
231
232 spin_lock_irq(&mapping->tree_lock);
233 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
234 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
235 !slot_locked(mapping, slot))) {
236 spin_unlock_irq(&mapping->tree_lock);
237 return;
238 }
239 unlock_slot(mapping, slot);
240 spin_unlock_irq(&mapping->tree_lock);
241 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
242}
243
422476c4 244static void put_locked_mapping_entry(struct address_space *mapping,
91d25ba8 245 pgoff_t index)
422476c4 246{
91d25ba8 247 dax_unlock_mapping_entry(mapping, index);
422476c4
RZ
248}
249
250/*
251 * Called when we are done with radix tree entry we looked up via
252 * get_unlocked_mapping_entry() and which we didn't lock in the end.
253 */
254static void put_unlocked_mapping_entry(struct address_space *mapping,
255 pgoff_t index, void *entry)
256{
91d25ba8 257 if (!entry)
422476c4
RZ
258 return;
259
260 /* We have to wake up next waiter for the radix tree entry lock */
261 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
262}
263
ac401cc7 264/*
91d25ba8
RZ
265 * Find radix tree entry at given index. If it points to an exceptional entry,
266 * return it with the radix tree entry locked. If the radix tree doesn't
267 * contain given index, create an empty exceptional entry for the index and
268 * return with it locked.
ac401cc7 269 *
642261ac
RZ
270 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
271 * either return that locked entry or will return an error. This error will
91d25ba8
RZ
272 * happen if there are any 4k entries within the 2MiB range that we are
273 * requesting.
642261ac
RZ
274 *
275 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
276 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
277 * insertion will fail if it finds any 4k entries already in the tree, and a
278 * 4k insertion will cause an existing 2MiB entry to be unmapped and
279 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
280 * well as 2MiB empty entries.
281 *
282 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
283 * real storage backing them. We will leave these real 2MiB DAX entries in
284 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
285 *
ac401cc7
JK
286 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
287 * persistent memory the benefit is doubtful. We can add that later if we can
288 * show it helps.
289 */
642261ac
RZ
290static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
291 unsigned long size_flag)
ac401cc7 292{
642261ac 293 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
e3ad61c6 294 void *entry, **slot;
ac401cc7
JK
295
296restart:
297 spin_lock_irq(&mapping->tree_lock);
e3ad61c6 298 entry = get_unlocked_mapping_entry(mapping, index, &slot);
642261ac 299
91d25ba8
RZ
300 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
301 entry = ERR_PTR(-EIO);
302 goto out_unlock;
303 }
304
642261ac
RZ
305 if (entry) {
306 if (size_flag & RADIX_DAX_PMD) {
91d25ba8 307 if (dax_is_pte_entry(entry)) {
642261ac
RZ
308 put_unlocked_mapping_entry(mapping, index,
309 entry);
310 entry = ERR_PTR(-EEXIST);
311 goto out_unlock;
312 }
313 } else { /* trying to grab a PTE entry */
91d25ba8 314 if (dax_is_pmd_entry(entry) &&
642261ac
RZ
315 (dax_is_zero_entry(entry) ||
316 dax_is_empty_entry(entry))) {
317 pmd_downgrade = true;
318 }
319 }
320 }
321
ac401cc7 322 /* No entry for given index? Make sure radix tree is big enough. */
642261ac 323 if (!entry || pmd_downgrade) {
ac401cc7
JK
324 int err;
325
642261ac
RZ
326 if (pmd_downgrade) {
327 /*
328 * Make sure 'entry' remains valid while we drop
329 * mapping->tree_lock.
330 */
331 entry = lock_slot(mapping, slot);
332 }
333
ac401cc7 334 spin_unlock_irq(&mapping->tree_lock);
642261ac
RZ
335 /*
336 * Besides huge zero pages the only other thing that gets
337 * downgraded are empty entries which don't need to be
338 * unmapped.
339 */
340 if (pmd_downgrade && dax_is_zero_entry(entry))
341 unmap_mapping_range(mapping,
342 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
343
ac401cc7
JK
344 err = radix_tree_preload(
345 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
0cb80b48
JK
346 if (err) {
347 if (pmd_downgrade)
91d25ba8 348 put_locked_mapping_entry(mapping, index);
ac401cc7 349 return ERR_PTR(err);
0cb80b48 350 }
ac401cc7 351 spin_lock_irq(&mapping->tree_lock);
642261ac 352
e11f8b7b
RZ
353 if (!entry) {
354 /*
355 * We needed to drop the page_tree lock while calling
356 * radix_tree_preload() and we didn't have an entry to
357 * lock. See if another thread inserted an entry at
358 * our index during this time.
359 */
360 entry = __radix_tree_lookup(&mapping->page_tree, index,
361 NULL, &slot);
362 if (entry) {
363 radix_tree_preload_end();
364 spin_unlock_irq(&mapping->tree_lock);
365 goto restart;
366 }
367 }
368
642261ac
RZ
369 if (pmd_downgrade) {
370 radix_tree_delete(&mapping->page_tree, index);
371 mapping->nrexceptional--;
372 dax_wake_mapping_entry_waiter(mapping, index, entry,
373 true);
374 }
375
376 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
377
378 err = __radix_tree_insert(&mapping->page_tree, index,
379 dax_radix_order(entry), entry);
ac401cc7
JK
380 radix_tree_preload_end();
381 if (err) {
382 spin_unlock_irq(&mapping->tree_lock);
642261ac 383 /*
e11f8b7b
RZ
384 * Our insertion of a DAX entry failed, most likely
385 * because we were inserting a PMD entry and it
386 * collided with a PTE sized entry at a different
387 * index in the PMD range. We haven't inserted
388 * anything into the radix tree and have no waiters to
389 * wake.
642261ac 390 */
ac401cc7
JK
391 return ERR_PTR(err);
392 }
393 /* Good, we have inserted empty locked entry into the tree. */
394 mapping->nrexceptional++;
395 spin_unlock_irq(&mapping->tree_lock);
e3ad61c6 396 return entry;
ac401cc7 397 }
e3ad61c6 398 entry = lock_slot(mapping, slot);
642261ac 399 out_unlock:
ac401cc7 400 spin_unlock_irq(&mapping->tree_lock);
e3ad61c6 401 return entry;
ac401cc7
JK
402}
403
c6dcf52c
JK
404static int __dax_invalidate_mapping_entry(struct address_space *mapping,
405 pgoff_t index, bool trunc)
406{
407 int ret = 0;
408 void *entry;
409 struct radix_tree_root *page_tree = &mapping->page_tree;
410
411 spin_lock_irq(&mapping->tree_lock);
412 entry = get_unlocked_mapping_entry(mapping, index, NULL);
91d25ba8 413 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
c6dcf52c
JK
414 goto out;
415 if (!trunc &&
416 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
417 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
418 goto out;
419 radix_tree_delete(page_tree, index);
420 mapping->nrexceptional--;
421 ret = 1;
422out:
423 put_unlocked_mapping_entry(mapping, index, entry);
424 spin_unlock_irq(&mapping->tree_lock);
425 return ret;
426}
ac401cc7
JK
427/*
428 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
429 * entry to get unlocked before deleting it.
430 */
431int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
432{
c6dcf52c 433 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
ac401cc7 434
ac401cc7
JK
435 /*
436 * This gets called from truncate / punch_hole path. As such, the caller
437 * must hold locks protecting against concurrent modifications of the
438 * radix tree (usually fs-private i_mmap_sem for writing). Since the
439 * caller has seen exceptional entry for this index, we better find it
440 * at that index as well...
441 */
c6dcf52c
JK
442 WARN_ON_ONCE(!ret);
443 return ret;
444}
445
c6dcf52c
JK
446/*
447 * Invalidate exceptional DAX entry if it is clean.
448 */
449int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
450 pgoff_t index)
451{
452 return __dax_invalidate_mapping_entry(mapping, index, false);
ac401cc7
JK
453}
454
cccbce67
DW
455static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
456 sector_t sector, size_t size, struct page *to,
457 unsigned long vaddr)
f7ca90b1 458{
cccbce67
DW
459 void *vto, *kaddr;
460 pgoff_t pgoff;
461 pfn_t pfn;
462 long rc;
463 int id;
464
465 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
466 if (rc)
467 return rc;
468
469 id = dax_read_lock();
470 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
471 if (rc < 0) {
472 dax_read_unlock(id);
473 return rc;
474 }
f7ca90b1 475 vto = kmap_atomic(to);
cccbce67 476 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
f7ca90b1 477 kunmap_atomic(vto);
cccbce67 478 dax_read_unlock(id);
f7ca90b1
MW
479 return 0;
480}
481
642261ac
RZ
482/*
483 * By this point grab_mapping_entry() has ensured that we have a locked entry
484 * of the appropriate size so we don't have to worry about downgrading PMDs to
485 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
486 * already in the tree, we will skip the insertion and just dirty the PMD as
487 * appropriate.
488 */
ac401cc7
JK
489static void *dax_insert_mapping_entry(struct address_space *mapping,
490 struct vm_fault *vmf,
642261ac
RZ
491 void *entry, sector_t sector,
492 unsigned long flags)
9973c98e
RZ
493{
494 struct radix_tree_root *page_tree = &mapping->page_tree;
ac401cc7
JK
495 void *new_entry;
496 pgoff_t index = vmf->pgoff;
9973c98e 497
ac401cc7 498 if (vmf->flags & FAULT_FLAG_WRITE)
d2b2a28e 499 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
9973c98e 500
91d25ba8
RZ
501 if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
502 /* we are replacing a zero page with block mapping */
503 if (dax_is_pmd_entry(entry))
504 unmap_mapping_range(mapping,
505 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK,
506 PMD_SIZE, 0);
507 else /* pte entry */
508 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
509 PAGE_SIZE, 0);
9973c98e
RZ
510 }
511
ac401cc7 512 spin_lock_irq(&mapping->tree_lock);
642261ac
RZ
513 new_entry = dax_radix_locked_entry(sector, flags);
514
91d25ba8 515 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
642261ac
RZ
516 /*
517 * Only swap our new entry into the radix tree if the current
518 * entry is a zero page or an empty entry. If a normal PTE or
519 * PMD entry is already in the tree, we leave it alone. This
520 * means that if we are trying to insert a PTE and the
521 * existing entry is a PMD, we will just leave the PMD in the
522 * tree and dirty it if necessary.
523 */
f7942430 524 struct radix_tree_node *node;
ac401cc7
JK
525 void **slot;
526 void *ret;
9973c98e 527
f7942430 528 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
ac401cc7 529 WARN_ON_ONCE(ret != entry);
4d693d08
JW
530 __radix_tree_replace(page_tree, node, slot,
531 new_entry, NULL, NULL);
91d25ba8 532 entry = new_entry;
9973c98e 533 }
91d25ba8 534
ac401cc7 535 if (vmf->flags & FAULT_FLAG_WRITE)
9973c98e 536 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
91d25ba8 537
9973c98e 538 spin_unlock_irq(&mapping->tree_lock);
91d25ba8 539 return entry;
9973c98e
RZ
540}
541
4b4bb46d
JK
542static inline unsigned long
543pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
544{
545 unsigned long address;
546
547 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
548 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
549 return address;
550}
551
552/* Walk all mappings of a given index of a file and writeprotect them */
553static void dax_mapping_entry_mkclean(struct address_space *mapping,
554 pgoff_t index, unsigned long pfn)
555{
556 struct vm_area_struct *vma;
f729c8c9
RZ
557 pte_t pte, *ptep = NULL;
558 pmd_t *pmdp = NULL;
4b4bb46d 559 spinlock_t *ptl;
4b4bb46d
JK
560
561 i_mmap_lock_read(mapping);
562 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
a4d1a885 563 unsigned long address, start, end;
4b4bb46d
JK
564
565 cond_resched();
566
567 if (!(vma->vm_flags & VM_SHARED))
568 continue;
569
570 address = pgoff_address(index, vma);
a4d1a885
JG
571
572 /*
573 * Note because we provide start/end to follow_pte_pmd it will
574 * call mmu_notifier_invalidate_range_start() on our behalf
575 * before taking any lock.
576 */
577 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
4b4bb46d 578 continue;
4b4bb46d 579
f729c8c9
RZ
580 if (pmdp) {
581#ifdef CONFIG_FS_DAX_PMD
582 pmd_t pmd;
583
584 if (pfn != pmd_pfn(*pmdp))
585 goto unlock_pmd;
586 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
587 goto unlock_pmd;
588
589 flush_cache_page(vma, address, pfn);
590 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
591 pmd = pmd_wrprotect(pmd);
592 pmd = pmd_mkclean(pmd);
593 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
a4d1a885 594 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
f729c8c9
RZ
595unlock_pmd:
596 spin_unlock(ptl);
597#endif
598 } else {
599 if (pfn != pte_pfn(*ptep))
600 goto unlock_pte;
601 if (!pte_dirty(*ptep) && !pte_write(*ptep))
602 goto unlock_pte;
603
604 flush_cache_page(vma, address, pfn);
605 pte = ptep_clear_flush(vma, address, ptep);
606 pte = pte_wrprotect(pte);
607 pte = pte_mkclean(pte);
608 set_pte_at(vma->vm_mm, address, ptep, pte);
a4d1a885 609 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
f729c8c9
RZ
610unlock_pte:
611 pte_unmap_unlock(ptep, ptl);
612 }
4b4bb46d 613
a4d1a885 614 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
4b4bb46d
JK
615 }
616 i_mmap_unlock_read(mapping);
617}
618
9973c98e 619static int dax_writeback_one(struct block_device *bdev,
cccbce67
DW
620 struct dax_device *dax_dev, struct address_space *mapping,
621 pgoff_t index, void *entry)
9973c98e
RZ
622{
623 struct radix_tree_root *page_tree = &mapping->page_tree;
cccbce67
DW
624 void *entry2, **slot, *kaddr;
625 long ret = 0, id;
626 sector_t sector;
627 pgoff_t pgoff;
628 size_t size;
629 pfn_t pfn;
9973c98e 630
9973c98e 631 /*
a6abc2c0
JK
632 * A page got tagged dirty in DAX mapping? Something is seriously
633 * wrong.
9973c98e 634 */
a6abc2c0
JK
635 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
636 return -EIO;
9973c98e 637
a6abc2c0
JK
638 spin_lock_irq(&mapping->tree_lock);
639 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
640 /* Entry got punched out / reallocated? */
91d25ba8 641 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
a6abc2c0
JK
642 goto put_unlocked;
643 /*
644 * Entry got reallocated elsewhere? No need to writeback. We have to
645 * compare sectors as we must not bail out due to difference in lockbit
646 * or entry type.
647 */
648 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
649 goto put_unlocked;
642261ac
RZ
650 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
651 dax_is_zero_entry(entry))) {
9973c98e 652 ret = -EIO;
a6abc2c0 653 goto put_unlocked;
9973c98e
RZ
654 }
655
a6abc2c0
JK
656 /* Another fsync thread may have already written back this entry */
657 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
658 goto put_unlocked;
659 /* Lock the entry to serialize with page faults */
660 entry = lock_slot(mapping, slot);
661 /*
662 * We can clear the tag now but we have to be careful so that concurrent
663 * dax_writeback_one() calls for the same index cannot finish before we
664 * actually flush the caches. This is achieved as the calls will look
665 * at the entry only under tree_lock and once they do that they will
666 * see the entry locked and wait for it to unlock.
667 */
668 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
669 spin_unlock_irq(&mapping->tree_lock);
670
642261ac
RZ
671 /*
672 * Even if dax_writeback_mapping_range() was given a wbc->range_start
673 * in the middle of a PMD, the 'index' we are given will be aligned to
674 * the start index of the PMD, as will the sector we pull from
675 * 'entry'. This allows us to flush for PMD_SIZE and not have to
676 * worry about partial PMD writebacks.
677 */
cccbce67
DW
678 sector = dax_radix_sector(entry);
679 size = PAGE_SIZE << dax_radix_order(entry);
680
681 id = dax_read_lock();
682 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
683 if (ret)
684 goto dax_unlock;
9973c98e
RZ
685
686 /*
cccbce67
DW
687 * dax_direct_access() may sleep, so cannot hold tree_lock over
688 * its invocation.
9973c98e 689 */
cccbce67
DW
690 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
691 if (ret < 0)
692 goto dax_unlock;
9973c98e 693
cccbce67 694 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
9973c98e 695 ret = -EIO;
cccbce67 696 goto dax_unlock;
9973c98e
RZ
697 }
698
cccbce67 699 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
6318770a 700 dax_flush(dax_dev, pgoff, kaddr, size);
4b4bb46d
JK
701 /*
702 * After we have flushed the cache, we can clear the dirty tag. There
703 * cannot be new dirty data in the pfn after the flush has completed as
704 * the pfn mappings are writeprotected and fault waits for mapping
705 * entry lock.
706 */
707 spin_lock_irq(&mapping->tree_lock);
708 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
709 spin_unlock_irq(&mapping->tree_lock);
f9bc3a07 710 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
cccbce67
DW
711 dax_unlock:
712 dax_read_unlock(id);
91d25ba8 713 put_locked_mapping_entry(mapping, index);
9973c98e
RZ
714 return ret;
715
a6abc2c0
JK
716 put_unlocked:
717 put_unlocked_mapping_entry(mapping, index, entry2);
9973c98e
RZ
718 spin_unlock_irq(&mapping->tree_lock);
719 return ret;
720}
721
722/*
723 * Flush the mapping to the persistent domain within the byte range of [start,
724 * end]. This is required by data integrity operations to ensure file data is
725 * on persistent storage prior to completion of the operation.
726 */
7f6d5b52
RZ
727int dax_writeback_mapping_range(struct address_space *mapping,
728 struct block_device *bdev, struct writeback_control *wbc)
9973c98e
RZ
729{
730 struct inode *inode = mapping->host;
642261ac 731 pgoff_t start_index, end_index;
9973c98e 732 pgoff_t indices[PAGEVEC_SIZE];
cccbce67 733 struct dax_device *dax_dev;
9973c98e
RZ
734 struct pagevec pvec;
735 bool done = false;
736 int i, ret = 0;
9973c98e
RZ
737
738 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
739 return -EIO;
740
7f6d5b52
RZ
741 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
742 return 0;
743
cccbce67
DW
744 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
745 if (!dax_dev)
746 return -EIO;
747
09cbfeaf
KS
748 start_index = wbc->range_start >> PAGE_SHIFT;
749 end_index = wbc->range_end >> PAGE_SHIFT;
9973c98e 750
d14a3f48
RZ
751 trace_dax_writeback_range(inode, start_index, end_index);
752
9973c98e
RZ
753 tag_pages_for_writeback(mapping, start_index, end_index);
754
755 pagevec_init(&pvec, 0);
756 while (!done) {
757 pvec.nr = find_get_entries_tag(mapping, start_index,
758 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
759 pvec.pages, indices);
760
761 if (pvec.nr == 0)
762 break;
763
764 for (i = 0; i < pvec.nr; i++) {
765 if (indices[i] > end_index) {
766 done = true;
767 break;
768 }
769
cccbce67
DW
770 ret = dax_writeback_one(bdev, dax_dev, mapping,
771 indices[i], pvec.pages[i]);
819ec6b9
JL
772 if (ret < 0) {
773 mapping_set_error(mapping, ret);
d14a3f48 774 goto out;
819ec6b9 775 }
9973c98e 776 }
1eb643d0 777 start_index = indices[pvec.nr - 1] + 1;
9973c98e 778 }
d14a3f48 779out:
cccbce67 780 put_dax(dax_dev);
d14a3f48
RZ
781 trace_dax_writeback_range_done(inode, start_index, end_index);
782 return (ret < 0 ? ret : 0);
9973c98e
RZ
783}
784EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
785
ac401cc7 786static int dax_insert_mapping(struct address_space *mapping,
cccbce67 787 struct block_device *bdev, struct dax_device *dax_dev,
91d25ba8 788 sector_t sector, size_t size, void *entry,
cccbce67 789 struct vm_area_struct *vma, struct vm_fault *vmf)
f7ca90b1 790{
1a29d85e 791 unsigned long vaddr = vmf->address;
cccbce67
DW
792 void *ret, *kaddr;
793 pgoff_t pgoff;
794 int id, rc;
795 pfn_t pfn;
f7ca90b1 796
cccbce67
DW
797 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
798 if (rc)
799 return rc;
f7ca90b1 800
cccbce67
DW
801 id = dax_read_lock();
802 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
803 if (rc < 0) {
804 dax_read_unlock(id);
805 return rc;
806 }
807 dax_read_unlock(id);
808
809 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector, 0);
4d9a2c87
JK
810 if (IS_ERR(ret))
811 return PTR_ERR(ret);
9973c98e 812
b4440734 813 trace_dax_insert_mapping(mapping->host, vmf, ret);
91d25ba8
RZ
814 if (vmf->flags & FAULT_FLAG_WRITE)
815 return vm_insert_mixed_mkwrite(vma, vaddr, pfn);
816 else
817 return vm_insert_mixed(vma, vaddr, pfn);
0e3b210c 818}
0e3b210c 819
e30331ff 820/*
91d25ba8
RZ
821 * The user has performed a load from a hole in the file. Allocating a new
822 * page in the file would cause excessive storage usage for workloads with
823 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
824 * If this page is ever written to we will re-fault and change the mapping to
825 * point to real DAX storage instead.
e30331ff 826 */
91d25ba8 827static int dax_load_hole(struct address_space *mapping, void *entry,
e30331ff
RZ
828 struct vm_fault *vmf)
829{
830 struct inode *inode = mapping->host;
91d25ba8
RZ
831 unsigned long vaddr = vmf->address;
832 int ret = VM_FAULT_NOPAGE;
833 struct page *zero_page;
834 void *entry2;
e30331ff 835
91d25ba8
RZ
836 zero_page = ZERO_PAGE(0);
837 if (unlikely(!zero_page)) {
e30331ff
RZ
838 ret = VM_FAULT_OOM;
839 goto out;
840 }
841
91d25ba8
RZ
842 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
843 RADIX_DAX_ZERO_PAGE);
844 if (IS_ERR(entry2)) {
845 ret = VM_FAULT_SIGBUS;
846 goto out;
e30331ff 847 }
91d25ba8
RZ
848
849 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
e30331ff
RZ
850out:
851 trace_dax_load_hole(inode, vmf, ret);
852 return ret;
853}
854
4b0228fa
VV
855static bool dax_range_is_aligned(struct block_device *bdev,
856 unsigned int offset, unsigned int length)
857{
858 unsigned short sector_size = bdev_logical_block_size(bdev);
859
860 if (!IS_ALIGNED(offset, sector_size))
861 return false;
862 if (!IS_ALIGNED(length, sector_size))
863 return false;
864
865 return true;
866}
867
cccbce67
DW
868int __dax_zero_page_range(struct block_device *bdev,
869 struct dax_device *dax_dev, sector_t sector,
870 unsigned int offset, unsigned int size)
679c8bd3 871{
cccbce67
DW
872 if (dax_range_is_aligned(bdev, offset, size)) {
873 sector_t start_sector = sector + (offset >> 9);
4b0228fa
VV
874
875 return blkdev_issue_zeroout(bdev, start_sector,
53ef7d0e 876 size >> 9, GFP_NOFS, 0);
4b0228fa 877 } else {
cccbce67
DW
878 pgoff_t pgoff;
879 long rc, id;
880 void *kaddr;
881 pfn_t pfn;
882
e84b83b9 883 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
cccbce67
DW
884 if (rc)
885 return rc;
886
887 id = dax_read_lock();
e84b83b9 888 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
cccbce67
DW
889 &pfn);
890 if (rc < 0) {
891 dax_read_unlock(id);
892 return rc;
893 }
81f55870
DW
894 memset(kaddr + offset, 0, size);
895 dax_flush(dax_dev, pgoff, kaddr + offset, size);
cccbce67 896 dax_read_unlock(id);
4b0228fa 897 }
679c8bd3
CH
898 return 0;
899}
900EXPORT_SYMBOL_GPL(__dax_zero_page_range);
901
333ccc97 902static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
25726bc1 903{
333ccc97 904 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
25726bc1 905}
a254e568 906
a254e568 907static loff_t
11c59c92 908dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
a254e568
CH
909 struct iomap *iomap)
910{
cccbce67
DW
911 struct block_device *bdev = iomap->bdev;
912 struct dax_device *dax_dev = iomap->dax_dev;
a254e568
CH
913 struct iov_iter *iter = data;
914 loff_t end = pos + length, done = 0;
915 ssize_t ret = 0;
cccbce67 916 int id;
a254e568
CH
917
918 if (iov_iter_rw(iter) == READ) {
919 end = min(end, i_size_read(inode));
920 if (pos >= end)
921 return 0;
922
923 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
924 return iov_iter_zero(min(length, end - pos), iter);
925 }
926
927 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
928 return -EIO;
929
e3fce68c
JK
930 /*
931 * Write can allocate block for an area which has a hole page mapped
932 * into page tables. We have to tear down these mappings so that data
933 * written by write(2) is visible in mmap.
934 */
cd656375 935 if (iomap->flags & IOMAP_F_NEW) {
e3fce68c
JK
936 invalidate_inode_pages2_range(inode->i_mapping,
937 pos >> PAGE_SHIFT,
938 (end - 1) >> PAGE_SHIFT);
939 }
940
cccbce67 941 id = dax_read_lock();
a254e568
CH
942 while (pos < end) {
943 unsigned offset = pos & (PAGE_SIZE - 1);
cccbce67
DW
944 const size_t size = ALIGN(length + offset, PAGE_SIZE);
945 const sector_t sector = dax_iomap_sector(iomap, pos);
a254e568 946 ssize_t map_len;
cccbce67
DW
947 pgoff_t pgoff;
948 void *kaddr;
949 pfn_t pfn;
a254e568 950
d1908f52
MH
951 if (fatal_signal_pending(current)) {
952 ret = -EINTR;
953 break;
954 }
955
cccbce67
DW
956 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
957 if (ret)
958 break;
959
960 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
961 &kaddr, &pfn);
a254e568
CH
962 if (map_len < 0) {
963 ret = map_len;
964 break;
965 }
966
cccbce67
DW
967 map_len = PFN_PHYS(map_len);
968 kaddr += offset;
a254e568
CH
969 map_len -= offset;
970 if (map_len > end - pos)
971 map_len = end - pos;
972
973 if (iov_iter_rw(iter) == WRITE)
fec53774
DW
974 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
975 map_len, iter);
a254e568 976 else
cccbce67 977 map_len = copy_to_iter(kaddr, map_len, iter);
a254e568
CH
978 if (map_len <= 0) {
979 ret = map_len ? map_len : -EFAULT;
980 break;
981 }
982
983 pos += map_len;
984 length -= map_len;
985 done += map_len;
986 }
cccbce67 987 dax_read_unlock(id);
a254e568
CH
988
989 return done ? done : ret;
990}
991
992/**
11c59c92 993 * dax_iomap_rw - Perform I/O to a DAX file
a254e568
CH
994 * @iocb: The control block for this I/O
995 * @iter: The addresses to do I/O from or to
996 * @ops: iomap ops passed from the file system
997 *
998 * This function performs read and write operations to directly mapped
999 * persistent memory. The callers needs to take care of read/write exclusion
1000 * and evicting any page cache pages in the region under I/O.
1001 */
1002ssize_t
11c59c92 1003dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
8ff6daa1 1004 const struct iomap_ops *ops)
a254e568
CH
1005{
1006 struct address_space *mapping = iocb->ki_filp->f_mapping;
1007 struct inode *inode = mapping->host;
1008 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1009 unsigned flags = 0;
1010
168316db
CH
1011 if (iov_iter_rw(iter) == WRITE) {
1012 lockdep_assert_held_exclusive(&inode->i_rwsem);
a254e568 1013 flags |= IOMAP_WRITE;
168316db
CH
1014 } else {
1015 lockdep_assert_held(&inode->i_rwsem);
1016 }
a254e568 1017
a254e568
CH
1018 while (iov_iter_count(iter)) {
1019 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
11c59c92 1020 iter, dax_iomap_actor);
a254e568
CH
1021 if (ret <= 0)
1022 break;
1023 pos += ret;
1024 done += ret;
1025 }
1026
1027 iocb->ki_pos += done;
1028 return done ? done : ret;
1029}
11c59c92 1030EXPORT_SYMBOL_GPL(dax_iomap_rw);
a7d73fe6 1031
9f141d6e
JK
1032static int dax_fault_return(int error)
1033{
1034 if (error == 0)
1035 return VM_FAULT_NOPAGE;
1036 if (error == -ENOMEM)
1037 return VM_FAULT_OOM;
1038 return VM_FAULT_SIGBUS;
1039}
1040
a2d58167
DJ
1041static int dax_iomap_pte_fault(struct vm_fault *vmf,
1042 const struct iomap_ops *ops)
a7d73fe6 1043{
11bac800 1044 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
a7d73fe6 1045 struct inode *inode = mapping->host;
1a29d85e 1046 unsigned long vaddr = vmf->address;
a7d73fe6
CH
1047 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1048 sector_t sector;
1049 struct iomap iomap = { 0 };
9484ab1b 1050 unsigned flags = IOMAP_FAULT;
a7d73fe6 1051 int error, major = 0;
b1aa812b 1052 int vmf_ret = 0;
a7d73fe6
CH
1053 void *entry;
1054
a9c42b33 1055 trace_dax_pte_fault(inode, vmf, vmf_ret);
a7d73fe6
CH
1056 /*
1057 * Check whether offset isn't beyond end of file now. Caller is supposed
1058 * to hold locks serializing us with truncate / punch hole so this is
1059 * a reliable test.
1060 */
a9c42b33
RZ
1061 if (pos >= i_size_read(inode)) {
1062 vmf_ret = VM_FAULT_SIGBUS;
1063 goto out;
1064 }
a7d73fe6 1065
a7d73fe6
CH
1066 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1067 flags |= IOMAP_WRITE;
1068
13e451fd
JK
1069 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1070 if (IS_ERR(entry)) {
1071 vmf_ret = dax_fault_return(PTR_ERR(entry));
1072 goto out;
1073 }
1074
e2093926
RZ
1075 /*
1076 * It is possible, particularly with mixed reads & writes to private
1077 * mappings, that we have raced with a PMD fault that overlaps with
1078 * the PTE we need to set up. If so just return and the fault will be
1079 * retried.
1080 */
1081 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1082 vmf_ret = VM_FAULT_NOPAGE;
1083 goto unlock_entry;
1084 }
1085
a7d73fe6
CH
1086 /*
1087 * Note that we don't bother to use iomap_apply here: DAX required
1088 * the file system block size to be equal the page size, which means
1089 * that we never have to deal with more than a single extent here.
1090 */
1091 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
a9c42b33
RZ
1092 if (error) {
1093 vmf_ret = dax_fault_return(error);
13e451fd 1094 goto unlock_entry;
a9c42b33 1095 }
a7d73fe6 1096 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
13e451fd
JK
1097 error = -EIO; /* fs corruption? */
1098 goto error_finish_iomap;
a7d73fe6
CH
1099 }
1100
333ccc97 1101 sector = dax_iomap_sector(&iomap, pos);
a7d73fe6
CH
1102
1103 if (vmf->cow_page) {
1104 switch (iomap.type) {
1105 case IOMAP_HOLE:
1106 case IOMAP_UNWRITTEN:
1107 clear_user_highpage(vmf->cow_page, vaddr);
1108 break;
1109 case IOMAP_MAPPED:
cccbce67
DW
1110 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1111 sector, PAGE_SIZE, vmf->cow_page, vaddr);
a7d73fe6
CH
1112 break;
1113 default:
1114 WARN_ON_ONCE(1);
1115 error = -EIO;
1116 break;
1117 }
1118
1119 if (error)
13e451fd 1120 goto error_finish_iomap;
b1aa812b
JK
1121
1122 __SetPageUptodate(vmf->cow_page);
1123 vmf_ret = finish_fault(vmf);
1124 if (!vmf_ret)
1125 vmf_ret = VM_FAULT_DONE_COW;
13e451fd 1126 goto finish_iomap;
a7d73fe6
CH
1127 }
1128
1129 switch (iomap.type) {
1130 case IOMAP_MAPPED:
1131 if (iomap.flags & IOMAP_F_NEW) {
1132 count_vm_event(PGMAJFAULT);
2262185c 1133 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
a7d73fe6
CH
1134 major = VM_FAULT_MAJOR;
1135 }
cccbce67 1136 error = dax_insert_mapping(mapping, iomap.bdev, iomap.dax_dev,
91d25ba8 1137 sector, PAGE_SIZE, entry, vmf->vma, vmf);
9f141d6e
JK
1138 /* -EBUSY is fine, somebody else faulted on the same PTE */
1139 if (error == -EBUSY)
1140 error = 0;
a7d73fe6
CH
1141 break;
1142 case IOMAP_UNWRITTEN:
1143 case IOMAP_HOLE:
1550290b 1144 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
91d25ba8 1145 vmf_ret = dax_load_hole(mapping, entry, vmf);
13e451fd 1146 goto finish_iomap;
1550290b 1147 }
a7d73fe6
CH
1148 /*FALLTHRU*/
1149 default:
1150 WARN_ON_ONCE(1);
1151 error = -EIO;
1152 break;
1153 }
1154
13e451fd 1155 error_finish_iomap:
9f141d6e 1156 vmf_ret = dax_fault_return(error) | major;
9f141d6e
JK
1157 finish_iomap:
1158 if (ops->iomap_end) {
1159 int copied = PAGE_SIZE;
1160
1161 if (vmf_ret & VM_FAULT_ERROR)
1162 copied = 0;
1163 /*
1164 * The fault is done by now and there's no way back (other
1165 * thread may be already happily using PTE we have installed).
1166 * Just ignore error from ->iomap_end since we cannot do much
1167 * with it.
1168 */
1169 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
1550290b 1170 }
13e451fd 1171 unlock_entry:
91d25ba8 1172 put_locked_mapping_entry(mapping, vmf->pgoff);
13e451fd 1173 out:
a9c42b33 1174 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
9f141d6e 1175 return vmf_ret;
a7d73fe6 1176}
642261ac
RZ
1177
1178#ifdef CONFIG_FS_DAX_PMD
1179/*
1180 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1181 * more often than one might expect in the below functions.
1182 */
1183#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1184
f4200391 1185static int dax_pmd_insert_mapping(struct vm_fault *vmf, struct iomap *iomap,
91d25ba8 1186 loff_t pos, void *entry)
642261ac 1187{
f4200391 1188 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
cccbce67
DW
1189 const sector_t sector = dax_iomap_sector(iomap, pos);
1190 struct dax_device *dax_dev = iomap->dax_dev;
642261ac 1191 struct block_device *bdev = iomap->bdev;
27a7ffac 1192 struct inode *inode = mapping->host;
cccbce67
DW
1193 const size_t size = PMD_SIZE;
1194 void *ret = NULL, *kaddr;
1195 long length = 0;
1196 pgoff_t pgoff;
1197 pfn_t pfn;
1198 int id;
1199
1200 if (bdev_dax_pgoff(bdev, sector, size, &pgoff) != 0)
27a7ffac 1201 goto fallback;
642261ac 1202
cccbce67
DW
1203 id = dax_read_lock();
1204 length = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
1205 if (length < 0)
1206 goto unlock_fallback;
1207 length = PFN_PHYS(length);
1208
1209 if (length < size)
1210 goto unlock_fallback;
1211 if (pfn_t_to_pfn(pfn) & PG_PMD_COLOUR)
1212 goto unlock_fallback;
1213 if (!pfn_t_devmap(pfn))
1214 goto unlock_fallback;
1215 dax_read_unlock(id);
1216
91d25ba8 1217 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector,
642261ac
RZ
1218 RADIX_DAX_PMD);
1219 if (IS_ERR(ret))
27a7ffac 1220 goto fallback;
642261ac 1221
cccbce67 1222 trace_dax_pmd_insert_mapping(inode, vmf, length, pfn, ret);
f4200391 1223 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
cccbce67 1224 pfn, vmf->flags & FAULT_FLAG_WRITE);
642261ac 1225
cccbce67
DW
1226unlock_fallback:
1227 dax_read_unlock(id);
27a7ffac 1228fallback:
cccbce67 1229 trace_dax_pmd_insert_mapping_fallback(inode, vmf, length, pfn, ret);
642261ac
RZ
1230 return VM_FAULT_FALLBACK;
1231}
1232
f4200391 1233static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
91d25ba8 1234 void *entry)
642261ac 1235{
f4200391
DJ
1236 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1237 unsigned long pmd_addr = vmf->address & PMD_MASK;
653b2ea3 1238 struct inode *inode = mapping->host;
642261ac 1239 struct page *zero_page;
653b2ea3 1240 void *ret = NULL;
642261ac
RZ
1241 spinlock_t *ptl;
1242 pmd_t pmd_entry;
642261ac 1243
f4200391 1244 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
642261ac
RZ
1245
1246 if (unlikely(!zero_page))
653b2ea3 1247 goto fallback;
642261ac 1248
91d25ba8
RZ
1249 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
1250 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE);
642261ac 1251 if (IS_ERR(ret))
653b2ea3 1252 goto fallback;
642261ac 1253
f4200391
DJ
1254 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1255 if (!pmd_none(*(vmf->pmd))) {
642261ac 1256 spin_unlock(ptl);
653b2ea3 1257 goto fallback;
642261ac
RZ
1258 }
1259
f4200391 1260 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
642261ac 1261 pmd_entry = pmd_mkhuge(pmd_entry);
f4200391 1262 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
642261ac 1263 spin_unlock(ptl);
f4200391 1264 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
642261ac 1265 return VM_FAULT_NOPAGE;
653b2ea3
RZ
1266
1267fallback:
f4200391 1268 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
653b2ea3 1269 return VM_FAULT_FALLBACK;
642261ac
RZ
1270}
1271
a2d58167
DJ
1272static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1273 const struct iomap_ops *ops)
642261ac 1274{
f4200391 1275 struct vm_area_struct *vma = vmf->vma;
642261ac 1276 struct address_space *mapping = vma->vm_file->f_mapping;
d8a849e1
DJ
1277 unsigned long pmd_addr = vmf->address & PMD_MASK;
1278 bool write = vmf->flags & FAULT_FLAG_WRITE;
9484ab1b 1279 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
642261ac
RZ
1280 struct inode *inode = mapping->host;
1281 int result = VM_FAULT_FALLBACK;
1282 struct iomap iomap = { 0 };
1283 pgoff_t max_pgoff, pgoff;
642261ac
RZ
1284 void *entry;
1285 loff_t pos;
1286 int error;
1287
282a8e03
RZ
1288 /*
1289 * Check whether offset isn't beyond end of file now. Caller is
1290 * supposed to hold locks serializing us with truncate / punch hole so
1291 * this is a reliable test.
1292 */
1293 pgoff = linear_page_index(vma, pmd_addr);
1294 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1295
f4200391 1296 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
282a8e03 1297
fffa281b
RZ
1298 /*
1299 * Make sure that the faulting address's PMD offset (color) matches
1300 * the PMD offset from the start of the file. This is necessary so
1301 * that a PMD range in the page table overlaps exactly with a PMD
1302 * range in the radix tree.
1303 */
1304 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1305 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1306 goto fallback;
1307
642261ac
RZ
1308 /* Fall back to PTEs if we're going to COW */
1309 if (write && !(vma->vm_flags & VM_SHARED))
1310 goto fallback;
1311
1312 /* If the PMD would extend outside the VMA */
1313 if (pmd_addr < vma->vm_start)
1314 goto fallback;
1315 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1316 goto fallback;
1317
282a8e03
RZ
1318 if (pgoff > max_pgoff) {
1319 result = VM_FAULT_SIGBUS;
1320 goto out;
1321 }
642261ac
RZ
1322
1323 /* If the PMD would extend beyond the file size */
1324 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1325 goto fallback;
1326
876f2946 1327 /*
91d25ba8
RZ
1328 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1329 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1330 * is already in the tree, for instance), it will return -EEXIST and
1331 * we just fall back to 4k entries.
876f2946
RZ
1332 */
1333 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1334 if (IS_ERR(entry))
1335 goto fallback;
1336
e2093926
RZ
1337 /*
1338 * It is possible, particularly with mixed reads & writes to private
1339 * mappings, that we have raced with a PTE fault that overlaps with
1340 * the PMD we need to set up. If so just return and the fault will be
1341 * retried.
1342 */
1343 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1344 !pmd_devmap(*vmf->pmd)) {
1345 result = 0;
1346 goto unlock_entry;
1347 }
1348
642261ac
RZ
1349 /*
1350 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1351 * setting up a mapping, so really we're using iomap_begin() as a way
1352 * to look up our filesystem block.
1353 */
1354 pos = (loff_t)pgoff << PAGE_SHIFT;
1355 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1356 if (error)
876f2946 1357 goto unlock_entry;
9f141d6e 1358
642261ac
RZ
1359 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1360 goto finish_iomap;
1361
642261ac
RZ
1362 switch (iomap.type) {
1363 case IOMAP_MAPPED:
91d25ba8 1364 result = dax_pmd_insert_mapping(vmf, &iomap, pos, entry);
642261ac
RZ
1365 break;
1366 case IOMAP_UNWRITTEN:
1367 case IOMAP_HOLE:
1368 if (WARN_ON_ONCE(write))
876f2946 1369 break;
91d25ba8 1370 result = dax_pmd_load_hole(vmf, &iomap, entry);
642261ac
RZ
1371 break;
1372 default:
1373 WARN_ON_ONCE(1);
1374 break;
1375 }
1376
1377 finish_iomap:
1378 if (ops->iomap_end) {
9f141d6e
JK
1379 int copied = PMD_SIZE;
1380
1381 if (result == VM_FAULT_FALLBACK)
1382 copied = 0;
1383 /*
1384 * The fault is done by now and there's no way back (other
1385 * thread may be already happily using PMD we have installed).
1386 * Just ignore error from ->iomap_end since we cannot do much
1387 * with it.
1388 */
1389 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1390 &iomap);
642261ac 1391 }
876f2946 1392 unlock_entry:
91d25ba8 1393 put_locked_mapping_entry(mapping, pgoff);
642261ac
RZ
1394 fallback:
1395 if (result == VM_FAULT_FALLBACK) {
d8a849e1 1396 split_huge_pmd(vma, vmf->pmd, vmf->address);
642261ac
RZ
1397 count_vm_event(THP_FAULT_FALLBACK);
1398 }
282a8e03 1399out:
f4200391 1400 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
642261ac
RZ
1401 return result;
1402}
a2d58167 1403#else
01cddfe9
AB
1404static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1405 const struct iomap_ops *ops)
a2d58167
DJ
1406{
1407 return VM_FAULT_FALLBACK;
1408}
642261ac 1409#endif /* CONFIG_FS_DAX_PMD */
a2d58167
DJ
1410
1411/**
1412 * dax_iomap_fault - handle a page fault on a DAX file
1413 * @vmf: The description of the fault
1414 * @ops: iomap ops passed from the file system
1415 *
1416 * When a page fault occurs, filesystems may call this helper in
1417 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1418 * has done all the necessary locking for page fault to proceed
1419 * successfully.
1420 */
c791ace1
DJ
1421int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1422 const struct iomap_ops *ops)
a2d58167 1423{
c791ace1
DJ
1424 switch (pe_size) {
1425 case PE_SIZE_PTE:
a2d58167 1426 return dax_iomap_pte_fault(vmf, ops);
c791ace1 1427 case PE_SIZE_PMD:
a2d58167
DJ
1428 return dax_iomap_pmd_fault(vmf, ops);
1429 default:
1430 return VM_FAULT_FALLBACK;
1431 }
1432}
1433EXPORT_SYMBOL_GPL(dax_iomap_fault);