ipmi: Remove ACPI SPMI probing from the SSIF (I2C) driver
[linux-2.6-block.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
917f3452
RZ
45/* The 'colour' (ie low bits) within a PMD of a page offset. */
46#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
977fbdcd 47#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
917f3452 48
ce95ab0f 49static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
ac401cc7
JK
50
51static int __init init_dax_wait_table(void)
52{
53 int i;
54
55 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
56 init_waitqueue_head(wait_table + i);
57 return 0;
58}
59fs_initcall(init_dax_wait_table);
60
527b19d0
RZ
61/*
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.
65 *
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
68 * block allocation.
69 */
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))
75
76static unsigned long dax_radix_sector(void *entry)
77{
78 return (unsigned long)entry >> RADIX_DAX_SHIFT;
79}
80
81static void *dax_radix_locked_entry(sector_t sector, unsigned long flags)
82{
83 return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags |
84 ((unsigned long)sector << RADIX_DAX_SHIFT) |
85 RADIX_DAX_ENTRY_LOCK);
86}
87
88static unsigned int dax_radix_order(void *entry)
89{
90 if ((unsigned long)entry & RADIX_DAX_PMD)
91 return PMD_SHIFT - PAGE_SHIFT;
92 return 0;
93}
94
642261ac 95static int dax_is_pmd_entry(void *entry)
d1a5f2b4 96{
642261ac 97 return (unsigned long)entry & RADIX_DAX_PMD;
d1a5f2b4
DW
98}
99
642261ac 100static int dax_is_pte_entry(void *entry)
d475c634 101{
642261ac 102 return !((unsigned long)entry & RADIX_DAX_PMD);
d475c634
MW
103}
104
642261ac 105static int dax_is_zero_entry(void *entry)
d475c634 106{
91d25ba8 107 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
d475c634
MW
108}
109
642261ac 110static int dax_is_empty_entry(void *entry)
b2e0d162 111{
642261ac 112 return (unsigned long)entry & RADIX_DAX_EMPTY;
b2e0d162
DW
113}
114
ac401cc7
JK
115/*
116 * DAX radix tree locking
117 */
118struct exceptional_entry_key {
119 struct address_space *mapping;
63e95b5c 120 pgoff_t entry_start;
ac401cc7
JK
121};
122
123struct wait_exceptional_entry_queue {
ac6424b9 124 wait_queue_entry_t wait;
ac401cc7
JK
125 struct exceptional_entry_key key;
126};
127
63e95b5c
RZ
128static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
129 pgoff_t index, void *entry, struct exceptional_entry_key *key)
130{
131 unsigned long hash;
132
133 /*
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.
137 */
642261ac 138 if (dax_is_pmd_entry(entry))
917f3452 139 index &= ~PG_PMD_COLOUR;
63e95b5c
RZ
140
141 key->mapping = mapping;
142 key->entry_start = index;
143
144 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
145 return wait_table + hash;
146}
147
ac6424b9 148static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
ac401cc7
JK
149 int sync, void *keyp)
150{
151 struct exceptional_entry_key *key = keyp;
152 struct wait_exceptional_entry_queue *ewait =
153 container_of(wait, struct wait_exceptional_entry_queue, wait);
154
155 if (key->mapping != ewait->key.mapping ||
63e95b5c 156 key->entry_start != ewait->key.entry_start)
ac401cc7
JK
157 return 0;
158 return autoremove_wake_function(wait, mode, sync, NULL);
159}
160
e30331ff
RZ
161/*
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
166 * wake them.
167 */
d01ad197 168static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
e30331ff
RZ
169 pgoff_t index, void *entry, bool wake_all)
170{
171 struct exceptional_entry_key key;
172 wait_queue_head_t *wq;
173
174 wq = dax_entry_waitqueue(mapping, index, entry, &key);
175
176 /*
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.
181 */
182 if (waitqueue_active(wq))
183 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
184}
185
ac401cc7
JK
186/*
187 * Check whether the given slot is locked. The function must be called with
188 * mapping->tree_lock held
189 */
190static inline int slot_locked(struct address_space *mapping, void **slot)
191{
192 unsigned long entry = (unsigned long)
193 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
194 return entry & RADIX_DAX_ENTRY_LOCK;
195}
196
197/*
198 * Mark the given slot is locked. The function must be called with
199 * mapping->tree_lock held
200 */
201static inline void *lock_slot(struct address_space *mapping, void **slot)
202{
203 unsigned long entry = (unsigned long)
204 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
205
206 entry |= RADIX_DAX_ENTRY_LOCK;
6d75f366 207 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
208 return (void *)entry;
209}
210
211/*
212 * Mark the given slot is unlocked. The function must be called with
213 * mapping->tree_lock held
214 */
215static inline void *unlock_slot(struct address_space *mapping, void **slot)
216{
217 unsigned long entry = (unsigned long)
218 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
219
220 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
6d75f366 221 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
222 return (void *)entry;
223}
224
225/*
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
230 * unlock it.
231 *
232 * The function must be called with mapping->tree_lock held.
233 */
234static void *get_unlocked_mapping_entry(struct address_space *mapping,
235 pgoff_t index, void ***slotp)
236{
e3ad61c6 237 void *entry, **slot;
ac401cc7 238 struct wait_exceptional_entry_queue ewait;
63e95b5c 239 wait_queue_head_t *wq;
ac401cc7
JK
240
241 init_wait(&ewait.wait);
242 ewait.wait.func = wake_exceptional_entry_func;
ac401cc7
JK
243
244 for (;;) {
e3ad61c6 245 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
ac401cc7 246 &slot);
91d25ba8
RZ
247 if (!entry ||
248 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
ac401cc7
JK
249 !slot_locked(mapping, slot)) {
250 if (slotp)
251 *slotp = slot;
e3ad61c6 252 return entry;
ac401cc7 253 }
63e95b5c
RZ
254
255 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
ac401cc7
JK
256 prepare_to_wait_exclusive(wq, &ewait.wait,
257 TASK_UNINTERRUPTIBLE);
258 spin_unlock_irq(&mapping->tree_lock);
259 schedule();
260 finish_wait(wq, &ewait.wait);
261 spin_lock_irq(&mapping->tree_lock);
262 }
263}
264
b1aa812b
JK
265static void dax_unlock_mapping_entry(struct address_space *mapping,
266 pgoff_t index)
267{
268 void *entry, **slot;
269
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);
275 return;
276 }
277 unlock_slot(mapping, slot);
278 spin_unlock_irq(&mapping->tree_lock);
279 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
280}
281
422476c4 282static void put_locked_mapping_entry(struct address_space *mapping,
91d25ba8 283 pgoff_t index)
422476c4 284{
91d25ba8 285 dax_unlock_mapping_entry(mapping, index);
422476c4
RZ
286}
287
288/*
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.
291 */
292static void put_unlocked_mapping_entry(struct address_space *mapping,
293 pgoff_t index, void *entry)
294{
91d25ba8 295 if (!entry)
422476c4
RZ
296 return;
297
298 /* We have to wake up next waiter for the radix tree entry lock */
299 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
300}
301
ac401cc7 302/*
91d25ba8
RZ
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.
ac401cc7 307 *
642261ac
RZ
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
91d25ba8
RZ
310 * happen if there are any 4k entries within the 2MiB range that we are
311 * requesting.
642261ac
RZ
312 *
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.
319 *
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.
323 *
ac401cc7
JK
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
326 * show it helps.
327 */
642261ac
RZ
328static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
329 unsigned long size_flag)
ac401cc7 330{
642261ac 331 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
e3ad61c6 332 void *entry, **slot;
ac401cc7
JK
333
334restart:
335 spin_lock_irq(&mapping->tree_lock);
e3ad61c6 336 entry = get_unlocked_mapping_entry(mapping, index, &slot);
642261ac 337
91d25ba8
RZ
338 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
339 entry = ERR_PTR(-EIO);
340 goto out_unlock;
341 }
342
642261ac
RZ
343 if (entry) {
344 if (size_flag & RADIX_DAX_PMD) {
91d25ba8 345 if (dax_is_pte_entry(entry)) {
642261ac
RZ
346 put_unlocked_mapping_entry(mapping, index,
347 entry);
348 entry = ERR_PTR(-EEXIST);
349 goto out_unlock;
350 }
351 } else { /* trying to grab a PTE entry */
91d25ba8 352 if (dax_is_pmd_entry(entry) &&
642261ac
RZ
353 (dax_is_zero_entry(entry) ||
354 dax_is_empty_entry(entry))) {
355 pmd_downgrade = true;
356 }
357 }
358 }
359
ac401cc7 360 /* No entry for given index? Make sure radix tree is big enough. */
642261ac 361 if (!entry || pmd_downgrade) {
ac401cc7
JK
362 int err;
363
642261ac
RZ
364 if (pmd_downgrade) {
365 /*
366 * Make sure 'entry' remains valid while we drop
367 * mapping->tree_lock.
368 */
369 entry = lock_slot(mapping, slot);
370 }
371
ac401cc7 372 spin_unlock_irq(&mapping->tree_lock);
642261ac
RZ
373 /*
374 * Besides huge zero pages the only other thing that gets
375 * downgraded are empty entries which don't need to be
376 * unmapped.
377 */
378 if (pmd_downgrade && dax_is_zero_entry(entry))
977fbdcd
MW
379 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
380 PG_PMD_NR, false);
642261ac 381
ac401cc7
JK
382 err = radix_tree_preload(
383 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
0cb80b48
JK
384 if (err) {
385 if (pmd_downgrade)
91d25ba8 386 put_locked_mapping_entry(mapping, index);
ac401cc7 387 return ERR_PTR(err);
0cb80b48 388 }
ac401cc7 389 spin_lock_irq(&mapping->tree_lock);
642261ac 390
e11f8b7b
RZ
391 if (!entry) {
392 /*
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.
397 */
398 entry = __radix_tree_lookup(&mapping->page_tree, index,
399 NULL, &slot);
400 if (entry) {
401 radix_tree_preload_end();
402 spin_unlock_irq(&mapping->tree_lock);
403 goto restart;
404 }
405 }
406
642261ac
RZ
407 if (pmd_downgrade) {
408 radix_tree_delete(&mapping->page_tree, index);
409 mapping->nrexceptional--;
410 dax_wake_mapping_entry_waiter(mapping, index, entry,
411 true);
412 }
413
414 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
415
416 err = __radix_tree_insert(&mapping->page_tree, index,
417 dax_radix_order(entry), entry);
ac401cc7
JK
418 radix_tree_preload_end();
419 if (err) {
420 spin_unlock_irq(&mapping->tree_lock);
642261ac 421 /*
e11f8b7b
RZ
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
427 * wake.
642261ac 428 */
ac401cc7
JK
429 return ERR_PTR(err);
430 }
431 /* Good, we have inserted empty locked entry into the tree. */
432 mapping->nrexceptional++;
433 spin_unlock_irq(&mapping->tree_lock);
e3ad61c6 434 return entry;
ac401cc7 435 }
e3ad61c6 436 entry = lock_slot(mapping, slot);
642261ac 437 out_unlock:
ac401cc7 438 spin_unlock_irq(&mapping->tree_lock);
e3ad61c6 439 return entry;
ac401cc7
JK
440}
441
c6dcf52c
JK
442static int __dax_invalidate_mapping_entry(struct address_space *mapping,
443 pgoff_t index, bool trunc)
444{
445 int ret = 0;
446 void *entry;
447 struct radix_tree_root *page_tree = &mapping->page_tree;
448
449 spin_lock_irq(&mapping->tree_lock);
450 entry = get_unlocked_mapping_entry(mapping, index, NULL);
91d25ba8 451 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
c6dcf52c
JK
452 goto out;
453 if (!trunc &&
454 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
455 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
456 goto out;
457 radix_tree_delete(page_tree, index);
458 mapping->nrexceptional--;
459 ret = 1;
460out:
461 put_unlocked_mapping_entry(mapping, index, entry);
462 spin_unlock_irq(&mapping->tree_lock);
463 return ret;
464}
ac401cc7
JK
465/*
466 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
467 * entry to get unlocked before deleting it.
468 */
469int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
470{
c6dcf52c 471 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
ac401cc7 472
ac401cc7
JK
473 /*
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...
479 */
c6dcf52c
JK
480 WARN_ON_ONCE(!ret);
481 return ret;
482}
483
c6dcf52c
JK
484/*
485 * Invalidate exceptional DAX entry if it is clean.
486 */
487int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
488 pgoff_t index)
489{
490 return __dax_invalidate_mapping_entry(mapping, index, false);
ac401cc7
JK
491}
492
cccbce67
DW
493static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
494 sector_t sector, size_t size, struct page *to,
495 unsigned long vaddr)
f7ca90b1 496{
cccbce67
DW
497 void *vto, *kaddr;
498 pgoff_t pgoff;
499 pfn_t pfn;
500 long rc;
501 int id;
502
503 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
504 if (rc)
505 return rc;
506
507 id = dax_read_lock();
508 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
509 if (rc < 0) {
510 dax_read_unlock(id);
511 return rc;
512 }
f7ca90b1 513 vto = kmap_atomic(to);
cccbce67 514 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
f7ca90b1 515 kunmap_atomic(vto);
cccbce67 516 dax_read_unlock(id);
f7ca90b1
MW
517 return 0;
518}
519
642261ac
RZ
520/*
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
525 * appropriate.
526 */
ac401cc7
JK
527static void *dax_insert_mapping_entry(struct address_space *mapping,
528 struct vm_fault *vmf,
642261ac 529 void *entry, sector_t sector,
f5b7b748 530 unsigned long flags, bool dirty)
9973c98e
RZ
531{
532 struct radix_tree_root *page_tree = &mapping->page_tree;
ac401cc7
JK
533 void *new_entry;
534 pgoff_t index = vmf->pgoff;
9973c98e 535
f5b7b748 536 if (dirty)
d2b2a28e 537 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
9973c98e 538
91d25ba8
RZ
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))
977fbdcd
MW
542 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
543 PG_PMD_NR, false);
91d25ba8 544 else /* pte entry */
977fbdcd 545 unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
9973c98e
RZ
546 }
547
ac401cc7 548 spin_lock_irq(&mapping->tree_lock);
642261ac
RZ
549 new_entry = dax_radix_locked_entry(sector, flags);
550
91d25ba8 551 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
642261ac
RZ
552 /*
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.
559 */
f7942430 560 struct radix_tree_node *node;
ac401cc7
JK
561 void **slot;
562 void *ret;
9973c98e 563
f7942430 564 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
ac401cc7 565 WARN_ON_ONCE(ret != entry);
4d693d08 566 __radix_tree_replace(page_tree, node, slot,
c7df8ad2 567 new_entry, NULL);
91d25ba8 568 entry = new_entry;
9973c98e 569 }
91d25ba8 570
f5b7b748 571 if (dirty)
9973c98e 572 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
91d25ba8 573
9973c98e 574 spin_unlock_irq(&mapping->tree_lock);
91d25ba8 575 return entry;
9973c98e
RZ
576}
577
4b4bb46d
JK
578static inline unsigned long
579pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
580{
581 unsigned long address;
582
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);
585 return address;
586}
587
588/* Walk all mappings of a given index of a file and writeprotect them */
589static void dax_mapping_entry_mkclean(struct address_space *mapping,
590 pgoff_t index, unsigned long pfn)
591{
592 struct vm_area_struct *vma;
f729c8c9
RZ
593 pte_t pte, *ptep = NULL;
594 pmd_t *pmdp = NULL;
4b4bb46d 595 spinlock_t *ptl;
4b4bb46d
JK
596
597 i_mmap_lock_read(mapping);
598 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
a4d1a885 599 unsigned long address, start, end;
4b4bb46d
JK
600
601 cond_resched();
602
603 if (!(vma->vm_flags & VM_SHARED))
604 continue;
605
606 address = pgoff_address(index, vma);
a4d1a885
JG
607
608 /*
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.
612 */
613 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
4b4bb46d 614 continue;
4b4bb46d 615
0f10851e
JG
616 /*
617 * No need to call mmu_notifier_invalidate_range() as we are
618 * downgrading page table protection not changing it to point
619 * to a new page.
620 *
621 * See Documentation/vm/mmu_notifier.txt
622 */
f729c8c9
RZ
623 if (pmdp) {
624#ifdef CONFIG_FS_DAX_PMD
625 pmd_t pmd;
626
627 if (pfn != pmd_pfn(*pmdp))
628 goto unlock_pmd;
f6f37321 629 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
f729c8c9
RZ
630 goto unlock_pmd;
631
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);
f729c8c9 637unlock_pmd:
f729c8c9 638#endif
ee190ca6 639 spin_unlock(ptl);
f729c8c9
RZ
640 } else {
641 if (pfn != pte_pfn(*ptep))
642 goto unlock_pte;
643 if (!pte_dirty(*ptep) && !pte_write(*ptep))
644 goto unlock_pte;
645
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);
f729c8c9
RZ
651unlock_pte:
652 pte_unmap_unlock(ptep, ptl);
653 }
4b4bb46d 654
a4d1a885 655 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
4b4bb46d
JK
656 }
657 i_mmap_unlock_read(mapping);
658}
659
9973c98e 660static int dax_writeback_one(struct block_device *bdev,
cccbce67
DW
661 struct dax_device *dax_dev, struct address_space *mapping,
662 pgoff_t index, void *entry)
9973c98e
RZ
663{
664 struct radix_tree_root *page_tree = &mapping->page_tree;
cccbce67
DW
665 void *entry2, **slot, *kaddr;
666 long ret = 0, id;
667 sector_t sector;
668 pgoff_t pgoff;
669 size_t size;
670 pfn_t pfn;
9973c98e 671
9973c98e 672 /*
a6abc2c0
JK
673 * A page got tagged dirty in DAX mapping? Something is seriously
674 * wrong.
9973c98e 675 */
a6abc2c0
JK
676 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
677 return -EIO;
9973c98e 678
a6abc2c0
JK
679 spin_lock_irq(&mapping->tree_lock);
680 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
681 /* Entry got punched out / reallocated? */
91d25ba8 682 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
a6abc2c0
JK
683 goto put_unlocked;
684 /*
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
687 * or entry type.
688 */
689 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
690 goto put_unlocked;
642261ac
RZ
691 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
692 dax_is_zero_entry(entry))) {
9973c98e 693 ret = -EIO;
a6abc2c0 694 goto put_unlocked;
9973c98e
RZ
695 }
696
a6abc2c0
JK
697 /* Another fsync thread may have already written back this entry */
698 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
699 goto put_unlocked;
700 /* Lock the entry to serialize with page faults */
701 entry = lock_slot(mapping, slot);
702 /*
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.
708 */
709 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
710 spin_unlock_irq(&mapping->tree_lock);
711
642261ac
RZ
712 /*
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.
718 */
cccbce67
DW
719 sector = dax_radix_sector(entry);
720 size = PAGE_SIZE << dax_radix_order(entry);
721
722 id = dax_read_lock();
723 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
724 if (ret)
725 goto dax_unlock;
9973c98e
RZ
726
727 /*
cccbce67
DW
728 * dax_direct_access() may sleep, so cannot hold tree_lock over
729 * its invocation.
9973c98e 730 */
cccbce67
DW
731 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
732 if (ret < 0)
733 goto dax_unlock;
9973c98e 734
cccbce67 735 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
9973c98e 736 ret = -EIO;
cccbce67 737 goto dax_unlock;
9973c98e
RZ
738 }
739
cccbce67 740 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
c3ca015f 741 dax_flush(dax_dev, kaddr, size);
4b4bb46d
JK
742 /*
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
746 * entry lock.
747 */
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);
f9bc3a07 751 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
cccbce67
DW
752 dax_unlock:
753 dax_read_unlock(id);
91d25ba8 754 put_locked_mapping_entry(mapping, index);
9973c98e
RZ
755 return ret;
756
a6abc2c0
JK
757 put_unlocked:
758 put_unlocked_mapping_entry(mapping, index, entry2);
9973c98e
RZ
759 spin_unlock_irq(&mapping->tree_lock);
760 return ret;
761}
762
763/*
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.
767 */
7f6d5b52
RZ
768int dax_writeback_mapping_range(struct address_space *mapping,
769 struct block_device *bdev, struct writeback_control *wbc)
9973c98e
RZ
770{
771 struct inode *inode = mapping->host;
642261ac 772 pgoff_t start_index, end_index;
9973c98e 773 pgoff_t indices[PAGEVEC_SIZE];
cccbce67 774 struct dax_device *dax_dev;
9973c98e
RZ
775 struct pagevec pvec;
776 bool done = false;
777 int i, ret = 0;
9973c98e
RZ
778
779 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
780 return -EIO;
781
7f6d5b52
RZ
782 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
783 return 0;
784
cccbce67
DW
785 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
786 if (!dax_dev)
787 return -EIO;
788
09cbfeaf
KS
789 start_index = wbc->range_start >> PAGE_SHIFT;
790 end_index = wbc->range_end >> PAGE_SHIFT;
9973c98e 791
d14a3f48
RZ
792 trace_dax_writeback_range(inode, start_index, end_index);
793
9973c98e
RZ
794 tag_pages_for_writeback(mapping, start_index, end_index);
795
86679820 796 pagevec_init(&pvec);
9973c98e
RZ
797 while (!done) {
798 pvec.nr = find_get_entries_tag(mapping, start_index,
799 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
800 pvec.pages, indices);
801
802 if (pvec.nr == 0)
803 break;
804
805 for (i = 0; i < pvec.nr; i++) {
806 if (indices[i] > end_index) {
807 done = true;
808 break;
809 }
810
cccbce67
DW
811 ret = dax_writeback_one(bdev, dax_dev, mapping,
812 indices[i], pvec.pages[i]);
819ec6b9
JL
813 if (ret < 0) {
814 mapping_set_error(mapping, ret);
d14a3f48 815 goto out;
819ec6b9 816 }
9973c98e 817 }
1eb643d0 818 start_index = indices[pvec.nr - 1] + 1;
9973c98e 819 }
d14a3f48 820out:
cccbce67 821 put_dax(dax_dev);
d14a3f48
RZ
822 trace_dax_writeback_range_done(inode, start_index, end_index);
823 return (ret < 0 ? ret : 0);
9973c98e
RZ
824}
825EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
826
31a6f1a6 827static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
f7ca90b1 828{
a3841f94 829 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
31a6f1a6
JK
830}
831
5e161e40
JK
832static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
833 pfn_t *pfnp)
f7ca90b1 834{
31a6f1a6 835 const sector_t sector = dax_iomap_sector(iomap, pos);
cccbce67 836 pgoff_t pgoff;
5e161e40 837 void *kaddr;
cccbce67 838 int id, rc;
5e161e40 839 long length;
f7ca90b1 840
5e161e40 841 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
cccbce67
DW
842 if (rc)
843 return rc;
cccbce67 844 id = dax_read_lock();
5e161e40
JK
845 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
846 &kaddr, pfnp);
847 if (length < 0) {
848 rc = length;
849 goto out;
cccbce67 850 }
5e161e40
JK
851 rc = -EINVAL;
852 if (PFN_PHYS(length) < size)
853 goto out;
854 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
855 goto out;
856 /* For larger pages we need devmap */
857 if (length > 1 && !pfn_t_devmap(*pfnp))
858 goto out;
859 rc = 0;
860out:
cccbce67 861 dax_read_unlock(id);
5e161e40 862 return rc;
0e3b210c 863}
0e3b210c 864
e30331ff 865/*
91d25ba8
RZ
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.
e30331ff 871 */
91d25ba8 872static int dax_load_hole(struct address_space *mapping, void *entry,
e30331ff
RZ
873 struct vm_fault *vmf)
874{
875 struct inode *inode = mapping->host;
91d25ba8
RZ
876 unsigned long vaddr = vmf->address;
877 int ret = VM_FAULT_NOPAGE;
878 struct page *zero_page;
879 void *entry2;
e30331ff 880
91d25ba8
RZ
881 zero_page = ZERO_PAGE(0);
882 if (unlikely(!zero_page)) {
e30331ff
RZ
883 ret = VM_FAULT_OOM;
884 goto out;
885 }
886
91d25ba8 887 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
f5b7b748 888 RADIX_DAX_ZERO_PAGE, false);
91d25ba8
RZ
889 if (IS_ERR(entry2)) {
890 ret = VM_FAULT_SIGBUS;
891 goto out;
e30331ff 892 }
91d25ba8
RZ
893
894 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
e30331ff
RZ
895out:
896 trace_dax_load_hole(inode, vmf, ret);
897 return ret;
898}
899
4b0228fa
VV
900static bool dax_range_is_aligned(struct block_device *bdev,
901 unsigned int offset, unsigned int length)
902{
903 unsigned short sector_size = bdev_logical_block_size(bdev);
904
905 if (!IS_ALIGNED(offset, sector_size))
906 return false;
907 if (!IS_ALIGNED(length, sector_size))
908 return false;
909
910 return true;
911}
912
cccbce67
DW
913int __dax_zero_page_range(struct block_device *bdev,
914 struct dax_device *dax_dev, sector_t sector,
915 unsigned int offset, unsigned int size)
679c8bd3 916{
cccbce67
DW
917 if (dax_range_is_aligned(bdev, offset, size)) {
918 sector_t start_sector = sector + (offset >> 9);
4b0228fa
VV
919
920 return blkdev_issue_zeroout(bdev, start_sector,
53ef7d0e 921 size >> 9, GFP_NOFS, 0);
4b0228fa 922 } else {
cccbce67
DW
923 pgoff_t pgoff;
924 long rc, id;
925 void *kaddr;
926 pfn_t pfn;
927
e84b83b9 928 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
cccbce67
DW
929 if (rc)
930 return rc;
931
932 id = dax_read_lock();
e84b83b9 933 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
cccbce67
DW
934 &pfn);
935 if (rc < 0) {
936 dax_read_unlock(id);
937 return rc;
938 }
81f55870 939 memset(kaddr + offset, 0, size);
c3ca015f 940 dax_flush(dax_dev, kaddr + offset, size);
cccbce67 941 dax_read_unlock(id);
4b0228fa 942 }
679c8bd3
CH
943 return 0;
944}
945EXPORT_SYMBOL_GPL(__dax_zero_page_range);
946
a254e568 947static loff_t
11c59c92 948dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
a254e568
CH
949 struct iomap *iomap)
950{
cccbce67
DW
951 struct block_device *bdev = iomap->bdev;
952 struct dax_device *dax_dev = iomap->dax_dev;
a254e568
CH
953 struct iov_iter *iter = data;
954 loff_t end = pos + length, done = 0;
955 ssize_t ret = 0;
cccbce67 956 int id;
a254e568
CH
957
958 if (iov_iter_rw(iter) == READ) {
959 end = min(end, i_size_read(inode));
960 if (pos >= end)
961 return 0;
962
963 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
964 return iov_iter_zero(min(length, end - pos), iter);
965 }
966
967 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
968 return -EIO;
969
e3fce68c
JK
970 /*
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.
974 */
cd656375 975 if (iomap->flags & IOMAP_F_NEW) {
e3fce68c
JK
976 invalidate_inode_pages2_range(inode->i_mapping,
977 pos >> PAGE_SHIFT,
978 (end - 1) >> PAGE_SHIFT);
979 }
980
cccbce67 981 id = dax_read_lock();
a254e568
CH
982 while (pos < end) {
983 unsigned offset = pos & (PAGE_SIZE - 1);
cccbce67
DW
984 const size_t size = ALIGN(length + offset, PAGE_SIZE);
985 const sector_t sector = dax_iomap_sector(iomap, pos);
a254e568 986 ssize_t map_len;
cccbce67
DW
987 pgoff_t pgoff;
988 void *kaddr;
989 pfn_t pfn;
a254e568 990
d1908f52
MH
991 if (fatal_signal_pending(current)) {
992 ret = -EINTR;
993 break;
994 }
995
cccbce67
DW
996 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
997 if (ret)
998 break;
999
1000 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
1001 &kaddr, &pfn);
a254e568
CH
1002 if (map_len < 0) {
1003 ret = map_len;
1004 break;
1005 }
1006
cccbce67
DW
1007 map_len = PFN_PHYS(map_len);
1008 kaddr += offset;
a254e568
CH
1009 map_len -= offset;
1010 if (map_len > end - pos)
1011 map_len = end - pos;
1012
a2e050f5
RZ
1013 /*
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.
1017 */
a254e568 1018 if (iov_iter_rw(iter) == WRITE)
fec53774
DW
1019 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1020 map_len, iter);
a254e568 1021 else
cccbce67 1022 map_len = copy_to_iter(kaddr, map_len, iter);
a254e568
CH
1023 if (map_len <= 0) {
1024 ret = map_len ? map_len : -EFAULT;
1025 break;
1026 }
1027
1028 pos += map_len;
1029 length -= map_len;
1030 done += map_len;
1031 }
cccbce67 1032 dax_read_unlock(id);
a254e568
CH
1033
1034 return done ? done : ret;
1035}
1036
1037/**
11c59c92 1038 * dax_iomap_rw - Perform I/O to a DAX file
a254e568
CH
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
1042 *
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.
1046 */
1047ssize_t
11c59c92 1048dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
8ff6daa1 1049 const struct iomap_ops *ops)
a254e568
CH
1050{
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;
1054 unsigned flags = 0;
1055
168316db
CH
1056 if (iov_iter_rw(iter) == WRITE) {
1057 lockdep_assert_held_exclusive(&inode->i_rwsem);
a254e568 1058 flags |= IOMAP_WRITE;
168316db
CH
1059 } else {
1060 lockdep_assert_held(&inode->i_rwsem);
1061 }
a254e568 1062
a254e568
CH
1063 while (iov_iter_count(iter)) {
1064 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
11c59c92 1065 iter, dax_iomap_actor);
a254e568
CH
1066 if (ret <= 0)
1067 break;
1068 pos += ret;
1069 done += ret;
1070 }
1071
1072 iocb->ki_pos += done;
1073 return done ? done : ret;
1074}
11c59c92 1075EXPORT_SYMBOL_GPL(dax_iomap_rw);
a7d73fe6 1076
9f141d6e
JK
1077static int dax_fault_return(int error)
1078{
1079 if (error == 0)
1080 return VM_FAULT_NOPAGE;
1081 if (error == -ENOMEM)
1082 return VM_FAULT_OOM;
1083 return VM_FAULT_SIGBUS;
1084}
1085
aaa422c4
DW
1086/*
1087 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1088 * flushed on write-faults (non-cow), but not read-faults.
1089 */
1090static bool dax_fault_is_synchronous(unsigned long flags,
1091 struct vm_area_struct *vma, struct iomap *iomap)
1092{
1093 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1094 && (iomap->flags & IOMAP_F_DIRTY);
1095}
1096
9a0dd422 1097static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
c0b24625 1098 int *iomap_errp, const struct iomap_ops *ops)
a7d73fe6 1099{
a0987ad5
JK
1100 struct vm_area_struct *vma = vmf->vma;
1101 struct address_space *mapping = vma->vm_file->f_mapping;
a7d73fe6 1102 struct inode *inode = mapping->host;
1a29d85e 1103 unsigned long vaddr = vmf->address;
a7d73fe6 1104 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
a7d73fe6 1105 struct iomap iomap = { 0 };
9484ab1b 1106 unsigned flags = IOMAP_FAULT;
a7d73fe6 1107 int error, major = 0;
d2c43ef1 1108 bool write = vmf->flags & FAULT_FLAG_WRITE;
caa51d26 1109 bool sync;
b1aa812b 1110 int vmf_ret = 0;
a7d73fe6 1111 void *entry;
1b5a1cb2 1112 pfn_t pfn;
a7d73fe6 1113
a9c42b33 1114 trace_dax_pte_fault(inode, vmf, vmf_ret);
a7d73fe6
CH
1115 /*
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
1118 * a reliable test.
1119 */
a9c42b33
RZ
1120 if (pos >= i_size_read(inode)) {
1121 vmf_ret = VM_FAULT_SIGBUS;
1122 goto out;
1123 }
a7d73fe6 1124
d2c43ef1 1125 if (write && !vmf->cow_page)
a7d73fe6
CH
1126 flags |= IOMAP_WRITE;
1127
13e451fd
JK
1128 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1129 if (IS_ERR(entry)) {
1130 vmf_ret = dax_fault_return(PTR_ERR(entry));
1131 goto out;
1132 }
1133
e2093926
RZ
1134 /*
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
1138 * retried.
1139 */
1140 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1141 vmf_ret = VM_FAULT_NOPAGE;
1142 goto unlock_entry;
1143 }
1144
a7d73fe6
CH
1145 /*
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.
1149 */
1150 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
c0b24625
JK
1151 if (iomap_errp)
1152 *iomap_errp = error;
a9c42b33
RZ
1153 if (error) {
1154 vmf_ret = dax_fault_return(error);
13e451fd 1155 goto unlock_entry;
a9c42b33 1156 }
a7d73fe6 1157 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
13e451fd
JK
1158 error = -EIO; /* fs corruption? */
1159 goto error_finish_iomap;
a7d73fe6
CH
1160 }
1161
a7d73fe6 1162 if (vmf->cow_page) {
31a6f1a6
JK
1163 sector_t sector = dax_iomap_sector(&iomap, pos);
1164
a7d73fe6
CH
1165 switch (iomap.type) {
1166 case IOMAP_HOLE:
1167 case IOMAP_UNWRITTEN:
1168 clear_user_highpage(vmf->cow_page, vaddr);
1169 break;
1170 case IOMAP_MAPPED:
cccbce67
DW
1171 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1172 sector, PAGE_SIZE, vmf->cow_page, vaddr);
a7d73fe6
CH
1173 break;
1174 default:
1175 WARN_ON_ONCE(1);
1176 error = -EIO;
1177 break;
1178 }
1179
1180 if (error)
13e451fd 1181 goto error_finish_iomap;
b1aa812b
JK
1182
1183 __SetPageUptodate(vmf->cow_page);
1184 vmf_ret = finish_fault(vmf);
1185 if (!vmf_ret)
1186 vmf_ret = VM_FAULT_DONE_COW;
13e451fd 1187 goto finish_iomap;
a7d73fe6
CH
1188 }
1189
aaa422c4 1190 sync = dax_fault_is_synchronous(flags, vma, &iomap);
caa51d26 1191
a7d73fe6
CH
1192 switch (iomap.type) {
1193 case IOMAP_MAPPED:
1194 if (iomap.flags & IOMAP_F_NEW) {
1195 count_vm_event(PGMAJFAULT);
a0987ad5 1196 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
a7d73fe6
CH
1197 major = VM_FAULT_MAJOR;
1198 }
1b5a1cb2
JK
1199 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1200 if (error < 0)
1201 goto error_finish_iomap;
1202
1203 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1204 dax_iomap_sector(&iomap, pos),
caa51d26 1205 0, write && !sync);
1b5a1cb2
JK
1206 if (IS_ERR(entry)) {
1207 error = PTR_ERR(entry);
1208 goto error_finish_iomap;
1209 }
1210
caa51d26
JK
1211 /*
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.
1216 */
1217 if (sync) {
1218 if (WARN_ON_ONCE(!pfnp)) {
1219 error = -EIO;
1220 goto error_finish_iomap;
1221 }
1222 *pfnp = pfn;
1223 vmf_ret = VM_FAULT_NEEDDSYNC | major;
1224 goto finish_iomap;
1225 }
1b5a1cb2
JK
1226 trace_dax_insert_mapping(inode, vmf, entry);
1227 if (write)
1228 error = vm_insert_mixed_mkwrite(vma, vaddr, pfn);
1229 else
1230 error = vm_insert_mixed(vma, vaddr, pfn);
1231
9f141d6e
JK
1232 /* -EBUSY is fine, somebody else faulted on the same PTE */
1233 if (error == -EBUSY)
1234 error = 0;
a7d73fe6
CH
1235 break;
1236 case IOMAP_UNWRITTEN:
1237 case IOMAP_HOLE:
d2c43ef1 1238 if (!write) {
91d25ba8 1239 vmf_ret = dax_load_hole(mapping, entry, vmf);
13e451fd 1240 goto finish_iomap;
1550290b 1241 }
a7d73fe6
CH
1242 /*FALLTHRU*/
1243 default:
1244 WARN_ON_ONCE(1);
1245 error = -EIO;
1246 break;
1247 }
1248
13e451fd 1249 error_finish_iomap:
9f141d6e 1250 vmf_ret = dax_fault_return(error) | major;
9f141d6e
JK
1251 finish_iomap:
1252 if (ops->iomap_end) {
1253 int copied = PAGE_SIZE;
1254
1255 if (vmf_ret & VM_FAULT_ERROR)
1256 copied = 0;
1257 /*
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
1261 * with it.
1262 */
1263 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
1550290b 1264 }
13e451fd 1265 unlock_entry:
91d25ba8 1266 put_locked_mapping_entry(mapping, vmf->pgoff);
13e451fd 1267 out:
a9c42b33 1268 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
9f141d6e 1269 return vmf_ret;
a7d73fe6 1270}
642261ac
RZ
1271
1272#ifdef CONFIG_FS_DAX_PMD
f4200391 1273static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
91d25ba8 1274 void *entry)
642261ac 1275{
f4200391
DJ
1276 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1277 unsigned long pmd_addr = vmf->address & PMD_MASK;
653b2ea3 1278 struct inode *inode = mapping->host;
642261ac 1279 struct page *zero_page;
653b2ea3 1280 void *ret = NULL;
642261ac
RZ
1281 spinlock_t *ptl;
1282 pmd_t pmd_entry;
642261ac 1283
f4200391 1284 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
642261ac
RZ
1285
1286 if (unlikely(!zero_page))
653b2ea3 1287 goto fallback;
642261ac 1288
91d25ba8 1289 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
f5b7b748 1290 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false);
642261ac 1291 if (IS_ERR(ret))
653b2ea3 1292 goto fallback;
642261ac 1293
f4200391
DJ
1294 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1295 if (!pmd_none(*(vmf->pmd))) {
642261ac 1296 spin_unlock(ptl);
653b2ea3 1297 goto fallback;
642261ac
RZ
1298 }
1299
f4200391 1300 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
642261ac 1301 pmd_entry = pmd_mkhuge(pmd_entry);
f4200391 1302 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
642261ac 1303 spin_unlock(ptl);
f4200391 1304 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
642261ac 1305 return VM_FAULT_NOPAGE;
653b2ea3
RZ
1306
1307fallback:
f4200391 1308 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
653b2ea3 1309 return VM_FAULT_FALLBACK;
642261ac
RZ
1310}
1311
9a0dd422 1312static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
a2d58167 1313 const struct iomap_ops *ops)
642261ac 1314{
f4200391 1315 struct vm_area_struct *vma = vmf->vma;
642261ac 1316 struct address_space *mapping = vma->vm_file->f_mapping;
d8a849e1
DJ
1317 unsigned long pmd_addr = vmf->address & PMD_MASK;
1318 bool write = vmf->flags & FAULT_FLAG_WRITE;
caa51d26 1319 bool sync;
9484ab1b 1320 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
642261ac
RZ
1321 struct inode *inode = mapping->host;
1322 int result = VM_FAULT_FALLBACK;
1323 struct iomap iomap = { 0 };
1324 pgoff_t max_pgoff, pgoff;
642261ac
RZ
1325 void *entry;
1326 loff_t pos;
1327 int error;
302a5e31 1328 pfn_t pfn;
642261ac 1329
282a8e03
RZ
1330 /*
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.
1334 */
1335 pgoff = linear_page_index(vma, pmd_addr);
957ac8c4 1336 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
282a8e03 1337
f4200391 1338 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
282a8e03 1339
fffa281b
RZ
1340 /*
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.
1345 */
1346 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1347 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1348 goto fallback;
1349
642261ac
RZ
1350 /* Fall back to PTEs if we're going to COW */
1351 if (write && !(vma->vm_flags & VM_SHARED))
1352 goto fallback;
1353
1354 /* If the PMD would extend outside the VMA */
1355 if (pmd_addr < vma->vm_start)
1356 goto fallback;
1357 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1358 goto fallback;
1359
957ac8c4 1360 if (pgoff >= max_pgoff) {
282a8e03
RZ
1361 result = VM_FAULT_SIGBUS;
1362 goto out;
1363 }
642261ac
RZ
1364
1365 /* If the PMD would extend beyond the file size */
957ac8c4 1366 if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
642261ac
RZ
1367 goto fallback;
1368
876f2946 1369 /*
91d25ba8
RZ
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.
876f2946
RZ
1374 */
1375 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1376 if (IS_ERR(entry))
1377 goto fallback;
1378
e2093926
RZ
1379 /*
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
1383 * retried.
1384 */
1385 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1386 !pmd_devmap(*vmf->pmd)) {
1387 result = 0;
1388 goto unlock_entry;
1389 }
1390
642261ac
RZ
1391 /*
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.
1395 */
1396 pos = (loff_t)pgoff << PAGE_SHIFT;
1397 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1398 if (error)
876f2946 1399 goto unlock_entry;
9f141d6e 1400
642261ac
RZ
1401 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1402 goto finish_iomap;
1403
aaa422c4 1404 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
caa51d26 1405
642261ac
RZ
1406 switch (iomap.type) {
1407 case IOMAP_MAPPED:
302a5e31
JK
1408 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1409 if (error < 0)
1410 goto finish_iomap;
1411
1412 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1413 dax_iomap_sector(&iomap, pos),
caa51d26 1414 RADIX_DAX_PMD, write && !sync);
302a5e31
JK
1415 if (IS_ERR(entry))
1416 goto finish_iomap;
1417
caa51d26
JK
1418 /*
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.
1423 */
1424 if (sync) {
1425 if (WARN_ON_ONCE(!pfnp))
1426 goto finish_iomap;
1427 *pfnp = pfn;
1428 result = VM_FAULT_NEEDDSYNC;
1429 goto finish_iomap;
1430 }
1431
302a5e31
JK
1432 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1433 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1434 write);
642261ac
RZ
1435 break;
1436 case IOMAP_UNWRITTEN:
1437 case IOMAP_HOLE:
1438 if (WARN_ON_ONCE(write))
876f2946 1439 break;
91d25ba8 1440 result = dax_pmd_load_hole(vmf, &iomap, entry);
642261ac
RZ
1441 break;
1442 default:
1443 WARN_ON_ONCE(1);
1444 break;
1445 }
1446
1447 finish_iomap:
1448 if (ops->iomap_end) {
9f141d6e
JK
1449 int copied = PMD_SIZE;
1450
1451 if (result == VM_FAULT_FALLBACK)
1452 copied = 0;
1453 /*
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
1457 * with it.
1458 */
1459 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1460 &iomap);
642261ac 1461 }
876f2946 1462 unlock_entry:
91d25ba8 1463 put_locked_mapping_entry(mapping, pgoff);
642261ac
RZ
1464 fallback:
1465 if (result == VM_FAULT_FALLBACK) {
d8a849e1 1466 split_huge_pmd(vma, vmf->pmd, vmf->address);
642261ac
RZ
1467 count_vm_event(THP_FAULT_FALLBACK);
1468 }
282a8e03 1469out:
f4200391 1470 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
642261ac
RZ
1471 return result;
1472}
a2d58167 1473#else
9a0dd422 1474static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
01cddfe9 1475 const struct iomap_ops *ops)
a2d58167
DJ
1476{
1477 return VM_FAULT_FALLBACK;
1478}
642261ac 1479#endif /* CONFIG_FS_DAX_PMD */
a2d58167
DJ
1480
1481/**
1482 * dax_iomap_fault - handle a page fault on a DAX file
1483 * @vmf: The description of the fault
cec04e8c 1484 * @pe_size: Size of the page to fault in
9a0dd422 1485 * @pfnp: PFN to insert for synchronous faults if fsync is required
c0b24625 1486 * @iomap_errp: Storage for detailed error code in case of error
cec04e8c 1487 * @ops: Iomap ops passed from the file system
a2d58167
DJ
1488 *
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
1492 * successfully.
1493 */
c791ace1 1494int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
c0b24625 1495 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
a2d58167 1496{
c791ace1
DJ
1497 switch (pe_size) {
1498 case PE_SIZE_PTE:
c0b24625 1499 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
c791ace1 1500 case PE_SIZE_PMD:
9a0dd422 1501 return dax_iomap_pmd_fault(vmf, pfnp, ops);
a2d58167
DJ
1502 default:
1503 return VM_FAULT_FALLBACK;
1504 }
1505}
1506EXPORT_SYMBOL_GPL(dax_iomap_fault);
71eab6df
JK
1507
1508/**
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
1513 *
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
1516 * as well.
1517 */
1518static int dax_insert_pfn_mkwrite(struct vm_fault *vmf,
1519 enum page_entry_size pe_size,
1520 pfn_t pfn)
1521{
1522 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1523 void *entry, **slot;
1524 pgoff_t index = vmf->pgoff;
1525 int vmf_ret, error;
1526
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? */
1530 if (!entry ||
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,
1536 VM_FAULT_NOPAGE);
1537 return VM_FAULT_NOPAGE;
1538 }
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);
1542 switch (pe_size) {
1543 case PE_SIZE_PTE:
1544 error = vm_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
1545 vmf_ret = dax_fault_return(error);
1546 break;
1547#ifdef CONFIG_FS_DAX_PMD
1548 case PE_SIZE_PMD:
1549 vmf_ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
1550 pfn, true);
1551 break;
1552#endif
1553 default:
1554 vmf_ret = VM_FAULT_FALLBACK;
1555 }
1556 put_locked_mapping_entry(mapping, index);
1557 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, vmf_ret);
1558 return vmf_ret;
1559}
1560
1561/**
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
1566 *
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
1569 * table entry.
1570 */
1571int dax_finish_sync_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1572 pfn_t pfn)
1573{
1574 int err;
1575 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1576 size_t len = 0;
1577
1578 if (pe_size == PE_SIZE_PTE)
1579 len = PAGE_SIZE;
1580 else if (pe_size == PE_SIZE_PMD)
1581 len = PMD_SIZE;
1582 else
1583 WARN_ON_ONCE(1);
1584 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1585 if (err)
1586 return VM_FAULT_SIGBUS;
1587 return dax_insert_pfn_mkwrite(vmf, pe_size, pfn);
1588}
1589EXPORT_SYMBOL_GPL(dax_finish_sync_fault);